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