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