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-2017 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 #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>
64 # include "xlat/hci_channels.h"
67 #define SIZEOF_SA_FAMILY sizeof(((struct sockaddr *) 0)->sa_family)
69 const size_t ethernet_protocols_size = ARRAY_SIZE(ethernet_protocols) - 1;
72 print_sockaddr_data_un(const void *const buf, const int addrlen)
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;
80 if (sa_un->sun_path[0]) {
81 print_quoted_string(sa_un->sun_path, path_len + 1,
85 print_quoted_string(sa_un->sun_path + 1, path_len - 1, 0);
90 print_inet_addr(const int af,
91 const void *const addr,
92 const unsigned int len,
93 const char *const var_name)
95 char buf[INET6_ADDRSTRLEN];
99 if (inet_ntop(af, addr, buf, sizeof(buf))) {
101 tprintf("%s=inet_addr(\"%s\")", var_name, buf);
108 if (inet_ntop(af, addr, buf, sizeof(buf))) {
110 tprintf("inet_pton(%s, \"%s\", &%s)",
111 "AF_INET6", buf, var_name);
120 tprintf("%s=", var_name);
121 print_quoted_string(addr, len, QUOTE_FORCE_HEX);
126 decode_inet_addr(struct tcb *const tcp,
127 const kernel_ulong_t addr,
128 const unsigned int len,
130 const char *const var_name)
140 size = sizeof(addrbuf.a4);
143 size = sizeof(addrbuf.a6);
147 if (!size || len < size) {
149 tprintf("%s=", var_name);
150 printstr_ex(tcp, addr, len, QUOTE_FORCE_HEX);
154 if (umoven(tcp, addr, size, &addrbuf) < 0) {
156 tprintf("%s=", var_name);
161 return print_inet_addr(family, &addrbuf, size, var_name);
165 print_sockaddr_data_in(const void *const buf, const int addrlen)
167 const struct sockaddr_in *const sa_in = buf;
169 PRINT_FIELD_NET_PORT("", *sa_in, sin_port);
170 PRINT_FIELD_INET4_ADDR(", ", *sa_in, sin_addr);
173 #define SIN6_MIN_LEN offsetof(struct sockaddr_in6, sin6_scope_id)
176 print_sockaddr_data_in6(const void *const buf, const int addrlen)
178 const struct sockaddr_in6 *const sa_in6 = buf;
180 PRINT_FIELD_NET_PORT("", *sa_in6, sin6_port);
181 PRINT_FIELD_INET_ADDR(", ", *sa_in6, sin6_addr, AF_INET6);
182 tprintf(", sin6_flowinfo=htonl(%u)", ntohl(sa_in6->sin6_flowinfo));
184 if (addrlen <= (int) SIN6_MIN_LEN)
187 #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))
190 PRINT_FIELD_IFINDEX(", ", *sa_in6, sin6_scope_id);
193 PRINT_FIELD_U(", ", *sa_in6, sin6_scope_id);
197 print_sockaddr_data_ipx(const void *const buf, const int addrlen)
199 const struct sockaddr_ipx *const sa_ipx = buf;
202 PRINT_FIELD_NET_PORT("", *sa_ipx, sipx_port);
203 tprintf(", sipx_network=htonl(%#08x)"
205 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]);
210 PRINT_FIELD_0X("], ", *sa_ipx, sipx_type);
214 print_sockaddr_data_nl(const void *const buf, const int addrlen)
216 const struct sockaddr_nl *const sa_nl = buf;
218 PRINT_FIELD_D("", *sa_nl, nl_pid);
219 PRINT_FIELD_0X(", ", *sa_nl, nl_groups);
223 print_sockaddr_data_ll(const void *const buf, const int addrlen)
225 const struct sockaddr_ll *const sa_ll = buf;
227 tprints("sll_protocol=htons(");
228 printxval_search(ethernet_protocols, ntohs(sa_ll->sll_protocol),
230 PRINT_FIELD_IFINDEX("), ", *sa_ll, sll_ifindex);
231 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);
241 tprints(", sll_addr=[");
242 for (i = 0; i < sa_ll->sll_halen; ++i) {
245 if (i >= oob_halen) {
249 tprintf("%#02x", sa_ll->sll_addr[i]);
256 print_sockaddr_data_raw(const void *const buf, const int addrlen)
258 const char *const data = buf + SIZEOF_SA_FAMILY;
259 const int datalen = addrlen - SIZEOF_SA_FAMILY;
262 print_quoted_string(data, datalen, 0);
265 #ifdef HAVE_BLUETOOTH_BLUETOOTH_H
267 print_sockaddr_data_bt(const void *const buf, const int 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,
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]);
286 case sizeof(struct sockaddr_rc): {
287 const struct sockaddr_rc *const rc = buf;
288 tprintf("rc_bdaddr=%02x:%02x:%02x:%02x:%02x:%02x"
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],
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",
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);
309 print_sockaddr_data_raw(buf, addrlen);
313 #endif /* HAVE_BLUETOOTH_BLUETOOTH_H */
315 typedef void (* const sockaddr_printer)(const void *const, const int);
317 static const struct {
318 const sockaddr_printer printer;
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 },
333 print_sockaddr(const void *const buf, const int addrlen)
335 const struct sockaddr *const sa = buf;
337 tprints("{sa_family=");
338 printxval(addrfams, sa->sa_family, "AF_???");
340 if (addrlen > (int) SIZEOF_SA_FAMILY) {
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);
348 print_sockaddr_data_raw(buf, addrlen);
356 decode_sockaddr(struct tcb *const tcp, const kernel_ulong_t addr, int addrlen)
365 struct sockaddr_storage storage;
366 char pad[sizeof(struct sockaddr_storage) + 1];
369 if ((unsigned) addrlen > sizeof(addrbuf.storage))
370 addrlen = sizeof(addrbuf.storage);
372 if (umoven_or_printaddr(tcp, addr, addrlen, addrbuf.pad))
375 memset(&addrbuf.pad[addrlen], 0, sizeof(addrbuf.pad) - addrlen);
377 print_sockaddr(&addrbuf, addrlen);
379 return addrbuf.sa.sa_family;