kernel tree reorganization stage 1: Major cvs repository work (not logged as
[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.4 2003/08/07 21:54:32 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 <sys/param.h>
56 #include <sys/systm.h>
57 #include <sys/errno.h>
58 #include <sys/kernel.h>
59 #include <sys/malloc.h>
60 #include <sys/mbuf.h>
61 #include <sys/errno.h>
62 #include <sys/sockio.h>
63 #include <sys/socket.h>
64 #include <sys/syslog.h>
65 #include <sys/libkern.h>
66
67 #include <net/if.h>
68 #include <net/if_types.h>
69 #include <net/intrq.h>
70 #include <net/bpf.h>
71
72 #include <netinet/in.h>
73
74 #include <netgraph/ng_message.h>
75 #include <netgraph/netgraph.h>
76 #include <netgraph/ng_parse.h>
77 #include "ng_iface.h"
78 #include <netgraph/cisco/ng_cisco.h>
79
80 /* This struct describes one address family */
81 struct iffam {
82         sa_family_t     family;         /* Address family */
83         const char      *hookname;      /* Name for hook */
84 };
85 typedef const struct iffam *iffam_p;
86
87 /* List of address families supported by our interface */
88 const static struct iffam gFamilies[] = {
89         { AF_INET,      NG_IFACE_HOOK_INET      },
90         { AF_INET6,     NG_IFACE_HOOK_INET6     },
91         { AF_APPLETALK, NG_IFACE_HOOK_ATALK     },
92         { AF_IPX,       NG_IFACE_HOOK_IPX       },
93         { AF_ATM,       NG_IFACE_HOOK_ATM       },
94         { AF_NATM,      NG_IFACE_HOOK_NATM      },
95         { AF_NS,        NG_IFACE_HOOK_NS        },
96 };
97 #define NUM_FAMILIES            (sizeof(gFamilies) / sizeof(*gFamilies))
98
99 /* Node private data */
100 struct ng_iface_private {
101         struct  ifnet *ifp;             /* Our interface */
102         int     unit;                   /* Interface unit number */
103         node_p  node;                   /* Our netgraph node */
104         hook_p  hooks[NUM_FAMILIES];    /* Hook for each address family */
105 };
106 typedef struct ng_iface_private *priv_p;
107
108 /* Interface methods */
109 static void     ng_iface_start(struct ifnet *ifp);
110 static int      ng_iface_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data);
111 static int      ng_iface_output(struct ifnet *ifp, struct mbuf *m0,
112                         struct sockaddr *dst, struct rtentry *rt0);
113 static void     ng_iface_bpftap(struct ifnet *ifp,
114                         struct mbuf *m, sa_family_t family);
115 #ifdef DEBUG
116 static void     ng_iface_print_ioctl(struct ifnet *ifp, int cmd, caddr_t data);
117 #endif
118
119 /* Netgraph methods */
120 static ng_constructor_t ng_iface_constructor;
121 static ng_rcvmsg_t      ng_iface_rcvmsg;
122 static ng_shutdown_t    ng_iface_rmnode;
123 static ng_newhook_t     ng_iface_newhook;
124 static ng_rcvdata_t     ng_iface_rcvdata;
125 static ng_disconnect_t  ng_iface_disconnect;
126
127 /* Helper stuff */
128 static iffam_p  get_iffam_from_af(sa_family_t family);
129 static iffam_p  get_iffam_from_hook(priv_p priv, hook_p hook);
130 static iffam_p  get_iffam_from_name(const char *name);
131 static hook_p  *get_hook_from_iffam(priv_p priv, iffam_p iffam);
132
133 /* Parse type for struct ng_iface_ifname */
134 static const struct ng_parse_fixedstring_info ng_iface_ifname_info = {
135         NG_IFACE_IFACE_NAME_MAX + 1
136 };
137 static const struct ng_parse_type ng_iface_ifname_type = {
138         &ng_parse_fixedstring_type,
139         &ng_iface_ifname_info
140 };
141
142 /* Parse type for struct ng_cisco_ipaddr */
143 static const struct ng_parse_struct_field ng_cisco_ipaddr_type_fields[]
144         = NG_CISCO_IPADDR_TYPE_INFO;
145 static const struct ng_parse_type ng_cisco_ipaddr_type = {
146         &ng_parse_struct_type,
147         &ng_cisco_ipaddr_type_fields
148 };
149
150 /* List of commands and how to convert arguments to/from ASCII */
151 static const struct ng_cmdlist ng_iface_cmds[] = {
152         {
153           NGM_IFACE_COOKIE,
154           NGM_IFACE_GET_IFNAME,
155           "getifname",
156           NULL,
157           &ng_iface_ifname_type
158         },
159         {
160           NGM_IFACE_COOKIE,
161           NGM_IFACE_POINT2POINT,
162           "point2point",
163           NULL,
164           NULL
165         },
166         {
167           NGM_IFACE_COOKIE,
168           NGM_IFACE_BROADCAST,
169           "broadcast",
170           NULL,
171           NULL
172         },
173         {
174           NGM_CISCO_COOKIE,
175           NGM_CISCO_GET_IPADDR,
176           "getipaddr",
177           NULL,
178           &ng_cisco_ipaddr_type
179         },
180         { 0 }
181 };
182
183 /* Node type descriptor */
184 static struct ng_type typestruct = {
185         NG_VERSION,
186         NG_IFACE_NODE_TYPE,
187         NULL,
188         ng_iface_constructor,
189         ng_iface_rcvmsg,
190         ng_iface_rmnode,
191         ng_iface_newhook,
192         NULL,
193         NULL,
194         ng_iface_rcvdata,
195         ng_iface_rcvdata,
196         ng_iface_disconnect,
197         ng_iface_cmds
198 };
199 NETGRAPH_INIT(iface, &typestruct);
200
201 /* We keep a bitmap indicating which unit numbers are free.
202    One means the unit number is free, zero means it's taken. */
203 static int      *ng_iface_units = NULL;
204 static int      ng_iface_units_len = 0;
205
206 #define UNITS_BITSPERWORD       (sizeof(*ng_iface_units) * NBBY)
207
208 /************************************************************************
209                         HELPER STUFF
210  ************************************************************************/
211
212 /*
213  * Get the family descriptor from the family ID
214  */
215 static __inline__ iffam_p
216 get_iffam_from_af(sa_family_t family)
217 {
218         iffam_p iffam;
219         int k;
220
221         for (k = 0; k < NUM_FAMILIES; k++) {
222                 iffam = &gFamilies[k];
223                 if (iffam->family == family)
224                         return (iffam);
225         }
226         return (NULL);
227 }
228
229 /*
230  * Get the family descriptor from the hook
231  */
232 static __inline__ iffam_p
233 get_iffam_from_hook(priv_p priv, hook_p hook)
234 {
235         int k;
236
237         for (k = 0; k < NUM_FAMILIES; k++)
238                 if (priv->hooks[k] == hook)
239                         return (&gFamilies[k]);
240         return (NULL);
241 }
242
243 /*
244  * Get the hook from the iffam descriptor
245  */
246
247 static __inline__ hook_p *
248 get_hook_from_iffam(priv_p priv, iffam_p iffam)
249 {
250         return (&priv->hooks[iffam - gFamilies]);
251 }
252
253 /*
254  * Get the iffam descriptor from the name
255  */
256 static __inline__ iffam_p
257 get_iffam_from_name(const char *name)
258 {
259         iffam_p iffam;
260         int k;
261
262         for (k = 0; k < NUM_FAMILIES; k++) {
263                 iffam = &gFamilies[k];
264                 if (!strcmp(iffam->hookname, name))
265                         return (iffam);
266         }
267         return (NULL);
268 }
269
270 /*
271  * Find the first free unit number for a new interface.
272  * Increase the size of the unit bitmap as necessary.
273  */
274 static __inline__ int
275 ng_iface_get_unit(int *unit)
276 {
277         int index, bit;
278
279         for (index = 0; index < ng_iface_units_len
280             && ng_iface_units[index] == 0; index++);
281         if (index == ng_iface_units_len) {              /* extend array */
282                 int i, *newarray, newlen;
283
284                 newlen = (2 * ng_iface_units_len) + 4;
285                 MALLOC(newarray, int *, newlen * sizeof(*ng_iface_units),
286                     M_NETGRAPH, M_NOWAIT);
287                 if (newarray == NULL)
288                         return (ENOMEM);
289                 bcopy(ng_iface_units, newarray,
290                     ng_iface_units_len * sizeof(*ng_iface_units));
291                 for (i = ng_iface_units_len; i < newlen; i++)
292                         newarray[i] = ~0;
293                 if (ng_iface_units != NULL)
294                         FREE(ng_iface_units, M_NETGRAPH);
295                 ng_iface_units = newarray;
296                 ng_iface_units_len = newlen;
297         }
298         bit = ffs(ng_iface_units[index]) - 1;
299         KASSERT(bit >= 0 && bit <= UNITS_BITSPERWORD - 1,
300             ("%s: word=%d bit=%d", __FUNCTION__, ng_iface_units[index], bit));
301         ng_iface_units[index] &= ~(1 << bit);
302         *unit = (index * UNITS_BITSPERWORD) + bit;
303         return (0);
304 }
305
306 /*
307  * Free a no longer needed unit number.
308  */
309 static __inline__ void
310 ng_iface_free_unit(int unit)
311 {
312         int index, bit;
313
314         index = unit / UNITS_BITSPERWORD;
315         bit = unit % UNITS_BITSPERWORD;
316         KASSERT(index < ng_iface_units_len,
317             ("%s: unit=%d len=%d", __FUNCTION__, unit, ng_iface_units_len));
318         KASSERT((ng_iface_units[index] & (1 << bit)) == 0,
319             ("%s: unit=%d is free", __FUNCTION__, unit));
320         ng_iface_units[index] |= (1 << bit);
321         /*
322          * XXX We could think about reducing the size of ng_iface_units[]
323          * XXX here if the last portion is all ones
324          */
325 }
326
327 /************************************************************************
328                         INTERFACE STUFF
329  ************************************************************************/
330
331 /*
332  * Process an ioctl for the virtual interface
333  */
334 static int
335 ng_iface_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
336 {
337         struct ifreq *const ifr = (struct ifreq *) data;
338         int s, error = 0;
339
340 #ifdef DEBUG
341         ng_iface_print_ioctl(ifp, command, data);
342 #endif
343         s = splimp();
344         switch (command) {
345
346         /* These two are mostly handled at a higher layer */
347         case SIOCSIFADDR:
348                 ifp->if_flags |= (IFF_UP | IFF_RUNNING);
349                 ifp->if_flags &= ~(IFF_OACTIVE);
350                 break;
351         case SIOCGIFADDR:
352                 break;
353
354         /* Set flags */
355         case SIOCSIFFLAGS:
356                 /*
357                  * If the interface is marked up and stopped, then start it.
358                  * If it is marked down and running, then stop it.
359                  */
360                 if (ifr->ifr_flags & IFF_UP) {
361                         if (!(ifp->if_flags & IFF_RUNNING)) {
362                                 ifp->if_flags &= ~(IFF_OACTIVE);
363                                 ifp->if_flags |= IFF_RUNNING;
364                         }
365                 } else {
366                         if (ifp->if_flags & IFF_RUNNING)
367                                 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
368                 }
369                 break;
370
371         /* Set the interface MTU */
372         case SIOCSIFMTU:
373                 if (ifr->ifr_mtu > NG_IFACE_MTU_MAX
374                     || ifr->ifr_mtu < NG_IFACE_MTU_MIN)
375                         error = EINVAL;
376                 else
377                         ifp->if_mtu = ifr->ifr_mtu;
378                 break;
379
380         /* Stuff that's not supported */
381         case SIOCADDMULTI:
382         case SIOCDELMULTI:
383                 error = 0;
384                 break;
385         case SIOCSIFPHYS:
386                 error = EOPNOTSUPP;
387                 break;
388
389         default:
390                 error = EINVAL;
391                 break;
392         }
393         (void) splx(s);
394         return (error);
395 }
396
397 /*
398  * This routine is called to deliver a packet out the interface.
399  * We simply look at the address family and relay the packet to
400  * the corresponding hook, if it exists and is connected.
401  */
402
403 static int
404 ng_iface_output(struct ifnet *ifp, struct mbuf *m,
405                 struct sockaddr *dst, struct rtentry *rt0)
406 {
407         const priv_p priv = (priv_p) ifp->if_softc;
408         const iffam_p iffam = get_iffam_from_af(dst->sa_family);
409         meta_p meta = NULL;
410         int len, error = 0;
411
412         /* Check interface flags */
413         if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING)) {
414                 m_freem(m);
415                 return (ENETDOWN);
416         }
417
418         /* BPF writes need to be handled specially */
419         if (dst->sa_family == AF_UNSPEC) {
420                 if (m->m_len < 4 && (m = m_pullup(m, 4)) == NULL)
421                         return (ENOBUFS);
422                 dst->sa_family = (sa_family_t)*mtod(m, int32_t *);
423                 m->m_data += 4;
424                 m->m_len -= 4;
425                 m->m_pkthdr.len -= 4;
426         }
427
428         /* Berkeley packet filter */
429         ng_iface_bpftap(ifp, m, dst->sa_family);
430
431         /* Check address family to determine hook (if known) */
432         if (iffam == NULL) {
433                 m_freem(m);
434                 log(LOG_WARNING, "%s%d: can't handle af%d\n",
435                        ifp->if_name, ifp->if_unit, (int)dst->sa_family);
436                 return (EAFNOSUPPORT);
437         }
438
439         /* Copy length before the mbuf gets invalidated */
440         len = m->m_pkthdr.len;
441
442         /* Send packet; if hook is not connected, mbuf will get freed. */
443         NG_SEND_DATA(error, *get_hook_from_iffam(priv, iffam), m, meta);
444
445         /* Update stats */
446         if (error == 0) {
447                 ifp->if_obytes += len;
448                 ifp->if_opackets++;
449         }
450         return (error);
451 }
452
453 /*
454  * This routine should never be called
455  */
456
457 static void
458 ng_iface_start(struct ifnet *ifp)
459 {
460         printf("%s%d: %s called?", ifp->if_name, ifp->if_unit, __FUNCTION__);
461 }
462
463 /*
464  * Flash a packet by the BPF (requires prepending 4 byte AF header)
465  * Note the phoney mbuf; this is OK because BPF treats it read-only.
466  */
467 static void
468 ng_iface_bpftap(struct ifnet *ifp, struct mbuf *m, sa_family_t family)
469 {
470         int32_t family4 = (int32_t)family;
471         struct mbuf m0;
472
473         KASSERT(family != AF_UNSPEC, ("%s: family=AF_UNSPEC", __FUNCTION__));
474         if (ifp->if_bpf != NULL) {
475                 bzero(&m0, sizeof(m0));
476                 m0.m_next = m;
477                 m0.m_len = sizeof(family4);
478                 m0.m_data = (char *)&family4;
479                 bpf_mtap(ifp, &m0);
480         }
481 }
482
483 #ifdef DEBUG
484 /*
485  * Display an ioctl to the virtual interface
486  */
487
488 static void
489 ng_iface_print_ioctl(struct ifnet *ifp, int command, caddr_t data)
490 {
491         char   *str;
492
493         switch (command & IOC_DIRMASK) {
494         case IOC_VOID:
495                 str = "IO";
496                 break;
497         case IOC_OUT:
498                 str = "IOR";
499                 break;
500         case IOC_IN:
501                 str = "IOW";
502                 break;
503         case IOC_INOUT:
504                 str = "IORW";
505                 break;
506         default:
507                 str = "IO??";
508         }
509         log(LOG_DEBUG, "%s%d: %s('%c', %d, char[%d])\n",
510                ifp->if_name, ifp->if_unit,
511                str,
512                IOCGROUP(command),
513                command & 0xff,
514                IOCPARM_LEN(command));
515 }
516 #endif /* DEBUG */
517
518 /************************************************************************
519                         NETGRAPH NODE STUFF
520  ************************************************************************/
521
522 /*
523  * Constructor for a node
524  */
525 static int
526 ng_iface_constructor(node_p *nodep)
527 {
528         char ifname[NG_IFACE_IFACE_NAME_MAX + 1];
529         struct ifnet *ifp;
530         node_p node;
531         priv_p priv;
532         int error = 0;
533
534         /* Allocate node and interface private structures */
535         MALLOC(priv, priv_p, sizeof(*priv), M_NETGRAPH, M_NOWAIT);
536         if (priv == NULL)
537                 return (ENOMEM);
538         bzero(priv, sizeof(*priv));
539         MALLOC(ifp, struct ifnet *, sizeof(*ifp), M_NETGRAPH, M_NOWAIT);
540         if (ifp == NULL) {
541                 FREE(priv, M_NETGRAPH);
542                 return (ENOMEM);
543         }
544         bzero(ifp, sizeof(*ifp));
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         ifp->if_name = NG_IFACE_IFACE_NAME;
572         ifp->if_unit = priv->unit;
573         ifp->if_output = ng_iface_output;
574         ifp->if_start = ng_iface_start;
575         ifp->if_ioctl = ng_iface_ioctl;
576         ifp->if_watchdog = NULL;
577         ifp->if_snd.ifq_maxlen = IFQ_MAXLEN;
578         ifp->if_mtu = NG_IFACE_MTU_DEFAULT;
579         ifp->if_flags = (IFF_SIMPLEX|IFF_POINTOPOINT|IFF_NOARP|IFF_MULTICAST);
580         ifp->if_type = IFT_PROPVIRTUAL;         /* XXX */
581         ifp->if_addrlen = 0;                    /* XXX */
582         ifp->if_hdrlen = 0;                     /* XXX */
583         ifp->if_baudrate = 64000;               /* XXX */
584         TAILQ_INIT(&ifp->if_addrhead);
585
586         /* Give this node the same name as the interface (if possible) */
587         bzero(ifname, sizeof(ifname));
588         snprintf(ifname, sizeof(ifname), "%s%d", ifp->if_name, ifp->if_unit);
589         if (ng_name_node(node, ifname) != 0)
590                 log(LOG_WARNING, "%s: can't acquire netgraph name\n", ifname);
591
592         /* Attach the interface */
593         if_attach(ifp);
594         bpfattach(ifp, DLT_NULL, sizeof(u_int));
595
596         /* Done */
597         return (0);
598 }
599
600 /*
601  * Give our ok for a hook to be added
602  */
603 static int
604 ng_iface_newhook(node_p node, hook_p hook, const char *name)
605 {
606         const iffam_p iffam = get_iffam_from_name(name);
607         hook_p *hookptr;
608
609         if (iffam == NULL)
610                 return (EPFNOSUPPORT);
611         hookptr = get_hook_from_iffam((priv_p) node->private, iffam);
612         if (*hookptr != NULL)
613                 return (EISCONN);
614         *hookptr = hook;
615         return (0);
616 }
617
618 /*
619  * Receive a control message
620  */
621 static int
622 ng_iface_rcvmsg(node_p node, struct ng_mesg *msg,
623                 const char *retaddr, struct ng_mesg **rptr)
624 {
625         const priv_p priv = node->private;
626         struct ifnet *const ifp = priv->ifp;
627         struct ng_mesg *resp = NULL;
628         int error = 0;
629
630         switch (msg->header.typecookie) {
631         case NGM_IFACE_COOKIE:
632                 switch (msg->header.cmd) {
633                 case NGM_IFACE_GET_IFNAME:
634                     {
635                         struct ng_iface_ifname *arg;
636
637                         NG_MKRESPONSE(resp, msg, sizeof(*arg), M_NOWAIT);
638                         if (resp == NULL) {
639                                 error = ENOMEM;
640                                 break;
641                         }
642                         arg = (struct ng_iface_ifname *)resp->data;
643                         snprintf(arg->ngif_name, sizeof(arg->ngif_name),
644                             "%s%d", ifp->if_name, ifp->if_unit);
645                         break;
646                     }
647
648                 case NGM_IFACE_POINT2POINT:
649                 case NGM_IFACE_BROADCAST:
650                     {
651
652                         /* Deny request if interface is UP */
653                         if ((ifp->if_flags & IFF_UP) != 0)
654                                 return (EBUSY);
655
656                         /* Change flags */
657                         switch (msg->header.cmd) {
658                         case NGM_IFACE_POINT2POINT:
659                                 ifp->if_flags |= IFF_POINTOPOINT;
660                                 ifp->if_flags &= ~IFF_BROADCAST;
661                                 break;
662                         case NGM_IFACE_BROADCAST:
663                                 ifp->if_flags &= ~IFF_POINTOPOINT;
664                                 ifp->if_flags |= IFF_BROADCAST;
665                                 break;
666                         }
667                         break;
668                     }
669
670                 default:
671                         error = EINVAL;
672                         break;
673                 }
674                 break;
675         case NGM_CISCO_COOKIE:
676                 switch (msg->header.cmd) {
677                 case NGM_CISCO_GET_IPADDR:      /* we understand this too */
678                     {
679                         struct ifaddr *ifa;
680
681                         /* Return the first configured IP address */
682                         TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
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 (ifa == 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
732         /* Sanity checks */
733         KASSERT(iffam != NULL, ("%s: iffam", __FUNCTION__));
734         KASSERT(m->m_flags & M_PKTHDR, ("%s: not pkthdr", __FUNCTION__));
735         if (m == NULL)
736                 return (EINVAL);
737         if ((ifp->if_flags & IFF_UP) == 0) {
738                 NG_FREE_DATA(m, meta);
739                 return (ENETDOWN);
740         }
741
742         /* Update interface stats */
743         ifp->if_ipackets++;
744         ifp->if_ibytes += m->m_pkthdr.len;
745
746         /* Note receiving interface */
747         m->m_pkthdr.rcvif = ifp;
748
749         /* Berkeley packet filter */
750         ng_iface_bpftap(ifp, m, iffam->family);
751
752         /* Ignore any meta-data */
753         NG_FREE_META(meta);
754
755         /* Send packet */
756         return family_enqueue(iffam->family, m);
757 }
758
759 /*
760  * Shutdown and remove the node and its associated interface.
761  */
762 static int
763 ng_iface_rmnode(node_p node)
764 {
765         const priv_p priv = node->private;
766
767         ng_cutlinks(node);
768         ng_unname(node);
769         bpfdetach(priv->ifp);
770         if_detach(priv->ifp);
771         FREE(priv->ifp, M_NETGRAPH);
772         priv->ifp = NULL;
773         ng_iface_free_unit(priv->unit);
774         FREE(priv, M_NETGRAPH);
775         node->private = NULL;
776         ng_unref(node);
777         return (0);
778 }
779
780 /*
781  * Hook disconnection. Note that we do *not* shutdown when all
782  * hooks have been disconnected.
783  */
784 static int
785 ng_iface_disconnect(hook_p hook)
786 {
787         const priv_p priv = hook->node->private;
788         const iffam_p iffam = get_iffam_from_hook(priv, hook);
789
790         if (iffam == NULL)
791                 panic(__FUNCTION__);
792         *get_hook_from_iffam(priv, iffam) = NULL;
793         return (0);
794 }
795