Add the rt linux 4.1.3-rt3 as base
[kvmfornfv.git] / kernel / net / netfilter / ipset / ip_set_hash_ipport.c
1 /* Copyright (C) 2003-2013 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
2  *
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  */
7
8 /* Kernel module implementing an IP set type: the hash:ip,port type */
9
10 #include <linux/jhash.h>
11 #include <linux/module.h>
12 #include <linux/ip.h>
13 #include <linux/skbuff.h>
14 #include <linux/errno.h>
15 #include <linux/random.h>
16 #include <net/ip.h>
17 #include <net/ipv6.h>
18 #include <net/netlink.h>
19 #include <net/tcp.h>
20
21 #include <linux/netfilter.h>
22 #include <linux/netfilter/ipset/pfxlen.h>
23 #include <linux/netfilter/ipset/ip_set.h>
24 #include <linux/netfilter/ipset/ip_set_getport.h>
25 #include <linux/netfilter/ipset/ip_set_hash.h>
26
27 #define IPSET_TYPE_REV_MIN      0
28 /*                              1    SCTP and UDPLITE support added */
29 /*                              2    Counters support added */
30 /*                              3    Comments support added */
31 /*                              4    Forceadd support added */
32 #define IPSET_TYPE_REV_MAX      5 /* skbinfo support added */
33
34 MODULE_LICENSE("GPL");
35 MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
36 IP_SET_MODULE_DESC("hash:ip,port", IPSET_TYPE_REV_MIN, IPSET_TYPE_REV_MAX);
37 MODULE_ALIAS("ip_set_hash:ip,port");
38
39 /* Type specific function prefix */
40 #define HTYPE           hash_ipport
41
42 /* IPv4 variant */
43
44 /* Member elements */
45 struct hash_ipport4_elem {
46         __be32 ip;
47         __be16 port;
48         u8 proto;
49         u8 padding;
50 };
51
52 /* Common functions */
53
54 static inline bool
55 hash_ipport4_data_equal(const struct hash_ipport4_elem *ip1,
56                         const struct hash_ipport4_elem *ip2,
57                         u32 *multi)
58 {
59         return ip1->ip == ip2->ip &&
60                ip1->port == ip2->port &&
61                ip1->proto == ip2->proto;
62 }
63
64 static bool
65 hash_ipport4_data_list(struct sk_buff *skb,
66                        const struct hash_ipport4_elem *data)
67 {
68         if (nla_put_ipaddr4(skb, IPSET_ATTR_IP, data->ip) ||
69             nla_put_net16(skb, IPSET_ATTR_PORT, data->port) ||
70             nla_put_u8(skb, IPSET_ATTR_PROTO, data->proto))
71                 goto nla_put_failure;
72         return 0;
73
74 nla_put_failure:
75         return 1;
76 }
77
78 static inline void
79 hash_ipport4_data_next(struct hash_ipport4_elem *next,
80                        const struct hash_ipport4_elem *d)
81 {
82         next->ip = d->ip;
83         next->port = d->port;
84 }
85
86 #define MTYPE           hash_ipport4
87 #define PF              4
88 #define HOST_MASK       32
89 #define HKEY_DATALEN    sizeof(struct hash_ipport4_elem)
90 #include "ip_set_hash_gen.h"
91
92 static int
93 hash_ipport4_kadt(struct ip_set *set, const struct sk_buff *skb,
94                   const struct xt_action_param *par,
95                   enum ipset_adt adt, struct ip_set_adt_opt *opt)
96 {
97         ipset_adtfn adtfn = set->variant->adt[adt];
98         struct hash_ipport4_elem e = { .ip = 0 };
99         struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set);
100
101         if (!ip_set_get_ip4_port(skb, opt->flags & IPSET_DIM_TWO_SRC,
102                                  &e.port, &e.proto))
103                 return -EINVAL;
104
105         ip4addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &e.ip);
106         return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags);
107 }
108
109 static int
110 hash_ipport4_uadt(struct ip_set *set, struct nlattr *tb[],
111                   enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
112 {
113         const struct hash_ipport *h = set->data;
114         ipset_adtfn adtfn = set->variant->adt[adt];
115         struct hash_ipport4_elem e = { .ip = 0 };
116         struct ip_set_ext ext = IP_SET_INIT_UEXT(set);
117         u32 ip, ip_to = 0, p = 0, port, port_to;
118         bool with_ports = false;
119         int ret;
120
121         if (unlikely(!tb[IPSET_ATTR_IP] ||
122                      !ip_set_attr_netorder(tb, IPSET_ATTR_PORT) ||
123                      !ip_set_optattr_netorder(tb, IPSET_ATTR_PORT_TO) ||
124                      !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT) ||
125                      !ip_set_optattr_netorder(tb, IPSET_ATTR_PACKETS) ||
126                      !ip_set_optattr_netorder(tb, IPSET_ATTR_BYTES) ||
127                      !ip_set_optattr_netorder(tb, IPSET_ATTR_SKBMARK) ||
128                      !ip_set_optattr_netorder(tb, IPSET_ATTR_SKBPRIO) ||
129                      !ip_set_optattr_netorder(tb, IPSET_ATTR_SKBQUEUE)))
130                 return -IPSET_ERR_PROTOCOL;
131
132         if (tb[IPSET_ATTR_LINENO])
133                 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
134
135         ret = ip_set_get_ipaddr4(tb[IPSET_ATTR_IP], &e.ip) ||
136               ip_set_get_extensions(set, tb, &ext);
137         if (ret)
138                 return ret;
139
140         if (tb[IPSET_ATTR_PORT])
141                 e.port = nla_get_be16(tb[IPSET_ATTR_PORT]);
142         else
143                 return -IPSET_ERR_PROTOCOL;
144
145         if (tb[IPSET_ATTR_PROTO]) {
146                 e.proto = nla_get_u8(tb[IPSET_ATTR_PROTO]);
147                 with_ports = ip_set_proto_with_ports(e.proto);
148
149                 if (e.proto == 0)
150                         return -IPSET_ERR_INVALID_PROTO;
151         } else
152                 return -IPSET_ERR_MISSING_PROTO;
153
154         if (!(with_ports || e.proto == IPPROTO_ICMP))
155                 e.port = 0;
156
157         if (adt == IPSET_TEST ||
158             !(tb[IPSET_ATTR_IP_TO] || tb[IPSET_ATTR_CIDR] ||
159               tb[IPSET_ATTR_PORT_TO])) {
160                 ret = adtfn(set, &e, &ext, &ext, flags);
161                 return ip_set_eexist(ret, flags) ? 0 : ret;
162         }
163
164         ip_to = ip = ntohl(e.ip);
165         if (tb[IPSET_ATTR_IP_TO]) {
166                 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP_TO], &ip_to);
167                 if (ret)
168                         return ret;
169                 if (ip > ip_to)
170                         swap(ip, ip_to);
171         } else if (tb[IPSET_ATTR_CIDR]) {
172                 u8 cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
173
174                 if (!cidr || cidr > 32)
175                         return -IPSET_ERR_INVALID_CIDR;
176                 ip_set_mask_from_to(ip, ip_to, cidr);
177         }
178
179         port_to = port = ntohs(e.port);
180         if (with_ports && tb[IPSET_ATTR_PORT_TO]) {
181                 port_to = ip_set_get_h16(tb[IPSET_ATTR_PORT_TO]);
182                 if (port > port_to)
183                         swap(port, port_to);
184         }
185
186         if (retried)
187                 ip = ntohl(h->next.ip);
188         for (; !before(ip_to, ip); ip++) {
189                 p = retried && ip == ntohl(h->next.ip) ? ntohs(h->next.port)
190                                                        : port;
191                 for (; p <= port_to; p++) {
192                         e.ip = htonl(ip);
193                         e.port = htons(p);
194                         ret = adtfn(set, &e, &ext, &ext, flags);
195
196                         if (ret && !ip_set_eexist(ret, flags))
197                                 return ret;
198                         else
199                                 ret = 0;
200                 }
201         }
202         return ret;
203 }
204
205 /* IPv6 variant */
206
207 struct hash_ipport6_elem {
208         union nf_inet_addr ip;
209         __be16 port;
210         u8 proto;
211         u8 padding;
212 };
213
214 /* Common functions */
215
216 static inline bool
217 hash_ipport6_data_equal(const struct hash_ipport6_elem *ip1,
218                         const struct hash_ipport6_elem *ip2,
219                         u32 *multi)
220 {
221         return ipv6_addr_equal(&ip1->ip.in6, &ip2->ip.in6) &&
222                ip1->port == ip2->port &&
223                ip1->proto == ip2->proto;
224 }
225
226 static bool
227 hash_ipport6_data_list(struct sk_buff *skb,
228                        const struct hash_ipport6_elem *data)
229 {
230         if (nla_put_ipaddr6(skb, IPSET_ATTR_IP, &data->ip.in6) ||
231             nla_put_net16(skb, IPSET_ATTR_PORT, data->port) ||
232             nla_put_u8(skb, IPSET_ATTR_PROTO, data->proto))
233                 goto nla_put_failure;
234         return 0;
235
236 nla_put_failure:
237         return 1;
238 }
239
240 static inline void
241 hash_ipport6_data_next(struct hash_ipport4_elem *next,
242                        const struct hash_ipport6_elem *d)
243 {
244         next->port = d->port;
245 }
246
247 #undef MTYPE
248 #undef PF
249 #undef HOST_MASK
250 #undef HKEY_DATALEN
251
252 #define MTYPE           hash_ipport6
253 #define PF              6
254 #define HOST_MASK       128
255 #define HKEY_DATALEN    sizeof(struct hash_ipport6_elem)
256 #define IP_SET_EMIT_CREATE
257 #include "ip_set_hash_gen.h"
258
259 static int
260 hash_ipport6_kadt(struct ip_set *set, const struct sk_buff *skb,
261                   const struct xt_action_param *par,
262                   enum ipset_adt adt, struct ip_set_adt_opt *opt)
263 {
264         ipset_adtfn adtfn = set->variant->adt[adt];
265         struct hash_ipport6_elem e = { .ip = { .all = { 0 } } };
266         struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set);
267
268         if (!ip_set_get_ip6_port(skb, opt->flags & IPSET_DIM_TWO_SRC,
269                                  &e.port, &e.proto))
270                 return -EINVAL;
271
272         ip6addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &e.ip.in6);
273         return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags);
274 }
275
276 static int
277 hash_ipport6_uadt(struct ip_set *set, struct nlattr *tb[],
278                   enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
279 {
280         const struct hash_ipport *h = set->data;
281         ipset_adtfn adtfn = set->variant->adt[adt];
282         struct hash_ipport6_elem e = { .ip = { .all = { 0 } } };
283         struct ip_set_ext ext = IP_SET_INIT_UEXT(set);
284         u32 port, port_to;
285         bool with_ports = false;
286         int ret;
287
288         if (unlikely(!tb[IPSET_ATTR_IP] ||
289                      !ip_set_attr_netorder(tb, IPSET_ATTR_PORT) ||
290                      !ip_set_optattr_netorder(tb, IPSET_ATTR_PORT_TO) ||
291                      !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT) ||
292                      !ip_set_optattr_netorder(tb, IPSET_ATTR_PACKETS) ||
293                      !ip_set_optattr_netorder(tb, IPSET_ATTR_BYTES) ||
294                      !ip_set_optattr_netorder(tb, IPSET_ATTR_SKBMARK) ||
295                      !ip_set_optattr_netorder(tb, IPSET_ATTR_SKBPRIO) ||
296                      !ip_set_optattr_netorder(tb, IPSET_ATTR_SKBQUEUE) ||
297                      tb[IPSET_ATTR_IP_TO] ||
298                      tb[IPSET_ATTR_CIDR]))
299                 return -IPSET_ERR_PROTOCOL;
300
301         if (tb[IPSET_ATTR_LINENO])
302                 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
303
304         ret = ip_set_get_ipaddr6(tb[IPSET_ATTR_IP], &e.ip) ||
305               ip_set_get_extensions(set, tb, &ext);
306         if (ret)
307                 return ret;
308
309         if (tb[IPSET_ATTR_PORT])
310                 e.port = nla_get_be16(tb[IPSET_ATTR_PORT]);
311         else
312                 return -IPSET_ERR_PROTOCOL;
313
314         if (tb[IPSET_ATTR_PROTO]) {
315                 e.proto = nla_get_u8(tb[IPSET_ATTR_PROTO]);
316                 with_ports = ip_set_proto_with_ports(e.proto);
317
318                 if (e.proto == 0)
319                         return -IPSET_ERR_INVALID_PROTO;
320         } else
321                 return -IPSET_ERR_MISSING_PROTO;
322
323         if (!(with_ports || e.proto == IPPROTO_ICMPV6))
324                 e.port = 0;
325
326         if (adt == IPSET_TEST || !with_ports || !tb[IPSET_ATTR_PORT_TO]) {
327                 ret = adtfn(set, &e, &ext, &ext, flags);
328                 return ip_set_eexist(ret, flags) ? 0 : ret;
329         }
330
331         port = ntohs(e.port);
332         port_to = ip_set_get_h16(tb[IPSET_ATTR_PORT_TO]);
333         if (port > port_to)
334                 swap(port, port_to);
335
336         if (retried)
337                 port = ntohs(h->next.port);
338         for (; port <= port_to; port++) {
339                 e.port = htons(port);
340                 ret = adtfn(set, &e, &ext, &ext, flags);
341
342                 if (ret && !ip_set_eexist(ret, flags))
343                         return ret;
344                 else
345                         ret = 0;
346         }
347         return ret;
348 }
349
350 static struct ip_set_type hash_ipport_type __read_mostly = {
351         .name           = "hash:ip,port",
352         .protocol       = IPSET_PROTOCOL,
353         .features       = IPSET_TYPE_IP | IPSET_TYPE_PORT,
354         .dimension      = IPSET_DIM_TWO,
355         .family         = NFPROTO_UNSPEC,
356         .revision_min   = IPSET_TYPE_REV_MIN,
357         .revision_max   = IPSET_TYPE_REV_MAX,
358         .create         = hash_ipport_create,
359         .create_policy  = {
360                 [IPSET_ATTR_HASHSIZE]   = { .type = NLA_U32 },
361                 [IPSET_ATTR_MAXELEM]    = { .type = NLA_U32 },
362                 [IPSET_ATTR_PROBES]     = { .type = NLA_U8 },
363                 [IPSET_ATTR_RESIZE]     = { .type = NLA_U8  },
364                 [IPSET_ATTR_PROTO]      = { .type = NLA_U8 },
365                 [IPSET_ATTR_TIMEOUT]    = { .type = NLA_U32 },
366                 [IPSET_ATTR_CADT_FLAGS] = { .type = NLA_U32 },
367         },
368         .adt_policy     = {
369                 [IPSET_ATTR_IP]         = { .type = NLA_NESTED },
370                 [IPSET_ATTR_IP_TO]      = { .type = NLA_NESTED },
371                 [IPSET_ATTR_PORT]       = { .type = NLA_U16 },
372                 [IPSET_ATTR_PORT_TO]    = { .type = NLA_U16 },
373                 [IPSET_ATTR_CIDR]       = { .type = NLA_U8 },
374                 [IPSET_ATTR_PROTO]      = { .type = NLA_U8 },
375                 [IPSET_ATTR_TIMEOUT]    = { .type = NLA_U32 },
376                 [IPSET_ATTR_LINENO]     = { .type = NLA_U32 },
377                 [IPSET_ATTR_BYTES]      = { .type = NLA_U64 },
378                 [IPSET_ATTR_PACKETS]    = { .type = NLA_U64 },
379                 [IPSET_ATTR_COMMENT]    = { .type = NLA_NUL_STRING },
380                 [IPSET_ATTR_SKBMARK]    = { .type = NLA_U64 },
381                 [IPSET_ATTR_SKBPRIO]    = { .type = NLA_U32 },
382                 [IPSET_ATTR_SKBQUEUE]   = { .type = NLA_U16 },
383         },
384         .me             = THIS_MODULE,
385 };
386
387 static int __init
388 hash_ipport_init(void)
389 {
390         return ip_set_type_register(&hash_ipport_type);
391 }
392
393 static void __exit
394 hash_ipport_fini(void)
395 {
396         ip_set_type_unregister(&hash_ipport_type);
397 }
398
399 module_init(hash_ipport_init);
400 module_exit(hash_ipport_fini);