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