]> CyberLeo.Net >> Repos - FreeBSD/releng/10.0.git/blob - sys/netgraph/ng_iface.c
- Copy stable/10 (r259064) to releng/10.0 as part of the
[FreeBSD/releng/10.0.git] / sys / netgraph / ng_iface.c
1 /*
2  * ng_iface.c
3  */
4
5 /*-
6  * Copyright (c) 1996-1999 Whistle Communications, Inc.
7  * All rights reserved.
8  * 
9  * Subject to the following obligations and disclaimer of warranty, use and
10  * redistribution of this software, in source or object code forms, with or
11  * without modifications are expressly permitted by Whistle Communications;
12  * provided, however, that:
13  * 1. Any and all reproductions of the source or object code must include the
14  *    copyright notice above and the following disclaimer of warranties; and
15  * 2. No rights are granted, in any manner or form, to use Whistle
16  *    Communications, Inc. trademarks, including the mark "WHISTLE
17  *    COMMUNICATIONS" on advertising, endorsements, or otherwise except as
18  *    such appears in the above copyright notice or in the software.
19  * 
20  * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
21  * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
22  * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
23  * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
24  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
25  * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
26  * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
27  * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
28  * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
29  * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
30  * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
31  * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
32  * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
33  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
35  * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
36  * OF SUCH DAMAGE.
37  *
38  * Author: Archie Cobbs <archie@freebsd.org>
39  *
40  * $FreeBSD$
41  * $Whistle: ng_iface.c,v 1.33 1999/11/01 09:24:51 julian Exp $
42  */
43
44 /*
45  * This node is also a system networking interface. It has
46  * a hook for each protocol (IP, AppleTalk, IPX, etc). Packets
47  * are simply relayed between the interface and the hooks.
48  *
49  * Interfaces are named ng0, ng1, etc.  New nodes take the
50  * first available interface name.
51  *
52  * This node also includes Berkeley packet filter support.
53  */
54
55 #include "opt_atalk.h"
56 #include "opt_inet.h"
57 #include "opt_inet6.h"
58 #include "opt_ipx.h"
59
60 #include <sys/param.h>
61 #include <sys/systm.h>
62 #include <sys/errno.h>
63 #include <sys/kernel.h>
64 #include <sys/malloc.h>
65 #include <sys/mbuf.h>
66 #include <sys/errno.h>
67 #include <sys/proc.h>
68 #include <sys/random.h>
69 #include <sys/sockio.h>
70 #include <sys/socket.h>
71 #include <sys/syslog.h>
72 #include <sys/libkern.h>
73
74 #include <net/if.h>
75 #include <net/if_types.h>
76 #include <net/bpf.h>
77 #include <net/netisr.h>
78 #include <net/route.h>
79 #include <net/vnet.h>
80
81 #include <netinet/in.h>
82
83 #include <netgraph/ng_message.h>
84 #include <netgraph/netgraph.h>
85 #include <netgraph/ng_parse.h>
86 #include <netgraph/ng_iface.h>
87 #include <netgraph/ng_cisco.h>
88
89 #ifdef NG_SEPARATE_MALLOC
90 static MALLOC_DEFINE(M_NETGRAPH_IFACE, "netgraph_iface", "netgraph iface node");
91 #else
92 #define M_NETGRAPH_IFACE M_NETGRAPH
93 #endif
94
95 /* This struct describes one address family */
96 struct iffam {
97         sa_family_t     family;         /* Address family */
98         const char      *hookname;      /* Name for hook */
99 };
100 typedef const struct iffam *iffam_p;
101
102 /* List of address families supported by our interface */
103 const static struct iffam gFamilies[] = {
104         { AF_INET,      NG_IFACE_HOOK_INET      },
105         { AF_INET6,     NG_IFACE_HOOK_INET6     },
106         { AF_APPLETALK, NG_IFACE_HOOK_ATALK     },
107         { AF_IPX,       NG_IFACE_HOOK_IPX       },
108         { AF_ATM,       NG_IFACE_HOOK_ATM       },
109         { AF_NATM,      NG_IFACE_HOOK_NATM      },
110 };
111 #define NUM_FAMILIES            (sizeof(gFamilies) / sizeof(*gFamilies))
112
113 /* Node private data */
114 struct ng_iface_private {
115         struct  ifnet *ifp;             /* Our interface */
116         int     unit;                   /* Interface unit number */
117         node_p  node;                   /* Our netgraph node */
118         hook_p  hooks[NUM_FAMILIES];    /* Hook for each address family */
119 };
120 typedef struct ng_iface_private *priv_p;
121
122 /* Interface methods */
123 static void     ng_iface_start(struct ifnet *ifp);
124 static int      ng_iface_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data);
125 static int      ng_iface_output(struct ifnet *ifp, struct mbuf *m0,
126                         const struct sockaddr *dst, struct route *ro);
127 static void     ng_iface_bpftap(struct ifnet *ifp,
128                         struct mbuf *m, sa_family_t family);
129 static int      ng_iface_send(struct ifnet *ifp, struct mbuf *m,
130                         sa_family_t sa);
131 #ifdef DEBUG
132 static void     ng_iface_print_ioctl(struct ifnet *ifp, int cmd, caddr_t data);
133 #endif
134
135 /* Netgraph methods */
136 static int              ng_iface_mod_event(module_t, int, void *);
137 static ng_constructor_t ng_iface_constructor;
138 static ng_rcvmsg_t      ng_iface_rcvmsg;
139 static ng_shutdown_t    ng_iface_shutdown;
140 static ng_newhook_t     ng_iface_newhook;
141 static ng_rcvdata_t     ng_iface_rcvdata;
142 static ng_disconnect_t  ng_iface_disconnect;
143
144 /* Helper stuff */
145 static iffam_p  get_iffam_from_af(sa_family_t family);
146 static iffam_p  get_iffam_from_hook(priv_p priv, hook_p hook);
147 static iffam_p  get_iffam_from_name(const char *name);
148 static hook_p  *get_hook_from_iffam(priv_p priv, iffam_p iffam);
149
150 /* Parse type for struct ng_cisco_ipaddr */
151 static const struct ng_parse_struct_field ng_cisco_ipaddr_type_fields[]
152         = NG_CISCO_IPADDR_TYPE_INFO;
153 static const struct ng_parse_type ng_cisco_ipaddr_type = {
154         &ng_parse_struct_type,
155         &ng_cisco_ipaddr_type_fields
156 };
157
158 /* List of commands and how to convert arguments to/from ASCII */
159 static const struct ng_cmdlist ng_iface_cmds[] = {
160         {
161           NGM_IFACE_COOKIE,
162           NGM_IFACE_GET_IFNAME,
163           "getifname",
164           NULL,
165           &ng_parse_string_type
166         },
167         {
168           NGM_IFACE_COOKIE,
169           NGM_IFACE_POINT2POINT,
170           "point2point",
171           NULL,
172           NULL
173         },
174         {
175           NGM_IFACE_COOKIE,
176           NGM_IFACE_BROADCAST,
177           "broadcast",
178           NULL,
179           NULL
180         },
181         {
182           NGM_CISCO_COOKIE,
183           NGM_CISCO_GET_IPADDR,
184           "getipaddr",
185           NULL,
186           &ng_cisco_ipaddr_type
187         },
188         {
189           NGM_IFACE_COOKIE,
190           NGM_IFACE_GET_IFINDEX,
191           "getifindex",
192           NULL,
193           &ng_parse_uint32_type
194         },
195         { 0 }
196 };
197
198 /* Node type descriptor */
199 static struct ng_type typestruct = {
200         .version =      NG_ABI_VERSION,
201         .name =         NG_IFACE_NODE_TYPE,
202         .mod_event =    ng_iface_mod_event,
203         .constructor =  ng_iface_constructor,
204         .rcvmsg =       ng_iface_rcvmsg,
205         .shutdown =     ng_iface_shutdown,
206         .newhook =      ng_iface_newhook,
207         .rcvdata =      ng_iface_rcvdata,
208         .disconnect =   ng_iface_disconnect,
209         .cmdlist =      ng_iface_cmds,
210 };
211 NETGRAPH_INIT(iface, &typestruct);
212
213 static VNET_DEFINE(struct unrhdr *, ng_iface_unit);
214 #define V_ng_iface_unit                 VNET(ng_iface_unit)
215
216 /************************************************************************
217                         HELPER STUFF
218  ************************************************************************/
219
220 /*
221  * Get the family descriptor from the family ID
222  */
223 static __inline iffam_p
224 get_iffam_from_af(sa_family_t family)
225 {
226         iffam_p iffam;
227         int k;
228
229         for (k = 0; k < NUM_FAMILIES; k++) {
230                 iffam = &gFamilies[k];
231                 if (iffam->family == family)
232                         return (iffam);
233         }
234         return (NULL);
235 }
236
237 /*
238  * Get the family descriptor from the hook
239  */
240 static __inline iffam_p
241 get_iffam_from_hook(priv_p priv, hook_p hook)
242 {
243         int k;
244
245         for (k = 0; k < NUM_FAMILIES; k++)
246                 if (priv->hooks[k] == hook)
247                         return (&gFamilies[k]);
248         return (NULL);
249 }
250
251 /*
252  * Get the hook from the iffam descriptor
253  */
254
255 static __inline hook_p *
256 get_hook_from_iffam(priv_p priv, iffam_p iffam)
257 {
258         return (&priv->hooks[iffam - gFamilies]);
259 }
260
261 /*
262  * Get the iffam descriptor from the name
263  */
264 static __inline iffam_p
265 get_iffam_from_name(const char *name)
266 {
267         iffam_p iffam;
268         int k;
269
270         for (k = 0; k < NUM_FAMILIES; k++) {
271                 iffam = &gFamilies[k];
272                 if (!strcmp(iffam->hookname, name))
273                         return (iffam);
274         }
275         return (NULL);
276 }
277
278 /************************************************************************
279                         INTERFACE STUFF
280  ************************************************************************/
281
282 /*
283  * Process an ioctl for the virtual interface
284  */
285 static int
286 ng_iface_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
287 {
288         struct ifreq *const ifr = (struct ifreq *) data;
289         int error = 0;
290
291 #ifdef DEBUG
292         ng_iface_print_ioctl(ifp, command, data);
293 #endif
294         switch (command) {
295
296         /* These two are mostly handled at a higher layer */
297         case SIOCSIFADDR:
298                 ifp->if_flags |= IFF_UP;
299                 ifp->if_drv_flags |= IFF_DRV_RUNNING;
300                 ifp->if_drv_flags &= ~(IFF_DRV_OACTIVE);
301                 break;
302         case SIOCGIFADDR:
303                 break;
304
305         /* Set flags */
306         case SIOCSIFFLAGS:
307                 /*
308                  * If the interface is marked up and stopped, then start it.
309                  * If it is marked down and running, then stop it.
310                  */
311                 if (ifr->ifr_flags & IFF_UP) {
312                         if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
313                                 ifp->if_drv_flags &= ~(IFF_DRV_OACTIVE);
314                                 ifp->if_drv_flags |= IFF_DRV_RUNNING;
315                         }
316                 } else {
317                         if (ifp->if_drv_flags & IFF_DRV_RUNNING)
318                                 ifp->if_drv_flags &= ~(IFF_DRV_RUNNING |
319                                     IFF_DRV_OACTIVE);
320                 }
321                 break;
322
323         /* Set the interface MTU */
324         case SIOCSIFMTU:
325                 if (ifr->ifr_mtu > NG_IFACE_MTU_MAX
326                     || ifr->ifr_mtu < NG_IFACE_MTU_MIN)
327                         error = EINVAL;
328                 else
329                         ifp->if_mtu = ifr->ifr_mtu;
330                 break;
331
332         /* Stuff that's not supported */
333         case SIOCADDMULTI:
334         case SIOCDELMULTI:
335                 error = 0;
336                 break;
337         case SIOCSIFPHYS:
338                 error = EOPNOTSUPP;
339                 break;
340
341         default:
342                 error = EINVAL;
343                 break;
344         }
345         return (error);
346 }
347
348 /*
349  * This routine is called to deliver a packet out the interface.
350  * We simply look at the address family and relay the packet to
351  * the corresponding hook, if it exists and is connected.
352  */
353
354 static int
355 ng_iface_output(struct ifnet *ifp, struct mbuf *m,
356         const struct sockaddr *dst, struct route *ro)
357 {
358         struct m_tag *mtag;
359         uint32_t af;
360         int error;
361
362         /* Check interface flags */
363         if (!((ifp->if_flags & IFF_UP) &&
364             (ifp->if_drv_flags & IFF_DRV_RUNNING))) {
365                 m_freem(m);
366                 return (ENETDOWN);
367         }
368
369         /* Protect from deadly infinite recursion. */
370         mtag = NULL;
371         while ((mtag = m_tag_locate(m, MTAG_NGIF, MTAG_NGIF_CALLED, mtag))) {
372                 if (*(struct ifnet **)(mtag + 1) == ifp) {
373                         log(LOG_NOTICE, "Loop detected on %s\n", ifp->if_xname);
374                         m_freem(m);
375                         return (EDEADLK);
376                 }
377         }
378         mtag = m_tag_alloc(MTAG_NGIF, MTAG_NGIF_CALLED, sizeof(struct ifnet *),
379             M_NOWAIT);
380         if (mtag == NULL) {
381                 m_freem(m);
382                 return (ENOMEM);
383         }
384         *(struct ifnet **)(mtag + 1) = ifp;
385         m_tag_prepend(m, mtag);
386
387         /* BPF writes need to be handled specially. */
388         if (dst->sa_family == AF_UNSPEC)
389                 bcopy(dst->sa_data, &af, sizeof(af));
390         else
391                 af = dst->sa_family;
392
393         /* Berkeley packet filter */
394         ng_iface_bpftap(ifp, m, af);
395
396         if (ALTQ_IS_ENABLED(&ifp->if_snd)) {
397                 M_PREPEND(m, sizeof(sa_family_t), M_NOWAIT);
398                 if (m == NULL) {
399                         IFQ_LOCK(&ifp->if_snd);
400                         IFQ_INC_DROPS(&ifp->if_snd);
401                         IFQ_UNLOCK(&ifp->if_snd);
402                         ifp->if_oerrors++;
403                         return (ENOBUFS);
404                 }
405                 *(sa_family_t *)m->m_data = af;
406                 error = (ifp->if_transmit)(ifp, m);
407         } else
408                 error = ng_iface_send(ifp, m, af);
409
410         return (error);
411 }
412
413 /*
414  * Start method is used only when ALTQ is enabled.
415  */
416 static void
417 ng_iface_start(struct ifnet *ifp)
418 {
419         struct mbuf *m;
420         sa_family_t sa;
421
422         KASSERT(ALTQ_IS_ENABLED(&ifp->if_snd), ("%s without ALTQ", __func__));
423
424         for(;;) {
425                 IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
426                 if (m == NULL)
427                         break;
428                 sa = *mtod(m, sa_family_t *);
429                 m_adj(m, sizeof(sa_family_t));
430                 ng_iface_send(ifp, m, sa);
431         }
432 }
433
434 /*
435  * Flash a packet by the BPF (requires prepending 4 byte AF header)
436  * Note the phoney mbuf; this is OK because BPF treats it read-only.
437  */
438 static void
439 ng_iface_bpftap(struct ifnet *ifp, struct mbuf *m, sa_family_t family)
440 {
441         KASSERT(family != AF_UNSPEC, ("%s: family=AF_UNSPEC", __func__));
442         if (bpf_peers_present(ifp->if_bpf)) {
443                 int32_t family4 = (int32_t)family;
444                 bpf_mtap2(ifp->if_bpf, &family4, sizeof(family4), m);
445         }
446 }
447
448 /*
449  * This routine does actual delivery of the packet into the
450  * netgraph(4). It is called from ng_iface_start() and
451  * ng_iface_output().
452  */
453 static int
454 ng_iface_send(struct ifnet *ifp, struct mbuf *m, sa_family_t sa)
455 {
456         const priv_p priv = (priv_p) ifp->if_softc;
457         const iffam_p iffam = get_iffam_from_af(sa);
458         int error;
459         int len;
460
461         /* Check address family to determine hook (if known) */
462         if (iffam == NULL) {
463                 m_freem(m);
464                 log(LOG_WARNING, "%s: can't handle af%d\n", ifp->if_xname, sa);
465                 return (EAFNOSUPPORT);
466         }
467
468         /* Copy length before the mbuf gets invalidated. */
469         len = m->m_pkthdr.len;
470
471         /* Send packet. If hook is not connected, mbuf will get freed. */
472         NG_OUTBOUND_THREAD_REF();
473         NG_SEND_DATA_ONLY(error, *get_hook_from_iffam(priv, iffam), m);
474         NG_OUTBOUND_THREAD_UNREF();
475
476         /* Update stats. */
477         if (error == 0) {
478                 ifp->if_obytes += len;
479                 ifp->if_opackets++;
480         }
481
482         return (error);
483 }
484
485 #ifdef DEBUG
486 /*
487  * Display an ioctl to the virtual interface
488  */
489
490 static void
491 ng_iface_print_ioctl(struct ifnet *ifp, int command, caddr_t data)
492 {
493         char   *str;
494
495         switch (command & IOC_DIRMASK) {
496         case IOC_VOID:
497                 str = "IO";
498                 break;
499         case IOC_OUT:
500                 str = "IOR";
501                 break;
502         case IOC_IN:
503                 str = "IOW";
504                 break;
505         case IOC_INOUT:
506                 str = "IORW";
507                 break;
508         default:
509                 str = "IO??";
510         }
511         log(LOG_DEBUG, "%s: %s('%c', %d, char[%d])\n",
512                ifp->if_xname,
513                str,
514                IOCGROUP(command),
515                command & 0xff,
516                IOCPARM_LEN(command));
517 }
518 #endif /* DEBUG */
519
520 /************************************************************************
521                         NETGRAPH NODE STUFF
522  ************************************************************************/
523
524 /*
525  * Constructor for a node
526  */
527 static int
528 ng_iface_constructor(node_p node)
529 {
530         struct ifnet *ifp;
531         priv_p priv;
532
533         /* Allocate node and interface private structures */
534         priv = malloc(sizeof(*priv), M_NETGRAPH_IFACE, M_WAITOK | M_ZERO);
535         ifp = if_alloc(IFT_PROPVIRTUAL);
536         if (ifp == NULL) {
537                 free(priv, M_NETGRAPH_IFACE);
538                 return (ENOMEM);
539         }
540
541         /* Link them together */
542         ifp->if_softc = priv;
543         priv->ifp = ifp;
544
545         /* Get an interface unit number */
546         priv->unit = alloc_unr(V_ng_iface_unit);
547
548         /* Link together node and private info */
549         NG_NODE_SET_PRIVATE(node, priv);
550         priv->node = node;
551
552         /* Initialize interface structure */
553         if_initname(ifp, NG_IFACE_IFACE_NAME, priv->unit);
554         ifp->if_output = ng_iface_output;
555         ifp->if_start = ng_iface_start;
556         ifp->if_ioctl = ng_iface_ioctl;
557         ifp->if_mtu = NG_IFACE_MTU_DEFAULT;
558         ifp->if_flags = (IFF_SIMPLEX|IFF_POINTOPOINT|IFF_NOARP|IFF_MULTICAST);
559         ifp->if_type = IFT_PROPVIRTUAL;         /* XXX */
560         ifp->if_addrlen = 0;                    /* XXX */
561         ifp->if_hdrlen = 0;                     /* XXX */
562         ifp->if_baudrate = 64000;               /* XXX */
563         IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
564         ifp->if_snd.ifq_drv_maxlen = ifqmaxlen;
565         IFQ_SET_READY(&ifp->if_snd);
566
567         /* Give this node the same name as the interface (if possible) */
568         if (ng_name_node(node, ifp->if_xname) != 0)
569                 log(LOG_WARNING, "%s: can't acquire netgraph name\n",
570                     ifp->if_xname);
571
572         /* Attach the interface */
573         if_attach(ifp);
574         bpfattach(ifp, DLT_NULL, sizeof(u_int32_t));
575
576         /* Done */
577         return (0);
578 }
579
580 /*
581  * Give our ok for a hook to be added
582  */
583 static int
584 ng_iface_newhook(node_p node, hook_p hook, const char *name)
585 {
586         const iffam_p iffam = get_iffam_from_name(name);
587         hook_p *hookptr;
588
589         if (iffam == NULL)
590                 return (EPFNOSUPPORT);
591         hookptr = get_hook_from_iffam(NG_NODE_PRIVATE(node), iffam);
592         if (*hookptr != NULL)
593                 return (EISCONN);
594         *hookptr = hook;
595         NG_HOOK_HI_STACK(hook);
596         NG_HOOK_SET_TO_INBOUND(hook);
597         return (0);
598 }
599
600 /*
601  * Receive a control message
602  */
603 static int
604 ng_iface_rcvmsg(node_p node, item_p item, hook_p lasthook)
605 {
606         const priv_p priv = NG_NODE_PRIVATE(node);
607         struct ifnet *const ifp = priv->ifp;
608         struct ng_mesg *resp = NULL;
609         int error = 0;
610         struct ng_mesg *msg;
611
612         NGI_GET_MSG(item, msg);
613         switch (msg->header.typecookie) {
614         case NGM_IFACE_COOKIE:
615                 switch (msg->header.cmd) {
616                 case NGM_IFACE_GET_IFNAME:
617                         NG_MKRESPONSE(resp, msg, IFNAMSIZ, M_NOWAIT);
618                         if (resp == NULL) {
619                                 error = ENOMEM;
620                                 break;
621                         }
622                         strlcpy(resp->data, ifp->if_xname, IFNAMSIZ);
623                         break;
624
625                 case NGM_IFACE_POINT2POINT:
626                 case NGM_IFACE_BROADCAST:
627                     {
628
629                         /* Deny request if interface is UP */
630                         if ((ifp->if_flags & IFF_UP) != 0)
631                                 return (EBUSY);
632
633                         /* Change flags */
634                         switch (msg->header.cmd) {
635                         case NGM_IFACE_POINT2POINT:
636                                 ifp->if_flags |= IFF_POINTOPOINT;
637                                 ifp->if_flags &= ~IFF_BROADCAST;
638                                 break;
639                         case NGM_IFACE_BROADCAST:
640                                 ifp->if_flags &= ~IFF_POINTOPOINT;
641                                 ifp->if_flags |= IFF_BROADCAST;
642                                 break;
643                         }
644                         break;
645                     }
646
647                 case NGM_IFACE_GET_IFINDEX:
648                         NG_MKRESPONSE(resp, msg, sizeof(uint32_t), M_NOWAIT);
649                         if (resp == NULL) {
650                                 error = ENOMEM;
651                                 break;
652                         }
653                         *((uint32_t *)resp->data) = priv->ifp->if_index;
654                         break;
655
656                 default:
657                         error = EINVAL;
658                         break;
659                 }
660                 break;
661         case NGM_CISCO_COOKIE:
662                 switch (msg->header.cmd) {
663                 case NGM_CISCO_GET_IPADDR:      /* we understand this too */
664                     {
665                         struct ifaddr *ifa;
666
667                         /* Return the first configured IP address */
668                         if_addr_rlock(ifp);
669                         TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
670                                 struct ng_cisco_ipaddr *ips;
671
672                                 if (ifa->ifa_addr->sa_family != AF_INET)
673                                         continue;
674                                 NG_MKRESPONSE(resp, msg, sizeof(ips), M_NOWAIT);
675                                 if (resp == NULL) {
676                                         error = ENOMEM;
677                                         break;
678                                 }
679                                 ips = (struct ng_cisco_ipaddr *)resp->data;
680                                 ips->ipaddr = ((struct sockaddr_in *)
681                                                 ifa->ifa_addr)->sin_addr;
682                                 ips->netmask = ((struct sockaddr_in *)
683                                                 ifa->ifa_netmask)->sin_addr;
684                                 break;
685                         }
686                         if_addr_runlock(ifp);
687
688                         /* No IP addresses on this interface? */
689                         if (ifa == NULL)
690                                 error = EADDRNOTAVAIL;
691                         break;
692                     }
693                 default:
694                         error = EINVAL;
695                         break;
696                 }
697                 break;
698         case NGM_FLOW_COOKIE:
699                 switch (msg->header.cmd) {
700                 case NGM_LINK_IS_UP:
701                         ifp->if_drv_flags |= IFF_DRV_RUNNING;
702                         break;
703                 case NGM_LINK_IS_DOWN:
704                         ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
705                         break;
706                 default:
707                         break;
708                 }
709                 break;
710         default:
711                 error = EINVAL;
712                 break;
713         }
714         NG_RESPOND_MSG(error, node, item, resp);
715         NG_FREE_MSG(msg);
716         return (error);
717 }
718
719 /*
720  * Recive data from a hook. Pass the packet to the correct input routine.
721  */
722 static int
723 ng_iface_rcvdata(hook_p hook, item_p item)
724 {
725         const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
726         const iffam_p iffam = get_iffam_from_hook(priv, hook);
727         struct ifnet *const ifp = priv->ifp;
728         struct mbuf *m;
729         int isr;
730
731         NGI_GET_M(item, m);
732         NG_FREE_ITEM(item);
733         /* Sanity checks */
734         KASSERT(iffam != NULL, ("%s: iffam", __func__));
735         M_ASSERTPKTHDR(m);
736         if ((ifp->if_flags & IFF_UP) == 0) {
737                 NG_FREE_M(m);
738                 return (ENETDOWN);
739         }
740
741         /* Update interface stats */
742         ifp->if_ipackets++;
743         ifp->if_ibytes += m->m_pkthdr.len;
744
745         /* Note receiving interface */
746         m->m_pkthdr.rcvif = ifp;
747
748         /* Berkeley packet filter */
749         ng_iface_bpftap(ifp, m, iffam->family);
750
751         /* Send packet */
752         switch (iffam->family) {
753 #ifdef INET
754         case AF_INET:
755                 isr = NETISR_IP;
756                 break;
757 #endif
758 #ifdef INET6
759         case AF_INET6:
760                 isr = NETISR_IPV6;
761                 break;
762 #endif
763 #ifdef IPX
764         case AF_IPX:
765                 isr = NETISR_IPX;
766                 break;
767 #endif
768 #ifdef NETATALK
769         case AF_APPLETALK:
770                 isr = NETISR_ATALK2;
771                 break;
772 #endif
773         default:
774                 m_freem(m);
775                 return (EAFNOSUPPORT);
776         }
777         if (harvest.point_to_point)
778                 random_harvest(&(m->m_data), 12, 2, RANDOM_NET_NG);
779         M_SETFIB(m, ifp->if_fib);
780         netisr_dispatch(isr, m);
781         return (0);
782 }
783
784 /*
785  * Shutdown and remove the node and its associated interface.
786  */
787 static int
788 ng_iface_shutdown(node_p node)
789 {
790         const priv_p priv = NG_NODE_PRIVATE(node);
791
792         /*
793          * The ifnet may be in a different vnet than the netgraph node, 
794          * hence we have to change the current vnet context here.
795          */
796         CURVNET_SET_QUIET(priv->ifp->if_vnet);
797         bpfdetach(priv->ifp);
798         if_detach(priv->ifp);
799         if_free(priv->ifp);
800         CURVNET_RESTORE();
801         priv->ifp = NULL;
802         free_unr(V_ng_iface_unit, priv->unit);
803         free(priv, M_NETGRAPH_IFACE);
804         NG_NODE_SET_PRIVATE(node, NULL);
805         NG_NODE_UNREF(node);
806         return (0);
807 }
808
809 /*
810  * Hook disconnection. Note that we do *not* shutdown when all
811  * hooks have been disconnected.
812  */
813 static int
814 ng_iface_disconnect(hook_p hook)
815 {
816         const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
817         const iffam_p iffam = get_iffam_from_hook(priv, hook);
818
819         if (iffam == NULL)
820                 panic("%s", __func__);
821         *get_hook_from_iffam(priv, iffam) = NULL;
822         return (0);
823 }
824
825 /*
826  * Handle loading and unloading for this node type.
827  */
828 static int
829 ng_iface_mod_event(module_t mod, int event, void *data)
830 {
831         int error = 0;
832
833         switch (event) {
834         case MOD_LOAD:
835         case MOD_UNLOAD:
836                 break;
837         default:
838                 error = EOPNOTSUPP;
839                 break;
840         }
841         return (error);
842 }
843
844 static void
845 vnet_ng_iface_init(const void *unused)
846 {
847
848         V_ng_iface_unit = new_unrhdr(0, 0xffff, NULL);
849 }
850 VNET_SYSINIT(vnet_ng_iface_init, SI_SUB_PSEUDO, SI_ORDER_ANY,
851     vnet_ng_iface_init, NULL);
852
853 static void
854 vnet_ng_iface_uninit(const void *unused)
855 {
856
857         delete_unrhdr(V_ng_iface_unit);
858 }
859 VNET_SYSUNINIT(vnet_ng_iface_uninit, SI_SUB_PSEUDO, SI_ORDER_ANY,
860     vnet_ng_iface_uninit, NULL);