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>
7 * Copyright (c) 2016-2018 The strace developers.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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.
34 #include "print_fields.h"
36 #include <sys/socket.h>
38 #include <netinet/in.h>
39 #include <arpa/inet.h>
42 #include <linux/if_packet.h>
43 #include <linux/if_arp.h>
44 #include <linux/if_ether.h>
46 #ifdef HAVE_NETIPX_IPX_H
47 # include <netipx/ipx.h>
49 # include <linux/ipx.h>
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"
57 #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"
62 #define SIZEOF_SA_FAMILY sizeof(((struct sockaddr *) 0)->sa_family)
64 const size_t arp_hardware_types_size = ARRAY_SIZE(arp_hardware_types) - 1;
65 const size_t ethernet_protocols_size = ARRAY_SIZE(ethernet_protocols) - 1;
68 print_sockaddr_data_un(const void *const buf, const int addrlen)
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;
76 if (sa_un->sun_path[0]) {
77 print_quoted_string(sa_un->sun_path, path_len + 1,
81 print_quoted_string(sa_un->sun_path + 1, path_len - 1, 0);
86 print_inet_addr(const int af,
87 const void *const addr,
88 const unsigned int len,
89 const char *const var_name)
91 char buf[INET6_ADDRSTRLEN];
95 if (inet_ntop(af, addr, buf, sizeof(buf))) {
97 tprintf("%s=inet_addr(\"%s\")", var_name, buf);
104 if (inet_ntop(af, addr, buf, sizeof(buf))) {
106 tprintf("inet_pton(%s, \"%s\", &%s)",
107 "AF_INET6", buf, var_name);
116 tprintf("%s=", var_name);
117 print_quoted_string(addr, len, QUOTE_FORCE_HEX);
122 decode_inet_addr(struct tcb *const tcp,
123 const kernel_ulong_t addr,
124 const unsigned int len,
126 const char *const var_name)
136 size = sizeof(addrbuf.a4);
139 size = sizeof(addrbuf.a6);
143 if (!size || len < size) {
145 tprintf("%s=", var_name);
146 printstr_ex(tcp, addr, len, QUOTE_FORCE_HEX);
150 if (umoven(tcp, addr, size, &addrbuf) < 0) {
152 tprintf("%s=", var_name);
157 return print_inet_addr(family, &addrbuf, size, var_name);
161 print_sockaddr_data_in(const void *const buf, const int addrlen)
163 const struct sockaddr_in *const sa_in = buf;
165 PRINT_FIELD_NET_PORT("", *sa_in, sin_port);
166 PRINT_FIELD_INET4_ADDR(", ", *sa_in, sin_addr);
169 #define SIN6_MIN_LEN offsetof(struct sockaddr_in6, sin6_scope_id)
172 print_sockaddr_data_in6(const void *const buf, const int addrlen)
174 const struct sockaddr_in6 *const sa_in6 = buf;
176 PRINT_FIELD_NET_PORT("", *sa_in6, sin6_port);
177 PRINT_FIELD_INET_ADDR(", ", *sa_in6, sin6_addr, AF_INET6);
178 tprintf(", sin6_flowinfo=htonl(%u)", ntohl(sa_in6->sin6_flowinfo));
180 if (addrlen <= (int) SIN6_MIN_LEN)
183 #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))
186 PRINT_FIELD_IFINDEX(", ", *sa_in6, sin6_scope_id);
189 PRINT_FIELD_U(", ", *sa_in6, sin6_scope_id);
193 print_sockaddr_data_ipx(const void *const buf, const int addrlen)
195 const struct sockaddr_ipx *const sa_ipx = buf;
198 PRINT_FIELD_NET_PORT("", *sa_ipx, sipx_port);
199 tprintf(", sipx_network=htonl(%#08x)"
201 ntohl(sa_ipx->sipx_network));
202 for (i = 0; i < IPX_NODE_LEN; ++i) {
203 tprintf("%s%#02x", i ? ", " : "",
204 sa_ipx->sipx_node[i]);
206 PRINT_FIELD_0X("], ", *sa_ipx, sipx_type);
210 print_sockaddr_data_nl(const void *const buf, const int addrlen)
212 const struct sockaddr_nl *const sa_nl = buf;
214 PRINT_FIELD_D("", *sa_nl, nl_pid);
215 PRINT_FIELD_0X(", ", *sa_nl, nl_groups);
219 print_sockaddr_data_ll(const void *const buf, const int addrlen)
221 const struct sockaddr_ll *const sa_ll = buf;
223 tprints("sll_protocol=htons(");
224 printxval_search(ethernet_protocols, ntohs(sa_ll->sll_protocol),
226 PRINT_FIELD_IFINDEX("), ", *sa_ll, sll_ifindex);
227 tprints(", sll_hatype=");
228 printxval_search(arp_hardware_types, sa_ll->sll_hatype, "ARPHRD_???");
229 tprints(", sll_pkttype=");
230 printxval_index(af_packet_types, sa_ll->sll_pkttype, "PACKET_???");
231 tprintf(", sll_halen=%u", sa_ll->sll_halen);
232 if (sa_ll->sll_halen) {
233 const unsigned int oob_halen =
234 addrlen - offsetof(struct sockaddr_ll, sll_addr);
237 tprints(", sll_addr=[");
238 for (i = 0; i < sa_ll->sll_halen; ++i) {
241 if (i >= oob_halen) {
245 tprintf("%#02x", sa_ll->sll_addr[i]);
252 print_sockaddr_data_raw(const void *const buf, const int addrlen)
254 const char *const data = buf + SIZEOF_SA_FAMILY;
255 const int datalen = addrlen - SIZEOF_SA_FAMILY;
258 print_quoted_string(data, datalen, 0);
264 #ifdef WORDS_BIGENDIAN
265 return (val << 8) | (val >> 8);
272 print_bluetooth_l2_psm(const char *prefix, uint16_t psm)
274 const uint16_t psm_he = btohs(psm);
275 const char *psm_name = xlookup(bluetooth_l2_psm, psm_he);
276 const bool psm_str = psm_name || (psm_he >= L2CAP_PSM_LE_DYN_START
277 && psm_he <= L2CAP_PSM_LE_DYN_END)
278 || (psm_he >= L2CAP_PSM_DYN_START);
280 tprintf("%shtobs(", prefix);
282 if (xlat_verbose(xlat_verbosity) != XLAT_STYLE_ABBREV || !psm_str)
283 tprintf("%#x", psm_he);
285 if (xlat_verbose(xlat_verbosity) == XLAT_STYLE_RAW)
286 goto print_bluetooth_l2_psm_end;
288 if (xlat_verbose(xlat_verbosity) == XLAT_STYLE_VERBOSE || !psm_str)
293 } else if (psm_he >= L2CAP_PSM_LE_DYN_START
294 && psm_he <= L2CAP_PSM_LE_DYN_END) {
295 print_xlat(L2CAP_PSM_LE_DYN_START);
296 tprintf(" + %u", psm_he - L2CAP_PSM_LE_DYN_START);
297 } else if (psm_he >= L2CAP_PSM_DYN_START) {
298 print_xlat(L2CAP_PSM_DYN_START);
299 tprintf(" + %u", psm_he - L2CAP_PSM_DYN_START);
301 tprints("L2CAP_PSM_???");
304 if (xlat_verbose(xlat_verbosity) == XLAT_STYLE_VERBOSE || !psm_str)
307 print_bluetooth_l2_psm_end:
312 print_bluetooth_l2_cid(const char *prefix, uint16_t cid)
314 const uint16_t cid_he = btohs(cid);
315 const char *cid_name = xlookup(bluetooth_l2_cid, cid_he);
316 const bool cid_str = cid_name || (cid_he >= L2CAP_CID_DYN_START);
318 tprintf("%shtobs(", prefix);
320 if (xlat_verbose(xlat_verbosity) != XLAT_STYLE_ABBREV || !cid_str)
321 tprintf("%#x", cid_he);
323 if (xlat_verbose(xlat_verbosity) == XLAT_STYLE_RAW)
324 goto print_bluetooth_l2_cid_end;
326 if (xlat_verbose(xlat_verbosity) == XLAT_STYLE_VERBOSE || !cid_str)
331 } else if (cid_he >= L2CAP_CID_DYN_START) {
332 print_xlat(L2CAP_CID_DYN_START);
333 tprintf(" + %u", cid_he - L2CAP_CID_DYN_START);
335 tprints("L2CAP_CID_???");
338 if (xlat_verbose(xlat_verbosity) == XLAT_STYLE_VERBOSE || !cid_str)
341 print_bluetooth_l2_cid_end:
346 print_sockaddr_data_bt(const void *const buf, const int addrlen)
348 struct sockaddr_hci {
349 /* sa_family_t */ uint16_t hci_family;
351 uint16_t hci_channel;
358 struct sockaddr_sco {
359 /* sa_family_t */ uint16_t sco_family;
360 struct bdaddr sco_bdaddr;
364 /* sa_family_t */ uint16_t rc_family;
365 struct bdaddr rc_bdaddr;
370 /* sa_family_t */ uint16_t l2_family;
371 /* little endiang */ uint16_t l2_psm;
372 struct bdaddr l2_bdaddr;
373 /* little endian */ uint16_t l2_cid;
374 uint8_t l2_bdaddr_type;
378 case sizeof(struct sockaddr_hci): {
379 const struct sockaddr_hci *const hci = buf;
380 tprintf("hci_dev=htobs(%hu), hci_channel=",
381 btohs(hci->hci_dev));
382 printxval_index(hci_channels, hci->hci_channel,
386 case sizeof(struct sockaddr_sco): {
387 const struct sockaddr_sco *const sco = buf;
388 print_mac_addr("sco_bdaddr=", sco->sco_bdaddr.b,
389 sizeof(sco->sco_bdaddr.b));
392 case sizeof(struct sockaddr_rc): {
393 const struct sockaddr_rc *const rc = buf;
394 print_mac_addr("rc_bdaddr=", rc->rc_bdaddr.b,
395 sizeof(rc->rc_bdaddr.b));
396 tprintf(", rc_channel=%u", rc->rc_channel);
399 case offsetof(struct sockaddr_l2, l2_bdaddr_type):
400 case sizeof(struct sockaddr_l2): {
401 const struct sockaddr_l2 *const l2 = buf;
402 print_bluetooth_l2_psm("l2_psm=", l2->l2_psm);
403 print_mac_addr(", l2_bdaddr=", l2->l2_bdaddr.b,
404 sizeof(l2->l2_bdaddr.b));
405 print_bluetooth_l2_cid(", l2_cid=", l2->l2_cid);
407 if (addrlen == sizeof(struct sockaddr_l2)) {
408 tprints(", l2_bdaddr_type=");
409 printxval_index(bdaddr_types,
417 print_sockaddr_data_raw(buf, addrlen);
422 typedef void (* const sockaddr_printer)(const void *const, const int);
424 static const struct {
425 const sockaddr_printer printer;
428 [AF_UNIX] = { print_sockaddr_data_un, SIZEOF_SA_FAMILY + 1 },
429 [AF_INET] = { print_sockaddr_data_in, sizeof(struct sockaddr_in) },
430 [AF_IPX] = { print_sockaddr_data_ipx, sizeof(struct sockaddr_ipx) },
431 [AF_INET6] = { print_sockaddr_data_in6, SIN6_MIN_LEN },
432 [AF_NETLINK] = { print_sockaddr_data_nl, SIZEOF_SA_FAMILY + 1 },
433 [AF_PACKET] = { print_sockaddr_data_ll, sizeof(struct sockaddr_ll) },
434 [AF_BLUETOOTH] = { print_sockaddr_data_bt, SIZEOF_SA_FAMILY + 1 },
438 print_sockaddr(const void *const buf, const int addrlen)
440 const struct sockaddr *const sa = buf;
442 tprints("{sa_family=");
443 printxval_index(addrfams, sa->sa_family, "AF_???");
445 if (addrlen > (int) SIZEOF_SA_FAMILY) {
448 if (sa->sa_family < ARRAY_SIZE(sa_printers)
449 && sa_printers[sa->sa_family].printer
450 && addrlen >= sa_printers[sa->sa_family].min_len) {
451 sa_printers[sa->sa_family].printer(buf, addrlen);
453 print_sockaddr_data_raw(buf, addrlen);
461 decode_sockaddr(struct tcb *const tcp, const kernel_ulong_t addr, int addrlen)
470 struct sockaddr_storage storage;
471 char pad[sizeof(struct sockaddr_storage) + 1];
474 if ((unsigned) addrlen > sizeof(addrbuf.storage))
475 addrlen = sizeof(addrbuf.storage);
477 if (umoven_or_printaddr(tcp, addr, addrlen, addrbuf.pad))
480 memset(&addrbuf.pad[addrlen], 0, sizeof(addrbuf.pad) - addrlen);
482 print_sockaddr(&addrbuf, addrlen);
484 return addrbuf.sa.sa_family;