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>
38 #include <linux/netlink.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_sockaddr_data_in(const void *const buf, const int addrlen)
87 const struct sockaddr_in *const sa_in = buf;
89 tprintf("sin_port=htons(%u), sin_addr=inet_addr(\"%s\")",
90 ntohs(sa_in->sin_port), inet_ntoa(sa_in->sin_addr));
93 #define SIN6_MIN_LEN offsetof(struct sockaddr_in6, sin6_scope_id)
96 print_sockaddr_data_in6(const void *const buf, const int addrlen)
98 const struct sockaddr_in6 *const sa_in6 = buf;
100 char string_addr[100];
101 inet_ntop(AF_INET6, &sa_in6->sin6_addr,
102 string_addr, sizeof(string_addr));
103 tprintf("sin6_port=htons(%u), inet_pton(AF_INET6"
104 ", \"%s\", &sin6_addr), sin6_flowinfo=htonl(%u)",
105 ntohs(sa_in6->sin6_port), string_addr,
106 ntohl(sa_in6->sin6_flowinfo));
108 if (addrlen <= (int) SIN6_MIN_LEN)
111 tprints(", sin6_scope_id=");
112 #if defined IN6_IS_ADDR_LINKLOCAL && defined IN6_IS_ADDR_MC_LINKLOCAL
113 if (IN6_IS_ADDR_LINKLOCAL(&sa_in6->sin6_addr)
114 || IN6_IS_ADDR_MC_LINKLOCAL(&sa_in6->sin6_addr))
115 print_ifindex(sa_in6->sin6_scope_id);
118 tprintf("%u", sa_in6->sin6_scope_id);
122 print_sockaddr_data_ipx(const void *const buf, const int addrlen)
124 const struct sockaddr_ipx *const sa_ipx = buf;
127 tprintf("sipx_port=htons(%u)"
128 ", sipx_network=htonl(%#08x)"
130 ntohs(sa_ipx->sipx_port),
131 ntohl(sa_ipx->sipx_network));
132 for (i = 0; i < IPX_NODE_LEN; ++i) {
133 tprintf("%s%#02x", i ? ", " : "",
134 sa_ipx->sipx_node[i]);
136 tprintf("], sipx_type=%#02x", sa_ipx->sipx_type);
140 print_sockaddr_data_nl(const void *const buf, const int addrlen)
142 const struct sockaddr_nl *const sa_nl = buf;
144 tprintf("nl_pid=%d, nl_groups=%#08x",
145 sa_nl->nl_pid, sa_nl->nl_groups);
149 print_sockaddr_data_ll(const void *const buf, const int addrlen)
151 const struct sockaddr_ll *const sa_ll = buf;
153 tprints("sll_protocol=htons(");
154 printxval(ethernet_protocols, ntohs(sa_ll->sll_protocol), "ETH_P_???");
155 tprints("), sll_ifindex=");
156 print_ifindex(sa_ll->sll_ifindex);
157 tprints(", sll_hatype=");
158 printxval(arp_hardware_types, sa_ll->sll_hatype, "ARPHRD_???");
159 tprints(", sll_pkttype=");
160 printxval(af_packet_types, sa_ll->sll_pkttype, "PACKET_???");
161 tprintf(", sll_halen=%u", sa_ll->sll_halen);
162 if (sa_ll->sll_halen) {
163 const unsigned int oob_halen =
164 addrlen - offsetof(struct sockaddr_ll, sll_addr);
167 tprints(", sll_addr=[");
168 for (i = 0; i < sa_ll->sll_halen; ++i) {
171 if (i >= oob_halen) {
175 tprintf("%#02x", sa_ll->sll_addr[i]);
182 print_sockaddr_data_raw(const void *const buf, const int addrlen)
184 const char *const data = buf + SIZEOF_SA_FAMILY;
185 const int datalen = addrlen - SIZEOF_SA_FAMILY;
188 print_quoted_string(data, datalen, 0);
191 #ifdef HAVE_BLUETOOTH_BLUETOOTH_H
193 print_sockaddr_data_bt(const void *const buf, const int addrlen)
196 case sizeof(struct sockaddr_hci): {
197 const struct sockaddr_hci *const hci = buf;
198 tprintf("hci_dev=htobs(%hu), hci_channel=",
199 btohs(hci->hci_dev));
200 printxval(hci_channels, hci->hci_channel,
204 case sizeof(struct sockaddr_sco): {
205 const struct sockaddr_sco *const sco = buf;
206 tprintf("sco_bdaddr=%02x:%02x:%02x:%02x:%02x:%02x",
207 sco->sco_bdaddr.b[0], sco->sco_bdaddr.b[1],
208 sco->sco_bdaddr.b[2], sco->sco_bdaddr.b[3],
209 sco->sco_bdaddr.b[4], sco->sco_bdaddr.b[5]);
212 case sizeof(struct sockaddr_rc): {
213 const struct sockaddr_rc *const rc = buf;
214 tprintf("rc_bdaddr=%02x:%02x:%02x:%02x:%02x:%02x"
216 rc->rc_bdaddr.b[0], rc->rc_bdaddr.b[1],
217 rc->rc_bdaddr.b[2], rc->rc_bdaddr.b[3],
218 rc->rc_bdaddr.b[4], rc->rc_bdaddr.b[5],
222 case sizeof(struct sockaddr_l2): {
223 const struct sockaddr_l2 *const l2 = buf;
224 tprintf("l2_psm=htobs(%hu)"
225 ", l2_bdaddr=%02x:%02x:%02x:%02x:%02x:%02x"
226 ", l2_cid=htobs(%hu), l2_bdaddr_type=%u",
228 l2->l2_bdaddr.b[0], l2->l2_bdaddr.b[1],
229 l2->l2_bdaddr.b[2], l2->l2_bdaddr.b[3],
230 l2->l2_bdaddr.b[4], l2->l2_bdaddr.b[5],
231 btohs(l2->l2_cid), l2->l2_bdaddr_type);
235 print_sockaddr_data_raw(buf, addrlen);
239 #endif /* HAVE_BLUETOOTH_BLUETOOTH_H */
241 typedef void (* const sockaddr_printer)(const void *const, const int);
243 static const struct {
244 const sockaddr_printer printer;
247 [AF_UNIX] = { print_sockaddr_data_un, SIZEOF_SA_FAMILY + 1 },
248 [AF_INET] = { print_sockaddr_data_in, sizeof(struct sockaddr_in) },
249 [AF_IPX] = { print_sockaddr_data_ipx, sizeof(struct sockaddr_ipx) },
250 [AF_INET6] = { print_sockaddr_data_in6, SIN6_MIN_LEN },
251 [AF_NETLINK] = { print_sockaddr_data_nl, SIZEOF_SA_FAMILY + 1 },
252 [AF_PACKET] = { print_sockaddr_data_ll, sizeof(struct sockaddr_ll) },
253 #ifdef HAVE_BLUETOOTH_BLUETOOTH_H
254 [AF_BLUETOOTH] = { print_sockaddr_data_bt, SIZEOF_SA_FAMILY + 1 },
259 print_sockaddr(struct tcb *tcp, const void *const buf, const int addrlen)
261 const struct sockaddr *const sa = buf;
263 tprints("{sa_family=");
264 printxval(addrfams, sa->sa_family, "AF_???");
266 if (addrlen > (int) SIZEOF_SA_FAMILY) {
269 if (sa->sa_family < ARRAY_SIZE(sa_printers)
270 && sa_printers[sa->sa_family].printer
271 && addrlen >= sa_printers[sa->sa_family].min_len) {
272 sa_printers[sa->sa_family].printer(buf, addrlen);
274 print_sockaddr_data_raw(buf, addrlen);
282 decode_sockaddr(struct tcb *tcp, long addr, int addrlen)
291 struct sockaddr_storage storage;
292 char pad[sizeof(struct sockaddr_storage) + 1];
295 if ((unsigned) addrlen > sizeof(addrbuf.storage))
296 addrlen = sizeof(addrbuf.storage);
298 if (umoven_or_printaddr(tcp, addr, addrlen, addrbuf.pad))
301 memset(&addrbuf.pad[addrlen], 0, sizeof(addrbuf.pad) - addrlen);
303 print_sockaddr(tcp, &addrbuf, addrlen);
305 return addrbuf.sa.sa_family;