3 * Wireshark - Network traffic analyzer
4 * By Gerald Combs <gerald@wireshark.org>
5 * Copyright 1998 Gerald Combs
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.
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.
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.
22 /* http://www.tcpdump.org/linktypes/LINKTYPE_NETLINK.html */
24 #define NEW_PROTO_TREE_API
28 #include <epan/packet.h>
29 #include <epan/arptypes.h>
30 #include <wiretap/wtap.h>
32 #include "packet-netlink.h"
34 void proto_register_netlink(void);
35 void proto_reg_handoff_netlink(void);
38 * A DLT_LINUX_SLL fake link-layer header.
40 #define SLL_HEADER_SIZE 16 /* total header length */
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" },
67 value_string_ext netlink_family_vals_ext = VALUE_STRING_EXT_INIT(netlink_family_vals);
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" },
77 static const value_string ha_types[] = {
78 { ARPHRD_NETLINK, "Netlink" },
82 static dissector_handle_t netlink_handle;
84 static header_field_info *hfi_netlink = NULL;
86 #define NETLINK_HFI_INIT HFI_INIT(proto_netlink)
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 };
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 };
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 };
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 };
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 };
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 };
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 };
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 };
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 };
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 };
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 };
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 };
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 };
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 };
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 };
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 };
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 };
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 };
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 };
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 };
169 static header_field_info hfi_netlink_attr_type NETLINK_HFI_INIT =
170 { "Type", "netlink.attr_type", FT_UINT16, BASE_HEX,
171 NULL, 0x0000, "Netlink Attribute type", HFILL };
173 static header_field_info hfi_netlink_attr_type_nested NETLINK_HFI_INIT =
174 { "Nested", "netlink.attr_type.nested", FT_UINT16, BASE_DEC,
175 NULL, NLA_F_NESTED, "Carries nested attributes", HFILL };
177 static header_field_info hfi_netlink_attr_type_net_byteorder NETLINK_HFI_INIT =
178 { "Network byte order", "netlink.attr_type.net_byteorder", FT_UINT16, BASE_DEC,
179 NULL, NLA_F_NET_BYTEORDER, "Payload stored in network byte order", HFILL };
181 static header_field_info hfi_netlink_attr_len NETLINK_HFI_INIT =
182 { "Len", "netlink.attr_len", FT_UINT16, BASE_DEC,
183 NULL, 0x00, NULL, HFILL };
185 /* TODO add a value_string for errno. */
186 static header_field_info hfi_netlink_error NETLINK_HFI_INIT =
187 { "Error code", "netlink.error", FT_INT32, BASE_DEC,
188 NULL, 0x00, "Negative errno or 0 for acknowledgements", HFILL };
190 static gint ett_netlink_cooked = -1;
191 static gint ett_netlink_msghdr = -1;
192 static gint ett_netlink_msg = -1;
193 static gint ett_netlink_hdr_flags = -1;
194 static gint ett_netlink_attr_type = -1;
196 static dissector_table_t netlink_dissector_table;
199 static const int *netlink_header_get_flags[] = {
200 &hfi_netlink_hdr_flag_request.id,
201 &hfi_netlink_hdr_flag_multi.id,
202 &hfi_netlink_hdr_flag_ack.id,
203 &hfi_netlink_hdr_flag_echo.id,
204 &hfi_netlink_hdr_flag_dumpintr.id,
205 &hfi_netlink_hdr_flag_dumpfiltered.id,
207 &hfi_netlink_hdr_flag_root.id,
208 &hfi_netlink_hdr_flag_match.id,
209 &hfi_netlink_hdr_flag_atomic.id,
213 static const int *netlink_header_new_flags[] = {
214 &hfi_netlink_hdr_flag_request.id,
215 &hfi_netlink_hdr_flag_multi.id,
216 &hfi_netlink_hdr_flag_ack.id,
217 &hfi_netlink_hdr_flag_echo.id,
218 &hfi_netlink_hdr_flag_dumpintr.id,
219 &hfi_netlink_hdr_flag_dumpfiltered.id,
221 &hfi_netlink_hdr_flag_replace.id,
222 &hfi_netlink_hdr_flag_excl.id,
223 &hfi_netlink_hdr_flag_create.id,
224 &hfi_netlink_hdr_flag_append.id,
228 static const int *netlink_header_standard_flags[] = {
229 &hfi_netlink_hdr_flag_request.id,
230 &hfi_netlink_hdr_flag_multi.id,
231 &hfi_netlink_hdr_flag_ack.id,
232 &hfi_netlink_hdr_flag_echo.id,
233 &hfi_netlink_hdr_flag_dumpintr.id,
234 &hfi_netlink_hdr_flag_dumpfiltered.id,
240 dissect_netlink_attributes(tvbuff_t *tvb, header_field_info *hfi_type, int ett, void *data, proto_tree *tree, int offset, int length, netlink_attributes_cb_t cb)
242 int padding = (4 - offset) & 3;
247 length = tvb_captured_length_remaining(tvb, offset);
252 while (length >= 4) {
253 guint16 rta_len, rta_type, type;
255 proto_item *ti, *type_item;
256 proto_tree *attr_tree, *type_tree;
258 rta_len = tvb_get_letohs(tvb, offset);
260 /* XXX invalid expert */
264 /* XXX expert info when rta_len < length? */
265 rta_len = MIN(rta_len, length);
267 attr_tree = proto_tree_add_subtree(tree, tvb, offset, rta_len, ett, &ti, "Attribute");
269 proto_tree_add_item(attr_tree, &hfi_netlink_attr_len, tvb, offset, 2, ENC_LITTLE_ENDIAN);
272 rta_type = tvb_get_letohs(tvb, offset);
273 type = rta_type & NLA_TYPE_MASK;
274 type_item = proto_tree_add_item(attr_tree, &hfi_netlink_attr_type, tvb, offset, 2, ENC_LITTLE_ENDIAN);
275 type_tree = proto_item_add_subtree(type_item, ett_netlink_attr_type);
276 proto_tree_add_item(type_tree, &hfi_netlink_attr_type_nested, tvb, offset, 2, ENC_LITTLE_ENDIAN);
277 proto_tree_add_item(type_tree, &hfi_netlink_attr_type_net_byteorder, tvb, offset, 2, ENC_LITTLE_ENDIAN);
278 proto_tree_add_item(type_tree, hfi_type, tvb, offset, 2, ENC_LITTLE_ENDIAN);
281 if (rta_type & NLA_F_NESTED)
282 proto_item_append_text(type_item, ", Nested");
284 if (hfi_type->strings) {
285 /* XXX, export hf_try_val_to_str */
286 const char *rta_str = try_val_to_str(type, (const value_string *) hfi_type->strings);
289 proto_item_append_text(type_item, ", %s (%d)", rta_str, type);
290 proto_item_append_text(ti, ": %s", rta_str);
294 if (!cb(tvb, data, attr_tree, rta_type, offset, rta_len - 4)) {
298 /* Assume offset already aligned, next offset is rta_len plus alignment. */
299 rta_len = MIN((rta_len + 3) & ~3, length);
300 offset += rta_len - 4; /* Header was already skipped */
308 dissect_netlink_hdr(tvbuff_t *tvb, proto_tree *tree, int offset, int encoding, guint16 *type, guint32 *port_id)
316 fh_hdr = proto_tree_add_subtree(tree, tvb, offset, 16, ett_netlink_msghdr, NULL, "Header");
318 proto_tree_add_item(fh_hdr, &hfi_netlink_hdr_len, tvb, offset, 4, encoding);
321 pi_type = proto_tree_add_item(fh_hdr, &hfi_netlink_hdr_type, tvb, offset, 2, encoding);
322 hdr_type = tvb_get_letohs(tvb, offset);
323 if (hdr_type >= WS_NLMSG_MIN_TYPE) {
324 proto_item_set_text(pi_type, "Message type: Protocol-specific (0x%04x)", hdr_type);
331 hdr_flags = tvb_get_guint16(tvb, offset, encoding);
332 if ((hdr_flags & WS_NLM_F_REQUEST) && (hdr_flags & 0x0f00)) {
333 /* XXX detect based on the protocol family and message type
334 * whether this is a GET, NEW or regular request. */
335 proto_tree_add_bitmask(fh_hdr, tvb, offset, hfi_netlink_hdr_flags.id,
336 ett_netlink_hdr_flags, netlink_header_get_flags, encoding);
337 proto_tree_add_bitmask(fh_hdr, tvb, offset, hfi_netlink_hdr_flags.id,
338 ett_netlink_hdr_flags, netlink_header_new_flags, encoding);
340 proto_tree_add_bitmask(fh_hdr, tvb, offset, hfi_netlink_hdr_flags.id,
341 ett_netlink_hdr_flags, netlink_header_standard_flags, encoding);
346 proto_tree_add_item(fh_hdr, &hfi_netlink_hdr_seq, tvb, offset, 4, encoding);
349 proto_tree_add_item_ret_uint(fh_hdr, hfi_netlink_hdr_pid.id, tvb, offset, 4, encoding, port_id);
356 dissect_netlink_error(tvbuff_t *tvb, proto_tree *tree, int offset, int encoding)
359 * XXX - this should make sure we don't run past the end of the
364 * Assume sizeof(int) == 4; RFC 3549 doesn't say "32 bits", it
365 * says "integer (typically 32 bits)".
367 proto_tree_add_item(tree, &hfi_netlink_error, tvb, offset, 4, encoding);
370 dissect_netlink_hdr(tvb, tree, offset, encoding, NULL, NULL);
374 dissect_netlink(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *_data _U_)
376 guint16 protocol, hatype;
383 hatype = tvb_get_ntohs(tvb, 2);
384 if (hatype != ARPHRD_NETLINK)
387 col_set_str(pinfo->cinfo, COL_PROTOCOL, "Netlink");
388 col_clear(pinfo->cinfo, COL_INFO);
390 ti = proto_tree_add_protocol_format(tree, hfi_netlink->id, tvb, 0,
391 SLL_HEADER_SIZE, "Linux netlink (cooked header)");
392 fh_tree = proto_item_add_subtree(ti, ett_netlink_cooked);
397 proto_tree_add_item(fh_tree, &hfi_netlink_hatype, tvb, offset, 2, ENC_BIG_ENDIAN);
403 protocol = tvb_get_ntohs(tvb, offset);
404 proto_tree_add_item(fh_tree, &hfi_netlink_family, tvb, offset, 2, ENC_BIG_ENDIAN);
407 /* DISSECTOR_ASSERT(offset == 16); */
409 while (tvb_reported_length_remaining(tvb, offset) >= 16) {
410 struct packet_netlink_data data;
419 int encoding = ENC_LITTLE_ENDIAN; /* XXX */
421 pkt_len = tvb_get_letohl(tvb, offset);
423 pkt_end_offset = offset + pkt_len;
425 fh_msg = proto_tree_add_subtree(tree, tvb, offset, pkt_len, ett_netlink_msg, NULL, "Netlink message");
429 * This field includes the length of the 16-byte header,
430 * so its value is invalid. Add it, report an error,
431 * and stop trying to dissect.
435 fh_hdr = proto_tree_add_subtree(tree, tvb, offset, 4, ett_netlink_msghdr, NULL, "Header");
437 proto_tree_add_item(fh_hdr, &hfi_netlink_hdr_len, tvb, offset, 4, encoding);
438 /* XXX invalid expert */
442 offset = dissect_netlink_hdr(tvb, fh_msg, offset, encoding, &data.type, &port_id);
446 pinfo->p2p_dir = P2P_DIR_SENT; /* userspace -> kernel */
448 pinfo->p2p_dir = P2P_DIR_RECV; /* userspace or kernel -> userspace */
450 if (data.type == WS_NLMSG_ERROR) {
451 dissect_netlink_error(tvb, fh_msg, offset, encoding);
452 } else if (pkt_len > 16) {
453 data.magic = PACKET_NETLINK_MAGIC;
454 data.encoding = encoding;
456 next_tvb = tvb_new_subset_length(tvb, offset, pkt_len-16);
458 if (!dissector_try_uint_new(netlink_dissector_table, protocol, next_tvb, pinfo, fh_msg, TRUE, &data))
459 call_data_dissector(next_tvb, pinfo, fh_msg);
463 offset = pkt_end_offset;
470 proto_register_netlink(void)
472 #ifndef HAVE_HFI_SECTION_INIT
473 static header_field_info *hfi[] = {
478 /* Netlink message header */
479 &hfi_netlink_hdr_len,
480 &hfi_netlink_hdr_type,
481 &hfi_netlink_hdr_flags,
482 &hfi_netlink_hdr_flag_request,
483 &hfi_netlink_hdr_flag_multi,
484 &hfi_netlink_hdr_flag_ack,
485 &hfi_netlink_hdr_flag_echo,
486 &hfi_netlink_hdr_flag_dumpintr,
487 &hfi_netlink_hdr_flag_dumpfiltered,
489 &hfi_netlink_hdr_flag_root,
490 &hfi_netlink_hdr_flag_match,
491 &hfi_netlink_hdr_flag_atomic,
493 &hfi_netlink_hdr_flag_replace,
494 &hfi_netlink_hdr_flag_excl,
495 &hfi_netlink_hdr_flag_create,
496 &hfi_netlink_hdr_flag_append,
498 &hfi_netlink_hdr_seq,
499 &hfi_netlink_hdr_pid,
501 /* Netlink message attribute */
502 &hfi_netlink_attr_len,
503 &hfi_netlink_attr_type,
504 &hfi_netlink_attr_type_nested,
505 &hfi_netlink_attr_type_net_byteorder,
507 /* Netlink message payloads */
512 static gint *ett[] = {
516 &ett_netlink_hdr_flags,
517 &ett_netlink_attr_type,
522 proto_netlink = proto_register_protocol("Linux netlink protocol", "NETLINK", "netlink" );
523 hfi_netlink = proto_registrar_get_nth(proto_netlink);
525 proto_register_fields(proto_netlink, hfi, array_length(hfi));
526 proto_register_subtree_array(ett, array_length(ett));
528 netlink_handle = create_dissector_handle(dissect_netlink, proto_netlink);
530 netlink_dissector_table = register_dissector_table(
532 "Linux netlink protocol type",
533 proto_netlink, FT_UINT16,
536 register_dissector("netlink", dissect_netlink, proto_netlink);
540 proto_reg_handoff_netlink(void)
542 dissector_add_uint("wtap_encap", WTAP_ENCAP_NETLINK, netlink_handle);
546 * Editor modelines - http://www.wireshark.org/tools/modelines.html
551 * indent-tabs-mode: t
554 * vi: set shiftwidth=8 tabstop=8 noexpandtab:
555 * :indentSize=8:tabSize=8:noTabs=false: