]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved. | |
3 | * | |
4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ | |
5 | * | |
6 | * This file contains Original Code and/or Modifications of Original Code | |
7 | * as defined in and that are subject to the Apple Public Source License | |
8 | * Version 2.0 (the 'License'). You may not use this file except in | |
9 | * compliance with the License. The rights granted to you under the License | |
10 | * may not be used to create, or enable the creation or redistribution of, | |
11 | * unlawful or unlicensed copies of an Apple operating system, or to | |
12 | * circumvent, violate, or enable the circumvention or violation of, any | |
13 | * terms of an Apple operating system software license agreement. | |
14 | * | |
15 | * Please obtain a copy of the License at | |
16 | * http://www.opensource.apple.com/apsl/ and read it before using this file. | |
17 | * | |
18 | * The Original Code and all software distributed under the License are | |
19 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER | |
20 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, | |
21 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
22 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. | |
23 | * Please see the License for the specific language governing rights and | |
24 | * limitations under the License. | |
25 | * | |
26 | * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ | |
27 | */ | |
28 | /* Copyright (c) 1997, 1998 Apple Computer, Inc. All Rights Reserved */ | |
29 | /* | |
30 | * @(#)ndrv.c 1.1 (MacOSX) 6/10/43 | |
31 | * Justin Walker, 970604 | |
32 | * AF_NDRV support | |
33 | * 980130 - Cleanup, reorg, performance improvemements | |
34 | * 000816 - Removal of Y adapter cruft | |
35 | */ | |
36 | ||
37 | /* | |
38 | * PF_NDRV allows raw access to a specified network device, directly | |
39 | * with a socket. Expected use involves a socket option to request | |
40 | * protocol packets. This lets ndrv_output() call dlil_output(), and | |
41 | * lets DLIL find the proper recipient for incoming packets. | |
42 | * The purpose here is for user-mode protocol implementation. | |
43 | * Note that "pure raw access" will still be accomplished with BPF. | |
44 | * | |
45 | * In addition to the former use, when combined with socket NKEs, | |
46 | * PF_NDRV permits a fairly flexible mechanism for implementing | |
47 | * strange protocol support. One of the main ones will be the | |
48 | * BlueBox/Classic Shared IP Address support. | |
49 | */ | |
50 | #include <mach/mach_types.h> | |
51 | ||
52 | #include <sys/param.h> | |
53 | #include <sys/systm.h> | |
54 | #include <sys/kernel.h> | |
55 | #include <sys/malloc.h> | |
56 | #include <sys/mbuf.h> | |
57 | #include <sys/protosw.h> | |
58 | #include <sys/domain.h> | |
59 | #include <sys/socket.h> | |
60 | #include <sys/socketvar.h> | |
61 | #include <sys/ioctl.h> | |
62 | #include <sys/errno.h> | |
63 | #include <sys/syslog.h> | |
64 | #include <sys/proc.h> | |
65 | ||
66 | #include <kern/queue.h> | |
67 | ||
68 | #include <net/ndrv.h> | |
69 | #include <net/route.h> | |
70 | #include <net/if_llc.h> | |
71 | #include <net/if_dl.h> | |
72 | #include <net/if_types.h> | |
73 | #include <net/ndrv_var.h> | |
74 | ||
75 | #if INET | |
76 | #include <netinet/in.h> | |
77 | #include <netinet/in_var.h> | |
78 | #endif | |
79 | #include <netinet/if_ether.h> | |
80 | ||
81 | #include <machine/spl.h> | |
82 | ||
83 | static int ndrv_do_detach(struct ndrv_cb *); | |
84 | static int ndrv_do_disconnect(struct ndrv_cb *); | |
85 | static struct ndrv_cb *ndrv_find_inbound(struct ifnet *ifp, u_long protocol_family); | |
86 | static int ndrv_setspec(struct ndrv_cb *np, struct sockopt *sopt); | |
87 | static int ndrv_delspec(struct ndrv_cb *); | |
88 | static int ndrv_to_dlil_demux(struct ndrv_demux_desc* ndrv, struct dlil_demux_desc* dlil); | |
89 | static void ndrv_handle_ifp_detach(u_long family, short unit); | |
90 | static int ndrv_do_add_multicast(struct ndrv_cb *np, struct sockopt *sopt); | |
91 | static int ndrv_do_remove_multicast(struct ndrv_cb *np, struct sockopt *sopt); | |
92 | static struct ndrv_multiaddr* ndrv_have_multicast(struct ndrv_cb *np, struct sockaddr* addr); | |
93 | static void ndrv_remove_all_multicast(struct ndrv_cb *np); | |
94 | ||
95 | unsigned long ndrv_sendspace = NDRVSNDQ; | |
96 | unsigned long ndrv_recvspace = NDRVRCVQ; | |
97 | TAILQ_HEAD(, ndrv_cb) ndrvl = TAILQ_HEAD_INITIALIZER(ndrvl); | |
98 | ||
99 | extern struct domain ndrvdomain; | |
100 | extern struct protosw ndrvsw; | |
101 | extern lck_mtx_t *domain_proto_mtx; | |
102 | ||
103 | extern void kprintf(const char *, ...); | |
104 | ||
105 | /* | |
106 | * Verify these values match. | |
107 | * To keep clients from including dlil.h, we define | |
108 | * these values independently in ndrv.h. They must | |
109 | * match or a conversion function must be written. | |
110 | */ | |
111 | #if NDRV_DEMUXTYPE_ETHERTYPE != DLIL_DESC_ETYPE2 | |
112 | #error NDRV_DEMUXTYPE_ETHERTYPE must match DLIL_DESC_ETYPE2 | |
113 | #endif | |
114 | #if NDRV_DEMUXTYPE_SAP != DLIL_DESC_SAP | |
115 | #error NDRV_DEMUXTYPE_SAP must match DLIL_DESC_SAP | |
116 | #endif | |
117 | #if NDRV_DEMUXTYPE_SNAP != DLIL_DESC_SNAP | |
118 | #error NDRV_DEMUXTYPE_SNAP must match DLIL_DESC_SNAP | |
119 | #endif | |
120 | ||
121 | /* | |
122 | * Protocol output - Called to output a raw network packet directly | |
123 | * to the driver. | |
124 | */ | |
125 | static int | |
126 | ndrv_output(struct mbuf *m, struct socket *so) | |
127 | { | |
128 | struct ndrv_cb *np = sotondrvcb(so); | |
129 | struct ifnet *ifp = np->nd_if; | |
130 | int result = 0; | |
131 | ||
132 | #if NDRV_DEBUG | |
133 | kprintf("NDRV output: %x, %x, %x\n", m, so, np); | |
134 | #endif | |
135 | ||
136 | /* | |
137 | * No header is a format error | |
138 | */ | |
139 | if ((m->m_flags&M_PKTHDR) == 0) | |
140 | return(EINVAL); | |
141 | ||
142 | /* Unlock before calling dlil_output */ | |
143 | socket_unlock(so, 0); | |
144 | ||
145 | /* | |
146 | * Call DLIL if we can. DLIL is much safer than calling the | |
147 | * ifp directly. | |
148 | */ | |
149 | result = dlil_output(ifp, np->nd_proto_family, m, (caddr_t)NULL, | |
150 | (struct sockaddr*)NULL, 1); | |
151 | ||
152 | socket_lock(so, 0); | |
153 | ||
154 | return (result); | |
155 | } | |
156 | ||
157 | /* Our input routine called from DLIL */ | |
158 | static int | |
159 | ndrv_input(struct mbuf *m, | |
160 | char *frame_header, | |
161 | struct ifnet *ifp, | |
162 | u_long proto_family, | |
163 | __unused int sync_ok) | |
164 | { | |
165 | struct socket *so; | |
166 | struct sockaddr_dl ndrvsrc = {sizeof (struct sockaddr_dl), AF_NDRV}; | |
167 | struct ndrv_cb *np; | |
168 | int error = 0; | |
169 | ||
170 | ||
171 | /* move packet from if queue to socket */ | |
172 | /* Should be media-independent */ | |
173 | ndrvsrc.sdl_type = IFT_ETHER; | |
174 | ndrvsrc.sdl_nlen = 0; | |
175 | ndrvsrc.sdl_alen = 6; | |
176 | ndrvsrc.sdl_slen = 0; | |
177 | bcopy(frame_header, &ndrvsrc.sdl_data, 6); | |
178 | ||
179 | np = ndrv_find_inbound(ifp, proto_family); | |
180 | if (np == NULL) | |
181 | { | |
182 | return(ENOENT); | |
183 | } | |
184 | so = np->nd_socket; | |
185 | /* prepend the frame header */ | |
186 | m = m_prepend(m, ifp->if_hdrlen, M_NOWAIT); | |
187 | if (m == NULL) | |
188 | return EJUSTRETURN; | |
189 | bcopy(frame_header, m->m_data, ifp->if_hdrlen); | |
190 | ||
191 | lck_mtx_assert(so->so_proto->pr_domain->dom_mtx, LCK_MTX_ASSERT_NOTOWNED); | |
192 | lck_mtx_lock(so->so_proto->pr_domain->dom_mtx); | |
193 | if (sbappendaddr(&(so->so_rcv), (struct sockaddr *)&ndrvsrc, | |
194 | m, (struct mbuf *)0, &error) != 0) { | |
195 | sorwakeup(so); | |
196 | } | |
197 | lck_mtx_unlock(so->so_proto->pr_domain->dom_mtx); | |
198 | return 0; /* radar 4030377 - always return 0 */ | |
199 | } | |
200 | ||
201 | /* | |
202 | * Allocate an ndrv control block and some buffer space for the socket | |
203 | */ | |
204 | static int | |
205 | ndrv_attach(struct socket *so, int proto, __unused struct proc *p) | |
206 | { | |
207 | int error; | |
208 | struct ndrv_cb *np = sotondrvcb(so); | |
209 | ||
210 | if ((so->so_state & SS_PRIV) == 0) | |
211 | return(EPERM); | |
212 | ||
213 | #if NDRV_DEBUG | |
214 | kprintf("NDRV attach: %x, %x, %x\n", so, proto, np); | |
215 | #endif | |
216 | ||
217 | if ((error = soreserve(so, ndrv_sendspace, ndrv_recvspace))) | |
218 | return(error); | |
219 | ||
220 | MALLOC(np, struct ndrv_cb *, sizeof(*np), M_PCB, M_WAITOK); | |
221 | if (np == NULL) | |
222 | return (ENOMEM); | |
223 | so->so_pcb = (caddr_t)np; | |
224 | bzero(np, sizeof(*np)); | |
225 | #if NDRV_DEBUG | |
226 | kprintf("NDRV attach: %x, %x, %x\n", so, proto, np); | |
227 | #endif | |
228 | TAILQ_INIT(&np->nd_dlist); | |
229 | np->nd_signature = NDRV_SIGNATURE; | |
230 | np->nd_socket = so; | |
231 | np->nd_proto.sp_family = so->so_proto->pr_domain->dom_family; | |
232 | np->nd_proto.sp_protocol = proto; | |
233 | np->nd_if = NULL; | |
234 | np->nd_proto_family = 0; | |
235 | np->nd_family = 0; | |
236 | np->nd_unit = 0; | |
237 | TAILQ_INSERT_TAIL(&ndrvl, np, nd_next); | |
238 | return(0); | |
239 | } | |
240 | ||
241 | /* | |
242 | * Destroy state just before socket deallocation. | |
243 | * Flush data or not depending on the options. | |
244 | */ | |
245 | ||
246 | static int | |
247 | ndrv_detach(struct socket *so) | |
248 | { | |
249 | struct ndrv_cb *np = sotondrvcb(so); | |
250 | ||
251 | if (np == 0) | |
252 | return EINVAL; | |
253 | return ndrv_do_detach(np); | |
254 | } | |
255 | ||
256 | ||
257 | /* | |
258 | * If a socket isn't bound to a single address, | |
259 | * the ndrv input routine will hand it anything | |
260 | * within that protocol family (assuming there's | |
261 | * nothing else around it should go to). | |
262 | * | |
263 | * Don't expect this to be used. | |
264 | */ | |
265 | ||
266 | static int | |
267 | ndrv_connect(struct socket *so, struct sockaddr *nam, __unused struct proc *p) | |
268 | { | |
269 | struct ndrv_cb *np = sotondrvcb(so); | |
270 | int result = 0; | |
271 | ||
272 | if (np == 0) | |
273 | return EINVAL; | |
274 | ||
275 | if (np->nd_faddr) | |
276 | return EISCONN; | |
277 | ||
278 | /* Allocate memory to store the remote address */ | |
279 | MALLOC(np->nd_faddr, struct sockaddr_ndrv*, | |
280 | nam->sa_len, M_IFADDR, M_WAITOK); | |
281 | if (result != 0) | |
282 | return result; | |
283 | if (np->nd_faddr == NULL) | |
284 | return ENOMEM; | |
285 | ||
286 | bcopy((caddr_t) nam, (caddr_t) np->nd_faddr, nam->sa_len); | |
287 | soisconnected(so); | |
288 | return 0; | |
289 | } | |
290 | ||
291 | static void | |
292 | ndrv_event(struct ifnet *ifp, struct kev_msg *event) | |
293 | { | |
294 | if (event->vendor_code == KEV_VENDOR_APPLE && | |
295 | event->kev_class == KEV_NETWORK_CLASS && | |
296 | event->kev_subclass == KEV_DL_SUBCLASS && | |
297 | event->event_code == KEV_DL_IF_DETACHING) { | |
298 | lck_mtx_assert(ndrvdomain.dom_mtx, LCK_MTX_ASSERT_NOTOWNED); | |
299 | lck_mtx_lock(ndrvdomain.dom_mtx); | |
300 | ndrv_handle_ifp_detach(ifp->if_family, ifp->if_unit); | |
301 | lck_mtx_unlock(ndrvdomain.dom_mtx); | |
302 | } | |
303 | } | |
304 | ||
305 | static int name_cmp(struct ifnet *, char *); | |
306 | ||
307 | /* | |
308 | * This is the "driver open" hook - we 'bind' to the | |
309 | * named driver. | |
310 | * Here's where we latch onto the driver. | |
311 | */ | |
312 | static int | |
313 | ndrv_bind(struct socket *so, struct sockaddr *nam, __unused struct proc *p) | |
314 | { | |
315 | struct sockaddr_ndrv *sa = (struct sockaddr_ndrv *) nam; | |
316 | char *dname; | |
317 | struct ndrv_cb *np; | |
318 | struct ifnet *ifp; | |
319 | int result; | |
320 | ||
321 | if TAILQ_EMPTY(&ifnet_head) | |
322 | return(EADDRNOTAVAIL); /* Quick sanity check */ | |
323 | np = sotondrvcb(so); | |
324 | if (np == 0) | |
325 | return EINVAL; | |
326 | ||
327 | if (np->nd_laddr) | |
328 | return EINVAL; /* XXX */ | |
329 | ||
330 | /* I think we just latch onto a copy here; the caller frees */ | |
331 | np->nd_laddr = _MALLOC(sizeof(struct sockaddr_ndrv), M_IFADDR, M_WAITOK); | |
332 | if (np->nd_laddr == NULL) | |
333 | return(ENOMEM); | |
334 | bcopy((caddr_t) sa, (caddr_t) np->nd_laddr, sizeof(struct sockaddr_ndrv)); | |
335 | dname = sa->snd_name; | |
336 | if (*dname == '\0') | |
337 | return(EINVAL); | |
338 | #if NDRV_DEBUG | |
339 | kprintf("NDRV bind: %x, %x, %s\n", so, np, dname); | |
340 | #endif | |
341 | /* Track down the driver and its ifnet structure. | |
342 | * There's no internal call for this so we have to dup the code | |
343 | * in if.c/ifconf() | |
344 | */ | |
345 | ifnet_head_lock_shared(); | |
346 | TAILQ_FOREACH(ifp, &ifnet_head, if_link) { | |
347 | if (name_cmp(ifp, dname) == 0) | |
348 | break; | |
349 | } | |
350 | ifnet_head_done(); | |
351 | ||
352 | if (ifp == NULL) | |
353 | return(EADDRNOTAVAIL); | |
354 | ||
355 | // PPP doesn't support PF_NDRV. | |
356 | if (ifp->if_family != APPLE_IF_FAM_PPP) | |
357 | { | |
358 | /* NDRV on this interface */ | |
359 | struct dlil_proto_reg_str ndrv_proto; | |
360 | result = 0; | |
361 | bzero(&ndrv_proto, sizeof(ndrv_proto)); | |
362 | TAILQ_INIT(&ndrv_proto.demux_desc_head); | |
363 | ||
364 | ndrv_proto.interface_family = ifp->if_family; | |
365 | ndrv_proto.protocol_family = PF_NDRV; | |
366 | ndrv_proto.unit_number = ifp->if_unit; | |
367 | ndrv_proto.event = ndrv_event; | |
368 | ||
369 | /* We aren't worried about double attaching, that should just return an error */ | |
370 | socket_unlock(so, 0); | |
371 | result = dlil_attach_protocol(&ndrv_proto); | |
372 | socket_lock(so, 0); | |
373 | if (result && result != EEXIST) { | |
374 | return result; | |
375 | } | |
376 | np->nd_proto_family = PF_NDRV; | |
377 | } | |
378 | else { | |
379 | np->nd_proto_family = 0; | |
380 | } | |
381 | ||
382 | np->nd_if = ifp; | |
383 | np->nd_family = ifp->if_family; | |
384 | np->nd_unit = ifp->if_unit; | |
385 | ||
386 | return(0); | |
387 | } | |
388 | ||
389 | static int | |
390 | ndrv_disconnect(struct socket *so) | |
391 | { | |
392 | struct ndrv_cb *np = sotondrvcb(so); | |
393 | ||
394 | if (np == 0) | |
395 | return EINVAL; | |
396 | ||
397 | if (np->nd_faddr == 0) | |
398 | return ENOTCONN; | |
399 | ||
400 | ndrv_do_disconnect(np); | |
401 | return 0; | |
402 | } | |
403 | ||
404 | /* | |
405 | * Mark the connection as being incapable of further input. | |
406 | */ | |
407 | static int | |
408 | ndrv_shutdown(struct socket *so) | |
409 | { | |
410 | lck_mtx_assert(so->so_proto->pr_domain->dom_mtx, LCK_MTX_ASSERT_OWNED); | |
411 | socantsendmore(so); | |
412 | return 0; | |
413 | } | |
414 | ||
415 | /* | |
416 | * Ship a packet out. The ndrv output will pass it | |
417 | * to the appropriate driver. The really tricky part | |
418 | * is the destination address... | |
419 | */ | |
420 | static int | |
421 | ndrv_send(struct socket *so, __unused int flags, struct mbuf *m, | |
422 | __unused struct sockaddr *addr, struct mbuf *control, | |
423 | __unused struct proc *p) | |
424 | { | |
425 | int error; | |
426 | ||
427 | if (control) | |
428 | return EOPNOTSUPP; | |
429 | ||
430 | error = ndrv_output(m, so); | |
431 | m = NULL; | |
432 | return error; | |
433 | } | |
434 | ||
435 | ||
436 | static int | |
437 | ndrv_abort(struct socket *so) | |
438 | { | |
439 | struct ndrv_cb *np = sotondrvcb(so); | |
440 | ||
441 | if (np == 0) | |
442 | return EINVAL; | |
443 | ||
444 | ndrv_do_disconnect(np); | |
445 | return 0; | |
446 | } | |
447 | ||
448 | static int | |
449 | ndrv_sockaddr(struct socket *so, struct sockaddr **nam) | |
450 | { | |
451 | struct ndrv_cb *np = sotondrvcb(so); | |
452 | int len; | |
453 | ||
454 | if (np == 0) | |
455 | return EINVAL; | |
456 | ||
457 | if (np->nd_laddr == 0) | |
458 | return EINVAL; | |
459 | ||
460 | len = np->nd_laddr->snd_len; | |
461 | MALLOC(*nam, struct sockaddr *, len, M_SONAME, M_WAITOK); | |
462 | if (*nam == NULL) | |
463 | return ENOMEM; | |
464 | bcopy((caddr_t)np->nd_laddr, *nam, | |
465 | (unsigned)len); | |
466 | return 0; | |
467 | } | |
468 | ||
469 | ||
470 | static int | |
471 | ndrv_peeraddr(struct socket *so, struct sockaddr **nam) | |
472 | { | |
473 | struct ndrv_cb *np = sotondrvcb(so); | |
474 | int len; | |
475 | ||
476 | if (np == 0) | |
477 | return EINVAL; | |
478 | ||
479 | if (np->nd_faddr == 0) | |
480 | return ENOTCONN; | |
481 | ||
482 | len = np->nd_faddr->snd_len; | |
483 | MALLOC(*nam, struct sockaddr *, len, M_SONAME, M_WAITOK); | |
484 | if (*nam == NULL) | |
485 | return ENOMEM; | |
486 | bcopy((caddr_t)np->nd_faddr, *nam, | |
487 | (unsigned)len); | |
488 | return 0; | |
489 | } | |
490 | ||
491 | ||
492 | /* Control output */ | |
493 | ||
494 | static int | |
495 | ndrv_ctloutput(struct socket *so, struct sockopt *sopt) | |
496 | { | |
497 | struct ndrv_cb *np = sotondrvcb(so); | |
498 | int error = 0; | |
499 | ||
500 | switch(sopt->sopt_name) | |
501 | { | |
502 | case NDRV_DELDMXSPEC: /* Delete current spec */ | |
503 | /* Verify no parameter was passed */ | |
504 | if (sopt->sopt_val != NULL || sopt->sopt_valsize != 0) { | |
505 | /* | |
506 | * We don't support deleting a specific demux, it's | |
507 | * all or nothing. | |
508 | */ | |
509 | return EINVAL; | |
510 | } | |
511 | error = ndrv_delspec(np); | |
512 | break; | |
513 | case NDRV_SETDMXSPEC: /* Set protocol spec */ | |
514 | error = ndrv_setspec(np, sopt); | |
515 | break; | |
516 | case NDRV_ADDMULTICAST: | |
517 | error = ndrv_do_add_multicast(np, sopt); | |
518 | break; | |
519 | case NDRV_DELMULTICAST: | |
520 | error = ndrv_do_remove_multicast(np, sopt); | |
521 | break; | |
522 | default: | |
523 | error = ENOTSUP; | |
524 | } | |
525 | #ifdef NDRV_DEBUG | |
526 | log(LOG_WARNING, "NDRV CTLOUT: %x returns %d\n", sopt->sopt_name, | |
527 | error); | |
528 | #endif | |
529 | return(error); | |
530 | } | |
531 | ||
532 | static int | |
533 | ndrv_do_detach(struct ndrv_cb *np) | |
534 | { | |
535 | struct ndrv_cb* cur_np = NULL; | |
536 | struct socket *so = np->nd_socket; | |
537 | int error = 0; | |
538 | struct ifnet * ifp; | |
539 | ||
540 | #if NDRV_DEBUG | |
541 | kprintf("NDRV detach: %x, %x\n", so, np); | |
542 | #endif | |
543 | ndrv_remove_all_multicast(np); | |
544 | ifp = np->nd_if; | |
545 | /* Remove from the linked list of control blocks */ | |
546 | TAILQ_REMOVE(&ndrvl, np, nd_next); | |
547 | if (ifp != NULL) { | |
548 | u_long proto_family = np->nd_proto_family; | |
549 | ||
550 | if (proto_family != PF_NDRV && proto_family != 0) { | |
551 | socket_unlock(so, 0); | |
552 | dlil_detach_protocol(ifp, proto_family); | |
553 | socket_lock(so, 0); | |
554 | } | |
555 | ||
556 | /* Check if this is the last socket attached to this interface */ | |
557 | TAILQ_FOREACH(cur_np, &ndrvl, nd_next) { | |
558 | if (cur_np->nd_family == np->nd_family && | |
559 | cur_np->nd_unit == np->nd_unit) { | |
560 | break; | |
561 | } | |
562 | } | |
563 | ||
564 | /* If there are no other interfaces, detach PF_NDRV from the interface */ | |
565 | if (cur_np == NULL) { | |
566 | socket_unlock(so, 0); | |
567 | dlil_detach_protocol(ifp, PF_NDRV); | |
568 | socket_lock(so, 0); | |
569 | } | |
570 | } | |
571 | ||
572 | if (np->nd_laddr != NULL) { | |
573 | FREE((caddr_t)np->nd_laddr, M_IFADDR); | |
574 | np->nd_laddr = NULL; | |
575 | } | |
576 | FREE((caddr_t)np, M_PCB); | |
577 | so->so_pcb = 0; | |
578 | so->so_flags |= SOF_PCBCLEARING; | |
579 | sofree(so); | |
580 | return error; | |
581 | } | |
582 | ||
583 | static int | |
584 | ndrv_do_disconnect(struct ndrv_cb *np) | |
585 | { | |
586 | struct socket * so = np->nd_socket; | |
587 | #if NDRV_DEBUG | |
588 | kprintf("NDRV disconnect: %x\n", np); | |
589 | #endif | |
590 | if (np->nd_faddr) | |
591 | { | |
592 | FREE(np->nd_faddr, M_IFADDR); | |
593 | np->nd_faddr = 0; | |
594 | } | |
595 | if (so->so_state & SS_NOFDREF) | |
596 | ndrv_do_detach(np); | |
597 | soisdisconnected(so); | |
598 | return(0); | |
599 | } | |
600 | ||
601 | /* Hackery - return a string version of a decimal number */ | |
602 | static char * | |
603 | sprint_d(u_int n, char *buf, int buflen) | |
604 | { char dbuf[IFNAMSIZ]; | |
605 | char *cp = dbuf+IFNAMSIZ-1; | |
606 | ||
607 | *cp = 0; | |
608 | do { buflen--; | |
609 | cp--; | |
610 | *cp = "0123456789"[n % 10]; | |
611 | n /= 10; | |
612 | } while (n != 0 && buflen > 0); | |
613 | strncpy(buf, cp, IFNAMSIZ-buflen); | |
614 | return (cp); | |
615 | } | |
616 | ||
617 | /* | |
618 | * Try to compare a device name (q) with one of the funky ifnet | |
619 | * device names (ifp). | |
620 | */ | |
621 | static int name_cmp(struct ifnet *ifp, char *q) | |
622 | { char *r; | |
623 | int len; | |
624 | char buf[IFNAMSIZ]; | |
625 | ||
626 | r = buf; | |
627 | len = strlen(ifp->if_name); | |
628 | strncpy(r, ifp->if_name, IFNAMSIZ); | |
629 | r += len; | |
630 | (void)sprint_d(ifp->if_unit, r, IFNAMSIZ-(r-buf)); | |
631 | #if NDRV_DEBUG | |
632 | kprintf("Comparing %s, %s\n", buf, q); | |
633 | #endif | |
634 | return(strncmp(buf, q, IFNAMSIZ)); | |
635 | } | |
636 | ||
637 | #if 0 | |
638 | //### Not used | |
639 | /* | |
640 | * When closing, dump any enqueued mbufs. | |
641 | */ | |
642 | void | |
643 | ndrv_flushq(struct ifqueue *q) | |
644 | { | |
645 | struct mbuf *m; | |
646 | for (;;) | |
647 | { | |
648 | IF_DEQUEUE(q, m); | |
649 | if (m == NULL) | |
650 | break; | |
651 | IF_DROP(q); | |
652 | if (m) | |
653 | m_freem(m); | |
654 | } | |
655 | } | |
656 | #endif | |
657 | ||
658 | int | |
659 | ndrv_setspec(struct ndrv_cb *np, struct sockopt *sopt) | |
660 | { | |
661 | struct dlil_proto_reg_str dlilSpec; | |
662 | struct ndrv_protocol_desc ndrvSpec; | |
663 | struct dlil_demux_desc* dlilDemux = NULL; | |
664 | struct ndrv_demux_desc* ndrvDemux = NULL; | |
665 | int error = 0; | |
666 | struct socket *so = np->nd_socket; | |
667 | ||
668 | /* Sanity checking */ | |
669 | if (np->nd_proto_family != PF_NDRV) | |
670 | return EBUSY; | |
671 | if (np->nd_if == NULL) | |
672 | return EINVAL; | |
673 | if (sopt->sopt_valsize != sizeof(struct ndrv_protocol_desc)) | |
674 | return EINVAL; | |
675 | ||
676 | /* Copy the ndrvSpec */ | |
677 | error = sooptcopyin(sopt, &ndrvSpec, sizeof(struct ndrv_protocol_desc), | |
678 | sizeof(struct ndrv_protocol_desc)); | |
679 | if (error != 0) | |
680 | return error; | |
681 | ||
682 | /* Verify the parameter */ | |
683 | if (ndrvSpec.version > NDRV_PROTOCOL_DESC_VERS) | |
684 | return ENOTSUP; // version is too new! | |
685 | else if (ndrvSpec.version < 1) | |
686 | return EINVAL; // version is not valid | |
687 | ||
688 | /* Allocate storage for demux array */ | |
689 | MALLOC(ndrvDemux, struct ndrv_demux_desc*, | |
690 | ndrvSpec.demux_count * sizeof(struct ndrv_demux_desc), M_TEMP, M_WAITOK); | |
691 | if (ndrvDemux == NULL) | |
692 | return ENOMEM; | |
693 | ||
694 | /* Allocate enough dlil_demux_descs */ | |
695 | MALLOC(dlilDemux, struct dlil_demux_desc*, | |
696 | sizeof(*dlilDemux) * ndrvSpec.demux_count, M_TEMP, M_WAITOK); | |
697 | if (dlilDemux == NULL) | |
698 | error = ENOMEM; | |
699 | ||
700 | if (error == 0) | |
701 | { | |
702 | /* Copy the ndrv demux array from userland */ | |
703 | error = copyin(CAST_USER_ADDR_T(ndrvSpec.demux_list), ndrvDemux, | |
704 | ndrvSpec.demux_count * sizeof(struct ndrv_demux_desc)); | |
705 | ndrvSpec.demux_list = ndrvDemux; | |
706 | } | |
707 | ||
708 | if (error == 0) | |
709 | { | |
710 | /* At this point, we've at least got enough bytes to start looking around */ | |
711 | u_long demuxOn = 0; | |
712 | ||
713 | bzero(&dlilSpec, sizeof(dlilSpec)); | |
714 | TAILQ_INIT(&dlilSpec.demux_desc_head); | |
715 | dlilSpec.interface_family = np->nd_family; | |
716 | dlilSpec.unit_number = np->nd_unit; | |
717 | dlilSpec.input = ndrv_input; | |
718 | dlilSpec.event = ndrv_event; | |
719 | dlilSpec.protocol_family = ndrvSpec.protocol_family; | |
720 | ||
721 | for (demuxOn = 0; demuxOn < ndrvSpec.demux_count; demuxOn++) | |
722 | { | |
723 | /* Convert an ndrv_demux_desc to a dlil_demux_desc */ | |
724 | error = ndrv_to_dlil_demux(&ndrvSpec.demux_list[demuxOn], &dlilDemux[demuxOn]); | |
725 | if (error) | |
726 | break; | |
727 | ||
728 | /* Add the dlil_demux_desc to the list */ | |
729 | TAILQ_INSERT_TAIL(&dlilSpec.demux_desc_head, &dlilDemux[demuxOn], next); | |
730 | } | |
731 | } | |
732 | ||
733 | if (error == 0) | |
734 | { | |
735 | /* We've got all our ducks lined up...lets attach! */ | |
736 | socket_unlock(so, 0); | |
737 | error = dlil_attach_protocol(&dlilSpec); | |
738 | socket_lock(so, 0); | |
739 | if (error == 0) | |
740 | np->nd_proto_family = dlilSpec.protocol_family; | |
741 | } | |
742 | ||
743 | /* Free any memory we've allocated */ | |
744 | if (dlilDemux) | |
745 | FREE(dlilDemux, M_TEMP); | |
746 | if (ndrvDemux) | |
747 | FREE(ndrvDemux, M_TEMP); | |
748 | ||
749 | return error; | |
750 | } | |
751 | ||
752 | ||
753 | int | |
754 | ndrv_to_dlil_demux(struct ndrv_demux_desc* ndrv, struct dlil_demux_desc* dlil) | |
755 | { | |
756 | bzero(dlil, sizeof(*dlil)); | |
757 | ||
758 | if (ndrv->type < DLIL_DESC_ETYPE2) | |
759 | { | |
760 | /* using old "type", not supported */ | |
761 | return ENOTSUP; | |
762 | } | |
763 | ||
764 | if (ndrv->length > 28) | |
765 | { | |
766 | return EINVAL; | |
767 | } | |
768 | ||
769 | dlil->type = ndrv->type; | |
770 | dlil->native_type = ndrv->data.other; | |
771 | dlil->variants.native_type_length = ndrv->length; | |
772 | ||
773 | return 0; | |
774 | } | |
775 | ||
776 | int | |
777 | ndrv_delspec(struct ndrv_cb *np) | |
778 | { | |
779 | int result = 0; | |
780 | ||
781 | if (np->nd_proto_family == PF_NDRV || | |
782 | np->nd_proto_family == 0) | |
783 | return EINVAL; | |
784 | ||
785 | /* Detach the protocol */ | |
786 | result = dlil_detach_protocol(np->nd_if, np->nd_proto_family); | |
787 | np->nd_proto_family = PF_NDRV; | |
788 | ||
789 | return result; | |
790 | } | |
791 | ||
792 | struct ndrv_cb * | |
793 | ndrv_find_inbound(struct ifnet *ifp, u_long protocol) | |
794 | { | |
795 | struct ndrv_cb* np; | |
796 | ||
797 | if (protocol == PF_NDRV) return NULL; | |
798 | ||
799 | TAILQ_FOREACH(np, &ndrvl, nd_next) { | |
800 | if (np->nd_proto_family == protocol && | |
801 | np->nd_if == ifp) { | |
802 | return np; | |
803 | } | |
804 | } | |
805 | ||
806 | return NULL; | |
807 | } | |
808 | ||
809 | static void ndrv_dominit(void) | |
810 | { | |
811 | static int ndrv_dominited = 0; | |
812 | ||
813 | if (ndrv_dominited == 0 && | |
814 | net_add_proto(&ndrvsw, &ndrvdomain) == 0) | |
815 | ndrv_dominited = 1; | |
816 | } | |
817 | ||
818 | static void | |
819 | ndrv_handle_ifp_detach(u_long family, short unit) | |
820 | { | |
821 | struct ndrv_cb* np; | |
822 | struct ifnet *ifp = NULL; | |
823 | struct socket *so; | |
824 | ||
825 | /* Find all sockets using this interface. */ | |
826 | TAILQ_FOREACH(np, &ndrvl, nd_next) { | |
827 | if (np->nd_family == family && | |
828 | np->nd_unit == unit) | |
829 | { | |
830 | /* This cb is using the detaching interface, but not for long. */ | |
831 | /* Let the protocol go */ | |
832 | ifp = np->nd_if; | |
833 | if (np->nd_proto_family != 0) | |
834 | ndrv_delspec(np); | |
835 | ||
836 | /* Delete the multicasts first */ | |
837 | ndrv_remove_all_multicast(np); | |
838 | ||
839 | /* Disavow all knowledge of the ifp */ | |
840 | np->nd_if = NULL; | |
841 | np->nd_unit = 0; | |
842 | np->nd_family = 0; | |
843 | ||
844 | so = np->nd_socket; | |
845 | /* Make sure sending returns an error */ | |
846 | /* Is this safe? Will we drop the funnel? */ | |
847 | lck_mtx_assert(so->so_proto->pr_domain->dom_mtx, LCK_MTX_ASSERT_OWNED); | |
848 | socantsendmore(so); | |
849 | socantrcvmore(so); | |
850 | } | |
851 | } | |
852 | ||
853 | /* Unregister our protocol */ | |
854 | if (ifp) { | |
855 | dlil_detach_protocol(ifp, PF_NDRV); | |
856 | } | |
857 | } | |
858 | ||
859 | static int | |
860 | ndrv_do_add_multicast(struct ndrv_cb *np, struct sockopt *sopt) | |
861 | { | |
862 | struct ndrv_multiaddr* ndrv_multi; | |
863 | int result; | |
864 | ||
865 | if (sopt->sopt_val == NULL || sopt->sopt_valsize < 2 || | |
866 | sopt->sopt_level != SOL_NDRVPROTO) | |
867 | return EINVAL; | |
868 | if (np->nd_if == NULL) | |
869 | return ENXIO; | |
870 | ||
871 | // Allocate storage | |
872 | MALLOC(ndrv_multi, struct ndrv_multiaddr*, sizeof(struct ndrv_multiaddr) - | |
873 | sizeof(struct sockaddr) + sopt->sopt_valsize, M_IFADDR, M_WAITOK); | |
874 | if (ndrv_multi == NULL) | |
875 | return ENOMEM; | |
876 | ||
877 | // Copy in the address | |
878 | result = copyin(sopt->sopt_val, &ndrv_multi->addr, sopt->sopt_valsize); | |
879 | ||
880 | // Validate the sockaddr | |
881 | if (result == 0 && sopt->sopt_valsize != ndrv_multi->addr.sa_len) | |
882 | result = EINVAL; | |
883 | ||
884 | if (result == 0 && ndrv_have_multicast(np, &ndrv_multi->addr)) | |
885 | result = EEXIST; | |
886 | ||
887 | if (result == 0) | |
888 | { | |
889 | // Try adding the multicast | |
890 | result = if_addmulti(np->nd_if, &ndrv_multi->addr, &ndrv_multi->ifma); | |
891 | } | |
892 | ||
893 | if (result == 0) | |
894 | { | |
895 | // Add to our linked list | |
896 | ndrv_multi->next = np->nd_multiaddrs; | |
897 | np->nd_multiaddrs = ndrv_multi; | |
898 | } | |
899 | else | |
900 | { | |
901 | // Free up the memory, something went wrong | |
902 | FREE(ndrv_multi, M_IFADDR); | |
903 | } | |
904 | ||
905 | return result; | |
906 | } | |
907 | ||
908 | static int | |
909 | ndrv_do_remove_multicast(struct ndrv_cb *np, struct sockopt *sopt) | |
910 | { | |
911 | struct sockaddr* multi_addr; | |
912 | struct ndrv_multiaddr* ndrv_entry = NULL; | |
913 | int result; | |
914 | ||
915 | if (sopt->sopt_val == NULL || sopt->sopt_valsize < 2 || | |
916 | sopt->sopt_level != SOL_NDRVPROTO) | |
917 | return EINVAL; | |
918 | if (np->nd_if == NULL) | |
919 | return ENXIO; | |
920 | ||
921 | // Allocate storage | |
922 | MALLOC(multi_addr, struct sockaddr*, sopt->sopt_valsize, | |
923 | M_TEMP, M_WAITOK); | |
924 | if (multi_addr == NULL) | |
925 | return ENOMEM; | |
926 | ||
927 | // Copy in the address | |
928 | result = copyin(sopt->sopt_val, multi_addr, sopt->sopt_valsize); | |
929 | ||
930 | // Validate the sockaddr | |
931 | if (result == 0 && sopt->sopt_valsize != multi_addr->sa_len) | |
932 | result = EINVAL; | |
933 | ||
934 | if (result == 0) | |
935 | { | |
936 | /* Find the old entry */ | |
937 | ndrv_entry = ndrv_have_multicast(np, multi_addr); | |
938 | ||
939 | if (ndrv_entry == NULL) | |
940 | result = ENOENT; | |
941 | } | |
942 | ||
943 | if (result == 0) | |
944 | { | |
945 | // Try deleting the multicast | |
946 | result = if_delmultiaddr(ndrv_entry->ifma, 0); | |
947 | } | |
948 | ||
949 | if (result == 0) | |
950 | { | |
951 | // Remove from our linked list | |
952 | struct ndrv_multiaddr* cur = np->nd_multiaddrs; | |
953 | ||
954 | ifma_release(ndrv_entry->ifma); | |
955 | ||
956 | if (cur == ndrv_entry) | |
957 | { | |
958 | np->nd_multiaddrs = cur->next; | |
959 | } | |
960 | else | |
961 | { | |
962 | for (cur = cur->next; cur != NULL; cur = cur->next) | |
963 | { | |
964 | if (cur->next == ndrv_entry) | |
965 | { | |
966 | cur->next = cur->next->next; | |
967 | break; | |
968 | } | |
969 | } | |
970 | } | |
971 | ||
972 | // Free the memory | |
973 | FREE(ndrv_entry, M_IFADDR); | |
974 | } | |
975 | FREE(multi_addr, M_TEMP); | |
976 | ||
977 | return result; | |
978 | } | |
979 | ||
980 | static struct ndrv_multiaddr* | |
981 | ndrv_have_multicast(struct ndrv_cb *np, struct sockaddr* inAddr) | |
982 | { | |
983 | struct ndrv_multiaddr* cur; | |
984 | for (cur = np->nd_multiaddrs; cur != NULL; cur = cur->next) | |
985 | { | |
986 | ||
987 | if ((inAddr->sa_len == cur->addr.sa_len) && | |
988 | (bcmp(&cur->addr, inAddr, inAddr->sa_len) == 0)) | |
989 | { | |
990 | // Found a match | |
991 | return cur; | |
992 | } | |
993 | } | |
994 | ||
995 | return NULL; | |
996 | } | |
997 | ||
998 | static void | |
999 | ndrv_remove_all_multicast(struct ndrv_cb* np) | |
1000 | { | |
1001 | struct ndrv_multiaddr* cur; | |
1002 | ||
1003 | if (np->nd_if != NULL) | |
1004 | { | |
1005 | while (np->nd_multiaddrs != NULL) | |
1006 | { | |
1007 | cur = np->nd_multiaddrs; | |
1008 | np->nd_multiaddrs = cur->next; | |
1009 | ||
1010 | if_delmultiaddr(cur->ifma, 0); | |
1011 | ifma_release(cur->ifma); | |
1012 | FREE(cur, M_IFADDR); | |
1013 | } | |
1014 | } | |
1015 | } | |
1016 | ||
1017 | struct pr_usrreqs ndrv_usrreqs = { | |
1018 | ndrv_abort, pru_accept_notsupp, ndrv_attach, ndrv_bind, | |
1019 | ndrv_connect, pru_connect2_notsupp, pru_control_notsupp, ndrv_detach, | |
1020 | ndrv_disconnect, pru_listen_notsupp, ndrv_peeraddr, pru_rcvd_notsupp, | |
1021 | pru_rcvoob_notsupp, ndrv_send, pru_sense_null, ndrv_shutdown, | |
1022 | ndrv_sockaddr, sosend, soreceive, pru_sopoll_notsupp | |
1023 | }; | |
1024 | ||
1025 | struct protosw ndrvsw = | |
1026 | { SOCK_RAW, &ndrvdomain, NDRVPROTO_NDRV, PR_ATOMIC|PR_ADDR, | |
1027 | 0, ndrv_output, 0, ndrv_ctloutput, | |
1028 | 0, 0, 0, 0, | |
1029 | 0, 0, | |
1030 | &ndrv_usrreqs, | |
1031 | 0, 0, 0 | |
1032 | }; | |
1033 | ||
1034 | struct domain ndrvdomain = | |
1035 | { AF_NDRV, "NetDriver", ndrv_dominit, NULL, NULL, | |
1036 | NULL, | |
1037 | NULL, NULL, 0, 0, 0, 0 | |
1038 | }; |