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 struct iodesc sockets[SOPEN_MAX];
51 #ifdef NETIF_DEBUG
52 int netif_debug = 0;
53 #endif
54
55 /*
56 * netif_init:
57 *
58 * initialize the generic network interface layer
59 */
60
61 void
netif_init(void)62 netif_init(void)
63 {
64 struct netif_driver *drv;
65 int d, i;
66
67 #ifdef NETIF_DEBUG
68 if (netif_debug)
69 printf("netif_init: called\n");
70 #endif
71 for (d = 0; netif_drivers[d]; d++) {
72 drv = netif_drivers[d];
73 for (i = 0; i < drv->netif_nifs; i++)
74 drv->netif_ifs[i].dif_used = 0;
75 }
76 }
77
78 int
netif_match(struct netif * nif,void * machdep_hint)79 netif_match(struct netif *nif, void *machdep_hint)
80 {
81 struct netif_driver *drv = nif->nif_driver;
82
83 #if NETIF_DEBUG
84 if (netif_debug)
85 printf("%s%d: netif_match (%d)\n", drv->netif_bname,
86 nif->nif_unit, nif->nif_sel);
87 #endif
88 return drv->netif_match(nif, machdep_hint);
89 }
90
91 struct netif *
netif_select(void * machdep_hint)92 netif_select(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
netif_probe(struct netif * nif,void * machdep_hint)162 netif_probe(struct netif *nif, void *machdep_hint)
163 {
164 struct netif_driver *drv = nif->nif_driver;
165
166 #ifdef NETIF_DEBUG
167 if (netif_debug)
168 printf("%s%d: netif_probe\n", drv->netif_bname, nif->nif_unit);
169 #endif
170 return drv->netif_probe(nif, machdep_hint);
171 }
172
173 void
netif_attach(struct netif * nif,struct iodesc * desc,void * machdep_hint)174 netif_attach(struct netif *nif, struct iodesc *desc, 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_attach\n", drv->netif_bname, nif->nif_unit);
181 #endif
182 desc->io_netif = nif;
183 #ifdef PARANOID
184 if (drv->netif_init == NULL)
185 panic("%s%d: no netif_init support", drv->netif_bname,
186 nif->nif_unit);
187 #endif
188 drv->netif_init(desc, machdep_hint);
189 bzero(drv->netif_ifs[nif->nif_unit].dif_stats,
190 sizeof(struct netif_stats));
191 }
192
193 void
netif_detach(struct netif * nif)194 netif_detach(struct netif *nif)
195 {
196 struct netif_driver *drv = nif->nif_driver;
197
198 #ifdef NETIF_DEBUG
199 if (netif_debug)
200 printf("%s%d: netif_detach\n", drv->netif_bname, nif->nif_unit);
201 #endif
202 #ifdef PARANOID
203 if (drv->netif_end == NULL)
204 panic("%s%d: no netif_end support", drv->netif_bname,
205 nif->nif_unit);
206 #endif
207 drv->netif_end(nif);
208 }
209
210 ssize_t
netif_get(struct iodesc * desc,void ** pkt,time_t timo)211 netif_get(struct iodesc *desc, void **pkt, time_t timo)
212 {
213 #ifdef NETIF_DEBUG
214 struct netif *nif = desc->io_netif;
215 #endif
216 struct netif_driver *drv = desc->io_netif->nif_driver;
217 ssize_t rv;
218
219 #ifdef NETIF_DEBUG
220 if (netif_debug)
221 printf("%s%d: netif_get\n", drv->netif_bname, nif->nif_unit);
222 #endif
223 #ifdef PARANOID
224 if (drv->netif_get == NULL)
225 panic("%s%d: no netif_get support", drv->netif_bname,
226 nif->nif_unit);
227 #endif
228 rv = drv->netif_get(desc, pkt, timo);
229 #ifdef NETIF_DEBUG
230 if (netif_debug)
231 printf("%s%d: netif_get returning %d\n", drv->netif_bname,
232 nif->nif_unit, (int)rv);
233 #endif
234 return (rv);
235 }
236
237 ssize_t
netif_put(struct iodesc * desc,void * pkt,size_t len)238 netif_put(struct iodesc *desc, void *pkt, size_t len)
239 {
240 #ifdef NETIF_DEBUG
241 struct netif *nif = desc->io_netif;
242 #endif
243 struct netif_driver *drv = desc->io_netif->nif_driver;
244 ssize_t rv;
245
246 #ifdef NETIF_DEBUG
247 if (netif_debug)
248 printf("%s%d: netif_put\n", drv->netif_bname, nif->nif_unit);
249 #endif
250 #ifdef PARANOID
251 if (drv->netif_put == NULL)
252 panic("%s%d: no netif_put support", drv->netif_bname,
253 nif->nif_unit);
254 #endif
255 rv = drv->netif_put(desc, pkt, len);
256 #ifdef NETIF_DEBUG
257 if (netif_debug)
258 printf("%s%d: netif_put returning %d\n", drv->netif_bname,
259 nif->nif_unit, (int)rv);
260 #endif
261 return (rv);
262 }
263
264 struct iodesc *
socktodesc(int sock)265 socktodesc(int sock)
266 {
267 if (sock >= SOPEN_MAX) {
268 errno = EBADF;
269 return (NULL);
270 }
271 return (&sockets[sock]);
272 }
273
274 int
netif_open(void * machdep_hint)275 netif_open(void *machdep_hint)
276 {
277 int fd;
278 struct iodesc *s;
279 struct netif *nif;
280
281 /* find a free socket */
282 for (fd = 0, s = sockets; fd < SOPEN_MAX; fd++, s++)
283 if (s->io_netif == (struct netif *)0)
284 goto fnd;
285 errno = EMFILE;
286 return (-1);
287
288 fnd:
289 bzero(s, sizeof(*s));
290 netif_init();
291 nif = netif_select(machdep_hint);
292 if (!nif)
293 panic("netboot: no interfaces left untried");
294 if (netif_probe(nif, machdep_hint)) {
295 printf("netboot: couldn't probe %s%d\n",
296 nif->nif_driver->netif_bname, nif->nif_unit);
297 errno = EINVAL;
298 return (-1);
299 }
300 netif_attach(nif, s, machdep_hint);
301
302 return (fd);
303 }
304
305 int
netif_close(int sock)306 netif_close(int sock)
307 {
308 if (sock >= SOPEN_MAX) {
309 errno = EBADF;
310 return (-1);
311 }
312 netif_detach(sockets[sock].io_netif);
313 sockets[sock].io_netif = (struct netif *)0;
314
315 return (0);
316 }
317