]>
Commit | Line | Data |
---|---|---|
1c79356b | 1 | /* |
fe8ab488 | 2 | * Copyright (c) 1997-2014 Apple Inc. All rights reserved. |
5d5c5d0d | 3 | * |
2d21ac55 | 4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ |
39037602 | 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. | |
39037602 | 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. | |
39037602 | 17 | * |
2d21ac55 A |
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. | |
39037602 | 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> | |
316670eb | 60 | #include <sys/sysctl.h> |
1c79356b A |
61 | #include <sys/errno.h> |
62 | #include <sys/syslog.h> | |
63 | #include <sys/proc.h> | |
64 | ||
65 | #include <kern/queue.h> | |
66 | ||
7b1edb79 | 67 | #include <net/ndrv.h> |
1c79356b A |
68 | #include <net/route.h> |
69 | #include <net/if_llc.h> | |
70 | #include <net/if_dl.h> | |
71 | #include <net/if_types.h> | |
7b1edb79 | 72 | #include <net/ndrv_var.h> |
2d21ac55 | 73 | #include <net/dlil.h> |
1c79356b A |
74 | |
75 | #if INET | |
76 | #include <netinet/in.h> | |
77 | #include <netinet/in_var.h> | |
78 | #endif | |
79 | #include <netinet/if_ether.h> | |
80 | ||
316670eb A |
81 | static unsigned int ndrv_multi_max_count = NDRV_DMUX_MAX_DESCR; |
82 | SYSCTL_UINT(_net, OID_AUTO, ndrv_multi_max_count, CTLFLAG_RW | CTLFLAG_LOCKED, | |
83 | &ndrv_multi_max_count, 0, "Number of allowed multicast addresses per NRDV socket"); | |
84 | ||
91447636 A |
85 | static int ndrv_do_detach(struct ndrv_cb *); |
86 | static int ndrv_do_disconnect(struct ndrv_cb *); | |
b0d623f7 | 87 | static struct ndrv_cb *ndrv_find_inbound(struct ifnet *ifp, u_int32_t protocol_family); |
91447636 A |
88 | static int ndrv_setspec(struct ndrv_cb *np, struct sockopt *sopt); |
89 | static int ndrv_delspec(struct ndrv_cb *); | |
2d21ac55 | 90 | static int ndrv_to_ifnet_demux(struct ndrv_demux_desc* ndrv, struct ifnet_demux_desc* ifdemux); |
b0d623f7 | 91 | static void ndrv_handle_ifp_detach(u_int32_t family, short unit); |
9bccf70c A |
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); | |
39236c6e | 96 | static void ndrv_dominit(struct domain *); |
1c79356b | 97 | |
b0d623f7 A |
98 | u_int32_t ndrv_sendspace = NDRVSNDQ; |
99 | u_int32_t ndrv_recvspace = NDRVRCVQ; | |
91447636 | 100 | TAILQ_HEAD(, ndrv_cb) ndrvl = TAILQ_HEAD_INITIALIZER(ndrvl); |
1c79356b | 101 | |
39236c6e A |
102 | static struct domain *ndrvdomain = NULL; |
103 | extern struct domain ndrvdomain_s; | |
1c79356b | 104 | |
6d2010ae A |
105 | #define NDRV_PROTODEMUX_COUNT 10 |
106 | ||
1c79356b | 107 | /* |
91447636 A |
108 | * Verify these values match. |
109 | * To keep clients from including dlil.h, we define | |
110 | * these values independently in ndrv.h. They must | |
111 | * match or a conversion function must be written. | |
1c79356b | 112 | */ |
91447636 A |
113 | #if NDRV_DEMUXTYPE_ETHERTYPE != DLIL_DESC_ETYPE2 |
114 | #error NDRV_DEMUXTYPE_ETHERTYPE must match DLIL_DESC_ETYPE2 | |
115 | #endif | |
116 | #if NDRV_DEMUXTYPE_SAP != DLIL_DESC_SAP | |
117 | #error NDRV_DEMUXTYPE_SAP must match DLIL_DESC_SAP | |
118 | #endif | |
119 | #if NDRV_DEMUXTYPE_SNAP != DLIL_DESC_SNAP | |
120 | #error NDRV_DEMUXTYPE_SNAP must match DLIL_DESC_SNAP | |
121 | #endif | |
1c79356b A |
122 | |
123 | /* | |
124 | * Protocol output - Called to output a raw network packet directly | |
125 | * to the driver. | |
126 | */ | |
91447636 A |
127 | static int |
128 | ndrv_output(struct mbuf *m, struct socket *so) | |
7b1edb79 | 129 | { |
91447636 A |
130 | struct ndrv_cb *np = sotondrvcb(so); |
131 | struct ifnet *ifp = np->nd_if; | |
7b1edb79 | 132 | int result = 0; |
1c79356b A |
133 | |
134 | #if NDRV_DEBUG | |
39037602 | 135 | printf("NDRV output: %x, %x, %x\n", m, so, np); |
1c79356b A |
136 | #endif |
137 | ||
138 | /* | |
139 | * No header is a format error | |
140 | */ | |
141 | if ((m->m_flags&M_PKTHDR) == 0) | |
142 | return(EINVAL); | |
143 | ||
2d21ac55 | 144 | /* Unlock before calling ifnet_output */ |
91447636 | 145 | socket_unlock(so, 0); |
39037602 | 146 | |
1c79356b | 147 | /* |
7b1edb79 A |
148 | * Call DLIL if we can. DLIL is much safer than calling the |
149 | * ifp directly. | |
150 | */ | |
2d21ac55 | 151 | result = ifnet_output_raw(ifp, np->nd_proto_family, m); |
39037602 | 152 | |
91447636 | 153 | socket_lock(so, 0); |
39037602 | 154 | |
7b1edb79 A |
155 | return (result); |
156 | } | |
157 | ||
158 | /* Our input routine called from DLIL */ | |
2d21ac55 A |
159 | static errno_t |
160 | ndrv_input( | |
161 | ifnet_t ifp, | |
162 | protocol_family_t proto_family, | |
163 | mbuf_t m, | |
164 | char *frame_header) | |
7b1edb79 | 165 | { |
1c79356b | 166 | struct socket *so; |
2d21ac55 | 167 | struct sockaddr_dl ndrvsrc; |
91447636 A |
168 | struct ndrv_cb *np; |
169 | int error = 0; | |
1c79356b | 170 | |
2d21ac55 A |
171 | ndrvsrc.sdl_len = sizeof (struct sockaddr_dl); |
172 | ndrvsrc.sdl_family = AF_NDRV; | |
173 | ndrvsrc.sdl_index = 0; | |
1c79356b | 174 | |
7b1edb79 | 175 | /* move packet from if queue to socket */ |
1c79356b | 176 | /* Should be media-independent */ |
7b1edb79 A |
177 | ndrvsrc.sdl_type = IFT_ETHER; |
178 | ndrvsrc.sdl_nlen = 0; | |
179 | ndrvsrc.sdl_alen = 6; | |
180 | ndrvsrc.sdl_slen = 0; | |
181 | bcopy(frame_header, &ndrvsrc.sdl_data, 6); | |
1c79356b | 182 | |
91447636 | 183 | np = ndrv_find_inbound(ifp, proto_family); |
1c79356b | 184 | if (np == NULL) |
7b1edb79 | 185 | { |
1c79356b A |
186 | return(ENOENT); |
187 | } | |
188 | so = np->nd_socket; | |
7b1edb79 | 189 | /* prepend the frame header */ |
2d21ac55 | 190 | m = m_prepend(m, ifnet_hdrlen(ifp), M_NOWAIT); |
7b1edb79 A |
191 | if (m == NULL) |
192 | return EJUSTRETURN; | |
2d21ac55 | 193 | bcopy(frame_header, m->m_data, ifnet_hdrlen(ifp)); |
91447636 | 194 | |
5ba3f43e | 195 | LCK_MTX_ASSERT(ndrvdomain->dom_mtx, LCK_MTX_ASSERT_NOTOWNED); |
39236c6e | 196 | lck_mtx_lock(ndrvdomain->dom_mtx); |
1c79356b | 197 | if (sbappendaddr(&(so->so_rcv), (struct sockaddr *)&ndrvsrc, |
91447636 | 198 | m, (struct mbuf *)0, &error) != 0) { |
1c79356b | 199 | sorwakeup(so); |
91447636 | 200 | } |
39236c6e | 201 | lck_mtx_unlock(ndrvdomain->dom_mtx); |
91447636 | 202 | return 0; /* radar 4030377 - always return 0 */ |
1c79356b A |
203 | } |
204 | ||
205 | /* | |
206 | * Allocate an ndrv control block and some buffer space for the socket | |
207 | */ | |
91447636 A |
208 | static int |
209 | ndrv_attach(struct socket *so, int proto, __unused struct proc *p) | |
7b1edb79 A |
210 | { |
211 | int error; | |
91447636 | 212 | struct ndrv_cb *np = sotondrvcb(so); |
1c79356b A |
213 | |
214 | if ((so->so_state & SS_PRIV) == 0) | |
215 | return(EPERM); | |
216 | ||
217 | #if NDRV_DEBUG | |
39037602 | 218 | printf("NDRV attach: %x, %x, %x\n", so, proto, np); |
1c79356b | 219 | #endif |
55e303ae A |
220 | |
221 | if ((error = soreserve(so, ndrv_sendspace, ndrv_recvspace))) | |
222 | return(error); | |
223 | ||
1c79356b | 224 | MALLOC(np, struct ndrv_cb *, sizeof(*np), M_PCB, M_WAITOK); |
0b4e3aa0 A |
225 | if (np == NULL) |
226 | return (ENOMEM); | |
7b1edb79 A |
227 | so->so_pcb = (caddr_t)np; |
228 | bzero(np, sizeof(*np)); | |
1c79356b | 229 | #if NDRV_DEBUG |
39037602 | 230 | printf("NDRV attach: %x, %x, %x\n", so, proto, np); |
1c79356b | 231 | #endif |
1c79356b A |
232 | TAILQ_INIT(&np->nd_dlist); |
233 | np->nd_signature = NDRV_SIGNATURE; | |
234 | np->nd_socket = so; | |
39236c6e | 235 | np->nd_proto.sp_family = SOCK_DOM(so); |
1c79356b | 236 | np->nd_proto.sp_protocol = proto; |
7b1edb79 | 237 | np->nd_if = NULL; |
91447636 | 238 | np->nd_proto_family = 0; |
7b1edb79 A |
239 | np->nd_family = 0; |
240 | np->nd_unit = 0; | |
91447636 | 241 | TAILQ_INSERT_TAIL(&ndrvl, np, nd_next); |
1c79356b A |
242 | return(0); |
243 | } | |
244 | ||
245 | /* | |
246 | * Destroy state just before socket deallocation. | |
247 | * Flush data or not depending on the options. | |
248 | */ | |
249 | ||
91447636 | 250 | static int |
1c79356b A |
251 | ndrv_detach(struct socket *so) |
252 | { | |
91447636 | 253 | struct ndrv_cb *np = sotondrvcb(so); |
1c79356b A |
254 | |
255 | if (np == 0) | |
256 | return EINVAL; | |
257 | return ndrv_do_detach(np); | |
258 | } | |
259 | ||
260 | ||
261 | /* | |
262 | * If a socket isn't bound to a single address, | |
263 | * the ndrv input routine will hand it anything | |
264 | * within that protocol family (assuming there's | |
265 | * nothing else around it should go to). | |
266 | * | |
267 | * Don't expect this to be used. | |
268 | */ | |
269 | ||
91447636 A |
270 | static int |
271 | ndrv_connect(struct socket *so, struct sockaddr *nam, __unused struct proc *p) | |
1c79356b | 272 | { |
91447636 | 273 | struct ndrv_cb *np = sotondrvcb(so); |
1c79356b A |
274 | |
275 | if (np == 0) | |
276 | return EINVAL; | |
277 | ||
278 | if (np->nd_faddr) | |
279 | return EISCONN; | |
39037602 | 280 | |
b0d623f7 A |
281 | /* Allocate memory to store the remote address */ |
282 | MALLOC(np->nd_faddr, struct sockaddr_ndrv*, | |
7b1edb79 | 283 | nam->sa_len, M_IFADDR, M_WAITOK); |
b0d623f7 A |
284 | if (np->nd_faddr == NULL) |
285 | return ENOMEM; | |
39037602 | 286 | |
7b1edb79 | 287 | bcopy((caddr_t) nam, (caddr_t) np->nd_faddr, nam->sa_len); |
1c79356b A |
288 | soisconnected(so); |
289 | return 0; | |
290 | } | |
291 | ||
91447636 | 292 | static void |
2d21ac55 A |
293 | ndrv_event(struct ifnet *ifp, __unused protocol_family_t protocol, |
294 | const struct kev_msg *event) | |
91447636 A |
295 | { |
296 | if (event->vendor_code == KEV_VENDOR_APPLE && | |
297 | event->kev_class == KEV_NETWORK_CLASS && | |
298 | event->kev_subclass == KEV_DL_SUBCLASS && | |
299 | event->event_code == KEV_DL_IF_DETACHING) { | |
5ba3f43e | 300 | LCK_MTX_ASSERT(ndrvdomain->dom_mtx, LCK_MTX_ASSERT_NOTOWNED); |
39236c6e | 301 | lck_mtx_lock(ndrvdomain->dom_mtx); |
2d21ac55 | 302 | ndrv_handle_ifp_detach(ifnet_family(ifp), ifnet_unit(ifp)); |
39236c6e | 303 | lck_mtx_unlock(ndrvdomain->dom_mtx); |
91447636 A |
304 | } |
305 | } | |
306 | ||
307 | static int name_cmp(struct ifnet *, char *); | |
308 | ||
1c79356b A |
309 | /* |
310 | * This is the "driver open" hook - we 'bind' to the | |
311 | * named driver. | |
7b1edb79 | 312 | * Here's where we latch onto the driver. |
1c79356b | 313 | */ |
91447636 A |
314 | static int |
315 | ndrv_bind(struct socket *so, struct sockaddr *nam, __unused struct proc *p) | |
7b1edb79 | 316 | { |
91447636 A |
317 | struct sockaddr_ndrv *sa = (struct sockaddr_ndrv *) nam; |
318 | char *dname; | |
319 | struct ndrv_cb *np; | |
320 | struct ifnet *ifp; | |
7b1edb79 | 321 | int result; |
1c79356b | 322 | |
91447636 | 323 | if TAILQ_EMPTY(&ifnet_head) |
1c79356b A |
324 | return(EADDRNOTAVAIL); /* Quick sanity check */ |
325 | np = sotondrvcb(so); | |
326 | if (np == 0) | |
327 | return EINVAL; | |
328 | ||
329 | if (np->nd_laddr) | |
330 | return EINVAL; /* XXX */ | |
331 | ||
332 | /* I think we just latch onto a copy here; the caller frees */ | |
333 | np->nd_laddr = _MALLOC(sizeof(struct sockaddr_ndrv), M_IFADDR, M_WAITOK); | |
334 | if (np->nd_laddr == NULL) | |
335 | return(ENOMEM); | |
336 | bcopy((caddr_t) sa, (caddr_t) np->nd_laddr, sizeof(struct sockaddr_ndrv)); | |
2d21ac55 | 337 | dname = (char *) sa->snd_name; |
4d15aeb1 | 338 | np->nd_laddr->snd_len = sizeof(struct sockaddr_ndrv); |
1c79356b A |
339 | if (*dname == '\0') |
340 | return(EINVAL); | |
341 | #if NDRV_DEBUG | |
39037602 | 342 | printf("NDRV bind: %x, %x, %s\n", so, np, dname); |
1c79356b A |
343 | #endif |
344 | /* Track down the driver and its ifnet structure. | |
345 | * There's no internal call for this so we have to dup the code | |
346 | * in if.c/ifconf() | |
347 | */ | |
91447636 A |
348 | ifnet_head_lock_shared(); |
349 | TAILQ_FOREACH(ifp, &ifnet_head, if_link) { | |
1c79356b A |
350 | if (name_cmp(ifp, dname) == 0) |
351 | break; | |
352 | } | |
91447636 | 353 | ifnet_head_done(); |
1c79356b A |
354 | |
355 | if (ifp == NULL) | |
356 | return(EADDRNOTAVAIL); | |
39037602 | 357 | |
91447636 | 358 | // PPP doesn't support PF_NDRV. |
2d21ac55 | 359 | if (ifnet_family(ifp) != APPLE_IF_FAM_PPP) |
91447636 A |
360 | { |
361 | /* NDRV on this interface */ | |
2d21ac55 | 362 | struct ifnet_attach_proto_param ndrv_proto; |
91447636 A |
363 | result = 0; |
364 | bzero(&ndrv_proto, sizeof(ndrv_proto)); | |
91447636 | 365 | ndrv_proto.event = ndrv_event; |
39037602 | 366 | |
91447636 | 367 | /* We aren't worried about double attaching, that should just return an error */ |
0c530ab8 | 368 | socket_unlock(so, 0); |
2d21ac55 | 369 | result = ifnet_attach_protocol(ifp, PF_NDRV, &ndrv_proto); |
0c530ab8 | 370 | socket_lock(so, 0); |
91447636 A |
371 | if (result && result != EEXIST) { |
372 | return result; | |
373 | } | |
374 | np->nd_proto_family = PF_NDRV; | |
375 | } | |
376 | else { | |
377 | np->nd_proto_family = 0; | |
378 | } | |
39037602 | 379 | |
1c79356b | 380 | np->nd_if = ifp; |
2d21ac55 A |
381 | np->nd_family = ifnet_family(ifp); |
382 | np->nd_unit = ifnet_unit(ifp); | |
39037602 | 383 | |
1c79356b A |
384 | return(0); |
385 | } | |
386 | ||
91447636 | 387 | static int |
1c79356b A |
388 | ndrv_disconnect(struct socket *so) |
389 | { | |
91447636 | 390 | struct ndrv_cb *np = sotondrvcb(so); |
1c79356b A |
391 | |
392 | if (np == 0) | |
393 | return EINVAL; | |
394 | ||
395 | if (np->nd_faddr == 0) | |
396 | return ENOTCONN; | |
397 | ||
398 | ndrv_do_disconnect(np); | |
399 | return 0; | |
400 | } | |
401 | ||
402 | /* | |
403 | * Mark the connection as being incapable of further input. | |
404 | */ | |
91447636 | 405 | static int |
1c79356b A |
406 | ndrv_shutdown(struct socket *so) |
407 | { | |
5ba3f43e | 408 | LCK_MTX_ASSERT(ndrvdomain->dom_mtx, LCK_MTX_ASSERT_OWNED); |
1c79356b A |
409 | socantsendmore(so); |
410 | return 0; | |
411 | } | |
412 | ||
413 | /* | |
414 | * Ship a packet out. The ndrv output will pass it | |
415 | * to the appropriate driver. The really tricky part | |
416 | * is the destination address... | |
417 | */ | |
91447636 A |
418 | static int |
419 | ndrv_send(struct socket *so, __unused int flags, struct mbuf *m, | |
420 | __unused struct sockaddr *addr, struct mbuf *control, | |
421 | __unused struct proc *p) | |
1c79356b A |
422 | { |
423 | int error; | |
424 | ||
425 | if (control) | |
426 | return EOPNOTSUPP; | |
427 | ||
428 | error = ndrv_output(m, so); | |
429 | m = NULL; | |
430 | return error; | |
431 | } | |
432 | ||
433 | ||
91447636 | 434 | static int |
1c79356b A |
435 | ndrv_abort(struct socket *so) |
436 | { | |
91447636 | 437 | struct ndrv_cb *np = sotondrvcb(so); |
1c79356b A |
438 | |
439 | if (np == 0) | |
440 | return EINVAL; | |
441 | ||
442 | ndrv_do_disconnect(np); | |
443 | return 0; | |
444 | } | |
445 | ||
91447636 | 446 | static int |
1c79356b A |
447 | ndrv_sockaddr(struct socket *so, struct sockaddr **nam) |
448 | { | |
91447636 | 449 | struct ndrv_cb *np = sotondrvcb(so); |
1c79356b A |
450 | int len; |
451 | ||
452 | if (np == 0) | |
453 | return EINVAL; | |
454 | ||
455 | if (np->nd_laddr == 0) | |
456 | return EINVAL; | |
457 | ||
458 | len = np->nd_laddr->snd_len; | |
91447636 A |
459 | MALLOC(*nam, struct sockaddr *, len, M_SONAME, M_WAITOK); |
460 | if (*nam == NULL) | |
461 | return ENOMEM; | |
1c79356b A |
462 | bcopy((caddr_t)np->nd_laddr, *nam, |
463 | (unsigned)len); | |
464 | return 0; | |
465 | } | |
466 | ||
467 | ||
91447636 | 468 | static int |
1c79356b A |
469 | ndrv_peeraddr(struct socket *so, struct sockaddr **nam) |
470 | { | |
91447636 | 471 | struct ndrv_cb *np = sotondrvcb(so); |
1c79356b A |
472 | int len; |
473 | ||
474 | if (np == 0) | |
475 | return EINVAL; | |
476 | ||
477 | if (np->nd_faddr == 0) | |
478 | return ENOTCONN; | |
479 | ||
480 | len = np->nd_faddr->snd_len; | |
91447636 A |
481 | MALLOC(*nam, struct sockaddr *, len, M_SONAME, M_WAITOK); |
482 | if (*nam == NULL) | |
483 | return ENOMEM; | |
1c79356b A |
484 | bcopy((caddr_t)np->nd_faddr, *nam, |
485 | (unsigned)len); | |
486 | return 0; | |
487 | } | |
488 | ||
489 | ||
1c79356b A |
490 | /* Control output */ |
491 | ||
91447636 | 492 | static int |
1c79356b | 493 | ndrv_ctloutput(struct socket *so, struct sockopt *sopt) |
7b1edb79 | 494 | { |
91447636 | 495 | struct ndrv_cb *np = sotondrvcb(so); |
7b1edb79 | 496 | int error = 0; |
39037602 | 497 | |
7b1edb79 A |
498 | switch(sopt->sopt_name) |
499 | { | |
500 | case NDRV_DELDMXSPEC: /* Delete current spec */ | |
501 | /* Verify no parameter was passed */ | |
2d21ac55 | 502 | if (sopt->sopt_val != 0 || sopt->sopt_valsize != 0) { |
7b1edb79 A |
503 | /* |
504 | * We don't support deleting a specific demux, it's | |
505 | * all or nothing. | |
506 | */ | |
507 | return EINVAL; | |
508 | } | |
509 | error = ndrv_delspec(np); | |
510 | break; | |
511 | case NDRV_SETDMXSPEC: /* Set protocol spec */ | |
512 | error = ndrv_setspec(np, sopt); | |
513 | break; | |
9bccf70c A |
514 | case NDRV_ADDMULTICAST: |
515 | error = ndrv_do_add_multicast(np, sopt); | |
516 | break; | |
517 | case NDRV_DELMULTICAST: | |
518 | error = ndrv_do_remove_multicast(np, sopt); | |
519 | break; | |
7b1edb79 A |
520 | default: |
521 | error = ENOTSUP; | |
522 | } | |
1c79356b A |
523 | #ifdef NDRV_DEBUG |
524 | log(LOG_WARNING, "NDRV CTLOUT: %x returns %d\n", sopt->sopt_name, | |
525 | error); | |
526 | #endif | |
527 | return(error); | |
528 | } | |
529 | ||
91447636 A |
530 | static int |
531 | ndrv_do_detach(struct ndrv_cb *np) | |
7b1edb79 A |
532 | { |
533 | struct ndrv_cb* cur_np = NULL; | |
534 | struct socket *so = np->nd_socket; | |
55e303ae | 535 | int error = 0; |
0c530ab8 | 536 | struct ifnet * ifp; |
1c79356b A |
537 | |
538 | #if NDRV_DEBUG | |
39037602 | 539 | printf("NDRV detach: %x, %x\n", so, np); |
1c79356b | 540 | #endif |
9bccf70c | 541 | ndrv_remove_all_multicast(np); |
2d21ac55 | 542 | |
0c530ab8 A |
543 | ifp = np->nd_if; |
544 | /* Remove from the linked list of control blocks */ | |
545 | TAILQ_REMOVE(&ndrvl, np, nd_next); | |
546 | if (ifp != NULL) { | |
b0d623f7 | 547 | u_int32_t proto_family = np->nd_proto_family; |
0c530ab8 A |
548 | |
549 | if (proto_family != PF_NDRV && proto_family != 0) { | |
550 | socket_unlock(so, 0); | |
2d21ac55 | 551 | ifnet_detach_protocol(ifp, proto_family); |
0c530ab8 | 552 | socket_lock(so, 0); |
91447636 | 553 | } |
39037602 | 554 | |
91447636 A |
555 | /* Check if this is the last socket attached to this interface */ |
556 | TAILQ_FOREACH(cur_np, &ndrvl, nd_next) { | |
557 | if (cur_np->nd_family == np->nd_family && | |
558 | cur_np->nd_unit == np->nd_unit) { | |
559 | break; | |
560 | } | |
561 | } | |
39037602 | 562 | |
91447636 A |
563 | /* If there are no other interfaces, detach PF_NDRV from the interface */ |
564 | if (cur_np == NULL) { | |
0c530ab8 | 565 | socket_unlock(so, 0); |
2d21ac55 | 566 | ifnet_detach_protocol(ifp, PF_NDRV); |
0c530ab8 | 567 | socket_lock(so, 0); |
91447636 A |
568 | } |
569 | } | |
0c530ab8 A |
570 | if (np->nd_laddr != NULL) { |
571 | FREE((caddr_t)np->nd_laddr, M_IFADDR); | |
572 | np->nd_laddr = NULL; | |
573 | } | |
1c79356b A |
574 | FREE((caddr_t)np, M_PCB); |
575 | so->so_pcb = 0; | |
91447636 | 576 | so->so_flags |= SOF_PCBCLEARING; |
1c79356b | 577 | sofree(so); |
7b1edb79 | 578 | return error; |
1c79356b A |
579 | } |
580 | ||
91447636 A |
581 | static int |
582 | ndrv_do_disconnect(struct ndrv_cb *np) | |
1c79356b | 583 | { |
0c530ab8 | 584 | struct socket * so = np->nd_socket; |
1c79356b | 585 | #if NDRV_DEBUG |
39037602 | 586 | printf("NDRV disconnect: %x\n", np); |
1c79356b A |
587 | #endif |
588 | if (np->nd_faddr) | |
7b1edb79 A |
589 | { |
590 | FREE(np->nd_faddr, M_IFADDR); | |
1c79356b A |
591 | np->nd_faddr = 0; |
592 | } | |
39236c6e A |
593 | /* |
594 | * A multipath subflow socket would have its SS_NOFDREF set by default, | |
595 | * so check for SOF_MP_SUBFLOW socket flag before detaching the PCB; | |
596 | * when the socket is closed for real, SOF_MP_SUBFLOW would be cleared. | |
597 | */ | |
598 | if (!(so->so_flags & SOF_MP_SUBFLOW) && (so->so_state & SS_NOFDREF)) | |
1c79356b | 599 | ndrv_do_detach(np); |
0c530ab8 | 600 | soisdisconnected(so); |
1c79356b A |
601 | return(0); |
602 | } | |
603 | ||
91447636 | 604 | /* Hackery - return a string version of a decimal number */ |
316670eb | 605 | static void |
91447636 A |
606 | sprint_d(u_int n, char *buf, int buflen) |
607 | { char dbuf[IFNAMSIZ]; | |
608 | char *cp = dbuf+IFNAMSIZ-1; | |
609 | ||
610 | *cp = 0; | |
611 | do { buflen--; | |
612 | cp--; | |
613 | *cp = "0123456789"[n % 10]; | |
614 | n /= 10; | |
615 | } while (n != 0 && buflen > 0); | |
fe8ab488 | 616 | strlcpy(buf, cp, IFNAMSIZ-buflen); |
316670eb | 617 | return; |
91447636 A |
618 | } |
619 | ||
1c79356b A |
620 | /* |
621 | * Try to compare a device name (q) with one of the funky ifnet | |
622 | * device names (ifp). | |
623 | */ | |
91447636 A |
624 | static int name_cmp(struct ifnet *ifp, char *q) |
625 | { char *r; | |
626 | int len; | |
1c79356b | 627 | char buf[IFNAMSIZ]; |
1c79356b A |
628 | |
629 | r = buf; | |
2d21ac55 | 630 | len = strlen(ifnet_name(ifp)); |
fe8ab488 | 631 | strlcpy(r, ifnet_name(ifp), IFNAMSIZ); |
1c79356b | 632 | r += len; |
316670eb | 633 | sprint_d(ifnet_unit(ifp), r, IFNAMSIZ-(r-buf)); |
1c79356b | 634 | #if NDRV_DEBUG |
39037602 | 635 | printf("Comparing %s, %s\n", buf, q); |
1c79356b A |
636 | #endif |
637 | return(strncmp(buf, q, IFNAMSIZ)); | |
638 | } | |
639 | ||
91447636 A |
640 | #if 0 |
641 | //### Not used | |
1c79356b A |
642 | /* |
643 | * When closing, dump any enqueued mbufs. | |
644 | */ | |
645 | void | |
91447636 | 646 | ndrv_flushq(struct ifqueue *q) |
7b1edb79 | 647 | { |
91447636 | 648 | struct mbuf *m; |
1c79356b | 649 | for (;;) |
7b1edb79 | 650 | { |
1c79356b A |
651 | IF_DEQUEUE(q, m); |
652 | if (m == NULL) | |
653 | break; | |
654 | IF_DROP(q); | |
1c79356b A |
655 | if (m) |
656 | m_freem(m); | |
657 | } | |
1c79356b | 658 | } |
39037602 | 659 | #endif |
1c79356b A |
660 | |
661 | int | |
7b1edb79 A |
662 | ndrv_setspec(struct ndrv_cb *np, struct sockopt *sopt) |
663 | { | |
2d21ac55 A |
664 | struct ifnet_attach_proto_param proto_param; |
665 | struct ndrv_protocol_desc ndrvSpec; | |
666 | struct ndrv_demux_desc* ndrvDemux = NULL; | |
667 | int error = 0; | |
39037602 | 668 | struct socket * so = np->nd_socket; |
2d21ac55 | 669 | user_addr_t user_addr; |
39037602 | 670 | |
2d21ac55 A |
671 | /* Sanity checking */ |
672 | if (np->nd_proto_family != PF_NDRV) | |
673 | return EBUSY; | |
674 | if (np->nd_if == NULL) | |
675 | return EINVAL; | |
676 | ||
677 | /* Copy the ndrvSpec */ | |
b0d623f7 | 678 | if (proc_is64bit(sopt->sopt_p)) { |
2d21ac55 A |
679 | struct ndrv_protocol_desc64 ndrvSpec64; |
680 | ||
681 | if (sopt->sopt_valsize != sizeof(ndrvSpec64)) | |
682 | return EINVAL; | |
39037602 | 683 | |
2d21ac55 A |
684 | error = sooptcopyin(sopt, &ndrvSpec64, sizeof(ndrvSpec64), sizeof(ndrvSpec64)); |
685 | if (error != 0) | |
686 | return error; | |
687 | ||
688 | ndrvSpec.version = ndrvSpec64.version; | |
689 | ndrvSpec.protocol_family = ndrvSpec64.protocol_family; | |
690 | ndrvSpec.demux_count = ndrvSpec64.demux_count; | |
691 | ||
692 | user_addr = ndrvSpec64.demux_list; | |
693 | } | |
694 | else { | |
b0d623f7 A |
695 | struct ndrv_protocol_desc32 ndrvSpec32; |
696 | ||
697 | if (sopt->sopt_valsize != sizeof(ndrvSpec32)) | |
2d21ac55 | 698 | return EINVAL; |
39037602 | 699 | |
b0d623f7 | 700 | error = sooptcopyin(sopt, &ndrvSpec32, sizeof(ndrvSpec32), sizeof(ndrvSpec32)); |
2d21ac55 A |
701 | if (error != 0) |
702 | return error; | |
703 | ||
b0d623f7 A |
704 | ndrvSpec.version = ndrvSpec32.version; |
705 | ndrvSpec.protocol_family = ndrvSpec32.protocol_family; | |
706 | ndrvSpec.demux_count = ndrvSpec32.demux_count; | |
707 | ||
708 | user_addr = CAST_USER_ADDR_T(ndrvSpec32.demux_list); | |
2d21ac55 | 709 | } |
39037602 | 710 | |
2d21ac55 A |
711 | /* Verify the parameter */ |
712 | if (ndrvSpec.version > NDRV_PROTOCOL_DESC_VERS) | |
713 | return ENOTSUP; // version is too new! | |
714 | else if (ndrvSpec.version < 1) | |
715 | return EINVAL; // version is not valid | |
6d2010ae A |
716 | else if (ndrvSpec.demux_count > NDRV_PROTODEMUX_COUNT || ndrvSpec.demux_count == 0) |
717 | return EINVAL; // demux_count is not valid | |
39037602 | 718 | |
2d21ac55 A |
719 | bzero(&proto_param, sizeof(proto_param)); |
720 | proto_param.demux_count = ndrvSpec.demux_count; | |
39037602 | 721 | |
2d21ac55 A |
722 | /* Allocate storage for demux array */ |
723 | MALLOC(ndrvDemux, struct ndrv_demux_desc*, proto_param.demux_count * | |
724 | sizeof(struct ndrv_demux_desc), M_TEMP, M_WAITOK); | |
725 | if (ndrvDemux == NULL) | |
726 | return ENOMEM; | |
39037602 | 727 | |
2d21ac55 A |
728 | /* Allocate enough ifnet_demux_descs */ |
729 | MALLOC(proto_param.demux_array, struct ifnet_demux_desc*, | |
730 | sizeof(*proto_param.demux_array) * ndrvSpec.demux_count, | |
731 | M_TEMP, M_WAITOK); | |
732 | if (proto_param.demux_array == NULL) | |
733 | error = ENOMEM; | |
39037602 | 734 | |
2d21ac55 A |
735 | if (error == 0) |
736 | { | |
737 | /* Copy the ndrv demux array from userland */ | |
738 | error = copyin(user_addr, ndrvDemux, | |
739 | ndrvSpec.demux_count * sizeof(struct ndrv_demux_desc)); | |
740 | ndrvSpec.demux_list = ndrvDemux; | |
741 | } | |
39037602 | 742 | |
2d21ac55 A |
743 | if (error == 0) |
744 | { | |
745 | /* At this point, we've at least got enough bytes to start looking around */ | |
b0d623f7 | 746 | u_int32_t demuxOn = 0; |
39037602 | 747 | |
2d21ac55 A |
748 | proto_param.demux_count = ndrvSpec.demux_count; |
749 | proto_param.input = ndrv_input; | |
750 | proto_param.event = ndrv_event; | |
39037602 | 751 | |
2d21ac55 A |
752 | for (demuxOn = 0; demuxOn < ndrvSpec.demux_count; demuxOn++) |
753 | { | |
754 | /* Convert an ndrv_demux_desc to a ifnet_demux_desc */ | |
755 | error = ndrv_to_ifnet_demux(&ndrvSpec.demux_list[demuxOn], | |
756 | &proto_param.demux_array[demuxOn]); | |
757 | if (error) | |
758 | break; | |
759 | } | |
760 | } | |
39037602 | 761 | |
2d21ac55 A |
762 | if (error == 0) |
763 | { | |
764 | /* We've got all our ducks lined up...lets attach! */ | |
765 | socket_unlock(so, 0); | |
766 | error = ifnet_attach_protocol(np->nd_if, ndrvSpec.protocol_family, | |
767 | &proto_param); | |
768 | socket_lock(so, 0); | |
769 | if (error == 0) | |
770 | np->nd_proto_family = ndrvSpec.protocol_family; | |
771 | } | |
39037602 | 772 | |
2d21ac55 A |
773 | /* Free any memory we've allocated */ |
774 | if (proto_param.demux_array) | |
775 | FREE(proto_param.demux_array, M_TEMP); | |
776 | if (ndrvDemux) | |
777 | FREE(ndrvDemux, M_TEMP); | |
39037602 | 778 | |
2d21ac55 | 779 | return error; |
1c79356b A |
780 | } |
781 | ||
1c79356b | 782 | |
7b1edb79 | 783 | int |
2d21ac55 | 784 | ndrv_to_ifnet_demux(struct ndrv_demux_desc* ndrv, struct ifnet_demux_desc* ifdemux) |
7b1edb79 | 785 | { |
2d21ac55 | 786 | bzero(ifdemux, sizeof(*ifdemux)); |
39037602 | 787 | |
7b1edb79 A |
788 | if (ndrv->type < DLIL_DESC_ETYPE2) |
789 | { | |
790 | /* using old "type", not supported */ | |
791 | return ENOTSUP; | |
792 | } | |
39037602 | 793 | |
7b1edb79 A |
794 | if (ndrv->length > 28) |
795 | { | |
796 | return EINVAL; | |
797 | } | |
39037602 | 798 | |
2d21ac55 A |
799 | ifdemux->type = ndrv->type; |
800 | ifdemux->data = ndrv->data.other; | |
801 | ifdemux->datalen = ndrv->length; | |
39037602 | 802 | |
7b1edb79 | 803 | return 0; |
1c79356b A |
804 | } |
805 | ||
806 | int | |
7b1edb79 A |
807 | ndrv_delspec(struct ndrv_cb *np) |
808 | { | |
809 | int result = 0; | |
39037602 | 810 | |
91447636 A |
811 | if (np->nd_proto_family == PF_NDRV || |
812 | np->nd_proto_family == 0) | |
7b1edb79 | 813 | return EINVAL; |
39037602 | 814 | |
7b1edb79 | 815 | /* Detach the protocol */ |
2d21ac55 | 816 | result = ifnet_detach_protocol(np->nd_if, np->nd_proto_family); |
91447636 | 817 | np->nd_proto_family = PF_NDRV; |
39037602 | 818 | |
7b1edb79 | 819 | return result; |
1c79356b A |
820 | } |
821 | ||
822 | struct ndrv_cb * | |
b0d623f7 | 823 | ndrv_find_inbound(struct ifnet *ifp, u_int32_t protocol) |
7b1edb79 A |
824 | { |
825 | struct ndrv_cb* np; | |
39037602 | 826 | |
91447636 | 827 | if (protocol == PF_NDRV) return NULL; |
39037602 | 828 | |
91447636 A |
829 | TAILQ_FOREACH(np, &ndrvl, nd_next) { |
830 | if (np->nd_proto_family == protocol && | |
831 | np->nd_if == ifp) { | |
7b1edb79 A |
832 | return np; |
833 | } | |
834 | } | |
39037602 | 835 | |
7b1edb79 | 836 | return NULL; |
1c79356b A |
837 | } |
838 | ||
91447636 | 839 | static void |
b0d623f7 | 840 | ndrv_handle_ifp_detach(u_int32_t family, short unit) |
1c79356b | 841 | { |
7b1edb79 | 842 | struct ndrv_cb* np; |
91447636 A |
843 | struct ifnet *ifp = NULL; |
844 | struct socket *so; | |
39037602 | 845 | |
7b1edb79 | 846 | /* Find all sockets using this interface. */ |
91447636 | 847 | TAILQ_FOREACH(np, &ndrvl, nd_next) { |
7b1edb79 A |
848 | if (np->nd_family == family && |
849 | np->nd_unit == unit) | |
850 | { | |
851 | /* This cb is using the detaching interface, but not for long. */ | |
852 | /* Let the protocol go */ | |
91447636 A |
853 | ifp = np->nd_if; |
854 | if (np->nd_proto_family != 0) | |
7b1edb79 | 855 | ndrv_delspec(np); |
39037602 | 856 | |
9bccf70c A |
857 | /* Delete the multicasts first */ |
858 | ndrv_remove_all_multicast(np); | |
39037602 | 859 | |
7b1edb79 A |
860 | /* Disavow all knowledge of the ifp */ |
861 | np->nd_if = NULL; | |
862 | np->nd_unit = 0; | |
863 | np->nd_family = 0; | |
39037602 A |
864 | |
865 | so = np->nd_socket; | |
7b1edb79 | 866 | /* Make sure sending returns an error */ |
5ba3f43e | 867 | LCK_MTX_ASSERT(ndrvdomain->dom_mtx, LCK_MTX_ASSERT_OWNED); |
91447636 A |
868 | socantsendmore(so); |
869 | socantrcvmore(so); | |
1c79356b | 870 | } |
7b1edb79 | 871 | } |
39037602 | 872 | |
7b1edb79 | 873 | /* Unregister our protocol */ |
91447636 | 874 | if (ifp) { |
2d21ac55 | 875 | ifnet_detach_protocol(ifp, PF_NDRV); |
7b1edb79 | 876 | } |
1c79356b A |
877 | } |
878 | ||
9bccf70c A |
879 | static int |
880 | ndrv_do_add_multicast(struct ndrv_cb *np, struct sockopt *sopt) | |
881 | { | |
882 | struct ndrv_multiaddr* ndrv_multi; | |
883 | int result; | |
39037602 | 884 | |
2d21ac55 | 885 | if (sopt->sopt_val == 0 || sopt->sopt_valsize < 2 || |
316670eb | 886 | sopt->sopt_level != SOL_NDRVPROTO || sopt->sopt_valsize > SOCK_MAXADDRLEN) |
9bccf70c A |
887 | return EINVAL; |
888 | if (np->nd_if == NULL) | |
889 | return ENXIO; | |
316670eb A |
890 | if (!(np->nd_dlist_cnt < ndrv_multi_max_count)) |
891 | return EPERM; | |
39037602 | 892 | |
9bccf70c A |
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; | |
39037602 | 898 | |
9bccf70c A |
899 | // Copy in the address |
900 | result = copyin(sopt->sopt_val, &ndrv_multi->addr, sopt->sopt_valsize); | |
39037602 | 901 | |
9bccf70c A |
902 | // Validate the sockaddr |
903 | if (result == 0 && sopt->sopt_valsize != ndrv_multi->addr.sa_len) | |
904 | result = EINVAL; | |
39037602 | 905 | |
9bccf70c A |
906 | if (result == 0 && ndrv_have_multicast(np, &ndrv_multi->addr)) |
907 | result = EEXIST; | |
39037602 | 908 | |
9bccf70c A |
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 | 914 | } |
39037602 | 915 | |
9bccf70c A |
916 | if (result == 0) |
917 | { | |
918 | // Add to our linked list | |
919 | ndrv_multi->next = np->nd_multiaddrs; | |
920 | np->nd_multiaddrs = ndrv_multi; | |
316670eb | 921 | np->nd_dlist_cnt++; |
9bccf70c A |
922 | } |
923 | else | |
924 | { | |
925 | // Free up the memory, something went wrong | |
926 | FREE(ndrv_multi, M_IFADDR); | |
927 | } | |
39037602 | 928 | |
9bccf70c A |
929 | return result; |
930 | } | |
931 | ||
932 | static int | |
933 | ndrv_do_remove_multicast(struct ndrv_cb *np, struct sockopt *sopt) | |
934 | { | |
935 | struct sockaddr* multi_addr; | |
936 | struct ndrv_multiaddr* ndrv_entry = NULL; | |
937 | int result; | |
39037602 | 938 | |
2d21ac55 | 939 | if (sopt->sopt_val == 0 || sopt->sopt_valsize < 2 || |
9bccf70c A |
940 | sopt->sopt_level != SOL_NDRVPROTO) |
941 | return EINVAL; | |
316670eb | 942 | if (np->nd_if == NULL || np->nd_dlist_cnt == 0) |
9bccf70c | 943 | return ENXIO; |
39037602 | 944 | |
9bccf70c A |
945 | // Allocate storage |
946 | MALLOC(multi_addr, struct sockaddr*, sopt->sopt_valsize, | |
947 | M_TEMP, M_WAITOK); | |
948 | if (multi_addr == NULL) | |
949 | return ENOMEM; | |
39037602 | 950 | |
9bccf70c A |
951 | // Copy in the address |
952 | result = copyin(sopt->sopt_val, multi_addr, sopt->sopt_valsize); | |
39037602 | 953 | |
9bccf70c A |
954 | // Validate the sockaddr |
955 | if (result == 0 && sopt->sopt_valsize != multi_addr->sa_len) | |
956 | result = EINVAL; | |
39037602 | 957 | |
9bccf70c A |
958 | if (result == 0) |
959 | { | |
960 | /* Find the old entry */ | |
961 | ndrv_entry = ndrv_have_multicast(np, multi_addr); | |
39037602 | 962 | |
9bccf70c A |
963 | if (ndrv_entry == NULL) |
964 | result = ENOENT; | |
965 | } | |
39037602 | 966 | |
9bccf70c A |
967 | if (result == 0) |
968 | { | |
969 | // Try deleting the multicast | |
2d21ac55 | 970 | result = ifnet_remove_multicast(ndrv_entry->ifma); |
9bccf70c | 971 | } |
39037602 | 972 | |
9bccf70c A |
973 | if (result == 0) |
974 | { | |
975 | // Remove from our linked list | |
976 | struct ndrv_multiaddr* cur = np->nd_multiaddrs; | |
39037602 | 977 | |
2d21ac55 | 978 | ifmaddr_release(ndrv_entry->ifma); |
39037602 | 979 | |
9bccf70c A |
980 | if (cur == ndrv_entry) |
981 | { | |
982 | np->nd_multiaddrs = cur->next; | |
983 | } | |
984 | else | |
985 | { | |
986 | for (cur = cur->next; cur != NULL; cur = cur->next) | |
987 | { | |
988 | if (cur->next == ndrv_entry) | |
989 | { | |
990 | cur->next = cur->next->next; | |
991 | break; | |
992 | } | |
993 | } | |
994 | } | |
39037602 | 995 | |
316670eb | 996 | np->nd_dlist_cnt--; |
39037602 | 997 | |
9bccf70c A |
998 | // Free the memory |
999 | FREE(ndrv_entry, M_IFADDR); | |
1000 | } | |
1001 | FREE(multi_addr, M_TEMP); | |
39037602 | 1002 | |
9bccf70c A |
1003 | return result; |
1004 | } | |
1005 | ||
1006 | static struct ndrv_multiaddr* | |
1007 | ndrv_have_multicast(struct ndrv_cb *np, struct sockaddr* inAddr) | |
1008 | { | |
1009 | struct ndrv_multiaddr* cur; | |
1010 | for (cur = np->nd_multiaddrs; cur != NULL; cur = cur->next) | |
1011 | { | |
39037602 | 1012 | |
9bccf70c A |
1013 | if ((inAddr->sa_len == cur->addr.sa_len) && |
1014 | (bcmp(&cur->addr, inAddr, inAddr->sa_len) == 0)) | |
1015 | { | |
1016 | // Found a match | |
1017 | return cur; | |
1018 | } | |
1019 | } | |
39037602 | 1020 | |
9bccf70c A |
1021 | return NULL; |
1022 | } | |
1023 | ||
1024 | static void | |
1025 | ndrv_remove_all_multicast(struct ndrv_cb* np) | |
1026 | { | |
1027 | struct ndrv_multiaddr* cur; | |
39037602 | 1028 | |
9bccf70c A |
1029 | if (np->nd_if != NULL) |
1030 | { | |
1031 | while (np->nd_multiaddrs != NULL) | |
1032 | { | |
1033 | cur = np->nd_multiaddrs; | |
1034 | np->nd_multiaddrs = cur->next; | |
39037602 | 1035 | |
2d21ac55 A |
1036 | ifnet_remove_multicast(cur->ifma); |
1037 | ifmaddr_release(cur->ifma); | |
9bccf70c A |
1038 | FREE(cur, M_IFADDR); |
1039 | } | |
1040 | } | |
1041 | } | |
1042 | ||
39236c6e A |
1043 | static struct pr_usrreqs ndrv_usrreqs = { |
1044 | .pru_abort = ndrv_abort, | |
1045 | .pru_attach = ndrv_attach, | |
1046 | .pru_bind = ndrv_bind, | |
1047 | .pru_connect = ndrv_connect, | |
1048 | .pru_detach = ndrv_detach, | |
1049 | .pru_disconnect = ndrv_disconnect, | |
1050 | .pru_peeraddr = ndrv_peeraddr, | |
1051 | .pru_send = ndrv_send, | |
1052 | .pru_shutdown = ndrv_shutdown, | |
1053 | .pru_sockaddr = ndrv_sockaddr, | |
1054 | .pru_sosend = sosend, | |
1055 | .pru_soreceive = soreceive, | |
1c79356b A |
1056 | }; |
1057 | ||
39236c6e A |
1058 | static struct protosw ndrvsw[] = { |
1059 | { | |
1060 | .pr_type = SOCK_RAW, | |
1061 | .pr_protocol = NDRVPROTO_NDRV, | |
1062 | .pr_flags = PR_ATOMIC|PR_ADDR, | |
1063 | .pr_output = ndrv_output, | |
1064 | .pr_ctloutput = ndrv_ctloutput, | |
1065 | .pr_usrreqs = &ndrv_usrreqs, | |
1066 | } | |
1c79356b A |
1067 | }; |
1068 | ||
39236c6e A |
1069 | static int ndrv_proto_count = (sizeof (ndrvsw) / sizeof (struct protosw)); |
1070 | ||
1071 | struct domain ndrvdomain_s = { | |
1072 | .dom_family = PF_NDRV, | |
1073 | .dom_name = "NetDriver", | |
1074 | .dom_init = ndrv_dominit, | |
1c79356b | 1075 | }; |
39236c6e A |
1076 | |
1077 | static void | |
1078 | ndrv_dominit(struct domain *dp) | |
1079 | { | |
1080 | struct protosw *pr; | |
1081 | int i; | |
1082 | ||
1083 | VERIFY(!(dp->dom_flags & DOM_INITIALIZED)); | |
1084 | VERIFY(ndrvdomain == NULL); | |
1085 | ||
1086 | ndrvdomain = dp; | |
1087 | ||
1088 | for (i = 0, pr = &ndrvsw[0]; i < ndrv_proto_count; i++, pr++) | |
1089 | net_add_proto(pr, dp, 1); | |
1090 | } |