Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* Kernel module to match AH parameters. */ |
2 | /* (C) 1999-2000 Yon Uriarte <yon@astaro.de> | |
3 | * | |
4 | * This program is free software; you can redistribute it and/or modify | |
5 | * it under the terms of the GNU General Public License version 2 as | |
6 | * published by the Free Software Foundation. | |
7 | */ | |
8 | ||
6709dbbb | 9 | #include <linux/in.h> |
1da177e4 LT |
10 | #include <linux/module.h> |
11 | #include <linux/skbuff.h> | |
12 | #include <linux/ip.h> | |
13 | ||
14 | #include <linux/netfilter_ipv4/ipt_ah.h> | |
6709dbbb | 15 | #include <linux/netfilter/x_tables.h> |
1da177e4 LT |
16 | |
17 | MODULE_LICENSE("GPL"); | |
18 | MODULE_AUTHOR("Yon Uriarte <yon@astaro.de>"); | |
19 | MODULE_DESCRIPTION("iptables AH SPI match module"); | |
20 | ||
21 | #ifdef DEBUG_CONNTRACK | |
22 | #define duprintf(format, args...) printk(format , ## args) | |
23 | #else | |
24 | #define duprintf(format, args...) | |
25 | #endif | |
26 | ||
27 | /* Returns 1 if the spi is matched by the range, 0 otherwise */ | |
28 | static inline int | |
29 | spi_match(u_int32_t min, u_int32_t max, u_int32_t spi, int invert) | |
30 | { | |
31 | int r=0; | |
32 | duprintf("ah spi_match:%c 0x%x <= 0x%x <= 0x%x",invert? '!':' ', | |
33 | min,spi,max); | |
34 | r=(spi >= min && spi <= max) ^ invert; | |
35 | duprintf(" result %s\n",r? "PASS" : "FAILED"); | |
36 | return r; | |
37 | } | |
38 | ||
39 | static int | |
40 | match(const struct sk_buff *skb, | |
41 | const struct net_device *in, | |
42 | const struct net_device *out, | |
c4986734 | 43 | const struct xt_match *match, |
1da177e4 LT |
44 | const void *matchinfo, |
45 | int offset, | |
2e4e6a17 | 46 | unsigned int protoff, |
1da177e4 LT |
47 | int *hotdrop) |
48 | { | |
49 | struct ip_auth_hdr _ahdr, *ah; | |
50 | const struct ipt_ah *ahinfo = matchinfo; | |
51 | ||
52 | /* Must not be a fragment. */ | |
53 | if (offset) | |
54 | return 0; | |
55 | ||
2e4e6a17 | 56 | ah = skb_header_pointer(skb, protoff, |
1da177e4 LT |
57 | sizeof(_ahdr), &_ahdr); |
58 | if (ah == NULL) { | |
59 | /* We've been asked to examine this packet, and we | |
60 | * can't. Hence, no choice but to drop. | |
61 | */ | |
62 | duprintf("Dropping evil AH tinygram.\n"); | |
63 | *hotdrop = 1; | |
64 | return 0; | |
65 | } | |
66 | ||
67 | return spi_match(ahinfo->spis[0], ahinfo->spis[1], | |
68 | ntohl(ah->spi), | |
69 | !!(ahinfo->invflags & IPT_AH_INV_SPI)); | |
70 | } | |
71 | ||
72 | /* Called when user tries to insert an entry of this type. */ | |
73 | static int | |
74 | checkentry(const char *tablename, | |
2e4e6a17 | 75 | const void *ip_void, |
c4986734 | 76 | const struct xt_match *match, |
1da177e4 | 77 | void *matchinfo, |
1da177e4 LT |
78 | unsigned int hook_mask) |
79 | { | |
80 | const struct ipt_ah *ahinfo = matchinfo; | |
81 | ||
1d5cd909 | 82 | /* Must specify no unknown invflags */ |
1da177e4 | 83 | if (ahinfo->invflags & ~IPT_AH_INV_MASK) { |
1d5cd909 | 84 | duprintf("ipt_ah: unknown flags %X\n", ahinfo->invflags); |
1da177e4 LT |
85 | return 0; |
86 | } | |
1da177e4 LT |
87 | return 1; |
88 | } | |
89 | ||
6709dbbb | 90 | static struct xt_match ah_match = { |
1da177e4 | 91 | .name = "ah", |
6709dbbb | 92 | .family = AF_INET, |
1d5cd909 PM |
93 | .match = match, |
94 | .matchsize = sizeof(struct ipt_ah), | |
95 | .proto = IPPROTO_AH, | |
96 | .checkentry = checkentry, | |
1da177e4 LT |
97 | .me = THIS_MODULE, |
98 | }; | |
99 | ||
65b4b4e8 | 100 | static int __init ipt_ah_init(void) |
1da177e4 | 101 | { |
6709dbbb | 102 | return xt_register_match(&ah_match); |
1da177e4 LT |
103 | } |
104 | ||
65b4b4e8 | 105 | static void __exit ipt_ah_fini(void) |
1da177e4 | 106 | { |
6709dbbb | 107 | xt_unregister_match(&ah_match); |
1da177e4 LT |
108 | } |
109 | ||
65b4b4e8 AM |
110 | module_init(ipt_ah_init); |
111 | module_exit(ipt_ah_fini); |