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 "print_fields.h"
35 #include <sys/socket.h>
37 #include <netinet/in.h>
38 #include <arpa/inet.h>
41 #include <linux/if_packet.h>
42 #include <linux/if_arp.h>
43 #include <linux/if_ether.h>
45 #ifdef HAVE_NETIPX_IPX_H
46 # include <netipx/ipx.h>
48 # include <linux/ipx.h>
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"
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>
63 # include "xlat/hci_channels.h"
66 #define SIZEOF_SA_FAMILY sizeof(((struct sockaddr *) 0)->sa_family)
69 print_sockaddr_data_un(const void *const buf, const int addrlen)
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;
77 if (sa_un->sun_path[0]) {
78 print_quoted_string(sa_un->sun_path, path_len + 1,
82 print_quoted_string(sa_un->sun_path + 1, path_len - 1, 0);
87 print_inet_addr(const int af,
88 const void *const addr,
89 const unsigned int len,
90 const char *const var_name)
92 char buf[INET6_ADDRSTRLEN];
96 if (inet_ntop(af, addr, buf, sizeof(buf))) {
98 tprintf("%s=inet_addr(\"%s\")", var_name, buf);
105 if (inet_ntop(af, addr, buf, sizeof(buf))) {
107 tprintf("inet_pton(%s, \"%s\", &%s)",
108 "AF_INET6", buf, var_name);
117 tprintf("%s=", var_name);
118 print_quoted_string(addr, len, QUOTE_FORCE_HEX);
123 print_sockaddr_data_in(const void *const buf, const int addrlen)
125 const struct sockaddr_in *const sa_in = buf;
127 PRINT_FIELD_NET_PORT("", *sa_in, sin_port);
128 PRINT_FIELD_INET4_ADDR(", ", *sa_in, sin_addr);
131 #define SIN6_MIN_LEN offsetof(struct sockaddr_in6, sin6_scope_id)
134 print_sockaddr_data_in6(const void *const buf, const int addrlen)
136 const struct sockaddr_in6 *const sa_in6 = buf;
138 PRINT_FIELD_NET_PORT("", *sa_in6, sin6_port);
139 PRINT_FIELD_INET_ADDR(", ", *sa_in6, sin6_addr, AF_INET6);
140 tprintf(", sin6_flowinfo=htonl(%u)", ntohl(sa_in6->sin6_flowinfo));
142 if (addrlen <= (int) SIN6_MIN_LEN)
145 #if defined IN6_IS_ADDR_LINKLOCAL && defined IN6_IS_ADDR_MC_LINKLOCAL
146 if (IN6_IS_ADDR_LINKLOCAL(&sa_in6->sin6_addr)
147 || IN6_IS_ADDR_MC_LINKLOCAL(&sa_in6->sin6_addr))
148 PRINT_FIELD_IFINDEX(", ", *sa_in6, sin6_scope_id);
151 PRINT_FIELD_U(", ", *sa_in6, sin6_scope_id);
155 print_sockaddr_data_ipx(const void *const buf, const int addrlen)
157 const struct sockaddr_ipx *const sa_ipx = buf;
160 PRINT_FIELD_NET_PORT("", *sa_ipx, sipx_port);
161 tprintf(", sipx_network=htonl(%#08x)"
163 ntohl(sa_ipx->sipx_network));
164 for (i = 0; i < IPX_NODE_LEN; ++i) {
165 tprintf("%s%#02x", i ? ", " : "",
166 sa_ipx->sipx_node[i]);
168 PRINT_FIELD_0X("], ", *sa_ipx, sipx_type);
172 print_sockaddr_data_nl(const void *const buf, const int addrlen)
174 const struct sockaddr_nl *const sa_nl = buf;
176 PRINT_FIELD_D("", *sa_nl, nl_pid);
177 PRINT_FIELD_0X(", ", *sa_nl, nl_groups);
181 print_sockaddr_data_ll(const void *const buf, const int addrlen)
183 const struct sockaddr_ll *const sa_ll = buf;
185 tprints("sll_protocol=htons(");
186 printxval(ethernet_protocols, ntohs(sa_ll->sll_protocol), "ETH_P_???");
187 PRINT_FIELD_IFINDEX("), ", *sa_ll, sll_ifindex);
188 tprints(", sll_hatype=");
189 printxval(arp_hardware_types, sa_ll->sll_hatype, "ARPHRD_???");
190 tprints(", sll_pkttype=");
191 printxval(af_packet_types, sa_ll->sll_pkttype, "PACKET_???");
192 tprintf(", sll_halen=%u", sa_ll->sll_halen);
193 if (sa_ll->sll_halen) {
194 const unsigned int oob_halen =
195 addrlen - offsetof(struct sockaddr_ll, sll_addr);
198 tprints(", sll_addr=[");
199 for (i = 0; i < sa_ll->sll_halen; ++i) {
202 if (i >= oob_halen) {
206 tprintf("%#02x", sa_ll->sll_addr[i]);
213 print_sockaddr_data_raw(const void *const buf, const int addrlen)
215 const char *const data = buf + SIZEOF_SA_FAMILY;
216 const int datalen = addrlen - SIZEOF_SA_FAMILY;
219 print_quoted_string(data, datalen, 0);
222 #ifdef HAVE_BLUETOOTH_BLUETOOTH_H
224 print_sockaddr_data_bt(const void *const buf, const int addrlen)
227 case sizeof(struct sockaddr_hci): {
228 const struct sockaddr_hci *const hci = buf;
229 tprintf("hci_dev=htobs(%hu), hci_channel=",
230 btohs(hci->hci_dev));
231 printxval(hci_channels, hci->hci_channel,
235 case sizeof(struct sockaddr_sco): {
236 const struct sockaddr_sco *const sco = buf;
237 tprintf("sco_bdaddr=%02x:%02x:%02x:%02x:%02x:%02x",
238 sco->sco_bdaddr.b[0], sco->sco_bdaddr.b[1],
239 sco->sco_bdaddr.b[2], sco->sco_bdaddr.b[3],
240 sco->sco_bdaddr.b[4], sco->sco_bdaddr.b[5]);
243 case sizeof(struct sockaddr_rc): {
244 const struct sockaddr_rc *const rc = buf;
245 tprintf("rc_bdaddr=%02x:%02x:%02x:%02x:%02x:%02x"
247 rc->rc_bdaddr.b[0], rc->rc_bdaddr.b[1],
248 rc->rc_bdaddr.b[2], rc->rc_bdaddr.b[3],
249 rc->rc_bdaddr.b[4], rc->rc_bdaddr.b[5],
253 case sizeof(struct sockaddr_l2): {
254 const struct sockaddr_l2 *const l2 = buf;
255 tprintf("l2_psm=htobs(%hu)"
256 ", l2_bdaddr=%02x:%02x:%02x:%02x:%02x:%02x"
257 ", l2_cid=htobs(%hu), l2_bdaddr_type=%u",
259 l2->l2_bdaddr.b[0], l2->l2_bdaddr.b[1],
260 l2->l2_bdaddr.b[2], l2->l2_bdaddr.b[3],
261 l2->l2_bdaddr.b[4], l2->l2_bdaddr.b[5],
262 btohs(l2->l2_cid), l2->l2_bdaddr_type);
266 print_sockaddr_data_raw(buf, addrlen);
270 #endif /* HAVE_BLUETOOTH_BLUETOOTH_H */
272 typedef void (* const sockaddr_printer)(const void *const, const int);
274 static const struct {
275 const sockaddr_printer printer;
278 [AF_UNIX] = { print_sockaddr_data_un, SIZEOF_SA_FAMILY + 1 },
279 [AF_INET] = { print_sockaddr_data_in, sizeof(struct sockaddr_in) },
280 [AF_IPX] = { print_sockaddr_data_ipx, sizeof(struct sockaddr_ipx) },
281 [AF_INET6] = { print_sockaddr_data_in6, SIN6_MIN_LEN },
282 [AF_NETLINK] = { print_sockaddr_data_nl, SIZEOF_SA_FAMILY + 1 },
283 [AF_PACKET] = { print_sockaddr_data_ll, sizeof(struct sockaddr_ll) },
284 #ifdef HAVE_BLUETOOTH_BLUETOOTH_H
285 [AF_BLUETOOTH] = { print_sockaddr_data_bt, SIZEOF_SA_FAMILY + 1 },
290 print_sockaddr(const void *const buf, const int addrlen)
292 const struct sockaddr *const sa = buf;
294 tprints("{sa_family=");
295 printxval(addrfams, sa->sa_family, "AF_???");
297 if (addrlen > (int) SIZEOF_SA_FAMILY) {
300 if (sa->sa_family < ARRAY_SIZE(sa_printers)
301 && sa_printers[sa->sa_family].printer
302 && addrlen >= sa_printers[sa->sa_family].min_len) {
303 sa_printers[sa->sa_family].printer(buf, addrlen);
305 print_sockaddr_data_raw(buf, addrlen);
313 decode_sockaddr(struct tcb *const tcp, const kernel_ulong_t addr, int addrlen)
322 struct sockaddr_storage storage;
323 char pad[sizeof(struct sockaddr_storage) + 1];
326 if ((unsigned) addrlen > sizeof(addrbuf.storage))
327 addrlen = sizeof(addrbuf.storage);
329 if (umoven_or_printaddr(tcp, addr, addrlen, addrbuf.pad))
332 memset(&addrbuf.pad[addrlen], 0, sizeof(addrbuf.pad) - addrlen);
334 print_sockaddr(&addrbuf, addrlen);
336 return addrbuf.sa.sa_family;