]> git.saurik.com Git - apple/xnu.git/blob - bsd/net/ndrv.c
87b6a77493821d3a76bf40834d582e2f40c1cfee
[apple/xnu.git] / bsd / net / ndrv.c
1 /*
2 * Copyright (c) 1997-2014 Apple 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 /*
29 * @(#)ndrv.c 1.1 (MacOSX) 6/10/43
30 * Justin Walker, 970604
31 * AF_NDRV support
32 * 980130 - Cleanup, reorg, performance improvemements
33 * 000816 - Removal of Y adapter cruft
34 */
35
36 /*
37 * PF_NDRV allows raw access to a specified network device, directly
38 * with a socket. Expected use involves a socket option to request
39 * protocol packets. This lets ndrv_output() call ifnet_output(), and
40 * lets DLIL find the proper recipient for incoming packets.
41 * The purpose here is for user-mode protocol implementation.
42 * Note that "pure raw access" will still be accomplished with BPF.
43 *
44 * In addition to the former use, when combined with socket NKEs,
45 * PF_NDRV permits a fairly flexible mechanism for implementing
46 * strange protocol support.
47 */
48 #include <mach/mach_types.h>
49
50 #include <sys/param.h>
51 #include <sys/systm.h>
52 #include <sys/kernel.h>
53 #include <sys/malloc.h>
54 #include <sys/mbuf.h>
55 #include <sys/protosw.h>
56 #include <sys/domain.h>
57 #include <sys/socket.h>
58 #include <sys/socketvar.h>
59 #include <sys/ioctl.h>
60 #include <sys/sysctl.h>
61 #include <sys/errno.h>
62 #include <sys/syslog.h>
63 #include <sys/proc.h>
64
65 #include <kern/queue.h>
66
67 #include <net/ndrv.h>
68 #include <net/route.h>
69 #include <net/if_llc.h>
70 #include <net/if_dl.h>
71 #include <net/if_types.h>
72 #include <net/ndrv_var.h>
73 #include <net/dlil.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 unsigned int ndrv_multi_max_count = NDRV_DMUX_MAX_DESCR;
84 SYSCTL_UINT(_net, OID_AUTO, ndrv_multi_max_count, CTLFLAG_RW | CTLFLAG_LOCKED,
85 &ndrv_multi_max_count, 0, "Number of allowed multicast addresses per NRDV socket");
86
87 static int ndrv_do_detach(struct ndrv_cb *);
88 static int ndrv_do_disconnect(struct ndrv_cb *);
89 static struct ndrv_cb *ndrv_find_inbound(struct ifnet *ifp, u_int32_t protocol_family);
90 static int ndrv_setspec(struct ndrv_cb *np, struct sockopt *sopt);
91 static int ndrv_delspec(struct ndrv_cb *);
92 static int ndrv_to_ifnet_demux(struct ndrv_demux_desc* ndrv, struct ifnet_demux_desc* ifdemux);
93 static void ndrv_handle_ifp_detach(u_int32_t family, short unit);
94 static int ndrv_do_add_multicast(struct ndrv_cb *np, struct sockopt *sopt);
95 static int ndrv_do_remove_multicast(struct ndrv_cb *np, struct sockopt *sopt);
96 static struct ndrv_multiaddr* ndrv_have_multicast(struct ndrv_cb *np, struct sockaddr* addr);
97 static void ndrv_remove_all_multicast(struct ndrv_cb *np);
98 static void ndrv_dominit(struct domain *);
99
100 u_int32_t ndrv_sendspace = NDRVSNDQ;
101 u_int32_t ndrv_recvspace = NDRVRCVQ;
102 TAILQ_HEAD(, ndrv_cb) ndrvl = TAILQ_HEAD_INITIALIZER(ndrvl);
103
104 static struct domain *ndrvdomain = NULL;
105 extern struct domain ndrvdomain_s;
106
107 #define NDRV_PROTODEMUX_COUNT 10
108
109 /*
110 * Verify these values match.
111 * To keep clients from including dlil.h, we define
112 * these values independently in ndrv.h. They must
113 * match or a conversion function must be written.
114 */
115 #if NDRV_DEMUXTYPE_ETHERTYPE != DLIL_DESC_ETYPE2
116 #error NDRV_DEMUXTYPE_ETHERTYPE must match DLIL_DESC_ETYPE2
117 #endif
118 #if NDRV_DEMUXTYPE_SAP != DLIL_DESC_SAP
119 #error NDRV_DEMUXTYPE_SAP must match DLIL_DESC_SAP
120 #endif
121 #if NDRV_DEMUXTYPE_SNAP != DLIL_DESC_SNAP
122 #error NDRV_DEMUXTYPE_SNAP must match DLIL_DESC_SNAP
123 #endif
124
125 /*
126 * Protocol output - Called to output a raw network packet directly
127 * to the driver.
128 */
129 static int
130 ndrv_output(struct mbuf *m, struct socket *so)
131 {
132 struct ndrv_cb *np = sotondrvcb(so);
133 struct ifnet *ifp = np->nd_if;
134 int result = 0;
135
136 #if NDRV_DEBUG
137 printf("NDRV output: %x, %x, %x\n", m, so, np);
138 #endif
139
140 /*
141 * No header is a format error
142 */
143 if ((m->m_flags&M_PKTHDR) == 0)
144 return(EINVAL);
145
146 /* Unlock before calling ifnet_output */
147 socket_unlock(so, 0);
148
149 /*
150 * Call DLIL if we can. DLIL is much safer than calling the
151 * ifp directly.
152 */
153 result = ifnet_output_raw(ifp, np->nd_proto_family, m);
154
155 socket_lock(so, 0);
156
157 return (result);
158 }
159
160 /* Our input routine called from DLIL */
161 static errno_t
162 ndrv_input(
163 ifnet_t ifp,
164 protocol_family_t proto_family,
165 mbuf_t m,
166 char *frame_header)
167 {
168 struct socket *so;
169 struct sockaddr_dl ndrvsrc;
170 struct ndrv_cb *np;
171 int error = 0;
172
173 ndrvsrc.sdl_len = sizeof (struct sockaddr_dl);
174 ndrvsrc.sdl_family = AF_NDRV;
175 ndrvsrc.sdl_index = 0;
176
177 /* move packet from if queue to socket */
178 /* Should be media-independent */
179 ndrvsrc.sdl_type = IFT_ETHER;
180 ndrvsrc.sdl_nlen = 0;
181 ndrvsrc.sdl_alen = 6;
182 ndrvsrc.sdl_slen = 0;
183 bcopy(frame_header, &ndrvsrc.sdl_data, 6);
184
185 np = ndrv_find_inbound(ifp, proto_family);
186 if (np == NULL)
187 {
188 return(ENOENT);
189 }
190 so = np->nd_socket;
191 /* prepend the frame header */
192 m = m_prepend(m, ifnet_hdrlen(ifp), M_NOWAIT);
193 if (m == NULL)
194 return EJUSTRETURN;
195 bcopy(frame_header, m->m_data, ifnet_hdrlen(ifp));
196
197 lck_mtx_assert(ndrvdomain->dom_mtx, LCK_MTX_ASSERT_NOTOWNED);
198 lck_mtx_lock(ndrvdomain->dom_mtx);
199 if (sbappendaddr(&(so->so_rcv), (struct sockaddr *)&ndrvsrc,
200 m, (struct mbuf *)0, &error) != 0) {
201 sorwakeup(so);
202 }
203 lck_mtx_unlock(ndrvdomain->dom_mtx);
204 return 0; /* radar 4030377 - always return 0 */
205 }
206
207 /*
208 * Allocate an ndrv control block and some buffer space for the socket
209 */
210 static int
211 ndrv_attach(struct socket *so, int proto, __unused struct proc *p)
212 {
213 int error;
214 struct ndrv_cb *np = sotondrvcb(so);
215
216 if ((so->so_state & SS_PRIV) == 0)
217 return(EPERM);
218
219 #if NDRV_DEBUG
220 printf("NDRV attach: %x, %x, %x\n", so, proto, np);
221 #endif
222
223 if ((error = soreserve(so, ndrv_sendspace, ndrv_recvspace)))
224 return(error);
225
226 MALLOC(np, struct ndrv_cb *, sizeof(*np), M_PCB, M_WAITOK);
227 if (np == NULL)
228 return (ENOMEM);
229 so->so_pcb = (caddr_t)np;
230 bzero(np, sizeof(*np));
231 #if NDRV_DEBUG
232 printf("NDRV attach: %x, %x, %x\n", so, proto, np);
233 #endif
234 TAILQ_INIT(&np->nd_dlist);
235 np->nd_signature = NDRV_SIGNATURE;
236 np->nd_socket = so;
237 np->nd_proto.sp_family = SOCK_DOM(so);
238 np->nd_proto.sp_protocol = proto;
239 np->nd_if = NULL;
240 np->nd_proto_family = 0;
241 np->nd_family = 0;
242 np->nd_unit = 0;
243 TAILQ_INSERT_TAIL(&ndrvl, np, nd_next);
244 return(0);
245 }
246
247 /*
248 * Destroy state just before socket deallocation.
249 * Flush data or not depending on the options.
250 */
251
252 static int
253 ndrv_detach(struct socket *so)
254 {
255 struct ndrv_cb *np = sotondrvcb(so);
256
257 if (np == 0)
258 return EINVAL;
259 return ndrv_do_detach(np);
260 }
261
262
263 /*
264 * If a socket isn't bound to a single address,
265 * the ndrv input routine will hand it anything
266 * within that protocol family (assuming there's
267 * nothing else around it should go to).
268 *
269 * Don't expect this to be used.
270 */
271
272 static int
273 ndrv_connect(struct socket *so, struct sockaddr *nam, __unused struct proc *p)
274 {
275 struct ndrv_cb *np = sotondrvcb(so);
276
277 if (np == 0)
278 return EINVAL;
279
280 if (np->nd_faddr)
281 return EISCONN;
282
283 /* Allocate memory to store the remote address */
284 MALLOC(np->nd_faddr, struct sockaddr_ndrv*,
285 nam->sa_len, M_IFADDR, M_WAITOK);
286 if (np->nd_faddr == NULL)
287 return ENOMEM;
288
289 bcopy((caddr_t) nam, (caddr_t) np->nd_faddr, nam->sa_len);
290 soisconnected(so);
291 return 0;
292 }
293
294 static void
295 ndrv_event(struct ifnet *ifp, __unused protocol_family_t protocol,
296 const struct kev_msg *event)
297 {
298 if (event->vendor_code == KEV_VENDOR_APPLE &&
299 event->kev_class == KEV_NETWORK_CLASS &&
300 event->kev_subclass == KEV_DL_SUBCLASS &&
301 event->event_code == KEV_DL_IF_DETACHING) {
302 lck_mtx_assert(ndrvdomain->dom_mtx, LCK_MTX_ASSERT_NOTOWNED);
303 lck_mtx_lock(ndrvdomain->dom_mtx);
304 ndrv_handle_ifp_detach(ifnet_family(ifp), ifnet_unit(ifp));
305 lck_mtx_unlock(ndrvdomain->dom_mtx);
306 }
307 }
308
309 static int name_cmp(struct ifnet *, char *);
310
311 /*
312 * This is the "driver open" hook - we 'bind' to the
313 * named driver.
314 * Here's where we latch onto the driver.
315 */
316 static int
317 ndrv_bind(struct socket *so, struct sockaddr *nam, __unused struct proc *p)
318 {
319 struct sockaddr_ndrv *sa = (struct sockaddr_ndrv *) nam;
320 char *dname;
321 struct ndrv_cb *np;
322 struct ifnet *ifp;
323 int result;
324
325 if TAILQ_EMPTY(&ifnet_head)
326 return(EADDRNOTAVAIL); /* Quick sanity check */
327 np = sotondrvcb(so);
328 if (np == 0)
329 return EINVAL;
330
331 if (np->nd_laddr)
332 return EINVAL; /* XXX */
333
334 /* I think we just latch onto a copy here; the caller frees */
335 np->nd_laddr = _MALLOC(sizeof(struct sockaddr_ndrv), M_IFADDR, M_WAITOK);
336 if (np->nd_laddr == NULL)
337 return(ENOMEM);
338 bcopy((caddr_t) sa, (caddr_t) np->nd_laddr, sizeof(struct sockaddr_ndrv));
339 dname = (char *) sa->snd_name;
340 np->nd_laddr->snd_len = sizeof(struct sockaddr_ndrv);
341 if (*dname == '\0')
342 return(EINVAL);
343 #if NDRV_DEBUG
344 printf("NDRV bind: %x, %x, %s\n", so, np, dname);
345 #endif
346 /* Track down the driver and its ifnet structure.
347 * There's no internal call for this so we have to dup the code
348 * in if.c/ifconf()
349 */
350 ifnet_head_lock_shared();
351 TAILQ_FOREACH(ifp, &ifnet_head, if_link) {
352 if (name_cmp(ifp, dname) == 0)
353 break;
354 }
355 ifnet_head_done();
356
357 if (ifp == NULL)
358 return(EADDRNOTAVAIL);
359
360 // PPP doesn't support PF_NDRV.
361 if (ifnet_family(ifp) != APPLE_IF_FAM_PPP)
362 {
363 /* NDRV on this interface */
364 struct ifnet_attach_proto_param ndrv_proto;
365 result = 0;
366 bzero(&ndrv_proto, sizeof(ndrv_proto));
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 = ifnet_attach_protocol(ifp, PF_NDRV, &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 = ifnet_family(ifp);
384 np->nd_unit = ifnet_unit(ifp);
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(ndrvdomain->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 != 0 || 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 printf("NDRV detach: %x, %x\n", so, np);
542 #endif
543 ndrv_remove_all_multicast(np);
544
545 ifp = np->nd_if;
546 /* Remove from the linked list of control blocks */
547 TAILQ_REMOVE(&ndrvl, np, nd_next);
548 if (ifp != NULL) {
549 u_int32_t proto_family = np->nd_proto_family;
550
551 if (proto_family != PF_NDRV && proto_family != 0) {
552 socket_unlock(so, 0);
553 ifnet_detach_protocol(ifp, proto_family);
554 socket_lock(so, 0);
555 }
556
557 /* Check if this is the last socket attached to this interface */
558 TAILQ_FOREACH(cur_np, &ndrvl, nd_next) {
559 if (cur_np->nd_family == np->nd_family &&
560 cur_np->nd_unit == np->nd_unit) {
561 break;
562 }
563 }
564
565 /* If there are no other interfaces, detach PF_NDRV from the interface */
566 if (cur_np == NULL) {
567 socket_unlock(so, 0);
568 ifnet_detach_protocol(ifp, PF_NDRV);
569 socket_lock(so, 0);
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 printf("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 /*
596 * A multipath subflow socket would have its SS_NOFDREF set by default,
597 * so check for SOF_MP_SUBFLOW socket flag before detaching the PCB;
598 * when the socket is closed for real, SOF_MP_SUBFLOW would be cleared.
599 */
600 if (!(so->so_flags & SOF_MP_SUBFLOW) && (so->so_state & SS_NOFDREF))
601 ndrv_do_detach(np);
602 soisdisconnected(so);
603 return(0);
604 }
605
606 /* Hackery - return a string version of a decimal number */
607 static void
608 sprint_d(u_int n, char *buf, int buflen)
609 { char dbuf[IFNAMSIZ];
610 char *cp = dbuf+IFNAMSIZ-1;
611
612 *cp = 0;
613 do { buflen--;
614 cp--;
615 *cp = "0123456789"[n % 10];
616 n /= 10;
617 } while (n != 0 && buflen > 0);
618 strlcpy(buf, cp, IFNAMSIZ-buflen);
619 return;
620 }
621
622 /*
623 * Try to compare a device name (q) with one of the funky ifnet
624 * device names (ifp).
625 */
626 static int name_cmp(struct ifnet *ifp, char *q)
627 { char *r;
628 int len;
629 char buf[IFNAMSIZ];
630
631 r = buf;
632 len = strlen(ifnet_name(ifp));
633 strlcpy(r, ifnet_name(ifp), IFNAMSIZ);
634 r += len;
635 sprint_d(ifnet_unit(ifp), r, IFNAMSIZ-(r-buf));
636 #if NDRV_DEBUG
637 printf("Comparing %s, %s\n", buf, q);
638 #endif
639 return(strncmp(buf, q, IFNAMSIZ));
640 }
641
642 #if 0
643 //### Not used
644 /*
645 * When closing, dump any enqueued mbufs.
646 */
647 void
648 ndrv_flushq(struct ifqueue *q)
649 {
650 struct mbuf *m;
651 for (;;)
652 {
653 IF_DEQUEUE(q, m);
654 if (m == NULL)
655 break;
656 IF_DROP(q);
657 if (m)
658 m_freem(m);
659 }
660 }
661 #endif
662
663 int
664 ndrv_setspec(struct ndrv_cb *np, struct sockopt *sopt)
665 {
666 struct ifnet_attach_proto_param proto_param;
667 struct ndrv_protocol_desc ndrvSpec;
668 struct ndrv_demux_desc* ndrvDemux = NULL;
669 int error = 0;
670 struct socket * so = np->nd_socket;
671 user_addr_t user_addr;
672
673 /* Sanity checking */
674 if (np->nd_proto_family != PF_NDRV)
675 return EBUSY;
676 if (np->nd_if == NULL)
677 return EINVAL;
678
679 /* Copy the ndrvSpec */
680 if (proc_is64bit(sopt->sopt_p)) {
681 struct ndrv_protocol_desc64 ndrvSpec64;
682
683 if (sopt->sopt_valsize != sizeof(ndrvSpec64))
684 return EINVAL;
685
686 error = sooptcopyin(sopt, &ndrvSpec64, sizeof(ndrvSpec64), sizeof(ndrvSpec64));
687 if (error != 0)
688 return error;
689
690 ndrvSpec.version = ndrvSpec64.version;
691 ndrvSpec.protocol_family = ndrvSpec64.protocol_family;
692 ndrvSpec.demux_count = ndrvSpec64.demux_count;
693
694 user_addr = ndrvSpec64.demux_list;
695 }
696 else {
697 struct ndrv_protocol_desc32 ndrvSpec32;
698
699 if (sopt->sopt_valsize != sizeof(ndrvSpec32))
700 return EINVAL;
701
702 error = sooptcopyin(sopt, &ndrvSpec32, sizeof(ndrvSpec32), sizeof(ndrvSpec32));
703 if (error != 0)
704 return error;
705
706 ndrvSpec.version = ndrvSpec32.version;
707 ndrvSpec.protocol_family = ndrvSpec32.protocol_family;
708 ndrvSpec.demux_count = ndrvSpec32.demux_count;
709
710 user_addr = CAST_USER_ADDR_T(ndrvSpec32.demux_list);
711 }
712
713 /* Verify the parameter */
714 if (ndrvSpec.version > NDRV_PROTOCOL_DESC_VERS)
715 return ENOTSUP; // version is too new!
716 else if (ndrvSpec.version < 1)
717 return EINVAL; // version is not valid
718 else if (ndrvSpec.demux_count > NDRV_PROTODEMUX_COUNT || ndrvSpec.demux_count == 0)
719 return EINVAL; // demux_count is not valid
720
721 bzero(&proto_param, sizeof(proto_param));
722 proto_param.demux_count = ndrvSpec.demux_count;
723
724 /* Allocate storage for demux array */
725 MALLOC(ndrvDemux, struct ndrv_demux_desc*, proto_param.demux_count *
726 sizeof(struct ndrv_demux_desc), M_TEMP, M_WAITOK);
727 if (ndrvDemux == NULL)
728 return ENOMEM;
729
730 /* Allocate enough ifnet_demux_descs */
731 MALLOC(proto_param.demux_array, struct ifnet_demux_desc*,
732 sizeof(*proto_param.demux_array) * ndrvSpec.demux_count,
733 M_TEMP, M_WAITOK);
734 if (proto_param.demux_array == NULL)
735 error = ENOMEM;
736
737 if (error == 0)
738 {
739 /* Copy the ndrv demux array from userland */
740 error = copyin(user_addr, ndrvDemux,
741 ndrvSpec.demux_count * sizeof(struct ndrv_demux_desc));
742 ndrvSpec.demux_list = ndrvDemux;
743 }
744
745 if (error == 0)
746 {
747 /* At this point, we've at least got enough bytes to start looking around */
748 u_int32_t demuxOn = 0;
749
750 proto_param.demux_count = ndrvSpec.demux_count;
751 proto_param.input = ndrv_input;
752 proto_param.event = ndrv_event;
753
754 for (demuxOn = 0; demuxOn < ndrvSpec.demux_count; demuxOn++)
755 {
756 /* Convert an ndrv_demux_desc to a ifnet_demux_desc */
757 error = ndrv_to_ifnet_demux(&ndrvSpec.demux_list[demuxOn],
758 &proto_param.demux_array[demuxOn]);
759 if (error)
760 break;
761 }
762 }
763
764 if (error == 0)
765 {
766 /* We've got all our ducks lined up...lets attach! */
767 socket_unlock(so, 0);
768 error = ifnet_attach_protocol(np->nd_if, ndrvSpec.protocol_family,
769 &proto_param);
770 socket_lock(so, 0);
771 if (error == 0)
772 np->nd_proto_family = ndrvSpec.protocol_family;
773 }
774
775 /* Free any memory we've allocated */
776 if (proto_param.demux_array)
777 FREE(proto_param.demux_array, M_TEMP);
778 if (ndrvDemux)
779 FREE(ndrvDemux, M_TEMP);
780
781 return error;
782 }
783
784
785 int
786 ndrv_to_ifnet_demux(struct ndrv_demux_desc* ndrv, struct ifnet_demux_desc* ifdemux)
787 {
788 bzero(ifdemux, sizeof(*ifdemux));
789
790 if (ndrv->type < DLIL_DESC_ETYPE2)
791 {
792 /* using old "type", not supported */
793 return ENOTSUP;
794 }
795
796 if (ndrv->length > 28)
797 {
798 return EINVAL;
799 }
800
801 ifdemux->type = ndrv->type;
802 ifdemux->data = ndrv->data.other;
803 ifdemux->datalen = ndrv->length;
804
805 return 0;
806 }
807
808 int
809 ndrv_delspec(struct ndrv_cb *np)
810 {
811 int result = 0;
812
813 if (np->nd_proto_family == PF_NDRV ||
814 np->nd_proto_family == 0)
815 return EINVAL;
816
817 /* Detach the protocol */
818 result = ifnet_detach_protocol(np->nd_if, np->nd_proto_family);
819 np->nd_proto_family = PF_NDRV;
820
821 return result;
822 }
823
824 struct ndrv_cb *
825 ndrv_find_inbound(struct ifnet *ifp, u_int32_t protocol)
826 {
827 struct ndrv_cb* np;
828
829 if (protocol == PF_NDRV) return NULL;
830
831 TAILQ_FOREACH(np, &ndrvl, nd_next) {
832 if (np->nd_proto_family == protocol &&
833 np->nd_if == ifp) {
834 return np;
835 }
836 }
837
838 return NULL;
839 }
840
841 static void
842 ndrv_handle_ifp_detach(u_int32_t family, short unit)
843 {
844 struct ndrv_cb* np;
845 struct ifnet *ifp = NULL;
846 struct socket *so;
847
848 /* Find all sockets using this interface. */
849 TAILQ_FOREACH(np, &ndrvl, nd_next) {
850 if (np->nd_family == family &&
851 np->nd_unit == unit)
852 {
853 /* This cb is using the detaching interface, but not for long. */
854 /* Let the protocol go */
855 ifp = np->nd_if;
856 if (np->nd_proto_family != 0)
857 ndrv_delspec(np);
858
859 /* Delete the multicasts first */
860 ndrv_remove_all_multicast(np);
861
862 /* Disavow all knowledge of the ifp */
863 np->nd_if = NULL;
864 np->nd_unit = 0;
865 np->nd_family = 0;
866
867 so = np->nd_socket;
868 /* Make sure sending returns an error */
869 lck_mtx_assert(ndrvdomain->dom_mtx, LCK_MTX_ASSERT_OWNED);
870 socantsendmore(so);
871 socantrcvmore(so);
872 }
873 }
874
875 /* Unregister our protocol */
876 if (ifp) {
877 ifnet_detach_protocol(ifp, PF_NDRV);
878 }
879 }
880
881 static int
882 ndrv_do_add_multicast(struct ndrv_cb *np, struct sockopt *sopt)
883 {
884 struct ndrv_multiaddr* ndrv_multi;
885 int result;
886
887 if (sopt->sopt_val == 0 || sopt->sopt_valsize < 2 ||
888 sopt->sopt_level != SOL_NDRVPROTO || sopt->sopt_valsize > SOCK_MAXADDRLEN)
889 return EINVAL;
890 if (np->nd_if == NULL)
891 return ENXIO;
892 if (!(np->nd_dlist_cnt < ndrv_multi_max_count))
893 return EPERM;
894
895 // Allocate storage
896 MALLOC(ndrv_multi, struct ndrv_multiaddr*, sizeof(struct ndrv_multiaddr) -
897 sizeof(struct sockaddr) + sopt->sopt_valsize, M_IFADDR, M_WAITOK);
898 if (ndrv_multi == NULL)
899 return ENOMEM;
900
901 // Copy in the address
902 result = copyin(sopt->sopt_val, &ndrv_multi->addr, sopt->sopt_valsize);
903
904 // Validate the sockaddr
905 if (result == 0 && sopt->sopt_valsize != ndrv_multi->addr.sa_len)
906 result = EINVAL;
907
908 if (result == 0 && ndrv_have_multicast(np, &ndrv_multi->addr))
909 result = EEXIST;
910
911 if (result == 0)
912 {
913 // Try adding the multicast
914 result = ifnet_add_multicast(np->nd_if, &ndrv_multi->addr,
915 &ndrv_multi->ifma);
916 }
917
918 if (result == 0)
919 {
920 // Add to our linked list
921 ndrv_multi->next = np->nd_multiaddrs;
922 np->nd_multiaddrs = ndrv_multi;
923 np->nd_dlist_cnt++;
924 }
925 else
926 {
927 // Free up the memory, something went wrong
928 FREE(ndrv_multi, M_IFADDR);
929 }
930
931 return result;
932 }
933
934 static int
935 ndrv_do_remove_multicast(struct ndrv_cb *np, struct sockopt *sopt)
936 {
937 struct sockaddr* multi_addr;
938 struct ndrv_multiaddr* ndrv_entry = NULL;
939 int result;
940
941 if (sopt->sopt_val == 0 || sopt->sopt_valsize < 2 ||
942 sopt->sopt_level != SOL_NDRVPROTO)
943 return EINVAL;
944 if (np->nd_if == NULL || np->nd_dlist_cnt == 0)
945 return ENXIO;
946
947 // Allocate storage
948 MALLOC(multi_addr, struct sockaddr*, sopt->sopt_valsize,
949 M_TEMP, M_WAITOK);
950 if (multi_addr == NULL)
951 return ENOMEM;
952
953 // Copy in the address
954 result = copyin(sopt->sopt_val, multi_addr, sopt->sopt_valsize);
955
956 // Validate the sockaddr
957 if (result == 0 && sopt->sopt_valsize != multi_addr->sa_len)
958 result = EINVAL;
959
960 if (result == 0)
961 {
962 /* Find the old entry */
963 ndrv_entry = ndrv_have_multicast(np, multi_addr);
964
965 if (ndrv_entry == NULL)
966 result = ENOENT;
967 }
968
969 if (result == 0)
970 {
971 // Try deleting the multicast
972 result = ifnet_remove_multicast(ndrv_entry->ifma);
973 }
974
975 if (result == 0)
976 {
977 // Remove from our linked list
978 struct ndrv_multiaddr* cur = np->nd_multiaddrs;
979
980 ifmaddr_release(ndrv_entry->ifma);
981
982 if (cur == ndrv_entry)
983 {
984 np->nd_multiaddrs = cur->next;
985 }
986 else
987 {
988 for (cur = cur->next; cur != NULL; cur = cur->next)
989 {
990 if (cur->next == ndrv_entry)
991 {
992 cur->next = cur->next->next;
993 break;
994 }
995 }
996 }
997
998 np->nd_dlist_cnt--;
999
1000 // Free the memory
1001 FREE(ndrv_entry, M_IFADDR);
1002 }
1003 FREE(multi_addr, M_TEMP);
1004
1005 return result;
1006 }
1007
1008 static struct ndrv_multiaddr*
1009 ndrv_have_multicast(struct ndrv_cb *np, struct sockaddr* inAddr)
1010 {
1011 struct ndrv_multiaddr* cur;
1012 for (cur = np->nd_multiaddrs; cur != NULL; cur = cur->next)
1013 {
1014
1015 if ((inAddr->sa_len == cur->addr.sa_len) &&
1016 (bcmp(&cur->addr, inAddr, inAddr->sa_len) == 0))
1017 {
1018 // Found a match
1019 return cur;
1020 }
1021 }
1022
1023 return NULL;
1024 }
1025
1026 static void
1027 ndrv_remove_all_multicast(struct ndrv_cb* np)
1028 {
1029 struct ndrv_multiaddr* cur;
1030
1031 if (np->nd_if != NULL)
1032 {
1033 while (np->nd_multiaddrs != NULL)
1034 {
1035 cur = np->nd_multiaddrs;
1036 np->nd_multiaddrs = cur->next;
1037
1038 ifnet_remove_multicast(cur->ifma);
1039 ifmaddr_release(cur->ifma);
1040 FREE(cur, M_IFADDR);
1041 }
1042 }
1043 }
1044
1045 static struct pr_usrreqs ndrv_usrreqs = {
1046 .pru_abort = ndrv_abort,
1047 .pru_attach = ndrv_attach,
1048 .pru_bind = ndrv_bind,
1049 .pru_connect = ndrv_connect,
1050 .pru_detach = ndrv_detach,
1051 .pru_disconnect = ndrv_disconnect,
1052 .pru_peeraddr = ndrv_peeraddr,
1053 .pru_send = ndrv_send,
1054 .pru_shutdown = ndrv_shutdown,
1055 .pru_sockaddr = ndrv_sockaddr,
1056 .pru_sosend = sosend,
1057 .pru_soreceive = soreceive,
1058 };
1059
1060 static struct protosw ndrvsw[] = {
1061 {
1062 .pr_type = SOCK_RAW,
1063 .pr_protocol = NDRVPROTO_NDRV,
1064 .pr_flags = PR_ATOMIC|PR_ADDR,
1065 .pr_output = ndrv_output,
1066 .pr_ctloutput = ndrv_ctloutput,
1067 .pr_usrreqs = &ndrv_usrreqs,
1068 }
1069 };
1070
1071 static int ndrv_proto_count = (sizeof (ndrvsw) / sizeof (struct protosw));
1072
1073 struct domain ndrvdomain_s = {
1074 .dom_family = PF_NDRV,
1075 .dom_name = "NetDriver",
1076 .dom_init = ndrv_dominit,
1077 };
1078
1079 static void
1080 ndrv_dominit(struct domain *dp)
1081 {
1082 struct protosw *pr;
1083 int i;
1084
1085 VERIFY(!(dp->dom_flags & DOM_INITIALIZED));
1086 VERIFY(ndrvdomain == NULL);
1087
1088 ndrvdomain = dp;
1089
1090 for (i = 0, pr = &ndrvsw[0]; i < ndrv_proto_count; i++, pr++)
1091 net_add_proto(pr, dp, 1);
1092 }