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 <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
netif_init(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
netif_match(struct netif * nif,void * machdep_hint)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 *
netif_select(void * machdep_hint)103 netif_select(void *machdep_hint)
104 {
105 int d, u, unit_done, 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 unit_done = 0;
122
123 #ifdef NETIF_DEBUG
124 if (netif_debug)
125 printf("\t%s%d:", drv->netif_bname,
126 cur_if.nif_unit);
127 #endif
128
129 for (s = 0; s < drv->netif_ifs[u].dif_nsel; s++) {
130 cur_if.nif_sel = s;
131
132 if (drv->netif_ifs[u].dif_used & (1 << s)) {
133 #ifdef NETIF_DEBUG
134 if (netif_debug)
135 printf(" [%d used]", s);
136 #endif
137 continue;
138 }
139
140 val = netif_match(&cur_if, machdep_hint);
141 #ifdef NETIF_DEBUG
142 if (netif_debug)
143 printf(" [%d -> %d]", s, val);
144 #endif
145 if (val > best_val) {
146 best_val = val;
147 best_if = cur_if;
148 }
149 }
150 #ifdef NETIF_DEBUG
151 if (netif_debug)
152 printf("\n");
153 #endif
154 }
155 }
156
157 if (best_if.nif_driver == NULL)
158 return NULL;
159
160 best_if.nif_driver->
161 netif_ifs[best_if.nif_unit].dif_used |= (1 << best_if.nif_sel);
162
163 #ifdef NETIF_DEBUG
164 if (netif_debug)
165 printf("netif_select: %s%d(%d) wins\n",
166 best_if.nif_driver->netif_bname,
167 best_if.nif_unit, best_if.nif_sel);
168 #endif
169 return &best_if;
170 }
171
172 int
netif_probe(struct netif * nif,void * machdep_hint)173 netif_probe(struct netif *nif, void *machdep_hint)
174 {
175 struct netif_driver *drv = nif->nif_driver;
176
177 #ifdef NETIF_DEBUG
178 if (netif_debug)
179 printf("%s%d: netif_probe\n", drv->netif_bname, nif->nif_unit);
180 #endif
181 return drv->netif_probe(nif, machdep_hint);
182 }
183
184 void
netif_attach(struct netif * nif,struct iodesc * desc,void * machdep_hint)185 netif_attach(struct netif *nif, struct iodesc *desc, void *machdep_hint)
186 {
187 struct netif_driver *drv = nif->nif_driver;
188
189 #ifdef NETIF_DEBUG
190 if (netif_debug)
191 printf("%s%d: netif_attach\n", drv->netif_bname, nif->nif_unit);
192 #endif
193 desc->io_netif = nif;
194 #ifdef PARANOID
195 if (drv->netif_init == NULL)
196 panic("%s%d: no netif_init support", drv->netif_bname,
197 nif->nif_unit);
198 #endif
199 drv->netif_init(desc, machdep_hint);
200 bzero(drv->netif_ifs[nif->nif_unit].dif_stats,
201 sizeof(struct netif_stats));
202 }
203
204 void
netif_detach(struct netif * nif)205 netif_detach(struct netif *nif)
206 {
207 struct netif_driver *drv = nif->nif_driver;
208
209 #ifdef NETIF_DEBUG
210 if (netif_debug)
211 printf("%s%d: netif_detach\n", drv->netif_bname, nif->nif_unit);
212 #endif
213 #ifdef PARANOID
214 if (drv->netif_end == NULL)
215 panic("%s%d: no netif_end support", drv->netif_bname,
216 nif->nif_unit);
217 #endif
218 drv->netif_end(nif);
219 }
220
221 ssize_t
netif_get(struct iodesc * desc,void ** pkt,time_t timo)222 netif_get(struct iodesc *desc, void **pkt, time_t timo)
223 {
224 #ifdef NETIF_DEBUG
225 struct netif *nif = desc->io_netif;
226 #endif
227 struct netif_driver *drv = desc->io_netif->nif_driver;
228 ssize_t rv;
229
230 #ifdef NETIF_DEBUG
231 if (netif_debug)
232 printf("%s%d: netif_get\n", drv->netif_bname, nif->nif_unit);
233 #endif
234 #ifdef PARANOID
235 if (drv->netif_get == NULL)
236 panic("%s%d: no netif_get support", drv->netif_bname,
237 nif->nif_unit);
238 #endif
239 rv = drv->netif_get(desc, pkt, timo);
240 #ifdef NETIF_DEBUG
241 if (netif_debug)
242 printf("%s%d: netif_get returning %d\n", drv->netif_bname,
243 nif->nif_unit, (int)rv);
244 #endif
245 return (rv);
246 }
247
248 ssize_t
netif_put(struct iodesc * desc,void * pkt,size_t len)249 netif_put(struct iodesc *desc, void *pkt, size_t len)
250 {
251 #ifdef NETIF_DEBUG
252 struct netif *nif = desc->io_netif;
253 #endif
254 struct netif_driver *drv = desc->io_netif->nif_driver;
255 ssize_t rv;
256
257 #ifdef NETIF_DEBUG
258 if (netif_debug)
259 printf("%s%d: netif_put\n", drv->netif_bname, nif->nif_unit);
260 #endif
261 #ifdef PARANOID
262 if (drv->netif_put == NULL)
263 panic("%s%d: no netif_put support", drv->netif_bname,
264 nif->nif_unit);
265 #endif
266 rv = drv->netif_put(desc, pkt, len);
267 #ifdef NETIF_DEBUG
268 if (netif_debug)
269 printf("%s%d: netif_put returning %d\n", drv->netif_bname,
270 nif->nif_unit, (int)rv);
271 #endif
272 return (rv);
273 }
274
275 /*
276 * socktodesc_impl:
277 *
278 * Walk socket list and return pointer to iodesc structure.
279 * if id is < 0, return first unused iodesc.
280 */
281 static struct iodesc *
socktodesc_impl(int socket)282 socktodesc_impl(int socket)
283 {
284 struct iodesc *s;
285
286 TAILQ_FOREACH(s, &sockets, io_link) {
287 /* search by socket id */
288 if (socket >= 0) {
289 if (s->io_id == socket)
290 break;
291 continue;
292 }
293 /* search for first unused entry */
294 if (s->io_netif == NULL)
295 break;
296 }
297 return (s);
298 }
299
300 struct iodesc *
socktodesc(int sock)301 socktodesc(int sock)
302 {
303 struct iodesc *desc;
304
305 if (sock < 0)
306 desc = NULL;
307 else
308 desc = socktodesc_impl(sock);
309
310 if (desc == NULL)
311 errno = EBADF;
312
313 return (desc);
314 }
315
316 int
netif_open(void * machdep_hint)317 netif_open(void *machdep_hint)
318 {
319 struct iodesc *s;
320 struct netif *nif;
321
322 /* find a free socket */
323 s = socktodesc_impl(-1);
324 if (s == NULL) {
325 struct iodesc *last;
326
327 s = calloc(1, sizeof (*s));
328 if (s == NULL)
329 return (-1);
330
331 last = TAILQ_LAST(&sockets, socket_list);
332 if (last != NULL)
333 s->io_id = last->io_id + 1;
334 TAILQ_INSERT_TAIL(&sockets, s, io_link);
335 }
336
337 netif_init();
338 nif = netif_select(machdep_hint);
339 if (!nif)
340 panic("netboot: no interfaces left untried");
341 if (netif_probe(nif, machdep_hint)) {
342 printf("netboot: couldn't probe %s%d\n",
343 nif->nif_driver->netif_bname, nif->nif_unit);
344 errno = EINVAL;
345 return (-1);
346 }
347 netif_attach(nif, s, machdep_hint);
348
349 return (s->io_id);
350 }
351
352 int
netif_close(int sock)353 netif_close(int sock)
354 {
355 struct iodesc *s, *last;
356 int err;
357
358 err = 0;
359 s = socktodesc_impl(sock);
360 if (s == NULL || sock < 0) {
361 err = EBADF;
362 return (-1);
363 }
364 netif_detach(s->io_netif);
365 bzero(&s->destip, sizeof (s->destip));
366 bzero(&s->myip, sizeof (s->myip));
367 s->destport = 0;
368 s->myport = 0;
369 s->xid = 0;
370 bzero(s->myea, sizeof (s->myea));
371 s->io_netif = NULL;
372
373 /* free unused entries from tail. */
374 TAILQ_FOREACH_REVERSE_SAFE(last, &sockets, socket_list, io_link, s) {
375 if (last->io_netif != NULL)
376 break;
377 TAILQ_REMOVE(&sockets, last, io_link);
378 free(last);
379 }
380
381 if (err) {
382 errno = err;
383 return (-1);
384 }
385
386 return (0);
387 }
388