xref: /openbsd-src/sys/lib/libsa/netif.c (revision 91f110e064cd7c194e59e019b83bb7496c1c84d4)
1 /*	$OpenBSD: netif.c,v 1.9 2012/12/05 23:20:23 deraadt Exp $	*/
2 /*	$NetBSD: netif.c,v 1.7 1996/10/13 02:29:03 christos Exp $	*/
3 
4 /*
5  * Copyright (c) 1993 Adam Glass
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *	This product includes software developed by Adam Glass.
19  * 4. The name of the Author may not be used to endorse or promote products
20  *    derived from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY Adam Glass ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  */
34 
35 #include <sys/param.h>
36 #include <sys/types.h>
37 #include <sys/mount.h>
38 
39 #include <netinet/in.h>
40 #include <netinet/in_systm.h>
41 
42 #include "stand.h"
43 #include "net.h"
44 #include "netif.h"
45 
46 struct iodesc sockets[SOPEN_MAX];
47 #ifdef NETIF_DEBUG
48 int netif_debug = 0;
49 #endif
50 
51 /*
52  * netif_init:
53  *
54  * initialize the generic network interface layer
55  */
56 
57 void
58 netif_init(void)
59 {
60 	struct netif_driver *drv;
61 	int d, i;
62 
63 #ifdef NETIF_DEBUG
64 	if (netif_debug)
65 		printf("netif_init: called\n");
66 #endif
67 	for (d = 0; d < n_netif_drivers; d++) {
68 		drv = netif_drivers[d];
69 		for (i = 0; i < drv->netif_nifs; i++)
70 			drv->netif_ifs[i].dif_used = 0;
71 	}
72 }
73 
74 static int
75 netif_match(struct netif *nif, void *machdep_hint)
76 {
77 	struct netif_driver *drv = nif->nif_driver;
78 
79 #if 0
80 	if (netif_debug)
81 		printf("%s%d: netif_match (%d)\n", drv->netif_bname,
82 		    nif->nif_unit, nif->nif_sel);
83 #endif
84 	return drv->netif_match(nif, machdep_hint);
85 }
86 
87 struct netif *
88 netif_select(void *machdep_hint)
89 {
90 	int d, u, unit_done, s;
91 	struct netif_driver *drv;
92 	struct netif cur_if;
93 	static struct netif best_if;
94 	int best_val;
95 	int val;
96 
97 	best_val = 0;
98 	best_if.nif_driver = NULL;
99 
100 #ifdef NETIF_DEBUG
101 	if (netif_debug)
102 		printf("netif_select: %d interfaces\n", n_netif_drivers);
103 #endif
104 
105 	for (d = 0; d < n_netif_drivers; d++) {
106 		cur_if.nif_driver = netif_drivers[d];
107 		drv = cur_if.nif_driver;
108 
109 		for (u = 0; u < drv->netif_nifs; u++) {
110 			cur_if.nif_unit = u;
111 			unit_done = 0;
112 
113 #ifdef NETIF_DEBUG
114 			if (netif_debug)
115 				printf("\t%s%d:", drv->netif_bname,
116 				    cur_if.nif_unit);
117 #endif
118 
119 			for (s = 0; s < drv->netif_ifs[u].dif_nsel; s++) {
120 				cur_if.nif_sel = s;
121 
122 				if (drv->netif_ifs[u].dif_used & (1 << s)) {
123 #ifdef NETIF_DEBUG
124 					if (netif_debug)
125 						printf(" [%d used]", s);
126 #endif
127 					continue;
128 				}
129 
130 				val = netif_match(&cur_if, machdep_hint);
131 #ifdef NETIF_DEBUG
132 				if (netif_debug)
133 					printf(" [%d -> %d]", s, val);
134 #endif
135 				if (val > best_val) {
136 					best_val = val;
137 					best_if = cur_if;
138 				}
139 			}
140 #ifdef NETIF_DEBUG
141 			if (netif_debug)
142 				printf("\n");
143 #endif
144 		}
145 	}
146 
147 	if (best_if.nif_driver == NULL)
148 		return NULL;
149 
150 	best_if.nif_driver->netif_ifs[best_if.nif_unit].dif_used |=
151 	    (1 << best_if.nif_sel);
152 
153 #ifdef NETIF_DEBUG
154 	if (netif_debug)
155 		printf("netif_select: %s%d(%d) wins\n",
156 		    best_if.nif_driver->netif_bname,
157 		    best_if.nif_unit, best_if.nif_sel);
158 #endif
159 	return &best_if;
160 }
161 
162 int
163 netif_probe(struct netif *nif, void *machdep_hint)
164 {
165 	struct netif_driver *drv = nif->nif_driver;
166 
167 #ifdef NETIF_DEBUG
168 	if (netif_debug)
169 		printf("%s%d: netif_probe\n", drv->netif_bname, nif->nif_unit);
170 #endif
171 	return drv->netif_probe(nif, machdep_hint);
172 }
173 
174 void
175 netif_attach(struct netif *nif, struct iodesc *desc, void *machdep_hint)
176 {
177 	struct netif_driver *drv = nif->nif_driver;
178 
179 #ifdef NETIF_DEBUG
180 	if (netif_debug)
181 		printf("%s%d: netif_attach\n", drv->netif_bname, nif->nif_unit);
182 #endif
183 	desc->io_netif = nif;
184 #ifdef PARANOID
185 	if (drv->netif_init == NULL)
186 		panic("%s%d: no netif_init support", drv->netif_bname,
187 		    nif->nif_unit);
188 #endif
189 	drv->netif_init(desc, machdep_hint);
190 	bzero(drv->netif_ifs[nif->nif_unit].dif_stats,
191 	    sizeof(struct netif_stats));
192 }
193 
194 void
195 netif_detach(struct netif *nif)
196 {
197 	struct netif_driver *drv = nif->nif_driver;
198 
199 #ifdef NETIF_DEBUG
200 	if (netif_debug)
201 		printf("%s%d: netif_detach\n", drv->netif_bname, nif->nif_unit);
202 #endif
203 #ifdef PARANOID
204 	if (drv->netif_end == NULL)
205 		panic("%s%d: no netif_end support", drv->netif_bname,
206 		    nif->nif_unit);
207 #endif
208 	drv->netif_end(nif);
209 }
210 
211 ssize_t
212 netif_get(struct iodesc *desc, void *pkt, size_t len, time_t timo)
213 {
214 #ifdef NETIF_DEBUG
215 	struct netif *nif = desc->io_netif;
216 #endif
217 	struct netif_driver *drv = desc->io_netif->nif_driver;
218 	ssize_t rv;
219 
220 #ifdef NETIF_DEBUG
221 	if (netif_debug)
222 		printf("%s%d: netif_get\n", drv->netif_bname, nif->nif_unit);
223 #endif
224 #ifdef PARANOID
225 	if (drv->netif_get == NULL)
226 		panic("%s%d: no netif_get support", drv->netif_bname,
227 		    nif->nif_unit);
228 #endif
229 	rv = drv->netif_get(desc, pkt, len, timo);
230 #ifdef NETIF_DEBUG
231 	if (netif_debug)
232 		printf("%s%d: netif_get returning %d\n", drv->netif_bname,
233 		    nif->nif_unit, rv);
234 #endif
235 	return rv;
236 }
237 
238 ssize_t
239 netif_put(struct iodesc *desc, void *pkt, size_t len)
240 {
241 #ifdef NETIF_DEBUG
242 	struct netif *nif = desc->io_netif;
243 #endif
244 	struct netif_driver *drv = desc->io_netif->nif_driver;
245 	ssize_t rv;
246 
247 #ifdef NETIF_DEBUG
248 	if (netif_debug)
249 		printf("%s%d: netif_put\n", drv->netif_bname, nif->nif_unit);
250 #endif
251 #ifdef PARANOID
252 	if (drv->netif_put == NULL)
253 		panic("%s%d: no netif_put support", drv->netif_bname,
254 		    nif->nif_unit);
255 #endif
256 	rv = drv->netif_put(desc, pkt, len);
257 #ifdef NETIF_DEBUG
258 	if (netif_debug)
259 		printf("%s%d: netif_put returning %d\n", drv->netif_bname,
260 		    nif->nif_unit, rv);
261 #endif
262 	return rv;
263 }
264 
265 struct iodesc *
266 socktodesc(sock)
267 	int sock;
268 {
269 	if (sock >= SOPEN_MAX) {
270 		errno = EBADF;
271 		return (NULL);
272 	}
273 	return (&sockets[sock]);
274 }
275 
276 int
277 netif_open(void *machdep_hint)
278 {
279 	int fd;
280 	struct iodesc *s;
281 	struct netif *nif;
282 
283 	/* find a free socket */
284 	for (fd = 0, s = sockets; fd < SOPEN_MAX; fd++, s++)
285 		if (s->io_netif == (struct netif *)0)
286 			goto fnd;
287 	errno = EMFILE;
288 	return (-1);
289 
290 fnd:
291 	bzero(s, sizeof(*s));
292 	netif_init();
293 	nif = netif_select(machdep_hint);
294 	if (!nif)
295 		panic("netboot: no interfaces left untried");
296 	if (netif_probe(nif, machdep_hint)) {
297 		printf("netboot: couldn't probe %s%d\n",
298 		    nif->nif_driver->netif_bname, nif->nif_unit);
299 		errno = EINVAL;
300 		return(-1);
301 	}
302 	netif_attach(nif, s, machdep_hint);
303 
304 	return(fd);
305 }
306 
307 int
308 netif_close(int sock)
309 {
310 	if (sock >= SOPEN_MAX) {
311 		errno = EBADF;
312 		return(-1);
313 	}
314 	netif_detach(sockets[sock].io_netif);
315 	sockets[sock].io_netif = (struct netif *)0;
316 
317 	return(0);
318 }
319