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