xref: /OK3568_Linux_fs/kernel/net/netfilter/ipset/ip_set_hash_ipmark.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (C) 2003-2013 Jozsef Kadlecsik <kadlec@netfilter.org> */
3 
4 /* Kernel module implementing an IP set type: the hash:ip,mark type */
5 
6 #include <linux/jhash.h>
7 #include <linux/module.h>
8 #include <linux/ip.h>
9 #include <linux/skbuff.h>
10 #include <linux/errno.h>
11 #include <linux/random.h>
12 #include <net/ip.h>
13 #include <net/ipv6.h>
14 #include <net/netlink.h>
15 #include <net/tcp.h>
16 
17 #include <linux/netfilter.h>
18 #include <linux/netfilter/ipset/pfxlen.h>
19 #include <linux/netfilter/ipset/ip_set.h>
20 #include <linux/netfilter/ipset/ip_set_hash.h>
21 
22 #define IPSET_TYPE_REV_MIN	0
23 /*				1	   Forceadd support */
24 #define IPSET_TYPE_REV_MAX	2	/* skbinfo support  */
25 
26 MODULE_LICENSE("GPL");
27 MODULE_AUTHOR("Vytas Dauksa <vytas.dauksa@smoothwall.net>");
28 IP_SET_MODULE_DESC("hash:ip,mark", IPSET_TYPE_REV_MIN, IPSET_TYPE_REV_MAX);
29 MODULE_ALIAS("ip_set_hash:ip,mark");
30 
31 /* Type specific function prefix */
32 #define HTYPE		hash_ipmark
33 #define IP_SET_HASH_WITH_MARKMASK
34 
35 /* IPv4 variant */
36 
37 /* Member elements */
38 struct hash_ipmark4_elem {
39 	__be32 ip;
40 	__u32 mark;
41 };
42 
43 /* Common functions */
44 
45 static bool
hash_ipmark4_data_equal(const struct hash_ipmark4_elem * ip1,const struct hash_ipmark4_elem * ip2,u32 * multi)46 hash_ipmark4_data_equal(const struct hash_ipmark4_elem *ip1,
47 			const struct hash_ipmark4_elem *ip2,
48 			u32 *multi)
49 {
50 	return ip1->ip == ip2->ip &&
51 	       ip1->mark == ip2->mark;
52 }
53 
54 static bool
hash_ipmark4_data_list(struct sk_buff * skb,const struct hash_ipmark4_elem * data)55 hash_ipmark4_data_list(struct sk_buff *skb,
56 		       const struct hash_ipmark4_elem *data)
57 {
58 	if (nla_put_ipaddr4(skb, IPSET_ATTR_IP, data->ip) ||
59 	    nla_put_net32(skb, IPSET_ATTR_MARK, htonl(data->mark)))
60 		goto nla_put_failure;
61 	return false;
62 
63 nla_put_failure:
64 	return true;
65 }
66 
67 static void
hash_ipmark4_data_next(struct hash_ipmark4_elem * next,const struct hash_ipmark4_elem * d)68 hash_ipmark4_data_next(struct hash_ipmark4_elem *next,
69 		       const struct hash_ipmark4_elem *d)
70 {
71 	next->ip = d->ip;
72 }
73 
74 #define MTYPE		hash_ipmark4
75 #define HOST_MASK	32
76 #include "ip_set_hash_gen.h"
77 
78 static int
hash_ipmark4_kadt(struct ip_set * set,const struct sk_buff * skb,const struct xt_action_param * par,enum ipset_adt adt,struct ip_set_adt_opt * opt)79 hash_ipmark4_kadt(struct ip_set *set, const struct sk_buff *skb,
80 		  const struct xt_action_param *par,
81 		  enum ipset_adt adt, struct ip_set_adt_opt *opt)
82 {
83 	const struct hash_ipmark4 *h = set->data;
84 	ipset_adtfn adtfn = set->variant->adt[adt];
85 	struct hash_ipmark4_elem e = { };
86 	struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set);
87 
88 	e.mark = skb->mark;
89 	e.mark &= h->markmask;
90 
91 	ip4addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &e.ip);
92 	return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags);
93 }
94 
95 static int
hash_ipmark4_uadt(struct ip_set * set,struct nlattr * tb[],enum ipset_adt adt,u32 * lineno,u32 flags,bool retried)96 hash_ipmark4_uadt(struct ip_set *set, struct nlattr *tb[],
97 		  enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
98 {
99 	const struct hash_ipmark4 *h = set->data;
100 	ipset_adtfn adtfn = set->variant->adt[adt];
101 	struct hash_ipmark4_elem e = { };
102 	struct ip_set_ext ext = IP_SET_INIT_UEXT(set);
103 	u32 ip, ip_to = 0;
104 	int ret;
105 
106 	if (tb[IPSET_ATTR_LINENO])
107 		*lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
108 
109 	if (unlikely(!tb[IPSET_ATTR_IP] ||
110 		     !ip_set_attr_netorder(tb, IPSET_ATTR_MARK)))
111 		return -IPSET_ERR_PROTOCOL;
112 
113 	ret = ip_set_get_ipaddr4(tb[IPSET_ATTR_IP], &e.ip);
114 	if (ret)
115 		return ret;
116 
117 	ret = ip_set_get_extensions(set, tb, &ext);
118 	if (ret)
119 		return ret;
120 
121 	e.mark = ntohl(nla_get_be32(tb[IPSET_ATTR_MARK]));
122 	e.mark &= h->markmask;
123 	if (e.mark == 0 && e.ip == 0)
124 		return -IPSET_ERR_HASH_ELEM;
125 
126 	if (adt == IPSET_TEST ||
127 	    !(tb[IPSET_ATTR_IP_TO] || tb[IPSET_ATTR_CIDR])) {
128 		ret = adtfn(set, &e, &ext, &ext, flags);
129 		return ip_set_eexist(ret, flags) ? 0 : ret;
130 	}
131 
132 	ip_to = ip = ntohl(e.ip);
133 	if (tb[IPSET_ATTR_IP_TO]) {
134 		ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP_TO], &ip_to);
135 		if (ret)
136 			return ret;
137 		if (ip > ip_to) {
138 			if (e.mark == 0 && ip_to == 0)
139 				return -IPSET_ERR_HASH_ELEM;
140 			swap(ip, ip_to);
141 		}
142 	} else if (tb[IPSET_ATTR_CIDR]) {
143 		u8 cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
144 
145 		if (!cidr || cidr > HOST_MASK)
146 			return -IPSET_ERR_INVALID_CIDR;
147 		ip_set_mask_from_to(ip, ip_to, cidr);
148 	}
149 
150 	if (((u64)ip_to - ip + 1) > IPSET_MAX_RANGE)
151 		return -ERANGE;
152 
153 	if (retried)
154 		ip = ntohl(h->next.ip);
155 	for (; ip <= ip_to; ip++) {
156 		e.ip = htonl(ip);
157 		ret = adtfn(set, &e, &ext, &ext, flags);
158 
159 		if (ret && !ip_set_eexist(ret, flags))
160 			return ret;
161 
162 		ret = 0;
163 	}
164 	return ret;
165 }
166 
167 /* IPv6 variant */
168 
169 struct hash_ipmark6_elem {
170 	union nf_inet_addr ip;
171 	__u32 mark;
172 };
173 
174 /* Common functions */
175 
176 static bool
hash_ipmark6_data_equal(const struct hash_ipmark6_elem * ip1,const struct hash_ipmark6_elem * ip2,u32 * multi)177 hash_ipmark6_data_equal(const struct hash_ipmark6_elem *ip1,
178 			const struct hash_ipmark6_elem *ip2,
179 			u32 *multi)
180 {
181 	return ipv6_addr_equal(&ip1->ip.in6, &ip2->ip.in6) &&
182 	       ip1->mark == ip2->mark;
183 }
184 
185 static bool
hash_ipmark6_data_list(struct sk_buff * skb,const struct hash_ipmark6_elem * data)186 hash_ipmark6_data_list(struct sk_buff *skb,
187 		       const struct hash_ipmark6_elem *data)
188 {
189 	if (nla_put_ipaddr6(skb, IPSET_ATTR_IP, &data->ip.in6) ||
190 	    nla_put_net32(skb, IPSET_ATTR_MARK, htonl(data->mark)))
191 		goto nla_put_failure;
192 	return false;
193 
194 nla_put_failure:
195 	return true;
196 }
197 
198 static void
hash_ipmark6_data_next(struct hash_ipmark6_elem * next,const struct hash_ipmark6_elem * d)199 hash_ipmark6_data_next(struct hash_ipmark6_elem *next,
200 		       const struct hash_ipmark6_elem *d)
201 {
202 }
203 
204 #undef MTYPE
205 #undef HOST_MASK
206 
207 #define MTYPE		hash_ipmark6
208 #define HOST_MASK	128
209 #define IP_SET_EMIT_CREATE
210 #include "ip_set_hash_gen.h"
211 
212 static int
hash_ipmark6_kadt(struct ip_set * set,const struct sk_buff * skb,const struct xt_action_param * par,enum ipset_adt adt,struct ip_set_adt_opt * opt)213 hash_ipmark6_kadt(struct ip_set *set, const struct sk_buff *skb,
214 		  const struct xt_action_param *par,
215 		  enum ipset_adt adt, struct ip_set_adt_opt *opt)
216 {
217 	const struct hash_ipmark6 *h = set->data;
218 	ipset_adtfn adtfn = set->variant->adt[adt];
219 	struct hash_ipmark6_elem e = { };
220 	struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set);
221 
222 	e.mark = skb->mark;
223 	e.mark &= h->markmask;
224 
225 	ip6addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &e.ip.in6);
226 	return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags);
227 }
228 
229 static int
hash_ipmark6_uadt(struct ip_set * set,struct nlattr * tb[],enum ipset_adt adt,u32 * lineno,u32 flags,bool retried)230 hash_ipmark6_uadt(struct ip_set *set, struct nlattr *tb[],
231 		  enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
232 {
233 	const struct hash_ipmark6 *h = set->data;
234 	ipset_adtfn adtfn = set->variant->adt[adt];
235 	struct hash_ipmark6_elem e = { };
236 	struct ip_set_ext ext = IP_SET_INIT_UEXT(set);
237 	int ret;
238 
239 	if (tb[IPSET_ATTR_LINENO])
240 		*lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
241 
242 	if (unlikely(!tb[IPSET_ATTR_IP] ||
243 		     !ip_set_attr_netorder(tb, IPSET_ATTR_MARK)))
244 		return -IPSET_ERR_PROTOCOL;
245 	if (unlikely(tb[IPSET_ATTR_IP_TO]))
246 		return -IPSET_ERR_HASH_RANGE_UNSUPPORTED;
247 	if (unlikely(tb[IPSET_ATTR_CIDR])) {
248 		u8 cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
249 
250 		if (cidr != HOST_MASK)
251 			return -IPSET_ERR_INVALID_CIDR;
252 	}
253 
254 	ret = ip_set_get_ipaddr6(tb[IPSET_ATTR_IP], &e.ip);
255 	if (ret)
256 		return ret;
257 
258 	ret = ip_set_get_extensions(set, tb, &ext);
259 	if (ret)
260 		return ret;
261 
262 	e.mark = ntohl(nla_get_be32(tb[IPSET_ATTR_MARK]));
263 	e.mark &= h->markmask;
264 
265 	if (adt == IPSET_TEST) {
266 		ret = adtfn(set, &e, &ext, &ext, flags);
267 		return ip_set_eexist(ret, flags) ? 0 : ret;
268 	}
269 
270 	ret = adtfn(set, &e, &ext, &ext, flags);
271 	if (ret && !ip_set_eexist(ret, flags))
272 		return ret;
273 
274 	return 0;
275 }
276 
277 static struct ip_set_type hash_ipmark_type __read_mostly = {
278 	.name		= "hash:ip,mark",
279 	.protocol	= IPSET_PROTOCOL,
280 	.features	= IPSET_TYPE_IP | IPSET_TYPE_MARK,
281 	.dimension	= IPSET_DIM_TWO,
282 	.family		= NFPROTO_UNSPEC,
283 	.revision_min	= IPSET_TYPE_REV_MIN,
284 	.revision_max	= IPSET_TYPE_REV_MAX,
285 	.create		= hash_ipmark_create,
286 	.create_policy	= {
287 		[IPSET_ATTR_MARKMASK]	= { .type = NLA_U32 },
288 		[IPSET_ATTR_HASHSIZE]	= { .type = NLA_U32 },
289 		[IPSET_ATTR_MAXELEM]	= { .type = NLA_U32 },
290 		[IPSET_ATTR_PROBES]	= { .type = NLA_U8 },
291 		[IPSET_ATTR_RESIZE]	= { .type = NLA_U8  },
292 		[IPSET_ATTR_TIMEOUT]	= { .type = NLA_U32 },
293 		[IPSET_ATTR_CADT_FLAGS]	= { .type = NLA_U32 },
294 	},
295 	.adt_policy	= {
296 		[IPSET_ATTR_IP]		= { .type = NLA_NESTED },
297 		[IPSET_ATTR_IP_TO]	= { .type = NLA_NESTED },
298 		[IPSET_ATTR_MARK]	= { .type = NLA_U32 },
299 		[IPSET_ATTR_CIDR]	= { .type = NLA_U8 },
300 		[IPSET_ATTR_TIMEOUT]	= { .type = NLA_U32 },
301 		[IPSET_ATTR_LINENO]	= { .type = NLA_U32 },
302 		[IPSET_ATTR_BYTES]	= { .type = NLA_U64 },
303 		[IPSET_ATTR_PACKETS]	= { .type = NLA_U64 },
304 		[IPSET_ATTR_COMMENT]	= { .type = NLA_NUL_STRING,
305 					    .len  = IPSET_MAX_COMMENT_SIZE },
306 		[IPSET_ATTR_SKBMARK]	= { .type = NLA_U64 },
307 		[IPSET_ATTR_SKBPRIO]	= { .type = NLA_U32 },
308 		[IPSET_ATTR_SKBQUEUE]	= { .type = NLA_U16 },
309 	},
310 	.me		= THIS_MODULE,
311 };
312 
313 static int __init
hash_ipmark_init(void)314 hash_ipmark_init(void)
315 {
316 	return ip_set_type_register(&hash_ipmark_type);
317 }
318 
319 static void __exit
hash_ipmark_fini(void)320 hash_ipmark_fini(void)
321 {
322 	rcu_barrier();
323 	ip_set_type_unregister(&hash_ipmark_type);
324 }
325 
326 module_init(hash_ipmark_init);
327 module_exit(hash_ipmark_fini);
328