• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2016 Fabien Siron <fabien.siron@epita.fr>
3  * Copyright (c) 2017 JingPiao Chen <chenjingpiao@gmail.com>
4  * Copyright (c) 2017-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.h"
32 #include "netlink_sock_diag.h"
33 #include "nlattr.h"
34 #include "print_fields.h"
35 
36 #include <linux/filter.h>
37 #include <linux/sock_diag.h>
38 #include <linux/packet_diag.h>
39 
40 #include "xlat/af_packet_versions.h"
41 #include "xlat/packet_diag_attrs.h"
42 #include "xlat/packet_diag_info_flags.h"
43 #include "xlat/packet_diag_show.h"
44 
DECL_NETLINK_DIAG_DECODER(decode_packet_diag_req)45 DECL_NETLINK_DIAG_DECODER(decode_packet_diag_req)
46 {
47 	struct packet_diag_req req = { .sdiag_family = family };
48 	const size_t offset = sizeof(req.sdiag_family);
49 
50 	PRINT_FIELD_XVAL("{", req, sdiag_family, addrfams, "AF_???");
51 	tprints(", ");
52 	if (len >= sizeof(req)) {
53 		if (!umoven_or_printaddr(tcp, addr + offset,
54 					 sizeof(req) - offset,
55 					 (char *) &req + offset)) {
56 			/*
57 			 * AF_PACKET currently doesn't support protocol values
58 			 * other than 0.
59 			 */
60 			PRINT_FIELD_X("", req, sdiag_protocol);
61 			PRINT_FIELD_U(", ", req, pdiag_ino);
62 			PRINT_FIELD_FLAGS(", ", req, pdiag_show,
63 					  packet_diag_show, "PACKET_SHOW_???");
64 			PRINT_FIELD_COOKIE(", ", req, pdiag_cookie);
65 		}
66 	} else
67 		tprints("...");
68 	tprints("}");
69 }
70 
71 static bool
decode_packet_diag_info(struct tcb * const tcp,const kernel_ulong_t addr,const unsigned int len,const void * const opaque_data)72 decode_packet_diag_info(struct tcb *const tcp,
73 			const kernel_ulong_t addr,
74 			const unsigned int len,
75 			const void *const opaque_data)
76 {
77 	struct packet_diag_info pinfo;
78 
79 	if (len < sizeof(pinfo))
80 		return false;
81 	if (umove_or_printaddr(tcp, addr, &pinfo))
82 		return true;
83 
84 	PRINT_FIELD_IFINDEX("{", pinfo, pdi_index);
85 	PRINT_FIELD_XVAL(", ", pinfo, pdi_version, af_packet_versions,
86 			 "TPACKET_???");
87 	PRINT_FIELD_U(", ", pinfo, pdi_reserve);
88 	PRINT_FIELD_U(", ", pinfo, pdi_copy_thresh);
89 	PRINT_FIELD_U(", ", pinfo, pdi_tstamp);
90 	PRINT_FIELD_FLAGS(", ", pinfo, pdi_flags,
91 			  packet_diag_info_flags, "PDI_???");
92 	tprints("}");
93 
94 	return true;
95 }
96 
97 static bool
print_packet_diag_mclist(struct tcb * const tcp,void * const elem_buf,const size_t elem_size,void * const opaque_data)98 print_packet_diag_mclist(struct tcb *const tcp, void *const elem_buf,
99 			 const size_t elem_size, void *const opaque_data)
100 {
101 	struct packet_diag_mclist *dml = elem_buf;
102 	uint16_t alen = MIN(dml->pdmc_alen, sizeof(dml->pdmc_addr));
103 
104 	PRINT_FIELD_IFINDEX("{", *dml, pdmc_index);
105 	PRINT_FIELD_U(", ", *dml, pdmc_count);
106 	PRINT_FIELD_U(", ", *dml, pdmc_type);
107 	PRINT_FIELD_U(", ", *dml, pdmc_alen);
108 	PRINT_FIELD_STRING(", ", *dml, pdmc_addr, alen, QUOTE_FORCE_HEX);
109 	tprints("}");
110 
111 	return true;
112 }
113 
114 static bool
decode_packet_diag_mclist(struct tcb * const tcp,const kernel_ulong_t addr,const unsigned int len,const void * const opaque_data)115 decode_packet_diag_mclist(struct tcb *const tcp,
116 			  const kernel_ulong_t addr,
117 			  const unsigned int len,
118 			  const void *const opaque_data)
119 {
120 	struct packet_diag_mclist dml;
121 	const size_t nmemb = len / sizeof(dml);
122 
123 	if (!nmemb)
124 		return false;
125 
126 	print_array(tcp, addr, nmemb, &dml, sizeof(dml),
127 		    tfetch_mem, print_packet_diag_mclist, 0);
128 
129 	return true;
130 }
131 
132 static bool
decode_packet_diag_ring(struct tcb * const tcp,const kernel_ulong_t addr,const unsigned int len,const void * const opaque_data)133 decode_packet_diag_ring(struct tcb *const tcp,
134 			const kernel_ulong_t addr,
135 			const unsigned int len,
136 			const void *const opaque_data)
137 {
138 	struct packet_diag_ring pdr;
139 
140 	if (len < sizeof(pdr))
141 		return false;
142 	if (umove_or_printaddr(tcp, addr, &pdr))
143 		return true;
144 
145 	PRINT_FIELD_U("{", pdr, pdr_block_size);
146 	PRINT_FIELD_U(", ", pdr, pdr_block_nr);
147 	PRINT_FIELD_U(", ", pdr, pdr_frame_size);
148 	PRINT_FIELD_U(", ", pdr, pdr_frame_nr);
149 	PRINT_FIELD_U(", ", pdr, pdr_retire_tmo);
150 	PRINT_FIELD_U(", ", pdr, pdr_sizeof_priv);
151 	PRINT_FIELD_U(", ", pdr, pdr_features);
152 	tprints("}");
153 
154 	return true;
155 }
156 
157 static bool
decode_packet_diag_filter(struct tcb * const tcp,const kernel_ulong_t addr,const unsigned int len,const void * const opaque_data)158 decode_packet_diag_filter(struct tcb *const tcp,
159 			  const kernel_ulong_t addr,
160 			  const unsigned int len,
161 			  const void *const opaque_data)
162 {
163 	const unsigned int nmemb = len / sizeof(struct sock_filter);
164 	if (!nmemb || (unsigned short) nmemb != nmemb)
165 		return false;
166 
167 	print_sock_fprog(tcp, addr, nmemb);
168 
169 	return true;
170 }
171 
172 static const nla_decoder_t packet_diag_msg_nla_decoders[] = {
173 	[PACKET_DIAG_INFO]	= decode_packet_diag_info,
174 	[PACKET_DIAG_MCLIST]	= decode_packet_diag_mclist,
175 	[PACKET_DIAG_RX_RING]	= decode_packet_diag_ring,
176 	[PACKET_DIAG_TX_RING]	= decode_packet_diag_ring,
177 	[PACKET_DIAG_FANOUT]	= decode_nla_u32,
178 	[PACKET_DIAG_UID]	= decode_nla_uid,
179 	[PACKET_DIAG_MEMINFO]	= decode_nla_meminfo,
180 	[PACKET_DIAG_FILTER]	= decode_packet_diag_filter
181 };
182 
DECL_NETLINK_DIAG_DECODER(decode_packet_diag_msg)183 DECL_NETLINK_DIAG_DECODER(decode_packet_diag_msg)
184 {
185 	struct packet_diag_msg msg = { .pdiag_family = family };
186 	size_t offset = sizeof(msg.pdiag_family);
187 	bool decode_nla = false;
188 
189 	PRINT_FIELD_XVAL("{", msg, pdiag_family, addrfams, "AF_???");
190 	tprints(", ");
191 	if (len >= sizeof(msg)) {
192 		if (!umoven_or_printaddr(tcp, addr + offset,
193 					 sizeof(msg) - offset,
194 					 (char *) &msg + offset)) {
195 			PRINT_FIELD_XVAL("", msg, pdiag_type,
196 					 socktypes, "SOCK_???");
197 			PRINT_FIELD_XVAL_SORTED_SIZED(", ", msg, pdiag_num,
198 						      ethernet_protocols,
199 						      ethernet_protocols_size,
200 						      "ETH_P_???");
201 			PRINT_FIELD_U(", ", msg, pdiag_ino);
202 			PRINT_FIELD_COOKIE(", ", msg, pdiag_cookie);
203 			decode_nla = true;
204 		}
205 	} else
206 		tprints("...");
207 	tprints("}");
208 
209 	offset = NLMSG_ALIGN(sizeof(msg));
210 	if (decode_nla && len > offset) {
211 		tprints(", ");
212 		decode_nlattr(tcp, addr + offset, len - offset,
213 			      packet_diag_attrs, "PACKET_DIAG_???",
214 			      packet_diag_msg_nla_decoders,
215 			      ARRAY_SIZE(packet_diag_msg_nla_decoders), NULL);
216 	}
217 }
218