Make all network interrupt service routines MPSAFE part 1/3.
[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.12 2005/11/28 17:13:46 dillon 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         printf("%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);
538         if (priv == NULL)
539                 return (ENOMEM);
540         bzero(priv, sizeof(*priv));
541         MALLOC(ifp, struct ifnet *, sizeof(*ifp), M_NETGRAPH, M_NOWAIT);
542         if (ifp == NULL) {
543                 FREE(priv, M_NETGRAPH);
544                 return (ENOMEM);
545         }
546         bzero(ifp, sizeof(*ifp));
547
548         /* Link them together */
549         ifp->if_softc = priv;
550         priv->ifp = ifp;
551
552         /* Get an interface unit number */
553         if ((error = ng_iface_get_unit(&priv->unit)) != 0) {
554                 FREE(ifp, M_NETGRAPH);
555                 FREE(priv, M_NETGRAPH);
556                 return (error);
557         }
558
559         /* Call generic node constructor */
560         if ((error = ng_make_node_common(&typestruct, nodep)) != 0) {
561                 ng_iface_free_unit(priv->unit);
562                 FREE(ifp, M_NETGRAPH);
563                 FREE(priv, M_NETGRAPH);
564                 return (error);
565         }
566         node = *nodep;
567
568         /* Link together node and private info */
569         node->private = priv;
570         priv->node = node;
571
572         /* Initialize interface structure */
573         if_initname(ifp, NG_IFACE_IFACE_NAME, priv->unit);
574         ifp->if_output = ng_iface_output;
575         ifp->if_start = ng_iface_start;
576         ifp->if_ioctl = ng_iface_ioctl;
577         ifp->if_watchdog = NULL;
578         ifp->if_snd.ifq_maxlen = IFQ_MAXLEN;
579         ifp->if_mtu = NG_IFACE_MTU_DEFAULT;
580         ifp->if_flags = (IFF_SIMPLEX|IFF_POINTOPOINT|IFF_NOARP|IFF_MULTICAST);
581         ifp->if_type = IFT_PROPVIRTUAL;         /* XXX */
582         ifp->if_addrlen = 0;                    /* XXX */
583         ifp->if_hdrlen = 0;                     /* XXX */
584         ifp->if_baudrate = 64000;               /* XXX */
585         TAILQ_INIT(&ifp->if_addrhead);
586
587         /* Give this node the same name as the interface (if possible) */
588         bzero(ifname, sizeof(ifname));
589         strlcpy(ifname, ifp->if_xname, sizeof(ifname));
590         if (ng_name_node(node, ifname) != 0)
591                 log(LOG_WARNING, "%s: can't acquire netgraph name\n", ifname);
592
593         /* Attach the interface */
594         if_attach(ifp, NULL);
595         bpfattach(ifp, DLT_NULL, sizeof(u_int));
596
597         /* Done */
598         return (0);
599 }
600
601 /*
602  * Give our ok for a hook to be added
603  */
604 static int
605 ng_iface_newhook(node_p node, hook_p hook, const char *name)
606 {
607         const iffam_p iffam = get_iffam_from_name(name);
608         hook_p *hookptr;
609
610         if (iffam == NULL)
611                 return (EPFNOSUPPORT);
612         hookptr = get_hook_from_iffam((priv_p) node->private, iffam);
613         if (*hookptr != NULL)
614                 return (EISCONN);
615         *hookptr = hook;
616         return (0);
617 }
618
619 /*
620  * Receive a control message
621  */
622 static int
623 ng_iface_rcvmsg(node_p node, struct ng_mesg *msg,
624                 const char *retaddr, struct ng_mesg **rptr)
625 {
626         const priv_p priv = node->private;
627         struct ifnet *const ifp = priv->ifp;
628         struct ng_mesg *resp = NULL;
629         int error = 0;
630
631         switch (msg->header.typecookie) {
632         case NGM_IFACE_COOKIE:
633                 switch (msg->header.cmd) {
634                 case NGM_IFACE_GET_IFNAME:
635                     {
636                         struct ng_iface_ifname *arg;
637
638                         NG_MKRESPONSE(resp, msg, sizeof(*arg), M_NOWAIT);
639                         if (resp == NULL) {
640                                 error = ENOMEM;
641                                 break;
642                         }
643                         arg = (struct ng_iface_ifname *)resp->data;
644                         strlcpy(arg->ngif_name, ifp->if_xname,
645                             sizeof(arg->ngif_name));
646                         break;
647                     }
648
649                 case NGM_IFACE_POINT2POINT:
650                 case NGM_IFACE_BROADCAST:
651                     {
652
653                         /* Deny request if interface is UP */
654                         if ((ifp->if_flags & IFF_UP) != 0)
655                                 return (EBUSY);
656
657                         /* Change flags */
658                         switch (msg->header.cmd) {
659                         case NGM_IFACE_POINT2POINT:
660                                 ifp->if_flags |= IFF_POINTOPOINT;
661                                 ifp->if_flags &= ~IFF_BROADCAST;
662                                 break;
663                         case NGM_IFACE_BROADCAST:
664                                 ifp->if_flags &= ~IFF_POINTOPOINT;
665                                 ifp->if_flags |= IFF_BROADCAST;
666                                 break;
667                         }
668                         break;
669                     }
670
671                 default:
672                         error = EINVAL;
673                         break;
674                 }
675                 break;
676         case NGM_CISCO_COOKIE:
677                 switch (msg->header.cmd) {
678                 case NGM_CISCO_GET_IPADDR:      /* we understand this too */
679                     {
680                         struct ifaddr *ifa;
681
682                         /* Return the first configured IP address */
683                         TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
684                                 struct ng_cisco_ipaddr *ips;
685
686                                 if (ifa->ifa_addr->sa_family != AF_INET)
687                                         continue;
688                                 NG_MKRESPONSE(resp, msg, sizeof(ips), M_NOWAIT);
689                                 if (resp == NULL) {
690                                         error = ENOMEM;
691                                         break;
692                                 }
693                                 ips = (struct ng_cisco_ipaddr *)resp->data;
694                                 ips->ipaddr = ((struct sockaddr_in *)
695                                                 ifa->ifa_addr)->sin_addr;
696                                 ips->netmask = ((struct sockaddr_in *)
697                                                 ifa->ifa_netmask)->sin_addr;
698                                 break;
699                         }
700
701                         /* No IP addresses on this interface? */
702                         if (ifa == NULL)
703                                 error = EADDRNOTAVAIL;
704                         break;
705                     }
706                 default:
707                         error = EINVAL;
708                         break;
709                 }
710                 break;
711         default:
712                 error = EINVAL;
713                 break;
714         }
715         if (rptr)
716                 *rptr = resp;
717         else if (resp)
718                 FREE(resp, M_NETGRAPH);
719         FREE(msg, M_NETGRAPH);
720         return (error);
721 }
722
723 /*
724  * Recive data from a hook. Pass the packet to the correct input routine.
725  */
726 static int
727 ng_iface_rcvdata(hook_p hook, struct mbuf *m, meta_p meta)
728 {
729         const priv_p priv = hook->node->private;
730         const iffam_p iffam = get_iffam_from_hook(priv, hook);
731         struct ifnet *const ifp = priv->ifp;
732         int isr;
733
734         /* Sanity checks */
735         KASSERT(iffam != NULL, ("%s: iffam", __func__));
736         KASSERT(m->m_flags & M_PKTHDR, ("%s: not pkthdr", __func__));
737         if (m == NULL)
738                 return (EINVAL);
739         if ((ifp->if_flags & IFF_UP) == 0) {
740                 NG_FREE_DATA(m, meta);
741                 return (ENETDOWN);
742         }
743
744         /* Update interface stats */
745         ifp->if_ipackets++;
746         ifp->if_ibytes += m->m_pkthdr.len;
747
748         /* Note receiving interface */
749         m->m_pkthdr.rcvif = ifp;
750
751         /* Berkeley packet filter */
752         ng_iface_bpftap(ifp, m, iffam->family);
753
754         /* Ignore any meta-data */
755         NG_FREE_META(meta);
756
757         /* Send packet */
758         switch (iffam->family) {
759 #ifdef INET
760         case AF_INET:
761                 isr = NETISR_IP;
762                 break;
763 #endif
764 #ifdef INET6
765         case AF_INET6:
766                 isr = NETISR_IPV6;
767                 break;
768 #endif
769 #ifdef IPX
770         case AF_IPX:
771                 isr = NETISR_IPX;
772                 break;
773 #endif
774 #ifdef NETATALK
775         case AF_APPLETALK:
776                 isr = NETISR_ATALK2;
777                 break;
778 #endif
779         default:
780                 m_freem(m);
781                 return (EAFNOSUPPORT);
782         }
783         netisr_dispatch(isr, m);
784         return (0);
785 }
786
787 /*
788  * Shutdown and remove the node and its associated interface.
789  */
790 static int
791 ng_iface_rmnode(node_p node)
792 {
793         const priv_p priv = node->private;
794
795         ng_cutlinks(node);
796         ng_unname(node);
797         bpfdetach(priv->ifp);
798         if_detach(priv->ifp);
799         FREE(priv->ifp, M_NETGRAPH);
800         priv->ifp = NULL;
801         ng_iface_free_unit(priv->unit);
802         FREE(priv, M_NETGRAPH);
803         node->private = NULL;
804         ng_unref(node);
805         return (0);
806 }
807
808 /*
809  * Hook disconnection. Note that we do *not* shutdown when all
810  * hooks have been disconnected.
811  */
812 static int
813 ng_iface_disconnect(hook_p hook)
814 {
815         const priv_p priv = hook->node->private;
816         const iffam_p iffam = get_iffam_from_hook(priv, hook);
817
818         if (iffam == NULL)
819                 panic(__func__);
820         *get_hook_from_iffam(priv, iffam) = NULL;
821         return (0);
822 }
823