1 /* packet-f5ethtrailer.h
2  *
3  * F5 Ethernet Trailer Copyright 2008-2018 F5 Networks
4  *
5  * SPDX-License-Identifier: GPL-2.0-or-later
6  */
7 
8 /* How to use the fileinfo version tap
9  *
10  * Captures taken on an F5 device in versions 11.2.0 and later contain an
11  * initial packet that has information about how the capture was taken and
12  * about the device it was taken on (tcpdump command line, platform, version,
13  * etc.).  This tap allows other dissectors to obtain the version of BIG-IP
14  * software (if it is available).
15  *
16  * There are two functions defined in this header file (f5fileinfo_tap_reset()
17  * and f5fileinfo_tap_pkt()).  These functions are registered with the tap and
18  * will populate a structure provided by you with the version information.
19  *
20  * Step 1: Define a static variable of type "struct f5fileinfo".  This is where
21  * the version information will be stored.
22  *   static struct f5fileinfo myver = F5FILEINFO_TAP_DATA_INIT;
23  *
24  * Step 2: Register with the tap listener using the macro provided in your
25  * proto_reg_handoff function:
26  *   F5FILEINFO_TAP_LISTEN(&myver);
27  *
28  * Step 3: Use the version information in other parts of your code.
29  *   if(myver.ver[0] == 11) {
30  *      ...
31  *   }
32  *
33  * If you need to do something additional when you run into a version, you can
34  * define the F5FILEINFO_TAP_POST_FUNC macro before including this header file
35  * to be the name of a fuction to call at the end of the tap function.  This
36  * function must have a prototype of
37  *   static void F5FILEINFO_TAP_POST_FUNC(struct f5fileinfo_tap_data *);
38  * Note that this function also gets called with version of all zeroes when the
39  * tap gets reset (reload file).
40  * Note that this function does not get called if the version number does not
41  * change.
42  * Example:
43  *   #define F5FILEINFO_TAP_POST_FUNC f5info_tap_local
44  *   #include <epan/dissectors/packet-f5ethtrailer.h>
45  *   ...
46  *   static void f5info_tap_local(struct f5fileinfo_tap_data *tap_data)
47  *   {
48  *       ...
49  *   }
50  */
51 
52 #ifndef _PACKETH_F5ETHTRAILER_H_
53 #define _PACKETH_F5ETHTRAILER_H_
54 
55 #include <glib.h>
56 
57 #define F5ETH_TAP_TMM_MAX   G_MAXUINT16
58 #define F5ETH_TAP_TMM_BITS  16
59 #define F5ETH_TAP_SLOT_MAX  G_MAXUINT16
60 #define F5ETH_TAP_SLOT_BITS 16
61 
62 /** Magic number for Ethernet trailer tap data to ensure that any tap and the dissector were both
63  *  compiled from the same source.  No need to htonl this since the dissector and the tap should
64  *  both be compiled on the same platform.
65  *
66  *  Increment this value when the struct f5eth_tap_data (below) is changed.
67  */
68 #define F5ETH_TAP_MAGIC     0x68744521
69 
70 /** Data structure to hold data returned by the f5ethtrailer tap.  Magic has to be first. */
71 typedef struct f5eth_tap_data {
72     guint32 magic;        /**< Verify proper version of dissector */
73     guint32 trailer_len;  /**< Overall length of the F5 trailer */
74     /* 64 bit align */
75     guint64 flow;         /**< Flow ID */
76     guint64 peer_flow;    /**< Peer Flow ID */
77     /* 64 bit align */
78     gchar  *virtual_name; /**< Virtual server name */
79     guint16 slot;         /**< The slot the handled the packet (F5ETH_TAP_TMM_MAX == unknown) */
80     guint16 tmm;          /**< The tmm that handled the packet (F5ETH_TAP_sLOT_MAX == unknown) */
81     guint8  noise_low:1;  /**< If the frame has low noise(1) or not(0) */
82     guint8  noise_med:1;  /**< If the frame has medium noise(1) or not(0) */
83     guint8  noise_high:1; /**< If the frame has high noise(1) or not(0) */
84     guint8  flows_set:1;  /**< If the frame has flow/peerflow fields(1) or not(0) */
85     guint8  ingress:2;    /**< Whether the packet was ingress(1), egress(0) or unknown(3) */
86 } f5eth_tap_data_t;
87 
88 /** \brief Tap data version matches compiled version
89  *
90  *  @param tdata Pointer to tapdata from f5ethtrailer
91  *  @return 1 if the version of the tapdata matches the compiled version of the tap. 0 otherwise.
92  *
93  *  Use this function to ensure that the data from the f5ethtrailer tap is the same as the
94  *  structure used when your tap was compiled.  Use this to protect your tap from running against
95  *  a newer/older version of the f5ethtrailer dissector.
96  *
97  *  For example, at the top of your tap packet function, you can use:
98  *    if(check_f5eth_tap_magic(tdata) == 0) return 0;
99  */
check_f5eth_tap_magic(f5eth_tap_data_t * tdata)100 inline static int check_f5eth_tap_magic(f5eth_tap_data_t *tdata)
101 {
102     return(tdata->magic == F5ETH_TAP_MAGIC ? 1 : 0);
103 } /* check_f5eth_tap_magic() */
104 
105 #define F5FILEINFO_TAP_MAGIC 0x46350001
106 
107 /** Data structure to hold data returned by the f5fileinfo tap. */
108 struct f5fileinfo_tap_data {
109     guint32 magic;  /**< Just to make sure that we have the same version. */
110     guint32 ver[6]; /**< Array for version and build elements. */
111 };
112 
113 #define F5FILEINFO_TAP_DATA_INIT { 0, { 0, 0, 0, 0, 0, 0 } }
114 
115 #define F5VER_KNOWN(v) ((v)->ver[0] > 0)
116 
117 
118 #define F5VER_GE_11_2(v) (((v)->ver[0] > 11) \
119     || ((v)->ver[0] == 11 && (v)->ver[1] >= 2))
120 
121 #define F5VER_GE_11_2_1(v) (((v)->ver[0] > 11) \
122     || ((v)->ver[0] == 11 && (v)->ver[1] > 2) \
123     || ((v)->ver[0] == 11 && (v)->ver[1] == 2 && (v)->ver[2] >= 1))
124 
125 #define F5VER_GE_11_3(v) (((v)->ver[0] > 11) \
126     || ((v)->ver[0] == 11 && (v)->ver[1] >= 3))
127 
128 #define F5VER_GE_11_4(v) (((v)->ver[0] > 11) \
129     || ((v)->ver[0] == 11 && (v)->ver[1] >= 4))
130 
131 #define F5VER_GE_11_4_1(v) (((v)->ver[0] > 11) \
132     || ((v)->ver[0] == 11 && (v)->ver[1] > 4) \
133     || ((v)->ver[0] == 11 && (v)->ver[1] == 4 && (v)->ver[2] >= 1))
134 
135 #define F5VER_GE_11_5(v) (((v)->ver[0] > 11) \
136     || ((v)->ver[0] == 11 && (v)->ver[1] >= 5))
137 
138 #define F5VER_GE_11_5_1(v) (((v)->ver[0] > 11) \
139     || ((v)->ver[0] == 11 && (v)->ver[1] > 5) \
140     || ((v)->ver[0] == 11 && (v)->ver[1] == 5 && (v)->ver[2] >= 1))
141 
142 #define F5VER_GE_11_6(v) (((v)->ver[0] > 11) \
143     || ((v)->ver[0] == 11 && (v)->ver[1] >= 6))
144 
145 #define F5VER_GE_12_0(v) (((v)->ver[0] >= 12))
146 
147 
148 #ifndef F5FILEINFOTAP_SRC
149 
150 #ifdef F5FILEINFO_TAP_POST_FUNC
151 static void F5FILEINFO_TAP_POST_FUNC(struct f5fileinfo_tap_data *);
152 #endif
153 
f5fileinfo_tap_reset(void * p)154 static void f5fileinfo_tap_reset(void *p)
155 {
156     struct f5fileinfo_tap_data *s;
157 
158     s = (struct f5fileinfo_tap_data *)p;
159     s->ver[0] = 0;
160     s->ver[1] = 0;
161     s->ver[2] = 0;
162     s->ver[3] = 0;
163     s->ver[4] = 0;
164     s->ver[5] = 0;
165 #   ifdef F5FILEINFO_TAP_POST_FUNC
166         F5FILEINFO_TAP_POST_FUNC(s);
167 #   endif
168 } /* f5fileinfo_tap_reset() */
169 
f5fileinfo_tap_pkt(void * tapdata,packet_info * pinfo _U_,epan_dissect_t * edt _U_,const void * data)170 static tap_packet_status f5fileinfo_tap_pkt(
171     void *tapdata,
172     packet_info *pinfo _U_,
173     epan_dissect_t *edt _U_,
174     const void *data
175 ) {
176     struct f5fileinfo_tap_data *s;
177     struct f5fileinfo_tap_data *fromtap;
178 
179     s = (struct f5fileinfo_tap_data *)tapdata;
180     fromtap = (struct f5fileinfo_tap_data *)data;
181     if(fromtap->magic != F5FILEINFO_TAP_MAGIC) {
182         /* Magic numbers do not match.  f5ethtrailer plugin was compiled from
183          * different source than this plugin. */
184         return(TAP_PACKET_DONT_REDRAW);
185     }
186     if (s->ver[0] == fromtap->ver[0] &&
187         s->ver[1] == fromtap->ver[1] &&
188         s->ver[2] == fromtap->ver[2] &&
189         s->ver[3] == fromtap->ver[3] &&
190         s->ver[4] == fromtap->ver[4] &&
191         s->ver[5] == fromtap->ver[5])
192     {
193         return(TAP_PACKET_DONT_REDRAW);
194     }
195     s->ver[0] = fromtap->ver[0];
196     s->ver[1] = fromtap->ver[1];
197     s->ver[2] = fromtap->ver[2];
198     s->ver[3] = fromtap->ver[3];
199     s->ver[4] = fromtap->ver[4];
200     s->ver[5] = fromtap->ver[5];
201 #   ifdef F5FILEINFO_TAP_POST_FUNC
202         F5FILEINFO_TAP_POST_FUNC(s);
203 #   endif
204     return(TAP_PACKET_REDRAW);
205 } /* f5fileinfo_tap_pkt() */
206 
207 
208 #define F5FILEINFO_TAP_LISTEN(a) \
209     register_tap_listener("f5fileinfo", (a), NULL, TL_REQUIRES_NOTHING, f5fileinfo_tap_reset, f5fileinfo_tap_pkt, NULL, NULL)
210 
211 
212 #endif /* ifndef F5INFOTAP_SRC */
213 
214 
215 #endif /* ifndef _PACKETH_F5ETHTRAILER_H_ */
216 
217 /*
218  * Editor modelines  -  https://www.wireshark.org/tools/modelines.html
219  *
220  * Local variables:
221  * c-basic-offset: 4
222  * tab-width: 8
223  * indent-tabs-mode: nil
224  * End:
225  *
226  * vi: set shiftwidth=4 tabstop=8 expandtab:
227  * :indentSize=4:tabSize=8:noTabs=true:
228  */