1/*	$NetBSD: svr4_32_sockio.c,v 1.20 2008/03/21 21:54:59 ad Exp $	 */
2
3/*-
4 * Copyright (c) 1995, 2008 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Christos Zoulas.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32#include <sys/cdefs.h>
33__KERNEL_RCSID(0, "$NetBSD: svr4_32_sockio.c,v 1.20 2008/03/21 21:54:59 ad Exp $");
34
35#include <sys/param.h>
36#include <sys/proc.h>
37#include <sys/systm.h>
38#include <sys/file.h>
39#include <sys/filedesc.h>
40#include <sys/ioctl.h>
41#include <sys/termios.h>
42#include <sys/tty.h>
43#include <sys/socket.h>
44#include <sys/mount.h>
45#include <net/if.h>
46#include <sys/malloc.h>
47
48#include <sys/syscallargs.h>
49
50#include <compat/sys/socket.h>
51#include <compat/sys/sockio.h>
52
53#include <compat/svr4_32/svr4_32_types.h>
54#include <compat/svr4_32/svr4_32_util.h>
55#include <compat/svr4_32/svr4_32_signal.h>
56#include <compat/svr4_32/svr4_32_lwp.h>
57#include <compat/svr4_32/svr4_32_ucontext.h>
58#include <compat/svr4_32/svr4_32_syscallargs.h>
59#include <compat/svr4_32/svr4_32_stropts.h>
60#include <compat/svr4_32/svr4_32_ioctl.h>
61#include <compat/svr4_32/svr4_32_sockio.h>
62
63static int bsd_to_svr4_flags(int);
64
65#define bsd_to_svr4_flag(a) \
66	if (bf & __CONCAT(I,a))	sf |= __CONCAT(SVR4_I,a)
67
68static int
69bsd_to_svr4_flags(int bf)
70{
71	int sf = 0;
72	bsd_to_svr4_flag(FF_UP);
73	bsd_to_svr4_flag(FF_BROADCAST);
74	bsd_to_svr4_flag(FF_DEBUG);
75	bsd_to_svr4_flag(FF_LOOPBACK);
76	bsd_to_svr4_flag(FF_POINTOPOINT);
77	bsd_to_svr4_flag(FF_NOTRAILERS);
78	bsd_to_svr4_flag(FF_RUNNING);
79	bsd_to_svr4_flag(FF_NOARP);
80	bsd_to_svr4_flag(FF_PROMISC);
81	bsd_to_svr4_flag(FF_ALLMULTI);
82	bsd_to_svr4_flag(FF_MULTICAST);
83	return sf;
84}
85
86int
87svr4_32_sock_ioctl(file_t *fp, struct lwp *l, register_t *retval, int fd, u_long cmd, void *data)
88{
89	int error;
90	int (*ctl)(file_t *, u_long, void *) = fp->f_ops->fo_ioctl;
91
92	*retval = 0;
93
94	switch (cmd) {
95	case SVR4_SIOCGIFNUM:
96		{
97			struct ifnet *ifp;
98			int ifnum = 0;
99
100			/*
101			 * This does not return the number of physical
102			 * interfaces (if_index), but the number of interfaces
103			 * + addresses like ifconf() does, because this number
104			 * is used by code that will call SVR4_SIOCGIFCONF to
105			 * find the space needed for SVR4_SIOCGIFCONF. So we
106			 * count the number of ifreq entries that the next
107			 * SVR4_SIOCGIFCONF will return. Maybe a more correct
108			 * fix is to make SVR4_SIOCGIFCONF return only one
109			 * entry per physical interface?
110			 */
111
112			IFNET_FOREACH(ifp)
113				ifnum += svr4_count_ifnum(ifp)
114
115			DPRINTF(("SIOCGIFNUM %d\n", ifnum));
116			return copyout(&ifnum, data, sizeof(ifnum));
117		}
118
119	case SVR4_32_SIOCGIFFLAGS:
120		{
121			struct oifreq br;
122			struct svr4_32_ifreq sr;
123
124			if ((error = copyin(data, &sr, sizeof(sr))) != 0)
125				return error;
126
127			(void) strncpy(br.ifr_name, sr.svr4_ifr_name,
128			    sizeof(br.ifr_name));
129
130			if ((error = (*ctl)(fp, SIOCGIFFLAGS, &br)) != 0) {
131				DPRINTF(("SIOCGIFFLAGS %s: error %d\n",
132					 sr.svr4_ifr_name, error));
133				return error;
134			}
135
136			sr.svr4_ifr_flags = bsd_to_svr4_flags(br.ifr_flags);
137			DPRINTF(("SIOCGIFFLAGS %s = %x\n",
138				sr.svr4_ifr_name, sr.svr4_ifr_flags));
139			return copyout(&sr, data, sizeof(sr));
140		}
141
142	case SVR4_32_SIOCGIFCONF:
143		{
144			struct svr4_32_ifconf sc;
145			struct oifconf ifc;
146
147			if ((error = copyin(data, &sc, sizeof(sc))) != 0)
148				return error;
149
150			DPRINTF(("ifreq %ld svr4_32_ifreq %ld ifc_len %d\n",
151				(unsigned long)sizeof(struct oifreq),
152				(unsigned long)sizeof(struct svr4_32_ifreq),
153				sc.svr4_32_ifc_len));
154
155			ifc.ifc_len = sc.svr4_32_ifc_len;
156			ifc.ifc_buf = NETBSD32PTR64(sc.ifc_ifcu.ifcu_buf);
157
158			if ((error = (*ctl)(fp, OOSIOCGIFCONF, &ifc)) != 0)
159				return error;
160
161			DPRINTF(("SIOCGIFCONF\n"));
162			return 0;
163		}
164
165
166	default:
167		DPRINTF(("Unknown svr4_32 sockio %lx\n", cmd));
168		return 0;	/* ENOSYS really */
169	}
170}
171