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 lck_mtx_assert(ndrvdomain
.dom_mtx
, LCK_MTX_ASSERT_NOTOWNED
);
299 lck_mtx_lock(ndrvdomain
.dom_mtx
);
300 ndrv_handle_ifp_detach(ifp
->if_family
, ifp
->if_unit
);
301 lck_mtx_unlock(ndrvdomain
.dom_mtx
);
305 static int name_cmp(struct ifnet
*, char *);
308 * This is the "driver open" hook - we 'bind' to the
310 * Here's where we latch onto the driver.
313 ndrv_bind(struct socket
*so
, struct sockaddr
*nam
, __unused
struct proc
*p
)
315 struct sockaddr_ndrv
*sa
= (struct sockaddr_ndrv
*) nam
;
321 if TAILQ_EMPTY(&ifnet_head
)
322 return(EADDRNOTAVAIL
); /* Quick sanity check */
328 return EINVAL
; /* XXX */
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
)
334 bcopy((caddr_t
) sa
, (caddr_t
) np
->nd_laddr
, sizeof(struct sockaddr_ndrv
));
335 dname
= sa
->snd_name
;
339 kprintf("NDRV bind: %x, %x, %s\n", so
, np
, dname
);
341 /* Track down the driver and its ifnet structure.
342 * There's no internal call for this so we have to dup the code
345 ifnet_head_lock_shared();
346 TAILQ_FOREACH(ifp
, &ifnet_head
, if_link
) {
347 if (name_cmp(ifp
, dname
) == 0)
353 return(EADDRNOTAVAIL
);
355 // PPP doesn't support PF_NDRV.
356 if (ifp
->if_family
!= APPLE_IF_FAM_PPP
)
358 /* NDRV on this interface */
359 struct dlil_proto_reg_str ndrv_proto
;
361 bzero(&ndrv_proto
, sizeof(ndrv_proto
));
362 TAILQ_INIT(&ndrv_proto
.demux_desc_head
);
364 ndrv_proto
.interface_family
= ifp
->if_family
;
365 ndrv_proto
.protocol_family
= PF_NDRV
;
366 ndrv_proto
.unit_number
= ifp
->if_unit
;
367 ndrv_proto
.event
= ndrv_event
;
369 /* We aren't worried about double attaching, that should just return an error */
370 socket_unlock(so
, 0);
371 result
= dlil_attach_protocol(&ndrv_proto
);
373 if (result
&& result
!= EEXIST
) {
376 np
->nd_proto_family
= PF_NDRV
;
379 np
->nd_proto_family
= 0;
383 np
->nd_family
= ifp
->if_family
;
384 np
->nd_unit
= ifp
->if_unit
;
390 ndrv_disconnect(struct socket
*so
)
392 struct ndrv_cb
*np
= sotondrvcb(so
);
397 if (np
->nd_faddr
== 0)
400 ndrv_do_disconnect(np
);
405 * Mark the connection as being incapable of further input.
408 ndrv_shutdown(struct socket
*so
)
410 lck_mtx_assert(so
->so_proto
->pr_domain
->dom_mtx
, LCK_MTX_ASSERT_OWNED
);
416 * Ship a packet out. The ndrv output will pass it
417 * to the appropriate driver. The really tricky part
418 * is the destination address...
421 ndrv_send(struct socket
*so
, __unused
int flags
, struct mbuf
*m
,
422 __unused
struct sockaddr
*addr
, struct mbuf
*control
,
423 __unused
struct proc
*p
)
430 error
= ndrv_output(m
, so
);
437 ndrv_abort(struct socket
*so
)
439 struct ndrv_cb
*np
= sotondrvcb(so
);
444 ndrv_do_disconnect(np
);
449 ndrv_sockaddr(struct socket
*so
, struct sockaddr
**nam
)
451 struct ndrv_cb
*np
= sotondrvcb(so
);
457 if (np
->nd_laddr
== 0)
460 len
= np
->nd_laddr
->snd_len
;
461 MALLOC(*nam
, struct sockaddr
*, len
, M_SONAME
, M_WAITOK
);
464 bcopy((caddr_t
)np
->nd_laddr
, *nam
,
471 ndrv_peeraddr(struct socket
*so
, struct sockaddr
**nam
)
473 struct ndrv_cb
*np
= sotondrvcb(so
);
479 if (np
->nd_faddr
== 0)
482 len
= np
->nd_faddr
->snd_len
;
483 MALLOC(*nam
, struct sockaddr
*, len
, M_SONAME
, M_WAITOK
);
486 bcopy((caddr_t
)np
->nd_faddr
, *nam
,
495 ndrv_ctloutput(struct socket
*so
, struct sockopt
*sopt
)
497 struct ndrv_cb
*np
= sotondrvcb(so
);
500 switch(sopt
->sopt_name
)
502 case NDRV_DELDMXSPEC
: /* Delete current spec */
503 /* Verify no parameter was passed */
504 if (sopt
->sopt_val
!= NULL
|| sopt
->sopt_valsize
!= 0) {
506 * We don't support deleting a specific demux, it's
511 error
= ndrv_delspec(np
);
513 case NDRV_SETDMXSPEC
: /* Set protocol spec */
514 error
= ndrv_setspec(np
, sopt
);
516 case NDRV_ADDMULTICAST
:
517 error
= ndrv_do_add_multicast(np
, sopt
);
519 case NDRV_DELMULTICAST
:
520 error
= ndrv_do_remove_multicast(np
, sopt
);
526 log(LOG_WARNING
, "NDRV CTLOUT: %x returns %d\n", sopt
->sopt_name
,
533 ndrv_do_detach(struct ndrv_cb
*np
)
535 struct ndrv_cb
* cur_np
= NULL
;
536 struct socket
*so
= np
->nd_socket
;
541 kprintf("NDRV detach: %x, %x\n", so
, np
);
543 ndrv_remove_all_multicast(np
);
545 /* Remove from the linked list of control blocks */
546 TAILQ_REMOVE(&ndrvl
, np
, nd_next
);
548 u_long proto_family
= np
->nd_proto_family
;
550 if (proto_family
!= PF_NDRV
&& proto_family
!= 0) {
551 socket_unlock(so
, 0);
552 dlil_detach_protocol(ifp
, proto_family
);
556 /* Check if this is the last socket attached to this interface */
557 TAILQ_FOREACH(cur_np
, &ndrvl
, nd_next
) {
558 if (cur_np
->nd_family
== np
->nd_family
&&
559 cur_np
->nd_unit
== np
->nd_unit
) {
564 /* If there are no other interfaces, detach PF_NDRV from the interface */
565 if (cur_np
== NULL
) {
566 socket_unlock(so
, 0);
567 dlil_detach_protocol(ifp
, PF_NDRV
);
572 if (np
->nd_laddr
!= NULL
) {
573 FREE((caddr_t
)np
->nd_laddr
, M_IFADDR
);
576 FREE((caddr_t
)np
, M_PCB
);
578 so
->so_flags
|= SOF_PCBCLEARING
;
584 ndrv_do_disconnect(struct ndrv_cb
*np
)
586 struct socket
* so
= np
->nd_socket
;
588 kprintf("NDRV disconnect: %x\n", np
);
592 FREE(np
->nd_faddr
, M_IFADDR
);
595 if (so
->so_state
& SS_NOFDREF
)
597 soisdisconnected(so
);
601 /* Hackery - return a string version of a decimal number */
603 sprint_d(u_int n
, char *buf
, int buflen
)
604 { char dbuf
[IFNAMSIZ
];
605 char *cp
= dbuf
+IFNAMSIZ
-1;
610 *cp
= "0123456789"[n
% 10];
612 } while (n
!= 0 && buflen
> 0);
613 strncpy(buf
, cp
, IFNAMSIZ
-buflen
);
618 * Try to compare a device name (q) with one of the funky ifnet
619 * device names (ifp).
621 static int name_cmp(struct ifnet
*ifp
, char *q
)
627 len
= strlen(ifp
->if_name
);
628 strncpy(r
, ifp
->if_name
, IFNAMSIZ
);
630 (void)sprint_d(ifp
->if_unit
, r
, IFNAMSIZ
-(r
-buf
));
632 kprintf("Comparing %s, %s\n", buf
, q
);
634 return(strncmp(buf
, q
, IFNAMSIZ
));
640 * When closing, dump any enqueued mbufs.
643 ndrv_flushq(struct ifqueue
*q
)
659 ndrv_setspec(struct ndrv_cb
*np
, struct sockopt
*sopt
)
661 struct dlil_proto_reg_str dlilSpec
;
662 struct ndrv_protocol_desc ndrvSpec
;
663 struct dlil_demux_desc
* dlilDemux
= NULL
;
664 struct ndrv_demux_desc
* ndrvDemux
= NULL
;
666 struct socket
*so
= np
->nd_socket
;
668 /* Sanity checking */
669 if (np
->nd_proto_family
!= PF_NDRV
)
671 if (np
->nd_if
== NULL
)
673 if (sopt
->sopt_valsize
!= sizeof(struct ndrv_protocol_desc
))
676 /* Copy the ndrvSpec */
677 error
= sooptcopyin(sopt
, &ndrvSpec
, sizeof(struct ndrv_protocol_desc
),
678 sizeof(struct ndrv_protocol_desc
));
682 /* Verify the parameter */
683 if (ndrvSpec
.version
> NDRV_PROTOCOL_DESC_VERS
)
684 return ENOTSUP
; // version is too new!
685 else if (ndrvSpec
.version
< 1)
686 return EINVAL
; // version is not valid
688 /* Allocate storage for demux array */
689 MALLOC(ndrvDemux
, struct ndrv_demux_desc
*,
690 ndrvSpec
.demux_count
* sizeof(struct ndrv_demux_desc
), M_TEMP
, M_WAITOK
);
691 if (ndrvDemux
== NULL
)
694 /* Allocate enough dlil_demux_descs */
695 MALLOC(dlilDemux
, struct dlil_demux_desc
*,
696 sizeof(*dlilDemux
) * ndrvSpec
.demux_count
, M_TEMP
, M_WAITOK
);
697 if (dlilDemux
== NULL
)
702 /* Copy the ndrv demux array from userland */
703 error
= copyin(CAST_USER_ADDR_T(ndrvSpec
.demux_list
), ndrvDemux
,
704 ndrvSpec
.demux_count
* sizeof(struct ndrv_demux_desc
));
705 ndrvSpec
.demux_list
= ndrvDemux
;
710 /* At this point, we've at least got enough bytes to start looking around */
713 bzero(&dlilSpec
, sizeof(dlilSpec
));
714 TAILQ_INIT(&dlilSpec
.demux_desc_head
);
715 dlilSpec
.interface_family
= np
->nd_family
;
716 dlilSpec
.unit_number
= np
->nd_unit
;
717 dlilSpec
.input
= ndrv_input
;
718 dlilSpec
.event
= ndrv_event
;
719 dlilSpec
.protocol_family
= ndrvSpec
.protocol_family
;
721 for (demuxOn
= 0; demuxOn
< ndrvSpec
.demux_count
; demuxOn
++)
723 /* Convert an ndrv_demux_desc to a dlil_demux_desc */
724 error
= ndrv_to_dlil_demux(&ndrvSpec
.demux_list
[demuxOn
], &dlilDemux
[demuxOn
]);
728 /* Add the dlil_demux_desc to the list */
729 TAILQ_INSERT_TAIL(&dlilSpec
.demux_desc_head
, &dlilDemux
[demuxOn
], next
);
735 /* We've got all our ducks lined up...lets attach! */
736 socket_unlock(so
, 0);
737 error
= dlil_attach_protocol(&dlilSpec
);
740 np
->nd_proto_family
= dlilSpec
.protocol_family
;
743 /* Free any memory we've allocated */
745 FREE(dlilDemux
, M_TEMP
);
747 FREE(ndrvDemux
, M_TEMP
);
754 ndrv_to_dlil_demux(struct ndrv_demux_desc
* ndrv
, struct dlil_demux_desc
* dlil
)
756 bzero(dlil
, sizeof(*dlil
));
758 if (ndrv
->type
< DLIL_DESC_ETYPE2
)
760 /* using old "type", not supported */
764 if (ndrv
->length
> 28)
769 dlil
->type
= ndrv
->type
;
770 dlil
->native_type
= ndrv
->data
.other
;
771 dlil
->variants
.native_type_length
= ndrv
->length
;
777 ndrv_delspec(struct ndrv_cb
*np
)
781 if (np
->nd_proto_family
== PF_NDRV
||
782 np
->nd_proto_family
== 0)
785 /* Detach the protocol */
786 result
= dlil_detach_protocol(np
->nd_if
, np
->nd_proto_family
);
787 np
->nd_proto_family
= PF_NDRV
;
793 ndrv_find_inbound(struct ifnet
*ifp
, u_long protocol
)
797 if (protocol
== PF_NDRV
) return NULL
;
799 TAILQ_FOREACH(np
, &ndrvl
, nd_next
) {
800 if (np
->nd_proto_family
== protocol
&&
809 static void ndrv_dominit(void)
811 static int ndrv_dominited
= 0;
813 if (ndrv_dominited
== 0 &&
814 net_add_proto(&ndrvsw
, &ndrvdomain
) == 0)
819 ndrv_handle_ifp_detach(u_long family
, short unit
)
822 struct ifnet
*ifp
= NULL
;
825 /* Find all sockets using this interface. */
826 TAILQ_FOREACH(np
, &ndrvl
, nd_next
) {
827 if (np
->nd_family
== family
&&
830 /* This cb is using the detaching interface, but not for long. */
831 /* Let the protocol go */
833 if (np
->nd_proto_family
!= 0)
836 /* Delete the multicasts first */
837 ndrv_remove_all_multicast(np
);
839 /* Disavow all knowledge of the ifp */
845 /* Make sure sending returns an error */
846 /* Is this safe? Will we drop the funnel? */
847 lck_mtx_assert(so
->so_proto
->pr_domain
->dom_mtx
, LCK_MTX_ASSERT_OWNED
);
853 /* Unregister our protocol */
855 dlil_detach_protocol(ifp
, PF_NDRV
);
860 ndrv_do_add_multicast(struct ndrv_cb
*np
, struct sockopt
*sopt
)
862 struct ndrv_multiaddr
* ndrv_multi
;
865 if (sopt
->sopt_val
== NULL
|| sopt
->sopt_valsize
< 2 ||
866 sopt
->sopt_level
!= SOL_NDRVPROTO
)
868 if (np
->nd_if
== NULL
)
872 MALLOC(ndrv_multi
, struct ndrv_multiaddr
*, sizeof(struct ndrv_multiaddr
) -
873 sizeof(struct sockaddr
) + sopt
->sopt_valsize
, M_IFADDR
, M_WAITOK
);
874 if (ndrv_multi
== NULL
)
877 // Copy in the address
878 result
= copyin(sopt
->sopt_val
, &ndrv_multi
->addr
, sopt
->sopt_valsize
);
880 // Validate the sockaddr
881 if (result
== 0 && sopt
->sopt_valsize
!= ndrv_multi
->addr
.sa_len
)
884 if (result
== 0 && ndrv_have_multicast(np
, &ndrv_multi
->addr
))
889 // Try adding the multicast
890 result
= if_addmulti(np
->nd_if
, &ndrv_multi
->addr
, &ndrv_multi
->ifma
);
895 // Add to our linked list
896 ndrv_multi
->next
= np
->nd_multiaddrs
;
897 np
->nd_multiaddrs
= ndrv_multi
;
901 // Free up the memory, something went wrong
902 FREE(ndrv_multi
, M_IFADDR
);
909 ndrv_do_remove_multicast(struct ndrv_cb
*np
, struct sockopt
*sopt
)
911 struct sockaddr
* multi_addr
;
912 struct ndrv_multiaddr
* ndrv_entry
= NULL
;
915 if (sopt
->sopt_val
== NULL
|| sopt
->sopt_valsize
< 2 ||
916 sopt
->sopt_level
!= SOL_NDRVPROTO
)
918 if (np
->nd_if
== NULL
)
922 MALLOC(multi_addr
, struct sockaddr
*, sopt
->sopt_valsize
,
924 if (multi_addr
== NULL
)
927 // Copy in the address
928 result
= copyin(sopt
->sopt_val
, multi_addr
, sopt
->sopt_valsize
);
930 // Validate the sockaddr
931 if (result
== 0 && sopt
->sopt_valsize
!= multi_addr
->sa_len
)
936 /* Find the old entry */
937 ndrv_entry
= ndrv_have_multicast(np
, multi_addr
);
939 if (ndrv_entry
== NULL
)
945 // Try deleting the multicast
946 result
= if_delmultiaddr(ndrv_entry
->ifma
, 0);
951 // Remove from our linked list
952 struct ndrv_multiaddr
* cur
= np
->nd_multiaddrs
;
954 ifma_release(ndrv_entry
->ifma
);
956 if (cur
== ndrv_entry
)
958 np
->nd_multiaddrs
= cur
->next
;
962 for (cur
= cur
->next
; cur
!= NULL
; cur
= cur
->next
)
964 if (cur
->next
== ndrv_entry
)
966 cur
->next
= cur
->next
->next
;
973 FREE(ndrv_entry
, M_IFADDR
);
975 FREE(multi_addr
, M_TEMP
);
980 static struct ndrv_multiaddr
*
981 ndrv_have_multicast(struct ndrv_cb
*np
, struct sockaddr
* inAddr
)
983 struct ndrv_multiaddr
* cur
;
984 for (cur
= np
->nd_multiaddrs
; cur
!= NULL
; cur
= cur
->next
)
987 if ((inAddr
->sa_len
== cur
->addr
.sa_len
) &&
988 (bcmp(&cur
->addr
, inAddr
, inAddr
->sa_len
) == 0))
999 ndrv_remove_all_multicast(struct ndrv_cb
* np
)
1001 struct ndrv_multiaddr
* cur
;
1003 if (np
->nd_if
!= NULL
)
1005 while (np
->nd_multiaddrs
!= NULL
)
1007 cur
= np
->nd_multiaddrs
;
1008 np
->nd_multiaddrs
= cur
->next
;
1010 if_delmultiaddr(cur
->ifma
, 0);
1011 ifma_release(cur
->ifma
);
1012 FREE(cur
, M_IFADDR
);
1017 struct pr_usrreqs ndrv_usrreqs
= {
1018 ndrv_abort
, pru_accept_notsupp
, ndrv_attach
, ndrv_bind
,
1019 ndrv_connect
, pru_connect2_notsupp
, pru_control_notsupp
, ndrv_detach
,
1020 ndrv_disconnect
, pru_listen_notsupp
, ndrv_peeraddr
, pru_rcvd_notsupp
,
1021 pru_rcvoob_notsupp
, ndrv_send
, pru_sense_null
, ndrv_shutdown
,
1022 ndrv_sockaddr
, sosend
, soreceive
, pru_sopoll_notsupp
1025 struct protosw ndrvsw
=
1026 { SOCK_RAW
, &ndrvdomain
, NDRVPROTO_NDRV
, PR_ATOMIC
|PR_ADDR
,
1027 0, ndrv_output
, 0, ndrv_ctloutput
,
1034 struct domain ndrvdomain
=
1035 { AF_NDRV
, "NetDriver", ndrv_dominit
, NULL
, NULL
,
1037 NULL
, NULL
, 0, 0, 0, 0