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