Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tiwai/sound-2.6
[linux-ginger.git] / net / ipv6 / netfilter / ip6t_ah.c
blob3a82f24746b99f82af6bb8a8cb34741ad2d37211
1 /* Kernel module to match AH parameters. */
3 /* (C) 2001-2002 Andras Kis-Szabo <kisza@sch.bme.hu>
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 */
10 #include <linux/module.h>
11 #include <linux/skbuff.h>
12 #include <linux/ip.h>
13 #include <linux/ipv6.h>
14 #include <linux/types.h>
15 #include <net/checksum.h>
16 #include <net/ipv6.h>
18 #include <linux/netfilter/x_tables.h>
19 #include <linux/netfilter_ipv6/ip6_tables.h>
20 #include <linux/netfilter_ipv6/ip6t_ah.h>
22 MODULE_LICENSE("GPL");
23 MODULE_DESCRIPTION("Xtables: IPv6 IPsec-AH match");
24 MODULE_AUTHOR("Andras Kis-Szabo <kisza@sch.bme.hu>");
26 /* Returns 1 if the spi is matched by the range, 0 otherwise */
27 static inline bool
28 spi_match(u_int32_t min, u_int32_t max, u_int32_t spi, bool invert)
30 bool r;
32 pr_debug("ah spi_match:%c 0x%x <= 0x%x <= 0x%x",
33 invert ? '!' : ' ', min, spi, max);
34 r = (spi >= min && spi <= max) ^ invert;
35 pr_debug(" result %s\n", r ? "PASS" : "FAILED");
36 return r;
39 static bool ah_mt6(const struct sk_buff *skb, const struct xt_match_param *par)
41 struct ip_auth_hdr _ah;
42 const struct ip_auth_hdr *ah;
43 const struct ip6t_ah *ahinfo = par->matchinfo;
44 unsigned int ptr;
45 unsigned int hdrlen = 0;
46 int err;
48 err = ipv6_find_hdr(skb, &ptr, NEXTHDR_AUTH, NULL);
49 if (err < 0) {
50 if (err != -ENOENT)
51 *par->hotdrop = true;
52 return false;
55 ah = skb_header_pointer(skb, ptr, sizeof(_ah), &_ah);
56 if (ah == NULL) {
57 *par->hotdrop = true;
58 return false;
61 hdrlen = (ah->hdrlen + 2) << 2;
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));
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));
80 return (ah != NULL)
82 spi_match(ahinfo->spis[0], ahinfo->spis[1],
83 ntohl(ah->spi),
84 !!(ahinfo->invflags & IP6T_AH_INV_SPI))
86 (!ahinfo->hdrlen ||
87 (ahinfo->hdrlen == hdrlen) ^
88 !!(ahinfo->invflags & IP6T_AH_INV_LEN))
90 !(ahinfo->hdrres && ah->reserved);
93 static bool ah_mt6_check(const struct xt_mtchk_param *par)
95 const struct ip6t_ah *ahinfo = par->matchinfo;
97 if (ahinfo->invflags & ~IP6T_AH_INV_MASK) {
98 pr_debug("ip6t_ah: unknown flags %X\n", ahinfo->invflags);
99 return false;
101 return true;
104 static struct xt_match ah_mt6_reg __read_mostly = {
105 .name = "ah",
106 .family = NFPROTO_IPV6,
107 .match = ah_mt6,
108 .matchsize = sizeof(struct ip6t_ah),
109 .checkentry = ah_mt6_check,
110 .me = THIS_MODULE,
113 static int __init ah_mt6_init(void)
115 return xt_register_match(&ah_mt6_reg);
118 static void __exit ah_mt6_exit(void)
120 xt_unregister_match(&ah_mt6_reg);
123 module_init(ah_mt6_init);
124 module_exit(ah_mt6_exit);