RT-AC66 3.0.0.4.374.130 core
[tomato.git] / release / src-rt-6.x / linux / linux-2.6 / net / ipv4 / netfilter / ip_set_portmap.c
blob8bb6e7602f957387c713e1adb569d0d5c5f169c8
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.
6 */
8 /* Kernel module implementing a port set type as a bitmap */
10 #include <linux/module.h>
11 #include <linux/ip.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>
20 #include <net/ip.h>
22 #include <linux/netfilter_ipv4/ip_set_portmap.h>
23 #include <linux/netfilter_ipv4/ip_set_getport.h>
25 static inline int
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)
31 return -ERANGE;
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) \
39 return 0;
41 UADT(portmap, test)
42 KADT(portmap, test, get_port)
44 static inline int
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)
50 return -ERANGE;
51 if (test_and_set_bit(port - map->first_ip, map->members))
52 return -EEXIST;
54 DP("set: %s, port %u", set->name, port);
55 return 0;
58 UADT(portmap, add)
59 KADT(portmap, add, get_port)
61 static inline int
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)
67 return -ERANGE;
68 if (!test_and_clear_bit(port - map->first_ip, map->members))
69 return -EEXIST;
71 DP("set: %s, port %u", set->name, port);
72 return 0;
75 UADT(portmap, del)
76 KADT(portmap, del, get_port)
78 static inline int
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);
85 return -ENOEXEC;
87 return bitmap_bytes(req->from, req->to);
90 BITMAP_CREATE(portmap)
91 BITMAP_DESTROY(portmap)
92 BITMAP_FLUSH(portmap)
94 static inline void
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))
104 static void
105 portmap_list_members(const struct ip_set *set, void *data, char dont_align)
107 const struct ip_set_portmap *map = set->data;
108 uint32_t i, n = 0;
109 ip_set_ip_t *d;
111 if (dont_align) {
112 memcpy(data, map->members, map->size);
113 return;
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;
120 n++;
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)