blob: e7c72b55caa3c535dd8583c3f9935caa29dc9aa0 [file] [log] [blame]
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001/*
2 * Copyright (c) 1993, 1994, 1995, 1996 Rick Sladkey <jrs@world.std.com>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 *
27 * $Id$
28 */
29
30#include "defs.h"
31
Roland McGrath561c7992003-04-02 01:10:44 +000032#ifdef LINUX
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000033#include <sys/socket.h>
Roland McGrath5687ff12004-07-12 07:13:06 +000034#include <linux/sockios.h>
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000035#else
Roland McGrath8988d1e2004-10-19 23:33:50 +000036#include <sys/socket.h>
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000037#include <sys/sockio.h>
38#endif
Roland McGrath5687ff12004-07-12 07:13:06 +000039#include <arpa/inet.h>
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000040
Roland McGrathf5a47772003-06-26 22:40:42 +000041#if defined (ALPHA) || defined(SH) || defined(SH64)
Wichert Akkerman2e2553a1999-05-09 00:29:58 +000042#ifdef HAVE_SYS_IOCTL_H
43#include <sys/ioctl.h>
44#elif defined(HAVE_IOCTLS_H)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000045#include <ioctls.h>
46#endif
Wichert Akkerman2e2553a1999-05-09 00:29:58 +000047#endif
Roland McGrath5687ff12004-07-12 07:13:06 +000048#include <net/if.h>
49
Roland McGrathe6a432d2005-02-02 20:25:17 +000050static const struct xlat iffflags[] = {
51 { IFF_UP, "IFF_UP" },
52 { IFF_BROADCAST, "IFF_BROADCAST" },
53 { IFF_DEBUG, "IFF_DEBUG" },
54 { IFF_LOOPBACK, "IFF_LOOPBACK" },
55 { IFF_POINTOPOINT, "IFF_POINTOPOINT" },
56 { IFF_NOTRAILERS, "IFF_NOTRAILERS" },
57 { IFF_RUNNING, "IFF_RUNNING" },
58 { IFF_NOARP, "IFF_NOARP" },
59 { IFF_PROMISC, "IFF_PROMISC" },
60 { IFF_ALLMULTI, "IFF_ALLMULTI" },
61 { IFF_MASTER, "IFF_MASTER" },
62 { IFF_SLAVE, "IFF_SLAVE" },
63 { IFF_MULTICAST, "IFF_MULTICAST" },
64 { IFF_PORTSEL, "IFF_PORTSEL" },
65 { IFF_AUTOMEDIA, "IFF_AUTOMEDIA" },
66 { 0, NULL }
67};
68
69
70static void
71print_addr(tcp, addr, ifr)
72struct tcb *tcp;
73long addr;
74struct ifreq *ifr;
75{
76 if (ifr->ifr_addr.sa_family == AF_INET) {
77 struct sockaddr_in *sinp;
78 sinp = (struct sockaddr_in *) &ifr->ifr_addr;
79 tprintf("inet_addr(\"%s\")", inet_ntoa(sinp->sin_addr));
80 } else
81 printstr(tcp, addr, sizeof(ifr->ifr_addr.sa_data));
82}
83
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000084int
Dmitry V. Levin40284242007-03-21 13:52:14 +000085sock_ioctl(struct tcb *tcp, long code, long arg)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000086{
Roland McGrath5687ff12004-07-12 07:13:06 +000087 struct ifreq ifr;
88 struct ifconf ifc;
Roland McGrathe6a432d2005-02-02 20:25:17 +000089 const char *str = NULL;
90 unsigned char *bytes;
Roland McGrath5687ff12004-07-12 07:13:06 +000091
92 if (entering(tcp)) {
93 if (code == SIOCGIFCONF) {
Dmitry V. Levin652e4482007-03-21 14:18:17 +000094 if (umove(tcp, tcp->u_arg[2], &ifc) >= 0
95 && ifc.ifc_buf == NULL)
Roland McGrath5687ff12004-07-12 07:13:06 +000096 tprintf(", {%d -> ", ifc.ifc_len);
97 else
98 tprintf(", {");
99 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000100 return 0;
Roland McGrath5687ff12004-07-12 07:13:06 +0000101 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000102
103 switch (code) {
104#ifdef SIOCSHIWAT
105 case SIOCSHIWAT:
106#endif
107#ifdef SIOCGHIWAT
108 case SIOCGHIWAT:
109#endif
110#ifdef SIOCSLOWAT
111 case SIOCSLOWAT:
112#endif
113#ifdef SIOCGLOWAT
114 case SIOCGLOWAT:
115#endif
116#ifdef FIOSETOWN
117 case FIOSETOWN:
118#endif
119#ifdef FIOGETOWN
120 case FIOGETOWN:
121#endif
122#ifdef SIOCSPGRP
123 case SIOCSPGRP:
124#endif
125#ifdef SIOCGPGRP
126 case SIOCGPGRP:
127#endif
128#ifdef SIOCATMARK
129 case SIOCATMARK:
130#endif
131 printnum(tcp, arg, ", %#d");
132 return 1;
Roland McGrath5687ff12004-07-12 07:13:06 +0000133#ifdef LINUX
134 case SIOCGIFNAME:
Dmitry V. Levin5414bf72008-12-10 13:51:40 +0000135 case SIOCSIFNAME:
Roland McGrath5687ff12004-07-12 07:13:06 +0000136 case SIOCGIFINDEX:
Roland McGrathe6a432d2005-02-02 20:25:17 +0000137 case SIOCGIFADDR:
Dmitry V. Levin5414bf72008-12-10 13:51:40 +0000138 case SIOCSIFADDR:
Roland McGrathe6a432d2005-02-02 20:25:17 +0000139 case SIOCGIFDSTADDR:
Dmitry V. Levin5414bf72008-12-10 13:51:40 +0000140 case SIOCSIFDSTADDR:
Roland McGrathe6a432d2005-02-02 20:25:17 +0000141 case SIOCGIFBRDADDR:
Dmitry V. Levin5414bf72008-12-10 13:51:40 +0000142 case SIOCSIFBRDADDR:
Roland McGrathe6a432d2005-02-02 20:25:17 +0000143 case SIOCGIFNETMASK:
Dmitry V. Levin5414bf72008-12-10 13:51:40 +0000144 case SIOCSIFNETMASK:
Roland McGrathe6a432d2005-02-02 20:25:17 +0000145 case SIOCGIFFLAGS:
Dmitry V. Levin5414bf72008-12-10 13:51:40 +0000146 case SIOCSIFFLAGS:
Roland McGrathe6a432d2005-02-02 20:25:17 +0000147 case SIOCGIFMETRIC:
Dmitry V. Levin5414bf72008-12-10 13:51:40 +0000148 case SIOCSIFMETRIC:
Roland McGrathe6a432d2005-02-02 20:25:17 +0000149 case SIOCGIFMTU:
Dmitry V. Levin5414bf72008-12-10 13:51:40 +0000150 case SIOCSIFMTU:
Roland McGrathe6a432d2005-02-02 20:25:17 +0000151 case SIOCGIFSLAVE:
Dmitry V. Levin5414bf72008-12-10 13:51:40 +0000152 case SIOCSIFSLAVE:
Roland McGrathe6a432d2005-02-02 20:25:17 +0000153 case SIOCGIFHWADDR:
Dmitry V. Levin5414bf72008-12-10 13:51:40 +0000154 case SIOCSIFHWADDR:
Dmitry V. Levin40284242007-03-21 13:52:14 +0000155 case SIOCGIFTXQLEN:
Dmitry V. Levin5414bf72008-12-10 13:51:40 +0000156 case SIOCSIFTXQLEN:
Dmitry V. Levinecdd0bb2007-03-21 13:57:50 +0000157 case SIOCGIFMAP:
Dmitry V. Levin5414bf72008-12-10 13:51:40 +0000158 case SIOCSIFMAP:
Dmitry V. Levin652e4482007-03-21 14:18:17 +0000159 if (umove(tcp, tcp->u_arg[2], &ifr) < 0)
160 tprintf(", %#lx", tcp->u_arg[2]);
161 else if (syserror(tcp)) {
Dmitry V. Levin5414bf72008-12-10 13:51:40 +0000162 if (code == SIOCGIFNAME || code == SIOCSIFNAME)
Roland McGrathe6a432d2005-02-02 20:25:17 +0000163 tprintf(", {ifr_index=%d, ifr_name=???}", ifr.ifr_ifindex);
164 else
165 tprintf(", {ifr_name=\"%s\", ???}", ifr.ifr_name);
Dmitry V. Levin5414bf72008-12-10 13:51:40 +0000166 } else if (code == SIOCGIFNAME || code == SIOCSIFNAME)
Roland McGrathe6a432d2005-02-02 20:25:17 +0000167 tprintf(", {ifr_index=%d, ifr_name=\"%s\"}",
Roland McGrath5687ff12004-07-12 07:13:06 +0000168 ifr.ifr_ifindex, ifr.ifr_name);
Roland McGrathe6a432d2005-02-02 20:25:17 +0000169 else {
170 tprintf(", {ifr_name=\"%s\", ", ifr.ifr_name);
171 switch (code) {
172 case SIOCGIFINDEX:
173 tprintf("ifr_index=%d", ifr.ifr_ifindex);
174 break;
175 case SIOCGIFADDR:
Dmitry V. Levin5414bf72008-12-10 13:51:40 +0000176 case SIOCSIFADDR:
Roland McGrathe6a432d2005-02-02 20:25:17 +0000177 str = "ifr_addr";
178 case SIOCGIFDSTADDR:
Dmitry V. Levin5414bf72008-12-10 13:51:40 +0000179 case SIOCSIFDSTADDR:
180 if (!str)
Roland McGrathe6a432d2005-02-02 20:25:17 +0000181 str = "ifr_dstaddr";
182 case SIOCGIFBRDADDR:
Dmitry V. Levin5414bf72008-12-10 13:51:40 +0000183 case SIOCSIFBRDADDR:
184 if (!str)
Roland McGrathe6a432d2005-02-02 20:25:17 +0000185 str = "ifr_broadaddr";
186 case SIOCGIFNETMASK:
Dmitry V. Levin5414bf72008-12-10 13:51:40 +0000187 case SIOCSIFNETMASK:
188 if (!str)
Roland McGrathe6a432d2005-02-02 20:25:17 +0000189 str = "ifr_netmask";
190 tprintf("%s={", str);
191 printxval(addrfams,
192 ifr.ifr_addr.sa_family,
Denys Vlasenkoadedb512008-12-30 18:47:55 +0000193 "AF_???");
Roland McGrathe6a432d2005-02-02 20:25:17 +0000194 tprintf(", ");
195 print_addr(tcp, ((long) tcp->u_arg[2]
196 + offsetof (struct ifreq,
197 ifr_addr.sa_data)),
198 &ifr);
199 tprintf("}");
200 break;
201 case SIOCGIFHWADDR:
Dmitry V. Levin5414bf72008-12-10 13:51:40 +0000202 case SIOCSIFHWADDR:
Roland McGrathe6a432d2005-02-02 20:25:17 +0000203 /* XXX Are there other hardware addresses
204 than 6-byte MACs? */
205 bytes = (unsigned char *) &ifr.ifr_hwaddr.sa_data;
206 tprintf("ifr_hwaddr=%02x:%02x:%02x:%02x:%02x:%02x",
207 bytes[0], bytes[1], bytes[2],
208 bytes[3], bytes[4], bytes[5]);
209 break;
210 case SIOCGIFFLAGS:
Dmitry V. Levin5414bf72008-12-10 13:51:40 +0000211 case SIOCSIFFLAGS:
Roland McGrathe6a432d2005-02-02 20:25:17 +0000212 tprintf("ifr_flags=");
Roland McGrathb2dee132005-06-01 19:02:36 +0000213 printflags(iffflags, ifr.ifr_flags, "IFF_???");
Roland McGrathe6a432d2005-02-02 20:25:17 +0000214 break;
215 case SIOCGIFMETRIC:
Dmitry V. Levin5414bf72008-12-10 13:51:40 +0000216 case SIOCSIFMETRIC:
Roland McGrathe6a432d2005-02-02 20:25:17 +0000217 tprintf("ifr_metric=%d", ifr.ifr_metric);
218 break;
219 case SIOCGIFMTU:
Dmitry V. Levin5414bf72008-12-10 13:51:40 +0000220 case SIOCSIFMTU:
Roland McGrathe6a432d2005-02-02 20:25:17 +0000221 tprintf("ifr_mtu=%d", ifr.ifr_mtu);
222 break;
223 case SIOCGIFSLAVE:
Dmitry V. Levin5414bf72008-12-10 13:51:40 +0000224 case SIOCSIFSLAVE:
Roland McGrathe6a432d2005-02-02 20:25:17 +0000225 tprintf("ifr_slave=\"%s\"", ifr.ifr_slave);
226 break;
Dmitry V. Levin40284242007-03-21 13:52:14 +0000227 case SIOCGIFTXQLEN:
Dmitry V. Levin5414bf72008-12-10 13:51:40 +0000228 case SIOCSIFTXQLEN:
Dmitry V. Levin40284242007-03-21 13:52:14 +0000229 tprintf("ifr_qlen=%d", ifr.ifr_qlen);
230 break;
Dmitry V. Levinecdd0bb2007-03-21 13:57:50 +0000231 case SIOCGIFMAP:
Dmitry V. Levin5414bf72008-12-10 13:51:40 +0000232 case SIOCSIFMAP:
Dmitry V. Levinecdd0bb2007-03-21 13:57:50 +0000233 tprintf("ifr_map={mem_start=%#lx, "
234 "mem_end=%#lx, base_addr=%#x, "
235 "irq=%u, dma=%u, port=%u}",
236 ifr.ifr_map.mem_start,
237 ifr.ifr_map.mem_end,
238 (unsigned) ifr.ifr_map.base_addr,
239 (unsigned) ifr.ifr_map.irq,
240 (unsigned) ifr.ifr_map.dma,
241 (unsigned) ifr.ifr_map.port);
242 break;
Roland McGrathe6a432d2005-02-02 20:25:17 +0000243 }
244 tprintf("}");
245 }
Roland McGrath5687ff12004-07-12 07:13:06 +0000246 return 1;
247 case SIOCGIFCONF:
Dmitry V. Levin652e4482007-03-21 14:18:17 +0000248 if (umove(tcp, tcp->u_arg[2], &ifc) < 0) {
249 tprintf("???}");
250 return 1;
251 }
Roland McGrath5687ff12004-07-12 07:13:06 +0000252 tprintf("%d, ", ifc.ifc_len);
Denys Vlasenkoadedb512008-12-30 18:47:55 +0000253 if (syserror(tcp)) {
Roland McGrath5687ff12004-07-12 07:13:06 +0000254 tprintf("%lx", (unsigned long) ifc.ifc_buf);
255 } else if (ifc.ifc_buf == NULL) {
256 tprintf("NULL");
257 } else {
258 int i;
259 unsigned nifra = ifc.ifc_len / sizeof(struct ifreq);
260 struct ifreq ifra[nifra];
Dmitry V. Levin62e05962009-11-03 14:38:44 +0000261
262 if (umoven(tcp, (unsigned long) ifc.ifc_buf,
263 sizeof(ifra), (char *) ifra) < 0) {
264 tprintf("%lx}", (unsigned long) ifc.ifc_buf);
265 return 1;
266 }
Roland McGrath5687ff12004-07-12 07:13:06 +0000267 tprintf("{");
268 for (i = 0; i < nifra; ++i ) {
269 if (i > 0)
270 tprintf(", ");
271 tprintf("{\"%s\", {",
272 ifra[i].ifr_name);
273 if (verbose(tcp)) {
274 printxval(addrfams,
275 ifra[i].ifr_addr.sa_family,
276 "AF_???");
277 tprintf(", ");
Roland McGrathe6a432d2005-02-02 20:25:17 +0000278 print_addr(tcp, ((long) tcp->u_arg[2]
279 + offsetof (struct ifreq,
280 ifr_addr.sa_data)
281 + ((char *) &ifra[i]
282 - (char *) &ifra[0])),
283 &ifra[i]);
Roland McGrath5687ff12004-07-12 07:13:06 +0000284 } else
285 tprintf("...");
286 tprintf("}}");
287 }
288 tprintf("}");
289 }
290 tprintf("}");
291 return 1;
292#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000293 default:
294 return 0;
295 }
296}