]> granicus.if.org Git - strace/blob - sockaddr.c
sockaddr: decode Bluetooth L2 PSM values
[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_psm.h"
59 #include "xlat/hci_channels.h"
60
61 #define SIZEOF_SA_FAMILY sizeof(((struct sockaddr *) 0)->sa_family)
62
63 const size_t ethernet_protocols_size = ARRAY_SIZE(ethernet_protocols) - 1;
64
65 static void
66 print_sockaddr_data_un(const void *const buf, const int addrlen)
67 {
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;
72
73         tprints("sun_path=");
74         if (sa_un->sun_path[0]) {
75                 print_quoted_string(sa_un->sun_path, path_len + 1,
76                                     QUOTE_0_TERMINATED);
77         } else {
78                 tprints("@");
79                 print_quoted_string(sa_un->sun_path + 1, path_len - 1, 0);
80         }
81 }
82
83 bool
84 print_inet_addr(const int af,
85                 const void *const addr,
86                 const unsigned int len,
87                 const char *const var_name)
88 {
89         char buf[INET6_ADDRSTRLEN];
90
91         switch (af) {
92         case AF_INET:
93                 if (inet_ntop(af, addr, buf, sizeof(buf))) {
94                         if (var_name)
95                                 tprintf("%s=inet_addr(\"%s\")", var_name, buf);
96                         else
97                                 tprints(buf);
98                         return true;
99                 }
100                 break;
101         case AF_INET6:
102                 if (inet_ntop(af, addr, buf, sizeof(buf))) {
103                         if (var_name)
104                                 tprintf("inet_pton(%s, \"%s\", &%s)",
105                                         "AF_INET6", buf, var_name);
106                         else
107                                 tprints(buf);
108                         return true;
109                 }
110                 break;
111         }
112
113         if (var_name)
114                 tprintf("%s=", var_name);
115         print_quoted_string(addr, len, QUOTE_FORCE_HEX);
116         return false;
117 }
118
119 bool
120 decode_inet_addr(struct tcb *const tcp,
121                  const kernel_ulong_t addr,
122                  const unsigned int len,
123                  const int family,
124                  const char *const var_name)
125 {
126         union {
127                 struct in_addr  a4;
128                 struct in6_addr a6;
129         } addrbuf;
130         size_t size = 0;
131
132         switch (family) {
133         case AF_INET:
134                 size = sizeof(addrbuf.a4);
135                 break;
136         case AF_INET6:
137                 size = sizeof(addrbuf.a6);
138                 break;
139         }
140
141         if (!size || len < size) {
142                 if (var_name)
143                         tprintf("%s=", var_name);
144                 printstr_ex(tcp, addr, len, QUOTE_FORCE_HEX);
145                 return false;
146         }
147
148         if (umoven(tcp, addr, size, &addrbuf) < 0) {
149                 if (var_name)
150                         tprintf("%s=", var_name);
151                 printaddr(addr);
152                 return false;
153         }
154
155         return print_inet_addr(family, &addrbuf, size, var_name);
156 }
157
158 static void
159 print_sockaddr_data_in(const void *const buf, const int addrlen)
160 {
161         const struct sockaddr_in *const sa_in = buf;
162
163         PRINT_FIELD_NET_PORT("", *sa_in, sin_port);
164         PRINT_FIELD_INET4_ADDR(", ", *sa_in, sin_addr);
165 }
166
167 #define SIN6_MIN_LEN offsetof(struct sockaddr_in6, sin6_scope_id)
168
169 static void
170 print_sockaddr_data_in6(const void *const buf, const int addrlen)
171 {
172         const struct sockaddr_in6 *const sa_in6 = buf;
173
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));
177
178         if (addrlen <= (int) SIN6_MIN_LEN)
179                 return;
180
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);
185         else
186 #endif
187                 PRINT_FIELD_U(", ", *sa_in6, sin6_scope_id);
188 }
189
190 static void
191 print_sockaddr_data_ipx(const void *const buf, const int addrlen)
192 {
193         const struct sockaddr_ipx *const sa_ipx = buf;
194         unsigned int i;
195
196         PRINT_FIELD_NET_PORT("", *sa_ipx, sipx_port);
197         tprintf(", sipx_network=htonl(%#08x)"
198                 ", sipx_node=[",
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]);
203         }
204         PRINT_FIELD_0X("], ", *sa_ipx, sipx_type);
205 }
206
207 static void
208 print_sockaddr_data_nl(const void *const buf, const int addrlen)
209 {
210         const struct sockaddr_nl *const sa_nl = buf;
211
212         PRINT_FIELD_D("", *sa_nl, nl_pid);
213         PRINT_FIELD_0X(", ", *sa_nl, nl_groups);
214 }
215
216 static void
217 print_sockaddr_data_ll(const void *const buf, const int addrlen)
218 {
219         const struct sockaddr_ll *const sa_ll = buf;
220
221         tprints("sll_protocol=htons(");
222         printxval_search(ethernet_protocols, ntohs(sa_ll->sll_protocol),
223                          "ETH_P_???");
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);
233                 unsigned int i;
234
235                 tprints(", sll_addr=[");
236                 for (i = 0; i < sa_ll->sll_halen; ++i) {
237                         if (i)
238                                 tprints(", ");
239                         if (i >= oob_halen) {
240                                 tprints("...");
241                                 break;
242                         }
243                         tprintf("%#02x", sa_ll->sll_addr[i]);
244                 }
245                 tprints("]");
246         }
247 }
248
249 static void
250 print_sockaddr_data_raw(const void *const buf, const int addrlen)
251 {
252         const char *const data = buf + SIZEOF_SA_FAMILY;
253         const int datalen = addrlen - SIZEOF_SA_FAMILY;
254
255         tprints("sa_data=");
256         print_quoted_string(data, datalen, 0);
257 }
258
259 static uint16_t
260 btohs(uint16_t val)
261 {
262 #ifdef WORDS_BIGENDIAN
263         return (val << 8) | (val >> 8);
264 #else
265         return val;
266 #endif
267 }
268
269 static void
270 print_bluetooth_l2_psm(const char *prefix, uint16_t psm)
271 {
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);
277
278         tprintf("%shtobs(", prefix);
279
280         if (xlat_verbose(xlat_verbosity) != XLAT_STYLE_ABBREV || !psm_str)
281                 tprintf("%#x", psm_he);
282
283         if (xlat_verbose(xlat_verbosity) == XLAT_STYLE_RAW)
284                 goto print_bluetooth_l2_psm_end;
285
286         if (xlat_verbose(xlat_verbosity) == XLAT_STYLE_VERBOSE || !psm_str)
287                 tprints(" /* ");
288
289         if (psm_name) {
290                 tprints(psm_name);
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);
298         } else {
299                 tprints("L2CAP_PSM_???");
300         }
301
302         if (xlat_verbose(xlat_verbosity) == XLAT_STYLE_VERBOSE || !psm_str)
303                 tprints(" */");
304
305 print_bluetooth_l2_psm_end:
306         tprints(")");
307 }
308
309 static void
310 print_sockaddr_data_bt(const void *const buf, const int addrlen)
311 {
312         struct sockaddr_hci {
313                 /* sa_family_t */ uint16_t      hci_family;
314                 uint16_t                        hci_dev;
315                 uint16_t                        hci_channel;
316         };
317
318         struct bdaddr {
319                 uint8_t                         b[6];
320         } ATTRIBUTE_PACKED;
321
322         struct sockaddr_sco {
323                 /* sa_family_t */ uint16_t      sco_family;
324                 struct bdaddr                   sco_bdaddr;
325         };
326
327         struct sockaddr_rc {
328                 /* sa_family_t */ uint16_t      rc_family;
329                 struct bdaddr                   rc_bdaddr;
330                 uint8_t                         rc_channel;
331         };
332
333         struct sockaddr_l2 {
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;
339         };
340
341         switch (addrlen) {
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,
347                                   "HCI_CHANNEL_???");
348                         break;
349                 }
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]);
356                         break;
357                 }
358                 case sizeof(struct sockaddr_rc): {
359                         const struct sockaddr_rc *const rc = buf;
360                         tprintf("rc_bdaddr=%02x:%02x:%02x:%02x:%02x:%02x"
361                                 ", rc_channel=%u",
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],
365                                 rc->rc_channel);
366                         break;
367                 }
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],
376                                 btohs(l2->l2_cid));
377                         printxval_index(bdaddr_types, l2->l2_bdaddr_type,
378                                         "BDADDR_???");
379                         break;
380                 }
381                 default:
382                         print_sockaddr_data_raw(buf, addrlen);
383                         break;
384         }
385 }
386
387 typedef void (* const sockaddr_printer)(const void *const, const int);
388
389 static const struct {
390         const sockaddr_printer printer;
391         const int min_len;
392 } sa_printers[] = {
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 },
400 };
401
402 void
403 print_sockaddr(const void *const buf, const int addrlen)
404 {
405         const struct sockaddr *const sa = buf;
406
407         tprints("{sa_family=");
408         printxval(addrfams, sa->sa_family, "AF_???");
409
410         if (addrlen > (int) SIZEOF_SA_FAMILY) {
411                 tprints(", ");
412
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);
417                 } else {
418                         print_sockaddr_data_raw(buf, addrlen);
419                 }
420         }
421
422         tprints("}");
423 }
424
425 int
426 decode_sockaddr(struct tcb *const tcp, const kernel_ulong_t addr, int addrlen)
427 {
428         if (addrlen < 2) {
429                 printaddr(addr);
430                 return -1;
431         }
432
433         union {
434                 struct sockaddr sa;
435                 struct sockaddr_storage storage;
436                 char pad[sizeof(struct sockaddr_storage) + 1];
437         } addrbuf;
438
439         if ((unsigned) addrlen > sizeof(addrbuf.storage))
440                 addrlen = sizeof(addrbuf.storage);
441
442         if (umoven_or_printaddr(tcp, addr, addrlen, addrbuf.pad))
443                 return -1;
444
445         memset(&addrbuf.pad[addrlen], 0, sizeof(addrbuf.pad) - addrlen);
446
447         print_sockaddr(&addrbuf, addrlen);
448
449         return addrbuf.sa.sa_family;
450 }