merge Linus head tree into my drm tree and fix up conflicts
[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/ipv6.h>
13 #include <linux/types.h>
14 #include <net/checksum.h>
15 #include <net/ipv6.h>
16
17 #include <linux/netfilter_ipv6/ip6_tables.h>
18 #include <linux/netfilter_ipv6/ip6t_ah.h>
19
20 MODULE_LICENSE("GPL");
21 MODULE_DESCRIPTION("IPv6 AH match");
22 MODULE_AUTHOR("Andras Kis-Szabo <kisza@sch.bme.hu>");
23
24 #if 0
25 #define DEBUGP printk
26 #else
27 #define DEBUGP(format, args...)
28 #endif
29
30 /* Returns 1 if the spi is matched by the range, 0 otherwise */
31 static inline int
32 spi_match(u_int32_t min, u_int32_t max, u_int32_t spi, int invert)
33 {
34         int r=0;
35         DEBUGP("ah spi_match:%c 0x%x <= 0x%x <= 0x%x",invert? '!':' ',
36                min,spi,max);
37         r = (spi >= min && spi <= max) ^ invert;
38         DEBUGP(" result %s\n",r? "PASS\n" : "FAILED\n");
39         return r;
40 }
41
42 static int
43 match(const struct sk_buff *skb,
44       const struct net_device *in,
45       const struct net_device *out,
46       const void *matchinfo,
47       int offset,
48       unsigned int protoff,
49       int *hotdrop)
50 {
51         struct ip_auth_hdr *ah, _ah;
52         const struct ip6t_ah *ahinfo = matchinfo;
53         unsigned int ptr;
54         unsigned int hdrlen = 0;
55
56         if (ipv6_find_hdr(skb, &ptr, NEXTHDR_AUTH) < 0)
57                 return 0;
58
59         ah = skb_header_pointer(skb, ptr, sizeof(_ah), &_ah);
60         if (ah == NULL) {
61                 *hotdrop = 1;
62                 return 0;
63         }
64
65         hdrlen = (ah->hdrlen + 2) << 2;
66
67         DEBUGP("IPv6 AH LEN %u %u ", hdrlen, ah->hdrlen);
68         DEBUGP("RES %04X ", ah->reserved);
69         DEBUGP("SPI %u %08X\n", ntohl(ah->spi), ntohl(ah->spi));
70
71         DEBUGP("IPv6 AH spi %02X ",
72                (spi_match(ahinfo->spis[0], ahinfo->spis[1],
73                           ntohl(ah->spi),
74                           !!(ahinfo->invflags & IP6T_AH_INV_SPI))));
75         DEBUGP("len %02X %04X %02X ",
76                ahinfo->hdrlen, hdrlen,
77                (!ahinfo->hdrlen ||
78                 (ahinfo->hdrlen == hdrlen) ^
79                 !!(ahinfo->invflags & IP6T_AH_INV_LEN)));
80         DEBUGP("res %02X %04X %02X\n",
81                ahinfo->hdrres, ah->reserved,
82                !(ahinfo->hdrres && ah->reserved));
83
84         return (ah != NULL)
85                &&
86                (spi_match(ahinfo->spis[0], ahinfo->spis[1],
87                           ntohl(ah->spi),
88                           !!(ahinfo->invflags & IP6T_AH_INV_SPI)))
89                &&
90                (!ahinfo->hdrlen ||
91                 (ahinfo->hdrlen == hdrlen) ^
92                 !!(ahinfo->invflags & IP6T_AH_INV_LEN))
93                &&
94                !(ahinfo->hdrres && ah->reserved);
95 }
96
97 /* Called when user tries to insert an entry of this type. */
98 static int
99 checkentry(const char *tablename,
100           const struct ip6t_ip6 *ip,
101           void *matchinfo,
102           unsigned int matchinfosize,
103           unsigned int hook_mask)
104 {
105         const struct ip6t_ah *ahinfo = matchinfo;
106
107         if (matchinfosize != IP6T_ALIGN(sizeof(struct ip6t_ah))) {
108                 DEBUGP("ip6t_ah: matchsize %u != %u\n",
109                        matchinfosize, IP6T_ALIGN(sizeof(struct ip6t_ah)));
110                 return 0;
111         }
112         if (ahinfo->invflags & ~IP6T_AH_INV_MASK) {
113                 DEBUGP("ip6t_ah: unknown flags %X\n", ahinfo->invflags);
114                 return 0;
115         }
116         return 1;
117 }
118
119 static struct ip6t_match ah_match = {
120         .name           = "ah",
121         .match          = &match,
122         .checkentry     = &checkentry,
123         .me             = THIS_MODULE,
124 };
125
126 static int __init init(void)
127 {
128         return ip6t_register_match(&ah_match);
129 }
130
131 static void __exit cleanup(void)
132 {
133         ip6t_unregister_match(&ah_match);
134 }
135
136 module_init(init);
137 module_exit(cleanup);