pf_norm.c revision 1.1 1 1.1 itojun /* $OpenBSD: pf_norm.c,v 1.80 2004/03/09 21:44:41 mcbride Exp $ */
2 1.1 itojun
3 1.1 itojun /*
4 1.1 itojun * Copyright 2001 Niels Provos <provos (at) citi.umich.edu>
5 1.1 itojun * All rights reserved.
6 1.1 itojun *
7 1.1 itojun * Redistribution and use in source and binary forms, with or without
8 1.1 itojun * modification, are permitted provided that the following conditions
9 1.1 itojun * are met:
10 1.1 itojun * 1. Redistributions of source code must retain the above copyright
11 1.1 itojun * notice, this list of conditions and the following disclaimer.
12 1.1 itojun * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 itojun * notice, this list of conditions and the following disclaimer in the
14 1.1 itojun * documentation and/or other materials provided with the distribution.
15 1.1 itojun *
16 1.1 itojun * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 1.1 itojun * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 1.1 itojun * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 1.1 itojun * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 1.1 itojun * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 1.1 itojun * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 1.1 itojun * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 1.1 itojun * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 1.1 itojun * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 1.1 itojun * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 1.1 itojun */
27 1.1 itojun
28 1.1 itojun #include "pflog.h"
29 1.1 itojun
30 1.1 itojun #include <sys/param.h>
31 1.1 itojun #include <sys/systm.h>
32 1.1 itojun #include <sys/mbuf.h>
33 1.1 itojun #include <sys/filio.h>
34 1.1 itojun #include <sys/fcntl.h>
35 1.1 itojun #include <sys/socket.h>
36 1.1 itojun #include <sys/kernel.h>
37 1.1 itojun #include <sys/time.h>
38 1.1 itojun #include <sys/pool.h>
39 1.1 itojun
40 1.1 itojun #include <dev/rndvar.h>
41 1.1 itojun #include <net/if.h>
42 1.1 itojun #include <net/if_types.h>
43 1.1 itojun #include <net/bpf.h>
44 1.1 itojun #include <net/route.h>
45 1.1 itojun #include <net/if_pflog.h>
46 1.1 itojun
47 1.1 itojun #include <netinet/in.h>
48 1.1 itojun #include <netinet/in_var.h>
49 1.1 itojun #include <netinet/in_systm.h>
50 1.1 itojun #include <netinet/ip.h>
51 1.1 itojun #include <netinet/ip_var.h>
52 1.1 itojun #include <netinet/tcp.h>
53 1.1 itojun #include <netinet/tcp_seq.h>
54 1.1 itojun #include <netinet/udp.h>
55 1.1 itojun #include <netinet/ip_icmp.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 itojun #include <net/pfvar.h>
62 1.1 itojun
63 1.1 itojun struct pf_frent {
64 1.1 itojun LIST_ENTRY(pf_frent) fr_next;
65 1.1 itojun struct ip *fr_ip;
66 1.1 itojun struct mbuf *fr_m;
67 1.1 itojun };
68 1.1 itojun
69 1.1 itojun struct pf_frcache {
70 1.1 itojun LIST_ENTRY(pf_frcache) fr_next;
71 1.1 itojun uint16_t fr_off;
72 1.1 itojun uint16_t fr_end;
73 1.1 itojun };
74 1.1 itojun
75 1.1 itojun #define PFFRAG_SEENLAST 0x0001 /* Seen the last fragment for this */
76 1.1 itojun #define PFFRAG_NOBUFFER 0x0002 /* Non-buffering fragment cache */
77 1.1 itojun #define PFFRAG_DROP 0x0004 /* Drop all fragments */
78 1.1 itojun #define BUFFER_FRAGMENTS(fr) (!((fr)->fr_flags & PFFRAG_NOBUFFER))
79 1.1 itojun
80 1.1 itojun struct pf_fragment {
81 1.1 itojun RB_ENTRY(pf_fragment) fr_entry;
82 1.1 itojun TAILQ_ENTRY(pf_fragment) frag_next;
83 1.1 itojun struct in_addr fr_src;
84 1.1 itojun struct in_addr fr_dst;
85 1.1 itojun u_int8_t fr_p; /* protocol of this fragment */
86 1.1 itojun u_int8_t fr_flags; /* status flags */
87 1.1 itojun u_int16_t fr_id; /* fragment id for reassemble */
88 1.1 itojun u_int16_t fr_max; /* fragment data max */
89 1.1 itojun u_int32_t fr_timeout;
90 1.1 itojun #define fr_queue fr_u.fru_queue
91 1.1 itojun #define fr_cache fr_u.fru_cache
92 1.1 itojun union {
93 1.1 itojun LIST_HEAD(pf_fragq, pf_frent) fru_queue; /* buffering */
94 1.1 itojun LIST_HEAD(pf_cacheq, pf_frcache) fru_cache; /* non-buf */
95 1.1 itojun } fr_u;
96 1.1 itojun };
97 1.1 itojun
98 1.1 itojun TAILQ_HEAD(pf_fragqueue, pf_fragment) pf_fragqueue;
99 1.1 itojun TAILQ_HEAD(pf_cachequeue, pf_fragment) pf_cachequeue;
100 1.1 itojun
101 1.1 itojun static __inline int pf_frag_compare(struct pf_fragment *,
102 1.1 itojun struct pf_fragment *);
103 1.1 itojun RB_HEAD(pf_frag_tree, pf_fragment) pf_frag_tree, pf_cache_tree;
104 1.1 itojun RB_PROTOTYPE(pf_frag_tree, pf_fragment, fr_entry, pf_frag_compare);
105 1.1 itojun RB_GENERATE(pf_frag_tree, pf_fragment, fr_entry, pf_frag_compare);
106 1.1 itojun
107 1.1 itojun /* Private prototypes */
108 1.1 itojun void pf_ip2key(struct pf_fragment *, struct ip *);
109 1.1 itojun void pf_remove_fragment(struct pf_fragment *);
110 1.1 itojun void pf_flush_fragments(void);
111 1.1 itojun void pf_free_fragment(struct pf_fragment *);
112 1.1 itojun struct pf_fragment *pf_find_fragment(struct ip *, struct pf_frag_tree *);
113 1.1 itojun struct mbuf *pf_reassemble(struct mbuf **, struct pf_fragment **,
114 1.1 itojun struct pf_frent *, int);
115 1.1 itojun struct mbuf *pf_fragcache(struct mbuf **, struct ip*,
116 1.1 itojun struct pf_fragment **, int, int, int *);
117 1.1 itojun u_int16_t pf_cksum_fixup(u_int16_t, u_int16_t, u_int16_t);
118 1.1 itojun int pf_normalize_tcpopt(struct pf_rule *, struct mbuf *,
119 1.1 itojun struct tcphdr *, int);
120 1.1 itojun
121 1.1 itojun #define DPFPRINTF(x) if (pf_status.debug >= PF_DEBUG_MISC) \
122 1.1 itojun { printf("%s: ", __func__); printf x ;}
123 1.1 itojun
124 1.1 itojun /* Globals */
125 1.1 itojun struct pool pf_frent_pl, pf_frag_pl, pf_cache_pl, pf_cent_pl;
126 1.1 itojun struct pool pf_state_scrub_pl;
127 1.1 itojun int pf_nfrents, pf_ncache;
128 1.1 itojun
129 1.1 itojun void
130 1.1 itojun pf_normalize_init(void)
131 1.1 itojun {
132 1.1 itojun pool_init(&pf_frent_pl, sizeof(struct pf_frent), 0, 0, 0, "pffrent",
133 1.1 itojun NULL);
134 1.1 itojun pool_init(&pf_frag_pl, sizeof(struct pf_fragment), 0, 0, 0, "pffrag",
135 1.1 itojun NULL);
136 1.1 itojun pool_init(&pf_cache_pl, sizeof(struct pf_fragment), 0, 0, 0,
137 1.1 itojun "pffrcache", NULL);
138 1.1 itojun pool_init(&pf_cent_pl, sizeof(struct pf_frcache), 0, 0, 0, "pffrcent",
139 1.1 itojun NULL);
140 1.1 itojun pool_init(&pf_state_scrub_pl, sizeof(struct pf_state_scrub), 0, 0, 0,
141 1.1 itojun "pfstscr", NULL);
142 1.1 itojun
143 1.1 itojun pool_sethiwat(&pf_frag_pl, PFFRAG_FRAG_HIWAT);
144 1.1 itojun pool_sethardlimit(&pf_frent_pl, PFFRAG_FRENT_HIWAT, NULL, 0);
145 1.1 itojun pool_sethardlimit(&pf_cache_pl, PFFRAG_FRCACHE_HIWAT, NULL, 0);
146 1.1 itojun pool_sethardlimit(&pf_cent_pl, PFFRAG_FRCENT_HIWAT, NULL, 0);
147 1.1 itojun
148 1.1 itojun TAILQ_INIT(&pf_fragqueue);
149 1.1 itojun TAILQ_INIT(&pf_cachequeue);
150 1.1 itojun }
151 1.1 itojun
152 1.1 itojun static __inline int
153 1.1 itojun pf_frag_compare(struct pf_fragment *a, struct pf_fragment *b)
154 1.1 itojun {
155 1.1 itojun int diff;
156 1.1 itojun
157 1.1 itojun if ((diff = a->fr_id - b->fr_id))
158 1.1 itojun return (diff);
159 1.1 itojun else if ((diff = a->fr_p - b->fr_p))
160 1.1 itojun return (diff);
161 1.1 itojun else if (a->fr_src.s_addr < b->fr_src.s_addr)
162 1.1 itojun return (-1);
163 1.1 itojun else if (a->fr_src.s_addr > b->fr_src.s_addr)
164 1.1 itojun return (1);
165 1.1 itojun else if (a->fr_dst.s_addr < b->fr_dst.s_addr)
166 1.1 itojun return (-1);
167 1.1 itojun else if (a->fr_dst.s_addr > b->fr_dst.s_addr)
168 1.1 itojun return (1);
169 1.1 itojun return (0);
170 1.1 itojun }
171 1.1 itojun
172 1.1 itojun void
173 1.1 itojun pf_purge_expired_fragments(void)
174 1.1 itojun {
175 1.1 itojun struct pf_fragment *frag;
176 1.1 itojun u_int32_t expire = time.tv_sec -
177 1.1 itojun pf_default_rule.timeout[PFTM_FRAG];
178 1.1 itojun
179 1.1 itojun while ((frag = TAILQ_LAST(&pf_fragqueue, pf_fragqueue)) != NULL) {
180 1.1 itojun KASSERT(BUFFER_FRAGMENTS(frag));
181 1.1 itojun if (frag->fr_timeout > expire)
182 1.1 itojun break;
183 1.1 itojun
184 1.1 itojun DPFPRINTF(("expiring %d(%p)\n", frag->fr_id, frag));
185 1.1 itojun pf_free_fragment(frag);
186 1.1 itojun }
187 1.1 itojun
188 1.1 itojun while ((frag = TAILQ_LAST(&pf_cachequeue, pf_cachequeue)) != NULL) {
189 1.1 itojun KASSERT(!BUFFER_FRAGMENTS(frag));
190 1.1 itojun if (frag->fr_timeout > expire)
191 1.1 itojun break;
192 1.1 itojun
193 1.1 itojun DPFPRINTF(("expiring %d(%p)\n", frag->fr_id, frag));
194 1.1 itojun pf_free_fragment(frag);
195 1.1 itojun KASSERT(TAILQ_EMPTY(&pf_cachequeue) ||
196 1.1 itojun TAILQ_LAST(&pf_cachequeue, pf_cachequeue) != frag);
197 1.1 itojun }
198 1.1 itojun }
199 1.1 itojun
200 1.1 itojun /*
201 1.1 itojun * Try to flush old fragments to make space for new ones
202 1.1 itojun */
203 1.1 itojun
204 1.1 itojun void
205 1.1 itojun pf_flush_fragments(void)
206 1.1 itojun {
207 1.1 itojun struct pf_fragment *frag;
208 1.1 itojun int goal;
209 1.1 itojun
210 1.1 itojun goal = pf_nfrents * 9 / 10;
211 1.1 itojun DPFPRINTF(("trying to free > %d frents\n",
212 1.1 itojun pf_nfrents - goal));
213 1.1 itojun while (goal < pf_nfrents) {
214 1.1 itojun frag = TAILQ_LAST(&pf_fragqueue, pf_fragqueue);
215 1.1 itojun if (frag == NULL)
216 1.1 itojun break;
217 1.1 itojun pf_free_fragment(frag);
218 1.1 itojun }
219 1.1 itojun
220 1.1 itojun
221 1.1 itojun goal = pf_ncache * 9 / 10;
222 1.1 itojun DPFPRINTF(("trying to free > %d cache entries\n",
223 1.1 itojun pf_ncache - goal));
224 1.1 itojun while (goal < pf_ncache) {
225 1.1 itojun frag = TAILQ_LAST(&pf_cachequeue, pf_cachequeue);
226 1.1 itojun if (frag == NULL)
227 1.1 itojun break;
228 1.1 itojun pf_free_fragment(frag);
229 1.1 itojun }
230 1.1 itojun }
231 1.1 itojun
232 1.1 itojun /* Frees the fragments and all associated entries */
233 1.1 itojun
234 1.1 itojun void
235 1.1 itojun pf_free_fragment(struct pf_fragment *frag)
236 1.1 itojun {
237 1.1 itojun struct pf_frent *frent;
238 1.1 itojun struct pf_frcache *frcache;
239 1.1 itojun
240 1.1 itojun /* Free all fragments */
241 1.1 itojun if (BUFFER_FRAGMENTS(frag)) {
242 1.1 itojun for (frent = LIST_FIRST(&frag->fr_queue); frent;
243 1.1 itojun frent = LIST_FIRST(&frag->fr_queue)) {
244 1.1 itojun LIST_REMOVE(frent, fr_next);
245 1.1 itojun
246 1.1 itojun m_freem(frent->fr_m);
247 1.1 itojun pool_put(&pf_frent_pl, frent);
248 1.1 itojun pf_nfrents--;
249 1.1 itojun }
250 1.1 itojun } else {
251 1.1 itojun for (frcache = LIST_FIRST(&frag->fr_cache); frcache;
252 1.1 itojun frcache = LIST_FIRST(&frag->fr_cache)) {
253 1.1 itojun LIST_REMOVE(frcache, fr_next);
254 1.1 itojun
255 1.1 itojun KASSERT(LIST_EMPTY(&frag->fr_cache) ||
256 1.1 itojun LIST_FIRST(&frag->fr_cache)->fr_off >
257 1.1 itojun frcache->fr_end);
258 1.1 itojun
259 1.1 itojun pool_put(&pf_cent_pl, frcache);
260 1.1 itojun pf_ncache--;
261 1.1 itojun }
262 1.1 itojun }
263 1.1 itojun
264 1.1 itojun pf_remove_fragment(frag);
265 1.1 itojun }
266 1.1 itojun
267 1.1 itojun void
268 1.1 itojun pf_ip2key(struct pf_fragment *key, struct ip *ip)
269 1.1 itojun {
270 1.1 itojun key->fr_p = ip->ip_p;
271 1.1 itojun key->fr_id = ip->ip_id;
272 1.1 itojun key->fr_src.s_addr = ip->ip_src.s_addr;
273 1.1 itojun key->fr_dst.s_addr = ip->ip_dst.s_addr;
274 1.1 itojun }
275 1.1 itojun
276 1.1 itojun struct pf_fragment *
277 1.1 itojun pf_find_fragment(struct ip *ip, struct pf_frag_tree *tree)
278 1.1 itojun {
279 1.1 itojun struct pf_fragment key;
280 1.1 itojun struct pf_fragment *frag;
281 1.1 itojun
282 1.1 itojun pf_ip2key(&key, ip);
283 1.1 itojun
284 1.1 itojun frag = RB_FIND(pf_frag_tree, tree, &key);
285 1.1 itojun if (frag != NULL) {
286 1.1 itojun /* XXX Are we sure we want to update the timeout? */
287 1.1 itojun frag->fr_timeout = time.tv_sec;
288 1.1 itojun if (BUFFER_FRAGMENTS(frag)) {
289 1.1 itojun TAILQ_REMOVE(&pf_fragqueue, frag, frag_next);
290 1.1 itojun TAILQ_INSERT_HEAD(&pf_fragqueue, frag, frag_next);
291 1.1 itojun } else {
292 1.1 itojun TAILQ_REMOVE(&pf_cachequeue, frag, frag_next);
293 1.1 itojun TAILQ_INSERT_HEAD(&pf_cachequeue, frag, frag_next);
294 1.1 itojun }
295 1.1 itojun }
296 1.1 itojun
297 1.1 itojun return (frag);
298 1.1 itojun }
299 1.1 itojun
300 1.1 itojun /* Removes a fragment from the fragment queue and frees the fragment */
301 1.1 itojun
302 1.1 itojun void
303 1.1 itojun pf_remove_fragment(struct pf_fragment *frag)
304 1.1 itojun {
305 1.1 itojun if (BUFFER_FRAGMENTS(frag)) {
306 1.1 itojun RB_REMOVE(pf_frag_tree, &pf_frag_tree, frag);
307 1.1 itojun TAILQ_REMOVE(&pf_fragqueue, frag, frag_next);
308 1.1 itojun pool_put(&pf_frag_pl, frag);
309 1.1 itojun } else {
310 1.1 itojun RB_REMOVE(pf_frag_tree, &pf_cache_tree, frag);
311 1.1 itojun TAILQ_REMOVE(&pf_cachequeue, frag, frag_next);
312 1.1 itojun pool_put(&pf_cache_pl, frag);
313 1.1 itojun }
314 1.1 itojun }
315 1.1 itojun
316 1.1 itojun #define FR_IP_OFF(fr) ((ntohs((fr)->fr_ip->ip_off) & IP_OFFMASK) << 3)
317 1.1 itojun struct mbuf *
318 1.1 itojun pf_reassemble(struct mbuf **m0, struct pf_fragment **frag,
319 1.1 itojun struct pf_frent *frent, int mff)
320 1.1 itojun {
321 1.1 itojun struct mbuf *m = *m0, *m2;
322 1.1 itojun struct pf_frent *frea, *next;
323 1.1 itojun struct pf_frent *frep = NULL;
324 1.1 itojun struct ip *ip = frent->fr_ip;
325 1.1 itojun int hlen = ip->ip_hl << 2;
326 1.1 itojun u_int16_t off = (ntohs(ip->ip_off) & IP_OFFMASK) << 3;
327 1.1 itojun u_int16_t ip_len = ntohs(ip->ip_len) - ip->ip_hl * 4;
328 1.1 itojun u_int16_t max = ip_len + off;
329 1.1 itojun
330 1.1 itojun KASSERT(*frag == NULL || BUFFER_FRAGMENTS(*frag));
331 1.1 itojun
332 1.1 itojun /* Strip off ip header */
333 1.1 itojun m->m_data += hlen;
334 1.1 itojun m->m_len -= hlen;
335 1.1 itojun
336 1.1 itojun /* Create a new reassembly queue for this packet */
337 1.1 itojun if (*frag == NULL) {
338 1.1 itojun *frag = pool_get(&pf_frag_pl, PR_NOWAIT);
339 1.1 itojun if (*frag == NULL) {
340 1.1 itojun pf_flush_fragments();
341 1.1 itojun *frag = pool_get(&pf_frag_pl, PR_NOWAIT);
342 1.1 itojun if (*frag == NULL)
343 1.1 itojun goto drop_fragment;
344 1.1 itojun }
345 1.1 itojun
346 1.1 itojun (*frag)->fr_flags = 0;
347 1.1 itojun (*frag)->fr_max = 0;
348 1.1 itojun (*frag)->fr_src = frent->fr_ip->ip_src;
349 1.1 itojun (*frag)->fr_dst = frent->fr_ip->ip_dst;
350 1.1 itojun (*frag)->fr_p = frent->fr_ip->ip_p;
351 1.1 itojun (*frag)->fr_id = frent->fr_ip->ip_id;
352 1.1 itojun (*frag)->fr_timeout = time.tv_sec;
353 1.1 itojun LIST_INIT(&(*frag)->fr_queue);
354 1.1 itojun
355 1.1 itojun RB_INSERT(pf_frag_tree, &pf_frag_tree, *frag);
356 1.1 itojun TAILQ_INSERT_HEAD(&pf_fragqueue, *frag, frag_next);
357 1.1 itojun
358 1.1 itojun /* We do not have a previous fragment */
359 1.1 itojun frep = NULL;
360 1.1 itojun goto insert;
361 1.1 itojun }
362 1.1 itojun
363 1.1 itojun /*
364 1.1 itojun * Find a fragment after the current one:
365 1.1 itojun * - off contains the real shifted offset.
366 1.1 itojun */
367 1.1 itojun LIST_FOREACH(frea, &(*frag)->fr_queue, fr_next) {
368 1.1 itojun if (FR_IP_OFF(frea) > off)
369 1.1 itojun break;
370 1.1 itojun frep = frea;
371 1.1 itojun }
372 1.1 itojun
373 1.1 itojun KASSERT(frep != NULL || frea != NULL);
374 1.1 itojun
375 1.1 itojun if (frep != NULL &&
376 1.1 itojun FR_IP_OFF(frep) + ntohs(frep->fr_ip->ip_len) - frep->fr_ip->ip_hl *
377 1.1 itojun 4 > off)
378 1.1 itojun {
379 1.1 itojun u_int16_t precut;
380 1.1 itojun
381 1.1 itojun precut = FR_IP_OFF(frep) + ntohs(frep->fr_ip->ip_len) -
382 1.1 itojun frep->fr_ip->ip_hl * 4 - off;
383 1.1 itojun if (precut >= ip_len)
384 1.1 itojun goto drop_fragment;
385 1.1 itojun m_adj(frent->fr_m, precut);
386 1.1 itojun DPFPRINTF(("overlap -%d\n", precut));
387 1.1 itojun /* Enforce 8 byte boundaries */
388 1.1 itojun ip->ip_off = htons(ntohs(ip->ip_off) + (precut >> 3));
389 1.1 itojun off = (ntohs(ip->ip_off) & IP_OFFMASK) << 3;
390 1.1 itojun ip_len -= precut;
391 1.1 itojun ip->ip_len = htons(ip_len);
392 1.1 itojun }
393 1.1 itojun
394 1.1 itojun for (; frea != NULL && ip_len + off > FR_IP_OFF(frea);
395 1.1 itojun frea = next)
396 1.1 itojun {
397 1.1 itojun u_int16_t aftercut;
398 1.1 itojun
399 1.1 itojun aftercut = ip_len + off - FR_IP_OFF(frea);
400 1.1 itojun DPFPRINTF(("adjust overlap %d\n", aftercut));
401 1.1 itojun if (aftercut < ntohs(frea->fr_ip->ip_len) - frea->fr_ip->ip_hl
402 1.1 itojun * 4)
403 1.1 itojun {
404 1.1 itojun frea->fr_ip->ip_len =
405 1.1 itojun htons(ntohs(frea->fr_ip->ip_len) - aftercut);
406 1.1 itojun frea->fr_ip->ip_off = htons(ntohs(frea->fr_ip->ip_off) +
407 1.1 itojun (aftercut >> 3));
408 1.1 itojun m_adj(frea->fr_m, aftercut);
409 1.1 itojun break;
410 1.1 itojun }
411 1.1 itojun
412 1.1 itojun /* This fragment is completely overlapped, loose it */
413 1.1 itojun next = LIST_NEXT(frea, fr_next);
414 1.1 itojun m_freem(frea->fr_m);
415 1.1 itojun LIST_REMOVE(frea, fr_next);
416 1.1 itojun pool_put(&pf_frent_pl, frea);
417 1.1 itojun pf_nfrents--;
418 1.1 itojun }
419 1.1 itojun
420 1.1 itojun insert:
421 1.1 itojun /* Update maximum data size */
422 1.1 itojun if ((*frag)->fr_max < max)
423 1.1 itojun (*frag)->fr_max = max;
424 1.1 itojun /* This is the last segment */
425 1.1 itojun if (!mff)
426 1.1 itojun (*frag)->fr_flags |= PFFRAG_SEENLAST;
427 1.1 itojun
428 1.1 itojun if (frep == NULL)
429 1.1 itojun LIST_INSERT_HEAD(&(*frag)->fr_queue, frent, fr_next);
430 1.1 itojun else
431 1.1 itojun LIST_INSERT_AFTER(frep, frent, fr_next);
432 1.1 itojun
433 1.1 itojun /* Check if we are completely reassembled */
434 1.1 itojun if (!((*frag)->fr_flags & PFFRAG_SEENLAST))
435 1.1 itojun return (NULL);
436 1.1 itojun
437 1.1 itojun /* Check if we have all the data */
438 1.1 itojun off = 0;
439 1.1 itojun for (frep = LIST_FIRST(&(*frag)->fr_queue); frep; frep = next) {
440 1.1 itojun next = LIST_NEXT(frep, fr_next);
441 1.1 itojun
442 1.1 itojun off += ntohs(frep->fr_ip->ip_len) - frep->fr_ip->ip_hl * 4;
443 1.1 itojun if (off < (*frag)->fr_max &&
444 1.1 itojun (next == NULL || FR_IP_OFF(next) != off))
445 1.1 itojun {
446 1.1 itojun DPFPRINTF(("missing fragment at %d, next %d, max %d\n",
447 1.1 itojun off, next == NULL ? -1 : FR_IP_OFF(next),
448 1.1 itojun (*frag)->fr_max));
449 1.1 itojun return (NULL);
450 1.1 itojun }
451 1.1 itojun }
452 1.1 itojun DPFPRINTF(("%d < %d?\n", off, (*frag)->fr_max));
453 1.1 itojun if (off < (*frag)->fr_max)
454 1.1 itojun return (NULL);
455 1.1 itojun
456 1.1 itojun /* We have all the data */
457 1.1 itojun frent = LIST_FIRST(&(*frag)->fr_queue);
458 1.1 itojun KASSERT(frent != NULL);
459 1.1 itojun if ((frent->fr_ip->ip_hl << 2) + off > IP_MAXPACKET) {
460 1.1 itojun DPFPRINTF(("drop: too big: %d\n", off));
461 1.1 itojun pf_free_fragment(*frag);
462 1.1 itojun *frag = NULL;
463 1.1 itojun return (NULL);
464 1.1 itojun }
465 1.1 itojun next = LIST_NEXT(frent, fr_next);
466 1.1 itojun
467 1.1 itojun /* Magic from ip_input */
468 1.1 itojun ip = frent->fr_ip;
469 1.1 itojun m = frent->fr_m;
470 1.1 itojun m2 = m->m_next;
471 1.1 itojun m->m_next = NULL;
472 1.1 itojun m_cat(m, m2);
473 1.1 itojun pool_put(&pf_frent_pl, frent);
474 1.1 itojun pf_nfrents--;
475 1.1 itojun for (frent = next; frent != NULL; frent = next) {
476 1.1 itojun next = LIST_NEXT(frent, fr_next);
477 1.1 itojun
478 1.1 itojun m2 = frent->fr_m;
479 1.1 itojun pool_put(&pf_frent_pl, frent);
480 1.1 itojun pf_nfrents--;
481 1.1 itojun m_cat(m, m2);
482 1.1 itojun }
483 1.1 itojun
484 1.1 itojun ip->ip_src = (*frag)->fr_src;
485 1.1 itojun ip->ip_dst = (*frag)->fr_dst;
486 1.1 itojun
487 1.1 itojun /* Remove from fragment queue */
488 1.1 itojun pf_remove_fragment(*frag);
489 1.1 itojun *frag = NULL;
490 1.1 itojun
491 1.1 itojun hlen = ip->ip_hl << 2;
492 1.1 itojun ip->ip_len = htons(off + hlen);
493 1.1 itojun m->m_len += hlen;
494 1.1 itojun m->m_data -= hlen;
495 1.1 itojun
496 1.1 itojun /* some debugging cruft by sklower, below, will go away soon */
497 1.1 itojun /* XXX this should be done elsewhere */
498 1.1 itojun if (m->m_flags & M_PKTHDR) {
499 1.1 itojun int plen = 0;
500 1.1 itojun for (m2 = m; m2; m2 = m2->m_next)
501 1.1 itojun plen += m2->m_len;
502 1.1 itojun m->m_pkthdr.len = plen;
503 1.1 itojun }
504 1.1 itojun
505 1.1 itojun DPFPRINTF(("complete: %p(%d)\n", m, ntohs(ip->ip_len)));
506 1.1 itojun return (m);
507 1.1 itojun
508 1.1 itojun drop_fragment:
509 1.1 itojun /* Oops - fail safe - drop packet */
510 1.1 itojun pool_put(&pf_frent_pl, frent);
511 1.1 itojun pf_nfrents--;
512 1.1 itojun m_freem(m);
513 1.1 itojun return (NULL);
514 1.1 itojun }
515 1.1 itojun
516 1.1 itojun struct mbuf *
517 1.1 itojun pf_fragcache(struct mbuf **m0, struct ip *h, struct pf_fragment **frag, int mff,
518 1.1 itojun int drop, int *nomem)
519 1.1 itojun {
520 1.1 itojun struct mbuf *m = *m0;
521 1.1 itojun struct pf_frcache *frp, *fra, *cur = NULL;
522 1.1 itojun int ip_len = ntohs(h->ip_len) - (h->ip_hl << 2);
523 1.1 itojun u_int16_t off = ntohs(h->ip_off) << 3;
524 1.1 itojun u_int16_t max = ip_len + off;
525 1.1 itojun int hosed = 0;
526 1.1 itojun
527 1.1 itojun KASSERT(*frag == NULL || !BUFFER_FRAGMENTS(*frag));
528 1.1 itojun
529 1.1 itojun /* Create a new range queue for this packet */
530 1.1 itojun if (*frag == NULL) {
531 1.1 itojun *frag = pool_get(&pf_cache_pl, PR_NOWAIT);
532 1.1 itojun if (*frag == NULL) {
533 1.1 itojun pf_flush_fragments();
534 1.1 itojun *frag = pool_get(&pf_cache_pl, PR_NOWAIT);
535 1.1 itojun if (*frag == NULL)
536 1.1 itojun goto no_mem;
537 1.1 itojun }
538 1.1 itojun
539 1.1 itojun /* Get an entry for the queue */
540 1.1 itojun cur = pool_get(&pf_cent_pl, PR_NOWAIT);
541 1.1 itojun if (cur == NULL) {
542 1.1 itojun pool_put(&pf_cache_pl, *frag);
543 1.1 itojun *frag = NULL;
544 1.1 itojun goto no_mem;
545 1.1 itojun }
546 1.1 itojun pf_ncache++;
547 1.1 itojun
548 1.1 itojun (*frag)->fr_flags = PFFRAG_NOBUFFER;
549 1.1 itojun (*frag)->fr_max = 0;
550 1.1 itojun (*frag)->fr_src = h->ip_src;
551 1.1 itojun (*frag)->fr_dst = h->ip_dst;
552 1.1 itojun (*frag)->fr_p = h->ip_p;
553 1.1 itojun (*frag)->fr_id = h->ip_id;
554 1.1 itojun (*frag)->fr_timeout = time.tv_sec;
555 1.1 itojun
556 1.1 itojun cur->fr_off = off;
557 1.1 itojun cur->fr_end = max;
558 1.1 itojun LIST_INIT(&(*frag)->fr_cache);
559 1.1 itojun LIST_INSERT_HEAD(&(*frag)->fr_cache, cur, fr_next);
560 1.1 itojun
561 1.1 itojun RB_INSERT(pf_frag_tree, &pf_cache_tree, *frag);
562 1.1 itojun TAILQ_INSERT_HEAD(&pf_cachequeue, *frag, frag_next);
563 1.1 itojun
564 1.1 itojun DPFPRINTF(("fragcache[%d]: new %d-%d\n", h->ip_id, off, max));
565 1.1 itojun
566 1.1 itojun goto pass;
567 1.1 itojun }
568 1.1 itojun
569 1.1 itojun /*
570 1.1 itojun * Find a fragment after the current one:
571 1.1 itojun * - off contains the real shifted offset.
572 1.1 itojun */
573 1.1 itojun frp = NULL;
574 1.1 itojun LIST_FOREACH(fra, &(*frag)->fr_cache, fr_next) {
575 1.1 itojun if (fra->fr_off > off)
576 1.1 itojun break;
577 1.1 itojun frp = fra;
578 1.1 itojun }
579 1.1 itojun
580 1.1 itojun KASSERT(frp != NULL || fra != NULL);
581 1.1 itojun
582 1.1 itojun if (frp != NULL) {
583 1.1 itojun int precut;
584 1.1 itojun
585 1.1 itojun precut = frp->fr_end - off;
586 1.1 itojun if (precut >= ip_len) {
587 1.1 itojun /* Fragment is entirely a duplicate */
588 1.1 itojun DPFPRINTF(("fragcache[%d]: dead (%d-%d) %d-%d\n",
589 1.1 itojun h->ip_id, frp->fr_off, frp->fr_end, off, max));
590 1.1 itojun goto drop_fragment;
591 1.1 itojun }
592 1.1 itojun if (precut == 0) {
593 1.1 itojun /* They are adjacent. Fixup cache entry */
594 1.1 itojun DPFPRINTF(("fragcache[%d]: adjacent (%d-%d) %d-%d\n",
595 1.1 itojun h->ip_id, frp->fr_off, frp->fr_end, off, max));
596 1.1 itojun frp->fr_end = max;
597 1.1 itojun } else if (precut > 0) {
598 1.1 itojun /* The first part of this payload overlaps with a
599 1.1 itojun * fragment that has already been passed.
600 1.1 itojun * Need to trim off the first part of the payload.
601 1.1 itojun * But to do so easily, we need to create another
602 1.1 itojun * mbuf to throw the original header into.
603 1.1 itojun */
604 1.1 itojun
605 1.1 itojun DPFPRINTF(("fragcache[%d]: chop %d (%d-%d) %d-%d\n",
606 1.1 itojun h->ip_id, precut, frp->fr_off, frp->fr_end, off,
607 1.1 itojun max));
608 1.1 itojun
609 1.1 itojun off += precut;
610 1.1 itojun max -= precut;
611 1.1 itojun /* Update the previous frag to encompass this one */
612 1.1 itojun frp->fr_end = max;
613 1.1 itojun
614 1.1 itojun if (!drop) {
615 1.1 itojun /* XXX Optimization opportunity
616 1.1 itojun * This is a very heavy way to trim the payload.
617 1.1 itojun * we could do it much faster by diddling mbuf
618 1.1 itojun * internals but that would be even less legible
619 1.1 itojun * than this mbuf magic. For my next trick,
620 1.1 itojun * I'll pull a rabbit out of my laptop.
621 1.1 itojun */
622 1.1 itojun *m0 = m_copym2(m, 0, h->ip_hl << 2, M_NOWAIT);
623 1.1 itojun if (*m0 == NULL)
624 1.1 itojun goto no_mem;
625 1.1 itojun KASSERT((*m0)->m_next == NULL);
626 1.1 itojun m_adj(m, precut + (h->ip_hl << 2));
627 1.1 itojun m_cat(*m0, m);
628 1.1 itojun m = *m0;
629 1.1 itojun if (m->m_flags & M_PKTHDR) {
630 1.1 itojun int plen = 0;
631 1.1 itojun struct mbuf *t;
632 1.1 itojun for (t = m; t; t = t->m_next)
633 1.1 itojun plen += t->m_len;
634 1.1 itojun m->m_pkthdr.len = plen;
635 1.1 itojun }
636 1.1 itojun
637 1.1 itojun
638 1.1 itojun h = mtod(m, struct ip *);
639 1.1 itojun
640 1.1 itojun
641 1.1 itojun KASSERT((int)m->m_len ==
642 1.1 itojun ntohs(h->ip_len) - precut);
643 1.1 itojun h->ip_off = htons(ntohs(h->ip_off) +
644 1.1 itojun (precut >> 3));
645 1.1 itojun h->ip_len = htons(ntohs(h->ip_len) - precut);
646 1.1 itojun } else {
647 1.1 itojun hosed++;
648 1.1 itojun }
649 1.1 itojun } else {
650 1.1 itojun /* There is a gap between fragments */
651 1.1 itojun
652 1.1 itojun DPFPRINTF(("fragcache[%d]: gap %d (%d-%d) %d-%d\n",
653 1.1 itojun h->ip_id, -precut, frp->fr_off, frp->fr_end, off,
654 1.1 itojun max));
655 1.1 itojun
656 1.1 itojun cur = pool_get(&pf_cent_pl, PR_NOWAIT);
657 1.1 itojun if (cur == NULL)
658 1.1 itojun goto no_mem;
659 1.1 itojun pf_ncache++;
660 1.1 itojun
661 1.1 itojun cur->fr_off = off;
662 1.1 itojun cur->fr_end = max;
663 1.1 itojun LIST_INSERT_AFTER(frp, cur, fr_next);
664 1.1 itojun }
665 1.1 itojun }
666 1.1 itojun
667 1.1 itojun if (fra != NULL) {
668 1.1 itojun int aftercut;
669 1.1 itojun int merge = 0;
670 1.1 itojun
671 1.1 itojun aftercut = max - fra->fr_off;
672 1.1 itojun if (aftercut == 0) {
673 1.1 itojun /* Adjacent fragments */
674 1.1 itojun DPFPRINTF(("fragcache[%d]: adjacent %d-%d (%d-%d)\n",
675 1.1 itojun h->ip_id, off, max, fra->fr_off, fra->fr_end));
676 1.1 itojun fra->fr_off = off;
677 1.1 itojun merge = 1;
678 1.1 itojun } else if (aftercut > 0) {
679 1.1 itojun /* Need to chop off the tail of this fragment */
680 1.1 itojun DPFPRINTF(("fragcache[%d]: chop %d %d-%d (%d-%d)\n",
681 1.1 itojun h->ip_id, aftercut, off, max, fra->fr_off,
682 1.1 itojun fra->fr_end));
683 1.1 itojun fra->fr_off = off;
684 1.1 itojun max -= aftercut;
685 1.1 itojun
686 1.1 itojun merge = 1;
687 1.1 itojun
688 1.1 itojun if (!drop) {
689 1.1 itojun m_adj(m, -aftercut);
690 1.1 itojun if (m->m_flags & M_PKTHDR) {
691 1.1 itojun int plen = 0;
692 1.1 itojun struct mbuf *t;
693 1.1 itojun for (t = m; t; t = t->m_next)
694 1.1 itojun plen += t->m_len;
695 1.1 itojun m->m_pkthdr.len = plen;
696 1.1 itojun }
697 1.1 itojun h = mtod(m, struct ip *);
698 1.1 itojun KASSERT((int)m->m_len ==
699 1.1 itojun ntohs(h->ip_len) - aftercut);
700 1.1 itojun h->ip_len = htons(ntohs(h->ip_len) - aftercut);
701 1.1 itojun } else {
702 1.1 itojun hosed++;
703 1.1 itojun }
704 1.1 itojun } else {
705 1.1 itojun /* There is a gap between fragments */
706 1.1 itojun DPFPRINTF(("fragcache[%d]: gap %d %d-%d (%d-%d)\n",
707 1.1 itojun h->ip_id, -aftercut, off, max, fra->fr_off,
708 1.1 itojun fra->fr_end));
709 1.1 itojun
710 1.1 itojun cur = pool_get(&pf_cent_pl, PR_NOWAIT);
711 1.1 itojun if (cur == NULL)
712 1.1 itojun goto no_mem;
713 1.1 itojun pf_ncache++;
714 1.1 itojun
715 1.1 itojun cur->fr_off = off;
716 1.1 itojun cur->fr_end = max;
717 1.1 itojun LIST_INSERT_BEFORE(fra, cur, fr_next);
718 1.1 itojun }
719 1.1 itojun
720 1.1 itojun
721 1.1 itojun /* Need to glue together two separate fragment descriptors */
722 1.1 itojun if (merge) {
723 1.1 itojun if (cur && fra->fr_off <= cur->fr_end) {
724 1.1 itojun /* Need to merge in a previous 'cur' */
725 1.1 itojun DPFPRINTF(("fragcache[%d]: adjacent(merge "
726 1.1 itojun "%d-%d) %d-%d (%d-%d)\n",
727 1.1 itojun h->ip_id, cur->fr_off, cur->fr_end, off,
728 1.1 itojun max, fra->fr_off, fra->fr_end));
729 1.1 itojun fra->fr_off = cur->fr_off;
730 1.1 itojun LIST_REMOVE(cur, fr_next);
731 1.1 itojun pool_put(&pf_cent_pl, cur);
732 1.1 itojun pf_ncache--;
733 1.1 itojun cur = NULL;
734 1.1 itojun
735 1.1 itojun } else if (frp && fra->fr_off <= frp->fr_end) {
736 1.1 itojun /* Need to merge in a modified 'frp' */
737 1.1 itojun KASSERT(cur == NULL);
738 1.1 itojun DPFPRINTF(("fragcache[%d]: adjacent(merge "
739 1.1 itojun "%d-%d) %d-%d (%d-%d)\n",
740 1.1 itojun h->ip_id, frp->fr_off, frp->fr_end, off,
741 1.1 itojun max, fra->fr_off, fra->fr_end));
742 1.1 itojun fra->fr_off = frp->fr_off;
743 1.1 itojun LIST_REMOVE(frp, fr_next);
744 1.1 itojun pool_put(&pf_cent_pl, frp);
745 1.1 itojun pf_ncache--;
746 1.1 itojun frp = NULL;
747 1.1 itojun
748 1.1 itojun }
749 1.1 itojun }
750 1.1 itojun }
751 1.1 itojun
752 1.1 itojun if (hosed) {
753 1.1 itojun /*
754 1.1 itojun * We must keep tracking the overall fragment even when
755 1.1 itojun * we're going to drop it anyway so that we know when to
756 1.1 itojun * free the overall descriptor. Thus we drop the frag late.
757 1.1 itojun */
758 1.1 itojun goto drop_fragment;
759 1.1 itojun }
760 1.1 itojun
761 1.1 itojun
762 1.1 itojun pass:
763 1.1 itojun /* Update maximum data size */
764 1.1 itojun if ((*frag)->fr_max < max)
765 1.1 itojun (*frag)->fr_max = max;
766 1.1 itojun
767 1.1 itojun /* This is the last segment */
768 1.1 itojun if (!mff)
769 1.1 itojun (*frag)->fr_flags |= PFFRAG_SEENLAST;
770 1.1 itojun
771 1.1 itojun /* Check if we are completely reassembled */
772 1.1 itojun if (((*frag)->fr_flags & PFFRAG_SEENLAST) &&
773 1.1 itojun LIST_FIRST(&(*frag)->fr_cache)->fr_off == 0 &&
774 1.1 itojun LIST_FIRST(&(*frag)->fr_cache)->fr_end == (*frag)->fr_max) {
775 1.1 itojun /* Remove from fragment queue */
776 1.1 itojun DPFPRINTF(("fragcache[%d]: done 0-%d\n", h->ip_id,
777 1.1 itojun (*frag)->fr_max));
778 1.1 itojun pf_free_fragment(*frag);
779 1.1 itojun *frag = NULL;
780 1.1 itojun }
781 1.1 itojun
782 1.1 itojun return (m);
783 1.1 itojun
784 1.1 itojun no_mem:
785 1.1 itojun *nomem = 1;
786 1.1 itojun
787 1.1 itojun /* Still need to pay attention to !IP_MF */
788 1.1 itojun if (!mff && *frag != NULL)
789 1.1 itojun (*frag)->fr_flags |= PFFRAG_SEENLAST;
790 1.1 itojun
791 1.1 itojun m_freem(m);
792 1.1 itojun return (NULL);
793 1.1 itojun
794 1.1 itojun drop_fragment:
795 1.1 itojun
796 1.1 itojun /* Still need to pay attention to !IP_MF */
797 1.1 itojun if (!mff && *frag != NULL)
798 1.1 itojun (*frag)->fr_flags |= PFFRAG_SEENLAST;
799 1.1 itojun
800 1.1 itojun if (drop) {
801 1.1 itojun /* This fragment has been deemed bad. Don't reass */
802 1.1 itojun if (((*frag)->fr_flags & PFFRAG_DROP) == 0)
803 1.1 itojun DPFPRINTF(("fragcache[%d]: dropping overall fragment\n",
804 1.1 itojun h->ip_id));
805 1.1 itojun (*frag)->fr_flags |= PFFRAG_DROP;
806 1.1 itojun }
807 1.1 itojun
808 1.1 itojun m_freem(m);
809 1.1 itojun return (NULL);
810 1.1 itojun }
811 1.1 itojun
812 1.1 itojun int
813 1.1 itojun pf_normalize_ip(struct mbuf **m0, int dir, struct pfi_kif *kif, u_short *reason)
814 1.1 itojun {
815 1.1 itojun struct mbuf *m = *m0;
816 1.1 itojun struct pf_rule *r;
817 1.1 itojun struct pf_frent *frent;
818 1.1 itojun struct pf_fragment *frag = NULL;
819 1.1 itojun struct ip *h = mtod(m, struct ip *);
820 1.1 itojun int mff = (ntohs(h->ip_off) & IP_MF);
821 1.1 itojun int hlen = h->ip_hl << 2;
822 1.1 itojun u_int16_t fragoff = (ntohs(h->ip_off) & IP_OFFMASK) << 3;
823 1.1 itojun u_int16_t max;
824 1.1 itojun int ip_len;
825 1.1 itojun int ip_off;
826 1.1 itojun
827 1.1 itojun r = TAILQ_FIRST(pf_main_ruleset.rules[PF_RULESET_SCRUB].active.ptr);
828 1.1 itojun while (r != NULL) {
829 1.1 itojun r->evaluations++;
830 1.1 itojun if (r->kif != NULL &&
831 1.1 itojun (r->kif != kif && r->kif != kif->pfik_parent) == !r->ifnot)
832 1.1 itojun r = r->skip[PF_SKIP_IFP].ptr;
833 1.1 itojun else if (r->direction && r->direction != dir)
834 1.1 itojun r = r->skip[PF_SKIP_DIR].ptr;
835 1.1 itojun else if (r->af && r->af != AF_INET)
836 1.1 itojun r = r->skip[PF_SKIP_AF].ptr;
837 1.1 itojun else if (r->proto && r->proto != h->ip_p)
838 1.1 itojun r = r->skip[PF_SKIP_PROTO].ptr;
839 1.1 itojun else if (PF_MISMATCHAW(&r->src.addr,
840 1.1 itojun (struct pf_addr *)&h->ip_src.s_addr, AF_INET, r->src.not))
841 1.1 itojun r = r->skip[PF_SKIP_SRC_ADDR].ptr;
842 1.1 itojun else if (PF_MISMATCHAW(&r->dst.addr,
843 1.1 itojun (struct pf_addr *)&h->ip_dst.s_addr, AF_INET, r->dst.not))
844 1.1 itojun r = r->skip[PF_SKIP_DST_ADDR].ptr;
845 1.1 itojun else
846 1.1 itojun break;
847 1.1 itojun }
848 1.1 itojun
849 1.1 itojun if (r == NULL)
850 1.1 itojun return (PF_PASS);
851 1.1 itojun else
852 1.1 itojun r->packets++;
853 1.1 itojun
854 1.1 itojun /* Check for illegal packets */
855 1.1 itojun if (hlen < (int)sizeof(struct ip))
856 1.1 itojun goto drop;
857 1.1 itojun
858 1.1 itojun if (hlen > ntohs(h->ip_len))
859 1.1 itojun goto drop;
860 1.1 itojun
861 1.1 itojun /* Clear IP_DF if the rule uses the no-df option */
862 1.1 itojun if (r->rule_flag & PFRULE_NODF)
863 1.1 itojun h->ip_off &= htons(~IP_DF);
864 1.1 itojun
865 1.1 itojun /* We will need other tests here */
866 1.1 itojun if (!fragoff && !mff)
867 1.1 itojun goto no_fragment;
868 1.1 itojun
869 1.1 itojun /* We're dealing with a fragment now. Don't allow fragments
870 1.1 itojun * with IP_DF to enter the cache. If the flag was cleared by
871 1.1 itojun * no-df above, fine. Otherwise drop it.
872 1.1 itojun */
873 1.1 itojun if (h->ip_off & htons(IP_DF)) {
874 1.1 itojun DPFPRINTF(("IP_DF\n"));
875 1.1 itojun goto bad;
876 1.1 itojun }
877 1.1 itojun
878 1.1 itojun ip_len = ntohs(h->ip_len) - hlen;
879 1.1 itojun ip_off = (ntohs(h->ip_off) & IP_OFFMASK) << 3;
880 1.1 itojun
881 1.1 itojun /* All fragments are 8 byte aligned */
882 1.1 itojun if (mff && (ip_len & 0x7)) {
883 1.1 itojun DPFPRINTF(("mff and %d\n", ip_len));
884 1.1 itojun goto bad;
885 1.1 itojun }
886 1.1 itojun
887 1.1 itojun /* Respect maximum length */
888 1.1 itojun if (fragoff + ip_len > IP_MAXPACKET) {
889 1.1 itojun DPFPRINTF(("max packet %d\n", fragoff + ip_len));
890 1.1 itojun goto bad;
891 1.1 itojun }
892 1.1 itojun max = fragoff + ip_len;
893 1.1 itojun
894 1.1 itojun if ((r->rule_flag & (PFRULE_FRAGCROP|PFRULE_FRAGDROP)) == 0) {
895 1.1 itojun /* Fully buffer all of the fragments */
896 1.1 itojun
897 1.1 itojun frag = pf_find_fragment(h, &pf_frag_tree);
898 1.1 itojun
899 1.1 itojun /* Check if we saw the last fragment already */
900 1.1 itojun if (frag != NULL && (frag->fr_flags & PFFRAG_SEENLAST) &&
901 1.1 itojun max > frag->fr_max)
902 1.1 itojun goto bad;
903 1.1 itojun
904 1.1 itojun /* Get an entry for the fragment queue */
905 1.1 itojun frent = pool_get(&pf_frent_pl, PR_NOWAIT);
906 1.1 itojun if (frent == NULL) {
907 1.1 itojun REASON_SET(reason, PFRES_MEMORY);
908 1.1 itojun return (PF_DROP);
909 1.1 itojun }
910 1.1 itojun pf_nfrents++;
911 1.1 itojun frent->fr_ip = h;
912 1.1 itojun frent->fr_m = m;
913 1.1 itojun
914 1.1 itojun /* Might return a completely reassembled mbuf, or NULL */
915 1.1 itojun DPFPRINTF(("reass frag %d @ %d-%d\n", h->ip_id, fragoff, max));
916 1.1 itojun *m0 = m = pf_reassemble(m0, &frag, frent, mff);
917 1.1 itojun
918 1.1 itojun if (m == NULL)
919 1.1 itojun return (PF_DROP);
920 1.1 itojun
921 1.1 itojun if (frag != NULL && (frag->fr_flags & PFFRAG_DROP))
922 1.1 itojun goto drop;
923 1.1 itojun
924 1.1 itojun h = mtod(m, struct ip *);
925 1.1 itojun } else {
926 1.1 itojun /* non-buffering fragment cache (drops or masks overlaps) */
927 1.1 itojun int nomem = 0;
928 1.1 itojun
929 1.1 itojun if (dir == PF_OUT) {
930 1.1 itojun if (m_tag_find(m, PACKET_TAG_PF_FRAGCACHE, NULL) !=
931 1.1 itojun NULL) {
932 1.1 itojun /* Already passed the fragment cache in the
933 1.1 itojun * input direction. If we continued, it would
934 1.1 itojun * appear to be a dup and would be dropped.
935 1.1 itojun */
936 1.1 itojun goto fragment_pass;
937 1.1 itojun }
938 1.1 itojun }
939 1.1 itojun
940 1.1 itojun frag = pf_find_fragment(h, &pf_cache_tree);
941 1.1 itojun
942 1.1 itojun /* Check if we saw the last fragment already */
943 1.1 itojun if (frag != NULL && (frag->fr_flags & PFFRAG_SEENLAST) &&
944 1.1 itojun max > frag->fr_max) {
945 1.1 itojun if (r->rule_flag & PFRULE_FRAGDROP)
946 1.1 itojun frag->fr_flags |= PFFRAG_DROP;
947 1.1 itojun goto bad;
948 1.1 itojun }
949 1.1 itojun
950 1.1 itojun *m0 = m = pf_fragcache(m0, h, &frag, mff,
951 1.1 itojun (r->rule_flag & PFRULE_FRAGDROP) ? 1 : 0, &nomem);
952 1.1 itojun if (m == NULL) {
953 1.1 itojun if (nomem)
954 1.1 itojun goto no_mem;
955 1.1 itojun goto drop;
956 1.1 itojun }
957 1.1 itojun
958 1.1 itojun if (dir == PF_IN) {
959 1.1 itojun struct m_tag *mtag;
960 1.1 itojun
961 1.1 itojun mtag = m_tag_get(PACKET_TAG_PF_FRAGCACHE, 0, M_NOWAIT);
962 1.1 itojun if (mtag == NULL)
963 1.1 itojun goto no_mem;
964 1.1 itojun m_tag_prepend(m, mtag);
965 1.1 itojun }
966 1.1 itojun if (frag != NULL && (frag->fr_flags & PFFRAG_DROP))
967 1.1 itojun goto drop;
968 1.1 itojun goto fragment_pass;
969 1.1 itojun }
970 1.1 itojun
971 1.1 itojun no_fragment:
972 1.1 itojun /* At this point, only IP_DF is allowed in ip_off */
973 1.1 itojun h->ip_off &= htons(IP_DF);
974 1.1 itojun
975 1.1 itojun /* Enforce a minimum ttl, may cause endless packet loops */
976 1.1 itojun if (r->min_ttl && h->ip_ttl < r->min_ttl)
977 1.1 itojun h->ip_ttl = r->min_ttl;
978 1.1 itojun
979 1.1 itojun if (r->rule_flag & PFRULE_RANDOMID)
980 1.1 itojun h->ip_id = ip_randomid();
981 1.1 itojun
982 1.1 itojun return (PF_PASS);
983 1.1 itojun
984 1.1 itojun fragment_pass:
985 1.1 itojun /* Enforce a minimum ttl, may cause endless packet loops */
986 1.1 itojun if (r->min_ttl && h->ip_ttl < r->min_ttl)
987 1.1 itojun h->ip_ttl = r->min_ttl;
988 1.1 itojun
989 1.1 itojun return (PF_PASS);
990 1.1 itojun
991 1.1 itojun no_mem:
992 1.1 itojun REASON_SET(reason, PFRES_MEMORY);
993 1.1 itojun if (r != NULL && r->log)
994 1.1 itojun PFLOG_PACKET(kif, h, m, AF_INET, dir, *reason, r, NULL, NULL);
995 1.1 itojun return (PF_DROP);
996 1.1 itojun
997 1.1 itojun drop:
998 1.1 itojun REASON_SET(reason, PFRES_NORM);
999 1.1 itojun if (r != NULL && r->log)
1000 1.1 itojun PFLOG_PACKET(kif, h, m, AF_INET, dir, *reason, r, NULL, NULL);
1001 1.1 itojun return (PF_DROP);
1002 1.1 itojun
1003 1.1 itojun bad:
1004 1.1 itojun DPFPRINTF(("dropping bad fragment\n"));
1005 1.1 itojun
1006 1.1 itojun /* Free associated fragments */
1007 1.1 itojun if (frag != NULL)
1008 1.1 itojun pf_free_fragment(frag);
1009 1.1 itojun
1010 1.1 itojun REASON_SET(reason, PFRES_FRAG);
1011 1.1 itojun if (r != NULL && r->log)
1012 1.1 itojun PFLOG_PACKET(kif, h, m, AF_INET, dir, *reason, r, NULL, NULL);
1013 1.1 itojun
1014 1.1 itojun return (PF_DROP);
1015 1.1 itojun }
1016 1.1 itojun
1017 1.1 itojun #ifdef INET6
1018 1.1 itojun int
1019 1.1 itojun pf_normalize_ip6(struct mbuf **m0, int dir, struct pfi_kif *kif,
1020 1.1 itojun u_short *reason)
1021 1.1 itojun {
1022 1.1 itojun struct mbuf *m = *m0;
1023 1.1 itojun struct pf_rule *r;
1024 1.1 itojun struct ip6_hdr *h = mtod(m, struct ip6_hdr *);
1025 1.1 itojun int off;
1026 1.1 itojun struct ip6_ext ext;
1027 1.1 itojun struct ip6_opt opt;
1028 1.1 itojun struct ip6_opt_jumbo jumbo;
1029 1.1 itojun struct ip6_frag frag;
1030 1.1 itojun u_int32_t jumbolen = 0, plen;
1031 1.1 itojun u_int16_t fragoff = 0;
1032 1.1 itojun int optend;
1033 1.1 itojun int ooff;
1034 1.1 itojun u_int8_t proto;
1035 1.1 itojun int terminal;
1036 1.1 itojun
1037 1.1 itojun r = TAILQ_FIRST(pf_main_ruleset.rules[PF_RULESET_SCRUB].active.ptr);
1038 1.1 itojun while (r != NULL) {
1039 1.1 itojun r->evaluations++;
1040 1.1 itojun if (r->kif != NULL &&
1041 1.1 itojun (r->kif != kif && r->kif != kif->pfik_parent) == !r->ifnot)
1042 1.1 itojun r = r->skip[PF_SKIP_IFP].ptr;
1043 1.1 itojun else if (r->direction && r->direction != dir)
1044 1.1 itojun r = r->skip[PF_SKIP_DIR].ptr;
1045 1.1 itojun else if (r->af && r->af != AF_INET6)
1046 1.1 itojun r = r->skip[PF_SKIP_AF].ptr;
1047 1.1 itojun #if 0 /* header chain! */
1048 1.1 itojun else if (r->proto && r->proto != h->ip6_nxt)
1049 1.1 itojun r = r->skip[PF_SKIP_PROTO].ptr;
1050 1.1 itojun #endif
1051 1.1 itojun else if (PF_MISMATCHAW(&r->src.addr,
1052 1.1 itojun (struct pf_addr *)&h->ip6_src, AF_INET6, r->src.not))
1053 1.1 itojun r = r->skip[PF_SKIP_SRC_ADDR].ptr;
1054 1.1 itojun else if (PF_MISMATCHAW(&r->dst.addr,
1055 1.1 itojun (struct pf_addr *)&h->ip6_dst, AF_INET6, r->dst.not))
1056 1.1 itojun r = r->skip[PF_SKIP_DST_ADDR].ptr;
1057 1.1 itojun else
1058 1.1 itojun break;
1059 1.1 itojun }
1060 1.1 itojun
1061 1.1 itojun if (r == NULL)
1062 1.1 itojun return (PF_PASS);
1063 1.1 itojun else
1064 1.1 itojun r->packets++;
1065 1.1 itojun
1066 1.1 itojun /* Check for illegal packets */
1067 1.1 itojun if (sizeof(struct ip6_hdr) + IPV6_MAXPACKET < m->m_pkthdr.len)
1068 1.1 itojun goto drop;
1069 1.1 itojun
1070 1.1 itojun off = sizeof(struct ip6_hdr);
1071 1.1 itojun proto = h->ip6_nxt;
1072 1.1 itojun terminal = 0;
1073 1.1 itojun do {
1074 1.1 itojun switch (proto) {
1075 1.1 itojun case IPPROTO_FRAGMENT:
1076 1.1 itojun goto fragment;
1077 1.1 itojun break;
1078 1.1 itojun case IPPROTO_AH:
1079 1.1 itojun case IPPROTO_ROUTING:
1080 1.1 itojun case IPPROTO_DSTOPTS:
1081 1.1 itojun if (!pf_pull_hdr(m, off, &ext, sizeof(ext), NULL,
1082 1.1 itojun NULL, AF_INET6))
1083 1.1 itojun goto shortpkt;
1084 1.1 itojun if (proto == IPPROTO_AH)
1085 1.1 itojun off += (ext.ip6e_len + 2) * 4;
1086 1.1 itojun else
1087 1.1 itojun off += (ext.ip6e_len + 1) * 8;
1088 1.1 itojun proto = ext.ip6e_nxt;
1089 1.1 itojun break;
1090 1.1 itojun case IPPROTO_HOPOPTS:
1091 1.1 itojun if (!pf_pull_hdr(m, off, &ext, sizeof(ext), NULL,
1092 1.1 itojun NULL, AF_INET6))
1093 1.1 itojun goto shortpkt;
1094 1.1 itojun optend = off + (ext.ip6e_len + 1) * 8;
1095 1.1 itojun ooff = off + sizeof(ext);
1096 1.1 itojun do {
1097 1.1 itojun if (!pf_pull_hdr(m, ooff, &opt.ip6o_type,
1098 1.1 itojun sizeof(opt.ip6o_type), NULL, NULL,
1099 1.1 itojun AF_INET6))
1100 1.1 itojun goto shortpkt;
1101 1.1 itojun if (opt.ip6o_type == IP6OPT_PAD1) {
1102 1.1 itojun ooff++;
1103 1.1 itojun continue;
1104 1.1 itojun }
1105 1.1 itojun if (!pf_pull_hdr(m, ooff, &opt, sizeof(opt),
1106 1.1 itojun NULL, NULL, AF_INET6))
1107 1.1 itojun goto shortpkt;
1108 1.1 itojun if (ooff + sizeof(opt) + opt.ip6o_len > optend)
1109 1.1 itojun goto drop;
1110 1.1 itojun switch (opt.ip6o_type) {
1111 1.1 itojun case IP6OPT_JUMBO:
1112 1.1 itojun if (h->ip6_plen != 0)
1113 1.1 itojun goto drop;
1114 1.1 itojun if (!pf_pull_hdr(m, ooff, &jumbo,
1115 1.1 itojun sizeof(jumbo), NULL, NULL,
1116 1.1 itojun AF_INET6))
1117 1.1 itojun goto shortpkt;
1118 1.1 itojun memcpy(&jumbolen, jumbo.ip6oj_jumbo_len,
1119 1.1 itojun sizeof(jumbolen));
1120 1.1 itojun jumbolen = ntohl(jumbolen);
1121 1.1 itojun if (jumbolen <= IPV6_MAXPACKET)
1122 1.1 itojun goto drop;
1123 1.1 itojun if (sizeof(struct ip6_hdr) + jumbolen !=
1124 1.1 itojun m->m_pkthdr.len)
1125 1.1 itojun goto drop;
1126 1.1 itojun break;
1127 1.1 itojun default:
1128 1.1 itojun break;
1129 1.1 itojun }
1130 1.1 itojun ooff += sizeof(opt) + opt.ip6o_len;
1131 1.1 itojun } while (ooff < optend);
1132 1.1 itojun
1133 1.1 itojun off = optend;
1134 1.1 itojun proto = ext.ip6e_nxt;
1135 1.1 itojun break;
1136 1.1 itojun default:
1137 1.1 itojun terminal = 1;
1138 1.1 itojun break;
1139 1.1 itojun }
1140 1.1 itojun } while (!terminal);
1141 1.1 itojun
1142 1.1 itojun /* jumbo payload option must be present, or plen > 0 */
1143 1.1 itojun if (ntohs(h->ip6_plen) == 0)
1144 1.1 itojun plen = jumbolen;
1145 1.1 itojun else
1146 1.1 itojun plen = ntohs(h->ip6_plen);
1147 1.1 itojun if (plen == 0)
1148 1.1 itojun goto drop;
1149 1.1 itojun if (sizeof(struct ip6_hdr) + plen > m->m_pkthdr.len)
1150 1.1 itojun goto shortpkt;
1151 1.1 itojun
1152 1.1 itojun /* Enforce a minimum ttl, may cause endless packet loops */
1153 1.1 itojun if (r->min_ttl && h->ip6_hlim < r->min_ttl)
1154 1.1 itojun h->ip6_hlim = r->min_ttl;
1155 1.1 itojun
1156 1.1 itojun return (PF_PASS);
1157 1.1 itojun
1158 1.1 itojun fragment:
1159 1.1 itojun if (ntohs(h->ip6_plen) == 0 || jumbolen)
1160 1.1 itojun goto drop;
1161 1.1 itojun plen = ntohs(h->ip6_plen);
1162 1.1 itojun
1163 1.1 itojun if (!pf_pull_hdr(m, off, &frag, sizeof(frag), NULL, NULL, AF_INET6))
1164 1.1 itojun goto shortpkt;
1165 1.1 itojun fragoff = ntohs(frag.ip6f_offlg & IP6F_OFF_MASK);
1166 1.1 itojun if (fragoff + (plen - off - sizeof(frag)) > IPV6_MAXPACKET)
1167 1.1 itojun goto badfrag;
1168 1.1 itojun
1169 1.1 itojun /* do something about it */
1170 1.1 itojun return (PF_PASS);
1171 1.1 itojun
1172 1.1 itojun shortpkt:
1173 1.1 itojun REASON_SET(reason, PFRES_SHORT);
1174 1.1 itojun if (r != NULL && r->log)
1175 1.1 itojun PFLOG_PACKET(kif, h, m, AF_INET6, dir, *reason, r, NULL, NULL);
1176 1.1 itojun return (PF_DROP);
1177 1.1 itojun
1178 1.1 itojun drop:
1179 1.1 itojun REASON_SET(reason, PFRES_NORM);
1180 1.1 itojun if (r != NULL && r->log)
1181 1.1 itojun PFLOG_PACKET(kif, h, m, AF_INET6, dir, *reason, r, NULL, NULL);
1182 1.1 itojun return (PF_DROP);
1183 1.1 itojun
1184 1.1 itojun badfrag:
1185 1.1 itojun REASON_SET(reason, PFRES_FRAG);
1186 1.1 itojun if (r != NULL && r->log)
1187 1.1 itojun PFLOG_PACKET(kif, h, m, AF_INET6, dir, *reason, r, NULL, NULL);
1188 1.1 itojun return (PF_DROP);
1189 1.1 itojun }
1190 1.1 itojun #endif
1191 1.1 itojun
1192 1.1 itojun int
1193 1.1 itojun pf_normalize_tcp(int dir, struct pfi_kif *kif, struct mbuf *m, int ipoff,
1194 1.1 itojun int off, void *h, struct pf_pdesc *pd)
1195 1.1 itojun {
1196 1.1 itojun struct pf_rule *r, *rm = NULL;
1197 1.1 itojun struct tcphdr *th = pd->hdr.tcp;
1198 1.1 itojun int rewrite = 0;
1199 1.1 itojun u_short reason;
1200 1.1 itojun u_int8_t flags;
1201 1.1 itojun sa_family_t af = pd->af;
1202 1.1 itojun
1203 1.1 itojun r = TAILQ_FIRST(pf_main_ruleset.rules[PF_RULESET_SCRUB].active.ptr);
1204 1.1 itojun while (r != NULL) {
1205 1.1 itojun r->evaluations++;
1206 1.1 itojun if (r->kif != NULL &&
1207 1.1 itojun (r->kif != kif && r->kif != kif->pfik_parent) == !r->ifnot)
1208 1.1 itojun r = r->skip[PF_SKIP_IFP].ptr;
1209 1.1 itojun else if (r->direction && r->direction != dir)
1210 1.1 itojun r = r->skip[PF_SKIP_DIR].ptr;
1211 1.1 itojun else if (r->af && r->af != af)
1212 1.1 itojun r = r->skip[PF_SKIP_AF].ptr;
1213 1.1 itojun else if (r->proto && r->proto != pd->proto)
1214 1.1 itojun r = r->skip[PF_SKIP_PROTO].ptr;
1215 1.1 itojun else if (PF_MISMATCHAW(&r->src.addr, pd->src, af, r->src.not))
1216 1.1 itojun r = r->skip[PF_SKIP_SRC_ADDR].ptr;
1217 1.1 itojun else if (r->src.port_op && !pf_match_port(r->src.port_op,
1218 1.1 itojun r->src.port[0], r->src.port[1], th->th_sport))
1219 1.1 itojun r = r->skip[PF_SKIP_SRC_PORT].ptr;
1220 1.1 itojun else if (PF_MISMATCHAW(&r->dst.addr, pd->dst, af, r->dst.not))
1221 1.1 itojun r = r->skip[PF_SKIP_DST_ADDR].ptr;
1222 1.1 itojun else if (r->dst.port_op && !pf_match_port(r->dst.port_op,
1223 1.1 itojun r->dst.port[0], r->dst.port[1], th->th_dport))
1224 1.1 itojun r = r->skip[PF_SKIP_DST_PORT].ptr;
1225 1.1 itojun else if (r->os_fingerprint != PF_OSFP_ANY && !pf_osfp_match(
1226 1.1 itojun pf_osfp_fingerprint(pd, m, off, th),
1227 1.1 itojun r->os_fingerprint))
1228 1.1 itojun r = TAILQ_NEXT(r, entries);
1229 1.1 itojun else {
1230 1.1 itojun rm = r;
1231 1.1 itojun break;
1232 1.1 itojun }
1233 1.1 itojun }
1234 1.1 itojun
1235 1.1 itojun if (rm == NULL)
1236 1.1 itojun return (PF_PASS);
1237 1.1 itojun else
1238 1.1 itojun r->packets++;
1239 1.1 itojun
1240 1.1 itojun if (rm->rule_flag & PFRULE_REASSEMBLE_TCP)
1241 1.1 itojun pd->flags |= PFDESC_TCP_NORM;
1242 1.1 itojun
1243 1.1 itojun flags = th->th_flags;
1244 1.1 itojun if (flags & TH_SYN) {
1245 1.1 itojun /* Illegal packet */
1246 1.1 itojun if (flags & TH_RST)
1247 1.1 itojun goto tcp_drop;
1248 1.1 itojun
1249 1.1 itojun if (flags & TH_FIN)
1250 1.1 itojun flags &= ~TH_FIN;
1251 1.1 itojun } else {
1252 1.1 itojun /* Illegal packet */
1253 1.1 itojun if (!(flags & (TH_ACK|TH_RST)))
1254 1.1 itojun goto tcp_drop;
1255 1.1 itojun }
1256 1.1 itojun
1257 1.1 itojun if (!(flags & TH_ACK)) {
1258 1.1 itojun /* These flags are only valid if ACK is set */
1259 1.1 itojun if ((flags & TH_FIN) || (flags & TH_PUSH) || (flags & TH_URG))
1260 1.1 itojun goto tcp_drop;
1261 1.1 itojun }
1262 1.1 itojun
1263 1.1 itojun /* Check for illegal header length */
1264 1.1 itojun if (th->th_off < (sizeof(struct tcphdr) >> 2))
1265 1.1 itojun goto tcp_drop;
1266 1.1 itojun
1267 1.1 itojun /* If flags changed, or reserved data set, then adjust */
1268 1.1 itojun if (flags != th->th_flags || th->th_x2 != 0) {
1269 1.1 itojun u_int16_t ov, nv;
1270 1.1 itojun
1271 1.1 itojun ov = *(u_int16_t *)(&th->th_ack + 1);
1272 1.1 itojun th->th_flags = flags;
1273 1.1 itojun th->th_x2 = 0;
1274 1.1 itojun nv = *(u_int16_t *)(&th->th_ack + 1);
1275 1.1 itojun
1276 1.1 itojun th->th_sum = pf_cksum_fixup(th->th_sum, ov, nv);
1277 1.1 itojun rewrite = 1;
1278 1.1 itojun }
1279 1.1 itojun
1280 1.1 itojun /* Remove urgent pointer, if TH_URG is not set */
1281 1.1 itojun if (!(flags & TH_URG) && th->th_urp) {
1282 1.1 itojun th->th_sum = pf_cksum_fixup(th->th_sum, th->th_urp, 0);
1283 1.1 itojun th->th_urp = 0;
1284 1.1 itojun rewrite = 1;
1285 1.1 itojun }
1286 1.1 itojun
1287 1.1 itojun /* Process options */
1288 1.1 itojun if (r->max_mss && pf_normalize_tcpopt(r, m, th, off))
1289 1.1 itojun rewrite = 1;
1290 1.1 itojun
1291 1.1 itojun /* copy back packet headers if we sanitized */
1292 1.1 itojun if (rewrite)
1293 1.1 itojun m_copyback(m, off, sizeof(*th), th);
1294 1.1 itojun
1295 1.1 itojun return (PF_PASS);
1296 1.1 itojun
1297 1.1 itojun tcp_drop:
1298 1.1 itojun REASON_SET(&reason, PFRES_NORM);
1299 1.1 itojun if (rm != NULL && r->log)
1300 1.1 itojun PFLOG_PACKET(kif, h, m, AF_INET, dir, reason, r, NULL, NULL);
1301 1.1 itojun return (PF_DROP);
1302 1.1 itojun }
1303 1.1 itojun
1304 1.1 itojun int
1305 1.1 itojun pf_normalize_tcp_init(struct mbuf *m, int off, struct pf_pdesc *pd,
1306 1.1 itojun struct tcphdr *th, struct pf_state_peer *src, struct pf_state_peer *dst)
1307 1.1 itojun {
1308 1.1 itojun u_int8_t hdr[60];
1309 1.1 itojun u_int8_t *opt;
1310 1.1 itojun
1311 1.1 itojun KASSERT(src->scrub == NULL);
1312 1.1 itojun
1313 1.1 itojun src->scrub = pool_get(&pf_state_scrub_pl, PR_NOWAIT);
1314 1.1 itojun if (src->scrub == NULL)
1315 1.1 itojun return (1);
1316 1.1 itojun bzero(src->scrub, sizeof(*src->scrub));
1317 1.1 itojun
1318 1.1 itojun switch (pd->af) {
1319 1.1 itojun #ifdef INET
1320 1.1 itojun case AF_INET: {
1321 1.1 itojun struct ip *h = mtod(m, struct ip *);
1322 1.1 itojun src->scrub->pfss_ttl = h->ip_ttl;
1323 1.1 itojun break;
1324 1.1 itojun }
1325 1.1 itojun #endif /* INET */
1326 1.1 itojun #ifdef INET6
1327 1.1 itojun case AF_INET6: {
1328 1.1 itojun struct ip6_hdr *h = mtod(m, struct ip6_hdr *);
1329 1.1 itojun src->scrub->pfss_ttl = h->ip6_hlim;
1330 1.1 itojun break;
1331 1.1 itojun }
1332 1.1 itojun #endif /* INET6 */
1333 1.1 itojun }
1334 1.1 itojun
1335 1.1 itojun
1336 1.1 itojun /*
1337 1.1 itojun * All normalizations below are only begun if we see the start of
1338 1.1 itojun * the connections. They must all set an enabled bit in pfss_flags
1339 1.1 itojun */
1340 1.1 itojun if ((th->th_flags & TH_SYN) == 0)
1341 1.1 itojun return (0);
1342 1.1 itojun
1343 1.1 itojun
1344 1.1 itojun if (th->th_off > (sizeof(struct tcphdr) >> 2) && src->scrub &&
1345 1.1 itojun pf_pull_hdr(m, off, hdr, th->th_off << 2, NULL, NULL, pd->af)) {
1346 1.1 itojun /* Diddle with TCP options */
1347 1.1 itojun int hlen;
1348 1.1 itojun opt = hdr + sizeof(struct tcphdr);
1349 1.1 itojun hlen = (th->th_off << 2) - sizeof(struct tcphdr);
1350 1.1 itojun while (hlen >= TCPOLEN_TIMESTAMP) {
1351 1.1 itojun switch (*opt) {
1352 1.1 itojun case TCPOPT_EOL: /* FALLTHROUGH */
1353 1.1 itojun case TCPOPT_NOP:
1354 1.1 itojun opt++;
1355 1.1 itojun hlen--;
1356 1.1 itojun break;
1357 1.1 itojun case TCPOPT_TIMESTAMP:
1358 1.1 itojun if (opt[1] >= TCPOLEN_TIMESTAMP) {
1359 1.1 itojun src->scrub->pfss_flags |=
1360 1.1 itojun PFSS_TIMESTAMP;
1361 1.1 itojun src->scrub->pfss_ts_mod = arc4random();
1362 1.1 itojun }
1363 1.1 itojun /* FALLTHROUGH */
1364 1.1 itojun default:
1365 1.1 itojun hlen -= opt[1];
1366 1.1 itojun opt += opt[1];
1367 1.1 itojun break;
1368 1.1 itojun }
1369 1.1 itojun }
1370 1.1 itojun }
1371 1.1 itojun
1372 1.1 itojun return (0);
1373 1.1 itojun }
1374 1.1 itojun
1375 1.1 itojun void
1376 1.1 itojun pf_normalize_tcp_cleanup(struct pf_state *state)
1377 1.1 itojun {
1378 1.1 itojun if (state->src.scrub)
1379 1.1 itojun pool_put(&pf_state_scrub_pl, state->src.scrub);
1380 1.1 itojun if (state->dst.scrub)
1381 1.1 itojun pool_put(&pf_state_scrub_pl, state->dst.scrub);
1382 1.1 itojun
1383 1.1 itojun /* Someday... flush the TCP segment reassembly descriptors. */
1384 1.1 itojun }
1385 1.1 itojun
1386 1.1 itojun int
1387 1.1 itojun pf_normalize_tcp_stateful(struct mbuf *m, int off, struct pf_pdesc *pd,
1388 1.1 itojun u_short *reason, struct tcphdr *th, struct pf_state_peer *src,
1389 1.1 itojun struct pf_state_peer *dst, int *writeback)
1390 1.1 itojun {
1391 1.1 itojun u_int8_t hdr[60];
1392 1.1 itojun u_int8_t *opt;
1393 1.1 itojun int copyback = 0;
1394 1.1 itojun
1395 1.1 itojun KASSERT(src->scrub || dst->scrub);
1396 1.1 itojun
1397 1.1 itojun /*
1398 1.1 itojun * Enforce the minimum TTL seen for this connection. Negate a common
1399 1.1 itojun * technique to evade an intrusion detection system and confuse
1400 1.1 itojun * firewall state code.
1401 1.1 itojun */
1402 1.1 itojun switch (pd->af) {
1403 1.1 itojun #ifdef INET
1404 1.1 itojun case AF_INET: {
1405 1.1 itojun if (src->scrub) {
1406 1.1 itojun struct ip *h = mtod(m, struct ip *);
1407 1.1 itojun if (h->ip_ttl > src->scrub->pfss_ttl)
1408 1.1 itojun src->scrub->pfss_ttl = h->ip_ttl;
1409 1.1 itojun h->ip_ttl = src->scrub->pfss_ttl;
1410 1.1 itojun }
1411 1.1 itojun break;
1412 1.1 itojun }
1413 1.1 itojun #endif /* INET */
1414 1.1 itojun #ifdef INET6
1415 1.1 itojun case AF_INET6: {
1416 1.1 itojun if (src->scrub) {
1417 1.1 itojun struct ip6_hdr *h = mtod(m, struct ip6_hdr *);
1418 1.1 itojun if (h->ip6_hlim > src->scrub->pfss_ttl)
1419 1.1 itojun src->scrub->pfss_ttl = h->ip6_hlim;
1420 1.1 itojun h->ip6_hlim = src->scrub->pfss_ttl;
1421 1.1 itojun }
1422 1.1 itojun break;
1423 1.1 itojun }
1424 1.1 itojun #endif /* INET6 */
1425 1.1 itojun }
1426 1.1 itojun
1427 1.1 itojun if (th->th_off > (sizeof(struct tcphdr) >> 2) &&
1428 1.1 itojun ((src->scrub && (src->scrub->pfss_flags & PFSS_TIMESTAMP)) ||
1429 1.1 itojun (dst->scrub && (dst->scrub->pfss_flags & PFSS_TIMESTAMP))) &&
1430 1.1 itojun pf_pull_hdr(m, off, hdr, th->th_off << 2, NULL, NULL, pd->af)) {
1431 1.1 itojun /* Diddle with TCP options */
1432 1.1 itojun int hlen;
1433 1.1 itojun opt = hdr + sizeof(struct tcphdr);
1434 1.1 itojun hlen = (th->th_off << 2) - sizeof(struct tcphdr);
1435 1.1 itojun while (hlen >= TCPOLEN_TIMESTAMP) {
1436 1.1 itojun switch (*opt) {
1437 1.1 itojun case TCPOPT_EOL: /* FALLTHROUGH */
1438 1.1 itojun case TCPOPT_NOP:
1439 1.1 itojun opt++;
1440 1.1 itojun hlen--;
1441 1.1 itojun break;
1442 1.1 itojun case TCPOPT_TIMESTAMP:
1443 1.1 itojun /* Modulate the timestamps. Can be used for
1444 1.1 itojun * NAT detection, OS uptime determination or
1445 1.1 itojun * reboot detection.
1446 1.1 itojun */
1447 1.1 itojun if (opt[1] >= TCPOLEN_TIMESTAMP) {
1448 1.1 itojun u_int32_t ts_value;
1449 1.1 itojun if (src->scrub &&
1450 1.1 itojun (src->scrub->pfss_flags &
1451 1.1 itojun PFSS_TIMESTAMP)) {
1452 1.1 itojun memcpy(&ts_value, &opt[2],
1453 1.1 itojun sizeof(u_int32_t));
1454 1.1 itojun ts_value = htonl(ntohl(ts_value)
1455 1.1 itojun + src->scrub->pfss_ts_mod);
1456 1.1 itojun pf_change_a(&opt[2],
1457 1.1 itojun &th->th_sum, ts_value, 0);
1458 1.1 itojun copyback = 1;
1459 1.1 itojun }
1460 1.1 itojun
1461 1.1 itojun /* Modulate TS reply iff valid (!0) */
1462 1.1 itojun memcpy(&ts_value, &opt[6],
1463 1.1 itojun sizeof(u_int32_t));
1464 1.1 itojun if (ts_value && dst->scrub &&
1465 1.1 itojun (dst->scrub->pfss_flags &
1466 1.1 itojun PFSS_TIMESTAMP)) {
1467 1.1 itojun ts_value = htonl(ntohl(ts_value)
1468 1.1 itojun - dst->scrub->pfss_ts_mod);
1469 1.1 itojun pf_change_a(&opt[6],
1470 1.1 itojun &th->th_sum, ts_value, 0);
1471 1.1 itojun copyback = 1;
1472 1.1 itojun }
1473 1.1 itojun }
1474 1.1 itojun /* FALLTHROUGH */
1475 1.1 itojun default:
1476 1.1 itojun hlen -= opt[1];
1477 1.1 itojun opt += opt[1];
1478 1.1 itojun break;
1479 1.1 itojun }
1480 1.1 itojun }
1481 1.1 itojun if (copyback) {
1482 1.1 itojun /* Copyback the options, caller copys back header */
1483 1.1 itojun *writeback = 1;
1484 1.1 itojun m_copyback(m, off + sizeof(struct tcphdr),
1485 1.1 itojun (th->th_off << 2) - sizeof(struct tcphdr), hdr +
1486 1.1 itojun sizeof(struct tcphdr));
1487 1.1 itojun }
1488 1.1 itojun }
1489 1.1 itojun
1490 1.1 itojun
1491 1.1 itojun /* I have a dream.... TCP segment reassembly.... */
1492 1.1 itojun return (0);
1493 1.1 itojun }
1494 1.1 itojun int
1495 1.1 itojun pf_normalize_tcpopt(struct pf_rule *r, struct mbuf *m, struct tcphdr *th,
1496 1.1 itojun int off)
1497 1.1 itojun {
1498 1.1 itojun u_int16_t *mss;
1499 1.1 itojun int thoff;
1500 1.1 itojun int opt, cnt, optlen = 0;
1501 1.1 itojun int rewrite = 0;
1502 1.1 itojun u_char *optp;
1503 1.1 itojun
1504 1.1 itojun thoff = th->th_off << 2;
1505 1.1 itojun cnt = thoff - sizeof(struct tcphdr);
1506 1.1 itojun optp = mtod(m, caddr_t) + off + sizeof(struct tcphdr);
1507 1.1 itojun
1508 1.1 itojun for (; cnt > 0; cnt -= optlen, optp += optlen) {
1509 1.1 itojun opt = optp[0];
1510 1.1 itojun if (opt == TCPOPT_EOL)
1511 1.1 itojun break;
1512 1.1 itojun if (opt == TCPOPT_NOP)
1513 1.1 itojun optlen = 1;
1514 1.1 itojun else {
1515 1.1 itojun if (cnt < 2)
1516 1.1 itojun break;
1517 1.1 itojun optlen = optp[1];
1518 1.1 itojun if (optlen < 2 || optlen > cnt)
1519 1.1 itojun break;
1520 1.1 itojun }
1521 1.1 itojun switch (opt) {
1522 1.1 itojun case TCPOPT_MAXSEG:
1523 1.1 itojun mss = (u_int16_t *)(optp + 2);
1524 1.1 itojun if ((ntohs(*mss)) > r->max_mss) {
1525 1.1 itojun th->th_sum = pf_cksum_fixup(th->th_sum,
1526 1.1 itojun *mss, htons(r->max_mss));
1527 1.1 itojun *mss = htons(r->max_mss);
1528 1.1 itojun rewrite = 1;
1529 1.1 itojun }
1530 1.1 itojun break;
1531 1.1 itojun default:
1532 1.1 itojun break;
1533 1.1 itojun }
1534 1.1 itojun }
1535 1.1 itojun
1536 1.1 itojun return (rewrite);
1537 1.1 itojun }
1538