]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/netgraph/ng_iface.c
Conditionally compile out V_ globals while instantiating the appropriate
[FreeBSD/FreeBSD.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/random.h>
68 #include <sys/sockio.h>
69 #include <sys/socket.h>
70 #include <sys/syslog.h>
71 #include <sys/libkern.h>
72 #include <sys/vimage.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 <netgraph/ng_message.h>
82 #include <netgraph/netgraph.h>
83 #include <netgraph/ng_parse.h>
84 #include <netgraph/ng_iface.h>
85 #include <netgraph/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 #ifdef VIMAGE_GLOBALS
212 static struct unrhdr    *ng_iface_unit;
213 #endif
214
215 /************************************************************************
216                         HELPER STUFF
217  ************************************************************************/
218
219 /*
220  * Get the family descriptor from the family ID
221  */
222 static __inline iffam_p
223 get_iffam_from_af(sa_family_t family)
224 {
225         iffam_p iffam;
226         int k;
227
228         for (k = 0; k < NUM_FAMILIES; k++) {
229                 iffam = &gFamilies[k];
230                 if (iffam->family == family)
231                         return (iffam);
232         }
233         return (NULL);
234 }
235
236 /*
237  * Get the family descriptor from the hook
238  */
239 static __inline iffam_p
240 get_iffam_from_hook(priv_p priv, hook_p hook)
241 {
242         int k;
243
244         for (k = 0; k < NUM_FAMILIES; k++)
245                 if (priv->hooks[k] == hook)
246                         return (&gFamilies[k]);
247         return (NULL);
248 }
249
250 /*
251  * Get the hook from the iffam descriptor
252  */
253
254 static __inline hook_p *
255 get_hook_from_iffam(priv_p priv, iffam_p iffam)
256 {
257         return (&priv->hooks[iffam - gFamilies]);
258 }
259
260 /*
261  * Get the iffam descriptor from the name
262  */
263 static __inline iffam_p
264 get_iffam_from_name(const char *name)
265 {
266         iffam_p iffam;
267         int k;
268
269         for (k = 0; k < NUM_FAMILIES; k++) {
270                 iffam = &gFamilies[k];
271                 if (!strcmp(iffam->hookname, name))
272                         return (iffam);
273         }
274         return (NULL);
275 }
276
277 /************************************************************************
278                         INTERFACE STUFF
279  ************************************************************************/
280
281 /*
282  * Process an ioctl for the virtual interface
283  */
284 static int
285 ng_iface_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
286 {
287         struct ifreq *const ifr = (struct ifreq *) data;
288         int s, error = 0;
289
290 #ifdef DEBUG
291         ng_iface_print_ioctl(ifp, command, data);
292 #endif
293         s = splimp();
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         (void) splx(s);
346         return (error);
347 }
348
349 /*
350  * This routine is called to deliver a packet out the interface.
351  * We simply look at the address family and relay the packet to
352  * the corresponding hook, if it exists and is connected.
353  */
354
355 static int
356 ng_iface_output(struct ifnet *ifp, struct mbuf *m,
357                 struct sockaddr *dst, struct rtentry *rt0)
358 {
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         /* BPF writes need to be handled specially. */
370         if (dst->sa_family == AF_UNSPEC) {
371                 bcopy(dst->sa_data, &af, sizeof(af));
372                 dst->sa_family = af;
373         }
374
375         /* Berkeley packet filter */
376         ng_iface_bpftap(ifp, m, dst->sa_family);
377
378         if (ALTQ_IS_ENABLED(&ifp->if_snd)) {
379                 M_PREPEND(m, sizeof(sa_family_t), M_DONTWAIT);
380                 if (m == NULL) {
381                         IFQ_LOCK(&ifp->if_snd);
382                         IFQ_INC_DROPS(&ifp->if_snd);
383                         IFQ_UNLOCK(&ifp->if_snd);
384                         ifp->if_oerrors++;
385                         return (ENOBUFS);
386                 }
387                 *(sa_family_t *)m->m_data = dst->sa_family;
388                 error = (ifp->if_transmit)(ifp, m);
389         } else
390                 error = ng_iface_send(ifp, m, dst->sa_family);
391
392         return (error);
393 }
394
395 /*
396  * Start method is used only when ALTQ is enabled.
397  */
398 static void
399 ng_iface_start(struct ifnet *ifp)
400 {
401         struct mbuf *m;
402         sa_family_t sa;
403
404         KASSERT(ALTQ_IS_ENABLED(&ifp->if_snd), ("%s without ALTQ", __func__));
405
406         for(;;) {
407                 IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
408                 if (m == NULL)
409                         break;
410                 sa = *mtod(m, sa_family_t *);
411                 m_adj(m, sizeof(sa_family_t));
412                 ng_iface_send(ifp, m, sa);
413         }
414 }
415
416 /*
417  * Flash a packet by the BPF (requires prepending 4 byte AF header)
418  * Note the phoney mbuf; this is OK because BPF treats it read-only.
419  */
420 static void
421 ng_iface_bpftap(struct ifnet *ifp, struct mbuf *m, sa_family_t family)
422 {
423         KASSERT(family != AF_UNSPEC, ("%s: family=AF_UNSPEC", __func__));
424         if (bpf_peers_present(ifp->if_bpf)) {
425                 int32_t family4 = (int32_t)family;
426                 bpf_mtap2(ifp->if_bpf, &family4, sizeof(family4), m);
427         }
428 }
429
430 /*
431  * This routine does actual delivery of the packet into the
432  * netgraph(4). It is called from ng_iface_start() and
433  * ng_iface_output().
434  */
435 static int
436 ng_iface_send(struct ifnet *ifp, struct mbuf *m, sa_family_t sa)
437 {
438         const priv_p priv = (priv_p) ifp->if_softc;
439         const iffam_p iffam = get_iffam_from_af(sa);
440         int error;
441         int len;
442
443         /* Check address family to determine hook (if known) */
444         if (iffam == NULL) {
445                 m_freem(m);
446                 log(LOG_WARNING, "%s: can't handle af%d\n", ifp->if_xname, sa);
447                 return (EAFNOSUPPORT);
448         }
449
450         /* Copy length before the mbuf gets invalidated. */
451         len = m->m_pkthdr.len;
452
453         /* Send packet. If hook is not connected,
454            mbuf will get freed. */
455         NG_SEND_DATA_ONLY(error, *get_hook_from_iffam(priv, iffam), m);
456
457         /* Update stats. */
458         if (error == 0) {
459                 ifp->if_obytes += len;
460                 ifp->if_opackets++;
461         }
462
463         return (error);
464 }
465
466 #ifdef DEBUG
467 /*
468  * Display an ioctl to the virtual interface
469  */
470
471 static void
472 ng_iface_print_ioctl(struct ifnet *ifp, int command, caddr_t data)
473 {
474         char   *str;
475
476         switch (command & IOC_DIRMASK) {
477         case IOC_VOID:
478                 str = "IO";
479                 break;
480         case IOC_OUT:
481                 str = "IOR";
482                 break;
483         case IOC_IN:
484                 str = "IOW";
485                 break;
486         case IOC_INOUT:
487                 str = "IORW";
488                 break;
489         default:
490                 str = "IO??";
491         }
492         log(LOG_DEBUG, "%s: %s('%c', %d, char[%d])\n",
493                ifp->if_xname,
494                str,
495                IOCGROUP(command),
496                command & 0xff,
497                IOCPARM_LEN(command));
498 }
499 #endif /* DEBUG */
500
501 /************************************************************************
502                         NETGRAPH NODE STUFF
503  ************************************************************************/
504
505 /*
506  * Constructor for a node
507  */
508 static int
509 ng_iface_constructor(node_p node)
510 {
511         INIT_VNET_NETGRAPH(curvnet);
512         struct ifnet *ifp;
513         priv_p priv;
514
515         /* Allocate node and interface private structures */
516         priv = malloc(sizeof(*priv), M_NETGRAPH_IFACE, M_NOWAIT|M_ZERO);
517         if (priv == NULL)
518                 return (ENOMEM);
519         ifp = if_alloc(IFT_PROPVIRTUAL);
520         if (ifp == NULL) {
521                 free(priv, M_NETGRAPH_IFACE);
522                 return (ENOMEM);
523         }
524
525         /* Link them together */
526         ifp->if_softc = priv;
527         priv->ifp = ifp;
528
529         /* Get an interface unit number */
530         priv->unit = alloc_unr(V_ng_iface_unit);
531
532         /* Link together node and private info */
533         NG_NODE_SET_PRIVATE(node, priv);
534         priv->node = node;
535
536         /* Initialize interface structure */
537         if_initname(ifp, NG_IFACE_IFACE_NAME, priv->unit);
538         ifp->if_output = ng_iface_output;
539         ifp->if_start = ng_iface_start;
540         ifp->if_ioctl = ng_iface_ioctl;
541         ifp->if_watchdog = NULL;
542         ifp->if_mtu = NG_IFACE_MTU_DEFAULT;
543         ifp->if_flags = (IFF_SIMPLEX|IFF_POINTOPOINT|IFF_NOARP|IFF_MULTICAST);
544         ifp->if_type = IFT_PROPVIRTUAL;         /* XXX */
545         ifp->if_addrlen = 0;                    /* XXX */
546         ifp->if_hdrlen = 0;                     /* XXX */
547         ifp->if_baudrate = 64000;               /* XXX */
548         IFQ_SET_MAXLEN(&ifp->if_snd, IFQ_MAXLEN);
549         ifp->if_snd.ifq_drv_maxlen = IFQ_MAXLEN;
550         IFQ_SET_READY(&ifp->if_snd);
551
552         /* Give this node the same name as the interface (if possible) */
553         if (ng_name_node(node, ifp->if_xname) != 0)
554                 log(LOG_WARNING, "%s: can't acquire netgraph name\n",
555                     ifp->if_xname);
556
557         /* Attach the interface */
558         if_attach(ifp);
559         bpfattach(ifp, DLT_NULL, sizeof(u_int32_t));
560
561         /* Done */
562         return (0);
563 }
564
565 /*
566  * Give our ok for a hook to be added
567  */
568 static int
569 ng_iface_newhook(node_p node, hook_p hook, const char *name)
570 {
571         const iffam_p iffam = get_iffam_from_name(name);
572         hook_p *hookptr;
573
574         if (iffam == NULL)
575                 return (EPFNOSUPPORT);
576         hookptr = get_hook_from_iffam(NG_NODE_PRIVATE(node), iffam);
577         if (*hookptr != NULL)
578                 return (EISCONN);
579         *hookptr = hook;
580         NG_HOOK_HI_STACK(hook);
581         return (0);
582 }
583
584 /*
585  * Receive a control message
586  */
587 static int
588 ng_iface_rcvmsg(node_p node, item_p item, hook_p lasthook)
589 {
590         const priv_p priv = NG_NODE_PRIVATE(node);
591         struct ifnet *const ifp = priv->ifp;
592         struct ng_mesg *resp = NULL;
593         int error = 0;
594         struct ng_mesg *msg;
595
596         NGI_GET_MSG(item, msg);
597         switch (msg->header.typecookie) {
598         case NGM_IFACE_COOKIE:
599                 switch (msg->header.cmd) {
600                 case NGM_IFACE_GET_IFNAME:
601                         NG_MKRESPONSE(resp, msg, IFNAMSIZ, M_NOWAIT);
602                         if (resp == NULL) {
603                                 error = ENOMEM;
604                                 break;
605                         }
606                         strlcpy(resp->data, ifp->if_xname, IFNAMSIZ);
607                         break;
608
609                 case NGM_IFACE_POINT2POINT:
610                 case NGM_IFACE_BROADCAST:
611                     {
612
613                         /* Deny request if interface is UP */
614                         if ((ifp->if_flags & IFF_UP) != 0)
615                                 return (EBUSY);
616
617                         /* Change flags */
618                         switch (msg->header.cmd) {
619                         case NGM_IFACE_POINT2POINT:
620                                 ifp->if_flags |= IFF_POINTOPOINT;
621                                 ifp->if_flags &= ~IFF_BROADCAST;
622                                 break;
623                         case NGM_IFACE_BROADCAST:
624                                 ifp->if_flags &= ~IFF_POINTOPOINT;
625                                 ifp->if_flags |= IFF_BROADCAST;
626                                 break;
627                         }
628                         break;
629                     }
630
631                 case NGM_IFACE_GET_IFINDEX:
632                         NG_MKRESPONSE(resp, msg, sizeof(uint32_t), M_NOWAIT);
633                         if (resp == NULL) {
634                                 error = ENOMEM;
635                                 break;
636                         }
637                         *((uint32_t *)resp->data) = priv->ifp->if_index;
638                         break;
639
640                 default:
641                         error = EINVAL;
642                         break;
643                 }
644                 break;
645         case NGM_CISCO_COOKIE:
646                 switch (msg->header.cmd) {
647                 case NGM_CISCO_GET_IPADDR:      /* we understand this too */
648                     {
649                         struct ifaddr *ifa;
650
651                         /* Return the first configured IP address */
652                         TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
653                                 struct ng_cisco_ipaddr *ips;
654
655                                 if (ifa->ifa_addr->sa_family != AF_INET)
656                                         continue;
657                                 NG_MKRESPONSE(resp, msg, sizeof(ips), M_NOWAIT);
658                                 if (resp == NULL) {
659                                         error = ENOMEM;
660                                         break;
661                                 }
662                                 ips = (struct ng_cisco_ipaddr *)resp->data;
663                                 ips->ipaddr = ((struct sockaddr_in *)
664                                                 ifa->ifa_addr)->sin_addr;
665                                 ips->netmask = ((struct sockaddr_in *)
666                                                 ifa->ifa_netmask)->sin_addr;
667                                 break;
668                         }
669
670                         /* No IP addresses on this interface? */
671                         if (ifa == NULL)
672                                 error = EADDRNOTAVAIL;
673                         break;
674                     }
675                 default:
676                         error = EINVAL;
677                         break;
678                 }
679                 break;
680         case NGM_FLOW_COOKIE:
681                 switch (msg->header.cmd) {
682                 case NGM_LINK_IS_UP:
683                         ifp->if_drv_flags |= IFF_DRV_RUNNING;
684                         break;
685                 case NGM_LINK_IS_DOWN:
686                         ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
687                         break;
688                 default:
689                         break;
690                 }
691                 break;
692         default:
693                 error = EINVAL;
694                 break;
695         }
696         NG_RESPOND_MSG(error, node, item, resp);
697         NG_FREE_MSG(msg);
698         return (error);
699 }
700
701 /*
702  * Recive data from a hook. Pass the packet to the correct input routine.
703  */
704 static int
705 ng_iface_rcvdata(hook_p hook, item_p item)
706 {
707         const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
708         const iffam_p iffam = get_iffam_from_hook(priv, hook);
709         struct ifnet *const ifp = priv->ifp;
710         struct mbuf *m;
711         int isr;
712
713         NGI_GET_M(item, m);
714         NG_FREE_ITEM(item);
715         /* Sanity checks */
716         KASSERT(iffam != NULL, ("%s: iffam", __func__));
717         M_ASSERTPKTHDR(m);
718         if ((ifp->if_flags & IFF_UP) == 0) {
719                 NG_FREE_M(m);
720                 return (ENETDOWN);
721         }
722
723         /* Update interface stats */
724         ifp->if_ipackets++;
725         ifp->if_ibytes += m->m_pkthdr.len;
726
727         /* Note receiving interface */
728         m->m_pkthdr.rcvif = ifp;
729
730         /* Berkeley packet filter */
731         ng_iface_bpftap(ifp, m, iffam->family);
732
733         /* Send packet */
734         switch (iffam->family) {
735 #ifdef INET
736         case AF_INET:
737                 isr = NETISR_IP;
738                 break;
739 #endif
740 #ifdef INET6
741         case AF_INET6:
742                 isr = NETISR_IPV6;
743                 break;
744 #endif
745 #ifdef IPX
746         case AF_IPX:
747                 isr = NETISR_IPX;
748                 break;
749 #endif
750 #ifdef NETATALK
751         case AF_APPLETALK:
752                 isr = NETISR_ATALK2;
753                 break;
754 #endif
755         default:
756                 m_freem(m);
757                 return (EAFNOSUPPORT);
758         }
759         /* First chunk of an mbuf contains good junk */
760         if (harvest.point_to_point)
761                 random_harvest(m, 16, 3, 0, RANDOM_NET);
762         netisr_dispatch(isr, m);
763         return (0);
764 }
765
766 /*
767  * Shutdown and remove the node and its associated interface.
768  */
769 static int
770 ng_iface_shutdown(node_p node)
771 {
772         INIT_VNET_NETGRAPH(curvnet);
773         const priv_p priv = NG_NODE_PRIVATE(node);
774
775         /*
776          * The ifnet may be in a different vnet than the netgraph node, 
777          * hence we have to change the current vnet context here.
778          */
779         CURVNET_SET_QUIET(priv->ifp->if_vnet);
780         bpfdetach(priv->ifp);
781         if_detach(priv->ifp);
782         if_free(priv->ifp);
783         CURVNET_RESTORE();
784         priv->ifp = NULL;
785         free_unr(V_ng_iface_unit, priv->unit);
786         free(priv, M_NETGRAPH_IFACE);
787         NG_NODE_SET_PRIVATE(node, NULL);
788         NG_NODE_UNREF(node);
789         return (0);
790 }
791
792 /*
793  * Hook disconnection. Note that we do *not* shutdown when all
794  * hooks have been disconnected.
795  */
796 static int
797 ng_iface_disconnect(hook_p hook)
798 {
799         const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
800         const iffam_p iffam = get_iffam_from_hook(priv, hook);
801
802         if (iffam == NULL)
803                 panic(__func__);
804         *get_hook_from_iffam(priv, iffam) = NULL;
805         return (0);
806 }
807
808 /*
809  * Handle loading and unloading for this node type.
810  */
811 static int
812 ng_iface_mod_event(module_t mod, int event, void *data)
813 {
814         int error = 0;
815
816         switch (event) {
817         case MOD_LOAD:
818                 V_ng_iface_unit = new_unrhdr(0, 0xffff, NULL);
819                 break;
820         case MOD_UNLOAD:
821                 delete_unrhdr(V_ng_iface_unit);
822                 break;
823         default:
824                 error = EOPNOTSUPP;
825                 break;
826         }
827         return (error);
828 }