1 /*
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 
28 #include "defs.h"
29 #include <sys/socket.h>
30 #include <linux/sockios.h>
31 #include <arpa/inet.h>
32 #if defined(ALPHA) || defined(SH) || defined(SH64)
33 # if defined(HAVE_SYS_IOCTL_H)
34 #  include <sys/ioctl.h>
35 # elif defined(HAVE_IOCTLS_H)
36 #  include <ioctls.h>
37 # endif
38 #endif
39 #include <net/if.h>
40 
41 #include "xlat/iffflags.h"
42 
43 static void
print_addr(struct tcb * tcp,long addr,struct ifreq * ifr)44 print_addr(struct tcb *tcp, long addr, struct ifreq *ifr)
45 {
46 	if (ifr->ifr_addr.sa_family == AF_INET) {
47 		struct sockaddr_in *sinp;
48 		sinp = (struct sockaddr_in *) &ifr->ifr_addr;
49 		tprintf("inet_addr(\"%s\")", inet_ntoa(sinp->sin_addr));
50 	} else
51 		printstr(tcp, addr, sizeof(ifr->ifr_addr.sa_data));
52 }
53 
54 static void
print_ifname(const char * ifname)55 print_ifname(const char *ifname)
56 {
57 	print_quoted_string(ifname, IFNAMSIZ + 1, QUOTE_0_TERMINATED);
58 }
59 
60 int
sock_ioctl(struct tcb * tcp,const unsigned int code,long arg)61 sock_ioctl(struct tcb *tcp, const unsigned int code, long arg)
62 {
63 	struct ifreq ifr;
64 	struct ifconf ifc;
65 	const char *str = NULL;
66 	unsigned char *bytes;
67 
68 	if (entering(tcp)) {
69 		switch (code) {
70 		case SIOCGIFCONF:
71 			if (umove(tcp, tcp->u_arg[2], &ifc) >= 0
72 			    && ifc.ifc_buf == NULL)
73 				tprintf(", {%d -> ", ifc.ifc_len);
74 			else
75 				tprints(", {");
76 			break;
77 		case SIOCSIFNAME:
78 			if (umove(tcp, tcp->u_arg[2], &ifr) < 0)
79 				tprintf(", %#lx", tcp->u_arg[2]);
80 			else {
81 				tprints(", {ifr_name=");
82 				print_ifname(ifr.ifr_name);
83 				tprints(", ifr_newname=");
84 				print_ifname(ifr.ifr_newname);
85 				tprints("}");
86 			}
87 			break;
88 		}
89 		return 0;
90 	}
91 
92 	switch (code) {
93 #ifdef SIOCSHIWAT
94 	case SIOCSHIWAT:
95 #endif
96 #ifdef SIOCGHIWAT
97 	case SIOCGHIWAT:
98 #endif
99 #ifdef SIOCSLOWAT
100 	case SIOCSLOWAT:
101 #endif
102 #ifdef SIOCGLOWAT
103 	case SIOCGLOWAT:
104 #endif
105 #ifdef FIOSETOWN
106 	case FIOSETOWN:
107 #endif
108 #ifdef FIOGETOWN
109 	case FIOGETOWN:
110 #endif
111 #ifdef SIOCSPGRP
112 	case SIOCSPGRP:
113 #endif
114 #ifdef SIOCGPGRP
115 	case SIOCGPGRP:
116 #endif
117 #ifdef SIOCATMARK
118 	case SIOCATMARK:
119 #endif
120 		printnum_int(tcp, arg, ", %d");
121 	case SIOCSIFNAME:
122 		return 1;
123 	case SIOCGIFNAME:
124 	case SIOCGIFINDEX:
125 	case SIOCGIFADDR:
126 	case SIOCSIFADDR:
127 	case SIOCGIFDSTADDR:
128 	case SIOCSIFDSTADDR:
129 	case SIOCGIFBRDADDR:
130 	case SIOCSIFBRDADDR:
131 	case SIOCGIFNETMASK:
132 	case SIOCSIFNETMASK:
133 	case SIOCGIFFLAGS:
134 	case SIOCSIFFLAGS:
135 	case SIOCGIFMETRIC:
136 	case SIOCSIFMETRIC:
137 	case SIOCGIFMTU:
138 	case SIOCSIFMTU:
139 	case SIOCGIFSLAVE:
140 	case SIOCSIFSLAVE:
141 	case SIOCGIFHWADDR:
142 	case SIOCSIFHWADDR:
143 	case SIOCGIFTXQLEN:
144 	case SIOCSIFTXQLEN:
145 	case SIOCGIFMAP:
146 	case SIOCSIFMAP:
147 		if (umove(tcp, tcp->u_arg[2], &ifr) < 0)
148 			tprintf(", %#lx", tcp->u_arg[2]);
149 		else if (syserror(tcp)) {
150 			if (code == SIOCGIFNAME) {
151 				tprintf(", {ifr_index=%d, ifr_name=???}",
152 					ifr.ifr_ifindex);
153 			} else {
154 				tprints(", {ifr_name=");
155 				print_ifname(ifr.ifr_name);
156 				tprints(", ???}");
157 			}
158 		} else if (code == SIOCGIFNAME) {
159 			tprintf(", {ifr_index=%d, ifr_name=", ifr.ifr_ifindex);
160 			print_ifname(ifr.ifr_name);
161 			tprints("}");
162 		} else {
163 			tprints(", {ifr_name=");
164 			print_ifname(ifr.ifr_name);
165 			tprints(", ");
166 			switch (code) {
167 			case SIOCGIFINDEX:
168 				tprintf("ifr_index=%d", ifr.ifr_ifindex);
169 				break;
170 			case SIOCGIFADDR:
171 			case SIOCSIFADDR:
172 				str = "ifr_addr";
173 			case SIOCGIFDSTADDR:
174 			case SIOCSIFDSTADDR:
175 				if (!str)
176 					str = "ifr_dstaddr";
177 			case SIOCGIFBRDADDR:
178 			case SIOCSIFBRDADDR:
179 				if (!str)
180 					str = "ifr_broadaddr";
181 			case SIOCGIFNETMASK:
182 			case SIOCSIFNETMASK:
183 				if (!str)
184 					str = "ifr_netmask";
185 				tprintf("%s={", str);
186 				printxval(addrfams,
187 					  ifr.ifr_addr.sa_family,
188 					  "AF_???");
189 				tprints(", ");
190 				print_addr(tcp, ((long) tcp->u_arg[2]
191 						 + offsetof(struct ifreq,
192 							     ifr_addr.sa_data)),
193 					   &ifr);
194 				tprints("}");
195 				break;
196 			case SIOCGIFHWADDR:
197 			case SIOCSIFHWADDR:
198 				/* XXX Are there other hardware addresses
199 				   than 6-byte MACs?  */
200 				bytes = (unsigned char *) &ifr.ifr_hwaddr.sa_data;
201 				tprintf("ifr_hwaddr=%02x:%02x:%02x:%02x:%02x:%02x",
202 					bytes[0], bytes[1], bytes[2],
203 					bytes[3], bytes[4], bytes[5]);
204 				break;
205 			case SIOCGIFFLAGS:
206 			case SIOCSIFFLAGS:
207 				tprints("ifr_flags=");
208 				printflags(iffflags, ifr.ifr_flags, "IFF_???");
209 				break;
210 			case SIOCGIFMETRIC:
211 			case SIOCSIFMETRIC:
212 				tprintf("ifr_metric=%d", ifr.ifr_metric);
213 				break;
214 			case SIOCGIFMTU:
215 			case SIOCSIFMTU:
216 				tprintf("ifr_mtu=%d", ifr.ifr_mtu);
217 				break;
218 			case SIOCGIFSLAVE:
219 			case SIOCSIFSLAVE:
220 				tprints("ifr_slave=");
221 				print_ifname(ifr.ifr_slave);
222 				break;
223 			case SIOCGIFTXQLEN:
224 			case SIOCSIFTXQLEN:
225 				tprintf("ifr_qlen=%d", ifr.ifr_qlen);
226 				break;
227 			case SIOCGIFMAP:
228 			case SIOCSIFMAP:
229 				tprintf("ifr_map={mem_start=%#lx, "
230 					"mem_end=%#lx, base_addr=%#x, "
231 					"irq=%u, dma=%u, port=%u}",
232 					ifr.ifr_map.mem_start,
233 					ifr.ifr_map.mem_end,
234 					(unsigned) ifr.ifr_map.base_addr,
235 					(unsigned) ifr.ifr_map.irq,
236 					(unsigned) ifr.ifr_map.dma,
237 					(unsigned) ifr.ifr_map.port);
238 				break;
239 			}
240 			tprints("}");
241 		}
242 		return 1;
243 	case SIOCGIFCONF:
244 		if (umove(tcp, tcp->u_arg[2], &ifc) < 0) {
245 			tprints("???}");
246 			return 1;
247 		}
248 		tprintf("%d, ", ifc.ifc_len);
249 		if (syserror(tcp)) {
250 			tprintf("%lx", (unsigned long) ifc.ifc_buf);
251 		} else if (ifc.ifc_buf == NULL) {
252 			tprints("NULL");
253 		} else {
254 			unsigned int i;
255 			unsigned int nifra = ifc.ifc_len / sizeof(struct ifreq);
256 			struct ifreq ifra[nifra];
257 
258 			if (umoven(tcp, (unsigned long) ifc.ifc_buf,
259 				sizeof(ifra), ifra) < 0) {
260 				tprintf("%lx}", (unsigned long) ifc.ifc_buf);
261 				return 1;
262 			}
263 			tprints("{");
264 			for (i = 0; i < nifra; ++i ) {
265 				if (i > 0)
266 					tprints(", ");
267 				tprints("{");
268 				print_ifname(ifra[i].ifr_newname);
269 				tprints(", {");
270 				if (verbose(tcp)) {
271 					printxval(addrfams,
272 						  ifra[i].ifr_addr.sa_family,
273 						  "AF_???");
274 					tprints(", ");
275 					print_addr(tcp, ((long) tcp->u_arg[2]
276 							 + offsetof(struct ifreq,
277 								     ifr_addr.sa_data)
278 							 + ((char *) &ifra[i]
279 							    - (char *) &ifra[0])),
280 						   &ifra[i]);
281 				} else
282 					tprints("...");
283 				tprints("}}");
284 			}
285 			tprints("}");
286 		}
287 		tprints("}");
288 		return 1;
289 	default:
290 		return 0;
291 	}
292 }
293 
SYS_FUNC(socketcall)294 SYS_FUNC(socketcall)
295 {
296 	return printargs(tcp);
297 }
298