xref: /illumos-gate/usr/src/boot/libsa/netif.c (revision ddb365bfc9e868ad24ccdcb0dc91af18b10df082)
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 
36 #include <sys/param.h>
37 #include <sys/types.h>
38 #include <sys/cdefs.h>
39 #include <sys/mount.h>
40 #include <string.h>
41 
42 #include <netinet/in.h>
43 #include <netinet/in_systm.h>
44 
45 #include "stand.h"
46 #include "net.h"
47 #include "netif.h"
48 
49 typedef TAILQ_HEAD(socket_list, iodesc) socket_list_t;
50 
51 /*
52  * Open socket list. The current implementation and assumption is,
53  * we only remove entries from tail and we only add new entries to tail.
54  * This decision is to keep iodesc id management simple - we get list
55  * entries ordered by continiously growing io_id field.
56  * If we do have multiple sockets open and we do close socket not from tail,
57  * this entry will be marked unused. netif_open() will reuse unused entry, or
58  * netif_close() will free all unused tail entries.
59  */
60 static socket_list_t sockets = TAILQ_HEAD_INITIALIZER(sockets);
61 
62 #ifdef NETIF_DEBUG
63 int netif_debug = 0;
64 #endif
65 
66 /*
67  * netif_init:
68  *
69  * initialize the generic network interface layer
70  */
71 
72 void
73 netif_init(void)
74 {
75 	struct netif_driver *drv;
76 	int d, i;
77 
78 #ifdef NETIF_DEBUG
79 	if (netif_debug)
80 		printf("netif_init: called\n");
81 #endif
82 	for (d = 0; netif_drivers[d]; d++) {
83 		drv = netif_drivers[d];
84 		for (i = 0; i < drv->netif_nifs; i++)
85 			drv->netif_ifs[i].dif_used = 0;
86 	}
87 }
88 
89 int
90 netif_match(struct netif *nif, void *machdep_hint)
91 {
92 	struct netif_driver *drv = nif->nif_driver;
93 
94 #if NETIF_DEBUG
95 	if (netif_debug)
96 		printf("%s%d: netif_match (%d)\n", drv->netif_bname,
97 		    nif->nif_unit, nif->nif_sel);
98 #endif
99 	return (drv->netif_match(nif, machdep_hint));
100 }
101 
102 struct netif *
103 netif_select(void *machdep_hint)
104 {
105 	int d, u, s;
106 	struct netif_driver *drv;
107 	struct netif cur_if;
108 	static struct netif best_if;
109 	int best_val;
110 	int val;
111 
112 	best_val = 0;
113 	best_if.nif_driver = NULL;
114 
115 	for (d = 0; netif_drivers[d] != NULL; d++) {
116 		cur_if.nif_driver = netif_drivers[d];
117 		drv = cur_if.nif_driver;
118 
119 		for (u = 0; u < drv->netif_nifs; u++) {
120 			cur_if.nif_unit = u;
121 #ifdef NETIF_DEBUG
122 			if (netif_debug)
123 				printf("\t%s%d:", drv->netif_bname,
124 				    cur_if.nif_unit);
125 #endif
126 
127 			for (s = 0; s < drv->netif_ifs[u].dif_nsel; s++) {
128 				cur_if.nif_sel = s;
129 
130 				if (drv->netif_ifs[u].dif_used & (1 << s)) {
131 #ifdef NETIF_DEBUG
132 					if (netif_debug)
133 						printf(" [%d used]", s);
134 #endif
135 					continue;
136 				}
137 
138 				val = netif_match(&cur_if, machdep_hint);
139 #ifdef NETIF_DEBUG
140 				if (netif_debug)
141 					printf(" [%d -> %d]", s, val);
142 #endif
143 				if (val > best_val) {
144 					best_val = val;
145 					best_if = cur_if;
146 				}
147 			}
148 #ifdef NETIF_DEBUG
149 			if (netif_debug)
150 				printf("\n");
151 #endif
152 		}
153 	}
154 
155 	if (best_if.nif_driver == NULL)
156 		return (NULL);
157 
158 	best_if.nif_driver->
159 	    netif_ifs[best_if.nif_unit].dif_used |= (1 << best_if.nif_sel);
160 
161 #ifdef NETIF_DEBUG
162 	if (netif_debug)
163 		printf("netif_select: %s%d(%d) wins\n",
164 		    best_if.nif_driver->netif_bname,
165 		    best_if.nif_unit, best_if.nif_sel);
166 #endif
167 	return (&best_if);
168 }
169 
170 int
171 netif_probe(struct netif *nif, void *machdep_hint)
172 {
173 	struct netif_driver *drv = nif->nif_driver;
174 
175 #ifdef NETIF_DEBUG
176 	if (netif_debug)
177 		printf("%s%d: netif_probe\n", drv->netif_bname, nif->nif_unit);
178 #endif
179 	return (drv->netif_probe(nif, machdep_hint));
180 }
181 
182 void
183 netif_attach(struct netif *nif, struct iodesc *desc, void *machdep_hint)
184 {
185 	struct netif_driver *drv = nif->nif_driver;
186 
187 #ifdef NETIF_DEBUG
188 	if (netif_debug)
189 		printf("%s%d: netif_attach\n", drv->netif_bname, nif->nif_unit);
190 #endif
191 	desc->io_netif = nif;
192 #ifdef PARANOID
193 	if (drv->netif_init == NULL)
194 		panic("%s%d: no netif_init support", drv->netif_bname,
195 		    nif->nif_unit);
196 #endif
197 	drv->netif_init(desc, machdep_hint);
198 	bzero(drv->netif_ifs[nif->nif_unit].dif_stats,
199 	    sizeof (struct netif_stats));
200 }
201 
202 void
203 netif_detach(struct netif *nif)
204 {
205 	struct netif_driver *drv = nif->nif_driver;
206 
207 #ifdef NETIF_DEBUG
208 	if (netif_debug)
209 		printf("%s%d: netif_detach\n", drv->netif_bname, nif->nif_unit);
210 #endif
211 #ifdef PARANOID
212 	if (drv->netif_end == NULL)
213 		panic("%s%d: no netif_end support", drv->netif_bname,
214 		    nif->nif_unit);
215 #endif
216 	drv->netif_end(nif);
217 }
218 
219 ssize_t
220 netif_get(struct iodesc *desc, void **pkt, time_t timo)
221 {
222 #ifdef NETIF_DEBUG
223 	struct netif *nif = desc->io_netif;
224 #endif
225 	struct netif_driver *drv = desc->io_netif->nif_driver;
226 	ssize_t rv;
227 
228 #ifdef NETIF_DEBUG
229 	if (netif_debug)
230 		printf("%s%d: netif_get\n", drv->netif_bname, nif->nif_unit);
231 #endif
232 #ifdef PARANOID
233 	if (drv->netif_get == NULL)
234 		panic("%s%d: no netif_get support", drv->netif_bname,
235 		    nif->nif_unit);
236 #endif
237 	rv = drv->netif_get(desc, pkt, timo);
238 #ifdef NETIF_DEBUG
239 	if (netif_debug)
240 		printf("%s%d: netif_get returning %d\n", drv->netif_bname,
241 		    nif->nif_unit, (int)rv);
242 #endif
243 	return (rv);
244 }
245 
246 ssize_t
247 netif_put(struct iodesc *desc, void *pkt, size_t len)
248 {
249 #ifdef NETIF_DEBUG
250 	struct netif *nif = desc->io_netif;
251 #endif
252 	struct netif_driver *drv = desc->io_netif->nif_driver;
253 	ssize_t rv;
254 
255 #ifdef NETIF_DEBUG
256 	if (netif_debug)
257 		printf("%s%d: netif_put\n", drv->netif_bname, nif->nif_unit);
258 #endif
259 #ifdef PARANOID
260 	if (drv->netif_put == NULL)
261 		panic("%s%d: no netif_put support", drv->netif_bname,
262 		    nif->nif_unit);
263 #endif
264 	rv = drv->netif_put(desc, pkt, len);
265 #ifdef NETIF_DEBUG
266 	if (netif_debug)
267 		printf("%s%d: netif_put returning %d\n", drv->netif_bname,
268 		    nif->nif_unit, (int)rv);
269 #endif
270 	return (rv);
271 }
272 
273 /*
274  * socktodesc_impl:
275  *
276  * Walk socket list and return pointer to iodesc structure.
277  * if id is < 0, return first unused iodesc.
278  */
279 static struct iodesc *
280 socktodesc_impl(int socket)
281 {
282 	struct iodesc *s;
283 
284 	TAILQ_FOREACH(s, &sockets, io_link) {
285 		/* search by socket id */
286 		if (socket >= 0) {
287 			if (s->io_id == socket)
288 				break;
289 			continue;
290 		}
291 		/* search for first unused entry */
292 		if (s->io_netif == NULL)
293 			break;
294 	}
295 	return (s);
296 }
297 
298 struct iodesc *
299 socktodesc(int sock)
300 {
301 	struct iodesc *desc;
302 
303 	if (sock < 0)
304 		desc = NULL;
305 	else
306 		desc = socktodesc_impl(sock);
307 
308 	if (desc == NULL)
309 		errno = EBADF;
310 
311 	return (desc);
312 }
313 
314 int
315 netif_open(void *machdep_hint)
316 {
317 	struct iodesc *s;
318 	struct netif *nif;
319 
320 	/* find a free socket */
321 	s = socktodesc_impl(-1);
322 	if (s == NULL) {
323 		struct iodesc *last;
324 
325 		s = calloc(1, sizeof (*s));
326 		if (s == NULL)
327 			return (-1);
328 		last = TAILQ_LAST(&sockets, socket_list);
329 		if (last != NULL)
330 			s->io_id = last->io_id + 1;
331 		TAILQ_INSERT_TAIL(&sockets, s, io_link);
332 	}
333 
334 	netif_init();
335 	nif = netif_select(machdep_hint);
336 	if (!nif)
337 		panic("netboot: no interfaces left untried");
338 	if (netif_probe(nif, machdep_hint)) {
339 		printf("netboot: couldn't probe %s%d\n",
340 		    nif->nif_driver->netif_bname, nif->nif_unit);
341 		errno = EINVAL;
342 		return (-1);
343 	}
344 	netif_attach(nif, s, machdep_hint);
345 
346 	return (s->io_id);
347 }
348 
349 int
350 netif_close(int sock)
351 {
352 	struct iodesc *s, *last;
353 	int err;
354 
355 	err = 0;
356 	s = socktodesc_impl(sock);
357 	if (s == NULL || sock < 0) {
358 		err = EBADF;
359 		return (-1);
360 	}
361 	netif_detach(s->io_netif);
362 
363 	bzero(&s->destip, sizeof (s->destip));
364 	bzero(&s->myip, sizeof (s->myip));
365 	s->destport = 0;
366 	s->myport = 0;
367 	s->xid = 0;
368 	bzero(s->myea, sizeof (s->myea));
369 	s->io_netif = NULL;
370 
371 	/* free unused entries from tail. */
372 	TAILQ_FOREACH_REVERSE_SAFE(last, &sockets, socket_list, io_link, s) {
373 		if (last->io_netif != NULL)
374 			break;
375 		TAILQ_REMOVE(&sockets, last, io_link);
376 		free(last);
377 	}
378 
379 	if (err) {
380 		errno = err;
381 		return (-1);
382 	}
383 
384 	return (0);
385 }
386