intel-iommu: Tidy up iommu->gcmd handling
[linux-2.6] / net / ipv6 / netfilter / ip6t_rt.c
1 /* Kernel module to match ROUTING parameters. */
2
3 /* (C) 2001-2002 Andras Kis-Szabo <kisza@sch.bme.hu>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation.
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 <asm/byteorder.h>
18
19 #include <linux/netfilter/x_tables.h>
20 #include <linux/netfilter_ipv6/ip6_tables.h>
21 #include <linux/netfilter_ipv6/ip6t_rt.h>
22
23 MODULE_LICENSE("GPL");
24 MODULE_DESCRIPTION("Xtables: IPv6 Routing Header match");
25 MODULE_AUTHOR("Andras Kis-Szabo <kisza@sch.bme.hu>");
26
27 /* Returns 1 if the id is matched by the range, 0 otherwise */
28 static inline bool
29 segsleft_match(u_int32_t min, u_int32_t max, u_int32_t id, bool invert)
30 {
31         bool r;
32         pr_debug("rt segsleft_match:%c 0x%x <= 0x%x <= 0x%x",
33                  invert ? '!' : ' ', min, id, max);
34         r = (id >= min && id <= max) ^ invert;
35         pr_debug(" result %s\n", r ? "PASS" : "FAILED");
36         return r;
37 }
38
39 static bool rt_mt6(const struct sk_buff *skb, const struct xt_match_param *par)
40 {
41         struct ipv6_rt_hdr _route;
42         const struct ipv6_rt_hdr *rh;
43         const struct ip6t_rt *rtinfo = par->matchinfo;
44         unsigned int temp;
45         unsigned int ptr;
46         unsigned int hdrlen = 0;
47         bool ret = false;
48         struct in6_addr _addr;
49         const struct in6_addr *ap;
50         int err;
51
52         err = ipv6_find_hdr(skb, &ptr, NEXTHDR_ROUTING, NULL);
53         if (err < 0) {
54                 if (err != -ENOENT)
55                         *par->hotdrop = true;
56                 return false;
57         }
58
59         rh = skb_header_pointer(skb, ptr, sizeof(_route), &_route);
60         if (rh == NULL) {
61                 *par->hotdrop = true;
62                 return false;
63         }
64
65         hdrlen = ipv6_optlen(rh);
66         if (skb->len - ptr < hdrlen) {
67                 /* Pcket smaller than its length field */
68                 return false;
69         }
70
71         pr_debug("IPv6 RT LEN %u %u ", hdrlen, rh->hdrlen);
72         pr_debug("TYPE %04X ", rh->type);
73         pr_debug("SGS_LEFT %u %02X\n", rh->segments_left, rh->segments_left);
74
75         pr_debug("IPv6 RT segsleft %02X ",
76                  segsleft_match(rtinfo->segsleft[0], rtinfo->segsleft[1],
77                                 rh->segments_left,
78                                 !!(rtinfo->invflags & IP6T_RT_INV_SGS)));
79         pr_debug("type %02X %02X %02X ",
80                  rtinfo->rt_type, rh->type,
81                  (!(rtinfo->flags & IP6T_RT_TYP) ||
82                   ((rtinfo->rt_type == rh->type) ^
83                    !!(rtinfo->invflags & IP6T_RT_INV_TYP))));
84         pr_debug("len %02X %04X %02X ",
85                  rtinfo->hdrlen, hdrlen,
86                  !(rtinfo->flags & IP6T_RT_LEN) ||
87                   ((rtinfo->hdrlen == hdrlen) ^
88                    !!(rtinfo->invflags & IP6T_RT_INV_LEN)));
89         pr_debug("res %02X %02X %02X ",
90                  rtinfo->flags & IP6T_RT_RES,
91                  ((const struct rt0_hdr *)rh)->reserved,
92                  !((rtinfo->flags & IP6T_RT_RES) &&
93                    (((const struct rt0_hdr *)rh)->reserved)));
94
95         ret = (rh != NULL)
96               &&
97               (segsleft_match(rtinfo->segsleft[0], rtinfo->segsleft[1],
98                               rh->segments_left,
99                               !!(rtinfo->invflags & IP6T_RT_INV_SGS)))
100               &&
101               (!(rtinfo->flags & IP6T_RT_LEN) ||
102                ((rtinfo->hdrlen == hdrlen) ^
103                 !!(rtinfo->invflags & IP6T_RT_INV_LEN)))
104               &&
105               (!(rtinfo->flags & IP6T_RT_TYP) ||
106                ((rtinfo->rt_type == rh->type) ^
107                 !!(rtinfo->invflags & IP6T_RT_INV_TYP)));
108
109         if (ret && (rtinfo->flags & IP6T_RT_RES)) {
110                 const u_int32_t *rp;
111                 u_int32_t _reserved;
112                 rp = skb_header_pointer(skb,
113                                         ptr + offsetof(struct rt0_hdr,
114                                                        reserved),
115                                         sizeof(_reserved),
116                                         &_reserved);
117
118                 ret = (*rp == 0);
119         }
120
121         pr_debug("#%d ", rtinfo->addrnr);
122         if (!(rtinfo->flags & IP6T_RT_FST)) {
123                 return ret;
124         } else if (rtinfo->flags & IP6T_RT_FST_NSTRICT) {
125                 pr_debug("Not strict ");
126                 if (rtinfo->addrnr > (unsigned int)((hdrlen - 8) / 16)) {
127                         pr_debug("There isn't enough space\n");
128                         return false;
129                 } else {
130                         unsigned int i = 0;
131
132                         pr_debug("#%d ", rtinfo->addrnr);
133                         for (temp = 0;
134                              temp < (unsigned int)((hdrlen - 8) / 16);
135                              temp++) {
136                                 ap = skb_header_pointer(skb,
137                                                         ptr
138                                                         + sizeof(struct rt0_hdr)
139                                                         + temp * sizeof(_addr),
140                                                         sizeof(_addr),
141                                                         &_addr);
142
143                                 BUG_ON(ap == NULL);
144
145                                 if (ipv6_addr_equal(ap, &rtinfo->addrs[i])) {
146                                         pr_debug("i=%d temp=%d;\n", i, temp);
147                                         i++;
148                                 }
149                                 if (i == rtinfo->addrnr)
150                                         break;
151                         }
152                         pr_debug("i=%d #%d\n", i, rtinfo->addrnr);
153                         if (i == rtinfo->addrnr)
154                                 return ret;
155                         else
156                                 return false;
157                 }
158         } else {
159                 pr_debug("Strict ");
160                 if (rtinfo->addrnr > (unsigned int)((hdrlen - 8) / 16)) {
161                         pr_debug("There isn't enough space\n");
162                         return false;
163                 } else {
164                         pr_debug("#%d ", rtinfo->addrnr);
165                         for (temp = 0; temp < rtinfo->addrnr; temp++) {
166                                 ap = skb_header_pointer(skb,
167                                                         ptr
168                                                         + sizeof(struct rt0_hdr)
169                                                         + temp * sizeof(_addr),
170                                                         sizeof(_addr),
171                                                         &_addr);
172                                 BUG_ON(ap == NULL);
173
174                                 if (!ipv6_addr_equal(ap, &rtinfo->addrs[temp]))
175                                         break;
176                         }
177                         pr_debug("temp=%d #%d\n", temp, rtinfo->addrnr);
178                         if (temp == rtinfo->addrnr &&
179                             temp == (unsigned int)((hdrlen - 8) / 16))
180                                 return ret;
181                         else
182                                 return false;
183                 }
184         }
185
186         return false;
187 }
188
189 static bool rt_mt6_check(const struct xt_mtchk_param *par)
190 {
191         const struct ip6t_rt *rtinfo = par->matchinfo;
192
193         if (rtinfo->invflags & ~IP6T_RT_INV_MASK) {
194                 pr_debug("ip6t_rt: unknown flags %X\n", rtinfo->invflags);
195                 return false;
196         }
197         if ((rtinfo->flags & (IP6T_RT_RES | IP6T_RT_FST_MASK)) &&
198             (!(rtinfo->flags & IP6T_RT_TYP) ||
199              (rtinfo->rt_type != 0) ||
200              (rtinfo->invflags & IP6T_RT_INV_TYP))) {
201                 pr_debug("`--rt-type 0' required before `--rt-0-*'");
202                 return false;
203         }
204
205         return true;
206 }
207
208 static struct xt_match rt_mt6_reg __read_mostly = {
209         .name           = "rt",
210         .family         = NFPROTO_IPV6,
211         .match          = rt_mt6,
212         .matchsize      = sizeof(struct ip6t_rt),
213         .checkentry     = rt_mt6_check,
214         .me             = THIS_MODULE,
215 };
216
217 static int __init rt_mt6_init(void)
218 {
219         return xt_register_match(&rt_mt6_reg);
220 }
221
222 static void __exit rt_mt6_exit(void)
223 {
224         xt_unregister_match(&rt_mt6_reg);
225 }
226
227 module_init(rt_mt6_init);
228 module_exit(rt_mt6_exit);