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