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