1 /* Copyright (C) 2003-2013 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
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.
8 /* Kernel module implementing an IP set type: the hash:ip,port type */
10 #include <linux/jhash.h>
11 #include <linux/module.h>
13 #include <linux/skbuff.h>
14 #include <linux/errno.h>
15 #include <linux/random.h>
18 #include <net/netlink.h>
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>
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 */
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");
39 /* Type specific function prefix */
40 #define HTYPE hash_ipport
45 struct hash_ipport4_elem
{
52 /* Common functions */
55 hash_ipport4_data_equal(const struct hash_ipport4_elem
*ip1
,
56 const struct hash_ipport4_elem
*ip2
,
59 return ip1
->ip
== ip2
->ip
&&
60 ip1
->port
== ip2
->port
&&
61 ip1
->proto
== ip2
->proto
;
65 hash_ipport4_data_list(struct sk_buff
*skb
,
66 const struct hash_ipport4_elem
*data
)
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
))
79 hash_ipport4_data_next(struct hash_ipport4_elem
*next
,
80 const struct hash_ipport4_elem
*d
)
86 #define MTYPE hash_ipport4
88 #define HKEY_DATALEN sizeof(struct hash_ipport4_elem)
89 #include "ip_set_hash_gen.h"
92 hash_ipport4_kadt(struct ip_set
*set
, const struct sk_buff
*skb
,
93 const struct xt_action_param
*par
,
94 enum ipset_adt adt
, struct ip_set_adt_opt
*opt
)
96 ipset_adtfn adtfn
= set
->variant
->adt
[adt
];
97 struct hash_ipport4_elem e
= { .ip
= 0 };
98 struct ip_set_ext ext
= IP_SET_INIT_KEXT(skb
, opt
, set
);
100 if (!ip_set_get_ip4_port(skb
, opt
->flags
& IPSET_DIM_TWO_SRC
,
104 ip4addrptr(skb
, opt
->flags
& IPSET_DIM_ONE_SRC
, &e
.ip
);
105 return adtfn(set
, &e
, &ext
, &opt
->ext
, opt
->cmdflags
);
109 hash_ipport4_uadt(struct ip_set
*set
, struct nlattr
*tb
[],
110 enum ipset_adt adt
, u32
*lineno
, u32 flags
, bool retried
)
112 const struct hash_ipport
*h
= set
->data
;
113 ipset_adtfn adtfn
= set
->variant
->adt
[adt
];
114 struct hash_ipport4_elem e
= { .ip
= 0 };
115 struct ip_set_ext ext
= IP_SET_INIT_UEXT(set
);
116 u32 ip
, ip_to
= 0, p
= 0, port
, port_to
;
117 bool with_ports
= false;
120 if (unlikely(!tb
[IPSET_ATTR_IP
] ||
121 !ip_set_attr_netorder(tb
, IPSET_ATTR_PORT
) ||
122 !ip_set_optattr_netorder(tb
, IPSET_ATTR_PORT_TO
) ||
123 !ip_set_optattr_netorder(tb
, IPSET_ATTR_TIMEOUT
) ||
124 !ip_set_optattr_netorder(tb
, IPSET_ATTR_PACKETS
) ||
125 !ip_set_optattr_netorder(tb
, IPSET_ATTR_BYTES
) ||
126 !ip_set_optattr_netorder(tb
, IPSET_ATTR_SKBMARK
) ||
127 !ip_set_optattr_netorder(tb
, IPSET_ATTR_SKBPRIO
) ||
128 !ip_set_optattr_netorder(tb
, IPSET_ATTR_SKBQUEUE
)))
129 return -IPSET_ERR_PROTOCOL
;
131 if (tb
[IPSET_ATTR_LINENO
])
132 *lineno
= nla_get_u32(tb
[IPSET_ATTR_LINENO
]);
134 ret
= ip_set_get_ipaddr4(tb
[IPSET_ATTR_IP
], &e
.ip
);
138 ret
= ip_set_get_extensions(set
, tb
, &ext
);
142 if (tb
[IPSET_ATTR_PORT
])
143 e
.port
= nla_get_be16(tb
[IPSET_ATTR_PORT
]);
145 return -IPSET_ERR_PROTOCOL
;
147 if (tb
[IPSET_ATTR_PROTO
]) {
148 e
.proto
= nla_get_u8(tb
[IPSET_ATTR_PROTO
]);
149 with_ports
= ip_set_proto_with_ports(e
.proto
);
152 return -IPSET_ERR_INVALID_PROTO
;
154 return -IPSET_ERR_MISSING_PROTO
;
156 if (!(with_ports
|| e
.proto
== IPPROTO_ICMP
))
159 if (adt
== IPSET_TEST
||
160 !(tb
[IPSET_ATTR_IP_TO
] || tb
[IPSET_ATTR_CIDR
] ||
161 tb
[IPSET_ATTR_PORT_TO
])) {
162 ret
= adtfn(set
, &e
, &ext
, &ext
, flags
);
163 return ip_set_eexist(ret
, flags
) ? 0 : ret
;
166 ip_to
= ip
= ntohl(e
.ip
);
167 if (tb
[IPSET_ATTR_IP_TO
]) {
168 ret
= ip_set_get_hostipaddr4(tb
[IPSET_ATTR_IP_TO
], &ip_to
);
173 } else if (tb
[IPSET_ATTR_CIDR
]) {
174 u8 cidr
= nla_get_u8(tb
[IPSET_ATTR_CIDR
]);
176 if (!cidr
|| cidr
> 32)
177 return -IPSET_ERR_INVALID_CIDR
;
178 ip_set_mask_from_to(ip
, ip_to
, cidr
);
181 port_to
= port
= ntohs(e
.port
);
182 if (with_ports
&& tb
[IPSET_ATTR_PORT_TO
]) {
183 port_to
= ip_set_get_h16(tb
[IPSET_ATTR_PORT_TO
]);
189 ip
= ntohl(h
->next
.ip
);
190 for (; !before(ip_to
, ip
); ip
++) {
191 p
= retried
&& ip
== ntohl(h
->next
.ip
) ? ntohs(h
->next
.port
)
193 for (; p
<= port_to
; p
++) {
196 ret
= adtfn(set
, &e
, &ext
, &ext
, flags
);
198 if (ret
&& !ip_set_eexist(ret
, flags
))
209 struct hash_ipport6_elem
{
210 union nf_inet_addr ip
;
216 /* Common functions */
219 hash_ipport6_data_equal(const struct hash_ipport6_elem
*ip1
,
220 const struct hash_ipport6_elem
*ip2
,
223 return ipv6_addr_equal(&ip1
->ip
.in6
, &ip2
->ip
.in6
) &&
224 ip1
->port
== ip2
->port
&&
225 ip1
->proto
== ip2
->proto
;
229 hash_ipport6_data_list(struct sk_buff
*skb
,
230 const struct hash_ipport6_elem
*data
)
232 if (nla_put_ipaddr6(skb
, IPSET_ATTR_IP
, &data
->ip
.in6
) ||
233 nla_put_net16(skb
, IPSET_ATTR_PORT
, data
->port
) ||
234 nla_put_u8(skb
, IPSET_ATTR_PROTO
, data
->proto
))
235 goto nla_put_failure
;
243 hash_ipport6_data_next(struct hash_ipport4_elem
*next
,
244 const struct hash_ipport6_elem
*d
)
246 next
->port
= d
->port
;
253 #define MTYPE hash_ipport6
254 #define HOST_MASK 128
255 #define HKEY_DATALEN sizeof(struct hash_ipport6_elem)
256 #define IP_SET_EMIT_CREATE
257 #include "ip_set_hash_gen.h"
260 hash_ipport6_kadt(struct ip_set
*set
, const struct sk_buff
*skb
,
261 const struct xt_action_param
*par
,
262 enum ipset_adt adt
, struct ip_set_adt_opt
*opt
)
264 ipset_adtfn adtfn
= set
->variant
->adt
[adt
];
265 struct hash_ipport6_elem e
= { .ip
= { .all
= { 0 } } };
266 struct ip_set_ext ext
= IP_SET_INIT_KEXT(skb
, opt
, set
);
268 if (!ip_set_get_ip6_port(skb
, opt
->flags
& IPSET_DIM_TWO_SRC
,
272 ip6addrptr(skb
, opt
->flags
& IPSET_DIM_ONE_SRC
, &e
.ip
.in6
);
273 return adtfn(set
, &e
, &ext
, &opt
->ext
, opt
->cmdflags
);
277 hash_ipport6_uadt(struct ip_set
*set
, struct nlattr
*tb
[],
278 enum ipset_adt adt
, u32
*lineno
, u32 flags
, bool retried
)
280 const struct hash_ipport
*h
= set
->data
;
281 ipset_adtfn adtfn
= set
->variant
->adt
[adt
];
282 struct hash_ipport6_elem e
= { .ip
= { .all
= { 0 } } };
283 struct ip_set_ext ext
= IP_SET_INIT_UEXT(set
);
285 bool with_ports
= false;
288 if (unlikely(!tb
[IPSET_ATTR_IP
] ||
289 !ip_set_attr_netorder(tb
, IPSET_ATTR_PORT
) ||
290 !ip_set_optattr_netorder(tb
, IPSET_ATTR_PORT_TO
) ||
291 !ip_set_optattr_netorder(tb
, IPSET_ATTR_TIMEOUT
) ||
292 !ip_set_optattr_netorder(tb
, IPSET_ATTR_PACKETS
) ||
293 !ip_set_optattr_netorder(tb
, IPSET_ATTR_BYTES
) ||
294 !ip_set_optattr_netorder(tb
, IPSET_ATTR_SKBMARK
) ||
295 !ip_set_optattr_netorder(tb
, IPSET_ATTR_SKBPRIO
) ||
296 !ip_set_optattr_netorder(tb
, IPSET_ATTR_SKBQUEUE
) ||
297 tb
[IPSET_ATTR_IP_TO
] ||
298 tb
[IPSET_ATTR_CIDR
]))
299 return -IPSET_ERR_PROTOCOL
;
301 if (tb
[IPSET_ATTR_LINENO
])
302 *lineno
= nla_get_u32(tb
[IPSET_ATTR_LINENO
]);
304 ret
= ip_set_get_ipaddr6(tb
[IPSET_ATTR_IP
], &e
.ip
);
308 ret
= ip_set_get_extensions(set
, tb
, &ext
);
312 if (tb
[IPSET_ATTR_PORT
])
313 e
.port
= nla_get_be16(tb
[IPSET_ATTR_PORT
]);
315 return -IPSET_ERR_PROTOCOL
;
317 if (tb
[IPSET_ATTR_PROTO
]) {
318 e
.proto
= nla_get_u8(tb
[IPSET_ATTR_PROTO
]);
319 with_ports
= ip_set_proto_with_ports(e
.proto
);
322 return -IPSET_ERR_INVALID_PROTO
;
324 return -IPSET_ERR_MISSING_PROTO
;
326 if (!(with_ports
|| e
.proto
== IPPROTO_ICMPV6
))
329 if (adt
== IPSET_TEST
|| !with_ports
|| !tb
[IPSET_ATTR_PORT_TO
]) {
330 ret
= adtfn(set
, &e
, &ext
, &ext
, flags
);
331 return ip_set_eexist(ret
, flags
) ? 0 : ret
;
334 port
= ntohs(e
.port
);
335 port_to
= ip_set_get_h16(tb
[IPSET_ATTR_PORT_TO
]);
340 port
= ntohs(h
->next
.port
);
341 for (; port
<= port_to
; port
++) {
342 e
.port
= htons(port
);
343 ret
= adtfn(set
, &e
, &ext
, &ext
, flags
);
345 if (ret
&& !ip_set_eexist(ret
, flags
))
353 static struct ip_set_type hash_ipport_type __read_mostly
= {
354 .name
= "hash:ip,port",
355 .protocol
= IPSET_PROTOCOL
,
356 .features
= IPSET_TYPE_IP
| IPSET_TYPE_PORT
,
357 .dimension
= IPSET_DIM_TWO
,
358 .family
= NFPROTO_UNSPEC
,
359 .revision_min
= IPSET_TYPE_REV_MIN
,
360 .revision_max
= IPSET_TYPE_REV_MAX
,
361 .create
= hash_ipport_create
,
363 [IPSET_ATTR_HASHSIZE
] = { .type
= NLA_U32
},
364 [IPSET_ATTR_MAXELEM
] = { .type
= NLA_U32
},
365 [IPSET_ATTR_PROBES
] = { .type
= NLA_U8
},
366 [IPSET_ATTR_RESIZE
] = { .type
= NLA_U8
},
367 [IPSET_ATTR_PROTO
] = { .type
= NLA_U8
},
368 [IPSET_ATTR_TIMEOUT
] = { .type
= NLA_U32
},
369 [IPSET_ATTR_CADT_FLAGS
] = { .type
= NLA_U32
},
372 [IPSET_ATTR_IP
] = { .type
= NLA_NESTED
},
373 [IPSET_ATTR_IP_TO
] = { .type
= NLA_NESTED
},
374 [IPSET_ATTR_PORT
] = { .type
= NLA_U16
},
375 [IPSET_ATTR_PORT_TO
] = { .type
= NLA_U16
},
376 [IPSET_ATTR_CIDR
] = { .type
= NLA_U8
},
377 [IPSET_ATTR_PROTO
] = { .type
= NLA_U8
},
378 [IPSET_ATTR_TIMEOUT
] = { .type
= NLA_U32
},
379 [IPSET_ATTR_LINENO
] = { .type
= NLA_U32
},
380 [IPSET_ATTR_BYTES
] = { .type
= NLA_U64
},
381 [IPSET_ATTR_PACKETS
] = { .type
= NLA_U64
},
382 [IPSET_ATTR_COMMENT
] = { .type
= NLA_NUL_STRING
},
383 [IPSET_ATTR_SKBMARK
] = { .type
= NLA_U64
},
384 [IPSET_ATTR_SKBPRIO
] = { .type
= NLA_U32
},
385 [IPSET_ATTR_SKBQUEUE
] = { .type
= NLA_U16
},
391 hash_ipport_init(void)
393 return ip_set_type_register(&hash_ipport_type
);
397 hash_ipport_fini(void)
399 ip_set_type_unregister(&hash_ipport_type
);
402 module_init(hash_ipport_init
);
403 module_exit(hash_ipport_fini
);