1
2 /*
3 * ng_ether.c
4 */
5
6 /*-
7 * Copyright (c) 1996-2000 Whistle Communications, Inc.
8 * All rights reserved.
9 *
10 * Subject to the following obligations and disclaimer of warranty, use and
11 * redistribution of this software, in source or object code forms, with or
12 * without modifications are expressly permitted by Whistle Communications;
13 * provided, however, that:
14 * 1. Any and all reproductions of the source or object code must include the
15 * copyright notice above and the following disclaimer of warranties; and
16 * 2. No rights are granted, in any manner or form, to use Whistle
17 * Communications, Inc. trademarks, including the mark "WHISTLE
18 * COMMUNICATIONS" on advertising, endorsements, or otherwise except as
19 * such appears in the above copyright notice or in the software.
20 *
21 * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
22 * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
23 * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
24 * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
26 * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
27 * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
28 * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
29 * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
30 * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
31 * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
32 * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
33 * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
34 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
36 * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
37 * OF SUCH DAMAGE.
38 *
39 * Authors: Archie Cobbs <archie@freebsd.org>
40 * Julian Elischer <julian@freebsd.org>
41 *
42 * $FreeBSD: releng/6.0/sys/netgraph/ng_ether.c 151571 2005-10-23 02:37:28Z thompsa $
43 */
44
45 /*
46 * ng_ether(4) netgraph node type
47 */
48
49 #include <sys/param.h>
50 #include <sys/systm.h>
51 #include <sys/kernel.h>
52 #include <sys/malloc.h>
53 #include <sys/mbuf.h>
54 #include <sys/errno.h>
55 #include <sys/syslog.h>
56 #include <sys/socket.h>
57
58 #include <net/bridge.h>
59 #include <net/if.h>
60 #include <net/if_dl.h>
61 #include <net/if_types.h>
62 #include <net/if_arp.h>
63 #include <net/if_var.h>
64 #include <net/ethernet.h>
65 #include <net/if_bridgevar.h>
66
67 #include <netgraph/ng_message.h>
68 #include <netgraph/netgraph.h>
69 #include <netgraph/ng_parse.h>
70 #include <netgraph/ng_ether.h>
71
72 #define IFP2NG(ifp) ((struct ng_node *)IFP2AC((ifp))->ac_netgraph)
73 #define IFP2NG_SET(ifp, val) (IFP2AC((ifp))->ac_netgraph = (val))
74
75 /* Per-node private data */
76 struct private {
77 struct ifnet *ifp; /* associated interface */
78 hook_p upper; /* upper hook connection */
79 hook_p lower; /* lower hook connection */
80 hook_p orphan; /* orphan hook connection */
81 u_char autoSrcAddr; /* always overwrite source address */
82 u_char promisc; /* promiscuous mode enabled */
83 u_long hwassist; /* hardware checksum capabilities */
84 u_int flags; /* flags e.g. really die */
85 };
86 typedef struct private *priv_p;
87
88 /* Hook pointers used by if_ethersubr.c to callback to netgraph */
89 extern void (*ng_ether_input_p)(struct ifnet *ifp, struct mbuf **mp);
90 extern void (*ng_ether_input_orphan_p)(struct ifnet *ifp, struct mbuf *m);
91 extern int (*ng_ether_output_p)(struct ifnet *ifp, struct mbuf **mp);
92 extern void (*ng_ether_attach_p)(struct ifnet *ifp);
93 extern void (*ng_ether_detach_p)(struct ifnet *ifp);
94 extern void (*ng_ether_link_state_p)(struct ifnet *ifp, int state);
95
96 /* Functional hooks called from if_ethersubr.c */
97 static void ng_ether_input(struct ifnet *ifp, struct mbuf **mp);
98 static void ng_ether_input_orphan(struct ifnet *ifp, struct mbuf *m);
99 static int ng_ether_output(struct ifnet *ifp, struct mbuf **mp);
100 static void ng_ether_attach(struct ifnet *ifp);
101 static void ng_ether_detach(struct ifnet *ifp);
102 static void ng_ether_link_state(struct ifnet *ifp, int state);
103
104 /* Other functions */
105 static int ng_ether_rcv_lower(node_p node, struct mbuf *m);
106 static int ng_ether_rcv_upper(node_p node, struct mbuf *m);
107
108 /* Netgraph node methods */
109 static ng_constructor_t ng_ether_constructor;
110 static ng_rcvmsg_t ng_ether_rcvmsg;
111 static ng_shutdown_t ng_ether_shutdown;
112 static ng_newhook_t ng_ether_newhook;
113 static ng_connect_t ng_ether_connect;
114 static ng_rcvdata_t ng_ether_rcvdata;
115 static ng_disconnect_t ng_ether_disconnect;
116 static int ng_ether_mod_event(module_t mod, int event, void *data);
117
118 /* List of commands and how to convert arguments to/from ASCII */
119 static const struct ng_cmdlist ng_ether_cmdlist[] = {
120 {
121 NGM_ETHER_COOKIE,
122 NGM_ETHER_GET_IFNAME,
123 "getifname",
124 NULL,
125 &ng_parse_string_type
126 },
127 {
128 NGM_ETHER_COOKIE,
129 NGM_ETHER_GET_IFINDEX,
130 "getifindex",
131 NULL,
132 &ng_parse_int32_type
133 },
134 {
135 NGM_ETHER_COOKIE,
136 NGM_ETHER_GET_ENADDR,
137 "getenaddr",
138 NULL,
139 &ng_parse_enaddr_type
140 },
141 {
142 NGM_ETHER_COOKIE,
143 NGM_ETHER_SET_ENADDR,
144 "setenaddr",
145 &ng_parse_enaddr_type,
146 NULL
147 },
148 {
149 NGM_ETHER_COOKIE,
150 NGM_ETHER_GET_PROMISC,
151 "getpromisc",
152 NULL,
153 &ng_parse_int32_type
154 },
155 {
156 NGM_ETHER_COOKIE,
157 NGM_ETHER_SET_PROMISC,
158 "setpromisc",
159 &ng_parse_int32_type,
160 NULL
161 },
162 {
163 NGM_ETHER_COOKIE,
164 NGM_ETHER_GET_AUTOSRC,
165 "getautosrc",
166 NULL,
167 &ng_parse_int32_type
168 },
169 {
170 NGM_ETHER_COOKIE,
171 NGM_ETHER_SET_AUTOSRC,
172 "setautosrc",
173 &ng_parse_int32_type,
174 NULL
175 },
176 {
177 NGM_ETHER_COOKIE,
178 NGM_ETHER_ADD_MULTI,
179 "addmulti",
180 &ng_parse_enaddr_type,
181 NULL
182 },
183 {
184 NGM_ETHER_COOKIE,
185 NGM_ETHER_DEL_MULTI,
186 "delmulti",
187 &ng_parse_enaddr_type,
188 NULL
189 },
190 {
191 NGM_ETHER_COOKIE,
192 NGM_ETHER_DETACH,
193 "detach",
194 NULL,
195 NULL
196 },
197 { 0 }
198 };
199
200 static struct ng_type ng_ether_typestruct = {
201 .version = NG_ABI_VERSION,
202 .name = NG_ETHER_NODE_TYPE,
203 .mod_event = ng_ether_mod_event,
204 .constructor = ng_ether_constructor,
205 .rcvmsg = ng_ether_rcvmsg,
206 .shutdown = ng_ether_shutdown,
207 .newhook = ng_ether_newhook,
208 .connect = ng_ether_connect,
209 .rcvdata = ng_ether_rcvdata,
210 .disconnect = ng_ether_disconnect,
211 .cmdlist = ng_ether_cmdlist,
212 };
213 NETGRAPH_INIT(ether, &ng_ether_typestruct);
214
215 /******************************************************************
216 ETHERNET FUNCTION HOOKS
217 ******************************************************************/
218
219 /*
220 * Handle a packet that has come in on an interface. We get to
221 * look at it here before any upper layer protocols do.
222 *
223 * NOTE: this function will get called at splimp()
224 */
225 static void
226 ng_ether_input(struct ifnet *ifp, struct mbuf **mp)
227 {
228 const node_p node = IFP2NG(ifp);
229 const priv_p priv = NG_NODE_PRIVATE(node);
230 int error;
231
232 /* If "lower" hook not connected, let packet continue */
233 if (priv->lower == NULL)
234 return;
235 NG_SEND_DATA_ONLY(error, priv->lower, *mp); /* sets *mp = NULL */
236 }
237
238 /*
239 * Handle a packet that has come in on an interface, and which
240 * does not match any of our known protocols (an ``orphan'').
241 *
242 * NOTE: this function will get called at splimp()
243 */
244 static void
245 ng_ether_input_orphan(struct ifnet *ifp, struct mbuf *m)
246 {
247 const node_p node = IFP2NG(ifp);
248 const priv_p priv = NG_NODE_PRIVATE(node);
249 int error;
250
251 /* If "orphan" hook not connected, discard packet */
252 if (priv->orphan == NULL) {
253 m_freem(m);
254 return;
255 }
256 NG_SEND_DATA_ONLY(error, priv->orphan, m);
257 }
258
259 /*
260 * Handle a packet that is going out on an interface.
261 * The Ethernet header is already attached to the mbuf.
262 */
263 static int
264 ng_ether_output(struct ifnet *ifp, struct mbuf **mp)
265 {
266 const node_p node = IFP2NG(ifp);
267 const priv_p priv = NG_NODE_PRIVATE(node);
268 int error = 0;
269
270 /* If "upper" hook not connected, let packet continue */
271 if (priv->upper == NULL)
272 return (0);
273
274 /* Send it out "upper" hook */
275 NG_SEND_DATA_ONLY(error, priv->upper, *mp);
276 return (error);
277 }
278
279 /*
280 * A new Ethernet interface has been attached.
281 * Create a new node for it, etc.
282 */
283 static void
284 ng_ether_attach(struct ifnet *ifp)
285 {
286 priv_p priv;
287 node_p node;
288
289 /* Create node */
290 KASSERT(!IFP2NG(ifp), ("%s: node already exists?", __func__));
291 if (ng_make_node_common(&ng_ether_typestruct, &node) != 0) {
292 log(LOG_ERR, "%s: can't %s for %s\n",
293 __func__, "create node", ifp->if_xname);
294 return;
295 }
296
297 /* Allocate private data */
298 MALLOC(priv, priv_p, sizeof(*priv), M_NETGRAPH, M_NOWAIT | M_ZERO);
299 if (priv == NULL) {
300 log(LOG_ERR, "%s: can't %s for %s\n",
301 __func__, "allocate memory", ifp->if_xname);
302 NG_NODE_UNREF(node);
303 return;
304 }
305 NG_NODE_SET_PRIVATE(node, priv);
306 priv->ifp = ifp;
307 IFP2NG_SET(ifp, node);
308 priv->autoSrcAddr = 1;
309 priv->hwassist = ifp->if_hwassist;
310
311 /* Try to give the node the same name as the interface */
312 if (ng_name_node(node, ifp->if_xname) != 0) {
313 log(LOG_WARNING, "%s: can't name node %s\n",
314 __func__, ifp->if_xname);
315 }
316 }
317
318 /*
319 * An Ethernet interface is being detached.
320 * REALLY Destroy its node.
321 */
322 static void
323 ng_ether_detach(struct ifnet *ifp)
324 {
325 const node_p node = IFP2NG(ifp);
326 const priv_p priv = NG_NODE_PRIVATE(node);
327
328 NG_NODE_REALLY_DIE(node); /* Force real removal of node */
329 /*
330 * We can't assume the ifnet is still around when we run shutdown
331 * So zap it now. XXX We HOPE that anything running at this time
332 * handles it (as it should in the non netgraph case).
333 */
334 IFP2NG_SET(ifp, NULL);
335 priv->ifp = NULL; /* XXX race if interrupted an output packet */
336 ng_rmnode_self(node); /* remove all netgraph parts */
337 }
338
339 /*
340 * Notify graph about link event.
341 * if_link_state_change() has already checked that the state has changed.
342 */
343 static void
344 ng_ether_link_state(struct ifnet *ifp, int state)
345 {
346 const node_p node = IFP2NG(ifp);
347 const priv_p priv = NG_NODE_PRIVATE(node);
348 struct ng_mesg *msg;
349 int cmd, dummy_error = 0;
350
351 if (priv->lower == NULL)
352 return;
353
354 if (state == LINK_STATE_UP)
355 cmd = NGM_LINK_IS_UP;
356 else if (state == LINK_STATE_DOWN)
357 cmd = NGM_LINK_IS_DOWN;
358 else
359 return;
360
361 NG_MKMESSAGE(msg, NGM_FLOW_COOKIE, cmd, 0, M_NOWAIT);
362 if (msg != NULL)
363 NG_SEND_MSG_HOOK(dummy_error, node, msg, priv->lower, 0);
364 }
365
366 /******************************************************************
367 NETGRAPH NODE METHODS
368 ******************************************************************/
369
370 /*
371 * It is not possible or allowable to create a node of this type.
372 * Nodes get created when the interface is attached (or, when
373 * this node type's KLD is loaded).
374 */
375 static int
376 ng_ether_constructor(node_p node)
377 {
378 return (EINVAL);
379 }
380
381 /*
382 * Check for attaching a new hook.
383 */
384 static int
385 ng_ether_newhook(node_p node, hook_p hook, const char *name)
386 {
387 const priv_p priv = NG_NODE_PRIVATE(node);
388 hook_p *hookptr;
389
390 /* Divert hook is an alias for lower */
391 if (strcmp(name, NG_ETHER_HOOK_DIVERT) == 0)
392 name = NG_ETHER_HOOK_LOWER;
393
394 /* Which hook? */
395 if (strcmp(name, NG_ETHER_HOOK_UPPER) == 0)
396 hookptr = &priv->upper;
397 else if (strcmp(name, NG_ETHER_HOOK_LOWER) == 0)
398 hookptr = &priv->lower;
399 else if (strcmp(name, NG_ETHER_HOOK_ORPHAN) == 0)
400 hookptr = &priv->orphan;
401 else
402 return (EINVAL);
403
404 /* Check if already connected (shouldn't be, but doesn't hurt) */
405 if (*hookptr != NULL)
406 return (EISCONN);
407
408 /* Disable hardware checksums while 'upper' hook is connected */
409 if (hookptr == &priv->upper)
410 priv->ifp->if_hwassist = 0;
411
412 /* OK */
413 *hookptr = hook;
414 return (0);
415 }
416
417 /*
418 * Hooks are attached, adjust to force queueing.
419 * We don't really care which hook it is.
420 * they should all be queuing for outgoing data.
421 */
422 static int
423 ng_ether_connect(hook_p hook)
424 {
425 NG_HOOK_FORCE_QUEUE(NG_HOOK_PEER(hook));
426 return (0);
427 }
428
429 /*
430 * Receive an incoming control message.
431 */
432 static int
433 ng_ether_rcvmsg(node_p node, item_p item, hook_p lasthook)
434 {
435 const priv_p priv = NG_NODE_PRIVATE(node);
436 struct ng_mesg *resp = NULL;
437 int error = 0;
438 struct ng_mesg *msg;
439
440 NGI_GET_MSG(item, msg);
441 switch (msg->header.typecookie) {
442 case NGM_ETHER_COOKIE:
443 switch (msg->header.cmd) {
444 case NGM_ETHER_GET_IFNAME:
445 NG_MKRESPONSE(resp, msg, IFNAMSIZ, M_NOWAIT);
446 if (resp == NULL) {
447 error = ENOMEM;
448 break;
449 }
450 strlcpy(resp->data, priv->ifp->if_xname, IFNAMSIZ);
451 break;
452 case NGM_ETHER_GET_IFINDEX:
453 NG_MKRESPONSE(resp, msg, sizeof(u_int32_t), M_NOWAIT);
454 if (resp == NULL) {
455 error = ENOMEM;
456 break;
457 }
458 *((u_int32_t *)resp->data) = priv->ifp->if_index;
459 break;
460 case NGM_ETHER_GET_ENADDR:
461 NG_MKRESPONSE(resp, msg, ETHER_ADDR_LEN, M_NOWAIT);
462 if (resp == NULL) {
463 error = ENOMEM;
464 break;
465 }
466 bcopy(IFP2ENADDR(priv->ifp),
467 resp->data, ETHER_ADDR_LEN);
468 break;
469 case NGM_ETHER_SET_ENADDR:
470 {
471 if (msg->header.arglen != ETHER_ADDR_LEN) {
472 error = EINVAL;
473 break;
474 }
475 error = if_setlladdr(priv->ifp,
476 (u_char *)msg->data, ETHER_ADDR_LEN);
477 break;
478 }
479 case NGM_ETHER_GET_PROMISC:
480 NG_MKRESPONSE(resp, msg, sizeof(u_int32_t), M_NOWAIT);
481 if (resp == NULL) {
482 error = ENOMEM;
483 break;
484 }
485 *((u_int32_t *)resp->data) = priv->promisc;
486 break;
487 case NGM_ETHER_SET_PROMISC:
488 {
489 u_char want;
490
491 if (msg->header.arglen != sizeof(u_int32_t)) {
492 error = EINVAL;
493 break;
494 }
495 want = !!*((u_int32_t *)msg->data);
496 if (want ^ priv->promisc) {
497 if ((error = ifpromisc(priv->ifp, want)) != 0)
498 break;
499 priv->promisc = want;
500 }
501 break;
502 }
503 case NGM_ETHER_GET_AUTOSRC:
504 NG_MKRESPONSE(resp, msg, sizeof(u_int32_t), M_NOWAIT);
505 if (resp == NULL) {
506 error = ENOMEM;
507 break;
508 }
509 *((u_int32_t *)resp->data) = priv->autoSrcAddr;
510 break;
511 case NGM_ETHER_SET_AUTOSRC:
512 if (msg->header.arglen != sizeof(u_int32_t)) {
513 error = EINVAL;
514 break;
515 }
516 priv->autoSrcAddr = !!*((u_int32_t *)msg->data);
517 break;
518 case NGM_ETHER_ADD_MULTI:
519 {
520 struct sockaddr_dl sa_dl;
521 struct ifmultiaddr *ifm;
522
523 if (msg->header.arglen != ETHER_ADDR_LEN) {
524 error = EINVAL;
525 break;
526 }
527 bzero(&sa_dl, sizeof(struct sockaddr_dl));
528 sa_dl.sdl_len = sizeof(struct sockaddr_dl);
529 sa_dl.sdl_family = AF_LINK;
530 sa_dl.sdl_alen = ETHER_ADDR_LEN;
531 bcopy((void *)msg->data, LLADDR(&sa_dl),
532 ETHER_ADDR_LEN);
533 error = if_addmulti(priv->ifp,
534 (struct sockaddr *)&sa_dl, &ifm);
535 break;
536 }
537 case NGM_ETHER_DEL_MULTI:
538 {
539 struct sockaddr_dl sa_dl;
540
541 if (msg->header.arglen != ETHER_ADDR_LEN) {
542 error = EINVAL;
543 break;
544 }
545 bzero(&sa_dl, sizeof(struct sockaddr_dl));
546 sa_dl.sdl_len = sizeof(struct sockaddr_dl);
547 sa_dl.sdl_family = AF_LINK;
548 sa_dl.sdl_alen = ETHER_ADDR_LEN;
549 bcopy((void *)msg->data, LLADDR(&sa_dl),
550 ETHER_ADDR_LEN);
551 error = if_delmulti(priv->ifp,
552 (struct sockaddr *)&sa_dl);
553 break;
554 }
555 case NGM_ETHER_DETACH:
556 ng_ether_detach(priv->ifp);
557 break;
558 default:
559 error = EINVAL;
560 break;
561 }
562 break;
563 default:
564 error = EINVAL;
565 break;
566 }
567 NG_RESPOND_MSG(error, node, item, resp);
568 NG_FREE_MSG(msg);
569 return (error);
570 }
571
572 /*
573 * Receive data on a hook.
574 */
575 static int
576 ng_ether_rcvdata(hook_p hook, item_p item)
577 {
578 const node_p node = NG_HOOK_NODE(hook);
579 const priv_p priv = NG_NODE_PRIVATE(node);
580 struct mbuf *m;
581
582 NGI_GET_M(item, m);
583 NG_FREE_ITEM(item);
584
585 if (hook == priv->lower || hook == priv->orphan)
586 return ng_ether_rcv_lower(node, m);
587 if (hook == priv->upper)
588 return ng_ether_rcv_upper(node, m);
589 panic("%s: weird hook", __func__);
590 #ifdef RESTARTABLE_PANICS /* so we don't get an error msg in LINT */
591 return (0);
592 #endif
593 }
594
595 /*
596 * Handle an mbuf received on the "lower" or "orphan" hook.
597 */
598 static int
599 ng_ether_rcv_lower(node_p node, struct mbuf *m)
600 {
601 const priv_p priv = NG_NODE_PRIVATE(node);
602 struct ifnet *const ifp = priv->ifp;
603
604 /* Check whether interface is ready for packets */
605 if (!((ifp->if_flags & IFF_UP) &&
606 (ifp->if_drv_flags & IFF_DRV_RUNNING))) {
607 NG_FREE_M(m);
608 return (ENETDOWN);
609 }
610
611 /* Make sure header is fully pulled up */
612 if (m->m_pkthdr.len < sizeof(struct ether_header)) {
613 NG_FREE_M(m);
614 return (EINVAL);
615 }
616 if (m->m_len < sizeof(struct ether_header)
617 && (m = m_pullup(m, sizeof(struct ether_header))) == NULL)
618 return (ENOBUFS);
619
620 /* Drop in the MAC address if desired */
621 if (priv->autoSrcAddr) {
622
623 /* Make the mbuf writable if it's not already */
624 if (!M_WRITABLE(m)
625 && (m = m_pullup(m, sizeof(struct ether_header))) == NULL)
626 return (ENOBUFS);
627
628 /* Overwrite source MAC address */
629 bcopy(IFP2ENADDR(ifp),
630 mtod(m, struct ether_header *)->ether_shost,
631 ETHER_ADDR_LEN);
632 }
633
634 /* Send it on its way */
635 return ether_output_frame(ifp, m);
636 }
637
638 /*
639 * Handle an mbuf received on the "upper" hook.
640 */
641 static int
642 ng_ether_rcv_upper(node_p node, struct mbuf *m)
643 {
644 const priv_p priv = NG_NODE_PRIVATE(node);
645 struct ifnet *ifp = priv->ifp;
646
647 m->m_pkthdr.rcvif = ifp;
648
649 if (BDG_ACTIVE(priv->ifp) )
650 if ((m = bridge_in_ptr(priv->ifp, m)) == NULL)
651 return (0);
652
653 /* Pass the packet to the bridge, it may come back to us */
654 if (ifp->if_bridge) {
655 BRIDGE_INPUT(ifp, m);
656 if (m == NULL)
657 return (0);
658 }
659
660 /* Route packet back in */
661 ether_demux(ifp, m);
662 return (0);
663 }
664
665 /*
666 * Shutdown node. This resets the node but does not remove it
667 * unless the REALLY_DIE flag is set.
668 */
669 static int
670 ng_ether_shutdown(node_p node)
671 {
672 const priv_p priv = NG_NODE_PRIVATE(node);
673
674 if (node->nd_flags & NGF_REALLY_DIE) {
675 /*
676 * WE came here because the ethernet card is being unloaded,
677 * so stop being persistant.
678 * Actually undo all the things we did on creation.
679 * Assume the ifp has already been freed.
680 */
681 NG_NODE_SET_PRIVATE(node, NULL);
682 FREE(priv, M_NETGRAPH);
683 NG_NODE_UNREF(node); /* free node itself */
684 return (0);
685 }
686 if (priv->promisc) { /* disable promiscuous mode */
687 (void)ifpromisc(priv->ifp, 0);
688 priv->promisc = 0;
689 }
690 priv->autoSrcAddr = 1; /* reset auto-src-addr flag */
691 NG_NODE_REVIVE(node); /* Signal ng_rmnode we are persisant */
692
693 return (0);
694 }
695
696 /*
697 * Hook disconnection.
698 */
699 static int
700 ng_ether_disconnect(hook_p hook)
701 {
702 const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
703
704 if (hook == priv->upper) {
705 priv->upper = NULL;
706 if (priv->ifp != NULL) /* restore h/w csum */
707 priv->ifp->if_hwassist = priv->hwassist;
708 } else if (hook == priv->lower)
709 priv->lower = NULL;
710 else if (hook == priv->orphan)
711 priv->orphan = NULL;
712 else
713 panic("%s: weird hook", __func__);
714 if ((NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0)
715 && (NG_NODE_IS_VALID(NG_HOOK_NODE(hook))))
716 ng_rmnode_self(NG_HOOK_NODE(hook)); /* reset node */
717 return (0);
718 }
719
720 /******************************************************************
721 INITIALIZATION
722 ******************************************************************/
723
724 /*
725 * Handle loading and unloading for this node type.
726 */
727 static int
728 ng_ether_mod_event(module_t mod, int event, void *data)
729 {
730 struct ifnet *ifp;
731 int error = 0;
732 int s;
733
734 s = splnet();
735 switch (event) {
736 case MOD_LOAD:
737
738 /* Register function hooks */
739 if (ng_ether_attach_p != NULL) {
740 error = EEXIST;
741 break;
742 }
743 ng_ether_attach_p = ng_ether_attach;
744 ng_ether_detach_p = ng_ether_detach;
745 ng_ether_output_p = ng_ether_output;
746 ng_ether_input_p = ng_ether_input;
747 ng_ether_input_orphan_p = ng_ether_input_orphan;
748 ng_ether_link_state_p = ng_ether_link_state;
749
750 /* Create nodes for any already-existing Ethernet interfaces */
751 IFNET_RLOCK();
752 TAILQ_FOREACH(ifp, &ifnet, if_link) {
753 if (ifp->if_type == IFT_ETHER
754 || ifp->if_type == IFT_L2VLAN)
755 ng_ether_attach(ifp);
756 }
757 IFNET_RUNLOCK();
758 break;
759
760 case MOD_UNLOAD:
761
762 /*
763 * Note that the base code won't try to unload us until
764 * all nodes have been removed, and that can't happen
765 * until all Ethernet interfaces are removed. In any
766 * case, we know there are no nodes left if the action
767 * is MOD_UNLOAD, so there's no need to detach any nodes.
768 */
769
770 /* Unregister function hooks */
771 ng_ether_attach_p = NULL;
772 ng_ether_detach_p = NULL;
773 ng_ether_output_p = NULL;
774 ng_ether_input_p = NULL;
775 ng_ether_input_orphan_p = NULL;
776 ng_ether_link_state_p = NULL;
777 break;
778
779 default:
780 error = EOPNOTSUPP;
781 break;
782 }
783 splx(s);
784 return (error);
785 }
786
Cache object: 16a8c2bdde5528c58f52259c4502d612
|