xref: /linux/net/ipv6/netfilter/ip6t_rt.c (revision f2c53ea949c5048f96b3dbb5a5ee7131ce4ff2de)
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 	return (id >= min && id <= max) ^ invert;
29 }
30 
rt_mt6(const struct sk_buff * skb,struct xt_action_param * par)31 static bool rt_mt6(const struct sk_buff *skb, struct xt_action_param *par)
32 {
33 	struct ipv6_rt_hdr _route;
34 	const struct ipv6_rt_hdr *rh;
35 	const struct ip6t_rt *rtinfo = par->matchinfo;
36 	unsigned int temp;
37 	unsigned int ptr = 0;
38 	unsigned int hdrlen = 0;
39 	bool ret = false;
40 	struct in6_addr _addr;
41 	const struct in6_addr *ap;
42 	int err;
43 
44 	err = ipv6_find_hdr(skb, &ptr, NEXTHDR_ROUTING, NULL, NULL);
45 	if (err < 0) {
46 		if (err != -ENOENT)
47 			par->hotdrop = true;
48 		return false;
49 	}
50 
51 	rh = skb_header_pointer(skb, ptr, sizeof(_route), &_route);
52 	if (rh == NULL) {
53 		par->hotdrop = true;
54 		return false;
55 	}
56 
57 	hdrlen = ipv6_optlen(rh);
58 	if (skb->len - ptr < hdrlen) {
59 		/* Packet smaller than its length field */
60 		par->hotdrop = true;
61 		return false;
62 	}
63 
64 	ret = (segsleft_match(rtinfo->segsleft[0], rtinfo->segsleft[1],
65 			      rh->segments_left,
66 			      !!(rtinfo->invflags & IP6T_RT_INV_SGS))) &&
67 	      (!(rtinfo->flags & IP6T_RT_LEN) ||
68 	       ((rtinfo->hdrlen == hdrlen) ^
69 		!!(rtinfo->invflags & IP6T_RT_INV_LEN))) &&
70 	      (!(rtinfo->flags & IP6T_RT_TYP) ||
71 	       ((rtinfo->rt_type == rh->type) ^
72 		!!(rtinfo->invflags & IP6T_RT_INV_TYP)));
73 
74 	if (ret && (rtinfo->flags & IP6T_RT_RES)) {
75 		const u_int32_t *rp;
76 		u_int32_t _reserved;
77 		rp = skb_header_pointer(skb,
78 					ptr + offsetof(struct rt0_hdr,
79 						       reserved),
80 					sizeof(_reserved),
81 					&_reserved);
82 		if (!rp) {
83 			par->hotdrop = true;
84 			return false;
85 		}
86 
87 		ret = (*rp == 0);
88 	}
89 
90 	if (!(rtinfo->flags & IP6T_RT_FST)) {
91 		return ret;
92 	} else if (rtinfo->flags & IP6T_RT_FST_NSTRICT) {
93 		if (rtinfo->addrnr > (unsigned int)((hdrlen - 8) / 16)) {
94 			return false;
95 		} else {
96 			unsigned int i = 0;
97 
98 			for (temp = 0;
99 			     temp < (unsigned int)((hdrlen - 8) / 16) &&
100 			     i < rtinfo->addrnr;
101 			     temp++) {
102 				ap = skb_header_pointer(skb,
103 							ptr
104 							+ sizeof(struct rt0_hdr)
105 							+ temp * sizeof(_addr),
106 							sizeof(_addr),
107 							&_addr);
108 
109 				if (ap == NULL) {
110 					par->hotdrop = true;
111 					return false;
112 				}
113 
114 				if (ipv6_addr_equal(ap, &rtinfo->addrs[i]))
115 					i++;
116 			}
117 			if (i == rtinfo->addrnr)
118 				return ret;
119 			else
120 				return false;
121 		}
122 	} else {
123 		if (rtinfo->addrnr > (unsigned int)((hdrlen - 8) / 16)) {
124 			return false;
125 		} else {
126 			for (temp = 0; temp < rtinfo->addrnr; temp++) {
127 				ap = skb_header_pointer(skb,
128 							ptr
129 							+ sizeof(struct rt0_hdr)
130 							+ temp * sizeof(_addr),
131 							sizeof(_addr),
132 							&_addr);
133 				if (ap == NULL) {
134 					par->hotdrop = true;
135 					return false;
136 				}
137 
138 				if (!ipv6_addr_equal(ap, &rtinfo->addrs[temp]))
139 					break;
140 			}
141 			if (temp == rtinfo->addrnr &&
142 			    temp == (unsigned int)((hdrlen - 8) / 16))
143 				return ret;
144 			else
145 				return false;
146 		}
147 	}
148 
149 	return false;
150 }
151 
rt_mt6_check(const struct xt_mtchk_param * par)152 static int rt_mt6_check(const struct xt_mtchk_param *par)
153 {
154 	const struct ip6t_rt *rtinfo = par->matchinfo;
155 
156 	if (rtinfo->invflags & ~IP6T_RT_INV_MASK) {
157 		pr_info_ratelimited("unknown flags %X\n", rtinfo->invflags);
158 		return -EINVAL;
159 	}
160 	if (rtinfo->addrnr > IP6T_RT_HOPS) {
161 		pr_info_ratelimited("too many addresses specified\n");
162 		return -EINVAL;
163 	}
164 
165 	if ((rtinfo->flags & IP6T_RT_FST_MASK) && !rtinfo->addrnr) {
166 		pr_info_ratelimited("address list match requested but addrnr is 0\n");
167 		return -EINVAL;
168 	}
169 
170 	if ((rtinfo->flags & (IP6T_RT_RES | IP6T_RT_FST_MASK)) &&
171 	    (!(rtinfo->flags & IP6T_RT_TYP) ||
172 	     (rtinfo->rt_type != 0) ||
173 	     (rtinfo->invflags & IP6T_RT_INV_TYP))) {
174 		pr_info_ratelimited("`--rt-type 0' required before `--rt-0-*'\n");
175 		return -EINVAL;
176 	}
177 
178 	return 0;
179 }
180 
181 static struct xt_match rt_mt6_reg __read_mostly = {
182 	.name		= "rt",
183 	.family		= NFPROTO_IPV6,
184 	.match		= rt_mt6,
185 	.matchsize	= sizeof(struct ip6t_rt),
186 	.checkentry	= rt_mt6_check,
187 	.me		= THIS_MODULE,
188 };
189 
rt_mt6_init(void)190 static int __init rt_mt6_init(void)
191 {
192 	return xt_register_match(&rt_mt6_reg);
193 }
194 
rt_mt6_exit(void)195 static void __exit rt_mt6_exit(void)
196 {
197 	xt_unregister_match(&rt_mt6_reg);
198 }
199 
200 module_init(rt_mt6_init);
201 module_exit(rt_mt6_exit);
202