Missing files from OpenSSH import
[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/cdefs.h>
37 #include <sys/mount.h>
38 #include <string.h>
39
40 #include <netinet/in.h>
41 #include <netinet/in_systm.h>
42
43 #include "stand.h"
44 #include "net.h"
45 #include "netif.h"
46
47 struct iodesc sockets[SOPEN_MAX];
48 #ifdef NETIF_DEBUG
49 int netif_debug = 0;
50 #endif
51
52 /*
53  * netif_init:
54  *
55  * initialize the generic network interface layer
56  */
57
58 void
59 netif_init()
60 {
61         struct netif_driver *drv;
62         int d, i;
63     
64 #ifdef NETIF_DEBUG
65         if (netif_debug)
66                 printf("netif_init: called\n");
67 #endif
68         for (d = 0; netif_drivers[d]; d++) {
69                 drv = netif_drivers[d];
70                 for (i = 0; i < drv->netif_nifs; i++)
71                         drv->netif_ifs[i].dif_used = 0;
72         }
73 }
74
75 int
76 netif_match(nif, machdep_hint)
77         struct netif *nif;
78         void *machdep_hint;
79 {
80         struct netif_driver *drv = nif->nif_driver;
81
82 #if 0
83         if (netif_debug)
84                 printf("%s%d: netif_match (%d)\n", drv->netif_bname,
85                     nif->nif_unit, nif->nif_sel);
86 #endif
87         return drv->netif_match(nif, machdep_hint);
88 }
89
90 struct netif *
91 netif_select(machdep_hint)
92         void *machdep_hint;
93 {
94         int d, u, unit_done, s;
95         struct netif_driver *drv;
96         struct netif cur_if;
97         static struct netif best_if;
98         int best_val;
99         int val;
100
101         best_val = 0;
102         best_if.nif_driver = NULL;
103
104         for (d = 0; netif_drivers[d] != NULL; d++) {
105                 cur_if.nif_driver = netif_drivers[d];
106                 drv = cur_if.nif_driver;
107
108                 for (u = 0; u < drv->netif_nifs; u++) {
109                         cur_if.nif_unit = u;
110                         unit_done = 0;
111                 
112 #ifdef NETIF_DEBUG
113                         if (netif_debug)
114                                 printf("\t%s%d:", drv->netif_bname,
115                                     cur_if.nif_unit);
116 #endif
117
118                         for (s = 0; s < drv->netif_ifs[u].dif_nsel; s++) {
119                                 cur_if.nif_sel = s;
120
121                                 if (drv->netif_ifs[u].dif_used & (1 << s)) {
122 #ifdef NETIF_DEBUG
123                                         if (netif_debug)
124                                                 printf(" [%d used]", s);
125 #endif
126                                         continue;
127                                 }
128
129                                 val = netif_match(&cur_if, machdep_hint);
130 #ifdef NETIF_DEBUG
131                                 if (netif_debug)
132                                         printf(" [%d -> %d]", s, val);
133 #endif
134                                 if (val > best_val) {
135                                         best_val = val;
136                                         best_if = cur_if;
137                                 }
138                         }
139 #ifdef NETIF_DEBUG
140                         if (netif_debug)
141                                 printf("\n");
142 #endif
143                 }
144         }
145
146         if (best_if.nif_driver == NULL)
147                 return NULL;
148
149         best_if.nif_driver->
150             netif_ifs[best_if.nif_unit].dif_used |= (1 << best_if.nif_sel);
151
152 #ifdef NETIF_DEBUG
153         if (netif_debug)
154                 printf("netif_select: %s%d(%d) wins\n",
155                         best_if.nif_driver->netif_bname,
156                         best_if.nif_unit, best_if.nif_sel);
157 #endif
158         return &best_if;
159 }
160
161 int
162 netif_probe(nif, machdep_hint)
163         struct netif *nif;
164         void *machdep_hint;
165 {
166         struct netif_driver *drv = nif->nif_driver;
167
168 #ifdef NETIF_DEBUG
169         if (netif_debug)
170                 printf("%s%d: netif_probe\n", drv->netif_bname, nif->nif_unit);
171 #endif
172         return drv->netif_probe(nif, machdep_hint);
173 }
174
175 void
176 netif_attach(nif, desc, machdep_hint)
177         struct netif *nif;
178         struct iodesc *desc;
179         void *machdep_hint;
180 {
181         struct netif_driver *drv = nif->nif_driver;
182
183 #ifdef NETIF_DEBUG
184         if (netif_debug)
185                 printf("%s%d: netif_attach\n", drv->netif_bname, nif->nif_unit);
186 #endif
187         desc->io_netif = nif; 
188 #ifdef PARANOID
189         if (drv->netif_init == NULL)
190                 panic("%s%d: no netif_init support\n", drv->netif_bname,
191                     nif->nif_unit);
192 #endif
193         drv->netif_init(desc, machdep_hint);
194         bzero(drv->netif_ifs[nif->nif_unit].dif_stats, 
195             sizeof(struct netif_stats));
196 }
197
198 void
199 netif_detach(nif)
200         struct netif *nif;
201 {
202         struct netif_driver *drv = nif->nif_driver;
203
204 #ifdef NETIF_DEBUG
205         if (netif_debug)
206                 printf("%s%d: netif_detach\n", drv->netif_bname, nif->nif_unit);
207 #endif
208 #ifdef PARANOID
209         if (drv->netif_end == NULL)
210                 panic("%s%d: no netif_end support\n", drv->netif_bname,
211                     nif->nif_unit);
212 #endif
213         drv->netif_end(nif);
214 }
215
216 ssize_t
217 netif_get(desc, pkt, len, timo)
218         struct iodesc *desc;
219         void *pkt;
220         size_t len;
221         time_t timo;
222 {
223 #ifdef NETIF_DEBUG
224         struct netif *nif = desc->io_netif;
225 #endif
226         struct netif_driver *drv = desc->io_netif->nif_driver;
227         ssize_t rv;
228
229 #ifdef NETIF_DEBUG
230         if (netif_debug)
231                 printf("%s%d: netif_get\n", drv->netif_bname, nif->nif_unit);
232 #endif
233 #ifdef PARANOID
234         if (drv->netif_get == NULL)
235                 panic("%s%d: no netif_get support\n", drv->netif_bname,
236                     nif->nif_unit);
237 #endif
238         rv = drv->netif_get(desc, pkt, len, timo);
239 #ifdef NETIF_DEBUG
240         if (netif_debug)
241                 printf("%s%d: netif_get returning %d\n", drv->netif_bname,
242                     nif->nif_unit, (int)rv);
243 #endif
244         return rv;
245 }
246
247 ssize_t
248 netif_put(desc, pkt, len)
249         struct iodesc *desc;
250         void *pkt;
251         size_t len;
252 {
253 #ifdef NETIF_DEBUG
254         struct netif *nif = desc->io_netif;
255 #endif
256         struct netif_driver *drv = desc->io_netif->nif_driver;
257         ssize_t rv;
258
259 #ifdef NETIF_DEBUG
260         if (netif_debug)
261                 printf("%s%d: netif_put\n", drv->netif_bname, nif->nif_unit);
262 #endif
263 #ifdef PARANOID
264         if (drv->netif_put == NULL)
265                 panic("%s%d: no netif_put support\n", drv->netif_bname,
266                     nif->nif_unit);
267 #endif
268         rv = drv->netif_put(desc, pkt, len);
269 #ifdef NETIF_DEBUG
270         if (netif_debug)
271                 printf("%s%d: netif_put returning %d\n", drv->netif_bname,
272                     nif->nif_unit, (int)rv);
273 #endif
274         return rv;
275 }
276
277 struct iodesc *
278 socktodesc(sock)
279         int sock;
280 {
281         if (sock >= SOPEN_MAX) {
282                 errno = EBADF;
283                 return (NULL);
284         }
285         return (&sockets[sock]);
286 }
287
288 int
289 netif_open(machdep_hint)
290         void *machdep_hint;
291 {
292         int fd;
293         register struct iodesc *s;
294         struct netif *nif;
295         
296         /* find a free socket */
297         for (fd = 0, s = sockets; fd < SOPEN_MAX; fd++, s++)
298                 if (s->io_netif == (struct netif *)0)
299                         goto fnd;
300         errno = EMFILE;
301         return (-1);
302
303 fnd:
304         bzero(s, sizeof(*s));
305         netif_init();
306         nif = netif_select(machdep_hint);
307         if (!nif) 
308                 panic("netboot: no interfaces left untried");
309         if (netif_probe(nif, machdep_hint)) {
310                 printf("netboot: couldn't probe %s%d\n",
311                     nif->nif_driver->netif_bname, nif->nif_unit);
312                 errno = EINVAL;
313                 return(-1);
314         }
315         netif_attach(nif, s, machdep_hint);
316
317         return(fd);
318 }
319
320 int
321 netif_close(sock)
322         int sock;
323 {
324         if (sock >= SOPEN_MAX) {
325                 errno = EBADF;
326                 return(-1);
327         }
328         netif_detach(sockets[sock].io_netif);
329         sockets[sock].io_netif = (struct netif *)0;
330
331         return(0);
332 }