[WATCHDOG] Merge code clean-up's from Alan Cox.
[linux-2.6] / 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/x_tables.h>
22 #include <linux/netfilter_ipv6/ip6_tables.h>
23 #include <linux/netfilter_ipv6/ip6t_ipv6header.h>
24
25 MODULE_LICENSE("GPL");
26 MODULE_DESCRIPTION("Xtables: IPv6 header types match");
27 MODULE_AUTHOR("Andras Kis-Szabo <kisza@sch.bme.hu>");
28
29 static bool
30 ipv6header_mt6(const struct sk_buff *skb, const struct net_device *in,
31                const struct net_device *out, const struct xt_match *match,
32                const void *matchinfo, int offset, unsigned int protoff,
33                bool *hotdrop)
34 {
35         const struct ip6t_ipv6header_info *info = matchinfo;
36         unsigned int temp;
37         int len;
38         u8 nexthdr;
39         unsigned int ptr;
40
41         /* Make sure this isn't an evil packet */
42
43         /* type of the 1st exthdr */
44         nexthdr = ipv6_hdr(skb)->nexthdr;
45         /* pointer to the 1st exthdr */
46         ptr = sizeof(struct ipv6hdr);
47         /* available length */
48         len = skb->len - ptr;
49         temp = 0;
50
51         while (ip6t_ext_hdr(nexthdr)) {
52                 const struct ipv6_opt_hdr *hp;
53                 struct ipv6_opt_hdr _hdr;
54                 int hdrlen;
55
56                 /* Is there enough space for the next ext header? */
57                 if (len < (int)sizeof(struct ipv6_opt_hdr))
58                         return false;
59                 /* No more exthdr -> evaluate */
60                 if (nexthdr == NEXTHDR_NONE) {
61                         temp |= MASK_NONE;
62                         break;
63                 }
64                 /* ESP -> evaluate */
65                 if (nexthdr == NEXTHDR_ESP) {
66                         temp |= MASK_ESP;
67                         break;
68                 }
69
70                 hp = skb_header_pointer(skb, ptr, sizeof(_hdr), &_hdr);
71                 BUG_ON(hp == NULL);
72
73                 /* Calculate the header length */
74                 if (nexthdr == NEXTHDR_FRAGMENT)
75                         hdrlen = 8;
76                 else if (nexthdr == NEXTHDR_AUTH)
77                         hdrlen = (hp->hdrlen + 2) << 2;
78                 else
79                         hdrlen = ipv6_optlen(hp);
80
81                 /* set the flag */
82                 switch (nexthdr) {
83                 case NEXTHDR_HOP:
84                         temp |= MASK_HOPOPTS;
85                         break;
86                 case NEXTHDR_ROUTING:
87                         temp |= MASK_ROUTING;
88                         break;
89                 case NEXTHDR_FRAGMENT:
90                         temp |= MASK_FRAGMENT;
91                         break;
92                 case NEXTHDR_AUTH:
93                         temp |= MASK_AH;
94                         break;
95                 case NEXTHDR_DEST:
96                         temp |= MASK_DSTOPTS;
97                         break;
98                 default:
99                         return false;
100                         break;
101                 }
102
103                 nexthdr = hp->nexthdr;
104                 len -= hdrlen;
105                 ptr += hdrlen;
106                 if (ptr > skb->len)
107                         break;
108         }
109
110         if (nexthdr != NEXTHDR_NONE && nexthdr != NEXTHDR_ESP)
111                 temp |= MASK_PROTO;
112
113         if (info->modeflag)
114                 return !((temp ^ info->matchflags ^ info->invflags)
115                          & info->matchflags);
116         else {
117                 if (info->invflags)
118                         return temp != info->matchflags;
119                 else
120                         return temp == info->matchflags;
121         }
122 }
123
124 static bool
125 ipv6header_mt6_check(const char *tablename, const void *ip,
126                      const struct xt_match *match, void *matchinfo,
127                      unsigned int hook_mask)
128 {
129         const struct ip6t_ipv6header_info *info = matchinfo;
130
131         /* invflags is 0 or 0xff in hard mode */
132         if ((!info->modeflag) && info->invflags != 0x00 &&
133             info->invflags != 0xFF)
134                 return false;
135
136         return true;
137 }
138
139 static struct xt_match ipv6header_mt6_reg __read_mostly = {
140         .name           = "ipv6header",
141         .family         = AF_INET6,
142         .match          = ipv6header_mt6,
143         .matchsize      = sizeof(struct ip6t_ipv6header_info),
144         .checkentry     = ipv6header_mt6_check,
145         .destroy        = NULL,
146         .me             = THIS_MODULE,
147 };
148
149 static int __init ipv6header_mt6_init(void)
150 {
151         return xt_register_match(&ipv6header_mt6_reg);
152 }
153
154 static void __exit ipv6header_mt6_exit(void)
155 {
156         xt_unregister_match(&ipv6header_mt6_reg);
157 }
158
159 module_init(ipv6header_mt6_init);
160 module_exit(ipv6header_mt6_exit);