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