]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* Kernel module to match AH parameters. */ |
2 | ||
3 | /* (C) 2001-2002 Andras Kis-Szabo <kisza@sch.bme.hu> | |
4 | * | |
5 | * This program is free software; you can redistribute it and/or modify | |
6 | * it under the terms of the GNU General Public License version 2 as | |
7 | * published by the Free Software Foundation. | |
8 | */ | |
9 | ||
10 | #include <linux/module.h> | |
11 | #include <linux/skbuff.h> | |
14c85021 | 12 | #include <linux/ip.h> |
1da177e4 LT |
13 | #include <linux/ipv6.h> |
14 | #include <linux/types.h> | |
15 | #include <net/checksum.h> | |
16 | #include <net/ipv6.h> | |
17 | ||
6709dbbb | 18 | #include <linux/netfilter/x_tables.h> |
1da177e4 LT |
19 | #include <linux/netfilter_ipv6/ip6_tables.h> |
20 | #include <linux/netfilter_ipv6/ip6t_ah.h> | |
21 | ||
22 | MODULE_LICENSE("GPL"); | |
2ae15b64 | 23 | MODULE_DESCRIPTION("Xtables: IPv6 IPsec-AH match"); |
1da177e4 LT |
24 | MODULE_AUTHOR("Andras Kis-Szabo <kisza@sch.bme.hu>"); |
25 | ||
1da177e4 | 26 | /* Returns 1 if the spi is matched by the range, 0 otherwise */ |
1d93a9cb JE |
27 | static inline bool |
28 | spi_match(u_int32_t min, u_int32_t max, u_int32_t spi, bool invert) | |
1da177e4 | 29 | { |
1d93a9cb | 30 | bool r; |
0d53778e PM |
31 | |
32 | pr_debug("ah spi_match:%c 0x%x <= 0x%x <= 0x%x", | |
33 | invert ? '!' : ' ', min, spi, max); | |
1da177e4 | 34 | r = (spi >= min && spi <= max) ^ invert; |
0d53778e | 35 | pr_debug(" result %s\n", r ? "PASS" : "FAILED"); |
1da177e4 LT |
36 | return r; |
37 | } | |
38 | ||
1d93a9cb | 39 | static bool |
d3c5ee6d JE |
40 | ah_mt6(const struct sk_buff *skb, const struct net_device *in, |
41 | const struct net_device *out, const struct xt_match *match, | |
42 | const void *matchinfo, int offset, unsigned int protoff, bool *hotdrop) | |
1da177e4 | 43 | { |
a47362a2 JE |
44 | struct ip_auth_hdr _ah; |
45 | const struct ip_auth_hdr *ah; | |
1da177e4 | 46 | const struct ip6t_ah *ahinfo = matchinfo; |
1da177e4 LT |
47 | unsigned int ptr; |
48 | unsigned int hdrlen = 0; | |
6d381634 | 49 | int err; |
1da177e4 | 50 | |
6d381634 PM |
51 | err = ipv6_find_hdr(skb, &ptr, NEXTHDR_AUTH, NULL); |
52 | if (err < 0) { | |
53 | if (err != -ENOENT) | |
cff533ac | 54 | *hotdrop = true; |
1d93a9cb | 55 | return false; |
6d381634 | 56 | } |
1da177e4 | 57 | |
e674d0f3 YK |
58 | ah = skb_header_pointer(skb, ptr, sizeof(_ah), &_ah); |
59 | if (ah == NULL) { | |
cff533ac | 60 | *hotdrop = true; |
1d93a9cb | 61 | return false; |
1da177e4 LT |
62 | } |
63 | ||
e674d0f3 | 64 | hdrlen = (ah->hdrlen + 2) << 2; |
1da177e4 | 65 | |
0d53778e PM |
66 | pr_debug("IPv6 AH LEN %u %u ", hdrlen, ah->hdrlen); |
67 | pr_debug("RES %04X ", ah->reserved); | |
68 | pr_debug("SPI %u %08X\n", ntohl(ah->spi), ntohl(ah->spi)); | |
69 | ||
70 | pr_debug("IPv6 AH spi %02X ", | |
71 | spi_match(ahinfo->spis[0], ahinfo->spis[1], | |
72 | ntohl(ah->spi), | |
73 | !!(ahinfo->invflags & IP6T_AH_INV_SPI))); | |
74 | pr_debug("len %02X %04X %02X ", | |
75 | ahinfo->hdrlen, hdrlen, | |
76 | (!ahinfo->hdrlen || | |
77 | (ahinfo->hdrlen == hdrlen) ^ | |
78 | !!(ahinfo->invflags & IP6T_AH_INV_LEN))); | |
79 | pr_debug("res %02X %04X %02X\n", | |
80 | ahinfo->hdrres, ah->reserved, | |
81 | !(ahinfo->hdrres && ah->reserved)); | |
1da177e4 LT |
82 | |
83 | return (ah != NULL) | |
84 | && | |
7c4e36bc JE |
85 | spi_match(ahinfo->spis[0], ahinfo->spis[1], |
86 | ntohl(ah->spi), | |
87 | !!(ahinfo->invflags & IP6T_AH_INV_SPI)) | |
1da177e4 LT |
88 | && |
89 | (!ahinfo->hdrlen || | |
1ab1457c YH |
90 | (ahinfo->hdrlen == hdrlen) ^ |
91 | !!(ahinfo->invflags & IP6T_AH_INV_LEN)) | |
1da177e4 LT |
92 | && |
93 | !(ahinfo->hdrres && ah->reserved); | |
94 | } | |
95 | ||
96 | /* Called when user tries to insert an entry of this type. */ | |
ccb79bdc | 97 | static bool |
d3c5ee6d JE |
98 | ah_mt6_check(const char *tablename, const void *entry, |
99 | const struct xt_match *match, void *matchinfo, | |
100 | unsigned int hook_mask) | |
1da177e4 LT |
101 | { |
102 | const struct ip6t_ah *ahinfo = matchinfo; | |
103 | ||
1da177e4 | 104 | if (ahinfo->invflags & ~IP6T_AH_INV_MASK) { |
0d53778e | 105 | pr_debug("ip6t_ah: unknown flags %X\n", ahinfo->invflags); |
ccb79bdc | 106 | return false; |
1da177e4 | 107 | } |
ccb79bdc | 108 | return true; |
1da177e4 LT |
109 | } |
110 | ||
d3c5ee6d | 111 | static struct xt_match ah_mt6_reg __read_mostly = { |
1da177e4 | 112 | .name = "ah", |
ee999d8b | 113 | .family = NFPROTO_IPV6, |
d3c5ee6d | 114 | .match = ah_mt6, |
7f939713 | 115 | .matchsize = sizeof(struct ip6t_ah), |
d3c5ee6d | 116 | .checkentry = ah_mt6_check, |
1da177e4 LT |
117 | .me = THIS_MODULE, |
118 | }; | |
119 | ||
d3c5ee6d | 120 | static int __init ah_mt6_init(void) |
1da177e4 | 121 | { |
d3c5ee6d | 122 | return xt_register_match(&ah_mt6_reg); |
1da177e4 LT |
123 | } |
124 | ||
d3c5ee6d | 125 | static void __exit ah_mt6_exit(void) |
1da177e4 | 126 | { |
d3c5ee6d | 127 | xt_unregister_match(&ah_mt6_reg); |
1da177e4 LT |
128 | } |
129 | ||
d3c5ee6d JE |
130 | module_init(ah_mt6_init); |
131 | module_exit(ah_mt6_exit); |