]>
Commit | Line | Data |
---|---|---|
5d50e1d8 | 1 | /* Copyright (C) 2003-2013 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu> |
21f45020 JK |
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:net,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> | |
21f45020 JK |
15 | #include <linux/random.h> |
16 | #include <net/ip.h> | |
17 | #include <net/ipv6.h> | |
18 | #include <net/netlink.h> | |
19 | ||
20 | #include <linux/netfilter.h> | |
21 | #include <linux/netfilter/ipset/pfxlen.h> | |
22 | #include <linux/netfilter/ipset/ip_set.h> | |
21f45020 JK |
23 | #include <linux/netfilter/ipset/ip_set_getport.h> |
24 | #include <linux/netfilter/ipset/ip_set_hash.h> | |
25 | ||
35b8dcf8 JK |
26 | #define IPSET_TYPE_REV_MIN 0 |
27 | /* 1 SCTP and UDPLITE support added */ | |
28 | /* 2 Range as input support for IPv4 added */ | |
29 | /* 3 nomatch flag support added */ | |
fda75c6d OS |
30 | /* 4 Counters support added */ |
31 | #define IPSET_TYPE_REV_MAX 5 /* Comments support added */ | |
10111a6e | 32 | |
21f45020 JK |
33 | MODULE_LICENSE("GPL"); |
34 | MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>"); | |
35b8dcf8 | 35 | IP_SET_MODULE_DESC("hash:net,port", IPSET_TYPE_REV_MIN, IPSET_TYPE_REV_MAX); |
21f45020 JK |
36 | MODULE_ALIAS("ip_set_hash:net,port"); |
37 | ||
38 | /* Type specific function prefix */ | |
5d50e1d8 JK |
39 | #define HTYPE hash_netport |
40 | #define IP_SET_HASH_WITH_PROTO | |
41 | #define IP_SET_HASH_WITH_NETS | |
21f45020 | 42 | |
2a7cef2a JK |
43 | /* We squeeze the "nomatch" flag into cidr: we don't support cidr == 0 |
44 | * However this way we have to store internally cidr - 1, | |
45 | * dancing back and forth. | |
46 | */ | |
47 | #define IP_SET_HASH_WITH_NETS_PACKED | |
48 | ||
03c8b234 | 49 | /* IPv4 variant */ |
5d50e1d8 JK |
50 | |
51 | /* Member elements */ | |
21f45020 JK |
52 | struct hash_netport4_elem { |
53 | __be32 ip; | |
54 | __be16 port; | |
55 | u8 proto; | |
2a7cef2a JK |
56 | u8 cidr:7; |
57 | u8 nomatch:1; | |
21f45020 JK |
58 | }; |
59 | ||
5d50e1d8 JK |
60 | /* Common functions */ |
61 | ||
21f45020 JK |
62 | static inline bool |
63 | hash_netport4_data_equal(const struct hash_netport4_elem *ip1, | |
89dc79b7 JK |
64 | const struct hash_netport4_elem *ip2, |
65 | u32 *multi) | |
21f45020 JK |
66 | { |
67 | return ip1->ip == ip2->ip && | |
68 | ip1->port == ip2->port && | |
69 | ip1->proto == ip2->proto && | |
70 | ip1->cidr == ip2->cidr; | |
71 | } | |
72 | ||
5d50e1d8 JK |
73 | static inline int |
74 | hash_netport4_do_data_match(const struct hash_netport4_elem *elem) | |
21f45020 | 75 | { |
5d50e1d8 | 76 | return elem->nomatch ? -ENOTEMPTY : 1; |
21f45020 JK |
77 | } |
78 | ||
79 | static inline void | |
5d50e1d8 | 80 | hash_netport4_data_set_flags(struct hash_netport4_elem *elem, u32 flags) |
21f45020 | 81 | { |
5d50e1d8 | 82 | elem->nomatch = !!((flags >> 16) & IPSET_FLAG_NOMATCH); |
2a7cef2a JK |
83 | } |
84 | ||
85 | static inline void | |
5d50e1d8 | 86 | hash_netport4_data_reset_flags(struct hash_netport4_elem *elem, u8 *flags) |
2a7cef2a | 87 | { |
5d50e1d8 | 88 | swap(*flags, elem->nomatch); |
21f45020 JK |
89 | } |
90 | ||
91 | static inline void | |
92 | hash_netport4_data_netmask(struct hash_netport4_elem *elem, u8 cidr) | |
93 | { | |
94 | elem->ip &= ip_set_netmask(cidr); | |
2a7cef2a | 95 | elem->cidr = cidr - 1; |
21f45020 JK |
96 | } |
97 | ||
21f45020 JK |
98 | static bool |
99 | hash_netport4_data_list(struct sk_buff *skb, | |
100 | const struct hash_netport4_elem *data) | |
101 | { | |
2a7cef2a JK |
102 | u32 flags = data->nomatch ? IPSET_FLAG_NOMATCH : 0; |
103 | ||
7cf7899d DM |
104 | if (nla_put_ipaddr4(skb, IPSET_ATTR_IP, data->ip) || |
105 | nla_put_net16(skb, IPSET_ATTR_PORT, data->port) || | |
106 | nla_put_u8(skb, IPSET_ATTR_CIDR, data->cidr + 1) || | |
107 | nla_put_u8(skb, IPSET_ATTR_PROTO, data->proto) || | |
108 | (flags && | |
109 | nla_put_net32(skb, IPSET_ATTR_CADT_FLAGS, htonl(flags)))) | |
110 | goto nla_put_failure; | |
21f45020 JK |
111 | return 0; |
112 | ||
113 | nla_put_failure: | |
114 | return 1; | |
115 | } | |
116 | ||
5d50e1d8 JK |
117 | static inline void |
118 | hash_netport4_data_next(struct hash_netport4_elem *next, | |
119 | const struct hash_netport4_elem *d) | |
21f45020 | 120 | { |
5d50e1d8 JK |
121 | next->ip = d->ip; |
122 | next->port = d->port; | |
21f45020 JK |
123 | } |
124 | ||
5d50e1d8 | 125 | #define MTYPE hash_netport4 |
21f45020 JK |
126 | #define PF 4 |
127 | #define HOST_MASK 32 | |
5d50e1d8 | 128 | #include "ip_set_hash_gen.h" |
3d14b171 | 129 | |
21f45020 JK |
130 | static int |
131 | hash_netport4_kadt(struct ip_set *set, const struct sk_buff *skb, | |
b66554cf | 132 | const struct xt_action_param *par, |
5d50e1d8 | 133 | enum ipset_adt adt, struct ip_set_adt_opt *opt) |
21f45020 | 134 | { |
5d50e1d8 | 135 | const struct hash_netport *h = set->data; |
21f45020 | 136 | ipset_adtfn adtfn = set->variant->adt[adt]; |
5d50e1d8 | 137 | struct hash_netport4_elem e = { |
a04d8b6b | 138 | .cidr = IP_SET_INIT_CIDR(h->nets[0].cidr[0], HOST_MASK) - 1, |
9b03a5ef | 139 | }; |
ca134ce8 | 140 | struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set); |
21f45020 | 141 | |
21f45020 | 142 | if (adt == IPSET_TEST) |
5d50e1d8 | 143 | e.cidr = HOST_MASK - 1; |
21f45020 | 144 | |
ac8cc925 | 145 | if (!ip_set_get_ip4_port(skb, opt->flags & IPSET_DIM_TWO_SRC, |
5d50e1d8 | 146 | &e.port, &e.proto)) |
21f45020 JK |
147 | return -EINVAL; |
148 | ||
5d50e1d8 JK |
149 | ip4addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &e.ip); |
150 | e.ip &= ip_set_netmask(e.cidr + 1); | |
21f45020 | 151 | |
5d50e1d8 | 152 | return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags); |
21f45020 JK |
153 | } |
154 | ||
155 | static int | |
156 | hash_netport4_uadt(struct ip_set *set, struct nlattr *tb[], | |
3d14b171 | 157 | enum ipset_adt adt, u32 *lineno, u32 flags, bool retried) |
21f45020 | 158 | { |
5d50e1d8 | 159 | const struct hash_netport *h = set->data; |
21f45020 | 160 | ipset_adtfn adtfn = set->variant->adt[adt]; |
5d50e1d8 | 161 | struct hash_netport4_elem e = { .cidr = HOST_MASK - 1 }; |
ca134ce8 | 162 | struct ip_set_ext ext = IP_SET_INIT_UEXT(set); |
20b2fab4 | 163 | u32 port, port_to, p = 0, ip = 0, ip_to = 0, last; |
5e0c1eb7 | 164 | bool with_ports = false; |
2a7cef2a | 165 | u8 cidr; |
21f45020 JK |
166 | int ret; |
167 | ||
168 | if (unlikely(!tb[IPSET_ATTR_IP] || | |
169 | !ip_set_attr_netorder(tb, IPSET_ATTR_PORT) || | |
170 | !ip_set_optattr_netorder(tb, IPSET_ATTR_PORT_TO) || | |
2a7cef2a | 171 | !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT) || |
00d71b27 JK |
172 | !ip_set_optattr_netorder(tb, IPSET_ATTR_CADT_FLAGS) || |
173 | !ip_set_optattr_netorder(tb, IPSET_ATTR_PACKETS) || | |
174 | !ip_set_optattr_netorder(tb, IPSET_ATTR_BYTES))) | |
21f45020 JK |
175 | return -IPSET_ERR_PROTOCOL; |
176 | ||
177 | if (tb[IPSET_ATTR_LINENO]) | |
178 | *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]); | |
179 | ||
5d50e1d8 JK |
180 | ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP], &ip) || |
181 | ip_set_get_extensions(set, tb, &ext); | |
21f45020 JK |
182 | if (ret) |
183 | return ret; | |
184 | ||
d0d9e0a5 | 185 | if (tb[IPSET_ATTR_CIDR]) { |
2a7cef2a JK |
186 | cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]); |
187 | if (!cidr || cidr > HOST_MASK) | |
15b4d93f | 188 | return -IPSET_ERR_INVALID_CIDR; |
5d50e1d8 | 189 | e.cidr = cidr - 1; |
d0d9e0a5 | 190 | } |
21f45020 JK |
191 | |
192 | if (tb[IPSET_ATTR_PORT]) | |
5d50e1d8 | 193 | e.port = nla_get_be16(tb[IPSET_ATTR_PORT]); |
21f45020 JK |
194 | else |
195 | return -IPSET_ERR_PROTOCOL; | |
196 | ||
197 | if (tb[IPSET_ATTR_PROTO]) { | |
5d50e1d8 JK |
198 | e.proto = nla_get_u8(tb[IPSET_ATTR_PROTO]); |
199 | with_ports = ip_set_proto_with_ports(e.proto); | |
21f45020 | 200 | |
5d50e1d8 | 201 | if (e.proto == 0) |
21f45020 JK |
202 | return -IPSET_ERR_INVALID_PROTO; |
203 | } else | |
204 | return -IPSET_ERR_MISSING_PROTO; | |
205 | ||
5d50e1d8 JK |
206 | if (!(with_ports || e.proto == IPPROTO_ICMP)) |
207 | e.port = 0; | |
21f45020 | 208 | |
d0d9e0a5 | 209 | with_ports = with_ports && tb[IPSET_ATTR_PORT_TO]; |
2a7cef2a | 210 | |
43c56e59 | 211 | if (tb[IPSET_ATTR_CADT_FLAGS]) { |
2a7cef2a JK |
212 | u32 cadt_flags = ip_set_get_h32(tb[IPSET_ATTR_CADT_FLAGS]); |
213 | if (cadt_flags & IPSET_FLAG_NOMATCH) | |
43c56e59 | 214 | flags |= (IPSET_FLAG_NOMATCH << 16); |
2a7cef2a JK |
215 | } |
216 | ||
d0d9e0a5 | 217 | if (adt == IPSET_TEST || !(with_ports || tb[IPSET_ATTR_IP_TO])) { |
5d50e1d8 JK |
218 | e.ip = htonl(ip & ip_set_hostmask(e.cidr + 1)); |
219 | ret = adtfn(set, &e, &ext, &ext, flags); | |
0f1799ba | 220 | return ip_set_enomatch(ret, flags, adt, set) ? -ret : |
43c56e59 | 221 | ip_set_eexist(ret, flags) ? 0 : ret; |
21f45020 JK |
222 | } |
223 | ||
5d50e1d8 | 224 | port = port_to = ntohs(e.port); |
d0d9e0a5 JK |
225 | if (tb[IPSET_ATTR_PORT_TO]) { |
226 | port_to = ip_set_get_h16(tb[IPSET_ATTR_PORT_TO]); | |
227 | if (port_to < port) | |
228 | swap(port, port_to); | |
229 | } | |
230 | if (tb[IPSET_ATTR_IP_TO]) { | |
231 | ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP_TO], &ip_to); | |
232 | if (ret) | |
233 | return ret; | |
234 | if (ip_to < ip) | |
235 | swap(ip, ip_to); | |
236 | if (ip + UINT_MAX == ip_to) | |
237 | return -IPSET_ERR_HASH_RANGE; | |
5d50e1d8 JK |
238 | } else |
239 | ip_set_mask_from_to(ip, ip_to, e.cidr + 1); | |
21f45020 | 240 | |
3d14b171 | 241 | if (retried) |
6e27c9b4 | 242 | ip = ntohl(h->next.ip); |
d0d9e0a5 | 243 | while (!after(ip, ip_to)) { |
5d50e1d8 | 244 | e.ip = htonl(ip); |
2a7cef2a | 245 | last = ip_set_range_to_cidr(ip, ip_to, &cidr); |
5d50e1d8 | 246 | e.cidr = cidr - 1; |
6e27c9b4 JK |
247 | p = retried && ip == ntohl(h->next.ip) ? ntohs(h->next.port) |
248 | : port; | |
d0d9e0a5 | 249 | for (; p <= port_to; p++) { |
5d50e1d8 JK |
250 | e.port = htons(p); |
251 | ret = adtfn(set, &e, &ext, &ext, flags); | |
d0d9e0a5 JK |
252 | |
253 | if (ret && !ip_set_eexist(ret, flags)) | |
254 | return ret; | |
255 | else | |
256 | ret = 0; | |
257 | } | |
258 | ip = last + 1; | |
21f45020 JK |
259 | } |
260 | return ret; | |
261 | } | |
262 | ||
03c8b234 | 263 | /* IPv6 variant */ |
21f45020 JK |
264 | |
265 | struct hash_netport6_elem { | |
266 | union nf_inet_addr ip; | |
267 | __be16 port; | |
268 | u8 proto; | |
2a7cef2a JK |
269 | u8 cidr:7; |
270 | u8 nomatch:1; | |
21f45020 JK |
271 | }; |
272 | ||
5d50e1d8 JK |
273 | /* Common functions */ |
274 | ||
21f45020 JK |
275 | static inline bool |
276 | hash_netport6_data_equal(const struct hash_netport6_elem *ip1, | |
89dc79b7 JK |
277 | const struct hash_netport6_elem *ip2, |
278 | u32 *multi) | |
21f45020 | 279 | { |
29e3b160 | 280 | return ipv6_addr_equal(&ip1->ip.in6, &ip2->ip.in6) && |
21f45020 JK |
281 | ip1->port == ip2->port && |
282 | ip1->proto == ip2->proto && | |
283 | ip1->cidr == ip2->cidr; | |
284 | } | |
285 | ||
5d50e1d8 JK |
286 | static inline int |
287 | hash_netport6_do_data_match(const struct hash_netport6_elem *elem) | |
2a7cef2a | 288 | { |
5d50e1d8 | 289 | return elem->nomatch ? -ENOTEMPTY : 1; |
2a7cef2a JK |
290 | } |
291 | ||
6eb4c7e9 | 292 | static inline void |
5d50e1d8 | 293 | hash_netport6_data_set_flags(struct hash_netport6_elem *elem, u32 flags) |
2a7cef2a | 294 | { |
5d50e1d8 | 295 | elem->nomatch = !!((flags >> 16) & IPSET_FLAG_NOMATCH); |
2a7cef2a JK |
296 | } |
297 | ||
21f45020 | 298 | static inline void |
5d50e1d8 | 299 | hash_netport6_data_reset_flags(struct hash_netport6_elem *elem, u8 *flags) |
21f45020 | 300 | { |
5d50e1d8 | 301 | swap(*flags, elem->nomatch); |
21f45020 JK |
302 | } |
303 | ||
21f45020 JK |
304 | static inline void |
305 | hash_netport6_data_netmask(struct hash_netport6_elem *elem, u8 cidr) | |
306 | { | |
307 | ip6_netmask(&elem->ip, cidr); | |
2a7cef2a | 308 | elem->cidr = cidr - 1; |
21f45020 JK |
309 | } |
310 | ||
311 | static bool | |
312 | hash_netport6_data_list(struct sk_buff *skb, | |
313 | const struct hash_netport6_elem *data) | |
314 | { | |
2a7cef2a JK |
315 | u32 flags = data->nomatch ? IPSET_FLAG_NOMATCH : 0; |
316 | ||
7cf7899d DM |
317 | if (nla_put_ipaddr6(skb, IPSET_ATTR_IP, &data->ip.in6) || |
318 | nla_put_net16(skb, IPSET_ATTR_PORT, data->port) || | |
319 | nla_put_u8(skb, IPSET_ATTR_CIDR, data->cidr + 1) || | |
320 | nla_put_u8(skb, IPSET_ATTR_PROTO, data->proto) || | |
321 | (flags && | |
322 | nla_put_net32(skb, IPSET_ATTR_CADT_FLAGS, htonl(flags)))) | |
323 | goto nla_put_failure; | |
21f45020 JK |
324 | return 0; |
325 | ||
326 | nla_put_failure: | |
327 | return 1; | |
328 | } | |
329 | ||
5d50e1d8 JK |
330 | static inline void |
331 | hash_netport6_data_next(struct hash_netport4_elem *next, | |
332 | const struct hash_netport6_elem *d) | |
21f45020 | 333 | { |
5d50e1d8 | 334 | next->port = d->port; |
21f45020 JK |
335 | } |
336 | ||
5d50e1d8 | 337 | #undef MTYPE |
21f45020 JK |
338 | #undef PF |
339 | #undef HOST_MASK | |
340 | ||
5d50e1d8 | 341 | #define MTYPE hash_netport6 |
21f45020 JK |
342 | #define PF 6 |
343 | #define HOST_MASK 128 | |
5d50e1d8 JK |
344 | #define IP_SET_EMIT_CREATE |
345 | #include "ip_set_hash_gen.h" | |
3d14b171 | 346 | |
21f45020 JK |
347 | static int |
348 | hash_netport6_kadt(struct ip_set *set, const struct sk_buff *skb, | |
b66554cf | 349 | const struct xt_action_param *par, |
5d50e1d8 | 350 | enum ipset_adt adt, struct ip_set_adt_opt *opt) |
21f45020 | 351 | { |
5d50e1d8 | 352 | const struct hash_netport *h = set->data; |
21f45020 | 353 | ipset_adtfn adtfn = set->variant->adt[adt]; |
5d50e1d8 | 354 | struct hash_netport6_elem e = { |
a04d8b6b | 355 | .cidr = IP_SET_INIT_CIDR(h->nets[0].cidr[0], HOST_MASK) - 1, |
9b03a5ef | 356 | }; |
ca134ce8 | 357 | struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set); |
21f45020 | 358 | |
21f45020 | 359 | if (adt == IPSET_TEST) |
5d50e1d8 | 360 | e.cidr = HOST_MASK - 1; |
21f45020 | 361 | |
ac8cc925 | 362 | if (!ip_set_get_ip6_port(skb, opt->flags & IPSET_DIM_TWO_SRC, |
5d50e1d8 | 363 | &e.port, &e.proto)) |
21f45020 JK |
364 | return -EINVAL; |
365 | ||
5d50e1d8 JK |
366 | ip6addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &e.ip.in6); |
367 | ip6_netmask(&e.ip, e.cidr + 1); | |
21f45020 | 368 | |
5d50e1d8 | 369 | return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags); |
21f45020 JK |
370 | } |
371 | ||
372 | static int | |
373 | hash_netport6_uadt(struct ip_set *set, struct nlattr *tb[], | |
3d14b171 | 374 | enum ipset_adt adt, u32 *lineno, u32 flags, bool retried) |
21f45020 | 375 | { |
5d50e1d8 | 376 | const struct hash_netport *h = set->data; |
21f45020 | 377 | ipset_adtfn adtfn = set->variant->adt[adt]; |
5d50e1d8 | 378 | struct hash_netport6_elem e = { .cidr = HOST_MASK - 1 }; |
ca134ce8 | 379 | struct ip_set_ext ext = IP_SET_INIT_UEXT(set); |
21f45020 | 380 | u32 port, port_to; |
5e0c1eb7 | 381 | bool with_ports = false; |
2a7cef2a | 382 | u8 cidr; |
21f45020 JK |
383 | int ret; |
384 | ||
385 | if (unlikely(!tb[IPSET_ATTR_IP] || | |
386 | !ip_set_attr_netorder(tb, IPSET_ATTR_PORT) || | |
387 | !ip_set_optattr_netorder(tb, IPSET_ATTR_PORT_TO) || | |
2a7cef2a | 388 | !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT) || |
00d71b27 JK |
389 | !ip_set_optattr_netorder(tb, IPSET_ATTR_CADT_FLAGS) || |
390 | !ip_set_optattr_netorder(tb, IPSET_ATTR_PACKETS) || | |
391 | !ip_set_optattr_netorder(tb, IPSET_ATTR_BYTES))) | |
21f45020 | 392 | return -IPSET_ERR_PROTOCOL; |
d0d9e0a5 JK |
393 | if (unlikely(tb[IPSET_ATTR_IP_TO])) |
394 | return -IPSET_ERR_HASH_RANGE_UNSUPPORTED; | |
21f45020 JK |
395 | |
396 | if (tb[IPSET_ATTR_LINENO]) | |
397 | *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]); | |
398 | ||
5d50e1d8 JK |
399 | ret = ip_set_get_ipaddr6(tb[IPSET_ATTR_IP], &e.ip) || |
400 | ip_set_get_extensions(set, tb, &ext); | |
21f45020 JK |
401 | if (ret) |
402 | return ret; | |
403 | ||
2a7cef2a JK |
404 | if (tb[IPSET_ATTR_CIDR]) { |
405 | cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]); | |
406 | if (!cidr || cidr > HOST_MASK) | |
407 | return -IPSET_ERR_INVALID_CIDR; | |
5d50e1d8 | 408 | e.cidr = cidr - 1; |
2a7cef2a | 409 | } |
5d50e1d8 | 410 | ip6_netmask(&e.ip, e.cidr + 1); |
21f45020 JK |
411 | |
412 | if (tb[IPSET_ATTR_PORT]) | |
5d50e1d8 | 413 | e.port = nla_get_be16(tb[IPSET_ATTR_PORT]); |
21f45020 JK |
414 | else |
415 | return -IPSET_ERR_PROTOCOL; | |
416 | ||
417 | if (tb[IPSET_ATTR_PROTO]) { | |
5d50e1d8 JK |
418 | e.proto = nla_get_u8(tb[IPSET_ATTR_PROTO]); |
419 | with_ports = ip_set_proto_with_ports(e.proto); | |
21f45020 | 420 | |
5d50e1d8 | 421 | if (e.proto == 0) |
21f45020 JK |
422 | return -IPSET_ERR_INVALID_PROTO; |
423 | } else | |
424 | return -IPSET_ERR_MISSING_PROTO; | |
425 | ||
5d50e1d8 JK |
426 | if (!(with_ports || e.proto == IPPROTO_ICMPV6)) |
427 | e.port = 0; | |
21f45020 | 428 | |
43c56e59 | 429 | if (tb[IPSET_ATTR_CADT_FLAGS]) { |
2a7cef2a JK |
430 | u32 cadt_flags = ip_set_get_h32(tb[IPSET_ATTR_CADT_FLAGS]); |
431 | if (cadt_flags & IPSET_FLAG_NOMATCH) | |
43c56e59 | 432 | flags |= (IPSET_FLAG_NOMATCH << 16); |
2a7cef2a JK |
433 | } |
434 | ||
5e0c1eb7 | 435 | if (adt == IPSET_TEST || !with_ports || !tb[IPSET_ATTR_PORT_TO]) { |
5d50e1d8 | 436 | ret = adtfn(set, &e, &ext, &ext, flags); |
0f1799ba | 437 | return ip_set_enomatch(ret, flags, adt, set) ? -ret : |
43c56e59 | 438 | ip_set_eexist(ret, flags) ? 0 : ret; |
21f45020 JK |
439 | } |
440 | ||
5d50e1d8 | 441 | port = ntohs(e.port); |
21f45020 JK |
442 | port_to = ip_set_get_h16(tb[IPSET_ATTR_PORT_TO]); |
443 | if (port > port_to) | |
444 | swap(port, port_to); | |
445 | ||
3d14b171 | 446 | if (retried) |
6e27c9b4 | 447 | port = ntohs(h->next.port); |
21f45020 | 448 | for (; port <= port_to; port++) { |
5d50e1d8 JK |
449 | e.port = htons(port); |
450 | ret = adtfn(set, &e, &ext, &ext, flags); | |
21f45020 JK |
451 | |
452 | if (ret && !ip_set_eexist(ret, flags)) | |
453 | return ret; | |
454 | else | |
455 | ret = 0; | |
456 | } | |
457 | return ret; | |
458 | } | |
459 | ||
21f45020 JK |
460 | static struct ip_set_type hash_netport_type __read_mostly = { |
461 | .name = "hash:net,port", | |
462 | .protocol = IPSET_PROTOCOL, | |
3e0304a5 | 463 | .features = IPSET_TYPE_IP | IPSET_TYPE_PORT | IPSET_TYPE_NOMATCH, |
21f45020 | 464 | .dimension = IPSET_DIM_TWO, |
c15f1c83 | 465 | .family = NFPROTO_UNSPEC, |
35b8dcf8 JK |
466 | .revision_min = IPSET_TYPE_REV_MIN, |
467 | .revision_max = IPSET_TYPE_REV_MAX, | |
21f45020 JK |
468 | .create = hash_netport_create, |
469 | .create_policy = { | |
470 | [IPSET_ATTR_HASHSIZE] = { .type = NLA_U32 }, | |
471 | [IPSET_ATTR_MAXELEM] = { .type = NLA_U32 }, | |
472 | [IPSET_ATTR_PROBES] = { .type = NLA_U8 }, | |
473 | [IPSET_ATTR_RESIZE] = { .type = NLA_U8 }, | |
474 | [IPSET_ATTR_PROTO] = { .type = NLA_U8 }, | |
475 | [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 }, | |
5d50e1d8 | 476 | [IPSET_ATTR_CADT_FLAGS] = { .type = NLA_U32 }, |
21f45020 JK |
477 | }, |
478 | .adt_policy = { | |
479 | [IPSET_ATTR_IP] = { .type = NLA_NESTED }, | |
d0d9e0a5 | 480 | [IPSET_ATTR_IP_TO] = { .type = NLA_NESTED }, |
21f45020 JK |
481 | [IPSET_ATTR_PORT] = { .type = NLA_U16 }, |
482 | [IPSET_ATTR_PORT_TO] = { .type = NLA_U16 }, | |
483 | [IPSET_ATTR_PROTO] = { .type = NLA_U8 }, | |
484 | [IPSET_ATTR_CIDR] = { .type = NLA_U8 }, | |
485 | [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 }, | |
486 | [IPSET_ATTR_LINENO] = { .type = NLA_U32 }, | |
2a7cef2a | 487 | [IPSET_ATTR_CADT_FLAGS] = { .type = NLA_U32 }, |
00d71b27 JK |
488 | [IPSET_ATTR_BYTES] = { .type = NLA_U64 }, |
489 | [IPSET_ATTR_PACKETS] = { .type = NLA_U64 }, | |
fda75c6d | 490 | [IPSET_ATTR_COMMENT] = { .type = NLA_NUL_STRING }, |
21f45020 JK |
491 | }, |
492 | .me = THIS_MODULE, | |
493 | }; | |
494 | ||
495 | static int __init | |
496 | hash_netport_init(void) | |
497 | { | |
498 | return ip_set_type_register(&hash_netport_type); | |
499 | } | |
500 | ||
501 | static void __exit | |
502 | hash_netport_fini(void) | |
503 | { | |
504 | ip_set_type_unregister(&hash_netport_type); | |
505 | } | |
506 | ||
507 | module_init(hash_netport_init); | |
508 | module_exit(hash_netport_fini); |