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