a9c4078326694d2185e2074858125f43e6501748
[metze/wireshark/wip.git] / epan / dissectors / packet-netlink.c
1 /* packet-netlink.c
2  *
3  * Wireshark - Network traffic analyzer
4  * By Gerald Combs <gerald@wireshark.org>
5  * Copyright 1998 Gerald Combs
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  */
21
22 /* http://www.tcpdump.org/linktypes/LINKTYPE_NETLINK.html */
23
24 #define NEW_PROTO_TREE_API
25
26 #include "config.h"
27
28 #include <epan/packet.h>
29 #include <epan/arptypes.h>
30 #include <wiretap/wtap.h>
31
32 #include "packet-netlink.h"
33
34 void proto_register_netlink(void);
35 void proto_reg_handoff_netlink(void);
36
37 /*
38  * A DLT_LINUX_SLL fake link-layer header.
39  */
40 #define SLL_HEADER_SIZE 16              /* total header length */
41
42 static const value_string netlink_family_vals[] = {
43         { WS_NETLINK_ROUTE,          "Route" },
44         { WS_NETLINK_UNUSED,         "Unused" },
45         { WS_NETLINK_USERSOCK,       "User-mode socket protocols" },
46         { WS_NETLINK_FIREWALL,       "Unused (formerly: ip_queue)" },
47         { WS_NETLINK_SOCK_DIAG,      "Socket monitoring" },
48         { WS_NETLINK_NFLOG,          "Netfilter ULOG" },
49         { WS_NETLINK_XFRM,           "IPsec" },
50         { WS_NETLINK_SELINUX,        "SELinux events" },
51         { WS_NETLINK_ISCSI,          "Open-iSCSI" },
52         { WS_NETLINK_AUDIT,          "Auditing" },
53         { WS_NETLINK_FIB_LOOKUP,     "FIB lookup" },
54         { WS_NETLINK_CONNECTOR,      "Kernel connector" },
55         { WS_NETLINK_NETFILTER,      "Netfilter" },
56         { WS_NETLINK_IP6_FW,         "Unused (formerly: ip6_queue)" },
57         { WS_NETLINK_DNRTMSG,        "DECnet routing messages" },
58         { WS_NETLINK_KOBJECT_UEVENT, "Kernel messages to userspace" },
59         { WS_NETLINK_GENERIC,        "Generic" },
60         { WS_NETLINK_SCSITRANSPORT,  "SCSI Transports" },
61         { WS_NETLINK_ECRYPTFS,       "ecryptfs" },
62         { WS_NETLINK_RDMA,           "RDMA" },
63         { WS_NETLINK_CRYPTO,         "Crypto layer" },
64         { 0, NULL }
65 };
66
67 value_string_ext netlink_family_vals_ext = VALUE_STRING_EXT_INIT(netlink_family_vals);
68
69 static const value_string type_vals[] = {
70         { WS_NLMSG_NOOP,    "Nothing" },
71         { WS_NLMSG_ERROR,   "Error" },
72         { WS_NLMSG_DONE,    "End of a dump" },
73         { WS_NLMSG_OVERRUN, "Data lost" },
74         { 0, NULL }
75 };
76
77 static const value_string ha_types[] = {
78         { ARPHRD_NETLINK,    "Netlink" },
79         { 0, NULL }
80 };
81
82 static dissector_handle_t netlink_handle;
83
84 static header_field_info *hfi_netlink = NULL;
85
86 #define NETLINK_HFI_INIT HFI_INIT(proto_netlink)
87
88 static header_field_info hfi_netlink_hatype NETLINK_HFI_INIT =
89         { "Link-layer address type", "netlink.hatype", FT_UINT16, BASE_DEC,
90                 VALS(ha_types), 0x0, NULL, HFILL };
91
92 /* Linux netlink protocol type */
93 static header_field_info hfi_netlink_family NETLINK_HFI_INIT =
94         { "Family", "netlink.family", FT_UINT16, BASE_HEX | BASE_EXT_STRING,
95                 &netlink_family_vals_ext, 0x00, NULL, HFILL };
96
97 static header_field_info hfi_netlink_hdr_len NETLINK_HFI_INIT =
98         { "Length", "netlink.hdr_len", FT_UINT32, BASE_DEC,
99                 NULL, 0x00, "Length of message including header", HFILL };
100
101 static header_field_info hfi_netlink_hdr_type NETLINK_HFI_INIT =
102         { "Message type", "netlink.hdr_type", FT_UINT16, BASE_HEX,
103                 VALS(type_vals), 0x00, "Type of message content", HFILL };
104
105 static header_field_info hfi_netlink_hdr_flags NETLINK_HFI_INIT =
106         { "Flags", "netlink.hdr_flags", FT_UINT16, BASE_HEX,
107                 NULL, 0x00, "Additional flags", HFILL };
108
109 static header_field_info hfi_netlink_hdr_flag_dumpfiltered NETLINK_HFI_INIT =
110         { "Dump filtered", "netlink.hdr_flags.dump_filtered", FT_UINT16, BASE_DEC,
111                 NULL, WS_NLM_F_DUMP_FILTERED, "Dump was filtered as requested", HFILL };
112
113 static header_field_info hfi_netlink_hdr_flag_dumpintr NETLINK_HFI_INIT =
114         { "Dump inconsistent", "netlink.hdr_flags.dump_intr", FT_UINT16, BASE_DEC,
115                 NULL, WS_NLM_F_DUMP_INTR, "Dump was inconsistent due to sequence change", HFILL };
116
117 static header_field_info hfi_netlink_hdr_flag_echo NETLINK_HFI_INIT =
118         { "Echo", "netlink.hdr_flags.echo", FT_UINT16, BASE_DEC,
119                 NULL, WS_NLM_F_ECHO, "Echo this request", HFILL };
120
121 static header_field_info hfi_netlink_hdr_flag_ack NETLINK_HFI_INIT =
122         { "Ack", "netlink.hdr_flags.ack", FT_UINT16, BASE_DEC,
123                 NULL, WS_NLM_F_ACK, "Asking for an ack", HFILL };
124
125 static header_field_info hfi_netlink_hdr_flag_multi NETLINK_HFI_INIT =
126         { "Multipart message", "netlink.hdr_flags.multi", FT_UINT16, BASE_DEC,
127                 NULL, WS_NLM_F_MULTI, "Part of multi-part message terminated by NLMSG_DONE", HFILL };
128
129 static header_field_info hfi_netlink_hdr_flag_request NETLINK_HFI_INIT =
130         { "Request", "netlink.hdr_flags.request", FT_UINT16, BASE_DEC,
131                 NULL, WS_NLM_F_REQUEST, "It is a request message", HFILL };
132
133 static header_field_info hfi_netlink_hdr_flag_root NETLINK_HFI_INIT =
134         { "Specify tree root", "netlink.hdr_flags.root", FT_UINT16, BASE_DEC,
135                 NULL, WS_NLM_F_ROOT, "Return the complete table instead of a single entry", HFILL };
136
137 static header_field_info hfi_netlink_hdr_flag_match NETLINK_HFI_INIT =
138         { "Return all matching", "netlink.hdr_flags.match", FT_UINT16, BASE_DEC,
139                 NULL, WS_NLM_F_MATCH, "Return all entries matching criteria in request", HFILL };
140
141 static header_field_info hfi_netlink_hdr_flag_atomic NETLINK_HFI_INIT =
142         { "Atomic", "netlink.hdr_flags.atomic", FT_UINT16, BASE_DEC,
143                 NULL, WS_NLM_F_ATOMIC, "Return an atomic snapshot of the table", HFILL };
144
145 static header_field_info hfi_netlink_hdr_flag_replace NETLINK_HFI_INIT =
146         { "Replace", "netlink.hdr_flags.replace", FT_UINT16, BASE_DEC,
147                 NULL, WS_NLM_F_REPLACE, "Replace existing objects", HFILL };
148
149 static header_field_info hfi_netlink_hdr_flag_excl NETLINK_HFI_INIT =
150         { "Excl", "netlink.hdr_flags.excl", FT_UINT16, BASE_DEC,
151                 NULL, WS_NLM_F_EXCL, "Do not replace existing objects", HFILL };
152
153 static header_field_info hfi_netlink_hdr_flag_create NETLINK_HFI_INIT =
154         { "Create", "netlink.hdr_flags.create", FT_UINT16, BASE_DEC,
155                 NULL, WS_NLM_F_CREATE, "Create objects if it does not already exist", HFILL };
156
157 static header_field_info hfi_netlink_hdr_flag_append NETLINK_HFI_INIT =
158         { "Append", "netlink.hdr_flags.append", FT_UINT16, BASE_DEC,
159                 NULL, WS_NLM_F_APPEND, "Add to end of object list", HFILL };
160
161 static header_field_info hfi_netlink_hdr_seq NETLINK_HFI_INIT =
162         { "Sequence", "netlink.hdr_seq", FT_UINT32, BASE_DEC,
163                 NULL, 0x00, "Sequence number", HFILL };
164
165 static header_field_info hfi_netlink_hdr_pid NETLINK_HFI_INIT =
166         { "Port ID", "netlink.hdr_pid", FT_UINT32, BASE_DEC,
167                 NULL, 0x00, "Sender port ID", HFILL };
168
169 static header_field_info hfi_netlink_attr_len NETLINK_HFI_INIT =
170         { "Len", "netlink.attr_len", FT_UINT16, BASE_DEC,
171                 NULL, 0x00, NULL, HFILL };
172
173 static header_field_info hfi_netlink_attr_type NETLINK_HFI_INIT =
174         { "Type", "netlink.attr_type", FT_UINT16, BASE_HEX,
175                 NULL, 0x0000, "Netlink Attribute type", HFILL };
176
177 static header_field_info hfi_netlink_attr_type_nested NETLINK_HFI_INIT =
178         { "Nested", "netlink.attr_type.nested", FT_UINT16, BASE_DEC,
179                 NULL, NLA_F_NESTED, "Carries nested attributes", HFILL };
180
181 static header_field_info hfi_netlink_attr_type_net_byteorder NETLINK_HFI_INIT =
182         { "Network byte order", "netlink.attr_type.net_byteorder", FT_UINT16, BASE_DEC,
183                 NULL, NLA_F_NET_BYTEORDER, "Payload stored in network byte order", HFILL };
184
185 static header_field_info hfi_netlink_attr_index NETLINK_HFI_INIT =
186         { "Index", "netlink.attr_index", FT_UINT16, BASE_DEC,
187                 NULL, 0x0000, "Netlink Attribute type (array index)", HFILL };
188
189 static header_field_info hfi_netlink_attr_data NETLINK_HFI_INIT =
190         { "Data", "netlink.attr_data", FT_BYTES, BASE_NONE,
191                 NULL, 0x00, NULL, HFILL };
192
193 /* TODO add a value_string for errno. */
194 static header_field_info hfi_netlink_error NETLINK_HFI_INIT =
195         { "Error code", "netlink.error", FT_INT32, BASE_DEC,
196                 NULL, 0x00, "Negative errno or 0 for acknowledgements", HFILL };
197
198 static gint ett_netlink_cooked = -1;
199 static gint ett_netlink_msghdr = -1;
200 static gint ett_netlink_msg = -1;
201 static gint ett_netlink_hdr_flags = -1;
202 static gint ett_netlink_attr_type = -1;
203
204 static dissector_table_t netlink_dissector_table;
205
206
207 static const int *netlink_header_get_flags[] = {
208         &hfi_netlink_hdr_flag_request.id,
209         &hfi_netlink_hdr_flag_multi.id,
210         &hfi_netlink_hdr_flag_ack.id,
211         &hfi_netlink_hdr_flag_echo.id,
212         &hfi_netlink_hdr_flag_dumpintr.id,
213         &hfi_netlink_hdr_flag_dumpfiltered.id,
214
215         &hfi_netlink_hdr_flag_root.id,
216         &hfi_netlink_hdr_flag_match.id,
217         &hfi_netlink_hdr_flag_atomic.id,
218         NULL
219 };
220
221 static const int *netlink_header_new_flags[] = {
222         &hfi_netlink_hdr_flag_request.id,
223         &hfi_netlink_hdr_flag_multi.id,
224         &hfi_netlink_hdr_flag_ack.id,
225         &hfi_netlink_hdr_flag_echo.id,
226         &hfi_netlink_hdr_flag_dumpintr.id,
227         &hfi_netlink_hdr_flag_dumpfiltered.id,
228
229         &hfi_netlink_hdr_flag_replace.id,
230         &hfi_netlink_hdr_flag_excl.id,
231         &hfi_netlink_hdr_flag_create.id,
232         &hfi_netlink_hdr_flag_append.id,
233         NULL
234 };
235
236 static const int *netlink_header_standard_flags[] = {
237         &hfi_netlink_hdr_flag_request.id,
238         &hfi_netlink_hdr_flag_multi.id,
239         &hfi_netlink_hdr_flag_ack.id,
240         &hfi_netlink_hdr_flag_echo.id,
241         &hfi_netlink_hdr_flag_dumpintr.id,
242         &hfi_netlink_hdr_flag_dumpfiltered.id,
243         NULL
244 };
245
246
247 static int
248 dissect_netlink_attributes_common(tvbuff_t *tvb, header_field_info *hfi_type, int ett_tree, int ett_attrib, void *data, struct packet_netlink_data *nl_data, proto_tree *tree, int offset, int length, netlink_attributes_cb_t cb)
249 {
250         int encoding;
251         int padding = (4 - offset) & 3;
252
253         DISSECTOR_ASSERT(nl_data);
254
255         encoding = nl_data->encoding;
256
257         /* align to 4 */
258         offset += padding;
259         if (length == -1) {
260                 length = tvb_captured_length_remaining(tvb, offset);
261         } else {
262                 length -= padding;
263         }
264
265         while (length >= 4) {
266                 guint16 rta_len, rta_type, type;
267
268                 proto_item *ti, *type_item;
269                 proto_tree *attr_tree, *type_tree;
270
271                 rta_len = tvb_get_guint16(tvb, offset, encoding);
272                 if (rta_len < 4) {
273                         /* XXX invalid expert */
274                         break;
275                 }
276
277                 /* XXX expert info when rta_len < length? */
278                 rta_len = MIN(rta_len, length);
279
280                 attr_tree = proto_tree_add_subtree(tree, tvb, offset, rta_len, ett_tree, &ti, "Attribute");
281
282                 proto_tree_add_item(attr_tree, &hfi_netlink_attr_len, tvb, offset, 2, encoding);
283                 offset += 2;
284
285                 rta_type = tvb_get_guint16(tvb, offset, encoding);
286                 if (ett_attrib == -1) {
287                         /* List of attributes */
288                         type = rta_type & NLA_TYPE_MASK;
289                         type_item = proto_tree_add_item(attr_tree, &hfi_netlink_attr_type, tvb, offset, 2, encoding);
290                         type_tree = proto_item_add_subtree(type_item, ett_netlink_attr_type);
291                         proto_tree_add_item(type_tree, &hfi_netlink_attr_type_nested, tvb, offset, 2, encoding);
292                         proto_tree_add_item(type_tree, &hfi_netlink_attr_type_net_byteorder, tvb, offset, 2, encoding);
293                         proto_tree_add_item(type_tree, hfi_type, tvb, offset, 2, encoding);
294                         offset += 2;
295
296                         if (rta_type & NLA_F_NESTED)
297                                 proto_item_append_text(type_item, ", Nested");
298
299                         if (hfi_type->strings) {
300                                 /* XXX, export hf_try_val_to_str */
301                                 const char *rta_str;
302
303                                 if (hfi_type->display & BASE_EXT_STRING) {
304                                         rta_str = try_val_to_str_ext(type, (value_string_ext *)hfi_type->strings);
305                                 } else {
306                                         rta_str = try_val_to_str(type, (const value_string *) hfi_type->strings);
307                                 }
308
309                                 if (rta_str) {
310                                         proto_item_append_text(type_item, ", %s (%d)", rta_str, type);
311                                         proto_item_append_text(ti, ": %s", rta_str);
312                                 }
313                         }
314
315                         if (!cb(tvb, data, attr_tree, rta_type, offset, rta_len - 4)) {
316                                 proto_tree_add_item(attr_tree, &hfi_netlink_attr_data, tvb, offset, rta_len - 4, encoding);
317                         }
318                 } else {
319                         /*
320                          * Nested attributes, constructing an array (list of
321                          * attributes where its type is the array index and its
322                          * value is the actual list of interesting attributes).
323                          */
324                         proto_tree_add_item(attr_tree, &hfi_netlink_attr_index, tvb, offset, 2, encoding);
325                         offset += 2;
326                         proto_item_append_text(ti, " %u", rta_type);
327
328                         dissect_netlink_attributes(tvb, hfi_type, ett_attrib, data, nl_data, attr_tree, offset, rta_len - 4, cb);
329                 }
330
331                 /* Assume offset already aligned, next offset is rta_len plus alignment. */
332                 rta_len = MIN((rta_len + 3) & ~3, length);
333                 offset += rta_len - 4;  /* Header was already skipped */
334                 length -= rta_len;
335         }
336
337         return offset;
338 }
339
340 int
341 dissect_netlink_attributes(tvbuff_t *tvb, header_field_info *hfi_type, int ett, void *data, struct packet_netlink_data *nl_data, proto_tree *tree, int offset, int length, netlink_attributes_cb_t cb)
342 {
343         return dissect_netlink_attributes_common(tvb, hfi_type, ett, -1, data, nl_data, tree, offset, length, cb);
344 }
345
346 int
347 dissect_netlink_attributes_array(tvbuff_t *tvb, header_field_info *hfi_type, int ett_array, int ett_attrib, void *data, struct packet_netlink_data *nl_data, proto_tree *tree, int offset, int length, netlink_attributes_cb_t cb)
348 {
349         DISSECTOR_ASSERT(ett_attrib != -1);
350         return dissect_netlink_attributes_common(tvb, hfi_type, ett_array, ett_attrib, data, nl_data, tree, offset, length, cb);
351 }
352
353 int
354 dissect_netlink_header(tvbuff_t *tvb, proto_tree *tree, int offset, int encoding, header_field_info *hfi_type, proto_item **pi_type)
355 {
356         guint16 hdr_flags;
357         guint16 hdr_type;
358         proto_tree *fh_hdr;
359         proto_item *pi;
360
361         fh_hdr = proto_tree_add_subtree(tree, tvb, offset, 16, ett_netlink_msghdr, NULL, "Netlink message header");
362
363         proto_tree_add_item(fh_hdr, &hfi_netlink_hdr_len, tvb, offset, 4, encoding);
364         offset += 4;
365
366         hdr_type = tvb_get_guint16(tvb, offset, encoding);
367         if (hdr_type < WS_NLMSG_MIN_TYPE) {
368                 /* Reserved control messages. */
369                 hfi_type = &hfi_netlink_hdr_type;
370                 pi = proto_tree_add_item(fh_hdr, hfi_type, tvb, offset, 2, encoding);
371         } else {
372                 if (hfi_type) {
373                         pi = proto_tree_add_item(fh_hdr, hfi_type, tvb, offset, 2, encoding);
374                 } else {
375                         hfi_type = &hfi_netlink_hdr_type;
376                         pi = proto_tree_add_item(fh_hdr, hfi_type, tvb, offset, 2, encoding);
377                         proto_item_set_text(pi, "Message type: Protocol-specific (0x%04x)", hdr_type);
378                 }
379         }
380         if (pi_type) {
381                 *pi_type = pi;
382         }
383         /* TODO export hf_try_val_to_str? */
384         if (hfi_type->strings && hfi_type->display & BASE_EXT_STRING) {
385                 proto_item_append_text(fh_hdr, " (type: %s)", val_to_str_ext(hdr_type, (value_string_ext *)hfi_type->strings, "0x%04x"));
386         } else if (hfi_type->strings) {
387                 proto_item_append_text(fh_hdr, " (type: %s)", val_to_str(hdr_type, (const value_string *)hfi_type->strings, "0x%04x"));
388         } else {
389                 proto_item_append_text(fh_hdr, " (type: 0x%04x)", hdr_type);
390         }
391         offset += 2;
392
393         hdr_flags = tvb_get_guint16(tvb, offset, encoding);
394         if ((hdr_flags & WS_NLM_F_REQUEST) && (hdr_flags & 0x0f00)) {
395                 /* XXX detect based on the protocol family and message type
396                  * whether this is a GET, NEW or regular request. */
397                 proto_tree_add_bitmask(fh_hdr, tvb, offset, hfi_netlink_hdr_flags.id,
398                         ett_netlink_hdr_flags, netlink_header_get_flags, encoding);
399                 proto_tree_add_bitmask(fh_hdr, tvb, offset, hfi_netlink_hdr_flags.id,
400                         ett_netlink_hdr_flags, netlink_header_new_flags, encoding);
401         } else {
402                 proto_tree_add_bitmask(fh_hdr, tvb, offset, hfi_netlink_hdr_flags.id,
403                         ett_netlink_hdr_flags, netlink_header_standard_flags, encoding);
404         }
405
406         offset += 2;
407
408         proto_tree_add_item(fh_hdr, &hfi_netlink_hdr_seq, tvb, offset, 4, encoding);
409         offset += 4;
410
411         proto_tree_add_item(fh_hdr, &hfi_netlink_hdr_pid, tvb, offset, 4, encoding);
412         offset += 4;
413
414         return offset;
415 }
416
417 static void
418 dissect_netlink_error(tvbuff_t *tvb, proto_tree *tree, int offset, int encoding)
419 {
420         /*
421          * XXX - this should make sure we don't run past the end of the
422          * message.
423          */
424
425         /*
426          * Assume sizeof(int) == 4; RFC 3549 doesn't say "32 bits", it
427          * says "integer (typically 32 bits)".
428          */
429         proto_tree_add_item(tree, &hfi_netlink_error, tvb, offset, 4, encoding);
430         offset += 4;
431
432         dissect_netlink_header(tvb, tree, offset, encoding, NULL, NULL);
433 }
434
435 static int
436 dissect_netlink(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *_data _U_)
437 {
438         guint16     protocol, hatype;
439         proto_item *ti;
440         tvbuff_t   *next_tvb;
441         proto_tree *fh_tree;
442
443         int offset;
444         int encoding;
445         guint len_rem, len_le, len_be;
446
447         hatype = tvb_get_ntohs(tvb, 2);
448         if (hatype != ARPHRD_NETLINK)
449                 return 0;
450
451         col_set_str(pinfo->cinfo, COL_PROTOCOL, "Netlink");
452         col_clear(pinfo->cinfo, COL_INFO);
453
454         ti = proto_tree_add_protocol_format(tree, hfi_netlink->id, tvb, 0,
455                         SLL_HEADER_SIZE, "Linux netlink (cooked header)");
456         fh_tree = proto_item_add_subtree(ti, ett_netlink_cooked);
457
458         /* Unused 2B */
459         offset = 2;
460
461         proto_tree_add_item(fh_tree, &hfi_netlink_hatype, tvb, offset, 2, ENC_BIG_ENDIAN);
462         offset += 2;
463
464         /* Unused 10B */
465         offset += 10;
466
467         protocol = tvb_get_ntohs(tvb, offset);
468         proto_tree_add_item(fh_tree, &hfi_netlink_family, tvb, offset, 2, ENC_BIG_ENDIAN);
469         offset += 2;
470
471         /* DISSECTOR_ASSERT(offset == 16); */
472
473         /*
474          * We are unable to detect the endianness, we have to guess. Compare
475          * the size of the inner package with the size of the outer package,
476          * take the endianness in which the inner package length is closer to
477          * the size of the outer package. Normally we have packages with less
478          * than 10KB here so the sizes are very huge in the wrong endianness.
479          */
480         len_rem = tvb_reported_length_remaining(tvb, offset);
481         len_le = tvb_get_letohl(tvb, offset);
482         len_be = tvb_get_ntohl(tvb, offset);
483         #define abs_diff(a, b) ((a) > (b) ? (a) - (b) : (b) - (a))
484         if (abs_diff(len_be, len_rem) < abs_diff(len_le, len_rem)) {
485                 encoding = ENC_BIG_ENDIAN;
486         } else {
487                 encoding = ENC_LITTLE_ENDIAN;
488         }
489
490         while (tvb_reported_length_remaining(tvb, offset) >= 16) {
491                 int pkt_end_offset;
492                 guint16 msg_type;
493                 guint32 pkt_len;
494                 guint32 port_id;
495                 proto_tree *fh_msg;
496                 gboolean dissected = FALSE;
497
498                 pkt_len = tvb_get_guint32(tvb, offset, encoding);
499
500                 pkt_end_offset = offset + pkt_len;
501
502                 if (pkt_len < 16) {
503                         /*
504                          * This field includes the length of the 16-byte header,
505                          * so its value is invalid.  Add it, report an error,
506                          * and stop trying to dissect.
507                          */
508                         proto_tree *fh_hdr;
509
510                         fh_hdr = proto_tree_add_subtree(tree, tvb, offset, 4, ett_netlink_msghdr, NULL, "Netlink message header");
511
512                         proto_tree_add_item(fh_hdr, &hfi_netlink_hdr_len, tvb, offset, 4, encoding);
513                         /* XXX invalid expert */
514                         break;
515                 }
516
517                 /* message type field comes after length field. */
518                 msg_type = tvb_get_guint16(tvb, offset + 4, encoding);
519                 port_id = tvb_get_guint32(tvb, offset + 12, encoding);
520
521                 /* XXX */
522                 if (port_id == 0x00)
523                         pinfo->p2p_dir = P2P_DIR_SENT; /* userspace -> kernel */
524                 else
525                         pinfo->p2p_dir = P2P_DIR_RECV; /* userspace or kernel -> userspace */
526
527                 /*
528                  * Try to invoke subdissectors for non-control messages.
529                  */
530                 if (msg_type >= WS_NLMSG_MIN_TYPE && pkt_len > 16) {
531                         struct packet_netlink_data data;
532
533                         data.magic = PACKET_NETLINK_MAGIC;
534                         data.encoding = encoding;
535                         data.type = msg_type;
536
537                         next_tvb = tvb_new_subset_length(tvb, offset, pkt_len);
538
539                         if (dissector_try_uint_new(netlink_dissector_table, protocol, next_tvb, pinfo, tree, TRUE, &data)) {
540                                 dissected = TRUE;
541                         }
542                 }
543
544                 if (!dissected) {
545                         /*
546                          * No subdissector was called, add a new layer with the
547                          * header and the payload. Note that pkt_len>=16.
548                          */
549                         fh_msg = proto_tree_add_subtree(tree, tvb, offset, pkt_len, ett_netlink_msg, NULL, "Netlink message");
550                         offset = dissect_netlink_header(tvb, fh_msg, offset, encoding, NULL, NULL);
551
552                         if (msg_type == WS_NLMSG_ERROR) {
553                                 dissect_netlink_error(tvb, fh_msg, offset, encoding);
554                         } else if (pkt_len > 16) {
555                                 next_tvb = tvb_new_subset_length(tvb, offset, pkt_len - 16);
556                                 call_data_dissector(next_tvb, pinfo, fh_msg);
557                         }
558                 }
559
560                 offset = pkt_end_offset;
561         }
562
563         return offset;
564 }
565
566 void
567 proto_register_netlink(void)
568 {
569 #ifndef HAVE_HFI_SECTION_INIT
570         static header_field_info *hfi[] = {
571         /* Cooked header */
572                 &hfi_netlink_hatype,
573                 &hfi_netlink_family,
574
575         /* Netlink message header */
576                 &hfi_netlink_hdr_len,
577                 &hfi_netlink_hdr_type,
578                 &hfi_netlink_hdr_flags,
579                 &hfi_netlink_hdr_flag_request,
580                 &hfi_netlink_hdr_flag_multi,
581                 &hfi_netlink_hdr_flag_ack,
582                 &hfi_netlink_hdr_flag_echo,
583                 &hfi_netlink_hdr_flag_dumpintr,
584                 &hfi_netlink_hdr_flag_dumpfiltered,
585
586                 &hfi_netlink_hdr_flag_root,
587                 &hfi_netlink_hdr_flag_match,
588                 &hfi_netlink_hdr_flag_atomic,
589
590                 &hfi_netlink_hdr_flag_replace,
591                 &hfi_netlink_hdr_flag_excl,
592                 &hfi_netlink_hdr_flag_create,
593                 &hfi_netlink_hdr_flag_append,
594
595                 &hfi_netlink_hdr_seq,
596                 &hfi_netlink_hdr_pid,
597
598         /* Netlink message attribute */
599                 &hfi_netlink_attr_len,
600                 &hfi_netlink_attr_type,
601                 &hfi_netlink_attr_type_nested,
602                 &hfi_netlink_attr_type_net_byteorder,
603                 &hfi_netlink_attr_index,
604                 &hfi_netlink_attr_data,
605
606         /* Netlink message payloads */
607                 &hfi_netlink_error,
608         };
609 #endif
610
611         static gint *ett[] = {
612                 &ett_netlink_cooked,
613                 &ett_netlink_msghdr,
614                 &ett_netlink_msg,
615                 &ett_netlink_hdr_flags,
616                 &ett_netlink_attr_type,
617         };
618
619         int proto_netlink;
620
621         proto_netlink = proto_register_protocol("Linux netlink protocol",  "NETLINK", "netlink" );
622         hfi_netlink = proto_registrar_get_nth(proto_netlink);
623
624         proto_register_fields(proto_netlink, hfi, array_length(hfi));
625         proto_register_subtree_array(ett, array_length(ett));
626
627         netlink_handle = create_dissector_handle(dissect_netlink, proto_netlink);
628
629         netlink_dissector_table = register_dissector_table(
630                 "netlink.protocol",
631                 "Linux netlink protocol type",
632                 proto_netlink, FT_UINT16,
633                 BASE_HEX
634         );
635         register_dissector("netlink", dissect_netlink, proto_netlink);
636 }
637
638 void
639 proto_reg_handoff_netlink(void)
640 {
641         dissector_add_uint("wtap_encap", WTAP_ENCAP_NETLINK, netlink_handle);
642 }
643
644 /*
645  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
646  *
647  * Local variables:
648  * c-basic-offset: 8
649  * tab-width: 8
650  * indent-tabs-mode: t
651  * End:
652  *
653  * vi: set shiftwidth=8 tabstop=8 noexpandtab:
654  * :indentSize=8:tabSize=8:noTabs=false:
655  */