|
| 1 | +/** |
| 2 | + * \file vlan.hpp |
| 3 | + * \brief Plugin for parsing vlan traffic. |
| 4 | + * \author Jakub Antonín Štigler xstigl00@[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_VLAN_HPP |
| 30 | +#define IPXP_PROCESS_VLAN_HPP |
| 31 | + |
| 32 | +#include <cstring> |
| 33 | + |
| 34 | +#ifdef WITH_NEMEA |
| 35 | + #include "fields.h" |
| 36 | +#endif |
| 37 | + |
| 38 | +#include <ipfixprobe/process.hpp> |
| 39 | +#include <ipfixprobe/flowifc.hpp> |
| 40 | +#include <ipfixprobe/packet.hpp> |
| 41 | +#include <ipfixprobe/ipfix-elements.hpp> |
| 42 | + |
| 43 | +#include <cstdint> |
| 44 | +#include <string> |
| 45 | +#include <sstream> |
| 46 | + |
| 47 | +namespace ipxp { |
| 48 | + |
| 49 | +#define VLAN_UNIREC_TEMPLATE "VLAN_ID" |
| 50 | + |
| 51 | +UR_FIELDS ( |
| 52 | + uint16 VLAN_ID |
| 53 | +) |
| 54 | + |
| 55 | +/** |
| 56 | + * \brief Flow record extension header for storing parsed VLAN data. |
| 57 | + */ |
| 58 | +struct RecordExtVLAN : public RecordExt { |
| 59 | + static int REGISTERED_ID; |
| 60 | + |
| 61 | + // vlan id is in the host byte order |
| 62 | + uint16_t vlan_id; |
| 63 | + |
| 64 | + RecordExtVLAN() : RecordExt(REGISTERED_ID), vlan_id(0) |
| 65 | + { |
| 66 | + } |
| 67 | + |
| 68 | +#ifdef WITH_NEMEA |
| 69 | + virtual void fill_unirec(ur_template_t *tmplt, void *record) |
| 70 | + { |
| 71 | + ur_set(tmplt, record, F_VLAN_ID, vlan_id); |
| 72 | + } |
| 73 | + |
| 74 | + const char *get_unirec_tmplt() const |
| 75 | + { |
| 76 | + return VLAN_UNIREC_TEMPLATE; |
| 77 | + } |
| 78 | +#endif |
| 79 | + |
| 80 | + virtual int fill_ipfix(uint8_t *buffer, int size) |
| 81 | + { |
| 82 | + const int LEN = sizeof(vlan_id); |
| 83 | + |
| 84 | + if (size < LEN) { |
| 85 | + return -1; |
| 86 | + } |
| 87 | + |
| 88 | + *reinterpret_cast<uint16_t *>(buffer) = htons(vlan_id); |
| 89 | + return 0; |
| 90 | + } |
| 91 | + |
| 92 | + const char **get_ipfix_tmplt() const |
| 93 | + { |
| 94 | + static const char *ipfix_template[] = { |
| 95 | + IPFIX_VLAN_TEMPLATE(IPFIX_FIELD_NAMES) |
| 96 | + NULL |
| 97 | + }; |
| 98 | + return ipfix_template; |
| 99 | + } |
| 100 | + |
| 101 | + std::string get_text() const |
| 102 | + { |
| 103 | + std::ostringstream out; |
| 104 | + out << "vlan_id=\"" << vlan_id << '"'; |
| 105 | + return out.str(); |
| 106 | + } |
| 107 | +}; |
| 108 | + |
| 109 | +/** |
| 110 | + * \brief Process plugin for parsing VLAN packets. |
| 111 | + */ |
| 112 | +class VLANPlugin : public ProcessPlugin |
| 113 | +{ |
| 114 | +public: |
| 115 | + OptionsParser *get_parser() const { return new OptionsParser("vlan", "Parse VLAN traffic"); } |
| 116 | + std::string get_name() const { return "vlan"; } |
| 117 | + RecordExt *get_ext() const { return new RecordExtVLAN(); } |
| 118 | + ProcessPlugin *copy(); |
| 119 | + |
| 120 | + int post_create(Flow &rec, const Packet &pkt); |
| 121 | +}; |
| 122 | + |
| 123 | +} |
| 124 | +#endif /* IPXP_PROCESS_VLAN_HPP */ |
| 125 | + |
0 commit comments