3 * Copyright (c) 1999-2001, Vitaly V Belekhov
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice unmodified, this list of conditions, and the following
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/errno.h>
34 #include <sys/kernel.h>
35 #include <sys/malloc.h>
37 #include <sys/errno.h>
38 #include <sys/sockio.h>
39 #include <sys/socket.h>
40 #include <sys/syslog.h>
43 #include <net/if_types.h>
44 #include <net/netisr.h>
46 #include <netgraph/ng_message.h>
47 #include <netgraph/netgraph.h>
48 #include <netgraph/ng_parse.h>
49 #include <netgraph/ng_eiface.h>
52 #include <net/ethernet.h>
53 #include <net/if_arp.h>
55 static const struct ng_cmdlist ng_eiface_cmdlist[] = {
58 NGM_EIFACE_GET_IFNAME,
67 &ng_parse_enaddr_type,
73 /* Node private data */
74 struct ng_eiface_private {
75 struct ifnet *ifp; /* per-interface network data */
76 int unit; /* Interface unit number */
77 node_p node; /* Our netgraph node */
78 hook_p ether; /* Hook for ethernet stream */
80 typedef struct ng_eiface_private *priv_p;
82 /* Interface methods */
83 static void ng_eiface_init(void *xsc);
84 static void ng_eiface_start(struct ifnet *ifp);
85 static int ng_eiface_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data);
87 static void ng_eiface_print_ioctl(struct ifnet *ifp, int cmd, caddr_t data);
90 /* Netgraph methods */
91 static int ng_eiface_mod_event(module_t, int, void *);
92 static ng_constructor_t ng_eiface_constructor;
93 static ng_rcvmsg_t ng_eiface_rcvmsg;
94 static ng_shutdown_t ng_eiface_rmnode;
95 static ng_newhook_t ng_eiface_newhook;
96 static ng_rcvdata_t ng_eiface_rcvdata;
97 static ng_disconnect_t ng_eiface_disconnect;
99 /* Node type descriptor */
100 static struct ng_type typestruct = {
101 .version = NG_ABI_VERSION,
102 .name = NG_EIFACE_NODE_TYPE,
103 .mod_event = ng_eiface_mod_event,
104 .constructor = ng_eiface_constructor,
105 .rcvmsg = ng_eiface_rcvmsg,
106 .shutdown = ng_eiface_rmnode,
107 .newhook = ng_eiface_newhook,
108 .rcvdata = ng_eiface_rcvdata,
109 .disconnect = ng_eiface_disconnect,
110 .cmdlist = ng_eiface_cmdlist
112 NETGRAPH_INIT(eiface, &typestruct);
114 static struct unrhdr *ng_eiface_unit;
116 /************************************************************************
118 ************************************************************************/
121 * Process an ioctl for the virtual interface
124 ng_eiface_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
126 struct ifreq *const ifr = (struct ifreq *)data;
130 ng_eiface_print_ioctl(ifp, command, data);
135 /* These two are mostly handled at a higher layer */
137 error = ether_ioctl(ifp, command, data);
145 * If the interface is marked up and stopped, then start it.
146 * If it is marked down and running, then stop it.
148 if (ifp->if_flags & IFF_UP) {
149 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
150 ifp->if_drv_flags &= ~(IFF_DRV_OACTIVE);
151 ifp->if_drv_flags |= IFF_DRV_RUNNING;
154 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
155 ifp->if_drv_flags &= ~(IFF_DRV_RUNNING |
160 /* Set the interface MTU */
162 if (ifr->ifr_mtu > NG_EIFACE_MTU_MAX ||
163 ifr->ifr_mtu < NG_EIFACE_MTU_MIN)
166 ifp->if_mtu = ifr->ifr_mtu;
169 /* Stuff that's not supported */
187 ng_eiface_init(void *xsc)
190 struct ifnet *ifp = sc->ifp;
195 ifp->if_drv_flags |= IFF_DRV_RUNNING;
196 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
202 * We simply relay the packet to the "ether" hook, if it is connected.
203 * We have been through the netgraph locking and are guaranteed to
204 * be the only code running in this node at this time.
207 ng_eiface_start2(node_p node, hook_p hook, void *arg1, int arg2)
209 struct ifnet *ifp = arg1;
210 const priv_p priv = (priv_p)ifp->if_softc;
214 /* Check interface flags */
216 if (!((ifp->if_flags & IFF_UP) &&
217 (ifp->if_drv_flags & IFF_DRV_RUNNING)))
222 * Grab a packet to transmit.
224 IF_DEQUEUE(&ifp->if_snd, m);
226 /* If there's nothing to send, break. */
231 * Berkeley packet filter.
232 * Pass packet to bpf if there is a listener.
233 * XXX is this safe? locking?
237 if (ifp->if_flags & IFF_MONITOR) {
244 * Send packet; if hook is not connected, mbuf will get
247 NG_SEND_DATA_ONLY(error, priv->ether, m);
256 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
262 * This routine is called to deliver a packet out the interface.
263 * We simply queue the netgraph version to be called when netgraph locking
264 * allows it to happen.
265 * Until we know what the rest of the networking code is doing for
266 * locking, we don't know how we will interact with it.
267 * Take comfort from the fact that the ifnet struct is part of our
268 * private info and can't go away while we are queued.
269 * [Though we don't know it is still there now....]
270 * it is possible we don't gain anything from this because
271 * we would like to get the mbuf and queue it as data
272 * somehow, but we can't and if we did would we solve anything?
275 ng_eiface_start(struct ifnet *ifp)
278 const priv_p priv = (priv_p)ifp->if_softc;
280 /* Don't do anything if output is active */
281 if (ifp->if_drv_flags & IFF_DRV_OACTIVE)
284 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
286 if (ng_send_fn(priv->node, NULL, &ng_eiface_start2, ifp, 0) != 0)
287 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
292 * Display an ioctl to the virtual interface
296 ng_eiface_print_ioctl(struct ifnet *ifp, int command, caddr_t data)
300 switch (command & IOC_DIRMASK) {
316 log(LOG_DEBUG, "%s: %s('%c', %d, char[%d])\n",
321 IOCPARM_LEN(command));
325 /************************************************************************
327 ************************************************************************/
330 * Constructor for a node
333 ng_eiface_constructor(node_p node)
337 u_char eaddr[6] = {0,0,0,0,0,0};
339 /* Allocate node and interface private structures */
340 MALLOC(priv, priv_p, sizeof(*priv), M_NETGRAPH, M_NOWAIT | M_ZERO);
344 ifp = priv->ifp = if_alloc(IFT_ETHER);
346 free(priv, M_NETGRAPH);
350 /* Link them together */
351 ifp->if_softc = priv;
353 /* Get an interface unit number */
354 priv->unit = alloc_unr(ng_eiface_unit);
356 /* Link together node and private info */
357 NG_NODE_SET_PRIVATE(node, priv);
360 /* Initialize interface structure */
361 if_initname(ifp, NG_EIFACE_EIFACE_NAME, priv->unit);
362 ifp->if_init = ng_eiface_init;
363 ifp->if_output = ether_output;
364 ifp->if_start = ng_eiface_start;
365 ifp->if_ioctl = ng_eiface_ioctl;
366 ifp->if_watchdog = NULL;
367 ifp->if_snd.ifq_maxlen = IFQ_MAXLEN;
368 ifp->if_flags = (IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST);
371 /* Give this node name */
372 bzero(ifname, sizeof(ifname));
373 sprintf(ifname, "if%s", ifp->if_xname);
374 (void)ng_name_node(node, ifname);
377 /* Attach the interface */
378 ether_ifattach(ifp, eaddr);
385 * Give our ok for a hook to be added
388 ng_eiface_newhook(node_p node, hook_p hook, const char *name)
390 priv_p priv = NG_NODE_PRIVATE(node);
391 struct ifnet *ifp = priv->ifp;
393 if (strcmp(name, NG_EIFACE_HOOK_ETHER))
394 return (EPFNOSUPPORT);
395 if (priv->ether != NULL)
398 NG_HOOK_SET_PRIVATE(hook, &priv->ether);
400 if_link_state_change(ifp, LINK_STATE_UP);
406 * Receive a control message
409 ng_eiface_rcvmsg(node_p node, item_p item, hook_p lasthook)
411 const priv_p priv = NG_NODE_PRIVATE(node);
412 struct ifnet *const ifp = priv->ifp;
413 struct ng_mesg *resp = NULL;
417 NGI_GET_MSG(item, msg);
418 switch (msg->header.typecookie) {
419 case NGM_EIFACE_COOKIE:
420 switch (msg->header.cmd) {
424 if (msg->header.arglen != ETHER_ADDR_LEN) {
428 error = if_setlladdr(priv->ifp,
429 (u_char *)msg->data, ETHER_ADDR_LEN);
433 case NGM_EIFACE_GET_IFNAME:
434 NG_MKRESPONSE(resp, msg, IFNAMSIZ, M_NOWAIT);
439 strlcpy(resp->data, ifp->if_xname, IFNAMSIZ);
442 case NGM_EIFACE_GET_IFADDRS:
448 #define SA_SIZE(s) ((s)->sa_len<sizeof(*(s))? sizeof(*(s)):(s)->sa_len)
450 /* Determine size of response and allocate it */
452 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
453 buflen += SA_SIZE(ifa->ifa_addr);
454 NG_MKRESPONSE(resp, msg, buflen, M_NOWAIT);
462 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
463 const int len = SA_SIZE(ifa->ifa_addr);
466 log(LOG_ERR, "%s: len changed?\n",
470 bcopy(ifa->ifa_addr, ptr, len);
481 } /* end of inner switch() */
483 case NGM_FLOW_COOKIE:
484 switch (msg->header.cmd) {
486 if_link_state_change(ifp, LINK_STATE_UP);
488 case NGM_LINK_IS_DOWN:
489 if_link_state_change(ifp, LINK_STATE_DOWN);
499 NG_RESPOND_MSG(error, node, item, resp);
505 * Receive data from a hook. Pass the packet to the ether_input routine.
508 ng_eiface_rcvdata(hook_p hook, item_p item)
510 const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
511 struct ifnet *const ifp = priv->ifp;
517 if (!((ifp->if_flags & IFF_UP) &&
518 (ifp->if_drv_flags & IFF_DRV_RUNNING))) {
523 if (m->m_len < ETHER_HDR_LEN) {
524 m = m_pullup(m, ETHER_HDR_LEN);
529 /* Note receiving interface */
530 m->m_pkthdr.rcvif = ifp;
532 /* Update interface stats */
535 (*ifp->if_input)(ifp, m);
542 * Shutdown processing.
545 ng_eiface_rmnode(node_p node)
547 const priv_p priv = NG_NODE_PRIVATE(node);
548 struct ifnet *const ifp = priv->ifp;
552 free_unr(ng_eiface_unit, priv->unit);
553 FREE(priv, M_NETGRAPH);
554 NG_NODE_SET_PRIVATE(node, NULL);
563 ng_eiface_disconnect(hook_p hook)
565 const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
572 * Handle loading and unloading for this node type.
575 ng_eiface_mod_event(module_t mod, int event, void *data)
581 ng_eiface_unit = new_unrhdr(0, 0xffff, NULL);
584 delete_unrhdr(ng_eiface_unit);