blob: 71d18c623faf219a993685c133a439b8f9fd9e17 [file] [log] [blame]
Dmitry V. Levine96aee72016-06-25 13:47:54 +00001/*
2 * Copyright (c) 1991, 1992 Paul Kranenburg <pk@cs.few.eur.nl>
3 * Copyright (c) 1993 Branko Lankester <branko@hacktic.nl>
4 * Copyright (c) 1993, 1994, 1995, 1996 Rick Sladkey <jrs@world.std.com>
5 * Copyright (c) 1996-2000 Wichert Akkerman <wichert@cistron.nl>
6 * Copyright (c) 2005-2016 Dmitry V. Levin <ldv@altlinux.org>
Elliott Hughes28e98bc2018-06-14 16:59:04 -07007 * Copyright (c) 2016-2018 The strace developers.
Dmitry V. Levine96aee72016-06-25 13:47:54 +00008 * All rights reserved.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. The name of the author may not be used to endorse or promote products
19 * derived from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33#include "defs.h"
Elliott Hughes77c3ff82017-09-08 17:11:00 -070034#include "print_fields.h"
35
Dmitry V. Levine96aee72016-06-25 13:47:54 +000036#include <sys/socket.h>
37#include <sys/un.h>
38#include <netinet/in.h>
39#include <arpa/inet.h>
40
Elliott Hughesdc75b012017-07-05 13:54:44 -070041#include "netlink.h"
Dmitry V. Levine96aee72016-06-25 13:47:54 +000042#include <linux/if_packet.h>
43#include <linux/if_arp.h>
44#include <linux/if_ether.h>
45
46#ifdef HAVE_NETIPX_IPX_H
47# include <netipx/ipx.h>
48#else
49# include <linux/ipx.h>
50#endif
51
52#include "xlat/addrfams.h"
53#include "xlat/arp_hardware_types.h"
54#include "xlat/ethernet_protocols.h"
55#include "xlat/af_packet_types.h"
56
57#ifdef HAVE_BLUETOOTH_BLUETOOTH_H
58# include <bluetooth/bluetooth.h>
59# include <bluetooth/hci.h>
60# include <bluetooth/l2cap.h>
61# include <bluetooth/rfcomm.h>
62# include <bluetooth/sco.h>
63
64# include "xlat/hci_channels.h"
65#endif
66
67#define SIZEOF_SA_FAMILY sizeof(((struct sockaddr *) 0)->sa_family)
68
Elliott Hughes28e98bc2018-06-14 16:59:04 -070069const size_t ethernet_protocols_size = ARRAY_SIZE(ethernet_protocols) - 1;
70
Dmitry V. Levine96aee72016-06-25 13:47:54 +000071static void
72print_sockaddr_data_un(const void *const buf, const int addrlen)
73{
74 const struct sockaddr_un *const sa_un = buf;
75 const int un_len = addrlen > (int) sizeof(*sa_un)
76 ? (int) sizeof(*sa_un) : addrlen;
77 const int path_len = un_len - SIZEOF_SA_FAMILY;
78
79 tprints("sun_path=");
80 if (sa_un->sun_path[0]) {
81 print_quoted_string(sa_un->sun_path, path_len + 1,
82 QUOTE_0_TERMINATED);
83 } else {
84 tprints("@");
85 print_quoted_string(sa_un->sun_path + 1, path_len - 1, 0);
86 }
87}
88
Elliott Hughesdc75b012017-07-05 13:54:44 -070089bool
90print_inet_addr(const int af,
91 const void *const addr,
92 const unsigned int len,
93 const char *const var_name)
94{
Elliott Hughesdc75b012017-07-05 13:54:44 -070095 char buf[INET6_ADDRSTRLEN];
96
97 switch (af) {
98 case AF_INET:
Elliott Hughes77c3ff82017-09-08 17:11:00 -070099 if (inet_ntop(af, addr, buf, sizeof(buf))) {
100 if (var_name)
101 tprintf("%s=inet_addr(\"%s\")", var_name, buf);
102 else
103 tprints(buf);
104 return true;
105 }
Elliott Hughesdc75b012017-07-05 13:54:44 -0700106 break;
107 case AF_INET6:
Elliott Hughes77c3ff82017-09-08 17:11:00 -0700108 if (inet_ntop(af, addr, buf, sizeof(buf))) {
109 if (var_name)
110 tprintf("inet_pton(%s, \"%s\", &%s)",
111 "AF_INET6", buf, var_name);
112 else
113 tprints(buf);
114 return true;
115 }
Elliott Hughesdc75b012017-07-05 13:54:44 -0700116 break;
117 }
118
Elliott Hughes77c3ff82017-09-08 17:11:00 -0700119 if (var_name)
Elliott Hughesdc75b012017-07-05 13:54:44 -0700120 tprintf("%s=", var_name);
Elliott Hughes77c3ff82017-09-08 17:11:00 -0700121 print_quoted_string(addr, len, QUOTE_FORCE_HEX);
122 return false;
123}
124
125bool
126decode_inet_addr(struct tcb *const tcp,
127 const kernel_ulong_t addr,
128 const unsigned int len,
129 const int family,
130 const char *const var_name)
131{
132 union {
133 struct in_addr a4;
134 struct in6_addr a6;
135 } addrbuf;
136 size_t size = 0;
137
138 switch (family) {
139 case AF_INET:
140 size = sizeof(addrbuf.a4);
141 break;
142 case AF_INET6:
143 size = sizeof(addrbuf.a6);
144 break;
145 }
146
147 if (!size || len < size) {
148 if (var_name)
149 tprintf("%s=", var_name);
150 printstr_ex(tcp, addr, len, QUOTE_FORCE_HEX);
Elliott Hughesdc75b012017-07-05 13:54:44 -0700151 return false;
152 }
Elliott Hughes77c3ff82017-09-08 17:11:00 -0700153
154 if (umoven(tcp, addr, size, &addrbuf) < 0) {
155 if (var_name)
156 tprintf("%s=", var_name);
157 printaddr(addr);
158 return false;
159 }
160
161 return print_inet_addr(family, &addrbuf, size, var_name);
Elliott Hughesdc75b012017-07-05 13:54:44 -0700162}
163
Dmitry V. Levine96aee72016-06-25 13:47:54 +0000164static void
165print_sockaddr_data_in(const void *const buf, const int addrlen)
166{
167 const struct sockaddr_in *const sa_in = buf;
168
Elliott Hughes77c3ff82017-09-08 17:11:00 -0700169 PRINT_FIELD_NET_PORT("", *sa_in, sin_port);
170 PRINT_FIELD_INET4_ADDR(", ", *sa_in, sin_addr);
Dmitry V. Levine96aee72016-06-25 13:47:54 +0000171}
172
173#define SIN6_MIN_LEN offsetof(struct sockaddr_in6, sin6_scope_id)
174
175static void
176print_sockaddr_data_in6(const void *const buf, const int addrlen)
177{
178 const struct sockaddr_in6 *const sa_in6 = buf;
179
Elliott Hughes77c3ff82017-09-08 17:11:00 -0700180 PRINT_FIELD_NET_PORT("", *sa_in6, sin6_port);
181 PRINT_FIELD_INET_ADDR(", ", *sa_in6, sin6_addr, AF_INET6);
Elliott Hughesdc75b012017-07-05 13:54:44 -0700182 tprintf(", sin6_flowinfo=htonl(%u)", ntohl(sa_in6->sin6_flowinfo));
Dmitry V. Levine96aee72016-06-25 13:47:54 +0000183
184 if (addrlen <= (int) SIN6_MIN_LEN)
185 return;
186
Dmitry V. Levine96aee72016-06-25 13:47:54 +0000187#if defined IN6_IS_ADDR_LINKLOCAL && defined IN6_IS_ADDR_MC_LINKLOCAL
188 if (IN6_IS_ADDR_LINKLOCAL(&sa_in6->sin6_addr)
189 || IN6_IS_ADDR_MC_LINKLOCAL(&sa_in6->sin6_addr))
Elliott Hughes77c3ff82017-09-08 17:11:00 -0700190 PRINT_FIELD_IFINDEX(", ", *sa_in6, sin6_scope_id);
Dmitry V. Levine96aee72016-06-25 13:47:54 +0000191 else
192#endif
Elliott Hughes77c3ff82017-09-08 17:11:00 -0700193 PRINT_FIELD_U(", ", *sa_in6, sin6_scope_id);
Dmitry V. Levine96aee72016-06-25 13:47:54 +0000194}
195
196static void
197print_sockaddr_data_ipx(const void *const buf, const int addrlen)
198{
199 const struct sockaddr_ipx *const sa_ipx = buf;
200 unsigned int i;
201
Elliott Hughes77c3ff82017-09-08 17:11:00 -0700202 PRINT_FIELD_NET_PORT("", *sa_ipx, sipx_port);
203 tprintf(", sipx_network=htonl(%#08x)"
Dmitry V. Levine96aee72016-06-25 13:47:54 +0000204 ", sipx_node=[",
Dmitry V. Levine96aee72016-06-25 13:47:54 +0000205 ntohl(sa_ipx->sipx_network));
206 for (i = 0; i < IPX_NODE_LEN; ++i) {
207 tprintf("%s%#02x", i ? ", " : "",
208 sa_ipx->sipx_node[i]);
209 }
Elliott Hughes77c3ff82017-09-08 17:11:00 -0700210 PRINT_FIELD_0X("], ", *sa_ipx, sipx_type);
Dmitry V. Levine96aee72016-06-25 13:47:54 +0000211}
212
213static void
214print_sockaddr_data_nl(const void *const buf, const int addrlen)
215{
216 const struct sockaddr_nl *const sa_nl = buf;
217
Elliott Hughes77c3ff82017-09-08 17:11:00 -0700218 PRINT_FIELD_D("", *sa_nl, nl_pid);
219 PRINT_FIELD_0X(", ", *sa_nl, nl_groups);
Dmitry V. Levine96aee72016-06-25 13:47:54 +0000220}
221
222static void
223print_sockaddr_data_ll(const void *const buf, const int addrlen)
224{
225 const struct sockaddr_ll *const sa_ll = buf;
226
227 tprints("sll_protocol=htons(");
Elliott Hughes28e98bc2018-06-14 16:59:04 -0700228 printxval_search(ethernet_protocols, ntohs(sa_ll->sll_protocol),
229 "ETH_P_???");
Elliott Hughes77c3ff82017-09-08 17:11:00 -0700230 PRINT_FIELD_IFINDEX("), ", *sa_ll, sll_ifindex);
Dmitry V. Levine96aee72016-06-25 13:47:54 +0000231 tprints(", sll_hatype=");
232 printxval(arp_hardware_types, sa_ll->sll_hatype, "ARPHRD_???");
233 tprints(", sll_pkttype=");
234 printxval(af_packet_types, sa_ll->sll_pkttype, "PACKET_???");
235 tprintf(", sll_halen=%u", sa_ll->sll_halen);
236 if (sa_ll->sll_halen) {
237 const unsigned int oob_halen =
238 addrlen - offsetof(struct sockaddr_ll, sll_addr);
239 unsigned int i;
240
241 tprints(", sll_addr=[");
242 for (i = 0; i < sa_ll->sll_halen; ++i) {
243 if (i)
244 tprints(", ");
245 if (i >= oob_halen) {
246 tprints("...");
247 break;
248 }
249 tprintf("%#02x", sa_ll->sll_addr[i]);
250 }
251 tprints("]");
252 }
253}
254
255static void
256print_sockaddr_data_raw(const void *const buf, const int addrlen)
257{
258 const char *const data = buf + SIZEOF_SA_FAMILY;
259 const int datalen = addrlen - SIZEOF_SA_FAMILY;
260
261 tprints("sa_data=");
262 print_quoted_string(data, datalen, 0);
263}
264
265#ifdef HAVE_BLUETOOTH_BLUETOOTH_H
266static void
267print_sockaddr_data_bt(const void *const buf, const int addrlen)
268{
269 switch (addrlen) {
270 case sizeof(struct sockaddr_hci): {
271 const struct sockaddr_hci *const hci = buf;
272 tprintf("hci_dev=htobs(%hu), hci_channel=",
273 btohs(hci->hci_dev));
274 printxval(hci_channels, hci->hci_channel,
275 "HCI_CHANNEL_???");
276 break;
277 }
278 case sizeof(struct sockaddr_sco): {
279 const struct sockaddr_sco *const sco = buf;
280 tprintf("sco_bdaddr=%02x:%02x:%02x:%02x:%02x:%02x",
281 sco->sco_bdaddr.b[0], sco->sco_bdaddr.b[1],
282 sco->sco_bdaddr.b[2], sco->sco_bdaddr.b[3],
283 sco->sco_bdaddr.b[4], sco->sco_bdaddr.b[5]);
284 break;
285 }
286 case sizeof(struct sockaddr_rc): {
287 const struct sockaddr_rc *const rc = buf;
288 tprintf("rc_bdaddr=%02x:%02x:%02x:%02x:%02x:%02x"
289 ", rc_channel=%u",
290 rc->rc_bdaddr.b[0], rc->rc_bdaddr.b[1],
291 rc->rc_bdaddr.b[2], rc->rc_bdaddr.b[3],
292 rc->rc_bdaddr.b[4], rc->rc_bdaddr.b[5],
293 rc->rc_channel);
294 break;
295 }
296 case sizeof(struct sockaddr_l2): {
297 const struct sockaddr_l2 *const l2 = buf;
298 tprintf("l2_psm=htobs(%hu)"
299 ", l2_bdaddr=%02x:%02x:%02x:%02x:%02x:%02x"
300 ", l2_cid=htobs(%hu), l2_bdaddr_type=%u",
301 btohs(l2->l2_psm),
302 l2->l2_bdaddr.b[0], l2->l2_bdaddr.b[1],
303 l2->l2_bdaddr.b[2], l2->l2_bdaddr.b[3],
304 l2->l2_bdaddr.b[4], l2->l2_bdaddr.b[5],
305 btohs(l2->l2_cid), l2->l2_bdaddr_type);
306 break;
307 }
308 default:
309 print_sockaddr_data_raw(buf, addrlen);
310 break;
311 }
312}
313#endif /* HAVE_BLUETOOTH_BLUETOOTH_H */
314
315typedef void (* const sockaddr_printer)(const void *const, const int);
316
317static const struct {
318 const sockaddr_printer printer;
319 const int min_len;
320} sa_printers[] = {
321 [AF_UNIX] = { print_sockaddr_data_un, SIZEOF_SA_FAMILY + 1 },
322 [AF_INET] = { print_sockaddr_data_in, sizeof(struct sockaddr_in) },
323 [AF_IPX] = { print_sockaddr_data_ipx, sizeof(struct sockaddr_ipx) },
324 [AF_INET6] = { print_sockaddr_data_in6, SIN6_MIN_LEN },
325 [AF_NETLINK] = { print_sockaddr_data_nl, SIZEOF_SA_FAMILY + 1 },
326 [AF_PACKET] = { print_sockaddr_data_ll, sizeof(struct sockaddr_ll) },
327#ifdef HAVE_BLUETOOTH_BLUETOOTH_H
328 [AF_BLUETOOTH] = { print_sockaddr_data_bt, SIZEOF_SA_FAMILY + 1 },
329#endif
330};
331
332void
Elliott Hughes77c3ff82017-09-08 17:11:00 -0700333print_sockaddr(const void *const buf, const int addrlen)
Dmitry V. Levine96aee72016-06-25 13:47:54 +0000334{
335 const struct sockaddr *const sa = buf;
336
337 tprints("{sa_family=");
338 printxval(addrfams, sa->sa_family, "AF_???");
339
340 if (addrlen > (int) SIZEOF_SA_FAMILY) {
341 tprints(", ");
342
343 if (sa->sa_family < ARRAY_SIZE(sa_printers)
344 && sa_printers[sa->sa_family].printer
345 && addrlen >= sa_printers[sa->sa_family].min_len) {
346 sa_printers[sa->sa_family].printer(buf, addrlen);
347 } else {
348 print_sockaddr_data_raw(buf, addrlen);
349 }
350 }
351
352 tprints("}");
353}
354
355int
Elliott Hughesd35df492017-02-15 15:19:05 -0800356decode_sockaddr(struct tcb *const tcp, const kernel_ulong_t addr, int addrlen)
Dmitry V. Levine96aee72016-06-25 13:47:54 +0000357{
358 if (addrlen < 2) {
359 printaddr(addr);
360 return -1;
361 }
362
363 union {
364 struct sockaddr sa;
365 struct sockaddr_storage storage;
366 char pad[sizeof(struct sockaddr_storage) + 1];
367 } addrbuf;
368
369 if ((unsigned) addrlen > sizeof(addrbuf.storage))
370 addrlen = sizeof(addrbuf.storage);
371
372 if (umoven_or_printaddr(tcp, addr, addrlen, addrbuf.pad))
373 return -1;
374
375 memset(&addrbuf.pad[addrlen], 0, sizeof(addrbuf.pad) - addrlen);
376
Elliott Hughes77c3ff82017-09-08 17:11:00 -0700377 print_sockaddr(&addrbuf, addrlen);
Dmitry V. Levine96aee72016-06-25 13:47:54 +0000378
379 return addrbuf.sa.sa_family;
380}