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