Parallelize ifnet.if_addrhead accessing by duplicating the list itself
[dragonfly.git] / sys / netgraph / iface / ng_iface.c
1
2 /*
3  * ng_iface.c
4  *
5  * Copyright (c) 1996-1999 Whistle Communications, Inc.
6  * All rights reserved.
7  * 
8  * Subject to the following obligations and disclaimer of warranty, use and
9  * redistribution of this software, in source or object code forms, with or
10  * without modifications are expressly permitted by Whistle Communications;
11  * provided, however, that:
12  * 1. Any and all reproductions of the source or object code must include the
13  *    copyright notice above and the following disclaimer of warranties; and
14  * 2. No rights are granted, in any manner or form, to use Whistle
15  *    Communications, Inc. trademarks, including the mark "WHISTLE
16  *    COMMUNICATIONS" on advertising, endorsements, or otherwise except as
17  *    such appears in the above copyright notice or in the software.
18  * 
19  * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
20  * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
21  * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
22  * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
23  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
24  * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
25  * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
26  * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
27  * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
28  * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
29  * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
30  * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
31  * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
32  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
34  * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
35  * OF SUCH DAMAGE.
36  *
37  * Author: Archie Cobbs <archie@freebsd.org>
38  *
39  * $FreeBSD: src/sys/netgraph/ng_iface.c,v 1.7.2.5 2002/07/02 23:44:02 archie Exp $
40  * $DragonFly: src/sys/netgraph/iface/ng_iface.c,v 1.15 2008/03/07 11:34:20 sephe Exp $
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/sockio.h>
68 #include <sys/socket.h>
69 #include <sys/syslog.h>
70 #include <sys/libkern.h>
71 #include <sys/thread2.h>
72
73 #include <net/if.h>
74 #include <net/if_types.h>
75 #include <net/netisr.h>
76 #include <net/bpf.h>
77
78 #include <netinet/in.h>
79
80 #include <netgraph/ng_message.h>
81 #include <netgraph/netgraph.h>
82 #include <netgraph/ng_parse.h>
83 #include "ng_iface.h"
84 #include <netgraph/cisco/ng_cisco.h>
85
86 /* This struct describes one address family */
87 struct iffam {
88         sa_family_t     family;         /* Address family */
89         const char      *hookname;      /* Name for hook */
90 };
91 typedef const struct iffam *iffam_p;
92
93 /* List of address families supported by our interface */
94 const static struct iffam gFamilies[] = {
95         { AF_INET,      NG_IFACE_HOOK_INET      },
96         { AF_INET6,     NG_IFACE_HOOK_INET6     },
97         { AF_APPLETALK, NG_IFACE_HOOK_ATALK     },
98         { AF_IPX,       NG_IFACE_HOOK_IPX       },
99         { AF_ATM,       NG_IFACE_HOOK_ATM       },
100         { AF_NATM,      NG_IFACE_HOOK_NATM      },
101         { AF_NS,        NG_IFACE_HOOK_NS        },
102 };
103 #define NUM_FAMILIES            (sizeof(gFamilies) / sizeof(*gFamilies))
104
105 /* Node private data */
106 struct ng_iface_private {
107         struct  ifnet *ifp;             /* Our interface */
108         int     unit;                   /* Interface unit number */
109         node_p  node;                   /* Our netgraph node */
110         hook_p  hooks[NUM_FAMILIES];    /* Hook for each address family */
111 };
112 typedef struct ng_iface_private *priv_p;
113
114 /* Interface methods */
115 static void     ng_iface_start(struct ifnet *ifp);
116 static int      ng_iface_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data,
117                         struct ucred *cr);
118 static int      ng_iface_output(struct ifnet *ifp, struct mbuf *m0,
119                         struct sockaddr *dst, struct rtentry *rt0);
120 static void     ng_iface_bpftap(struct ifnet *ifp,
121                         struct mbuf *m, sa_family_t family);
122 #ifdef DEBUG
123 static void     ng_iface_print_ioctl(struct ifnet *ifp, int cmd, caddr_t data);
124 #endif
125
126 /* Netgraph methods */
127 static ng_constructor_t ng_iface_constructor;
128 static ng_rcvmsg_t      ng_iface_rcvmsg;
129 static ng_shutdown_t    ng_iface_rmnode;
130 static ng_newhook_t     ng_iface_newhook;
131 static ng_rcvdata_t     ng_iface_rcvdata;
132 static ng_disconnect_t  ng_iface_disconnect;
133
134 /* Helper stuff */
135 static iffam_p  get_iffam_from_af(sa_family_t family);
136 static iffam_p  get_iffam_from_hook(priv_p priv, hook_p hook);
137 static iffam_p  get_iffam_from_name(const char *name);
138 static hook_p  *get_hook_from_iffam(priv_p priv, iffam_p iffam);
139
140 /* Parse type for struct ng_iface_ifname */
141 static const struct ng_parse_fixedstring_info ng_iface_ifname_info = {
142         NG_IFACE_IFACE_NAME_MAX + 1
143 };
144 static const struct ng_parse_type ng_iface_ifname_type = {
145         &ng_parse_fixedstring_type,
146         &ng_iface_ifname_info
147 };
148
149 /* Parse type for struct ng_cisco_ipaddr */
150 static const struct ng_parse_struct_field ng_cisco_ipaddr_type_fields[]
151         = NG_CISCO_IPADDR_TYPE_INFO;
152 static const struct ng_parse_type ng_cisco_ipaddr_type = {
153         &ng_parse_struct_type,
154         &ng_cisco_ipaddr_type_fields
155 };
156
157 /* List of commands and how to convert arguments to/from ASCII */
158 static const struct ng_cmdlist ng_iface_cmds[] = {
159         {
160           NGM_IFACE_COOKIE,
161           NGM_IFACE_GET_IFNAME,
162           "getifname",
163           NULL,
164           &ng_iface_ifname_type
165         },
166         {
167           NGM_IFACE_COOKIE,
168           NGM_IFACE_POINT2POINT,
169           "point2point",
170           NULL,
171           NULL
172         },
173         {
174           NGM_IFACE_COOKIE,
175           NGM_IFACE_BROADCAST,
176           "broadcast",
177           NULL,
178           NULL
179         },
180         {
181           NGM_CISCO_COOKIE,
182           NGM_CISCO_GET_IPADDR,
183           "getipaddr",
184           NULL,
185           &ng_cisco_ipaddr_type
186         },
187         { 0 }
188 };
189
190 /* Node type descriptor */
191 static struct ng_type typestruct = {
192         NG_VERSION,
193         NG_IFACE_NODE_TYPE,
194         NULL,
195         ng_iface_constructor,
196         ng_iface_rcvmsg,
197         ng_iface_rmnode,
198         ng_iface_newhook,
199         NULL,
200         NULL,
201         ng_iface_rcvdata,
202         ng_iface_rcvdata,
203         ng_iface_disconnect,
204         ng_iface_cmds
205 };
206 NETGRAPH_INIT(iface, &typestruct);
207
208 /* We keep a bitmap indicating which unit numbers are free.
209    One means the unit number is free, zero means it's taken. */
210 static int      *ng_iface_units = NULL;
211 static int      ng_iface_units_len = 0;
212
213 #define UNITS_BITSPERWORD       (sizeof(*ng_iface_units) * NBBY)
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  * Find the first free unit number for a new interface.
279  * Increase the size of the unit bitmap as necessary.
280  */
281 static __inline__ int
282 ng_iface_get_unit(int *unit)
283 {
284         int index, bit;
285
286         for (index = 0; index < ng_iface_units_len
287             && ng_iface_units[index] == 0; index++);
288         if (index == ng_iface_units_len) {              /* extend array */
289                 int i, *newarray, newlen;
290
291                 newlen = (2 * ng_iface_units_len) + 4;
292                 MALLOC(newarray, int *, newlen * sizeof(*ng_iface_units),
293                     M_NETGRAPH, M_NOWAIT);
294                 if (newarray == NULL)
295                         return (ENOMEM);
296                 bcopy(ng_iface_units, newarray,
297                     ng_iface_units_len * sizeof(*ng_iface_units));
298                 for (i = ng_iface_units_len; i < newlen; i++)
299                         newarray[i] = ~0;
300                 if (ng_iface_units != NULL)
301                         FREE(ng_iface_units, M_NETGRAPH);
302                 ng_iface_units = newarray;
303                 ng_iface_units_len = newlen;
304         }
305         bit = ffs(ng_iface_units[index]) - 1;
306         KASSERT(bit >= 0 && bit <= UNITS_BITSPERWORD - 1,
307             ("%s: word=%d bit=%d", __func__, ng_iface_units[index], bit));
308         ng_iface_units[index] &= ~(1 << bit);
309         *unit = (index * UNITS_BITSPERWORD) + bit;
310         return (0);
311 }
312
313 /*
314  * Free a no longer needed unit number.
315  */
316 static __inline__ void
317 ng_iface_free_unit(int unit)
318 {
319         int index, bit;
320
321         index = unit / UNITS_BITSPERWORD;
322         bit = unit % UNITS_BITSPERWORD;
323         KASSERT(index < ng_iface_units_len,
324             ("%s: unit=%d len=%d", __func__, unit, ng_iface_units_len));
325         KASSERT((ng_iface_units[index] & (1 << bit)) == 0,
326             ("%s: unit=%d is free", __func__, unit));
327         ng_iface_units[index] |= (1 << bit);
328         /*
329          * XXX We could think about reducing the size of ng_iface_units[]
330          * XXX here if the last portion is all ones
331          */
332 }
333
334 /************************************************************************
335                         INTERFACE STUFF
336  ************************************************************************/
337
338 /*
339  * Process an ioctl for the virtual interface
340  */
341 static int
342 ng_iface_ioctl(struct ifnet *ifp, u_long command, caddr_t data,
343     struct ucred *cr)
344 {
345         struct ifreq *const ifr = (struct ifreq *) data;
346         int error = 0;
347
348 #ifdef DEBUG
349         ng_iface_print_ioctl(ifp, command, data);
350 #endif
351         crit_enter();
352         switch (command) {
353
354         /* These two are mostly handled at a higher layer */
355         case SIOCSIFADDR:
356                 ifp->if_flags |= (IFF_UP | IFF_RUNNING);
357                 ifp->if_flags &= ~(IFF_OACTIVE);
358                 break;
359         case SIOCGIFADDR:
360                 break;
361
362         /* Set flags */
363         case SIOCSIFFLAGS:
364                 /*
365                  * If the interface is marked up and stopped, then start it.
366                  * If it is marked down and running, then stop it.
367                  */
368                 if (ifr->ifr_flags & IFF_UP) {
369                         if (!(ifp->if_flags & IFF_RUNNING)) {
370                                 ifp->if_flags &= ~(IFF_OACTIVE);
371                                 ifp->if_flags |= IFF_RUNNING;
372                         }
373                 } else {
374                         if (ifp->if_flags & IFF_RUNNING)
375                                 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
376                 }
377                 break;
378
379         /* Set the interface MTU */
380         case SIOCSIFMTU:
381                 if (ifr->ifr_mtu > NG_IFACE_MTU_MAX
382                     || ifr->ifr_mtu < NG_IFACE_MTU_MIN)
383                         error = EINVAL;
384                 else
385                         ifp->if_mtu = ifr->ifr_mtu;
386                 break;
387
388         /* Stuff that's not supported */
389         case SIOCADDMULTI:
390         case SIOCDELMULTI:
391                 error = 0;
392                 break;
393         case SIOCSIFPHYS:
394                 error = EOPNOTSUPP;
395                 break;
396
397         default:
398                 error = EINVAL;
399                 break;
400         }
401         crit_exit();
402         return (error);
403 }
404
405 /*
406  * This routine is called to deliver a packet out the interface.
407  * We simply look at the address family and relay the packet to
408  * the corresponding hook, if it exists and is connected.
409  */
410
411 static int
412 ng_iface_output(struct ifnet *ifp, struct mbuf *m,
413                 struct sockaddr *dst, struct rtentry *rt0)
414 {
415         const priv_p priv = (priv_p) ifp->if_softc;
416         const iffam_p iffam = get_iffam_from_af(dst->sa_family);
417         meta_p meta = NULL;
418         int len, error = 0;
419
420         /* Check interface flags */
421         if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING)) {
422                 m_freem(m);
423                 return (ENETDOWN);
424         }
425
426         /* BPF writes need to be handled specially */
427         if (dst->sa_family == AF_UNSPEC) {
428                 if (m->m_len < 4 && (m = m_pullup(m, 4)) == NULL)
429                         return (ENOBUFS);
430                 dst->sa_family = (sa_family_t)*mtod(m, int32_t *);
431                 m->m_data += 4;
432                 m->m_len -= 4;
433                 m->m_pkthdr.len -= 4;
434         }
435
436         /* Berkeley packet filter */
437         ng_iface_bpftap(ifp, m, dst->sa_family);
438
439         /* Check address family to determine hook (if known) */
440         if (iffam == NULL) {
441                 m_freem(m);
442                 log(LOG_WARNING, "%s: can't handle af%d\n",
443                        ifp->if_xname, (int)dst->sa_family);
444                 return (EAFNOSUPPORT);
445         }
446
447         /* Copy length before the mbuf gets invalidated */
448         len = m->m_pkthdr.len;
449
450         /* Send packet; if hook is not connected, mbuf will get freed. */
451         NG_SEND_DATA(error, *get_hook_from_iffam(priv, iffam), m, meta);
452
453         /* Update stats */
454         if (error == 0) {
455                 ifp->if_obytes += len;
456                 ifp->if_opackets++;
457         }
458         return (error);
459 }
460
461 /*
462  * This routine should never be called
463  */
464
465 static void
466 ng_iface_start(struct ifnet *ifp)
467 {
468         kprintf("%s: %s called?", ifp->if_xname, __func__);
469 }
470
471 /*
472  * Flash a packet by the BPF (requires prepending 4 byte AF header)
473  */
474 static void
475 ng_iface_bpftap(struct ifnet *ifp, struct mbuf *m, sa_family_t family)
476 {
477         int32_t family4 = (int32_t)family;
478
479         KASSERT(family != AF_UNSPEC, ("%s: family=AF_UNSPEC", __func__));
480
481         if (ifp->if_bpf)
482                 bpf_ptap(ifp->if_bpf, m, &family, sizeof(family4));
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 *nodep)
529 {
530         char ifname[NG_IFACE_IFACE_NAME_MAX + 1];
531         struct ifnet *ifp;
532         node_p node;
533         priv_p priv;
534         int error = 0;
535
536         /* Allocate node and interface private structures */
537         MALLOC(priv, priv_p, sizeof(*priv), M_NETGRAPH, M_NOWAIT | M_ZERO);
538         if (priv == NULL)
539                 return (ENOMEM);
540         MALLOC(ifp, struct ifnet *, sizeof(*ifp), M_NETGRAPH, M_NOWAIT | M_ZERO);
541         if (ifp == NULL) {
542                 FREE(priv, M_NETGRAPH);
543                 return (ENOMEM);
544         }
545
546         /* Link them together */
547         ifp->if_softc = priv;
548         priv->ifp = ifp;
549
550         /* Get an interface unit number */
551         if ((error = ng_iface_get_unit(&priv->unit)) != 0) {
552                 FREE(ifp, M_NETGRAPH);
553                 FREE(priv, M_NETGRAPH);
554                 return (error);
555         }
556
557         /* Call generic node constructor */
558         if ((error = ng_make_node_common(&typestruct, nodep)) != 0) {
559                 ng_iface_free_unit(priv->unit);
560                 FREE(ifp, M_NETGRAPH);
561                 FREE(priv, M_NETGRAPH);
562                 return (error);
563         }
564         node = *nodep;
565
566         /* Link together node and private info */
567         node->private = priv;
568         priv->node = node;
569
570         /* Initialize interface structure */
571         if_initname(ifp, NG_IFACE_IFACE_NAME, priv->unit);
572         ifp->if_output = ng_iface_output;
573         ifp->if_start = ng_iface_start;
574         ifp->if_ioctl = ng_iface_ioctl;
575         ifp->if_watchdog = NULL;
576         ifp->if_snd.ifq_maxlen = IFQ_MAXLEN;
577         ifp->if_mtu = NG_IFACE_MTU_DEFAULT;
578         ifp->if_flags = (IFF_SIMPLEX|IFF_POINTOPOINT|IFF_NOARP|IFF_MULTICAST);
579         ifp->if_type = IFT_PROPVIRTUAL;         /* XXX */
580         ifp->if_addrlen = 0;                    /* XXX */
581         ifp->if_hdrlen = 0;                     /* XXX */
582         ifp->if_baudrate = 64000;               /* XXX */
583
584         /* Give this node the same name as the interface (if possible) */
585         bzero(ifname, sizeof(ifname));
586         strlcpy(ifname, ifp->if_xname, sizeof(ifname));
587         if (ng_name_node(node, ifname) != 0)
588                 log(LOG_WARNING, "%s: can't acquire netgraph name\n", ifname);
589
590         /* Attach the interface */
591         if_attach(ifp, NULL);
592         bpfattach(ifp, DLT_NULL, sizeof(u_int));
593
594         /* Done */
595         return (0);
596 }
597
598 /*
599  * Give our ok for a hook to be added
600  */
601 static int
602 ng_iface_newhook(node_p node, hook_p hook, const char *name)
603 {
604         const iffam_p iffam = get_iffam_from_name(name);
605         hook_p *hookptr;
606
607         if (iffam == NULL)
608                 return (EPFNOSUPPORT);
609         hookptr = get_hook_from_iffam((priv_p) node->private, iffam);
610         if (*hookptr != NULL)
611                 return (EISCONN);
612         *hookptr = hook;
613         return (0);
614 }
615
616 /*
617  * Receive a control message
618  */
619 static int
620 ng_iface_rcvmsg(node_p node, struct ng_mesg *msg,
621                 const char *retaddr, struct ng_mesg **rptr)
622 {
623         const priv_p priv = node->private;
624         struct ifnet *const ifp = priv->ifp;
625         struct ng_mesg *resp = NULL;
626         int error = 0;
627
628         switch (msg->header.typecookie) {
629         case NGM_IFACE_COOKIE:
630                 switch (msg->header.cmd) {
631                 case NGM_IFACE_GET_IFNAME:
632                     {
633                         struct ng_iface_ifname *arg;
634
635                         NG_MKRESPONSE(resp, msg, sizeof(*arg), M_NOWAIT);
636                         if (resp == NULL) {
637                                 error = ENOMEM;
638                                 break;
639                         }
640                         arg = (struct ng_iface_ifname *)resp->data;
641                         strlcpy(arg->ngif_name, ifp->if_xname,
642                             sizeof(arg->ngif_name));
643                         break;
644                     }
645
646                 case NGM_IFACE_POINT2POINT:
647                 case NGM_IFACE_BROADCAST:
648                     {
649
650                         /* Deny request if interface is UP */
651                         if ((ifp->if_flags & IFF_UP) != 0)
652                                 return (EBUSY);
653
654                         /* Change flags */
655                         switch (msg->header.cmd) {
656                         case NGM_IFACE_POINT2POINT:
657                                 ifp->if_flags |= IFF_POINTOPOINT;
658                                 ifp->if_flags &= ~IFF_BROADCAST;
659                                 break;
660                         case NGM_IFACE_BROADCAST:
661                                 ifp->if_flags &= ~IFF_POINTOPOINT;
662                                 ifp->if_flags |= IFF_BROADCAST;
663                                 break;
664                         }
665                         break;
666                     }
667
668                 default:
669                         error = EINVAL;
670                         break;
671                 }
672                 break;
673         case NGM_CISCO_COOKIE:
674                 switch (msg->header.cmd) {
675                 case NGM_CISCO_GET_IPADDR:      /* we understand this too */
676                     {
677                         struct ifaddr_container *ifac;
678
679                         /* Return the first configured IP address */
680                         TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid],
681                                       ifa_link) {
682                                 struct ifaddr *ifa = ifac->ifa;
683                                 struct ng_cisco_ipaddr *ips;
684
685                                 if (ifa->ifa_addr->sa_family != AF_INET)
686                                         continue;
687                                 NG_MKRESPONSE(resp, msg, sizeof(ips), M_NOWAIT);
688                                 if (resp == NULL) {
689                                         error = ENOMEM;
690                                         break;
691                                 }
692                                 ips = (struct ng_cisco_ipaddr *)resp->data;
693                                 ips->ipaddr = ((struct sockaddr_in *)
694                                                 ifa->ifa_addr)->sin_addr;
695                                 ips->netmask = ((struct sockaddr_in *)
696                                                 ifa->ifa_netmask)->sin_addr;
697                                 break;
698                         }
699
700                         /* No IP addresses on this interface? */
701                         if (ifac == NULL)
702                                 error = EADDRNOTAVAIL;
703                         break;
704                     }
705                 default:
706                         error = EINVAL;
707                         break;
708                 }
709                 break;
710         default:
711                 error = EINVAL;
712                 break;
713         }
714         if (rptr)
715                 *rptr = resp;
716         else if (resp)
717                 FREE(resp, M_NETGRAPH);
718         FREE(msg, M_NETGRAPH);
719         return (error);
720 }
721
722 /*
723  * Recive data from a hook. Pass the packet to the correct input routine.
724  */
725 static int
726 ng_iface_rcvdata(hook_p hook, struct mbuf *m, meta_p meta)
727 {
728         const priv_p priv = hook->node->private;
729         const iffam_p iffam = get_iffam_from_hook(priv, hook);
730         struct ifnet *const ifp = priv->ifp;
731         int isr;
732
733         /* Sanity checks */
734         KASSERT(iffam != NULL, ("%s: iffam", __func__));
735         KASSERT(m->m_flags & M_PKTHDR, ("%s: not pkthdr", __func__));
736         if (m == NULL)
737                 return (EINVAL);
738         if ((ifp->if_flags & IFF_UP) == 0) {
739                 NG_FREE_DATA(m, meta);
740                 return (ENETDOWN);
741         }
742
743         /* Update interface stats */
744         ifp->if_ipackets++;
745         ifp->if_ibytes += m->m_pkthdr.len;
746
747         /* Note receiving interface */
748         m->m_pkthdr.rcvif = ifp;
749
750         /* Berkeley packet filter */
751         ng_iface_bpftap(ifp, m, iffam->family);
752
753         /* Ignore any meta-data */
754         NG_FREE_META(meta);
755
756         /* Send packet */
757         switch (iffam->family) {
758 #ifdef INET
759         case AF_INET:
760                 isr = NETISR_IP;
761                 break;
762 #endif
763 #ifdef INET6
764         case AF_INET6:
765                 isr = NETISR_IPV6;
766                 break;
767 #endif
768 #ifdef IPX
769         case AF_IPX:
770                 isr = NETISR_IPX;
771                 break;
772 #endif
773 #ifdef NETATALK
774         case AF_APPLETALK:
775                 isr = NETISR_ATALK2;
776                 break;
777 #endif
778         default:
779                 m_freem(m);
780                 return (EAFNOSUPPORT);
781         }
782         netisr_dispatch(isr, m);
783         return (0);
784 }
785
786 /*
787  * Shutdown and remove the node and its associated interface.
788  */
789 static int
790 ng_iface_rmnode(node_p node)
791 {
792         const priv_p priv = node->private;
793
794         ng_cutlinks(node);
795         ng_unname(node);
796         bpfdetach(priv->ifp);
797         if_detach(priv->ifp);
798         FREE(priv->ifp, M_NETGRAPH);
799         priv->ifp = NULL;
800         ng_iface_free_unit(priv->unit);
801         FREE(priv, M_NETGRAPH);
802         node->private = NULL;
803         ng_unref(node);
804         return (0);
805 }
806
807 /*
808  * Hook disconnection. Note that we do *not* shutdown when all
809  * hooks have been disconnected.
810  */
811 static int
812 ng_iface_disconnect(hook_p hook)
813 {
814         const priv_p priv = hook->node->private;
815         const iffam_p iffam = get_iffam_from_hook(priv, hook);
816
817         if (iffam == NULL)
818                 panic(__func__);
819         *get_hook_from_iffam(priv, iffam) = NULL;
820         return (0);
821 }
822