]> granicus.if.org Git - strace/blob - sockaddr.c
s390: wire up s390_guarded_storage syscall
[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  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. The name of the author may not be used to endorse or promote products
18  *    derived from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31
32 #include "defs.h"
33 #include "print_fields.h"
34
35 #include <sys/socket.h>
36 #include <sys/un.h>
37 #include <netinet/in.h>
38 #include <arpa/inet.h>
39
40 #include "netlink.h"
41 #include <linux/if_packet.h>
42 #include <linux/if_arp.h>
43 #include <linux/if_ether.h>
44
45 #ifdef HAVE_NETIPX_IPX_H
46 # include <netipx/ipx.h>
47 #else
48 # include <linux/ipx.h>
49 #endif
50
51 #include "xlat/addrfams.h"
52 #include "xlat/arp_hardware_types.h"
53 #include "xlat/ethernet_protocols.h"
54 #include "xlat/af_packet_types.h"
55
56 #ifdef HAVE_BLUETOOTH_BLUETOOTH_H
57 # include <bluetooth/bluetooth.h>
58 # include <bluetooth/hci.h>
59 # include <bluetooth/l2cap.h>
60 # include <bluetooth/rfcomm.h>
61 # include <bluetooth/sco.h>
62
63 # include "xlat/hci_channels.h"
64 #endif
65
66 #define SIZEOF_SA_FAMILY sizeof(((struct sockaddr *) 0)->sa_family)
67
68 static void
69 print_sockaddr_data_un(const void *const buf, const int addrlen)
70 {
71         const struct sockaddr_un *const sa_un = buf;
72         const int un_len = addrlen > (int) sizeof(*sa_un)
73                            ? (int) sizeof(*sa_un) : addrlen;
74         const int path_len = un_len - SIZEOF_SA_FAMILY;
75
76         tprints("sun_path=");
77         if (sa_un->sun_path[0]) {
78                 print_quoted_string(sa_un->sun_path, path_len + 1,
79                                     QUOTE_0_TERMINATED);
80         } else {
81                 tprints("@");
82                 print_quoted_string(sa_un->sun_path + 1, path_len - 1, 0);
83         }
84 }
85
86 bool
87 print_inet_addr(const int af,
88                 const void *const addr,
89                 const unsigned int len,
90                 const char *const var_name)
91 {
92         char buf[INET6_ADDRSTRLEN];
93
94         switch (af) {
95         case AF_INET:
96                 if (inet_ntop(af, addr, buf, sizeof(buf))) {
97                         if (var_name)
98                                 tprintf("%s=inet_addr(\"%s\")", var_name, buf);
99                         else
100                                 tprints(buf);
101                         return true;
102                 }
103                 break;
104         case AF_INET6:
105                 if (inet_ntop(af, addr, buf, sizeof(buf))) {
106                         if (var_name)
107                                 tprintf("inet_pton(%s, \"%s\", &%s)",
108                                         "AF_INET6", buf, var_name);
109                         else
110                                 tprints(buf);
111                         return true;
112                 }
113                 break;
114         }
115
116         if (var_name)
117                 tprintf("%s=", var_name);
118         print_quoted_string(addr, len, QUOTE_FORCE_HEX);
119         return false;
120 }
121
122 bool
123 decode_inet_addr(struct tcb *const tcp,
124                  const kernel_ulong_t addr,
125                  const unsigned int len,
126                  const int family,
127                  const char *const var_name)
128 {
129         union {
130                 struct in_addr  a4;
131                 struct in6_addr a6;
132         } addrbuf;
133         size_t size = 0;
134
135         switch (family) {
136         case AF_INET:
137                 size = sizeof(addrbuf.a4);
138                 break;
139         case AF_INET6:
140                 size = sizeof(addrbuf.a6);
141                 break;
142         }
143
144         if (!size || len < size) {
145                 if (var_name)
146                         tprintf("%s=", var_name);
147                 printstr_ex(tcp, addr, len, QUOTE_FORCE_HEX);
148                 return false;
149         }
150
151         if (umoven(tcp, addr, size, &addrbuf) < 0) {
152                 if (var_name)
153                         tprintf("%s=", var_name);
154                 printaddr(addr);
155                 return false;
156         }
157
158         return print_inet_addr(family, &addrbuf, size, var_name);
159 }
160
161 static void
162 print_sockaddr_data_in(const void *const buf, const int addrlen)
163 {
164         const struct sockaddr_in *const sa_in = buf;
165
166         PRINT_FIELD_NET_PORT("", *sa_in, sin_port);
167         PRINT_FIELD_INET4_ADDR(", ", *sa_in, sin_addr);
168 }
169
170 #define SIN6_MIN_LEN offsetof(struct sockaddr_in6, sin6_scope_id)
171
172 static void
173 print_sockaddr_data_in6(const void *const buf, const int addrlen)
174 {
175         const struct sockaddr_in6 *const sa_in6 = buf;
176
177         PRINT_FIELD_NET_PORT("", *sa_in6, sin6_port);
178         PRINT_FIELD_INET_ADDR(", ", *sa_in6, sin6_addr, AF_INET6);
179         tprintf(", sin6_flowinfo=htonl(%u)", ntohl(sa_in6->sin6_flowinfo));
180
181         if (addrlen <= (int) SIN6_MIN_LEN)
182                 return;
183
184 #if defined IN6_IS_ADDR_LINKLOCAL && defined IN6_IS_ADDR_MC_LINKLOCAL
185         if (IN6_IS_ADDR_LINKLOCAL(&sa_in6->sin6_addr)
186             || IN6_IS_ADDR_MC_LINKLOCAL(&sa_in6->sin6_addr))
187                 PRINT_FIELD_IFINDEX(", ", *sa_in6, sin6_scope_id);
188         else
189 #endif
190                 PRINT_FIELD_U(", ", *sa_in6, sin6_scope_id);
191 }
192
193 static void
194 print_sockaddr_data_ipx(const void *const buf, const int addrlen)
195 {
196         const struct sockaddr_ipx *const sa_ipx = buf;
197         unsigned int i;
198
199         PRINT_FIELD_NET_PORT("", *sa_ipx, sipx_port);
200         tprintf(", sipx_network=htonl(%#08x)"
201                 ", sipx_node=[",
202                 ntohl(sa_ipx->sipx_network));
203         for (i = 0; i < IPX_NODE_LEN; ++i) {
204                 tprintf("%s%#02x", i ? ", " : "",
205                         sa_ipx->sipx_node[i]);
206         }
207         PRINT_FIELD_0X("], ", *sa_ipx, sipx_type);
208 }
209
210 static void
211 print_sockaddr_data_nl(const void *const buf, const int addrlen)
212 {
213         const struct sockaddr_nl *const sa_nl = buf;
214
215         PRINT_FIELD_D("", *sa_nl, nl_pid);
216         PRINT_FIELD_0X(", ", *sa_nl, nl_groups);
217 }
218
219 static void
220 print_sockaddr_data_ll(const void *const buf, const int addrlen)
221 {
222         const struct sockaddr_ll *const sa_ll = buf;
223
224         tprints("sll_protocol=htons(");
225         printxval(ethernet_protocols, ntohs(sa_ll->sll_protocol), "ETH_P_???");
226         PRINT_FIELD_IFINDEX("), ", *sa_ll, sll_ifindex);
227         tprints(", sll_hatype=");
228         printxval(arp_hardware_types, sa_ll->sll_hatype, "ARPHRD_???");
229         tprints(", sll_pkttype=");
230         printxval(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 #ifdef HAVE_BLUETOOTH_BLUETOOTH_H
262 static void
263 print_sockaddr_data_bt(const void *const buf, const int addrlen)
264 {
265         switch (addrlen) {
266                 case sizeof(struct sockaddr_hci): {
267                         const struct sockaddr_hci *const hci = buf;
268                         tprintf("hci_dev=htobs(%hu), hci_channel=",
269                                 btohs(hci->hci_dev));
270                         printxval(hci_channels, hci->hci_channel,
271                                   "HCI_CHANNEL_???");
272                         break;
273                 }
274                 case sizeof(struct sockaddr_sco): {
275                         const struct sockaddr_sco *const sco = buf;
276                         tprintf("sco_bdaddr=%02x:%02x:%02x:%02x:%02x:%02x",
277                                 sco->sco_bdaddr.b[0], sco->sco_bdaddr.b[1],
278                                 sco->sco_bdaddr.b[2], sco->sco_bdaddr.b[3],
279                                 sco->sco_bdaddr.b[4], sco->sco_bdaddr.b[5]);
280                         break;
281                 }
282                 case sizeof(struct sockaddr_rc): {
283                         const struct sockaddr_rc *const rc = buf;
284                         tprintf("rc_bdaddr=%02x:%02x:%02x:%02x:%02x:%02x"
285                                 ", rc_channel=%u",
286                                 rc->rc_bdaddr.b[0], rc->rc_bdaddr.b[1],
287                                 rc->rc_bdaddr.b[2], rc->rc_bdaddr.b[3],
288                                 rc->rc_bdaddr.b[4], rc->rc_bdaddr.b[5],
289                                 rc->rc_channel);
290                         break;
291                 }
292                 case sizeof(struct sockaddr_l2): {
293                         const struct sockaddr_l2 *const l2 = buf;
294                         tprintf("l2_psm=htobs(%hu)"
295                                 ", l2_bdaddr=%02x:%02x:%02x:%02x:%02x:%02x"
296                                 ", l2_cid=htobs(%hu), l2_bdaddr_type=%u",
297                                 btohs(l2->l2_psm),
298                                 l2->l2_bdaddr.b[0], l2->l2_bdaddr.b[1],
299                                 l2->l2_bdaddr.b[2], l2->l2_bdaddr.b[3],
300                                 l2->l2_bdaddr.b[4], l2->l2_bdaddr.b[5],
301                                 btohs(l2->l2_cid), l2->l2_bdaddr_type);
302                         break;
303                 }
304                 default:
305                         print_sockaddr_data_raw(buf, addrlen);
306                         break;
307         }
308 }
309 #endif /* HAVE_BLUETOOTH_BLUETOOTH_H */
310
311 typedef void (* const sockaddr_printer)(const void *const, const int);
312
313 static const struct {
314         const sockaddr_printer printer;
315         const int min_len;
316 } sa_printers[] = {
317         [AF_UNIX] = { print_sockaddr_data_un, SIZEOF_SA_FAMILY + 1 },
318         [AF_INET] = { print_sockaddr_data_in, sizeof(struct sockaddr_in) },
319         [AF_IPX] = { print_sockaddr_data_ipx, sizeof(struct sockaddr_ipx) },
320         [AF_INET6] = { print_sockaddr_data_in6, SIN6_MIN_LEN },
321         [AF_NETLINK] = { print_sockaddr_data_nl, SIZEOF_SA_FAMILY + 1 },
322         [AF_PACKET] = { print_sockaddr_data_ll, sizeof(struct sockaddr_ll) },
323 #ifdef HAVE_BLUETOOTH_BLUETOOTH_H
324         [AF_BLUETOOTH] = { print_sockaddr_data_bt, SIZEOF_SA_FAMILY + 1 },
325 #endif
326 };
327
328 void
329 print_sockaddr(const void *const buf, const int addrlen)
330 {
331         const struct sockaddr *const sa = buf;
332
333         tprints("{sa_family=");
334         printxval(addrfams, sa->sa_family, "AF_???");
335
336         if (addrlen > (int) SIZEOF_SA_FAMILY) {
337                 tprints(", ");
338
339                 if (sa->sa_family < ARRAY_SIZE(sa_printers)
340                     && sa_printers[sa->sa_family].printer
341                     && addrlen >= sa_printers[sa->sa_family].min_len) {
342                         sa_printers[sa->sa_family].printer(buf, addrlen);
343                 } else {
344                         print_sockaddr_data_raw(buf, addrlen);
345                 }
346         }
347
348         tprints("}");
349 }
350
351 int
352 decode_sockaddr(struct tcb *const tcp, const kernel_ulong_t addr, int addrlen)
353 {
354         if (addrlen < 2) {
355                 printaddr(addr);
356                 return -1;
357         }
358
359         union {
360                 struct sockaddr sa;
361                 struct sockaddr_storage storage;
362                 char pad[sizeof(struct sockaddr_storage) + 1];
363         } addrbuf;
364
365         if ((unsigned) addrlen > sizeof(addrbuf.storage))
366                 addrlen = sizeof(addrbuf.storage);
367
368         if (umoven_or_printaddr(tcp, addr, addrlen, addrbuf.pad))
369                 return -1;
370
371         memset(&addrbuf.pad[addrlen], 0, sizeof(addrbuf.pad) - addrlen);
372
373         print_sockaddr(&addrbuf, addrlen);
374
375         return addrbuf.sa.sa_family;
376 }