svr4_sockio.c revision 160980
1/*-
2 * Copyright (c) 1998 Mark Newton
3 * Copyright (c) 1995 Christos Zoulas
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 *    derived from this software without specific prior written permission
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#include <sys/cdefs.h>
30__FBSDID("$FreeBSD: head/sys/compat/svr4/svr4_sockio.c 160980 2006-08-04 21:15:09Z brooks $");
31
32#include <sys/param.h>
33#include <sys/proc.h>
34#include <sys/systm.h>
35#include <sys/file.h>
36#include <sys/filedesc.h>
37#include <sys/sockio.h>
38#include <sys/socket.h>
39#include <net/if.h>
40
41
42#include <compat/svr4/svr4.h>
43#include <compat/svr4/svr4_util.h>
44#include <compat/svr4/svr4_ioctl.h>
45#include <compat/svr4/svr4_sockio.h>
46
47static int bsd_to_svr4_flags(int);
48
49#define bsd_to_svr4_flag(a) \
50	if (bf & __CONCAT(I,a))	sf |= __CONCAT(SVR4_I,a)
51
52static int
53bsd_to_svr4_flags(bf)
54	int bf;
55{
56	int sf = 0;
57	bsd_to_svr4_flag(FF_UP);
58	bsd_to_svr4_flag(FF_BROADCAST);
59	bsd_to_svr4_flag(FF_DEBUG);
60	bsd_to_svr4_flag(FF_LOOPBACK);
61	bsd_to_svr4_flag(FF_POINTOPOINT);
62#if defined(IFF_NOTRAILERS)
63	bsd_to_svr4_flag(FF_NOTRAILERS);
64#endif
65	if (bf & IFF_DRV_RUNNING)
66		sf |= SVR4_IFF_RUNNING;
67	bsd_to_svr4_flag(FF_NOARP);
68	bsd_to_svr4_flag(FF_PROMISC);
69	bsd_to_svr4_flag(FF_ALLMULTI);
70	bsd_to_svr4_flag(FF_MULTICAST);
71	return sf;
72}
73
74int
75svr4_sock_ioctl(fp, td, retval, fd, cmd, data)
76	struct file *fp;
77	struct thread *td;
78	register_t *retval;
79	int fd;
80	u_long cmd;
81	caddr_t data;
82{
83	int error;
84
85	*retval = 0;
86
87	switch (cmd) {
88	case SVR4_SIOCGIFNUM:
89		{
90			struct ifnet *ifp;
91			struct ifaddr *ifa;
92			int ifnum = 0;
93
94			/*
95			 * This does not return the number of physical
96			 * interfaces (if_index), but the number of interfaces
97			 * + addresses like ifconf() does, because this number
98			 * is used by code that will call SVR4_SIOCGIFCONF to
99			 * find the space needed for SVR4_SIOCGIFCONF. So we
100			 * count the number of ifreq entries that the next
101			 * SVR4_SIOCGIFCONF will return. Maybe a more correct
102			 * fix is to make SVR4_SIOCGIFCONF return only one
103			 * entry per physical interface?
104			 */
105			IFNET_RLOCK();
106			TAILQ_FOREACH(ifp, &ifnet, if_link)
107				if (TAILQ_EMPTY(&ifp->if_addrhead))
108					ifnum++;
109				else
110					TAILQ_FOREACH(ifa, &ifp->if_addrhead,
111					    ifa_link)
112						ifnum++;
113			IFNET_RUNLOCK();
114			DPRINTF(("SIOCGIFNUM %d\n", ifnum));
115			return copyout(&ifnum, data, sizeof(ifnum));
116		}
117
118	case SVR4_SIOCGIFFLAGS:
119		{
120			struct ifreq br;
121			struct svr4_ifreq sr;
122
123			if ((error = copyin(data, &sr, sizeof(sr))) != 0)
124				return error;
125
126			(void) strlcpy(br.ifr_name, sr.svr4_ifr_name,
127			    sizeof(br.ifr_name));
128			if ((error = fo_ioctl(fp, SIOCGIFFLAGS,
129					    (caddr_t) &br, td->td_ucred,
130					    td)) != 0) {
131				DPRINTF(("SIOCGIFFLAGS (%s) %s: error %d\n",
132					 br.ifr_name, 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_SIOCGIFCONF:
143		{
144			struct svr4_ifconf sc;
145
146			if ((error = copyin(data, &sc, sizeof(sc))) != 0)
147				return error;
148
149			DPRINTF(("ifreq %d svr4_ifreq %d ifc_len %d\n",
150				sizeof(struct ifreq), sizeof(struct svr4_ifreq),
151				sc.svr4_ifc_len));
152
153			if ((error = fo_ioctl(fp, OSIOCGIFCONF,
154					    (caddr_t) &sc, td->td_ucred,
155					    td)) != 0)
156				return error;
157
158			DPRINTF(("SIOCGIFCONF\n"));
159			return 0;
160		}
161
162
163	default:
164		DPRINTF(("Unknown svr4 sockio %lx\n", cmd));
165		return 0;	/* ENOSYS really */
166	}
167}
168