c78caa18efc4d3300e970ff08674dcade9028d30
[dragonfly.git] / lib / libstand / netif.c
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/param.h>
35 #include <sys/types.h>
36 #include <sys/mount.h>
37 #include <string.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; netif_drivers[d]; 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 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         for (d = 0; netif_drivers[d] != NULL; d++) {
101                 cur_if.nif_driver = netif_drivers[d];
102                 drv = cur_if.nif_driver;
103
104                 for (u = 0; u < drv->netif_nifs; u++) {
105                         cur_if.nif_unit = u;
106                         unit_done = 0;
107                 
108 #ifdef NETIF_DEBUG
109                         if (netif_debug)
110                                 printf("\t%s%d:", drv->netif_bname,
111                                     cur_if.nif_unit);
112 #endif
113
114                         for (s = 0; s < drv->netif_ifs[u].dif_nsel; s++) {
115                                 cur_if.nif_sel = s;
116
117                                 if (drv->netif_ifs[u].dif_used & (1 << s)) {
118 #ifdef NETIF_DEBUG
119                                         if (netif_debug)
120                                                 printf(" [%d used]", s);
121 #endif
122                                         continue;
123                                 }
124
125                                 val = netif_match(&cur_if, machdep_hint);
126 #ifdef NETIF_DEBUG
127                                 if (netif_debug)
128                                         printf(" [%d -> %d]", s, val);
129 #endif
130                                 if (val > best_val) {
131                                         best_val = val;
132                                         best_if = cur_if;
133                                 }
134                         }
135 #ifdef NETIF_DEBUG
136                         if (netif_debug)
137                                 printf("\n");
138 #endif
139                 }
140         }
141
142         if (best_if.nif_driver == NULL)
143                 return NULL;
144
145         best_if.nif_driver->
146             netif_ifs[best_if.nif_unit].dif_used |= (1 << best_if.nif_sel);
147
148 #ifdef NETIF_DEBUG
149         if (netif_debug)
150                 printf("netif_select: %s%d(%d) wins\n",
151                         best_if.nif_driver->netif_bname,
152                         best_if.nif_unit, best_if.nif_sel);
153 #endif
154         return &best_if;
155 }
156
157 int
158 netif_probe(struct netif *nif, void *machdep_hint)
159 {
160         struct netif_driver *drv = nif->nif_driver;
161
162 #ifdef NETIF_DEBUG
163         if (netif_debug)
164                 printf("%s%d: netif_probe\n", drv->netif_bname, nif->nif_unit);
165 #endif
166         return drv->netif_probe(nif, machdep_hint);
167 }
168
169 void
170 netif_attach(struct netif *nif, struct iodesc *desc, void *machdep_hint)
171 {
172         struct netif_driver *drv = nif->nif_driver;
173
174 #ifdef NETIF_DEBUG
175         if (netif_debug)
176                 printf("%s%d: netif_attach\n", drv->netif_bname, nif->nif_unit);
177 #endif
178         desc->io_netif = nif; 
179 #ifdef PARANOID
180         if (drv->netif_init == NULL)
181                 panic("%s%d: no netif_init support\n", drv->netif_bname,
182                     nif->nif_unit);
183 #endif
184         drv->netif_init(desc, machdep_hint);
185         bzero(drv->netif_ifs[nif->nif_unit].dif_stats, 
186             sizeof(struct netif_stats));
187 }
188
189 void
190 netif_detach(struct netif *nif)
191 {
192         struct netif_driver *drv = nif->nif_driver;
193
194 #ifdef NETIF_DEBUG
195         if (netif_debug)
196                 printf("%s%d: netif_detach\n", drv->netif_bname, nif->nif_unit);
197 #endif
198 #ifdef PARANOID
199         if (drv->netif_end == NULL)
200                 panic("%s%d: no netif_end support\n", drv->netif_bname,
201                     nif->nif_unit);
202 #endif
203         drv->netif_end(nif);
204 }
205
206 ssize_t
207 netif_get(struct iodesc *desc, void *pkt, size_t len, time_t timo)
208 {
209 #ifdef NETIF_DEBUG
210         struct netif *nif = desc->io_netif;
211 #endif
212         struct netif_driver *drv = desc->io_netif->nif_driver;
213         ssize_t rv;
214
215 #ifdef NETIF_DEBUG
216         if (netif_debug)
217                 printf("%s%d: netif_get\n", drv->netif_bname, nif->nif_unit);
218 #endif
219 #ifdef PARANOID
220         if (drv->netif_get == NULL)
221                 panic("%s%d: no netif_get support\n", drv->netif_bname,
222                     nif->nif_unit);
223 #endif
224         rv = drv->netif_get(desc, pkt, len, timo);
225 #ifdef NETIF_DEBUG
226         if (netif_debug)
227                 printf("%s%d: netif_get returning %d\n", drv->netif_bname,
228                     nif->nif_unit, (int)rv);
229 #endif
230         return rv;
231 }
232
233 ssize_t
234 netif_put(struct iodesc *desc, void *pkt, size_t len)
235 {
236 #ifdef NETIF_DEBUG
237         struct netif *nif = desc->io_netif;
238 #endif
239         struct netif_driver *drv = desc->io_netif->nif_driver;
240         ssize_t rv;
241
242 #ifdef NETIF_DEBUG
243         if (netif_debug)
244                 printf("%s%d: netif_put\n", drv->netif_bname, nif->nif_unit);
245 #endif
246 #ifdef PARANOID
247         if (drv->netif_put == NULL)
248                 panic("%s%d: no netif_put support\n", drv->netif_bname,
249                     nif->nif_unit);
250 #endif
251         rv = drv->netif_put(desc, pkt, len);
252 #ifdef NETIF_DEBUG
253         if (netif_debug)
254                 printf("%s%d: netif_put returning %d\n", drv->netif_bname,
255                     nif->nif_unit, (int)rv);
256 #endif
257         return rv;
258 }
259
260 struct iodesc *
261 socktodesc(int sock)
262 {
263         if (sock >= SOPEN_MAX) {
264                 errno = EBADF;
265                 return (NULL);
266         }
267         return (&sockets[sock]);
268 }
269
270 int
271 netif_open(void *machdep_hint)
272 {
273         int fd;
274         struct iodesc *s;
275         struct netif *nif;
276         
277         /* find a free socket */
278         for (fd = 0, s = sockets; fd < SOPEN_MAX; fd++, s++)
279                 if (s->io_netif == NULL)
280                         goto fnd;
281         errno = EMFILE;
282         return (-1);
283
284 fnd:
285         bzero(s, sizeof(*s));
286         netif_init();
287         nif = netif_select(machdep_hint);
288         if (!nif) 
289                 panic("netboot: no interfaces left untried");
290         if (netif_probe(nif, machdep_hint)) {
291                 printf("netboot: couldn't probe %s%d\n",
292                     nif->nif_driver->netif_bname, nif->nif_unit);
293                 errno = EINVAL;
294                 return(-1);
295         }
296         netif_attach(nif, s, machdep_hint);
297
298         return(fd);
299 }
300
301 int
302 netif_close(int sock)
303 {
304         if (sock >= SOPEN_MAX) {
305                 errno = EBADF;
306                 return(-1);
307         }
308         netif_detach(sockets[sock].io_netif);
309         sockets[sock].io_netif = NULL;
310
311         return(0);
312 }