vserver 1.9.5.x5
[linux-2.6.git] / net / ipv6 / netfilter / ip6t_ipv6header.c
1 /* ipv6header match - matches IPv6 packets based
2    on whether they contain certain headers */
3
4 /* Original idea: Brad Chapman 
5  * Rewritten by: Andras Kis-Szabo <kisza@sch.bme.hu> */
6
7 /* (C) 2001-2002 Andras Kis-Szabo <kisza@sch.bme.hu>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License version 2 as
11  * published by the Free Software Foundation.
12  */
13
14 #include <linux/module.h>
15 #include <linux/skbuff.h>
16 #include <linux/ipv6.h>
17 #include <linux/types.h>
18 #include <net/checksum.h>
19 #include <net/ipv6.h>
20
21 #include <linux/netfilter_ipv6/ip6_tables.h>
22 #include <linux/netfilter_ipv6/ip6t_ipv6header.h>
23
24 MODULE_LICENSE("GPL");
25 MODULE_DESCRIPTION("IPv6 headers match");
26 MODULE_AUTHOR("Andras Kis-Szabo <kisza@sch.bme.hu>");
27
28 static int
29 ipv6header_match(const struct sk_buff *skb,
30                  const struct net_device *in,
31                  const struct net_device *out,
32                  const void *matchinfo,
33                  int offset,
34                  unsigned int protoff,
35                  int *hotdrop)
36 {
37         const struct ip6t_ipv6header_info *info = matchinfo;
38         unsigned int temp;
39         int len;
40         u8 nexthdr;
41         unsigned int ptr;
42
43         /* Make sure this isn't an evil packet */
44
45         /* type of the 1st exthdr */
46         nexthdr = skb->nh.ipv6h->nexthdr;
47         /* pointer to the 1st exthdr */
48         ptr = sizeof(struct ipv6hdr);
49         /* available length */
50         len = skb->len - ptr;
51         temp = 0;
52
53         while (ip6t_ext_hdr(nexthdr)) {
54                 struct ipv6_opt_hdr _hdr, *hp;
55                 int hdrlen;
56
57                 /* Is there enough space for the next ext header? */
58                 if (len < (int)sizeof(struct ipv6_opt_hdr))
59                         return 0;
60                 /* No more exthdr -> evaluate */
61                 if (nexthdr == NEXTHDR_NONE) {
62                         temp |= MASK_NONE;
63                         break;
64                 }
65                 /* ESP -> evaluate */
66                 if (nexthdr == NEXTHDR_ESP) {
67                         temp |= MASK_ESP;
68                         break;
69                 }
70
71                 hp = skb_header_pointer(skb, ptr, sizeof(_hdr), &_hdr);
72                 BUG_ON(hp == NULL);
73
74                 /* Calculate the header length */
75                 if (nexthdr == NEXTHDR_FRAGMENT) {
76                         hdrlen = 8;
77                 } else if (nexthdr == NEXTHDR_AUTH)
78                         hdrlen = (hp->hdrlen+2)<<2;
79                 else
80                         hdrlen = ipv6_optlen(hp);
81
82                 /* set the flag */
83                 switch (nexthdr){
84                         case NEXTHDR_HOP:
85                                 temp |= MASK_HOPOPTS;
86                                 break;
87                         case NEXTHDR_ROUTING:
88                                 temp |= MASK_ROUTING;
89                                 break;
90                         case NEXTHDR_FRAGMENT:
91                                 temp |= MASK_FRAGMENT;
92                                 break;
93                         case NEXTHDR_AUTH:
94                                 temp |= MASK_AH;
95                                 break;
96                         case NEXTHDR_DEST:
97                                 temp |= MASK_DSTOPTS;
98                                 break;
99                         default:
100                                 return 0;
101                                 break;
102                 }
103
104                 nexthdr = hp->nexthdr;
105                 len -= hdrlen;
106                 ptr += hdrlen;
107                 if (ptr > skb->len)
108                         break;
109         }
110
111         if ( (nexthdr != NEXTHDR_NONE ) && (nexthdr != NEXTHDR_ESP) )
112                 temp |= MASK_PROTO;
113
114         if (info->modeflag)
115                 return !((temp ^ info->matchflags ^ info->invflags)
116                          & info->matchflags);
117         else {
118                 if (info->invflags)
119                         return temp != info->matchflags;
120                 else
121                         return temp == info->matchflags;
122         }
123 }
124
125 static int
126 ipv6header_checkentry(const char *tablename,
127                       const struct ip6t_ip6 *ip,
128                       void *matchinfo,
129                       unsigned int matchsize,
130                       unsigned int hook_mask)
131 {
132         const struct ip6t_ipv6header_info *info = matchinfo;
133
134         /* Check for obvious errors */
135         /* This match is valid in all hooks! */
136         if (matchsize != IP6T_ALIGN(sizeof(struct ip6t_ipv6header_info)))
137                 return 0;
138
139         /* invflags is 0 or 0xff in hard mode */
140         if ((!info->modeflag) && info->invflags != 0x00
141                               && info->invflags != 0xFF)
142                 return 0;
143
144         return 1;
145 }
146
147 static struct ip6t_match ip6t_ipv6header_match = {
148         .name           = "ipv6header",
149         .match          = &ipv6header_match,
150         .checkentry     = &ipv6header_checkentry,
151         .destroy        = NULL,
152         .me             = THIS_MODULE,
153 };
154
155 static int  __init ipv6header_init(void)
156 {
157         return ip6t_register_match(&ip6t_ipv6header_match);
158 }
159
160 static void __exit ipv6header_exit(void)
161 {
162         ip6t_unregister_match(&ip6t_ipv6header_match);
163 }
164
165 module_init(ipv6header_init);
166 module_exit(ipv6header_exit);
167