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 
32 #ifdef HAVE_STRUCT_BR_PORT_MSG
33 
34 # include "netlink_route.h"
35 # include "nlattr.h"
36 # include "print_fields.h"
37 
38 # include <netinet/in.h>
39 # include <linux/if_bridge.h>
40 # include "netlink.h"
41 
42 # ifdef HAVE_STRUCT_BR_MDB_ENTRY_FLAGS
43 #  include "xlat/mdb_flags.h"
44 # endif
45 # include "xlat/mdb_states.h"
46 # include "xlat/multicast_router_types.h"
47 # include "xlat/rtnl_mdb_attrs.h"
48 # include "xlat/rtnl_mdba_mdb_attrs.h"
49 # include "xlat/rtnl_mdba_mdb_eattr_attrs.h"
50 # include "xlat/rtnl_mdba_mdb_entry_attrs.h"
51 # include "xlat/rtnl_mdba_router_attrs.h"
52 # include "xlat/rtnl_mdba_router_pattr_attrs.h"
53 
54 static const nla_decoder_t mdba_mdb_eattr_nla_decoders[] = {
55 	[MDBA_MDB_EATTR_TIMER]	= decode_nla_u32
56 };
57 
58 static bool
decode_mdba_mdb_entry_info(struct tcb * const tcp,const kernel_ulong_t addr,const unsigned int len,const void * const opaque_data)59 decode_mdba_mdb_entry_info(struct tcb *const tcp,
60 			   const kernel_ulong_t addr,
61 			   const unsigned int len,
62 			   const void *const opaque_data)
63 {
64 # ifdef HAVE_STRUCT_BR_MDB_ENTRY
65 	struct br_mdb_entry entry;
66 
67 	if (len < sizeof(entry))
68 		return false;
69 	else if (!umove_or_printaddr(tcp, addr, &entry)) {
70 		PRINT_FIELD_IFINDEX("{", entry, ifindex);
71 		PRINT_FIELD_XVAL(", ", entry, state, mdb_states, "MDB_???");
72 #  ifdef HAVE_STRUCT_BR_MDB_ENTRY_FLAGS
73 		PRINT_FIELD_FLAGS(", ", entry, flags,
74 				  mdb_flags, "MDB_FLAGS_???");
75 #  endif
76 #  ifdef HAVE_STRUCT_BR_MDB_ENTRY_VID
77 		PRINT_FIELD_U(", ", entry, vid);
78 #  endif
79 
80 		const int proto = ntohs(entry.addr.proto);
81 
82 		tprints(", addr={");
83 		print_inet_addr(proto, &entry.addr.u,
84 				sizeof(entry.addr.u), "u");
85 		tprints(", proto=htons(");
86 		printxval(addrfams, proto, "AF_???");
87 		tprints(")}}");
88 	}
89 
90 	const size_t offset = NLMSG_ALIGN(sizeof(entry));
91 	if (len > offset) {
92 		tprints(", ");
93 		decode_nlattr(tcp, addr + offset, len - offset,
94 			      rtnl_mdba_mdb_eattr_attrs, "MDBA_MDB_EATTR_???",
95 			      mdba_mdb_eattr_nla_decoders,
96 			      ARRAY_SIZE(mdba_mdb_eattr_nla_decoders), NULL);
97 	}
98 
99 	return true;
100 # else
101 	return false;
102 # endif /* HAVE_STRUCT_BR_MDB_ENTRY */
103 }
104 
105 static const nla_decoder_t mdba_mdb_entry_nla_decoders[] = {
106 	[MDBA_MDB_ENTRY_INFO]	= decode_mdba_mdb_entry_info
107 };
108 
109 static bool
decode_mdba_mdb_entry(struct tcb * const tcp,const kernel_ulong_t addr,const unsigned int len,const void * const opaque_data)110 decode_mdba_mdb_entry(struct tcb *const tcp,
111 		      const kernel_ulong_t addr,
112 		      const unsigned int len,
113 		      const void *const opaque_data)
114 {
115 	decode_nlattr(tcp, addr, len, rtnl_mdba_mdb_entry_attrs,
116 		      "MDBA_MDB_ENTRY_???", mdba_mdb_entry_nla_decoders,
117 		      ARRAY_SIZE(mdba_mdb_entry_nla_decoders), NULL);
118 
119 	return true;
120 }
121 
122 static const nla_decoder_t mdba_mdb_nla_decoders[] = {
123 	[MDBA_MDB_ENTRY]	= decode_mdba_mdb_entry
124 };
125 
126 static bool
decode_mdba_mdb(struct tcb * const tcp,const kernel_ulong_t addr,const unsigned int len,const void * const opaque_data)127 decode_mdba_mdb(struct tcb *const tcp,
128 		const kernel_ulong_t addr,
129 		const unsigned int len,
130 		const void *const opaque_data)
131 {
132 	decode_nlattr(tcp, addr, len, rtnl_mdba_mdb_attrs, "MDBA_MDB_???",
133 		      mdba_mdb_nla_decoders,
134 		      ARRAY_SIZE(mdba_mdb_nla_decoders), NULL);
135 
136 	return true;
137 }
138 
139 static bool
decode_multicast_router_type(struct tcb * const tcp,const kernel_ulong_t addr,const unsigned int len,const void * const opaque_data)140 decode_multicast_router_type(struct tcb *const tcp,
141 			     const kernel_ulong_t addr,
142 			     const unsigned int len,
143 			     const void *const opaque_data)
144 {
145 	uint8_t type;
146 
147 	if (!umove_or_printaddr(tcp, addr, &type))
148 		printxval(multicast_router_types, type, "MDB_RTR_TYPE_???");
149 
150 	return true;
151 }
152 
153 static const nla_decoder_t mdba_router_pattr_nla_decoders[] = {
154 	[MDBA_ROUTER_PATTR_TIMER]	= decode_nla_u32,
155 	[MDBA_ROUTER_PATTR_TYPE]	= decode_multicast_router_type
156 };
157 
158 static bool
decode_mdba_router_port(struct tcb * const tcp,const kernel_ulong_t addr,const unsigned int len,const void * const opaque_data)159 decode_mdba_router_port(struct tcb *const tcp,
160 			const kernel_ulong_t addr,
161 			const unsigned int len,
162 			const void *const opaque_data)
163 {
164 	uint32_t ifindex;
165 
166 	if (len < sizeof(ifindex))
167 		return false;
168 	else if (!umove_or_printaddr(tcp, addr, &ifindex))
169 		print_ifindex(ifindex);
170 
171 	const size_t offset = NLMSG_ALIGN(sizeof(ifindex));
172 	if (len > offset) {
173 		tprints(", ");
174 		decode_nlattr(tcp, addr + offset, len - offset,
175 			      rtnl_mdba_router_pattr_attrs,
176 			      "MDBA_ROUTER_PATTR_???",
177 			      mdba_router_pattr_nla_decoders,
178 			      ARRAY_SIZE(mdba_router_pattr_nla_decoders), NULL);
179 	}
180 
181 	return true;
182 }
183 
184 static const nla_decoder_t mdba_router_nla_decoders[] = {
185 	[MDBA_ROUTER_PORT]	= decode_mdba_router_port
186 };
187 
188 static bool
decode_mdba_router(struct tcb * const tcp,const kernel_ulong_t addr,const unsigned int len,const void * const opaque_data)189 decode_mdba_router(struct tcb *const tcp,
190 		   const kernel_ulong_t addr,
191 		   const unsigned int len,
192 		   const void *const opaque_data)
193 {
194 	decode_nlattr(tcp, addr, len, rtnl_mdba_router_attrs, "MDBA_ROUTER_???",
195 		      mdba_router_nla_decoders,
196 		      ARRAY_SIZE(mdba_router_nla_decoders), NULL);
197 
198 	return true;
199 }
200 
201 static const nla_decoder_t br_port_msg_nla_decoders[] = {
202 	[MDBA_MDB]	= decode_mdba_mdb,
203 	[MDBA_ROUTER]	= decode_mdba_router
204 };
205 
DECL_NETLINK_ROUTE_DECODER(decode_br_port_msg)206 DECL_NETLINK_ROUTE_DECODER(decode_br_port_msg)
207 {
208 	struct br_port_msg bpm = { .family = family };
209 	size_t offset = sizeof(bpm.family);
210 	bool decode_nla = false;
211 
212 	PRINT_FIELD_XVAL("{", bpm, family, addrfams, "AF_???");
213 
214 	tprints(", ");
215 	if (len >= sizeof(bpm)) {
216 		if (!umoven_or_printaddr(tcp, addr + offset,
217 					 sizeof(bpm) - offset,
218 					 (char *) &bpm + offset)) {
219 			PRINT_FIELD_IFINDEX("", bpm, ifindex);
220 			decode_nla = true;
221 		}
222 	} else
223 		tprints("...");
224 	tprints("}");
225 
226 	offset = NLMSG_ALIGN(sizeof(bpm));
227 	if (decode_nla && len > offset) {
228 		tprints(", ");
229 		decode_nlattr(tcp, addr + offset, len - offset,
230 			      rtnl_mdb_attrs, "MDBA_???",
231 			      br_port_msg_nla_decoders,
232 			      ARRAY_SIZE(br_port_msg_nla_decoders), NULL);
233 	}
234 }
235 
236 #endif
237