]>
Commit | Line | Data |
---|---|---|
990de541 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 | |
ca6038b2 | 28 | /* 0 Comments support added */ |
9c133301 AD |
29 | /* 1 Forceadd support added */ |
30 | #define IPSET_TYPE_REV_MAX 2 /* skbinfo support added */ | |
990de541 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 | }; | |
ab144ee5 | 56 | u16 padding; |
990de541 OS |
57 | u8 nomatch:1; |
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; | |
127 | return 0; | |
128 | ||
129 | nla_put_failure: | |
130 | return 1; | |
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 | |
142 | #define PF 4 | |
143 | #define HOST_MASK 32 | |
144 | #include "ip_set_hash_gen.h" | |
145 | ||
146 | static int | |
147 | hash_netportnet4_kadt(struct ip_set *set, const struct sk_buff *skb, | |
148 | const struct xt_action_param *par, | |
149 | enum ipset_adt adt, struct ip_set_adt_opt *opt) | |
150 | { | |
151 | const struct hash_netportnet *h = set->data; | |
152 | ipset_adtfn adtfn = set->variant->adt[adt]; | |
864a7f59 | 153 | struct hash_netportnet4_elem e = { }; |
990de541 OS |
154 | struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set); |
155 | ||
864a7f59 JK |
156 | e.cidr[0] = IP_SET_INIT_CIDR(h->nets[0].cidr[0], HOST_MASK); |
157 | e.cidr[1] = IP_SET_INIT_CIDR(h->nets[0].cidr[1], HOST_MASK); | |
990de541 OS |
158 | if (adt == IPSET_TEST) |
159 | e.ccmp = (HOST_MASK << (sizeof(e.cidr[0]) * 8)) | HOST_MASK; | |
160 | ||
161 | if (!ip_set_get_ip4_port(skb, opt->flags & IPSET_DIM_TWO_SRC, | |
162 | &e.port, &e.proto)) | |
163 | return -EINVAL; | |
164 | ||
165 | ip4addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &e.ip[0]); | |
166 | ip4addrptr(skb, opt->flags & IPSET_DIM_THREE_SRC, &e.ip[1]); | |
167 | e.ip[0] &= ip_set_netmask(e.cidr[0]); | |
168 | e.ip[1] &= ip_set_netmask(e.cidr[1]); | |
169 | ||
170 | return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags); | |
171 | } | |
172 | ||
173 | static int | |
174 | hash_netportnet4_uadt(struct ip_set *set, struct nlattr *tb[], | |
175 | enum ipset_adt adt, u32 *lineno, u32 flags, bool retried) | |
176 | { | |
177 | const struct hash_netportnet *h = set->data; | |
178 | ipset_adtfn adtfn = set->variant->adt[adt]; | |
864a7f59 | 179 | struct hash_netportnet4_elem e = { }; |
990de541 OS |
180 | struct ip_set_ext ext = IP_SET_INIT_UEXT(set); |
181 | u32 ip = 0, ip_to = 0, ip_last, p = 0, port, port_to; | |
182 | u32 ip2_from = 0, ip2_to = 0, ip2_last, ip2; | |
183 | bool with_ports = false; | |
184 | u8 cidr, cidr2; | |
185 | int ret; | |
186 | ||
864a7f59 | 187 | e.cidr[0] = e.cidr[1] = HOST_MASK; |
990de541 OS |
188 | if (unlikely(!tb[IPSET_ATTR_IP] || !tb[IPSET_ATTR_IP2] || |
189 | !ip_set_attr_netorder(tb, IPSET_ATTR_PORT) || | |
190 | !ip_set_optattr_netorder(tb, IPSET_ATTR_PORT_TO) || | |
191 | !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT) || | |
192 | !ip_set_optattr_netorder(tb, IPSET_ATTR_CADT_FLAGS) || | |
193 | !ip_set_optattr_netorder(tb, IPSET_ATTR_PACKETS) || | |
9c133301 AD |
194 | !ip_set_optattr_netorder(tb, IPSET_ATTR_BYTES) || |
195 | !ip_set_optattr_netorder(tb, IPSET_ATTR_SKBMARK) || | |
196 | !ip_set_optattr_netorder(tb, IPSET_ATTR_SKBPRIO) || | |
197 | !ip_set_optattr_netorder(tb, IPSET_ATTR_SKBQUEUE))) | |
990de541 OS |
198 | return -IPSET_ERR_PROTOCOL; |
199 | ||
200 | if (tb[IPSET_ATTR_LINENO]) | |
201 | *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]); | |
202 | ||
203 | ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP], &ip) || | |
204 | ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP2], &ip2_from) || | |
205 | ip_set_get_extensions(set, tb, &ext); | |
206 | if (ret) | |
207 | return ret; | |
208 | ||
209 | if (tb[IPSET_ATTR_CIDR]) { | |
210 | cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]); | |
211 | if (!cidr || cidr > HOST_MASK) | |
212 | return -IPSET_ERR_INVALID_CIDR; | |
213 | e.cidr[0] = cidr; | |
214 | } | |
215 | ||
216 | if (tb[IPSET_ATTR_CIDR2]) { | |
217 | cidr = nla_get_u8(tb[IPSET_ATTR_CIDR2]); | |
218 | if (!cidr || cidr > HOST_MASK) | |
219 | return -IPSET_ERR_INVALID_CIDR; | |
220 | e.cidr[1] = cidr; | |
221 | } | |
222 | ||
223 | if (tb[IPSET_ATTR_PORT]) | |
224 | e.port = nla_get_be16(tb[IPSET_ATTR_PORT]); | |
225 | else | |
226 | return -IPSET_ERR_PROTOCOL; | |
227 | ||
228 | if (tb[IPSET_ATTR_PROTO]) { | |
229 | e.proto = nla_get_u8(tb[IPSET_ATTR_PROTO]); | |
230 | with_ports = ip_set_proto_with_ports(e.proto); | |
231 | ||
232 | if (e.proto == 0) | |
233 | return -IPSET_ERR_INVALID_PROTO; | |
234 | } else | |
235 | return -IPSET_ERR_MISSING_PROTO; | |
236 | ||
237 | if (!(with_ports || e.proto == IPPROTO_ICMP)) | |
238 | e.port = 0; | |
239 | ||
240 | if (tb[IPSET_ATTR_CADT_FLAGS]) { | |
241 | u32 cadt_flags = ip_set_get_h32(tb[IPSET_ATTR_CADT_FLAGS]); | |
1ad6ec25 | 242 | |
990de541 OS |
243 | if (cadt_flags & IPSET_FLAG_NOMATCH) |
244 | flags |= (IPSET_FLAG_NOMATCH << 16); | |
245 | } | |
246 | ||
247 | with_ports = with_ports && tb[IPSET_ATTR_PORT_TO]; | |
248 | if (adt == IPSET_TEST || | |
249 | !(tb[IPSET_ATTR_IP_TO] || with_ports || tb[IPSET_ATTR_IP2_TO])) { | |
250 | e.ip[0] = htonl(ip & ip_set_hostmask(e.cidr[0])); | |
251 | e.ip[1] = htonl(ip2_from & ip_set_hostmask(e.cidr[1])); | |
252 | ret = adtfn(set, &e, &ext, &ext, flags); | |
253 | return ip_set_enomatch(ret, flags, adt, set) ? -ret : | |
254 | ip_set_eexist(ret, flags) ? 0 : ret; | |
255 | } | |
256 | ||
257 | ip_to = ip; | |
258 | if (tb[IPSET_ATTR_IP_TO]) { | |
259 | ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP_TO], &ip_to); | |
260 | if (ret) | |
261 | return ret; | |
262 | if (ip > ip_to) | |
263 | swap(ip, ip_to); | |
264 | if (unlikely(ip + UINT_MAX == ip_to)) | |
265 | return -IPSET_ERR_HASH_RANGE; | |
a31f30ce SP |
266 | } else |
267 | ip_set_mask_from_to(ip, ip_to, e.cidr[0]); | |
990de541 OS |
268 | |
269 | port_to = port = ntohs(e.port); | |
270 | if (tb[IPSET_ATTR_PORT_TO]) { | |
271 | port_to = ip_set_get_h16(tb[IPSET_ATTR_PORT_TO]); | |
272 | if (port > port_to) | |
273 | swap(port, port_to); | |
274 | } | |
275 | ||
276 | ip2_to = ip2_from; | |
277 | if (tb[IPSET_ATTR_IP2_TO]) { | |
278 | ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP2_TO], &ip2_to); | |
279 | if (ret) | |
280 | return ret; | |
281 | if (ip2_from > ip2_to) | |
282 | swap(ip2_from, ip2_to); | |
283 | if (unlikely(ip2_from + UINT_MAX == ip2_to)) | |
284 | return -IPSET_ERR_HASH_RANGE; | |
a31f30ce SP |
285 | } else |
286 | ip_set_mask_from_to(ip2_from, ip2_to, e.cidr[1]); | |
990de541 OS |
287 | |
288 | if (retried) | |
289 | ip = ntohl(h->next.ip[0]); | |
290 | ||
291 | while (!after(ip, ip_to)) { | |
292 | e.ip[0] = htonl(ip); | |
293 | ip_last = ip_set_range_to_cidr(ip, ip_to, &cidr); | |
294 | e.cidr[0] = cidr; | |
295 | p = retried && ip == ntohl(h->next.ip[0]) ? ntohs(h->next.port) | |
296 | : port; | |
297 | for (; p <= port_to; p++) { | |
298 | e.port = htons(p); | |
299 | ip2 = (retried && ip == ntohl(h->next.ip[0]) && | |
300 | p == ntohs(h->next.port)) ? ntohl(h->next.ip[1]) | |
301 | : ip2_from; | |
302 | while (!after(ip2, ip2_to)) { | |
303 | e.ip[1] = htonl(ip2); | |
304 | ip2_last = ip_set_range_to_cidr(ip2, ip2_to, | |
305 | &cidr2); | |
306 | e.cidr[1] = cidr2; | |
307 | ret = adtfn(set, &e, &ext, &ext, flags); | |
308 | if (ret && !ip_set_eexist(ret, flags)) | |
309 | return ret; | |
310 | else | |
311 | ret = 0; | |
312 | ip2 = ip2_last + 1; | |
313 | } | |
314 | } | |
315 | ip = ip_last + 1; | |
316 | } | |
317 | return ret; | |
318 | } | |
319 | ||
320 | /* IPv6 variant */ | |
321 | ||
322 | struct hash_netportnet6_elem { | |
323 | union nf_inet_addr ip[2]; | |
324 | __be16 port; | |
325 | union { | |
326 | u8 cidr[2]; | |
327 | u16 ccmp; | |
328 | }; | |
ab144ee5 | 329 | u16 padding; |
990de541 OS |
330 | u8 nomatch:1; |
331 | u8 proto; | |
332 | }; | |
333 | ||
334 | /* Common functions */ | |
335 | ||
336 | static inline bool | |
337 | hash_netportnet6_data_equal(const struct hash_netportnet6_elem *ip1, | |
338 | const struct hash_netportnet6_elem *ip2, | |
339 | u32 *multi) | |
340 | { | |
341 | return ipv6_addr_equal(&ip1->ip[0].in6, &ip2->ip[0].in6) && | |
342 | ipv6_addr_equal(&ip1->ip[1].in6, &ip2->ip[1].in6) && | |
343 | ip1->ccmp == ip2->ccmp && | |
344 | ip1->port == ip2->port && | |
345 | ip1->proto == ip2->proto; | |
346 | } | |
347 | ||
348 | static inline int | |
349 | hash_netportnet6_do_data_match(const struct hash_netportnet6_elem *elem) | |
350 | { | |
351 | return elem->nomatch ? -ENOTEMPTY : 1; | |
352 | } | |
353 | ||
354 | static inline void | |
355 | hash_netportnet6_data_set_flags(struct hash_netportnet6_elem *elem, u32 flags) | |
356 | { | |
357 | elem->nomatch = !!((flags >> 16) & IPSET_FLAG_NOMATCH); | |
358 | } | |
359 | ||
360 | static inline void | |
361 | hash_netportnet6_data_reset_flags(struct hash_netportnet6_elem *elem, u8 *flags) | |
362 | { | |
363 | swap(*flags, elem->nomatch); | |
364 | } | |
365 | ||
366 | static inline void | |
367 | hash_netportnet6_data_reset_elem(struct hash_netportnet6_elem *elem, | |
368 | struct hash_netportnet6_elem *orig) | |
369 | { | |
370 | elem->ip[1] = orig->ip[1]; | |
371 | } | |
372 | ||
373 | static inline void | |
374 | hash_netportnet6_data_netmask(struct hash_netportnet6_elem *elem, | |
375 | u8 cidr, bool inner) | |
376 | { | |
377 | if (inner) { | |
378 | ip6_netmask(&elem->ip[1], cidr); | |
379 | elem->cidr[1] = cidr; | |
380 | } else { | |
381 | ip6_netmask(&elem->ip[0], cidr); | |
382 | elem->cidr[0] = cidr; | |
383 | } | |
384 | } | |
385 | ||
386 | static bool | |
387 | hash_netportnet6_data_list(struct sk_buff *skb, | |
388 | const struct hash_netportnet6_elem *data) | |
389 | { | |
390 | u32 flags = data->nomatch ? IPSET_FLAG_NOMATCH : 0; | |
391 | ||
392 | if (nla_put_ipaddr6(skb, IPSET_ATTR_IP, &data->ip[0].in6) || | |
393 | nla_put_ipaddr6(skb, IPSET_ATTR_IP2, &data->ip[1].in6) || | |
394 | nla_put_net16(skb, IPSET_ATTR_PORT, data->port) || | |
395 | nla_put_u8(skb, IPSET_ATTR_CIDR, data->cidr[0]) || | |
396 | nla_put_u8(skb, IPSET_ATTR_CIDR2, data->cidr[1]) || | |
397 | nla_put_u8(skb, IPSET_ATTR_PROTO, data->proto) || | |
398 | (flags && | |
399 | nla_put_net32(skb, IPSET_ATTR_CADT_FLAGS, htonl(flags)))) | |
400 | goto nla_put_failure; | |
401 | return 0; | |
402 | ||
403 | nla_put_failure: | |
404 | return 1; | |
405 | } | |
406 | ||
407 | static inline void | |
408 | hash_netportnet6_data_next(struct hash_netportnet4_elem *next, | |
409 | const struct hash_netportnet6_elem *d) | |
410 | { | |
411 | next->port = d->port; | |
412 | } | |
413 | ||
414 | #undef MTYPE | |
415 | #undef PF | |
416 | #undef HOST_MASK | |
417 | ||
418 | #define MTYPE hash_netportnet6 | |
419 | #define PF 6 | |
420 | #define HOST_MASK 128 | |
421 | #define IP_SET_EMIT_CREATE | |
422 | #include "ip_set_hash_gen.h" | |
423 | ||
424 | static int | |
425 | hash_netportnet6_kadt(struct ip_set *set, const struct sk_buff *skb, | |
426 | const struct xt_action_param *par, | |
427 | enum ipset_adt adt, struct ip_set_adt_opt *opt) | |
428 | { | |
429 | const struct hash_netportnet *h = set->data; | |
430 | ipset_adtfn adtfn = set->variant->adt[adt]; | |
864a7f59 | 431 | struct hash_netportnet6_elem e = { }; |
990de541 OS |
432 | struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set); |
433 | ||
864a7f59 JK |
434 | e.cidr[0] = IP_SET_INIT_CIDR(h->nets[0].cidr[0], HOST_MASK); |
435 | e.cidr[1] = IP_SET_INIT_CIDR(h->nets[0].cidr[1], HOST_MASK); | |
990de541 OS |
436 | if (adt == IPSET_TEST) |
437 | e.ccmp = (HOST_MASK << (sizeof(u8) * 8)) | HOST_MASK; | |
438 | ||
439 | if (!ip_set_get_ip6_port(skb, opt->flags & IPSET_DIM_TWO_SRC, | |
440 | &e.port, &e.proto)) | |
441 | return -EINVAL; | |
442 | ||
443 | ip6addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &e.ip[0].in6); | |
444 | ip6addrptr(skb, opt->flags & IPSET_DIM_THREE_SRC, &e.ip[1].in6); | |
445 | ip6_netmask(&e.ip[0], e.cidr[0]); | |
446 | ip6_netmask(&e.ip[1], e.cidr[1]); | |
447 | ||
448 | return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags); | |
449 | } | |
450 | ||
451 | static int | |
452 | hash_netportnet6_uadt(struct ip_set *set, struct nlattr *tb[], | |
453 | enum ipset_adt adt, u32 *lineno, u32 flags, bool retried) | |
454 | { | |
455 | const struct hash_netportnet *h = set->data; | |
456 | ipset_adtfn adtfn = set->variant->adt[adt]; | |
864a7f59 | 457 | struct hash_netportnet6_elem e = { }; |
990de541 OS |
458 | struct ip_set_ext ext = IP_SET_INIT_UEXT(set); |
459 | u32 port, port_to; | |
460 | bool with_ports = false; | |
461 | int ret; | |
462 | ||
864a7f59 | 463 | e.cidr[0] = e.cidr[1] = HOST_MASK; |
990de541 OS |
464 | if (unlikely(!tb[IPSET_ATTR_IP] || !tb[IPSET_ATTR_IP2] || |
465 | !ip_set_attr_netorder(tb, IPSET_ATTR_PORT) || | |
466 | !ip_set_optattr_netorder(tb, IPSET_ATTR_PORT_TO) || | |
467 | !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT) || | |
468 | !ip_set_optattr_netorder(tb, IPSET_ATTR_CADT_FLAGS) || | |
469 | !ip_set_optattr_netorder(tb, IPSET_ATTR_PACKETS) || | |
9c133301 AD |
470 | !ip_set_optattr_netorder(tb, IPSET_ATTR_BYTES) || |
471 | !ip_set_optattr_netorder(tb, IPSET_ATTR_SKBMARK) || | |
472 | !ip_set_optattr_netorder(tb, IPSET_ATTR_SKBPRIO) || | |
473 | !ip_set_optattr_netorder(tb, IPSET_ATTR_SKBQUEUE))) | |
990de541 OS |
474 | return -IPSET_ERR_PROTOCOL; |
475 | if (unlikely(tb[IPSET_ATTR_IP_TO] || tb[IPSET_ATTR_IP2_TO])) | |
476 | return -IPSET_ERR_HASH_RANGE_UNSUPPORTED; | |
477 | ||
478 | if (tb[IPSET_ATTR_LINENO]) | |
479 | *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]); | |
480 | ||
481 | ret = ip_set_get_ipaddr6(tb[IPSET_ATTR_IP], &e.ip[0]) || | |
482 | ip_set_get_ipaddr6(tb[IPSET_ATTR_IP2], &e.ip[1]) || | |
483 | ip_set_get_extensions(set, tb, &ext); | |
484 | if (ret) | |
485 | return ret; | |
486 | ||
487 | if (tb[IPSET_ATTR_CIDR]) | |
488 | e.cidr[0] = nla_get_u8(tb[IPSET_ATTR_CIDR]); | |
489 | ||
490 | if (tb[IPSET_ATTR_CIDR2]) | |
491 | e.cidr[1] = nla_get_u8(tb[IPSET_ATTR_CIDR2]); | |
492 | ||
493 | if (unlikely(!e.cidr[0] || e.cidr[0] > HOST_MASK || !e.cidr[1] || | |
494 | e.cidr[1] > HOST_MASK)) | |
495 | return -IPSET_ERR_INVALID_CIDR; | |
496 | ||
497 | ip6_netmask(&e.ip[0], e.cidr[0]); | |
498 | ip6_netmask(&e.ip[1], e.cidr[1]); | |
499 | ||
500 | if (tb[IPSET_ATTR_PORT]) | |
501 | e.port = nla_get_be16(tb[IPSET_ATTR_PORT]); | |
502 | else | |
503 | return -IPSET_ERR_PROTOCOL; | |
504 | ||
505 | if (tb[IPSET_ATTR_PROTO]) { | |
506 | e.proto = nla_get_u8(tb[IPSET_ATTR_PROTO]); | |
507 | with_ports = ip_set_proto_with_ports(e.proto); | |
508 | ||
509 | if (e.proto == 0) | |
510 | return -IPSET_ERR_INVALID_PROTO; | |
511 | } else | |
512 | return -IPSET_ERR_MISSING_PROTO; | |
513 | ||
514 | if (!(with_ports || e.proto == IPPROTO_ICMPV6)) | |
515 | e.port = 0; | |
516 | ||
517 | if (tb[IPSET_ATTR_CADT_FLAGS]) { | |
518 | u32 cadt_flags = ip_set_get_h32(tb[IPSET_ATTR_CADT_FLAGS]); | |
1ad6ec25 | 519 | |
990de541 OS |
520 | if (cadt_flags & IPSET_FLAG_NOMATCH) |
521 | flags |= (IPSET_FLAG_NOMATCH << 16); | |
522 | } | |
523 | ||
524 | if (adt == IPSET_TEST || !with_ports || !tb[IPSET_ATTR_PORT_TO]) { | |
525 | ret = adtfn(set, &e, &ext, &ext, flags); | |
526 | return ip_set_enomatch(ret, flags, adt, set) ? -ret : | |
527 | ip_set_eexist(ret, flags) ? 0 : ret; | |
528 | } | |
529 | ||
530 | port = ntohs(e.port); | |
531 | port_to = ip_set_get_h16(tb[IPSET_ATTR_PORT_TO]); | |
532 | if (port > port_to) | |
533 | swap(port, port_to); | |
534 | ||
535 | if (retried) | |
536 | port = ntohs(h->next.port); | |
537 | for (; port <= port_to; port++) { | |
538 | e.port = htons(port); | |
539 | ret = adtfn(set, &e, &ext, &ext, flags); | |
540 | ||
541 | if (ret && !ip_set_eexist(ret, flags)) | |
542 | return ret; | |
543 | else | |
544 | ret = 0; | |
545 | } | |
546 | return ret; | |
547 | } | |
548 | ||
549 | static struct ip_set_type hash_netportnet_type __read_mostly = { | |
550 | .name = "hash:net,port,net", | |
551 | .protocol = IPSET_PROTOCOL, | |
552 | .features = IPSET_TYPE_IP | IPSET_TYPE_PORT | IPSET_TYPE_IP2 | | |
553 | IPSET_TYPE_NOMATCH, | |
554 | .dimension = IPSET_DIM_THREE, | |
555 | .family = NFPROTO_UNSPEC, | |
556 | .revision_min = IPSET_TYPE_REV_MIN, | |
557 | .revision_max = IPSET_TYPE_REV_MAX, | |
558 | .create = hash_netportnet_create, | |
559 | .create_policy = { | |
560 | [IPSET_ATTR_HASHSIZE] = { .type = NLA_U32 }, | |
561 | [IPSET_ATTR_MAXELEM] = { .type = NLA_U32 }, | |
562 | [IPSET_ATTR_PROBES] = { .type = NLA_U8 }, | |
563 | [IPSET_ATTR_RESIZE] = { .type = NLA_U8 }, | |
564 | [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 }, | |
565 | [IPSET_ATTR_CADT_FLAGS] = { .type = NLA_U32 }, | |
566 | }, | |
567 | .adt_policy = { | |
568 | [IPSET_ATTR_IP] = { .type = NLA_NESTED }, | |
569 | [IPSET_ATTR_IP_TO] = { .type = NLA_NESTED }, | |
570 | [IPSET_ATTR_IP2] = { .type = NLA_NESTED }, | |
571 | [IPSET_ATTR_IP2_TO] = { .type = NLA_NESTED }, | |
572 | [IPSET_ATTR_PORT] = { .type = NLA_U16 }, | |
573 | [IPSET_ATTR_PORT_TO] = { .type = NLA_U16 }, | |
574 | [IPSET_ATTR_CIDR] = { .type = NLA_U8 }, | |
575 | [IPSET_ATTR_CIDR2] = { .type = NLA_U8 }, | |
576 | [IPSET_ATTR_PROTO] = { .type = NLA_U8 }, | |
577 | [IPSET_ATTR_CADT_FLAGS] = { .type = NLA_U32 }, | |
578 | [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 }, | |
579 | [IPSET_ATTR_LINENO] = { .type = NLA_U32 }, | |
580 | [IPSET_ATTR_BYTES] = { .type = NLA_U64 }, | |
581 | [IPSET_ATTR_PACKETS] = { .type = NLA_U64 }, | |
582 | [IPSET_ATTR_COMMENT] = { .type = NLA_NUL_STRING }, | |
9c133301 AD |
583 | [IPSET_ATTR_SKBMARK] = { .type = NLA_U64 }, |
584 | [IPSET_ATTR_SKBPRIO] = { .type = NLA_U32 }, | |
585 | [IPSET_ATTR_SKBQUEUE] = { .type = NLA_U16 }, | |
990de541 OS |
586 | }, |
587 | .me = THIS_MODULE, | |
588 | }; | |
589 | ||
590 | static int __init | |
591 | hash_netportnet_init(void) | |
592 | { | |
593 | return ip_set_type_register(&hash_netportnet_type); | |
594 | } | |
595 | ||
596 | static void __exit | |
597 | hash_netportnet_fini(void) | |
598 | { | |
2ca175d7 | 599 | rcu_barrier(); |
990de541 OS |
600 | ip_set_type_unregister(&hash_netportnet_type); |
601 | } | |
602 | ||
603 | module_init(hash_netportnet_init); | |
604 | module_exit(hash_netportnet_fini); |