ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[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                  const void *protohdr,
35                  u_int16_t datalen,
36                  int *hotdrop)
37 {
38         const struct ip6t_ipv6header_info *info = matchinfo;
39         unsigned int temp;
40         int len;
41         u8 nexthdr;
42         unsigned int ptr;
43
44         /* Make sure this isn't an evil packet */
45
46         /* type of the 1st exthdr */
47         nexthdr = skb->nh.ipv6h->nexthdr;
48         /* pointer to the 1st exthdr */
49         ptr = sizeof(struct ipv6hdr);
50         /* available length */
51         len = skb->len - ptr;
52         temp = 0;
53
54         while (ip6t_ext_hdr(nexthdr)) {
55                 struct ipv6_opt_hdr *hdr;
56                 int hdrlen;
57
58                 /* Is there enough space for the next ext header? */
59                 if (len < (int)sizeof(struct ipv6_opt_hdr))
60                         return 0;
61                 /* No more exthdr -> evaluate */
62                 if (nexthdr == NEXTHDR_NONE) {
63                         temp |= MASK_NONE;
64                         break;
65                 }
66                 /* ESP -> evaluate */
67                 if (nexthdr == NEXTHDR_ESP) {
68                         temp |= MASK_ESP;
69                         break;
70                 }
71
72                 hdr=(struct ipv6_opt_hdr *)skb->data+ptr;
73
74                 /* Calculate the header length */
75                 if (nexthdr == NEXTHDR_FRAGMENT) {
76                         hdrlen = 8;
77                 } else if (nexthdr == NEXTHDR_AUTH)
78                         hdrlen = (hdr->hdrlen+2)<<2;
79                 else
80                         hdrlen = ipv6_optlen(hdr);
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 = hdr->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)
116                         ^ info->matchflags) ^ info->invflags);
117         else
118                 return (!( temp ^ info->matchflags) ^ info->invflags);
119 }
120
121 static int
122 ipv6header_checkentry(const char *tablename,
123                       const struct ip6t_ip6 *ip,
124                       void *matchinfo,
125                       unsigned int matchsize,
126                       unsigned int hook_mask)
127 {
128         /* Check for obvious errors */
129         /* This match is valid in all hooks! */
130         if (matchsize != IP6T_ALIGN(sizeof(struct ip6t_ipv6header_info)))
131                 return 0;
132
133         return 1;
134 }
135
136 static struct ip6t_match ip6t_ipv6header_match = {
137         .name           = "ipv6header",
138         .match          = &ipv6header_match,
139         .checkentry     = &ipv6header_checkentry,
140         .destroy        = NULL,
141         .me             = THIS_MODULE,
142 };
143
144 static int  __init ipv6header_init(void)
145 {
146         return ip6t_register_match(&ip6t_ipv6header_match);
147 }
148
149 static void __exit ipv6header_exit(void)
150 {
151         ip6t_unregister_match(&ip6t_ipv6header_match);
152 }
153
154 module_init(ipv6header_init);
155 module_exit(ipv6header_exit);
156