blob: bbbdd5b9908352e3eb1df3fb5ba08b8d8a08fa3e [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
Denys Vlasenko12014262011-05-30 14:00:14 +020071print_addr(struct tcb *tcp, long addr, struct ifreq *ifr)
Roland McGrathe6a432d2005-02-02 20:25:17 +000072{
73 if (ifr->ifr_addr.sa_family == AF_INET) {
74 struct sockaddr_in *sinp;
75 sinp = (struct sockaddr_in *) &ifr->ifr_addr;
76 tprintf("inet_addr(\"%s\")", inet_ntoa(sinp->sin_addr));
77 } else
78 printstr(tcp, addr, sizeof(ifr->ifr_addr.sa_data));
79}
80
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000081int
Dmitry V. Levin40284242007-03-21 13:52:14 +000082sock_ioctl(struct tcb *tcp, long code, long arg)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000083{
Roland McGrath5687ff12004-07-12 07:13:06 +000084 struct ifreq ifr;
85 struct ifconf ifc;
Roland McGrathe6a432d2005-02-02 20:25:17 +000086 const char *str = NULL;
87 unsigned char *bytes;
Roland McGrath5687ff12004-07-12 07:13:06 +000088
89 if (entering(tcp)) {
90 if (code == SIOCGIFCONF) {
Dmitry V. Levin652e4482007-03-21 14:18:17 +000091 if (umove(tcp, tcp->u_arg[2], &ifc) >= 0
92 && ifc.ifc_buf == NULL)
Roland McGrath5687ff12004-07-12 07:13:06 +000093 tprintf(", {%d -> ", ifc.ifc_len);
94 else
95 tprintf(", {");
96 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000097 return 0;
Roland McGrath5687ff12004-07-12 07:13:06 +000098 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000099
100 switch (code) {
101#ifdef SIOCSHIWAT
102 case SIOCSHIWAT:
103#endif
104#ifdef SIOCGHIWAT
105 case SIOCGHIWAT:
106#endif
107#ifdef SIOCSLOWAT
108 case SIOCSLOWAT:
109#endif
110#ifdef SIOCGLOWAT
111 case SIOCGLOWAT:
112#endif
113#ifdef FIOSETOWN
114 case FIOSETOWN:
115#endif
116#ifdef FIOGETOWN
117 case FIOGETOWN:
118#endif
119#ifdef SIOCSPGRP
120 case SIOCSPGRP:
121#endif
122#ifdef SIOCGPGRP
123 case SIOCGPGRP:
124#endif
125#ifdef SIOCATMARK
126 case SIOCATMARK:
127#endif
128 printnum(tcp, arg, ", %#d");
129 return 1;
Roland McGrath5687ff12004-07-12 07:13:06 +0000130#ifdef LINUX
131 case SIOCGIFNAME:
Dmitry V. Levin5414bf72008-12-10 13:51:40 +0000132 case SIOCSIFNAME:
Roland McGrath5687ff12004-07-12 07:13:06 +0000133 case SIOCGIFINDEX:
Roland McGrathe6a432d2005-02-02 20:25:17 +0000134 case SIOCGIFADDR:
Dmitry V. Levin5414bf72008-12-10 13:51:40 +0000135 case SIOCSIFADDR:
Roland McGrathe6a432d2005-02-02 20:25:17 +0000136 case SIOCGIFDSTADDR:
Dmitry V. Levin5414bf72008-12-10 13:51:40 +0000137 case SIOCSIFDSTADDR:
Roland McGrathe6a432d2005-02-02 20:25:17 +0000138 case SIOCGIFBRDADDR:
Dmitry V. Levin5414bf72008-12-10 13:51:40 +0000139 case SIOCSIFBRDADDR:
Roland McGrathe6a432d2005-02-02 20:25:17 +0000140 case SIOCGIFNETMASK:
Dmitry V. Levin5414bf72008-12-10 13:51:40 +0000141 case SIOCSIFNETMASK:
Roland McGrathe6a432d2005-02-02 20:25:17 +0000142 case SIOCGIFFLAGS:
Dmitry V. Levin5414bf72008-12-10 13:51:40 +0000143 case SIOCSIFFLAGS:
Roland McGrathe6a432d2005-02-02 20:25:17 +0000144 case SIOCGIFMETRIC:
Dmitry V. Levin5414bf72008-12-10 13:51:40 +0000145 case SIOCSIFMETRIC:
Roland McGrathe6a432d2005-02-02 20:25:17 +0000146 case SIOCGIFMTU:
Dmitry V. Levin5414bf72008-12-10 13:51:40 +0000147 case SIOCSIFMTU:
Roland McGrathe6a432d2005-02-02 20:25:17 +0000148 case SIOCGIFSLAVE:
Dmitry V. Levin5414bf72008-12-10 13:51:40 +0000149 case SIOCSIFSLAVE:
Roland McGrathe6a432d2005-02-02 20:25:17 +0000150 case SIOCGIFHWADDR:
Dmitry V. Levin5414bf72008-12-10 13:51:40 +0000151 case SIOCSIFHWADDR:
Dmitry V. Levin40284242007-03-21 13:52:14 +0000152 case SIOCGIFTXQLEN:
Dmitry V. Levin5414bf72008-12-10 13:51:40 +0000153 case SIOCSIFTXQLEN:
Dmitry V. Levinecdd0bb2007-03-21 13:57:50 +0000154 case SIOCGIFMAP:
Dmitry V. Levin5414bf72008-12-10 13:51:40 +0000155 case SIOCSIFMAP:
Dmitry V. Levin652e4482007-03-21 14:18:17 +0000156 if (umove(tcp, tcp->u_arg[2], &ifr) < 0)
157 tprintf(", %#lx", tcp->u_arg[2]);
158 else if (syserror(tcp)) {
Dmitry V. Levin5414bf72008-12-10 13:51:40 +0000159 if (code == SIOCGIFNAME || code == SIOCSIFNAME)
Roland McGrathe6a432d2005-02-02 20:25:17 +0000160 tprintf(", {ifr_index=%d, ifr_name=???}", ifr.ifr_ifindex);
161 else
162 tprintf(", {ifr_name=\"%s\", ???}", ifr.ifr_name);
Dmitry V. Levin5414bf72008-12-10 13:51:40 +0000163 } else if (code == SIOCGIFNAME || code == SIOCSIFNAME)
Roland McGrathe6a432d2005-02-02 20:25:17 +0000164 tprintf(", {ifr_index=%d, ifr_name=\"%s\"}",
Roland McGrath5687ff12004-07-12 07:13:06 +0000165 ifr.ifr_ifindex, ifr.ifr_name);
Roland McGrathe6a432d2005-02-02 20:25:17 +0000166 else {
167 tprintf(", {ifr_name=\"%s\", ", ifr.ifr_name);
168 switch (code) {
169 case SIOCGIFINDEX:
170 tprintf("ifr_index=%d", ifr.ifr_ifindex);
171 break;
172 case SIOCGIFADDR:
Dmitry V. Levin5414bf72008-12-10 13:51:40 +0000173 case SIOCSIFADDR:
Roland McGrathe6a432d2005-02-02 20:25:17 +0000174 str = "ifr_addr";
175 case SIOCGIFDSTADDR:
Dmitry V. Levin5414bf72008-12-10 13:51:40 +0000176 case SIOCSIFDSTADDR:
177 if (!str)
Roland McGrathe6a432d2005-02-02 20:25:17 +0000178 str = "ifr_dstaddr";
179 case SIOCGIFBRDADDR:
Dmitry V. Levin5414bf72008-12-10 13:51:40 +0000180 case SIOCSIFBRDADDR:
181 if (!str)
Roland McGrathe6a432d2005-02-02 20:25:17 +0000182 str = "ifr_broadaddr";
183 case SIOCGIFNETMASK:
Dmitry V. Levin5414bf72008-12-10 13:51:40 +0000184 case SIOCSIFNETMASK:
185 if (!str)
Roland McGrathe6a432d2005-02-02 20:25:17 +0000186 str = "ifr_netmask";
187 tprintf("%s={", str);
188 printxval(addrfams,
189 ifr.ifr_addr.sa_family,
Denys Vlasenkoadedb512008-12-30 18:47:55 +0000190 "AF_???");
Roland McGrathe6a432d2005-02-02 20:25:17 +0000191 tprintf(", ");
192 print_addr(tcp, ((long) tcp->u_arg[2]
Denys Vlasenkob63256e2011-06-07 12:13:24 +0200193 + offsetof(struct ifreq,
Roland McGrathe6a432d2005-02-02 20:25:17 +0000194 ifr_addr.sa_data)),
195 &ifr);
196 tprintf("}");
197 break;
198 case SIOCGIFHWADDR:
Dmitry V. Levin5414bf72008-12-10 13:51:40 +0000199 case SIOCSIFHWADDR:
Roland McGrathe6a432d2005-02-02 20:25:17 +0000200 /* XXX Are there other hardware addresses
201 than 6-byte MACs? */
202 bytes = (unsigned char *) &ifr.ifr_hwaddr.sa_data;
203 tprintf("ifr_hwaddr=%02x:%02x:%02x:%02x:%02x:%02x",
204 bytes[0], bytes[1], bytes[2],
205 bytes[3], bytes[4], bytes[5]);
206 break;
207 case SIOCGIFFLAGS:
Dmitry V. Levin5414bf72008-12-10 13:51:40 +0000208 case SIOCSIFFLAGS:
Roland McGrathe6a432d2005-02-02 20:25:17 +0000209 tprintf("ifr_flags=");
Roland McGrathb2dee132005-06-01 19:02:36 +0000210 printflags(iffflags, ifr.ifr_flags, "IFF_???");
Roland McGrathe6a432d2005-02-02 20:25:17 +0000211 break;
212 case SIOCGIFMETRIC:
Dmitry V. Levin5414bf72008-12-10 13:51:40 +0000213 case SIOCSIFMETRIC:
Roland McGrathe6a432d2005-02-02 20:25:17 +0000214 tprintf("ifr_metric=%d", ifr.ifr_metric);
215 break;
216 case SIOCGIFMTU:
Dmitry V. Levin5414bf72008-12-10 13:51:40 +0000217 case SIOCSIFMTU:
Roland McGrathe6a432d2005-02-02 20:25:17 +0000218 tprintf("ifr_mtu=%d", ifr.ifr_mtu);
219 break;
220 case SIOCGIFSLAVE:
Dmitry V. Levin5414bf72008-12-10 13:51:40 +0000221 case SIOCSIFSLAVE:
Roland McGrathe6a432d2005-02-02 20:25:17 +0000222 tprintf("ifr_slave=\"%s\"", ifr.ifr_slave);
223 break;
Dmitry V. Levin40284242007-03-21 13:52:14 +0000224 case SIOCGIFTXQLEN:
Dmitry V. Levin5414bf72008-12-10 13:51:40 +0000225 case SIOCSIFTXQLEN:
Dmitry V. Levin40284242007-03-21 13:52:14 +0000226 tprintf("ifr_qlen=%d", ifr.ifr_qlen);
227 break;
Dmitry V. Levinecdd0bb2007-03-21 13:57:50 +0000228 case SIOCGIFMAP:
Dmitry V. Levin5414bf72008-12-10 13:51:40 +0000229 case SIOCSIFMAP:
Dmitry V. Levinecdd0bb2007-03-21 13:57:50 +0000230 tprintf("ifr_map={mem_start=%#lx, "
231 "mem_end=%#lx, base_addr=%#x, "
232 "irq=%u, dma=%u, port=%u}",
233 ifr.ifr_map.mem_start,
234 ifr.ifr_map.mem_end,
235 (unsigned) ifr.ifr_map.base_addr,
236 (unsigned) ifr.ifr_map.irq,
237 (unsigned) ifr.ifr_map.dma,
238 (unsigned) ifr.ifr_map.port);
239 break;
Roland McGrathe6a432d2005-02-02 20:25:17 +0000240 }
241 tprintf("}");
242 }
Roland McGrath5687ff12004-07-12 07:13:06 +0000243 return 1;
244 case SIOCGIFCONF:
Dmitry V. Levin652e4482007-03-21 14:18:17 +0000245 if (umove(tcp, tcp->u_arg[2], &ifc) < 0) {
246 tprintf("???}");
247 return 1;
248 }
Roland McGrath5687ff12004-07-12 07:13:06 +0000249 tprintf("%d, ", ifc.ifc_len);
Denys Vlasenkoadedb512008-12-30 18:47:55 +0000250 if (syserror(tcp)) {
Roland McGrath5687ff12004-07-12 07:13:06 +0000251 tprintf("%lx", (unsigned long) ifc.ifc_buf);
252 } else if (ifc.ifc_buf == NULL) {
253 tprintf("NULL");
254 } else {
255 int i;
256 unsigned nifra = ifc.ifc_len / sizeof(struct ifreq);
257 struct ifreq ifra[nifra];
Dmitry V. Levin62e05962009-11-03 14:38:44 +0000258
259 if (umoven(tcp, (unsigned long) ifc.ifc_buf,
260 sizeof(ifra), (char *) ifra) < 0) {
261 tprintf("%lx}", (unsigned long) ifc.ifc_buf);
262 return 1;
263 }
Roland McGrath5687ff12004-07-12 07:13:06 +0000264 tprintf("{");
265 for (i = 0; i < nifra; ++i ) {
266 if (i > 0)
267 tprintf(", ");
268 tprintf("{\"%s\", {",
269 ifra[i].ifr_name);
270 if (verbose(tcp)) {
271 printxval(addrfams,
272 ifra[i].ifr_addr.sa_family,
273 "AF_???");
274 tprintf(", ");
Roland McGrathe6a432d2005-02-02 20:25:17 +0000275 print_addr(tcp, ((long) tcp->u_arg[2]
Denys Vlasenkob63256e2011-06-07 12:13:24 +0200276 + offsetof(struct ifreq,
Roland McGrathe6a432d2005-02-02 20:25:17 +0000277 ifr_addr.sa_data)
278 + ((char *) &ifra[i]
279 - (char *) &ifra[0])),
280 &ifra[i]);
Roland McGrath5687ff12004-07-12 07:13:06 +0000281 } else
282 tprintf("...");
283 tprintf("}}");
284 }
285 tprintf("}");
286 }
287 tprintf("}");
288 return 1;
289#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000290 default:
291 return 0;
292 }
293}