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