Deleted Added
full compact
ng_ether.c (82586) ng_ether.c (83366)
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 *
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: head/sys/netgraph/ng_ether.c 82586 2001-08-30 19:09:10Z archie $
40 * $FreeBSD: head/sys/netgraph/ng_ether.c 83366 2001-09-12 08:38:13Z julian $
41 */
42
43/*
44 * ng_ether(4) netgraph node type
45 */
46
47#include <sys/param.h>
48#include <sys/systm.h>
49#include <sys/kernel.h>
50#include <sys/malloc.h>
51#include <sys/mbuf.h>
52#include <sys/errno.h>
53#include <sys/syslog.h>
54#include <sys/socket.h>
55
56#include <net/if.h>
57#include <net/if_types.h>
58#include <net/if_arp.h>
59#include <net/if_var.h>
60#include <net/ethernet.h>
61
62#include <netgraph/ng_message.h>
63#include <netgraph/netgraph.h>
64#include <netgraph/ng_parse.h>
65#include <netgraph/ng_ether.h>
66
67#define IFP2AC(IFP) ((struct arpcom *)IFP)
68#define IFP2NG(ifp) ((struct ng_node *)((struct arpcom *)(ifp))->ac_netgraph)
69
70/* Per-node private data */
71struct private {
72 struct ifnet *ifp; /* associated interface */
73 hook_p upper; /* upper hook connection */
74 hook_p lower; /* lower OR orphan hook connection */
75 u_char lowerOrphan; /* whether lower is lower or orphan */
76 u_char autoSrcAddr; /* always overwrite source address */
77 u_char promisc; /* promiscuous mode enabled */
78 u_int flags; /* flags e.g. really die */
79};
80typedef struct private *priv_p;
81
82/* Functional hooks called from if_ethersubr.c */
83static void ng_ether_input(struct ifnet *ifp,
84 struct mbuf **mp, struct ether_header *eh);
85static void ng_ether_input_orphan(struct ifnet *ifp,
86 struct mbuf *m, struct ether_header *eh);
87static int ng_ether_output(struct ifnet *ifp, struct mbuf **mp);
88static void ng_ether_attach(struct ifnet *ifp);
89static void ng_ether_detach(struct ifnet *ifp);
90
91/* Other functions */
92static void ng_ether_input2(node_p node,
93 struct mbuf **mp, struct ether_header *eh);
94static int ng_ether_glueback_header(struct mbuf **mp,
95 struct ether_header *eh);
96static int ng_ether_rcv_lower(node_p node, struct mbuf *m, meta_p meta);
97static int ng_ether_rcv_upper(node_p node, struct mbuf *m, meta_p meta);
98
99/* Netgraph node methods */
100static ng_constructor_t ng_ether_constructor;
101static ng_rcvmsg_t ng_ether_rcvmsg;
102static ng_shutdown_t ng_ether_shutdown;
103static ng_newhook_t ng_ether_newhook;
104static ng_connect_t ng_ether_connect;
105static ng_rcvdata_t ng_ether_rcvdata;
106static ng_disconnect_t ng_ether_disconnect;
107static int ng_ether_mod_event(module_t mod, int event, void *data);
108
109/* Parse type for an Ethernet address */
110static ng_parse_t ng_enaddr_parse;
111static ng_unparse_t ng_enaddr_unparse;
112const struct ng_parse_type ng_ether_enaddr_type = {
113 NULL,
114 NULL,
115 NULL,
116 ng_enaddr_parse,
117 ng_enaddr_unparse,
118 NULL, /* no such thing as a "default" EN address */
119 0
120};
121
122/* List of commands and how to convert arguments to/from ASCII */
123static const struct ng_cmdlist ng_ether_cmdlist[] = {
124 {
125 NGM_ETHER_COOKIE,
126 NGM_ETHER_GET_IFNAME,
127 "getifname",
128 NULL,
129 &ng_parse_string_type
130 },
131 {
132 NGM_ETHER_COOKIE,
133 NGM_ETHER_GET_IFINDEX,
134 "getifindex",
135 NULL,
136 &ng_parse_int32_type
137 },
138 {
139 NGM_ETHER_COOKIE,
140 NGM_ETHER_GET_ENADDR,
141 "getenaddr",
142 NULL,
143 &ng_ether_enaddr_type
144 },
145 {
146 NGM_ETHER_COOKIE,
147 NGM_ETHER_SET_ENADDR,
148 "setenaddr",
149 &ng_ether_enaddr_type,
150 NULL
151 },
152 {
153 NGM_ETHER_COOKIE,
154 NGM_ETHER_GET_PROMISC,
155 "getpromisc",
156 NULL,
157 &ng_parse_int32_type
158 },
159 {
160 NGM_ETHER_COOKIE,
161 NGM_ETHER_SET_PROMISC,
162 "setpromisc",
163 &ng_parse_int32_type,
164 NULL
165 },
166 {
167 NGM_ETHER_COOKIE,
168 NGM_ETHER_GET_AUTOSRC,
169 "getautosrc",
170 NULL,
171 &ng_parse_int32_type
172 },
173 {
174 NGM_ETHER_COOKIE,
175 NGM_ETHER_SET_AUTOSRC,
176 "setautosrc",
177 &ng_parse_int32_type,
178 NULL
179 },
180 { 0 }
181};
182
183static struct ng_type ng_ether_typestruct = {
184 NG_ABI_VERSION,
185 NG_ETHER_NODE_TYPE,
186 ng_ether_mod_event,
187 ng_ether_constructor,
188 ng_ether_rcvmsg,
189 ng_ether_shutdown,
190 ng_ether_newhook,
191 NULL,
192 ng_ether_connect,
193 ng_ether_rcvdata,
194 ng_ether_disconnect,
195 ng_ether_cmdlist,
196};
197MODULE_VERSION(ng_ether, 1);
198NETGRAPH_INIT(ether, &ng_ether_typestruct);
199
200/******************************************************************
201 ETHERNET FUNCTION HOOKS
202******************************************************************/
203
204/*
205 * Handle a packet that has come in on an interface. We get to
206 * look at it here before any upper layer protocols do.
207 *
208 * NOTE: this function will get called at splimp()
209 */
210static void
211ng_ether_input(struct ifnet *ifp,
212 struct mbuf **mp, struct ether_header *eh)
213{
214 const node_p node = IFP2NG(ifp);
215 const priv_p priv = NG_NODE_PRIVATE(node);
216
217 /* If "lower" hook not connected, let packet continue */
218 if (priv->lower == NULL || priv->lowerOrphan)
219 return;
220 ng_ether_input2(node, mp, eh);
221}
222
223/*
224 * Handle a packet that has come in on an interface, and which
225 * does not match any of our known protocols (an ``orphan'').
226 *
227 * NOTE: this function will get called at splimp()
228 */
229static void
230ng_ether_input_orphan(struct ifnet *ifp,
231 struct mbuf *m, struct ether_header *eh)
232{
233 const node_p node = IFP2NG(ifp);
234 const priv_p priv = NG_NODE_PRIVATE(node);
235
236 /* If "orphan" hook not connected, let packet continue */
237 if (priv->lower == NULL || !priv->lowerOrphan) {
238 m_freem(m);
239 return;
240 }
241 ng_ether_input2(node, &m, eh);
242 if (m != NULL)
243 m_freem(m);
244}
245
246/*
247 * Handle a packet that has come in on an ethernet interface.
248 * The Ethernet header has already been detached from the mbuf,
249 * so we have to put it back.
250 *
251 * NOTE: this function will get called at splimp()
252 */
253static void
254ng_ether_input2(node_p node, struct mbuf **mp, struct ether_header *eh)
255{
256 const priv_p priv = NG_NODE_PRIVATE(node);
257 int error;
258
259 /* Glue Ethernet header back on */
260 if ((error = ng_ether_glueback_header(mp, eh)) != 0)
261 return;
262
263 /* Send out lower/orphan hook */
264 NG_SEND_DATA_ONLY(error, priv->lower, *mp);
265 *mp = NULL;
266}
267
268/*
269 * Handle a packet that is going out on an interface.
270 * The Ethernet header is already attached to the mbuf.
271 */
272static int
273ng_ether_output(struct ifnet *ifp, struct mbuf **mp)
274{
275 const node_p node = IFP2NG(ifp);
276 const priv_p priv = NG_NODE_PRIVATE(node);
277 int error = 0;
278
279 /* If "upper" hook not connected, let packet continue */
280 if (priv->upper == NULL)
281 return (0);
282
283 /* Send it out "upper" hook */
284 NG_SEND_DATA_ONLY(error, priv->upper, *mp);
285 return (error);
286}
287
288/*
289 * A new Ethernet interface has been attached.
290 * Create a new node for it, etc.
291 */
292static void
293ng_ether_attach(struct ifnet *ifp)
294{
295 char name[IFNAMSIZ + 1];
296 priv_p priv;
297 node_p node;
298
299 /* Create node */
300 KASSERT(!IFP2NG(ifp), ("%s: node already exists?", __FUNCTION__));
301 snprintf(name, sizeof(name), "%s%d", ifp->if_name, ifp->if_unit);
302 if (ng_make_node_common(&ng_ether_typestruct, &node) != 0) {
303 log(LOG_ERR, "%s: can't %s for %s\n",
304 __FUNCTION__, "create node", name);
305 return;
306 }
307
308 /* Allocate private data */
309 MALLOC(priv, priv_p, sizeof(*priv), M_NETGRAPH, M_NOWAIT | M_ZERO);
310 if (priv == NULL) {
311 log(LOG_ERR, "%s: can't %s for %s\n",
312 __FUNCTION__, "allocate memory", name);
313 NG_NODE_UNREF(node);
314 return;
315 }
316 NG_NODE_SET_PRIVATE(node, priv);
317 priv->ifp = ifp;
318 IFP2NG(ifp) = node;
319 priv->autoSrcAddr = 1;
320
321 /* Try to give the node the same name as the interface */
322 if (ng_name_node(node, name) != 0) {
323 log(LOG_WARNING, "%s: can't name node %s\n",
324 __FUNCTION__, name);
325 }
326}
327
328/*
329 * An Ethernet interface is being detached.
330 * REALLY Destroy its node.
331 */
332static void
333ng_ether_detach(struct ifnet *ifp)
334{
335 const node_p node = IFP2NG(ifp);
336 const priv_p priv = NG_NODE_PRIVATE(node);
337
338 if (node == NULL) /* no node (why not?), ignore */
339 return;
340 NG_NODE_REALLY_DIE(node); /* Force real removal of node */
341 /*
342 * We can't assume the ifnet is still around when we run shutdown
343 * So zap it now. XXX We HOPE that anything running at this time
344 * handles it (as it should in the non netgraph case).
345 */
346 IFP2NG(ifp) = NULL;
347 priv->ifp = NULL; /* XXX race if interrupted an output packet */
348 ng_rmnode_self(node); /* remove all netgraph parts */
349}
350
351/*
352 * Optimization for gluing the Ethernet header back onto
353 * the front of an incoming packet.
354 */
355static int
356ng_ether_glueback_header(struct mbuf **mp, struct ether_header *eh)
357{
358 struct mbuf *m = *mp;
359 uintfptr_t room;
360 int error = 0;
361
362 /*
363 * Possibly the header is already on the front.
364 * If this is the case so just move the markers back
365 * to re-include it. We lucked out.
366 * This allows us to avoid a yucky m_pullup
367 * in later nodes if it works.
368 */
369 if (eh == mtod(m, struct ether_header *) - 1) {
370 m->m_len += sizeof(*eh);
371 m->m_data -= sizeof(*eh);
372 m->m_pkthdr.len += sizeof(*eh);
373 goto done;
374 }
375
376 /*
377 * Alternatively there may be room even though
378 * it is stored somewhere else. If so, copy it in.
379 * This only safe because we KNOW that this packet has
380 * just been generated by an ethernet card, so there are
381 * no aliases to the buffer (not so for outgoing packets).
382 * Nearly all ethernet cards will end up producing mbufs
383 * that fall into these cases. So we are not optimizing
384 * contorted cases.
385 */
386 if ((m->m_flags & M_EXT) != 0) {
387 room = mtod(m, caddr_t) - m->m_ext.ext_buf;
388 if (room > m->m_ext.ext_size) /* garbage, fail immediately */
389 room = 0;
390 } else
391 room = mtod(m, caddr_t) - m->m_pktdat;
392
393 /*
394 * If we have room, just copy it and adjust
395 */
396 if (room >= sizeof(*eh)) {
397 m->m_len += sizeof(*eh);
398 m->m_data -= sizeof(*eh);
399 m->m_pkthdr.len += sizeof(*eh);
400 goto copy;
401 }
402
403 /*
404 * Doing anything more is likely to get more
405 * expensive than it's worth..
406 * it's probable that everything else is in one
407 * big lump. The next node will do an m_pullup()
408 * for exactly the amount of data it needs and
409 * hopefully everything after that will not
410 * need one. So let's just use M_PREPEND.
411 */
412 M_PREPEND(m, sizeof (*eh), M_DONTWAIT);
413 if (m == NULL) {
414 error = ENOBUFS;
415 goto done;
416 }
417
418copy:
419 /* Copy header and return (possibly new) mbuf */
420 bcopy((caddr_t)eh, mtod(m, struct ether_header *), sizeof(*eh));
421done:
422 *mp = m;
423 return error;
424}
425
426/******************************************************************
427 NETGRAPH NODE METHODS
428******************************************************************/
429
430/*
431 * It is not possible or allowable to create a node of this type.
432 * Nodes get created when the interface is attached (or, when
433 * this node type's KLD is loaded).
434 */
435static int
436ng_ether_constructor(node_p node)
437{
438 return (EINVAL);
439}
440
441/*
442 * Check for attaching a new hook.
443 */
444static int
445ng_ether_newhook(node_p node, hook_p hook, const char *name)
446{
447 const priv_p priv = NG_NODE_PRIVATE(node);
448 u_char orphan = priv->lowerOrphan;
449 hook_p *hookptr;
450
451 /* Divert hook is an alias for lower */
452 if (strcmp(name, NG_ETHER_HOOK_DIVERT) == 0)
453 name = NG_ETHER_HOOK_LOWER;
454
455 /* Which hook? */
456 if (strcmp(name, NG_ETHER_HOOK_UPPER) == 0)
457 hookptr = &priv->upper;
458 else if (strcmp(name, NG_ETHER_HOOK_LOWER) == 0) {
459 hookptr = &priv->lower;
460 orphan = 0;
461 } else if (strcmp(name, NG_ETHER_HOOK_ORPHAN) == 0) {
462 hookptr = &priv->lower;
463 orphan = 1;
464 } else
465 return (EINVAL);
466
467 /* Check if already connected (shouldn't be, but doesn't hurt) */
468 if (*hookptr != NULL)
469 return (EISCONN);
470
471 /* OK */
472 *hookptr = hook;
473 priv->lowerOrphan = orphan;
474 return (0);
475}
476
477/*
478 * Hooks are attached, adjust to force queueing.
479 * We don't really care which hook it is.
480 * they should all be queuing for outgoing data.
481 */
482static int
483ng_ether_connect(hook_p hook)
484{
485 NG_HOOK_FORCE_QUEUE(NG_HOOK_PEER(hook));
486 return (0);
487}
488
489/*
490 * Receive an incoming control message.
491 */
492static int
493ng_ether_rcvmsg(node_p node, item_p item, hook_p lasthook)
494{
495 const priv_p priv = NG_NODE_PRIVATE(node);
496 struct ng_mesg *resp = NULL;
497 int error = 0;
498 struct ng_mesg *msg;
499
500 NGI_GET_MSG(item, msg);
501 switch (msg->header.typecookie) {
502 case NGM_ETHER_COOKIE:
503 switch (msg->header.cmd) {
504 case NGM_ETHER_GET_IFNAME:
505 NG_MKRESPONSE(resp, msg, IFNAMSIZ + 1, M_NOWAIT);
506 if (resp == NULL) {
507 error = ENOMEM;
508 break;
509 }
510 snprintf(resp->data, IFNAMSIZ + 1,
511 "%s%d", priv->ifp->if_name, priv->ifp->if_unit);
512 break;
513 case NGM_ETHER_GET_IFINDEX:
514 NG_MKRESPONSE(resp, msg, sizeof(u_int32_t), M_NOWAIT);
515 if (resp == NULL) {
516 error = ENOMEM;
517 break;
518 }
519 *((u_int32_t *)resp->data) = priv->ifp->if_index;
520 break;
521 case NGM_ETHER_GET_ENADDR:
522 NG_MKRESPONSE(resp, msg, ETHER_ADDR_LEN, M_NOWAIT);
523 if (resp == NULL) {
524 error = ENOMEM;
525 break;
526 }
527 bcopy((IFP2AC(priv->ifp))->ac_enaddr,
528 resp->data, ETHER_ADDR_LEN);
529 break;
530 case NGM_ETHER_SET_ENADDR:
531 {
532 if (msg->header.arglen != ETHER_ADDR_LEN) {
533 error = EINVAL;
534 break;
535 }
536 error = if_setlladdr(priv->ifp,
537 (u_char *)msg->data, ETHER_ADDR_LEN);
538 break;
539 }
540 case NGM_ETHER_GET_PROMISC:
541 NG_MKRESPONSE(resp, msg, sizeof(u_int32_t), M_NOWAIT);
542 if (resp == NULL) {
543 error = ENOMEM;
544 break;
545 }
546 *((u_int32_t *)resp->data) = priv->promisc;
547 break;
548 case NGM_ETHER_SET_PROMISC:
549 {
550 u_char want;
551
552 if (msg->header.arglen != sizeof(u_int32_t)) {
553 error = EINVAL;
554 break;
555 }
556 want = !!*((u_int32_t *)msg->data);
557 if (want ^ priv->promisc) {
558 if ((error = ifpromisc(priv->ifp, want)) != 0)
559 break;
560 priv->promisc = want;
561 }
562 break;
563 }
564 case NGM_ETHER_GET_AUTOSRC:
565 NG_MKRESPONSE(resp, msg, sizeof(u_int32_t), M_NOWAIT);
566 if (resp == NULL) {
567 error = ENOMEM;
568 break;
569 }
570 *((u_int32_t *)resp->data) = priv->autoSrcAddr;
571 break;
572 case NGM_ETHER_SET_AUTOSRC:
573 if (msg->header.arglen != sizeof(u_int32_t)) {
574 error = EINVAL;
575 break;
576 }
577 priv->autoSrcAddr = !!*((u_int32_t *)msg->data);
578 break;
579 default:
580 error = EINVAL;
581 break;
582 }
583 break;
584 default:
585 error = EINVAL;
586 break;
587 }
588 NG_RESPOND_MSG(error, node, item, resp);
589 NG_FREE_MSG(msg);
590 return (error);
591}
592
593/*
594 * Receive data on a hook.
595 */
596static int
597ng_ether_rcvdata(hook_p hook, item_p item)
598{
599 const node_p node = NG_HOOK_NODE(hook);
600 const priv_p priv = NG_NODE_PRIVATE(node);
601 struct mbuf *m;
602 meta_p meta;
603
604 NGI_GET_M(item, m);
605 NGI_GET_META(item, meta);
606 NG_FREE_ITEM(item);
607 if (hook == priv->lower)
608 return ng_ether_rcv_lower(node, m, meta);
609 if (hook == priv->upper)
610 return ng_ether_rcv_upper(node, m, meta);
611 panic("%s: weird hook", __FUNCTION__);
41 */
42
43/*
44 * ng_ether(4) netgraph node type
45 */
46
47#include <sys/param.h>
48#include <sys/systm.h>
49#include <sys/kernel.h>
50#include <sys/malloc.h>
51#include <sys/mbuf.h>
52#include <sys/errno.h>
53#include <sys/syslog.h>
54#include <sys/socket.h>
55
56#include <net/if.h>
57#include <net/if_types.h>
58#include <net/if_arp.h>
59#include <net/if_var.h>
60#include <net/ethernet.h>
61
62#include <netgraph/ng_message.h>
63#include <netgraph/netgraph.h>
64#include <netgraph/ng_parse.h>
65#include <netgraph/ng_ether.h>
66
67#define IFP2AC(IFP) ((struct arpcom *)IFP)
68#define IFP2NG(ifp) ((struct ng_node *)((struct arpcom *)(ifp))->ac_netgraph)
69
70/* Per-node private data */
71struct private {
72 struct ifnet *ifp; /* associated interface */
73 hook_p upper; /* upper hook connection */
74 hook_p lower; /* lower OR orphan hook connection */
75 u_char lowerOrphan; /* whether lower is lower or orphan */
76 u_char autoSrcAddr; /* always overwrite source address */
77 u_char promisc; /* promiscuous mode enabled */
78 u_int flags; /* flags e.g. really die */
79};
80typedef struct private *priv_p;
81
82/* Functional hooks called from if_ethersubr.c */
83static void ng_ether_input(struct ifnet *ifp,
84 struct mbuf **mp, struct ether_header *eh);
85static void ng_ether_input_orphan(struct ifnet *ifp,
86 struct mbuf *m, struct ether_header *eh);
87static int ng_ether_output(struct ifnet *ifp, struct mbuf **mp);
88static void ng_ether_attach(struct ifnet *ifp);
89static void ng_ether_detach(struct ifnet *ifp);
90
91/* Other functions */
92static void ng_ether_input2(node_p node,
93 struct mbuf **mp, struct ether_header *eh);
94static int ng_ether_glueback_header(struct mbuf **mp,
95 struct ether_header *eh);
96static int ng_ether_rcv_lower(node_p node, struct mbuf *m, meta_p meta);
97static int ng_ether_rcv_upper(node_p node, struct mbuf *m, meta_p meta);
98
99/* Netgraph node methods */
100static ng_constructor_t ng_ether_constructor;
101static ng_rcvmsg_t ng_ether_rcvmsg;
102static ng_shutdown_t ng_ether_shutdown;
103static ng_newhook_t ng_ether_newhook;
104static ng_connect_t ng_ether_connect;
105static ng_rcvdata_t ng_ether_rcvdata;
106static ng_disconnect_t ng_ether_disconnect;
107static int ng_ether_mod_event(module_t mod, int event, void *data);
108
109/* Parse type for an Ethernet address */
110static ng_parse_t ng_enaddr_parse;
111static ng_unparse_t ng_enaddr_unparse;
112const struct ng_parse_type ng_ether_enaddr_type = {
113 NULL,
114 NULL,
115 NULL,
116 ng_enaddr_parse,
117 ng_enaddr_unparse,
118 NULL, /* no such thing as a "default" EN address */
119 0
120};
121
122/* List of commands and how to convert arguments to/from ASCII */
123static const struct ng_cmdlist ng_ether_cmdlist[] = {
124 {
125 NGM_ETHER_COOKIE,
126 NGM_ETHER_GET_IFNAME,
127 "getifname",
128 NULL,
129 &ng_parse_string_type
130 },
131 {
132 NGM_ETHER_COOKIE,
133 NGM_ETHER_GET_IFINDEX,
134 "getifindex",
135 NULL,
136 &ng_parse_int32_type
137 },
138 {
139 NGM_ETHER_COOKIE,
140 NGM_ETHER_GET_ENADDR,
141 "getenaddr",
142 NULL,
143 &ng_ether_enaddr_type
144 },
145 {
146 NGM_ETHER_COOKIE,
147 NGM_ETHER_SET_ENADDR,
148 "setenaddr",
149 &ng_ether_enaddr_type,
150 NULL
151 },
152 {
153 NGM_ETHER_COOKIE,
154 NGM_ETHER_GET_PROMISC,
155 "getpromisc",
156 NULL,
157 &ng_parse_int32_type
158 },
159 {
160 NGM_ETHER_COOKIE,
161 NGM_ETHER_SET_PROMISC,
162 "setpromisc",
163 &ng_parse_int32_type,
164 NULL
165 },
166 {
167 NGM_ETHER_COOKIE,
168 NGM_ETHER_GET_AUTOSRC,
169 "getautosrc",
170 NULL,
171 &ng_parse_int32_type
172 },
173 {
174 NGM_ETHER_COOKIE,
175 NGM_ETHER_SET_AUTOSRC,
176 "setautosrc",
177 &ng_parse_int32_type,
178 NULL
179 },
180 { 0 }
181};
182
183static struct ng_type ng_ether_typestruct = {
184 NG_ABI_VERSION,
185 NG_ETHER_NODE_TYPE,
186 ng_ether_mod_event,
187 ng_ether_constructor,
188 ng_ether_rcvmsg,
189 ng_ether_shutdown,
190 ng_ether_newhook,
191 NULL,
192 ng_ether_connect,
193 ng_ether_rcvdata,
194 ng_ether_disconnect,
195 ng_ether_cmdlist,
196};
197MODULE_VERSION(ng_ether, 1);
198NETGRAPH_INIT(ether, &ng_ether_typestruct);
199
200/******************************************************************
201 ETHERNET FUNCTION HOOKS
202******************************************************************/
203
204/*
205 * Handle a packet that has come in on an interface. We get to
206 * look at it here before any upper layer protocols do.
207 *
208 * NOTE: this function will get called at splimp()
209 */
210static void
211ng_ether_input(struct ifnet *ifp,
212 struct mbuf **mp, struct ether_header *eh)
213{
214 const node_p node = IFP2NG(ifp);
215 const priv_p priv = NG_NODE_PRIVATE(node);
216
217 /* If "lower" hook not connected, let packet continue */
218 if (priv->lower == NULL || priv->lowerOrphan)
219 return;
220 ng_ether_input2(node, mp, eh);
221}
222
223/*
224 * Handle a packet that has come in on an interface, and which
225 * does not match any of our known protocols (an ``orphan'').
226 *
227 * NOTE: this function will get called at splimp()
228 */
229static void
230ng_ether_input_orphan(struct ifnet *ifp,
231 struct mbuf *m, struct ether_header *eh)
232{
233 const node_p node = IFP2NG(ifp);
234 const priv_p priv = NG_NODE_PRIVATE(node);
235
236 /* If "orphan" hook not connected, let packet continue */
237 if (priv->lower == NULL || !priv->lowerOrphan) {
238 m_freem(m);
239 return;
240 }
241 ng_ether_input2(node, &m, eh);
242 if (m != NULL)
243 m_freem(m);
244}
245
246/*
247 * Handle a packet that has come in on an ethernet interface.
248 * The Ethernet header has already been detached from the mbuf,
249 * so we have to put it back.
250 *
251 * NOTE: this function will get called at splimp()
252 */
253static void
254ng_ether_input2(node_p node, struct mbuf **mp, struct ether_header *eh)
255{
256 const priv_p priv = NG_NODE_PRIVATE(node);
257 int error;
258
259 /* Glue Ethernet header back on */
260 if ((error = ng_ether_glueback_header(mp, eh)) != 0)
261 return;
262
263 /* Send out lower/orphan hook */
264 NG_SEND_DATA_ONLY(error, priv->lower, *mp);
265 *mp = NULL;
266}
267
268/*
269 * Handle a packet that is going out on an interface.
270 * The Ethernet header is already attached to the mbuf.
271 */
272static int
273ng_ether_output(struct ifnet *ifp, struct mbuf **mp)
274{
275 const node_p node = IFP2NG(ifp);
276 const priv_p priv = NG_NODE_PRIVATE(node);
277 int error = 0;
278
279 /* If "upper" hook not connected, let packet continue */
280 if (priv->upper == NULL)
281 return (0);
282
283 /* Send it out "upper" hook */
284 NG_SEND_DATA_ONLY(error, priv->upper, *mp);
285 return (error);
286}
287
288/*
289 * A new Ethernet interface has been attached.
290 * Create a new node for it, etc.
291 */
292static void
293ng_ether_attach(struct ifnet *ifp)
294{
295 char name[IFNAMSIZ + 1];
296 priv_p priv;
297 node_p node;
298
299 /* Create node */
300 KASSERT(!IFP2NG(ifp), ("%s: node already exists?", __FUNCTION__));
301 snprintf(name, sizeof(name), "%s%d", ifp->if_name, ifp->if_unit);
302 if (ng_make_node_common(&ng_ether_typestruct, &node) != 0) {
303 log(LOG_ERR, "%s: can't %s for %s\n",
304 __FUNCTION__, "create node", name);
305 return;
306 }
307
308 /* Allocate private data */
309 MALLOC(priv, priv_p, sizeof(*priv), M_NETGRAPH, M_NOWAIT | M_ZERO);
310 if (priv == NULL) {
311 log(LOG_ERR, "%s: can't %s for %s\n",
312 __FUNCTION__, "allocate memory", name);
313 NG_NODE_UNREF(node);
314 return;
315 }
316 NG_NODE_SET_PRIVATE(node, priv);
317 priv->ifp = ifp;
318 IFP2NG(ifp) = node;
319 priv->autoSrcAddr = 1;
320
321 /* Try to give the node the same name as the interface */
322 if (ng_name_node(node, name) != 0) {
323 log(LOG_WARNING, "%s: can't name node %s\n",
324 __FUNCTION__, name);
325 }
326}
327
328/*
329 * An Ethernet interface is being detached.
330 * REALLY Destroy its node.
331 */
332static void
333ng_ether_detach(struct ifnet *ifp)
334{
335 const node_p node = IFP2NG(ifp);
336 const priv_p priv = NG_NODE_PRIVATE(node);
337
338 if (node == NULL) /* no node (why not?), ignore */
339 return;
340 NG_NODE_REALLY_DIE(node); /* Force real removal of node */
341 /*
342 * We can't assume the ifnet is still around when we run shutdown
343 * So zap it now. XXX We HOPE that anything running at this time
344 * handles it (as it should in the non netgraph case).
345 */
346 IFP2NG(ifp) = NULL;
347 priv->ifp = NULL; /* XXX race if interrupted an output packet */
348 ng_rmnode_self(node); /* remove all netgraph parts */
349}
350
351/*
352 * Optimization for gluing the Ethernet header back onto
353 * the front of an incoming packet.
354 */
355static int
356ng_ether_glueback_header(struct mbuf **mp, struct ether_header *eh)
357{
358 struct mbuf *m = *mp;
359 uintfptr_t room;
360 int error = 0;
361
362 /*
363 * Possibly the header is already on the front.
364 * If this is the case so just move the markers back
365 * to re-include it. We lucked out.
366 * This allows us to avoid a yucky m_pullup
367 * in later nodes if it works.
368 */
369 if (eh == mtod(m, struct ether_header *) - 1) {
370 m->m_len += sizeof(*eh);
371 m->m_data -= sizeof(*eh);
372 m->m_pkthdr.len += sizeof(*eh);
373 goto done;
374 }
375
376 /*
377 * Alternatively there may be room even though
378 * it is stored somewhere else. If so, copy it in.
379 * This only safe because we KNOW that this packet has
380 * just been generated by an ethernet card, so there are
381 * no aliases to the buffer (not so for outgoing packets).
382 * Nearly all ethernet cards will end up producing mbufs
383 * that fall into these cases. So we are not optimizing
384 * contorted cases.
385 */
386 if ((m->m_flags & M_EXT) != 0) {
387 room = mtod(m, caddr_t) - m->m_ext.ext_buf;
388 if (room > m->m_ext.ext_size) /* garbage, fail immediately */
389 room = 0;
390 } else
391 room = mtod(m, caddr_t) - m->m_pktdat;
392
393 /*
394 * If we have room, just copy it and adjust
395 */
396 if (room >= sizeof(*eh)) {
397 m->m_len += sizeof(*eh);
398 m->m_data -= sizeof(*eh);
399 m->m_pkthdr.len += sizeof(*eh);
400 goto copy;
401 }
402
403 /*
404 * Doing anything more is likely to get more
405 * expensive than it's worth..
406 * it's probable that everything else is in one
407 * big lump. The next node will do an m_pullup()
408 * for exactly the amount of data it needs and
409 * hopefully everything after that will not
410 * need one. So let's just use M_PREPEND.
411 */
412 M_PREPEND(m, sizeof (*eh), M_DONTWAIT);
413 if (m == NULL) {
414 error = ENOBUFS;
415 goto done;
416 }
417
418copy:
419 /* Copy header and return (possibly new) mbuf */
420 bcopy((caddr_t)eh, mtod(m, struct ether_header *), sizeof(*eh));
421done:
422 *mp = m;
423 return error;
424}
425
426/******************************************************************
427 NETGRAPH NODE METHODS
428******************************************************************/
429
430/*
431 * It is not possible or allowable to create a node of this type.
432 * Nodes get created when the interface is attached (or, when
433 * this node type's KLD is loaded).
434 */
435static int
436ng_ether_constructor(node_p node)
437{
438 return (EINVAL);
439}
440
441/*
442 * Check for attaching a new hook.
443 */
444static int
445ng_ether_newhook(node_p node, hook_p hook, const char *name)
446{
447 const priv_p priv = NG_NODE_PRIVATE(node);
448 u_char orphan = priv->lowerOrphan;
449 hook_p *hookptr;
450
451 /* Divert hook is an alias for lower */
452 if (strcmp(name, NG_ETHER_HOOK_DIVERT) == 0)
453 name = NG_ETHER_HOOK_LOWER;
454
455 /* Which hook? */
456 if (strcmp(name, NG_ETHER_HOOK_UPPER) == 0)
457 hookptr = &priv->upper;
458 else if (strcmp(name, NG_ETHER_HOOK_LOWER) == 0) {
459 hookptr = &priv->lower;
460 orphan = 0;
461 } else if (strcmp(name, NG_ETHER_HOOK_ORPHAN) == 0) {
462 hookptr = &priv->lower;
463 orphan = 1;
464 } else
465 return (EINVAL);
466
467 /* Check if already connected (shouldn't be, but doesn't hurt) */
468 if (*hookptr != NULL)
469 return (EISCONN);
470
471 /* OK */
472 *hookptr = hook;
473 priv->lowerOrphan = orphan;
474 return (0);
475}
476
477/*
478 * Hooks are attached, adjust to force queueing.
479 * We don't really care which hook it is.
480 * they should all be queuing for outgoing data.
481 */
482static int
483ng_ether_connect(hook_p hook)
484{
485 NG_HOOK_FORCE_QUEUE(NG_HOOK_PEER(hook));
486 return (0);
487}
488
489/*
490 * Receive an incoming control message.
491 */
492static int
493ng_ether_rcvmsg(node_p node, item_p item, hook_p lasthook)
494{
495 const priv_p priv = NG_NODE_PRIVATE(node);
496 struct ng_mesg *resp = NULL;
497 int error = 0;
498 struct ng_mesg *msg;
499
500 NGI_GET_MSG(item, msg);
501 switch (msg->header.typecookie) {
502 case NGM_ETHER_COOKIE:
503 switch (msg->header.cmd) {
504 case NGM_ETHER_GET_IFNAME:
505 NG_MKRESPONSE(resp, msg, IFNAMSIZ + 1, M_NOWAIT);
506 if (resp == NULL) {
507 error = ENOMEM;
508 break;
509 }
510 snprintf(resp->data, IFNAMSIZ + 1,
511 "%s%d", priv->ifp->if_name, priv->ifp->if_unit);
512 break;
513 case NGM_ETHER_GET_IFINDEX:
514 NG_MKRESPONSE(resp, msg, sizeof(u_int32_t), M_NOWAIT);
515 if (resp == NULL) {
516 error = ENOMEM;
517 break;
518 }
519 *((u_int32_t *)resp->data) = priv->ifp->if_index;
520 break;
521 case NGM_ETHER_GET_ENADDR:
522 NG_MKRESPONSE(resp, msg, ETHER_ADDR_LEN, M_NOWAIT);
523 if (resp == NULL) {
524 error = ENOMEM;
525 break;
526 }
527 bcopy((IFP2AC(priv->ifp))->ac_enaddr,
528 resp->data, ETHER_ADDR_LEN);
529 break;
530 case NGM_ETHER_SET_ENADDR:
531 {
532 if (msg->header.arglen != ETHER_ADDR_LEN) {
533 error = EINVAL;
534 break;
535 }
536 error = if_setlladdr(priv->ifp,
537 (u_char *)msg->data, ETHER_ADDR_LEN);
538 break;
539 }
540 case NGM_ETHER_GET_PROMISC:
541 NG_MKRESPONSE(resp, msg, sizeof(u_int32_t), M_NOWAIT);
542 if (resp == NULL) {
543 error = ENOMEM;
544 break;
545 }
546 *((u_int32_t *)resp->data) = priv->promisc;
547 break;
548 case NGM_ETHER_SET_PROMISC:
549 {
550 u_char want;
551
552 if (msg->header.arglen != sizeof(u_int32_t)) {
553 error = EINVAL;
554 break;
555 }
556 want = !!*((u_int32_t *)msg->data);
557 if (want ^ priv->promisc) {
558 if ((error = ifpromisc(priv->ifp, want)) != 0)
559 break;
560 priv->promisc = want;
561 }
562 break;
563 }
564 case NGM_ETHER_GET_AUTOSRC:
565 NG_MKRESPONSE(resp, msg, sizeof(u_int32_t), M_NOWAIT);
566 if (resp == NULL) {
567 error = ENOMEM;
568 break;
569 }
570 *((u_int32_t *)resp->data) = priv->autoSrcAddr;
571 break;
572 case NGM_ETHER_SET_AUTOSRC:
573 if (msg->header.arglen != sizeof(u_int32_t)) {
574 error = EINVAL;
575 break;
576 }
577 priv->autoSrcAddr = !!*((u_int32_t *)msg->data);
578 break;
579 default:
580 error = EINVAL;
581 break;
582 }
583 break;
584 default:
585 error = EINVAL;
586 break;
587 }
588 NG_RESPOND_MSG(error, node, item, resp);
589 NG_FREE_MSG(msg);
590 return (error);
591}
592
593/*
594 * Receive data on a hook.
595 */
596static int
597ng_ether_rcvdata(hook_p hook, item_p item)
598{
599 const node_p node = NG_HOOK_NODE(hook);
600 const priv_p priv = NG_NODE_PRIVATE(node);
601 struct mbuf *m;
602 meta_p meta;
603
604 NGI_GET_M(item, m);
605 NGI_GET_META(item, meta);
606 NG_FREE_ITEM(item);
607 if (hook == priv->lower)
608 return ng_ether_rcv_lower(node, m, meta);
609 if (hook == priv->upper)
610 return ng_ether_rcv_upper(node, m, meta);
611 panic("%s: weird hook", __FUNCTION__);
612#ifdef RESTARTABLE_PANICS /* so we don;t get an error msg in LINT */
613 return NULL;
614#endif
612}
613
614/*
615 * Handle an mbuf received on the "lower" hook.
616 */
617static int
618ng_ether_rcv_lower(node_p node, struct mbuf *m, meta_p meta)
619{
620 const priv_p priv = NG_NODE_PRIVATE(node);
621
622 /* Make sure header is fully pulled up */
623 if (m->m_pkthdr.len < sizeof(struct ether_header)) {
624 NG_FREE_M(m);
625 NG_FREE_META(meta);
626 return (EINVAL);
627 }
628 if (m->m_len < sizeof(struct ether_header)
629 && (m = m_pullup(m, sizeof(struct ether_header))) == NULL) {
630 NG_FREE_META(meta);
631 return (ENOBUFS);
632 }
633
634 /* Drop in the MAC address if desired */
635 if (priv->autoSrcAddr) {
636 bcopy((IFP2AC(priv->ifp))->ac_enaddr,
637 mtod(m, struct ether_header *)->ether_shost,
638 ETHER_ADDR_LEN);
639 }
640
641 /* Send it on its way */
642 NG_FREE_META(meta);
643 return ether_output_frame(priv->ifp, m);
644}
645
646/*
647 * Handle an mbuf received on the "upper" hook.
648 */
649static int
650ng_ether_rcv_upper(node_p node, struct mbuf *m, meta_p meta)
651{
652 const priv_p priv = NG_NODE_PRIVATE(node);
653 struct ether_header *eh;
654
655 /* Check length and pull off header */
656 if (m->m_pkthdr.len < sizeof(*eh)) {
657 NG_FREE_M(m);
658 NG_FREE_META(meta);
659 return (EINVAL);
660 }
661 if (m->m_len < sizeof(*eh) && (m = m_pullup(m, sizeof(*eh))) == NULL) {
662 NG_FREE_META(meta);
663 return (ENOBUFS);
664 }
665 eh = mtod(m, struct ether_header *);
666 m->m_data += sizeof(*eh);
667 m->m_len -= sizeof(*eh);
668 m->m_pkthdr.len -= sizeof(*eh);
669 m->m_pkthdr.rcvif = priv->ifp;
670
671 /* Route packet back in */
672 NG_FREE_META(meta);
673 ether_demux(priv->ifp, eh, m);
674 return (0);
675}
676
677/*
678 * Shutdown node. This resets the node but does not remove it
679 * unless the REALLY_DIE flag is set.
680 */
681static int
682ng_ether_shutdown(node_p node)
683{
684 const priv_p priv = NG_NODE_PRIVATE(node);
685
686 if (priv->promisc) { /* disable promiscuous mode */
687 (void)ifpromisc(priv->ifp, 0);
688 priv->promisc = 0;
689 }
690 if (node->nd_flags & NG_REALLY_DIE) {
691 /*
692 * WE came here because the ethernet card is being unloaded,
693 * so stop being persistant.
694 * Actually undo all the things we did on creation.
695 * Assume the ifp has already been freed.
696 */
697 NG_NODE_SET_PRIVATE(node, NULL);
698 FREE(priv, M_NETGRAPH);
699 NG_NODE_UNREF(node); /* free node itself */
700 return (0);
701 }
702 priv->autoSrcAddr = 1; /* reset auto-src-addr flag */
703 node->nd_flags &= ~NG_INVALID; /* Signal ng_rmnode we are persisant */
704 return (0);
705}
706
707/*
708 * Hook disconnection.
709 */
710static int
711ng_ether_disconnect(hook_p hook)
712{
713 const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
714
715 if (hook == priv->upper)
716 priv->upper = NULL;
717 else if (hook == priv->lower) {
718 priv->lower = NULL;
719 priv->lowerOrphan = 0;
720 } else
721 panic("%s: weird hook", __FUNCTION__);
722 if ((NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0)
723 && (NG_NODE_IS_VALID(NG_HOOK_NODE(hook))))
724 ng_rmnode_self(NG_HOOK_NODE(hook)); /* reset node */
725 return (0);
726}
727
728static int
729ng_enaddr_parse(const struct ng_parse_type *type,
730 const char *s, int *const off, const u_char *const start,
731 u_char *const buf, int *const buflen)
732{
733 char *eptr;
734 u_long val;
735 int i;
736
737 if (*buflen < ETHER_ADDR_LEN)
738 return (ERANGE);
739 for (i = 0; i < ETHER_ADDR_LEN; i++) {
740 val = strtoul(s + *off, &eptr, 16);
741 if (val > 0xff || eptr == s + *off)
742 return (EINVAL);
743 buf[i] = (u_char)val;
744 *off = (eptr - s);
745 if (i < ETHER_ADDR_LEN - 1) {
746 if (*eptr != ':')
747 return (EINVAL);
748 (*off)++;
749 }
750 }
751 *buflen = ETHER_ADDR_LEN;
752 return (0);
753}
754
755static int
756ng_enaddr_unparse(const struct ng_parse_type *type,
757 const u_char *data, int *off, char *cbuf, int cbuflen)
758{
759 int len;
760
761 len = snprintf(cbuf, cbuflen, "%02x:%02x:%02x:%02x:%02x:%02x",
762 data[*off], data[*off + 1], data[*off + 2],
763 data[*off + 3], data[*off + 4], data[*off + 5]);
764 if (len >= cbuflen)
765 return (ERANGE);
766 *off += ETHER_ADDR_LEN;
767 return (0);
768}
769
770/******************************************************************
771 INITIALIZATION
772******************************************************************/
773
774/*
775 * Handle loading and unloading for this node type.
776 */
777static int
778ng_ether_mod_event(module_t mod, int event, void *data)
779{
780 struct ifnet *ifp;
781 int error = 0;
782 int s;
783
784 s = splnet();
785 switch (event) {
786 case MOD_LOAD:
787
788 /* Register function hooks */
789 if (ng_ether_attach_p != NULL) {
790 error = EEXIST;
791 break;
792 }
793 ng_ether_attach_p = ng_ether_attach;
794 ng_ether_detach_p = ng_ether_detach;
795 ng_ether_output_p = ng_ether_output;
796 ng_ether_input_p = ng_ether_input;
797 ng_ether_input_orphan_p = ng_ether_input_orphan;
798
799 /* Create nodes for any already-existing Ethernet interfaces */
800 TAILQ_FOREACH(ifp, &ifnet, if_link) {
801 if (ifp->if_type == IFT_ETHER
802 || ifp->if_type == IFT_L2VLAN)
803 ng_ether_attach(ifp);
804 }
805 break;
806
807 case MOD_UNLOAD:
808
809 /*
810 * Note that the base code won't try to unload us until
811 * all nodes have been removed, and that can't happen
812 * until all Ethernet interfaces are removed. In any
813 * case, we know there are no nodes left if the action
814 * is MOD_UNLOAD, so there's no need to detach any nodes.
815 */
816
817 /* Unregister function hooks */
818 ng_ether_attach_p = NULL;
819 ng_ether_detach_p = NULL;
820 ng_ether_output_p = NULL;
821 ng_ether_input_p = NULL;
822 ng_ether_input_orphan_p = NULL;
823 break;
824
825 default:
826 error = EOPNOTSUPP;
827 break;
828 }
829 splx(s);
830 return (error);
831}
832
615}
616
617/*
618 * Handle an mbuf received on the "lower" hook.
619 */
620static int
621ng_ether_rcv_lower(node_p node, struct mbuf *m, meta_p meta)
622{
623 const priv_p priv = NG_NODE_PRIVATE(node);
624
625 /* Make sure header is fully pulled up */
626 if (m->m_pkthdr.len < sizeof(struct ether_header)) {
627 NG_FREE_M(m);
628 NG_FREE_META(meta);
629 return (EINVAL);
630 }
631 if (m->m_len < sizeof(struct ether_header)
632 && (m = m_pullup(m, sizeof(struct ether_header))) == NULL) {
633 NG_FREE_META(meta);
634 return (ENOBUFS);
635 }
636
637 /* Drop in the MAC address if desired */
638 if (priv->autoSrcAddr) {
639 bcopy((IFP2AC(priv->ifp))->ac_enaddr,
640 mtod(m, struct ether_header *)->ether_shost,
641 ETHER_ADDR_LEN);
642 }
643
644 /* Send it on its way */
645 NG_FREE_META(meta);
646 return ether_output_frame(priv->ifp, m);
647}
648
649/*
650 * Handle an mbuf received on the "upper" hook.
651 */
652static int
653ng_ether_rcv_upper(node_p node, struct mbuf *m, meta_p meta)
654{
655 const priv_p priv = NG_NODE_PRIVATE(node);
656 struct ether_header *eh;
657
658 /* Check length and pull off header */
659 if (m->m_pkthdr.len < sizeof(*eh)) {
660 NG_FREE_M(m);
661 NG_FREE_META(meta);
662 return (EINVAL);
663 }
664 if (m->m_len < sizeof(*eh) && (m = m_pullup(m, sizeof(*eh))) == NULL) {
665 NG_FREE_META(meta);
666 return (ENOBUFS);
667 }
668 eh = mtod(m, struct ether_header *);
669 m->m_data += sizeof(*eh);
670 m->m_len -= sizeof(*eh);
671 m->m_pkthdr.len -= sizeof(*eh);
672 m->m_pkthdr.rcvif = priv->ifp;
673
674 /* Route packet back in */
675 NG_FREE_META(meta);
676 ether_demux(priv->ifp, eh, m);
677 return (0);
678}
679
680/*
681 * Shutdown node. This resets the node but does not remove it
682 * unless the REALLY_DIE flag is set.
683 */
684static int
685ng_ether_shutdown(node_p node)
686{
687 const priv_p priv = NG_NODE_PRIVATE(node);
688
689 if (priv->promisc) { /* disable promiscuous mode */
690 (void)ifpromisc(priv->ifp, 0);
691 priv->promisc = 0;
692 }
693 if (node->nd_flags & NG_REALLY_DIE) {
694 /*
695 * WE came here because the ethernet card is being unloaded,
696 * so stop being persistant.
697 * Actually undo all the things we did on creation.
698 * Assume the ifp has already been freed.
699 */
700 NG_NODE_SET_PRIVATE(node, NULL);
701 FREE(priv, M_NETGRAPH);
702 NG_NODE_UNREF(node); /* free node itself */
703 return (0);
704 }
705 priv->autoSrcAddr = 1; /* reset auto-src-addr flag */
706 node->nd_flags &= ~NG_INVALID; /* Signal ng_rmnode we are persisant */
707 return (0);
708}
709
710/*
711 * Hook disconnection.
712 */
713static int
714ng_ether_disconnect(hook_p hook)
715{
716 const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
717
718 if (hook == priv->upper)
719 priv->upper = NULL;
720 else if (hook == priv->lower) {
721 priv->lower = NULL;
722 priv->lowerOrphan = 0;
723 } else
724 panic("%s: weird hook", __FUNCTION__);
725 if ((NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0)
726 && (NG_NODE_IS_VALID(NG_HOOK_NODE(hook))))
727 ng_rmnode_self(NG_HOOK_NODE(hook)); /* reset node */
728 return (0);
729}
730
731static int
732ng_enaddr_parse(const struct ng_parse_type *type,
733 const char *s, int *const off, const u_char *const start,
734 u_char *const buf, int *const buflen)
735{
736 char *eptr;
737 u_long val;
738 int i;
739
740 if (*buflen < ETHER_ADDR_LEN)
741 return (ERANGE);
742 for (i = 0; i < ETHER_ADDR_LEN; i++) {
743 val = strtoul(s + *off, &eptr, 16);
744 if (val > 0xff || eptr == s + *off)
745 return (EINVAL);
746 buf[i] = (u_char)val;
747 *off = (eptr - s);
748 if (i < ETHER_ADDR_LEN - 1) {
749 if (*eptr != ':')
750 return (EINVAL);
751 (*off)++;
752 }
753 }
754 *buflen = ETHER_ADDR_LEN;
755 return (0);
756}
757
758static int
759ng_enaddr_unparse(const struct ng_parse_type *type,
760 const u_char *data, int *off, char *cbuf, int cbuflen)
761{
762 int len;
763
764 len = snprintf(cbuf, cbuflen, "%02x:%02x:%02x:%02x:%02x:%02x",
765 data[*off], data[*off + 1], data[*off + 2],
766 data[*off + 3], data[*off + 4], data[*off + 5]);
767 if (len >= cbuflen)
768 return (ERANGE);
769 *off += ETHER_ADDR_LEN;
770 return (0);
771}
772
773/******************************************************************
774 INITIALIZATION
775******************************************************************/
776
777/*
778 * Handle loading and unloading for this node type.
779 */
780static int
781ng_ether_mod_event(module_t mod, int event, void *data)
782{
783 struct ifnet *ifp;
784 int error = 0;
785 int s;
786
787 s = splnet();
788 switch (event) {
789 case MOD_LOAD:
790
791 /* Register function hooks */
792 if (ng_ether_attach_p != NULL) {
793 error = EEXIST;
794 break;
795 }
796 ng_ether_attach_p = ng_ether_attach;
797 ng_ether_detach_p = ng_ether_detach;
798 ng_ether_output_p = ng_ether_output;
799 ng_ether_input_p = ng_ether_input;
800 ng_ether_input_orphan_p = ng_ether_input_orphan;
801
802 /* Create nodes for any already-existing Ethernet interfaces */
803 TAILQ_FOREACH(ifp, &ifnet, if_link) {
804 if (ifp->if_type == IFT_ETHER
805 || ifp->if_type == IFT_L2VLAN)
806 ng_ether_attach(ifp);
807 }
808 break;
809
810 case MOD_UNLOAD:
811
812 /*
813 * Note that the base code won't try to unload us until
814 * all nodes have been removed, and that can't happen
815 * until all Ethernet interfaces are removed. In any
816 * case, we know there are no nodes left if the action
817 * is MOD_UNLOAD, so there's no need to detach any nodes.
818 */
819
820 /* Unregister function hooks */
821 ng_ether_attach_p = NULL;
822 ng_ether_detach_p = NULL;
823 ng_ether_output_p = NULL;
824 ng_ether_input_p = NULL;
825 ng_ether_input_orphan_p = NULL;
826 break;
827
828 default:
829 error = EOPNOTSUPP;
830 break;
831 }
832 splx(s);
833 return (error);
834}
835