Commit | Line | Data |
---|---|---|
7c3ad056 OS |
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,net 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 | |
07cf8f5a | 28 | /* 0 Comments support added */ |
af331419 AD |
29 | /* 1 Forceadd support added */ |
30 | #define IPSET_TYPE_REV_MAX 2 /* skbinfo support added */ | |
7c3ad056 OS |
31 | |
32 | MODULE_LICENSE("GPL"); | |
33 | MODULE_AUTHOR("Oliver Smith <oliver@8.c.9.b.0.7.4.0.1.0.0.2.ip6.arpa>"); | |
34 | IP_SET_MODULE_DESC("hash:net,port,net", IPSET_TYPE_REV_MIN, IPSET_TYPE_REV_MAX); | |
35 | MODULE_ALIAS("ip_set_hash:net,port,net"); | |
36 | ||
37 | /* Type specific function prefix */ | |
38 | #define HTYPE hash_netportnet | |
39 | #define IP_SET_HASH_WITH_PROTO | |
40 | #define IP_SET_HASH_WITH_NETS | |
41 | #define IPSET_NET_COUNT 2 | |
42 | ||
43 | /* IPv4 variant */ | |
44 | ||
45 | /* Member elements */ | |
46 | struct hash_netportnet4_elem { | |
47 | union { | |
48 | __be32 ip[2]; | |
49 | __be64 ipcmp; | |
50 | }; | |
51 | __be16 port; | |
52 | union { | |
53 | u8 cidr[2]; | |
54 | u16 ccmp; | |
55 | }; | |
cac37639 | 56 | u16 padding; |
2b67d6e0 | 57 | u8 nomatch; |
7c3ad056 OS |
58 | u8 proto; |
59 | }; | |
60 | ||
61 | /* Common functions */ | |
62 | ||
63 | static inline bool | |
64 | hash_netportnet4_data_equal(const struct hash_netportnet4_elem *ip1, | |
65 | const struct hash_netportnet4_elem *ip2, | |
66 | u32 *multi) | |
67 | { | |
68 | return ip1->ipcmp == ip2->ipcmp && | |
69 | ip1->ccmp == ip2->ccmp && | |
70 | ip1->port == ip2->port && | |
71 | ip1->proto == ip2->proto; | |
72 | } | |
73 | ||
74 | static inline int | |
75 | hash_netportnet4_do_data_match(const struct hash_netportnet4_elem *elem) | |
76 | { | |
77 | return elem->nomatch ? -ENOTEMPTY : 1; | |
78 | } | |
79 | ||
80 | static inline void | |
81 | hash_netportnet4_data_set_flags(struct hash_netportnet4_elem *elem, u32 flags) | |
82 | { | |
83 | elem->nomatch = !!((flags >> 16) & IPSET_FLAG_NOMATCH); | |
84 | } | |
85 | ||
86 | static inline void | |
87 | hash_netportnet4_data_reset_flags(struct hash_netportnet4_elem *elem, u8 *flags) | |
88 | { | |
89 | swap(*flags, elem->nomatch); | |
90 | } | |
91 | ||
92 | static inline void | |
93 | hash_netportnet4_data_reset_elem(struct hash_netportnet4_elem *elem, | |
94 | struct hash_netportnet4_elem *orig) | |
95 | { | |
96 | elem->ip[1] = orig->ip[1]; | |
97 | } | |
98 | ||
99 | static inline void | |
100 | hash_netportnet4_data_netmask(struct hash_netportnet4_elem *elem, | |
101 | u8 cidr, bool inner) | |
102 | { | |
103 | if (inner) { | |
104 | elem->ip[1] &= ip_set_netmask(cidr); | |
105 | elem->cidr[1] = cidr; | |
106 | } else { | |
107 | elem->ip[0] &= ip_set_netmask(cidr); | |
108 | elem->cidr[0] = cidr; | |
109 | } | |
110 | } | |
111 | ||
112 | static bool | |
113 | hash_netportnet4_data_list(struct sk_buff *skb, | |
114 | const struct hash_netportnet4_elem *data) | |
115 | { | |
116 | u32 flags = data->nomatch ? IPSET_FLAG_NOMATCH : 0; | |
117 | ||
118 | if (nla_put_ipaddr4(skb, IPSET_ATTR_IP, data->ip[0]) || | |
119 | nla_put_ipaddr4(skb, IPSET_ATTR_IP2, data->ip[1]) || | |
120 | nla_put_net16(skb, IPSET_ATTR_PORT, data->port) || | |
121 | nla_put_u8(skb, IPSET_ATTR_CIDR, data->cidr[0]) || | |
122 | nla_put_u8(skb, IPSET_ATTR_CIDR2, data->cidr[1]) || | |
123 | nla_put_u8(skb, IPSET_ATTR_PROTO, data->proto) || | |
124 | (flags && | |
125 | nla_put_net32(skb, IPSET_ATTR_CADT_FLAGS, htonl(flags)))) | |
126 | goto nla_put_failure; | |
728a7e69 | 127 | return false; |
7c3ad056 OS |
128 | |
129 | nla_put_failure: | |
728a7e69 | 130 | return true; |
7c3ad056 OS |
131 | } |
132 | ||
133 | static inline void | |
134 | hash_netportnet4_data_next(struct hash_netportnet4_elem *next, | |
135 | const struct hash_netportnet4_elem *d) | |
136 | { | |
137 | next->ipcmp = d->ipcmp; | |
138 | next->port = d->port; | |
139 | } | |
140 | ||
141 | #define MTYPE hash_netportnet4 | |
7c3ad056 OS |
142 | #define HOST_MASK 32 |
143 | #include "ip_set_hash_gen.h" | |
144 | ||
145 | static int | |
146 | hash_netportnet4_kadt(struct ip_set *set, const struct sk_buff *skb, | |
147 | const struct xt_action_param *par, | |
148 | enum ipset_adt adt, struct ip_set_adt_opt *opt) | |
149 | { | |
150 | const struct hash_netportnet *h = set->data; | |
151 | ipset_adtfn adtfn = set->variant->adt[adt]; | |
1a869205 | 152 | struct hash_netportnet4_elem e = { }; |
7c3ad056 OS |
153 | struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set); |
154 | ||
1a869205 JK |
155 | e.cidr[0] = IP_SET_INIT_CIDR(h->nets[0].cidr[0], HOST_MASK); |
156 | e.cidr[1] = IP_SET_INIT_CIDR(h->nets[0].cidr[1], HOST_MASK); | |
7c3ad056 OS |
157 | if (adt == IPSET_TEST) |
158 | e.ccmp = (HOST_MASK << (sizeof(e.cidr[0]) * 8)) | HOST_MASK; | |
159 | ||
160 | if (!ip_set_get_ip4_port(skb, opt->flags & IPSET_DIM_TWO_SRC, | |
161 | &e.port, &e.proto)) | |
162 | return -EINVAL; | |
163 | ||
164 | ip4addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &e.ip[0]); | |
165 | ip4addrptr(skb, opt->flags & IPSET_DIM_THREE_SRC, &e.ip[1]); | |
166 | e.ip[0] &= ip_set_netmask(e.cidr[0]); | |
167 | e.ip[1] &= ip_set_netmask(e.cidr[1]); | |
168 | ||
169 | return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags); | |
170 | } | |
171 | ||
172 | static int | |
173 | hash_netportnet4_uadt(struct ip_set *set, struct nlattr *tb[], | |
174 | enum ipset_adt adt, u32 *lineno, u32 flags, bool retried) | |
175 | { | |
176 | const struct hash_netportnet *h = set->data; | |
177 | ipset_adtfn adtfn = set->variant->adt[adt]; | |
1a869205 | 178 | struct hash_netportnet4_elem e = { }; |
7c3ad056 OS |
179 | struct ip_set_ext ext = IP_SET_INIT_UEXT(set); |
180 | u32 ip = 0, ip_to = 0, ip_last, p = 0, port, port_to; | |
181 | u32 ip2_from = 0, ip2_to = 0, ip2_last, ip2; | |
182 | bool with_ports = false; | |
183 | u8 cidr, cidr2; | |
184 | int ret; | |
185 | ||
a212e08e SP |
186 | if (tb[IPSET_ATTR_LINENO]) |
187 | *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]); | |
188 | ||
1a869205 | 189 | e.cidr[0] = e.cidr[1] = HOST_MASK; |
7c3ad056 OS |
190 | if (unlikely(!tb[IPSET_ATTR_IP] || !tb[IPSET_ATTR_IP2] || |
191 | !ip_set_attr_netorder(tb, IPSET_ATTR_PORT) || | |
192 | !ip_set_optattr_netorder(tb, IPSET_ATTR_PORT_TO) || | |
7dd37bc8 | 193 | !ip_set_optattr_netorder(tb, IPSET_ATTR_CADT_FLAGS))) |
7c3ad056 OS |
194 | return -IPSET_ERR_PROTOCOL; |
195 | ||
8e55d2e5 SP |
196 | ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP], &ip); |
197 | if (ret) | |
198 | return ret; | |
199 | ||
200 | ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP2], &ip2_from); | |
201 | if (ret) | |
202 | return ret; | |
203 | ||
204 | ret = ip_set_get_extensions(set, tb, &ext); | |
7c3ad056 OS |
205 | if (ret) |
206 | return ret; | |
207 | ||
208 | if (tb[IPSET_ATTR_CIDR]) { | |
209 | cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]); | |
210 | if (!cidr || cidr > HOST_MASK) | |
211 | return -IPSET_ERR_INVALID_CIDR; | |
212 | e.cidr[0] = cidr; | |
213 | } | |
214 | ||
215 | if (tb[IPSET_ATTR_CIDR2]) { | |
216 | cidr = nla_get_u8(tb[IPSET_ATTR_CIDR2]); | |
217 | if (!cidr || cidr > HOST_MASK) | |
218 | return -IPSET_ERR_INVALID_CIDR; | |
219 | e.cidr[1] = cidr; | |
220 | } | |
221 | ||
d25472e4 | 222 | e.port = nla_get_be16(tb[IPSET_ATTR_PORT]); |
7c3ad056 OS |
223 | |
224 | if (tb[IPSET_ATTR_PROTO]) { | |
225 | e.proto = nla_get_u8(tb[IPSET_ATTR_PROTO]); | |
226 | with_ports = ip_set_proto_with_ports(e.proto); | |
227 | ||
228 | if (e.proto == 0) | |
229 | return -IPSET_ERR_INVALID_PROTO; | |
230 | } else | |
231 | return -IPSET_ERR_MISSING_PROTO; | |
232 | ||
233 | if (!(with_ports || e.proto == IPPROTO_ICMP)) | |
234 | e.port = 0; | |
235 | ||
236 | if (tb[IPSET_ATTR_CADT_FLAGS]) { | |
237 | u32 cadt_flags = ip_set_get_h32(tb[IPSET_ATTR_CADT_FLAGS]); | |
238 | if (cadt_flags & IPSET_FLAG_NOMATCH) | |
239 | flags |= (IPSET_FLAG_NOMATCH << 16); | |
240 | } | |
241 | ||
242 | with_ports = with_ports && tb[IPSET_ATTR_PORT_TO]; | |
243 | if (adt == IPSET_TEST || | |
244 | !(tb[IPSET_ATTR_IP_TO] || with_ports || tb[IPSET_ATTR_IP2_TO])) { | |
245 | e.ip[0] = htonl(ip & ip_set_hostmask(e.cidr[0])); | |
246 | e.ip[1] = htonl(ip2_from & ip_set_hostmask(e.cidr[1])); | |
247 | ret = adtfn(set, &e, &ext, &ext, flags); | |
248 | return ip_set_enomatch(ret, flags, adt, set) ? -ret : | |
249 | ip_set_eexist(ret, flags) ? 0 : ret; | |
250 | } | |
251 | ||
252 | ip_to = ip; | |
253 | if (tb[IPSET_ATTR_IP_TO]) { | |
254 | ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP_TO], &ip_to); | |
255 | if (ret) | |
256 | return ret; | |
257 | if (ip > ip_to) | |
258 | swap(ip, ip_to); | |
259 | if (unlikely(ip + UINT_MAX == ip_to)) | |
260 | return -IPSET_ERR_HASH_RANGE; | |
6e41ee68 SP |
261 | } else |
262 | ip_set_mask_from_to(ip, ip_to, e.cidr[0]); | |
7c3ad056 OS |
263 | |
264 | port_to = port = ntohs(e.port); | |
265 | if (tb[IPSET_ATTR_PORT_TO]) { | |
266 | port_to = ip_set_get_h16(tb[IPSET_ATTR_PORT_TO]); | |
267 | if (port > port_to) | |
268 | swap(port, port_to); | |
269 | } | |
270 | ||
271 | ip2_to = ip2_from; | |
272 | if (tb[IPSET_ATTR_IP2_TO]) { | |
273 | ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP2_TO], &ip2_to); | |
274 | if (ret) | |
275 | return ret; | |
276 | if (ip2_from > ip2_to) | |
277 | swap(ip2_from, ip2_to); | |
278 | if (unlikely(ip2_from + UINT_MAX == ip2_to)) | |
279 | return -IPSET_ERR_HASH_RANGE; | |
6e41ee68 SP |
280 | } else |
281 | ip_set_mask_from_to(ip2_from, ip2_to, e.cidr[1]); | |
7c3ad056 OS |
282 | |
283 | if (retried) | |
284 | ip = ntohl(h->next.ip[0]); | |
285 | ||
286 | while (!after(ip, ip_to)) { | |
287 | e.ip[0] = htonl(ip); | |
288 | ip_last = ip_set_range_to_cidr(ip, ip_to, &cidr); | |
289 | e.cidr[0] = cidr; | |
290 | p = retried && ip == ntohl(h->next.ip[0]) ? ntohs(h->next.port) | |
291 | : port; | |
292 | for (; p <= port_to; p++) { | |
293 | e.port = htons(p); | |
294 | ip2 = (retried && ip == ntohl(h->next.ip[0]) && | |
295 | p == ntohs(h->next.port)) ? ntohl(h->next.ip[1]) | |
296 | : ip2_from; | |
297 | while (!after(ip2, ip2_to)) { | |
298 | e.ip[1] = htonl(ip2); | |
299 | ip2_last = ip_set_range_to_cidr(ip2, ip2_to, | |
300 | &cidr2); | |
301 | e.cidr[1] = cidr2; | |
302 | ret = adtfn(set, &e, &ext, &ext, flags); | |
303 | if (ret && !ip_set_eexist(ret, flags)) | |
304 | return ret; | |
305 | else | |
306 | ret = 0; | |
307 | ip2 = ip2_last + 1; | |
308 | } | |
309 | } | |
310 | ip = ip_last + 1; | |
311 | } | |
312 | return ret; | |
313 | } | |
314 | ||
315 | /* IPv6 variant */ | |
316 | ||
317 | struct hash_netportnet6_elem { | |
318 | union nf_inet_addr ip[2]; | |
319 | __be16 port; | |
320 | union { | |
321 | u8 cidr[2]; | |
322 | u16 ccmp; | |
323 | }; | |
cac37639 | 324 | u16 padding; |
2b67d6e0 | 325 | u8 nomatch; |
7c3ad056 OS |
326 | u8 proto; |
327 | }; | |
328 | ||
329 | /* Common functions */ | |
330 | ||
331 | static inline bool | |
332 | hash_netportnet6_data_equal(const struct hash_netportnet6_elem *ip1, | |
333 | const struct hash_netportnet6_elem *ip2, | |
334 | u32 *multi) | |
335 | { | |
336 | return ipv6_addr_equal(&ip1->ip[0].in6, &ip2->ip[0].in6) && | |
337 | ipv6_addr_equal(&ip1->ip[1].in6, &ip2->ip[1].in6) && | |
338 | ip1->ccmp == ip2->ccmp && | |
339 | ip1->port == ip2->port && | |
340 | ip1->proto == ip2->proto; | |
341 | } | |
342 | ||
343 | static inline int | |
344 | hash_netportnet6_do_data_match(const struct hash_netportnet6_elem *elem) | |
345 | { | |
346 | return elem->nomatch ? -ENOTEMPTY : 1; | |
347 | } | |
348 | ||
349 | static inline void | |
350 | hash_netportnet6_data_set_flags(struct hash_netportnet6_elem *elem, u32 flags) | |
351 | { | |
352 | elem->nomatch = !!((flags >> 16) & IPSET_FLAG_NOMATCH); | |
353 | } | |
354 | ||
355 | static inline void | |
356 | hash_netportnet6_data_reset_flags(struct hash_netportnet6_elem *elem, u8 *flags) | |
357 | { | |
358 | swap(*flags, elem->nomatch); | |
359 | } | |
360 | ||
361 | static inline void | |
362 | hash_netportnet6_data_reset_elem(struct hash_netportnet6_elem *elem, | |
363 | struct hash_netportnet6_elem *orig) | |
364 | { | |
365 | elem->ip[1] = orig->ip[1]; | |
366 | } | |
367 | ||
368 | static inline void | |
369 | hash_netportnet6_data_netmask(struct hash_netportnet6_elem *elem, | |
370 | u8 cidr, bool inner) | |
371 | { | |
372 | if (inner) { | |
373 | ip6_netmask(&elem->ip[1], cidr); | |
374 | elem->cidr[1] = cidr; | |
375 | } else { | |
376 | ip6_netmask(&elem->ip[0], cidr); | |
377 | elem->cidr[0] = cidr; | |
378 | } | |
379 | } | |
380 | ||
381 | static bool | |
382 | hash_netportnet6_data_list(struct sk_buff *skb, | |
383 | const struct hash_netportnet6_elem *data) | |
384 | { | |
385 | u32 flags = data->nomatch ? IPSET_FLAG_NOMATCH : 0; | |
386 | ||
387 | if (nla_put_ipaddr6(skb, IPSET_ATTR_IP, &data->ip[0].in6) || | |
388 | nla_put_ipaddr6(skb, IPSET_ATTR_IP2, &data->ip[1].in6) || | |
389 | nla_put_net16(skb, IPSET_ATTR_PORT, data->port) || | |
390 | nla_put_u8(skb, IPSET_ATTR_CIDR, data->cidr[0]) || | |
391 | nla_put_u8(skb, IPSET_ATTR_CIDR2, data->cidr[1]) || | |
392 | nla_put_u8(skb, IPSET_ATTR_PROTO, data->proto) || | |
393 | (flags && | |
394 | nla_put_net32(skb, IPSET_ATTR_CADT_FLAGS, htonl(flags)))) | |
395 | goto nla_put_failure; | |
728a7e69 | 396 | return false; |
7c3ad056 OS |
397 | |
398 | nla_put_failure: | |
728a7e69 | 399 | return true; |
7c3ad056 OS |
400 | } |
401 | ||
402 | static inline void | |
403 | hash_netportnet6_data_next(struct hash_netportnet4_elem *next, | |
404 | const struct hash_netportnet6_elem *d) | |
405 | { | |
406 | next->port = d->port; | |
407 | } | |
408 | ||
409 | #undef MTYPE | |
7c3ad056 OS |
410 | #undef HOST_MASK |
411 | ||
412 | #define MTYPE hash_netportnet6 | |
7c3ad056 OS |
413 | #define HOST_MASK 128 |
414 | #define IP_SET_EMIT_CREATE | |
415 | #include "ip_set_hash_gen.h" | |
416 | ||
417 | static int | |
418 | hash_netportnet6_kadt(struct ip_set *set, const struct sk_buff *skb, | |
419 | const struct xt_action_param *par, | |
420 | enum ipset_adt adt, struct ip_set_adt_opt *opt) | |
421 | { | |
422 | const struct hash_netportnet *h = set->data; | |
423 | ipset_adtfn adtfn = set->variant->adt[adt]; | |
1a869205 | 424 | struct hash_netportnet6_elem e = { }; |
7c3ad056 OS |
425 | struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set); |
426 | ||
1a869205 JK |
427 | e.cidr[0] = IP_SET_INIT_CIDR(h->nets[0].cidr[0], HOST_MASK); |
428 | e.cidr[1] = IP_SET_INIT_CIDR(h->nets[0].cidr[1], HOST_MASK); | |
7c3ad056 OS |
429 | if (adt == IPSET_TEST) |
430 | e.ccmp = (HOST_MASK << (sizeof(u8) * 8)) | HOST_MASK; | |
431 | ||
432 | if (!ip_set_get_ip6_port(skb, opt->flags & IPSET_DIM_TWO_SRC, | |
433 | &e.port, &e.proto)) | |
434 | return -EINVAL; | |
435 | ||
436 | ip6addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &e.ip[0].in6); | |
437 | ip6addrptr(skb, opt->flags & IPSET_DIM_THREE_SRC, &e.ip[1].in6); | |
438 | ip6_netmask(&e.ip[0], e.cidr[0]); | |
439 | ip6_netmask(&e.ip[1], e.cidr[1]); | |
440 | ||
441 | return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags); | |
442 | } | |
443 | ||
444 | static int | |
445 | hash_netportnet6_uadt(struct ip_set *set, struct nlattr *tb[], | |
446 | enum ipset_adt adt, u32 *lineno, u32 flags, bool retried) | |
447 | { | |
448 | const struct hash_netportnet *h = set->data; | |
449 | ipset_adtfn adtfn = set->variant->adt[adt]; | |
1a869205 | 450 | struct hash_netportnet6_elem e = { }; |
7c3ad056 OS |
451 | struct ip_set_ext ext = IP_SET_INIT_UEXT(set); |
452 | u32 port, port_to; | |
453 | bool with_ports = false; | |
454 | int ret; | |
455 | ||
a212e08e SP |
456 | if (tb[IPSET_ATTR_LINENO]) |
457 | *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]); | |
458 | ||
1a869205 | 459 | e.cidr[0] = e.cidr[1] = HOST_MASK; |
7c3ad056 OS |
460 | if (unlikely(!tb[IPSET_ATTR_IP] || !tb[IPSET_ATTR_IP2] || |
461 | !ip_set_attr_netorder(tb, IPSET_ATTR_PORT) || | |
462 | !ip_set_optattr_netorder(tb, IPSET_ATTR_PORT_TO) || | |
7dd37bc8 | 463 | !ip_set_optattr_netorder(tb, IPSET_ATTR_CADT_FLAGS))) |
7c3ad056 OS |
464 | return -IPSET_ERR_PROTOCOL; |
465 | if (unlikely(tb[IPSET_ATTR_IP_TO] || tb[IPSET_ATTR_IP2_TO])) | |
466 | return -IPSET_ERR_HASH_RANGE_UNSUPPORTED; | |
467 | ||
8e55d2e5 SP |
468 | ret = ip_set_get_ipaddr6(tb[IPSET_ATTR_IP], &e.ip[0]); |
469 | if (ret) | |
470 | return ret; | |
471 | ||
472 | ret = ip_set_get_ipaddr6(tb[IPSET_ATTR_IP2], &e.ip[1]); | |
473 | if (ret) | |
474 | return ret; | |
475 | ||
476 | ret = ip_set_get_extensions(set, tb, &ext); | |
7c3ad056 OS |
477 | if (ret) |
478 | return ret; | |
479 | ||
480 | if (tb[IPSET_ATTR_CIDR]) | |
481 | e.cidr[0] = nla_get_u8(tb[IPSET_ATTR_CIDR]); | |
482 | ||
483 | if (tb[IPSET_ATTR_CIDR2]) | |
484 | e.cidr[1] = nla_get_u8(tb[IPSET_ATTR_CIDR2]); | |
485 | ||
486 | if (unlikely(!e.cidr[0] || e.cidr[0] > HOST_MASK || !e.cidr[1] || | |
487 | e.cidr[1] > HOST_MASK)) | |
488 | return -IPSET_ERR_INVALID_CIDR; | |
489 | ||
490 | ip6_netmask(&e.ip[0], e.cidr[0]); | |
491 | ip6_netmask(&e.ip[1], e.cidr[1]); | |
492 | ||
d25472e4 | 493 | e.port = nla_get_be16(tb[IPSET_ATTR_PORT]); |
7c3ad056 OS |
494 | |
495 | if (tb[IPSET_ATTR_PROTO]) { | |
496 | e.proto = nla_get_u8(tb[IPSET_ATTR_PROTO]); | |
497 | with_ports = ip_set_proto_with_ports(e.proto); | |
498 | ||
499 | if (e.proto == 0) | |
500 | return -IPSET_ERR_INVALID_PROTO; | |
501 | } else | |
502 | return -IPSET_ERR_MISSING_PROTO; | |
503 | ||
504 | if (!(with_ports || e.proto == IPPROTO_ICMPV6)) | |
505 | e.port = 0; | |
506 | ||
507 | if (tb[IPSET_ATTR_CADT_FLAGS]) { | |
508 | u32 cadt_flags = ip_set_get_h32(tb[IPSET_ATTR_CADT_FLAGS]); | |
509 | if (cadt_flags & IPSET_FLAG_NOMATCH) | |
510 | flags |= (IPSET_FLAG_NOMATCH << 16); | |
511 | } | |
512 | ||
513 | if (adt == IPSET_TEST || !with_ports || !tb[IPSET_ATTR_PORT_TO]) { | |
514 | ret = adtfn(set, &e, &ext, &ext, flags); | |
515 | return ip_set_enomatch(ret, flags, adt, set) ? -ret : | |
516 | ip_set_eexist(ret, flags) ? 0 : ret; | |
517 | } | |
518 | ||
519 | port = ntohs(e.port); | |
520 | port_to = ip_set_get_h16(tb[IPSET_ATTR_PORT_TO]); | |
521 | if (port > port_to) | |
522 | swap(port, port_to); | |
523 | ||
524 | if (retried) | |
525 | port = ntohs(h->next.port); | |
526 | for (; port <= port_to; port++) { | |
527 | e.port = htons(port); | |
528 | ret = adtfn(set, &e, &ext, &ext, flags); | |
529 | ||
530 | if (ret && !ip_set_eexist(ret, flags)) | |
531 | return ret; | |
532 | else | |
533 | ret = 0; | |
534 | } | |
535 | return ret; | |
536 | } | |
537 | ||
538 | static struct ip_set_type hash_netportnet_type __read_mostly = { | |
539 | .name = "hash:net,port,net", | |
540 | .protocol = IPSET_PROTOCOL, | |
541 | .features = IPSET_TYPE_IP | IPSET_TYPE_PORT | IPSET_TYPE_IP2 | | |
542 | IPSET_TYPE_NOMATCH, | |
543 | .dimension = IPSET_DIM_THREE, | |
544 | .family = NFPROTO_UNSPEC, | |
545 | .revision_min = IPSET_TYPE_REV_MIN, | |
546 | .revision_max = IPSET_TYPE_REV_MAX, | |
547 | .create = hash_netportnet_create, | |
548 | .create_policy = { | |
549 | [IPSET_ATTR_HASHSIZE] = { .type = NLA_U32 }, | |
550 | [IPSET_ATTR_MAXELEM] = { .type = NLA_U32 }, | |
551 | [IPSET_ATTR_PROBES] = { .type = NLA_U8 }, | |
552 | [IPSET_ATTR_RESIZE] = { .type = NLA_U8 }, | |
553 | [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 }, | |
554 | [IPSET_ATTR_CADT_FLAGS] = { .type = NLA_U32 }, | |
555 | }, | |
556 | .adt_policy = { | |
557 | [IPSET_ATTR_IP] = { .type = NLA_NESTED }, | |
558 | [IPSET_ATTR_IP_TO] = { .type = NLA_NESTED }, | |
559 | [IPSET_ATTR_IP2] = { .type = NLA_NESTED }, | |
560 | [IPSET_ATTR_IP2_TO] = { .type = NLA_NESTED }, | |
561 | [IPSET_ATTR_PORT] = { .type = NLA_U16 }, | |
562 | [IPSET_ATTR_PORT_TO] = { .type = NLA_U16 }, | |
563 | [IPSET_ATTR_CIDR] = { .type = NLA_U8 }, | |
564 | [IPSET_ATTR_CIDR2] = { .type = NLA_U8 }, | |
565 | [IPSET_ATTR_PROTO] = { .type = NLA_U8 }, | |
566 | [IPSET_ATTR_CADT_FLAGS] = { .type = NLA_U32 }, | |
567 | [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 }, | |
568 | [IPSET_ATTR_LINENO] = { .type = NLA_U32 }, | |
569 | [IPSET_ATTR_BYTES] = { .type = NLA_U64 }, | |
570 | [IPSET_ATTR_PACKETS] = { .type = NLA_U64 }, | |
03726186 SP |
571 | [IPSET_ATTR_COMMENT] = { .type = NLA_NUL_STRING, |
572 | .len = IPSET_MAX_COMMENT_SIZE }, | |
af331419 AD |
573 | [IPSET_ATTR_SKBMARK] = { .type = NLA_U64 }, |
574 | [IPSET_ATTR_SKBPRIO] = { .type = NLA_U32 }, | |
575 | [IPSET_ATTR_SKBQUEUE] = { .type = NLA_U16 }, | |
7c3ad056 OS |
576 | }, |
577 | .me = THIS_MODULE, | |
578 | }; | |
579 | ||
580 | static int __init | |
581 | hash_netportnet_init(void) | |
582 | { | |
583 | return ip_set_type_register(&hash_netportnet_type); | |
584 | } | |
585 | ||
586 | static void __exit | |
587 | hash_netportnet_fini(void) | |
588 | { | |
589 | ip_set_type_unregister(&hash_netportnet_type); | |
590 | } | |
591 | ||
592 | module_init(hash_netportnet_init); | |
593 | module_exit(hash_netportnet_fini); |