xref: /linux/net/netfilter/ipset/ip_set_bitmap_ipmac.c (revision 2ed2e359dea752e7758d29a423033031a0b96584)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (C) 2000-2002 Joakim Axelsson <gozem@linux.nu>
3  *                         Patrick Schaaf <bof@bof.de>
4  *			   Martin Josefsson <gandalf@wlug.westbo.se>
5  */
6 
7 /* Kernel module implementing an IP set type: the bitmap:ip,mac type */
8 
9 #include <linux/module.h>
10 #include <linux/ip.h>
11 #include <linux/etherdevice.h>
12 #include <linux/skbuff.h>
13 #include <linux/errno.h>
14 #include <linux/if_arp.h>
15 #include <linux/if_ether.h>
16 #include <linux/netlink.h>
17 #include <linux/jiffies.h>
18 #include <linux/timer.h>
19 #include <net/netlink.h>
20 
21 #include <linux/netfilter/ipset/pfxlen.h>
22 #include <linux/netfilter/ipset/ip_set.h>
23 #include <linux/netfilter/ipset/ip_set_bitmap.h>
24 
25 #define IPSET_TYPE_REV_MIN	0
26 /*				1	   Counter support added */
27 /*				2	   Comment support added */
28 #define IPSET_TYPE_REV_MAX	3	/* skbinfo support added */
29 
30 MODULE_LICENSE("GPL");
31 MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@netfilter.org>");
32 IP_SET_MODULE_DESC("bitmap:ip,mac", IPSET_TYPE_REV_MIN, IPSET_TYPE_REV_MAX);
33 MODULE_ALIAS("ip_set_bitmap:ip,mac");
34 
35 #define MTYPE		bitmap_ipmac
36 #define HOST_MASK	32
37 #define IP_SET_BITMAP_STORED_TIMEOUT
38 
39 enum {
40 	MAC_UNSET,		/* element is set, without MAC */
41 	MAC_FILLED,		/* element is set with MAC */
42 };
43 
44 /* Type structure */
45 struct bitmap_ipmac {
46 	unsigned long *members;	/* the set members */
47 	u32 first_ip;		/* host byte order, included in range */
48 	u32 last_ip;		/* host byte order, included in range */
49 	u32 elements;		/* number of max elements in the set */
50 	size_t memsize;		/* members size */
51 	struct timer_list gc;	/* garbage collector */
52 	struct ip_set *set;	/* attached to this ip_set */
53 	unsigned char extensions[]	/* MAC + data extensions */
54 		__aligned(__alignof__(u64));
55 };
56 
57 /* ADT structure for generic function args */
58 struct bitmap_ipmac_adt_elem {
59 	unsigned char ether[ETH_ALEN] __aligned(2);
60 	u16 id;
61 	u16 add_mac;
62 };
63 
64 struct bitmap_ipmac_elem {
65 	unsigned char ether[ETH_ALEN];
66 	unsigned char filled;
67 } __aligned(__alignof__(u64));
68 
69 static u32
ip_to_id(const struct bitmap_ipmac * m,u32 ip)70 ip_to_id(const struct bitmap_ipmac *m, u32 ip)
71 {
72 	return ip - m->first_ip;
73 }
74 
75 #define get_elem(extensions, id, dsize)		\
76 	(struct bitmap_ipmac_elem *)(extensions + (id) * (dsize))
77 
78 #define get_const_elem(extensions, id, dsize)	\
79 	(const struct bitmap_ipmac_elem *)(extensions + (id) * (dsize))
80 
81 /* Common functions */
82 
83 static int
bitmap_ipmac_do_test(const struct bitmap_ipmac_adt_elem * e,const struct bitmap_ipmac * map,size_t dsize)84 bitmap_ipmac_do_test(const struct bitmap_ipmac_adt_elem *e,
85 		     const struct bitmap_ipmac *map, size_t dsize)
86 {
87 	const struct bitmap_ipmac_elem *elem;
88 
89 	if (!test_bit_acquire(e->id, map->members))
90 		return 0;
91 	elem = get_const_elem(map->extensions, e->id, dsize);
92 	if (e->add_mac && elem->filled == MAC_FILLED)
93 		return ether_addr_equal(e->ether, elem->ether);
94 	/* Trigger kernel to fill out the ethernet address */
95 	return -EAGAIN;
96 }
97 
98 static int
bitmap_ipmac_gc_test(u16 id,const struct bitmap_ipmac * map,size_t dsize)99 bitmap_ipmac_gc_test(u16 id, const struct bitmap_ipmac *map, size_t dsize)
100 {
101 	const struct bitmap_ipmac_elem *elem;
102 
103 	if (!test_bit(id, map->members))
104 		return 0;
105 	elem = get_const_elem(map->extensions, id, dsize);
106 	/* Timer not started for the incomplete elements */
107 	return elem->filled == MAC_FILLED;
108 }
109 
110 static int
bitmap_ipmac_is_filled(const struct bitmap_ipmac_elem * elem)111 bitmap_ipmac_is_filled(const struct bitmap_ipmac_elem *elem)
112 {
113 	return elem->filled == MAC_FILLED;
114 }
115 
116 static int
bitmap_ipmac_add_timeout(unsigned long * timeout,const struct bitmap_ipmac_adt_elem * e,const struct ip_set_ext * ext,struct ip_set * set,struct bitmap_ipmac * map,int mode)117 bitmap_ipmac_add_timeout(unsigned long *timeout,
118 			 const struct bitmap_ipmac_adt_elem *e,
119 			 const struct ip_set_ext *ext, struct ip_set *set,
120 			 struct bitmap_ipmac *map, int mode)
121 {
122 	u32 t = ext->timeout;
123 
124 	if (mode == IPSET_ADD_START_STORED_TIMEOUT) {
125 		if (t == set->timeout)
126 			/* Timeout was not specified, get stored one */
127 			t = *timeout;
128 		ip_set_timeout_set(timeout, t);
129 	} else {
130 		/* If MAC is unset yet, we store plain timeout value
131 		 * because the timer is not activated yet
132 		 * and we can reuse it later when MAC is filled out,
133 		 * possibly by the kernel
134 		 */
135 		if (e->add_mac)
136 			ip_set_timeout_set(timeout, t);
137 		else
138 			*timeout = t;
139 	}
140 	return 0;
141 }
142 
143 static int
bitmap_ipmac_do_add(const struct bitmap_ipmac_adt_elem * e,struct bitmap_ipmac * map,u32 flags,size_t dsize)144 bitmap_ipmac_do_add(const struct bitmap_ipmac_adt_elem *e,
145 		    struct bitmap_ipmac *map, u32 flags, size_t dsize)
146 {
147 	struct bitmap_ipmac_elem *elem;
148 
149 	elem = get_elem(map->extensions, e->id, dsize);
150 	if (test_bit(e->id, map->members)) {
151 		if (elem->filled == MAC_FILLED) {
152 			if (e->add_mac &&
153 			    (flags & IPSET_FLAG_EXIST) &&
154 			    !ether_addr_equal(e->ether, elem->ether)) {
155 				/* memcpy isn't atomic */
156 				clear_bit(e->id, map->members);
157 				smp_mb__after_atomic();
158 				ether_addr_copy(elem->ether, e->ether);
159 			}
160 			return IPSET_ADD_FAILED;
161 		} else if (!e->add_mac)
162 			/* Already added without ethernet address */
163 			return IPSET_ADD_FAILED;
164 		/* Fill the MAC address and trigger the timer activation */
165 		clear_bit(e->id, map->members);
166 		smp_mb__after_atomic();
167 		ether_addr_copy(elem->ether, e->ether);
168 		elem->filled = MAC_FILLED;
169 		return IPSET_ADD_START_STORED_TIMEOUT;
170 	} else if (e->add_mac) {
171 		/* We can store MAC too */
172 		ether_addr_copy(elem->ether, e->ether);
173 		elem->filled = MAC_FILLED;
174 		return 0;
175 	}
176 	elem->filled = MAC_UNSET;
177 	/* MAC is not stored yet, don't start timer */
178 	return IPSET_ADD_STORE_PLAIN_TIMEOUT;
179 }
180 
181 static int
bitmap_ipmac_do_del(const struct bitmap_ipmac_adt_elem * e,struct bitmap_ipmac * map)182 bitmap_ipmac_do_del(const struct bitmap_ipmac_adt_elem *e,
183 		    struct bitmap_ipmac *map)
184 {
185 	return !test_and_clear_bit(e->id, map->members);
186 }
187 
188 static int
bitmap_ipmac_do_list(struct sk_buff * skb,const struct bitmap_ipmac * map,u32 id,size_t dsize)189 bitmap_ipmac_do_list(struct sk_buff *skb, const struct bitmap_ipmac *map,
190 		     u32 id, size_t dsize)
191 {
192 	const struct bitmap_ipmac_elem *elem =
193 		get_const_elem(map->extensions, id, dsize);
194 
195 	return nla_put_ipaddr4(skb, IPSET_ATTR_IP,
196 			       htonl(map->first_ip + id)) ||
197 	       (elem->filled == MAC_FILLED &&
198 		nla_put(skb, IPSET_ATTR_ETHER, ETH_ALEN, elem->ether));
199 }
200 
201 static int
bitmap_ipmac_do_head(struct sk_buff * skb,const struct bitmap_ipmac * map)202 bitmap_ipmac_do_head(struct sk_buff *skb, const struct bitmap_ipmac *map)
203 {
204 	return nla_put_ipaddr4(skb, IPSET_ATTR_IP, htonl(map->first_ip)) ||
205 	       nla_put_ipaddr4(skb, IPSET_ATTR_IP_TO, htonl(map->last_ip));
206 }
207 
208 static int
bitmap_ipmac_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)209 bitmap_ipmac_kadt(struct ip_set *set, const struct sk_buff *skb,
210 		  const struct xt_action_param *par,
211 		  enum ipset_adt adt, struct ip_set_adt_opt *opt)
212 {
213 	struct bitmap_ipmac *map = set->data;
214 	ipset_adtfn adtfn = set->variant->adt[adt];
215 	struct bitmap_ipmac_adt_elem e = { .id = 0, .add_mac = 1 };
216 	struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set);
217 	u32 ip;
218 
219 	ip = ntohl(ip4addr(skb, opt->flags & IPSET_DIM_ONE_SRC));
220 	if (ip < map->first_ip || ip > map->last_ip)
221 		return -IPSET_ERR_BITMAP_RANGE;
222 
223 	/* Backward compatibility: we don't check the second flag */
224 	if (!skb->dev || skb->dev->type != ARPHRD_ETHER ||
225 	    !skb_mac_header_was_set(skb) || skb_mac_header_len(skb) < ETH_HLEN)
226 		return -EINVAL;
227 
228 	e.id = ip_to_id(map, ip);
229 
230 	if (opt->flags & IPSET_DIM_TWO_SRC)
231 		ether_addr_copy(e.ether, eth_hdr(skb)->h_source);
232 	else
233 		ether_addr_copy(e.ether, eth_hdr(skb)->h_dest);
234 
235 	if (is_zero_ether_addr(e.ether))
236 		return -EINVAL;
237 
238 	return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags);
239 }
240 
241 static int
bitmap_ipmac_uadt(struct ip_set * set,struct nlattr * tb[],enum ipset_adt adt,u32 * lineno,u32 flags,bool retried)242 bitmap_ipmac_uadt(struct ip_set *set, struct nlattr *tb[],
243 		  enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
244 {
245 	const struct bitmap_ipmac *map = set->data;
246 	ipset_adtfn adtfn = set->variant->adt[adt];
247 	struct bitmap_ipmac_adt_elem e = { .id = 0 };
248 	struct ip_set_ext ext = IP_SET_INIT_UEXT(set);
249 	u32 ip = 0;
250 	int ret = 0;
251 
252 	if (tb[IPSET_ATTR_LINENO])
253 		*lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
254 
255 	if (unlikely(!tb[IPSET_ATTR_IP]))
256 		return -IPSET_ERR_PROTOCOL;
257 
258 	ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP], &ip);
259 	if (ret)
260 		return ret;
261 
262 	ret = ip_set_get_extensions(set, tb, &ext);
263 	if (ret)
264 		return ret;
265 
266 	if (ip < map->first_ip || ip > map->last_ip)
267 		return -IPSET_ERR_BITMAP_RANGE;
268 
269 	e.id = ip_to_id(map, ip);
270 	if (tb[IPSET_ATTR_ETHER]) {
271 		if (nla_len(tb[IPSET_ATTR_ETHER]) != ETH_ALEN)
272 			return -IPSET_ERR_PROTOCOL;
273 		memcpy(e.ether, nla_data(tb[IPSET_ATTR_ETHER]), ETH_ALEN);
274 		e.add_mac = 1;
275 	}
276 	ret = adtfn(set, &e, &ext, &ext, flags);
277 
278 	return ip_set_eexist(ret, flags) ? 0 : ret;
279 }
280 
281 static bool
bitmap_ipmac_same_set(const struct ip_set * a,const struct ip_set * b)282 bitmap_ipmac_same_set(const struct ip_set *a, const struct ip_set *b)
283 {
284 	const struct bitmap_ipmac *x = a->data;
285 	const struct bitmap_ipmac *y = b->data;
286 
287 	return x->first_ip == y->first_ip &&
288 	       x->last_ip == y->last_ip &&
289 	       a->timeout == b->timeout &&
290 	       a->extensions == b->extensions;
291 }
292 
293 /* Plain variant */
294 
295 #include "ip_set_bitmap_gen.h"
296 
297 /* Create bitmap:ip,mac type of sets */
298 
299 static bool
init_map_ipmac(struct ip_set * set,struct bitmap_ipmac * map,u32 first_ip,u32 last_ip,u32 elements)300 init_map_ipmac(struct ip_set *set, struct bitmap_ipmac *map,
301 	       u32 first_ip, u32 last_ip, u32 elements)
302 {
303 	map->members = bitmap_zalloc(elements, GFP_KERNEL | __GFP_NOWARN);
304 	if (!map->members)
305 		return false;
306 	map->first_ip = first_ip;
307 	map->last_ip = last_ip;
308 	map->elements = elements;
309 	set->timeout = IPSET_NO_TIMEOUT;
310 
311 	map->set = set;
312 	set->data = map;
313 	set->family = NFPROTO_IPV4;
314 
315 	return true;
316 }
317 
318 static int
bitmap_ipmac_create(struct net * net,struct ip_set * set,struct nlattr * tb[],u32 flags)319 bitmap_ipmac_create(struct net *net, struct ip_set *set, struct nlattr *tb[],
320 		    u32 flags)
321 {
322 	u32 first_ip = 0, last_ip = 0;
323 	u64 elements;
324 	struct bitmap_ipmac *map;
325 	int ret;
326 
327 	if (unlikely(!tb[IPSET_ATTR_IP] ||
328 		     !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT) ||
329 		     !ip_set_optattr_netorder(tb, IPSET_ATTR_CADT_FLAGS)))
330 		return -IPSET_ERR_PROTOCOL;
331 
332 	ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP], &first_ip);
333 	if (ret)
334 		return ret;
335 
336 	if (tb[IPSET_ATTR_IP_TO]) {
337 		ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP_TO], &last_ip);
338 		if (ret)
339 			return ret;
340 		if (first_ip > last_ip)
341 			swap(first_ip, last_ip);
342 	} else if (tb[IPSET_ATTR_CIDR]) {
343 		u8 cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
344 
345 		if (cidr >= HOST_MASK)
346 			return -IPSET_ERR_INVALID_CIDR;
347 		ip_set_mask_from_to(first_ip, last_ip, cidr);
348 	} else {
349 		return -IPSET_ERR_PROTOCOL;
350 	}
351 
352 	elements = (u64)last_ip - first_ip + 1;
353 
354 	if (elements > IPSET_BITMAP_MAX_RANGE + 1)
355 		return -IPSET_ERR_BITMAP_RANGE_SIZE;
356 
357 	set->dsize = ip_set_elem_len(set, tb,
358 				     sizeof(struct bitmap_ipmac_elem),
359 				     __alignof__(struct bitmap_ipmac_elem));
360 	map = ip_set_alloc(sizeof(*map) + elements * set->dsize);
361 	if (!map)
362 		return -ENOMEM;
363 
364 	map->memsize = BITS_TO_LONGS(elements) * sizeof(unsigned long);
365 	set->variant = &bitmap_ipmac;
366 	if (!init_map_ipmac(set, map, first_ip, last_ip, elements)) {
367 		ip_set_free(map);
368 		return -ENOMEM;
369 	}
370 	if (tb[IPSET_ATTR_TIMEOUT]) {
371 		set->timeout = ip_set_timeout_uget(tb[IPSET_ATTR_TIMEOUT]);
372 		bitmap_ipmac_gc_init(set, bitmap_ipmac_gc);
373 	}
374 	return 0;
375 }
376 
377 static struct ip_set_type bitmap_ipmac_type = {
378 	.name		= "bitmap:ip,mac",
379 	.protocol	= IPSET_PROTOCOL,
380 	.features	= IPSET_TYPE_IP | IPSET_TYPE_MAC,
381 	.dimension	= IPSET_DIM_TWO,
382 	.family		= NFPROTO_IPV4,
383 	.revision_min	= IPSET_TYPE_REV_MIN,
384 	.revision_max	= IPSET_TYPE_REV_MAX,
385 	.create		= bitmap_ipmac_create,
386 	.create_policy	= {
387 		[IPSET_ATTR_IP]		= { .type = NLA_NESTED },
388 		[IPSET_ATTR_IP_TO]	= { .type = NLA_NESTED },
389 		[IPSET_ATTR_CIDR]	= { .type = NLA_U8 },
390 		[IPSET_ATTR_TIMEOUT]	= { .type = NLA_U32 },
391 		[IPSET_ATTR_CADT_FLAGS]	= { .type = NLA_U32 },
392 	},
393 	.adt_policy	= {
394 		[IPSET_ATTR_IP]		= { .type = NLA_NESTED },
395 		[IPSET_ATTR_ETHER]	= { .type = NLA_BINARY,
396 					    .len  = ETH_ALEN },
397 		[IPSET_ATTR_TIMEOUT]	= { .type = NLA_U32 },
398 		[IPSET_ATTR_LINENO]	= { .type = NLA_U32 },
399 		[IPSET_ATTR_BYTES]	= { .type = NLA_U64 },
400 		[IPSET_ATTR_PACKETS]	= { .type = NLA_U64 },
401 		[IPSET_ATTR_COMMENT]	= { .type = NLA_NUL_STRING,
402 					    .len  = IPSET_MAX_COMMENT_SIZE },
403 		[IPSET_ATTR_SKBMARK]	= { .type = NLA_U64 },
404 		[IPSET_ATTR_SKBPRIO]	= { .type = NLA_U32 },
405 		[IPSET_ATTR_SKBQUEUE]	= { .type = NLA_U16 },
406 	},
407 	.me		= THIS_MODULE,
408 };
409 
410 static int __init
bitmap_ipmac_init(void)411 bitmap_ipmac_init(void)
412 {
413 	return ip_set_type_register(&bitmap_ipmac_type);
414 }
415 
416 static void __exit
bitmap_ipmac_fini(void)417 bitmap_ipmac_fini(void)
418 {
419 	rcu_barrier();
420 	ip_set_type_unregister(&bitmap_ipmac_type);
421 }
422 
423 module_init(bitmap_ipmac_init);
424 module_exit(bitmap_ipmac_fini);
425