ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / net / ipv6 / netfilter / ip6t_esp.c
1 /* Kernel module to match ESP parameters. */
2 /* (C) 2001-2002 Andras Kis-Szabo <kisza@sch.bme.hu>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation.
7  */
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_esp.h>
19
20 MODULE_LICENSE("GPL");
21 MODULE_DESCRIPTION("IPv6 ESP 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("esp 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_esp_hdr *esp = NULL;
53         const struct ip6t_esp *espinfo = matchinfo;
54         unsigned int temp;
55         int len;
56         u8 nexthdr;
57         unsigned int ptr;
58
59         /* Make sure this isn't an evil packet */
60         /*DEBUGP("ipv6_esp entered \n");*/
61
62         /* type of the 1st exthdr */
63         nexthdr = skb->nh.ipv6h->nexthdr;
64         /* pointer to the 1st exthdr */
65         ptr = sizeof(struct ipv6hdr);
66         /* available length */
67         len = skb->len - ptr;
68         temp = 0;
69
70         while (ip6t_ext_hdr(nexthdr)) {
71                 struct ipv6_opt_hdr *hdr;
72                 int hdrlen;
73
74                 DEBUGP("ipv6_esp header iteration \n");
75
76                 /* Is there enough space for the next ext header? */
77                 if (len < (int)sizeof(struct ipv6_opt_hdr))
78                         return 0;
79                 /* No more exthdr -> evaluate */
80                 if (nexthdr == NEXTHDR_NONE) {
81                         break;
82                 }
83                 /* ESP -> evaluate */
84                 if (nexthdr == NEXTHDR_ESP) {
85                         temp |= MASK_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                 /* set the flag */
100                 switch (nexthdr){
101                         case NEXTHDR_HOP:
102                         case NEXTHDR_ROUTING:
103                         case NEXTHDR_FRAGMENT:
104                         case NEXTHDR_AUTH:
105                         case NEXTHDR_DEST:
106                                 break;
107                         default:
108                                 DEBUGP("ipv6_esp match: unknown nextheader %u\n",nexthdr);
109                                 return 0;
110                                 break;
111                 }
112
113                 nexthdr = hdr->nexthdr;
114                 len -= hdrlen;
115                 ptr += hdrlen;
116                 if ( ptr > skb->len ) {
117                         DEBUGP("ipv6_esp: new pointer too large! \n");
118                         break;
119                 }
120         }
121
122         /* ESP header not found */
123         if ( temp != MASK_ESP ) return 0;
124
125        if (len < (int)sizeof(struct ip_esp_hdr)){
126                *hotdrop = 1;
127                 return 0;
128        }
129
130         esp = (struct ip_esp_hdr *) (skb->data + ptr);
131
132         DEBUGP("IPv6 ESP SPI %u %08X\n", ntohl(esp->spi), ntohl(esp->spi));
133
134         return (esp != NULL)
135                 && spi_match(espinfo->spis[0], espinfo->spis[1],
136                               ntohl(esp->spi),
137                               !!(espinfo->invflags & IP6T_ESP_INV_SPI));
138 }
139
140 /* Called when user tries to insert an entry of this type. */
141 static int
142 checkentry(const char *tablename,
143            const struct ip6t_ip6 *ip,
144            void *matchinfo,
145            unsigned int matchinfosize,
146            unsigned int hook_mask)
147 {
148         const struct ip6t_esp *espinfo = matchinfo;
149
150         if (matchinfosize != IP6T_ALIGN(sizeof(struct ip6t_esp))) {
151                 DEBUGP("ip6t_esp: matchsize %u != %u\n",
152                          matchinfosize, IP6T_ALIGN(sizeof(struct ip6t_esp)));
153                 return 0;
154         }
155         if (espinfo->invflags & ~IP6T_ESP_INV_MASK) {
156                 DEBUGP("ip6t_esp: unknown flags %X\n",
157                          espinfo->invflags);
158                 return 0;
159         }
160
161         return 1;
162 }
163
164 static struct ip6t_match esp_match = {
165         .name           = "esp",
166         .match          = &match,
167         .checkentry     = &checkentry,
168         .me             = THIS_MODULE,
169 };
170
171 static int __init init(void)
172 {
173         return ip6t_register_match(&esp_match);
174 }
175
176 static void __exit cleanup(void)
177 {
178         ip6t_unregister_match(&esp_match);
179 }
180
181 module_init(init);
182 module_exit(cleanup);