1 /* $NetBSD: netif.c,v 1.25 2013/10/20 17:16:57 christos Exp $ */
4 * Copyright (c) 1993 Adam Glass
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
34 #include <sys/param.h>
35 #include <sys/cdefs.h>
36 #include <sys/mount.h>
38 #include <lib/libkern/libkern.h>
43 #include <netinet/in.h>
44 #include <netinet/in_systm.h>
49 struct iodesc sockets
[SOPEN_MAX
];
57 * initialize the generic network interface layer
63 struct netif_driver
*drv
;
68 printf("netif_init: called\n");
70 for (d
= 0; d
< n_netif_drivers
; d
++) {
71 drv
= netif_drivers
[d
];
72 for (i
= 0; i
< drv
->netif_nifs
; i
++)
73 drv
->netif_ifs
[i
].dif_used
= 0;
77 int netif_match(struct netif
*, void *);
80 netif_match(struct netif
*nif
, void *machdep_hint
)
82 struct netif_driver
*drv
= nif
->nif_driver
;
86 printf("%s%d: netif_match (%d)\n", drv
->netif_bname
,
87 nif
->nif_unit
, nif
->nif_sel
);
89 return drv
->netif_match(nif
, machdep_hint
);
93 netif_select(void *machdep_hint
)
96 struct netif_driver
*drv
;
98 static struct netif best_if
;
103 best_if
.nif_driver
= NULL
;
107 printf("netif_select: %d interfaces\n", n_netif_drivers
);
110 for (d
= 0; d
< n_netif_drivers
; d
++) {
111 cur_if
.nif_driver
= netif_drivers
[d
];
112 drv
= cur_if
.nif_driver
;
114 for (u
= 0; u
< drv
->netif_nifs
; u
++) {
119 printf("\t%s%d:", drv
->netif_bname
,
123 for (s
= 0; s
< drv
->netif_ifs
[u
].dif_nsel
; s
++) {
126 if (drv
->netif_ifs
[u
].dif_used
& (1 << s
)) {
129 printf(" [%d used]", s
);
134 val
= netif_match(&cur_if
, machdep_hint
);
137 printf(" [%d -> %d]", s
, val
);
139 if (val
> best_val
) {
151 if (best_if
.nif_driver
== NULL
)
155 netif_ifs
[best_if
.nif_unit
].dif_used
|= (1 << best_if
.nif_sel
);
159 printf("netif_select: %s%d(%d) wins\n",
160 best_if
.nif_driver
->netif_bname
,
161 best_if
.nif_unit
, best_if
.nif_sel
);
167 netif_probe(struct netif
*nif
, void *machdep_hint
)
169 struct netif_driver
*drv
= nif
->nif_driver
;
173 printf("%s%d: netif_probe\n", drv
->netif_bname
, nif
->nif_unit
);
175 return drv
->netif_probe(nif
, machdep_hint
);
179 netif_attach(struct netif
*nif
, struct iodesc
*desc
, void *machdep_hint
)
181 struct netif_driver
*drv
= nif
->nif_driver
;
185 printf("%s%d: netif_attach\n", drv
->netif_bname
, nif
->nif_unit
);
187 desc
->io_netif
= nif
;
189 if (drv
->netif_init
== NULL
)
190 panic("%s%d: no netif_init support", drv
->netif_bname
,
193 drv
->netif_init(desc
, machdep_hint
);
194 (void)memset(drv
->netif_ifs
[nif
->nif_unit
].dif_stats
, 0,
195 sizeof(struct netif_stats
));
199 netif_detach(struct netif
*nif
)
201 struct netif_driver
*drv
= nif
->nif_driver
;
205 printf("%s%d: netif_detach\n", drv
->netif_bname
, nif
->nif_unit
);
208 if (drv
->netif_end
== NULL
)
209 panic("%s%d: no netif_end support", drv
->netif_bname
,
216 netif_get(struct iodesc
*desc
, void *pkt
, size_t len
, saseconds_t timo
)
218 struct netif
*nif
= desc
->io_netif
;
219 struct netif_driver
*drv
= nif
->nif_driver
;
224 printf("%s%d: netif_get\n", drv
->netif_bname
, nif
->nif_unit
);
227 if (drv
->netif_get
== NULL
)
228 panic("%s%d: no netif_get support", drv
->netif_bname
,
231 rv
= drv
->netif_get(desc
, pkt
, len
, timo
);
234 printf("%s%d: netif_get returning %d\n", drv
->netif_bname
,
235 nif
->nif_unit
, (int)rv
);
241 netif_put(struct iodesc
*desc
, void *pkt
, size_t len
)
243 struct netif
*nif
= desc
->io_netif
;
244 struct netif_driver
*drv
= nif
->nif_driver
;
249 printf("%s%d: netif_put\n", drv
->netif_bname
, nif
->nif_unit
);
252 if (drv
->netif_put
== NULL
)
253 panic("%s%d: no netif_put support", drv
->netif_bname
,
256 rv
= drv
->netif_put(desc
, pkt
, len
);
259 printf("%s%d: netif_put returning %d\n", drv
->netif_bname
,
260 nif
->nif_unit
, (int)rv
);
268 #if !defined(LIBSA_NO_FD_CHECKING)
269 if (sock
>= SOPEN_MAX
) {
274 return &sockets
[sock
];
278 netif_open(void *machdep_hint
)
284 /* find a free socket */
285 for (fd
= 0, s
= sockets
; fd
< SOPEN_MAX
; fd
++, s
++)
286 if (s
->io_netif
== (struct netif
*)0)
292 (void)memset(s
, 0, sizeof(*s
));
294 nif
= netif_select(machdep_hint
);
296 panic("netboot: no interfaces left untried");
297 if (netif_probe(nif
, machdep_hint
)) {
298 printf("netboot: couldn't probe %s%d\n",
299 nif
->nif_driver
->netif_bname
, nif
->nif_unit
);
303 netif_attach(nif
, s
, machdep_hint
);
309 netif_close(int sock
)
311 #if !defined(LIBSA_NO_FD_CHECKING)
312 if (sock
>= SOPEN_MAX
) {
317 netif_detach(sockets
[sock
].io_netif
);
318 sockets
[sock
].io_netif
= (struct netif
*)0;