2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_OSREFERENCE_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
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
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
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.
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
30 /* Copyright (c) 1997, 1998 Apple Computer, Inc. All Rights Reserved */
32 * @(#)ndrv.c 1.1 (MacOSX) 6/10/43
33 * Justin Walker, 970604
35 * 980130 - Cleanup, reorg, performance improvemements
36 * 000816 - Removal of Y adapter cruft
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.
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.
52 #include <mach/mach_types.h>
54 #include <sys/param.h>
55 #include <sys/systm.h>
56 #include <sys/kernel.h>
57 #include <sys/malloc.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>
68 #include <kern/queue.h>
71 #include <net/route.h>
72 #include <net/if_llc.h>
73 #include <net/if_dl.h>
74 #include <net/if_types.h>
75 #include <net/ndrv_var.h>
78 #include <netinet/in.h>
79 #include <netinet/in_var.h>
81 #include <netinet/if_ether.h>
83 #include <machine/spl.h>
85 static int ndrv_do_detach(struct ndrv_cb
*);
86 static int ndrv_do_disconnect(struct ndrv_cb
*);
87 static struct ndrv_cb
*ndrv_find_inbound(struct ifnet
*ifp
, u_long protocol_family
);
88 static int ndrv_setspec(struct ndrv_cb
*np
, struct sockopt
*sopt
);
89 static int ndrv_delspec(struct ndrv_cb
*);
90 static int ndrv_to_dlil_demux(struct ndrv_demux_desc
* ndrv
, struct dlil_demux_desc
* dlil
);
91 static void ndrv_handle_ifp_detach(u_long family
, short unit
);
92 static int ndrv_do_add_multicast(struct ndrv_cb
*np
, struct sockopt
*sopt
);
93 static int ndrv_do_remove_multicast(struct ndrv_cb
*np
, struct sockopt
*sopt
);
94 static struct ndrv_multiaddr
* ndrv_have_multicast(struct ndrv_cb
*np
, struct sockaddr
* addr
);
95 static void ndrv_remove_all_multicast(struct ndrv_cb
*np
);
97 unsigned long ndrv_sendspace
= NDRVSNDQ
;
98 unsigned long ndrv_recvspace
= NDRVRCVQ
;
99 TAILQ_HEAD(, ndrv_cb
) ndrvl
= TAILQ_HEAD_INITIALIZER(ndrvl
);
101 extern struct domain ndrvdomain
;
102 extern struct protosw ndrvsw
;
103 extern lck_mtx_t
*domain_proto_mtx
;
105 extern void kprintf(const char *, ...);
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.
113 #if NDRV_DEMUXTYPE_ETHERTYPE != DLIL_DESC_ETYPE2
114 #error NDRV_DEMUXTYPE_ETHERTYPE must match DLIL_DESC_ETYPE2
116 #if NDRV_DEMUXTYPE_SAP != DLIL_DESC_SAP
117 #error NDRV_DEMUXTYPE_SAP must match DLIL_DESC_SAP
119 #if NDRV_DEMUXTYPE_SNAP != DLIL_DESC_SNAP
120 #error NDRV_DEMUXTYPE_SNAP must match DLIL_DESC_SNAP
124 * Protocol output - Called to output a raw network packet directly
128 ndrv_output(struct mbuf
*m
, struct socket
*so
)
130 struct ndrv_cb
*np
= sotondrvcb(so
);
131 struct ifnet
*ifp
= np
->nd_if
;
135 kprintf("NDRV output: %x, %x, %x\n", m
, so
, np
);
139 * No header is a format error
141 if ((m
->m_flags
&M_PKTHDR
) == 0)
144 /* Unlock before calling dlil_output */
145 socket_unlock(so
, 0);
148 * Call DLIL if we can. DLIL is much safer than calling the
151 result
= dlil_output(ifp
, np
->nd_proto_family
, m
, (caddr_t
)NULL
,
152 (struct sockaddr
*)NULL
, 1);
159 /* Our input routine called from DLIL */
161 ndrv_input(struct mbuf
*m
,
165 __unused
int sync_ok
)
168 struct sockaddr_dl ndrvsrc
= {sizeof (struct sockaddr_dl
), AF_NDRV
};
173 /* move packet from if queue to socket */
174 /* Should be media-independent */
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);
181 np
= ndrv_find_inbound(ifp
, proto_family
);
187 /* prepend the frame header */
188 m
= m_prepend(m
, ifp
->if_hdrlen
, M_NOWAIT
);
191 bcopy(frame_header
, m
->m_data
, ifp
->if_hdrlen
);
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
);
195 if (sbappendaddr(&(so
->so_rcv
), (struct sockaddr
*)&ndrvsrc
,
196 m
, (struct mbuf
*)0, &error
) != 0) {
199 lck_mtx_unlock(so
->so_proto
->pr_domain
->dom_mtx
);
200 return 0; /* radar 4030377 - always return 0 */
204 * Allocate an ndrv control block and some buffer space for the socket
207 ndrv_attach(struct socket
*so
, int proto
, __unused
struct proc
*p
)
210 struct ndrv_cb
*np
= sotondrvcb(so
);
212 if ((so
->so_state
& SS_PRIV
) == 0)
216 kprintf("NDRV attach: %x, %x, %x\n", so
, proto
, np
);
219 if ((error
= soreserve(so
, ndrv_sendspace
, ndrv_recvspace
)))
222 MALLOC(np
, struct ndrv_cb
*, sizeof(*np
), M_PCB
, M_WAITOK
);
225 so
->so_pcb
= (caddr_t
)np
;
226 bzero(np
, sizeof(*np
));
228 kprintf("NDRV attach: %x, %x, %x\n", so
, proto
, np
);
230 TAILQ_INIT(&np
->nd_dlist
);
231 np
->nd_signature
= NDRV_SIGNATURE
;
233 np
->nd_proto
.sp_family
= so
->so_proto
->pr_domain
->dom_family
;
234 np
->nd_proto
.sp_protocol
= proto
;
236 np
->nd_proto_family
= 0;
239 TAILQ_INSERT_TAIL(&ndrvl
, np
, nd_next
);
244 * Destroy state just before socket deallocation.
245 * Flush data or not depending on the options.
249 ndrv_detach(struct socket
*so
)
251 struct ndrv_cb
*np
= sotondrvcb(so
);
255 return ndrv_do_detach(np
);
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).
265 * Don't expect this to be used.
269 ndrv_connect(struct socket
*so
, struct sockaddr
*nam
, __unused
struct proc
*p
)
271 struct ndrv_cb
*np
= sotondrvcb(so
);
280 /* Allocate memory to store the remote address */
281 MALLOC(np
->nd_faddr
, struct sockaddr_ndrv
*,
282 nam
->sa_len
, M_IFADDR
, M_WAITOK
);
285 if (np
->nd_faddr
== NULL
)
288 bcopy((caddr_t
) nam
, (caddr_t
) np
->nd_faddr
, nam
->sa_len
);
294 ndrv_event(struct ifnet
*ifp
, struct kev_msg
*event
)
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
) {
300 lck_mtx_assert(ndrvdomain
.dom_mtx
, LCK_MTX_ASSERT_NOTOWNED
);
301 lck_mtx_lock(ndrvdomain
.dom_mtx
);
302 ndrv_handle_ifp_detach(ifp
->if_family
, ifp
->if_unit
);
303 lck_mtx_unlock(ndrvdomain
.dom_mtx
);
307 static int name_cmp(struct ifnet
*, char *);
310 * This is the "driver open" hook - we 'bind' to the
312 * Here's where we latch onto the driver.
315 ndrv_bind(struct socket
*so
, struct sockaddr
*nam
, __unused
struct proc
*p
)
317 struct sockaddr_ndrv
*sa
= (struct sockaddr_ndrv
*) nam
;
323 if TAILQ_EMPTY(&ifnet_head
)
324 return(EADDRNOTAVAIL
); /* Quick sanity check */
330 return EINVAL
; /* XXX */
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
)
336 bcopy((caddr_t
) sa
, (caddr_t
) np
->nd_laddr
, sizeof(struct sockaddr_ndrv
));
337 dname
= sa
->snd_name
;
341 kprintf("NDRV bind: %x, %x, %s\n", so
, np
, dname
);
343 /* Track down the driver and its ifnet structure.
344 * There's no internal call for this so we have to dup the code
347 ifnet_head_lock_shared();
348 TAILQ_FOREACH(ifp
, &ifnet_head
, if_link
) {
349 if (name_cmp(ifp
, dname
) == 0)
355 return(EADDRNOTAVAIL
);
357 // PPP doesn't support PF_NDRV.
358 if (ifp
->if_family
!= APPLE_IF_FAM_PPP
)
360 /* NDRV on this interface */
361 struct dlil_proto_reg_str ndrv_proto
;
363 bzero(&ndrv_proto
, sizeof(ndrv_proto
));
364 TAILQ_INIT(&ndrv_proto
.demux_desc_head
);
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
;
371 /* We aren't worried about double attaching, that should just return an error */
372 socket_unlock(so
, 0);
373 result
= dlil_attach_protocol(&ndrv_proto
);
375 if (result
&& result
!= EEXIST
) {
378 np
->nd_proto_family
= PF_NDRV
;
381 np
->nd_proto_family
= 0;
385 np
->nd_family
= ifp
->if_family
;
386 np
->nd_unit
= ifp
->if_unit
;
392 ndrv_disconnect(struct socket
*so
)
394 struct ndrv_cb
*np
= sotondrvcb(so
);
399 if (np
->nd_faddr
== 0)
402 ndrv_do_disconnect(np
);
407 * Mark the connection as being incapable of further input.
410 ndrv_shutdown(struct socket
*so
)
412 lck_mtx_assert(so
->so_proto
->pr_domain
->dom_mtx
, LCK_MTX_ASSERT_OWNED
);
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...
423 ndrv_send(struct socket
*so
, __unused
int flags
, struct mbuf
*m
,
424 __unused
struct sockaddr
*addr
, struct mbuf
*control
,
425 __unused
struct proc
*p
)
432 error
= ndrv_output(m
, so
);
439 ndrv_abort(struct socket
*so
)
441 struct ndrv_cb
*np
= sotondrvcb(so
);
446 ndrv_do_disconnect(np
);
451 ndrv_sockaddr(struct socket
*so
, struct sockaddr
**nam
)
453 struct ndrv_cb
*np
= sotondrvcb(so
);
459 if (np
->nd_laddr
== 0)
462 len
= np
->nd_laddr
->snd_len
;
463 MALLOC(*nam
, struct sockaddr
*, len
, M_SONAME
, M_WAITOK
);
466 bcopy((caddr_t
)np
->nd_laddr
, *nam
,
473 ndrv_peeraddr(struct socket
*so
, struct sockaddr
**nam
)
475 struct ndrv_cb
*np
= sotondrvcb(so
);
481 if (np
->nd_faddr
== 0)
484 len
= np
->nd_faddr
->snd_len
;
485 MALLOC(*nam
, struct sockaddr
*, len
, M_SONAME
, M_WAITOK
);
488 bcopy((caddr_t
)np
->nd_faddr
, *nam
,
497 ndrv_ctloutput(struct socket
*so
, struct sockopt
*sopt
)
499 struct ndrv_cb
*np
= sotondrvcb(so
);
502 switch(sopt
->sopt_name
)
504 case NDRV_DELDMXSPEC
: /* Delete current spec */
505 /* Verify no parameter was passed */
506 if (sopt
->sopt_val
!= NULL
|| sopt
->sopt_valsize
!= 0) {
508 * We don't support deleting a specific demux, it's
513 error
= ndrv_delspec(np
);
515 case NDRV_SETDMXSPEC
: /* Set protocol spec */
516 error
= ndrv_setspec(np
, sopt
);
518 case NDRV_ADDMULTICAST
:
519 error
= ndrv_do_add_multicast(np
, sopt
);
521 case NDRV_DELMULTICAST
:
522 error
= ndrv_do_remove_multicast(np
, sopt
);
528 log(LOG_WARNING
, "NDRV CTLOUT: %x returns %d\n", sopt
->sopt_name
,
535 ndrv_do_detach(struct ndrv_cb
*np
)
537 struct ndrv_cb
* cur_np
= NULL
;
538 struct socket
*so
= np
->nd_socket
;
543 kprintf("NDRV detach: %x, %x\n", so
, np
);
545 ndrv_remove_all_multicast(np
);
547 /* Remove from the linked list of control blocks */
548 TAILQ_REMOVE(&ndrvl
, np
, nd_next
);
550 u_long proto_family
= np
->nd_proto_family
;
552 if (proto_family
!= PF_NDRV
&& proto_family
!= 0) {
553 socket_unlock(so
, 0);
554 dlil_detach_protocol(ifp
, proto_family
);
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
) {
566 /* If there are no other interfaces, detach PF_NDRV from the interface */
567 if (cur_np
== NULL
) {
568 socket_unlock(so
, 0);
569 dlil_detach_protocol(ifp
, PF_NDRV
);
574 if (np
->nd_laddr
!= NULL
) {
575 FREE((caddr_t
)np
->nd_laddr
, M_IFADDR
);
578 FREE((caddr_t
)np
, M_PCB
);
580 so
->so_flags
|= SOF_PCBCLEARING
;
586 ndrv_do_disconnect(struct ndrv_cb
*np
)
588 struct socket
* so
= np
->nd_socket
;
590 kprintf("NDRV disconnect: %x\n", np
);
594 FREE(np
->nd_faddr
, M_IFADDR
);
597 if (so
->so_state
& SS_NOFDREF
)
599 soisdisconnected(so
);
603 /* Hackery - return a string version of a decimal number */
605 sprint_d(u_int n
, char *buf
, int buflen
)
606 { char dbuf
[IFNAMSIZ
];
607 char *cp
= dbuf
+IFNAMSIZ
-1;
612 *cp
= "0123456789"[n
% 10];
614 } while (n
!= 0 && buflen
> 0);
615 strncpy(buf
, cp
, IFNAMSIZ
-buflen
);
620 * Try to compare a device name (q) with one of the funky ifnet
621 * device names (ifp).
623 static int name_cmp(struct ifnet
*ifp
, char *q
)
629 len
= strlen(ifp
->if_name
);
630 strncpy(r
, ifp
->if_name
, IFNAMSIZ
);
632 (void)sprint_d(ifp
->if_unit
, r
, IFNAMSIZ
-(r
-buf
));
634 kprintf("Comparing %s, %s\n", buf
, q
);
636 return(strncmp(buf
, q
, IFNAMSIZ
));
642 * When closing, dump any enqueued mbufs.
645 ndrv_flushq(struct ifqueue
*q
)
661 ndrv_setspec(struct ndrv_cb
*np
, struct sockopt
*sopt
)
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
;
668 struct socket
*so
= np
->nd_socket
;
670 /* Sanity checking */
671 if (np
->nd_proto_family
!= PF_NDRV
)
673 if (np
->nd_if
== NULL
)
675 if (sopt
->sopt_valsize
!= sizeof(struct ndrv_protocol_desc
))
678 /* Copy the ndrvSpec */
679 error
= sooptcopyin(sopt
, &ndrvSpec
, sizeof(struct ndrv_protocol_desc
),
680 sizeof(struct ndrv_protocol_desc
));
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
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
)
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
)
704 /* Copy the ndrv demux array from userland */
705 error
= copyin(CAST_USER_ADDR_T(ndrvSpec
.demux_list
), ndrvDemux
,
706 ndrvSpec
.demux_count
* sizeof(struct ndrv_demux_desc
));
707 ndrvSpec
.demux_list
= ndrvDemux
;
712 /* At this point, we've at least got enough bytes to start looking around */
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
;
720 dlilSpec
.event
= ndrv_event
;
721 dlilSpec
.protocol_family
= ndrvSpec
.protocol_family
;
723 for (demuxOn
= 0; demuxOn
< ndrvSpec
.demux_count
; demuxOn
++)
725 /* Convert an ndrv_demux_desc to a dlil_demux_desc */
726 error
= ndrv_to_dlil_demux(&ndrvSpec
.demux_list
[demuxOn
], &dlilDemux
[demuxOn
]);
730 /* Add the dlil_demux_desc to the list */
731 TAILQ_INSERT_TAIL(&dlilSpec
.demux_desc_head
, &dlilDemux
[demuxOn
], next
);
737 /* We've got all our ducks lined up...lets attach! */
738 socket_unlock(so
, 0);
739 error
= dlil_attach_protocol(&dlilSpec
);
742 np
->nd_proto_family
= dlilSpec
.protocol_family
;
745 /* Free any memory we've allocated */
747 FREE(dlilDemux
, M_TEMP
);
749 FREE(ndrvDemux
, M_TEMP
);
756 ndrv_to_dlil_demux(struct ndrv_demux_desc
* ndrv
, struct dlil_demux_desc
* dlil
)
758 bzero(dlil
, sizeof(*dlil
));
760 if (ndrv
->type
< DLIL_DESC_ETYPE2
)
762 /* using old "type", not supported */
766 if (ndrv
->length
> 28)
771 dlil
->type
= ndrv
->type
;
772 dlil
->native_type
= ndrv
->data
.other
;
773 dlil
->variants
.native_type_length
= ndrv
->length
;
779 ndrv_delspec(struct ndrv_cb
*np
)
783 if (np
->nd_proto_family
== PF_NDRV
||
784 np
->nd_proto_family
== 0)
787 /* Detach the protocol */
788 result
= dlil_detach_protocol(np
->nd_if
, np
->nd_proto_family
);
789 np
->nd_proto_family
= PF_NDRV
;
795 ndrv_find_inbound(struct ifnet
*ifp
, u_long protocol
)
799 if (protocol
== PF_NDRV
) return NULL
;
801 TAILQ_FOREACH(np
, &ndrvl
, nd_next
) {
802 if (np
->nd_proto_family
== protocol
&&
811 static void ndrv_dominit(void)
813 static int ndrv_dominited
= 0;
815 if (ndrv_dominited
== 0 &&
816 net_add_proto(&ndrvsw
, &ndrvdomain
) == 0)
821 ndrv_handle_ifp_detach(u_long family
, short unit
)
824 struct ifnet
*ifp
= NULL
;
827 /* Find all sockets using this interface. */
828 TAILQ_FOREACH(np
, &ndrvl
, nd_next
) {
829 if (np
->nd_family
== family
&&
832 /* This cb is using the detaching interface, but not for long. */
833 /* Let the protocol go */
835 if (np
->nd_proto_family
!= 0)
838 /* Delete the multicasts first */
839 ndrv_remove_all_multicast(np
);
841 /* Disavow all knowledge of the ifp */
847 /* Make sure sending returns an error */
848 /* Is this safe? Will we drop the funnel? */
849 lck_mtx_assert(so
->so_proto
->pr_domain
->dom_mtx
, LCK_MTX_ASSERT_OWNED
);
855 /* Unregister our protocol */
857 dlil_detach_protocol(ifp
, PF_NDRV
);
862 ndrv_do_add_multicast(struct ndrv_cb
*np
, struct sockopt
*sopt
)
864 struct ndrv_multiaddr
* ndrv_multi
;
867 if (sopt
->sopt_val
== NULL
|| sopt
->sopt_valsize
< 2 ||
868 sopt
->sopt_level
!= SOL_NDRVPROTO
)
870 if (np
->nd_if
== NULL
)
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
)
879 // Copy in the address
880 result
= copyin(sopt
->sopt_val
, &ndrv_multi
->addr
, sopt
->sopt_valsize
);
882 // Validate the sockaddr
883 if (result
== 0 && sopt
->sopt_valsize
!= ndrv_multi
->addr
.sa_len
)
886 if (result
== 0 && ndrv_have_multicast(np
, &ndrv_multi
->addr
))
891 // Try adding the multicast
892 result
= if_addmulti(np
->nd_if
, &ndrv_multi
->addr
, &ndrv_multi
->ifma
);
897 // Add to our linked list
898 ndrv_multi
->next
= np
->nd_multiaddrs
;
899 np
->nd_multiaddrs
= ndrv_multi
;
903 // Free up the memory, something went wrong
904 FREE(ndrv_multi
, M_IFADDR
);
911 ndrv_do_remove_multicast(struct ndrv_cb
*np
, struct sockopt
*sopt
)
913 struct sockaddr
* multi_addr
;
914 struct ndrv_multiaddr
* ndrv_entry
= NULL
;
917 if (sopt
->sopt_val
== NULL
|| sopt
->sopt_valsize
< 2 ||
918 sopt
->sopt_level
!= SOL_NDRVPROTO
)
920 if (np
->nd_if
== NULL
)
924 MALLOC(multi_addr
, struct sockaddr
*, sopt
->sopt_valsize
,
926 if (multi_addr
== NULL
)
929 // Copy in the address
930 result
= copyin(sopt
->sopt_val
, multi_addr
, sopt
->sopt_valsize
);
932 // Validate the sockaddr
933 if (result
== 0 && sopt
->sopt_valsize
!= multi_addr
->sa_len
)
938 /* Find the old entry */
939 ndrv_entry
= ndrv_have_multicast(np
, multi_addr
);
941 if (ndrv_entry
== NULL
)
947 // Try deleting the multicast
948 result
= if_delmultiaddr(ndrv_entry
->ifma
, 0);
953 // Remove from our linked list
954 struct ndrv_multiaddr
* cur
= np
->nd_multiaddrs
;
956 ifma_release(ndrv_entry
->ifma
);
958 if (cur
== ndrv_entry
)
960 np
->nd_multiaddrs
= cur
->next
;
964 for (cur
= cur
->next
; cur
!= NULL
; cur
= cur
->next
)
966 if (cur
->next
== ndrv_entry
)
968 cur
->next
= cur
->next
->next
;
975 FREE(ndrv_entry
, M_IFADDR
);
977 FREE(multi_addr
, M_TEMP
);
982 static struct ndrv_multiaddr
*
983 ndrv_have_multicast(struct ndrv_cb
*np
, struct sockaddr
* inAddr
)
985 struct ndrv_multiaddr
* cur
;
986 for (cur
= np
->nd_multiaddrs
; cur
!= NULL
; cur
= cur
->next
)
989 if ((inAddr
->sa_len
== cur
->addr
.sa_len
) &&
990 (bcmp(&cur
->addr
, inAddr
, inAddr
->sa_len
) == 0))
1001 ndrv_remove_all_multicast(struct ndrv_cb
* np
)
1003 struct ndrv_multiaddr
* cur
;
1005 if (np
->nd_if
!= NULL
)
1007 while (np
->nd_multiaddrs
!= NULL
)
1009 cur
= np
->nd_multiaddrs
;
1010 np
->nd_multiaddrs
= cur
->next
;
1012 if_delmultiaddr(cur
->ifma
, 0);
1013 ifma_release(cur
->ifma
);
1014 FREE(cur
, M_IFADDR
);
1019 struct pr_usrreqs ndrv_usrreqs
= {
1020 ndrv_abort
, pru_accept_notsupp
, ndrv_attach
, ndrv_bind
,
1021 ndrv_connect
, pru_connect2_notsupp
, pru_control_notsupp
, ndrv_detach
,
1022 ndrv_disconnect
, pru_listen_notsupp
, ndrv_peeraddr
, pru_rcvd_notsupp
,
1023 pru_rcvoob_notsupp
, ndrv_send
, pru_sense_null
, ndrv_shutdown
,
1024 ndrv_sockaddr
, sosend
, soreceive
, pru_sopoll_notsupp
1027 struct protosw ndrvsw
=
1028 { SOCK_RAW
, &ndrvdomain
, NDRVPROTO_NDRV
, PR_ATOMIC
|PR_ADDR
,
1029 0, ndrv_output
, 0, ndrv_ctloutput
,
1036 struct domain ndrvdomain
=
1037 { AF_NDRV
, "NetDriver", ndrv_dominit
, NULL
, NULL
,
1039 NULL
, NULL
, 0, 0, 0, 0