xref: /freebsd/stand/libsa/netif.c (revision 2faf504d1ab821fe2b9df9d2afb49bb35e1334f4)
1 /*	$NetBSD: netif.c,v 1.10 1997/09/06 13:57:14 drochner Exp $	*/
2 
3 /*
4  * Copyright (c) 1993 Adam Glass
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed by Adam Glass.
18  * 4. The name of the Author may not be used to endorse or promote products
19  *    derived from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY Adam Glass ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
36 
37 #include <sys/param.h>
38 #include <sys/types.h>
39 #include <sys/cdefs.h>
40 #include <sys/mount.h>
41 #include <string.h>
42 
43 #include <netinet/in.h>
44 #include <netinet/in_systm.h>
45 
46 #include "stand.h"
47 #include "net.h"
48 #include "netif.h"
49 
50 typedef TAILQ_HEAD(socket_list, iodesc) socket_list_t;
51 
52 /*
53  * Open socket list. The current implementation and assumption is,
54  * we only remove entries from tail and we only add new entries to tail.
55  * This decision is to keep iodesc id management simple - we get list
56  * entries ordered by continiously growing io_id field.
57  * If we do have multiple sockets open and we do close socket not from tail,
58  * this entry will be marked unused. netif_open() will reuse unused entry, or
59  * netif_close() will free all unused tail entries.
60  */
61 static socket_list_t sockets = TAILQ_HEAD_INITIALIZER(sockets);
62 
63 #ifdef NETIF_DEBUG
64 int netif_debug = 0;
65 #endif
66 
67 /*
68  * netif_init:
69  *
70  * initialize the generic network interface layer
71  */
72 
73 void
74 netif_init(void)
75 {
76 	struct netif_driver *drv;
77 	int d, i;
78 
79 #ifdef NETIF_DEBUG
80 	if (netif_debug)
81 		printf("netif_init: called\n");
82 #endif
83 	for (d = 0; netif_drivers[d]; d++) {
84 		drv = netif_drivers[d];
85 		for (i = 0; i < drv->netif_nifs; i++)
86 			drv->netif_ifs[i].dif_used = 0;
87 	}
88 }
89 
90 int
91 netif_match(struct netif *nif, void *machdep_hint)
92 {
93 	struct netif_driver *drv = nif->nif_driver;
94 
95 #if NETIF_DEBUG
96 	if (netif_debug)
97 		printf("%s%d: netif_match (%d)\n", drv->netif_bname,
98 		    nif->nif_unit, nif->nif_sel);
99 #endif
100 	return drv->netif_match(nif, machdep_hint);
101 }
102 
103 struct netif *
104 netif_select(void *machdep_hint)
105 {
106 	int d, u, unit_done, s;
107 	struct netif_driver *drv;
108 	struct netif cur_if;
109 	static struct netif best_if;
110 	int best_val;
111 	int val;
112 
113 	best_val = 0;
114 	best_if.nif_driver = NULL;
115 
116 	for (d = 0; netif_drivers[d] != NULL; d++) {
117 		cur_if.nif_driver = netif_drivers[d];
118 		drv = cur_if.nif_driver;
119 
120 		for (u = 0; u < drv->netif_nifs; u++) {
121 			cur_if.nif_unit = u;
122 			unit_done = 0;
123 
124 #ifdef NETIF_DEBUG
125 			if (netif_debug)
126 				printf("\t%s%d:", drv->netif_bname,
127 				    cur_if.nif_unit);
128 #endif
129 
130 			for (s = 0; s < drv->netif_ifs[u].dif_nsel; s++) {
131 				cur_if.nif_sel = s;
132 
133 				if (drv->netif_ifs[u].dif_used & (1 << s)) {
134 #ifdef NETIF_DEBUG
135 					if (netif_debug)
136 						printf(" [%d used]", s);
137 #endif
138 					continue;
139 				}
140 
141 				val = netif_match(&cur_if, machdep_hint);
142 #ifdef NETIF_DEBUG
143 				if (netif_debug)
144 					printf(" [%d -> %d]", s, val);
145 #endif
146 				if (val > best_val) {
147 					best_val = val;
148 					best_if = cur_if;
149 				}
150 			}
151 #ifdef NETIF_DEBUG
152 			if (netif_debug)
153 				printf("\n");
154 #endif
155 		}
156 	}
157 
158 	if (best_if.nif_driver == NULL)
159 		return NULL;
160 
161 	best_if.nif_driver->
162 	    netif_ifs[best_if.nif_unit].dif_used |= (1 << best_if.nif_sel);
163 
164 #ifdef NETIF_DEBUG
165 	if (netif_debug)
166 		printf("netif_select: %s%d(%d) wins\n",
167 			best_if.nif_driver->netif_bname,
168 			best_if.nif_unit, best_if.nif_sel);
169 #endif
170 	return &best_if;
171 }
172 
173 int
174 netif_probe(struct netif *nif, void *machdep_hint)
175 {
176 	struct netif_driver *drv = nif->nif_driver;
177 
178 #ifdef NETIF_DEBUG
179 	if (netif_debug)
180 		printf("%s%d: netif_probe\n", drv->netif_bname, nif->nif_unit);
181 #endif
182 	return drv->netif_probe(nif, machdep_hint);
183 }
184 
185 void
186 netif_attach(struct netif *nif, struct iodesc *desc, void *machdep_hint)
187 {
188 	struct netif_driver *drv = nif->nif_driver;
189 
190 #ifdef NETIF_DEBUG
191 	if (netif_debug)
192 		printf("%s%d: netif_attach\n", drv->netif_bname, nif->nif_unit);
193 #endif
194 	desc->io_netif = nif;
195 #ifdef PARANOID
196 	if (drv->netif_init == NULL)
197 		panic("%s%d: no netif_init support", drv->netif_bname,
198 		    nif->nif_unit);
199 #endif
200 	drv->netif_init(desc, machdep_hint);
201 	bzero(drv->netif_ifs[nif->nif_unit].dif_stats,
202 	    sizeof(struct netif_stats));
203 }
204 
205 void
206 netif_detach(struct netif *nif)
207 {
208 	struct netif_driver *drv = nif->nif_driver;
209 
210 #ifdef NETIF_DEBUG
211 	if (netif_debug)
212 		printf("%s%d: netif_detach\n", drv->netif_bname, nif->nif_unit);
213 #endif
214 #ifdef PARANOID
215 	if (drv->netif_end == NULL)
216 		panic("%s%d: no netif_end support", drv->netif_bname,
217 		    nif->nif_unit);
218 #endif
219 	drv->netif_end(nif);
220 }
221 
222 ssize_t
223 netif_get(struct iodesc *desc, void **pkt, time_t timo)
224 {
225 #ifdef NETIF_DEBUG
226 	struct netif *nif = desc->io_netif;
227 #endif
228 	struct netif_driver *drv = desc->io_netif->nif_driver;
229 	ssize_t rv;
230 
231 #ifdef NETIF_DEBUG
232 	if (netif_debug)
233 		printf("%s%d: netif_get\n", drv->netif_bname, nif->nif_unit);
234 #endif
235 #ifdef PARANOID
236 	if (drv->netif_get == NULL)
237 		panic("%s%d: no netif_get support", drv->netif_bname,
238 		    nif->nif_unit);
239 #endif
240 	rv = drv->netif_get(desc, pkt, timo);
241 #ifdef NETIF_DEBUG
242 	if (netif_debug)
243 		printf("%s%d: netif_get returning %d\n", drv->netif_bname,
244 		    nif->nif_unit, (int)rv);
245 #endif
246 	return (rv);
247 }
248 
249 ssize_t
250 netif_put(struct iodesc *desc, void *pkt, size_t len)
251 {
252 #ifdef NETIF_DEBUG
253 	struct netif *nif = desc->io_netif;
254 #endif
255 	struct netif_driver *drv = desc->io_netif->nif_driver;
256 	ssize_t rv;
257 
258 #ifdef NETIF_DEBUG
259 	if (netif_debug)
260 		printf("%s%d: netif_put\n", drv->netif_bname, nif->nif_unit);
261 #endif
262 #ifdef PARANOID
263 	if (drv->netif_put == NULL)
264 		panic("%s%d: no netif_put support", drv->netif_bname,
265 		    nif->nif_unit);
266 #endif
267 	rv = drv->netif_put(desc, pkt, len);
268 #ifdef NETIF_DEBUG
269 	if (netif_debug)
270 		printf("%s%d: netif_put returning %d\n", drv->netif_bname,
271 		    nif->nif_unit, (int)rv);
272 #endif
273 	return (rv);
274 }
275 
276 /*
277  * socktodesc_impl:
278  *
279  * Walk socket list and return pointer to iodesc structure.
280  * if id is < 0, return first unused iodesc.
281  */
282 static struct iodesc *
283 socktodesc_impl(int socket)
284 {
285 	struct iodesc *s;
286 
287 	TAILQ_FOREACH(s, &sockets, io_link) {
288 		/* search by socket id */
289 		if (socket >= 0) {
290 			if (s->io_id == socket)
291 				break;
292 			continue;
293 		}
294 		/* search for first unused entry */
295 		if (s->io_netif == NULL)
296 			break;
297 	}
298 	return (s);
299 }
300 
301 struct iodesc *
302 socktodesc(int sock)
303 {
304 	struct iodesc *desc;
305 
306 	if (sock < 0)
307 		desc = NULL;
308 	else
309 		desc = socktodesc_impl(sock);
310 
311 	if (desc == NULL)
312 		errno = EBADF;
313 
314 	return (desc);
315 }
316 
317 int
318 netif_open(void *machdep_hint)
319 {
320 	struct iodesc *s;
321 	struct netif *nif;
322 
323 	/* find a free socket */
324 	s = socktodesc_impl(-1);
325 	if (s == NULL) {
326 		struct iodesc *last;
327 
328 		s = calloc(1, sizeof (*s));
329 		if (s == NULL)
330 			return (-1);
331 
332 		last = TAILQ_LAST(&sockets, socket_list);
333 		if (last != NULL)
334 			s->io_id = last->io_id + 1;
335 		TAILQ_INSERT_TAIL(&sockets, s, io_link);
336 	}
337 
338 	netif_init();
339 	nif = netif_select(machdep_hint);
340 	if (!nif)
341 		panic("netboot: no interfaces left untried");
342 	if (netif_probe(nif, machdep_hint)) {
343 		printf("netboot: couldn't probe %s%d\n",
344 		    nif->nif_driver->netif_bname, nif->nif_unit);
345 		errno = EINVAL;
346 		return (-1);
347 	}
348 	netif_attach(nif, s, machdep_hint);
349 
350 	return (s->io_id);
351 }
352 
353 int
354 netif_close(int sock)
355 {
356 	struct iodesc *s, *last;
357 	int err;
358 
359 	err = 0;
360 	s = socktodesc_impl(sock);
361 	if (s == NULL || sock < 0) {
362 		err = EBADF;
363 		return (-1);
364 	}
365 	netif_detach(s->io_netif);
366 	bzero(&s->destip, sizeof (s->destip));
367 	bzero(&s->myip, sizeof (s->myip));
368 	s->destport = 0;
369 	s->myport = 0;
370 	s->xid = 0;
371 	bzero(s->myea, sizeof (s->myea));
372 	s->io_netif = NULL;
373 
374 	/* free unused entries from tail. */
375 	TAILQ_FOREACH_REVERSE_SAFE(last, &sockets, socket_list, io_link, s) {
376 		if (last->io_netif != NULL)
377 			break;
378 		TAILQ_REMOVE(&sockets, last, io_link);
379 		free(last);
380 	}
381 
382 	if (err) {
383 		errno = err;
384 		return (-1);
385 	}
386 
387 	return (0);
388 }
389