4 * Copyright (c) 1999-2000, Vitaly V Belekhov
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice unmodified, this list of conditions, and the following
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * $Id: ng_eiface.c,v 1.14 2000/03/15 12:28:44 vitaly Exp $
30 * $FreeBSD: src/sys/netgraph/ng_eiface.c,v 1.4.2.5 2002/12/17 21:47:48 julian Exp $
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/errno.h>
36 #include <sys/kernel.h>
37 #include <sys/malloc.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_types.h>
46 #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_parse_struct_field ng_eiface_par_fields[]
59 = NG_EIFACE_PAR_FIELDS;
61 static const struct ng_parse_type ng_eiface_par_type = {
62 &ng_parse_struct_type,
66 static const struct ng_cmdlist ng_eiface_cmdlist[] = {
77 /* Node private data */
78 struct ng_eiface_private {
79 struct arpcom arpcom; /* per-interface network data */
80 struct ifnet *ifp; /* This interface */
81 node_p node; /* Our netgraph node */
82 hook_p ether; /* Hook for ethernet stream */
83 struct private *next; /* When hung on the free list */
85 typedef struct ng_eiface_private *priv_p;
87 /* Interface methods */
88 static void ng_eiface_init(void *xsc);
89 static void ng_eiface_start(struct ifnet *ifp);
90 static int ng_eiface_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data);
92 static void ng_eiface_print_ioctl(struct ifnet *ifp, int cmd, caddr_t data);
95 /* Netgraph methods */
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 = {
109 ng_eiface_constructor,
117 ng_eiface_disconnect,
120 NETGRAPH_INIT(eiface, &typestruct);
122 static char ng_eiface_ifname[] = NG_EIFACE_EIFACE_NAME;
123 static int ng_eiface_next_unit;
125 /************************************************************************
127 ************************************************************************/
130 * Process an ioctl for the virtual interface
133 ng_eiface_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
135 struct ifreq *const ifr = (struct ifreq *) data;
139 ng_eiface_print_ioctl(ifp, command, data);
144 /* These two are mostly handled at a higher layer */
146 error = ether_ioctl(ifp, command, data);
154 * If the interface is marked up and stopped, then start it.
155 * If it is marked down and running, then stop it.
157 if (ifr->ifr_flags & IFF_UP) {
158 if (!(ifp->if_flags & IFF_RUNNING)) {
159 ifp->if_flags &= ~(IFF_OACTIVE);
160 ifp->if_flags |= IFF_RUNNING;
163 if (ifp->if_flags & IFF_RUNNING)
164 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
168 /* Set the interface MTU */
170 if (ifr->ifr_mtu > NG_EIFACE_MTU_MAX
171 || ifr->ifr_mtu < NG_EIFACE_MTU_MIN)
174 ifp->if_mtu = ifr->ifr_mtu;
177 /* Stuff that's not supported */
195 ng_eiface_init(void *xsc)
198 struct ifnet *ifp = sc->ifp;
203 ifp->if_flags |= IFF_RUNNING;
204 ifp->if_flags &= ~IFF_OACTIVE;
211 * This routine is called to deliver a packet out the interface.
212 * We simply relay the packet to
213 * the ether hook, if it is connected.
217 ng_eiface_start(struct ifnet *ifp)
219 const priv_p priv = (priv_p) ifp->if_softc;
224 /* Check interface flags */
225 if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
228 /* Don't do anything if output is active */
229 if( ifp->if_flags & IFF_OACTIVE )
232 ifp->if_flags |= IFF_OACTIVE;
235 * Grab a packet to transmit.
237 IF_DEQUEUE(&ifp->if_snd, m);
239 /* If there's nothing to send, return. */
242 ifp->if_flags &= ~IFF_OACTIVE;
246 /* Berkeley packet filter */
248 * Pass packet to bpf if there is a listener.
253 /* Copy length before the mbuf gets invalidated */
254 len = m->m_pkthdr.len;
256 /* Send packet; if hook is not connected, mbuf will get freed. */
257 NG_SEND_DATA(error, priv->ether, m, meta);
261 ifp->if_obytes += len;
265 ifp->if_flags &= ~IFF_OACTIVE;
272 * Display an ioctl to the virtual interface
276 ng_eiface_print_ioctl(struct ifnet *ifp, int command, caddr_t data)
280 switch (command & IOC_DIRMASK) {
296 log(LOG_DEBUG, "%s%d: %s('%c', %d, char[%d])\n",
297 ifp->if_name, ifp->if_unit,
301 IOCPARM_LEN(command));
305 /************************************************************************
307 ************************************************************************/
310 * Constructor for a node
313 ng_eiface_constructor(node_p *nodep)
320 /* Allocate node and interface private structures */
321 MALLOC(priv, priv_p, sizeof(*priv), M_NETGRAPH, M_WAITOK);
324 bzero(priv, sizeof(*priv));
326 ifp = &(priv->arpcom.ac_if);
328 /* Link them together */
329 ifp->if_softc = priv;
332 /* Call generic node constructor */
333 if ((error = ng_make_node_common(&typestruct, nodep))) {
334 FREE(priv, M_NETGRAPH);
339 /* Link together node and private info */
340 node->private = priv;
343 /* Initialize interface structure */
344 ifp->if_name = ng_eiface_ifname;
345 ifp->if_unit = ng_eiface_next_unit++;
346 ifp->if_init = ng_eiface_init;
347 ifp->if_output = ether_output;
348 ifp->if_start = ng_eiface_start;
349 ifp->if_ioctl = ng_eiface_ioctl;
350 ifp->if_watchdog = NULL;
351 ifp->if_snd.ifq_maxlen = IFQ_MAXLEN;
352 ifp->if_flags = (IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST);
354 TAILQ_INIT(&ifp->if_addrhead);
356 /* Give this node name *
357 bzero(ifname, sizeof(ifname));
358 sprintf(ifname, "if%s%d", ifp->if_name, ifp->if_unit);
359 (void) ng_name_node(node, ifname);
362 /* Attach the interface */
363 ether_ifattach(ifp, ETHER_BPF_SUPPORTED);
370 * Give our ok for a hook to be added
373 ng_eiface_newhook(node_p node, hook_p hook, const char *name)
375 priv_p priv = node->private;
377 if (strcmp(name, NG_EIFACE_HOOK_ETHER))
378 return (EPFNOSUPPORT);
379 if (priv->ether != NULL)
382 hook->private = &priv->ether;
388 * Receive a control message
391 ng_eiface_rcvmsg(node_p node, struct ng_mesg *msg,
392 const char *retaddr, struct ng_mesg **rptr)
394 const priv_p priv = node->private;
395 struct ifnet *const ifp = priv->ifp;
396 struct ng_mesg *resp = NULL;
399 switch (msg->header.typecookie) {
400 case NGM_EIFACE_COOKIE:
401 switch (msg->header.cmd) {
405 struct ng_eiface_par *eaddr;
407 if (msg->header.arglen != sizeof(struct ng_eiface_par))
412 eaddr = (struct ng_eiface_par *)(msg->data);
414 priv->arpcom.ac_enaddr[0] = eaddr->oct0;
415 priv->arpcom.ac_enaddr[1] = eaddr->oct1;
416 priv->arpcom.ac_enaddr[2] = eaddr->oct2;
417 priv->arpcom.ac_enaddr[3] = eaddr->oct3;
418 priv->arpcom.ac_enaddr[4] = eaddr->oct4;
419 priv->arpcom.ac_enaddr[5] = eaddr->oct5;
424 case NGM_EIFACE_GET_IFNAME:
426 struct ng_eiface_ifname *arg;
428 NG_MKRESPONSE(resp, msg, sizeof(*arg), M_NOWAIT);
433 arg = (struct ng_eiface_ifname *) resp->data;
434 sprintf(arg->ngif_name,
435 "%s%d", ifp->if_name, ifp->if_unit);
439 case NGM_EIFACE_GET_IFADDRS:
445 #define SA_SIZE(s) ((s)->sa_len<sizeof(*(s))? sizeof(*(s)):(s)->sa_len)
447 /* Determine size of response and allocate it */
449 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
450 buflen += SA_SIZE(ifa->ifa_addr);
451 NG_MKRESPONSE(resp, msg, buflen, M_NOWAIT);
459 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
460 const int len = SA_SIZE(ifa->ifa_addr);
463 log(LOG_ERR, "%s%d: len changed?\n",
464 ifp->if_name, ifp->if_unit);
467 bcopy(ifa->ifa_addr, ptr, len);
487 FREE(resp, M_NETGRAPH);
488 FREE(msg, M_NETGRAPH);
493 * Recive data from a hook. Pass the packet to the ether_input routine.
496 ng_eiface_rcvdata(hook_p hook, struct mbuf *m, meta_p meta)
498 const priv_p priv = hook->node->private;
499 struct ifnet *const ifp = priv->ifp;
501 struct ether_header *eh;
504 /* Meta-data is end its life here... */
509 printf("ng_eiface: mbuf is null.\n");
513 if ( !(ifp->if_flags & IFF_UP) ) {
517 /* Note receiving interface */
518 m->m_pkthdr.rcvif = ifp;
520 /* Update interface stats */
523 /* Berkeley packet filter */
527 eh = mtod( m, struct ether_header * );
528 ether_type = ntohs(eh->ether_type);
531 m->m_pkthdr.len -= sizeof(*eh);
532 m->m_len -= sizeof(*eh);
535 m->m_data += sizeof(*eh);
539 if ( ether_type == ETHERTYPE_ARP )
541 m->m_len = m->m_next->m_len;
542 m->m_data = m->m_next->m_data;
547 ether_input(ifp, eh, m);
554 * Because the BSD networking code doesn't support the removal of
555 * networking interfaces, iface nodes (once created) are persistent.
556 * So this method breaks all connections and marks the interface
557 * down, but does not remove the node.
560 ng_eiface_rmnode(node_p node)
562 const priv_p priv = node->private;
563 struct ifnet *const ifp = priv->ifp;
566 node->flags &= ~NG_INVALID;
567 ifp->if_flags &= ~(IFF_UP | IFF_RUNNING | IFF_OACTIVE);
573 * This is called once we've already connected a new hook to the other node.
574 * It gives us a chance to balk at the last minute.
577 ng_eiface_connect(hook_p hook)
579 /* be really amiable and just say "YUP that's OK by me! " */
587 ng_eiface_disconnect(hook_p hook)
589 const priv_p priv = hook->node->private;