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_psm.h"
59 #include "xlat/hci_channels.h"
61 #define SIZEOF_SA_FAMILY sizeof(((struct sockaddr *) 0)->sa_family)
63 const size_t ethernet_protocols_size = ARRAY_SIZE(ethernet_protocols) - 1;
66 print_sockaddr_data_un(const void *const buf, const int addrlen)
68 const struct sockaddr_un *const sa_un = buf;
69 const int un_len = addrlen > (int) sizeof(*sa_un)
70 ? (int) sizeof(*sa_un) : addrlen;
71 const int path_len = un_len - SIZEOF_SA_FAMILY;
74 if (sa_un->sun_path[0]) {
75 print_quoted_string(sa_un->sun_path, path_len + 1,
79 print_quoted_string(sa_un->sun_path + 1, path_len - 1, 0);
84 print_inet_addr(const int af,
85 const void *const addr,
86 const unsigned int len,
87 const char *const var_name)
89 char buf[INET6_ADDRSTRLEN];
93 if (inet_ntop(af, addr, buf, sizeof(buf))) {
95 tprintf("%s=inet_addr(\"%s\")", var_name, buf);
102 if (inet_ntop(af, addr, buf, sizeof(buf))) {
104 tprintf("inet_pton(%s, \"%s\", &%s)",
105 "AF_INET6", buf, var_name);
114 tprintf("%s=", var_name);
115 print_quoted_string(addr, len, QUOTE_FORCE_HEX);
120 decode_inet_addr(struct tcb *const tcp,
121 const kernel_ulong_t addr,
122 const unsigned int len,
124 const char *const var_name)
134 size = sizeof(addrbuf.a4);
137 size = sizeof(addrbuf.a6);
141 if (!size || len < size) {
143 tprintf("%s=", var_name);
144 printstr_ex(tcp, addr, len, QUOTE_FORCE_HEX);
148 if (umoven(tcp, addr, size, &addrbuf) < 0) {
150 tprintf("%s=", var_name);
155 return print_inet_addr(family, &addrbuf, size, var_name);
159 print_sockaddr_data_in(const void *const buf, const int addrlen)
161 const struct sockaddr_in *const sa_in = buf;
163 PRINT_FIELD_NET_PORT("", *sa_in, sin_port);
164 PRINT_FIELD_INET4_ADDR(", ", *sa_in, sin_addr);
167 #define SIN6_MIN_LEN offsetof(struct sockaddr_in6, sin6_scope_id)
170 print_sockaddr_data_in6(const void *const buf, const int addrlen)
172 const struct sockaddr_in6 *const sa_in6 = buf;
174 PRINT_FIELD_NET_PORT("", *sa_in6, sin6_port);
175 PRINT_FIELD_INET_ADDR(", ", *sa_in6, sin6_addr, AF_INET6);
176 tprintf(", sin6_flowinfo=htonl(%u)", ntohl(sa_in6->sin6_flowinfo));
178 if (addrlen <= (int) SIN6_MIN_LEN)
181 #if defined IN6_IS_ADDR_LINKLOCAL && defined IN6_IS_ADDR_MC_LINKLOCAL
182 if (IN6_IS_ADDR_LINKLOCAL(&sa_in6->sin6_addr)
183 || IN6_IS_ADDR_MC_LINKLOCAL(&sa_in6->sin6_addr))
184 PRINT_FIELD_IFINDEX(", ", *sa_in6, sin6_scope_id);
187 PRINT_FIELD_U(", ", *sa_in6, sin6_scope_id);
191 print_sockaddr_data_ipx(const void *const buf, const int addrlen)
193 const struct sockaddr_ipx *const sa_ipx = buf;
196 PRINT_FIELD_NET_PORT("", *sa_ipx, sipx_port);
197 tprintf(", sipx_network=htonl(%#08x)"
199 ntohl(sa_ipx->sipx_network));
200 for (i = 0; i < IPX_NODE_LEN; ++i) {
201 tprintf("%s%#02x", i ? ", " : "",
202 sa_ipx->sipx_node[i]);
204 PRINT_FIELD_0X("], ", *sa_ipx, sipx_type);
208 print_sockaddr_data_nl(const void *const buf, const int addrlen)
210 const struct sockaddr_nl *const sa_nl = buf;
212 PRINT_FIELD_D("", *sa_nl, nl_pid);
213 PRINT_FIELD_0X(", ", *sa_nl, nl_groups);
217 print_sockaddr_data_ll(const void *const buf, const int addrlen)
219 const struct sockaddr_ll *const sa_ll = buf;
221 tprints("sll_protocol=htons(");
222 printxval_search(ethernet_protocols, ntohs(sa_ll->sll_protocol),
224 PRINT_FIELD_IFINDEX("), ", *sa_ll, sll_ifindex);
225 tprints(", sll_hatype=");
226 printxval(arp_hardware_types, sa_ll->sll_hatype, "ARPHRD_???");
227 tprints(", sll_pkttype=");
228 printxval(af_packet_types, sa_ll->sll_pkttype, "PACKET_???");
229 tprintf(", sll_halen=%u", sa_ll->sll_halen);
230 if (sa_ll->sll_halen) {
231 const unsigned int oob_halen =
232 addrlen - offsetof(struct sockaddr_ll, sll_addr);
235 tprints(", sll_addr=[");
236 for (i = 0; i < sa_ll->sll_halen; ++i) {
239 if (i >= oob_halen) {
243 tprintf("%#02x", sa_ll->sll_addr[i]);
250 print_sockaddr_data_raw(const void *const buf, const int addrlen)
252 const char *const data = buf + SIZEOF_SA_FAMILY;
253 const int datalen = addrlen - SIZEOF_SA_FAMILY;
256 print_quoted_string(data, datalen, 0);
262 #ifdef WORDS_BIGENDIAN
263 return (val << 8) | (val >> 8);
270 print_bluetooth_l2_psm(const char *prefix, uint16_t psm)
272 const uint16_t psm_he = btohs(psm);
273 const char *psm_name = xlookup(bluetooth_l2_psm, psm_he);
274 const bool psm_str = psm_name || (psm_he >= L2CAP_PSM_LE_DYN_START
275 && psm_he <= L2CAP_PSM_LE_DYN_END)
276 || (psm_he >= L2CAP_PSM_DYN_START);
278 tprintf("%shtobs(", prefix);
280 if (xlat_verbose(xlat_verbosity) != XLAT_STYLE_ABBREV || !psm_str)
281 tprintf("%#x", psm_he);
283 if (xlat_verbose(xlat_verbosity) == XLAT_STYLE_RAW)
284 goto print_bluetooth_l2_psm_end;
286 if (xlat_verbose(xlat_verbosity) == XLAT_STYLE_VERBOSE || !psm_str)
291 } else if (psm_he >= L2CAP_PSM_LE_DYN_START
292 && psm_he <= L2CAP_PSM_LE_DYN_END) {
293 print_xlat(L2CAP_PSM_LE_DYN_START);
294 tprintf(" + %u", psm_he - L2CAP_PSM_LE_DYN_START);
295 } else if (psm_he >= L2CAP_PSM_DYN_START) {
296 print_xlat(L2CAP_PSM_DYN_START);
297 tprintf(" + %u", psm_he - L2CAP_PSM_DYN_START);
299 tprints("L2CAP_PSM_???");
302 if (xlat_verbose(xlat_verbosity) == XLAT_STYLE_VERBOSE || !psm_str)
305 print_bluetooth_l2_psm_end:
310 print_sockaddr_data_bt(const void *const buf, const int addrlen)
312 struct sockaddr_hci {
313 /* sa_family_t */ uint16_t hci_family;
315 uint16_t hci_channel;
322 struct sockaddr_sco {
323 /* sa_family_t */ uint16_t sco_family;
324 struct bdaddr sco_bdaddr;
328 /* sa_family_t */ uint16_t rc_family;
329 struct bdaddr rc_bdaddr;
334 /* sa_family_t */ uint16_t l2_family;
335 /* little endiang */ uint16_t l2_psm;
336 struct bdaddr l2_bdaddr;
337 /* little endian */ uint16_t l2_cid;
338 uint8_t l2_bdaddr_type;
342 case sizeof(struct sockaddr_hci): {
343 const struct sockaddr_hci *const hci = buf;
344 tprintf("hci_dev=htobs(%hu), hci_channel=",
345 btohs(hci->hci_dev));
346 printxval(hci_channels, hci->hci_channel,
350 case sizeof(struct sockaddr_sco): {
351 const struct sockaddr_sco *const sco = buf;
352 tprintf("sco_bdaddr=%02x:%02x:%02x:%02x:%02x:%02x",
353 sco->sco_bdaddr.b[0], sco->sco_bdaddr.b[1],
354 sco->sco_bdaddr.b[2], sco->sco_bdaddr.b[3],
355 sco->sco_bdaddr.b[4], sco->sco_bdaddr.b[5]);
358 case sizeof(struct sockaddr_rc): {
359 const struct sockaddr_rc *const rc = buf;
360 tprintf("rc_bdaddr=%02x:%02x:%02x:%02x:%02x:%02x"
362 rc->rc_bdaddr.b[0], rc->rc_bdaddr.b[1],
363 rc->rc_bdaddr.b[2], rc->rc_bdaddr.b[3],
364 rc->rc_bdaddr.b[4], rc->rc_bdaddr.b[5],
368 case sizeof(struct sockaddr_l2): {
369 const struct sockaddr_l2 *const l2 = buf;
370 print_bluetooth_l2_psm("l2_psm=", l2->l2_psm);
371 tprintf(", l2_bdaddr=%02x:%02x:%02x:%02x:%02x:%02x"
372 ", l2_cid=htobs(%hu), l2_bdaddr_type=",
373 l2->l2_bdaddr.b[0], l2->l2_bdaddr.b[1],
374 l2->l2_bdaddr.b[2], l2->l2_bdaddr.b[3],
375 l2->l2_bdaddr.b[4], l2->l2_bdaddr.b[5],
377 printxval_index(bdaddr_types, l2->l2_bdaddr_type,
382 print_sockaddr_data_raw(buf, addrlen);
387 typedef void (* const sockaddr_printer)(const void *const, const int);
389 static const struct {
390 const sockaddr_printer printer;
393 [AF_UNIX] = { print_sockaddr_data_un, SIZEOF_SA_FAMILY + 1 },
394 [AF_INET] = { print_sockaddr_data_in, sizeof(struct sockaddr_in) },
395 [AF_IPX] = { print_sockaddr_data_ipx, sizeof(struct sockaddr_ipx) },
396 [AF_INET6] = { print_sockaddr_data_in6, SIN6_MIN_LEN },
397 [AF_NETLINK] = { print_sockaddr_data_nl, SIZEOF_SA_FAMILY + 1 },
398 [AF_PACKET] = { print_sockaddr_data_ll, sizeof(struct sockaddr_ll) },
399 [AF_BLUETOOTH] = { print_sockaddr_data_bt, SIZEOF_SA_FAMILY + 1 },
403 print_sockaddr(const void *const buf, const int addrlen)
405 const struct sockaddr *const sa = buf;
407 tprints("{sa_family=");
408 printxval(addrfams, sa->sa_family, "AF_???");
410 if (addrlen > (int) SIZEOF_SA_FAMILY) {
413 if (sa->sa_family < ARRAY_SIZE(sa_printers)
414 && sa_printers[sa->sa_family].printer
415 && addrlen >= sa_printers[sa->sa_family].min_len) {
416 sa_printers[sa->sa_family].printer(buf, addrlen);
418 print_sockaddr_data_raw(buf, addrlen);
426 decode_sockaddr(struct tcb *const tcp, const kernel_ulong_t addr, int addrlen)
435 struct sockaddr_storage storage;
436 char pad[sizeof(struct sockaddr_storage) + 1];
439 if ((unsigned) addrlen > sizeof(addrbuf.storage))
440 addrlen = sizeof(addrbuf.storage);
442 if (umoven_or_printaddr(tcp, addr, addrlen, addrbuf.pad))
445 memset(&addrbuf.pad[addrlen], 0, sizeof(addrbuf.pad) - addrlen);
447 print_sockaddr(&addrbuf, addrlen);
449 return addrbuf.sa.sa_family;