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 ethernet_protocols_size = ARRAY_SIZE(ethernet_protocols) - 1;
67 print_sockaddr_data_un(const void *const buf, const int addrlen)
69 const struct sockaddr_un *const sa_un = buf;
70 const int un_len = addrlen > (int) sizeof(*sa_un)
71 ? (int) sizeof(*sa_un) : addrlen;
72 const int path_len = un_len - SIZEOF_SA_FAMILY;
75 if (sa_un->sun_path[0]) {
76 print_quoted_string(sa_un->sun_path, path_len + 1,
80 print_quoted_string(sa_un->sun_path + 1, path_len - 1, 0);
85 print_inet_addr(const int af,
86 const void *const addr,
87 const unsigned int len,
88 const char *const var_name)
90 char buf[INET6_ADDRSTRLEN];
94 if (inet_ntop(af, addr, buf, sizeof(buf))) {
96 tprintf("%s=inet_addr(\"%s\")", var_name, buf);
103 if (inet_ntop(af, addr, buf, sizeof(buf))) {
105 tprintf("inet_pton(%s, \"%s\", &%s)",
106 "AF_INET6", buf, var_name);
115 tprintf("%s=", var_name);
116 print_quoted_string(addr, len, QUOTE_FORCE_HEX);
121 decode_inet_addr(struct tcb *const tcp,
122 const kernel_ulong_t addr,
123 const unsigned int len,
125 const char *const var_name)
135 size = sizeof(addrbuf.a4);
138 size = sizeof(addrbuf.a6);
142 if (!size || len < size) {
144 tprintf("%s=", var_name);
145 printstr_ex(tcp, addr, len, QUOTE_FORCE_HEX);
149 if (umoven(tcp, addr, size, &addrbuf) < 0) {
151 tprintf("%s=", var_name);
156 return print_inet_addr(family, &addrbuf, size, var_name);
160 print_sockaddr_data_in(const void *const buf, const int addrlen)
162 const struct sockaddr_in *const sa_in = buf;
164 PRINT_FIELD_NET_PORT("", *sa_in, sin_port);
165 PRINT_FIELD_INET4_ADDR(", ", *sa_in, sin_addr);
168 #define SIN6_MIN_LEN offsetof(struct sockaddr_in6, sin6_scope_id)
171 print_sockaddr_data_in6(const void *const buf, const int addrlen)
173 const struct sockaddr_in6 *const sa_in6 = buf;
175 PRINT_FIELD_NET_PORT("", *sa_in6, sin6_port);
176 PRINT_FIELD_INET_ADDR(", ", *sa_in6, sin6_addr, AF_INET6);
177 tprintf(", sin6_flowinfo=htonl(%u)", ntohl(sa_in6->sin6_flowinfo));
179 if (addrlen <= (int) SIN6_MIN_LEN)
182 #if defined IN6_IS_ADDR_LINKLOCAL && defined IN6_IS_ADDR_MC_LINKLOCAL
183 if (IN6_IS_ADDR_LINKLOCAL(&sa_in6->sin6_addr)
184 || IN6_IS_ADDR_MC_LINKLOCAL(&sa_in6->sin6_addr))
185 PRINT_FIELD_IFINDEX(", ", *sa_in6, sin6_scope_id);
188 PRINT_FIELD_U(", ", *sa_in6, sin6_scope_id);
192 print_sockaddr_data_ipx(const void *const buf, const int addrlen)
194 const struct sockaddr_ipx *const sa_ipx = buf;
197 PRINT_FIELD_NET_PORT("", *sa_ipx, sipx_port);
198 tprintf(", sipx_network=htonl(%#08x)"
200 ntohl(sa_ipx->sipx_network));
201 for (i = 0; i < IPX_NODE_LEN; ++i) {
202 tprintf("%s%#02x", i ? ", " : "",
203 sa_ipx->sipx_node[i]);
205 PRINT_FIELD_0X("], ", *sa_ipx, sipx_type);
209 print_sockaddr_data_nl(const void *const buf, const int addrlen)
211 const struct sockaddr_nl *const sa_nl = buf;
213 PRINT_FIELD_D("", *sa_nl, nl_pid);
214 PRINT_FIELD_0X(", ", *sa_nl, nl_groups);
218 print_sockaddr_data_ll(const void *const buf, const int addrlen)
220 const struct sockaddr_ll *const sa_ll = buf;
222 tprints("sll_protocol=htons(");
223 printxval_search(ethernet_protocols, ntohs(sa_ll->sll_protocol),
225 PRINT_FIELD_IFINDEX("), ", *sa_ll, sll_ifindex);
226 tprints(", sll_hatype=");
227 printxval_search(arp_hardware_types, sa_ll->sll_hatype, "ARPHRD_???");
228 tprints(", sll_pkttype=");
229 printxval_index(af_packet_types, sa_ll->sll_pkttype, "PACKET_???");
230 tprintf(", sll_halen=%u", sa_ll->sll_halen);
231 if (sa_ll->sll_halen) {
232 const unsigned int oob_halen =
233 addrlen - offsetof(struct sockaddr_ll, sll_addr);
236 tprints(", sll_addr=[");
237 for (i = 0; i < sa_ll->sll_halen; ++i) {
240 if (i >= oob_halen) {
244 tprintf("%#02x", sa_ll->sll_addr[i]);
251 print_sockaddr_data_raw(const void *const buf, const int addrlen)
253 const char *const data = buf + SIZEOF_SA_FAMILY;
254 const int datalen = addrlen - SIZEOF_SA_FAMILY;
257 print_quoted_string(data, datalen, 0);
263 #ifdef WORDS_BIGENDIAN
264 return (val << 8) | (val >> 8);
271 print_bluetooth_l2_psm(const char *prefix, uint16_t psm)
273 const uint16_t psm_he = btohs(psm);
274 const char *psm_name = xlookup(bluetooth_l2_psm, psm_he);
275 const bool psm_str = psm_name || (psm_he >= L2CAP_PSM_LE_DYN_START
276 && psm_he <= L2CAP_PSM_LE_DYN_END)
277 || (psm_he >= L2CAP_PSM_DYN_START);
279 tprintf("%shtobs(", prefix);
281 if (xlat_verbose(xlat_verbosity) != XLAT_STYLE_ABBREV || !psm_str)
282 tprintf("%#x", psm_he);
284 if (xlat_verbose(xlat_verbosity) == XLAT_STYLE_RAW)
285 goto print_bluetooth_l2_psm_end;
287 if (xlat_verbose(xlat_verbosity) == XLAT_STYLE_VERBOSE || !psm_str)
292 } else if (psm_he >= L2CAP_PSM_LE_DYN_START
293 && psm_he <= L2CAP_PSM_LE_DYN_END) {
294 print_xlat(L2CAP_PSM_LE_DYN_START);
295 tprintf(" + %u", psm_he - L2CAP_PSM_LE_DYN_START);
296 } else if (psm_he >= L2CAP_PSM_DYN_START) {
297 print_xlat(L2CAP_PSM_DYN_START);
298 tprintf(" + %u", psm_he - L2CAP_PSM_DYN_START);
300 tprints("L2CAP_PSM_???");
303 if (xlat_verbose(xlat_verbosity) == XLAT_STYLE_VERBOSE || !psm_str)
306 print_bluetooth_l2_psm_end:
311 print_bluetooth_l2_cid(const char *prefix, uint16_t cid)
313 const uint16_t cid_he = btohs(cid);
314 const char *cid_name = xlookup(bluetooth_l2_cid, cid_he);
315 const bool cid_str = cid_name || (cid_he >= L2CAP_CID_DYN_START);
317 tprintf("%shtobs(", prefix);
319 if (xlat_verbose(xlat_verbosity) != XLAT_STYLE_ABBREV || !cid_str)
320 tprintf("%#x", cid_he);
322 if (xlat_verbose(xlat_verbosity) == XLAT_STYLE_RAW)
323 goto print_bluetooth_l2_cid_end;
325 if (xlat_verbose(xlat_verbosity) == XLAT_STYLE_VERBOSE || !cid_str)
330 } else if (cid_he >= L2CAP_CID_DYN_START) {
331 print_xlat(L2CAP_CID_DYN_START);
332 tprintf(" + %u", cid_he - L2CAP_CID_DYN_START);
334 tprints("L2CAP_CID_???");
337 if (xlat_verbose(xlat_verbosity) == XLAT_STYLE_VERBOSE || !cid_str)
340 print_bluetooth_l2_cid_end:
345 print_sockaddr_data_bt(const void *const buf, const int addrlen)
347 struct sockaddr_hci {
348 /* sa_family_t */ uint16_t hci_family;
350 uint16_t hci_channel;
357 struct sockaddr_sco {
358 /* sa_family_t */ uint16_t sco_family;
359 struct bdaddr sco_bdaddr;
363 /* sa_family_t */ uint16_t rc_family;
364 struct bdaddr rc_bdaddr;
369 /* sa_family_t */ uint16_t l2_family;
370 /* little endiang */ uint16_t l2_psm;
371 struct bdaddr l2_bdaddr;
372 /* little endian */ uint16_t l2_cid;
373 uint8_t l2_bdaddr_type;
377 case sizeof(struct sockaddr_hci): {
378 const struct sockaddr_hci *const hci = buf;
379 tprintf("hci_dev=htobs(%hu), hci_channel=",
380 btohs(hci->hci_dev));
381 printxval_index(hci_channels, hci->hci_channel,
385 case sizeof(struct sockaddr_sco): {
386 const struct sockaddr_sco *const sco = buf;
387 print_mac_addr("sco_bdaddr=", sco->sco_bdaddr.b,
388 sizeof(sco->sco_bdaddr.b));
391 case sizeof(struct sockaddr_rc): {
392 const struct sockaddr_rc *const rc = buf;
393 print_mac_addr("rc_bdaddr=", rc->rc_bdaddr.b,
394 sizeof(rc->rc_bdaddr.b));
395 tprintf(", rc_channel=%u", rc->rc_channel);
398 case sizeof(struct sockaddr_l2): {
399 const struct sockaddr_l2 *const l2 = buf;
400 print_bluetooth_l2_psm("l2_psm=", l2->l2_psm);
401 print_mac_addr(", l2_bdaddr=", l2->l2_bdaddr.b,
402 sizeof(l2->l2_bdaddr.b));
403 print_bluetooth_l2_cid(", l2_cid=", l2->l2_cid);
404 tprints(", l2_bdaddr_type=");
405 printxval_index(bdaddr_types, l2->l2_bdaddr_type,
410 print_sockaddr_data_raw(buf, addrlen);
415 typedef void (* const sockaddr_printer)(const void *const, const int);
417 static const struct {
418 const sockaddr_printer printer;
421 [AF_UNIX] = { print_sockaddr_data_un, SIZEOF_SA_FAMILY + 1 },
422 [AF_INET] = { print_sockaddr_data_in, sizeof(struct sockaddr_in) },
423 [AF_IPX] = { print_sockaddr_data_ipx, sizeof(struct sockaddr_ipx) },
424 [AF_INET6] = { print_sockaddr_data_in6, SIN6_MIN_LEN },
425 [AF_NETLINK] = { print_sockaddr_data_nl, SIZEOF_SA_FAMILY + 1 },
426 [AF_PACKET] = { print_sockaddr_data_ll, sizeof(struct sockaddr_ll) },
427 [AF_BLUETOOTH] = { print_sockaddr_data_bt, SIZEOF_SA_FAMILY + 1 },
431 print_sockaddr(const void *const buf, const int addrlen)
433 const struct sockaddr *const sa = buf;
435 tprints("{sa_family=");
436 printxval_index(addrfams, sa->sa_family, "AF_???");
438 if (addrlen > (int) SIZEOF_SA_FAMILY) {
441 if (sa->sa_family < ARRAY_SIZE(sa_printers)
442 && sa_printers[sa->sa_family].printer
443 && addrlen >= sa_printers[sa->sa_family].min_len) {
444 sa_printers[sa->sa_family].printer(buf, addrlen);
446 print_sockaddr_data_raw(buf, addrlen);
454 decode_sockaddr(struct tcb *const tcp, const kernel_ulong_t addr, int addrlen)
463 struct sockaddr_storage storage;
464 char pad[sizeof(struct sockaddr_storage) + 1];
467 if ((unsigned) addrlen > sizeof(addrbuf.storage))
468 addrlen = sizeof(addrbuf.storage);
470 if (umoven_or_printaddr(tcp, addr, addrlen, addrbuf.pad))
473 memset(&addrbuf.pad[addrlen], 0, sizeof(addrbuf.pad) - addrlen);
475 print_sockaddr(&addrbuf, addrlen);
477 return addrbuf.sa.sa_family;