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