Merge tag 'edac_for_3.18' of git://git.kernel.org/pub/scm/linux/kernel/git/bp/bp
[cascardo/linux.git] / net / netfilter / ipset / ip_set_hash_ipmark.c
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
28 #define IPSET_TYPE_REV_MAX      1       /* Forceadd support */
29
30 MODULE_LICENSE("GPL");
31 MODULE_AUTHOR("Vytas Dauksa <vytas.dauksa@smoothwall.net>");
32 IP_SET_MODULE_DESC("hash:ip,mark", IPSET_TYPE_REV_MIN, IPSET_TYPE_REV_MAX);
33 MODULE_ALIAS("ip_set_hash:ip,mark");
34
35 /* Type specific function prefix */
36 #define HTYPE           hash_ipmark
37 #define IP_SET_HASH_WITH_MARKMASK
38
39 /* IPv4 variant */
40
41 /* Member elements */
42 struct hash_ipmark4_elem {
43         __be32 ip;
44         __u32 mark;
45 };
46
47 /* Common functions */
48
49 static inline bool
50 hash_ipmark4_data_equal(const struct hash_ipmark4_elem *ip1,
51                         const struct hash_ipmark4_elem *ip2,
52                         u32 *multi)
53 {
54         return ip1->ip == ip2->ip &&
55                ip1->mark == ip2->mark;
56 }
57
58 static bool
59 hash_ipmark4_data_list(struct sk_buff *skb,
60                        const struct hash_ipmark4_elem *data)
61 {
62         if (nla_put_ipaddr4(skb, IPSET_ATTR_IP, data->ip) ||
63             nla_put_net32(skb, IPSET_ATTR_MARK, htonl(data->mark)))
64                 goto nla_put_failure;
65         return 0;
66
67 nla_put_failure:
68         return 1;
69 }
70
71 static inline void
72 hash_ipmark4_data_next(struct hash_ipmark4_elem *next,
73                        const struct hash_ipmark4_elem *d)
74 {
75         next->ip = d->ip;
76 }
77
78 #define MTYPE           hash_ipmark4
79 #define PF              4
80 #define HOST_MASK       32
81 #define HKEY_DATALEN    sizeof(struct hash_ipmark4_elem)
82 #include "ip_set_hash_gen.h"
83
84 static int
85 hash_ipmark4_kadt(struct ip_set *set, const struct sk_buff *skb,
86                   const struct xt_action_param *par,
87                   enum ipset_adt adt, struct ip_set_adt_opt *opt)
88 {
89         const struct hash_ipmark *h = set->data;
90         ipset_adtfn adtfn = set->variant->adt[adt];
91         struct hash_ipmark4_elem e = { };
92         struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set);
93
94         e.mark = skb->mark;
95         e.mark &= h->markmask;
96
97         ip4addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &e.ip);
98         return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags);
99 }
100
101 static int
102 hash_ipmark4_uadt(struct ip_set *set, struct nlattr *tb[],
103                   enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
104 {
105         const struct hash_ipmark *h = set->data;
106         ipset_adtfn adtfn = set->variant->adt[adt];
107         struct hash_ipmark4_elem e = { };
108         struct ip_set_ext ext = IP_SET_INIT_UEXT(set);
109         u32 ip, ip_to = 0;
110         int ret;
111
112         if (unlikely(!tb[IPSET_ATTR_IP] ||
113                      !ip_set_attr_netorder(tb, IPSET_ATTR_MARK) ||
114                      !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT) ||
115                      !ip_set_optattr_netorder(tb, IPSET_ATTR_PACKETS) ||
116                      !ip_set_optattr_netorder(tb, IPSET_ATTR_BYTES)))
117                 return -IPSET_ERR_PROTOCOL;
118
119         if (tb[IPSET_ATTR_LINENO])
120                 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
121
122         ret = ip_set_get_ipaddr4(tb[IPSET_ATTR_IP], &e.ip) ||
123               ip_set_get_extensions(set, tb, &ext);
124         if (ret)
125                 return ret;
126
127         e.mark = ntohl(nla_get_u32(tb[IPSET_ATTR_MARK]));
128         e.mark &= h->markmask;
129
130         if (adt == IPSET_TEST ||
131             !(tb[IPSET_ATTR_IP_TO] || tb[IPSET_ATTR_CIDR])) {
132                 ret = adtfn(set, &e, &ext, &ext, flags);
133                 return ip_set_eexist(ret, flags) ? 0 : ret;
134         }
135
136         ip_to = ip = ntohl(e.ip);
137         if (tb[IPSET_ATTR_IP_TO]) {
138                 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP_TO], &ip_to);
139                 if (ret)
140                         return ret;
141                 if (ip > ip_to)
142                         swap(ip, ip_to);
143         } else if (tb[IPSET_ATTR_CIDR]) {
144                 u8 cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
145
146                 if (!cidr || cidr > 32)
147                         return -IPSET_ERR_INVALID_CIDR;
148                 ip_set_mask_from_to(ip, ip_to, cidr);
149         }
150
151         if (retried)
152                 ip = ntohl(h->next.ip);
153         for (; !before(ip_to, ip); ip++) {
154                 e.ip = htonl(ip);
155                 ret = adtfn(set, &e, &ext, &ext, flags);
156
157                 if (ret && !ip_set_eexist(ret, flags))
158                         return ret;
159                 else
160                         ret = 0;
161         }
162         return ret;
163 }
164
165 /* IPv6 variant */
166
167 struct hash_ipmark6_elem {
168         union nf_inet_addr ip;
169         __u32 mark;
170 };
171
172 /* Common functions */
173
174 static inline bool
175 hash_ipmark6_data_equal(const struct hash_ipmark6_elem *ip1,
176                         const struct hash_ipmark6_elem *ip2,
177                         u32 *multi)
178 {
179         return ipv6_addr_equal(&ip1->ip.in6, &ip2->ip.in6) &&
180                ip1->mark == ip2->mark;
181 }
182
183 static bool
184 hash_ipmark6_data_list(struct sk_buff *skb,
185                        const struct hash_ipmark6_elem *data)
186 {
187         if (nla_put_ipaddr6(skb, IPSET_ATTR_IP, &data->ip.in6) ||
188             nla_put_net32(skb, IPSET_ATTR_MARK, htonl(data->mark)))
189                 goto nla_put_failure;
190         return 0;
191
192 nla_put_failure:
193         return 1;
194 }
195
196 static inline void
197 hash_ipmark6_data_next(struct hash_ipmark4_elem *next,
198                        const struct hash_ipmark6_elem *d)
199 {
200 }
201
202 #undef MTYPE
203 #undef PF
204 #undef HOST_MASK
205 #undef HKEY_DATALEN
206
207 #define MTYPE           hash_ipmark6
208 #define PF              6
209 #define HOST_MASK       128
210 #define HKEY_DATALEN    sizeof(struct hash_ipmark6_elem)
211 #define IP_SET_EMIT_CREATE
212 #include "ip_set_hash_gen.h"
213
214
215 static int
216 hash_ipmark6_kadt(struct ip_set *set, const struct sk_buff *skb,
217                   const struct xt_action_param *par,
218                   enum ipset_adt adt, struct ip_set_adt_opt *opt)
219 {
220         const struct hash_ipmark *h = set->data;
221         ipset_adtfn adtfn = set->variant->adt[adt];
222         struct hash_ipmark6_elem e = { };
223         struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set);
224
225         e.mark = skb->mark;
226         e.mark &= h->markmask;
227
228         ip6addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &e.ip.in6);
229         return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags);
230 }
231
232 static int
233 hash_ipmark6_uadt(struct ip_set *set, struct nlattr *tb[],
234                   enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
235 {
236         const struct hash_ipmark *h = set->data;
237         ipset_adtfn adtfn = set->variant->adt[adt];
238         struct hash_ipmark6_elem e = { };
239         struct ip_set_ext ext = IP_SET_INIT_UEXT(set);
240         int ret;
241
242         if (unlikely(!tb[IPSET_ATTR_IP] ||
243                      !ip_set_attr_netorder(tb, IPSET_ATTR_MARK) ||
244                      !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT) ||
245                      !ip_set_optattr_netorder(tb, IPSET_ATTR_PACKETS) ||
246                      !ip_set_optattr_netorder(tb, IPSET_ATTR_BYTES) ||
247                      tb[IPSET_ATTR_IP_TO] ||
248                      tb[IPSET_ATTR_CIDR]))
249                 return -IPSET_ERR_PROTOCOL;
250
251         if (tb[IPSET_ATTR_LINENO])
252                 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
253
254         ret = ip_set_get_ipaddr6(tb[IPSET_ATTR_IP], &e.ip) ||
255               ip_set_get_extensions(set, tb, &ext);
256         if (ret)
257                 return ret;
258
259         e.mark = ntohl(nla_get_u32(tb[IPSET_ATTR_MARK]));
260         e.mark &= h->markmask;
261
262         if (adt == IPSET_TEST) {
263                 ret = adtfn(set, &e, &ext, &ext, flags);
264                 return ip_set_eexist(ret, flags) ? 0 : ret;
265         }
266
267         ret = adtfn(set, &e, &ext, &ext, flags);
268         if (ret && !ip_set_eexist(ret, flags))
269                 return ret;
270         else
271                 ret = 0;
272
273         return ret;
274 }
275
276 static struct ip_set_type hash_ipmark_type __read_mostly = {
277         .name           = "hash:ip,mark",
278         .protocol       = IPSET_PROTOCOL,
279         .features       = IPSET_TYPE_IP | IPSET_TYPE_MARK,
280         .dimension      = IPSET_DIM_TWO,
281         .family         = NFPROTO_UNSPEC,
282         .revision_min   = IPSET_TYPE_REV_MIN,
283         .revision_max   = IPSET_TYPE_REV_MAX,
284         .create         = hash_ipmark_create,
285         .create_policy  = {
286                 [IPSET_ATTR_MARKMASK]   = { .type = NLA_U32 },
287                 [IPSET_ATTR_HASHSIZE]   = { .type = NLA_U32 },
288                 [IPSET_ATTR_MAXELEM]    = { .type = NLA_U32 },
289                 [IPSET_ATTR_PROBES]     = { .type = NLA_U8 },
290                 [IPSET_ATTR_RESIZE]     = { .type = NLA_U8  },
291                 [IPSET_ATTR_TIMEOUT]    = { .type = NLA_U32 },
292                 [IPSET_ATTR_CADT_FLAGS] = { .type = NLA_U32 },
293         },
294         .adt_policy     = {
295                 [IPSET_ATTR_IP]         = { .type = NLA_NESTED },
296                 [IPSET_ATTR_IP_TO]      = { .type = NLA_NESTED },
297                 [IPSET_ATTR_MARK]       = { .type = NLA_U32 },
298                 [IPSET_ATTR_CIDR]       = { .type = NLA_U8 },
299                 [IPSET_ATTR_TIMEOUT]    = { .type = NLA_U32 },
300                 [IPSET_ATTR_LINENO]     = { .type = NLA_U32 },
301                 [IPSET_ATTR_BYTES]      = { .type = NLA_U64 },
302                 [IPSET_ATTR_PACKETS]    = { .type = NLA_U64 },
303                 [IPSET_ATTR_COMMENT]    = { .type = NLA_NUL_STRING },
304         },
305         .me             = THIS_MODULE,
306 };
307
308 static int __init
309 hash_ipmark_init(void)
310 {
311         return ip_set_type_register(&hash_ipmark_type);
312 }
313
314 static void __exit
315 hash_ipmark_fini(void)
316 {
317         ip_set_type_unregister(&hash_ipmark_type);
318 }
319
320 module_init(hash_ipmark_init);
321 module_exit(hash_ipmark_fini);