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