pf_if.c revision 1.1.1.4 1 1.1.1.4 martti /* $NetBSD: pf_if.c,v 1.1.1.4 2009/12/01 07:03:13 martti Exp $ */
2 1.1.1.4 martti /* $OpenBSD: pf_if.c,v 1.47 2007/07/13 09:17:48 markus Exp $ */
3 1.1 itojun
4 1.1 itojun /*
5 1.1.1.4 martti * Copyright 2005 Henning Brauer <henning (at) openbsd.org>
6 1.1.1.4 martti * Copyright 2005 Ryan McBride <mcbride (at) openbsd.org>
7 1.1 itojun * Copyright (c) 2001 Daniel Hartmeier
8 1.1 itojun * Copyright (c) 2003 Cedric Berger
9 1.1 itojun * All rights reserved.
10 1.1 itojun *
11 1.1 itojun * Redistribution and use in source and binary forms, with or without
12 1.1 itojun * modification, are permitted provided that the following conditions
13 1.1 itojun * are met:
14 1.1 itojun *
15 1.1 itojun * - Redistributions of source code must retain the above copyright
16 1.1 itojun * notice, this list of conditions and the following disclaimer.
17 1.1 itojun * - Redistributions in binary form must reproduce the above
18 1.1 itojun * copyright notice, this list of conditions and the following
19 1.1 itojun * disclaimer in the documentation and/or other materials provided
20 1.1 itojun * with the distribution.
21 1.1 itojun *
22 1.1 itojun * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 1.1 itojun * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 1.1 itojun * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25 1.1 itojun * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26 1.1 itojun * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27 1.1 itojun * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28 1.1 itojun * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29 1.1 itojun * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
30 1.1 itojun * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 1.1 itojun * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
32 1.1 itojun * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 1.1 itojun * POSSIBILITY OF SUCH DAMAGE.
34 1.1 itojun */
35 1.1 itojun
36 1.1 itojun #include <sys/param.h>
37 1.1 itojun #include <sys/systm.h>
38 1.1 itojun #include <sys/mbuf.h>
39 1.1 itojun #include <sys/filio.h>
40 1.1 itojun #include <sys/socket.h>
41 1.1 itojun #include <sys/socketvar.h>
42 1.1 itojun #include <sys/kernel.h>
43 1.1 itojun #include <sys/device.h>
44 1.1 itojun #include <sys/time.h>
45 1.1 itojun
46 1.1 itojun #include <net/if.h>
47 1.1 itojun #include <net/if_types.h>
48 1.1 itojun
49 1.1 itojun #include <netinet/in.h>
50 1.1 itojun #include <netinet/in_var.h>
51 1.1 itojun #include <netinet/in_systm.h>
52 1.1 itojun #include <netinet/ip.h>
53 1.1 itojun #include <netinet/ip_var.h>
54 1.1 itojun
55 1.1 itojun #include <net/pfvar.h>
56 1.1 itojun
57 1.1 itojun #ifdef INET6
58 1.1 itojun #include <netinet/ip6.h>
59 1.1 itojun #endif /* INET6 */
60 1.1 itojun
61 1.1.1.4 martti struct pfi_kif *pfi_all = NULL;
62 1.1 itojun struct pool pfi_addr_pl;
63 1.1.1.4 martti struct pfi_ifhead pfi_ifs;
64 1.1 itojun long pfi_update = 1;
65 1.1 itojun struct pfr_addr *pfi_buffer;
66 1.1 itojun int pfi_buffer_cnt;
67 1.1 itojun int pfi_buffer_max;
68 1.1 itojun
69 1.1.1.4 martti void pfi_kif_update(struct pfi_kif *);
70 1.1.1.4 martti void pfi_dynaddr_update(struct pfi_dynaddr *dyn);
71 1.1 itojun void pfi_table_update(struct pfr_ktable *, struct pfi_kif *,
72 1.1 itojun int, int);
73 1.1.1.4 martti void pfi_kifaddr_update(void *);
74 1.1 itojun void pfi_instance_add(struct ifnet *, int, int);
75 1.1 itojun void pfi_address_add(struct sockaddr *, int, int);
76 1.1 itojun int pfi_if_compare(struct pfi_kif *, struct pfi_kif *);
77 1.1.1.4 martti int pfi_skip_if(const char *, struct pfi_kif *);
78 1.1 itojun int pfi_unmask(void *);
79 1.1 itojun
80 1.1 itojun RB_PROTOTYPE(pfi_ifhead, pfi_kif, pfik_tree, pfi_if_compare);
81 1.1 itojun RB_GENERATE(pfi_ifhead, pfi_kif, pfik_tree, pfi_if_compare);
82 1.1 itojun
83 1.1 itojun #define PFI_BUFFER_MAX 0x10000
84 1.1 itojun #define PFI_MTYPE M_IFADDR
85 1.1 itojun
86 1.1 itojun void
87 1.1 itojun pfi_initialize(void)
88 1.1 itojun {
89 1.1.1.4 martti if (pfi_all != NULL) /* already initialized */
90 1.1 itojun return;
91 1.1 itojun
92 1.1 itojun pool_init(&pfi_addr_pl, sizeof(struct pfi_dynaddr), 0, 0, 0,
93 1.1 itojun "pfiaddrpl", &pool_allocator_nointr);
94 1.1 itojun pfi_buffer_max = 64;
95 1.1 itojun pfi_buffer = malloc(pfi_buffer_max * sizeof(*pfi_buffer),
96 1.1 itojun PFI_MTYPE, M_WAITOK);
97 1.1.1.4 martti
98 1.1.1.4 martti if ((pfi_all = pfi_kif_get(IFG_ALL)) == NULL)
99 1.1.1.4 martti panic("pfi_kif_get for pfi_all failed");
100 1.1.1.4 martti }
101 1.1.1.4 martti
102 1.1.1.4 martti struct pfi_kif *
103 1.1.1.4 martti pfi_kif_get(const char *kif_name)
104 1.1.1.4 martti {
105 1.1.1.4 martti struct pfi_kif *kif;
106 1.1.1.4 martti struct pfi_kif_cmp s;
107 1.1.1.4 martti
108 1.1.1.4 martti bzero(&s, sizeof(s));
109 1.1.1.4 martti strlcpy(s.pfik_name, kif_name, sizeof(s.pfik_name));
110 1.1.1.4 martti if ((kif = RB_FIND(pfi_ifhead, &pfi_ifs, (struct pfi_kif *)&s)) != NULL)
111 1.1.1.4 martti return (kif);
112 1.1.1.4 martti
113 1.1.1.4 martti /* create new one */
114 1.1.1.4 martti if ((kif = malloc(sizeof(*kif), PFI_MTYPE, M_DONTWAIT)) == NULL)
115 1.1.1.4 martti return (NULL);
116 1.1.1.4 martti
117 1.1.1.4 martti bzero(kif, sizeof(*kif));
118 1.1.1.4 martti strlcpy(kif->pfik_name, kif_name, sizeof(kif->pfik_name));
119 1.1.1.4 martti kif->pfik_tzero = time_second;
120 1.1.1.4 martti TAILQ_INIT(&kif->pfik_dynaddrs);
121 1.1.1.4 martti
122 1.1.1.4 martti RB_INSERT(pfi_ifhead, &pfi_ifs, kif);
123 1.1.1.4 martti return (kif);
124 1.1 itojun }
125 1.1 itojun
126 1.1 itojun void
127 1.1.1.4 martti pfi_kif_ref(struct pfi_kif *kif, enum pfi_kif_refs what)
128 1.1 itojun {
129 1.1.1.4 martti switch (what) {
130 1.1.1.4 martti case PFI_KIF_REF_RULE:
131 1.1.1.4 martti kif->pfik_rules++;
132 1.1.1.4 martti break;
133 1.1.1.4 martti case PFI_KIF_REF_STATE:
134 1.1.1.4 martti kif->pfik_states++;
135 1.1.1.4 martti break;
136 1.1.1.4 martti default:
137 1.1.1.4 martti panic("pfi_kif_ref with unknown type");
138 1.1.1.4 martti }
139 1.1.1.4 martti }
140 1.1.1.4 martti
141 1.1.1.4 martti void
142 1.1.1.4 martti pfi_kif_unref(struct pfi_kif *kif, enum pfi_kif_refs what)
143 1.1.1.4 martti {
144 1.1.1.4 martti if (kif == NULL)
145 1.1.1.4 martti return;
146 1.1.1.4 martti
147 1.1.1.4 martti switch (what) {
148 1.1.1.4 martti case PFI_KIF_REF_NONE:
149 1.1.1.4 martti break;
150 1.1.1.4 martti case PFI_KIF_REF_RULE:
151 1.1.1.4 martti if (kif->pfik_rules <= 0) {
152 1.1.1.4 martti printf("pfi_kif_unref: rules refcount <= 0\n");
153 1.1.1.4 martti return;
154 1.1.1.4 martti }
155 1.1.1.4 martti kif->pfik_rules--;
156 1.1.1.4 martti break;
157 1.1.1.4 martti case PFI_KIF_REF_STATE:
158 1.1.1.4 martti if (kif->pfik_states <= 0) {
159 1.1.1.4 martti printf("pfi_kif_unref: state refcount <= 0\n");
160 1.1.1.4 martti return;
161 1.1.1.4 martti }
162 1.1.1.4 martti kif->pfik_states--;
163 1.1.1.4 martti break;
164 1.1.1.4 martti default:
165 1.1.1.4 martti panic("pfi_kif_unref with unknown type");
166 1.1.1.4 martti }
167 1.1.1.4 martti
168 1.1.1.4 martti if (kif->pfik_ifp != NULL || kif->pfik_group != NULL || kif == pfi_all)
169 1.1.1.4 martti return;
170 1.1.1.4 martti
171 1.1.1.4 martti if (kif->pfik_rules || kif->pfik_states)
172 1.1.1.4 martti return;
173 1.1.1.4 martti
174 1.1.1.4 martti RB_REMOVE(pfi_ifhead, &pfi_ifs, kif);
175 1.1.1.4 martti free(kif, PFI_MTYPE);
176 1.1.1.4 martti }
177 1.1.1.4 martti
178 1.1.1.4 martti int
179 1.1.1.4 martti pfi_kif_match(struct pfi_kif *rule_kif, struct pfi_kif *packet_kif)
180 1.1.1.4 martti {
181 1.1.1.4 martti struct ifg_list *p;
182 1.1.1.4 martti
183 1.1.1.4 martti if (rule_kif == NULL || rule_kif == packet_kif)
184 1.1.1.4 martti return (1);
185 1.1.1.4 martti
186 1.1.1.4 martti if (rule_kif->pfik_group != NULL)
187 1.1.1.4 martti TAILQ_FOREACH(p, &packet_kif->pfik_ifp->if_groups, ifgl_next)
188 1.1.1.4 martti if (p->ifgl_group == rule_kif->pfik_group)
189 1.1.1.4 martti return (1);
190 1.1.1.4 martti
191 1.1.1.4 martti return (0);
192 1.1 itojun }
193 1.1 itojun
194 1.1 itojun void
195 1.1 itojun pfi_attach_ifnet(struct ifnet *ifp)
196 1.1 itojun {
197 1.1.1.4 martti struct pfi_kif *kif;
198 1.1.1.4 martti int s;
199 1.1 itojun
200 1.1 itojun pfi_initialize();
201 1.1 itojun s = splsoftnet();
202 1.1 itojun pfi_update++;
203 1.1.1.4 martti if ((kif = pfi_kif_get(ifp->if_xname)) == NULL)
204 1.1.1.4 martti panic("pfi_kif_get failed");
205 1.1.1.4 martti
206 1.1.1.4 martti kif->pfik_ifp = ifp;
207 1.1.1.4 martti ifp->if_pf_kif = (caddr_t)kif;
208 1.1.1.4 martti
209 1.1.1.4 martti if ((kif->pfik_ah_cookie = hook_establish(ifp->if_addrhooks, 1,
210 1.1.1.4 martti pfi_kifaddr_update, kif)) == NULL)
211 1.1.1.4 martti panic("pfi_attach_ifnet: cannot allocate '%s' address hook",
212 1.1.1.4 martti ifp->if_xname);
213 1.1.1.4 martti
214 1.1.1.4 martti pfi_kif_update(kif);
215 1.1 itojun
216 1.1 itojun splx(s);
217 1.1 itojun }
218 1.1 itojun
219 1.1 itojun void
220 1.1 itojun pfi_detach_ifnet(struct ifnet *ifp)
221 1.1 itojun {
222 1.1.1.4 martti int s;
223 1.1.1.4 martti struct pfi_kif *kif;
224 1.1 itojun
225 1.1.1.4 martti if ((kif = (struct pfi_kif *)ifp->if_pf_kif) == NULL)
226 1.1.1.4 martti return;
227 1.1 itojun
228 1.1 itojun s = splsoftnet();
229 1.1 itojun pfi_update++;
230 1.1.1.4 martti hook_disestablish(ifp->if_addrhooks, kif->pfik_ah_cookie);
231 1.1.1.4 martti pfi_kif_update(kif);
232 1.1.1.4 martti
233 1.1.1.4 martti kif->pfik_ifp = NULL;
234 1.1.1.4 martti ifp->if_pf_kif = NULL;
235 1.1.1.4 martti pfi_kif_unref(kif, PFI_KIF_REF_NONE);
236 1.1 itojun splx(s);
237 1.1 itojun }
238 1.1 itojun
239 1.1.1.4 martti void
240 1.1.1.4 martti pfi_attach_ifgroup(struct ifg_group *ifg)
241 1.1 itojun {
242 1.1.1.4 martti struct pfi_kif *kif;
243 1.1 itojun int s;
244 1.1 itojun
245 1.1.1.4 martti pfi_initialize();
246 1.1 itojun s = splsoftnet();
247 1.1.1.4 martti pfi_update++;
248 1.1.1.4 martti if ((kif = pfi_kif_get(ifg->ifg_group)) == NULL)
249 1.1.1.4 martti panic("pfi_kif_get failed");
250 1.1 itojun
251 1.1.1.4 martti kif->pfik_group = ifg;
252 1.1.1.4 martti ifg->ifg_pf_kif = (caddr_t)kif;
253 1.1 itojun
254 1.1.1.4 martti splx(s);
255 1.1 itojun }
256 1.1 itojun
257 1.1 itojun void
258 1.1.1.4 martti pfi_detach_ifgroup(struct ifg_group *ifg)
259 1.1 itojun {
260 1.1.1.4 martti int s;
261 1.1.1.4 martti struct pfi_kif *kif;
262 1.1.1.4 martti
263 1.1.1.4 martti if ((kif = (struct pfi_kif *)ifg->ifg_pf_kif) == NULL)
264 1.1 itojun return;
265 1.1.1.4 martti
266 1.1.1.4 martti s = splsoftnet();
267 1.1.1.4 martti pfi_update++;
268 1.1.1.4 martti
269 1.1.1.4 martti kif->pfik_group = NULL;
270 1.1.1.4 martti ifg->ifg_pf_kif = NULL;
271 1.1.1.4 martti pfi_kif_unref(kif, PFI_KIF_REF_NONE);
272 1.1.1.4 martti splx(s);
273 1.1 itojun }
274 1.1 itojun
275 1.1 itojun void
276 1.1.1.4 martti pfi_group_change(const char *group)
277 1.1 itojun {
278 1.1.1.4 martti struct pfi_kif *kif;
279 1.1.1.4 martti int s;
280 1.1.1.4 martti
281 1.1.1.4 martti s = splsoftnet();
282 1.1.1.4 martti pfi_update++;
283 1.1.1.4 martti if ((kif = pfi_kif_get(group)) == NULL)
284 1.1.1.4 martti panic("pfi_kif_get failed");
285 1.1.1.4 martti
286 1.1.1.4 martti pfi_kif_update(kif);
287 1.1.1.4 martti
288 1.1.1.4 martti splx(s);
289 1.1 itojun }
290 1.1 itojun
291 1.1.1.4 martti int
292 1.1.1.4 martti pfi_match_addr(struct pfi_dynaddr *dyn, struct pf_addr *a, sa_family_t af)
293 1.1 itojun {
294 1.1.1.4 martti switch (af) {
295 1.1.1.4 martti #ifdef INET
296 1.1.1.4 martti case AF_INET:
297 1.1.1.4 martti switch (dyn->pfid_acnt4) {
298 1.1.1.4 martti case 0:
299 1.1.1.4 martti return (0);
300 1.1.1.4 martti case 1:
301 1.1.1.4 martti return (PF_MATCHA(0, &dyn->pfid_addr4,
302 1.1.1.4 martti &dyn->pfid_mask4, a, AF_INET));
303 1.1.1.4 martti default:
304 1.1.1.4 martti return (pfr_match_addr(dyn->pfid_kt, a, AF_INET));
305 1.1.1.4 martti }
306 1.1.1.4 martti break;
307 1.1.1.4 martti #endif /* INET */
308 1.1.1.4 martti #ifdef INET6
309 1.1.1.4 martti case AF_INET6:
310 1.1.1.4 martti switch (dyn->pfid_acnt6) {
311 1.1.1.4 martti case 0:
312 1.1.1.4 martti return (0);
313 1.1.1.4 martti case 1:
314 1.1.1.4 martti return (PF_MATCHA(0, &dyn->pfid_addr6,
315 1.1.1.4 martti &dyn->pfid_mask6, a, AF_INET6));
316 1.1.1.4 martti default:
317 1.1.1.4 martti return (pfr_match_addr(dyn->pfid_kt, a, AF_INET6));
318 1.1.1.4 martti }
319 1.1.1.4 martti break;
320 1.1.1.4 martti #endif /* INET6 */
321 1.1.1.4 martti default:
322 1.1.1.4 martti return (0);
323 1.1 itojun }
324 1.1 itojun }
325 1.1 itojun
326 1.1 itojun int
327 1.1 itojun pfi_dynaddr_setup(struct pf_addr_wrap *aw, sa_family_t af)
328 1.1 itojun {
329 1.1 itojun struct pfi_dynaddr *dyn;
330 1.1 itojun char tblname[PF_TABLE_NAME_SIZE];
331 1.1 itojun struct pf_ruleset *ruleset = NULL;
332 1.1 itojun int s, rv = 0;
333 1.1 itojun
334 1.1 itojun if (aw->type != PF_ADDR_DYNIFTL)
335 1.1 itojun return (0);
336 1.1.1.4 martti if ((dyn = pool_get(&pfi_addr_pl, PR_NOWAIT)) == NULL)
337 1.1 itojun return (1);
338 1.1 itojun bzero(dyn, sizeof(*dyn));
339 1.1 itojun
340 1.1 itojun s = splsoftnet();
341 1.1.1.4 martti if (!strcmp(aw->v.ifname, "self"))
342 1.1.1.4 martti dyn->pfid_kif = pfi_kif_get(IFG_ALL);
343 1.1.1.4 martti else
344 1.1.1.4 martti dyn->pfid_kif = pfi_kif_get(aw->v.ifname);
345 1.1.1.4 martti if (dyn->pfid_kif == NULL) {
346 1.1.1.4 martti rv = 1;
347 1.1.1.4 martti goto _bad;
348 1.1.1.4 martti }
349 1.1.1.4 martti pfi_kif_ref(dyn->pfid_kif, PFI_KIF_REF_RULE);
350 1.1 itojun
351 1.1 itojun dyn->pfid_net = pfi_unmask(&aw->v.a.mask);
352 1.1 itojun if (af == AF_INET && dyn->pfid_net == 32)
353 1.1 itojun dyn->pfid_net = 128;
354 1.1 itojun strlcpy(tblname, aw->v.ifname, sizeof(tblname));
355 1.1 itojun if (aw->iflags & PFI_AFLAG_NETWORK)
356 1.1 itojun strlcat(tblname, ":network", sizeof(tblname));
357 1.1 itojun if (aw->iflags & PFI_AFLAG_BROADCAST)
358 1.1 itojun strlcat(tblname, ":broadcast", sizeof(tblname));
359 1.1 itojun if (aw->iflags & PFI_AFLAG_PEER)
360 1.1 itojun strlcat(tblname, ":peer", sizeof(tblname));
361 1.1 itojun if (aw->iflags & PFI_AFLAG_NOALIAS)
362 1.1 itojun strlcat(tblname, ":0", sizeof(tblname));
363 1.1 itojun if (dyn->pfid_net != 128)
364 1.1 itojun snprintf(tblname + strlen(tblname),
365 1.1 itojun sizeof(tblname) - strlen(tblname), "/%d", dyn->pfid_net);
366 1.1.1.4 martti if ((ruleset = pf_find_or_create_ruleset(PF_RESERVED_ANCHOR)) == NULL) {
367 1.1.1.4 martti rv = 1;
368 1.1.1.4 martti goto _bad;
369 1.1.1.4 martti }
370 1.1.1.4 martti
371 1.1.1.4 martti if ((dyn->pfid_kt = pfr_attach_table(ruleset, tblname)) == NULL) {
372 1.1.1.4 martti rv = 1;
373 1.1.1.4 martti goto _bad;
374 1.1.1.4 martti }
375 1.1 itojun
376 1.1 itojun dyn->pfid_kt->pfrkt_flags |= PFR_TFLAG_ACTIVE;
377 1.1 itojun dyn->pfid_iflags = aw->iflags;
378 1.1 itojun dyn->pfid_af = af;
379 1.1 itojun
380 1.1.1.4 martti TAILQ_INSERT_TAIL(&dyn->pfid_kif->pfik_dynaddrs, dyn, entry);
381 1.1 itojun aw->p.dyn = dyn;
382 1.1.1.4 martti pfi_kif_update(dyn->pfid_kif);
383 1.1 itojun splx(s);
384 1.1 itojun return (0);
385 1.1 itojun
386 1.1 itojun _bad:
387 1.1 itojun if (dyn->pfid_kt != NULL)
388 1.1 itojun pfr_detach_table(dyn->pfid_kt);
389 1.1 itojun if (ruleset != NULL)
390 1.1 itojun pf_remove_if_empty_ruleset(ruleset);
391 1.1 itojun if (dyn->pfid_kif != NULL)
392 1.1.1.4 martti pfi_kif_unref(dyn->pfid_kif, PFI_KIF_REF_RULE);
393 1.1 itojun pool_put(&pfi_addr_pl, dyn);
394 1.1 itojun splx(s);
395 1.1 itojun return (rv);
396 1.1 itojun }
397 1.1 itojun
398 1.1 itojun void
399 1.1.1.4 martti pfi_kif_update(struct pfi_kif *kif)
400 1.1.1.4 martti {
401 1.1.1.4 martti struct ifg_list *ifgl;
402 1.1.1.4 martti struct pfi_dynaddr *p;
403 1.1.1.4 martti
404 1.1.1.4 martti /* update all dynaddr */
405 1.1.1.4 martti TAILQ_FOREACH(p, &kif->pfik_dynaddrs, entry)
406 1.1.1.4 martti pfi_dynaddr_update(p);
407 1.1.1.4 martti
408 1.1.1.4 martti /* again for all groups kif is member of */
409 1.1.1.4 martti if (kif->pfik_ifp != NULL)
410 1.1.1.4 martti TAILQ_FOREACH(ifgl, &kif->pfik_ifp->if_groups, ifgl_next)
411 1.1.1.4 martti pfi_kif_update((struct pfi_kif *)
412 1.1.1.4 martti ifgl->ifgl_group->ifg_pf_kif);
413 1.1.1.4 martti }
414 1.1.1.4 martti
415 1.1.1.4 martti void
416 1.1.1.4 martti pfi_dynaddr_update(struct pfi_dynaddr *dyn)
417 1.1 itojun {
418 1.1.1.3 peter struct pfi_kif *kif;
419 1.1.1.3 peter struct pfr_ktable *kt;
420 1.1 itojun
421 1.1.1.3 peter if (dyn == NULL || dyn->pfid_kif == NULL || dyn->pfid_kt == NULL)
422 1.1 itojun panic("pfi_dynaddr_update");
423 1.1.1.3 peter
424 1.1.1.3 peter kif = dyn->pfid_kif;
425 1.1.1.3 peter kt = dyn->pfid_kt;
426 1.1.1.4 martti
427 1.1 itojun if (kt->pfrkt_larg != pfi_update) {
428 1.1 itojun /* this table needs to be brought up-to-date */
429 1.1 itojun pfi_table_update(kt, kif, dyn->pfid_net, dyn->pfid_iflags);
430 1.1 itojun kt->pfrkt_larg = pfi_update;
431 1.1 itojun }
432 1.1 itojun pfr_dynaddr_update(kt, dyn);
433 1.1 itojun }
434 1.1 itojun
435 1.1 itojun void
436 1.1 itojun pfi_table_update(struct pfr_ktable *kt, struct pfi_kif *kif, int net, int flags)
437 1.1 itojun {
438 1.1 itojun int e, size2 = 0;
439 1.1.1.4 martti struct ifg_member *ifgm;
440 1.1 itojun
441 1.1 itojun pfi_buffer_cnt = 0;
442 1.1.1.4 martti
443 1.1.1.4 martti if (kif->pfik_ifp != NULL)
444 1.1 itojun pfi_instance_add(kif->pfik_ifp, net, flags);
445 1.1.1.4 martti else if (kif->pfik_group != NULL)
446 1.1.1.4 martti TAILQ_FOREACH(ifgm, &kif->pfik_group->ifg_members, ifgm_next)
447 1.1.1.4 martti pfi_instance_add(ifgm->ifgm_ifp, net, flags);
448 1.1.1.4 martti
449 1.1.1.4 martti if ((e = pfr_set_addrs(&kt->pfrkt_t, pfi_buffer, pfi_buffer_cnt, &size2,
450 1.1.1.4 martti NULL, NULL, NULL, 0, PFR_TFLAG_ALLMASK)))
451 1.1 itojun printf("pfi_table_update: cannot set %d new addresses "
452 1.1 itojun "into table %s: %d\n", pfi_buffer_cnt, kt->pfrkt_name, e);
453 1.1 itojun }
454 1.1 itojun
455 1.1 itojun void
456 1.1 itojun pfi_instance_add(struct ifnet *ifp, int net, int flags)
457 1.1 itojun {
458 1.1 itojun struct ifaddr *ia;
459 1.1 itojun int got4 = 0, got6 = 0;
460 1.1 itojun int net2, af;
461 1.1 itojun
462 1.1 itojun if (ifp == NULL)
463 1.1 itojun return;
464 1.1 itojun TAILQ_FOREACH(ia, &ifp->if_addrlist, ifa_list) {
465 1.1 itojun if (ia->ifa_addr == NULL)
466 1.1 itojun continue;
467 1.1 itojun af = ia->ifa_addr->sa_family;
468 1.1 itojun if (af != AF_INET && af != AF_INET6)
469 1.1 itojun continue;
470 1.1 itojun if ((flags & PFI_AFLAG_BROADCAST) && af == AF_INET6)
471 1.1 itojun continue;
472 1.1 itojun if ((flags & PFI_AFLAG_BROADCAST) &&
473 1.1 itojun !(ifp->if_flags & IFF_BROADCAST))
474 1.1 itojun continue;
475 1.1 itojun if ((flags & PFI_AFLAG_PEER) &&
476 1.1 itojun !(ifp->if_flags & IFF_POINTOPOINT))
477 1.1 itojun continue;
478 1.1 itojun if ((flags & PFI_AFLAG_NETWORK) && af == AF_INET6 &&
479 1.1 itojun IN6_IS_ADDR_LINKLOCAL(
480 1.1 itojun &((struct sockaddr_in6 *)ia->ifa_addr)->sin6_addr))
481 1.1 itojun continue;
482 1.1 itojun if (flags & PFI_AFLAG_NOALIAS) {
483 1.1 itojun if (af == AF_INET && got4)
484 1.1 itojun continue;
485 1.1 itojun if (af == AF_INET6 && got6)
486 1.1 itojun continue;
487 1.1 itojun }
488 1.1 itojun if (af == AF_INET)
489 1.1 itojun got4 = 1;
490 1.1.1.2 yamt else if (af == AF_INET6)
491 1.1 itojun got6 = 1;
492 1.1 itojun net2 = net;
493 1.1 itojun if (net2 == 128 && (flags & PFI_AFLAG_NETWORK)) {
494 1.1.1.4 martti if (af == AF_INET)
495 1.1 itojun net2 = pfi_unmask(&((struct sockaddr_in *)
496 1.1 itojun ia->ifa_netmask)->sin_addr);
497 1.1.1.4 martti else if (af == AF_INET6)
498 1.1 itojun net2 = pfi_unmask(&((struct sockaddr_in6 *)
499 1.1 itojun ia->ifa_netmask)->sin6_addr);
500 1.1 itojun }
501 1.1 itojun if (af == AF_INET && net2 > 32)
502 1.1 itojun net2 = 32;
503 1.1 itojun if (flags & PFI_AFLAG_BROADCAST)
504 1.1 itojun pfi_address_add(ia->ifa_broadaddr, af, net2);
505 1.1 itojun else if (flags & PFI_AFLAG_PEER)
506 1.1 itojun pfi_address_add(ia->ifa_dstaddr, af, net2);
507 1.1 itojun else
508 1.1 itojun pfi_address_add(ia->ifa_addr, af, net2);
509 1.1 itojun }
510 1.1 itojun }
511 1.1 itojun
512 1.1 itojun void
513 1.1 itojun pfi_address_add(struct sockaddr *sa, int af, int net)
514 1.1 itojun {
515 1.1 itojun struct pfr_addr *p;
516 1.1 itojun int i;
517 1.1 itojun
518 1.1 itojun if (pfi_buffer_cnt >= pfi_buffer_max) {
519 1.1 itojun int new_max = pfi_buffer_max * 2;
520 1.1 itojun
521 1.1 itojun if (new_max > PFI_BUFFER_MAX) {
522 1.1 itojun printf("pfi_address_add: address buffer full (%d/%d)\n",
523 1.1 itojun pfi_buffer_cnt, PFI_BUFFER_MAX);
524 1.1 itojun return;
525 1.1 itojun }
526 1.1 itojun p = malloc(new_max * sizeof(*pfi_buffer), PFI_MTYPE,
527 1.1 itojun M_DONTWAIT);
528 1.1 itojun if (p == NULL) {
529 1.1 itojun printf("pfi_address_add: no memory to grow buffer "
530 1.1 itojun "(%d/%d)\n", pfi_buffer_cnt, PFI_BUFFER_MAX);
531 1.1 itojun return;
532 1.1 itojun }
533 1.1 itojun memcpy(pfi_buffer, p, pfi_buffer_cnt * sizeof(*pfi_buffer));
534 1.1 itojun /* no need to zero buffer */
535 1.1 itojun free(pfi_buffer, PFI_MTYPE);
536 1.1 itojun pfi_buffer = p;
537 1.1 itojun pfi_buffer_max = new_max;
538 1.1 itojun }
539 1.1 itojun if (af == AF_INET && net > 32)
540 1.1 itojun net = 128;
541 1.1 itojun p = pfi_buffer + pfi_buffer_cnt++;
542 1.1 itojun bzero(p, sizeof(*p));
543 1.1 itojun p->pfra_af = af;
544 1.1 itojun p->pfra_net = net;
545 1.1 itojun if (af == AF_INET)
546 1.1 itojun p->pfra_ip4addr = ((struct sockaddr_in *)sa)->sin_addr;
547 1.1.1.4 martti else if (af == AF_INET6) {
548 1.1 itojun p->pfra_ip6addr = ((struct sockaddr_in6 *)sa)->sin6_addr;
549 1.1.1.4 martti if (IN6_IS_SCOPE_EMBED(&p->pfra_ip6addr))
550 1.1 itojun p->pfra_ip6addr.s6_addr16[1] = 0;
551 1.1 itojun }
552 1.1 itojun /* mask network address bits */
553 1.1 itojun if (net < 128)
554 1.1 itojun ((caddr_t)p)[p->pfra_net/8] &= ~(0xFF >> (p->pfra_net%8));
555 1.1 itojun for (i = (p->pfra_net+7)/8; i < sizeof(p->pfra_u); i++)
556 1.1 itojun ((caddr_t)p)[i] = 0;
557 1.1 itojun }
558 1.1 itojun
559 1.1 itojun void
560 1.1 itojun pfi_dynaddr_remove(struct pf_addr_wrap *aw)
561 1.1 itojun {
562 1.1 itojun int s;
563 1.1 itojun
564 1.1 itojun if (aw->type != PF_ADDR_DYNIFTL || aw->p.dyn == NULL ||
565 1.1 itojun aw->p.dyn->pfid_kif == NULL || aw->p.dyn->pfid_kt == NULL)
566 1.1 itojun return;
567 1.1 itojun
568 1.1 itojun s = splsoftnet();
569 1.1.1.4 martti TAILQ_REMOVE(&aw->p.dyn->pfid_kif->pfik_dynaddrs, aw->p.dyn, entry);
570 1.1.1.4 martti pfi_kif_unref(aw->p.dyn->pfid_kif, PFI_KIF_REF_RULE);
571 1.1 itojun aw->p.dyn->pfid_kif = NULL;
572 1.1 itojun pfr_detach_table(aw->p.dyn->pfid_kt);
573 1.1 itojun aw->p.dyn->pfid_kt = NULL;
574 1.1 itojun pool_put(&pfi_addr_pl, aw->p.dyn);
575 1.1 itojun aw->p.dyn = NULL;
576 1.1 itojun splx(s);
577 1.1 itojun }
578 1.1 itojun
579 1.1 itojun void
580 1.1 itojun pfi_dynaddr_copyout(struct pf_addr_wrap *aw)
581 1.1 itojun {
582 1.1 itojun if (aw->type != PF_ADDR_DYNIFTL || aw->p.dyn == NULL ||
583 1.1 itojun aw->p.dyn->pfid_kif == NULL)
584 1.1 itojun return;
585 1.1 itojun aw->p.dyncnt = aw->p.dyn->pfid_acnt4 + aw->p.dyn->pfid_acnt6;
586 1.1 itojun }
587 1.1 itojun
588 1.1 itojun void
589 1.1 itojun pfi_kifaddr_update(void *v)
590 1.1 itojun {
591 1.1.1.4 martti int s;
592 1.1.1.4 martti struct pfi_kif *kif = (struct pfi_kif *)v;
593 1.1 itojun
594 1.1 itojun s = splsoftnet();
595 1.1 itojun pfi_update++;
596 1.1.1.4 martti pfi_kif_update(kif);
597 1.1 itojun splx(s);
598 1.1 itojun }
599 1.1 itojun
600 1.1 itojun int
601 1.1 itojun pfi_if_compare(struct pfi_kif *p, struct pfi_kif *q)
602 1.1 itojun {
603 1.1 itojun return (strncmp(p->pfik_name, q->pfik_name, IFNAMSIZ));
604 1.1 itojun }
605 1.1 itojun
606 1.1 itojun void
607 1.1 itojun pfi_fill_oldstatus(struct pf_status *pfs)
608 1.1 itojun {
609 1.1.1.4 martti struct pfi_kif *p;
610 1.1.1.4 martti struct pfi_kif_cmp key;
611 1.1.1.4 martti int i, j, k, s;
612 1.1 itojun
613 1.1 itojun strlcpy(key.pfik_name, pfs->ifname, sizeof(key.pfik_name));
614 1.1 itojun s = splsoftnet();
615 1.1.1.4 martti p = RB_FIND(pfi_ifhead, &pfi_ifs, (struct pfi_kif *)&key);
616 1.1 itojun if (p == NULL) {
617 1.1 itojun splx(s);
618 1.1 itojun return;
619 1.1 itojun }
620 1.1 itojun bzero(pfs->pcounters, sizeof(pfs->pcounters));
621 1.1 itojun bzero(pfs->bcounters, sizeof(pfs->bcounters));
622 1.1 itojun for (i = 0; i < 2; i++)
623 1.1 itojun for (j = 0; j < 2; j++)
624 1.1 itojun for (k = 0; k < 2; k++) {
625 1.1 itojun pfs->pcounters[i][j][k] =
626 1.1 itojun p->pfik_packets[i][j][k];
627 1.1 itojun pfs->bcounters[i][j] +=
628 1.1 itojun p->pfik_bytes[i][j][k];
629 1.1 itojun }
630 1.1 itojun splx(s);
631 1.1 itojun }
632 1.1 itojun
633 1.1 itojun int
634 1.1.1.4 martti pfi_clr_istats(const char *name)
635 1.1 itojun {
636 1.1 itojun struct pfi_kif *p;
637 1.1.1.4 martti int s;
638 1.1 itojun
639 1.1.1.3 peter s = splsoftnet();
640 1.1 itojun RB_FOREACH(p, pfi_ifhead, &pfi_ifs) {
641 1.1.1.4 martti if (pfi_skip_if(name, p))
642 1.1 itojun continue;
643 1.1 itojun bzero(p->pfik_packets, sizeof(p->pfik_packets));
644 1.1 itojun bzero(p->pfik_bytes, sizeof(p->pfik_bytes));
645 1.1.1.4 martti p->pfik_tzero = time_second;
646 1.1 itojun }
647 1.1 itojun splx(s);
648 1.1.1.3 peter
649 1.1.1.3 peter return (0);
650 1.1.1.3 peter }
651 1.1.1.3 peter
652 1.1.1.3 peter int
653 1.1.1.4 martti pfi_get_ifaces(const char *name, struct pfi_kif *buf, int *size)
654 1.1 itojun {
655 1.1.1.4 martti struct pfi_kif *p, *nextp;
656 1.1 itojun int s, n = 0;
657 1.1 itojun
658 1.1 itojun s = splsoftnet();
659 1.1.1.4 martti for (p = RB_MIN(pfi_ifhead, &pfi_ifs); p; p = nextp) {
660 1.1.1.4 martti nextp = RB_NEXT(pfi_ifhead, &pfi_ifs, p);
661 1.1.1.4 martti if (pfi_skip_if(name, p))
662 1.1 itojun continue;
663 1.1 itojun if (*size > n++) {
664 1.1 itojun if (!p->pfik_tzero)
665 1.1.1.2 yamt p->pfik_tzero = time_second;
666 1.1.1.4 martti pfi_kif_ref(p, PFI_KIF_REF_RULE);
667 1.1 itojun if (copyout(p, buf++, sizeof(*buf))) {
668 1.1.1.4 martti pfi_kif_unref(p, PFI_KIF_REF_RULE);
669 1.1 itojun splx(s);
670 1.1 itojun return (EFAULT);
671 1.1 itojun }
672 1.1.1.4 martti nextp = RB_NEXT(pfi_ifhead, &pfi_ifs, p);
673 1.1.1.4 martti pfi_kif_unref(p, PFI_KIF_REF_RULE);
674 1.1 itojun }
675 1.1 itojun }
676 1.1 itojun splx(s);
677 1.1 itojun *size = n;
678 1.1 itojun return (0);
679 1.1 itojun }
680 1.1 itojun
681 1.1 itojun int
682 1.1.1.4 martti pfi_skip_if(const char *filter, struct pfi_kif *p)
683 1.1 itojun {
684 1.1 itojun int n;
685 1.1 itojun
686 1.1 itojun if (filter == NULL || !*filter)
687 1.1 itojun return (0);
688 1.1 itojun if (!strcmp(p->pfik_name, filter))
689 1.1 itojun return (0); /* exact match */
690 1.1 itojun n = strlen(filter);
691 1.1 itojun if (n < 1 || n >= IFNAMSIZ)
692 1.1 itojun return (1); /* sanity check */
693 1.1 itojun if (filter[n-1] >= '0' && filter[n-1] <= '9')
694 1.1 itojun return (1); /* only do exact match in that case */
695 1.1 itojun if (strncmp(p->pfik_name, filter, n))
696 1.1 itojun return (1); /* prefix doesn't match */
697 1.1 itojun return (p->pfik_name[n] < '0' || p->pfik_name[n] > '9');
698 1.1 itojun }
699 1.1 itojun
700 1.1.1.4 martti int
701 1.1.1.4 martti pfi_set_flags(const char *name, int flags)
702 1.1.1.4 martti {
703 1.1.1.4 martti struct pfi_kif *p;
704 1.1.1.4 martti int s;
705 1.1.1.4 martti
706 1.1.1.4 martti s = splsoftnet();
707 1.1.1.4 martti RB_FOREACH(p, pfi_ifhead, &pfi_ifs) {
708 1.1.1.4 martti if (pfi_skip_if(name, p))
709 1.1.1.4 martti continue;
710 1.1.1.4 martti p->pfik_flags |= flags;
711 1.1.1.4 martti }
712 1.1.1.4 martti splx(s);
713 1.1.1.4 martti return (0);
714 1.1.1.4 martti }
715 1.1.1.4 martti
716 1.1.1.4 martti int
717 1.1.1.4 martti pfi_clear_flags(const char *name, int flags)
718 1.1.1.4 martti {
719 1.1.1.4 martti struct pfi_kif *p;
720 1.1.1.4 martti int s;
721 1.1.1.4 martti
722 1.1.1.4 martti s = splsoftnet();
723 1.1.1.4 martti RB_FOREACH(p, pfi_ifhead, &pfi_ifs) {
724 1.1.1.4 martti if (pfi_skip_if(name, p))
725 1.1.1.4 martti continue;
726 1.1.1.4 martti p->pfik_flags &= ~flags;
727 1.1.1.4 martti }
728 1.1.1.4 martti splx(s);
729 1.1.1.4 martti return (0);
730 1.1.1.4 martti }
731 1.1.1.4 martti
732 1.1 itojun /* from pf_print_state.c */
733 1.1 itojun int
734 1.1 itojun pfi_unmask(void *addr)
735 1.1 itojun {
736 1.1 itojun struct pf_addr *m = addr;
737 1.1 itojun int i = 31, j = 0, b = 0;
738 1.1 itojun u_int32_t tmp;
739 1.1 itojun
740 1.1 itojun while (j < 4 && m->addr32[j] == 0xffffffff) {
741 1.1 itojun b += 32;
742 1.1 itojun j++;
743 1.1 itojun }
744 1.1 itojun if (j < 4) {
745 1.1 itojun tmp = ntohl(m->addr32[j]);
746 1.1 itojun for (i = 31; tmp & (1 << i); --i)
747 1.1 itojun b++;
748 1.1 itojun }
749 1.1 itojun return (b);
750 1.1 itojun }
751 1.1 itojun
752