Deleted Added
sdiff udiff text old ( 62143 ) new ( 62678 )
full compact
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 62143 2000-06-26 23:34:54Z archie $
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 IFP2NG(ifp) ((struct ng_node *)((struct arpcom *)(ifp))->ac_netgraph)
68
69/* Per-node private data */
70struct private {
71 struct ifnet *ifp; /* associated interface */
72 hook_p upper; /* upper hook connection */
73 hook_p lower; /* lower OR orphan hook connection */
74 u_char lowerOrphan; /* whether lower is lower or orphan */
75};
76typedef struct private *priv_p;
77
78/* Functional hooks called from if_ethersubr.c */
79static void ng_ether_input(struct ifnet *ifp,
80 struct mbuf **mp, struct ether_header *eh);
81static void ng_ether_input_orphan(struct ifnet *ifp,
82 struct mbuf *m, struct ether_header *eh);
83static int ng_ether_output(struct ifnet *ifp, struct mbuf **mp);
84static void ng_ether_attach(struct ifnet *ifp);
85static void ng_ether_detach(struct ifnet *ifp);
86
87/* Other functions */
88static void ng_ether_input2(node_p node,
89 struct mbuf **mp, struct ether_header *eh);
90static int ng_ether_glueback_header(struct mbuf **mp,
91 struct ether_header *eh);
92static int ng_ether_rcv_lower(node_p node, struct mbuf *m, meta_p meta);
93static int ng_ether_rcv_upper(node_p node, struct mbuf *m, meta_p meta);
94
95/* Netgraph node methods */
96static ng_constructor_t ng_ether_constructor;
97static ng_rcvmsg_t ng_ether_rcvmsg;
98static ng_shutdown_t ng_ether_rmnode;
99static ng_newhook_t ng_ether_newhook;
100static ng_rcvdata_t ng_ether_rcvdata;
101static ng_disconnect_t ng_ether_disconnect;
102static int ng_ether_mod_event(module_t mod, int event, void *data);
103
104/* List of commands and how to convert arguments to/from ASCII */
105static const struct ng_cmdlist ng_ether_cmdlist[] = {
106 {
107 NGM_ETHER_COOKIE,
108 NGM_ETHER_GET_IFNAME,
109 "getifname",
110 NULL,
111 &ng_parse_string_type
112 },
113 {
114 NGM_ETHER_COOKIE,
115 NGM_ETHER_GET_IFINDEX,
116 "getifindex",
117 NULL,
118 &ng_parse_int32_type
119 },
120 { 0 }
121};
122
123static struct ng_type ng_ether_typestruct = {
124 NG_VERSION,
125 NG_ETHER_NODE_TYPE,
126 ng_ether_mod_event,
127 ng_ether_constructor,
128 ng_ether_rcvmsg,
129 ng_ether_rmnode,
130 ng_ether_newhook,
131 NULL,
132 NULL,
133 ng_ether_rcvdata,
134 ng_ether_rcvdata,
135 ng_ether_disconnect,
136 ng_ether_cmdlist,
137};
138NETGRAPH_INIT(ether, &ng_ether_typestruct);
139
140/******************************************************************
141 ETHERNET FUNCTION HOOKS
142******************************************************************/
143
144/*
145 * Handle a packet that has come in on an interface. We get to
146 * look at it here before any upper layer protocols do.
147 *
148 * NOTE: this function will get called at splimp()
149 */
150static void
151ng_ether_input(struct ifnet *ifp,
152 struct mbuf **mp, struct ether_header *eh)
153{
154 const node_p node = IFP2NG(ifp);
155 const priv_p priv = node->private;
156
157 /* If "lower" hook not connected, let packet continue */
158 if (priv->lower == NULL || priv->lowerOrphan)
159 return;
160 ng_ether_input2(node, mp, eh);
161}
162
163/*
164 * Handle a packet that has come in on an interface, and which
165 * does not match any of our known protocols (an ``orphan'').
166 *
167 * NOTE: this function will get called at splimp()
168 */
169static void
170ng_ether_input_orphan(struct ifnet *ifp,
171 struct mbuf *m, struct ether_header *eh)
172{
173 const node_p node = IFP2NG(ifp);
174 const priv_p priv = node->private;
175
176 /* If "orphan" hook not connected, let packet continue */
177 if (priv->lower == NULL || !priv->lowerOrphan) {
178 m_freem(m);
179 return;
180 }
181 ng_ether_input2(node, &m, eh);
182 if (m != NULL)
183 m_freem(m);
184}
185
186/*
187 * Handle a packet that has come in on an interface.
188 * The Ethernet header has already been detached from the mbuf,
189 * so we have to put it back.
190 *
191 * NOTE: this function will get called at splimp()
192 */
193static void
194ng_ether_input2(node_p node, struct mbuf **mp, struct ether_header *eh)
195{
196 const priv_p priv = node->private;
197 meta_p meta = NULL;
198 int error;
199
200 /* Glue Ethernet header back on */
201 if ((error = ng_ether_glueback_header(mp, eh)) != 0)
202 return;
203
204 /* Send out lower/orphan hook */
205 NG_SEND_DATAQ(error, priv->lower, *mp, meta);
206
207 /* Any reflected packet must come later due to queuing */
208 *mp = NULL;
209}
210
211/*
212 * Handle a packet that is going out on an interface.
213 * The Ethernet header is already attached to the mbuf.
214 */
215static int
216ng_ether_output(struct ifnet *ifp, struct mbuf **mp)
217{
218 const node_p node = IFP2NG(ifp);
219 const priv_p priv = node->private;
220 meta_p meta = NULL;
221 int error = 0;
222
223 /* If "upper" hook not connected, let packet continue */
224 if (priv->upper == NULL)
225 return (0);
226
227 /* Send it out "upper" hook */
228 NG_SEND_DATA_RET(error, priv->upper, *mp, meta);
229
230 /* If we got a reflected packet back, handle it */
231 if (error == 0 && *mp != NULL) {
232 error = ng_ether_rcv_upper(node, *mp, meta);
233 *mp = NULL;
234 }
235 return (error);
236}
237
238/*
239 * A new Ethernet interface has been attached.
240 * Create a new node for it, etc.
241 */
242static void
243ng_ether_attach(struct ifnet *ifp)
244{
245 char name[IFNAMSIZ + 1];
246 priv_p priv;
247 node_p node;
248
249 /* Create node */
250 KASSERT(!IFP2NG(ifp), ("%s: node already exists?", __FUNCTION__));
251 snprintf(name, sizeof(name), "%s%d", ifp->if_name, ifp->if_unit);
252 if (ng_make_node_common(&ng_ether_typestruct, &node) != 0) {
253 log(LOG_ERR, "%s: can't %s for %s\n",
254 __FUNCTION__, "create node", name);
255 return;
256 }
257
258 /* Allocate private data */
259 MALLOC(priv, priv_p, sizeof(*priv), M_NETGRAPH, M_NOWAIT);
260 if (priv == NULL) {
261 log(LOG_ERR, "%s: can't %s for %s\n",
262 __FUNCTION__, "allocate memory", name);
263 ng_unref(node);
264 return;
265 }
266 bzero(priv, sizeof(*priv));
267 node->private = priv;
268 priv->ifp = ifp;
269 IFP2NG(ifp) = node;
270
271 /* Try to give the node the same name as the interface */
272 if (ng_name_node(node, name) != 0) {
273 log(LOG_WARNING, "%s: can't name node %s\n",
274 __FUNCTION__, name);
275 }
276}
277
278/*
279 * An Ethernet interface is being detached.
280 * Destroy its node.
281 */
282static void
283ng_ether_detach(struct ifnet *ifp)
284{
285 const node_p node = IFP2NG(ifp);
286 priv_p priv;
287
288 if (node == NULL) /* no node (why not?), ignore */
289 return;
290 ng_rmnode(node); /* break all links to other nodes */
291 IFP2NG(ifp) = NULL; /* detach node from interface */
292 priv = node->private; /* free node private info */
293 bzero(priv, sizeof(*priv));
294 FREE(priv, M_NETGRAPH);
295 node->private = NULL;
296 ng_unref(node); /* free node itself */
297}
298
299/*
300 * Optimization for gluing the Ethernet header back onto
301 * the front of an incoming packet.
302 */
303static int
304ng_ether_glueback_header(struct mbuf **mp, struct ether_header *eh)
305{
306 struct mbuf *m = *mp;
307 uintfptr_t room;
308 int error = 0;
309
310 /*
311 * Possibly the header is already on the front.
312 * If this is the case so just move the markers back
313 * to re-include it. We lucked out.
314 * This allows us to avoid a yucky m_pullup
315 * in later nodes if it works.
316 */
317 if (eh == mtod(m, struct ether_header *) - 1) {
318 m->m_len += sizeof(*eh);
319 m->m_data -= sizeof(*eh);
320 m->m_pkthdr.len += sizeof(*eh);
321 goto done;
322 }
323
324 /*
325 * Alternatively there may be room even though
326 * it is stored somewhere else. If so, copy it in.
327 * This only safe because we KNOW that this packet has
328 * just been generated by an ethernet card, so there are
329 * no aliases to the buffer (not so for outgoing packets).
330 * Nearly all ethernet cards will end up producing mbufs
331 * that fall into these cases. So we are not optimizing
332 * contorted cases.
333 */
334 if ((m->m_flags & M_EXT) != 0) {
335 room = mtod(m, caddr_t) - m->m_ext.ext_buf;
336 if (room > m->m_ext.ext_size) /* garbage, fail immediately */
337 room = 0;
338 } else
339 room = mtod(m, caddr_t) - m->m_pktdat;
340
341 /*
342 * If we have room, just copy it and adjust
343 */
344 if (room >= sizeof(*eh)) {
345 m->m_len += sizeof(*eh);
346 m->m_data -= sizeof(*eh);
347 m->m_pkthdr.len += sizeof(*eh);
348 goto copy;
349 }
350
351 /*
352 * Doing anything more is likely to get more
353 * expensive than it's worth..
354 * it's probable that everything else is in one
355 * big lump. The next node will do an m_pullup()
356 * for exactly the amount of data it needs and
357 * hopefully everything after that will not
358 * need one. So let's just use M_PREPEND.
359 */
360 M_PREPEND(m, sizeof (*eh), M_DONTWAIT);
361 if (m == NULL) {
362 error = ENOBUFS;
363 goto done;
364 }
365
366copy:
367 /* Copy header and return (possibly new) mbuf */
368 bcopy((caddr_t)eh, mtod(m, struct ether_header *), sizeof(*eh));
369done:
370 *mp = m;
371 return error;
372}
373
374/******************************************************************
375 NETGRAPH NODE METHODS
376******************************************************************/
377
378/*
379 * It is not possible or allowable to create a node of this type.
380 * Nodes get created when the interface is attached (or, when
381 * this node type's KLD is loaded).
382 */
383static int
384ng_ether_constructor(node_p *nodep)
385{
386 return (EINVAL);
387}
388
389/*
390 * Check for attaching a new hook.
391 */
392static int
393ng_ether_newhook(node_p node, hook_p hook, const char *name)
394{
395 const priv_p priv = node->private;
396 u_char orphan = priv->lowerOrphan;
397 hook_p *hookptr;
398
399 /* Divert hook is an alias for lower */
400 if (strcmp(name, NG_ETHER_HOOK_DIVERT) == 0)
401 name = NG_ETHER_HOOK_LOWER;
402
403 /* Which hook? */
404 if (strcmp(name, NG_ETHER_HOOK_UPPER) == 0)
405 hookptr = &priv->upper;
406 else if (strcmp(name, NG_ETHER_HOOK_LOWER) == 0) {
407 hookptr = &priv->lower;
408 orphan = 0;
409 } else if (strcmp(name, NG_ETHER_HOOK_ORPHAN) == 0) {
410 hookptr = &priv->lower;
411 orphan = 1;
412 } else
413 return (EINVAL);
414
415 /* Check if already connected (shouldn't be, but doesn't hurt) */
416 if (*hookptr != NULL)
417 return (EISCONN);
418
419 /* OK */
420 *hookptr = hook;
421 priv->lowerOrphan = orphan;
422 return (0);
423}
424
425/*
426 * Receive an incoming control message.
427 */
428static int
429ng_ether_rcvmsg(node_p node, struct ng_mesg *msg, const char *retaddr,
430 struct ng_mesg **rptr, hook_p lasthook)
431{
432 const priv_p priv = node->private;
433 struct ng_mesg *resp = NULL;
434 int error = 0;
435
436 switch (msg->header.typecookie) {
437 case NGM_ETHER_COOKIE:
438 switch (msg->header.cmd) {
439 case NGM_ETHER_GET_IFNAME:
440 NG_MKRESPONSE(resp, msg, IFNAMSIZ + 1, M_NOWAIT);
441 if (resp == NULL) {
442 error = ENOMEM;
443 break;
444 }
445 snprintf(resp->data, IFNAMSIZ + 1,
446 "%s%d", priv->ifp->if_name, priv->ifp->if_unit);
447 break;
448 case NGM_ETHER_GET_IFINDEX:
449 NG_MKRESPONSE(resp, msg, sizeof(u_int32_t), M_NOWAIT);
450 if (resp == NULL) {
451 error = ENOMEM;
452 break;
453 }
454 *((u_int32_t *)resp->data) = priv->ifp->if_index;
455 break;
456 default:
457 error = EINVAL;
458 break;
459 }
460 break;
461 default:
462 error = EINVAL;
463 break;
464 }
465 if (rptr)
466 *rptr = resp;
467 else if (resp != NULL)
468 FREE(resp, M_NETGRAPH);
469 FREE(msg, M_NETGRAPH);
470 return (error);
471}
472
473/*
474 * Receive data on a hook.
475 */
476static int
477ng_ether_rcvdata(hook_p hook, struct mbuf *m, meta_p meta,
478 struct mbuf **ret_m, meta_p *ret_meta)
479{
480 const node_p node = hook->node;
481 const priv_p priv = node->private;
482
483 if (hook == priv->lower)
484 return ng_ether_rcv_lower(node, m, meta);
485 if (hook == priv->upper)
486 return ng_ether_rcv_upper(node, m, meta);
487 panic("%s: weird hook", __FUNCTION__);
488}
489
490/*
491 * Handle an mbuf received on the "lower" hook.
492 */
493static int
494ng_ether_rcv_lower(node_p node, struct mbuf *m, meta_p meta)
495{
496 const priv_p priv = node->private;
497
498 /* Make sure header is fully pulled up */
499 if (m->m_pkthdr.len < sizeof(struct ether_header)) {
500 NG_FREE_DATA(m, meta);
501 return (EINVAL);
502 }
503 if (m->m_len < sizeof(struct ether_header)
504 && (m = m_pullup(m, sizeof(struct ether_header))) == NULL) {
505 NG_FREE_META(meta);
506 return (ENOBUFS);
507 }
508
509 /* Send it on its way */
510 NG_FREE_META(meta);
511 return ether_output_frame(priv->ifp, m);
512}
513
514/*
515 * Handle an mbuf received on the "upper" hook.
516 */
517static int
518ng_ether_rcv_upper(node_p node, struct mbuf *m, meta_p meta)
519{
520 const priv_p priv = node->private;
521 struct ether_header *eh;
522
523 /* Check length and pull off header */
524 if (m->m_pkthdr.len < sizeof(*eh)) {
525 NG_FREE_DATA(m, meta);
526 return (EINVAL);
527 }
528 if (m->m_len < sizeof(*eh) && (m = m_pullup(m, sizeof(*eh))) == NULL) {
529 NG_FREE_META(meta);
530 return (ENOBUFS);
531 }
532 eh = mtod(m, struct ether_header *);
533 m->m_data += sizeof(*eh);
534 m->m_len -= sizeof(*eh);
535 m->m_pkthdr.len -= sizeof(*eh);
536
537 /* Route packet back in */
538 NG_FREE_META(meta);
539 ether_demux(priv->ifp, eh, m);
540 return (0);
541}
542
543/*
544 * Shutdown node. This resets the node but does not remove it.
545 */
546static int
547ng_ether_rmnode(node_p node)
548{
549 ng_cutlinks(node);
550 node->flags &= ~NG_INVALID; /* bounce back to life */
551 return (0);
552}
553
554/*
555 * Hook disconnection.
556 */
557static int
558ng_ether_disconnect(hook_p hook)
559{
560 const priv_p priv = hook->node->private;
561
562 if (hook == priv->upper)
563 priv->upper = NULL;
564 else if (hook == priv->lower) {
565 priv->lower = NULL;
566 priv->lowerOrphan = 0;
567 } else
568 panic("%s: weird hook", __FUNCTION__);
569 return (0);
570}
571
572/******************************************************************
573 INITIALIZATION
574******************************************************************/
575
576/*
577 * Handle loading and unloading for this node type.
578 */
579static int
580ng_ether_mod_event(module_t mod, int event, void *data)
581{
582 struct ifnet *ifp;
583 int error = 0;
584 int s;
585
586 s = splnet();
587 switch (event) {
588 case MOD_LOAD:
589
590 /* Register function hooks */
591 if (ng_ether_attach_p != NULL) {
592 error = EEXIST;
593 break;
594 }
595 ng_ether_attach_p = ng_ether_attach;
596 ng_ether_detach_p = ng_ether_detach;
597 ng_ether_output_p = ng_ether_output;
598 ng_ether_input_p = ng_ether_input;
599 ng_ether_input_orphan_p = ng_ether_input_orphan;
600
601 /* Create nodes for any already-existing Ethernet interfaces */
602 TAILQ_FOREACH(ifp, &ifnet, if_link) {
603 if (ifp->if_type == IFT_ETHER)
604 ng_ether_attach(ifp);
605 }
606 break;
607
608 case MOD_UNLOAD:
609
610 /*
611 * Note that the base code won't try to unload us until
612 * all nodes have been removed, and that can't happen
613 * until all Ethernet interfaces are removed. In any
614 * case, we know there are no nodes left if the action
615 * is MOD_UNLOAD, so there's no need to detach any nodes.
616 */
617
618 /* Unregister function hooks */
619 ng_ether_attach_p = NULL;
620 ng_ether_detach_p = NULL;
621 ng_ether_output_p = NULL;
622 ng_ether_input_p = NULL;
623 ng_ether_input_orphan_p = NULL;
624 break;
625
626 default:
627 error = EOPNOTSUPP;
628 break;
629 }
630 splx(s);
631 return (error);
632}
633