]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* Kernel module to match AH parameters. */ |
2 | ||
3 | /* (C) 2001-2002 Andras Kis-Szabo <[email protected]> | |
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 | */ | |
ff67e4e4 | 9 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
1da177e4 LT |
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 <[email protected]>"); |
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 | 31 | |
ff67e4e4 | 32 | pr_debug("spi_match:%c 0x%x <= 0x%x <= 0x%x\n", |
0d53778e | 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 | ||
62fc8051 | 39 | static bool ah_mt6(const struct sk_buff *skb, struct xt_action_param *par) |
1da177e4 | 40 | { |
a47362a2 JE |
41 | struct ip_auth_hdr _ah; |
42 | const struct ip_auth_hdr *ah; | |
f7108a20 | 43 | const struct ip6t_ah *ahinfo = par->matchinfo; |
1da177e4 LT |
44 | unsigned int ptr; |
45 | unsigned int hdrlen = 0; | |
6d381634 | 46 | int err; |
1da177e4 | 47 | |
6d381634 PM |
48 | err = ipv6_find_hdr(skb, &ptr, NEXTHDR_AUTH, NULL); |
49 | if (err < 0) { | |
50 | if (err != -ENOENT) | |
b4ba2611 | 51 | par->hotdrop = true; |
1d93a9cb | 52 | return false; |
6d381634 | 53 | } |
1da177e4 | 54 | |
e674d0f3 YK |
55 | ah = skb_header_pointer(skb, ptr, sizeof(_ah), &_ah); |
56 | if (ah == NULL) { | |
b4ba2611 | 57 | par->hotdrop = true; |
1d93a9cb | 58 | return false; |
1da177e4 LT |
59 | } |
60 | ||
e674d0f3 | 61 | hdrlen = (ah->hdrlen + 2) << 2; |
1da177e4 | 62 | |
0d53778e PM |
63 | pr_debug("IPv6 AH LEN %u %u ", hdrlen, ah->hdrlen); |
64 | pr_debug("RES %04X ", ah->reserved); | |
65 | pr_debug("SPI %u %08X\n", ntohl(ah->spi), ntohl(ah->spi)); | |
66 | ||
67 | pr_debug("IPv6 AH spi %02X ", | |
68 | spi_match(ahinfo->spis[0], ahinfo->spis[1], | |
69 | ntohl(ah->spi), | |
70 | !!(ahinfo->invflags & IP6T_AH_INV_SPI))); | |
71 | pr_debug("len %02X %04X %02X ", | |
72 | ahinfo->hdrlen, hdrlen, | |
73 | (!ahinfo->hdrlen || | |
74 | (ahinfo->hdrlen == hdrlen) ^ | |
75 | !!(ahinfo->invflags & IP6T_AH_INV_LEN))); | |
76 | pr_debug("res %02X %04X %02X\n", | |
77 | ahinfo->hdrres, ah->reserved, | |
78 | !(ahinfo->hdrres && ah->reserved)); | |
1da177e4 | 79 | |
3666ed1c JP |
80 | return (ah != NULL) && |
81 | spi_match(ahinfo->spis[0], ahinfo->spis[1], | |
82 | ntohl(ah->spi), | |
83 | !!(ahinfo->invflags & IP6T_AH_INV_SPI)) && | |
84 | (!ahinfo->hdrlen || | |
85 | (ahinfo->hdrlen == hdrlen) ^ | |
86 | !!(ahinfo->invflags & IP6T_AH_INV_LEN)) && | |
87 | !(ahinfo->hdrres && ah->reserved); | |
1da177e4 LT |
88 | } |
89 | ||
b0f38452 | 90 | static int ah_mt6_check(const struct xt_mtchk_param *par) |
1da177e4 | 91 | { |
9b4fce7a | 92 | const struct ip6t_ah *ahinfo = par->matchinfo; |
1da177e4 | 93 | |
1da177e4 | 94 | if (ahinfo->invflags & ~IP6T_AH_INV_MASK) { |
ff67e4e4 | 95 | pr_debug("unknown flags %X\n", ahinfo->invflags); |
bd414ee6 | 96 | return -EINVAL; |
1da177e4 | 97 | } |
bd414ee6 | 98 | return 0; |
1da177e4 LT |
99 | } |
100 | ||
d3c5ee6d | 101 | static struct xt_match ah_mt6_reg __read_mostly = { |
1da177e4 | 102 | .name = "ah", |
ee999d8b | 103 | .family = NFPROTO_IPV6, |
d3c5ee6d | 104 | .match = ah_mt6, |
7f939713 | 105 | .matchsize = sizeof(struct ip6t_ah), |
d3c5ee6d | 106 | .checkentry = ah_mt6_check, |
1da177e4 LT |
107 | .me = THIS_MODULE, |
108 | }; | |
109 | ||
d3c5ee6d | 110 | static int __init ah_mt6_init(void) |
1da177e4 | 111 | { |
d3c5ee6d | 112 | return xt_register_match(&ah_mt6_reg); |
1da177e4 LT |
113 | } |
114 | ||
d3c5ee6d | 115 | static void __exit ah_mt6_exit(void) |
1da177e4 | 116 | { |
d3c5ee6d | 117 | xt_unregister_match(&ah_mt6_reg); |
1da177e4 LT |
118 | } |
119 | ||
d3c5ee6d JE |
120 | module_init(ah_mt6_init); |
121 | module_exit(ah_mt6_exit); |