]> granicus.if.org Git - strace/blob - sockaddr.c
linux/x32/syscallent.h: change 64-bit syscall designation
[strace] / sockaddr.c
1 /*
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.
8  * 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"
34 #include "print_fields.h"
35
36 #include <sys/socket.h>
37 #include <sys/un.h>
38 #include <netinet/in.h>
39 #include <arpa/inet.h>
40
41 #include "netlink.h"
42 #include <linux/if_packet.h>
43 #include <linux/if_arp.h>
44 #include <linux/if_ether.h>
45
46 #ifdef HAVE_NETIPX_IPX_H
47 # include <netipx/ipx.h>
48 #else
49 # include <linux/ipx.h>
50 #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
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"
61
62 #define SIZEOF_SA_FAMILY sizeof(((struct sockaddr *) 0)->sa_family)
63
64 const size_t ethernet_protocols_size = ARRAY_SIZE(ethernet_protocols) - 1;
65
66 static void
67 print_sockaddr_data_un(const void *const buf, const int addrlen)
68 {
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;
73
74         tprints("sun_path=");
75         if (sa_un->sun_path[0]) {
76                 print_quoted_string(sa_un->sun_path, path_len + 1,
77                                     QUOTE_0_TERMINATED);
78         } else {
79                 tprints("@");
80                 print_quoted_string(sa_un->sun_path + 1, path_len - 1, 0);
81         }
82 }
83
84 bool
85 print_inet_addr(const int af,
86                 const void *const addr,
87                 const unsigned int len,
88                 const char *const var_name)
89 {
90         char buf[INET6_ADDRSTRLEN];
91
92         switch (af) {
93         case AF_INET:
94                 if (inet_ntop(af, addr, buf, sizeof(buf))) {
95                         if (var_name)
96                                 tprintf("%s=inet_addr(\"%s\")", var_name, buf);
97                         else
98                                 tprints(buf);
99                         return true;
100                 }
101                 break;
102         case AF_INET6:
103                 if (inet_ntop(af, addr, buf, sizeof(buf))) {
104                         if (var_name)
105                                 tprintf("inet_pton(%s, \"%s\", &%s)",
106                                         "AF_INET6", buf, var_name);
107                         else
108                                 tprints(buf);
109                         return true;
110                 }
111                 break;
112         }
113
114         if (var_name)
115                 tprintf("%s=", var_name);
116         print_quoted_string(addr, len, QUOTE_FORCE_HEX);
117         return false;
118 }
119
120 bool
121 decode_inet_addr(struct tcb *const tcp,
122                  const kernel_ulong_t addr,
123                  const unsigned int len,
124                  const int family,
125                  const char *const var_name)
126 {
127         union {
128                 struct in_addr  a4;
129                 struct in6_addr a6;
130         } addrbuf;
131         size_t size = 0;
132
133         switch (family) {
134         case AF_INET:
135                 size = sizeof(addrbuf.a4);
136                 break;
137         case AF_INET6:
138                 size = sizeof(addrbuf.a6);
139                 break;
140         }
141
142         if (!size || len < size) {
143                 if (var_name)
144                         tprintf("%s=", var_name);
145                 printstr_ex(tcp, addr, len, QUOTE_FORCE_HEX);
146                 return false;
147         }
148
149         if (umoven(tcp, addr, size, &addrbuf) < 0) {
150                 if (var_name)
151                         tprintf("%s=", var_name);
152                 printaddr(addr);
153                 return false;
154         }
155
156         return print_inet_addr(family, &addrbuf, size, var_name);
157 }
158
159 static void
160 print_sockaddr_data_in(const void *const buf, const int addrlen)
161 {
162         const struct sockaddr_in *const sa_in = buf;
163
164         PRINT_FIELD_NET_PORT("", *sa_in, sin_port);
165         PRINT_FIELD_INET4_ADDR(", ", *sa_in, sin_addr);
166 }
167
168 #define SIN6_MIN_LEN offsetof(struct sockaddr_in6, sin6_scope_id)
169
170 static void
171 print_sockaddr_data_in6(const void *const buf, const int addrlen)
172 {
173         const struct sockaddr_in6 *const sa_in6 = buf;
174
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));
178
179         if (addrlen <= (int) SIN6_MIN_LEN)
180                 return;
181
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);
186         else
187 #endif
188                 PRINT_FIELD_U(", ", *sa_in6, sin6_scope_id);
189 }
190
191 static void
192 print_sockaddr_data_ipx(const void *const buf, const int addrlen)
193 {
194         const struct sockaddr_ipx *const sa_ipx = buf;
195         unsigned int i;
196
197         PRINT_FIELD_NET_PORT("", *sa_ipx, sipx_port);
198         tprintf(", sipx_network=htonl(%#08x)"
199                 ", sipx_node=[",
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]);
204         }
205         PRINT_FIELD_0X("], ", *sa_ipx, sipx_type);
206 }
207
208 static void
209 print_sockaddr_data_nl(const void *const buf, const int addrlen)
210 {
211         const struct sockaddr_nl *const sa_nl = buf;
212
213         PRINT_FIELD_D("", *sa_nl, nl_pid);
214         PRINT_FIELD_0X(", ", *sa_nl, nl_groups);
215 }
216
217 static void
218 print_sockaddr_data_ll(const void *const buf, const int addrlen)
219 {
220         const struct sockaddr_ll *const sa_ll = buf;
221
222         tprints("sll_protocol=htons(");
223         printxval_search(ethernet_protocols, ntohs(sa_ll->sll_protocol),
224                          "ETH_P_???");
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);
234                 unsigned int i;
235
236                 tprints(", sll_addr=[");
237                 for (i = 0; i < sa_ll->sll_halen; ++i) {
238                         if (i)
239                                 tprints(", ");
240                         if (i >= oob_halen) {
241                                 tprints("...");
242                                 break;
243                         }
244                         tprintf("%#02x", sa_ll->sll_addr[i]);
245                 }
246                 tprints("]");
247         }
248 }
249
250 static void
251 print_sockaddr_data_raw(const void *const buf, const int addrlen)
252 {
253         const char *const data = buf + SIZEOF_SA_FAMILY;
254         const int datalen = addrlen - SIZEOF_SA_FAMILY;
255
256         tprints("sa_data=");
257         print_quoted_string(data, datalen, 0);
258 }
259
260 static uint16_t
261 btohs(uint16_t val)
262 {
263 #ifdef WORDS_BIGENDIAN
264         return (val << 8) | (val >> 8);
265 #else
266         return val;
267 #endif
268 }
269
270 static void
271 print_bluetooth_l2_psm(const char *prefix, uint16_t psm)
272 {
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);
278
279         tprintf("%shtobs(", prefix);
280
281         if (xlat_verbose(xlat_verbosity) != XLAT_STYLE_ABBREV || !psm_str)
282                 tprintf("%#x", psm_he);
283
284         if (xlat_verbose(xlat_verbosity) == XLAT_STYLE_RAW)
285                 goto print_bluetooth_l2_psm_end;
286
287         if (xlat_verbose(xlat_verbosity) == XLAT_STYLE_VERBOSE || !psm_str)
288                 tprints(" /* ");
289
290         if (psm_name) {
291                 tprints(psm_name);
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);
299         } else {
300                 tprints("L2CAP_PSM_???");
301         }
302
303         if (xlat_verbose(xlat_verbosity) == XLAT_STYLE_VERBOSE || !psm_str)
304                 tprints(" */");
305
306 print_bluetooth_l2_psm_end:
307         tprints(")");
308 }
309
310 static void
311 print_bluetooth_l2_cid(const char *prefix, uint16_t cid)
312 {
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);
316
317         tprintf("%shtobs(", prefix);
318
319         if (xlat_verbose(xlat_verbosity) != XLAT_STYLE_ABBREV || !cid_str)
320                 tprintf("%#x", cid_he);
321
322         if (xlat_verbose(xlat_verbosity) == XLAT_STYLE_RAW)
323                 goto print_bluetooth_l2_cid_end;
324
325         if (xlat_verbose(xlat_verbosity) == XLAT_STYLE_VERBOSE || !cid_str)
326                 tprints(" /* ");
327
328         if (cid_name) {
329                 tprints(cid_name);
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);
333         } else {
334                 tprints("L2CAP_CID_???");
335         }
336
337         if (xlat_verbose(xlat_verbosity) == XLAT_STYLE_VERBOSE || !cid_str)
338                 tprints(" */");
339
340 print_bluetooth_l2_cid_end:
341         tprints(")");
342 }
343
344 static void
345 print_sockaddr_data_bt(const void *const buf, const int addrlen)
346 {
347         struct sockaddr_hci {
348                 /* sa_family_t */ uint16_t      hci_family;
349                 uint16_t                        hci_dev;
350                 uint16_t                        hci_channel;
351         };
352
353         struct bdaddr {
354                 uint8_t                         b[6];
355         } ATTRIBUTE_PACKED;
356
357         struct sockaddr_sco {
358                 /* sa_family_t */ uint16_t      sco_family;
359                 struct bdaddr                   sco_bdaddr;
360         };
361
362         struct sockaddr_rc {
363                 /* sa_family_t */ uint16_t      rc_family;
364                 struct bdaddr                   rc_bdaddr;
365                 uint8_t                         rc_channel;
366         };
367
368         struct sockaddr_l2 {
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;
374         };
375
376         switch (addrlen) {
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,
382                                         "HCI_CHANNEL_???");
383                         break;
384                 }
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));
389                         break;
390                 }
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);
396                         break;
397                 }
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,
406                                         "BDADDR_???");
407                         break;
408                 }
409                 default:
410                         print_sockaddr_data_raw(buf, addrlen);
411                         break;
412         }
413 }
414
415 typedef void (* const sockaddr_printer)(const void *const, const int);
416
417 static const struct {
418         const sockaddr_printer printer;
419         const int min_len;
420 } sa_printers[] = {
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 },
428 };
429
430 void
431 print_sockaddr(const void *const buf, const int addrlen)
432 {
433         const struct sockaddr *const sa = buf;
434
435         tprints("{sa_family=");
436         printxval_index(addrfams, sa->sa_family, "AF_???");
437
438         if (addrlen > (int) SIZEOF_SA_FAMILY) {
439                 tprints(", ");
440
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);
445                 } else {
446                         print_sockaddr_data_raw(buf, addrlen);
447                 }
448         }
449
450         tprints("}");
451 }
452
453 int
454 decode_sockaddr(struct tcb *const tcp, const kernel_ulong_t addr, int addrlen)
455 {
456         if (addrlen < 2) {
457                 printaddr(addr);
458                 return -1;
459         }
460
461         union {
462                 struct sockaddr sa;
463                 struct sockaddr_storage storage;
464                 char pad[sizeof(struct sockaddr_storage) + 1];
465         } addrbuf;
466
467         if ((unsigned) addrlen > sizeof(addrbuf.storage))
468                 addrlen = sizeof(addrbuf.storage);
469
470         if (umoven_or_printaddr(tcp, addr, addrlen, addrbuf.pad))
471                 return -1;
472
473         memset(&addrbuf.pad[addrlen], 0, sizeof(addrbuf.pad) - addrlen);
474
475         print_sockaddr(&addrbuf, addrlen);
476
477         return addrbuf.sa.sa_family;
478 }