linprocfs - Introduce /proc/mounts
[dragonfly.git] / sys / net / pf / pf_if.c
blobb68ac99c7ec45e206b1c293d70cf332502f7c441
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 $ */
6 /*
7 * Copyright (c) 2004 The DragonFly Project. All rights reserved.
9 * Copyright (c) 2001 Daniel Hartmeier
10 * Copyright (c) 2003 Cedric Berger
11 * All rights reserved.
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
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.
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.
38 #include "opt_inet.h"
39 #include "opt_inet6.h"
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>
54 #include <net/if.h>
55 #include <net/if_types.h>
56 #include <net/route.h>
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>
64 #include <net/pf/pfvar.h>
66 #ifdef INET6
67 #include <netinet/ip6.h>
68 #endif /* INET6 */
70 #define ACCEPT_FLAGS(oklist) \
71 do { \
72 if ((flags & ~(oklist)) & \
73 PFI_FLAG_ALLMASK) \
74 return (EINVAL); \
75 } while (0)
77 #define senderr(e) do { rv = (e); goto _bad; } while (0)
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;
95 eventhandler_tag pfi_clone_cookie = NULL;
96 eventhandler_tag pfi_attach_cookie = NULL;
97 eventhandler_tag pfi_detach_cookie = NULL;
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 enum ifaddr_event, struct ifaddr *);
115 void pfi_attach_clone_event(void *, struct if_clone *);
116 void pfi_attach_ifnet_event(void *, struct ifnet *);
117 void pfi_detach_ifnet_event(void *, struct ifnet *);
119 RB_PROTOTYPE(pfi_ifhead, pfi_kif, pfik_tree, pfi_if_compare);
120 RB_GENERATE(pfi_ifhead, pfi_kif, pfik_tree, pfi_if_compare);
122 #define PFI_DYNAMIC_BUSES { "pcmcia", "cardbus", "uhub" }
123 #define PFI_BUFFER_MAX 0x10000
124 MALLOC_DEFINE(PFI_MTYPE, "pf_if", "pf interface table");
126 void
127 pfi_initialize(void)
129 struct ifnet *ifp;
131 if (pfi_self != NULL) /* already initialized */
132 return;
134 TAILQ_INIT(&pfi_statehead);
135 pfi_buffer_max = 64;
136 pfi_buffer = kmalloc(pfi_buffer_max * sizeof(*pfi_buffer),
137 PFI_MTYPE, M_WAITOK);
138 pfi_self = pfi_if_create("self", NULL, PFI_IFLAG_GROUP);
139 pfi_dynamic_drivers();
141 TAILQ_FOREACH(ifp, &ifnet, if_link) {
142 if (ifp->if_dunit != IF_DUNIT_NONE)
143 pfi_attach_ifnet(ifp);
145 pfi_dummy = pfi_if_create("notyet", pfi_self,
146 PFI_IFLAG_GROUP | PFI_IFLAG_DYNAMIC);
147 pfi_attach_cookie = EVENTHANDLER_REGISTER(ifnet_attach_event,
148 pfi_attach_ifnet_event, NULL, EVENTHANDLER_PRI_ANY);
149 pfi_detach_cookie = EVENTHANDLER_REGISTER(ifnet_detach_event,
150 pfi_detach_ifnet_event, NULL, EVENTHANDLER_PRI_ANY);
151 pfi_clone_cookie = EVENTHANDLER_REGISTER(if_clone_event,
152 pfi_attach_clone_event, NULL, EVENTHANDLER_PRI_ANY);
155 void
156 pfi_cleanup(void)
158 struct pfi_kif *p, key;
159 struct ifnet *ifp;
161 EVENTHANDLER_DEREGISTER(ifnet_attach_event, pfi_attach_cookie);
162 EVENTHANDLER_DEREGISTER(ifnet_detach_event, pfi_detach_cookie);
163 EVENTHANDLER_DEREGISTER(if_clone_event, pfi_clone_cookie);
165 /* release PFI_IFLAG_INSTANCE */
166 TAILQ_FOREACH(ifp, &ifnet, if_link) {
167 strlcpy(key.pfik_name, ifp->if_xname, sizeof(key.pfik_name));
168 p = RB_FIND(pfi_ifhead, &pfi_ifs, &key);
169 if (p != NULL)
170 pfi_detach_ifnet(ifp);
173 /* XXX clear all other interface group */
174 while ((p = RB_MIN(pfi_ifhead, &pfi_ifs))) {
175 RB_REMOVE(pfi_ifhead, &pfi_ifs, p);
177 kfree(p->pfik_ah_head, PFI_MTYPE);
178 kfree(p, PFI_MTYPE);
180 kfree(pfi_index2kif, PFI_MTYPE);
181 kfree(pfi_buffer, PFI_MTYPE);
182 pfi_index2kif = NULL;
183 pfi_buffer = NULL;
184 pfi_self = NULL;
188 * Wrapper functions for FreeBSD eventhandler
190 void
191 pfi_kifaddr_update_event(void *arg, struct ifnet *ifp,
192 enum ifaddr_event event __unused, struct ifaddr *ifa __unused)
194 struct pfi_kif *p = arg;
197 * Check to see if it is 'our' interface as we do not have per
198 * interface hooks and thus get an update for every interface.
200 if (p && p->pfik_ifp == ifp)
201 pfi_kifaddr_update(p);
204 void
205 pfi_attach_clone_event(void *arg __unused, struct if_clone *ifc)
207 pfi_attach_clone(ifc);
210 void
211 pfi_attach_ifnet_event(void *arg __unused, struct ifnet *ifp)
213 if (ifp->if_dunit != IF_DUNIT_NONE)
214 pfi_attach_ifnet(ifp);
217 void
218 pfi_detach_ifnet_event(void *arg __unused, struct ifnet *ifp)
220 pfi_detach_ifnet(ifp);
223 void
224 pfi_attach_clone(struct if_clone *ifc)
226 pfi_initialize();
227 pfi_newgroup(ifc->ifc_name, PFI_IFLAG_CLONABLE);
230 void
231 pfi_attach_ifnet(struct ifnet *ifp)
233 struct pfi_kif *p, *q, key;
234 int realname;
236 pfi_initialize();
237 crit_enter();
238 pfi_update++;
239 if (ifp->if_index >= pfi_indexlim) {
241 * grow pfi_index2kif, similar to ifindex2ifnet code in if.c
243 size_t m, n, oldlim;
244 struct pfi_kif **mp, **np;
246 oldlim = pfi_indexlim;
247 if (pfi_indexlim == 0)
248 pfi_indexlim = 64;
249 while (ifp->if_index >= pfi_indexlim)
250 pfi_indexlim <<= 1;
252 m = oldlim * sizeof(struct pfi_kif *);
253 mp = pfi_index2kif;
254 n = pfi_indexlim * sizeof(struct pfi_kif *);
255 np = kmalloc(n, PFI_MTYPE, M_NOWAIT | M_ZERO);
256 if (np == NULL)
257 panic("pfi_attach_ifnet: "
258 "cannot allocate translation table");
259 if (mp != NULL)
260 bcopy(mp, np, m);
261 pfi_index2kif = np;
262 if (mp != NULL)
263 kfree(mp, PFI_MTYPE);
266 strlcpy(key.pfik_name, ifp->if_xname, sizeof(key.pfik_name));
267 p = RB_FIND(pfi_ifhead, &pfi_ifs, &key);
268 /* some additional trickery for placeholders */
269 if ((p == NULL) || (p->pfik_parent == pfi_dummy)) {
270 /* are we looking at a renamed instance or not? */
271 pfi_copy_group(key.pfik_name, ifp->if_xname,
272 sizeof(key.pfik_name));
273 realname = (strncmp(key.pfik_name, ifp->if_dname,
274 sizeof(key.pfik_name)) == 0);
275 /* add group */
276 /* we can change if_xname, hence use if_dname as group id */
277 pfi_copy_group(key.pfik_name, ifp->if_dname,
278 sizeof(key.pfik_name));
279 q = RB_FIND(pfi_ifhead, &pfi_ifs, &key);
280 if (q == NULL)
281 q = pfi_if_create(key.pfik_name, pfi_self,
282 PFI_IFLAG_GROUP|PFI_IFLAG_DYNAMIC);
283 else if (q->pfik_parent == pfi_dummy) {
284 q->pfik_parent = pfi_self;
285 q->pfik_flags = (PFI_IFLAG_GROUP | PFI_IFLAG_DYNAMIC);
287 if (q == NULL)
288 panic("pfi_attach_ifnet: "
289 "cannot allocate '%s' group", key.pfik_name);
291 /* add/modify interface */
292 if (p == NULL)
293 p = pfi_if_create(ifp->if_xname, q,
294 realname?PFI_IFLAG_INSTANCE:PFI_IFLAG_PLACEHOLDER);
295 else {
296 /* remove from the dummy group */
297 /* XXX: copy stats? We should not have any!!! */
298 pfi_dummy->pfik_delcnt++;
299 TAILQ_REMOVE(&pfi_dummy->pfik_grouphead, p,
300 pfik_instances);
301 /* move to the right group */
302 p->pfik_parent = q;
303 q->pfik_addcnt++;
304 TAILQ_INSERT_TAIL(&q->pfik_grouphead, p,
305 pfik_instances);
306 if (realname) {
307 p->pfik_flags &= ~PFI_IFLAG_PLACEHOLDER;
308 p->pfik_flags |= PFI_IFLAG_INSTANCE;
311 if (p == NULL)
312 panic("pfi_attach_ifnet: "
313 "cannot allocate '%s' interface", ifp->if_xname);
314 } else
315 q = p->pfik_parent;
316 p->pfik_ifp = ifp;
317 p->pfik_flags |= PFI_IFLAG_ATTACHED;
318 p->pfik_ah_cookie = EVENTHANDLER_REGISTER(ifaddr_event,
319 pfi_kifaddr_update_event, p, EVENTHANDLER_PRI_ANY);
320 pfi_index2kif[ifp->if_index] = p;
321 pfi_dohooks(p);
322 crit_exit();
325 void
326 pfi_detach_ifnet(struct ifnet *ifp)
328 struct pfi_kif *p, *q, key;
330 strlcpy(key.pfik_name, ifp->if_xname, sizeof(key.pfik_name));
332 crit_enter();
333 pfi_update++;
334 p = RB_FIND(pfi_ifhead, &pfi_ifs, &key);
335 if (p == NULL) {
336 kprintf("pfi_detach_ifnet: cannot find %s", ifp->if_xname);
337 crit_exit();
338 return;
340 EVENTHANDLER_DEREGISTER(ifaddr_event, p->pfik_ah_cookie);
341 q = p->pfik_parent;
342 p->pfik_ifp = NULL;
343 p->pfik_flags &= ~PFI_IFLAG_ATTACHED;
344 pfi_index2kif[ifp->if_index] = NULL;
345 pfi_dohooks(p);
346 pfi_maybe_destroy(p);
347 crit_exit();
350 struct pfi_kif *
351 pfi_lookup_create(const char *name)
353 struct pfi_kif *p, *q, key;
355 crit_enter();
356 p = pfi_lookup_if(name);
357 if (p == NULL) {
358 pfi_copy_group(key.pfik_name, name, sizeof(key.pfik_name));
359 q = pfi_lookup_if(key.pfik_name);
360 if ((q != NULL) && (q->pfik_parent != pfi_dummy))
361 p = pfi_if_create(name, q, PFI_IFLAG_INSTANCE);
362 else {
363 if (pfi_dummy == NULL)
364 panic("no 'notyet' dummy group");
365 p = pfi_if_create(name, pfi_dummy,
366 PFI_IFLAG_PLACEHOLDER);
369 crit_exit();
370 return (p);
373 struct pfi_kif *
374 pfi_attach_rule(const char *name)
376 struct pfi_kif *p;
378 p = pfi_lookup_create(name);
379 if (p != NULL)
380 p->pfik_rules++;
381 return (p);
384 void
385 pfi_detach_rule(struct pfi_kif *p)
387 if (p == NULL)
388 return;
389 if (p->pfik_rules > 0)
390 p->pfik_rules--;
391 else
392 kprintf("pfi_detach_rule: reference count at 0\n");
393 pfi_maybe_destroy(p);
396 void
397 pfi_attach_state(struct pfi_kif *p)
399 if (!p->pfik_states++)
400 TAILQ_INSERT_TAIL(&pfi_statehead, p, pfik_w_states);
403 void
404 pfi_detach_state(struct pfi_kif *p)
406 if (p == NULL)
407 return;
408 if (p->pfik_states <= 0) {
409 kprintf("pfi_detach_state: reference count <= 0\n");
410 return;
412 if (!--p->pfik_states)
413 TAILQ_REMOVE(&pfi_statehead, p, pfik_w_states);
414 pfi_maybe_destroy(p);
418 pfi_dynaddr_setup(struct pf_addr_wrap *aw, sa_family_t af)
420 struct pfi_dynaddr *dyn;
421 char tblname[PF_TABLE_NAME_SIZE];
422 struct pf_ruleset *ruleset = NULL;
423 int rv = 0;
425 if (aw->type != PF_ADDR_DYNIFTL)
426 return (0);
427 dyn = pool_get(&pfi_addr_pl, PR_NOWAIT);
428 if (dyn == NULL)
429 return (1);
430 bzero(dyn, sizeof(*dyn));
432 crit_enter();
433 dyn->pfid_kif = pfi_attach_rule(aw->v.ifname);
434 if (dyn->pfid_kif == NULL)
435 senderr(1);
437 dyn->pfid_net = pfi_unmask(&aw->v.a.mask);
438 if (af == AF_INET && dyn->pfid_net == 32)
439 dyn->pfid_net = 128;
440 strlcpy(tblname, aw->v.ifname, sizeof(tblname));
441 if (aw->iflags & PFI_AFLAG_NETWORK)
442 strlcat(tblname, ":network", sizeof(tblname));
443 if (aw->iflags & PFI_AFLAG_BROADCAST)
444 strlcat(tblname, ":broadcast", sizeof(tblname));
445 if (aw->iflags & PFI_AFLAG_PEER)
446 strlcat(tblname, ":peer", sizeof(tblname));
447 if (aw->iflags & PFI_AFLAG_NOALIAS)
448 strlcat(tblname, ":0", sizeof(tblname));
449 if (dyn->pfid_net != 128)
450 ksnprintf(tblname + strlen(tblname),
451 sizeof(tblname) - strlen(tblname), "/%d", dyn->pfid_net);
452 ruleset = pf_find_or_create_ruleset(pfi_reserved_anchor,
453 pfi_interface_ruleset);
454 if (ruleset == NULL)
455 senderr(1);
457 dyn->pfid_kt = pfr_attach_table(ruleset, tblname);
458 if (dyn->pfid_kt == NULL)
459 senderr(1);
461 dyn->pfid_kt->pfrkt_flags |= PFR_TFLAG_ACTIVE;
462 dyn->pfid_iflags = aw->iflags;
463 dyn->pfid_af = af;
464 dyn->pfid_hook_cookie = hook_establish(dyn->pfid_kif->pfik_ah_head, 1,
465 pfi_dynaddr_update, dyn);
466 if (dyn->pfid_hook_cookie == NULL)
467 senderr(1);
469 aw->p.dyn = dyn;
470 pfi_dynaddr_update(aw->p.dyn);
471 crit_exit();
472 return (0);
474 _bad:
475 if (dyn->pfid_kt != NULL)
476 pfr_detach_table(dyn->pfid_kt);
477 if (ruleset != NULL)
478 pf_remove_if_empty_ruleset(ruleset);
479 if (dyn->pfid_kif != NULL)
480 pfi_detach_rule(dyn->pfid_kif);
481 pool_put(&pfi_addr_pl, dyn);
482 crit_exit();
483 return (rv);
486 void
487 pfi_dynaddr_update(void *p)
489 struct pfi_dynaddr *dyn = (struct pfi_dynaddr *)p;
490 struct pfi_kif *kif = dyn->pfid_kif;
491 struct pfr_ktable *kt = dyn->pfid_kt;
493 if (dyn == NULL || kif == NULL || kt == NULL)
494 panic("pfi_dynaddr_update");
495 if (kt->pfrkt_larg != pfi_update) {
496 /* this table needs to be brought up-to-date */
497 pfi_table_update(kt, kif, dyn->pfid_net, dyn->pfid_iflags);
498 kt->pfrkt_larg = pfi_update;
500 pfr_dynaddr_update(kt, dyn);
503 void
504 pfi_table_update(struct pfr_ktable *kt, struct pfi_kif *kif, int net, int flags)
506 int e, size2 = 0;
507 struct pfi_kif *p;
508 struct pfr_table t;
510 if ((kif->pfik_flags & PFI_IFLAG_INSTANCE) && kif->pfik_ifp == NULL) {
511 pfr_clr_addrs(&kt->pfrkt_t, NULL, 0);
512 return;
514 pfi_buffer_cnt = 0;
515 if ((kif->pfik_flags & PFI_IFLAG_INSTANCE))
516 pfi_instance_add(kif->pfik_ifp, net, flags);
517 else if (strcmp(kif->pfik_name, "self")) {
518 TAILQ_FOREACH(p, &kif->pfik_grouphead, pfik_instances)
519 pfi_instance_add(p->pfik_ifp, net, flags);
520 } else {
521 RB_FOREACH(p, pfi_ifhead, &pfi_ifs)
522 if (p->pfik_flags & PFI_IFLAG_INSTANCE)
523 pfi_instance_add(p->pfik_ifp, net, flags);
525 t = kt->pfrkt_t;
526 t.pfrt_flags = 0;
527 if ((e = pfr_set_addrs(&t, pfi_buffer, pfi_buffer_cnt, &size2,
528 NULL, NULL, NULL, 0)))
529 kprintf("pfi_table_update: cannot set %d new addresses "
530 "into table %s: %d\n", pfi_buffer_cnt, kt->pfrkt_name, e);
533 void
534 pfi_instance_add(struct ifnet *ifp, int net, int flags)
536 struct ifaddr_container *ifac;
537 int got4 = 0, got6 = 0;
538 int net2, af;
540 if (ifp == NULL)
541 return;
542 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
543 struct ifaddr *ia = ifac->ifa;
545 if (ia->ifa_addr == NULL)
546 continue;
547 af = ia->ifa_addr->sa_family;
548 if (af != AF_INET && af != AF_INET6)
549 continue;
551 * XXX: For point-to-point interfaces, (ifname:0) and IPv4,
552 * jump over address without a proper route to work
553 * around a problem with ppp not fully removing the
554 * address used during IPCP.
556 if ((ifp->if_flags & IFF_POINTOPOINT) &&
557 !(ia->ifa_flags & IFA_ROUTE) &&
558 (flags & PFI_AFLAG_NOALIAS) && (af == AF_INET))
559 continue;
560 if ((flags & PFI_AFLAG_BROADCAST) && af == AF_INET6)
561 continue;
562 if ((flags & PFI_AFLAG_BROADCAST) &&
563 !(ifp->if_flags & IFF_BROADCAST))
564 continue;
565 if ((flags & PFI_AFLAG_PEER) &&
566 !(ifp->if_flags & IFF_POINTOPOINT))
567 continue;
568 if ((flags & PFI_AFLAG_NETWORK) && af == AF_INET6 &&
569 IN6_IS_ADDR_LINKLOCAL(
570 &((struct sockaddr_in6 *)ia->ifa_addr)->sin6_addr))
571 continue;
572 if (flags & PFI_AFLAG_NOALIAS) {
573 if (af == AF_INET && got4)
574 continue;
575 if (af == AF_INET6 && got6)
576 continue;
578 if (af == AF_INET)
579 got4 = 1;
580 else
581 got6 = 1;
582 net2 = net;
583 if (net2 == 128 && (flags & PFI_AFLAG_NETWORK)) {
584 if (af == AF_INET) {
585 net2 = pfi_unmask(&((struct sockaddr_in *)
586 ia->ifa_netmask)->sin_addr);
587 } else {
588 net2 = pfi_unmask(&((struct sockaddr_in6 *)
589 ia->ifa_netmask)->sin6_addr);
592 if (af == AF_INET && net2 > 32)
593 net2 = 32;
594 if (flags & PFI_AFLAG_BROADCAST)
595 pfi_address_add(ia->ifa_broadaddr, af, net2);
596 else if (flags & PFI_AFLAG_PEER)
597 pfi_address_add(ia->ifa_dstaddr, af, net2);
598 else
599 pfi_address_add(ia->ifa_addr, af, net2);
603 void
604 pfi_address_add(struct sockaddr *sa, int af, int net)
606 struct pfr_addr *p;
607 int i;
609 if (pfi_buffer_cnt >= pfi_buffer_max) {
610 int new_max = pfi_buffer_max * 2;
612 if (new_max > PFI_BUFFER_MAX) {
613 kprintf("pfi_address_add: address buffer full (%d/%d)\n",
614 pfi_buffer_cnt, PFI_BUFFER_MAX);
615 return;
617 p = kmalloc(new_max * sizeof(*pfi_buffer), PFI_MTYPE,
618 M_NOWAIT);
619 if (p == NULL) {
620 kprintf("pfi_address_add: no memory to grow buffer "
621 "(%d/%d)\n", pfi_buffer_cnt, PFI_BUFFER_MAX);
622 return;
624 memcpy(pfi_buffer, p, pfi_buffer_cnt * sizeof(*pfi_buffer));
625 /* no need to zero buffer */
626 kfree(pfi_buffer, PFI_MTYPE);
627 pfi_buffer = p;
628 pfi_buffer_max = new_max;
630 if (af == AF_INET && net > 32)
631 net = 128;
632 p = pfi_buffer + pfi_buffer_cnt++;
633 bzero(p, sizeof(*p));
634 p->pfra_af = af;
635 p->pfra_net = net;
636 if (af == AF_INET)
637 p->pfra_ip4addr = ((struct sockaddr_in *)sa)->sin_addr;
638 if (af == AF_INET6) {
639 p->pfra_ip6addr = ((struct sockaddr_in6 *)sa)->sin6_addr;
640 if (IN6_IS_ADDR_LINKLOCAL(&p->pfra_ip6addr))
641 p->pfra_ip6addr.s6_addr16[1] = 0;
643 /* mask network address bits */
644 if (net < 128)
645 ((caddr_t)p)[p->pfra_net/8] &= ~(0xFF >> (p->pfra_net%8));
646 for (i = (p->pfra_net+7)/8; i < sizeof(p->pfra_u); i++)
647 ((caddr_t)p)[i] = 0;
650 void
651 pfi_dynaddr_remove(struct pf_addr_wrap *aw)
653 if (aw->type != PF_ADDR_DYNIFTL || aw->p.dyn == NULL ||
654 aw->p.dyn->pfid_kif == NULL || aw->p.dyn->pfid_kt == NULL)
655 return;
657 crit_enter();
658 hook_disestablish(aw->p.dyn->pfid_kif->pfik_ah_head,
659 aw->p.dyn->pfid_hook_cookie);
660 pfi_detach_rule(aw->p.dyn->pfid_kif);
661 aw->p.dyn->pfid_kif = NULL;
662 pfr_detach_table(aw->p.dyn->pfid_kt);
663 aw->p.dyn->pfid_kt = NULL;
664 pool_put(&pfi_addr_pl, aw->p.dyn);
665 aw->p.dyn = NULL;
666 crit_exit();
669 void
670 pfi_dynaddr_copyout(struct pf_addr_wrap *aw)
672 if (aw->type != PF_ADDR_DYNIFTL || aw->p.dyn == NULL ||
673 aw->p.dyn->pfid_kif == NULL)
674 return;
675 aw->p.dyncnt = aw->p.dyn->pfid_acnt4 + aw->p.dyn->pfid_acnt6;
678 void
679 pfi_kifaddr_update(void *v)
681 crit_enter();
682 pfi_update++;
683 pfi_dohooks(v);
684 crit_exit();
688 pfi_if_compare(struct pfi_kif *p, struct pfi_kif *q)
690 return (strncmp(p->pfik_name, q->pfik_name, IFNAMSIZ));
693 struct pfi_kif *
694 pfi_if_create(const char *name, struct pfi_kif *q, int flags)
696 struct pfi_kif *p;
698 p = kmalloc(sizeof(*p), PFI_MTYPE, M_NOWAIT | M_ZERO);
699 if (p == NULL)
700 return (NULL);
701 p->pfik_ah_head = kmalloc(sizeof(*p->pfik_ah_head), PFI_MTYPE,
702 M_NOWAIT | M_ZERO);
703 if (p->pfik_ah_head == NULL) {
704 kfree(p, PFI_MTYPE);
705 return (NULL);
707 TAILQ_INIT(p->pfik_ah_head);
708 TAILQ_INIT(&p->pfik_grouphead);
709 strlcpy(p->pfik_name, name, sizeof(p->pfik_name));
710 RB_INIT(&p->pfik_lan_ext);
711 RB_INIT(&p->pfik_ext_gwy);
712 p->pfik_flags = flags;
713 p->pfik_parent = q;
714 p->pfik_tzero = time_second;
716 RB_INSERT(pfi_ifhead, &pfi_ifs, p);
717 if (q != NULL) {
718 q->pfik_addcnt++;
719 TAILQ_INSERT_TAIL(&q->pfik_grouphead, p, pfik_instances);
721 pfi_ifcnt++;
722 return (p);
726 pfi_maybe_destroy(struct pfi_kif *p)
728 int i, j, k;
729 struct pfi_kif *q = p->pfik_parent;
731 if ((p->pfik_flags & (PFI_IFLAG_ATTACHED | PFI_IFLAG_GROUP)) ||
732 p->pfik_rules > 0 || p->pfik_states > 0)
733 if (!(p->pfik_flags & PFI_IFLAG_PLACEHOLDER))
734 return (0);
736 crit_enter();
737 if (q != NULL) {
738 for (i = 0; i < 2; i++)
739 for (j = 0; j < 2; j++)
740 for (k = 0; k < 2; k++) {
741 q->pfik_bytes[i][j][k] +=
742 p->pfik_bytes[i][j][k];
743 q->pfik_packets[i][j][k] +=
744 p->pfik_packets[i][j][k];
745 /* clear stats in case we return to the dummy group */
746 p->pfik_bytes[i][j][k] = 0;
747 p->pfik_packets[i][j][k] = 0;
749 q->pfik_delcnt++;
750 TAILQ_REMOVE(&q->pfik_grouphead, p, pfik_instances);
752 if (p->pfik_rules > 0 || p->pfik_states > 0) {
753 /* move back to the dummy group */
754 p->pfik_parent = pfi_dummy;
755 pfi_dummy->pfik_addcnt++;
756 TAILQ_INSERT_TAIL(&pfi_dummy->pfik_grouphead, p,
757 pfik_instances);
758 crit_exit();
759 return (0);
761 pfi_ifcnt--;
762 RB_REMOVE(pfi_ifhead, &pfi_ifs, p);
763 crit_exit();
765 kfree(p->pfik_ah_head, PFI_MTYPE);
766 kfree(p, PFI_MTYPE);
767 return (1);
770 void
771 pfi_copy_group(char *p, const char *q, int m)
773 while (m > 1 && *q && !(*q >= '0' && *q <= '9')) {
774 *p++ = *q++;
775 m--;
777 if (m > 0)
778 *p++ = '\0';
781 void
782 pfi_dynamic_drivers(void)
784 struct ifnet *ifp;
787 * For FreeBSD basically every interface is "dynamic" as we can unload
788 * modules e.g.
790 TAILQ_FOREACH(ifp, &ifnet, if_link) {
791 if (ifp->if_dunit == IF_DUNIT_NONE)
792 continue;
793 pfi_newgroup(ifp->if_dname, PFI_IFLAG_DYNAMIC);
797 void
798 pfi_newgroup(const char *name, int flags)
800 struct pfi_kif *p;
802 p = pfi_lookup_if(name);
803 if (p == NULL)
804 p = pfi_if_create(name, pfi_self, PFI_IFLAG_GROUP);
805 if (p == NULL) {
806 kprintf("pfi_newgroup: cannot allocate '%s' group", name);
807 return;
809 p->pfik_flags |= flags;
812 void
813 pfi_fill_oldstatus(struct pf_status *pfs)
815 struct pfi_kif *p, key;
816 int i, j, k;
818 strlcpy(key.pfik_name, pfs->ifname, sizeof(key.pfik_name));
819 crit_enter();
820 p = RB_FIND(pfi_ifhead, &pfi_ifs, &key);
821 if (p == NULL) {
822 crit_exit();
823 return;
825 bzero(pfs->pcounters, sizeof(pfs->pcounters));
826 bzero(pfs->bcounters, sizeof(pfs->bcounters));
827 for (i = 0; i < 2; i++)
828 for (j = 0; j < 2; j++)
829 for (k = 0; k < 2; k++) {
830 pfs->pcounters[i][j][k] =
831 p->pfik_packets[i][j][k];
832 pfs->bcounters[i][j] +=
833 p->pfik_bytes[i][j][k];
835 crit_exit();
839 pfi_clr_istats(const char *name, int *nzero, int flags)
841 struct pfi_kif *p;
842 int n = 0;
843 long tzero = time_second;
845 crit_enter();
846 ACCEPT_FLAGS(PFI_FLAG_GROUP|PFI_FLAG_INSTANCE);
847 RB_FOREACH(p, pfi_ifhead, &pfi_ifs) {
848 if (pfi_skip_if(name, p, flags))
849 continue;
850 bzero(p->pfik_packets, sizeof(p->pfik_packets));
851 bzero(p->pfik_bytes, sizeof(p->pfik_bytes));
852 p->pfik_tzero = tzero;
853 n++;
855 crit_exit();
856 if (nzero != NULL)
857 *nzero = n;
858 return (0);
862 pfi_get_ifaces(const char *name, struct pfi_if *buf, int *size, int flags)
864 struct pfi_kif *p;
865 int n = 0;
867 ACCEPT_FLAGS(PFI_FLAG_GROUP|PFI_FLAG_INSTANCE);
868 crit_enter();
869 RB_FOREACH(p, pfi_ifhead, &pfi_ifs) {
870 if (pfi_skip_if(name, p, flags))
871 continue;
872 if (*size > n++) {
873 if (!p->pfik_tzero)
874 p->pfik_tzero = boottime.tv_sec;
875 if (copyout(p, buf++, sizeof(*buf))) {
876 crit_exit();
877 return (EFAULT);
881 crit_exit();
882 *size = n;
883 return (0);
886 struct pfi_kif *
887 pfi_lookup_if(const char *name)
889 struct pfi_kif *p, key;
891 strlcpy(key.pfik_name, name, sizeof(key.pfik_name));
892 p = RB_FIND(pfi_ifhead, &pfi_ifs, &key);
893 return (p);
897 pfi_skip_if(const char *filter, struct pfi_kif *p, int f)
899 int n;
901 if ((p->pfik_flags & PFI_IFLAG_GROUP) && !(f & PFI_FLAG_GROUP))
902 return (1);
903 if ((p->pfik_flags & PFI_IFLAG_INSTANCE) && !(f & PFI_FLAG_INSTANCE))
904 return (1);
905 if (filter == NULL || !*filter)
906 return (0);
907 if (!strcmp(p->pfik_name, filter))
908 return (0); /* exact match */
909 n = strlen(filter);
910 if (n < 1 || n >= IFNAMSIZ)
911 return (1); /* sanity check */
912 if (filter[n-1] >= '0' && filter[n-1] <= '9')
913 return (1); /* only do exact match in that case */
914 if (strncmp(p->pfik_name, filter, n))
915 return (1); /* prefix doesn't match */
916 return (p->pfik_name[n] < '0' || p->pfik_name[n] > '9');
919 /* from pf_print_state.c */
921 pfi_unmask(void *addr)
923 struct pf_addr *m = addr;
924 int i = 31, j = 0, b = 0;
925 u_int32_t tmp;
927 while (j < 4 && m->addr32[j] == 0xffffffff) {
928 b += 32;
929 j++;
931 if (j < 4) {
932 tmp = ntohl(m->addr32[j]);
933 for (i = 31; tmp & (1 << i); --i)
934 b++;
936 return (b);
939 void
940 pfi_dohooks(struct pfi_kif *p)
942 for (; p != NULL; p = p->pfik_parent)
943 dohooks(p->pfik_ah_head, 0);
947 pfi_match_addr(struct pfi_dynaddr *dyn, struct pf_addr *a, sa_family_t af)
949 if (af == AF_INET) {
950 switch (dyn->pfid_acnt4) {
951 case 0:
952 return (0);
953 case 1:
954 return (PF_MATCHA(0, &dyn->pfid_addr4,
955 &dyn->pfid_mask4, a, AF_INET));
956 default:
957 return (pfr_match_addr(dyn->pfid_kt, a, AF_INET));
959 } else {
960 switch (dyn->pfid_acnt6) {
961 case 0:
962 return (0);
963 case 1:
964 return (PF_MATCHA(0, &dyn->pfid_addr6,
965 &dyn->pfid_mask6, a, AF_INET6));
966 default:
967 return (pfr_match_addr(dyn->pfid_kt, a, AF_INET6));