ng_ether.c revision 63195
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 63195 2000-07-14 22:35:13Z 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 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};
77typedef struct private *priv_p;
78
79/* Functional hooks called from if_ethersubr.c */
80static void	ng_ether_input(struct ifnet *ifp,
81		    struct mbuf **mp, struct ether_header *eh);
82static void	ng_ether_input_orphan(struct ifnet *ifp,
83		    struct mbuf *m, struct ether_header *eh);
84static int	ng_ether_output(struct ifnet *ifp, struct mbuf **mp);
85static void	ng_ether_attach(struct ifnet *ifp);
86static void	ng_ether_detach(struct ifnet *ifp);
87
88/* Other functions */
89static void	ng_ether_input2(node_p node,
90		    struct mbuf **mp, struct ether_header *eh);
91static int	ng_ether_glueback_header(struct mbuf **mp,
92			struct ether_header *eh);
93static int	ng_ether_rcv_lower(node_p node, struct mbuf *m, meta_p meta);
94static int	ng_ether_rcv_upper(node_p node, struct mbuf *m, meta_p meta);
95
96/* Netgraph node methods */
97static ng_constructor_t	ng_ether_constructor;
98static ng_rcvmsg_t	ng_ether_rcvmsg;
99static ng_shutdown_t	ng_ether_rmnode;
100static ng_newhook_t	ng_ether_newhook;
101static ng_rcvdata_t	ng_ether_rcvdata;
102static ng_disconnect_t	ng_ether_disconnect;
103static int		ng_ether_mod_event(module_t mod, int event, void *data);
104
105/* List of commands and how to convert arguments to/from ASCII */
106static const struct ng_cmdlist ng_ether_cmdlist[] = {
107	{
108	  NGM_ETHER_COOKIE,
109	  NGM_ETHER_GET_IFNAME,
110	  "getifname",
111	  NULL,
112	  &ng_parse_string_type
113	},
114	{
115	  NGM_ETHER_COOKIE,
116	  NGM_ETHER_GET_IFINDEX,
117	  "getifindex",
118	  NULL,
119	  &ng_parse_int32_type
120	},
121	{ 0 }
122};
123
124static struct ng_type ng_ether_typestruct = {
125	NG_VERSION,
126	NG_ETHER_NODE_TYPE,
127	ng_ether_mod_event,
128	ng_ether_constructor,
129	ng_ether_rcvmsg,
130	ng_ether_rmnode,
131	ng_ether_newhook,
132	NULL,
133	NULL,
134	ng_ether_rcvdata,
135	ng_ether_rcvdata,
136	ng_ether_disconnect,
137	ng_ether_cmdlist,
138};
139NETGRAPH_INIT(ether, &ng_ether_typestruct);
140
141/******************************************************************
142		    ETHERNET FUNCTION HOOKS
143******************************************************************/
144
145/*
146 * Handle a packet that has come in on an interface. We get to
147 * look at it here before any upper layer protocols do.
148 *
149 * NOTE: this function will get called at splimp()
150 */
151static void
152ng_ether_input(struct ifnet *ifp,
153	struct mbuf **mp, struct ether_header *eh)
154{
155	const node_p node = IFP2NG(ifp);
156	const priv_p priv = node->private;
157
158	/* If "lower" hook not connected, let packet continue */
159	if (priv->lower == NULL || priv->lowerOrphan)
160		return;
161	ng_ether_input2(node, mp, eh);
162}
163
164/*
165 * Handle a packet that has come in on an interface, and which
166 * does not match any of our known protocols (an ``orphan'').
167 *
168 * NOTE: this function will get called at splimp()
169 */
170static void
171ng_ether_input_orphan(struct ifnet *ifp,
172	struct mbuf *m, struct ether_header *eh)
173{
174	const node_p node = IFP2NG(ifp);
175	const priv_p priv = node->private;
176
177	/* If "orphan" hook not connected, let packet continue */
178	if (priv->lower == NULL || !priv->lowerOrphan) {
179		m_freem(m);
180		return;
181	}
182	ng_ether_input2(node, &m, eh);
183	if (m != NULL)
184		m_freem(m);
185}
186
187/*
188 * Handle a packet that has come in on an interface.
189 * The Ethernet header has already been detached from the mbuf,
190 * so we have to put it back.
191 *
192 * NOTE: this function will get called at splimp()
193 */
194static void
195ng_ether_input2(node_p node, struct mbuf **mp, struct ether_header *eh)
196{
197	const priv_p priv = node->private;
198	meta_p meta = NULL;
199	int error;
200
201	/* Glue Ethernet header back on */
202	if ((error = ng_ether_glueback_header(mp, eh)) != 0)
203		return;
204
205	/* Send out lower/orphan hook */
206	(void)ng_queue_data(priv->lower, *mp, meta);
207	*mp = NULL;
208}
209
210/*
211 * Handle a packet that is going out on an interface.
212 * The Ethernet header is already attached to the mbuf.
213 */
214static int
215ng_ether_output(struct ifnet *ifp, struct mbuf **mp)
216{
217	const node_p node = IFP2NG(ifp);
218	const priv_p priv = node->private;
219	meta_p meta = NULL;
220	int error = 0;
221
222	/* If "upper" hook not connected, let packet continue */
223	if (priv->upper == NULL)
224		return (0);
225
226	/* Send it out "upper" hook */
227	NG_SEND_DATA_RET(error, priv->upper, *mp, meta);
228
229	/* If we got a reflected packet back, handle it */
230	if (error == 0 && *mp != NULL) {
231		error = ng_ether_rcv_upper(node, *mp, meta);
232		*mp = NULL;
233	}
234	return (error);
235}
236
237/*
238 * A new Ethernet interface has been attached.
239 * Create a new node for it, etc.
240 */
241static void
242ng_ether_attach(struct ifnet *ifp)
243{
244	char name[IFNAMSIZ + 1];
245	priv_p priv;
246	node_p node;
247
248	/* Create node */
249	KASSERT(!IFP2NG(ifp), ("%s: node already exists?", __FUNCTION__));
250	snprintf(name, sizeof(name), "%s%d", ifp->if_name, ifp->if_unit);
251	if (ng_make_node_common(&ng_ether_typestruct, &node) != 0) {
252		log(LOG_ERR, "%s: can't %s for %s\n",
253		    __FUNCTION__, "create node", name);
254		return;
255	}
256
257	/* Allocate private data */
258	MALLOC(priv, priv_p, sizeof(*priv), M_NETGRAPH, M_NOWAIT);
259	if (priv == NULL) {
260		log(LOG_ERR, "%s: can't %s for %s\n",
261		    __FUNCTION__, "allocate memory", name);
262		ng_unref(node);
263		return;
264	}
265	bzero(priv, sizeof(*priv));
266	node->private = priv;
267	priv->ifp = ifp;
268	IFP2NG(ifp) = node;
269
270	/* Try to give the node the same name as the interface */
271	if (ng_name_node(node, name) != 0) {
272		log(LOG_WARNING, "%s: can't name node %s\n",
273		    __FUNCTION__, name);
274	}
275}
276
277/*
278 * An Ethernet interface is being detached.
279 * Destroy its node.
280 */
281static void
282ng_ether_detach(struct ifnet *ifp)
283{
284	const node_p node = IFP2NG(ifp);
285	priv_p priv;
286
287	if (node == NULL)		/* no node (why not?), ignore */
288		return;
289	ng_rmnode(node);		/* break all links to other nodes */
290	node->flags |= NG_INVALID;
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	struct ether_header *eh;
498
499	/* Make sure header is fully pulled up */
500	if (m->m_pkthdr.len < sizeof(struct ether_header)) {
501		NG_FREE_DATA(m, meta);
502		return (EINVAL);
503	}
504	if (m->m_len < sizeof(struct ether_header)
505	    && (m = m_pullup(m, sizeof(struct ether_header))) == NULL) {
506		NG_FREE_META(meta);
507		return (ENOBUFS);
508	}
509
510        /* drop in the MAC address */
511	eh = mtod(m, struct ether_header *);
512	bcopy((IFP2AC(priv->ifp))->ac_enaddr, eh->ether_shost, 6);
513
514	/* Send it on its way */
515	NG_FREE_META(meta);
516	return ether_output_frame(priv->ifp, m);
517}
518
519/*
520 * Handle an mbuf received on the "upper" hook.
521 */
522static int
523ng_ether_rcv_upper(node_p node, struct mbuf *m, meta_p meta)
524{
525	const priv_p priv = node->private;
526	struct ether_header *eh;
527
528	/* Check length and pull off header */
529	if (m->m_pkthdr.len < sizeof(*eh)) {
530		NG_FREE_DATA(m, meta);
531		return (EINVAL);
532	}
533	if (m->m_len < sizeof(*eh) && (m = m_pullup(m, sizeof(*eh))) == NULL) {
534		NG_FREE_META(meta);
535		return (ENOBUFS);
536	}
537	eh = mtod(m, struct ether_header *);
538	m->m_data += sizeof(*eh);
539	m->m_len -= sizeof(*eh);
540	m->m_pkthdr.len -= sizeof(*eh);
541
542	/* Route packet back in */
543	NG_FREE_META(meta);
544	ether_demux(priv->ifp, eh, m);
545	return (0);
546}
547
548/*
549 * Shutdown node. This resets the node but does not remove it.
550 */
551static int
552ng_ether_rmnode(node_p node)
553{
554	ng_cutlinks(node);
555	node->flags &= ~NG_INVALID;	/* bounce back to life */
556	return (0);
557}
558
559/*
560 * Hook disconnection.
561 */
562static int
563ng_ether_disconnect(hook_p hook)
564{
565	const priv_p priv = hook->node->private;
566
567	if (hook == priv->upper)
568		priv->upper = NULL;
569	else if (hook == priv->lower) {
570		priv->lower = NULL;
571		priv->lowerOrphan = 0;
572	} else
573		panic("%s: weird hook", __FUNCTION__);
574	return (0);
575}
576
577/******************************************************************
578		    	INITIALIZATION
579******************************************************************/
580
581/*
582 * Handle loading and unloading for this node type.
583 */
584static int
585ng_ether_mod_event(module_t mod, int event, void *data)
586{
587	struct ifnet *ifp;
588	int error = 0;
589	int s;
590
591	s = splnet();
592	switch (event) {
593	case MOD_LOAD:
594
595		/* Register function hooks */
596		if (ng_ether_attach_p != NULL) {
597			error = EEXIST;
598			break;
599		}
600		ng_ether_attach_p = ng_ether_attach;
601		ng_ether_detach_p = ng_ether_detach;
602		ng_ether_output_p = ng_ether_output;
603		ng_ether_input_p = ng_ether_input;
604		ng_ether_input_orphan_p = ng_ether_input_orphan;
605
606		/* Create nodes for any already-existing Ethernet interfaces */
607		TAILQ_FOREACH(ifp, &ifnet, if_link) {
608			if (ifp->if_type == IFT_ETHER)
609				ng_ether_attach(ifp);
610		}
611		break;
612
613	case MOD_UNLOAD:
614
615		/*
616		 * Note that the base code won't try to unload us until
617		 * all nodes have been removed, and that can't happen
618		 * until all Ethernet interfaces are removed. In any
619		 * case, we know there are no nodes left if the action
620		 * is MOD_UNLOAD, so there's no need to detach any nodes.
621		 */
622
623		/* Unregister function hooks */
624		ng_ether_attach_p = NULL;
625		ng_ether_detach_p = NULL;
626		ng_ether_output_p = NULL;
627		ng_ether_input_p = NULL;
628		ng_ether_input_orphan_p = NULL;
629		break;
630
631	default:
632		error = EOPNOTSUPP;
633		break;
634	}
635	splx(s);
636	return (error);
637}
638
639