xref: /openbsd/sys/lib/libsa/netif.c (revision e5dd7070)
1 /*	$OpenBSD: netif.c,v 1.12 2017/09/08 05:36:53 deraadt Exp $	*/
2 /*	$NetBSD: netif.c,v 1.7 1996/10/13 02:29:03 christos Exp $	*/
3 
4 /*
5  * Copyright (c) 1993 Adam Glass
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *	This product includes software developed by Adam Glass.
19  * 4. The name of the Author may not be used to endorse or promote products
20  *    derived from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY Adam Glass ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  */
34 
35 #include <sys/param.h>
36 #include <sys/mount.h>
37 
38 #include <netinet/in.h>
39 
40 #include "stand.h"
41 #include "net.h"
42 #include "netif.h"
43 
44 struct iodesc sockets[SOPEN_MAX];
45 #ifdef NETIF_DEBUG
46 int netif_debug = 0;
47 #endif
48 
49 /*
50  * netif_init:
51  *
52  * initialize the generic network interface layer
53  */
54 
55 void
56 netif_init(void)
57 {
58 	struct netif_driver *drv;
59 	int d, i;
60 
61 #ifdef NETIF_DEBUG
62 	if (netif_debug)
63 		printf("netif_init: called\n");
64 #endif
65 	for (d = 0; d < n_netif_drivers; d++) {
66 		drv = netif_drivers[d];
67 		for (i = 0; i < drv->netif_nifs; i++)
68 			drv->netif_ifs[i].dif_used = 0;
69 	}
70 }
71 
72 static int
73 netif_match(struct netif *nif, void *machdep_hint)
74 {
75 	struct netif_driver *drv = nif->nif_driver;
76 
77 #if 0
78 	if (netif_debug)
79 		printf("%s%d: netif_match (%d)\n", drv->netif_bname,
80 		    nif->nif_unit, nif->nif_sel);
81 #endif
82 	return drv->netif_match(nif, machdep_hint);
83 }
84 
85 struct netif *
86 netif_select(void *machdep_hint)
87 {
88 	int d, u, unit_done, s;
89 	struct netif_driver *drv;
90 	struct netif cur_if;
91 	static struct netif best_if;
92 	int best_val;
93 	int val;
94 
95 	best_val = 0;
96 	best_if.nif_driver = NULL;
97 
98 #ifdef NETIF_DEBUG
99 	if (netif_debug)
100 		printf("netif_select: %d interfaces\n", n_netif_drivers);
101 #endif
102 
103 	for (d = 0; d < n_netif_drivers; d++) {
104 		cur_if.nif_driver = netif_drivers[d];
105 		drv = cur_if.nif_driver;
106 
107 		for (u = 0; u < drv->netif_nifs; u++) {
108 			cur_if.nif_unit = u;
109 			unit_done = 0;
110 
111 #ifdef NETIF_DEBUG
112 			if (netif_debug)
113 				printf("\t%s%d:", drv->netif_bname,
114 				    cur_if.nif_unit);
115 #endif
116 
117 			for (s = 0; s < drv->netif_ifs[u].dif_nsel; s++) {
118 				cur_if.nif_sel = s;
119 
120 				if (drv->netif_ifs[u].dif_used & (1 << s)) {
121 #ifdef NETIF_DEBUG
122 					if (netif_debug)
123 						printf(" [%d used]", s);
124 #endif
125 					continue;
126 				}
127 
128 				val = netif_match(&cur_if, machdep_hint);
129 #ifdef NETIF_DEBUG
130 				if (netif_debug)
131 					printf(" [%d -> %d]", s, val);
132 #endif
133 				if (val > best_val) {
134 					best_val = val;
135 					best_if = cur_if;
136 				}
137 			}
138 #ifdef NETIF_DEBUG
139 			if (netif_debug)
140 				printf("\n");
141 #endif
142 		}
143 	}
144 
145 	if (best_if.nif_driver == NULL)
146 		return NULL;
147 
148 	best_if.nif_driver->netif_ifs[best_if.nif_unit].dif_used |=
149 	    (1 << best_if.nif_sel);
150 
151 #ifdef NETIF_DEBUG
152 	if (netif_debug)
153 		printf("netif_select: %s%d(%d) wins\n",
154 		    best_if.nif_driver->netif_bname,
155 		    best_if.nif_unit, best_if.nif_sel);
156 #endif
157 	return &best_if;
158 }
159 
160 int
161 netif_probe(struct netif *nif, void *machdep_hint)
162 {
163 	struct netif_driver *drv = nif->nif_driver;
164 
165 #ifdef NETIF_DEBUG
166 	if (netif_debug)
167 		printf("%s%d: netif_probe\n", drv->netif_bname, nif->nif_unit);
168 #endif
169 	return drv->netif_probe(nif, machdep_hint);
170 }
171 
172 void
173 netif_attach(struct netif *nif, struct iodesc *desc, 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_attach\n", drv->netif_bname, nif->nif_unit);
180 #endif
181 	desc->io_netif = nif;
182 #ifdef PARANOID
183 	if (drv->netif_init == NULL)
184 		panic("%s%d: no netif_init support", drv->netif_bname,
185 		    nif->nif_unit);
186 #endif
187 	drv->netif_init(desc, machdep_hint);
188 	bzero(drv->netif_ifs[nif->nif_unit].dif_stats,
189 	    sizeof(struct netif_stats));
190 }
191 
192 void
193 netif_detach(struct netif *nif)
194 {
195 	struct netif_driver *drv = nif->nif_driver;
196 
197 #ifdef NETIF_DEBUG
198 	if (netif_debug)
199 		printf("%s%d: netif_detach\n", drv->netif_bname, nif->nif_unit);
200 #endif
201 #ifdef PARANOID
202 	if (drv->netif_end == NULL)
203 		panic("%s%d: no netif_end support", drv->netif_bname,
204 		    nif->nif_unit);
205 #endif
206 	drv->netif_end(nif);
207 }
208 
209 ssize_t
210 netif_get(struct iodesc *desc, void *pkt, size_t len, time_t timo)
211 {
212 #ifdef NETIF_DEBUG
213 	struct netif *nif = desc->io_netif;
214 #endif
215 	struct netif_driver *drv = desc->io_netif->nif_driver;
216 	ssize_t rv;
217 
218 #ifdef NETIF_DEBUG
219 	if (netif_debug)
220 		printf("%s%d: netif_get\n", drv->netif_bname, nif->nif_unit);
221 #endif
222 #ifdef PARANOID
223 	if (drv->netif_get == NULL)
224 		panic("%s%d: no netif_get support", drv->netif_bname,
225 		    nif->nif_unit);
226 #endif
227 	rv = drv->netif_get(desc, pkt, len, timo);
228 #ifdef NETIF_DEBUG
229 	if (netif_debug)
230 		printf("%s%d: netif_get returning %d\n", drv->netif_bname,
231 		    nif->nif_unit, rv);
232 #endif
233 	return rv;
234 }
235 
236 ssize_t
237 netif_put(struct iodesc *desc, void *pkt, size_t len)
238 {
239 #ifdef NETIF_DEBUG
240 	struct netif *nif = desc->io_netif;
241 #endif
242 	struct netif_driver *drv = desc->io_netif->nif_driver;
243 	ssize_t rv;
244 
245 #ifdef NETIF_DEBUG
246 	if (netif_debug)
247 		printf("%s%d: netif_put\n", drv->netif_bname, nif->nif_unit);
248 #endif
249 #ifdef PARANOID
250 	if (drv->netif_put == NULL)
251 		panic("%s%d: no netif_put support", drv->netif_bname,
252 		    nif->nif_unit);
253 #endif
254 	rv = drv->netif_put(desc, pkt, len);
255 #ifdef NETIF_DEBUG
256 	if (netif_debug)
257 		printf("%s%d: netif_put returning %d\n", drv->netif_bname,
258 		    nif->nif_unit, rv);
259 #endif
260 	return rv;
261 }
262 
263 struct iodesc *
264 socktodesc(sock)
265 	int sock;
266 {
267 	if (sock >= SOPEN_MAX) {
268 		errno = EBADF;
269 		return (NULL);
270 	}
271 	return (&sockets[sock]);
272 }
273 
274 int
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 == NULL)
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
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 = NULL;
314 
315 	return(0);
316 }
317