ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / 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       const void *protohdr,
49       u_int16_t datalen,
50       int *hotdrop)
51 {
52        struct ip_auth_hdr *ah = NULL;
53        const struct ip6t_ah *ahinfo = matchinfo;
54        unsigned int temp;
55        int len;
56        u8 nexthdr;
57        unsigned int ptr;
58        unsigned int hdrlen = 0;
59
60        /*DEBUGP("IPv6 AH entered\n");*/
61        /* if (opt->auth == 0) return 0;
62        * It does not filled on output */
63
64        /* type of the 1st exthdr */
65        nexthdr = skb->nh.ipv6h->nexthdr;
66        /* pointer to the 1st exthdr */
67        ptr = sizeof(struct ipv6hdr);
68        /* available length */
69        len = skb->len - ptr;
70        temp = 0;
71
72         while (ip6t_ext_hdr(nexthdr)) {
73                struct ipv6_opt_hdr *hdr;
74
75               DEBUGP("ipv6_ah header iteration \n");
76
77               /* Is there enough space for the next ext header? */
78                 if (len < (int)sizeof(struct ipv6_opt_hdr))
79                         return 0;
80               /* No more exthdr -> evaluate */
81                 if (nexthdr == NEXTHDR_NONE) {
82                      break;
83               }
84               /* ESP -> evaluate */
85                 if (nexthdr == NEXTHDR_ESP) {
86                      break;
87               }
88
89               hdr=(struct ipv6_opt_hdr *)skb->data+ptr;
90
91               /* Calculate the header length */
92                 if (nexthdr == NEXTHDR_FRAGMENT) {
93                         hdrlen = 8;
94                 } else if (nexthdr == NEXTHDR_AUTH)
95                         hdrlen = (hdr->hdrlen+2)<<2;
96                 else
97                         hdrlen = ipv6_optlen(hdr);
98
99               /* AH -> evaluate */
100                 if (nexthdr == NEXTHDR_AUTH) {
101                      temp |= MASK_AH;
102                      break;
103               }
104
105
106               /* set the flag */
107               switch (nexthdr){
108                      case NEXTHDR_HOP:
109                      case NEXTHDR_ROUTING:
110                      case NEXTHDR_FRAGMENT:
111                      case NEXTHDR_AUTH:
112                      case NEXTHDR_DEST:
113                             break;
114                      default:
115                             DEBUGP("ipv6_ah match: unknown nextheader %u\n",nexthdr);
116                             return 0;
117                             break;
118               }
119
120                 nexthdr = hdr->nexthdr;
121                 len -= hdrlen;
122                 ptr += hdrlen;
123                 if ( ptr > skb->len ) {
124                         DEBUGP("ipv6_ah: new pointer too large! \n");
125                         break;
126                 }
127         }
128
129        /* AH header not found */
130        if ( temp != MASK_AH ) return 0;
131
132        if (len < (int)sizeof(struct ip_auth_hdr)){
133                *hotdrop = 1;
134                 return 0;
135        }
136
137        ah = (struct ip_auth_hdr *) (skb->data + ptr);
138
139        DEBUGP("IPv6 AH LEN %u %u ", hdrlen, ah->hdrlen);
140        DEBUGP("RES %04X ", ah->reserved);
141        DEBUGP("SPI %u %08X\n", ntohl(ah->spi), ntohl(ah->spi));
142
143        DEBUGP("IPv6 AH spi %02X ",
144                 (spi_match(ahinfo->spis[0], ahinfo->spis[1],
145                            ntohl(ah->spi),
146                            !!(ahinfo->invflags & IP6T_AH_INV_SPI))));
147        DEBUGP("len %02X %04X %02X ",
148                 ahinfo->hdrlen, hdrlen,
149                 (!ahinfo->hdrlen ||
150                            (ahinfo->hdrlen == hdrlen) ^
151                            !!(ahinfo->invflags & IP6T_AH_INV_LEN)));
152        DEBUGP("res %02X %04X %02X\n", 
153                 ahinfo->hdrres, ah->reserved,
154                 !(ahinfo->hdrres && ah->reserved));
155
156        return (ah != NULL)
157               &&
158               (spi_match(ahinfo->spis[0], ahinfo->spis[1],
159                            ntohl(ah->spi),
160                            !!(ahinfo->invflags & IP6T_AH_INV_SPI)))
161               &&
162               (!ahinfo->hdrlen ||
163                            (ahinfo->hdrlen == hdrlen) ^
164                            !!(ahinfo->invflags & IP6T_AH_INV_LEN))
165               &&
166               !(ahinfo->hdrres && ah->reserved);
167 }
168
169 /* Called when user tries to insert an entry of this type. */
170 static int
171 checkentry(const char *tablename,
172           const struct ip6t_ip6 *ip,
173           void *matchinfo,
174           unsigned int matchinfosize,
175           unsigned int hook_mask)
176 {
177        const struct ip6t_ah *ahinfo = matchinfo;
178
179        if (matchinfosize != IP6T_ALIGN(sizeof(struct ip6t_ah))) {
180               DEBUGP("ip6t_ah: matchsize %u != %u\n",
181                       matchinfosize, IP6T_ALIGN(sizeof(struct ip6t_ah)));
182               return 0;
183        }
184        if (ahinfo->invflags & ~IP6T_AH_INV_MASK) {
185               DEBUGP("ip6t_ah: unknown flags %X\n",
186                       ahinfo->invflags);
187               return 0;
188        }
189
190        return 1;
191 }
192
193 static struct ip6t_match ah_match = {
194         .name           = "ah",
195         .match          = &match,
196         .checkentry     = &checkentry,
197         .me             = THIS_MODULE,
198 };
199
200 static int __init init(void)
201 {
202        return ip6t_register_match(&ah_match);
203 }
204
205 static void __exit cleanup(void)
206 {
207        ip6t_unregister_match(&ah_match);
208 }
209
210 module_init(init);
211 module_exit(cleanup);