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