blob: 67604637141963c765ab86294d8d4562c1173d10 [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"
Elliott Hughesc1873762018-12-19 15:13:36 -080042#include <linux/ax25.h>
Dmitry V. Levine96aee72016-06-25 13:47:54 +000043#include <linux/if_packet.h>
44#include <linux/if_arp.h>
45#include <linux/if_ether.h>
Elliott Hughesc1873762018-12-19 15:13:36 -080046#include <linux/x25.h>
Dmitry V. Levine96aee72016-06-25 13:47:54 +000047
48#ifdef HAVE_NETIPX_IPX_H
49# include <netipx/ipx.h>
Dmitry V. Levine96aee72016-06-25 13:47:54 +000050#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
Elliott Hughes03a418e2018-06-15 13:11:40 -070057#include "xlat/bdaddr_types.h"
58#include "xlat/bluetooth_l2_cid.h"
59#include "xlat/bluetooth_l2_psm.h"
60#include "xlat/hci_channels.h"
Dmitry V. Levine96aee72016-06-25 13:47:54 +000061
62#define SIZEOF_SA_FAMILY sizeof(((struct sockaddr *) 0)->sa_family)
63
Elliott Hughes03a418e2018-06-15 13:11:40 -070064const size_t arp_hardware_types_size = ARRAY_SIZE(arp_hardware_types) - 1;
Elliott Hughes28e98bc2018-06-14 16:59:04 -070065const size_t ethernet_protocols_size = ARRAY_SIZE(ethernet_protocols) - 1;
66
Dmitry V. Levine96aee72016-06-25 13:47:54 +000067static void
68print_sockaddr_data_un(const void *const buf, const int addrlen)
69{
70 const struct sockaddr_un *const sa_un = buf;
71 const int un_len = addrlen > (int) sizeof(*sa_un)
72 ? (int) sizeof(*sa_un) : addrlen;
73 const int path_len = un_len - SIZEOF_SA_FAMILY;
74
75 tprints("sun_path=");
76 if (sa_un->sun_path[0]) {
77 print_quoted_string(sa_un->sun_path, path_len + 1,
78 QUOTE_0_TERMINATED);
79 } else {
80 tprints("@");
81 print_quoted_string(sa_un->sun_path + 1, path_len - 1, 0);
82 }
83}
84
Elliott Hughesdc75b012017-07-05 13:54:44 -070085bool
86print_inet_addr(const int af,
87 const void *const addr,
88 const unsigned int len,
89 const char *const var_name)
90{
Elliott Hughesdc75b012017-07-05 13:54:44 -070091 char buf[INET6_ADDRSTRLEN];
92
93 switch (af) {
94 case AF_INET:
Elliott Hughes77c3ff82017-09-08 17:11:00 -070095 if (inet_ntop(af, addr, buf, sizeof(buf))) {
96 if (var_name)
97 tprintf("%s=inet_addr(\"%s\")", var_name, buf);
98 else
99 tprints(buf);
100 return true;
101 }
Elliott Hughesdc75b012017-07-05 13:54:44 -0700102 break;
103 case AF_INET6:
Elliott Hughes77c3ff82017-09-08 17:11:00 -0700104 if (inet_ntop(af, addr, buf, sizeof(buf))) {
105 if (var_name)
106 tprintf("inet_pton(%s, \"%s\", &%s)",
107 "AF_INET6", buf, var_name);
108 else
109 tprints(buf);
110 return true;
111 }
Elliott Hughesdc75b012017-07-05 13:54:44 -0700112 break;
113 }
114
Elliott Hughes77c3ff82017-09-08 17:11:00 -0700115 if (var_name)
Elliott Hughesdc75b012017-07-05 13:54:44 -0700116 tprintf("%s=", var_name);
Elliott Hughes77c3ff82017-09-08 17:11:00 -0700117 print_quoted_string(addr, len, QUOTE_FORCE_HEX);
118 return false;
119}
120
121bool
122decode_inet_addr(struct tcb *const tcp,
123 const kernel_ulong_t addr,
124 const unsigned int len,
125 const int family,
126 const char *const var_name)
127{
128 union {
129 struct in_addr a4;
130 struct in6_addr a6;
131 } addrbuf;
132 size_t size = 0;
133
134 switch (family) {
135 case AF_INET:
136 size = sizeof(addrbuf.a4);
137 break;
138 case AF_INET6:
139 size = sizeof(addrbuf.a6);
140 break;
141 }
142
143 if (!size || len < size) {
144 if (var_name)
145 tprintf("%s=", var_name);
146 printstr_ex(tcp, addr, len, QUOTE_FORCE_HEX);
Elliott Hughesdc75b012017-07-05 13:54:44 -0700147 return false;
148 }
Elliott Hughes77c3ff82017-09-08 17:11:00 -0700149
150 if (umoven(tcp, addr, size, &addrbuf) < 0) {
151 if (var_name)
152 tprintf("%s=", var_name);
153 printaddr(addr);
154 return false;
155 }
156
157 return print_inet_addr(family, &addrbuf, size, var_name);
Elliott Hughesdc75b012017-07-05 13:54:44 -0700158}
159
Dmitry V. Levine96aee72016-06-25 13:47:54 +0000160static void
161print_sockaddr_data_in(const void *const buf, const int addrlen)
162{
163 const struct sockaddr_in *const sa_in = buf;
164
Elliott Hughes77c3ff82017-09-08 17:11:00 -0700165 PRINT_FIELD_NET_PORT("", *sa_in, sin_port);
166 PRINT_FIELD_INET4_ADDR(", ", *sa_in, sin_addr);
Dmitry V. Levine96aee72016-06-25 13:47:54 +0000167}
168
169#define SIN6_MIN_LEN offsetof(struct sockaddr_in6, sin6_scope_id)
170
171static void
172print_sockaddr_data_in6(const void *const buf, const int addrlen)
173{
174 const struct sockaddr_in6 *const sa_in6 = buf;
175
Elliott Hughes77c3ff82017-09-08 17:11:00 -0700176 PRINT_FIELD_NET_PORT("", *sa_in6, sin6_port);
177 PRINT_FIELD_INET_ADDR(", ", *sa_in6, sin6_addr, AF_INET6);
Elliott Hughesdc75b012017-07-05 13:54:44 -0700178 tprintf(", sin6_flowinfo=htonl(%u)", ntohl(sa_in6->sin6_flowinfo));
Dmitry V. Levine96aee72016-06-25 13:47:54 +0000179
180 if (addrlen <= (int) SIN6_MIN_LEN)
181 return;
182
Dmitry V. Levine96aee72016-06-25 13:47:54 +0000183#if defined IN6_IS_ADDR_LINKLOCAL && defined IN6_IS_ADDR_MC_LINKLOCAL
184 if (IN6_IS_ADDR_LINKLOCAL(&sa_in6->sin6_addr)
185 || IN6_IS_ADDR_MC_LINKLOCAL(&sa_in6->sin6_addr))
Elliott Hughes77c3ff82017-09-08 17:11:00 -0700186 PRINT_FIELD_IFINDEX(", ", *sa_in6, sin6_scope_id);
Dmitry V. Levine96aee72016-06-25 13:47:54 +0000187 else
188#endif
Elliott Hughes77c3ff82017-09-08 17:11:00 -0700189 PRINT_FIELD_U(", ", *sa_in6, sin6_scope_id);
Dmitry V. Levine96aee72016-06-25 13:47:54 +0000190}
191
Elliott Hughesc1873762018-12-19 15:13:36 -0800192/**
193 * Check that we can print an AX.25 address in its native form, otherwise it
194 * makes sense to print it in raw also (or in raw only).
195 */
196enum xlat_style
197check_ax25_address(const ax25_address *addr)
198{
199 enum xlat_style ret = XLAT_STYLE_DEFAULT;
200 bool space_seen = false;
201 bool char_seen = false;
202
203 for (size_t i = 0; i < ARRAY_SIZE(addr->ax25_call) - 1; i++) {
204 unsigned char c = addr->ax25_call[i];
205
206 /* The lowest bit should be zero */
207 if (c & 1)
208 ret = XLAT_STYLE_VERBOSE;
209
210 c >>= 1;
211
212 if (c == ' ')
213 space_seen = true;
214 else
215 char_seen = true;
216
217 /* Sane address contains only numbers and uppercase letters */
218 if ((c < '0' || c > '9') && (c < 'A' || c > 'Z') && c != ' ')
219 ret = XLAT_STYLE_VERBOSE;
220 if (c != ' ' && space_seen)
221 ret = XLAT_STYLE_VERBOSE;
222
223 /* non-printable chars */
224 if (c < ' ' || c > 0x7e
225 /* characters used for printing comments */
226 || c == '*' || c == '/')
227 return XLAT_STYLE_RAW;
228 }
229
230 if (addr->ax25_call[ARRAY_SIZE(addr->ax25_call) - 1] & ~0x1e)
231 ret = XLAT_STYLE_VERBOSE;
232
233 if (!char_seen && addr->ax25_call[ARRAY_SIZE(addr->ax25_call) - 1])
234 ret = XLAT_STYLE_VERBOSE;
235
236 return ret;
237}
238
239/** Convert a (presumably) valid AX.25 to a string */
240static const char *
241ax25_addr2str(const ax25_address *addr)
242{
243 static char buf[ARRAY_SIZE(addr->ax25_call) + sizeof("-15")];
244 char *p = buf;
245 size_t end;
246
247 for (end = ARRAY_SIZE(addr->ax25_call) - 1; end; end--)
248 if ((addr->ax25_call[end - 1] >> 1) != ' ')
249 break;
250
251 for (size_t i = 0; i < end; i++)
252 *p++ = ((unsigned char) addr->ax25_call[i]) >> 1;
253
254 *p++ = '-';
255
256 unsigned char ssid = (addr->ax25_call[ARRAY_SIZE(addr->ax25_call) - 1]
257 >> 1) & 0xf;
258
259 if (ssid > 9) {
260 *p++ = '1';
261 ssid -= 10;
262 }
263
264 *p++ = ssid + '0';
265 *p = '\0';
266
267 if (buf[0] == '-' && buf[1] == '0')
268 return "*";
269
270 return buf;
271}
272
273static void
274print_ax25_addr_raw(const ax25_address *addr)
275{
276 PRINT_FIELD_HEX_ARRAY("{", *addr, ax25_call);
277 tprints("}");
278}
279
280void
281print_ax25_addr(const void /* ax25_address */ *addr_void)
282{
283 const ax25_address *addr = addr_void;
284 enum xlat_style xs = check_ax25_address(addr);
285
286 if (xs == XLAT_STYLE_DEFAULT)
287 xs = xlat_verbose(xlat_verbosity);
288
289 if (xs != XLAT_STYLE_ABBREV)
290 print_ax25_addr_raw(addr);
291
292 if (xs == XLAT_STYLE_RAW)
293 return;
294
295 const char *addr_str = ax25_addr2str(addr);
296
297 (xs == XLAT_STYLE_VERBOSE ? tprints_comment : tprints)(addr_str);
298}
299
300static void
301print_sockaddr_data_ax25(const void *const buf, const int addrlen)
302{
303 const struct full_sockaddr_ax25 *const sax25 = buf;
304 size_t addrlen_us = MAX(addrlen, 0);
305 bool full = sax25->fsa_ax25.sax25_ndigis ||
306 (addrlen_us > sizeof(struct sockaddr_ax25));
307
308 if (full)
309 tprints("fsa_ax25={");
310
311 tprints("sax25_call=");
312 print_ax25_addr(&sax25->fsa_ax25.sax25_call);
313 PRINT_FIELD_D(", ", sax25->fsa_ax25, sax25_ndigis);
314
315 if (!full)
316 return;
317
318 tprints("}");
319
320 size_t has_digis = MIN((addrlen_us - sizeof(sax25->fsa_ax25))
321 / sizeof(sax25->fsa_digipeater[0]),
322 ARRAY_SIZE(sax25->fsa_digipeater));
323 size_t want_digis = MIN(
324 (unsigned int) MAX(sax25->fsa_ax25.sax25_ndigis, 0),
325 ARRAY_SIZE(sax25->fsa_digipeater));
326 size_t digis = MIN(has_digis, want_digis);
327
328 if (want_digis == 0)
329 goto digis_end;
330
331 tprints(", fsa_digipeater=[");
332 for (size_t i = 0; i < digis; i++) {
333 if (i)
334 tprints(", ");
335
336 print_ax25_addr(sax25->fsa_digipeater + i);
337 }
338
339 if (want_digis > has_digis)
340 tprintf("%s/* ??? */", digis ? ", " : "");
341
342 tprints("]");
343
344digis_end:
345 if (addrlen_us > (has_digis * sizeof(sax25->fsa_digipeater[0])
346 + sizeof(sax25->fsa_ax25)))
347 tprints(", ...");
348}
349
Elliott Hughesc1873762018-12-19 15:13:36 -0800350void
351print_x25_addr(const void /* struct x25_address */ *addr_void)
352{
353 const struct x25_address *addr = addr_void;
354
355 tprints("{x25_addr=");
356 print_quoted_cstring(addr->x25_addr, sizeof(addr->x25_addr));
357 tprints("}");
358}
359
360static void
361print_sockaddr_data_x25(const void *const buf, const int addrlen)
362{
363 const struct sockaddr_x25 *const sa_x25 = buf;
364
365 PRINT_FIELD_X25_ADDR("", *sa_x25, sx25_addr);
366}
367
Dmitry V. Levine96aee72016-06-25 13:47:54 +0000368static void
369print_sockaddr_data_nl(const void *const buf, const int addrlen)
370{
371 const struct sockaddr_nl *const sa_nl = buf;
372
Elliott Hughes77c3ff82017-09-08 17:11:00 -0700373 PRINT_FIELD_D("", *sa_nl, nl_pid);
374 PRINT_FIELD_0X(", ", *sa_nl, nl_groups);
Dmitry V. Levine96aee72016-06-25 13:47:54 +0000375}
376
377static void
378print_sockaddr_data_ll(const void *const buf, const int addrlen)
379{
380 const struct sockaddr_ll *const sa_ll = buf;
381
382 tprints("sll_protocol=htons(");
Elliott Hughes28e98bc2018-06-14 16:59:04 -0700383 printxval_search(ethernet_protocols, ntohs(sa_ll->sll_protocol),
384 "ETH_P_???");
Elliott Hughes77c3ff82017-09-08 17:11:00 -0700385 PRINT_FIELD_IFINDEX("), ", *sa_ll, sll_ifindex);
Dmitry V. Levine96aee72016-06-25 13:47:54 +0000386 tprints(", sll_hatype=");
Elliott Hughes03a418e2018-06-15 13:11:40 -0700387 printxval_search(arp_hardware_types, sa_ll->sll_hatype, "ARPHRD_???");
Dmitry V. Levine96aee72016-06-25 13:47:54 +0000388 tprints(", sll_pkttype=");
Elliott Hughes03a418e2018-06-15 13:11:40 -0700389 printxval_index(af_packet_types, sa_ll->sll_pkttype, "PACKET_???");
Dmitry V. Levine96aee72016-06-25 13:47:54 +0000390 tprintf(", sll_halen=%u", sa_ll->sll_halen);
391 if (sa_ll->sll_halen) {
392 const unsigned int oob_halen =
393 addrlen - offsetof(struct sockaddr_ll, sll_addr);
394 unsigned int i;
395
396 tprints(", sll_addr=[");
397 for (i = 0; i < sa_ll->sll_halen; ++i) {
398 if (i)
399 tprints(", ");
400 if (i >= oob_halen) {
401 tprints("...");
402 break;
403 }
404 tprintf("%#02x", sa_ll->sll_addr[i]);
405 }
406 tprints("]");
407 }
408}
409
410static void
411print_sockaddr_data_raw(const void *const buf, const int addrlen)
412{
413 const char *const data = buf + SIZEOF_SA_FAMILY;
414 const int datalen = addrlen - SIZEOF_SA_FAMILY;
415
416 tprints("sa_data=");
417 print_quoted_string(data, datalen, 0);
418}
419
Elliott Hughes03a418e2018-06-15 13:11:40 -0700420static uint16_t
421btohs(uint16_t val)
422{
423#ifdef WORDS_BIGENDIAN
424 return (val << 8) | (val >> 8);
425#else
426 return val;
427#endif
428}
429
430static void
431print_bluetooth_l2_psm(const char *prefix, uint16_t psm)
432{
433 const uint16_t psm_he = btohs(psm);
434 const char *psm_name = xlookup(bluetooth_l2_psm, psm_he);
435 const bool psm_str = psm_name || (psm_he >= L2CAP_PSM_LE_DYN_START
436 && psm_he <= L2CAP_PSM_LE_DYN_END)
437 || (psm_he >= L2CAP_PSM_DYN_START);
438
439 tprintf("%shtobs(", prefix);
440
441 if (xlat_verbose(xlat_verbosity) != XLAT_STYLE_ABBREV || !psm_str)
442 tprintf("%#x", psm_he);
443
444 if (xlat_verbose(xlat_verbosity) == XLAT_STYLE_RAW)
445 goto print_bluetooth_l2_psm_end;
446
447 if (xlat_verbose(xlat_verbosity) == XLAT_STYLE_VERBOSE || !psm_str)
448 tprints(" /* ");
449
450 if (psm_name) {
451 tprints(psm_name);
452 } else if (psm_he >= L2CAP_PSM_LE_DYN_START
453 && psm_he <= L2CAP_PSM_LE_DYN_END) {
454 print_xlat(L2CAP_PSM_LE_DYN_START);
455 tprintf(" + %u", psm_he - L2CAP_PSM_LE_DYN_START);
456 } else if (psm_he >= L2CAP_PSM_DYN_START) {
457 print_xlat(L2CAP_PSM_DYN_START);
458 tprintf(" + %u", psm_he - L2CAP_PSM_DYN_START);
459 } else {
460 tprints("L2CAP_PSM_???");
461 }
462
463 if (xlat_verbose(xlat_verbosity) == XLAT_STYLE_VERBOSE || !psm_str)
464 tprints(" */");
465
466print_bluetooth_l2_psm_end:
467 tprints(")");
468}
469
470static void
471print_bluetooth_l2_cid(const char *prefix, uint16_t cid)
472{
473 const uint16_t cid_he = btohs(cid);
474 const char *cid_name = xlookup(bluetooth_l2_cid, cid_he);
475 const bool cid_str = cid_name || (cid_he >= L2CAP_CID_DYN_START);
476
477 tprintf("%shtobs(", prefix);
478
479 if (xlat_verbose(xlat_verbosity) != XLAT_STYLE_ABBREV || !cid_str)
480 tprintf("%#x", cid_he);
481
482 if (xlat_verbose(xlat_verbosity) == XLAT_STYLE_RAW)
483 goto print_bluetooth_l2_cid_end;
484
485 if (xlat_verbose(xlat_verbosity) == XLAT_STYLE_VERBOSE || !cid_str)
486 tprints(" /* ");
487
488 if (cid_name) {
489 tprints(cid_name);
490 } else if (cid_he >= L2CAP_CID_DYN_START) {
491 print_xlat(L2CAP_CID_DYN_START);
492 tprintf(" + %u", cid_he - L2CAP_CID_DYN_START);
493 } else {
494 tprints("L2CAP_CID_???");
495 }
496
497 if (xlat_verbose(xlat_verbosity) == XLAT_STYLE_VERBOSE || !cid_str)
498 tprints(" */");
499
500print_bluetooth_l2_cid_end:
501 tprints(")");
502}
503
Dmitry V. Levine96aee72016-06-25 13:47:54 +0000504static void
505print_sockaddr_data_bt(const void *const buf, const int addrlen)
506{
Elliott Hughes03a418e2018-06-15 13:11:40 -0700507 struct sockaddr_hci {
508 /* sa_family_t */ uint16_t hci_family;
509 uint16_t hci_dev;
510 uint16_t hci_channel;
511 };
512
513 struct bdaddr {
514 uint8_t b[6];
515 } ATTRIBUTE_PACKED;
516
517 struct sockaddr_sco {
518 /* sa_family_t */ uint16_t sco_family;
519 struct bdaddr sco_bdaddr;
520 };
521
522 struct sockaddr_rc {
523 /* sa_family_t */ uint16_t rc_family;
524 struct bdaddr rc_bdaddr;
525 uint8_t rc_channel;
526 };
527
528 struct sockaddr_l2 {
529 /* sa_family_t */ uint16_t l2_family;
Elliott Hughesc1873762018-12-19 15:13:36 -0800530 /* little endian */ uint16_t l2_psm;
Elliott Hughes03a418e2018-06-15 13:11:40 -0700531 struct bdaddr l2_bdaddr;
532 /* little endian */ uint16_t l2_cid;
533 uint8_t l2_bdaddr_type;
534 };
535
Dmitry V. Levine96aee72016-06-25 13:47:54 +0000536 switch (addrlen) {
Elliott Hughesc1873762018-12-19 15:13:36 -0800537 case sizeof(struct sockaddr_hci): {
538 const struct sockaddr_hci *const hci = buf;
539 tprintf("hci_dev=htobs(%hu), hci_channel=",
540 btohs(hci->hci_dev));
541 printxval_index(hci_channels, hci->hci_channel,
542 "HCI_CHANNEL_???");
543 break;
544 }
545 case sizeof(struct sockaddr_sco): {
546 const struct sockaddr_sco *const sco = buf;
547 print_mac_addr("sco_bdaddr=", sco->sco_bdaddr.b,
548 sizeof(sco->sco_bdaddr.b));
549 break;
550 }
551 case sizeof(struct sockaddr_rc): {
552 const struct sockaddr_rc *const rc = buf;
553 print_mac_addr("rc_bdaddr=", rc->rc_bdaddr.b,
554 sizeof(rc->rc_bdaddr.b));
555 tprintf(", rc_channel=%u", rc->rc_channel);
556 break;
557 }
558 case offsetof(struct sockaddr_l2, l2_bdaddr_type):
559 case sizeof(struct sockaddr_l2): {
560 const struct sockaddr_l2 *const l2 = buf;
561 print_bluetooth_l2_psm("l2_psm=", l2->l2_psm);
562 print_mac_addr(", l2_bdaddr=", l2->l2_bdaddr.b,
563 sizeof(l2->l2_bdaddr.b));
564 print_bluetooth_l2_cid(", l2_cid=", l2->l2_cid);
Elliott Hughes03a418e2018-06-15 13:11:40 -0700565
Elliott Hughesc1873762018-12-19 15:13:36 -0800566 if (addrlen == sizeof(struct sockaddr_l2)) {
567 tprints(", l2_bdaddr_type=");
568 printxval_index(bdaddr_types, l2->l2_bdaddr_type,
569 "BDADDR_???");
Dmitry V. Levine96aee72016-06-25 13:47:54 +0000570 }
Elliott Hughesc1873762018-12-19 15:13:36 -0800571
572 break;
573 }
574 default:
575 print_sockaddr_data_raw(buf, addrlen);
576 break;
Dmitry V. Levine96aee72016-06-25 13:47:54 +0000577 }
578}
Dmitry V. Levine96aee72016-06-25 13:47:54 +0000579
580typedef void (* const sockaddr_printer)(const void *const, const int);
581
582static const struct {
583 const sockaddr_printer printer;
584 const int min_len;
585} sa_printers[] = {
586 [AF_UNIX] = { print_sockaddr_data_un, SIZEOF_SA_FAMILY + 1 },
587 [AF_INET] = { print_sockaddr_data_in, sizeof(struct sockaddr_in) },
Elliott Hughesc1873762018-12-19 15:13:36 -0800588 [AF_AX25] = { print_sockaddr_data_ax25, sizeof(struct sockaddr_ax25) },
Elliott Hughesc1873762018-12-19 15:13:36 -0800589 [AF_X25] = { print_sockaddr_data_x25, sizeof(struct sockaddr_x25) },
Dmitry V. Levine96aee72016-06-25 13:47:54 +0000590 [AF_INET6] = { print_sockaddr_data_in6, SIN6_MIN_LEN },
591 [AF_NETLINK] = { print_sockaddr_data_nl, SIZEOF_SA_FAMILY + 1 },
592 [AF_PACKET] = { print_sockaddr_data_ll, sizeof(struct sockaddr_ll) },
Dmitry V. Levine96aee72016-06-25 13:47:54 +0000593 [AF_BLUETOOTH] = { print_sockaddr_data_bt, SIZEOF_SA_FAMILY + 1 },
Dmitry V. Levine96aee72016-06-25 13:47:54 +0000594};
595
596void
Elliott Hughes77c3ff82017-09-08 17:11:00 -0700597print_sockaddr(const void *const buf, const int addrlen)
Dmitry V. Levine96aee72016-06-25 13:47:54 +0000598{
599 const struct sockaddr *const sa = buf;
600
601 tprints("{sa_family=");
Elliott Hughes03a418e2018-06-15 13:11:40 -0700602 printxval_index(addrfams, sa->sa_family, "AF_???");
Dmitry V. Levine96aee72016-06-25 13:47:54 +0000603
604 if (addrlen > (int) SIZEOF_SA_FAMILY) {
605 tprints(", ");
606
607 if (sa->sa_family < ARRAY_SIZE(sa_printers)
608 && sa_printers[sa->sa_family].printer
609 && addrlen >= sa_printers[sa->sa_family].min_len) {
610 sa_printers[sa->sa_family].printer(buf, addrlen);
611 } else {
612 print_sockaddr_data_raw(buf, addrlen);
613 }
614 }
615
616 tprints("}");
617}
618
619int
Elliott Hughesd35df492017-02-15 15:19:05 -0800620decode_sockaddr(struct tcb *const tcp, const kernel_ulong_t addr, int addrlen)
Dmitry V. Levine96aee72016-06-25 13:47:54 +0000621{
622 if (addrlen < 2) {
623 printaddr(addr);
624 return -1;
625 }
626
627 union {
628 struct sockaddr sa;
629 struct sockaddr_storage storage;
630 char pad[sizeof(struct sockaddr_storage) + 1];
631 } addrbuf;
632
633 if ((unsigned) addrlen > sizeof(addrbuf.storage))
634 addrlen = sizeof(addrbuf.storage);
635
636 if (umoven_or_printaddr(tcp, addr, addrlen, addrbuf.pad))
637 return -1;
638
639 memset(&addrbuf.pad[addrlen], 0, sizeof(addrbuf.pad) - addrlen);
640
Elliott Hughes77c3ff82017-09-08 17:11:00 -0700641 print_sockaddr(&addrbuf, addrlen);
Dmitry V. Levine96aee72016-06-25 13:47:54 +0000642
643 return addrbuf.sa.sa_family;
644}