This source file includes following definitions.
- segsleft_match
- rt_mt6
- rt_mt6_check
- rt_mt6_init
- rt_mt6_exit
1
2
3
4
5
6 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
7 #include <linux/module.h>
8 #include <linux/skbuff.h>
9 #include <linux/ipv6.h>
10 #include <linux/types.h>
11 #include <net/checksum.h>
12 #include <net/ipv6.h>
13
14 #include <asm/byteorder.h>
15
16 #include <linux/netfilter/x_tables.h>
17 #include <linux/netfilter_ipv6/ip6_tables.h>
18 #include <linux/netfilter_ipv6/ip6t_rt.h>
19
20 MODULE_LICENSE("GPL");
21 MODULE_DESCRIPTION("Xtables: IPv6 Routing Header match");
22 MODULE_AUTHOR("Andras Kis-Szabo <kisza@sch.bme.hu>");
23
24
25 static inline bool
26 segsleft_match(u_int32_t min, u_int32_t max, u_int32_t id, bool invert)
27 {
28 bool r;
29 pr_debug("segsleft_match:%c 0x%x <= 0x%x <= 0x%x\n",
30 invert ? '!' : ' ', min, id, max);
31 r = (id >= min && id <= max) ^ invert;
32 pr_debug(" result %s\n", r ? "PASS" : "FAILED");
33 return r;
34 }
35
36 static bool rt_mt6(const struct sk_buff *skb, struct xt_action_param *par)
37 {
38 struct ipv6_rt_hdr _route;
39 const struct ipv6_rt_hdr *rh;
40 const struct ip6t_rt *rtinfo = par->matchinfo;
41 unsigned int temp;
42 unsigned int ptr = 0;
43 unsigned int hdrlen = 0;
44 bool ret = false;
45 struct in6_addr _addr;
46 const struct in6_addr *ap;
47 int err;
48
49 err = ipv6_find_hdr(skb, &ptr, NEXTHDR_ROUTING, NULL, NULL);
50 if (err < 0) {
51 if (err != -ENOENT)
52 par->hotdrop = true;
53 return false;
54 }
55
56 rh = skb_header_pointer(skb, ptr, sizeof(_route), &_route);
57 if (rh == NULL) {
58 par->hotdrop = true;
59 return false;
60 }
61
62 hdrlen = ipv6_optlen(rh);
63 if (skb->len - ptr < hdrlen) {
64
65 return false;
66 }
67
68 pr_debug("IPv6 RT LEN %u %u ", hdrlen, rh->hdrlen);
69 pr_debug("TYPE %04X ", rh->type);
70 pr_debug("SGS_LEFT %u %02X\n", rh->segments_left, rh->segments_left);
71
72 pr_debug("IPv6 RT segsleft %02X ",
73 segsleft_match(rtinfo->segsleft[0], rtinfo->segsleft[1],
74 rh->segments_left,
75 !!(rtinfo->invflags & IP6T_RT_INV_SGS)));
76 pr_debug("type %02X %02X %02X ",
77 rtinfo->rt_type, rh->type,
78 (!(rtinfo->flags & IP6T_RT_TYP) ||
79 ((rtinfo->rt_type == rh->type) ^
80 !!(rtinfo->invflags & IP6T_RT_INV_TYP))));
81 pr_debug("len %02X %04X %02X ",
82 rtinfo->hdrlen, hdrlen,
83 !(rtinfo->flags & IP6T_RT_LEN) ||
84 ((rtinfo->hdrlen == hdrlen) ^
85 !!(rtinfo->invflags & IP6T_RT_INV_LEN)));
86 pr_debug("res %02X %02X %02X ",
87 rtinfo->flags & IP6T_RT_RES,
88 ((const struct rt0_hdr *)rh)->reserved,
89 !((rtinfo->flags & IP6T_RT_RES) &&
90 (((const struct rt0_hdr *)rh)->reserved)));
91
92 ret = (rh != NULL) &&
93 (segsleft_match(rtinfo->segsleft[0], rtinfo->segsleft[1],
94 rh->segments_left,
95 !!(rtinfo->invflags & IP6T_RT_INV_SGS))) &&
96 (!(rtinfo->flags & IP6T_RT_LEN) ||
97 ((rtinfo->hdrlen == hdrlen) ^
98 !!(rtinfo->invflags & IP6T_RT_INV_LEN))) &&
99 (!(rtinfo->flags & IP6T_RT_TYP) ||
100 ((rtinfo->rt_type == rh->type) ^
101 !!(rtinfo->invflags & IP6T_RT_INV_TYP)));
102
103 if (ret && (rtinfo->flags & IP6T_RT_RES)) {
104 const u_int32_t *rp;
105 u_int32_t _reserved;
106 rp = skb_header_pointer(skb,
107 ptr + offsetof(struct rt0_hdr,
108 reserved),
109 sizeof(_reserved),
110 &_reserved);
111
112 ret = (*rp == 0);
113 }
114
115 pr_debug("#%d ", rtinfo->addrnr);
116 if (!(rtinfo->flags & IP6T_RT_FST)) {
117 return ret;
118 } else if (rtinfo->flags & IP6T_RT_FST_NSTRICT) {
119 pr_debug("Not strict ");
120 if (rtinfo->addrnr > (unsigned int)((hdrlen - 8) / 16)) {
121 pr_debug("There isn't enough space\n");
122 return false;
123 } else {
124 unsigned int i = 0;
125
126 pr_debug("#%d ", rtinfo->addrnr);
127 for (temp = 0;
128 temp < (unsigned int)((hdrlen - 8) / 16);
129 temp++) {
130 ap = skb_header_pointer(skb,
131 ptr
132 + sizeof(struct rt0_hdr)
133 + temp * sizeof(_addr),
134 sizeof(_addr),
135 &_addr);
136
137 if (ap == NULL) {
138 par->hotdrop = true;
139 return false;
140 }
141
142 if (ipv6_addr_equal(ap, &rtinfo->addrs[i])) {
143 pr_debug("i=%d temp=%d;\n", i, temp);
144 i++;
145 }
146 if (i == rtinfo->addrnr)
147 break;
148 }
149 pr_debug("i=%d #%d\n", i, rtinfo->addrnr);
150 if (i == rtinfo->addrnr)
151 return ret;
152 else
153 return false;
154 }
155 } else {
156 pr_debug("Strict ");
157 if (rtinfo->addrnr > (unsigned int)((hdrlen - 8) / 16)) {
158 pr_debug("There isn't enough space\n");
159 return false;
160 } else {
161 pr_debug("#%d ", rtinfo->addrnr);
162 for (temp = 0; temp < rtinfo->addrnr; temp++) {
163 ap = skb_header_pointer(skb,
164 ptr
165 + sizeof(struct rt0_hdr)
166 + temp * sizeof(_addr),
167 sizeof(_addr),
168 &_addr);
169 if (ap == NULL) {
170 par->hotdrop = true;
171 return false;
172 }
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 int 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("unknown flags %X\n", rtinfo->invflags);
195 return -EINVAL;
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 -EINVAL;
203 }
204
205 return 0;
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);