2 * Copyright (c) 1993, 1994, 1995, 1996 Rick Sladkey <jrs@world.std.com>
3 * Copyright (c) 1996-2017 The strace developers.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #include "print_fields.h"
32 #include <sys/socket.h>
33 #if defined ALPHA || defined SH || defined SH64
34 # include <linux/ioctl.h>
36 #include <linux/sockios.h>
37 #include <arpa/inet.h>
40 #include DEF_MPERS_TYPE(struct_ifconf)
41 #include DEF_MPERS_TYPE(struct_ifreq)
43 typedef struct ifconf struct_ifconf;
44 typedef struct ifreq struct_ifreq;
48 #include "xlat/iffflags.h"
51 print_ifname(const char *ifname)
53 print_quoted_string(ifname, IFNAMSIZ + 1, QUOTE_0_TERMINATED);
57 print_ifreq(struct tcb *const tcp, const unsigned int code,
58 const kernel_ulong_t arg, const struct_ifreq *const ifr)
63 PRINT_FIELD_SOCKADDR("", *ifr, ifr_addr);
67 PRINT_FIELD_SOCKADDR("", *ifr, ifr_dstaddr);
71 PRINT_FIELD_SOCKADDR("", *ifr, ifr_broadaddr);
75 PRINT_FIELD_SOCKADDR("", *ifr, ifr_netmask);
79 /* XXX Are there other hardware addresses
81 const unsigned char *bytes =
82 (unsigned char *) &ifr->ifr_hwaddr.sa_data;
83 tprintf("ifr_hwaddr=%02x:%02x:%02x:%02x:%02x:%02x",
84 bytes[0], bytes[1], bytes[2],
85 bytes[3], bytes[4], bytes[5]);
90 tprints("ifr_flags=");
91 printflags(iffflags, (unsigned short) ifr->ifr_flags, "IFF_???");
95 tprintf("ifr_metric=%d", ifr->ifr_metric);
99 tprintf("ifr_mtu=%d", ifr->ifr_mtu);
103 tprints("ifr_slave=");
104 print_ifname(ifr->ifr_slave);
108 tprintf("ifr_qlen=%d", ifr->ifr_qlen);
112 tprintf("ifr_map={mem_start=%#" PRI_klx ", "
113 "mem_end=%#" PRI_klx ", base_addr=%#x, "
114 "irq=%u, dma=%u, port=%u}",
115 (kernel_ulong_t) ifr->ifr_map.mem_start,
116 (kernel_ulong_t) ifr->ifr_map.mem_end,
117 (unsigned) ifr->ifr_map.base_addr,
118 (unsigned) ifr->ifr_map.irq,
119 (unsigned) ifr->ifr_map.dma,
120 (unsigned) ifr->ifr_map.port);
126 print_ifc_len(int len)
128 const unsigned int n = (unsigned int) len / sizeof(struct_ifreq);
130 if (len < 0 || n * sizeof(struct_ifreq) != (unsigned int) len)
133 tprintf("%u * sizeof(struct ifreq)", n);
139 print_ifconf_ifreq(struct tcb *tcp, void *elem_buf, size_t elem_size,
142 struct_ifreq *ifr = elem_buf;
144 tprints("{ifr_name=");
145 print_ifname(ifr->ifr_name);
146 PRINT_FIELD_SOCKADDR(", ", *ifr, ifr_addr);
153 * There are two different modes of operation:
155 * - Get buffer size. In this case, the callee sets ifc_buf to NULL,
156 * and the kernel returns the buffer size in ifc_len.
157 * - Get actual data. In this case, the callee specifies the buffer address
158 * in ifc_buf and its size in ifc_len. The kernel fills the buffer with
159 * the data, and its amount is returned in ifc_len.
161 * Note that, technically, the whole struct ifconf is overwritten,
162 * so ifc_buf could be different on exit, but current ioctl handler
163 * implementation does not touch it.
166 decode_ifconf(struct tcb *const tcp, const kernel_ulong_t addr)
168 struct_ifconf *entering_ifc = NULL;
170 entering(tcp) ? malloc(sizeof(*ifc)) : alloca(sizeof(*ifc));
173 entering_ifc = get_tcb_priv_data(tcp);
176 error_msg("decode_ifconf: where is my ifconf?");
181 if (!ifc || umove(tcp, addr, ifc) < 0) {
189 * We failed to fetch the structure on exiting syscall,
190 * print whatever was fetched on entering syscall.
192 if (!entering_ifc->ifc_buf)
193 print_ifc_len(entering_ifc->ifc_len);
195 tprints(", ifc_buf=");
196 printaddr(ptr_to_kulong(entering_ifc->ifc_buf));
201 return RVAL_IOCTL_DECODED;
205 tprints(", {ifc_len=");
207 print_ifc_len(ifc->ifc_len);
209 set_tcb_priv_data(tcp, ifc, free);
216 if (entering_ifc->ifc_buf && (entering_ifc->ifc_len != ifc->ifc_len))
218 if (!entering_ifc->ifc_buf || (entering_ifc->ifc_len != ifc->ifc_len))
219 print_ifc_len(ifc->ifc_len);
221 tprints(", ifc_buf=");
223 if (!entering_ifc->ifc_buf || syserror(tcp)) {
224 printaddr(ptr_to_kulong(entering_ifc->ifc_buf));
225 if (entering_ifc->ifc_buf != ifc->ifc_buf) {
227 printaddr(ptr_to_kulong(ifc->ifc_buf));
232 print_array(tcp, ptr_to_kulong(ifc->ifc_buf),
233 ifc->ifc_len / sizeof(struct_ifreq),
235 umoven_or_printaddr, print_ifconf_ifreq, NULL);
240 return RVAL_IOCTL_DECODED;
243 MPERS_PRINTER_DECL(int, sock_ioctl,
244 struct tcb *tcp, const unsigned int code,
245 const kernel_ulong_t arg)
251 return decode_ifconf(tcp, arg);
268 printnum_int(tcp, arg, "%d");
283 printnum_int(tcp, arg, "%d");
308 if (umove_or_printaddr(tcp, arg, &ifr))
311 tprints("{ifr_name=");
312 print_ifname(ifr.ifr_name);
314 if (code == SIOCSIFNAME) {
315 tprints("ifr_newname=");
316 print_ifname(ifr.ifr_newname);
318 print_ifreq(tcp, code, arg, &ifr);
338 if (umove_or_printaddr(tcp, arg, &ifr))
341 if (SIOCGIFNAME == code) {
342 tprintf("{ifr_index=%d", ifr.ifr_ifindex);
344 tprints("{ifr_name=");
345 print_ifname(ifr.ifr_name);
355 if (umove(tcp, arg, &ifr) < 0) {
360 if (SIOCGIFNAME == code) {
361 tprints("ifr_name=");
362 print_ifname(ifr.ifr_name);
364 print_ifreq(tcp, code, arg, &ifr);
374 return RVAL_IOCTL_DECODED;