svr4_sockio.c revision 189106
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 189106 2009-02-27 14:12:05Z bz $");
31
32#include "opt_route.h"
33
34#include <sys/param.h>
35#include <sys/proc.h>
36#include <sys/systm.h>
37#include <sys/file.h>
38#include <sys/filedesc.h>
39#include <sys/sockio.h>
40#include <sys/socket.h>
41#include <sys/vimage.h>
42
43#include <net/if.h>
44#include <net/route.h>
45#include <net/vnet.h>
46
47#include <compat/svr4/svr4.h>
48#include <compat/svr4/svr4_util.h>
49#include <compat/svr4/svr4_ioctl.h>
50#include <compat/svr4/svr4_sockio.h>
51
52static int bsd_to_svr4_flags(int);
53
54#define bsd_to_svr4_flag(a) \
55	if (bf & __CONCAT(I,a))	sf |= __CONCAT(SVR4_I,a)
56
57static int
58bsd_to_svr4_flags(bf)
59	int bf;
60{
61	int sf = 0;
62	bsd_to_svr4_flag(FF_UP);
63	bsd_to_svr4_flag(FF_BROADCAST);
64	bsd_to_svr4_flag(FF_DEBUG);
65	bsd_to_svr4_flag(FF_LOOPBACK);
66	bsd_to_svr4_flag(FF_POINTOPOINT);
67#if defined(IFF_NOTRAILERS)
68	bsd_to_svr4_flag(FF_NOTRAILERS);
69#endif
70	if (bf & IFF_DRV_RUNNING)
71		sf |= SVR4_IFF_RUNNING;
72	bsd_to_svr4_flag(FF_NOARP);
73	bsd_to_svr4_flag(FF_PROMISC);
74	bsd_to_svr4_flag(FF_ALLMULTI);
75	bsd_to_svr4_flag(FF_MULTICAST);
76	return sf;
77}
78
79int
80svr4_sock_ioctl(fp, td, retval, fd, cmd, data)
81	struct file *fp;
82	struct thread *td;
83	register_t *retval;
84	int fd;
85	u_long cmd;
86	caddr_t data;
87{
88	int error;
89
90	*retval = 0;
91
92	switch (cmd) {
93	case SVR4_SIOCGIFNUM:
94		{
95			INIT_VNET_NET(curvnet);
96			struct ifnet *ifp;
97			struct ifaddr *ifa;
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			IFNET_RLOCK();
112			TAILQ_FOREACH(ifp, &V_ifnet, if_link)
113				if (TAILQ_EMPTY(&ifp->if_addrhead))
114					ifnum++;
115				else
116					TAILQ_FOREACH(ifa, &ifp->if_addrhead,
117					    ifa_link)
118						ifnum++;
119			IFNET_RUNLOCK();
120			DPRINTF(("SIOCGIFNUM %d\n", ifnum));
121			return copyout(&ifnum, data, sizeof(ifnum));
122		}
123
124	case SVR4_SIOCGIFFLAGS:
125		{
126			struct ifreq br;
127			struct svr4_ifreq sr;
128
129			if ((error = copyin(data, &sr, sizeof(sr))) != 0)
130				return error;
131
132			(void) strlcpy(br.ifr_name, sr.svr4_ifr_name,
133			    sizeof(br.ifr_name));
134			if ((error = fo_ioctl(fp, SIOCGIFFLAGS,
135					    (caddr_t) &br, td->td_ucred,
136					    td)) != 0) {
137				DPRINTF(("SIOCGIFFLAGS (%s) %s: error %d\n",
138					 br.ifr_name, sr.svr4_ifr_name, error));
139				return error;
140			}
141
142			sr.svr4_ifr_flags = bsd_to_svr4_flags(br.ifr_flags);
143			DPRINTF(("SIOCGIFFLAGS %s = %x\n",
144				sr.svr4_ifr_name, sr.svr4_ifr_flags));
145			return copyout(&sr, data, sizeof(sr));
146		}
147
148	case SVR4_SIOCGIFCONF:
149		{
150			struct svr4_ifconf sc;
151
152			if ((error = copyin(data, &sc, sizeof(sc))) != 0)
153				return error;
154
155			DPRINTF(("ifreq %d svr4_ifreq %d ifc_len %d\n",
156				sizeof(struct ifreq), sizeof(struct svr4_ifreq),
157				sc.svr4_ifc_len));
158
159			if ((error = fo_ioctl(fp, OSIOCGIFCONF,
160					    (caddr_t) &sc, td->td_ucred,
161					    td)) != 0)
162				return error;
163
164			DPRINTF(("SIOCGIFCONF\n"));
165			return 0;
166		}
167
168
169	default:
170		DPRINTF(("Unknown svr4 sockio %lx\n", cmd));
171		return 0;	/* ENOSYS really */
172	}
173}
174