|
| 1 | +/** |
| 2 | + * \file mpls.hpp |
| 3 | + * \brief Plugin for parsing mpls traffic. |
| 4 | + * \author Jakub Antonín Štigler [email protected] |
| 5 | + * \date 2023 |
| 6 | + */ |
| 7 | +/* |
| 8 | + * Copyright (C) 2023 CESNET |
| 9 | + * |
| 10 | + * LICENSE TERMS |
| 11 | + * |
| 12 | + * Redistribution and use in source and binary forms, with or without |
| 13 | + * modification, are permitted provided that the following conditions |
| 14 | + * are met: |
| 15 | + * 1. Redistributions of source code must retain the above copyright |
| 16 | + * notice, this list of conditions and the following disclaimer. |
| 17 | + * 2. Redistributions in binary form must reproduce the above copyright |
| 18 | + * notice, this list of conditions and the following disclaimer in |
| 19 | + * the documentation and/or other materials provided with the |
| 20 | + * distribution. |
| 21 | + * 3. Neither the name of the Company nor the names of its contributors |
| 22 | + * may be used to endorse or promote products derived from this |
| 23 | + * software without specific prior written permission. |
| 24 | + * |
| 25 | + * |
| 26 | + * |
| 27 | + */ |
| 28 | + |
| 29 | +#ifndef IPXP_PROCESS_MPLS_HPP |
| 30 | +#define IPXP_PROCESS_MPLS_HPP |
| 31 | + |
| 32 | +#include <cstring> |
| 33 | +#include <string> |
| 34 | +#include <sstream> |
| 35 | + |
| 36 | +#ifdef WITH_NEMEA |
| 37 | + #include "fields.h" |
| 38 | +#endif |
| 39 | + |
| 40 | +#include <ipfixprobe/process.hpp> |
| 41 | +#include <ipfixprobe/flowifc.hpp> |
| 42 | +#include <ipfixprobe/packet.hpp> |
| 43 | +#include <ipfixprobe/ipfix-elements.hpp> |
| 44 | + |
| 45 | +namespace ipxp { |
| 46 | + |
| 47 | +#define MPLS_LABEL_SECTION_LENGTH 3 |
| 48 | + |
| 49 | +#define MPLS_UNIREC_TEMPLATE "MPLS_TOP_LABEL_STACK_SECTION" |
| 50 | + |
| 51 | +UR_FIELDS ( |
| 52 | + bytes MPLS_TOP_LABEL_STACK_SECTION |
| 53 | +) |
| 54 | + |
| 55 | +/** |
| 56 | + * \brief Flow record extension header for storing parsed MPLS data. |
| 57 | + */ |
| 58 | +struct RecordExtMPLS : public RecordExt { |
| 59 | + static int REGISTERED_ID; |
| 60 | + |
| 61 | + // Contents are (from MSb to LSb): |
| 62 | + // 20-bit - Label, |
| 63 | + // 3-bit - Traffic class / EXP, |
| 64 | + // 1-bit - Bottom of stack (true if last), |
| 65 | + // 8-bit - TTL (Time To Live) |
| 66 | + uint32_t mpls; |
| 67 | + |
| 68 | + RecordExtMPLS() : RecordExt(REGISTERED_ID), mpls(0) { } |
| 69 | + |
| 70 | +#ifdef WITH_NEMEA |
| 71 | + void fill_unirec(ur_template_t *tmplt, void *record) override |
| 72 | + { |
| 73 | + auto v = htonl(mpls); |
| 74 | + auto arr = reinterpret_cast<uint8_t *>(&v); |
| 75 | + ur_set_var(tmplt, record, F_MPLS_TOP_LABEL_STACK_SECTION, arr, MPLS_LABEL_SECTION_LENGTH); |
| 76 | + } |
| 77 | + |
| 78 | + const char *get_unirec_tmplt() const |
| 79 | + { |
| 80 | + return MPLS_UNIREC_TEMPLATE; |
| 81 | + } |
| 82 | +#endif |
| 83 | + |
| 84 | + int fill_ipfix(uint8_t *buffer, int size) override |
| 85 | + { |
| 86 | + if (size < MPLS_LABEL_SECTION_LENGTH + 1) { |
| 87 | + return -1; |
| 88 | + } |
| 89 | + |
| 90 | + buffer[0] = MPLS_LABEL_SECTION_LENGTH; |
| 91 | + auto v = htonl(mpls); |
| 92 | + auto arr = reinterpret_cast<uint8_t *>(&v); |
| 93 | + memcpy(buffer + 2, arr, MPLS_LABEL_SECTION_LENGTH); |
| 94 | + |
| 95 | + return MPLS_LABEL_SECTION_LENGTH + 1; |
| 96 | + } |
| 97 | + |
| 98 | + const char **get_ipfix_tmplt() const |
| 99 | + { |
| 100 | + static const char *ipfix_template[] = { |
| 101 | + IPFIX_MPLS_TEMPLATE(IPFIX_FIELD_NAMES) |
| 102 | + NULL |
| 103 | + }; |
| 104 | + return ipfix_template; |
| 105 | + } |
| 106 | + |
| 107 | + std::string get_text() const |
| 108 | + { |
| 109 | + std::ostringstream out; |
| 110 | + out << "mpls_label_1=\"" << (mpls >> 8) << '"'; |
| 111 | + return out.str(); |
| 112 | + } |
| 113 | +}; |
| 114 | + |
| 115 | +/** |
| 116 | + * \brief Process plugin for parsing MPLS packets. |
| 117 | + */ |
| 118 | +class MPLSPlugin : public ProcessPlugin |
| 119 | +{ |
| 120 | +public: |
| 121 | + OptionsParser *get_parser() const { return new OptionsParser("mpls", "Parse MPLS traffic"); } |
| 122 | + std::string get_name() const { return "mpls"; } |
| 123 | + RecordExt *get_ext() const { return new RecordExtMPLS(); } |
| 124 | + ProcessPlugin *copy(); |
| 125 | + |
| 126 | + int post_create(Flow &rec, const Packet &pkt); |
| 127 | +}; |
| 128 | + |
| 129 | +} |
| 130 | +#endif /* IPXP_PROCESS_MPLS_HPP */ |
| 131 | + |
0 commit comments