blob: 5e2ab57d90733279988183cf25f06e6be1c8629f [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 McGrathd9f816f2004-09-04 03:39:20 +000050extern const struct xlat addrfams[];
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000051
Roland McGrathe6a432d2005-02-02 20:25:17 +000052static const struct xlat iffflags[] = {
53 { IFF_UP, "IFF_UP" },
54 { IFF_BROADCAST, "IFF_BROADCAST" },
55 { IFF_DEBUG, "IFF_DEBUG" },
56 { IFF_LOOPBACK, "IFF_LOOPBACK" },
57 { IFF_POINTOPOINT, "IFF_POINTOPOINT" },
58 { IFF_NOTRAILERS, "IFF_NOTRAILERS" },
59 { IFF_RUNNING, "IFF_RUNNING" },
60 { IFF_NOARP, "IFF_NOARP" },
61 { IFF_PROMISC, "IFF_PROMISC" },
62 { IFF_ALLMULTI, "IFF_ALLMULTI" },
63 { IFF_MASTER, "IFF_MASTER" },
64 { IFF_SLAVE, "IFF_SLAVE" },
65 { IFF_MULTICAST, "IFF_MULTICAST" },
66 { IFF_PORTSEL, "IFF_PORTSEL" },
67 { IFF_AUTOMEDIA, "IFF_AUTOMEDIA" },
68 { 0, NULL }
69};
70
71
72static void
73print_addr(tcp, addr, ifr)
74struct tcb *tcp;
75long addr;
76struct ifreq *ifr;
77{
78 if (ifr->ifr_addr.sa_family == AF_INET) {
79 struct sockaddr_in *sinp;
80 sinp = (struct sockaddr_in *) &ifr->ifr_addr;
81 tprintf("inet_addr(\"%s\")", inet_ntoa(sinp->sin_addr));
82 } else
83 printstr(tcp, addr, sizeof(ifr->ifr_addr.sa_data));
84}
85
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000086int
Dmitry V. Levin40284242007-03-21 13:52:14 +000087sock_ioctl(struct tcb *tcp, long code, long arg)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000088{
Roland McGrath5687ff12004-07-12 07:13:06 +000089 struct ifreq ifr;
90 struct ifconf ifc;
Roland McGrathe6a432d2005-02-02 20:25:17 +000091 const char *str = NULL;
92 unsigned char *bytes;
Roland McGrath5687ff12004-07-12 07:13:06 +000093
94 if (entering(tcp)) {
95 if (code == SIOCGIFCONF) {
Dmitry V. Levin652e4482007-03-21 14:18:17 +000096 if (umove(tcp, tcp->u_arg[2], &ifc) >= 0
97 && ifc.ifc_buf == NULL)
Roland McGrath5687ff12004-07-12 07:13:06 +000098 tprintf(", {%d -> ", ifc.ifc_len);
99 else
100 tprintf(", {");
101 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000102 return 0;
Roland McGrath5687ff12004-07-12 07:13:06 +0000103 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000104
105 switch (code) {
106#ifdef SIOCSHIWAT
107 case SIOCSHIWAT:
108#endif
109#ifdef SIOCGHIWAT
110 case SIOCGHIWAT:
111#endif
112#ifdef SIOCSLOWAT
113 case SIOCSLOWAT:
114#endif
115#ifdef SIOCGLOWAT
116 case SIOCGLOWAT:
117#endif
118#ifdef FIOSETOWN
119 case FIOSETOWN:
120#endif
121#ifdef FIOGETOWN
122 case FIOGETOWN:
123#endif
124#ifdef SIOCSPGRP
125 case SIOCSPGRP:
126#endif
127#ifdef SIOCGPGRP
128 case SIOCGPGRP:
129#endif
130#ifdef SIOCATMARK
131 case SIOCATMARK:
132#endif
133 printnum(tcp, arg, ", %#d");
134 return 1;
Roland McGrath5687ff12004-07-12 07:13:06 +0000135#ifdef LINUX
136 case SIOCGIFNAME:
Dmitry V. Levin5414bf72008-12-10 13:51:40 +0000137 case SIOCSIFNAME:
Roland McGrath5687ff12004-07-12 07:13:06 +0000138 case SIOCGIFINDEX:
Roland McGrathe6a432d2005-02-02 20:25:17 +0000139 case SIOCGIFADDR:
Dmitry V. Levin5414bf72008-12-10 13:51:40 +0000140 case SIOCSIFADDR:
Roland McGrathe6a432d2005-02-02 20:25:17 +0000141 case SIOCGIFDSTADDR:
Dmitry V. Levin5414bf72008-12-10 13:51:40 +0000142 case SIOCSIFDSTADDR:
Roland McGrathe6a432d2005-02-02 20:25:17 +0000143 case SIOCGIFBRDADDR:
Dmitry V. Levin5414bf72008-12-10 13:51:40 +0000144 case SIOCSIFBRDADDR:
Roland McGrathe6a432d2005-02-02 20:25:17 +0000145 case SIOCGIFNETMASK:
Dmitry V. Levin5414bf72008-12-10 13:51:40 +0000146 case SIOCSIFNETMASK:
Roland McGrathe6a432d2005-02-02 20:25:17 +0000147 case SIOCGIFFLAGS:
Dmitry V. Levin5414bf72008-12-10 13:51:40 +0000148 case SIOCSIFFLAGS:
Roland McGrathe6a432d2005-02-02 20:25:17 +0000149 case SIOCGIFMETRIC:
Dmitry V. Levin5414bf72008-12-10 13:51:40 +0000150 case SIOCSIFMETRIC:
Roland McGrathe6a432d2005-02-02 20:25:17 +0000151 case SIOCGIFMTU:
Dmitry V. Levin5414bf72008-12-10 13:51:40 +0000152 case SIOCSIFMTU:
Roland McGrathe6a432d2005-02-02 20:25:17 +0000153 case SIOCGIFSLAVE:
Dmitry V. Levin5414bf72008-12-10 13:51:40 +0000154 case SIOCSIFSLAVE:
Roland McGrathe6a432d2005-02-02 20:25:17 +0000155 case SIOCGIFHWADDR:
Dmitry V. Levin5414bf72008-12-10 13:51:40 +0000156 case SIOCSIFHWADDR:
Dmitry V. Levin40284242007-03-21 13:52:14 +0000157 case SIOCGIFTXQLEN:
Dmitry V. Levin5414bf72008-12-10 13:51:40 +0000158 case SIOCSIFTXQLEN:
Dmitry V. Levinecdd0bb2007-03-21 13:57:50 +0000159 case SIOCGIFMAP:
Dmitry V. Levin5414bf72008-12-10 13:51:40 +0000160 case SIOCSIFMAP:
Dmitry V. Levin652e4482007-03-21 14:18:17 +0000161 if (umove(tcp, tcp->u_arg[2], &ifr) < 0)
162 tprintf(", %#lx", tcp->u_arg[2]);
163 else if (syserror(tcp)) {
Dmitry V. Levin5414bf72008-12-10 13:51:40 +0000164 if (code == SIOCGIFNAME || code == SIOCSIFNAME)
Roland McGrathe6a432d2005-02-02 20:25:17 +0000165 tprintf(", {ifr_index=%d, ifr_name=???}", ifr.ifr_ifindex);
166 else
167 tprintf(", {ifr_name=\"%s\", ???}", ifr.ifr_name);
Dmitry V. Levin5414bf72008-12-10 13:51:40 +0000168 } else if (code == SIOCGIFNAME || code == SIOCSIFNAME)
Roland McGrathe6a432d2005-02-02 20:25:17 +0000169 tprintf(", {ifr_index=%d, ifr_name=\"%s\"}",
Roland McGrath5687ff12004-07-12 07:13:06 +0000170 ifr.ifr_ifindex, ifr.ifr_name);
Roland McGrathe6a432d2005-02-02 20:25:17 +0000171 else {
172 tprintf(", {ifr_name=\"%s\", ", ifr.ifr_name);
173 switch (code) {
174 case SIOCGIFINDEX:
175 tprintf("ifr_index=%d", ifr.ifr_ifindex);
176 break;
177 case SIOCGIFADDR:
Dmitry V. Levin5414bf72008-12-10 13:51:40 +0000178 case SIOCSIFADDR:
Roland McGrathe6a432d2005-02-02 20:25:17 +0000179 str = "ifr_addr";
180 case SIOCGIFDSTADDR:
Dmitry V. Levin5414bf72008-12-10 13:51:40 +0000181 case SIOCSIFDSTADDR:
182 if (!str)
Roland McGrathe6a432d2005-02-02 20:25:17 +0000183 str = "ifr_dstaddr";
184 case SIOCGIFBRDADDR:
Dmitry V. Levin5414bf72008-12-10 13:51:40 +0000185 case SIOCSIFBRDADDR:
186 if (!str)
Roland McGrathe6a432d2005-02-02 20:25:17 +0000187 str = "ifr_broadaddr";
188 case SIOCGIFNETMASK:
Dmitry V. Levin5414bf72008-12-10 13:51:40 +0000189 case SIOCSIFNETMASK:
190 if (!str)
Roland McGrathe6a432d2005-02-02 20:25:17 +0000191 str = "ifr_netmask";
192 tprintf("%s={", str);
193 printxval(addrfams,
194 ifr.ifr_addr.sa_family,
195 "AF_???");
196 tprintf(", ");
197 print_addr(tcp, ((long) tcp->u_arg[2]
198 + offsetof (struct ifreq,
199 ifr_addr.sa_data)),
200 &ifr);
201 tprintf("}");
202 break;
203 case SIOCGIFHWADDR:
Dmitry V. Levin5414bf72008-12-10 13:51:40 +0000204 case SIOCSIFHWADDR:
Roland McGrathe6a432d2005-02-02 20:25:17 +0000205 /* XXX Are there other hardware addresses
206 than 6-byte MACs? */
207 bytes = (unsigned char *) &ifr.ifr_hwaddr.sa_data;
208 tprintf("ifr_hwaddr=%02x:%02x:%02x:%02x:%02x:%02x",
209 bytes[0], bytes[1], bytes[2],
210 bytes[3], bytes[4], bytes[5]);
211 break;
212 case SIOCGIFFLAGS:
Dmitry V. Levin5414bf72008-12-10 13:51:40 +0000213 case SIOCSIFFLAGS:
Roland McGrathe6a432d2005-02-02 20:25:17 +0000214 tprintf("ifr_flags=");
Roland McGrathb2dee132005-06-01 19:02:36 +0000215 printflags(iffflags, ifr.ifr_flags, "IFF_???");
Roland McGrathe6a432d2005-02-02 20:25:17 +0000216 break;
217 case SIOCGIFMETRIC:
Dmitry V. Levin5414bf72008-12-10 13:51:40 +0000218 case SIOCSIFMETRIC:
Roland McGrathe6a432d2005-02-02 20:25:17 +0000219 tprintf("ifr_metric=%d", ifr.ifr_metric);
220 break;
221 case SIOCGIFMTU:
Dmitry V. Levin5414bf72008-12-10 13:51:40 +0000222 case SIOCSIFMTU:
Roland McGrathe6a432d2005-02-02 20:25:17 +0000223 tprintf("ifr_mtu=%d", ifr.ifr_mtu);
224 break;
225 case SIOCGIFSLAVE:
Dmitry V. Levin5414bf72008-12-10 13:51:40 +0000226 case SIOCSIFSLAVE:
Roland McGrathe6a432d2005-02-02 20:25:17 +0000227 tprintf("ifr_slave=\"%s\"", ifr.ifr_slave);
228 break;
Dmitry V. Levin40284242007-03-21 13:52:14 +0000229 case SIOCGIFTXQLEN:
Dmitry V. Levin5414bf72008-12-10 13:51:40 +0000230 case SIOCSIFTXQLEN:
Dmitry V. Levin40284242007-03-21 13:52:14 +0000231 tprintf("ifr_qlen=%d", ifr.ifr_qlen);
232 break;
Dmitry V. Levinecdd0bb2007-03-21 13:57:50 +0000233 case SIOCGIFMAP:
Dmitry V. Levin5414bf72008-12-10 13:51:40 +0000234 case SIOCSIFMAP:
Dmitry V. Levinecdd0bb2007-03-21 13:57:50 +0000235 tprintf("ifr_map={mem_start=%#lx, "
236 "mem_end=%#lx, base_addr=%#x, "
237 "irq=%u, dma=%u, port=%u}",
238 ifr.ifr_map.mem_start,
239 ifr.ifr_map.mem_end,
240 (unsigned) ifr.ifr_map.base_addr,
241 (unsigned) ifr.ifr_map.irq,
242 (unsigned) ifr.ifr_map.dma,
243 (unsigned) ifr.ifr_map.port);
244 break;
Roland McGrathe6a432d2005-02-02 20:25:17 +0000245 }
246 tprintf("}");
247 }
Roland McGrath5687ff12004-07-12 07:13:06 +0000248 return 1;
249 case SIOCGIFCONF:
Dmitry V. Levin652e4482007-03-21 14:18:17 +0000250 if (umove(tcp, tcp->u_arg[2], &ifc) < 0) {
251 tprintf("???}");
252 return 1;
253 }
Roland McGrath5687ff12004-07-12 07:13:06 +0000254 tprintf("%d, ", ifc.ifc_len);
255 if (syserror(tcp)) {
256 tprintf("%lx", (unsigned long) ifc.ifc_buf);
257 } else if (ifc.ifc_buf == NULL) {
258 tprintf("NULL");
259 } else {
260 int i;
261 unsigned nifra = ifc.ifc_len / sizeof(struct ifreq);
262 struct ifreq ifra[nifra];
263 umoven(tcp, (unsigned long) ifc.ifc_buf, sizeof(ifra),
264 (char *) ifra);
265 tprintf("{");
266 for (i = 0; i < nifra; ++i ) {
267 if (i > 0)
268 tprintf(", ");
269 tprintf("{\"%s\", {",
270 ifra[i].ifr_name);
271 if (verbose(tcp)) {
272 printxval(addrfams,
273 ifra[i].ifr_addr.sa_family,
274 "AF_???");
275 tprintf(", ");
Roland McGrathe6a432d2005-02-02 20:25:17 +0000276 print_addr(tcp, ((long) tcp->u_arg[2]
277 + offsetof (struct ifreq,
278 ifr_addr.sa_data)
279 + ((char *) &ifra[i]
280 - (char *) &ifra[0])),
281 &ifra[i]);
Roland McGrath5687ff12004-07-12 07:13:06 +0000282 } else
283 tprintf("...");
284 tprintf("}}");
285 }
286 tprintf("}");
287 }
288 tprintf("}");
289 return 1;
290#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000291 default:
292 return 0;
293 }
294}