]>
Commit | Line | Data |
---|---|---|
1c79356b A |
1 | /* |
2 | * Copyright (c) 2000 Apple Computer, Inc. All rights reserved. | |
3 | * | |
4 | * @APPLE_LICENSE_HEADER_START@ | |
5 | * | |
6 | * The contents of this file constitute Original Code as defined in and | |
7 | * are subject to the Apple Public Source License Version 1.1 (the | |
8 | * "License"). You may not use this file except in compliance with the | |
9 | * License. Please obtain a copy of the License at | |
10 | * http://www.apple.com/publicsource and read it before using this file. | |
11 | * | |
12 | * This Original Code and all software distributed under the License are | |
13 | * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER | |
14 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, | |
15 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
16 | * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the | |
17 | * License for the specific language governing rights and limitations | |
18 | * under the License. | |
19 | * | |
20 | * @APPLE_LICENSE_HEADER_END@ | |
21 | */ | |
22 | /* Copyright (c) 1997, 1998 Apple Computer, Inc. All Rights Reserved */ | |
23 | /* | |
24 | * @(#)ndrv.c 1.1 (MacOSX) 6/10/43 | |
25 | * Justin Walker, 970604 | |
26 | * AF_NDRV support | |
27 | * 980130 - Cleanup, reorg, performance improvemements | |
28 | * 000816 - Removal of Y adapter cruft | |
29 | */ | |
30 | ||
31 | /* | |
32 | * PF_NDRV allows raw access to a specified network device, directly | |
33 | * with a socket. Expected use involves a socket option to request | |
34 | * protocol packets. This lets ndrv_output() call dlil_output(), and | |
35 | * lets DLIL find the proper recipient for incoming packets. | |
36 | * The purpose here is for user-mode protocol implementation. | |
37 | * Note that "pure raw access" will still be accomplished with BPF. | |
38 | * | |
39 | * In addition to the former use, when combined with socket NKEs, | |
40 | * PF_NDRV permits a fairly flexible mechanism for implementing | |
41 | * strange protocol support. One of the main ones will be the | |
42 | * BlueBox/Classic Shared IP Address support. | |
43 | */ | |
7b1edb79 | 44 | #include <mach/mach_types.h> |
1c79356b A |
45 | |
46 | #include <sys/param.h> | |
47 | #include <sys/systm.h> | |
48 | #include <sys/kernel.h> | |
49 | #include <sys/malloc.h> | |
50 | #include <sys/mbuf.h> | |
51 | #include <sys/protosw.h> | |
52 | #include <sys/domain.h> | |
53 | #include <sys/socket.h> | |
54 | #include <sys/socketvar.h> | |
55 | #include <sys/ioctl.h> | |
56 | #include <sys/errno.h> | |
57 | #include <sys/syslog.h> | |
58 | #include <sys/proc.h> | |
59 | ||
60 | #include <kern/queue.h> | |
61 | ||
7b1edb79 | 62 | #include <net/ndrv.h> |
1c79356b A |
63 | #include <net/netisr.h> |
64 | #include <net/route.h> | |
65 | #include <net/if_llc.h> | |
66 | #include <net/if_dl.h> | |
67 | #include <net/if_types.h> | |
7b1edb79 | 68 | #include <net/ndrv_var.h> |
1c79356b A |
69 | |
70 | #if INET | |
71 | #include <netinet/in.h> | |
72 | #include <netinet/in_var.h> | |
73 | #endif | |
74 | #include <netinet/if_ether.h> | |
75 | ||
1c79356b A |
76 | #include <machine/spl.h> |
77 | ||
78 | int ndrv_do_detach(struct ndrv_cb *); | |
79 | int ndrv_do_disconnect(struct ndrv_cb *); | |
7b1edb79 A |
80 | struct ndrv_cb *ndrv_find_tag(unsigned int); |
81 | void ndrv_read_event(struct socket* inSo, caddr_t ref, int waitf); | |
82 | int ndrv_setspec(struct ndrv_cb *np, struct sockopt *sopt); | |
83 | int ndrv_delspec(struct ndrv_cb *); | |
84 | int ndrv_to_dlil_demux(struct ndrv_demux_desc* ndrv, struct dlil_demux_desc* dlil); | |
85 | void ndrv_handle_ifp_detach(u_long family, short unit); | |
9bccf70c A |
86 | static int ndrv_do_add_multicast(struct ndrv_cb *np, struct sockopt *sopt); |
87 | static int ndrv_do_remove_multicast(struct ndrv_cb *np, struct sockopt *sopt); | |
88 | static struct ndrv_multiaddr* ndrv_have_multicast(struct ndrv_cb *np, struct sockaddr* addr); | |
89 | static void ndrv_remove_all_multicast(struct ndrv_cb *np); | |
1c79356b A |
90 | |
91 | unsigned long ndrv_sendspace = NDRVSNDQ; | |
92 | unsigned long ndrv_recvspace = NDRVRCVQ; | |
93 | struct ndrv_cb ndrvl; /* Head of controlblock list */ | |
94 | ||
1c79356b | 95 | struct domain ndrvdomain; |
7b1edb79 A |
96 | struct protosw ndrvsw; |
97 | static struct socket* ndrv_so; | |
1c79356b A |
98 | |
99 | ||
100 | /* | |
101 | * Protocol init function for NDRV protocol | |
102 | * Init the control block list. | |
103 | */ | |
104 | void | |
105 | ndrv_init() | |
106 | { | |
7b1edb79 A |
107 | int retval; |
108 | struct kev_request kev_request; | |
109 | ||
1c79356b | 110 | ndrvl.nd_next = ndrvl.nd_prev = &ndrvl; |
7b1edb79 A |
111 | |
112 | /* Create a PF_SYSTEM socket so we can listen for events */ | |
113 | retval = socreate(PF_SYSTEM, &ndrv_so, SOCK_RAW, SYSPROTO_EVENT); | |
114 | if (retval != 0 || ndrv_so == NULL) | |
115 | retval = KERN_FAILURE; | |
116 | ||
117 | /* Install a callback function for the socket */ | |
118 | ndrv_so->so_rcv.sb_flags |= SB_NOTIFY|SB_UPCALL; | |
119 | ndrv_so->so_upcall = ndrv_read_event; | |
120 | ndrv_so->so_upcallarg = NULL; | |
121 | ||
122 | /* Configure the socket to receive the events we're interested in */ | |
123 | kev_request.vendor_code = KEV_VENDOR_APPLE; | |
124 | kev_request.kev_class = KEV_NETWORK_CLASS; | |
125 | kev_request.kev_subclass = KEV_DL_SUBCLASS; | |
126 | retval = ndrv_so->so_proto->pr_usrreqs->pru_control(ndrv_so, SIOCSKEVFILT, (caddr_t)&kev_request, 0, 0); | |
127 | if (retval != 0) | |
128 | { | |
129 | /* | |
130 | * We will not get attaching or detaching events in this case. | |
131 | * We should probably prevent any sockets from binding so we won't | |
132 | * panic later if the interface goes away. | |
133 | */ | |
134 | log(LOG_WARNING, "PF_NDRV: ndrv_init - failed to set event filter (%d)", | |
135 | retval); | |
136 | } | |
1c79356b A |
137 | } |
138 | ||
139 | /* | |
140 | * Protocol output - Called to output a raw network packet directly | |
141 | * to the driver. | |
142 | */ | |
143 | int | |
144 | ndrv_output(register struct mbuf *m, register struct socket *so) | |
7b1edb79 A |
145 | { |
146 | register struct ndrv_cb *np = sotondrvcb(so); | |
1c79356b | 147 | register struct ifnet *ifp = np->nd_if; |
1c79356b | 148 | extern void kprintf(const char *, ...); |
7b1edb79 | 149 | int result = 0; |
1c79356b A |
150 | |
151 | #if NDRV_DEBUG | |
152 | kprintf("NDRV output: %x, %x, %x\n", m, so, np); | |
153 | #endif | |
154 | ||
155 | /* | |
156 | * No header is a format error | |
157 | */ | |
158 | if ((m->m_flags&M_PKTHDR) == 0) | |
159 | return(EINVAL); | |
160 | ||
161 | /* | |
7b1edb79 A |
162 | * Call DLIL if we can. DLIL is much safer than calling the |
163 | * ifp directly. | |
164 | */ | |
165 | if (np->nd_tag != 0) | |
166 | result = dlil_output(np->nd_tag, m, (caddr_t)NULL, | |
167 | (struct sockaddr*)NULL, 1); | |
168 | else if (np->nd_send_tag != 0) | |
169 | result = dlil_output(np->nd_send_tag, m, (caddr_t)NULL, | |
170 | (struct sockaddr*)NULL, 1); | |
171 | else | |
172 | result = ENXIO; | |
173 | return (result); | |
174 | } | |
175 | ||
176 | /* Our input routine called from DLIL */ | |
1c79356b A |
177 | int |
178 | ndrv_input(struct mbuf *m, | |
179 | char *frame_header, | |
180 | struct ifnet *ifp, | |
181 | u_long dl_tag, | |
182 | int sync_ok) | |
7b1edb79 | 183 | { |
1c79356b A |
184 | struct socket *so; |
185 | struct sockaddr_dl ndrvsrc = {sizeof (struct sockaddr_dl), AF_NDRV}; | |
186 | register struct ndrv_cb *np; | |
1c79356b A |
187 | |
188 | ||
7b1edb79 | 189 | /* move packet from if queue to socket */ |
1c79356b | 190 | /* Should be media-independent */ |
7b1edb79 A |
191 | ndrvsrc.sdl_type = IFT_ETHER; |
192 | ndrvsrc.sdl_nlen = 0; | |
193 | ndrvsrc.sdl_alen = 6; | |
194 | ndrvsrc.sdl_slen = 0; | |
195 | bcopy(frame_header, &ndrvsrc.sdl_data, 6); | |
1c79356b | 196 | |
1c79356b A |
197 | np = ndrv_find_tag(dl_tag); |
198 | if (np == NULL) | |
7b1edb79 | 199 | { |
1c79356b A |
200 | return(ENOENT); |
201 | } | |
202 | so = np->nd_socket; | |
7b1edb79 A |
203 | /* prepend the frame header */ |
204 | m = m_prepend(m, ifp->if_data.ifi_hdrlen, M_NOWAIT); | |
205 | if (m == NULL) | |
206 | return EJUSTRETURN; | |
207 | bcopy(frame_header, m->m_data, ifp->if_data.ifi_hdrlen); | |
1c79356b A |
208 | if (sbappendaddr(&(so->so_rcv), (struct sockaddr *)&ndrvsrc, |
209 | m, (struct mbuf *)0) == 0) | |
7b1edb79 A |
210 | { |
211 | /* yes, sbappendaddr returns zero if the sockbuff is full... */ | |
212 | /* caller will free m */ | |
1c79356b A |
213 | return(ENOMEM); |
214 | } else | |
215 | sorwakeup(so); | |
1c79356b A |
216 | return(0); |
217 | } | |
218 | ||
1c79356b A |
219 | int |
220 | ndrv_control(struct socket *so, u_long cmd, caddr_t data, | |
221 | struct ifnet *ifp, struct proc *p) | |
222 | { | |
223 | return (0); | |
224 | } | |
225 | ||
226 | /* | |
227 | * Allocate an ndrv control block and some buffer space for the socket | |
228 | */ | |
229 | int | |
230 | ndrv_attach(struct socket *so, int proto, struct proc *p) | |
7b1edb79 A |
231 | { |
232 | int error; | |
1c79356b A |
233 | register struct ndrv_cb *np = sotondrvcb(so); |
234 | ||
235 | if ((so->so_state & SS_PRIV) == 0) | |
236 | return(EPERM); | |
237 | ||
238 | #if NDRV_DEBUG | |
239 | kprintf("NDRV attach: %x, %x, %x\n", so, proto, np); | |
240 | #endif | |
241 | MALLOC(np, struct ndrv_cb *, sizeof(*np), M_PCB, M_WAITOK); | |
0b4e3aa0 A |
242 | if (np == NULL) |
243 | return (ENOMEM); | |
7b1edb79 A |
244 | so->so_pcb = (caddr_t)np; |
245 | bzero(np, sizeof(*np)); | |
1c79356b A |
246 | #if NDRV_DEBUG |
247 | kprintf("NDRV attach: %x, %x, %x\n", so, proto, np); | |
248 | #endif | |
1c79356b A |
249 | if ((error = soreserve(so, ndrv_sendspace, ndrv_recvspace))) |
250 | return(error); | |
251 | TAILQ_INIT(&np->nd_dlist); | |
252 | np->nd_signature = NDRV_SIGNATURE; | |
253 | np->nd_socket = so; | |
254 | np->nd_proto.sp_family = so->so_proto->pr_domain->dom_family; | |
255 | np->nd_proto.sp_protocol = proto; | |
7b1edb79 A |
256 | np->nd_if = NULL; |
257 | np->nd_tag = 0; | |
258 | np->nd_family = 0; | |
259 | np->nd_unit = 0; | |
1c79356b A |
260 | insque((queue_t)np, (queue_t)&ndrvl); |
261 | return(0); | |
262 | } | |
263 | ||
264 | /* | |
265 | * Destroy state just before socket deallocation. | |
266 | * Flush data or not depending on the options. | |
267 | */ | |
268 | ||
269 | int | |
270 | ndrv_detach(struct socket *so) | |
271 | { | |
272 | register struct ndrv_cb *np = sotondrvcb(so); | |
273 | ||
274 | if (np == 0) | |
275 | return EINVAL; | |
276 | return ndrv_do_detach(np); | |
277 | } | |
278 | ||
279 | ||
280 | /* | |
281 | * If a socket isn't bound to a single address, | |
282 | * the ndrv input routine will hand it anything | |
283 | * within that protocol family (assuming there's | |
284 | * nothing else around it should go to). | |
285 | * | |
286 | * Don't expect this to be used. | |
287 | */ | |
288 | ||
289 | int ndrv_connect(struct socket *so, struct sockaddr *nam, struct proc *p) | |
290 | { | |
291 | register struct ndrv_cb *np = sotondrvcb(so); | |
7b1edb79 | 292 | int result = 0; |
1c79356b A |
293 | |
294 | if (np == 0) | |
295 | return EINVAL; | |
296 | ||
297 | if (np->nd_faddr) | |
298 | return EISCONN; | |
7b1edb79 A |
299 | |
300 | /* Allocate memory to store the remote address */ | |
301 | MALLOC(np->nd_faddr, struct sockaddr_ndrv*, | |
302 | nam->sa_len, M_IFADDR, M_WAITOK); | |
303 | if (result != 0) | |
304 | return result; | |
305 | if (np->nd_faddr == NULL) | |
306 | return ENOMEM; | |
307 | ||
308 | bcopy((caddr_t) nam, (caddr_t) np->nd_faddr, nam->sa_len); | |
1c79356b A |
309 | soisconnected(so); |
310 | return 0; | |
311 | } | |
312 | ||
313 | /* | |
314 | * This is the "driver open" hook - we 'bind' to the | |
315 | * named driver. | |
7b1edb79 | 316 | * Here's where we latch onto the driver. |
1c79356b A |
317 | */ |
318 | int | |
319 | ndrv_bind(struct socket *so, struct sockaddr *nam, struct proc *p) | |
7b1edb79 A |
320 | { |
321 | register struct sockaddr_ndrv *sa = (struct sockaddr_ndrv *) nam; | |
1c79356b A |
322 | register char *dname; |
323 | register struct ndrv_cb *np; | |
324 | register struct ifnet *ifp; | |
325 | extern int name_cmp(struct ifnet *, char *); | |
7b1edb79 | 326 | int result; |
1c79356b A |
327 | |
328 | if TAILQ_EMPTY(&ifnet) | |
329 | return(EADDRNOTAVAIL); /* Quick sanity check */ | |
330 | np = sotondrvcb(so); | |
331 | if (np == 0) | |
332 | return EINVAL; | |
333 | ||
334 | if (np->nd_laddr) | |
335 | return EINVAL; /* XXX */ | |
336 | ||
337 | /* I think we just latch onto a copy here; the caller frees */ | |
338 | np->nd_laddr = _MALLOC(sizeof(struct sockaddr_ndrv), M_IFADDR, M_WAITOK); | |
339 | if (np->nd_laddr == NULL) | |
340 | return(ENOMEM); | |
341 | bcopy((caddr_t) sa, (caddr_t) np->nd_laddr, sizeof(struct sockaddr_ndrv)); | |
342 | dname = sa->snd_name; | |
343 | if (*dname == '\0') | |
344 | return(EINVAL); | |
345 | #if NDRV_DEBUG | |
346 | kprintf("NDRV bind: %x, %x, %s\n", so, np, dname); | |
347 | #endif | |
348 | /* Track down the driver and its ifnet structure. | |
349 | * There's no internal call for this so we have to dup the code | |
350 | * in if.c/ifconf() | |
351 | */ | |
352 | TAILQ_FOREACH(ifp, &ifnet, if_link) { | |
353 | if (name_cmp(ifp, dname) == 0) | |
354 | break; | |
355 | } | |
356 | ||
357 | if (ifp == NULL) | |
358 | return(EADDRNOTAVAIL); | |
7b1edb79 A |
359 | |
360 | /* | |
361 | * Loopback demuxing doesn't work with PF_NDRV. | |
362 | * The first 4 bytes of the packet must be the | |
363 | * protocol ptr. Can't get that from userland. | |
364 | */ | |
365 | if (ifp->if_family == APPLE_IF_FAM_LOOPBACK) | |
366 | return (ENOTSUP); | |
367 | ||
368 | if ((dlil_find_dltag(ifp->if_family, ifp->if_unit, | |
369 | PF_NDRV, &np->nd_send_tag) != 0) && | |
9bccf70c | 370 | (ifp->if_family != APPLE_IF_FAM_PPP)) { |
7b1edb79 A |
371 | /* NDRV isn't registered on this interface, lets change that */ |
372 | struct dlil_proto_reg_str ndrv_proto; | |
373 | int result = 0; | |
374 | bzero(&ndrv_proto, sizeof(ndrv_proto)); | |
375 | TAILQ_INIT(&ndrv_proto.demux_desc_head); | |
376 | ||
377 | ndrv_proto.interface_family = ifp->if_family; | |
378 | ndrv_proto.protocol_family = PF_NDRV; | |
379 | ndrv_proto.unit_number = ifp->if_unit; | |
380 | ||
381 | result = dlil_attach_protocol(&ndrv_proto, &np->nd_send_tag); | |
382 | ||
383 | /* | |
384 | * If the interface does not allow PF_NDRV to attach, we will | |
385 | * respect it's wishes. Sending will be disabled. No error is | |
386 | * returned because the client may later attach a real protocol | |
387 | * that the interface may accept. | |
388 | */ | |
389 | if (result != 0) | |
390 | np->nd_send_tag = 0; | |
391 | } | |
392 | ||
1c79356b | 393 | np->nd_if = ifp; |
7b1edb79 A |
394 | np->nd_family = ifp->if_family; |
395 | np->nd_unit = ifp->if_unit; | |
396 | ||
1c79356b A |
397 | return(0); |
398 | } | |
399 | ||
400 | int | |
401 | ndrv_disconnect(struct socket *so) | |
402 | { | |
403 | register struct ndrv_cb *np = sotondrvcb(so); | |
404 | ||
405 | if (np == 0) | |
406 | return EINVAL; | |
407 | ||
408 | if (np->nd_faddr == 0) | |
409 | return ENOTCONN; | |
410 | ||
411 | ndrv_do_disconnect(np); | |
412 | return 0; | |
413 | } | |
414 | ||
7b1edb79 A |
415 | /* |
416 | * Accessor function | |
417 | */ | |
418 | struct ifnet* | |
419 | ndrv_get_ifp(caddr_t ndrv_pcb) | |
420 | { | |
421 | struct ndrv_cb* np = (struct ndrv_cb*)ndrv_pcb; | |
422 | ||
423 | #if DEBUG | |
424 | { | |
425 | struct ndrv_cb* temp = ndrvl.nd_next; | |
426 | /* Verify existence of pcb */ | |
427 | for (temp = ndrvl.nd_next; temp != &ndrvl; temp = temp->nd_next) | |
428 | { | |
429 | if (temp == np) | |
430 | break; | |
431 | } | |
432 | ||
433 | if (temp != np) | |
434 | { | |
435 | log(LOG_WARNING, "PF_NDRV: ndrv_get_ifp called with invalid ndrv_cb!"); | |
436 | return NULL; | |
437 | } | |
438 | } | |
439 | #endif | |
440 | ||
441 | return np->nd_if; | |
442 | } | |
443 | ||
1c79356b A |
444 | /* |
445 | * Mark the connection as being incapable of further input. | |
446 | */ | |
447 | int | |
448 | ndrv_shutdown(struct socket *so) | |
449 | { | |
450 | socantsendmore(so); | |
451 | return 0; | |
452 | } | |
453 | ||
454 | /* | |
455 | * Ship a packet out. The ndrv output will pass it | |
456 | * to the appropriate driver. The really tricky part | |
457 | * is the destination address... | |
458 | */ | |
459 | int | |
460 | ndrv_send(struct socket *so, int flags, struct mbuf *m, | |
461 | struct sockaddr *addr, struct mbuf *control, | |
462 | struct proc *p) | |
463 | { | |
464 | int error; | |
465 | ||
466 | if (control) | |
467 | return EOPNOTSUPP; | |
468 | ||
469 | error = ndrv_output(m, so); | |
470 | m = NULL; | |
471 | return error; | |
472 | } | |
473 | ||
474 | ||
475 | int | |
476 | ndrv_abort(struct socket *so) | |
477 | { | |
478 | register struct ndrv_cb *np = sotondrvcb(so); | |
479 | ||
480 | if (np == 0) | |
481 | return EINVAL; | |
482 | ||
483 | ndrv_do_disconnect(np); | |
484 | return 0; | |
485 | } | |
486 | ||
487 | int | |
488 | ndrv_sense(struct socket *so, struct stat *sb) | |
489 | { | |
490 | /* | |
491 | * stat: don't bother with a blocksize. | |
492 | */ | |
493 | return (0); | |
494 | } | |
495 | ||
496 | int | |
497 | ndrv_sockaddr(struct socket *so, struct sockaddr **nam) | |
498 | { | |
499 | register struct ndrv_cb *np = sotondrvcb(so); | |
500 | int len; | |
501 | ||
502 | if (np == 0) | |
503 | return EINVAL; | |
504 | ||
505 | if (np->nd_laddr == 0) | |
506 | return EINVAL; | |
507 | ||
508 | len = np->nd_laddr->snd_len; | |
509 | bcopy((caddr_t)np->nd_laddr, *nam, | |
510 | (unsigned)len); | |
511 | return 0; | |
512 | } | |
513 | ||
514 | ||
515 | int | |
516 | ndrv_peeraddr(struct socket *so, struct sockaddr **nam) | |
517 | { | |
518 | register struct ndrv_cb *np = sotondrvcb(so); | |
519 | int len; | |
520 | ||
521 | if (np == 0) | |
522 | return EINVAL; | |
523 | ||
524 | if (np->nd_faddr == 0) | |
525 | return ENOTCONN; | |
526 | ||
527 | len = np->nd_faddr->snd_len; | |
528 | bcopy((caddr_t)np->nd_faddr, *nam, | |
529 | (unsigned)len); | |
530 | return 0; | |
531 | } | |
532 | ||
533 | ||
534 | /* Control input */ | |
535 | ||
536 | void | |
537 | ndrv_ctlinput(int dummy1, struct sockaddr *dummy2, void *dummy3) | |
538 | { | |
539 | } | |
540 | ||
541 | /* Control output */ | |
542 | ||
543 | int | |
544 | ndrv_ctloutput(struct socket *so, struct sockopt *sopt) | |
7b1edb79 A |
545 | { |
546 | register struct ndrv_cb *np = sotondrvcb(so); | |
547 | int error = 0; | |
548 | ||
549 | switch(sopt->sopt_name) | |
550 | { | |
551 | case NDRV_DELDMXSPEC: /* Delete current spec */ | |
552 | /* Verify no parameter was passed */ | |
553 | if (sopt->sopt_val != NULL || sopt->sopt_valsize != 0) { | |
554 | /* | |
555 | * We don't support deleting a specific demux, it's | |
556 | * all or nothing. | |
557 | */ | |
558 | return EINVAL; | |
559 | } | |
560 | error = ndrv_delspec(np); | |
561 | break; | |
562 | case NDRV_SETDMXSPEC: /* Set protocol spec */ | |
563 | error = ndrv_setspec(np, sopt); | |
564 | break; | |
9bccf70c A |
565 | case NDRV_ADDMULTICAST: |
566 | error = ndrv_do_add_multicast(np, sopt); | |
567 | break; | |
568 | case NDRV_DELMULTICAST: | |
569 | error = ndrv_do_remove_multicast(np, sopt); | |
570 | break; | |
7b1edb79 A |
571 | default: |
572 | error = ENOTSUP; | |
573 | } | |
1c79356b A |
574 | #ifdef NDRV_DEBUG |
575 | log(LOG_WARNING, "NDRV CTLOUT: %x returns %d\n", sopt->sopt_name, | |
576 | error); | |
577 | #endif | |
578 | return(error); | |
579 | } | |
580 | ||
581 | /* Drain the queues */ | |
582 | void | |
583 | ndrv_drain() | |
584 | { | |
585 | } | |
586 | ||
587 | /* Sysctl hook for NDRV */ | |
588 | int | |
589 | ndrv_sysctl() | |
590 | { | |
591 | return(0); | |
592 | } | |
593 | ||
594 | int | |
595 | ndrv_do_detach(register struct ndrv_cb *np) | |
7b1edb79 A |
596 | { |
597 | struct ndrv_cb* cur_np = NULL; | |
598 | struct socket *so = np->nd_socket; | |
599 | struct ndrv_multicast* next; | |
600 | int error; | |
1c79356b A |
601 | |
602 | #if NDRV_DEBUG | |
603 | kprintf("NDRV detach: %x, %x\n", so, np); | |
604 | #endif | |
9bccf70c A |
605 | ndrv_remove_all_multicast(np); |
606 | ||
7b1edb79 A |
607 | if (np->nd_tag != 0) |
608 | { | |
609 | error = dlil_detach_protocol(np->nd_tag); | |
610 | if (error) | |
611 | { | |
612 | log(LOG_WARNING, "NDRV ndrv_do_detach: error %d removing dl_tag %d", | |
613 | error, np->nd_tag); | |
614 | return error; | |
615 | } | |
616 | } | |
617 | ||
618 | /* Remove from the linked list of control blocks */ | |
1c79356b | 619 | remque((queue_t)np); |
7b1edb79 A |
620 | |
621 | if (np->nd_send_tag != 0) | |
622 | { | |
623 | /* Check if this is the last socket attached to this interface */ | |
624 | for (cur_np = ndrvl.nd_next; cur_np != &ndrvl; cur_np = cur_np->nd_next) | |
625 | { | |
626 | if (cur_np->nd_family == np->nd_family && | |
627 | cur_np->nd_unit == np->nd_unit) | |
628 | { | |
629 | break; | |
630 | } | |
631 | } | |
632 | ||
633 | /* If there are no other interfaces, detach PF_NDRV from the interface */ | |
634 | if (cur_np == &ndrvl) | |
635 | { | |
636 | dlil_detach_protocol(np->nd_send_tag); | |
637 | } | |
638 | } | |
639 | ||
1c79356b A |
640 | FREE((caddr_t)np, M_PCB); |
641 | so->so_pcb = 0; | |
642 | sofree(so); | |
7b1edb79 | 643 | return error; |
1c79356b A |
644 | } |
645 | ||
646 | int | |
647 | ndrv_do_disconnect(register struct ndrv_cb *np) | |
648 | { | |
649 | #if NDRV_DEBUG | |
650 | kprintf("NDRV disconnect: %x\n", np); | |
651 | #endif | |
652 | if (np->nd_faddr) | |
7b1edb79 A |
653 | { |
654 | FREE(np->nd_faddr, M_IFADDR); | |
1c79356b A |
655 | np->nd_faddr = 0; |
656 | } | |
657 | if (np->nd_socket->so_state & SS_NOFDREF) | |
658 | ndrv_do_detach(np); | |
659 | soisdisconnected(np->nd_socket); | |
660 | return(0); | |
661 | } | |
662 | ||
663 | /* | |
664 | * Try to compare a device name (q) with one of the funky ifnet | |
665 | * device names (ifp). | |
666 | */ | |
667 | int name_cmp(register struct ifnet *ifp, register char *q) | |
668 | { register char *r; | |
669 | register int len; | |
670 | char buf[IFNAMSIZ]; | |
671 | static char *sprint_d(); | |
672 | ||
673 | r = buf; | |
674 | len = strlen(ifp->if_name); | |
675 | strncpy(r, ifp->if_name, IFNAMSIZ); | |
676 | r += len; | |
677 | (void)sprint_d(ifp->if_unit, r, IFNAMSIZ-(r-buf)); | |
678 | #if NDRV_DEBUG | |
679 | kprintf("Comparing %s, %s\n", buf, q); | |
680 | #endif | |
681 | return(strncmp(buf, q, IFNAMSIZ)); | |
682 | } | |
683 | ||
684 | /* Hackery - return a string version of a decimal number */ | |
685 | static char * | |
686 | sprint_d(n, buf, buflen) | |
687 | u_int n; | |
688 | char *buf; | |
689 | int buflen; | |
690 | { char dbuf[IFNAMSIZ]; | |
691 | register char *cp = dbuf+IFNAMSIZ-1; | |
692 | ||
693 | *cp = 0; | |
694 | do { buflen--; | |
695 | cp--; | |
696 | *cp = "0123456789"[n % 10]; | |
697 | n /= 10; | |
698 | } while (n != 0 && buflen > 0); | |
699 | strncpy(buf, cp, IFNAMSIZ-buflen); | |
700 | return (cp); | |
701 | } | |
702 | ||
703 | /* | |
704 | * When closing, dump any enqueued mbufs. | |
705 | */ | |
706 | void | |
707 | ndrv_flushq(register struct ifqueue *q) | |
7b1edb79 A |
708 | { |
709 | register struct mbuf *m; | |
1c79356b | 710 | for (;;) |
7b1edb79 | 711 | { |
1c79356b A |
712 | IF_DEQUEUE(q, m); |
713 | if (m == NULL) | |
714 | break; | |
715 | IF_DROP(q); | |
1c79356b A |
716 | if (m) |
717 | m_freem(m); | |
718 | } | |
1c79356b A |
719 | } |
720 | ||
721 | int | |
7b1edb79 A |
722 | ndrv_setspec(struct ndrv_cb *np, struct sockopt *sopt) |
723 | { | |
724 | struct dlil_proto_reg_str dlilSpec; | |
725 | struct ndrv_protocol_desc ndrvSpec; | |
726 | struct dlil_demux_desc* dlilDemux = NULL; | |
727 | struct ndrv_demux_desc* ndrvDemux = NULL; | |
728 | int error = 0; | |
729 | ||
730 | /* Sanity checking */ | |
731 | if (np->nd_tag) | |
732 | return EBUSY; | |
733 | if (np->nd_if == NULL) | |
734 | return EINVAL; | |
735 | if (sopt->sopt_valsize != sizeof(struct ndrv_protocol_desc)) | |
736 | return EINVAL; | |
737 | ||
738 | /* Copy the ndrvSpec */ | |
739 | error = sooptcopyin(sopt, &ndrvSpec, sizeof(struct ndrv_protocol_desc), | |
740 | sizeof(struct ndrv_protocol_desc)); | |
741 | if (error != 0) | |
742 | return error; | |
743 | ||
744 | /* Verify the parameter */ | |
745 | if (ndrvSpec.version > NDRV_PROTOCOL_DESC_VERS) | |
746 | return ENOTSUP; // version is too new! | |
747 | else if (ndrvSpec.version < 1) | |
748 | return EINVAL; // version is not valid | |
749 | ||
750 | /* Allocate storage for demux array */ | |
751 | MALLOC(ndrvDemux, struct ndrv_demux_desc*, | |
752 | ndrvSpec.demux_count * sizeof(struct ndrv_demux_desc), M_TEMP, M_WAITOK); | |
753 | if (ndrvDemux == NULL) | |
754 | return ENOMEM; | |
755 | ||
756 | /* Allocate enough dlil_demux_descs */ | |
757 | MALLOC(dlilDemux, struct dlil_demux_desc*, | |
758 | sizeof(*dlilDemux) * ndrvSpec.demux_count, M_TEMP, M_WAITOK); | |
759 | if (dlilDemux == NULL) | |
760 | error = ENOMEM; | |
761 | ||
762 | if (error == 0) | |
763 | { | |
764 | /* Copy the ndrv demux array from userland */ | |
765 | error = copyin(ndrvSpec.demux_list, ndrvDemux, | |
766 | ndrvSpec.demux_count * sizeof(struct ndrv_demux_desc)); | |
767 | ndrvSpec.demux_list = ndrvDemux; | |
768 | } | |
769 | ||
770 | if (error == 0) | |
771 | { | |
772 | /* At this point, we've at least got enough bytes to start looking around */ | |
773 | u_long demuxOn = 0; | |
774 | ||
775 | bzero(&dlilSpec, sizeof(dlilSpec)); | |
776 | TAILQ_INIT(&dlilSpec.demux_desc_head); | |
777 | dlilSpec.interface_family = np->nd_family; | |
778 | dlilSpec.unit_number = np->nd_unit; | |
779 | dlilSpec.input = ndrv_input; | |
780 | dlilSpec.protocol_family = ndrvSpec.protocol_family; | |
781 | ||
782 | for (demuxOn = 0; demuxOn < ndrvSpec.demux_count; demuxOn++) | |
783 | { | |
784 | /* Convert an ndrv_demux_desc to a dlil_demux_desc */ | |
785 | error = ndrv_to_dlil_demux(&ndrvSpec.demux_list[demuxOn], &dlilDemux[demuxOn]); | |
786 | if (error) | |
787 | break; | |
788 | ||
789 | /* Add the dlil_demux_desc to the list */ | |
790 | TAILQ_INSERT_TAIL(&dlilSpec.demux_desc_head, &dlilDemux[demuxOn], next); | |
791 | } | |
792 | } | |
793 | ||
794 | if (error == 0) | |
795 | { | |
796 | /* We've got all our ducks lined up...lets attach! */ | |
797 | error = dlil_attach_protocol(&dlilSpec, &np->nd_tag); | |
798 | } | |
799 | ||
800 | /* Free any memory we've allocated */ | |
801 | if (dlilDemux) | |
802 | FREE(dlilDemux, M_TEMP); | |
803 | if (ndrvDemux) | |
804 | FREE(ndrvDemux, M_TEMP); | |
805 | ||
806 | return error; | |
1c79356b A |
807 | } |
808 | ||
1c79356b | 809 | |
7b1edb79 A |
810 | int |
811 | ndrv_to_dlil_demux(struct ndrv_demux_desc* ndrv, struct dlil_demux_desc* dlil) | |
812 | { | |
813 | bzero(dlil, sizeof(*dlil)); | |
814 | ||
815 | if (ndrv->type < DLIL_DESC_ETYPE2) | |
816 | { | |
817 | /* using old "type", not supported */ | |
818 | return ENOTSUP; | |
819 | } | |
820 | ||
821 | if (ndrv->length > 28) | |
822 | { | |
823 | return EINVAL; | |
824 | } | |
825 | ||
826 | dlil->type = ndrv->type; | |
827 | dlil->native_type = ndrv->data.other; | |
828 | dlil->variants.native_type_length = ndrv->length; | |
829 | ||
830 | return 0; | |
1c79356b A |
831 | } |
832 | ||
833 | int | |
7b1edb79 A |
834 | ndrv_delspec(struct ndrv_cb *np) |
835 | { | |
836 | int result = 0; | |
837 | ||
838 | if (np->nd_tag == 0) | |
839 | return EINVAL; | |
840 | ||
841 | /* Detach the protocol */ | |
842 | result = dlil_detach_protocol(np->nd_tag); | |
843 | if (result == 0) | |
844 | { | |
845 | np->nd_tag = 0; | |
846 | } | |
847 | ||
848 | return result; | |
1c79356b A |
849 | } |
850 | ||
851 | struct ndrv_cb * | |
852 | ndrv_find_tag(unsigned int tag) | |
7b1edb79 A |
853 | { |
854 | struct ndrv_cb* np; | |
1c79356b | 855 | int i; |
7b1edb79 A |
856 | |
857 | if (tag == 0) | |
858 | return NULL; | |
859 | ||
860 | for (np = ndrvl.nd_next; np != NULL; np = np->nd_next) | |
861 | { | |
862 | if (np->nd_tag == tag) | |
863 | { | |
864 | return np; | |
865 | } | |
866 | } | |
867 | ||
868 | return NULL; | |
1c79356b A |
869 | } |
870 | ||
7b1edb79 A |
871 | void ndrv_dominit() |
872 | { | |
873 | static int ndrv_dominited = 0; | |
1c79356b | 874 | |
7b1edb79 A |
875 | if (ndrv_dominited == 0 && |
876 | net_add_proto(&ndrvsw, &ndrvdomain) == 0) | |
877 | ndrv_dominited = 1; | |
1c79356b A |
878 | } |
879 | ||
7b1edb79 A |
880 | void |
881 | ndrv_read_event(struct socket* so, caddr_t ref, int waitf) | |
882 | { | |
883 | // Read an event | |
884 | struct mbuf *m = NULL; | |
885 | struct kern_event_msg *msg; | |
886 | struct uio auio = {0}; | |
887 | int result = 0; | |
888 | int flags = 0; | |
889 | ||
890 | // Get the data | |
891 | auio.uio_resid = 1000000; // large number to get all of the data | |
892 | flags = MSG_DONTWAIT; | |
893 | result = soreceive(so, (struct sockaddr**)NULL, &auio, &m, | |
894 | (struct mbuf**)NULL, &flags); | |
895 | if (result != 0 || m == NULL) | |
896 | return; | |
897 | ||
898 | // cast the mbuf to a kern_event_msg | |
899 | // this is dangerous, doesn't handle linked mbufs | |
900 | msg = mtod(m, struct kern_event_msg*); | |
901 | ||
902 | // check for detaches, assume even filtering is working | |
903 | if (msg->event_code == KEV_DL_IF_DETACHING || | |
904 | msg->event_code == KEV_DL_IF_DETACHED) | |
905 | { | |
906 | struct net_event_data *ev_data; | |
907 | ev_data = (struct net_event_data*)msg->event_data; | |
908 | ndrv_handle_ifp_detach(ev_data->if_family, ev_data->if_unit); | |
909 | } | |
910 | ||
911 | m_free(m); | |
1c79356b A |
912 | } |
913 | ||
7b1edb79 A |
914 | void |
915 | ndrv_handle_ifp_detach(u_long family, short unit) | |
1c79356b | 916 | { |
7b1edb79 A |
917 | struct ndrv_cb* np; |
918 | u_long dl_tag; | |
919 | ||
920 | /* Find all sockets using this interface. */ | |
921 | for (np = ndrvl.nd_next; np != &ndrvl; np = np->nd_next) | |
922 | { | |
923 | if (np->nd_family == family && | |
924 | np->nd_unit == unit) | |
925 | { | |
926 | /* This cb is using the detaching interface, but not for long. */ | |
927 | /* Let the protocol go */ | |
928 | if (np->nd_tag != 0) | |
929 | ndrv_delspec(np); | |
930 | ||
9bccf70c A |
931 | /* Delete the multicasts first */ |
932 | ndrv_remove_all_multicast(np); | |
933 | ||
7b1edb79 A |
934 | /* Disavow all knowledge of the ifp */ |
935 | np->nd_if = NULL; | |
936 | np->nd_unit = 0; | |
937 | np->nd_family = 0; | |
938 | np->nd_send_tag = 0; | |
939 | ||
940 | /* Make sure sending returns an error */ | |
941 | /* Is this safe? Will we drop the funnel? */ | |
942 | socantsendmore(np->nd_socket); | |
943 | socantrcvmore(np->nd_socket); | |
1c79356b | 944 | } |
7b1edb79 A |
945 | } |
946 | ||
947 | /* Unregister our protocol */ | |
948 | if (dlil_find_dltag(family, unit, PF_NDRV, &dl_tag) == 0) { | |
949 | dlil_detach_protocol(dl_tag); | |
950 | } | |
1c79356b A |
951 | } |
952 | ||
9bccf70c A |
953 | static int |
954 | ndrv_do_add_multicast(struct ndrv_cb *np, struct sockopt *sopt) | |
955 | { | |
956 | struct ndrv_multiaddr* ndrv_multi; | |
957 | int result; | |
958 | ||
959 | if (sopt->sopt_val == NULL || sopt->sopt_valsize < 2 || | |
960 | sopt->sopt_level != SOL_NDRVPROTO) | |
961 | return EINVAL; | |
962 | if (np->nd_if == NULL) | |
963 | return ENXIO; | |
964 | ||
965 | // Allocate storage | |
966 | MALLOC(ndrv_multi, struct ndrv_multiaddr*, sizeof(struct ndrv_multiaddr) - | |
967 | sizeof(struct sockaddr) + sopt->sopt_valsize, M_IFADDR, M_WAITOK); | |
968 | if (ndrv_multi == NULL) | |
969 | return ENOMEM; | |
970 | ||
971 | // Copy in the address | |
972 | result = copyin(sopt->sopt_val, &ndrv_multi->addr, sopt->sopt_valsize); | |
973 | ||
974 | // Validate the sockaddr | |
975 | if (result == 0 && sopt->sopt_valsize != ndrv_multi->addr.sa_len) | |
976 | result = EINVAL; | |
977 | ||
978 | if (result == 0 && ndrv_have_multicast(np, &ndrv_multi->addr)) | |
979 | result = EEXIST; | |
980 | ||
981 | if (result == 0) | |
982 | { | |
983 | // Try adding the multicast | |
984 | result = if_addmulti(np->nd_if, &ndrv_multi->addr, NULL); | |
985 | } | |
986 | ||
987 | if (result == 0) | |
988 | { | |
989 | // Add to our linked list | |
990 | ndrv_multi->next = np->nd_multiaddrs; | |
991 | np->nd_multiaddrs = ndrv_multi; | |
992 | } | |
993 | else | |
994 | { | |
995 | // Free up the memory, something went wrong | |
996 | FREE(ndrv_multi, M_IFADDR); | |
997 | } | |
998 | ||
999 | return result; | |
1000 | } | |
1001 | ||
1002 | static int | |
1003 | ndrv_do_remove_multicast(struct ndrv_cb *np, struct sockopt *sopt) | |
1004 | { | |
1005 | struct sockaddr* multi_addr; | |
1006 | struct ndrv_multiaddr* ndrv_entry = NULL; | |
1007 | int result; | |
1008 | ||
1009 | if (sopt->sopt_val == NULL || sopt->sopt_valsize < 2 || | |
1010 | sopt->sopt_level != SOL_NDRVPROTO) | |
1011 | return EINVAL; | |
1012 | if (np->nd_if == NULL) | |
1013 | return ENXIO; | |
1014 | ||
1015 | // Allocate storage | |
1016 | MALLOC(multi_addr, struct sockaddr*, sopt->sopt_valsize, | |
1017 | M_TEMP, M_WAITOK); | |
1018 | if (multi_addr == NULL) | |
1019 | return ENOMEM; | |
1020 | ||
1021 | // Copy in the address | |
1022 | result = copyin(sopt->sopt_val, multi_addr, sopt->sopt_valsize); | |
1023 | ||
1024 | // Validate the sockaddr | |
1025 | if (result == 0 && sopt->sopt_valsize != multi_addr->sa_len) | |
1026 | result = EINVAL; | |
1027 | ||
1028 | if (result == 0) | |
1029 | { | |
1030 | /* Find the old entry */ | |
1031 | ndrv_entry = ndrv_have_multicast(np, multi_addr); | |
1032 | ||
1033 | if (ndrv_entry == NULL) | |
1034 | result = ENOENT; | |
1035 | } | |
1036 | ||
1037 | if (result == 0) | |
1038 | { | |
1039 | // Try deleting the multicast | |
1040 | result = if_delmulti(np->nd_if, &ndrv_entry->addr); | |
1041 | } | |
1042 | ||
1043 | if (result == 0) | |
1044 | { | |
1045 | // Remove from our linked list | |
1046 | struct ndrv_multiaddr* cur = np->nd_multiaddrs; | |
1047 | ||
1048 | if (cur == ndrv_entry) | |
1049 | { | |
1050 | np->nd_multiaddrs = cur->next; | |
1051 | } | |
1052 | else | |
1053 | { | |
1054 | for (cur = cur->next; cur != NULL; cur = cur->next) | |
1055 | { | |
1056 | if (cur->next == ndrv_entry) | |
1057 | { | |
1058 | cur->next = cur->next->next; | |
1059 | break; | |
1060 | } | |
1061 | } | |
1062 | } | |
1063 | ||
1064 | // Free the memory | |
1065 | FREE(ndrv_entry, M_IFADDR); | |
1066 | } | |
1067 | FREE(multi_addr, M_TEMP); | |
1068 | ||
1069 | return result; | |
1070 | } | |
1071 | ||
1072 | static struct ndrv_multiaddr* | |
1073 | ndrv_have_multicast(struct ndrv_cb *np, struct sockaddr* inAddr) | |
1074 | { | |
1075 | struct ndrv_multiaddr* cur; | |
1076 | for (cur = np->nd_multiaddrs; cur != NULL; cur = cur->next) | |
1077 | { | |
1078 | ||
1079 | if ((inAddr->sa_len == cur->addr.sa_len) && | |
1080 | (bcmp(&cur->addr, inAddr, inAddr->sa_len) == 0)) | |
1081 | { | |
1082 | // Found a match | |
1083 | return cur; | |
1084 | } | |
1085 | } | |
1086 | ||
1087 | return NULL; | |
1088 | } | |
1089 | ||
1090 | static void | |
1091 | ndrv_remove_all_multicast(struct ndrv_cb* np) | |
1092 | { | |
1093 | struct ndrv_multiaddr* cur; | |
1094 | ||
1095 | if (np->nd_if != NULL) | |
1096 | { | |
1097 | while (np->nd_multiaddrs != NULL) | |
1098 | { | |
1099 | cur = np->nd_multiaddrs; | |
1100 | np->nd_multiaddrs = cur->next; | |
1101 | ||
1102 | if_delmulti(np->nd_if, &cur->addr); | |
1103 | FREE(cur, M_IFADDR); | |
1104 | } | |
1105 | } | |
1106 | } | |
1107 | ||
1c79356b A |
1108 | struct pr_usrreqs ndrv_usrreqs = { |
1109 | ndrv_abort, pru_accept_notsupp, ndrv_attach, ndrv_bind, | |
1110 | ndrv_connect, pru_connect2_notsupp, ndrv_control, ndrv_detach, | |
1111 | ndrv_disconnect, pru_listen_notsupp, ndrv_peeraddr, pru_rcvd_notsupp, | |
1112 | pru_rcvoob_notsupp, ndrv_send, ndrv_sense, ndrv_shutdown, | |
1113 | ndrv_sockaddr, sosend, soreceive, sopoll | |
1114 | }; | |
1115 | ||
7b1edb79 A |
1116 | struct protosw ndrvsw = |
1117 | { SOCK_RAW, &ndrvdomain, NDRVPROTO_NDRV, PR_ATOMIC|PR_ADDR, | |
1118 | 0, ndrv_output, ndrv_ctlinput, ndrv_ctloutput, | |
1119 | 0, ndrv_init, 0, 0, | |
1120 | ndrv_drain, ndrv_sysctl, &ndrv_usrreqs | |
1c79356b A |
1121 | }; |
1122 | ||
1123 | struct domain ndrvdomain = | |
1124 | { AF_NDRV, "NetDriver", ndrv_dominit, NULL, NULL, | |
1125 | NULL, | |
1126 | NULL, NULL, 0, 0, 0, 0 | |
1127 | }; |