pf_osfp.c revision 1.6 1 1.6 ad /* $NetBSD: pf_osfp.c,v 1.6 2007/03/12 18:18:31 ad Exp $ */
2 1.4 yamt /* $OpenBSD: pf_osfp.c,v 1.10 2004/04/09 19:30:41 frantzen Exp $ */
3 1.1 itojun
4 1.1 itojun /*
5 1.1 itojun * Copyright (c) 2003 Mike Frantzen <frantzen (at) w4g.org>
6 1.1 itojun *
7 1.1 itojun * Permission to use, copy, modify, and distribute this software for any
8 1.1 itojun * purpose with or without fee is hereby granted, provided that the above
9 1.1 itojun * copyright notice and this permission notice appear in all copies.
10 1.1 itojun *
11 1.1 itojun * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 1.1 itojun * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 1.1 itojun * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 1.1 itojun * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 1.1 itojun * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 1.1 itojun * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 1.1 itojun * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 1.1 itojun *
19 1.1 itojun */
20 1.1 itojun
21 1.2 itojun #ifdef _KERNEL_OPT
22 1.2 itojun #include "opt_inet.h"
23 1.2 itojun #endif
24 1.2 itojun
25 1.1 itojun #include <sys/param.h>
26 1.1 itojun #include <sys/socket.h>
27 1.1 itojun #ifdef _KERNEL
28 1.1 itojun # include <sys/systm.h>
29 1.1 itojun #endif /* _KERNEL */
30 1.1 itojun #include <sys/mbuf.h>
31 1.1 itojun
32 1.1 itojun #include <netinet/in.h>
33 1.1 itojun #include <netinet/in_systm.h>
34 1.1 itojun #include <netinet/ip.h>
35 1.1 itojun #include <netinet/tcp.h>
36 1.1 itojun
37 1.1 itojun #include <net/if.h>
38 1.1 itojun #include <net/pfvar.h>
39 1.1 itojun
40 1.1 itojun #ifdef INET6
41 1.1 itojun #include <netinet/ip6.h>
42 1.1 itojun #endif /* INET6 */
43 1.1 itojun
44 1.1 itojun
45 1.1 itojun #ifdef _KERNEL
46 1.1 itojun # define DPFPRINTF(format, x...) \
47 1.1 itojun if (pf_status.debug >= PF_DEBUG_NOISY) \
48 1.1 itojun printf(format , ##x)
49 1.1 itojun typedef struct pool pool_t;
50 1.1 itojun
51 1.1 itojun #else
52 1.1 itojun /* Userland equivalents so we can lend code to tcpdump et al. */
53 1.1 itojun
54 1.1 itojun # include <arpa/inet.h>
55 1.1 itojun # include <errno.h>
56 1.1 itojun # include <stdio.h>
57 1.1 itojun # include <stdlib.h>
58 1.1 itojun # include <string.h>
59 1.1 itojun # define pool_t int
60 1.1 itojun # define pool_get(pool, flags) malloc(*(pool))
61 1.1 itojun # define pool_put(pool, item) free(item)
62 1.1 itojun # define pool_init(pool, size, a, ao, f, m, p) (*(pool)) = (size)
63 1.1 itojun
64 1.1 itojun # ifdef PFDEBUG
65 1.1 itojun # include <sys/stdarg.h>
66 1.1 itojun # define DPFPRINTF(format, x...) fprintf(stderr, format , ##x)
67 1.1 itojun # else
68 1.1 itojun # define DPFPRINTF(format, x...) ((void)0)
69 1.1 itojun # endif /* PFDEBUG */
70 1.1 itojun #endif /* _KERNEL */
71 1.1 itojun
72 1.1 itojun
73 1.1 itojun SLIST_HEAD(pf_osfp_list, pf_os_fingerprint) pf_osfp_list;
74 1.1 itojun pool_t pf_osfp_entry_pl;
75 1.1 itojun pool_t pf_osfp_pl;
76 1.1 itojun
77 1.1 itojun struct pf_os_fingerprint *pf_osfp_find(struct pf_osfp_list *,
78 1.1 itojun struct pf_os_fingerprint *, u_int8_t);
79 1.1 itojun struct pf_os_fingerprint *pf_osfp_find_exact(struct pf_osfp_list *,
80 1.1 itojun struct pf_os_fingerprint *);
81 1.1 itojun void pf_osfp_insert(struct pf_osfp_list *,
82 1.1 itojun struct pf_os_fingerprint *);
83 1.1 itojun
84 1.1 itojun
85 1.1 itojun #ifdef _KERNEL
86 1.1 itojun /*
87 1.1 itojun * Passively fingerprint the OS of the host (IPv4 TCP SYN packets only)
88 1.1 itojun * Returns the list of possible OSes.
89 1.1 itojun */
90 1.1 itojun struct pf_osfp_enlist *
91 1.1 itojun pf_osfp_fingerprint(struct pf_pdesc *pd, struct mbuf *m, int off,
92 1.1 itojun const struct tcphdr *tcp)
93 1.1 itojun {
94 1.1 itojun struct ip *ip;
95 1.1 itojun char hdr[60];
96 1.1 itojun
97 1.1 itojun /* XXX don't have a fingerprint database for IPv6 :-( */
98 1.1 itojun if (pd->af != PF_INET || pd->proto != IPPROTO_TCP || (tcp->th_off << 2)
99 1.1 itojun < sizeof(*tcp))
100 1.1 itojun return (NULL);
101 1.1 itojun
102 1.1 itojun ip = mtod(m, struct ip *);
103 1.1 itojun if (!pf_pull_hdr(m, off, hdr, tcp->th_off << 2, NULL, NULL, pd->af))
104 1.1 itojun return (NULL);
105 1.1 itojun
106 1.1 itojun return (pf_osfp_fingerprint_hdr(ip, (struct tcphdr *)hdr));
107 1.1 itojun }
108 1.1 itojun #endif /* _KERNEL */
109 1.1 itojun
110 1.1 itojun struct pf_osfp_enlist *
111 1.1 itojun pf_osfp_fingerprint_hdr(const struct ip *ip, const struct tcphdr *tcp)
112 1.1 itojun {
113 1.1 itojun struct pf_os_fingerprint fp, *fpresult;
114 1.1 itojun int cnt, optlen = 0;
115 1.1 itojun const u_int8_t *optp;
116 1.1 itojun
117 1.1 itojun if ((tcp->th_flags & (TH_SYN|TH_ACK)) != TH_SYN || (ip->ip_off &
118 1.1 itojun htons(IP_OFFMASK)))
119 1.1 itojun return (NULL);
120 1.1 itojun
121 1.1 itojun memset(&fp, 0, sizeof(fp));
122 1.1 itojun
123 1.1 itojun fp.fp_psize = ntohs(ip->ip_len);
124 1.1 itojun fp.fp_ttl = ip->ip_ttl;
125 1.1 itojun if (ip->ip_off & htons(IP_DF))
126 1.1 itojun fp.fp_flags |= PF_OSFP_DF;
127 1.1 itojun fp.fp_wsize = ntohs(tcp->th_win);
128 1.1 itojun
129 1.1 itojun
130 1.1 itojun cnt = (tcp->th_off << 2) - sizeof(*tcp);
131 1.1 itojun optp = (const u_int8_t *)((const char *)tcp + sizeof(*tcp));
132 1.1 itojun for (; cnt > 0; cnt -= optlen, optp += optlen) {
133 1.1 itojun if (*optp == TCPOPT_EOL)
134 1.1 itojun break;
135 1.1 itojun
136 1.1 itojun fp.fp_optcnt++;
137 1.1 itojun if (*optp == TCPOPT_NOP) {
138 1.1 itojun fp.fp_tcpopts = (fp.fp_tcpopts << PF_OSFP_TCPOPT_BITS) |
139 1.1 itojun PF_OSFP_TCPOPT_NOP;
140 1.1 itojun optlen = 1;
141 1.1 itojun } else {
142 1.1 itojun if (cnt < 2)
143 1.1 itojun return (NULL);
144 1.1 itojun optlen = optp[1];
145 1.1 itojun if (optlen > cnt || optlen < 2)
146 1.1 itojun return (NULL);
147 1.1 itojun switch (*optp) {
148 1.1 itojun case TCPOPT_MAXSEG:
149 1.1 itojun if (optlen >= TCPOLEN_MAXSEG)
150 1.1 itojun memcpy(&fp.fp_mss, &optp[2],
151 1.1 itojun sizeof(fp.fp_mss));
152 1.1 itojun fp.fp_tcpopts = (fp.fp_tcpopts <<
153 1.1 itojun PF_OSFP_TCPOPT_BITS) | PF_OSFP_TCPOPT_MSS;
154 1.1 itojun NTOHS(fp.fp_mss);
155 1.1 itojun break;
156 1.1 itojun case TCPOPT_WINDOW:
157 1.1 itojun if (optlen >= TCPOLEN_WINDOW)
158 1.1 itojun memcpy(&fp.fp_wscale, &optp[2],
159 1.1 itojun sizeof(fp.fp_wscale));
160 1.1 itojun NTOHS(fp.fp_wscale);
161 1.1 itojun fp.fp_tcpopts = (fp.fp_tcpopts <<
162 1.1 itojun PF_OSFP_TCPOPT_BITS) |
163 1.1 itojun PF_OSFP_TCPOPT_WSCALE;
164 1.1 itojun break;
165 1.1 itojun case TCPOPT_SACK_PERMITTED:
166 1.1 itojun fp.fp_tcpopts = (fp.fp_tcpopts <<
167 1.1 itojun PF_OSFP_TCPOPT_BITS) | PF_OSFP_TCPOPT_SACK;
168 1.1 itojun break;
169 1.1 itojun case TCPOPT_TIMESTAMP:
170 1.1 itojun if (optlen >= TCPOLEN_TIMESTAMP) {
171 1.1 itojun u_int32_t ts;
172 1.1 itojun memcpy(&ts, &optp[2], sizeof(ts));
173 1.1 itojun if (ts == 0)
174 1.1 itojun fp.fp_flags |= PF_OSFP_TS0;
175 1.1 itojun
176 1.1 itojun }
177 1.1 itojun fp.fp_tcpopts = (fp.fp_tcpopts <<
178 1.1 itojun PF_OSFP_TCPOPT_BITS) | PF_OSFP_TCPOPT_TS;
179 1.1 itojun break;
180 1.1 itojun default:
181 1.1 itojun return (NULL);
182 1.1 itojun }
183 1.1 itojun }
184 1.1 itojun optlen = MAX(optlen, 1); /* paranoia */
185 1.1 itojun }
186 1.1 itojun
187 1.1 itojun DPFPRINTF("fingerprinted %s:%d %d:%d:%d:%d:%llx (%d) "
188 1.1 itojun "(TS=%s,M=%s%d,W=%s%d)\n",
189 1.1 itojun inet_ntoa(ip->ip_src), ntohs(tcp->th_sport),
190 1.1 itojun fp.fp_wsize, fp.fp_ttl, (fp.fp_flags & PF_OSFP_DF) != 0,
191 1.1 itojun fp.fp_psize, (long long int)fp.fp_tcpopts, fp.fp_optcnt,
192 1.1 itojun (fp.fp_flags & PF_OSFP_TS0) ? "0" : "",
193 1.1 itojun (fp.fp_flags & PF_OSFP_MSS_MOD) ? "%" :
194 1.1 itojun (fp.fp_flags & PF_OSFP_MSS_DC) ? "*" : "",
195 1.1 itojun fp.fp_mss,
196 1.1 itojun (fp.fp_flags & PF_OSFP_WSCALE_MOD) ? "%" :
197 1.1 itojun (fp.fp_flags & PF_OSFP_WSCALE_DC) ? "*" : "",
198 1.1 itojun fp.fp_wscale);
199 1.1 itojun
200 1.1 itojun if ((fpresult = pf_osfp_find(&pf_osfp_list, &fp,
201 1.1 itojun PF_OSFP_MAXTTL_OFFSET)))
202 1.1 itojun return (&fpresult->fp_oses);
203 1.1 itojun return (NULL);
204 1.1 itojun }
205 1.1 itojun
206 1.1 itojun /* Match a fingerprint ID against a list of OSes */
207 1.1 itojun int
208 1.1 itojun pf_osfp_match(struct pf_osfp_enlist *list, pf_osfp_t os)
209 1.1 itojun {
210 1.1 itojun struct pf_osfp_entry *entry;
211 1.1 itojun int os_class, os_version, os_subtype;
212 1.1 itojun int en_class, en_version, en_subtype;
213 1.1 itojun
214 1.1 itojun if (os == PF_OSFP_ANY)
215 1.1 itojun return (1);
216 1.1 itojun if (list == NULL) {
217 1.1 itojun DPFPRINTF("osfp no match against %x\n", os);
218 1.1 itojun return (os == PF_OSFP_UNKNOWN);
219 1.1 itojun }
220 1.1 itojun PF_OSFP_UNPACK(os, os_class, os_version, os_subtype);
221 1.1 itojun SLIST_FOREACH(entry, list, fp_entry) {
222 1.1 itojun PF_OSFP_UNPACK(entry->fp_os, en_class, en_version, en_subtype);
223 1.1 itojun if ((os_class == PF_OSFP_ANY || en_class == os_class) &&
224 1.1 itojun (os_version == PF_OSFP_ANY || en_version == os_version) &&
225 1.1 itojun (os_subtype == PF_OSFP_ANY || en_subtype == os_subtype)) {
226 1.1 itojun DPFPRINTF("osfp matched %s %s %s %x==%x\n",
227 1.1 itojun entry->fp_class_nm, entry->fp_version_nm,
228 1.1 itojun entry->fp_subtype_nm, os, entry->fp_os);
229 1.1 itojun return (1);
230 1.1 itojun }
231 1.1 itojun }
232 1.1 itojun DPFPRINTF("fingerprint 0x%x didn't match\n", os);
233 1.1 itojun return (0);
234 1.1 itojun }
235 1.1 itojun
236 1.1 itojun /* Initialize the OS fingerprint system */
237 1.1 itojun void
238 1.1 itojun pf_osfp_initialize(void)
239 1.1 itojun {
240 1.6 ad #ifdef __NetBSD__
241 1.6 ad pool_init(&pf_osfp_entry_pl, sizeof(struct pf_osfp_entry), 0, 0, 0,
242 1.6 ad "pfosfpen", &pool_allocator_nointr, IPL_NONE);
243 1.6 ad pool_init(&pf_osfp_pl, sizeof(struct pf_os_fingerprint), 0, 0, 0,
244 1.6 ad "pfosfp", &pool_allocator_nointr, IPL_NONE);
245 1.6 ad #else
246 1.1 itojun pool_init(&pf_osfp_entry_pl, sizeof(struct pf_osfp_entry), 0, 0, 0,
247 1.4 yamt "pfosfpen", &pool_allocator_nointr);
248 1.1 itojun pool_init(&pf_osfp_pl, sizeof(struct pf_os_fingerprint), 0, 0, 0,
249 1.4 yamt "pfosfp", &pool_allocator_nointr);
250 1.6 ad #endif
251 1.1 itojun SLIST_INIT(&pf_osfp_list);
252 1.1 itojun }
253 1.1 itojun
254 1.3 itojun #ifdef _LKM
255 1.3 itojun void
256 1.3 itojun pf_osfp_destroy(void)
257 1.3 itojun {
258 1.3 itojun pf_osfp_flush();
259 1.3 itojun
260 1.3 itojun pool_destroy(&pf_osfp_pl);
261 1.3 itojun pool_destroy(&pf_osfp_entry_pl);
262 1.3 itojun }
263 1.3 itojun #endif
264 1.3 itojun
265 1.1 itojun /* Flush the fingerprint list */
266 1.1 itojun void
267 1.1 itojun pf_osfp_flush(void)
268 1.1 itojun {
269 1.1 itojun struct pf_os_fingerprint *fp;
270 1.1 itojun struct pf_osfp_entry *entry;
271 1.1 itojun
272 1.1 itojun while ((fp = SLIST_FIRST(&pf_osfp_list))) {
273 1.1 itojun SLIST_REMOVE_HEAD(&pf_osfp_list, fp_next);
274 1.1 itojun while ((entry = SLIST_FIRST(&fp->fp_oses))) {
275 1.1 itojun SLIST_REMOVE_HEAD(&fp->fp_oses, fp_entry);
276 1.1 itojun pool_put(&pf_osfp_entry_pl, entry);
277 1.1 itojun }
278 1.1 itojun pool_put(&pf_osfp_pl, fp);
279 1.1 itojun }
280 1.1 itojun }
281 1.1 itojun
282 1.1 itojun
283 1.1 itojun /* Add a fingerprint */
284 1.1 itojun int
285 1.1 itojun pf_osfp_add(struct pf_osfp_ioctl *fpioc)
286 1.1 itojun {
287 1.1 itojun struct pf_os_fingerprint *fp, fpadd;
288 1.1 itojun struct pf_osfp_entry *entry;
289 1.1 itojun
290 1.1 itojun memset(&fpadd, 0, sizeof(fpadd));
291 1.1 itojun fpadd.fp_tcpopts = fpioc->fp_tcpopts;
292 1.1 itojun fpadd.fp_wsize = fpioc->fp_wsize;
293 1.1 itojun fpadd.fp_psize = fpioc->fp_psize;
294 1.1 itojun fpadd.fp_mss = fpioc->fp_mss;
295 1.1 itojun fpadd.fp_flags = fpioc->fp_flags;
296 1.1 itojun fpadd.fp_optcnt = fpioc->fp_optcnt;
297 1.1 itojun fpadd.fp_wscale = fpioc->fp_wscale;
298 1.1 itojun fpadd.fp_ttl = fpioc->fp_ttl;
299 1.1 itojun
300 1.1 itojun DPFPRINTF("adding osfp %s %s %s = %s%d:%d:%d:%s%d:0x%llx %d "
301 1.1 itojun "(TS=%s,M=%s%d,W=%s%d) %x\n",
302 1.1 itojun fpioc->fp_os.fp_class_nm, fpioc->fp_os.fp_version_nm,
303 1.1 itojun fpioc->fp_os.fp_subtype_nm,
304 1.1 itojun (fpadd.fp_flags & PF_OSFP_WSIZE_MOD) ? "%" :
305 1.1 itojun (fpadd.fp_flags & PF_OSFP_WSIZE_MSS) ? "S" :
306 1.1 itojun (fpadd.fp_flags & PF_OSFP_WSIZE_MTU) ? "T" :
307 1.1 itojun (fpadd.fp_flags & PF_OSFP_WSIZE_DC) ? "*" : "",
308 1.1 itojun fpadd.fp_wsize,
309 1.1 itojun fpadd.fp_ttl,
310 1.1 itojun (fpadd.fp_flags & PF_OSFP_DF) ? 1 : 0,
311 1.1 itojun (fpadd.fp_flags & PF_OSFP_PSIZE_MOD) ? "%" :
312 1.1 itojun (fpadd.fp_flags & PF_OSFP_PSIZE_DC) ? "*" : "",
313 1.1 itojun fpadd.fp_psize,
314 1.1 itojun (long long int)fpadd.fp_tcpopts, fpadd.fp_optcnt,
315 1.1 itojun (fpadd.fp_flags & PF_OSFP_TS0) ? "0" : "",
316 1.1 itojun (fpadd.fp_flags & PF_OSFP_MSS_MOD) ? "%" :
317 1.1 itojun (fpadd.fp_flags & PF_OSFP_MSS_DC) ? "*" : "",
318 1.1 itojun fpadd.fp_mss,
319 1.1 itojun (fpadd.fp_flags & PF_OSFP_WSCALE_MOD) ? "%" :
320 1.1 itojun (fpadd.fp_flags & PF_OSFP_WSCALE_DC) ? "*" : "",
321 1.1 itojun fpadd.fp_wscale,
322 1.1 itojun fpioc->fp_os.fp_os);
323 1.1 itojun
324 1.1 itojun
325 1.1 itojun if ((fp = pf_osfp_find_exact(&pf_osfp_list, &fpadd))) {
326 1.1 itojun SLIST_FOREACH(entry, &fp->fp_oses, fp_entry) {
327 1.1 itojun if (PF_OSFP_ENTRY_EQ(entry, &fpioc->fp_os))
328 1.1 itojun return (EEXIST);
329 1.1 itojun }
330 1.1 itojun if ((entry = pool_get(&pf_osfp_entry_pl, PR_NOWAIT)) == NULL)
331 1.1 itojun return (ENOMEM);
332 1.1 itojun } else {
333 1.1 itojun if ((fp = pool_get(&pf_osfp_pl, PR_NOWAIT)) == NULL)
334 1.1 itojun return (ENOMEM);
335 1.1 itojun memset(fp, 0, sizeof(*fp));
336 1.1 itojun fp->fp_tcpopts = fpioc->fp_tcpopts;
337 1.1 itojun fp->fp_wsize = fpioc->fp_wsize;
338 1.1 itojun fp->fp_psize = fpioc->fp_psize;
339 1.1 itojun fp->fp_mss = fpioc->fp_mss;
340 1.1 itojun fp->fp_flags = fpioc->fp_flags;
341 1.1 itojun fp->fp_optcnt = fpioc->fp_optcnt;
342 1.1 itojun fp->fp_wscale = fpioc->fp_wscale;
343 1.1 itojun fp->fp_ttl = fpioc->fp_ttl;
344 1.1 itojun SLIST_INIT(&fp->fp_oses);
345 1.1 itojun if ((entry = pool_get(&pf_osfp_entry_pl, PR_NOWAIT)) == NULL) {
346 1.1 itojun pool_put(&pf_osfp_pl, fp);
347 1.1 itojun return (ENOMEM);
348 1.1 itojun }
349 1.1 itojun pf_osfp_insert(&pf_osfp_list, fp);
350 1.1 itojun }
351 1.1 itojun memcpy(entry, &fpioc->fp_os, sizeof(*entry));
352 1.1 itojun
353 1.1 itojun /* Make sure the strings are NUL terminated */
354 1.1 itojun entry->fp_class_nm[sizeof(entry->fp_class_nm)-1] = '\0';
355 1.1 itojun entry->fp_version_nm[sizeof(entry->fp_version_nm)-1] = '\0';
356 1.1 itojun entry->fp_subtype_nm[sizeof(entry->fp_subtype_nm)-1] = '\0';
357 1.1 itojun
358 1.1 itojun SLIST_INSERT_HEAD(&fp->fp_oses, entry, fp_entry);
359 1.1 itojun
360 1.1 itojun #ifdef PFDEBUG
361 1.1 itojun if ((fp = pf_osfp_validate()))
362 1.1 itojun printf("Invalid fingerprint list\n");
363 1.1 itojun #endif /* PFDEBUG */
364 1.1 itojun return (0);
365 1.1 itojun }
366 1.1 itojun
367 1.1 itojun
368 1.1 itojun /* Find a fingerprint in the list */
369 1.1 itojun struct pf_os_fingerprint *
370 1.1 itojun pf_osfp_find(struct pf_osfp_list *list, struct pf_os_fingerprint *find,
371 1.1 itojun u_int8_t ttldiff)
372 1.1 itojun {
373 1.1 itojun struct pf_os_fingerprint *f;
374 1.1 itojun
375 1.1 itojun #define MATCH_INT(_MOD, _DC, _field) \
376 1.1 itojun if ((f->fp_flags & _DC) == 0) { \
377 1.1 itojun if ((f->fp_flags & _MOD) == 0) { \
378 1.1 itojun if (f->_field != find->_field) \
379 1.1 itojun continue; \
380 1.1 itojun } else { \
381 1.1 itojun if (f->_field == 0 || find->_field % f->_field) \
382 1.1 itojun continue; \
383 1.1 itojun } \
384 1.1 itojun }
385 1.1 itojun
386 1.1 itojun SLIST_FOREACH(f, list, fp_next) {
387 1.1 itojun if (f->fp_tcpopts != find->fp_tcpopts ||
388 1.1 itojun f->fp_optcnt != find->fp_optcnt ||
389 1.1 itojun f->fp_ttl < find->fp_ttl ||
390 1.1 itojun f->fp_ttl - find->fp_ttl > ttldiff ||
391 1.1 itojun (f->fp_flags & (PF_OSFP_DF|PF_OSFP_TS0)) !=
392 1.1 itojun (find->fp_flags & (PF_OSFP_DF|PF_OSFP_TS0)))
393 1.1 itojun continue;
394 1.1 itojun
395 1.1 itojun MATCH_INT(PF_OSFP_PSIZE_MOD, PF_OSFP_PSIZE_DC, fp_psize)
396 1.1 itojun MATCH_INT(PF_OSFP_MSS_MOD, PF_OSFP_MSS_DC, fp_mss)
397 1.1 itojun MATCH_INT(PF_OSFP_WSCALE_MOD, PF_OSFP_WSCALE_DC, fp_wscale)
398 1.1 itojun if ((f->fp_flags & PF_OSFP_WSIZE_DC) == 0) {
399 1.1 itojun if (f->fp_flags & PF_OSFP_WSIZE_MSS) {
400 1.1 itojun if (find->fp_mss == 0)
401 1.1 itojun continue;
402 1.1 itojun
403 1.1 itojun /* Some "smart" NAT devices and DSL routers will tweak the MSS size and
404 1.1 itojun * will set it to whatever is suitable for the link type.
405 1.1 itojun */
406 1.1 itojun #define SMART_MSS 1460
407 1.1 itojun if ((find->fp_wsize % find->fp_mss ||
408 1.1 itojun find->fp_wsize / find->fp_mss !=
409 1.1 itojun f->fp_wsize) &&
410 1.1 itojun (find->fp_wsize % SMART_MSS ||
411 1.1 itojun find->fp_wsize / SMART_MSS !=
412 1.1 itojun f->fp_wsize))
413 1.1 itojun continue;
414 1.1 itojun } else if (f->fp_flags & PF_OSFP_WSIZE_MTU) {
415 1.1 itojun if (find->fp_mss == 0)
416 1.1 itojun continue;
417 1.1 itojun
418 1.1 itojun #define MTUOFF (sizeof(struct ip) + sizeof(struct tcphdr))
419 1.1 itojun #define SMART_MTU (SMART_MSS + MTUOFF)
420 1.1 itojun if ((find->fp_wsize % (find->fp_mss + MTUOFF) ||
421 1.1 itojun find->fp_wsize / (find->fp_mss + MTUOFF) !=
422 1.1 itojun f->fp_wsize) &&
423 1.1 itojun (find->fp_wsize % SMART_MTU ||
424 1.1 itojun find->fp_wsize / SMART_MTU !=
425 1.1 itojun f->fp_wsize))
426 1.1 itojun continue;
427 1.1 itojun } else if (f->fp_flags & PF_OSFP_WSIZE_MOD) {
428 1.1 itojun if (f->fp_wsize == 0 || find->fp_wsize %
429 1.1 itojun f->fp_wsize)
430 1.1 itojun continue;
431 1.1 itojun } else {
432 1.1 itojun if (f->fp_wsize != find->fp_wsize)
433 1.1 itojun continue;
434 1.1 itojun }
435 1.1 itojun }
436 1.1 itojun return (f);
437 1.1 itojun }
438 1.1 itojun
439 1.1 itojun return (NULL);
440 1.1 itojun }
441 1.1 itojun
442 1.1 itojun /* Find an exact fingerprint in the list */
443 1.1 itojun struct pf_os_fingerprint *
444 1.1 itojun pf_osfp_find_exact(struct pf_osfp_list *list, struct pf_os_fingerprint *find)
445 1.1 itojun {
446 1.1 itojun struct pf_os_fingerprint *f;
447 1.1 itojun
448 1.1 itojun SLIST_FOREACH(f, list, fp_next) {
449 1.1 itojun if (f->fp_tcpopts == find->fp_tcpopts &&
450 1.1 itojun f->fp_wsize == find->fp_wsize &&
451 1.1 itojun f->fp_psize == find->fp_psize &&
452 1.1 itojun f->fp_mss == find->fp_mss &&
453 1.1 itojun f->fp_flags == find->fp_flags &&
454 1.1 itojun f->fp_optcnt == find->fp_optcnt &&
455 1.1 itojun f->fp_wscale == find->fp_wscale &&
456 1.1 itojun f->fp_ttl == find->fp_ttl)
457 1.1 itojun return (f);
458 1.1 itojun }
459 1.1 itojun
460 1.1 itojun return (NULL);
461 1.1 itojun }
462 1.1 itojun
463 1.1 itojun /* Insert a fingerprint into the list */
464 1.1 itojun void
465 1.1 itojun pf_osfp_insert(struct pf_osfp_list *list, struct pf_os_fingerprint *ins)
466 1.1 itojun {
467 1.1 itojun struct pf_os_fingerprint *f, *prev = NULL;
468 1.1 itojun
469 1.1 itojun /* XXX need to go semi tree based. can key on tcp options */
470 1.1 itojun
471 1.1 itojun SLIST_FOREACH(f, list, fp_next)
472 1.1 itojun prev = f;
473 1.1 itojun if (prev)
474 1.1 itojun SLIST_INSERT_AFTER(prev, ins, fp_next);
475 1.1 itojun else
476 1.1 itojun SLIST_INSERT_HEAD(list, ins, fp_next);
477 1.1 itojun }
478 1.1 itojun
479 1.1 itojun /* Fill a fingerprint by its number (from an ioctl) */
480 1.1 itojun int
481 1.1 itojun pf_osfp_get(struct pf_osfp_ioctl *fpioc)
482 1.1 itojun {
483 1.1 itojun struct pf_os_fingerprint *fp;
484 1.1 itojun struct pf_osfp_entry *entry;
485 1.1 itojun int num = fpioc->fp_getnum;
486 1.1 itojun int i = 0;
487 1.1 itojun
488 1.1 itojun
489 1.1 itojun memset(fpioc, 0, sizeof(*fpioc));
490 1.1 itojun SLIST_FOREACH(fp, &pf_osfp_list, fp_next) {
491 1.1 itojun SLIST_FOREACH(entry, &fp->fp_oses, fp_entry) {
492 1.1 itojun if (i++ == num) {
493 1.1 itojun fpioc->fp_mss = fp->fp_mss;
494 1.1 itojun fpioc->fp_wsize = fp->fp_wsize;
495 1.1 itojun fpioc->fp_flags = fp->fp_flags;
496 1.1 itojun fpioc->fp_psize = fp->fp_psize;
497 1.1 itojun fpioc->fp_ttl = fp->fp_ttl;
498 1.1 itojun fpioc->fp_wscale = fp->fp_wscale;
499 1.1 itojun fpioc->fp_getnum = num;
500 1.1 itojun memcpy(&fpioc->fp_os, entry,
501 1.1 itojun sizeof(fpioc->fp_os));
502 1.1 itojun return (0);
503 1.1 itojun }
504 1.1 itojun }
505 1.1 itojun }
506 1.1 itojun
507 1.1 itojun return (EBUSY);
508 1.1 itojun }
509 1.1 itojun
510 1.1 itojun
511 1.1 itojun /* Validate that each signature is reachable */
512 1.1 itojun struct pf_os_fingerprint *
513 1.1 itojun pf_osfp_validate(void)
514 1.1 itojun {
515 1.1 itojun struct pf_os_fingerprint *f, *f2, find;
516 1.1 itojun
517 1.1 itojun SLIST_FOREACH(f, &pf_osfp_list, fp_next) {
518 1.1 itojun memcpy(&find, f, sizeof(find));
519 1.1 itojun
520 1.1 itojun /* We do a few MSS/th_win percolations to make things unique */
521 1.1 itojun if (find.fp_mss == 0)
522 1.1 itojun find.fp_mss = 128;
523 1.1 itojun if (f->fp_flags & PF_OSFP_WSIZE_MSS)
524 1.1 itojun find.fp_wsize *= find.fp_mss, 1;
525 1.1 itojun else if (f->fp_flags & PF_OSFP_WSIZE_MTU)
526 1.1 itojun find.fp_wsize *= (find.fp_mss + 40);
527 1.1 itojun else if (f->fp_flags & PF_OSFP_WSIZE_MOD)
528 1.1 itojun find.fp_wsize *= 2;
529 1.1 itojun if (f != (f2 = pf_osfp_find(&pf_osfp_list, &find, 0))) {
530 1.1 itojun if (f2)
531 1.1 itojun printf("Found \"%s %s %s\" instead of "
532 1.1 itojun "\"%s %s %s\"\n",
533 1.1 itojun SLIST_FIRST(&f2->fp_oses)->fp_class_nm,
534 1.1 itojun SLIST_FIRST(&f2->fp_oses)->fp_version_nm,
535 1.1 itojun SLIST_FIRST(&f2->fp_oses)->fp_subtype_nm,
536 1.1 itojun SLIST_FIRST(&f->fp_oses)->fp_class_nm,
537 1.1 itojun SLIST_FIRST(&f->fp_oses)->fp_version_nm,
538 1.1 itojun SLIST_FIRST(&f->fp_oses)->fp_subtype_nm);
539 1.1 itojun else
540 1.1 itojun printf("Couldn't find \"%s %s %s\"\n",
541 1.1 itojun SLIST_FIRST(&f->fp_oses)->fp_class_nm,
542 1.1 itojun SLIST_FIRST(&f->fp_oses)->fp_version_nm,
543 1.1 itojun SLIST_FIRST(&f->fp_oses)->fp_subtype_nm);
544 1.1 itojun return (f);
545 1.1 itojun }
546 1.1 itojun }
547 1.1 itojun return (NULL);
548 1.1 itojun }
549