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