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