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>
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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.
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.
33 #include <sys/socket.h>
35 #include <netinet/in.h>
36 #include <arpa/inet.h>
39 #include <linux/if_packet.h>
40 #include <linux/if_arp.h>
41 #include <linux/if_ether.h>
43 #ifdef HAVE_NETIPX_IPX_H
44 # include <netipx/ipx.h>
46 # include <linux/ipx.h>
49 #include "xlat/addrfams.h"
50 #include "xlat/arp_hardware_types.h"
51 #include "xlat/ethernet_protocols.h"
52 #include "xlat/af_packet_types.h"
54 #ifdef HAVE_BLUETOOTH_BLUETOOTH_H
55 # include <bluetooth/bluetooth.h>
56 # include <bluetooth/hci.h>
57 # include <bluetooth/l2cap.h>
58 # include <bluetooth/rfcomm.h>
59 # include <bluetooth/sco.h>
61 # include "xlat/hci_channels.h"
64 #define SIZEOF_SA_FAMILY sizeof(((struct sockaddr *) 0)->sa_family)
67 print_sockaddr_data_un(const void *const buf, const int addrlen)
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;
75 if (sa_un->sun_path[0]) {
76 print_quoted_string(sa_un->sun_path, path_len + 1,
80 print_quoted_string(sa_un->sun_path + 1, path_len - 1, 0);
85 print_inet_addr(const int af,
86 const void *const addr,
87 const unsigned int len,
88 const char *const var_name)
90 const char *af_name = NULL;
91 char buf[INET6_ADDRSTRLEN];
102 if (af_name && inet_ntop(af, addr, buf, sizeof(buf))) {
103 tprintf("inet_pton(%s, \"%s\", &%s)", af_name, buf, var_name);
106 tprintf("%s=", var_name);
107 print_quoted_string(addr, len, 0);
113 print_sockaddr_data_in(const void *const buf, const int addrlen)
115 const struct sockaddr_in *const sa_in = buf;
117 tprintf("sin_port=htons(%u), sin_addr=inet_addr(\"%s\")",
118 ntohs(sa_in->sin_port), inet_ntoa(sa_in->sin_addr));
121 #define SIN6_MIN_LEN offsetof(struct sockaddr_in6, sin6_scope_id)
124 print_sockaddr_data_in6(const void *const buf, const int addrlen)
126 const struct sockaddr_in6 *const sa_in6 = buf;
128 tprintf("sin6_port=htons(%u), ", ntohs(sa_in6->sin6_port));
129 print_inet_addr(AF_INET6, &sa_in6->sin6_addr,
130 sizeof(sa_in6->sin6_addr), "sin6_addr");
131 tprintf(", sin6_flowinfo=htonl(%u)", ntohl(sa_in6->sin6_flowinfo));
133 if (addrlen <= (int) SIN6_MIN_LEN)
136 tprints(", sin6_scope_id=");
137 #if defined IN6_IS_ADDR_LINKLOCAL && defined IN6_IS_ADDR_MC_LINKLOCAL
138 if (IN6_IS_ADDR_LINKLOCAL(&sa_in6->sin6_addr)
139 || IN6_IS_ADDR_MC_LINKLOCAL(&sa_in6->sin6_addr))
140 print_ifindex(sa_in6->sin6_scope_id);
143 tprintf("%u", sa_in6->sin6_scope_id);
147 print_sockaddr_data_ipx(const void *const buf, const int addrlen)
149 const struct sockaddr_ipx *const sa_ipx = buf;
152 tprintf("sipx_port=htons(%u)"
153 ", sipx_network=htonl(%#08x)"
155 ntohs(sa_ipx->sipx_port),
156 ntohl(sa_ipx->sipx_network));
157 for (i = 0; i < IPX_NODE_LEN; ++i) {
158 tprintf("%s%#02x", i ? ", " : "",
159 sa_ipx->sipx_node[i]);
161 tprintf("], sipx_type=%#02x", sa_ipx->sipx_type);
165 print_sockaddr_data_nl(const void *const buf, const int addrlen)
167 const struct sockaddr_nl *const sa_nl = buf;
169 tprintf("nl_pid=%d, nl_groups=%#08x",
170 sa_nl->nl_pid, sa_nl->nl_groups);
174 print_sockaddr_data_ll(const void *const buf, const int addrlen)
176 const struct sockaddr_ll *const sa_ll = buf;
178 tprints("sll_protocol=htons(");
179 printxval(ethernet_protocols, ntohs(sa_ll->sll_protocol), "ETH_P_???");
180 tprints("), sll_ifindex=");
181 print_ifindex(sa_ll->sll_ifindex);
182 tprints(", sll_hatype=");
183 printxval(arp_hardware_types, sa_ll->sll_hatype, "ARPHRD_???");
184 tprints(", sll_pkttype=");
185 printxval(af_packet_types, sa_ll->sll_pkttype, "PACKET_???");
186 tprintf(", sll_halen=%u", sa_ll->sll_halen);
187 if (sa_ll->sll_halen) {
188 const unsigned int oob_halen =
189 addrlen - offsetof(struct sockaddr_ll, sll_addr);
192 tprints(", sll_addr=[");
193 for (i = 0; i < sa_ll->sll_halen; ++i) {
196 if (i >= oob_halen) {
200 tprintf("%#02x", sa_ll->sll_addr[i]);
207 print_sockaddr_data_raw(const void *const buf, const int addrlen)
209 const char *const data = buf + SIZEOF_SA_FAMILY;
210 const int datalen = addrlen - SIZEOF_SA_FAMILY;
213 print_quoted_string(data, datalen, 0);
216 #ifdef HAVE_BLUETOOTH_BLUETOOTH_H
218 print_sockaddr_data_bt(const void *const buf, const int addrlen)
221 case sizeof(struct sockaddr_hci): {
222 const struct sockaddr_hci *const hci = buf;
223 tprintf("hci_dev=htobs(%hu), hci_channel=",
224 btohs(hci->hci_dev));
225 printxval(hci_channels, hci->hci_channel,
229 case sizeof(struct sockaddr_sco): {
230 const struct sockaddr_sco *const sco = buf;
231 tprintf("sco_bdaddr=%02x:%02x:%02x:%02x:%02x:%02x",
232 sco->sco_bdaddr.b[0], sco->sco_bdaddr.b[1],
233 sco->sco_bdaddr.b[2], sco->sco_bdaddr.b[3],
234 sco->sco_bdaddr.b[4], sco->sco_bdaddr.b[5]);
237 case sizeof(struct sockaddr_rc): {
238 const struct sockaddr_rc *const rc = buf;
239 tprintf("rc_bdaddr=%02x:%02x:%02x:%02x:%02x:%02x"
241 rc->rc_bdaddr.b[0], rc->rc_bdaddr.b[1],
242 rc->rc_bdaddr.b[2], rc->rc_bdaddr.b[3],
243 rc->rc_bdaddr.b[4], rc->rc_bdaddr.b[5],
247 case sizeof(struct sockaddr_l2): {
248 const struct sockaddr_l2 *const l2 = buf;
249 tprintf("l2_psm=htobs(%hu)"
250 ", l2_bdaddr=%02x:%02x:%02x:%02x:%02x:%02x"
251 ", l2_cid=htobs(%hu), l2_bdaddr_type=%u",
253 l2->l2_bdaddr.b[0], l2->l2_bdaddr.b[1],
254 l2->l2_bdaddr.b[2], l2->l2_bdaddr.b[3],
255 l2->l2_bdaddr.b[4], l2->l2_bdaddr.b[5],
256 btohs(l2->l2_cid), l2->l2_bdaddr_type);
260 print_sockaddr_data_raw(buf, addrlen);
264 #endif /* HAVE_BLUETOOTH_BLUETOOTH_H */
266 typedef void (* const sockaddr_printer)(const void *const, const int);
268 static const struct {
269 const sockaddr_printer printer;
272 [AF_UNIX] = { print_sockaddr_data_un, SIZEOF_SA_FAMILY + 1 },
273 [AF_INET] = { print_sockaddr_data_in, sizeof(struct sockaddr_in) },
274 [AF_IPX] = { print_sockaddr_data_ipx, sizeof(struct sockaddr_ipx) },
275 [AF_INET6] = { print_sockaddr_data_in6, SIN6_MIN_LEN },
276 [AF_NETLINK] = { print_sockaddr_data_nl, SIZEOF_SA_FAMILY + 1 },
277 [AF_PACKET] = { print_sockaddr_data_ll, sizeof(struct sockaddr_ll) },
278 #ifdef HAVE_BLUETOOTH_BLUETOOTH_H
279 [AF_BLUETOOTH] = { print_sockaddr_data_bt, SIZEOF_SA_FAMILY + 1 },
284 print_sockaddr(struct tcb *tcp, const void *const buf, const int addrlen)
286 const struct sockaddr *const sa = buf;
288 tprints("{sa_family=");
289 printxval(addrfams, sa->sa_family, "AF_???");
291 if (addrlen > (int) SIZEOF_SA_FAMILY) {
294 if (sa->sa_family < ARRAY_SIZE(sa_printers)
295 && sa_printers[sa->sa_family].printer
296 && addrlen >= sa_printers[sa->sa_family].min_len) {
297 sa_printers[sa->sa_family].printer(buf, addrlen);
299 print_sockaddr_data_raw(buf, addrlen);
307 decode_sockaddr(struct tcb *const tcp, const kernel_ulong_t addr, int addrlen)
316 struct sockaddr_storage storage;
317 char pad[sizeof(struct sockaddr_storage) + 1];
320 if ((unsigned) addrlen > sizeof(addrbuf.storage))
321 addrlen = sizeof(addrbuf.storage);
323 if (umoven_or_printaddr(tcp, addr, addrlen, addrbuf.pad))
326 memset(&addrbuf.pad[addrlen], 0, sizeof(addrbuf.pad) - addrlen);
328 print_sockaddr(tcp, &addrbuf, addrlen);
330 return addrbuf.sa.sa_family;