3 * exported_pdu helper functions
4 * Copyright 2013, Anders Broman <anders-broman@ericsson.com>
6 * Wireshark - Network traffic analyzer
7 * By Gerald Combs <gerald@wireshark.org>
8 * Copyright 1998 Gerald Combs
10 * SPDX-License-Identifier: GPL-2.0-or-later
16 #include <epan/packet.h>
17 #include <epan/exported_pdu.h>
18 #include <epan/address_types.h>
21 static GSList *export_pdu_tap_name_list = NULL;
23 static int exp_pdu_data_ip_size(const address* addr)
25 if (addr->type == AT_IPv4){
26 return 4 + EXP_PDU_TAG_IPV4_LEN;
27 } else if(addr->type == AT_IPv6){
28 return 4 + EXP_PDU_TAG_IPV6_LEN;
34 static int exp_pdu_data_src_ip_size(packet_info *pinfo, void* data _U_)
36 return exp_pdu_data_ip_size(&pinfo->net_src);
39 static int exp_pdu_data_src_ip_populate_data(packet_info *pinfo, void* data _U_, guint8 *tlv_buffer, guint32 buffer_size _U_)
41 if(pinfo->net_src.type == AT_IPv4){
43 tlv_buffer[1] = EXP_PDU_TAG_IPV4_SRC;
45 tlv_buffer[3] = EXP_PDU_TAG_IPV4_LEN; /* tag length */
46 memcpy(tlv_buffer+4, pinfo->net_src.data, EXP_PDU_TAG_IPV4_LEN);
47 return 4 + EXP_PDU_TAG_IPV4_LEN;
48 }else if(pinfo->net_src.type == AT_IPv6){
50 tlv_buffer[1] = EXP_PDU_TAG_IPV6_SRC;
52 tlv_buffer[3] = EXP_PDU_TAG_IPV6_LEN; /* tag length */
53 memcpy(tlv_buffer+4, pinfo->net_src.data, EXP_PDU_TAG_IPV6_LEN);
54 return 4 + EXP_PDU_TAG_IPV6_LEN;
60 static int exp_pdu_data_dst_ip_size(packet_info *pinfo, void* data _U_)
62 return exp_pdu_data_ip_size(&pinfo->net_dst);
65 static int exp_pdu_data_dst_ip_populate_data(packet_info *pinfo, void* data _U_, guint8 *tlv_buffer, guint32 buffer_size _U_)
67 if(pinfo->net_dst.type == AT_IPv4){
69 tlv_buffer[1] = EXP_PDU_TAG_IPV4_DST;
71 tlv_buffer[3] = EXP_PDU_TAG_IPV4_LEN; /* tag length */
72 memcpy(tlv_buffer+4, pinfo->net_dst.data, EXP_PDU_TAG_IPV4_LEN);
73 return 4 + EXP_PDU_TAG_IPV4_LEN;
74 }else if(pinfo->net_dst.type == AT_IPv6){
76 tlv_buffer[1] = EXP_PDU_TAG_IPV6_DST;
78 tlv_buffer[3] = EXP_PDU_TAG_IPV6_LEN; /* tag length */
79 memcpy(tlv_buffer+4, pinfo->net_dst.data, EXP_PDU_TAG_IPV6_LEN);
80 return 4 + EXP_PDU_TAG_IPV6_LEN;
86 static int exp_pdu_data_port_type_size(packet_info *pinfo _U_, void* data _U_)
88 return EXP_PDU_TAG_PORT_LEN + 4;
91 static guint exp_pdu_new_to_old_port_type(port_type pt)
118 return OLD_PT_BLUETOOTH;
121 DISSECTOR_ASSERT(FALSE);
125 static int exp_pdu_data_port_type_populate_data(packet_info *pinfo, void* data, guint8 *tlv_buffer, guint32 buffer_size _U_)
130 tlv_buffer[1] = EXP_PDU_TAG_PORT_TYPE;
132 tlv_buffer[3] = EXP_PDU_TAG_PORT_TYPE_LEN; /* tag length */
133 pt = exp_pdu_new_to_old_port_type(pinfo->ptype);
134 tlv_buffer[4] = (pt & 0xff000000) >> 24;
135 tlv_buffer[5] = (pt & 0x00ff0000) >> 16;
136 tlv_buffer[6] = (pt & 0x0000ff00) >> 8;
137 tlv_buffer[7] = (pt & 0x000000ff);
139 return exp_pdu_data_port_type_size(pinfo, data);
142 static int exp_pdu_data_port_size(packet_info *pinfo _U_, void* data _U_)
144 return EXP_PDU_TAG_PORT_LEN + 4;
147 static int exp_pdu_data_port_populate_data(guint32 port, guint8 porttype, guint8 *tlv_buffer, guint32 buffer_size _U_)
150 tlv_buffer[1] = porttype;
152 tlv_buffer[3] = EXP_PDU_TAG_PORT_LEN; /* tag length */
153 tlv_buffer[4] = (port & 0xff000000) >> 24;
154 tlv_buffer[5] = (port & 0x00ff0000) >> 16;
155 tlv_buffer[6] = (port & 0x0000ff00) >> 8;
156 tlv_buffer[7] = (port & 0x000000ff);
158 return EXP_PDU_TAG_PORT_LEN + 4;
161 static int exp_pdu_data_src_port_populate_data(packet_info *pinfo, void* data _U_, guint8 *tlv_buffer, guint32 buffer_size)
163 return exp_pdu_data_port_populate_data(pinfo->srcport, EXP_PDU_TAG_SRC_PORT, tlv_buffer, buffer_size);
166 static int exp_pdu_data_dst_port_populate_data(packet_info *pinfo, void* data _U_, guint8 *tlv_buffer, guint32 buffer_size)
168 return exp_pdu_data_port_populate_data(pinfo->destport, EXP_PDU_TAG_DST_PORT, tlv_buffer, buffer_size);
171 static int exp_pdu_data_orig_frame_num_size(packet_info *pinfo _U_, void* data _U_)
173 return EXP_PDU_TAG_ORIG_FNO_LEN + 4;
176 static int exp_pdu_data_orig_frame_num_populate_data(packet_info *pinfo, void* data, guint8 *tlv_buffer, guint32 buffer_size _U_)
179 tlv_buffer[1] = EXP_PDU_TAG_ORIG_FNO;
181 tlv_buffer[3] = EXP_PDU_TAG_ORIG_FNO_LEN; /* tag length */
182 tlv_buffer[4] = (pinfo->num & 0xff000000) >> 24;
183 tlv_buffer[5] = (pinfo->num & 0x00ff0000) >> 16;
184 tlv_buffer[6] = (pinfo->num & 0x0000ff00) >> 8;
185 tlv_buffer[7] = (pinfo->num & 0x000000ff);
187 return exp_pdu_data_orig_frame_num_size(pinfo, data);
190 WS_DLL_PUBLIC int exp_pdu_data_dissector_table_num_value_size(packet_info *pinfo _U_, void* data _U_)
192 return EXP_PDU_TAG_DISSECTOR_TABLE_NUM_VAL_LEN + 4;
195 WS_DLL_PUBLIC int exp_pdu_data_dissector_table_num_value_populate_data(packet_info *pinfo _U_, void* data, guint8 *tlv_buffer, guint32 buffer_size _U_)
197 guint32 value = GPOINTER_TO_UINT(data);
200 tlv_buffer[1] = EXP_PDU_TAG_DISSECTOR_TABLE_NAME_NUM_VAL;
202 tlv_buffer[3] = EXP_PDU_TAG_DISSECTOR_TABLE_NUM_VAL_LEN; /* tag length */
203 tlv_buffer[4] = (value & 0xff000000) >> 24;
204 tlv_buffer[5] = (value & 0x00ff0000) >> 16;
205 tlv_buffer[6] = (value & 0x0000ff00) >> 8;
206 tlv_buffer[7] = (value & 0x000000ff);
208 return exp_pdu_data_dissector_table_num_value_size(pinfo, data);
212 exp_pdu_data_item_t exp_pdu_data_src_ip = {exp_pdu_data_src_ip_size, exp_pdu_data_src_ip_populate_data, NULL};
213 exp_pdu_data_item_t exp_pdu_data_dst_ip = {exp_pdu_data_dst_ip_size, exp_pdu_data_dst_ip_populate_data, NULL};
214 exp_pdu_data_item_t exp_pdu_data_port_type = {exp_pdu_data_port_type_size, exp_pdu_data_port_type_populate_data, NULL};
215 exp_pdu_data_item_t exp_pdu_data_src_port = {exp_pdu_data_port_size, exp_pdu_data_src_port_populate_data, NULL};
216 exp_pdu_data_item_t exp_pdu_data_dst_port = {exp_pdu_data_port_size, exp_pdu_data_dst_port_populate_data, NULL};
217 exp_pdu_data_item_t exp_pdu_data_orig_frame_num = {exp_pdu_data_orig_frame_num_size, exp_pdu_data_orig_frame_num_populate_data, NULL};
219 exp_pdu_data_t *export_pdu_create_common_tags(packet_info *pinfo, const char *proto_name, guint16 tag_type)
221 const exp_pdu_data_item_t *common_exp_pdu_items[] = {
222 &exp_pdu_data_src_ip,
223 &exp_pdu_data_dst_ip,
224 &exp_pdu_data_port_type,
225 &exp_pdu_data_src_port,
226 &exp_pdu_data_dst_port,
227 &exp_pdu_data_orig_frame_num,
231 return export_pdu_create_tags(pinfo, proto_name, tag_type, common_exp_pdu_items);
235 * Allocates and fills the exp_pdu_data_t struct according to the list of items
237 * The tags in the tag buffer SHOULD be added in numerical order.
240 export_pdu_create_tags(packet_info *pinfo, const char* proto_name, guint16 tag_type, const exp_pdu_data_item_t **items_list)
242 exp_pdu_data_t *exp_pdu_data;
243 const exp_pdu_data_item_t **loop_items = items_list;
244 int tag_buf_size = 0;
245 int proto_str_len, proto_tag_len, buf_remaining, item_size;
248 DISSECTOR_ASSERT(proto_name != NULL);
249 DISSECTOR_ASSERT((tag_type == EXP_PDU_TAG_PROTO_NAME) || (tag_type == EXP_PDU_TAG_HEUR_PROTO_NAME) || (tag_type == EXP_PDU_TAG_DISSECTOR_TABLE_NAME));
251 exp_pdu_data = (exp_pdu_data_t *)wmem_alloc(wmem_packet_scope(), sizeof(exp_pdu_data_t));
253 /* Start by computing size of protocol name as a tag */
254 proto_str_len = (int)strlen(proto_name);
256 /* Ensure that tag length is a multiple of 4 bytes */
257 proto_tag_len = ((proto_str_len + 3) & 0xfffffffc);
259 /* Add Tag + length */
260 tag_buf_size += (proto_tag_len + 4);
262 /* Compute size of items */
263 while (*loop_items) {
264 tag_buf_size += (*loop_items)->size_func(pinfo, (*loop_items)->data);
268 /* Add end of options length */
271 exp_pdu_data->tlv_buffer = (guint8 *)wmem_alloc0(wmem_packet_scope(), tag_buf_size);
272 exp_pdu_data->tlv_buffer_len = tag_buf_size;
274 buffer_data = exp_pdu_data->tlv_buffer;
275 buf_remaining = exp_pdu_data->tlv_buffer_len;
277 /* Start by adding protocol name as a tag */
278 buffer_data[0] = (tag_type & 0xff00) >> 8;
279 buffer_data[1] = tag_type & 0x00ff;
280 buffer_data[2] = (proto_tag_len & 0xff00) >> 8;
281 buffer_data[3] = proto_tag_len & 0x00ff; /* tag length */
282 memcpy(buffer_data+4, proto_name, proto_str_len);
283 buffer_data += (proto_tag_len+4);
284 buf_remaining -= (proto_tag_len+4);
287 loop_items = items_list;
288 while (*loop_items) {
289 item_size = (*loop_items)->populate_data(pinfo, (*loop_items)->data, buffer_data, buf_remaining);
290 buffer_data += item_size;
291 buf_remaining -= item_size;
299 register_export_pdu_tap(const char *name)
301 gchar *tap_name = g_strdup(name);
302 export_pdu_tap_name_list = g_slist_prepend(export_pdu_tap_name_list, tap_name);
303 return register_tap(tap_name);
307 gint sort_pdu_tap_name_list(gconstpointer a, gconstpointer b)
309 return g_strcmp0((const char *)a, (const char*)b);
313 get_export_pdu_tap_list(void)
315 export_pdu_tap_name_list = g_slist_sort(export_pdu_tap_name_list, sort_pdu_tap_name_list);
316 return export_pdu_tap_name_list;
319 void export_pdu_init(void)
323 void export_pdu_cleanup(void)
325 g_slist_free_full(export_pdu_tap_name_list, g_free);
329 * Editor modelines - http://www.wireshark.org/tools/modelines.html
334 * indent-tabs-mode: t
337 * vi: set shiftwidth=8 tabstop=8 noexpandtab:
338 * :indentSize=8:tabSize=8:noTabs=false: