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