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