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