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