Pull dock-bay into release branch
[linux-2.6] / net / ipv6 / netfilter / ip6t_ah.c
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>
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>
17
18 #include <linux/netfilter/x_tables.h>
19 #include <linux/netfilter_ipv6/ip6_tables.h>
20 #include <linux/netfilter_ipv6/ip6t_ah.h>
21
22 MODULE_LICENSE("GPL");
23 MODULE_DESCRIPTION("IPv6 AH match");
24 MODULE_AUTHOR("Andras Kis-Szabo <kisza@sch.bme.hu>");
25
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)
29 {
30         bool r;
31
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;
37 }
38
39 static bool
40 match(const struct sk_buff *skb,
41       const struct net_device *in,
42       const struct net_device *out,
43       const struct xt_match *match,
44       const void *matchinfo,
45       int offset,
46       unsigned int protoff,
47       bool *hotdrop)
48 {
49         struct ip_auth_hdr _ah;
50         const struct ip_auth_hdr *ah;
51         const struct ip6t_ah *ahinfo = matchinfo;
52         unsigned int ptr;
53         unsigned int hdrlen = 0;
54         int err;
55
56         err = ipv6_find_hdr(skb, &ptr, NEXTHDR_AUTH, NULL);
57         if (err < 0) {
58                 if (err != -ENOENT)
59                         *hotdrop = true;
60                 return false;
61         }
62
63         ah = skb_header_pointer(skb, ptr, sizeof(_ah), &_ah);
64         if (ah == NULL) {
65                 *hotdrop = true;
66                 return false;
67         }
68
69         hdrlen = (ah->hdrlen + 2) << 2;
70
71         pr_debug("IPv6 AH LEN %u %u ", hdrlen, ah->hdrlen);
72         pr_debug("RES %04X ", ah->reserved);
73         pr_debug("SPI %u %08X\n", ntohl(ah->spi), ntohl(ah->spi));
74
75         pr_debug("IPv6 AH spi %02X ",
76                  spi_match(ahinfo->spis[0], ahinfo->spis[1],
77                            ntohl(ah->spi),
78                            !!(ahinfo->invflags & IP6T_AH_INV_SPI)));
79         pr_debug("len %02X %04X %02X ",
80                  ahinfo->hdrlen, hdrlen,
81                  (!ahinfo->hdrlen ||
82                   (ahinfo->hdrlen == hdrlen) ^
83                   !!(ahinfo->invflags & IP6T_AH_INV_LEN)));
84         pr_debug("res %02X %04X %02X\n",
85                  ahinfo->hdrres, ah->reserved,
86                  !(ahinfo->hdrres && ah->reserved));
87
88         return (ah != NULL)
89                &&
90                spi_match(ahinfo->spis[0], ahinfo->spis[1],
91                          ntohl(ah->spi),
92                          !!(ahinfo->invflags & IP6T_AH_INV_SPI))
93                &&
94                (!ahinfo->hdrlen ||
95                 (ahinfo->hdrlen == hdrlen) ^
96                 !!(ahinfo->invflags & IP6T_AH_INV_LEN))
97                &&
98                !(ahinfo->hdrres && ah->reserved);
99 }
100
101 /* Called when user tries to insert an entry of this type. */
102 static bool
103 checkentry(const char *tablename,
104           const void *entry,
105           const struct xt_match *match,
106           void *matchinfo,
107           unsigned int hook_mask)
108 {
109         const struct ip6t_ah *ahinfo = matchinfo;
110
111         if (ahinfo->invflags & ~IP6T_AH_INV_MASK) {
112                 pr_debug("ip6t_ah: unknown flags %X\n", ahinfo->invflags);
113                 return false;
114         }
115         return true;
116 }
117
118 static struct xt_match ah_match __read_mostly = {
119         .name           = "ah",
120         .family         = AF_INET6,
121         .match          = match,
122         .matchsize      = sizeof(struct ip6t_ah),
123         .checkentry     = checkentry,
124         .me             = THIS_MODULE,
125 };
126
127 static int __init ip6t_ah_init(void)
128 {
129         return xt_register_match(&ah_match);
130 }
131
132 static void __exit ip6t_ah_fini(void)
133 {
134         xt_unregister_match(&ah_match);
135 }
136
137 module_init(ip6t_ah_init);
138 module_exit(ip6t_ah_fini);