1 /* Copyright (C) 2003-2008 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 as
5 * published by the Free Software Foundation.
8 /* Kernel module implementing a port set type as a bitmap */
10 #include <linux/module.h>
12 #include <linux/tcp.h>
13 #include <linux/udp.h>
14 #include <linux/skbuff.h>
15 #include <linux/errno.h>
16 #include <asm/uaccess.h>
17 #include <asm/bitops.h>
18 #include <linux/spinlock.h>
22 #include <linux/netfilter_ipv4/ip_set_portmap.h>
23 #include <linux/netfilter_ipv4/ip_set_getport.h>
26 portmap_test(const struct ip_set
*set
, ip_set_ip_t port
)
28 const struct ip_set_portmap
*map
= set
->data
;
30 if (port
< map
->first_ip
|| port
> map
->last_ip
)
33 DP("set: %s, port: %u", set
->name
, port
);
34 return !!test_bit(port
- map
->first_ip
, map
->members
);
37 #define KADT_CONDITION \
38 if (ip == INVALID_PORT) \
42 KADT(portmap
, test
, get_port
)
45 portmap_add(struct ip_set
*set
, ip_set_ip_t port
)
47 struct ip_set_portmap
*map
= set
->data
;
49 if (port
< map
->first_ip
|| port
> map
->last_ip
)
51 if (test_and_set_bit(port
- map
->first_ip
, map
->members
))
54 DP("set: %s, port %u", set
->name
, port
);
59 KADT(portmap
, add
, get_port
)
62 portmap_del(struct ip_set
*set
, ip_set_ip_t port
)
64 struct ip_set_portmap
*map
= set
->data
;
66 if (port
< map
->first_ip
|| port
> map
->last_ip
)
68 if (!test_and_clear_bit(port
- map
->first_ip
, map
->members
))
71 DP("set: %s, port %u", set
->name
, port
);
76 KADT(portmap
, del
, get_port
)
79 __portmap_create(const struct ip_set_req_portmap_create
*req
,
80 struct ip_set_portmap
*map
)
82 if (req
->to
- req
->from
> MAX_RANGE
) {
83 ip_set_printk("range too big, %d elements (max %d)",
84 req
->to
- req
->from
+ 1, MAX_RANGE
+1);
87 return bitmap_bytes(req
->from
, req
->to
);
90 BITMAP_CREATE(portmap
)
91 BITMAP_DESTROY(portmap
)
95 __portmap_list_header(const struct ip_set_portmap
*map
,
96 struct ip_set_req_portmap_create
*header
)
100 BITMAP_LIST_HEADER(portmap
)
101 BITMAP_LIST_MEMBERS_SIZE(portmap
, ip_set_ip_t
, (map
->last_ip
- map
->first_ip
+ 1),
102 test_bit(i
, map
->members
))
105 portmap_list_members(const struct ip_set
*set
, void *data
, char dont_align
)
107 const struct ip_set_portmap
*map
= set
->data
;
112 memcpy(data
, map
->members
, map
->size
);
116 for (i
= 0; i
< map
->last_ip
- map
->first_ip
+ 1; i
++)
117 if (test_bit(i
, map
->members
)) {
118 d
= data
+ n
* IPSET_ALIGN(sizeof(ip_set_ip_t
));
119 *d
= map
->first_ip
+ i
;
124 IP_SET_TYPE(portmap
, IPSET_TYPE_PORT
| IPSET_DATA_SINGLE
)
126 MODULE_LICENSE("GPL");
127 MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
128 MODULE_DESCRIPTION("portmap type of IP sets");
130 REGISTER_MODULE(portmap
)