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>
36 #include <sys/malloc.h>
38 #include <sys/mutex.h>
39 #include <sys/errno.h>
40 #include <sys/sockio.h>
41 #include <sys/socket.h>
42 #include <sys/syslog.h>
45 #include <net/if_dl.h>
46 #include <net/if_types.h>
47 #include <net/netisr.h>
49 #include <netgraph/ng_message.h>
50 #include <netgraph/netgraph.h>
51 #include <netgraph/ng_parse.h>
52 #include <netgraph/ng_eiface.h>
55 #include <net/ethernet.h>
56 #include <net/if_arp.h>
58 static const struct ng_cmdlist ng_eiface_cmdlist[] = {
61 NGM_EIFACE_GET_IFNAME,
70 &ng_parse_enaddr_type,
76 /* Node private data */
77 struct ng_eiface_private {
78 struct arpcom arpcom; /* per-interface network data */
79 struct ifnet *ifp; /* This interface */
80 int unit; /* Interface unit number */
81 node_p node; /* Our netgraph node */
82 hook_p ether; /* Hook for ethernet stream */
84 typedef struct ng_eiface_private *priv_p;
86 /* Interface methods */
87 static void ng_eiface_init(void *xsc);
88 static void ng_eiface_start(struct ifnet *ifp);
89 static int ng_eiface_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data);
91 static void ng_eiface_print_ioctl(struct ifnet *ifp, int cmd, caddr_t data);
94 /* Netgraph methods */
95 static int ng_eiface_mod_event(module_t, int, void *);
96 static ng_constructor_t ng_eiface_constructor;
97 static ng_rcvmsg_t ng_eiface_rcvmsg;
98 static ng_shutdown_t ng_eiface_rmnode;
99 static ng_newhook_t ng_eiface_newhook;
100 static ng_rcvdata_t ng_eiface_rcvdata;
101 static ng_connect_t ng_eiface_connect;
102 static ng_disconnect_t ng_eiface_disconnect;
104 /* Node type descriptor */
105 static struct ng_type typestruct = {
106 .version = NG_ABI_VERSION,
107 .name = NG_EIFACE_NODE_TYPE,
108 .mod_event = ng_eiface_mod_event,
109 .constructor = ng_eiface_constructor,
110 .rcvmsg = ng_eiface_rcvmsg,
111 .shutdown = ng_eiface_rmnode,
112 .newhook = ng_eiface_newhook,
113 .connect = ng_eiface_connect,
114 .rcvdata = ng_eiface_rcvdata,
115 .disconnect = ng_eiface_disconnect,
116 .cmdlist = ng_eiface_cmdlist
118 NETGRAPH_INIT(eiface, &typestruct);
120 /* We keep a bitmap indicating which unit numbers are free.
121 One means the unit number is free, zero means it's taken. */
122 static int *ng_eiface_units = NULL;
123 static int ng_eiface_units_len = 0;
124 static int ng_units_in_use = 0;
126 #define UNITS_BITSPERWORD (sizeof(*ng_eiface_units) * NBBY)
128 static struct mtx ng_eiface_mtx;
130 /************************************************************************
132 ************************************************************************/
134 * Find the first free unit number for a new interface.
135 * Increase the size of the unit bitmap as necessary.
138 ng_eiface_get_unit(int *unit)
142 mtx_lock(&ng_eiface_mtx);
143 for (index = 0; index < ng_eiface_units_len
144 && ng_eiface_units[index] == 0; index++);
145 if (index == ng_eiface_units_len) { /* extend array */
146 int i, *newarray, newlen;
148 newlen = (2 * ng_eiface_units_len) + 4;
149 MALLOC(newarray, int *, newlen * sizeof(*ng_eiface_units),
150 M_NETGRAPH, M_NOWAIT);
151 if (newarray == NULL) {
152 mtx_unlock(&ng_eiface_mtx);
155 bcopy(ng_eiface_units, newarray,
156 ng_eiface_units_len * sizeof(*ng_eiface_units));
157 for (i = ng_eiface_units_len; i < newlen; i++)
159 if (ng_eiface_units != NULL)
160 FREE(ng_eiface_units, M_NETGRAPH);
161 ng_eiface_units = newarray;
162 ng_eiface_units_len = newlen;
164 bit = ffs(ng_eiface_units[index]) - 1;
165 KASSERT(bit >= 0 && bit <= UNITS_BITSPERWORD - 1,
166 ("%s: word=%d bit=%d", __func__, ng_eiface_units[index], bit));
167 ng_eiface_units[index] &= ~(1 << bit);
168 *unit = (index * UNITS_BITSPERWORD) + bit;
170 mtx_unlock(&ng_eiface_mtx);
175 * Free a no longer needed unit number.
178 ng_eiface_free_unit(int unit)
182 index = unit / UNITS_BITSPERWORD;
183 bit = unit % UNITS_BITSPERWORD;
184 mtx_lock(&ng_eiface_mtx);
185 KASSERT(index < ng_eiface_units_len,
186 ("%s: unit=%d len=%d", __func__, unit, ng_eiface_units_len));
187 KASSERT((ng_eiface_units[index] & (1 << bit)) == 0,
188 ("%s: unit=%d is free", __func__, unit));
189 ng_eiface_units[index] |= (1 << bit);
191 * XXX We could think about reducing the size of ng_eiface_units[]
192 * XXX here if the last portion is all ones
193 * XXX At least free it if no more units.
194 * Needed if we are to eventually be able to unload.
197 if (ng_units_in_use == 0) { /* XXX make SMP safe */
198 FREE(ng_eiface_units, M_NETGRAPH);
199 ng_eiface_units_len = 0;
200 ng_eiface_units = NULL;
202 mtx_unlock(&ng_eiface_mtx);
205 /************************************************************************
207 ************************************************************************/
210 * Process an ioctl for the virtual interface
213 ng_eiface_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
215 struct ifreq *const ifr = (struct ifreq *)data;
219 ng_eiface_print_ioctl(ifp, command, data);
224 /* These two are mostly handled at a higher layer */
226 error = ether_ioctl(ifp, command, data);
234 * If the interface is marked up and stopped, then start it.
235 * If it is marked down and running, then stop it.
237 if (ifr->ifr_flags & IFF_UP) {
238 if (!(ifp->if_flags & IFF_RUNNING)) {
239 ifp->if_flags &= ~(IFF_OACTIVE);
240 ifp->if_flags |= IFF_RUNNING;
243 if (ifp->if_flags & IFF_RUNNING)
244 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
248 /* Set the interface MTU */
250 if (ifr->ifr_mtu > NG_EIFACE_MTU_MAX ||
251 ifr->ifr_mtu < NG_EIFACE_MTU_MIN)
254 ifp->if_mtu = ifr->ifr_mtu;
257 /* Stuff that's not supported */
275 ng_eiface_init(void *xsc)
278 struct ifnet *ifp = sc->ifp;
283 ifp->if_flags |= IFF_RUNNING;
284 ifp->if_flags &= ~IFF_OACTIVE;
290 * We simply relay the packet to the "ether" hook, if it is connected.
291 * We have been through the netgraph locking and are guaranteed to
292 * be the only code running in this node at this time.
295 ng_eiface_start2(node_p node, hook_p hook, void *arg1, int arg2)
297 struct ifnet *ifp = arg1;
298 const priv_p priv = (priv_p)ifp->if_softc;
302 /* Check interface flags */
303 if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) != (IFF_UP | IFF_RUNNING))
306 /* Don't do anything if output is active */
307 if (ifp->if_flags & IFF_OACTIVE)
310 ifp->if_flags |= IFF_OACTIVE;
313 * Grab a packet to transmit.
315 IF_DEQUEUE(&ifp->if_snd, m);
317 /* If there's nothing to send, return. */
319 ifp->if_flags &= ~IFF_OACTIVE;
324 * Berkeley packet filter.
325 * Pass packet to bpf if there is a listener.
326 * XXX is this safe? locking?
330 /* Copy length before the mbuf gets invalidated */
331 len = m->m_pkthdr.len;
334 * Send packet; if hook is not connected, mbuf will get
337 NG_SEND_DATA_ONLY(error, priv->ether, m);
341 ifp->if_obytes += len;
345 ifp->if_flags &= ~IFF_OACTIVE;
351 * This routine is called to deliver a packet out the interface.
352 * We simply queue the netgraph version to be called when netgraph locking
353 * allows it to happen.
354 * Until we know what the rest of the networking code is doing for
355 * locking, we don't know how we will interact with it.
356 * Take comfort from the fact that the ifnet struct is part of our
357 * private info and can't go away while we are queued.
358 * [Though we don't know it is still there now....]
359 * it is possible we don't gain anything from this because
360 * we would like to get the mbuf and queue it as data
361 * somehow, but we can't and if we did would we solve anything?
364 ng_eiface_start(struct ifnet *ifp)
367 const priv_p priv = (priv_p)ifp->if_softc;
369 ng_send_fn(priv->node, NULL, &ng_eiface_start2, ifp, 0);
374 * Display an ioctl to the virtual interface
378 ng_eiface_print_ioctl(struct ifnet *ifp, int command, caddr_t data)
382 switch (command & IOC_DIRMASK) {
398 log(LOG_DEBUG, "%s: %s('%c', %d, char[%d])\n",
403 IOCPARM_LEN(command));
407 /************************************************************************
409 ************************************************************************/
412 * Constructor for a node
415 ng_eiface_constructor(node_p node)
421 /* Allocate node and interface private structures */
422 MALLOC(priv, priv_p, sizeof(*priv), M_NETGRAPH, M_NOWAIT | M_ZERO);
426 ifp = &(priv->arpcom.ac_if);
428 /* Link them together */
429 ifp->if_softc = priv;
432 /* Get an interface unit number */
433 if ((error = ng_eiface_get_unit(&priv->unit)) != 0) {
434 FREE(priv, M_NETGRAPH);
438 /* Link together node and private info */
439 NG_NODE_SET_PRIVATE(node, priv);
442 /* Initialize interface structure */
443 if_initname(ifp, NG_EIFACE_EIFACE_NAME, priv->unit);
444 ifp->if_init = ng_eiface_init;
445 ifp->if_output = ether_output;
446 ifp->if_start = ng_eiface_start;
447 ifp->if_ioctl = ng_eiface_ioctl;
448 ifp->if_watchdog = NULL;
449 ifp->if_snd.ifq_maxlen = IFQ_MAXLEN;
450 ifp->if_flags = (IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST);
453 /* Give this node name */
454 bzero(ifname, sizeof(ifname));
455 sprintf(ifname, "if%s", ifp->if_xname);
456 (void)ng_name_node(node, ifname);
459 /* Attach the interface */
460 ether_ifattach(ifp, priv->arpcom.ac_enaddr);
467 * Give our ok for a hook to be added
470 ng_eiface_newhook(node_p node, hook_p hook, const char *name)
472 priv_p priv = NG_NODE_PRIVATE(node);
474 if (strcmp(name, NG_EIFACE_HOOK_ETHER))
475 return (EPFNOSUPPORT);
476 if (priv->ether != NULL)
479 NG_HOOK_SET_PRIVATE(hook, &priv->ether);
485 * Receive a control message
488 ng_eiface_rcvmsg(node_p node, item_p item, hook_p lasthook)
490 const priv_p priv = NG_NODE_PRIVATE(node);
491 struct ifnet *const ifp = priv->ifp;
492 struct ng_mesg *resp = NULL;
496 NGI_GET_MSG(item, msg);
497 switch (msg->header.typecookie) {
498 case NGM_EIFACE_COOKIE:
499 switch (msg->header.cmd) {
503 struct ether_addr *eaddr;
505 struct sockaddr_dl *sdl;
507 if (msg->header.arglen != sizeof(struct ether_addr)) {
511 eaddr = (struct ether_addr *)(msg->data);
512 bcopy(eaddr, priv->arpcom.ac_enaddr, ETHER_ADDR_LEN);
514 /* And put it in the ifaddr list */
515 TAILQ_FOREACH(ifa, &(ifp->if_addrhead), ifa_link) {
516 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
517 if (sdl->sdl_type == IFT_ETHER) {
518 bcopy((IFP2AC(ifp))->ac_enaddr,
519 LLADDR(sdl), ifp->if_addrlen);
526 case NGM_EIFACE_GET_IFNAME:
527 NG_MKRESPONSE(resp, msg, IFNAMSIZ, M_NOWAIT);
532 strlcpy(resp->data, ifp->if_xname, IFNAMSIZ);
535 case NGM_EIFACE_GET_IFADDRS:
541 #define SA_SIZE(s) ((s)->sa_len<sizeof(*(s))? sizeof(*(s)):(s)->sa_len)
543 /* Determine size of response and allocate it */
545 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
546 buflen += SA_SIZE(ifa->ifa_addr);
547 NG_MKRESPONSE(resp, msg, buflen, M_NOWAIT);
555 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
556 const int len = SA_SIZE(ifa->ifa_addr);
559 log(LOG_ERR, "%s: len changed?\n",
563 bcopy(ifa->ifa_addr, ptr, len);
574 } /* end of inner switch() */
576 case NGM_FLOW_COOKIE:
577 switch (msg->header.cmd) {
579 ifp->if_flags |= IFF_RUNNING;
581 case NGM_LINK_IS_DOWN:
582 ifp->if_flags &= ~IFF_RUNNING;
592 NG_RESPOND_MSG(error, node, item, resp);
598 * Receive data from a hook. Pass the packet to the ether_input routine.
601 ng_eiface_rcvdata(hook_p hook, item_p item)
603 const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
604 struct ifnet *const ifp = priv->ifp;
610 if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) !=
611 (IFF_UP | IFF_RUNNING)) {
616 /* Note receiving interface */
617 m->m_pkthdr.rcvif = ifp;
619 /* Update interface stats */
622 (*ifp->if_input)(ifp, m);
629 * Shutdown processing.
632 ng_eiface_rmnode(node_p node)
634 const priv_p priv = NG_NODE_PRIVATE(node);
635 struct ifnet *const ifp = priv->ifp;
638 ng_eiface_free_unit(priv->unit);
639 FREE(priv, M_NETGRAPH);
640 NG_NODE_SET_PRIVATE(node, NULL);
647 * This is called once we've already connected a new hook to the other node.
648 * It gives us a chance to balk at the last minute.
651 ng_eiface_connect(hook_p hook)
653 /* be really amiable and just say "YUP that's OK by me! " */
661 ng_eiface_disconnect(hook_p hook)
663 const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
670 * Handle loading and unloading for this node type.
673 ng_eiface_mod_event(module_t mod, int event, void *data)
679 mtx_init(&ng_eiface_mtx, "ng_eiface", NULL, MTX_DEF);
682 mtx_destroy(&ng_eiface_mtx);