Parallelize ifnet.if_addrhead accessing by duplicating the list itself
[dragonfly.git] / sys / net / pf / pf_if.c
1 /*      $FreeBSD: src/sys/contrib/pf/net/pf_if.c,v 1.6 2004/09/14 15:20:24 mlaier Exp $ */
2 /*      $OpenBSD: pf_if.c,v 1.11 2004/03/15 11:38:23 cedric Exp $ */
3 /* add  $OpenBSD: pf_if.c,v 1.19 2004/08/11 12:06:44 henning Exp $ */
4 /*      $DragonFly: src/sys/net/pf/pf_if.c,v 1.10 2008/03/07 11:34:20 sephe Exp $ */
5
6 /*
7  * Copyright (c) 2004 The DragonFly Project.  All rights reserved.
8  *
9  * Copyright (c) 2001 Daniel Hartmeier
10  * Copyright (c) 2003 Cedric Berger
11  * All rights reserved.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  *
17  *    - Redistributions of source code must retain the above copyright
18  *      notice, this list of conditions and the following disclaimer.
19  *    - Redistributions in binary form must reproduce the above
20  *      copyright notice, this list of conditions and the following
21  *      disclaimer in the documentation and/or other materials provided
22  *      with the distribution.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
27  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
28  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
29  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
30  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
34  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35  * POSSIBILITY OF SUCH DAMAGE.
36  */
37
38 #include "opt_inet.h"
39 #include "opt_inet6.h"
40
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/malloc.h>
44 #include <sys/mbuf.h>
45 #include <sys/eventhandler.h>
46 #include <sys/filio.h>
47 #include <sys/socket.h>
48 #include <sys/socketvar.h>
49 #include <sys/kernel.h>
50 #include <sys/thread2.h>
51 #include <sys/time.h>
52 #include <vm/vm_zone.h>
53
54 #include <net/if.h>
55 #include <net/if_types.h>
56 #include <net/route.h>
57
58 #include <netinet/in.h>
59 #include <netinet/in_var.h>
60 #include <netinet/in_systm.h>
61 #include <netinet/ip.h>
62 #include <netinet/ip_var.h>
63
64 #include <net/pf/pfvar.h>
65
66 #ifdef INET6
67 #include <netinet/ip6.h>
68 #endif /* INET6 */
69
70 #define ACCEPT_FLAGS(oklist)                    \
71         do {                                    \
72                 if ((flags & ~(oklist)) &       \
73                     PFI_FLAG_ALLMASK)           \
74                         return (EINVAL);        \
75         } while (0)
76
77 #define senderr(e)      do { rv = (e); goto _bad; } while (0)
78
79 struct pfi_kif          **pfi_index2kif;
80 struct pfi_kif           *pfi_self, *pfi_dummy;
81 int                       pfi_indexlim;
82 struct pfi_ifhead         pfi_ifs;
83 struct pfi_statehead      pfi_statehead;
84 int                       pfi_ifcnt;
85 vm_zone_t                 pfi_addr_pl;
86 long                      pfi_update = 1;
87 struct pfr_addr          *pfi_buffer;
88 int                       pfi_buffer_cnt;
89 int                       pfi_buffer_max;
90 char                      pfi_reserved_anchor[PF_ANCHOR_NAME_SIZE] =
91                                 PF_RESERVED_ANCHOR;
92 char                      pfi_interface_ruleset[PF_RULESET_NAME_SIZE] =
93                                 PF_INTERFACE_RULESET;
94
95 eventhandler_tag         pfi_clone_cookie = NULL;
96 eventhandler_tag         pfi_attach_cookie = NULL;
97 eventhandler_tag         pfi_detach_cookie = NULL;
98
99 void             pfi_dynaddr_update(void *);
100 void             pfi_kifaddr_update(void *);
101 void             pfi_table_update(struct pfr_ktable *, struct pfi_kif *,
102                     int, int);
103 void             pfi_instance_add(struct ifnet *, int, int);
104 void             pfi_address_add(struct sockaddr *, int, int);
105 int              pfi_if_compare(struct pfi_kif *, struct pfi_kif *);
106 struct pfi_kif  *pfi_if_create(const char *, struct pfi_kif *, int);
107 void             pfi_copy_group(char *, const char *, int);
108 void             pfi_dynamic_drivers(void);
109 void             pfi_newgroup(const char *, int);
110 int              pfi_skip_if(const char *, struct pfi_kif *, int);
111 int              pfi_unmask(void *);
112 void             pfi_dohooks(struct pfi_kif *);
113 void             pfi_kifaddr_update_event(void *, struct ifnet *);
114 void             pfi_attach_clone_event(void *, struct if_clone *);
115 void             pfi_attach_ifnet_event(void *, struct ifnet *);
116 void             pfi_detach_ifnet_event(void *, struct ifnet *);
117
118 RB_PROTOTYPE(pfi_ifhead, pfi_kif, pfik_tree, pfi_if_compare);
119 RB_GENERATE(pfi_ifhead, pfi_kif, pfik_tree, pfi_if_compare);
120
121 #define PFI_DYNAMIC_BUSES       { "pcmcia", "cardbus", "uhub" }
122 #define PFI_BUFFER_MAX          0x10000
123 MALLOC_DEFINE(PFI_MTYPE, "pf_if", "pf interface table");
124
125 void
126 pfi_initialize(void)
127 {
128         struct ifnet    *ifp;
129
130         if (pfi_self != NULL)   /* already initialized */
131                 return;
132
133         TAILQ_INIT(&pfi_statehead);
134         pfi_buffer_max = 64;
135         pfi_buffer = kmalloc(pfi_buffer_max * sizeof(*pfi_buffer),
136             PFI_MTYPE, M_WAITOK);
137         pfi_self = pfi_if_create("self", NULL, PFI_IFLAG_GROUP);
138         pfi_dynamic_drivers();
139
140         TAILQ_FOREACH(ifp, &ifnet, if_link) {
141                 if (ifp->if_dunit != IF_DUNIT_NONE)
142                         pfi_attach_ifnet(ifp);
143         }
144         pfi_dummy = pfi_if_create("notyet", pfi_self,
145             PFI_IFLAG_GROUP | PFI_IFLAG_DYNAMIC);
146         pfi_attach_cookie = EVENTHANDLER_REGISTER(ifnet_attach_event,
147             pfi_attach_ifnet_event, NULL, EVENTHANDLER_PRI_ANY);
148         pfi_detach_cookie = EVENTHANDLER_REGISTER(ifnet_detach_event,
149             pfi_detach_ifnet_event, NULL, EVENTHANDLER_PRI_ANY);
150         pfi_clone_cookie = EVENTHANDLER_REGISTER(if_clone_event,
151             pfi_attach_clone_event, NULL, EVENTHANDLER_PRI_ANY);
152 }
153
154 void
155 pfi_cleanup(void)
156 {
157         struct pfi_kif *p, key;
158         struct ifnet *ifp;
159
160         EVENTHANDLER_DEREGISTER(ifnet_attach_event, pfi_attach_cookie);
161         EVENTHANDLER_DEREGISTER(ifnet_detach_event, pfi_detach_cookie);
162         EVENTHANDLER_DEREGISTER(if_clone_event, pfi_clone_cookie);
163
164         /* release PFI_IFLAG_INSTANCE */
165         TAILQ_FOREACH(ifp, &ifnet, if_link) {
166                 strlcpy(key.pfik_name, ifp->if_xname, sizeof(key.pfik_name));
167                 p = RB_FIND(pfi_ifhead, &pfi_ifs, &key);
168                 if (p != NULL)
169                         pfi_detach_ifnet(ifp);
170         }
171
172         /* XXX clear all other interface group */
173         while ((p = RB_MIN(pfi_ifhead, &pfi_ifs))) {
174                 RB_REMOVE(pfi_ifhead, &pfi_ifs, p);
175
176                 kfree(p->pfik_ah_head, PFI_MTYPE);
177                 kfree(p, PFI_MTYPE);
178         }
179         kfree(pfi_index2kif, PFI_MTYPE);
180         kfree(pfi_buffer, PFI_MTYPE);
181         pfi_index2kif = NULL;
182         pfi_buffer = NULL;
183         pfi_self = NULL;
184 }
185
186 /*
187  * Wrapper functions for FreeBSD eventhandler
188  */
189 void
190 pfi_kifaddr_update_event(void *arg, struct ifnet *ifp)
191 {
192         struct pfi_kif *p = arg;
193         
194         /* 
195          * Check to see if it is 'our' interface as we do not have per
196          * interface hooks and thus get an update for every interface.
197          */
198         if (p && p->pfik_ifp == ifp)
199                 pfi_kifaddr_update(p);
200 }
201
202 void
203 pfi_attach_clone_event(void *arg __unused, struct if_clone *ifc)
204 {
205         pfi_attach_clone(ifc);
206 }
207
208 void
209 pfi_attach_ifnet_event(void *arg __unused, struct ifnet *ifp)
210 {
211         if (ifp->if_dunit != IF_DUNIT_NONE)
212                 pfi_attach_ifnet(ifp);
213 }
214
215 void
216 pfi_detach_ifnet_event(void *arg __unused, struct ifnet *ifp)
217 {
218         pfi_detach_ifnet(ifp);
219 }
220
221 void
222 pfi_attach_clone(struct if_clone *ifc)
223 {
224         pfi_initialize();
225         pfi_newgroup(ifc->ifc_name, PFI_IFLAG_CLONABLE);
226 }
227
228 void
229 pfi_attach_ifnet(struct ifnet *ifp)
230 {
231         struct pfi_kif  *p, *q, key;
232         int              realname;
233
234         pfi_initialize();
235         crit_enter();
236         pfi_update++;
237         if (ifp->if_index >= pfi_indexlim) {
238                 /*
239                  * grow pfi_index2kif,  similar to ifindex2ifnet code in if.c
240                  */
241                 size_t m, n, oldlim;
242                 struct pfi_kif **mp, **np;
243
244                 oldlim = pfi_indexlim;
245                 if (pfi_indexlim == 0)
246                         pfi_indexlim = 64;
247                 while (ifp->if_index >= pfi_indexlim)
248                         pfi_indexlim <<= 1;
249
250                 m = oldlim * sizeof(struct pfi_kif *);
251                 mp = pfi_index2kif;
252                 n = pfi_indexlim * sizeof(struct pfi_kif *);
253                 np = kmalloc(n, PFI_MTYPE, M_NOWAIT | M_ZERO);
254                 if (np == NULL)
255                         panic("pfi_attach_ifnet: "
256                             "cannot allocate translation table");
257                 if (mp != NULL)
258                         bcopy(mp, np, m);
259                 pfi_index2kif = np;
260                 if (mp != NULL)
261                         kfree(mp, PFI_MTYPE);
262         }
263
264         strlcpy(key.pfik_name, ifp->if_xname, sizeof(key.pfik_name));
265         p = RB_FIND(pfi_ifhead, &pfi_ifs, &key);
266         /* some additional trickery for placeholders */
267         if ((p == NULL) || (p->pfik_parent == pfi_dummy)) {
268                 /* are we looking at a renamed instance or not? */
269                 pfi_copy_group(key.pfik_name, ifp->if_xname,
270                     sizeof(key.pfik_name));
271                 realname = (strncmp(key.pfik_name, ifp->if_dname,
272                     sizeof(key.pfik_name)) == 0);
273                 /* add group */
274                 /* we can change if_xname, hence use if_dname as group id */
275                 pfi_copy_group(key.pfik_name, ifp->if_dname,
276                     sizeof(key.pfik_name));
277                 q = RB_FIND(pfi_ifhead, &pfi_ifs, &key);
278                 if (q == NULL)
279                     q = pfi_if_create(key.pfik_name, pfi_self,
280                         PFI_IFLAG_GROUP|PFI_IFLAG_DYNAMIC);
281                 else if (q->pfik_parent == pfi_dummy) {
282                         q->pfik_parent = pfi_self;
283                         q->pfik_flags = (PFI_IFLAG_GROUP | PFI_IFLAG_DYNAMIC);
284                 }
285                 if (q == NULL)
286                         panic("pfi_attach_ifnet: "
287                             "cannot allocate '%s' group", key.pfik_name);
288
289                 /* add/modify interface */
290                 if (p == NULL)
291                         p = pfi_if_create(ifp->if_xname, q,
292                             realname?PFI_IFLAG_INSTANCE:PFI_IFLAG_PLACEHOLDER);
293                 else {
294                         /* remove from the dummy group */
295                         /* XXX: copy stats? We should not have any!!! */
296                         pfi_dummy->pfik_delcnt++;
297                         TAILQ_REMOVE(&pfi_dummy->pfik_grouphead, p,
298                             pfik_instances);
299                         /* move to the right group */
300                         p->pfik_parent = q;
301                         q->pfik_addcnt++;
302                         TAILQ_INSERT_TAIL(&q->pfik_grouphead, p,
303                             pfik_instances);
304                         if (realname) {
305                                 p->pfik_flags &= ~PFI_IFLAG_PLACEHOLDER;
306                                 p->pfik_flags |= PFI_IFLAG_INSTANCE;
307                         }
308                 }
309                 if (p == NULL)
310                         panic("pfi_attach_ifnet: "
311                             "cannot allocate '%s' interface", ifp->if_xname);
312         } else
313                 q = p->pfik_parent;
314         p->pfik_ifp = ifp;
315         p->pfik_flags |= PFI_IFLAG_ATTACHED;
316         p->pfik_ah_cookie = EVENTHANDLER_REGISTER(ifaddr_event,
317             pfi_kifaddr_update_event, p, EVENTHANDLER_PRI_ANY);
318         pfi_index2kif[ifp->if_index] = p;
319         pfi_dohooks(p);
320         crit_exit();
321 }
322
323 void
324 pfi_detach_ifnet(struct ifnet *ifp)
325 {
326         struct pfi_kif  *p, *q, key;
327
328         strlcpy(key.pfik_name, ifp->if_xname, sizeof(key.pfik_name));
329
330         crit_enter();
331         pfi_update++;
332         p = RB_FIND(pfi_ifhead, &pfi_ifs, &key);
333         if (p == NULL) {
334                 kprintf("pfi_detach_ifnet: cannot find %s", ifp->if_xname);
335                 crit_exit();
336                 return;
337         }
338         EVENTHANDLER_DEREGISTER(ifaddr_event, p->pfik_ah_cookie);
339         q = p->pfik_parent;
340         p->pfik_ifp = NULL;
341         p->pfik_flags &= ~PFI_IFLAG_ATTACHED;
342         pfi_index2kif[ifp->if_index] = NULL;
343         pfi_dohooks(p);
344         pfi_maybe_destroy(p);
345         crit_exit();
346 }
347
348 struct pfi_kif *
349 pfi_lookup_create(const char *name)
350 {
351         struct pfi_kif  *p, *q, key;
352
353         crit_enter();
354         p = pfi_lookup_if(name);
355         if (p == NULL) {
356                 pfi_copy_group(key.pfik_name, name, sizeof(key.pfik_name));
357                 q = pfi_lookup_if(key.pfik_name);
358                 if ((q != NULL) && (q->pfik_parent != pfi_dummy))
359                         p = pfi_if_create(name, q, PFI_IFLAG_INSTANCE);
360                 else {
361                         if (pfi_dummy == NULL)
362                                 panic("no 'notyet' dummy group");
363                         p = pfi_if_create(name, pfi_dummy,
364                             PFI_IFLAG_PLACEHOLDER);
365                 }
366         }
367         crit_exit();
368         return (p);
369 }
370
371 struct pfi_kif *
372 pfi_attach_rule(const char *name)
373 {
374         struct pfi_kif  *p;
375
376         p = pfi_lookup_create(name);
377         if (p != NULL)
378                 p->pfik_rules++;
379         return (p);
380 }
381
382 void
383 pfi_detach_rule(struct pfi_kif *p)
384 {
385         if (p == NULL)
386                 return;
387         if (p->pfik_rules > 0)
388                 p->pfik_rules--;
389         else
390                 kprintf("pfi_detach_rule: reference count at 0\n");
391         pfi_maybe_destroy(p);
392 }
393
394 void
395 pfi_attach_state(struct pfi_kif *p)
396 {
397         if (!p->pfik_states++)
398                 TAILQ_INSERT_TAIL(&pfi_statehead, p, pfik_w_states);
399 }
400
401 void
402 pfi_detach_state(struct pfi_kif *p)
403 {
404         if (p == NULL)
405                 return;
406         if (p->pfik_states <= 0) {
407                 kprintf("pfi_detach_state: reference count <= 0\n");
408                 return;
409         }
410         if (!--p->pfik_states)
411                 TAILQ_REMOVE(&pfi_statehead, p, pfik_w_states);
412         pfi_maybe_destroy(p);
413 }
414
415 int
416 pfi_dynaddr_setup(struct pf_addr_wrap *aw, sa_family_t af)
417 {
418         struct pfi_dynaddr      *dyn;
419         char                     tblname[PF_TABLE_NAME_SIZE];
420         struct pf_ruleset       *ruleset = NULL;
421         int                      rv = 0;
422
423         if (aw->type != PF_ADDR_DYNIFTL)
424                 return (0);
425         dyn = pool_get(&pfi_addr_pl, PR_NOWAIT);
426         if (dyn == NULL)
427                 return (1);
428         bzero(dyn, sizeof(*dyn));
429
430         crit_enter();
431         dyn->pfid_kif = pfi_attach_rule(aw->v.ifname);
432         if (dyn->pfid_kif == NULL)
433                 senderr(1);
434
435         dyn->pfid_net = pfi_unmask(&aw->v.a.mask);
436         if (af == AF_INET && dyn->pfid_net == 32)
437                 dyn->pfid_net = 128;
438         strlcpy(tblname, aw->v.ifname, sizeof(tblname));
439         if (aw->iflags & PFI_AFLAG_NETWORK)
440                 strlcat(tblname, ":network", sizeof(tblname));
441         if (aw->iflags & PFI_AFLAG_BROADCAST)
442                 strlcat(tblname, ":broadcast", sizeof(tblname));
443         if (aw->iflags & PFI_AFLAG_PEER)
444                 strlcat(tblname, ":peer", sizeof(tblname));
445         if (aw->iflags & PFI_AFLAG_NOALIAS)
446                 strlcat(tblname, ":0", sizeof(tblname));
447         if (dyn->pfid_net != 128)
448                 ksnprintf(tblname + strlen(tblname),
449                     sizeof(tblname) - strlen(tblname), "/%d", dyn->pfid_net);
450         ruleset = pf_find_or_create_ruleset(pfi_reserved_anchor,
451             pfi_interface_ruleset);
452         if (ruleset == NULL)
453                 senderr(1);
454
455         dyn->pfid_kt = pfr_attach_table(ruleset, tblname);
456         if (dyn->pfid_kt == NULL)
457                 senderr(1);
458
459         dyn->pfid_kt->pfrkt_flags |= PFR_TFLAG_ACTIVE;
460         dyn->pfid_iflags = aw->iflags;
461         dyn->pfid_af = af;
462         dyn->pfid_hook_cookie = hook_establish(dyn->pfid_kif->pfik_ah_head, 1,
463             pfi_dynaddr_update, dyn);
464         if (dyn->pfid_hook_cookie == NULL)
465                 senderr(1);
466
467         aw->p.dyn = dyn;
468         pfi_dynaddr_update(aw->p.dyn);
469         crit_exit();
470         return (0);
471
472 _bad:
473         if (dyn->pfid_kt != NULL)
474                 pfr_detach_table(dyn->pfid_kt);
475         if (ruleset != NULL)
476                 pf_remove_if_empty_ruleset(ruleset);
477         if (dyn->pfid_kif != NULL)
478                 pfi_detach_rule(dyn->pfid_kif);
479         pool_put(&pfi_addr_pl, dyn);
480         crit_exit();
481         return (rv);
482 }
483
484 void
485 pfi_dynaddr_update(void *p)
486 {
487         struct pfi_dynaddr      *dyn = (struct pfi_dynaddr *)p;
488         struct pfi_kif          *kif = dyn->pfid_kif;
489         struct pfr_ktable       *kt = dyn->pfid_kt;
490
491         if (dyn == NULL || kif == NULL || kt == NULL)
492                 panic("pfi_dynaddr_update");
493         if (kt->pfrkt_larg != pfi_update) {
494                 /* this table needs to be brought up-to-date */
495                 pfi_table_update(kt, kif, dyn->pfid_net, dyn->pfid_iflags);
496                 kt->pfrkt_larg = pfi_update;
497         }
498         pfr_dynaddr_update(kt, dyn);
499 }
500
501 void
502 pfi_table_update(struct pfr_ktable *kt, struct pfi_kif *kif, int net, int flags)
503 {
504         int                      e, size2 = 0;
505         struct pfi_kif          *p;
506         struct pfr_table         t;
507
508         if ((kif->pfik_flags & PFI_IFLAG_INSTANCE) && kif->pfik_ifp == NULL) {
509                 pfr_clr_addrs(&kt->pfrkt_t, NULL, 0);
510                 return;
511         }
512         pfi_buffer_cnt = 0;
513         if ((kif->pfik_flags & PFI_IFLAG_INSTANCE))
514                 pfi_instance_add(kif->pfik_ifp, net, flags);
515         else if (strcmp(kif->pfik_name, "self")) {
516                 TAILQ_FOREACH(p, &kif->pfik_grouphead, pfik_instances)
517                         pfi_instance_add(p->pfik_ifp, net, flags);
518         } else {
519                 RB_FOREACH(p, pfi_ifhead, &pfi_ifs)
520                         if (p->pfik_flags & PFI_IFLAG_INSTANCE)
521                                 pfi_instance_add(p->pfik_ifp, net, flags);
522         }
523         t = kt->pfrkt_t;
524         t.pfrt_flags = 0;
525         if ((e = pfr_set_addrs(&t, pfi_buffer, pfi_buffer_cnt, &size2,
526             NULL, NULL, NULL, 0)))
527                 kprintf("pfi_table_update: cannot set %d new addresses "
528                     "into table %s: %d\n", pfi_buffer_cnt, kt->pfrkt_name, e);
529 }
530
531 void
532 pfi_instance_add(struct ifnet *ifp, int net, int flags)
533 {
534         struct ifaddr_container *ifac;
535         int              got4 = 0, got6 = 0;
536         int              net2, af;
537
538         if (ifp == NULL)
539                 return;
540         TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
541                 struct ifaddr *ia = ifac->ifa;
542
543                 if (ia->ifa_addr == NULL)
544                         continue;
545                 af = ia->ifa_addr->sa_family;
546                 if (af != AF_INET && af != AF_INET6)
547                         continue;
548                 /*
549                  * XXX: For point-to-point interfaces, (ifname:0) and IPv4,
550                  *      jump over address without a proper route to work
551                  *      around a problem with ppp not fully removing the
552                  *      address used during IPCP.
553                  */
554                 if ((ifp->if_flags & IFF_POINTOPOINT) &&
555                     !(ia->ifa_flags & IFA_ROUTE) &&
556                     (flags & PFI_AFLAG_NOALIAS) && (af == AF_INET))
557                         continue;
558                 if ((flags & PFI_AFLAG_BROADCAST) && af == AF_INET6)
559                         continue;
560                 if ((flags & PFI_AFLAG_BROADCAST) &&
561                     !(ifp->if_flags & IFF_BROADCAST))
562                         continue;
563                 if ((flags & PFI_AFLAG_PEER) &&
564                     !(ifp->if_flags & IFF_POINTOPOINT))
565                         continue;
566                 if ((flags & PFI_AFLAG_NETWORK) && af == AF_INET6 &&
567                     IN6_IS_ADDR_LINKLOCAL(
568                     &((struct sockaddr_in6 *)ia->ifa_addr)->sin6_addr))
569                         continue;
570                 if (flags & PFI_AFLAG_NOALIAS) {
571                         if (af == AF_INET && got4)
572                                 continue;
573                         if (af == AF_INET6 && got6)
574                                 continue;
575                 }
576                 if (af == AF_INET)
577                         got4 = 1;
578                 else
579                         got6 = 1;
580                 net2 = net;
581                 if (net2 == 128 && (flags & PFI_AFLAG_NETWORK)) {
582                         if (af == AF_INET) {
583                                 net2 = pfi_unmask(&((struct sockaddr_in *)
584                                     ia->ifa_netmask)->sin_addr);
585                         } else {
586                                 net2 = pfi_unmask(&((struct sockaddr_in6 *)
587                                     ia->ifa_netmask)->sin6_addr);
588                         }
589                 }
590                 if (af == AF_INET && net2 > 32)
591                         net2 = 32;
592                 if (flags & PFI_AFLAG_BROADCAST)
593                         pfi_address_add(ia->ifa_broadaddr, af, net2);
594                 else if (flags & PFI_AFLAG_PEER)
595                         pfi_address_add(ia->ifa_dstaddr, af, net2);
596                 else
597                         pfi_address_add(ia->ifa_addr, af, net2);
598         }
599 }
600
601 void
602 pfi_address_add(struct sockaddr *sa, int af, int net)
603 {
604         struct pfr_addr *p;
605         int              i;
606
607         if (pfi_buffer_cnt >= pfi_buffer_max) {
608                 int              new_max = pfi_buffer_max * 2;
609
610                 if (new_max > PFI_BUFFER_MAX) {
611                         kprintf("pfi_address_add: address buffer full (%d/%d)\n",
612                             pfi_buffer_cnt, PFI_BUFFER_MAX);
613                         return;
614                 }
615                 p = kmalloc(new_max * sizeof(*pfi_buffer), PFI_MTYPE,
616                     M_NOWAIT);
617                 if (p == NULL) {
618                         kprintf("pfi_address_add: no memory to grow buffer "
619                             "(%d/%d)\n", pfi_buffer_cnt, PFI_BUFFER_MAX);
620                         return;
621                 }
622                 memcpy(pfi_buffer, p, pfi_buffer_cnt * sizeof(*pfi_buffer));
623                 /* no need to zero buffer */
624                 kfree(pfi_buffer, PFI_MTYPE);
625                 pfi_buffer = p;
626                 pfi_buffer_max = new_max;
627         }
628         if (af == AF_INET && net > 32)
629                 net = 128;
630         p = pfi_buffer + pfi_buffer_cnt++;
631         bzero(p, sizeof(*p));
632         p->pfra_af = af;
633         p->pfra_net = net;
634         if (af == AF_INET)
635                 p->pfra_ip4addr = ((struct sockaddr_in *)sa)->sin_addr;
636         if (af == AF_INET6) {
637                 p->pfra_ip6addr = ((struct sockaddr_in6 *)sa)->sin6_addr;
638                 if (IN6_IS_ADDR_LINKLOCAL(&p->pfra_ip6addr))
639                         p->pfra_ip6addr.s6_addr16[1] = 0;
640         }
641         /* mask network address bits */
642         if (net < 128)
643                 ((caddr_t)p)[p->pfra_net/8] &= ~(0xFF >> (p->pfra_net%8));
644         for (i = (p->pfra_net+7)/8; i < sizeof(p->pfra_u); i++)
645                 ((caddr_t)p)[i] = 0;
646 }
647
648 void
649 pfi_dynaddr_remove(struct pf_addr_wrap *aw)
650 {
651         if (aw->type != PF_ADDR_DYNIFTL || aw->p.dyn == NULL ||
652             aw->p.dyn->pfid_kif == NULL || aw->p.dyn->pfid_kt == NULL)
653                 return;
654
655         crit_enter();
656         hook_disestablish(aw->p.dyn->pfid_kif->pfik_ah_head,
657             aw->p.dyn->pfid_hook_cookie);
658         pfi_detach_rule(aw->p.dyn->pfid_kif);
659         aw->p.dyn->pfid_kif = NULL;
660         pfr_detach_table(aw->p.dyn->pfid_kt);
661         aw->p.dyn->pfid_kt = NULL;
662         pool_put(&pfi_addr_pl, aw->p.dyn);
663         aw->p.dyn = NULL;
664         crit_exit();
665 }
666
667 void
668 pfi_dynaddr_copyout(struct pf_addr_wrap *aw)
669 {
670         if (aw->type != PF_ADDR_DYNIFTL || aw->p.dyn == NULL ||
671             aw->p.dyn->pfid_kif == NULL)
672                 return;
673         aw->p.dyncnt = aw->p.dyn->pfid_acnt4 + aw->p.dyn->pfid_acnt6;
674 }
675
676 void
677 pfi_kifaddr_update(void *v)
678 {
679         crit_enter();
680         pfi_update++;
681         pfi_dohooks(v);
682         crit_exit();
683 }
684
685 int
686 pfi_if_compare(struct pfi_kif *p, struct pfi_kif *q)
687 {
688         return (strncmp(p->pfik_name, q->pfik_name, IFNAMSIZ));
689 }
690
691 struct pfi_kif *
692 pfi_if_create(const char *name, struct pfi_kif *q, int flags)
693 {
694         struct pfi_kif *p;
695
696         p = kmalloc(sizeof(*p), PFI_MTYPE, M_NOWAIT | M_ZERO);
697         if (p == NULL)
698                 return (NULL);
699         p->pfik_ah_head = kmalloc(sizeof(*p->pfik_ah_head), PFI_MTYPE,
700             M_NOWAIT | M_ZERO);
701         if (p->pfik_ah_head == NULL) {
702                 kfree(p, PFI_MTYPE);
703                 return (NULL);
704         }
705         TAILQ_INIT(p->pfik_ah_head);
706         TAILQ_INIT(&p->pfik_grouphead);
707         strlcpy(p->pfik_name, name, sizeof(p->pfik_name));
708         RB_INIT(&p->pfik_lan_ext);
709         RB_INIT(&p->pfik_ext_gwy);
710         p->pfik_flags = flags;
711         p->pfik_parent = q;
712         p->pfik_tzero = time_second;
713
714         RB_INSERT(pfi_ifhead, &pfi_ifs, p);
715         if (q != NULL) {
716                 q->pfik_addcnt++;
717                 TAILQ_INSERT_TAIL(&q->pfik_grouphead, p, pfik_instances);
718         }
719         pfi_ifcnt++;
720         return (p);
721 }
722
723 int
724 pfi_maybe_destroy(struct pfi_kif *p)
725 {
726         int              i, j, k;
727         struct pfi_kif  *q = p->pfik_parent;
728
729         if ((p->pfik_flags & (PFI_IFLAG_ATTACHED | PFI_IFLAG_GROUP)) ||
730             p->pfik_rules > 0 || p->pfik_states > 0)
731                 if (!(p->pfik_flags & PFI_IFLAG_PLACEHOLDER))
732                         return (0);
733
734         crit_enter();
735         if (q != NULL) {
736                 for (i = 0; i < 2; i++)
737                         for (j = 0; j < 2; j++)
738                                 for (k = 0; k < 2; k++) {
739                                         q->pfik_bytes[i][j][k] +=
740                                             p->pfik_bytes[i][j][k];
741                                         q->pfik_packets[i][j][k] +=
742                                             p->pfik_packets[i][j][k];
743                         /* clear stats in case we return to the dummy group */
744                                         p->pfik_bytes[i][j][k] = 0;
745                                         p->pfik_packets[i][j][k] = 0;
746                                 }
747                 q->pfik_delcnt++;
748                 TAILQ_REMOVE(&q->pfik_grouphead, p, pfik_instances);
749         }
750         if (p->pfik_rules > 0 || p->pfik_states > 0) {
751                 /* move back to the dummy group */
752                 p->pfik_parent = pfi_dummy;
753                 pfi_dummy->pfik_addcnt++;
754                 TAILQ_INSERT_TAIL(&pfi_dummy->pfik_grouphead, p,
755                     pfik_instances);
756                 crit_exit();
757                 return (0);
758         }
759         pfi_ifcnt--;
760         RB_REMOVE(pfi_ifhead, &pfi_ifs, p);
761         crit_exit();
762
763         kfree(p->pfik_ah_head, PFI_MTYPE);
764         kfree(p, PFI_MTYPE);
765         return (1);
766 }
767
768 void
769 pfi_copy_group(char *p, const char *q, int m)
770 {
771         while (m > 1 && *q && !(*q >= '0' && *q <= '9')) {
772                 *p++ = *q++;
773                 m--;
774         }
775         if (m > 0)
776                 *p++ = '\0';
777 }
778
779 void
780 pfi_dynamic_drivers(void)
781 {
782         struct ifnet    *ifp;
783
784 /*
785  * For FreeBSD basically every interface is "dynamic" as we can unload
786  * modules e.g.
787  */
788         TAILQ_FOREACH(ifp, &ifnet, if_link) {
789                 if (ifp->if_dunit == IF_DUNIT_NONE)
790                         continue;
791                 pfi_newgroup(ifp->if_dname, PFI_IFLAG_DYNAMIC);
792         }
793 }
794
795 void
796 pfi_newgroup(const char *name, int flags)
797 {
798         struct pfi_kif  *p;
799
800         p = pfi_lookup_if(name);
801         if (p == NULL)
802                 p = pfi_if_create(name, pfi_self, PFI_IFLAG_GROUP);
803         if (p == NULL) {
804                 kprintf("pfi_newgroup: cannot allocate '%s' group", name);
805                 return;
806         }
807         p->pfik_flags |= flags;
808 }
809
810 void
811 pfi_fill_oldstatus(struct pf_status *pfs)
812 {
813         struct pfi_kif  *p, key;
814         int              i, j, k;
815
816         strlcpy(key.pfik_name, pfs->ifname, sizeof(key.pfik_name));
817         crit_enter();
818         p = RB_FIND(pfi_ifhead, &pfi_ifs, &key);
819         if (p == NULL) {
820                 crit_exit();
821                 return;
822         }
823         bzero(pfs->pcounters, sizeof(pfs->pcounters));
824         bzero(pfs->bcounters, sizeof(pfs->bcounters));
825         for (i = 0; i < 2; i++)
826                 for (j = 0; j < 2; j++)
827                         for (k = 0; k < 2; k++) {
828                                 pfs->pcounters[i][j][k] =
829                                         p->pfik_packets[i][j][k];
830                                 pfs->bcounters[i][j] +=
831                                         p->pfik_bytes[i][j][k];
832                         }
833         crit_exit();
834 }
835
836 int
837 pfi_clr_istats(const char *name, int *nzero, int flags)
838 {
839         struct pfi_kif  *p;
840         int              n = 0;
841         long             tzero = time_second;
842
843         crit_enter();
844         ACCEPT_FLAGS(PFI_FLAG_GROUP|PFI_FLAG_INSTANCE);
845         RB_FOREACH(p, pfi_ifhead, &pfi_ifs) {
846                 if (pfi_skip_if(name, p, flags))
847                         continue;
848                 bzero(p->pfik_packets, sizeof(p->pfik_packets));
849                 bzero(p->pfik_bytes, sizeof(p->pfik_bytes));
850                 p->pfik_tzero = tzero;
851                 n++;
852         }
853         crit_exit();
854         if (nzero != NULL)
855                 *nzero = n;
856         return (0);
857 }
858
859 int
860 pfi_get_ifaces(const char *name, struct pfi_if *buf, int *size, int flags)
861 {
862         struct pfi_kif  *p;
863         int              n = 0;
864
865         ACCEPT_FLAGS(PFI_FLAG_GROUP|PFI_FLAG_INSTANCE);
866         crit_enter();
867         RB_FOREACH(p, pfi_ifhead, &pfi_ifs) {
868                 if (pfi_skip_if(name, p, flags))
869                         continue;
870                 if (*size > n++) {
871                         if (!p->pfik_tzero)
872                                 p->pfik_tzero = boottime.tv_sec;
873                         if (copyout(p, buf++, sizeof(*buf))) {
874                                 crit_exit();
875                                 return (EFAULT);
876                         }
877                 }
878         }
879         crit_exit();
880         *size = n;
881         return (0);
882 }
883
884 struct pfi_kif *
885 pfi_lookup_if(const char *name)
886 {
887         struct pfi_kif  *p, key;
888
889         strlcpy(key.pfik_name, name, sizeof(key.pfik_name));
890         p = RB_FIND(pfi_ifhead, &pfi_ifs, &key);
891         return (p);
892 }
893
894 int
895 pfi_skip_if(const char *filter, struct pfi_kif *p, int f)
896 {
897         int     n;
898
899         if ((p->pfik_flags & PFI_IFLAG_GROUP) && !(f & PFI_FLAG_GROUP))
900                 return (1);
901         if ((p->pfik_flags & PFI_IFLAG_INSTANCE) && !(f & PFI_FLAG_INSTANCE))
902                 return (1);
903         if (filter == NULL || !*filter)
904                 return (0);
905         if (!strcmp(p->pfik_name, filter))
906                 return (0);     /* exact match */
907         n = strlen(filter);
908         if (n < 1 || n >= IFNAMSIZ)
909                 return (1);     /* sanity check */
910         if (filter[n-1] >= '0' && filter[n-1] <= '9')
911                 return (1);     /* only do exact match in that case */
912         if (strncmp(p->pfik_name, filter, n))
913                 return (1);     /* prefix doesn't match */
914         return (p->pfik_name[n] < '0' || p->pfik_name[n] > '9');
915 }
916
917 /* from pf_print_state.c */
918 int
919 pfi_unmask(void *addr)
920 {
921         struct pf_addr *m = addr;
922         int i = 31, j = 0, b = 0;
923         u_int32_t tmp;
924
925         while (j < 4 && m->addr32[j] == 0xffffffff) {
926                 b += 32;
927                 j++;
928         }
929         if (j < 4) {
930                 tmp = ntohl(m->addr32[j]);
931                 for (i = 31; tmp & (1 << i); --i)
932                         b++;
933         }
934         return (b);
935 }
936
937 void
938 pfi_dohooks(struct pfi_kif *p)
939 {
940         for (; p != NULL; p = p->pfik_parent)
941                 dohooks(p->pfik_ah_head, 0);
942 }
943
944 int
945 pfi_match_addr(struct pfi_dynaddr *dyn, struct pf_addr *a, sa_family_t af)
946 {
947         if (af == AF_INET) {
948                 switch (dyn->pfid_acnt4) {
949                 case 0:
950                         return (0);
951                 case 1:
952                         return (PF_MATCHA(0, &dyn->pfid_addr4,
953                             &dyn->pfid_mask4, a, AF_INET));
954                 default:
955                         return (pfr_match_addr(dyn->pfid_kt, a, AF_INET));
956                 }
957         } else {
958                 switch (dyn->pfid_acnt6) {
959                 case 0:
960                         return (0);
961                 case 1:
962                         return (PF_MATCHA(0, &dyn->pfid_addr6,
963                             &dyn->pfid_mask6, a, AF_INET6));
964                 default:
965                         return (pfr_match_addr(dyn->pfid_kt, a, AF_INET6));
966                 }
967         }
968 }