]> git.saurik.com Git - apple/xnu.git/blame - bsd/net/ndrv.c
xnu-1699.22.73.tar.gz
[apple/xnu.git] / bsd / net / ndrv.c
CommitLineData
1c79356b 1/*
b0d623f7 2 * Copyright (c) 1997-2008 Apple Inc. All rights reserved.
5d5c5d0d 3 *
2d21ac55 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
1c79356b 5 *
2d21ac55
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.
8f6c56a5 14 *
2d21ac55
A
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
8f6c56a5
A
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
2d21ac55
A
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.
8f6c56a5 25 *
2d21ac55 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
1c79356b 27 */
1c79356b
A
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
2d21ac55 39 * protocol packets. This lets ndrv_output() call ifnet_output(), and
1c79356b
A
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
b0d623f7 46 * strange protocol support.
1c79356b 47 */
7b1edb79 48#include <mach/mach_types.h>
1c79356b
A
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/errno.h>
61#include <sys/syslog.h>
62#include <sys/proc.h>
63
64#include <kern/queue.h>
65
7b1edb79 66#include <net/ndrv.h>
1c79356b
A
67#include <net/route.h>
68#include <net/if_llc.h>
69#include <net/if_dl.h>
70#include <net/if_types.h>
7b1edb79 71#include <net/ndrv_var.h>
2d21ac55 72#include <net/dlil.h>
1c79356b
A
73
74#if INET
75#include <netinet/in.h>
76#include <netinet/in_var.h>
77#endif
78#include <netinet/if_ether.h>
79
1c79356b
A
80#include <machine/spl.h>
81
91447636
A
82static int ndrv_do_detach(struct ndrv_cb *);
83static int ndrv_do_disconnect(struct ndrv_cb *);
b0d623f7 84static struct ndrv_cb *ndrv_find_inbound(struct ifnet *ifp, u_int32_t protocol_family);
91447636
A
85static int ndrv_setspec(struct ndrv_cb *np, struct sockopt *sopt);
86static int ndrv_delspec(struct ndrv_cb *);
2d21ac55 87static int ndrv_to_ifnet_demux(struct ndrv_demux_desc* ndrv, struct ifnet_demux_desc* ifdemux);
b0d623f7 88static void ndrv_handle_ifp_detach(u_int32_t family, short unit);
9bccf70c
A
89static int ndrv_do_add_multicast(struct ndrv_cb *np, struct sockopt *sopt);
90static int ndrv_do_remove_multicast(struct ndrv_cb *np, struct sockopt *sopt);
91static struct ndrv_multiaddr* ndrv_have_multicast(struct ndrv_cb *np, struct sockaddr* addr);
92static void ndrv_remove_all_multicast(struct ndrv_cb *np);
2d21ac55 93static void ndrv_dominit(void) __attribute__((section("__TEXT, initcode")));
1c79356b 94
b0d623f7
A
95u_int32_t ndrv_sendspace = NDRVSNDQ;
96u_int32_t 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
6d2010ae
A
103#define NDRV_PROTODEMUX_COUNT 10
104
1c79356b 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
2d21ac55 142 /* Unlock before calling ifnet_output */
91447636
A
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 */
2d21ac55 149 result = ifnet_output_raw(ifp, np->nd_proto_family, m);
91447636
A
150
151 socket_lock(so, 0);
152
7b1edb79
A
153 return (result);
154}
155
156/* Our input routine called from DLIL */
2d21ac55
A
157static errno_t
158ndrv_input(
159 ifnet_t ifp,
160 protocol_family_t proto_family,
161 mbuf_t m,
162 char *frame_header)
7b1edb79 163{
1c79356b 164 struct socket *so;
2d21ac55 165 struct sockaddr_dl ndrvsrc;
91447636
A
166 struct ndrv_cb *np;
167 int error = 0;
1c79356b 168
2d21ac55
A
169 ndrvsrc.sdl_len = sizeof (struct sockaddr_dl);
170 ndrvsrc.sdl_family = AF_NDRV;
171 ndrvsrc.sdl_index = 0;
1c79356b 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 */
2d21ac55 188 m = m_prepend(m, ifnet_hdrlen(ifp), M_NOWAIT);
7b1edb79
A
189 if (m == NULL)
190 return EJUSTRETURN;
2d21ac55 191 bcopy(frame_header, m->m_data, ifnet_hdrlen(ifp));
91447636
A
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);
1c79356b
A
272
273 if (np == 0)
274 return EINVAL;
275
276 if (np->nd_faddr)
277 return EISCONN;
7b1edb79 278
b0d623f7
A
279 /* Allocate memory to store the remote address */
280 MALLOC(np->nd_faddr, struct sockaddr_ndrv*,
7b1edb79 281 nam->sa_len, M_IFADDR, M_WAITOK);
b0d623f7
A
282 if (np->nd_faddr == NULL)
283 return ENOMEM;
7b1edb79
A
284
285 bcopy((caddr_t) nam, (caddr_t) np->nd_faddr, nam->sa_len);
1c79356b
A
286 soisconnected(so);
287 return 0;
288}
289
91447636 290static void
2d21ac55
A
291ndrv_event(struct ifnet *ifp, __unused protocol_family_t protocol,
292 const struct kev_msg *event)
91447636
A
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) {
0c530ab8
A
298 lck_mtx_assert(ndrvdomain.dom_mtx, LCK_MTX_ASSERT_NOTOWNED);
299 lck_mtx_lock(ndrvdomain.dom_mtx);
2d21ac55 300 ndrv_handle_ifp_detach(ifnet_family(ifp), ifnet_unit(ifp));
0c530ab8 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));
2d21ac55 335 dname = (char *) sa->snd_name;
1c79356b
A
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.
2d21ac55 356 if (ifnet_family(ifp) != APPLE_IF_FAM_PPP)
91447636
A
357 {
358 /* NDRV on this interface */
2d21ac55 359 struct ifnet_attach_proto_param ndrv_proto;
91447636
A
360 result = 0;
361 bzero(&ndrv_proto, sizeof(ndrv_proto));
91447636
A
362 ndrv_proto.event = ndrv_event;
363
364 /* We aren't worried about double attaching, that should just return an error */
0c530ab8 365 socket_unlock(so, 0);
2d21ac55 366 result = ifnet_attach_protocol(ifp, PF_NDRV, &ndrv_proto);
0c530ab8 367 socket_lock(so, 0);
91447636
A
368 if (result && result != EEXIST) {
369 return result;
370 }
371 np->nd_proto_family = PF_NDRV;
372 }
373 else {
374 np->nd_proto_family = 0;
375 }
7b1edb79 376
1c79356b 377 np->nd_if = ifp;
2d21ac55
A
378 np->nd_family = ifnet_family(ifp);
379 np->nd_unit = ifnet_unit(ifp);
7b1edb79 380
1c79356b
A
381 return(0);
382}
383
91447636 384static int
1c79356b
A
385ndrv_disconnect(struct socket *so)
386{
91447636 387 struct ndrv_cb *np = sotondrvcb(so);
1c79356b
A
388
389 if (np == 0)
390 return EINVAL;
391
392 if (np->nd_faddr == 0)
393 return ENOTCONN;
394
395 ndrv_do_disconnect(np);
396 return 0;
397}
398
399/*
400 * Mark the connection as being incapable of further input.
401 */
91447636 402static int
1c79356b
A
403ndrv_shutdown(struct socket *so)
404{
91447636 405 lck_mtx_assert(so->so_proto->pr_domain->dom_mtx, LCK_MTX_ASSERT_OWNED);
1c79356b
A
406 socantsendmore(so);
407 return 0;
408}
409
410/*
411 * Ship a packet out. The ndrv output will pass it
412 * to the appropriate driver. The really tricky part
413 * is the destination address...
414 */
91447636
A
415static int
416ndrv_send(struct socket *so, __unused int flags, struct mbuf *m,
417 __unused struct sockaddr *addr, struct mbuf *control,
418 __unused struct proc *p)
1c79356b
A
419{
420 int error;
421
422 if (control)
423 return EOPNOTSUPP;
424
425 error = ndrv_output(m, so);
426 m = NULL;
427 return error;
428}
429
430
91447636 431static int
1c79356b
A
432ndrv_abort(struct socket *so)
433{
91447636 434 struct ndrv_cb *np = sotondrvcb(so);
1c79356b
A
435
436 if (np == 0)
437 return EINVAL;
438
439 ndrv_do_disconnect(np);
440 return 0;
441}
442
91447636 443static int
1c79356b
A
444ndrv_sockaddr(struct socket *so, struct sockaddr **nam)
445{
91447636 446 struct ndrv_cb *np = sotondrvcb(so);
1c79356b
A
447 int len;
448
449 if (np == 0)
450 return EINVAL;
451
452 if (np->nd_laddr == 0)
453 return EINVAL;
454
455 len = np->nd_laddr->snd_len;
91447636
A
456 MALLOC(*nam, struct sockaddr *, len, M_SONAME, M_WAITOK);
457 if (*nam == NULL)
458 return ENOMEM;
1c79356b
A
459 bcopy((caddr_t)np->nd_laddr, *nam,
460 (unsigned)len);
461 return 0;
462}
463
464
91447636 465static int
1c79356b
A
466ndrv_peeraddr(struct socket *so, struct sockaddr **nam)
467{
91447636 468 struct ndrv_cb *np = sotondrvcb(so);
1c79356b
A
469 int len;
470
471 if (np == 0)
472 return EINVAL;
473
474 if (np->nd_faddr == 0)
475 return ENOTCONN;
476
477 len = np->nd_faddr->snd_len;
91447636
A
478 MALLOC(*nam, struct sockaddr *, len, M_SONAME, M_WAITOK);
479 if (*nam == NULL)
480 return ENOMEM;
1c79356b
A
481 bcopy((caddr_t)np->nd_faddr, *nam,
482 (unsigned)len);
483 return 0;
484}
485
486
1c79356b
A
487/* Control output */
488
91447636 489static int
1c79356b 490ndrv_ctloutput(struct socket *so, struct sockopt *sopt)
7b1edb79 491{
91447636 492 struct ndrv_cb *np = sotondrvcb(so);
7b1edb79
A
493 int error = 0;
494
495 switch(sopt->sopt_name)
496 {
497 case NDRV_DELDMXSPEC: /* Delete current spec */
498 /* Verify no parameter was passed */
2d21ac55 499 if (sopt->sopt_val != 0 || sopt->sopt_valsize != 0) {
7b1edb79
A
500 /*
501 * We don't support deleting a specific demux, it's
502 * all or nothing.
503 */
504 return EINVAL;
505 }
506 error = ndrv_delspec(np);
507 break;
508 case NDRV_SETDMXSPEC: /* Set protocol spec */
509 error = ndrv_setspec(np, sopt);
510 break;
9bccf70c
A
511 case NDRV_ADDMULTICAST:
512 error = ndrv_do_add_multicast(np, sopt);
513 break;
514 case NDRV_DELMULTICAST:
515 error = ndrv_do_remove_multicast(np, sopt);
516 break;
7b1edb79
A
517 default:
518 error = ENOTSUP;
519 }
1c79356b
A
520#ifdef NDRV_DEBUG
521 log(LOG_WARNING, "NDRV CTLOUT: %x returns %d\n", sopt->sopt_name,
522 error);
523#endif
524 return(error);
525}
526
91447636
A
527static int
528ndrv_do_detach(struct ndrv_cb *np)
7b1edb79
A
529{
530 struct ndrv_cb* cur_np = NULL;
531 struct socket *so = np->nd_socket;
55e303ae 532 int error = 0;
0c530ab8 533 struct ifnet * ifp;
1c79356b
A
534
535#if NDRV_DEBUG
536 kprintf("NDRV detach: %x, %x\n", so, np);
537#endif
9bccf70c 538 ndrv_remove_all_multicast(np);
2d21ac55 539
0c530ab8
A
540 ifp = np->nd_if;
541 /* Remove from the linked list of control blocks */
542 TAILQ_REMOVE(&ndrvl, np, nd_next);
543 if (ifp != NULL) {
b0d623f7 544 u_int32_t proto_family = np->nd_proto_family;
0c530ab8
A
545
546 if (proto_family != PF_NDRV && proto_family != 0) {
547 socket_unlock(so, 0);
2d21ac55 548 ifnet_detach_protocol(ifp, proto_family);
0c530ab8 549 socket_lock(so, 0);
91447636
A
550 }
551
91447636
A
552 /* Check if this is the last socket attached to this interface */
553 TAILQ_FOREACH(cur_np, &ndrvl, nd_next) {
554 if (cur_np->nd_family == np->nd_family &&
555 cur_np->nd_unit == np->nd_unit) {
556 break;
557 }
558 }
559
560 /* If there are no other interfaces, detach PF_NDRV from the interface */
561 if (cur_np == NULL) {
0c530ab8 562 socket_unlock(so, 0);
2d21ac55 563 ifnet_detach_protocol(ifp, PF_NDRV);
0c530ab8 564 socket_lock(so, 0);
91447636
A
565 }
566 }
0c530ab8
A
567 if (np->nd_laddr != NULL) {
568 FREE((caddr_t)np->nd_laddr, M_IFADDR);
569 np->nd_laddr = NULL;
570 }
1c79356b
A
571 FREE((caddr_t)np, M_PCB);
572 so->so_pcb = 0;
91447636 573 so->so_flags |= SOF_PCBCLEARING;
1c79356b 574 sofree(so);
7b1edb79 575 return error;
1c79356b
A
576}
577
91447636
A
578static int
579ndrv_do_disconnect(struct ndrv_cb *np)
1c79356b 580{
0c530ab8 581 struct socket * so = np->nd_socket;
1c79356b
A
582#if NDRV_DEBUG
583 kprintf("NDRV disconnect: %x\n", np);
584#endif
585 if (np->nd_faddr)
7b1edb79
A
586 {
587 FREE(np->nd_faddr, M_IFADDR);
1c79356b
A
588 np->nd_faddr = 0;
589 }
0c530ab8 590 if (so->so_state & SS_NOFDREF)
1c79356b 591 ndrv_do_detach(np);
0c530ab8 592 soisdisconnected(so);
1c79356b
A
593 return(0);
594}
595
91447636
A
596/* Hackery - return a string version of a decimal number */
597static char *
598sprint_d(u_int n, char *buf, int buflen)
599{ char dbuf[IFNAMSIZ];
600 char *cp = dbuf+IFNAMSIZ-1;
601
602 *cp = 0;
603 do { buflen--;
604 cp--;
605 *cp = "0123456789"[n % 10];
606 n /= 10;
607 } while (n != 0 && buflen > 0);
608 strncpy(buf, cp, IFNAMSIZ-buflen);
609 return (cp);
610}
611
1c79356b
A
612/*
613 * Try to compare a device name (q) with one of the funky ifnet
614 * device names (ifp).
615 */
91447636
A
616static int name_cmp(struct ifnet *ifp, char *q)
617{ char *r;
618 int len;
1c79356b 619 char buf[IFNAMSIZ];
1c79356b
A
620
621 r = buf;
2d21ac55
A
622 len = strlen(ifnet_name(ifp));
623 strncpy(r, ifnet_name(ifp), IFNAMSIZ);
1c79356b 624 r += len;
2d21ac55 625 (void)sprint_d(ifnet_unit(ifp), r, IFNAMSIZ-(r-buf));
1c79356b
A
626#if NDRV_DEBUG
627 kprintf("Comparing %s, %s\n", buf, q);
628#endif
629 return(strncmp(buf, q, IFNAMSIZ));
630}
631
91447636
A
632#if 0
633//### Not used
1c79356b
A
634/*
635 * When closing, dump any enqueued mbufs.
636 */
637void
91447636 638ndrv_flushq(struct ifqueue *q)
7b1edb79 639{
91447636 640 struct mbuf *m;
1c79356b 641 for (;;)
7b1edb79 642 {
1c79356b
A
643 IF_DEQUEUE(q, m);
644 if (m == NULL)
645 break;
646 IF_DROP(q);
1c79356b
A
647 if (m)
648 m_freem(m);
649 }
1c79356b 650}
91447636 651#endif
1c79356b
A
652
653int
7b1edb79
A
654ndrv_setspec(struct ndrv_cb *np, struct sockopt *sopt)
655{
2d21ac55
A
656 struct ifnet_attach_proto_param proto_param;
657 struct ndrv_protocol_desc ndrvSpec;
658 struct ndrv_demux_desc* ndrvDemux = NULL;
659 int error = 0;
660 struct socket * so = np->nd_socket;
661 user_addr_t user_addr;
662
663 /* Sanity checking */
664 if (np->nd_proto_family != PF_NDRV)
665 return EBUSY;
666 if (np->nd_if == NULL)
667 return EINVAL;
668
669 /* Copy the ndrvSpec */
b0d623f7 670 if (proc_is64bit(sopt->sopt_p)) {
2d21ac55
A
671 struct ndrv_protocol_desc64 ndrvSpec64;
672
673 if (sopt->sopt_valsize != sizeof(ndrvSpec64))
674 return EINVAL;
675
676 error = sooptcopyin(sopt, &ndrvSpec64, sizeof(ndrvSpec64), sizeof(ndrvSpec64));
677 if (error != 0)
678 return error;
679
680 ndrvSpec.version = ndrvSpec64.version;
681 ndrvSpec.protocol_family = ndrvSpec64.protocol_family;
682 ndrvSpec.demux_count = ndrvSpec64.demux_count;
683
684 user_addr = ndrvSpec64.demux_list;
685 }
686 else {
b0d623f7
A
687 struct ndrv_protocol_desc32 ndrvSpec32;
688
689 if (sopt->sopt_valsize != sizeof(ndrvSpec32))
2d21ac55
A
690 return EINVAL;
691
b0d623f7 692 error = sooptcopyin(sopt, &ndrvSpec32, sizeof(ndrvSpec32), sizeof(ndrvSpec32));
2d21ac55
A
693 if (error != 0)
694 return error;
695
b0d623f7
A
696 ndrvSpec.version = ndrvSpec32.version;
697 ndrvSpec.protocol_family = ndrvSpec32.protocol_family;
698 ndrvSpec.demux_count = ndrvSpec32.demux_count;
699
700 user_addr = CAST_USER_ADDR_T(ndrvSpec32.demux_list);
2d21ac55
A
701 }
702
703 /* Verify the parameter */
704 if (ndrvSpec.version > NDRV_PROTOCOL_DESC_VERS)
705 return ENOTSUP; // version is too new!
706 else if (ndrvSpec.version < 1)
707 return EINVAL; // version is not valid
6d2010ae
A
708 else if (ndrvSpec.demux_count > NDRV_PROTODEMUX_COUNT || ndrvSpec.demux_count == 0)
709 return EINVAL; // demux_count is not valid
2d21ac55
A
710
711 bzero(&proto_param, sizeof(proto_param));
712 proto_param.demux_count = ndrvSpec.demux_count;
713
714 /* Allocate storage for demux array */
715 MALLOC(ndrvDemux, struct ndrv_demux_desc*, proto_param.demux_count *
716 sizeof(struct ndrv_demux_desc), M_TEMP, M_WAITOK);
717 if (ndrvDemux == NULL)
718 return ENOMEM;
719
720 /* Allocate enough ifnet_demux_descs */
721 MALLOC(proto_param.demux_array, struct ifnet_demux_desc*,
722 sizeof(*proto_param.demux_array) * ndrvSpec.demux_count,
723 M_TEMP, M_WAITOK);
724 if (proto_param.demux_array == NULL)
725 error = ENOMEM;
726
727 if (error == 0)
728 {
729 /* Copy the ndrv demux array from userland */
730 error = copyin(user_addr, ndrvDemux,
731 ndrvSpec.demux_count * sizeof(struct ndrv_demux_desc));
732 ndrvSpec.demux_list = ndrvDemux;
733 }
734
735 if (error == 0)
736 {
737 /* At this point, we've at least got enough bytes to start looking around */
b0d623f7 738 u_int32_t demuxOn = 0;
2d21ac55
A
739
740 proto_param.demux_count = ndrvSpec.demux_count;
741 proto_param.input = ndrv_input;
742 proto_param.event = ndrv_event;
743
744 for (demuxOn = 0; demuxOn < ndrvSpec.demux_count; demuxOn++)
745 {
746 /* Convert an ndrv_demux_desc to a ifnet_demux_desc */
747 error = ndrv_to_ifnet_demux(&ndrvSpec.demux_list[demuxOn],
748 &proto_param.demux_array[demuxOn]);
749 if (error)
750 break;
751 }
752 }
753
754 if (error == 0)
755 {
756 /* We've got all our ducks lined up...lets attach! */
757 socket_unlock(so, 0);
758 error = ifnet_attach_protocol(np->nd_if, ndrvSpec.protocol_family,
759 &proto_param);
760 socket_lock(so, 0);
761 if (error == 0)
762 np->nd_proto_family = ndrvSpec.protocol_family;
763 }
764
765 /* Free any memory we've allocated */
766 if (proto_param.demux_array)
767 FREE(proto_param.demux_array, M_TEMP);
768 if (ndrvDemux)
769 FREE(ndrvDemux, M_TEMP);
770
771 return error;
1c79356b
A
772}
773
1c79356b 774
7b1edb79 775int
2d21ac55 776ndrv_to_ifnet_demux(struct ndrv_demux_desc* ndrv, struct ifnet_demux_desc* ifdemux)
7b1edb79 777{
2d21ac55 778 bzero(ifdemux, sizeof(*ifdemux));
7b1edb79
A
779
780 if (ndrv->type < DLIL_DESC_ETYPE2)
781 {
782 /* using old "type", not supported */
783 return ENOTSUP;
784 }
785
786 if (ndrv->length > 28)
787 {
788 return EINVAL;
789 }
790
2d21ac55
A
791 ifdemux->type = ndrv->type;
792 ifdemux->data = ndrv->data.other;
793 ifdemux->datalen = ndrv->length;
7b1edb79
A
794
795 return 0;
1c79356b
A
796}
797
798int
7b1edb79
A
799ndrv_delspec(struct ndrv_cb *np)
800{
801 int result = 0;
802
91447636
A
803 if (np->nd_proto_family == PF_NDRV ||
804 np->nd_proto_family == 0)
7b1edb79
A
805 return EINVAL;
806
807 /* Detach the protocol */
2d21ac55 808 result = ifnet_detach_protocol(np->nd_if, np->nd_proto_family);
91447636 809 np->nd_proto_family = PF_NDRV;
7b1edb79
A
810
811 return result;
1c79356b
A
812}
813
814struct ndrv_cb *
b0d623f7 815ndrv_find_inbound(struct ifnet *ifp, u_int32_t protocol)
7b1edb79
A
816{
817 struct ndrv_cb* np;
91447636
A
818
819 if (protocol == PF_NDRV) return NULL;
7b1edb79 820
91447636
A
821 TAILQ_FOREACH(np, &ndrvl, nd_next) {
822 if (np->nd_proto_family == protocol &&
823 np->nd_if == ifp) {
7b1edb79
A
824 return np;
825 }
826 }
827
828 return NULL;
1c79356b
A
829}
830
91447636 831static void ndrv_dominit(void)
7b1edb79
A
832{
833 static int ndrv_dominited = 0;
1c79356b 834
7b1edb79
A
835 if (ndrv_dominited == 0 &&
836 net_add_proto(&ndrvsw, &ndrvdomain) == 0)
837 ndrv_dominited = 1;
1c79356b
A
838}
839
91447636 840static void
b0d623f7 841ndrv_handle_ifp_detach(u_int32_t family, short unit)
1c79356b 842{
7b1edb79 843 struct ndrv_cb* np;
91447636
A
844 struct ifnet *ifp = NULL;
845 struct socket *so;
7b1edb79
A
846
847 /* Find all sockets using this interface. */
91447636 848 TAILQ_FOREACH(np, &ndrvl, nd_next) {
7b1edb79
A
849 if (np->nd_family == family &&
850 np->nd_unit == unit)
851 {
852 /* This cb is using the detaching interface, but not for long. */
853 /* Let the protocol go */
91447636
A
854 ifp = np->nd_if;
855 if (np->nd_proto_family != 0)
7b1edb79
A
856 ndrv_delspec(np);
857
9bccf70c
A
858 /* Delete the multicasts first */
859 ndrv_remove_all_multicast(np);
860
7b1edb79
A
861 /* Disavow all knowledge of the ifp */
862 np->nd_if = NULL;
863 np->nd_unit = 0;
864 np->nd_family = 0;
91447636
A
865
866 so = np->nd_socket;
7b1edb79
A
867 /* Make sure sending returns an error */
868 /* Is this safe? Will we drop the funnel? */
91447636
A
869 lck_mtx_assert(so->so_proto->pr_domain->dom_mtx, LCK_MTX_ASSERT_OWNED);
870 socantsendmore(so);
871 socantrcvmore(so);
1c79356b 872 }
7b1edb79
A
873 }
874
875 /* Unregister our protocol */
91447636 876 if (ifp) {
2d21ac55 877 ifnet_detach_protocol(ifp, PF_NDRV);
7b1edb79 878 }
1c79356b
A
879}
880
9bccf70c
A
881static int
882ndrv_do_add_multicast(struct ndrv_cb *np, struct sockopt *sopt)
883{
884 struct ndrv_multiaddr* ndrv_multi;
885 int result;
886
2d21ac55 887 if (sopt->sopt_val == 0 || sopt->sopt_valsize < 2 ||
9bccf70c
A
888 sopt->sopt_level != SOL_NDRVPROTO)
889 return EINVAL;
890 if (np->nd_if == NULL)
891 return ENXIO;
892
893 // Allocate storage
894 MALLOC(ndrv_multi, struct ndrv_multiaddr*, sizeof(struct ndrv_multiaddr) -
895 sizeof(struct sockaddr) + sopt->sopt_valsize, M_IFADDR, M_WAITOK);
896 if (ndrv_multi == NULL)
897 return ENOMEM;
898
899 // Copy in the address
900 result = copyin(sopt->sopt_val, &ndrv_multi->addr, sopt->sopt_valsize);
901
902 // Validate the sockaddr
903 if (result == 0 && sopt->sopt_valsize != ndrv_multi->addr.sa_len)
904 result = EINVAL;
905
906 if (result == 0 && ndrv_have_multicast(np, &ndrv_multi->addr))
907 result = EEXIST;
908
909 if (result == 0)
910 {
911 // Try adding the multicast
2d21ac55
A
912 result = ifnet_add_multicast(np->nd_if, &ndrv_multi->addr,
913 &ndrv_multi->ifma);
9bccf70c
A
914 }
915
916 if (result == 0)
917 {
918 // Add to our linked list
919 ndrv_multi->next = np->nd_multiaddrs;
920 np->nd_multiaddrs = ndrv_multi;
921 }
922 else
923 {
924 // Free up the memory, something went wrong
925 FREE(ndrv_multi, M_IFADDR);
926 }
927
928 return result;
929}
930
931static int
932ndrv_do_remove_multicast(struct ndrv_cb *np, struct sockopt *sopt)
933{
934 struct sockaddr* multi_addr;
935 struct ndrv_multiaddr* ndrv_entry = NULL;
936 int result;
937
2d21ac55 938 if (sopt->sopt_val == 0 || sopt->sopt_valsize < 2 ||
9bccf70c
A
939 sopt->sopt_level != SOL_NDRVPROTO)
940 return EINVAL;
941 if (np->nd_if == NULL)
942 return ENXIO;
943
944 // Allocate storage
945 MALLOC(multi_addr, struct sockaddr*, sopt->sopt_valsize,
946 M_TEMP, M_WAITOK);
947 if (multi_addr == NULL)
948 return ENOMEM;
949
950 // Copy in the address
951 result = copyin(sopt->sopt_val, multi_addr, sopt->sopt_valsize);
952
953 // Validate the sockaddr
954 if (result == 0 && sopt->sopt_valsize != multi_addr->sa_len)
955 result = EINVAL;
956
957 if (result == 0)
958 {
959 /* Find the old entry */
960 ndrv_entry = ndrv_have_multicast(np, multi_addr);
961
962 if (ndrv_entry == NULL)
963 result = ENOENT;
964 }
965
966 if (result == 0)
967 {
968 // Try deleting the multicast
2d21ac55 969 result = ifnet_remove_multicast(ndrv_entry->ifma);
9bccf70c
A
970 }
971
972 if (result == 0)
973 {
974 // Remove from our linked list
975 struct ndrv_multiaddr* cur = np->nd_multiaddrs;
976
2d21ac55 977 ifmaddr_release(ndrv_entry->ifma);
91447636 978
9bccf70c
A
979 if (cur == ndrv_entry)
980 {
981 np->nd_multiaddrs = cur->next;
982 }
983 else
984 {
985 for (cur = cur->next; cur != NULL; cur = cur->next)
986 {
987 if (cur->next == ndrv_entry)
988 {
989 cur->next = cur->next->next;
990 break;
991 }
992 }
993 }
994
995 // Free the memory
996 FREE(ndrv_entry, M_IFADDR);
997 }
998 FREE(multi_addr, M_TEMP);
999
1000 return result;
1001}
1002
1003static struct ndrv_multiaddr*
1004ndrv_have_multicast(struct ndrv_cb *np, struct sockaddr* inAddr)
1005{
1006 struct ndrv_multiaddr* cur;
1007 for (cur = np->nd_multiaddrs; cur != NULL; cur = cur->next)
1008 {
1009
1010 if ((inAddr->sa_len == cur->addr.sa_len) &&
1011 (bcmp(&cur->addr, inAddr, inAddr->sa_len) == 0))
1012 {
1013 // Found a match
1014 return cur;
1015 }
1016 }
1017
1018 return NULL;
1019}
1020
1021static void
1022ndrv_remove_all_multicast(struct ndrv_cb* np)
1023{
1024 struct ndrv_multiaddr* cur;
1025
1026 if (np->nd_if != NULL)
1027 {
1028 while (np->nd_multiaddrs != NULL)
1029 {
1030 cur = np->nd_multiaddrs;
1031 np->nd_multiaddrs = cur->next;
1032
2d21ac55
A
1033 ifnet_remove_multicast(cur->ifma);
1034 ifmaddr_release(cur->ifma);
9bccf70c
A
1035 FREE(cur, M_IFADDR);
1036 }
1037 }
1038}
1039
1c79356b
A
1040struct pr_usrreqs ndrv_usrreqs = {
1041 ndrv_abort, pru_accept_notsupp, ndrv_attach, ndrv_bind,
91447636 1042 ndrv_connect, pru_connect2_notsupp, pru_control_notsupp, ndrv_detach,
1c79356b 1043 ndrv_disconnect, pru_listen_notsupp, ndrv_peeraddr, pru_rcvd_notsupp,
91447636
A
1044 pru_rcvoob_notsupp, ndrv_send, pru_sense_null, ndrv_shutdown,
1045 ndrv_sockaddr, sosend, soreceive, pru_sopoll_notsupp
1c79356b
A
1046};
1047
7b1edb79
A
1048struct protosw ndrvsw =
1049{ SOCK_RAW, &ndrvdomain, NDRVPROTO_NDRV, PR_ATOMIC|PR_ADDR,
2d21ac55
A
1050 NULL, ndrv_output, NULL, ndrv_ctloutput,
1051 NULL,
1052 NULL, NULL, NULL, NULL, NULL,
91447636 1053 &ndrv_usrreqs,
2d21ac55
A
1054 NULL, NULL, NULL,
1055 { NULL, NULL}, NULL,
1056 { 0 }
1c79356b
A
1057};
1058
1059struct domain ndrvdomain =
2d21ac55
A
1060{ AF_NDRV,
1061 "NetDriver",
1062 ndrv_dominit,
1063 NULL,
1064 NULL,
1065 NULL,
1066 NULL,
1067 NULL,
1068 0,
1069 0,
1070 0,
1071 0,
1c79356b 1072 NULL,
2d21ac55
A
1073 0,
1074 {0, 0}
1c79356b 1075};