1e1e1452dSArchie Cobbs
2e1e1452dSArchie Cobbs /*
3e1e1452dSArchie Cobbs * ng_ether.c
4c398230bSWarner Losh */
5c398230bSWarner Losh
6c398230bSWarner Losh /*-
7e1e1452dSArchie Cobbs * Copyright (c) 1996-2000 Whistle Communications, Inc.
8e1e1452dSArchie Cobbs * All rights reserved.
9e1e1452dSArchie Cobbs *
10e1e1452dSArchie Cobbs * Subject to the following obligations and disclaimer of warranty, use and
11e1e1452dSArchie Cobbs * redistribution of this software, in source or object code forms, with or
12e1e1452dSArchie Cobbs * without modifications are expressly permitted by Whistle Communications;
13e1e1452dSArchie Cobbs * provided, however, that:
14e1e1452dSArchie Cobbs * 1. Any and all reproductions of the source or object code must include the
15e1e1452dSArchie Cobbs * copyright notice above and the following disclaimer of warranties; and
16e1e1452dSArchie Cobbs * 2. No rights are granted, in any manner or form, to use Whistle
17e1e1452dSArchie Cobbs * Communications, Inc. trademarks, including the mark "WHISTLE
18e1e1452dSArchie Cobbs * COMMUNICATIONS" on advertising, endorsements, or otherwise except as
19e1e1452dSArchie Cobbs * such appears in the above copyright notice or in the software.
20e1e1452dSArchie Cobbs *
21e1e1452dSArchie Cobbs * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
22e1e1452dSArchie Cobbs * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
23e1e1452dSArchie Cobbs * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
24e1e1452dSArchie Cobbs * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
25e1e1452dSArchie Cobbs * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
26e1e1452dSArchie Cobbs * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
27e1e1452dSArchie Cobbs * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
28e1e1452dSArchie Cobbs * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
29e1e1452dSArchie Cobbs * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
30e1e1452dSArchie Cobbs * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
31e1e1452dSArchie Cobbs * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
32e1e1452dSArchie Cobbs * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
33e1e1452dSArchie Cobbs * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
34e1e1452dSArchie Cobbs * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35e1e1452dSArchie Cobbs * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
36e1e1452dSArchie Cobbs * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
37e1e1452dSArchie Cobbs * OF SUCH DAMAGE.
38e1e1452dSArchie Cobbs *
39e1e1452dSArchie Cobbs * Authors: Archie Cobbs <archie@freebsd.org>
40e1e1452dSArchie Cobbs * Julian Elischer <julian@freebsd.org>
41e1e1452dSArchie Cobbs */
42e1e1452dSArchie Cobbs
43e1e1452dSArchie Cobbs /*
44e1e1452dSArchie Cobbs * ng_ether(4) netgraph node type
45e1e1452dSArchie Cobbs */
46e1e1452dSArchie Cobbs
47e1e1452dSArchie Cobbs #include <sys/param.h>
48c3322cb9SGleb Smirnoff #include <sys/eventhandler.h>
49e1e1452dSArchie Cobbs #include <sys/systm.h>
50e1e1452dSArchie Cobbs #include <sys/kernel.h>
51e1e1452dSArchie Cobbs #include <sys/malloc.h>
52e1e1452dSArchie Cobbs #include <sys/mbuf.h>
53e1e1452dSArchie Cobbs #include <sys/errno.h>
54530c0060SRobert Watson #include <sys/proc.h>
55e1e1452dSArchie Cobbs #include <sys/syslog.h>
56e1e1452dSArchie Cobbs #include <sys/socket.h>
572cdf8c49SMarko Zec #include <sys/taskqueue.h>
58e1e1452dSArchie Cobbs
59e1e1452dSArchie Cobbs #include <net/if.h>
60810d5e89SGleb Smirnoff #include <net/if_dl.h>
61e1e1452dSArchie Cobbs #include <net/if_types.h>
62e1e1452dSArchie Cobbs #include <net/if_arp.h>
63e1e1452dSArchie Cobbs #include <net/if_var.h>
64*3d0d5b21SJustin Hibbits #include <net/if_private.h>
65e1e1452dSArchie Cobbs #include <net/ethernet.h>
66fd6238a6SAndrew Thompson #include <net/if_bridgevar.h>
674b79449eSBjoern A. Zeeb #include <net/vnet.h>
68e1e1452dSArchie Cobbs
69e1e1452dSArchie Cobbs #include <netgraph/ng_message.h>
70e1e1452dSArchie Cobbs #include <netgraph/netgraph.h>
71e1e1452dSArchie Cobbs #include <netgraph/ng_parse.h>
72e1e1452dSArchie Cobbs #include <netgraph/ng_ether.h>
73e1e1452dSArchie Cobbs
7439b553ceSEd Maste MODULE_VERSION(ng_ether, 1);
7539b553ceSEd Maste
76833e8dc5SGleb Smirnoff #define IFP2NG(ifp) ((ifp)->if_l2com)
77e1e1452dSArchie Cobbs
783c976c3fSPawel Jakub Dawidek /* Per-node private data */
793c976c3fSPawel Jakub Dawidek struct private {
803c976c3fSPawel Jakub Dawidek struct ifnet *ifp; /* associated interface */
813c976c3fSPawel Jakub Dawidek hook_p upper; /* upper hook connection */
821a292b80SArchie Cobbs hook_p lower; /* lower hook connection */
831a292b80SArchie Cobbs hook_p orphan; /* orphan hook connection */
843c976c3fSPawel Jakub Dawidek u_char autoSrcAddr; /* always overwrite source address */
853c976c3fSPawel Jakub Dawidek u_char promisc; /* promiscuous mode enabled */
863c976c3fSPawel Jakub Dawidek u_long hwassist; /* hardware checksum capabilities */
873c976c3fSPawel Jakub Dawidek u_int flags; /* flags e.g. really die */
883c976c3fSPawel Jakub Dawidek };
893c976c3fSPawel Jakub Dawidek typedef struct private *priv_p;
90e1e1452dSArchie Cobbs
91edbb5246SSam Leffler /* Hook pointers used by if_ethersubr.c to callback to netgraph */
92edbb5246SSam Leffler extern void (*ng_ether_input_p)(struct ifnet *ifp, struct mbuf **mp);
93edbb5246SSam Leffler extern void (*ng_ether_input_orphan_p)(struct ifnet *ifp, struct mbuf *m);
94edbb5246SSam Leffler extern int (*ng_ether_output_p)(struct ifnet *ifp, struct mbuf **mp);
95edbb5246SSam Leffler extern void (*ng_ether_attach_p)(struct ifnet *ifp);
96edbb5246SSam Leffler extern void (*ng_ether_detach_p)(struct ifnet *ifp);
971c7899c7SGleb Smirnoff extern void (*ng_ether_link_state_p)(struct ifnet *ifp, int state);
98edbb5246SSam Leffler
99e1e1452dSArchie Cobbs /* Functional hooks called from if_ethersubr.c */
100edbb5246SSam Leffler static void ng_ether_input(struct ifnet *ifp, struct mbuf **mp);
101edbb5246SSam Leffler static void ng_ether_input_orphan(struct ifnet *ifp, struct mbuf *m);
102e1e1452dSArchie Cobbs static int ng_ether_output(struct ifnet *ifp, struct mbuf **mp);
103e1e1452dSArchie Cobbs static void ng_ether_attach(struct ifnet *ifp);
104e1e1452dSArchie Cobbs static void ng_ether_detach(struct ifnet *ifp);
1051c7899c7SGleb Smirnoff static void ng_ether_link_state(struct ifnet *ifp, int state);
106e1e1452dSArchie Cobbs
107e1e1452dSArchie Cobbs /* Other functions */
108f664dcdeSJulian Elischer static int ng_ether_rcv_lower(hook_p node, item_p item);
109f664dcdeSJulian Elischer static int ng_ether_rcv_upper(hook_p node, item_p item);
110e1e1452dSArchie Cobbs
111e1e1452dSArchie Cobbs /* Netgraph node methods */
112e1e1452dSArchie Cobbs static ng_constructor_t ng_ether_constructor;
113e1e1452dSArchie Cobbs static ng_rcvmsg_t ng_ether_rcvmsg;
114069154d5SJulian Elischer static ng_shutdown_t ng_ether_shutdown;
115e1e1452dSArchie Cobbs static ng_newhook_t ng_ether_newhook;
116e1e1452dSArchie Cobbs static ng_rcvdata_t ng_ether_rcvdata;
117e1e1452dSArchie Cobbs static ng_disconnect_t ng_ether_disconnect;
118e1e1452dSArchie Cobbs static int ng_ether_mod_event(module_t mod, int event, void *data);
119e1e1452dSArchie Cobbs
120499f60b1SAndriy Gapon static eventhandler_tag ng_ether_ifnet_arrival_cookie;
121499f60b1SAndriy Gapon
122e1e1452dSArchie Cobbs /* List of commands and how to convert arguments to/from ASCII */
123e1e1452dSArchie Cobbs static const struct ng_cmdlist ng_ether_cmdlist[] = {
124e1e1452dSArchie Cobbs {
125e1e1452dSArchie Cobbs NGM_ETHER_COOKIE,
126e1e1452dSArchie Cobbs NGM_ETHER_GET_IFNAME,
127e1e1452dSArchie Cobbs "getifname",
128e1e1452dSArchie Cobbs NULL,
129e1e1452dSArchie Cobbs &ng_parse_string_type
130e1e1452dSArchie Cobbs },
131e1e1452dSArchie Cobbs {
132e1e1452dSArchie Cobbs NGM_ETHER_COOKIE,
133e1e1452dSArchie Cobbs NGM_ETHER_GET_IFINDEX,
134e1e1452dSArchie Cobbs "getifindex",
135e1e1452dSArchie Cobbs NULL,
136e1e1452dSArchie Cobbs &ng_parse_int32_type
137e1e1452dSArchie Cobbs },
1384b39c3c7SArchie Cobbs {
1394b39c3c7SArchie Cobbs NGM_ETHER_COOKIE,
1404b39c3c7SArchie Cobbs NGM_ETHER_GET_ENADDR,
1414b39c3c7SArchie Cobbs "getenaddr",
1424b39c3c7SArchie Cobbs NULL,
1438c7e4101SRuslan Ermilov &ng_parse_enaddr_type
1444b39c3c7SArchie Cobbs },
1454b39c3c7SArchie Cobbs {
1464b39c3c7SArchie Cobbs NGM_ETHER_COOKIE,
14756045c66SArchie Cobbs NGM_ETHER_SET_ENADDR,
14856045c66SArchie Cobbs "setenaddr",
1498c7e4101SRuslan Ermilov &ng_parse_enaddr_type,
15056045c66SArchie Cobbs NULL
15156045c66SArchie Cobbs },
15256045c66SArchie Cobbs {
15356045c66SArchie Cobbs NGM_ETHER_COOKIE,
15456045c66SArchie Cobbs NGM_ETHER_GET_PROMISC,
15556045c66SArchie Cobbs "getpromisc",
15656045c66SArchie Cobbs NULL,
15756045c66SArchie Cobbs &ng_parse_int32_type
15856045c66SArchie Cobbs },
15956045c66SArchie Cobbs {
16056045c66SArchie Cobbs NGM_ETHER_COOKIE,
1614b39c3c7SArchie Cobbs NGM_ETHER_SET_PROMISC,
1624b39c3c7SArchie Cobbs "setpromisc",
1634b39c3c7SArchie Cobbs &ng_parse_int32_type,
1644b39c3c7SArchie Cobbs NULL
1654b39c3c7SArchie Cobbs },
1664b39c3c7SArchie Cobbs {
1674b39c3c7SArchie Cobbs NGM_ETHER_COOKIE,
16856045c66SArchie Cobbs NGM_ETHER_GET_AUTOSRC,
16956045c66SArchie Cobbs "getautosrc",
17056045c66SArchie Cobbs NULL,
17156045c66SArchie Cobbs &ng_parse_int32_type
17256045c66SArchie Cobbs },
17356045c66SArchie Cobbs {
17456045c66SArchie Cobbs NGM_ETHER_COOKIE,
1754b39c3c7SArchie Cobbs NGM_ETHER_SET_AUTOSRC,
1764b39c3c7SArchie Cobbs "setautosrc",
1774b39c3c7SArchie Cobbs &ng_parse_int32_type,
1784b39c3c7SArchie Cobbs NULL
1794b39c3c7SArchie Cobbs },
180810d5e89SGleb Smirnoff {
181810d5e89SGleb Smirnoff NGM_ETHER_COOKIE,
182810d5e89SGleb Smirnoff NGM_ETHER_ADD_MULTI,
183810d5e89SGleb Smirnoff "addmulti",
184810d5e89SGleb Smirnoff &ng_parse_enaddr_type,
185810d5e89SGleb Smirnoff NULL
186810d5e89SGleb Smirnoff },
187810d5e89SGleb Smirnoff {
188810d5e89SGleb Smirnoff NGM_ETHER_COOKIE,
189810d5e89SGleb Smirnoff NGM_ETHER_DEL_MULTI,
190810d5e89SGleb Smirnoff "delmulti",
191810d5e89SGleb Smirnoff &ng_parse_enaddr_type,
192810d5e89SGleb Smirnoff NULL
193810d5e89SGleb Smirnoff },
194cefddd66SGleb Smirnoff {
195cefddd66SGleb Smirnoff NGM_ETHER_COOKIE,
196cefddd66SGleb Smirnoff NGM_ETHER_DETACH,
197cefddd66SGleb Smirnoff "detach",
198cefddd66SGleb Smirnoff NULL,
199cefddd66SGleb Smirnoff NULL
200cefddd66SGleb Smirnoff },
201e1e1452dSArchie Cobbs { 0 }
202e1e1452dSArchie Cobbs };
203e1e1452dSArchie Cobbs
204e1e1452dSArchie Cobbs static struct ng_type ng_ether_typestruct = {
205f8aae777SJulian Elischer .version = NG_ABI_VERSION,
206f8aae777SJulian Elischer .name = NG_ETHER_NODE_TYPE,
207f8aae777SJulian Elischer .mod_event = ng_ether_mod_event,
208f8aae777SJulian Elischer .constructor = ng_ether_constructor,
209f8aae777SJulian Elischer .rcvmsg = ng_ether_rcvmsg,
210f8aae777SJulian Elischer .shutdown = ng_ether_shutdown,
211f8aae777SJulian Elischer .newhook = ng_ether_newhook,
212f8aae777SJulian Elischer .rcvdata = ng_ether_rcvdata,
213f8aae777SJulian Elischer .disconnect = ng_ether_disconnect,
214f8aae777SJulian Elischer .cmdlist = ng_ether_cmdlist,
215e1e1452dSArchie Cobbs };
216e1e1452dSArchie Cobbs NETGRAPH_INIT(ether, &ng_ether_typestruct);
217e1e1452dSArchie Cobbs
218e1e1452dSArchie Cobbs /******************************************************************
219499f60b1SAndriy Gapon UTILITY FUNCTIONS
220499f60b1SAndriy Gapon ******************************************************************/
221499f60b1SAndriy Gapon static void
ng_ether_sanitize_ifname(const char * ifname,char * name)222499f60b1SAndriy Gapon ng_ether_sanitize_ifname(const char *ifname, char *name)
223499f60b1SAndriy Gapon {
224499f60b1SAndriy Gapon int i;
225499f60b1SAndriy Gapon
226499f60b1SAndriy Gapon for (i = 0; i < IFNAMSIZ; i++) {
227499f60b1SAndriy Gapon if (ifname[i] == '.' || ifname[i] == ':')
228499f60b1SAndriy Gapon name[i] = '_';
229499f60b1SAndriy Gapon else
230499f60b1SAndriy Gapon name[i] = ifname[i];
231499f60b1SAndriy Gapon if (name[i] == '\0')
232499f60b1SAndriy Gapon break;
233499f60b1SAndriy Gapon }
234499f60b1SAndriy Gapon }
235499f60b1SAndriy Gapon
236499f60b1SAndriy Gapon /******************************************************************
237e1e1452dSArchie Cobbs ETHERNET FUNCTION HOOKS
238e1e1452dSArchie Cobbs ******************************************************************/
239e1e1452dSArchie Cobbs
240e1e1452dSArchie Cobbs /*
241e1e1452dSArchie Cobbs * Handle a packet that has come in on an interface. We get to
242e1e1452dSArchie Cobbs * look at it here before any upper layer protocols do.
243e1e1452dSArchie Cobbs */
244e1e1452dSArchie Cobbs static void
ng_ether_input(struct ifnet * ifp,struct mbuf ** mp)245edbb5246SSam Leffler ng_ether_input(struct ifnet *ifp, struct mbuf **mp)
246e1e1452dSArchie Cobbs {
247e1e1452dSArchie Cobbs const node_p node = IFP2NG(ifp);
24830400f03SJulian Elischer const priv_p priv = NG_NODE_PRIVATE(node);
2491a292b80SArchie Cobbs int error;
250e1e1452dSArchie Cobbs
251e1e1452dSArchie Cobbs /* If "lower" hook not connected, let packet continue */
2521a292b80SArchie Cobbs if (priv->lower == NULL)
253e1e1452dSArchie Cobbs return;
2541a292b80SArchie Cobbs NG_SEND_DATA_ONLY(error, priv->lower, *mp); /* sets *mp = NULL */
255e1e1452dSArchie Cobbs }
256e1e1452dSArchie Cobbs
257e1e1452dSArchie Cobbs /*
258e1e1452dSArchie Cobbs * Handle a packet that has come in on an interface, and which
259e1e1452dSArchie Cobbs * does not match any of our known protocols (an ``orphan'').
260e1e1452dSArchie Cobbs */
261e1e1452dSArchie Cobbs static void
ng_ether_input_orphan(struct ifnet * ifp,struct mbuf * m)262edbb5246SSam Leffler ng_ether_input_orphan(struct ifnet *ifp, struct mbuf *m)
263e1e1452dSArchie Cobbs {
264e1e1452dSArchie Cobbs const node_p node = IFP2NG(ifp);
26530400f03SJulian Elischer const priv_p priv = NG_NODE_PRIVATE(node);
2661a292b80SArchie Cobbs int error;
267e1e1452dSArchie Cobbs
2681a292b80SArchie Cobbs /* If "orphan" hook not connected, discard packet */
2691a292b80SArchie Cobbs if (priv->orphan == NULL) {
270e1e1452dSArchie Cobbs m_freem(m);
271e1e1452dSArchie Cobbs return;
272e1e1452dSArchie Cobbs }
2731a292b80SArchie Cobbs NG_SEND_DATA_ONLY(error, priv->orphan, m);
274e1e1452dSArchie Cobbs }
275e1e1452dSArchie Cobbs
276e1e1452dSArchie Cobbs /*
277e1e1452dSArchie Cobbs * Handle a packet that is going out on an interface.
278e1e1452dSArchie Cobbs * The Ethernet header is already attached to the mbuf.
279e1e1452dSArchie Cobbs */
280e1e1452dSArchie Cobbs static int
ng_ether_output(struct ifnet * ifp,struct mbuf ** mp)281e1e1452dSArchie Cobbs ng_ether_output(struct ifnet *ifp, struct mbuf **mp)
282e1e1452dSArchie Cobbs {
283e1e1452dSArchie Cobbs const node_p node = IFP2NG(ifp);
28430400f03SJulian Elischer const priv_p priv = NG_NODE_PRIVATE(node);
285e1e1452dSArchie Cobbs int error = 0;
286e1e1452dSArchie Cobbs
287e1e1452dSArchie Cobbs /* If "upper" hook not connected, let packet continue */
288e1e1452dSArchie Cobbs if (priv->upper == NULL)
289e1e1452dSArchie Cobbs return (0);
290e1e1452dSArchie Cobbs
291e1e1452dSArchie Cobbs /* Send it out "upper" hook */
292f089869fSMarko Zec NG_OUTBOUND_THREAD_REF();
293069154d5SJulian Elischer NG_SEND_DATA_ONLY(error, priv->upper, *mp);
294f089869fSMarko Zec NG_OUTBOUND_THREAD_UNREF();
295e1e1452dSArchie Cobbs return (error);
296e1e1452dSArchie Cobbs }
297e1e1452dSArchie Cobbs
298e1e1452dSArchie Cobbs /*
299e1e1452dSArchie Cobbs * A new Ethernet interface has been attached.
300e1e1452dSArchie Cobbs * Create a new node for it, etc.
301e1e1452dSArchie Cobbs */
302e1e1452dSArchie Cobbs static void
ng_ether_attach(struct ifnet * ifp)303e1e1452dSArchie Cobbs ng_ether_attach(struct ifnet *ifp)
304e1e1452dSArchie Cobbs {
305499f60b1SAndriy Gapon char name[IFNAMSIZ];
306e1e1452dSArchie Cobbs priv_p priv;
307e1e1452dSArchie Cobbs node_p node;
308e1e1452dSArchie Cobbs
309aef8f344SMarko Zec /*
310aef8f344SMarko Zec * Do not create / attach an ether node to this ifnet if
311aef8f344SMarko Zec * a netgraph node with the same name already exists.
312aef8f344SMarko Zec * This should prevent ether nodes to become attached to
313aef8f344SMarko Zec * eiface nodes, which may be problematic due to naming
314aef8f344SMarko Zec * clashes.
315aef8f344SMarko Zec */
31602fd7b50SLuiz Otavio O Souza ng_ether_sanitize_ifname(ifp->if_xname, name);
31702fd7b50SLuiz Otavio O Souza if ((node = ng_name2noderef(NULL, name)) != NULL) {
318aef8f344SMarko Zec NG_NODE_UNREF(node);
319aef8f344SMarko Zec return;
320aef8f344SMarko Zec }
321aef8f344SMarko Zec
322e1e1452dSArchie Cobbs /* Create node */
3236e551fb6SDavid E. O'Brien KASSERT(!IFP2NG(ifp), ("%s: node already exists?", __func__));
324e1e1452dSArchie Cobbs if (ng_make_node_common(&ng_ether_typestruct, &node) != 0) {
325e1e1452dSArchie Cobbs log(LOG_ERR, "%s: can't %s for %s\n",
3269bf40edeSBrooks Davis __func__, "create node", ifp->if_xname);
327e1e1452dSArchie Cobbs return;
328e1e1452dSArchie Cobbs }
329e1e1452dSArchie Cobbs
330e1e1452dSArchie Cobbs /* Allocate private data */
3311ede983cSDag-Erling Smørgrav priv = malloc(sizeof(*priv), M_NETGRAPH, M_NOWAIT | M_ZERO);
332e1e1452dSArchie Cobbs if (priv == NULL) {
333e1e1452dSArchie Cobbs log(LOG_ERR, "%s: can't %s for %s\n",
3349bf40edeSBrooks Davis __func__, "allocate memory", ifp->if_xname);
33530400f03SJulian Elischer NG_NODE_UNREF(node);
336e1e1452dSArchie Cobbs return;
337e1e1452dSArchie Cobbs }
33830400f03SJulian Elischer NG_NODE_SET_PRIVATE(node, priv);
339e1e1452dSArchie Cobbs priv->ifp = ifp;
3405240dcdbSRuslan Ermilov IFP2NG(ifp) = node;
341a3e232d6SArchie Cobbs priv->hwassist = ifp->if_hwassist;
342e1e1452dSArchie Cobbs
343e1e1452dSArchie Cobbs /* Try to give the node the same name as the interface */
344499f60b1SAndriy Gapon if (ng_name_node(node, name) != 0)
345499f60b1SAndriy Gapon log(LOG_WARNING, "%s: can't name node %s\n", __func__, name);
346e1e1452dSArchie Cobbs }
347e1e1452dSArchie Cobbs
348e1e1452dSArchie Cobbs /*
349e1e1452dSArchie Cobbs * An Ethernet interface is being detached.
3501acb27c6SJulian Elischer * REALLY Destroy its node.
351e1e1452dSArchie Cobbs */
352e1e1452dSArchie Cobbs static void
ng_ether_detach(struct ifnet * ifp)353e1e1452dSArchie Cobbs ng_ether_detach(struct ifnet *ifp)
354e1e1452dSArchie Cobbs {
355e1e1452dSArchie Cobbs const node_p node = IFP2NG(ifp);
3561acb27c6SJulian Elischer const priv_p priv = NG_NODE_PRIVATE(node);
357e1e1452dSArchie Cobbs
3582cdf8c49SMarko Zec taskqueue_drain(taskqueue_swi, &ifp->if_linktask);
3591acb27c6SJulian Elischer NG_NODE_REALLY_DIE(node); /* Force real removal of node */
3601acb27c6SJulian Elischer /*
3611acb27c6SJulian Elischer * We can't assume the ifnet is still around when we run shutdown
3621acb27c6SJulian Elischer * So zap it now. XXX We HOPE that anything running at this time
3631acb27c6SJulian Elischer * handles it (as it should in the non netgraph case).
3641acb27c6SJulian Elischer */
3655240dcdbSRuslan Ermilov IFP2NG(ifp) = NULL;
3661acb27c6SJulian Elischer priv->ifp = NULL; /* XXX race if interrupted an output packet */
3671acb27c6SJulian Elischer ng_rmnode_self(node); /* remove all netgraph parts */
368e1e1452dSArchie Cobbs }
369e1e1452dSArchie Cobbs
3701c7899c7SGleb Smirnoff /*
3711c7899c7SGleb Smirnoff * Notify graph about link event.
3721c7899c7SGleb Smirnoff * if_link_state_change() has already checked that the state has changed.
3731c7899c7SGleb Smirnoff */
3741c7899c7SGleb Smirnoff static void
ng_ether_link_state(struct ifnet * ifp,int state)3751c7899c7SGleb Smirnoff ng_ether_link_state(struct ifnet *ifp, int state)
3761c7899c7SGleb Smirnoff {
3771c7899c7SGleb Smirnoff const node_p node = IFP2NG(ifp);
3781c7899c7SGleb Smirnoff const priv_p priv = NG_NODE_PRIVATE(node);
3791c7899c7SGleb Smirnoff struct ng_mesg *msg;
3801c7899c7SGleb Smirnoff int cmd, dummy_error = 0;
3811c7899c7SGleb Smirnoff
3821c7899c7SGleb Smirnoff if (state == LINK_STATE_UP)
3831c7899c7SGleb Smirnoff cmd = NGM_LINK_IS_UP;
3841c7899c7SGleb Smirnoff else if (state == LINK_STATE_DOWN)
3851c7899c7SGleb Smirnoff cmd = NGM_LINK_IS_DOWN;
3861c7899c7SGleb Smirnoff else
3871c7899c7SGleb Smirnoff return;
3881c7899c7SGleb Smirnoff
389bb027e06SMax Khon if (priv->lower != NULL) {
3901c7899c7SGleb Smirnoff NG_MKMESSAGE(msg, NGM_FLOW_COOKIE, cmd, 0, M_NOWAIT);
3911c7899c7SGleb Smirnoff if (msg != NULL)
3921c7899c7SGleb Smirnoff NG_SEND_MSG_HOOK(dummy_error, node, msg, priv->lower, 0);
3931c7899c7SGleb Smirnoff }
394bb027e06SMax Khon if (priv->orphan != NULL) {
395bb027e06SMax Khon NG_MKMESSAGE(msg, NGM_FLOW_COOKIE, cmd, 0, M_NOWAIT);
396bb027e06SMax Khon if (msg != NULL)
397bb027e06SMax Khon NG_SEND_MSG_HOOK(dummy_error, node, msg, priv->orphan, 0);
398bb027e06SMax Khon }
399bb027e06SMax Khon }
4001c7899c7SGleb Smirnoff
401499f60b1SAndriy Gapon /*
402499f60b1SAndriy Gapon * Interface arrival notification handler.
403499f60b1SAndriy Gapon * The notification is produced in two cases:
404499f60b1SAndriy Gapon * o a new interface arrives
405499f60b1SAndriy Gapon * o an existing interface got renamed
406499f60b1SAndriy Gapon * Currently the first case is handled by ng_ether_attach via special
407499f60b1SAndriy Gapon * hook ng_ether_attach_p.
408499f60b1SAndriy Gapon */
409499f60b1SAndriy Gapon static void
ng_ether_ifnet_arrival_event(void * arg __unused,struct ifnet * ifp)410499f60b1SAndriy Gapon ng_ether_ifnet_arrival_event(void *arg __unused, struct ifnet *ifp)
411499f60b1SAndriy Gapon {
412499f60b1SAndriy Gapon char name[IFNAMSIZ];
41330bc1032SAndriy Gapon node_p node;
41430bc1032SAndriy Gapon
41530bc1032SAndriy Gapon /* Only ethernet interfaces are of interest. */
416d653b188SYoshihiro Takahashi if (ifp->if_type != IFT_ETHER &&
417d653b188SYoshihiro Takahashi ifp->if_type != IFT_L2VLAN &&
418d653b188SYoshihiro Takahashi ifp->if_type != IFT_BRIDGE)
41930bc1032SAndriy Gapon return;
420499f60b1SAndriy Gapon
421499f60b1SAndriy Gapon /*
422499f60b1SAndriy Gapon * Just return if it's a new interface without an ng_ether companion.
423499f60b1SAndriy Gapon */
42430bc1032SAndriy Gapon node = IFP2NG(ifp);
425499f60b1SAndriy Gapon if (node == NULL)
426499f60b1SAndriy Gapon return;
427499f60b1SAndriy Gapon
428499f60b1SAndriy Gapon /* Try to give the node the same name as the new interface name */
429499f60b1SAndriy Gapon ng_ether_sanitize_ifname(ifp->if_xname, name);
430499f60b1SAndriy Gapon if (ng_name_node(node, name) != 0)
431499f60b1SAndriy Gapon log(LOG_WARNING, "%s: can't re-name node %s\n", __func__, name);
432499f60b1SAndriy Gapon }
433499f60b1SAndriy Gapon
434e1e1452dSArchie Cobbs /******************************************************************
435e1e1452dSArchie Cobbs NETGRAPH NODE METHODS
436e1e1452dSArchie Cobbs ******************************************************************/
437e1e1452dSArchie Cobbs
438e1e1452dSArchie Cobbs /*
439e1e1452dSArchie Cobbs * It is not possible or allowable to create a node of this type.
440e1e1452dSArchie Cobbs * Nodes get created when the interface is attached (or, when
441e1e1452dSArchie Cobbs * this node type's KLD is loaded).
442e1e1452dSArchie Cobbs */
443e1e1452dSArchie Cobbs static int
ng_ether_constructor(node_p node)444069154d5SJulian Elischer ng_ether_constructor(node_p node)
445e1e1452dSArchie Cobbs {
446e1e1452dSArchie Cobbs return (EINVAL);
447e1e1452dSArchie Cobbs }
448e1e1452dSArchie Cobbs
449e1e1452dSArchie Cobbs /*
450e1e1452dSArchie Cobbs * Check for attaching a new hook.
451e1e1452dSArchie Cobbs */
452e1e1452dSArchie Cobbs static int
ng_ether_newhook(node_p node,hook_p hook,const char * name)453e1e1452dSArchie Cobbs ng_ether_newhook(node_p node, hook_p hook, const char *name)
454e1e1452dSArchie Cobbs {
45530400f03SJulian Elischer const priv_p priv = NG_NODE_PRIVATE(node);
456e1e1452dSArchie Cobbs hook_p *hookptr;
457e1e1452dSArchie Cobbs
458e1e1452dSArchie Cobbs /* Divert hook is an alias for lower */
459e1e1452dSArchie Cobbs if (strcmp(name, NG_ETHER_HOOK_DIVERT) == 0)
460e1e1452dSArchie Cobbs name = NG_ETHER_HOOK_LOWER;
461e1e1452dSArchie Cobbs
462e1e1452dSArchie Cobbs /* Which hook? */
463f664dcdeSJulian Elischer if (strcmp(name, NG_ETHER_HOOK_UPPER) == 0) {
464e1e1452dSArchie Cobbs hookptr = &priv->upper;
465f664dcdeSJulian Elischer NG_HOOK_SET_RCVDATA(hook, ng_ether_rcv_upper);
466f089869fSMarko Zec NG_HOOK_SET_TO_INBOUND(hook);
467f664dcdeSJulian Elischer } else if (strcmp(name, NG_ETHER_HOOK_LOWER) == 0) {
468e1e1452dSArchie Cobbs hookptr = &priv->lower;
469f664dcdeSJulian Elischer NG_HOOK_SET_RCVDATA(hook, ng_ether_rcv_lower);
470f664dcdeSJulian Elischer } else if (strcmp(name, NG_ETHER_HOOK_ORPHAN) == 0) {
4711a292b80SArchie Cobbs hookptr = &priv->orphan;
472f664dcdeSJulian Elischer NG_HOOK_SET_RCVDATA(hook, ng_ether_rcv_lower);
473f664dcdeSJulian Elischer } else
474e1e1452dSArchie Cobbs return (EINVAL);
475e1e1452dSArchie Cobbs
476e1e1452dSArchie Cobbs /* Check if already connected (shouldn't be, but doesn't hurt) */
477e1e1452dSArchie Cobbs if (*hookptr != NULL)
478e1e1452dSArchie Cobbs return (EISCONN);
479e1e1452dSArchie Cobbs
480a3e232d6SArchie Cobbs /* Disable hardware checksums while 'upper' hook is connected */
481a3e232d6SArchie Cobbs if (hookptr == &priv->upper)
482a3e232d6SArchie Cobbs priv->ifp->if_hwassist = 0;
483c1b8a9edSAlexander Motin NG_HOOK_HI_STACK(hook);
484e1e1452dSArchie Cobbs /* OK */
485e1e1452dSArchie Cobbs *hookptr = hook;
486e1e1452dSArchie Cobbs return (0);
487e1e1452dSArchie Cobbs }
488e1e1452dSArchie Cobbs
489e1e1452dSArchie Cobbs /*
490e1e1452dSArchie Cobbs * Receive an incoming control message.
491e1e1452dSArchie Cobbs */
492e1e1452dSArchie Cobbs static int
ng_ether_rcvmsg(node_p node,item_p item,hook_p lasthook)493069154d5SJulian Elischer ng_ether_rcvmsg(node_p node, item_p item, hook_p lasthook)
494e1e1452dSArchie Cobbs {
49530400f03SJulian Elischer const priv_p priv = NG_NODE_PRIVATE(node);
496e1e1452dSArchie Cobbs struct ng_mesg *resp = NULL;
497e1e1452dSArchie Cobbs int error = 0;
498069154d5SJulian Elischer struct ng_mesg *msg;
499e1e1452dSArchie Cobbs
500069154d5SJulian Elischer NGI_GET_MSG(item, msg);
501e1e1452dSArchie Cobbs switch (msg->header.typecookie) {
502e1e1452dSArchie Cobbs case NGM_ETHER_COOKIE:
503e1e1452dSArchie Cobbs switch (msg->header.cmd) {
504e1e1452dSArchie Cobbs case NGM_ETHER_GET_IFNAME:
505bbb75d78SRuslan Ermilov NG_MKRESPONSE(resp, msg, IFNAMSIZ, M_NOWAIT);
506e1e1452dSArchie Cobbs if (resp == NULL) {
507e1e1452dSArchie Cobbs error = ENOMEM;
508e1e1452dSArchie Cobbs break;
509e1e1452dSArchie Cobbs }
510bbb75d78SRuslan Ermilov strlcpy(resp->data, priv->ifp->if_xname, IFNAMSIZ);
511e1e1452dSArchie Cobbs break;
512e1e1452dSArchie Cobbs case NGM_ETHER_GET_IFINDEX:
513e1e1452dSArchie Cobbs NG_MKRESPONSE(resp, msg, sizeof(u_int32_t), M_NOWAIT);
514e1e1452dSArchie Cobbs if (resp == NULL) {
515e1e1452dSArchie Cobbs error = ENOMEM;
516e1e1452dSArchie Cobbs break;
517e1e1452dSArchie Cobbs }
518e1e1452dSArchie Cobbs *((u_int32_t *)resp->data) = priv->ifp->if_index;
519e1e1452dSArchie Cobbs break;
5204b39c3c7SArchie Cobbs case NGM_ETHER_GET_ENADDR:
5214b39c3c7SArchie Cobbs NG_MKRESPONSE(resp, msg, ETHER_ADDR_LEN, M_NOWAIT);
5224b39c3c7SArchie Cobbs if (resp == NULL) {
5234b39c3c7SArchie Cobbs error = ENOMEM;
5244b39c3c7SArchie Cobbs break;
5254b39c3c7SArchie Cobbs }
5264a0d6638SRuslan Ermilov bcopy(IF_LLADDR(priv->ifp),
5274b39c3c7SArchie Cobbs resp->data, ETHER_ADDR_LEN);
5284b39c3c7SArchie Cobbs break;
52956045c66SArchie Cobbs case NGM_ETHER_SET_ENADDR:
53056045c66SArchie Cobbs {
53156045c66SArchie Cobbs if (msg->header.arglen != ETHER_ADDR_LEN) {
53256045c66SArchie Cobbs error = EINVAL;
53356045c66SArchie Cobbs break;
53456045c66SArchie Cobbs }
53556045c66SArchie Cobbs error = if_setlladdr(priv->ifp,
53656045c66SArchie Cobbs (u_char *)msg->data, ETHER_ADDR_LEN);
53756045c66SArchie Cobbs break;
53856045c66SArchie Cobbs }
53956045c66SArchie Cobbs case NGM_ETHER_GET_PROMISC:
54056045c66SArchie Cobbs NG_MKRESPONSE(resp, msg, sizeof(u_int32_t), M_NOWAIT);
54156045c66SArchie Cobbs if (resp == NULL) {
54256045c66SArchie Cobbs error = ENOMEM;
54356045c66SArchie Cobbs break;
54456045c66SArchie Cobbs }
54556045c66SArchie Cobbs *((u_int32_t *)resp->data) = priv->promisc;
54656045c66SArchie Cobbs break;
5474b39c3c7SArchie Cobbs case NGM_ETHER_SET_PROMISC:
5484b39c3c7SArchie Cobbs {
5494b39c3c7SArchie Cobbs u_char want;
5504b39c3c7SArchie Cobbs
5514b39c3c7SArchie Cobbs if (msg->header.arglen != sizeof(u_int32_t)) {
5524b39c3c7SArchie Cobbs error = EINVAL;
5534b39c3c7SArchie Cobbs break;
5544b39c3c7SArchie Cobbs }
5554b39c3c7SArchie Cobbs want = !!*((u_int32_t *)msg->data);
5564b39c3c7SArchie Cobbs if (want ^ priv->promisc) {
5574b39c3c7SArchie Cobbs if ((error = ifpromisc(priv->ifp, want)) != 0)
5584b39c3c7SArchie Cobbs break;
5594b39c3c7SArchie Cobbs priv->promisc = want;
5604b39c3c7SArchie Cobbs }
5614b39c3c7SArchie Cobbs break;
5624b39c3c7SArchie Cobbs }
56356045c66SArchie Cobbs case NGM_ETHER_GET_AUTOSRC:
56456045c66SArchie Cobbs NG_MKRESPONSE(resp, msg, sizeof(u_int32_t), M_NOWAIT);
56556045c66SArchie Cobbs if (resp == NULL) {
56656045c66SArchie Cobbs error = ENOMEM;
56756045c66SArchie Cobbs break;
56856045c66SArchie Cobbs }
56956045c66SArchie Cobbs *((u_int32_t *)resp->data) = priv->autoSrcAddr;
57056045c66SArchie Cobbs break;
5714b39c3c7SArchie Cobbs case NGM_ETHER_SET_AUTOSRC:
5724b39c3c7SArchie Cobbs if (msg->header.arglen != sizeof(u_int32_t)) {
5734b39c3c7SArchie Cobbs error = EINVAL;
5744b39c3c7SArchie Cobbs break;
5754b39c3c7SArchie Cobbs }
5764b39c3c7SArchie Cobbs priv->autoSrcAddr = !!*((u_int32_t *)msg->data);
5774b39c3c7SArchie Cobbs break;
578810d5e89SGleb Smirnoff case NGM_ETHER_ADD_MULTI:
579810d5e89SGleb Smirnoff {
580810d5e89SGleb Smirnoff struct sockaddr_dl sa_dl;
58157985d11SGleb Smirnoff struct epoch_tracker et;
582ec002feeSBruce M Simpson struct ifmultiaddr *ifma;
583810d5e89SGleb Smirnoff
584810d5e89SGleb Smirnoff if (msg->header.arglen != ETHER_ADDR_LEN) {
585810d5e89SGleb Smirnoff error = EINVAL;
586810d5e89SGleb Smirnoff break;
587810d5e89SGleb Smirnoff }
588ba20540eSGleb Smirnoff bzero(&sa_dl, sizeof(struct sockaddr_dl));
589810d5e89SGleb Smirnoff sa_dl.sdl_len = sizeof(struct sockaddr_dl);
590810d5e89SGleb Smirnoff sa_dl.sdl_family = AF_LINK;
591ba20540eSGleb Smirnoff sa_dl.sdl_alen = ETHER_ADDR_LEN;
592810d5e89SGleb Smirnoff bcopy((void *)msg->data, LLADDR(&sa_dl),
593810d5e89SGleb Smirnoff ETHER_ADDR_LEN);
594ec002feeSBruce M Simpson /*
595ec002feeSBruce M Simpson * Netgraph is only permitted to join groups once
596ec002feeSBruce M Simpson * via the if_addmulti() KPI, because it cannot hold
597ec002feeSBruce M Simpson * struct ifmultiaddr * between calls. It may also
598ec002feeSBruce M Simpson * lose a race while we check if the membership
599ec002feeSBruce M Simpson * already exists.
600ec002feeSBruce M Simpson */
60157985d11SGleb Smirnoff NET_EPOCH_ENTER(et);
602ec002feeSBruce M Simpson ifma = if_findmulti(priv->ifp,
603ec002feeSBruce M Simpson (struct sockaddr *)&sa_dl);
60457985d11SGleb Smirnoff NET_EPOCH_EXIT(et);
605ec002feeSBruce M Simpson if (ifma != NULL) {
606ec002feeSBruce M Simpson error = EADDRINUSE;
607ec002feeSBruce M Simpson } else {
608810d5e89SGleb Smirnoff error = if_addmulti(priv->ifp,
609ec002feeSBruce M Simpson (struct sockaddr *)&sa_dl, &ifma);
610ec002feeSBruce M Simpson }
611810d5e89SGleb Smirnoff break;
612810d5e89SGleb Smirnoff }
613810d5e89SGleb Smirnoff case NGM_ETHER_DEL_MULTI:
614810d5e89SGleb Smirnoff {
615810d5e89SGleb Smirnoff struct sockaddr_dl sa_dl;
616810d5e89SGleb Smirnoff
617810d5e89SGleb Smirnoff if (msg->header.arglen != ETHER_ADDR_LEN) {
618810d5e89SGleb Smirnoff error = EINVAL;
619810d5e89SGleb Smirnoff break;
620810d5e89SGleb Smirnoff }
621ba20540eSGleb Smirnoff bzero(&sa_dl, sizeof(struct sockaddr_dl));
622810d5e89SGleb Smirnoff sa_dl.sdl_len = sizeof(struct sockaddr_dl);
623810d5e89SGleb Smirnoff sa_dl.sdl_family = AF_LINK;
624ba20540eSGleb Smirnoff sa_dl.sdl_alen = ETHER_ADDR_LEN;
625810d5e89SGleb Smirnoff bcopy((void *)msg->data, LLADDR(&sa_dl),
626810d5e89SGleb Smirnoff ETHER_ADDR_LEN);
627810d5e89SGleb Smirnoff error = if_delmulti(priv->ifp,
628810d5e89SGleb Smirnoff (struct sockaddr *)&sa_dl);
629810d5e89SGleb Smirnoff break;
630810d5e89SGleb Smirnoff }
631cefddd66SGleb Smirnoff case NGM_ETHER_DETACH:
632cefddd66SGleb Smirnoff ng_ether_detach(priv->ifp);
633cefddd66SGleb Smirnoff break;
634e1e1452dSArchie Cobbs default:
635e1e1452dSArchie Cobbs error = EINVAL;
636e1e1452dSArchie Cobbs break;
637e1e1452dSArchie Cobbs }
638e1e1452dSArchie Cobbs break;
639e1e1452dSArchie Cobbs default:
640e1e1452dSArchie Cobbs error = EINVAL;
641e1e1452dSArchie Cobbs break;
642e1e1452dSArchie Cobbs }
643069154d5SJulian Elischer NG_RESPOND_MSG(error, node, item, resp);
644069154d5SJulian Elischer NG_FREE_MSG(msg);
645e1e1452dSArchie Cobbs return (error);
646e1e1452dSArchie Cobbs }
647e1e1452dSArchie Cobbs
648e1e1452dSArchie Cobbs /*
649e1e1452dSArchie Cobbs * Receive data on a hook.
650f664dcdeSJulian Elischer * Since we use per-hook recveive methods this should never be called.
651e1e1452dSArchie Cobbs */
652e1e1452dSArchie Cobbs static int
ng_ether_rcvdata(hook_p hook,item_p item)653069154d5SJulian Elischer ng_ether_rcvdata(hook_p hook, item_p item)
654e1e1452dSArchie Cobbs {
655069154d5SJulian Elischer NG_FREE_ITEM(item);
6563ca24c28SJulian Elischer
6576e551fb6SDavid E. O'Brien panic("%s: weird hook", __func__);
658e1e1452dSArchie Cobbs }
659e1e1452dSArchie Cobbs
660e1e1452dSArchie Cobbs /*
6611a292b80SArchie Cobbs * Handle an mbuf received on the "lower" or "orphan" hook.
662e1e1452dSArchie Cobbs */
663e1e1452dSArchie Cobbs static int
ng_ether_rcv_lower(hook_p hook,item_p item)664f664dcdeSJulian Elischer ng_ether_rcv_lower(hook_p hook, item_p item)
665e1e1452dSArchie Cobbs {
666f664dcdeSJulian Elischer struct mbuf *m;
667f664dcdeSJulian Elischer const node_p node = NG_HOOK_NODE(hook);
66830400f03SJulian Elischer const priv_p priv = NG_NODE_PRIVATE(node);
669a1479aa2SArchie Cobbs struct ifnet *const ifp = priv->ifp;
670a1479aa2SArchie Cobbs
671f664dcdeSJulian Elischer NGI_GET_M(item, m);
672f664dcdeSJulian Elischer NG_FREE_ITEM(item);
673f664dcdeSJulian Elischer
674a1479aa2SArchie Cobbs /* Check whether interface is ready for packets */
675f664dcdeSJulian Elischer
67613f4c340SRobert Watson if (!((ifp->if_flags & IFF_UP) &&
67713f4c340SRobert Watson (ifp->if_drv_flags & IFF_DRV_RUNNING))) {
678a1479aa2SArchie Cobbs NG_FREE_M(m);
679a1479aa2SArchie Cobbs return (ENETDOWN);
680a1479aa2SArchie Cobbs }
681e1e1452dSArchie Cobbs
682e1e1452dSArchie Cobbs /* Make sure header is fully pulled up */
683e1e1452dSArchie Cobbs if (m->m_pkthdr.len < sizeof(struct ether_header)) {
684069154d5SJulian Elischer NG_FREE_M(m);
685e1e1452dSArchie Cobbs return (EINVAL);
686e1e1452dSArchie Cobbs }
687e1e1452dSArchie Cobbs if (m->m_len < sizeof(struct ether_header)
6887b9f235fSArchie Cobbs && (m = m_pullup(m, sizeof(struct ether_header))) == NULL)
689e1e1452dSArchie Cobbs return (ENOBUFS);
690e1e1452dSArchie Cobbs
6914b39c3c7SArchie Cobbs /* Drop in the MAC address if desired */
6924b39c3c7SArchie Cobbs if (priv->autoSrcAddr) {
6937b9f235fSArchie Cobbs /* Make the mbuf writable if it's not already */
6947b9f235fSArchie Cobbs if (!M_WRITABLE(m)
6957b9f235fSArchie Cobbs && (m = m_pullup(m, sizeof(struct ether_header))) == NULL)
6967b9f235fSArchie Cobbs return (ENOBUFS);
6977b9f235fSArchie Cobbs
6987b9f235fSArchie Cobbs /* Overwrite source MAC address */
6994a0d6638SRuslan Ermilov bcopy(IF_LLADDR(ifp),
7004b39c3c7SArchie Cobbs mtod(m, struct ether_header *)->ether_shost,
7014b39c3c7SArchie Cobbs ETHER_ADDR_LEN);
7024b39c3c7SArchie Cobbs }
703561e4fb9SJulian Elischer
704e1e1452dSArchie Cobbs /* Send it on its way */
705a1479aa2SArchie Cobbs return ether_output_frame(ifp, m);
706e1e1452dSArchie Cobbs }
707e1e1452dSArchie Cobbs
708e1e1452dSArchie Cobbs /*
709e1e1452dSArchie Cobbs * Handle an mbuf received on the "upper" hook.
710e1e1452dSArchie Cobbs */
711e1e1452dSArchie Cobbs static int
ng_ether_rcv_upper(hook_p hook,item_p item)712f664dcdeSJulian Elischer ng_ether_rcv_upper(hook_p hook, item_p item)
713e1e1452dSArchie Cobbs {
714f664dcdeSJulian Elischer struct mbuf *m;
715f664dcdeSJulian Elischer const node_p node = NG_HOOK_NODE(hook);
71630400f03SJulian Elischer const priv_p priv = NG_NODE_PRIVATE(node);
7176512768bSGleb Smirnoff struct ifnet *ifp = priv->ifp;
718e1e1452dSArchie Cobbs
719f664dcdeSJulian Elischer NGI_GET_M(item, m);
720f664dcdeSJulian Elischer NG_FREE_ITEM(item);
721f664dcdeSJulian Elischer
722c60c00bcSRuslan Ermilov /* Check length and pull off header */
723c60c00bcSRuslan Ermilov if (m->m_pkthdr.len < sizeof(struct ether_header)) {
724c60c00bcSRuslan Ermilov NG_FREE_M(m);
725c60c00bcSRuslan Ermilov return (EINVAL);
726c60c00bcSRuslan Ermilov }
727c60c00bcSRuslan Ermilov if (m->m_len < sizeof(struct ether_header) &&
728c60c00bcSRuslan Ermilov (m = m_pullup(m, sizeof(struct ether_header))) == NULL)
729c60c00bcSRuslan Ermilov return (ENOBUFS);
730c60c00bcSRuslan Ermilov
7316512768bSGleb Smirnoff m->m_pkthdr.rcvif = ifp;
732e1e1452dSArchie Cobbs
733fd6238a6SAndrew Thompson /* Pass the packet to the bridge, it may come back to us */
7346512768bSGleb Smirnoff if (ifp->if_bridge) {
735fd6238a6SAndrew Thompson BRIDGE_INPUT(ifp, m);
7366512768bSGleb Smirnoff if (m == NULL)
7376512768bSGleb Smirnoff return (0);
7386512768bSGleb Smirnoff }
739a176c2aeSGleb Smirnoff
740e1e1452dSArchie Cobbs /* Route packet back in */
741fd6238a6SAndrew Thompson ether_demux(ifp, m);
742e1e1452dSArchie Cobbs return (0);
743e1e1452dSArchie Cobbs }
744e1e1452dSArchie Cobbs
745e1e1452dSArchie Cobbs /*
7461acb27c6SJulian Elischer * Shutdown node. This resets the node but does not remove it
7471acb27c6SJulian Elischer * unless the REALLY_DIE flag is set.
748e1e1452dSArchie Cobbs */
749e1e1452dSArchie Cobbs static int
ng_ether_shutdown(node_p node)750069154d5SJulian Elischer ng_ether_shutdown(node_p node)
751e1e1452dSArchie Cobbs {
75230400f03SJulian Elischer const priv_p priv = NG_NODE_PRIVATE(node);
7534b39c3c7SArchie Cobbs
754be4252b3SJulian Elischer if (node->nd_flags & NGF_REALLY_DIE) {
7551acb27c6SJulian Elischer /*
756cd698c51SMark Johnston * The ifnet is going away, perhaps because the driver was
757cd698c51SMark Johnston * unloaded or its vnet is being torn down.
7581acb27c6SJulian Elischer */
7591acb27c6SJulian Elischer NG_NODE_SET_PRIVATE(node, NULL);
760cd698c51SMark Johnston if (priv->ifp != NULL)
761cd698c51SMark Johnston IFP2NG(priv->ifp) = NULL;
7621ede983cSDag-Erling Smørgrav free(priv, M_NETGRAPH);
7631acb27c6SJulian Elischer NG_NODE_UNREF(node); /* free node itself */
7641acb27c6SJulian Elischer return (0);
765069154d5SJulian Elischer }
766018df1c3SBrian Feldman if (priv->promisc) { /* disable promiscuous mode */
767018df1c3SBrian Feldman (void)ifpromisc(priv->ifp, 0);
768018df1c3SBrian Feldman priv->promisc = 0;
769018df1c3SBrian Feldman }
770be4252b3SJulian Elischer NG_NODE_REVIVE(node); /* Signal ng_rmnode we are persisant */
771be4252b3SJulian Elischer
772e1e1452dSArchie Cobbs return (0);
773e1e1452dSArchie Cobbs }
774e1e1452dSArchie Cobbs
775e1e1452dSArchie Cobbs /*
776e1e1452dSArchie Cobbs * Hook disconnection.
777e1e1452dSArchie Cobbs */
778e1e1452dSArchie Cobbs static int
ng_ether_disconnect(hook_p hook)779e1e1452dSArchie Cobbs ng_ether_disconnect(hook_p hook)
780e1e1452dSArchie Cobbs {
78130400f03SJulian Elischer const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
782e1e1452dSArchie Cobbs
783a3e232d6SArchie Cobbs if (hook == priv->upper) {
784e1e1452dSArchie Cobbs priv->upper = NULL;
785b712e9ecSBrian Feldman if (priv->ifp != NULL) /* restore h/w csum */
786b712e9ecSBrian Feldman priv->ifp->if_hwassist = priv->hwassist;
7871a292b80SArchie Cobbs } else if (hook == priv->lower)
788e1e1452dSArchie Cobbs priv->lower = NULL;
7891a292b80SArchie Cobbs else if (hook == priv->orphan)
7901a292b80SArchie Cobbs priv->orphan = NULL;
7911a292b80SArchie Cobbs else
7926e551fb6SDavid E. O'Brien panic("%s: weird hook", __func__);
79330400f03SJulian Elischer if ((NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0)
79430400f03SJulian Elischer && (NG_NODE_IS_VALID(NG_HOOK_NODE(hook))))
79530400f03SJulian Elischer ng_rmnode_self(NG_HOOK_NODE(hook)); /* reset node */
796e1e1452dSArchie Cobbs return (0);
797e1e1452dSArchie Cobbs }
798e1e1452dSArchie Cobbs
799e1e1452dSArchie Cobbs /******************************************************************
800e1e1452dSArchie Cobbs INITIALIZATION
801e1e1452dSArchie Cobbs ******************************************************************/
802e1e1452dSArchie Cobbs
803e1e1452dSArchie Cobbs /*
804e1e1452dSArchie Cobbs * Handle loading and unloading for this node type.
805e1e1452dSArchie Cobbs */
806e1e1452dSArchie Cobbs static int
ng_ether_mod_event(module_t mod,int event,void * data)807e1e1452dSArchie Cobbs ng_ether_mod_event(module_t mod, int event, void *data)
808e1e1452dSArchie Cobbs {
809e1e1452dSArchie Cobbs int error = 0;
810e1e1452dSArchie Cobbs
811e1e1452dSArchie Cobbs switch (event) {
812e1e1452dSArchie Cobbs case MOD_LOAD:
813e1e1452dSArchie Cobbs
814e1e1452dSArchie Cobbs /* Register function hooks */
815e1e1452dSArchie Cobbs if (ng_ether_attach_p != NULL) {
816e1e1452dSArchie Cobbs error = EEXIST;
817e1e1452dSArchie Cobbs break;
818e1e1452dSArchie Cobbs }
819e1e1452dSArchie Cobbs ng_ether_attach_p = ng_ether_attach;
820e1e1452dSArchie Cobbs ng_ether_detach_p = ng_ether_detach;
821e1e1452dSArchie Cobbs ng_ether_output_p = ng_ether_output;
822e1e1452dSArchie Cobbs ng_ether_input_p = ng_ether_input;
823e1e1452dSArchie Cobbs ng_ether_input_orphan_p = ng_ether_input_orphan;
8241c7899c7SGleb Smirnoff ng_ether_link_state_p = ng_ether_link_state;
825e1e1452dSArchie Cobbs
826499f60b1SAndriy Gapon ng_ether_ifnet_arrival_cookie =
827499f60b1SAndriy Gapon EVENTHANDLER_REGISTER(ifnet_arrival_event,
828499f60b1SAndriy Gapon ng_ether_ifnet_arrival_event, NULL, EVENTHANDLER_PRI_ANY);
829e1e1452dSArchie Cobbs break;
830e1e1452dSArchie Cobbs
831e1e1452dSArchie Cobbs case MOD_UNLOAD:
832e1e1452dSArchie Cobbs
833e1e1452dSArchie Cobbs /*
834e1e1452dSArchie Cobbs * Note that the base code won't try to unload us until
835e1e1452dSArchie Cobbs * all nodes have been removed, and that can't happen
836e1e1452dSArchie Cobbs * until all Ethernet interfaces are removed. In any
837e1e1452dSArchie Cobbs * case, we know there are no nodes left if the action
838e1e1452dSArchie Cobbs * is MOD_UNLOAD, so there's no need to detach any nodes.
839e1e1452dSArchie Cobbs */
840e1e1452dSArchie Cobbs
841499f60b1SAndriy Gapon EVENTHANDLER_DEREGISTER(ifnet_arrival_event,
842499f60b1SAndriy Gapon ng_ether_ifnet_arrival_cookie);
843499f60b1SAndriy Gapon
844e1e1452dSArchie Cobbs /* Unregister function hooks */
845e1e1452dSArchie Cobbs ng_ether_attach_p = NULL;
846e1e1452dSArchie Cobbs ng_ether_detach_p = NULL;
847e1e1452dSArchie Cobbs ng_ether_output_p = NULL;
848e1e1452dSArchie Cobbs ng_ether_input_p = NULL;
849e1e1452dSArchie Cobbs ng_ether_input_orphan_p = NULL;
8501c7899c7SGleb Smirnoff ng_ether_link_state_p = NULL;
851e1e1452dSArchie Cobbs break;
852e1e1452dSArchie Cobbs
853e1e1452dSArchie Cobbs default:
854e1e1452dSArchie Cobbs error = EOPNOTSUPP;
855e1e1452dSArchie Cobbs break;
856e1e1452dSArchie Cobbs }
857e1e1452dSArchie Cobbs return (error);
858e1e1452dSArchie Cobbs }
859e1e1452dSArchie Cobbs
860d0728d71SRobert Watson static void
vnet_ng_ether_init(const void * unused)861d0728d71SRobert Watson vnet_ng_ether_init(const void *unused)
862aef8f344SMarko Zec {
863aef8f344SMarko Zec struct ifnet *ifp;
864aef8f344SMarko Zec
865d0728d71SRobert Watson /* If module load was rejected, don't attach to vnets. */
866d0728d71SRobert Watson if (ng_ether_attach_p != ng_ether_attach)
867d0728d71SRobert Watson return;
868d0728d71SRobert Watson
869aef8f344SMarko Zec /* Create nodes for any already-existing Ethernet interfaces. */
870aef8f344SMarko Zec IFNET_RLOCK();
8714560b78dSJohn Baldwin CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
872d653b188SYoshihiro Takahashi if (ifp->if_type == IFT_ETHER ||
873d653b188SYoshihiro Takahashi ifp->if_type == IFT_L2VLAN ||
874d653b188SYoshihiro Takahashi ifp->if_type == IFT_BRIDGE)
875aef8f344SMarko Zec ng_ether_attach(ifp);
876aef8f344SMarko Zec }
877aef8f344SMarko Zec IFNET_RUNLOCK();
878aef8f344SMarko Zec }
879d0728d71SRobert Watson VNET_SYSINIT(vnet_ng_ether_init, SI_SUB_PSEUDO, SI_ORDER_ANY,
880d0728d71SRobert Watson vnet_ng_ether_init, NULL);
881