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>
39 #include <sys/sockio.h>
40 #include <sys/socket.h>
41 #include <sys/syslog.h>
44 #include <net/if_media.h>
45 #include <net/if_types.h>
46 #include <net/netisr.h>
47 #include <net/route.h>
50 #include <netgraph/ng_message.h>
51 #include <netgraph/netgraph.h>
52 #include <netgraph/ng_parse.h>
53 #include <netgraph/ng_eiface.h>
56 #include <net/ethernet.h>
57 #include <net/if_arp.h>
59 static const struct ng_cmdlist ng_eiface_cmdlist[] = {
62 NGM_EIFACE_GET_IFNAME,
71 &ng_parse_enaddr_type,
77 /* Node private data */
78 struct ng_eiface_private {
79 struct ifnet *ifp; /* per-interface network data */
80 struct ifmedia media; /* (fake) media information */
81 int link_status; /* fake */
82 int unit; /* Interface unit number */
83 node_p node; /* Our netgraph node */
84 hook_p ether; /* Hook for ethernet stream */
86 typedef struct ng_eiface_private *priv_p;
88 /* Interface methods */
89 static void ng_eiface_init(void *xsc);
90 static void ng_eiface_start(struct ifnet *ifp);
91 static int ng_eiface_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data);
93 static void ng_eiface_print_ioctl(struct ifnet *ifp, int cmd, caddr_t data);
96 /* Netgraph methods */
97 static int ng_eiface_mod_event(module_t, int, void *);
98 static ng_constructor_t ng_eiface_constructor;
99 static ng_rcvmsg_t ng_eiface_rcvmsg;
100 static ng_shutdown_t ng_eiface_rmnode;
101 static ng_newhook_t ng_eiface_newhook;
102 static ng_rcvdata_t ng_eiface_rcvdata;
103 static ng_disconnect_t ng_eiface_disconnect;
105 /* Node type descriptor */
106 static struct ng_type typestruct = {
107 .version = NG_ABI_VERSION,
108 .name = NG_EIFACE_NODE_TYPE,
109 .mod_event = ng_eiface_mod_event,
110 .constructor = ng_eiface_constructor,
111 .rcvmsg = ng_eiface_rcvmsg,
112 .shutdown = ng_eiface_rmnode,
113 .newhook = ng_eiface_newhook,
114 .rcvdata = ng_eiface_rcvdata,
115 .disconnect = ng_eiface_disconnect,
116 .cmdlist = ng_eiface_cmdlist
118 NETGRAPH_INIT(eiface, &typestruct);
120 static VNET_DEFINE(struct unrhdr *, ng_eiface_unit);
121 #define V_ng_eiface_unit VNET(ng_eiface_unit)
123 /************************************************************************
125 ************************************************************************/
128 * Process an ioctl for the virtual interface
131 ng_eiface_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
133 const priv_p priv = (priv_p)ifp->if_softc;
134 struct ifreq *const ifr = (struct ifreq *)data;
138 ng_eiface_print_ioctl(ifp, command, data);
142 /* These two are mostly handled at a higher layer */
144 error = ether_ioctl(ifp, command, data);
152 * If the interface is marked up and stopped, then start it.
153 * If it is marked down and running, then stop it.
155 if (ifp->if_flags & IFF_UP) {
156 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
157 ifp->if_drv_flags &= ~(IFF_DRV_OACTIVE);
158 ifp->if_drv_flags |= IFF_DRV_RUNNING;
161 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
162 ifp->if_drv_flags &= ~(IFF_DRV_RUNNING |
167 /* Set the interface MTU */
169 if (ifr->ifr_mtu > NG_EIFACE_MTU_MAX ||
170 ifr->ifr_mtu < NG_EIFACE_MTU_MIN)
173 ifp->if_mtu = ifr->ifr_mtu;
176 /* (Fake) media type manipulation */
179 error = ifmedia_ioctl(ifp, ifr, &priv->media, command);
182 /* Stuff that's not supported */
199 ng_eiface_init(void *xsc)
202 struct ifnet *ifp = sc->ifp;
204 ifp->if_drv_flags |= IFF_DRV_RUNNING;
205 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
209 * We simply relay the packet to the "ether" hook, if it is connected.
210 * We have been through the netgraph locking and are guaranteed to
211 * be the only code running in this node at this time.
214 ng_eiface_start2(node_p node, hook_p hook, void *arg1, int arg2)
216 struct ifnet *ifp = arg1;
217 const priv_p priv = (priv_p)ifp->if_softc;
221 /* Check interface flags */
223 if (!((ifp->if_flags & IFF_UP) &&
224 (ifp->if_drv_flags & IFF_DRV_RUNNING)))
229 * Grab a packet to transmit.
231 IF_DEQUEUE(&ifp->if_snd, m);
233 /* If there's nothing to send, break. */
238 * Berkeley packet filter.
239 * Pass packet to bpf if there is a listener.
240 * XXX is this safe? locking?
244 if (ifp->if_flags & IFF_MONITOR) {
251 * Send packet; if hook is not connected, mbuf will get
254 NG_OUTBOUND_THREAD_REF();
255 NG_SEND_DATA_ONLY(error, priv->ether, m);
256 NG_OUTBOUND_THREAD_UNREF();
265 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
271 * This routine is called to deliver a packet out the interface.
272 * We simply queue the netgraph version to be called when netgraph locking
273 * allows it to happen.
274 * Until we know what the rest of the networking code is doing for
275 * locking, we don't know how we will interact with it.
276 * Take comfort from the fact that the ifnet struct is part of our
277 * private info and can't go away while we are queued.
278 * [Though we don't know it is still there now....]
279 * it is possible we don't gain anything from this because
280 * we would like to get the mbuf and queue it as data
281 * somehow, but we can't and if we did would we solve anything?
284 ng_eiface_start(struct ifnet *ifp)
286 const priv_p priv = (priv_p)ifp->if_softc;
288 /* Don't do anything if output is active */
289 if (ifp->if_drv_flags & IFF_DRV_OACTIVE)
292 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
294 if (ng_send_fn(priv->node, NULL, &ng_eiface_start2, ifp, 0) != 0)
295 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
300 * Display an ioctl to the virtual interface
304 ng_eiface_print_ioctl(struct ifnet *ifp, int command, caddr_t data)
308 switch (command & IOC_DIRMASK) {
324 log(LOG_DEBUG, "%s: %s('%c', %d, char[%d])\n",
329 IOCPARM_LEN(command));
337 ng_eiface_mediachange(struct ifnet *ifp)
339 const priv_p priv = (priv_p)ifp->if_softc;
340 struct ifmedia *ifm = &priv->media;
342 if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
344 if (IFM_SUBTYPE(ifm->ifm_media) == IFM_AUTO)
345 ifp->if_baudrate = ifmedia_baudrate(IFM_ETHER | IFM_1000_T);
347 ifp->if_baudrate = ifmedia_baudrate(ifm->ifm_media);
353 ng_eiface_mediastatus(struct ifnet *ifp, struct ifmediareq *ifmr)
355 const priv_p priv = (priv_p)ifp->if_softc;
356 struct ifmedia *ifm = &priv->media;
358 if (ifm->ifm_cur->ifm_media == (IFM_ETHER | IFM_AUTO) &&
359 (priv->link_status & IFM_ACTIVE))
360 ifmr->ifm_active = IFM_ETHER | IFM_1000_T | IFM_FDX;
362 ifmr->ifm_active = ifm->ifm_cur->ifm_media;
363 ifmr->ifm_status = priv->link_status;
368 /************************************************************************
370 ************************************************************************/
373 * Constructor for a node
376 ng_eiface_constructor(node_p node)
380 u_char eaddr[6] = {0,0,0,0,0,0};
382 /* Allocate node and interface private structures */
383 priv = malloc(sizeof(*priv), M_NETGRAPH, M_WAITOK | M_ZERO);
385 ifp = priv->ifp = if_alloc(IFT_ETHER);
387 free(priv, M_NETGRAPH);
391 /* Link them together */
392 ifp->if_softc = priv;
394 /* Get an interface unit number */
395 priv->unit = alloc_unr(V_ng_eiface_unit);
397 /* Link together node and private info */
398 NG_NODE_SET_PRIVATE(node, priv);
401 /* Initialize interface structure */
402 if_initname(ifp, NG_EIFACE_EIFACE_NAME, priv->unit);
403 ifp->if_init = ng_eiface_init;
404 ifp->if_output = ether_output;
405 ifp->if_start = ng_eiface_start;
406 ifp->if_ioctl = ng_eiface_ioctl;
407 ifp->if_snd.ifq_maxlen = ifqmaxlen;
408 ifp->if_flags = (IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST);
409 ifp->if_capabilities = IFCAP_VLAN_MTU | IFCAP_JUMBO_MTU;
410 ifp->if_capenable = IFCAP_VLAN_MTU | IFCAP_JUMBO_MTU;
411 ifmedia_init(&priv->media, 0, ng_eiface_mediachange,
412 ng_eiface_mediastatus);
413 ifmedia_add(&priv->media, IFM_ETHER | IFM_10_T, 0, NULL);
414 ifmedia_add(&priv->media, IFM_ETHER | IFM_10_T | IFM_FDX, 0, NULL);
415 ifmedia_add(&priv->media, IFM_ETHER | IFM_100_TX, 0, NULL);
416 ifmedia_add(&priv->media, IFM_ETHER | IFM_100_TX | IFM_FDX, 0, NULL);
417 ifmedia_add(&priv->media, IFM_ETHER | IFM_1000_T, 0, NULL);
418 ifmedia_add(&priv->media, IFM_ETHER | IFM_1000_T | IFM_FDX, 0, NULL);
419 ifmedia_add(&priv->media, IFM_ETHER | IFM_10G_T | IFM_FDX, 0, NULL);
420 ifmedia_add(&priv->media, IFM_ETHER | IFM_AUTO, 0, NULL);
421 ifmedia_set(&priv->media, IFM_ETHER | IFM_AUTO);
422 priv->link_status = IFM_AVALID;
424 /* Give this node the same name as the interface (if possible) */
425 if (ng_name_node(node, ifp->if_xname) != 0)
426 log(LOG_WARNING, "%s: can't acquire netgraph name\n",
429 /* Attach the interface */
430 ether_ifattach(ifp, eaddr);
431 ifp->if_baudrate = ifmedia_baudrate(IFM_ETHER | IFM_1000_T);
438 * Give our ok for a hook to be added
441 ng_eiface_newhook(node_p node, hook_p hook, const char *name)
443 priv_p priv = NG_NODE_PRIVATE(node);
444 struct ifnet *ifp = priv->ifp;
446 if (strcmp(name, NG_EIFACE_HOOK_ETHER))
447 return (EPFNOSUPPORT);
448 if (priv->ether != NULL)
451 NG_HOOK_SET_PRIVATE(hook, &priv->ether);
452 NG_HOOK_SET_TO_INBOUND(hook);
454 priv->link_status |= IFM_ACTIVE;
455 CURVNET_SET_QUIET(ifp->if_vnet);
456 if_link_state_change(ifp, LINK_STATE_UP);
463 * Receive a control message
466 ng_eiface_rcvmsg(node_p node, item_p item, hook_p lasthook)
468 const priv_p priv = NG_NODE_PRIVATE(node);
469 struct ifnet *const ifp = priv->ifp;
470 struct ng_mesg *resp = NULL;
474 NGI_GET_MSG(item, msg);
475 switch (msg->header.typecookie) {
476 case NGM_EIFACE_COOKIE:
477 switch (msg->header.cmd) {
481 if (msg->header.arglen != ETHER_ADDR_LEN) {
485 error = if_setlladdr(priv->ifp,
486 (u_char *)msg->data, ETHER_ADDR_LEN);
487 EVENTHANDLER_INVOKE(iflladdr_event, priv->ifp);
491 case NGM_EIFACE_GET_IFNAME:
492 NG_MKRESPONSE(resp, msg, IFNAMSIZ, M_NOWAIT);
497 strlcpy(resp->data, ifp->if_xname, IFNAMSIZ);
500 case NGM_EIFACE_GET_IFADDRS:
506 /* Determine size of response and allocate it */
509 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
510 buflen += SA_SIZE(ifa->ifa_addr);
511 NG_MKRESPONSE(resp, msg, buflen, M_NOWAIT);
513 if_addr_runlock(ifp);
520 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
521 const int len = SA_SIZE(ifa->ifa_addr);
524 log(LOG_ERR, "%s: len changed?\n",
528 bcopy(ifa->ifa_addr, ptr, len);
532 if_addr_runlock(ifp);
539 } /* end of inner switch() */
541 case NGM_FLOW_COOKIE:
542 CURVNET_SET_QUIET(ifp->if_vnet);
543 switch (msg->header.cmd) {
545 priv->link_status |= IFM_ACTIVE;
546 if_link_state_change(ifp, LINK_STATE_UP);
548 case NGM_LINK_IS_DOWN:
549 priv->link_status &= ~IFM_ACTIVE;
550 if_link_state_change(ifp, LINK_STATE_DOWN);
561 NG_RESPOND_MSG(error, node, item, resp);
567 * Receive data from a hook. Pass the packet to the ether_input routine.
570 ng_eiface_rcvdata(hook_p hook, item_p item)
572 const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
573 struct ifnet *const ifp = priv->ifp;
579 if (!((ifp->if_flags & IFF_UP) &&
580 (ifp->if_drv_flags & IFF_DRV_RUNNING))) {
585 if (m->m_len < ETHER_HDR_LEN) {
586 m = m_pullup(m, ETHER_HDR_LEN);
591 /* Note receiving interface */
592 m->m_pkthdr.rcvif = ifp;
594 /* Update interface stats */
597 (*ifp->if_input)(ifp, m);
604 * Shutdown processing.
607 ng_eiface_rmnode(node_p node)
609 const priv_p priv = NG_NODE_PRIVATE(node);
610 struct ifnet *const ifp = priv->ifp;
613 * the ifnet may be in a different vnet than the netgraph node,
614 * hence we have to change the current vnet context here.
616 CURVNET_SET_QUIET(ifp->if_vnet);
617 ifmedia_removeall(&priv->media);
621 free_unr(V_ng_eiface_unit, priv->unit);
622 free(priv, M_NETGRAPH);
623 NG_NODE_SET_PRIVATE(node, NULL);
632 ng_eiface_disconnect(hook_p hook)
634 const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
637 priv->link_status &= ~IFM_ACTIVE;
638 CURVNET_SET_QUIET(priv->ifp->if_vnet);
639 if_link_state_change(priv->ifp, LINK_STATE_DOWN);
645 * Handle loading and unloading for this node type.
648 ng_eiface_mod_event(module_t mod, int event, void *data)
664 vnet_ng_eiface_init(const void *unused)
667 V_ng_eiface_unit = new_unrhdr(0, 0xffff, NULL);
669 VNET_SYSINIT(vnet_ng_eiface_init, SI_SUB_PSEUDO, SI_ORDER_ANY,
670 vnet_ng_eiface_init, NULL);
673 vnet_ng_eiface_uninit(const void *unused)
676 delete_unrhdr(V_ng_eiface_unit);
678 VNET_SYSUNINIT(vnet_ng_eiface_uninit, SI_SUB_PSEUDO, SI_ORDER_ANY,
679 vnet_ng_eiface_uninit, NULL);