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