svr4_sockio.c revision 49267
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 * $Id$
29 */
30
31#include <sys/param.h>
32#include <sys/proc.h>
33#include <sys/systm.h>
34#include <sys/file.h>
35#include <sys/filedesc.h>
36#include <sys/sockio.h>
37#include <sys/termios.h>
38#include <sys/tty.h>
39#include <sys/socket.h>
40#include <sys/mount.h>
41#include <net/if.h>
42#include <sys/malloc.h>
43
44#include <sys/sysproto.h>
45
46#include <svr4/svr4.h>
47#include <svr4/svr4_types.h>
48#include <svr4/svr4_util.h>
49#include <svr4/svr4_signal.h>
50#include <svr4/svr4_proto.h>
51#include <svr4/svr4_stropts.h>
52#include <svr4/svr4_ioctl.h>
53#include <svr4/svr4_sockio.h>
54
55static int bsd_to_svr4_flags __P((int));
56
57#define bsd_to_svr4_flag(a) \
58	if (bf & __CONCAT(I,a))	sf |= __CONCAT(SVR4_I,a)
59
60static int
61bsd_to_svr4_flags(bf)
62	int bf;
63{
64	int sf = 0;
65	bsd_to_svr4_flag(FF_UP);
66	bsd_to_svr4_flag(FF_BROADCAST);
67	bsd_to_svr4_flag(FF_DEBUG);
68	bsd_to_svr4_flag(FF_LOOPBACK);
69	bsd_to_svr4_flag(FF_POINTOPOINT);
70#if defined(IFF_NOTRAILERS)
71	bsd_to_svr4_flag(FF_NOTRAILERS);
72#endif
73	bsd_to_svr4_flag(FF_RUNNING);
74	bsd_to_svr4_flag(FF_NOARP);
75	bsd_to_svr4_flag(FF_PROMISC);
76	bsd_to_svr4_flag(FF_ALLMULTI);
77	bsd_to_svr4_flag(FF_MULTICAST);
78	return sf;
79}
80
81int
82svr4_sock_ioctl(fp, p, retval, fd, cmd, data)
83	struct file *fp;
84	struct proc *p;
85	register_t *retval;
86	int fd;
87	u_long cmd;
88	caddr_t data;
89{
90	int error;
91	int (*ctl) __P((struct file *, u_long,  caddr_t, struct proc *)) =
92			fp->f_ops->fo_ioctl;
93
94	*retval = 0;
95
96	switch (cmd) {
97	case SVR4_SIOCGIFNUM:
98		{
99			struct ifnet *ifp;
100			struct ifaddr *ifa;
101			int ifnum = 0;
102
103			/*
104			 * This does not return the number of physical
105			 * interfaces (if_index), but the number of interfaces
106			 * + addresses like ifconf() does, because this number
107			 * is used by code that will call SVR4_SIOCGIFCONF to
108			 * find the space needed for SVR4_SIOCGIFCONF. So we
109			 * count the number of ifreq entries that the next
110			 * SVR4_SIOCGIFCONF will return. Maybe a more correct
111			 * fix is to make SVR4_SIOCGIFCONF return only one
112			 * entry per physical interface?
113			 */
114
115			for (ifp = ifnet.tqh_first;
116			     ifp != 0; ifp = ifp->if_link.tqe_next)
117				if ((ifa = ifp->if_addrhead.tqh_first) == NULL)
118					ifnum++;
119				else
120					for (;ifa != NULL;
121					    ifa = ifa->ifa_link.tqe_next)
122						ifnum++;
123
124
125			DPRINTF(("SIOCGIFNUM %d\n", ifnum));
126			return copyout(&ifnum, data, sizeof(ifnum));
127		}
128
129	case SVR4_SIOCGIFFLAGS:
130		{
131			struct ifreq br;
132			struct svr4_ifreq sr;
133
134			if ((error = copyin(data, &sr, sizeof(sr))) != 0)
135				return error;
136
137			(void) strncpy(br.ifr_name, sr.svr4_ifr_name,
138			    sizeof(br.ifr_name));
139			if ((error = (*ctl)(fp, SIOCGIFFLAGS,
140					    (caddr_t) &br, p)) != 0) {
141				DPRINTF(("SIOCGIFFLAGS (%s) %s: error %d\n",
142					 br.ifr_name, sr.svr4_ifr_name, error));
143				return error;
144			}
145
146			sr.svr4_ifr_flags = bsd_to_svr4_flags(br.ifr_flags);
147			DPRINTF(("SIOCGIFFLAGS %s = %x\n",
148				sr.svr4_ifr_name, sr.svr4_ifr_flags));
149			return copyout(&sr, data, sizeof(sr));
150		}
151
152	case SVR4_SIOCGIFCONF:
153		{
154			struct svr4_ifconf sc;
155
156			if ((error = copyin(data, &sc, sizeof(sc))) != 0)
157				return error;
158
159			DPRINTF(("ifreq %d svr4_ifreq %d ifc_len %d\n",
160				sizeof(struct ifreq), sizeof(struct svr4_ifreq),
161				sc.svr4_ifc_len));
162
163			if ((error = (*ctl)(fp, OSIOCGIFCONF,
164					    (caddr_t) &sc, p)) != 0)
165				return error;
166
167			DPRINTF(("SIOCGIFCONF\n"));
168			return 0;
169		}
170
171
172	default:
173		DPRINTF(("Unknown svr4 sockio %lx\n", cmd));
174		return 0;	/* ENOSYS really */
175	}
176}
177