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