1 /*
2  * Copyright (c) 2016 Fabien Siron <fabien.siron@epita.fr>
3  * Copyright (c) 2017 JingPiao Chen <chenjingpiao@gmail.com>
4  * Copyright (c) 2016-2018 The strace developers.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. The name of the author may not be used to endorse or promote products
16  *    derived from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #include "defs.h"
31 #include "netlink_route.h"
32 #include "nlattr.h"
33 #include "print_fields.h"
34 
35 #include "netlink.h"
36 #include <linux/rtnetlink.h>
37 #ifdef HAVE_LINUX_NEIGHBOUR_H
38 # include <linux/neighbour.h>
39 #endif
40 
41 #include "xlat/neighbor_cache_entry_flags.h"
42 #include "xlat/neighbor_cache_entry_states.h"
43 #include "xlat/rtnl_neigh_attrs.h"
44 
45 static bool
decode_neigh_addr(struct tcb * const tcp,const kernel_ulong_t addr,const unsigned int len,const void * const opaque_data)46 decode_neigh_addr(struct tcb *const tcp,
47 		  const kernel_ulong_t addr,
48 		  const unsigned int len,
49 		  const void *const opaque_data)
50 {
51 	const struct ndmsg *const ndmsg = opaque_data;
52 
53 	decode_inet_addr(tcp, addr, len, ndmsg->ndm_family, NULL);
54 
55 	return true;
56 }
57 
58 static bool
decode_nda_cacheinfo(struct tcb * const tcp,const kernel_ulong_t addr,const unsigned int len,const void * const opaque_data)59 decode_nda_cacheinfo(struct tcb *const tcp,
60 		     const kernel_ulong_t addr,
61 		     const unsigned int len,
62 		     const void *const opaque_data)
63 {
64 	struct nda_cacheinfo ci;
65 
66 	if (len < sizeof(ci))
67 		return false;
68 	else if (!umove_or_printaddr(tcp, addr, &ci)) {
69 		PRINT_FIELD_U("{", ci, ndm_confirmed);
70 		PRINT_FIELD_U(", ", ci, ndm_used);
71 		PRINT_FIELD_U(", ", ci, ndm_updated);
72 		PRINT_FIELD_U(", ", ci, ndm_refcnt);
73 		tprints("}");
74 	}
75 
76 	return true;
77 }
78 
79 static const nla_decoder_t ndmsg_nla_decoders[] = {
80 	[NDA_DST]		= decode_neigh_addr,
81 	[NDA_LLADDR]		= decode_neigh_addr,
82 	[NDA_CACHEINFO]		= decode_nda_cacheinfo,
83 	[NDA_PROBES]		= decode_nla_u32,
84 	[NDA_VLAN]		= decode_nla_u16,
85 	[NDA_PORT]		= decode_nla_be16,
86 	[NDA_VNI]		= decode_nla_u32,
87 	[NDA_IFINDEX]		= decode_nla_ifindex,
88 	[NDA_MASTER]		= decode_nla_ifindex,
89 	[NDA_LINK_NETNSID]	= decode_nla_u32,
90 	[NDA_SRC_VNI]		= NULL,
91 };
92 
DECL_NETLINK_ROUTE_DECODER(decode_ndmsg)93 DECL_NETLINK_ROUTE_DECODER(decode_ndmsg)
94 {
95 	struct ndmsg ndmsg = { .ndm_family = family };
96 	size_t offset = sizeof(ndmsg.ndm_family);
97 	bool decode_nla = false;
98 
99 	PRINT_FIELD_XVAL("{", ndmsg, ndm_family, addrfams, "AF_???");
100 
101 	tprints(", ");
102 	if (len >= sizeof(ndmsg)) {
103 		if (!umoven_or_printaddr(tcp, addr + offset,
104 					 sizeof(ndmsg) - offset,
105 					 (char *) &ndmsg + offset)) {
106 			PRINT_FIELD_IFINDEX("", ndmsg, ndm_ifindex);
107 			PRINT_FIELD_FLAGS(", ", ndmsg, ndm_state,
108 					  neighbor_cache_entry_states,
109 					  "NUD_???");
110 			PRINT_FIELD_FLAGS(", ", ndmsg, ndm_flags,
111 					  neighbor_cache_entry_flags,
112 					  "NTF_???");
113 			PRINT_FIELD_XVAL(", ", ndmsg, ndm_type,
114 					 routing_types, "RTN_???");
115 			decode_nla = true;
116 		}
117 	} else
118 		tprints("...");
119 	tprints("}");
120 
121 	offset = NLMSG_ALIGN(sizeof(ndmsg));
122 	if (decode_nla && len > offset) {
123 		tprints(", ");
124 		decode_nlattr(tcp, addr + offset, len - offset,
125 			      rtnl_neigh_attrs, "NDA_???",
126 			      ndmsg_nla_decoders,
127 			      ARRAY_SIZE(ndmsg_nla_decoders), &ndmsg);
128 	}
129 }
130 
DECL_NETLINK_ROUTE_DECODER(decode_rtm_getneigh)131 DECL_NETLINK_ROUTE_DECODER(decode_rtm_getneigh)
132 {
133 	if (family == AF_BRIDGE)
134 		decode_ifinfomsg(tcp, nlmsghdr, family, addr, len);
135 	else
136 		decode_ndmsg(tcp, nlmsghdr, family, addr, len);
137 }
138