2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
28 /* Copyright (c) 1997, 1998 Apple Computer, Inc. All Rights Reserved */
30 * @(#)ndrv.c 1.1 (MacOSX) 6/10/43
31 * Justin Walker, 970604
33 * 980130 - Cleanup, reorg, performance improvemements
34 * 000816 - Removal of Y adapter cruft
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.
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.
50 #include <mach/mach_types.h>
52 #include <sys/param.h>
53 #include <sys/systm.h>
54 #include <sys/kernel.h>
55 #include <sys/malloc.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>
66 #include <kern/queue.h>
69 #include <net/route.h>
70 #include <net/if_llc.h>
71 #include <net/if_dl.h>
72 #include <net/if_types.h>
73 #include <net/ndrv_var.h>
76 #include <netinet/in.h>
77 #include <netinet/in_var.h>
79 #include <netinet/if_ether.h>
81 #include <machine/spl.h>
83 static int ndrv_do_detach(struct ndrv_cb
*);
84 static int ndrv_do_disconnect(struct ndrv_cb
*);
85 static struct ndrv_cb
*ndrv_find_inbound(struct ifnet
*ifp
, u_long protocol_family
);
86 static int ndrv_setspec(struct ndrv_cb
*np
, struct sockopt
*sopt
);
87 static int ndrv_delspec(struct ndrv_cb
*);
88 static int ndrv_to_dlil_demux(struct ndrv_demux_desc
* ndrv
, struct dlil_demux_desc
* dlil
);
89 static void ndrv_handle_ifp_detach(u_long family
, short unit
);
90 static int ndrv_do_add_multicast(struct ndrv_cb
*np
, struct sockopt
*sopt
);
91 static int ndrv_do_remove_multicast(struct ndrv_cb
*np
, struct sockopt
*sopt
);
92 static struct ndrv_multiaddr
* ndrv_have_multicast(struct ndrv_cb
*np
, struct sockaddr
* addr
);
93 static void ndrv_remove_all_multicast(struct ndrv_cb
*np
);
95 unsigned long ndrv_sendspace
= NDRVSNDQ
;
96 unsigned long ndrv_recvspace
= NDRVRCVQ
;
97 TAILQ_HEAD(, ndrv_cb
) ndrvl
= TAILQ_HEAD_INITIALIZER(ndrvl
);
99 extern struct domain ndrvdomain
;
100 extern struct protosw ndrvsw
;
101 extern lck_mtx_t
*domain_proto_mtx
;
103 extern void kprintf(const char *, ...);
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.
111 #if NDRV_DEMUXTYPE_ETHERTYPE != DLIL_DESC_ETYPE2
112 #error NDRV_DEMUXTYPE_ETHERTYPE must match DLIL_DESC_ETYPE2
114 #if NDRV_DEMUXTYPE_SAP != DLIL_DESC_SAP
115 #error NDRV_DEMUXTYPE_SAP must match DLIL_DESC_SAP
117 #if NDRV_DEMUXTYPE_SNAP != DLIL_DESC_SNAP
118 #error NDRV_DEMUXTYPE_SNAP must match DLIL_DESC_SNAP
122 * Protocol output - Called to output a raw network packet directly
126 ndrv_output(struct mbuf
*m
, struct socket
*so
)
128 struct ndrv_cb
*np
= sotondrvcb(so
);
129 struct ifnet
*ifp
= np
->nd_if
;
133 kprintf("NDRV output: %x, %x, %x\n", m
, so
, np
);
137 * No header is a format error
139 if ((m
->m_flags
&M_PKTHDR
) == 0)
142 /* Unlock before calling dlil_output */
143 socket_unlock(so
, 0);
146 * Call DLIL if we can. DLIL is much safer than calling the
149 result
= dlil_output(ifp
, np
->nd_proto_family
, m
, (caddr_t
)NULL
,
150 (struct sockaddr
*)NULL
, 1);
157 /* Our input routine called from DLIL */
159 ndrv_input(struct mbuf
*m
,
163 __unused
int sync_ok
)
166 struct sockaddr_dl ndrvsrc
= {sizeof (struct sockaddr_dl
), AF_NDRV
};
171 /* move packet from if queue to socket */
172 /* Should be media-independent */
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);
179 np
= ndrv_find_inbound(ifp
, proto_family
);
185 /* prepend the frame header */
186 m
= m_prepend(m
, ifp
->if_hdrlen
, M_NOWAIT
);
189 bcopy(frame_header
, m
->m_data
, ifp
->if_hdrlen
);
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
);
193 if (sbappendaddr(&(so
->so_rcv
), (struct sockaddr
*)&ndrvsrc
,
194 m
, (struct mbuf
*)0, &error
) != 0) {
197 lck_mtx_unlock(so
->so_proto
->pr_domain
->dom_mtx
);
198 return 0; /* radar 4030377 - always return 0 */
202 * Allocate an ndrv control block and some buffer space for the socket
205 ndrv_attach(struct socket
*so
, int proto
, __unused
struct proc
*p
)
208 struct ndrv_cb
*np
= sotondrvcb(so
);
210 if ((so
->so_state
& SS_PRIV
) == 0)
214 kprintf("NDRV attach: %x, %x, %x\n", so
, proto
, np
);
217 if ((error
= soreserve(so
, ndrv_sendspace
, ndrv_recvspace
)))
220 MALLOC(np
, struct ndrv_cb
*, sizeof(*np
), M_PCB
, M_WAITOK
);
223 so
->so_pcb
= (caddr_t
)np
;
224 bzero(np
, sizeof(*np
));
226 kprintf("NDRV attach: %x, %x, %x\n", so
, proto
, np
);
228 TAILQ_INIT(&np
->nd_dlist
);
229 np
->nd_signature
= NDRV_SIGNATURE
;
231 np
->nd_proto
.sp_family
= so
->so_proto
->pr_domain
->dom_family
;
232 np
->nd_proto
.sp_protocol
= proto
;
234 np
->nd_proto_family
= 0;
237 TAILQ_INSERT_TAIL(&ndrvl
, np
, nd_next
);
242 * Destroy state just before socket deallocation.
243 * Flush data or not depending on the options.
247 ndrv_detach(struct socket
*so
)
249 struct ndrv_cb
*np
= sotondrvcb(so
);
253 return ndrv_do_detach(np
);
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).
263 * Don't expect this to be used.
267 ndrv_connect(struct socket
*so
, struct sockaddr
*nam
, __unused
struct proc
*p
)
269 struct ndrv_cb
*np
= sotondrvcb(so
);
278 /* Allocate memory to store the remote address */
279 MALLOC(np
->nd_faddr
, struct sockaddr_ndrv
*,
280 nam
->sa_len
, M_IFADDR
, M_WAITOK
);
283 if (np
->nd_faddr
== NULL
)
286 bcopy((caddr_t
) nam
, (caddr_t
) np
->nd_faddr
, nam
->sa_len
);
292 ndrv_event(struct ifnet
*ifp
, struct kev_msg
*event
)
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
) {
298 ndrv_handle_ifp_detach(ifp
->if_family
, ifp
->if_unit
);
302 static int name_cmp(struct ifnet
*, char *);
305 * This is the "driver open" hook - we 'bind' to the
307 * Here's where we latch onto the driver.
310 ndrv_bind(struct socket
*so
, struct sockaddr
*nam
, __unused
struct proc
*p
)
312 struct sockaddr_ndrv
*sa
= (struct sockaddr_ndrv
*) nam
;
318 if TAILQ_EMPTY(&ifnet_head
)
319 return(EADDRNOTAVAIL
); /* Quick sanity check */
325 return EINVAL
; /* XXX */
327 /* I think we just latch onto a copy here; the caller frees */
328 np
->nd_laddr
= _MALLOC(sizeof(struct sockaddr_ndrv
), M_IFADDR
, M_WAITOK
);
329 if (np
->nd_laddr
== NULL
)
331 bcopy((caddr_t
) sa
, (caddr_t
) np
->nd_laddr
, sizeof(struct sockaddr_ndrv
));
332 dname
= sa
->snd_name
;
336 kprintf("NDRV bind: %x, %x, %s\n", so
, np
, dname
);
338 /* Track down the driver and its ifnet structure.
339 * There's no internal call for this so we have to dup the code
342 ifnet_head_lock_shared();
343 TAILQ_FOREACH(ifp
, &ifnet_head
, if_link
) {
344 if (name_cmp(ifp
, dname
) == 0)
350 return(EADDRNOTAVAIL
);
352 // PPP doesn't support PF_NDRV.
353 if (ifp
->if_family
!= APPLE_IF_FAM_PPP
)
355 /* NDRV on this interface */
356 struct dlil_proto_reg_str ndrv_proto
;
358 bzero(&ndrv_proto
, sizeof(ndrv_proto
));
359 TAILQ_INIT(&ndrv_proto
.demux_desc_head
);
361 ndrv_proto
.interface_family
= ifp
->if_family
;
362 ndrv_proto
.protocol_family
= PF_NDRV
;
363 ndrv_proto
.unit_number
= ifp
->if_unit
;
364 ndrv_proto
.event
= ndrv_event
;
366 /* We aren't worried about double attaching, that should just return an error */
367 result
= dlil_attach_protocol(&ndrv_proto
);
368 if (result
&& result
!= EEXIST
) {
371 np
->nd_proto_family
= PF_NDRV
;
374 np
->nd_proto_family
= 0;
378 np
->nd_family
= ifp
->if_family
;
379 np
->nd_unit
= ifp
->if_unit
;
385 ndrv_disconnect(struct socket
*so
)
387 struct ndrv_cb
*np
= sotondrvcb(so
);
392 if (np
->nd_faddr
== 0)
395 ndrv_do_disconnect(np
);
400 * Mark the connection as being incapable of further input.
403 ndrv_shutdown(struct socket
*so
)
405 lck_mtx_assert(so
->so_proto
->pr_domain
->dom_mtx
, LCK_MTX_ASSERT_OWNED
);
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...
416 ndrv_send(struct socket
*so
, __unused
int flags
, struct mbuf
*m
,
417 __unused
struct sockaddr
*addr
, struct mbuf
*control
,
418 __unused
struct proc
*p
)
425 error
= ndrv_output(m
, so
);
432 ndrv_abort(struct socket
*so
)
434 struct ndrv_cb
*np
= sotondrvcb(so
);
439 ndrv_do_disconnect(np
);
444 ndrv_sockaddr(struct socket
*so
, struct sockaddr
**nam
)
446 struct ndrv_cb
*np
= sotondrvcb(so
);
452 if (np
->nd_laddr
== 0)
455 len
= np
->nd_laddr
->snd_len
;
456 MALLOC(*nam
, struct sockaddr
*, len
, M_SONAME
, M_WAITOK
);
459 bcopy((caddr_t
)np
->nd_laddr
, *nam
,
466 ndrv_peeraddr(struct socket
*so
, struct sockaddr
**nam
)
468 struct ndrv_cb
*np
= sotondrvcb(so
);
474 if (np
->nd_faddr
== 0)
477 len
= np
->nd_faddr
->snd_len
;
478 MALLOC(*nam
, struct sockaddr
*, len
, M_SONAME
, M_WAITOK
);
481 bcopy((caddr_t
)np
->nd_faddr
, *nam
,
490 ndrv_ctloutput(struct socket
*so
, struct sockopt
*sopt
)
492 struct ndrv_cb
*np
= sotondrvcb(so
);
495 switch(sopt
->sopt_name
)
497 case NDRV_DELDMXSPEC
: /* Delete current spec */
498 /* Verify no parameter was passed */
499 if (sopt
->sopt_val
!= NULL
|| sopt
->sopt_valsize
!= 0) {
501 * We don't support deleting a specific demux, it's
506 error
= ndrv_delspec(np
);
508 case NDRV_SETDMXSPEC
: /* Set protocol spec */
509 error
= ndrv_setspec(np
, sopt
);
511 case NDRV_ADDMULTICAST
:
512 error
= ndrv_do_add_multicast(np
, sopt
);
514 case NDRV_DELMULTICAST
:
515 error
= ndrv_do_remove_multicast(np
, sopt
);
521 log(LOG_WARNING
, "NDRV CTLOUT: %x returns %d\n", sopt
->sopt_name
,
528 ndrv_do_detach(struct ndrv_cb
*np
)
530 struct ndrv_cb
* cur_np
= NULL
;
531 struct socket
*so
= np
->nd_socket
;
535 kprintf("NDRV detach: %x, %x\n", so
, np
);
537 ndrv_remove_all_multicast(np
);
540 if (np
->nd_proto_family
!= PF_NDRV
&&
541 np
->nd_proto_family
!= 0) {
542 dlil_detach_protocol(np
->nd_if
, np
->nd_proto_family
);
545 /* Remove from the linked list of control blocks */
546 TAILQ_REMOVE(&ndrvl
, np
, nd_next
);
548 /* Check if this is the last socket attached to this interface */
549 TAILQ_FOREACH(cur_np
, &ndrvl
, nd_next
) {
550 if (cur_np
->nd_family
== np
->nd_family
&&
551 cur_np
->nd_unit
== np
->nd_unit
) {
556 /* If there are no other interfaces, detach PF_NDRV from the interface */
557 if (cur_np
== NULL
) {
558 dlil_detach_protocol(np
->nd_if
, PF_NDRV
);
561 /* Remove from the linked list of control blocks */
562 TAILQ_REMOVE(&ndrvl
, np
, nd_next
);
565 FREE((caddr_t
)np
, M_PCB
);
567 so
->so_flags
|= SOF_PCBCLEARING
;
573 ndrv_do_disconnect(struct ndrv_cb
*np
)
576 kprintf("NDRV disconnect: %x\n", np
);
580 FREE(np
->nd_faddr
, M_IFADDR
);
583 if (np
->nd_socket
->so_state
& SS_NOFDREF
)
585 soisdisconnected(np
->nd_socket
);
589 /* Hackery - return a string version of a decimal number */
591 sprint_d(u_int n
, char *buf
, int buflen
)
592 { char dbuf
[IFNAMSIZ
];
593 char *cp
= dbuf
+IFNAMSIZ
-1;
598 *cp
= "0123456789"[n
% 10];
600 } while (n
!= 0 && buflen
> 0);
601 strncpy(buf
, cp
, IFNAMSIZ
-buflen
);
606 * Try to compare a device name (q) with one of the funky ifnet
607 * device names (ifp).
609 static int name_cmp(struct ifnet
*ifp
, char *q
)
615 len
= strlen(ifp
->if_name
);
616 strncpy(r
, ifp
->if_name
, IFNAMSIZ
);
618 (void)sprint_d(ifp
->if_unit
, r
, IFNAMSIZ
-(r
-buf
));
620 kprintf("Comparing %s, %s\n", buf
, q
);
622 return(strncmp(buf
, q
, IFNAMSIZ
));
628 * When closing, dump any enqueued mbufs.
631 ndrv_flushq(struct ifqueue
*q
)
647 ndrv_setspec(struct ndrv_cb
*np
, struct sockopt
*sopt
)
649 struct dlil_proto_reg_str dlilSpec
;
650 struct ndrv_protocol_desc ndrvSpec
;
651 struct dlil_demux_desc
* dlilDemux
= NULL
;
652 struct ndrv_demux_desc
* ndrvDemux
= NULL
;
655 /* Sanity checking */
656 if (np
->nd_proto_family
!= PF_NDRV
)
658 if (np
->nd_if
== NULL
)
660 if (sopt
->sopt_valsize
!= sizeof(struct ndrv_protocol_desc
))
663 /* Copy the ndrvSpec */
664 error
= sooptcopyin(sopt
, &ndrvSpec
, sizeof(struct ndrv_protocol_desc
),
665 sizeof(struct ndrv_protocol_desc
));
669 /* Verify the parameter */
670 if (ndrvSpec
.version
> NDRV_PROTOCOL_DESC_VERS
)
671 return ENOTSUP
; // version is too new!
672 else if (ndrvSpec
.version
< 1)
673 return EINVAL
; // version is not valid
675 /* Allocate storage for demux array */
676 MALLOC(ndrvDemux
, struct ndrv_demux_desc
*,
677 ndrvSpec
.demux_count
* sizeof(struct ndrv_demux_desc
), M_TEMP
, M_WAITOK
);
678 if (ndrvDemux
== NULL
)
681 /* Allocate enough dlil_demux_descs */
682 MALLOC(dlilDemux
, struct dlil_demux_desc
*,
683 sizeof(*dlilDemux
) * ndrvSpec
.demux_count
, M_TEMP
, M_WAITOK
);
684 if (dlilDemux
== NULL
)
689 /* Copy the ndrv demux array from userland */
690 error
= copyin(CAST_USER_ADDR_T(ndrvSpec
.demux_list
), ndrvDemux
,
691 ndrvSpec
.demux_count
* sizeof(struct ndrv_demux_desc
));
692 ndrvSpec
.demux_list
= ndrvDemux
;
697 /* At this point, we've at least got enough bytes to start looking around */
700 bzero(&dlilSpec
, sizeof(dlilSpec
));
701 TAILQ_INIT(&dlilSpec
.demux_desc_head
);
702 dlilSpec
.interface_family
= np
->nd_family
;
703 dlilSpec
.unit_number
= np
->nd_unit
;
704 dlilSpec
.input
= ndrv_input
;
705 dlilSpec
.event
= ndrv_event
;
706 dlilSpec
.protocol_family
= ndrvSpec
.protocol_family
;
708 for (demuxOn
= 0; demuxOn
< ndrvSpec
.demux_count
; demuxOn
++)
710 /* Convert an ndrv_demux_desc to a dlil_demux_desc */
711 error
= ndrv_to_dlil_demux(&ndrvSpec
.demux_list
[demuxOn
], &dlilDemux
[demuxOn
]);
715 /* Add the dlil_demux_desc to the list */
716 TAILQ_INSERT_TAIL(&dlilSpec
.demux_desc_head
, &dlilDemux
[demuxOn
], next
);
722 /* We've got all our ducks lined up...lets attach! */
723 error
= dlil_attach_protocol(&dlilSpec
);
725 np
->nd_proto_family
= dlilSpec
.protocol_family
;
728 /* Free any memory we've allocated */
730 FREE(dlilDemux
, M_TEMP
);
732 FREE(ndrvDemux
, M_TEMP
);
739 ndrv_to_dlil_demux(struct ndrv_demux_desc
* ndrv
, struct dlil_demux_desc
* dlil
)
741 bzero(dlil
, sizeof(*dlil
));
743 if (ndrv
->type
< DLIL_DESC_ETYPE2
)
745 /* using old "type", not supported */
749 if (ndrv
->length
> 28)
754 dlil
->type
= ndrv
->type
;
755 dlil
->native_type
= ndrv
->data
.other
;
756 dlil
->variants
.native_type_length
= ndrv
->length
;
762 ndrv_delspec(struct ndrv_cb
*np
)
766 if (np
->nd_proto_family
== PF_NDRV
||
767 np
->nd_proto_family
== 0)
770 /* Detach the protocol */
771 result
= dlil_detach_protocol(np
->nd_if
, np
->nd_proto_family
);
772 np
->nd_proto_family
= PF_NDRV
;
778 ndrv_find_inbound(struct ifnet
*ifp
, u_long protocol
)
782 if (protocol
== PF_NDRV
) return NULL
;
784 TAILQ_FOREACH(np
, &ndrvl
, nd_next
) {
785 if (np
->nd_proto_family
== protocol
&&
794 static void ndrv_dominit(void)
796 static int ndrv_dominited
= 0;
798 if (ndrv_dominited
== 0 &&
799 net_add_proto(&ndrvsw
, &ndrvdomain
) == 0)
804 ndrv_handle_ifp_detach(u_long family
, short unit
)
807 struct ifnet
*ifp
= NULL
;
810 /* Find all sockets using this interface. */
811 TAILQ_FOREACH(np
, &ndrvl
, nd_next
) {
812 if (np
->nd_family
== family
&&
815 /* This cb is using the detaching interface, but not for long. */
816 /* Let the protocol go */
818 if (np
->nd_proto_family
!= 0)
821 /* Delete the multicasts first */
822 ndrv_remove_all_multicast(np
);
824 /* Disavow all knowledge of the ifp */
830 /* Make sure sending returns an error */
831 /* Is this safe? Will we drop the funnel? */
832 lck_mtx_assert(so
->so_proto
->pr_domain
->dom_mtx
, LCK_MTX_ASSERT_OWNED
);
838 /* Unregister our protocol */
840 dlil_detach_protocol(ifp
, PF_NDRV
);
845 ndrv_do_add_multicast(struct ndrv_cb
*np
, struct sockopt
*sopt
)
847 struct ndrv_multiaddr
* ndrv_multi
;
850 if (sopt
->sopt_val
== NULL
|| sopt
->sopt_valsize
< 2 ||
851 sopt
->sopt_level
!= SOL_NDRVPROTO
)
853 if (np
->nd_if
== NULL
)
857 MALLOC(ndrv_multi
, struct ndrv_multiaddr
*, sizeof(struct ndrv_multiaddr
) -
858 sizeof(struct sockaddr
) + sopt
->sopt_valsize
, M_IFADDR
, M_WAITOK
);
859 if (ndrv_multi
== NULL
)
862 // Copy in the address
863 result
= copyin(sopt
->sopt_val
, &ndrv_multi
->addr
, sopt
->sopt_valsize
);
865 // Validate the sockaddr
866 if (result
== 0 && sopt
->sopt_valsize
!= ndrv_multi
->addr
.sa_len
)
869 if (result
== 0 && ndrv_have_multicast(np
, &ndrv_multi
->addr
))
874 // Try adding the multicast
875 result
= if_addmulti(np
->nd_if
, &ndrv_multi
->addr
, &ndrv_multi
->ifma
);
880 // Add to our linked list
881 ndrv_multi
->next
= np
->nd_multiaddrs
;
882 np
->nd_multiaddrs
= ndrv_multi
;
886 // Free up the memory, something went wrong
887 FREE(ndrv_multi
, M_IFADDR
);
894 ndrv_do_remove_multicast(struct ndrv_cb
*np
, struct sockopt
*sopt
)
896 struct sockaddr
* multi_addr
;
897 struct ndrv_multiaddr
* ndrv_entry
= NULL
;
900 if (sopt
->sopt_val
== NULL
|| sopt
->sopt_valsize
< 2 ||
901 sopt
->sopt_level
!= SOL_NDRVPROTO
)
903 if (np
->nd_if
== NULL
)
907 MALLOC(multi_addr
, struct sockaddr
*, sopt
->sopt_valsize
,
909 if (multi_addr
== NULL
)
912 // Copy in the address
913 result
= copyin(sopt
->sopt_val
, multi_addr
, sopt
->sopt_valsize
);
915 // Validate the sockaddr
916 if (result
== 0 && sopt
->sopt_valsize
!= multi_addr
->sa_len
)
921 /* Find the old entry */
922 ndrv_entry
= ndrv_have_multicast(np
, multi_addr
);
924 if (ndrv_entry
== NULL
)
930 // Try deleting the multicast
931 result
= if_delmultiaddr(ndrv_entry
->ifma
, 0);
936 // Remove from our linked list
937 struct ndrv_multiaddr
* cur
= np
->nd_multiaddrs
;
939 ifma_release(ndrv_entry
->ifma
);
941 if (cur
== ndrv_entry
)
943 np
->nd_multiaddrs
= cur
->next
;
947 for (cur
= cur
->next
; cur
!= NULL
; cur
= cur
->next
)
949 if (cur
->next
== ndrv_entry
)
951 cur
->next
= cur
->next
->next
;
958 FREE(ndrv_entry
, M_IFADDR
);
960 FREE(multi_addr
, M_TEMP
);
965 static struct ndrv_multiaddr
*
966 ndrv_have_multicast(struct ndrv_cb
*np
, struct sockaddr
* inAddr
)
968 struct ndrv_multiaddr
* cur
;
969 for (cur
= np
->nd_multiaddrs
; cur
!= NULL
; cur
= cur
->next
)
972 if ((inAddr
->sa_len
== cur
->addr
.sa_len
) &&
973 (bcmp(&cur
->addr
, inAddr
, inAddr
->sa_len
) == 0))
984 ndrv_remove_all_multicast(struct ndrv_cb
* np
)
986 struct ndrv_multiaddr
* cur
;
988 if (np
->nd_if
!= NULL
)
990 while (np
->nd_multiaddrs
!= NULL
)
992 cur
= np
->nd_multiaddrs
;
993 np
->nd_multiaddrs
= cur
->next
;
995 if_delmultiaddr(cur
->ifma
, 0);
996 ifma_release(cur
->ifma
);
1002 struct pr_usrreqs ndrv_usrreqs
= {
1003 ndrv_abort
, pru_accept_notsupp
, ndrv_attach
, ndrv_bind
,
1004 ndrv_connect
, pru_connect2_notsupp
, pru_control_notsupp
, ndrv_detach
,
1005 ndrv_disconnect
, pru_listen_notsupp
, ndrv_peeraddr
, pru_rcvd_notsupp
,
1006 pru_rcvoob_notsupp
, ndrv_send
, pru_sense_null
, ndrv_shutdown
,
1007 ndrv_sockaddr
, sosend
, soreceive
, pru_sopoll_notsupp
1010 struct protosw ndrvsw
=
1011 { SOCK_RAW
, &ndrvdomain
, NDRVPROTO_NDRV
, PR_ATOMIC
|PR_ADDR
,
1012 0, ndrv_output
, 0, ndrv_ctloutput
,
1019 struct domain ndrvdomain
=
1020 { AF_NDRV
, "NetDriver", ndrv_dominit
, NULL
, NULL
,
1022 NULL
, NULL
, 0, 0, 0, 0