1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Kernel module to match ROUTING parameters. */
3
4 /* (C) 2001-2002 Andras Kis-Szabo <kisza@sch.bme.hu>
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 /* Returns 1 if the id is matched by the range, 0 otherwise */
25 static inline bool
segsleft_match(u_int32_t min,u_int32_t max,u_int32_t id,bool invert)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
rt_mt6(const struct sk_buff * skb,struct xt_action_param * par)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 /* Pcket smaller than its length field */
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 = (segsleft_match(rtinfo->segsleft[0], rtinfo->segsleft[1],
93 rh->segments_left,
94 !!(rtinfo->invflags & IP6T_RT_INV_SGS))) &&
95 (!(rtinfo->flags & IP6T_RT_LEN) ||
96 ((rtinfo->hdrlen == hdrlen) ^
97 !!(rtinfo->invflags & IP6T_RT_INV_LEN))) &&
98 (!(rtinfo->flags & IP6T_RT_TYP) ||
99 ((rtinfo->rt_type == rh->type) ^
100 !!(rtinfo->invflags & IP6T_RT_INV_TYP)));
101
102 if (ret && (rtinfo->flags & IP6T_RT_RES)) {
103 const u_int32_t *rp;
104 u_int32_t _reserved;
105 rp = skb_header_pointer(skb,
106 ptr + offsetof(struct rt0_hdr,
107 reserved),
108 sizeof(_reserved),
109 &_reserved);
110
111 ret = (*rp == 0);
112 }
113
114 pr_debug("#%d ", rtinfo->addrnr);
115 if (!(rtinfo->flags & IP6T_RT_FST)) {
116 return ret;
117 } else if (rtinfo->flags & IP6T_RT_FST_NSTRICT) {
118 pr_debug("Not strict ");
119 if (rtinfo->addrnr > (unsigned int)((hdrlen - 8) / 16)) {
120 pr_debug("There isn't enough space\n");
121 return false;
122 } else {
123 unsigned int i = 0;
124
125 pr_debug("#%d ", rtinfo->addrnr);
126 for (temp = 0;
127 temp < (unsigned int)((hdrlen - 8) / 16);
128 temp++) {
129 ap = skb_header_pointer(skb,
130 ptr
131 + sizeof(struct rt0_hdr)
132 + temp * sizeof(_addr),
133 sizeof(_addr),
134 &_addr);
135
136 if (ap == NULL) {
137 par->hotdrop = true;
138 return false;
139 }
140
141 if (ipv6_addr_equal(ap, &rtinfo->addrs[i])) {
142 pr_debug("i=%d temp=%d;\n", i, temp);
143 i++;
144 }
145 if (i == rtinfo->addrnr)
146 break;
147 }
148 pr_debug("i=%d #%d\n", i, rtinfo->addrnr);
149 if (i == rtinfo->addrnr)
150 return ret;
151 else
152 return false;
153 }
154 } else {
155 pr_debug("Strict ");
156 if (rtinfo->addrnr > (unsigned int)((hdrlen - 8) / 16)) {
157 pr_debug("There isn't enough space\n");
158 return false;
159 } else {
160 pr_debug("#%d ", rtinfo->addrnr);
161 for (temp = 0; temp < rtinfo->addrnr; temp++) {
162 ap = skb_header_pointer(skb,
163 ptr
164 + sizeof(struct rt0_hdr)
165 + temp * sizeof(_addr),
166 sizeof(_addr),
167 &_addr);
168 if (ap == NULL) {
169 par->hotdrop = true;
170 return false;
171 }
172
173 if (!ipv6_addr_equal(ap, &rtinfo->addrs[temp]))
174 break;
175 }
176 pr_debug("temp=%d #%d\n", temp, rtinfo->addrnr);
177 if (temp == rtinfo->addrnr &&
178 temp == (unsigned int)((hdrlen - 8) / 16))
179 return ret;
180 else
181 return false;
182 }
183 }
184
185 return false;
186 }
187
rt_mt6_check(const struct xt_mtchk_param * par)188 static int rt_mt6_check(const struct xt_mtchk_param *par)
189 {
190 const struct ip6t_rt *rtinfo = par->matchinfo;
191
192 if (rtinfo->invflags & ~IP6T_RT_INV_MASK) {
193 pr_debug("unknown flags %X\n", rtinfo->invflags);
194 return -EINVAL;
195 }
196 if ((rtinfo->flags & (IP6T_RT_RES | IP6T_RT_FST_MASK)) &&
197 (!(rtinfo->flags & IP6T_RT_TYP) ||
198 (rtinfo->rt_type != 0) ||
199 (rtinfo->invflags & IP6T_RT_INV_TYP))) {
200 pr_debug("`--rt-type 0' required before `--rt-0-*'");
201 return -EINVAL;
202 }
203
204 return 0;
205 }
206
207 static struct xt_match rt_mt6_reg __read_mostly = {
208 .name = "rt",
209 .family = NFPROTO_IPV6,
210 .match = rt_mt6,
211 .matchsize = sizeof(struct ip6t_rt),
212 .checkentry = rt_mt6_check,
213 .me = THIS_MODULE,
214 };
215
rt_mt6_init(void)216 static int __init rt_mt6_init(void)
217 {
218 return xt_register_match(&rt_mt6_reg);
219 }
220
rt_mt6_exit(void)221 static void __exit rt_mt6_exit(void)
222 {
223 xt_unregister_match(&rt_mt6_reg);
224 }
225
226 module_init(rt_mt6_init);
227 module_exit(rt_mt6_exit);
228