Commit | Line | Data |
---|---|---|
5d50e1d8 | 1 | /* Copyright (C) 2003-2013 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu> |
07896ed3 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:ip,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> | |
07896ed3 JK |
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> | |
07896ed3 JK |
24 | #include <linux/netfilter/ipset/ip_set_getport.h> |
25 | #include <linux/netfilter/ipset/ip_set_hash.h> | |
26 | ||
35b8dcf8 JK |
27 | #define IPSET_TYPE_REV_MIN 0 |
28 | /* 1 SCTP and UDPLITE support added */ | |
fda75c6d | 29 | /* 2 Counters support added */ |
07cf8f5a | 30 | /* 3 Comments support added */ |
af331419 AD |
31 | /* 4 Forceadd support added */ |
32 | #define IPSET_TYPE_REV_MAX 5 /* skbinfo support added */ | |
10111a6e | 33 | |
07896ed3 JK |
34 | MODULE_LICENSE("GPL"); |
35 | MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>"); | |
35b8dcf8 | 36 | IP_SET_MODULE_DESC("hash:ip,port", IPSET_TYPE_REV_MIN, IPSET_TYPE_REV_MAX); |
07896ed3 JK |
37 | MODULE_ALIAS("ip_set_hash:ip,port"); |
38 | ||
39 | /* Type specific function prefix */ | |
5d50e1d8 | 40 | #define HTYPE hash_ipport |
07896ed3 | 41 | |
03c8b234 | 42 | /* IPv4 variant */ |
07896ed3 | 43 | |
5d50e1d8 | 44 | /* Member elements */ |
07896ed3 JK |
45 | struct hash_ipport4_elem { |
46 | __be32 ip; | |
47 | __be16 port; | |
48 | u8 proto; | |
49 | u8 padding; | |
50 | }; | |
51 | ||
5d50e1d8 JK |
52 | /* Common functions */ |
53 | ||
07896ed3 JK |
54 | static inline bool |
55 | hash_ipport4_data_equal(const struct hash_ipport4_elem *ip1, | |
89dc79b7 JK |
56 | const struct hash_ipport4_elem *ip2, |
57 | u32 *multi) | |
07896ed3 JK |
58 | { |
59 | return ip1->ip == ip2->ip && | |
60 | ip1->port == ip2->port && | |
61 | ip1->proto == ip2->proto; | |
62 | } | |
63 | ||
07896ed3 JK |
64 | static bool |
65 | hash_ipport4_data_list(struct sk_buff *skb, | |
66 | const struct hash_ipport4_elem *data) | |
67 | { | |
7cf7899d DM |
68 | if (nla_put_ipaddr4(skb, IPSET_ATTR_IP, data->ip) || |
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; | |
728a7e69 | 72 | return false; |
07896ed3 JK |
73 | |
74 | nla_put_failure: | |
728a7e69 | 75 | return true; |
07896ed3 JK |
76 | } |
77 | ||
3d14b171 | 78 | static inline void |
5d50e1d8 | 79 | hash_ipport4_data_next(struct hash_ipport4_elem *next, |
3d14b171 JK |
80 | const struct hash_ipport4_elem *d) |
81 | { | |
5d50e1d8 JK |
82 | next->ip = d->ip; |
83 | next->port = d->port; | |
3d14b171 JK |
84 | } |
85 | ||
1823fb79 SP |
86 | #define MTYPE hash_ipport4 |
87 | #define HOST_MASK 32 | |
5d50e1d8 JK |
88 | #include "ip_set_hash_gen.h" |
89 | ||
07896ed3 JK |
90 | static int |
91 | hash_ipport4_kadt(struct ip_set *set, const struct sk_buff *skb, | |
b66554cf | 92 | const struct xt_action_param *par, |
5d50e1d8 | 93 | enum ipset_adt adt, struct ip_set_adt_opt *opt) |
07896ed3 | 94 | { |
07896ed3 | 95 | ipset_adtfn adtfn = set->variant->adt[adt]; |
94729f8a | 96 | struct hash_ipport4_elem e = { .ip = 0 }; |
ca134ce8 | 97 | struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set); |
07896ed3 | 98 | |
ac8cc925 | 99 | if (!ip_set_get_ip4_port(skb, opt->flags & IPSET_DIM_TWO_SRC, |
5d50e1d8 | 100 | &e.port, &e.proto)) |
07896ed3 JK |
101 | return -EINVAL; |
102 | ||
5d50e1d8 JK |
103 | ip4addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &e.ip); |
104 | return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags); | |
07896ed3 JK |
105 | } |
106 | ||
107 | static int | |
108 | hash_ipport4_uadt(struct ip_set *set, struct nlattr *tb[], | |
3d14b171 | 109 | enum ipset_adt adt, u32 *lineno, u32 flags, bool retried) |
07896ed3 | 110 | { |
5d50e1d8 | 111 | const struct hash_ipport *h = set->data; |
07896ed3 | 112 | ipset_adtfn adtfn = set->variant->adt[adt]; |
94729f8a | 113 | struct hash_ipport4_elem e = { .ip = 0 }; |
ca134ce8 | 114 | struct ip_set_ext ext = IP_SET_INIT_UEXT(set); |
20b2fab4 | 115 | u32 ip, ip_to = 0, p = 0, port, port_to; |
5e0c1eb7 | 116 | bool with_ports = false; |
07896ed3 JK |
117 | int ret; |
118 | ||
a212e08e SP |
119 | if (tb[IPSET_ATTR_LINENO]) |
120 | *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]); | |
121 | ||
07896ed3 JK |
122 | if (unlikely(!tb[IPSET_ATTR_IP] || |
123 | !ip_set_attr_netorder(tb, IPSET_ATTR_PORT) || | |
7dd37bc8 | 124 | !ip_set_optattr_netorder(tb, IPSET_ATTR_PORT_TO))) |
07896ed3 JK |
125 | return -IPSET_ERR_PROTOCOL; |
126 | ||
8e55d2e5 SP |
127 | ret = ip_set_get_ipaddr4(tb[IPSET_ATTR_IP], &e.ip); |
128 | if (ret) | |
129 | return ret; | |
130 | ||
131 | ret = ip_set_get_extensions(set, tb, &ext); | |
07896ed3 JK |
132 | if (ret) |
133 | return ret; | |
134 | ||
d25472e4 | 135 | e.port = nla_get_be16(tb[IPSET_ATTR_PORT]); |
07896ed3 JK |
136 | |
137 | if (tb[IPSET_ATTR_PROTO]) { | |
5d50e1d8 JK |
138 | e.proto = nla_get_u8(tb[IPSET_ATTR_PROTO]); |
139 | with_ports = ip_set_proto_with_ports(e.proto); | |
07896ed3 | 140 | |
5d50e1d8 | 141 | if (e.proto == 0) |
07896ed3 | 142 | return -IPSET_ERR_INVALID_PROTO; |
ca0f6a5c | 143 | } else { |
07896ed3 | 144 | return -IPSET_ERR_MISSING_PROTO; |
ca0f6a5c | 145 | } |
07896ed3 | 146 | |
5d50e1d8 JK |
147 | if (!(with_ports || e.proto == IPPROTO_ICMP)) |
148 | e.port = 0; | |
07896ed3 JK |
149 | |
150 | if (adt == IPSET_TEST || | |
07896ed3 JK |
151 | !(tb[IPSET_ATTR_IP_TO] || tb[IPSET_ATTR_CIDR] || |
152 | tb[IPSET_ATTR_PORT_TO])) { | |
5d50e1d8 | 153 | ret = adtfn(set, &e, &ext, &ext, flags); |
07896ed3 JK |
154 | return ip_set_eexist(ret, flags) ? 0 : ret; |
155 | } | |
156 | ||
5d50e1d8 | 157 | ip_to = ip = ntohl(e.ip); |
07896ed3 JK |
158 | if (tb[IPSET_ATTR_IP_TO]) { |
159 | ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP_TO], &ip_to); | |
160 | if (ret) | |
161 | return ret; | |
162 | if (ip > ip_to) | |
163 | swap(ip, ip_to); | |
164 | } else if (tb[IPSET_ATTR_CIDR]) { | |
165 | u8 cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]); | |
166 | ||
cabfd139 | 167 | if (!cidr || cidr > HOST_MASK) |
07896ed3 | 168 | return -IPSET_ERR_INVALID_CIDR; |
e6146e86 | 169 | ip_set_mask_from_to(ip, ip_to, cidr); |
4fe198e6 | 170 | } |
07896ed3 | 171 | |
5d50e1d8 | 172 | port_to = port = ntohs(e.port); |
5e0c1eb7 | 173 | if (with_ports && tb[IPSET_ATTR_PORT_TO]) { |
07896ed3 JK |
174 | port_to = ip_set_get_h16(tb[IPSET_ATTR_PORT_TO]); |
175 | if (port > port_to) | |
176 | swap(port, port_to); | |
5e0c1eb7 | 177 | } |
07896ed3 | 178 | |
3d14b171 | 179 | if (retried) |
6e27c9b4 | 180 | ip = ntohl(h->next.ip); |
3d14b171 | 181 | for (; !before(ip_to, ip); ip++) { |
6e27c9b4 JK |
182 | p = retried && ip == ntohl(h->next.ip) ? ntohs(h->next.port) |
183 | : port; | |
3d14b171 | 184 | for (; p <= port_to; p++) { |
5d50e1d8 JK |
185 | e.ip = htonl(ip); |
186 | e.port = htons(p); | |
187 | ret = adtfn(set, &e, &ext, &ext, flags); | |
07896ed3 JK |
188 | |
189 | if (ret && !ip_set_eexist(ret, flags)) | |
190 | return ret; | |
ca0f6a5c JK |
191 | |
192 | ret = 0; | |
07896ed3 | 193 | } |
3d14b171 | 194 | } |
07896ed3 JK |
195 | return ret; |
196 | } | |
197 | ||
03c8b234 | 198 | /* IPv6 variant */ |
07896ed3 JK |
199 | |
200 | struct hash_ipport6_elem { | |
201 | union nf_inet_addr ip; | |
202 | __be16 port; | |
203 | u8 proto; | |
204 | u8 padding; | |
205 | }; | |
206 | ||
5d50e1d8 JK |
207 | /* Common functions */ |
208 | ||
07896ed3 JK |
209 | static inline bool |
210 | hash_ipport6_data_equal(const struct hash_ipport6_elem *ip1, | |
89dc79b7 JK |
211 | const struct hash_ipport6_elem *ip2, |
212 | u32 *multi) | |
07896ed3 | 213 | { |
29e3b160 | 214 | return ipv6_addr_equal(&ip1->ip.in6, &ip2->ip.in6) && |
07896ed3 JK |
215 | ip1->port == ip2->port && |
216 | ip1->proto == ip2->proto; | |
217 | } | |
218 | ||
07896ed3 JK |
219 | static bool |
220 | hash_ipport6_data_list(struct sk_buff *skb, | |
221 | const struct hash_ipport6_elem *data) | |
222 | { | |
7cf7899d DM |
223 | if (nla_put_ipaddr6(skb, IPSET_ATTR_IP, &data->ip.in6) || |
224 | nla_put_net16(skb, IPSET_ATTR_PORT, data->port) || | |
225 | nla_put_u8(skb, IPSET_ATTR_PROTO, data->proto)) | |
226 | goto nla_put_failure; | |
728a7e69 | 227 | return false; |
07896ed3 JK |
228 | |
229 | nla_put_failure: | |
728a7e69 | 230 | return true; |
07896ed3 JK |
231 | } |
232 | ||
5d50e1d8 JK |
233 | static inline void |
234 | hash_ipport6_data_next(struct hash_ipport4_elem *next, | |
235 | const struct hash_ipport6_elem *d) | |
07896ed3 | 236 | { |
5d50e1d8 | 237 | next->port = d->port; |
07896ed3 JK |
238 | } |
239 | ||
5d50e1d8 | 240 | #undef MTYPE |
07896ed3 JK |
241 | #undef HOST_MASK |
242 | ||
5d50e1d8 | 243 | #define MTYPE hash_ipport6 |
07896ed3 | 244 | #define HOST_MASK 128 |
1823fb79 | 245 | #define IP_SET_EMIT_CREATE |
5d50e1d8 | 246 | #include "ip_set_hash_gen.h" |
3d14b171 | 247 | |
07896ed3 JK |
248 | static int |
249 | hash_ipport6_kadt(struct ip_set *set, const struct sk_buff *skb, | |
b66554cf | 250 | const struct xt_action_param *par, |
5d50e1d8 | 251 | enum ipset_adt adt, struct ip_set_adt_opt *opt) |
07896ed3 | 252 | { |
07896ed3 | 253 | ipset_adtfn adtfn = set->variant->adt[adt]; |
94729f8a | 254 | struct hash_ipport6_elem e = { .ip = { .all = { 0 } } }; |
ca134ce8 | 255 | struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set); |
07896ed3 | 256 | |
ac8cc925 | 257 | if (!ip_set_get_ip6_port(skb, opt->flags & IPSET_DIM_TWO_SRC, |
5d50e1d8 | 258 | &e.port, &e.proto)) |
07896ed3 JK |
259 | return -EINVAL; |
260 | ||
5d50e1d8 JK |
261 | ip6addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &e.ip.in6); |
262 | return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags); | |
07896ed3 JK |
263 | } |
264 | ||
265 | static int | |
266 | hash_ipport6_uadt(struct ip_set *set, struct nlattr *tb[], | |
3d14b171 | 267 | enum ipset_adt adt, u32 *lineno, u32 flags, bool retried) |
07896ed3 | 268 | { |
5d50e1d8 | 269 | const struct hash_ipport *h = set->data; |
07896ed3 | 270 | ipset_adtfn adtfn = set->variant->adt[adt]; |
94729f8a | 271 | struct hash_ipport6_elem e = { .ip = { .all = { 0 } } }; |
ca134ce8 | 272 | struct ip_set_ext ext = IP_SET_INIT_UEXT(set); |
07896ed3 | 273 | u32 port, port_to; |
5e0c1eb7 | 274 | bool with_ports = false; |
07896ed3 JK |
275 | int ret; |
276 | ||
a212e08e SP |
277 | if (tb[IPSET_ATTR_LINENO]) |
278 | *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]); | |
279 | ||
07896ed3 JK |
280 | if (unlikely(!tb[IPSET_ATTR_IP] || |
281 | !ip_set_attr_netorder(tb, IPSET_ATTR_PORT) || | |
2c227f27 | 282 | !ip_set_optattr_netorder(tb, IPSET_ATTR_PORT_TO))) |
07896ed3 | 283 | return -IPSET_ERR_PROTOCOL; |
2c227f27 SP |
284 | if (unlikely(tb[IPSET_ATTR_IP_TO])) |
285 | return -IPSET_ERR_HASH_RANGE_UNSUPPORTED; | |
286 | if (unlikely(tb[IPSET_ATTR_CIDR])) { | |
287 | u8 cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]); | |
288 | ||
289 | if (cidr != HOST_MASK) | |
290 | return -IPSET_ERR_INVALID_CIDR; | |
291 | } | |
07896ed3 | 292 | |
8e55d2e5 SP |
293 | ret = ip_set_get_ipaddr6(tb[IPSET_ATTR_IP], &e.ip); |
294 | if (ret) | |
295 | return ret; | |
296 | ||
297 | ret = ip_set_get_extensions(set, tb, &ext); | |
07896ed3 JK |
298 | if (ret) |
299 | return ret; | |
300 | ||
d25472e4 | 301 | e.port = nla_get_be16(tb[IPSET_ATTR_PORT]); |
07896ed3 JK |
302 | |
303 | if (tb[IPSET_ATTR_PROTO]) { | |
5d50e1d8 JK |
304 | e.proto = nla_get_u8(tb[IPSET_ATTR_PROTO]); |
305 | with_ports = ip_set_proto_with_ports(e.proto); | |
07896ed3 | 306 | |
5d50e1d8 | 307 | if (e.proto == 0) |
07896ed3 | 308 | return -IPSET_ERR_INVALID_PROTO; |
ca0f6a5c | 309 | } else { |
07896ed3 | 310 | return -IPSET_ERR_MISSING_PROTO; |
ca0f6a5c | 311 | } |
07896ed3 | 312 | |
5d50e1d8 JK |
313 | if (!(with_ports || e.proto == IPPROTO_ICMPV6)) |
314 | e.port = 0; | |
07896ed3 | 315 | |
5e0c1eb7 | 316 | if (adt == IPSET_TEST || !with_ports || !tb[IPSET_ATTR_PORT_TO]) { |
5d50e1d8 | 317 | ret = adtfn(set, &e, &ext, &ext, flags); |
07896ed3 JK |
318 | return ip_set_eexist(ret, flags) ? 0 : ret; |
319 | } | |
320 | ||
5d50e1d8 | 321 | port = ntohs(e.port); |
07896ed3 JK |
322 | port_to = ip_set_get_h16(tb[IPSET_ATTR_PORT_TO]); |
323 | if (port > port_to) | |
324 | swap(port, port_to); | |
325 | ||
3d14b171 | 326 | if (retried) |
6e27c9b4 | 327 | port = ntohs(h->next.port); |
07896ed3 | 328 | for (; port <= port_to; port++) { |
5d50e1d8 JK |
329 | e.port = htons(port); |
330 | ret = adtfn(set, &e, &ext, &ext, flags); | |
07896ed3 JK |
331 | |
332 | if (ret && !ip_set_eexist(ret, flags)) | |
333 | return ret; | |
ca0f6a5c JK |
334 | |
335 | ret = 0; | |
07896ed3 JK |
336 | } |
337 | return ret; | |
338 | } | |
339 | ||
07896ed3 JK |
340 | static struct ip_set_type hash_ipport_type __read_mostly = { |
341 | .name = "hash:ip,port", | |
342 | .protocol = IPSET_PROTOCOL, | |
343 | .features = IPSET_TYPE_IP | IPSET_TYPE_PORT, | |
344 | .dimension = IPSET_DIM_TWO, | |
c15f1c83 | 345 | .family = NFPROTO_UNSPEC, |
35b8dcf8 JK |
346 | .revision_min = IPSET_TYPE_REV_MIN, |
347 | .revision_max = IPSET_TYPE_REV_MAX, | |
07896ed3 JK |
348 | .create = hash_ipport_create, |
349 | .create_policy = { | |
350 | [IPSET_ATTR_HASHSIZE] = { .type = NLA_U32 }, | |
351 | [IPSET_ATTR_MAXELEM] = { .type = NLA_U32 }, | |
352 | [IPSET_ATTR_PROBES] = { .type = NLA_U8 }, | |
353 | [IPSET_ATTR_RESIZE] = { .type = NLA_U8 }, | |
354 | [IPSET_ATTR_PROTO] = { .type = NLA_U8 }, | |
355 | [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 }, | |
00d71b27 | 356 | [IPSET_ATTR_CADT_FLAGS] = { .type = NLA_U32 }, |
07896ed3 JK |
357 | }, |
358 | .adt_policy = { | |
359 | [IPSET_ATTR_IP] = { .type = NLA_NESTED }, | |
360 | [IPSET_ATTR_IP_TO] = { .type = NLA_NESTED }, | |
361 | [IPSET_ATTR_PORT] = { .type = NLA_U16 }, | |
362 | [IPSET_ATTR_PORT_TO] = { .type = NLA_U16 }, | |
363 | [IPSET_ATTR_CIDR] = { .type = NLA_U8 }, | |
364 | [IPSET_ATTR_PROTO] = { .type = NLA_U8 }, | |
365 | [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 }, | |
366 | [IPSET_ATTR_LINENO] = { .type = NLA_U32 }, | |
00d71b27 JK |
367 | [IPSET_ATTR_BYTES] = { .type = NLA_U64 }, |
368 | [IPSET_ATTR_PACKETS] = { .type = NLA_U64 }, | |
03726186 SP |
369 | [IPSET_ATTR_COMMENT] = { .type = NLA_NUL_STRING, |
370 | .len = IPSET_MAX_COMMENT_SIZE }, | |
af331419 AD |
371 | [IPSET_ATTR_SKBMARK] = { .type = NLA_U64 }, |
372 | [IPSET_ATTR_SKBPRIO] = { .type = NLA_U32 }, | |
373 | [IPSET_ATTR_SKBQUEUE] = { .type = NLA_U16 }, | |
07896ed3 JK |
374 | }, |
375 | .me = THIS_MODULE, | |
376 | }; | |
377 | ||
378 | static int __init | |
379 | hash_ipport_init(void) | |
380 | { | |
381 | return ip_set_type_register(&hash_ipport_type); | |
382 | } | |
383 | ||
384 | static void __exit | |
385 | hash_ipport_fini(void) | |
386 | { | |
18f84d41 | 387 | rcu_barrier(); |
07896ed3 JK |
388 | ip_set_type_unregister(&hash_ipport_type); |
389 | } | |
390 | ||
391 | module_init(hash_ipport_init); | |
392 | module_exit(hash_ipport_fini); |