netfilter: ipset: Introduce RCU locking in list type
[deliverable/linux.git] / net / netfilter / ipset / ip_set_hash_ipmark.c
CommitLineData
3b02b56c
VD
1/* Copyright (C) 2003-2013 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
2 * Copyright (C) 2013 Smoothwall Ltd. <vytas.dauksa@smoothwall.net>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 */
8
9/* Kernel module implementing an IP set type: the hash:ip,mark type */
10
11#include <linux/jhash.h>
12#include <linux/module.h>
13#include <linux/ip.h>
14#include <linux/skbuff.h>
15#include <linux/errno.h>
16#include <linux/random.h>
17#include <net/ip.h>
18#include <net/ipv6.h>
19#include <net/netlink.h>
20#include <net/tcp.h>
21
22#include <linux/netfilter.h>
23#include <linux/netfilter/ipset/pfxlen.h>
24#include <linux/netfilter/ipset/ip_set.h>
25#include <linux/netfilter/ipset/ip_set_hash.h>
26
27#define IPSET_TYPE_REV_MIN 0
af331419
AD
28/* 1 Forceadd support */
29#define IPSET_TYPE_REV_MAX 2 /* skbinfo support */
3b02b56c
VD
30
31MODULE_LICENSE("GPL");
32MODULE_AUTHOR("Vytas Dauksa <vytas.dauksa@smoothwall.net>");
33IP_SET_MODULE_DESC("hash:ip,mark", IPSET_TYPE_REV_MIN, IPSET_TYPE_REV_MAX);
34MODULE_ALIAS("ip_set_hash:ip,mark");
35
36/* Type specific function prefix */
37#define HTYPE hash_ipmark
4d0e5c07 38#define IP_SET_HASH_WITH_MARKMASK
3b02b56c
VD
39
40/* IPv4 variant */
41
42/* Member elements */
43struct hash_ipmark4_elem {
44 __be32 ip;
45 __u32 mark;
46};
47
48/* Common functions */
49
50static inline bool
51hash_ipmark4_data_equal(const struct hash_ipmark4_elem *ip1,
52 const struct hash_ipmark4_elem *ip2,
53 u32 *multi)
54{
55 return ip1->ip == ip2->ip &&
56 ip1->mark == ip2->mark;
57}
58
59static bool
60hash_ipmark4_data_list(struct sk_buff *skb,
61 const struct hash_ipmark4_elem *data)
62{
63 if (nla_put_ipaddr4(skb, IPSET_ATTR_IP, data->ip) ||
64 nla_put_net32(skb, IPSET_ATTR_MARK, htonl(data->mark)))
65 goto nla_put_failure;
728a7e69 66 return false;
3b02b56c
VD
67
68nla_put_failure:
728a7e69 69 return true;
3b02b56c
VD
70}
71
72static inline void
73hash_ipmark4_data_next(struct hash_ipmark4_elem *next,
74 const struct hash_ipmark4_elem *d)
75{
76 next->ip = d->ip;
77}
78
1823fb79
SP
79#define MTYPE hash_ipmark4
80#define HOST_MASK 32
3b02b56c
VD
81#include "ip_set_hash_gen.h"
82
83static int
84hash_ipmark4_kadt(struct ip_set *set, const struct sk_buff *skb,
85 const struct xt_action_param *par,
86 enum ipset_adt adt, struct ip_set_adt_opt *opt)
87{
4d0e5c07 88 const struct hash_ipmark *h = set->data;
3b02b56c
VD
89 ipset_adtfn adtfn = set->variant->adt[adt];
90 struct hash_ipmark4_elem e = { };
91 struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set);
92
93 e.mark = skb->mark;
4d0e5c07 94 e.mark &= h->markmask;
3b02b56c
VD
95
96 ip4addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &e.ip);
97 return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags);
98}
99
100static int
101hash_ipmark4_uadt(struct ip_set *set, struct nlattr *tb[],
102 enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
103{
104 const struct hash_ipmark *h = set->data;
105 ipset_adtfn adtfn = set->variant->adt[adt];
106 struct hash_ipmark4_elem e = { };
107 struct ip_set_ext ext = IP_SET_INIT_UEXT(set);
108 u32 ip, ip_to = 0;
109 int ret;
110
a212e08e
SP
111 if (tb[IPSET_ATTR_LINENO])
112 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
113
3b02b56c 114 if (unlikely(!tb[IPSET_ATTR_IP] ||
7dd37bc8 115 !ip_set_attr_netorder(tb, IPSET_ATTR_MARK)))
3b02b56c
VD
116 return -IPSET_ERR_PROTOCOL;
117
8e55d2e5
SP
118 ret = ip_set_get_ipaddr4(tb[IPSET_ATTR_IP], &e.ip);
119 if (ret)
120 return ret;
121
122 ret = ip_set_get_extensions(set, tb, &ext);
3b02b56c
VD
123 if (ret)
124 return ret;
125
2006aa4a 126 e.mark = ntohl(nla_get_be32(tb[IPSET_ATTR_MARK]));
4d0e5c07 127 e.mark &= h->markmask;
3b02b56c
VD
128
129 if (adt == IPSET_TEST ||
130 !(tb[IPSET_ATTR_IP_TO] || tb[IPSET_ATTR_CIDR])) {
131 ret = adtfn(set, &e, &ext, &ext, flags);
132 return ip_set_eexist(ret, flags) ? 0 : ret;
133 }
134
135 ip_to = ip = ntohl(e.ip);
136 if (tb[IPSET_ATTR_IP_TO]) {
137 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP_TO], &ip_to);
138 if (ret)
139 return ret;
140 if (ip > ip_to)
141 swap(ip, ip_to);
142 } else if (tb[IPSET_ATTR_CIDR]) {
143 u8 cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
144
cabfd139 145 if (!cidr || cidr > HOST_MASK)
3b02b56c
VD
146 return -IPSET_ERR_INVALID_CIDR;
147 ip_set_mask_from_to(ip, ip_to, cidr);
148 }
149
150 if (retried)
151 ip = ntohl(h->next.ip);
152 for (; !before(ip_to, ip); ip++) {
153 e.ip = htonl(ip);
154 ret = adtfn(set, &e, &ext, &ext, flags);
155
156 if (ret && !ip_set_eexist(ret, flags))
157 return ret;
158 else
159 ret = 0;
160 }
161 return ret;
162}
163
164/* IPv6 variant */
165
166struct hash_ipmark6_elem {
167 union nf_inet_addr ip;
168 __u32 mark;
169};
170
171/* Common functions */
172
173static inline bool
174hash_ipmark6_data_equal(const struct hash_ipmark6_elem *ip1,
175 const struct hash_ipmark6_elem *ip2,
176 u32 *multi)
177{
178 return ipv6_addr_equal(&ip1->ip.in6, &ip2->ip.in6) &&
179 ip1->mark == ip2->mark;
180}
181
182static bool
183hash_ipmark6_data_list(struct sk_buff *skb,
184 const struct hash_ipmark6_elem *data)
185{
186 if (nla_put_ipaddr6(skb, IPSET_ATTR_IP, &data->ip.in6) ||
187 nla_put_net32(skb, IPSET_ATTR_MARK, htonl(data->mark)))
188 goto nla_put_failure;
728a7e69 189 return false;
3b02b56c
VD
190
191nla_put_failure:
728a7e69 192 return true;
3b02b56c
VD
193}
194
195static inline void
196hash_ipmark6_data_next(struct hash_ipmark4_elem *next,
197 const struct hash_ipmark6_elem *d)
198{
199}
200
201#undef MTYPE
3b02b56c 202#undef HOST_MASK
3b02b56c
VD
203
204#define MTYPE hash_ipmark6
3b02b56c 205#define HOST_MASK 128
1823fb79 206#define IP_SET_EMIT_CREATE
3b02b56c
VD
207#include "ip_set_hash_gen.h"
208
209
210static int
211hash_ipmark6_kadt(struct ip_set *set, const struct sk_buff *skb,
212 const struct xt_action_param *par,
213 enum ipset_adt adt, struct ip_set_adt_opt *opt)
214{
4d0e5c07 215 const struct hash_ipmark *h = set->data;
3b02b56c
VD
216 ipset_adtfn adtfn = set->variant->adt[adt];
217 struct hash_ipmark6_elem e = { };
218 struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set);
219
220 e.mark = skb->mark;
4d0e5c07 221 e.mark &= h->markmask;
3b02b56c
VD
222
223 ip6addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &e.ip.in6);
224 return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags);
225}
226
227static int
228hash_ipmark6_uadt(struct ip_set *set, struct nlattr *tb[],
229 enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
230{
4d0e5c07 231 const struct hash_ipmark *h = set->data;
3b02b56c
VD
232 ipset_adtfn adtfn = set->variant->adt[adt];
233 struct hash_ipmark6_elem e = { };
234 struct ip_set_ext ext = IP_SET_INIT_UEXT(set);
235 int ret;
236
a212e08e
SP
237 if (tb[IPSET_ATTR_LINENO])
238 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
239
3b02b56c 240 if (unlikely(!tb[IPSET_ATTR_IP] ||
2c227f27 241 !ip_set_attr_netorder(tb, IPSET_ATTR_MARK)))
3b02b56c 242 return -IPSET_ERR_PROTOCOL;
2c227f27
SP
243 if (unlikely(tb[IPSET_ATTR_IP_TO]))
244 return -IPSET_ERR_HASH_RANGE_UNSUPPORTED;
245 if (unlikely(tb[IPSET_ATTR_CIDR])) {
246 u8 cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
247
248 if (cidr != HOST_MASK)
249 return -IPSET_ERR_INVALID_CIDR;
250 }
3b02b56c 251
8e55d2e5
SP
252 ret = ip_set_get_ipaddr6(tb[IPSET_ATTR_IP], &e.ip);
253 if (ret)
254 return ret;
255
256 ret = ip_set_get_extensions(set, tb, &ext);
3b02b56c
VD
257 if (ret)
258 return ret;
259
2006aa4a 260 e.mark = ntohl(nla_get_be32(tb[IPSET_ATTR_MARK]));
4d0e5c07 261 e.mark &= h->markmask;
3b02b56c
VD
262
263 if (adt == IPSET_TEST) {
264 ret = adtfn(set, &e, &ext, &ext, flags);
265 return ip_set_eexist(ret, flags) ? 0 : ret;
266 }
267
268 ret = adtfn(set, &e, &ext, &ext, flags);
269 if (ret && !ip_set_eexist(ret, flags))
270 return ret;
271 else
272 ret = 0;
273
274 return ret;
275}
276
277static 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 = {
4d0e5c07 287 [IPSET_ATTR_MARKMASK] = { .type = NLA_U32 },
3b02b56c
VD
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 },
03726186
SP
304 [IPSET_ATTR_COMMENT] = { .type = NLA_NUL_STRING,
305 .len = IPSET_MAX_COMMENT_SIZE },
af331419
AD
306 [IPSET_ATTR_SKBMARK] = { .type = NLA_U64 },
307 [IPSET_ATTR_SKBPRIO] = { .type = NLA_U32 },
308 [IPSET_ATTR_SKBQUEUE] = { .type = NLA_U16 },
3b02b56c
VD
309 },
310 .me = THIS_MODULE,
311};
312
313static int __init
314hash_ipmark_init(void)
315{
316 return ip_set_type_register(&hash_ipmark_type);
317}
318
319static void __exit
320hash_ipmark_fini(void)
321{
18f84d41 322 rcu_barrier();
3b02b56c
VD
323 ip_set_type_unregister(&hash_ipmark_type);
324}
325
326module_init(hash_ipmark_init);
327module_exit(hash_ipmark_fini);
This page took 0.094465 seconds and 5 git commands to generate.