linux32_socket.c revision 1.9.8.1 1 1.9.8.1 matt /* $NetBSD: linux32_socket.c,v 1.9.8.1 2010/04/21 00:27:26 matt Exp $ */
2 1.1 manu
3 1.1 manu /*-
4 1.1 manu * Copyright (c) 2006 Emmanuel Dreyfus, all rights reserved.
5 1.1 manu *
6 1.1 manu * Redistribution and use in source and binary forms, with or without
7 1.1 manu * modification, are permitted provided that the following conditions
8 1.1 manu * are met:
9 1.1 manu * 1. Redistributions of source code must retain the above copyright
10 1.1 manu * notice, this list of conditions and the following disclaimer.
11 1.1 manu * 2. Redistributions in binary form must reproduce the above copyright
12 1.1 manu * notice, this list of conditions and the following disclaimer in the
13 1.1 manu * documentation and/or other materials provided with the distribution.
14 1.1 manu * 3. All advertising materials mentioning features or use of this software
15 1.1 manu * must display the following acknowledgement:
16 1.1 manu * This product includes software developed by Emmanuel Dreyfus
17 1.1 manu * 4. The name of the author may not be used to endorse or promote
18 1.1 manu * products derived from this software without specific prior written
19 1.1 manu * permission.
20 1.1 manu *
21 1.1 manu * THIS SOFTWARE IS PROVIDED BY THE THE AUTHOR AND CONTRIBUTORS ``AS IS''
22 1.1 manu * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
23 1.1 manu * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 1.1 manu * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS
25 1.1 manu * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 1.1 manu * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 1.1 manu * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 1.1 manu * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 1.1 manu * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 1.1 manu * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 1.1 manu * POSSIBILITY OF SUCH DAMAGE.
32 1.1 manu */
33 1.1 manu
34 1.1 manu #include <sys/cdefs.h>
35 1.1 manu
36 1.9.8.1 matt __KERNEL_RCSID(0, "$NetBSD: linux32_socket.c,v 1.9.8.1 2010/04/21 00:27:26 matt Exp $");
37 1.1 manu
38 1.1 manu #include <sys/types.h>
39 1.1 manu #include <sys/param.h>
40 1.1 manu #include <sys/fstypes.h>
41 1.1 manu #include <sys/signal.h>
42 1.1 manu #include <sys/dirent.h>
43 1.1 manu #include <sys/kernel.h>
44 1.1 manu #include <sys/fcntl.h>
45 1.1 manu #include <sys/select.h>
46 1.1 manu #include <sys/proc.h>
47 1.1 manu #include <sys/ucred.h>
48 1.1 manu #include <sys/swap.h>
49 1.6 christos #include <sys/file.h>
50 1.6 christos #include <sys/vnode.h>
51 1.6 christos #include <sys/filedesc.h>
52 1.1 manu
53 1.1 manu #include <machine/types.h>
54 1.1 manu
55 1.6 christos #include <net/if.h>
56 1.8 njoly #include <net/if_dl.h>
57 1.8 njoly #include <net/if_types.h>
58 1.7 njoly #include <net/route.h>
59 1.7 njoly
60 1.7 njoly #include <netinet/in.h>
61 1.7 njoly #include <netinet/ip_mroute.h>
62 1.6 christos
63 1.1 manu #include <sys/syscallargs.h>
64 1.1 manu
65 1.1 manu #include <compat/netbsd32/netbsd32.h>
66 1.7 njoly #include <compat/netbsd32/netbsd32_ioctl.h>
67 1.1 manu #include <compat/netbsd32/netbsd32_conv.h>
68 1.1 manu #include <compat/netbsd32/netbsd32_syscallargs.h>
69 1.8 njoly
70 1.8 njoly #include <compat/sys/socket.h>
71 1.6 christos #include <compat/sys/sockio.h>
72 1.1 manu
73 1.1 manu #include <compat/linux/common/linux_types.h>
74 1.6 christos #include <compat/linux/common/linux_types.h>
75 1.1 manu #include <compat/linux/common/linux_signal.h>
76 1.1 manu #include <compat/linux/common/linux_machdep.h>
77 1.1 manu #include <compat/linux/common/linux_misc.h>
78 1.1 manu #include <compat/linux/common/linux_oldolduname.h>
79 1.6 christos #include <compat/linux/common/linux_ioctl.h>
80 1.6 christos #include <compat/linux/common/linux_sockio.h>
81 1.1 manu #include <compat/linux/linux_syscallargs.h>
82 1.1 manu
83 1.1 manu #include <compat/linux32/common/linux32_types.h>
84 1.1 manu #include <compat/linux32/common/linux32_signal.h>
85 1.1 manu #include <compat/linux32/common/linux32_machdep.h>
86 1.1 manu #include <compat/linux32/common/linux32_sysctl.h>
87 1.1 manu #include <compat/linux32/common/linux32_socketcall.h>
88 1.8 njoly #include <compat/linux32/common/linux32_sockio.h>
89 1.6 christos #include <compat/linux32/common/linux32_ioctl.h>
90 1.1 manu #include <compat/linux32/linux32_syscallargs.h>
91 1.1 manu
92 1.9.8.1 matt int linux32_getifname(struct lwp *, register_t *, void *);
93 1.9 njoly int linux32_getifconf(struct lwp *, register_t *, void *);
94 1.8 njoly int linux32_getifhwaddr(struct lwp *, register_t *, u_int, void *);
95 1.8 njoly
96 1.1 manu int
97 1.5 dsl linux32_sys_socketpair(struct lwp *l, const struct linux32_sys_socketpair_args *uap, register_t *retval)
98 1.1 manu {
99 1.5 dsl /* {
100 1.1 manu syscallarg(int) domain;
101 1.1 manu syscallarg(int) type;
102 1.1 manu syscallarg(int) protocol;
103 1.1 manu syscallarg(netbsd32_intp) rsv;
104 1.5 dsl } */
105 1.1 manu struct linux_sys_socketpair_args ua;
106 1.1 manu
107 1.1 manu NETBSD32TO64_UAP(domain);
108 1.1 manu NETBSD32TO64_UAP(type);
109 1.1 manu NETBSD32TO64_UAP(protocol);
110 1.1 manu NETBSD32TOP_UAP(rsv, int)
111 1.1 manu
112 1.1 manu return linux_sys_socketpair(l, &ua, retval);
113 1.1 manu }
114 1.1 manu
115 1.1 manu int
116 1.5 dsl linux32_sys_sendto(struct lwp *l, const struct linux32_sys_sendto_args *uap, register_t *retval)
117 1.1 manu {
118 1.5 dsl /* {
119 1.1 manu syscallarg(int) s;
120 1.5 dsl syscallarg(netbsd32_voidp) msg;
121 1.1 manu syscallarg(int) len;
122 1.1 manu syscallarg(int) flags;
123 1.1 manu syscallarg(netbsd32_osockaddrp_t) to;
124 1.1 manu syscallarg(int) tolen;
125 1.5 dsl } */
126 1.1 manu struct linux_sys_sendto_args ua;
127 1.1 manu
128 1.1 manu NETBSD32TO64_UAP(s);
129 1.1 manu NETBSD32TOP_UAP(msg, void);
130 1.1 manu NETBSD32TO64_UAP(len);
131 1.1 manu NETBSD32TO64_UAP(flags);
132 1.1 manu NETBSD32TOP_UAP(to, struct osockaddr);
133 1.1 manu NETBSD32TO64_UAP(tolen);
134 1.1 manu
135 1.1 manu return linux_sys_sendto(l, &ua, retval);
136 1.1 manu }
137 1.1 manu
138 1.1 manu
139 1.1 manu int
140 1.5 dsl linux32_sys_recvfrom(struct lwp *l, const struct linux32_sys_recvfrom_args *uap, register_t *retval)
141 1.1 manu {
142 1.5 dsl /* {
143 1.1 manu syscallarg(int) s;
144 1.5 dsl syscallarg(netbsd32_voidp) buf;
145 1.1 manu syscallarg(netbsd32_size_t) len;
146 1.1 manu syscallarg(int) flags;
147 1.1 manu syscallarg(netbsd32_osockaddrp_t) from;
148 1.1 manu syscallarg(netbsd32_intp) fromlenaddr;
149 1.5 dsl } */
150 1.1 manu struct linux_sys_recvfrom_args ua;
151 1.1 manu
152 1.1 manu NETBSD32TO64_UAP(s);
153 1.1 manu NETBSD32TOP_UAP(buf, void);
154 1.1 manu NETBSD32TO64_UAP(len);
155 1.1 manu NETBSD32TO64_UAP(flags);
156 1.1 manu NETBSD32TOP_UAP(from, struct osockaddr);
157 1.1 manu NETBSD32TOP_UAP(fromlenaddr, unsigned int);
158 1.1 manu
159 1.1 manu return linux_sys_recvfrom(l, &ua, retval);
160 1.1 manu }
161 1.1 manu
162 1.1 manu int
163 1.5 dsl linux32_sys_setsockopt(struct lwp *l, const struct linux32_sys_setsockopt_args *uap, register_t *retval)
164 1.1 manu {
165 1.5 dsl /* {
166 1.1 manu syscallarg(int) s;
167 1.1 manu syscallarg(int) level;
168 1.1 manu syscallarg(int) optname;
169 1.1 manu syscallarg(netbsd32_voidp) optval;
170 1.1 manu syscallarg(int) optlen;
171 1.5 dsl } */
172 1.1 manu struct linux_sys_setsockopt_args ua;
173 1.1 manu
174 1.1 manu NETBSD32TO64_UAP(s);
175 1.1 manu NETBSD32TO64_UAP(level);
176 1.1 manu NETBSD32TO64_UAP(optname);
177 1.1 manu NETBSD32TOP_UAP(optval, void);
178 1.1 manu NETBSD32TO64_UAP(optlen);
179 1.1 manu
180 1.1 manu return linux_sys_setsockopt(l, &ua, retval);
181 1.1 manu }
182 1.1 manu
183 1.1 manu
184 1.1 manu int
185 1.5 dsl linux32_sys_getsockopt(struct lwp *l, const struct linux32_sys_getsockopt_args *uap, register_t *retval)
186 1.1 manu {
187 1.5 dsl /* {
188 1.1 manu syscallarg(int) s;
189 1.1 manu syscallarg(int) level;
190 1.1 manu syscallarg(int) optname;
191 1.1 manu syscallarg(netbsd32_voidp) optval;
192 1.1 manu syscallarg(netbsd32_intp) optlen;
193 1.5 dsl } */
194 1.1 manu struct linux_sys_getsockopt_args ua;
195 1.1 manu
196 1.1 manu NETBSD32TO64_UAP(s);
197 1.1 manu NETBSD32TO64_UAP(level);
198 1.1 manu NETBSD32TO64_UAP(optname);
199 1.1 manu NETBSD32TOP_UAP(optval, void);
200 1.1 manu NETBSD32TOP_UAP(optlen, int);
201 1.1 manu
202 1.1 manu return linux_sys_getsockopt(l, &ua, retval);
203 1.1 manu }
204 1.1 manu
205 1.1 manu int
206 1.5 dsl linux32_sys_socket(struct lwp *l, const struct linux32_sys_socket_args *uap, register_t *retval)
207 1.1 manu {
208 1.5 dsl /* {
209 1.1 manu syscallarg(int) domain;
210 1.1 manu syscallarg(int) type;
211 1.1 manu syscallarg(int) protocol;
212 1.5 dsl } */
213 1.1 manu struct linux_sys_socket_args ua;
214 1.1 manu
215 1.1 manu NETBSD32TO64_UAP(domain);
216 1.1 manu NETBSD32TO64_UAP(type);
217 1.1 manu NETBSD32TO64_UAP(protocol);
218 1.1 manu
219 1.1 manu return linux_sys_socket(l, &ua, retval);
220 1.1 manu }
221 1.1 manu
222 1.1 manu int
223 1.5 dsl linux32_sys_bind(struct lwp *l, const struct linux32_sys_bind_args *uap, register_t *retval)
224 1.1 manu {
225 1.5 dsl /* {
226 1.1 manu syscallarg(int) s;
227 1.1 manu syscallarg(netbsd32_osockaddrp_t) name;
228 1.1 manu syscallarg(int) namelen;
229 1.5 dsl } */
230 1.1 manu struct linux_sys_bind_args ua;
231 1.1 manu
232 1.1 manu NETBSD32TO64_UAP(s);
233 1.1 manu NETBSD32TOP_UAP(name, struct osockaddr)
234 1.1 manu NETBSD32TO64_UAP(namelen);
235 1.1 manu
236 1.1 manu return linux_sys_bind(l, &ua, retval);
237 1.1 manu }
238 1.1 manu
239 1.1 manu int
240 1.5 dsl linux32_sys_connect(struct lwp *l, const struct linux32_sys_connect_args *uap, register_t *retval)
241 1.1 manu {
242 1.5 dsl /* {
243 1.1 manu syscallarg(int) s;
244 1.1 manu syscallarg(netbsd32_osockaddrp_t) name;
245 1.1 manu syscallarg(int) namelen;
246 1.5 dsl } */
247 1.1 manu struct linux_sys_connect_args ua;
248 1.1 manu
249 1.1 manu NETBSD32TO64_UAP(s);
250 1.1 manu NETBSD32TOP_UAP(name, struct osockaddr)
251 1.1 manu NETBSD32TO64_UAP(namelen);
252 1.1 manu
253 1.2 manu #ifdef DEBUG_LINUX
254 1.1 manu printf("linux32_sys_connect: s = %d, name = %p, namelen = %d\n",
255 1.1 manu SCARG(&ua, s), SCARG(&ua, name), SCARG(&ua, namelen));
256 1.2 manu #endif
257 1.1 manu
258 1.1 manu return linux_sys_connect(l, &ua, retval);
259 1.1 manu }
260 1.1 manu
261 1.1 manu int
262 1.5 dsl linux32_sys_accept(struct lwp *l, const struct linux32_sys_accept_args *uap, register_t *retval)
263 1.1 manu {
264 1.5 dsl /* {
265 1.1 manu syscallarg(int) s;
266 1.1 manu syscallarg(netbsd32_osockaddrp_t) name;
267 1.1 manu syscallarg(netbsd32_intp) anamelen;
268 1.5 dsl } */
269 1.1 manu struct linux_sys_accept_args ua;
270 1.1 manu
271 1.1 manu NETBSD32TO64_UAP(s);
272 1.1 manu NETBSD32TOP_UAP(name, struct osockaddr)
273 1.1 manu NETBSD32TOP_UAP(anamelen, int);
274 1.1 manu
275 1.1 manu return linux_sys_accept(l, &ua, retval);
276 1.1 manu }
277 1.1 manu
278 1.1 manu int
279 1.5 dsl linux32_sys_getpeername(struct lwp *l, const struct linux32_sys_getpeername_args *uap, register_t *retval)
280 1.1 manu {
281 1.5 dsl /* {
282 1.1 manu syscallarg(int) fdes;
283 1.1 manu syscallarg(netbsd32_sockaddrp_t) asa;
284 1.1 manu syscallarg(netbsd32_intp) alen;
285 1.5 dsl } */
286 1.1 manu struct linux_sys_getpeername_args ua;
287 1.1 manu
288 1.1 manu NETBSD32TO64_UAP(fdes);
289 1.1 manu NETBSD32TOP_UAP(asa, struct sockaddr)
290 1.1 manu NETBSD32TOP_UAP(alen, int);
291 1.1 manu
292 1.1 manu return linux_sys_getpeername(l, &ua, retval);
293 1.1 manu }
294 1.1 manu
295 1.1 manu int
296 1.5 dsl linux32_sys_getsockname(struct lwp *l, const struct linux32_sys_getsockname_args *uap, register_t *retval)
297 1.1 manu {
298 1.5 dsl /* {
299 1.1 manu syscallarg(int) fdec;
300 1.1 manu syscallarg(netbsd32_charp) asa;
301 1.1 manu syscallarg(netbsd32_intp) alen;
302 1.5 dsl } */
303 1.1 manu struct linux_sys_getsockname_args ua;
304 1.1 manu
305 1.1 manu NETBSD32TO64_UAP(fdec);
306 1.1 manu NETBSD32TOP_UAP(asa, char)
307 1.1 manu NETBSD32TOP_UAP(alen, int);
308 1.1 manu
309 1.1 manu return linux_sys_getsockname(l, &ua, retval);
310 1.1 manu }
311 1.1 manu
312 1.1 manu int
313 1.5 dsl linux32_sys_sendmsg(struct lwp *l, const struct linux32_sys_sendmsg_args *uap, register_t *retval)
314 1.1 manu {
315 1.5 dsl /* {
316 1.1 manu syscallarg(int) s;
317 1.1 manu syscallarg(netbsd32_msghdrp_t) msg;
318 1.1 manu syscallarg(int) flags;
319 1.5 dsl } */
320 1.1 manu struct linux_sys_sendmsg_args ua;
321 1.1 manu
322 1.1 manu NETBSD32TO64_UAP(s);
323 1.1 manu NETBSD32TOP_UAP(msg, struct msghdr);
324 1.1 manu NETBSD32TO64_UAP(flags);
325 1.1 manu
326 1.1 manu return linux_sys_sendmsg(l, &ua, retval);
327 1.1 manu }
328 1.1 manu
329 1.1 manu int
330 1.5 dsl linux32_sys_recvmsg(struct lwp *l, const struct linux32_sys_recvmsg_args *uap, register_t *retval)
331 1.1 manu {
332 1.5 dsl /* {
333 1.1 manu syscallarg(int) s;
334 1.1 manu syscallarg(netbsd32_msghdrp_t) msg;
335 1.1 manu syscallarg(int) flags;
336 1.5 dsl } */
337 1.1 manu struct linux_sys_recvmsg_args ua;
338 1.1 manu
339 1.1 manu NETBSD32TO64_UAP(s);
340 1.1 manu NETBSD32TOP_UAP(msg, struct msghdr);
341 1.1 manu NETBSD32TO64_UAP(flags);
342 1.1 manu
343 1.1 manu return linux_sys_recvmsg(l, &ua, retval);
344 1.1 manu }
345 1.1 manu
346 1.1 manu int
347 1.5 dsl linux32_sys_send(struct lwp *l, const struct linux32_sys_send_args *uap, register_t *retval)
348 1.1 manu {
349 1.5 dsl /* {
350 1.1 manu syscallarg(int) s;
351 1.1 manu syscallarg(netbsd32_voidp) buf;
352 1.1 manu syscallarg(int) len;
353 1.1 manu syscallarg(int) flags;
354 1.5 dsl } */
355 1.1 manu struct sys_sendto_args ua;
356 1.1 manu
357 1.1 manu NETBSD32TO64_UAP(s);
358 1.1 manu NETBSD32TOP_UAP(buf, void);
359 1.1 manu NETBSD32TO64_UAP(len);
360 1.1 manu NETBSD32TO64_UAP(flags);
361 1.1 manu SCARG(&ua, to) = NULL;
362 1.1 manu SCARG(&ua, tolen) = 0;
363 1.1 manu
364 1.1 manu return sys_sendto(l, &ua, retval);
365 1.1 manu }
366 1.1 manu
367 1.1 manu int
368 1.5 dsl linux32_sys_recv(struct lwp *l, const struct linux32_sys_recv_args *uap, register_t *retval)
369 1.1 manu {
370 1.5 dsl /* {
371 1.1 manu syscallarg(int) s;
372 1.1 manu syscallarg(netbsd32_voidp) buf;
373 1.1 manu syscallarg(int) len;
374 1.1 manu syscallarg(int) flags;
375 1.5 dsl } */
376 1.1 manu struct sys_recvfrom_args ua;
377 1.1 manu
378 1.1 manu NETBSD32TO64_UAP(s);
379 1.1 manu NETBSD32TOP_UAP(buf, void);
380 1.1 manu NETBSD32TO64_UAP(len);
381 1.1 manu NETBSD32TO64_UAP(flags);
382 1.1 manu SCARG(&ua, from) = NULL;
383 1.1 manu SCARG(&ua, fromlenaddr) = NULL;
384 1.1 manu
385 1.1 manu return sys_recvfrom(l, &ua, retval);
386 1.1 manu }
387 1.6 christos
388 1.6 christos int
389 1.9.8.1 matt linux32_getifname(struct lwp *l, register_t *retval, void *data)
390 1.9.8.1 matt {
391 1.9.8.1 matt struct ifnet *ifp;
392 1.9.8.1 matt struct linux32_ifreq ifr;
393 1.9.8.1 matt int error;
394 1.9.8.1 matt
395 1.9.8.1 matt error = copyin(data, &ifr, sizeof(ifr));
396 1.9.8.1 matt if (error)
397 1.9.8.1 matt return error;
398 1.9.8.1 matt
399 1.9.8.1 matt if (ifr.ifr_ifru.ifru_ifindex >= if_indexlim)
400 1.9.8.1 matt return ENODEV;
401 1.9.8.1 matt
402 1.9.8.1 matt ifp = ifindex2ifnet[ifr.ifr_ifru.ifru_ifindex];
403 1.9.8.1 matt if (ifp == NULL)
404 1.9.8.1 matt return ENODEV;
405 1.9.8.1 matt
406 1.9.8.1 matt strncpy(ifr.ifr_name, ifp->if_xname, sizeof(ifr.ifr_name));
407 1.9.8.1 matt
408 1.9.8.1 matt return copyout(&ifr, data, sizeof(ifr));
409 1.9.8.1 matt }
410 1.9.8.1 matt
411 1.9.8.1 matt int
412 1.9 njoly linux32_getifconf(struct lwp *l, register_t *retval, void *data)
413 1.9 njoly {
414 1.9 njoly struct linux32_ifreq ifr, *ifrp;
415 1.9 njoly struct netbsd32_ifconf *ifc = data;
416 1.9 njoly struct ifnet *ifp;
417 1.9 njoly struct ifaddr *ifa;
418 1.9 njoly struct sockaddr *sa;
419 1.9 njoly struct osockaddr *osa;
420 1.9 njoly int space, error = 0;
421 1.9 njoly const int sz = (int)sizeof(ifr);
422 1.9 njoly
423 1.9 njoly ifrp = (struct linux32_ifreq *)NETBSD32PTR64(ifc->ifc_req);
424 1.9 njoly if (ifrp == NULL)
425 1.9 njoly space = 0;
426 1.9 njoly else
427 1.9 njoly space = ifc->ifc_len;
428 1.9 njoly
429 1.9 njoly IFNET_FOREACH(ifp) {
430 1.9 njoly (void)strncpy(ifr.ifr_name, ifp->if_xname,
431 1.9 njoly sizeof(ifr.ifr_name));
432 1.9 njoly if (ifr.ifr_name[sizeof(ifr.ifr_name) - 1] != '\0')
433 1.9 njoly return ENAMETOOLONG;
434 1.9 njoly if (IFADDR_EMPTY(ifp))
435 1.9 njoly continue;
436 1.9 njoly IFADDR_FOREACH(ifa, ifp) {
437 1.9 njoly sa = ifa->ifa_addr;
438 1.9 njoly if (sa->sa_family != AF_INET ||
439 1.9 njoly sa->sa_len > sizeof(*osa))
440 1.9 njoly continue;
441 1.9 njoly memcpy(&ifr.ifr_addr, sa, sa->sa_len);
442 1.9 njoly osa = (struct osockaddr *)&ifr.ifr_addr;
443 1.9 njoly osa->sa_family = sa->sa_family;
444 1.9 njoly if (space >= sz) {
445 1.9 njoly error = copyout(&ifr, ifrp, sz);
446 1.9 njoly if (error != 0)
447 1.9 njoly return error;
448 1.9 njoly ifrp++;
449 1.9 njoly }
450 1.9 njoly space -= sz;
451 1.9 njoly }
452 1.9 njoly }
453 1.9 njoly
454 1.9 njoly if (ifrp != NULL)
455 1.9 njoly ifc->ifc_len -= space;
456 1.9 njoly else
457 1.9 njoly ifc->ifc_len = -space;
458 1.9 njoly
459 1.9 njoly return 0;
460 1.9 njoly }
461 1.9 njoly
462 1.9 njoly int
463 1.8 njoly linux32_getifhwaddr(struct lwp *l, register_t *retval, u_int fd,
464 1.8 njoly void *data)
465 1.8 njoly {
466 1.8 njoly struct linux32_ifreq lreq;
467 1.8 njoly file_t *fp;
468 1.8 njoly struct ifaddr *ifa;
469 1.8 njoly struct ifnet *ifp;
470 1.8 njoly struct sockaddr_dl *sadl;
471 1.8 njoly int error, found;
472 1.8 njoly int index, ifnum;
473 1.8 njoly
474 1.8 njoly /*
475 1.8 njoly * We can't emulate this ioctl by calling sys_ioctl() to run
476 1.8 njoly * SIOCGIFCONF, because the user buffer is not of the right
477 1.8 njoly * type to take those results. We can't use kernel buffers to
478 1.8 njoly * receive the results, as the implementation of sys_ioctl()
479 1.8 njoly * and ifconf() [which implements SIOCGIFCONF] use
480 1.8 njoly * copyin()/copyout() which will fail on kernel addresses.
481 1.8 njoly *
482 1.8 njoly * So, we must duplicate code from sys_ioctl() and ifconf(). Ugh.
483 1.8 njoly */
484 1.8 njoly
485 1.8 njoly if ((fp = fd_getfile(fd)) == NULL)
486 1.8 njoly return (EBADF);
487 1.8 njoly
488 1.8 njoly KERNEL_LOCK(1, NULL);
489 1.8 njoly
490 1.8 njoly if ((fp->f_flag & (FREAD | FWRITE)) == 0) {
491 1.8 njoly error = EBADF;
492 1.8 njoly goto out;
493 1.8 njoly }
494 1.8 njoly
495 1.8 njoly error = copyin(data, &lreq, sizeof(lreq));
496 1.8 njoly if (error)
497 1.8 njoly goto out;
498 1.8 njoly lreq.ifr_name[LINUX32_IFNAMSIZ-1] = '\0'; /* just in case */
499 1.8 njoly
500 1.8 njoly /*
501 1.8 njoly * Try real interface name first, then fake "ethX"
502 1.8 njoly */
503 1.8 njoly found = 0;
504 1.8 njoly IFNET_FOREACH(ifp) {
505 1.8 njoly if (found)
506 1.8 njoly break;
507 1.8 njoly if (strcmp(lreq.ifr_name, ifp->if_xname))
508 1.8 njoly /* not this interface */
509 1.8 njoly continue;
510 1.8 njoly found=1;
511 1.8 njoly if (IFADDR_EMPTY(ifp)) {
512 1.8 njoly error = ENODEV;
513 1.8 njoly goto out;
514 1.8 njoly }
515 1.8 njoly IFADDR_FOREACH(ifa, ifp) {
516 1.8 njoly sadl = satosdl(ifa->ifa_addr);
517 1.8 njoly /* only return ethernet addresses */
518 1.8 njoly /* XXX what about FDDI, etc. ? */
519 1.8 njoly if (sadl->sdl_family != AF_LINK ||
520 1.8 njoly sadl->sdl_type != IFT_ETHER)
521 1.8 njoly continue;
522 1.8 njoly memcpy(&lreq.ifr_hwaddr.sa_data, CLLADDR(sadl),
523 1.8 njoly MIN(sadl->sdl_alen,
524 1.8 njoly sizeof(lreq.ifr_hwaddr.sa_data)));
525 1.8 njoly lreq.ifr_hwaddr.sa_family =
526 1.8 njoly sadl->sdl_family;
527 1.8 njoly error = copyout(&lreq, data, sizeof(lreq));
528 1.8 njoly goto out;
529 1.8 njoly }
530 1.8 njoly }
531 1.8 njoly
532 1.8 njoly if (strncmp(lreq.ifr_name, "eth", 3) == 0) {
533 1.8 njoly for (ifnum = 0, index = 3;
534 1.8 njoly lreq.ifr_name[index] != '\0' && index < LINUX32_IFNAMSIZ;
535 1.8 njoly index++) {
536 1.8 njoly ifnum *= 10;
537 1.8 njoly ifnum += lreq.ifr_name[index] - '0';
538 1.8 njoly }
539 1.8 njoly
540 1.8 njoly error = EINVAL; /* in case we don't find one */
541 1.8 njoly found = 0;
542 1.8 njoly IFNET_FOREACH(ifp) {
543 1.8 njoly if (found)
544 1.8 njoly break;
545 1.8 njoly memcpy(lreq.ifr_name, ifp->if_xname,
546 1.8 njoly MIN(LINUX32_IFNAMSIZ, IFNAMSIZ));
547 1.8 njoly IFADDR_FOREACH(ifa, ifp) {
548 1.8 njoly sadl = satosdl(ifa->ifa_addr);
549 1.8 njoly /* only return ethernet addresses */
550 1.8 njoly /* XXX what about FDDI, etc. ? */
551 1.8 njoly if (sadl->sdl_family != AF_LINK ||
552 1.8 njoly sadl->sdl_type != IFT_ETHER)
553 1.8 njoly continue;
554 1.8 njoly if (ifnum--)
555 1.8 njoly /* not the reqested iface */
556 1.8 njoly continue;
557 1.8 njoly memcpy(&lreq.ifr_hwaddr.sa_data,
558 1.8 njoly CLLADDR(sadl),
559 1.8 njoly MIN(sadl->sdl_alen,
560 1.8 njoly sizeof(lreq.ifr_hwaddr.sa_data)));
561 1.8 njoly lreq.ifr_hwaddr.sa_family =
562 1.8 njoly sadl->sdl_family;
563 1.8 njoly error = copyout(&lreq, data, sizeof(lreq));
564 1.8 njoly found = 1;
565 1.8 njoly break;
566 1.8 njoly }
567 1.8 njoly }
568 1.8 njoly } else {
569 1.8 njoly /* unknown interface, not even an "eth*" name */
570 1.8 njoly error = ENODEV;
571 1.8 njoly }
572 1.8 njoly
573 1.8 njoly out:
574 1.8 njoly KERNEL_UNLOCK_ONE(NULL);
575 1.8 njoly fd_putfile(fd);
576 1.8 njoly return error;
577 1.8 njoly }
578 1.8 njoly
579 1.8 njoly int
580 1.6 christos linux32_ioctl_socket(struct lwp *l, const struct linux32_sys_ioctl_args *uap, register_t *retval)
581 1.6 christos {
582 1.6 christos /* {
583 1.6 christos syscallarg(int) fd;
584 1.6 christos syscallarg(u_long) com;
585 1.6 christos syscallarg(void *) data;
586 1.6 christos } */
587 1.6 christos u_long com;
588 1.6 christos int error = 0, isdev = 0, dosys = 1;
589 1.6 christos struct netbsd32_ioctl_args ia;
590 1.6 christos file_t *fp;
591 1.6 christos struct vnode *vp;
592 1.6 christos int (*ioctlf)(file_t *, u_long, void *);
593 1.6 christos struct ioctl_pt pt;
594 1.6 christos
595 1.6 christos if ((fp = fd_getfile(SCARG(uap, fd))) == NULL)
596 1.6 christos return (EBADF);
597 1.6 christos
598 1.6 christos if (fp->f_type == DTYPE_VNODE) {
599 1.6 christos vp = (struct vnode *)fp->f_data;
600 1.6 christos isdev = vp->v_type == VCHR;
601 1.6 christos }
602 1.6 christos
603 1.6 christos /*
604 1.6 christos * Don't try to interpret socket ioctl calls that are done
605 1.6 christos * on a device filedescriptor, just pass them through, to
606 1.6 christos * emulate Linux behaviour. Use PTIOCLINUX so that the
607 1.6 christos * device will only handle these if it's prepared to do
608 1.6 christos * so, to avoid unexpected things from happening.
609 1.6 christos */
610 1.6 christos if (isdev) {
611 1.6 christos dosys = 0;
612 1.6 christos ioctlf = fp->f_ops->fo_ioctl;
613 1.6 christos pt.com = SCARG(uap, com);
614 1.6 christos pt.data = (void *)NETBSD32PTR64(SCARG(uap, data));
615 1.6 christos error = ioctlf(fp, PTIOCLINUX, &pt);
616 1.6 christos /*
617 1.6 christos * XXX hack: if the function returns EJUSTRETURN,
618 1.6 christos * it has stuffed a sysctl return value in pt.data.
619 1.6 christos */
620 1.6 christos if (error == EJUSTRETURN) {
621 1.6 christos retval[0] = (register_t)pt.data;
622 1.6 christos error = 0;
623 1.6 christos }
624 1.6 christos goto out;
625 1.6 christos }
626 1.6 christos
627 1.6 christos com = SCARG(uap, com);
628 1.6 christos retval[0] = 0;
629 1.6 christos
630 1.6 christos switch (com) {
631 1.9.8.1 matt case LINUX_SIOCGIFNAME:
632 1.9.8.1 matt error = linux32_getifname(l, retval, SCARG_P32(uap, data));
633 1.9.8.1 matt dosys = 0;
634 1.9.8.1 matt break;
635 1.6 christos case LINUX_SIOCGIFCONF:
636 1.9 njoly error = linux32_getifconf(l, retval, SCARG_P32(uap, data));
637 1.9 njoly dosys = 0;
638 1.6 christos break;
639 1.6 christos case LINUX_SIOCGIFFLAGS:
640 1.6 christos SCARG(&ia, com) = OSIOCGIFFLAGS;
641 1.6 christos break;
642 1.6 christos case LINUX_SIOCSIFFLAGS:
643 1.6 christos SCARG(&ia, com) = OSIOCSIFFLAGS;
644 1.6 christos break;
645 1.6 christos case LINUX_SIOCGIFADDR:
646 1.6 christos SCARG(&ia, com) = OOSIOCGIFADDR;
647 1.6 christos break;
648 1.6 christos case LINUX_SIOCGIFDSTADDR:
649 1.6 christos SCARG(&ia, com) = OOSIOCGIFDSTADDR;
650 1.6 christos break;
651 1.6 christos case LINUX_SIOCGIFBRDADDR:
652 1.6 christos SCARG(&ia, com) = OOSIOCGIFBRDADDR;
653 1.6 christos break;
654 1.6 christos case LINUX_SIOCGIFNETMASK:
655 1.6 christos SCARG(&ia, com) = OOSIOCGIFNETMASK;
656 1.6 christos break;
657 1.6 christos case LINUX_SIOCADDMULTI:
658 1.6 christos SCARG(&ia, com) = OSIOCADDMULTI;
659 1.6 christos break;
660 1.6 christos case LINUX_SIOCDELMULTI:
661 1.6 christos SCARG(&ia, com) = OSIOCDELMULTI;
662 1.6 christos break;
663 1.6 christos case LINUX_SIOCGIFHWADDR:
664 1.8 njoly error = linux32_getifhwaddr(l, retval, SCARG(uap, fd),
665 1.8 njoly SCARG_P32(uap, data));
666 1.6 christos dosys = 0;
667 1.6 christos break;
668 1.6 christos default:
669 1.6 christos error = EINVAL;
670 1.6 christos }
671 1.6 christos
672 1.6 christos out:
673 1.6 christos fd_putfile(SCARG(uap, fd));
674 1.6 christos
675 1.6 christos if (error == 0 && dosys) {
676 1.6 christos SCARG(&ia, fd) = SCARG(uap, fd);
677 1.6 christos SCARG(&ia, data) = SCARG(uap, data);
678 1.6 christos error = netbsd32_ioctl(curlwp, &ia, retval);
679 1.6 christos }
680 1.6 christos
681 1.6 christos return error;
682 1.6 christos }
683