original comment: +Wilson03172004,marked due to this pci host does not support MWI
[linux-2.4.git] / net / ipv6 / netfilter / ip6t_esp.c
1 /* Kernel module to match ESP parameters. */
2 #include <linux/module.h>
3 #include <linux/skbuff.h>
4 #include <linux/ipv6.h>
5 #include <linux/types.h>
6 #include <net/checksum.h>
7 #include <net/ipv6.h>
8
9 #include <linux/netfilter_ipv6/ip6_tables.h>
10 #include <linux/netfilter_ipv6/ip6t_esp.h>
11
12 EXPORT_NO_SYMBOLS;
13 MODULE_LICENSE("GPL");
14 MODULE_DESCRIPTION("IPv6 ESP match");
15 MODULE_AUTHOR("Andras Kis-Szabo <kisza@sch.bme.hu>");
16
17 #if 0
18 #define DEBUGP printk
19 #else
20 #define DEBUGP(format, args...)
21 #endif
22
23 struct esphdr {
24         __u32   spi;
25         __u32   seq_no;
26 };
27
28 /* Returns 1 if the spi is matched by the range, 0 otherwise */
29 static inline int
30 spi_match(u_int32_t min, u_int32_t max, u_int32_t spi, int invert)
31 {
32         int r=0;
33         DEBUGP("esp spi_match:%c 0x%x <= 0x%x <= 0x%x",invert? '!':' ',
34                 min,spi,max);
35         r=(spi >= min && spi <= max) ^ invert;
36         DEBUGP(" result %s\n",r? "PASS\n" : "FAILED\n");
37         return r;
38 }
39
40 static int
41 match(const struct sk_buff *skb,
42       const struct net_device *in,
43       const struct net_device *out,
44       const void *matchinfo,
45       int offset,
46       const void *protohdr,
47       u_int16_t datalen,
48       int *hotdrop)
49 {
50         struct esphdr *esp = NULL;
51         const struct ip6t_esp *espinfo = matchinfo;
52         unsigned int temp;
53         int len;
54         u8 nexthdr;
55         unsigned int ptr;
56
57         /* Make sure this isn't an evil packet */
58         /*DEBUGP("ipv6_esp entered \n");*/
59
60         /* type of the 1st exthdr */
61         nexthdr = skb->nh.ipv6h->nexthdr;
62         /* pointer to the 1st exthdr */
63         ptr = sizeof(struct ipv6hdr);
64         /* available length */
65         len = skb->len - ptr;
66         temp = 0;
67
68         while (ip6t_ext_hdr(nexthdr)) {
69                 struct ipv6_opt_hdr *hdr;
70                 int hdrlen;
71
72                 DEBUGP("ipv6_esp header iteration \n");
73
74                 /* Is there enough space for the next ext header? */
75                 if (len < (int)sizeof(struct ipv6_opt_hdr))
76                         return 0;
77                 /* No more exthdr -> evaluate */
78                 if (nexthdr == NEXTHDR_NONE) {
79                         break;
80                 }
81                 /* ESP -> evaluate */
82                 if (nexthdr == NEXTHDR_ESP) {
83                         temp |= MASK_ESP;
84                         break;
85                 }
86
87                 hdr=(struct ipv6_opt_hdr *)skb->data+ptr;
88
89                 /* Calculate the header length */
90                 if (nexthdr == NEXTHDR_FRAGMENT) {
91                         hdrlen = 8;
92                 } else if (nexthdr == NEXTHDR_AUTH)
93                         hdrlen = (hdr->hdrlen+2)<<2;
94                 else
95                         hdrlen = ipv6_optlen(hdr);
96
97                 /* set the flag */
98                 switch (nexthdr){
99                         case NEXTHDR_HOP:
100                         case NEXTHDR_ROUTING:
101                         case NEXTHDR_FRAGMENT:
102                         case NEXTHDR_AUTH:
103                         case NEXTHDR_DEST:
104                                 break;
105                         default:
106                                 DEBUGP("ipv6_esp match: unknown nextheader %u\n",nexthdr);
107                                 return 0;
108                                 break;
109                 }
110
111                 nexthdr = hdr->nexthdr;
112                 len -= hdrlen;
113                 ptr += hdrlen;
114                 if ( ptr > skb->len ) {
115                         DEBUGP("ipv6_esp: new pointer too large! \n");
116                         break;
117                 }
118         }
119
120         /* ESP header not found */
121         if ( temp != MASK_ESP ) return 0;
122
123        if (len < (int)sizeof(struct esphdr)){
124                *hotdrop = 1;
125                 return 0;
126        }
127
128         esp = (struct esphdr *) (skb->data + ptr);
129
130         DEBUGP("IPv6 ESP SPI %u %08X\n", ntohl(esp->spi), ntohl(esp->spi));
131
132         return (esp != NULL)
133                 && spi_match(espinfo->spis[0], espinfo->spis[1],
134                               ntohl(esp->spi),
135                               !!(espinfo->invflags & IP6T_ESP_INV_SPI));
136 }
137
138 /* Called when user tries to insert an entry of this type. */
139 static int
140 checkentry(const char *tablename,
141            const struct ip6t_ip6 *ip,
142            void *matchinfo,
143            unsigned int matchinfosize,
144            unsigned int hook_mask)
145 {
146         const struct ip6t_esp *espinfo = matchinfo;
147
148         if (matchinfosize != IP6T_ALIGN(sizeof(struct ip6t_esp))) {
149                 DEBUGP("ip6t_esp: matchsize %u != %u\n",
150                          matchinfosize, IP6T_ALIGN(sizeof(struct ip6t_esp)));
151                 return 0;
152         }
153         if (espinfo->invflags & ~IP6T_ESP_INV_MASK) {
154                 DEBUGP("ip6t_esp: unknown flags %X\n",
155                          espinfo->invflags);
156                 return 0;
157         }
158
159         return 1;
160 }
161
162 static struct ip6t_match esp_match
163 = { { NULL, NULL }, "esp", &match, &checkentry, NULL, THIS_MODULE };
164
165 static int __init init(void)
166 {
167         return ip6t_register_match(&esp_match);
168 }
169
170 static void __exit cleanup(void)
171 {
172         ip6t_unregister_match(&esp_match);
173 }
174
175 module_init(init);
176 module_exit(cleanup);