1 /* Copyright (C) 2003-2013 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
2 * Copyright (C) 2013 Smoothwall Ltd. <vytas.dauksa@smoothwall.net>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
9 /* Kernel module implementing an IP set type: the hash:ip,mark type */
11 #include <linux/jhash.h>
12 #include <linux/module.h>
14 #include <linux/skbuff.h>
15 #include <linux/errno.h>
16 #include <linux/random.h>
19 #include <net/netlink.h>
22 #include <linux/netfilter.h>
23 #include <linux/netfilter/ipset/pfxlen.h>
24 #include <linux/netfilter/ipset/ip_set.h>
25 #include <linux/netfilter/ipset/ip_set_hash.h>
27 #define IPSET_TYPE_REV_MIN 0
28 /* 1 Forceadd support */
29 #define IPSET_TYPE_REV_MAX 2 /* skbinfo support */
31 MODULE_LICENSE("GPL");
32 MODULE_AUTHOR("Vytas Dauksa <vytas.dauksa@smoothwall.net>");
33 IP_SET_MODULE_DESC("hash:ip,mark", IPSET_TYPE_REV_MIN, IPSET_TYPE_REV_MAX);
34 MODULE_ALIAS("ip_set_hash:ip,mark");
36 /* Type specific function prefix */
37 #define HTYPE hash_ipmark
38 #define IP_SET_HASH_WITH_MARKMASK
43 struct hash_ipmark4_elem {
48 /* Common functions */
51 hash_ipmark4_data_equal(const struct hash_ipmark4_elem *ip1,
52 const struct hash_ipmark4_elem *ip2,
55 return ip1->ip == ip2->ip &&
56 ip1->mark == ip2->mark;
60 hash_ipmark4_data_list(struct sk_buff *skb,
61 const struct hash_ipmark4_elem *data)
63 if (nla_put_ipaddr4(skb, IPSET_ATTR_IP, data->ip) ||
64 nla_put_net32(skb, IPSET_ATTR_MARK, htonl(data->mark)))
73 hash_ipmark4_data_next(struct hash_ipmark4_elem *next,
74 const struct hash_ipmark4_elem *d)
79 #define MTYPE hash_ipmark4
81 #include "ip_set_hash_gen.h"
84 hash_ipmark4_kadt(struct ip_set *set, const struct sk_buff *skb,
85 const struct xt_action_param *par,
86 enum ipset_adt adt, struct ip_set_adt_opt *opt)
88 const struct hash_ipmark4 *h = set->data;
89 ipset_adtfn adtfn = set->variant->adt[adt];
90 struct hash_ipmark4_elem e = { };
91 struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set);
94 e.mark &= h->markmask;
96 ip4addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &e.ip);
97 return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags);
101 hash_ipmark4_uadt(struct ip_set *set, struct nlattr *tb[],
102 enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
104 const struct hash_ipmark4 *h = set->data;
105 ipset_adtfn adtfn = set->variant->adt[adt];
106 struct hash_ipmark4_elem e = { };
107 struct ip_set_ext ext = IP_SET_INIT_UEXT(set);
111 if (tb[IPSET_ATTR_LINENO])
112 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
114 if (unlikely(!tb[IPSET_ATTR_IP] ||
115 !ip_set_attr_netorder(tb, IPSET_ATTR_MARK)))
116 return -IPSET_ERR_PROTOCOL;
118 ret = ip_set_get_ipaddr4(tb[IPSET_ATTR_IP], &e.ip);
122 ret = ip_set_get_extensions(set, tb, &ext);
126 e.mark = ntohl(nla_get_be32(tb[IPSET_ATTR_MARK]));
127 e.mark &= h->markmask;
129 if (adt == IPSET_TEST ||
130 !(tb[IPSET_ATTR_IP_TO] || tb[IPSET_ATTR_CIDR])) {
131 ret = adtfn(set, &e, &ext, &ext, flags);
132 return ip_set_eexist(ret, flags) ? 0 : ret;
135 ip_to = ip = ntohl(e.ip);
136 if (tb[IPSET_ATTR_IP_TO]) {
137 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP_TO], &ip_to);
142 } else if (tb[IPSET_ATTR_CIDR]) {
143 u8 cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
145 if (!cidr || cidr > HOST_MASK)
146 return -IPSET_ERR_INVALID_CIDR;
147 ip_set_mask_from_to(ip, ip_to, cidr);
151 ip = ntohl(h->next.ip);
152 for (; ip <= ip_to; ip++) {
154 ret = adtfn(set, &e, &ext, &ext, flags);
156 if (ret && !ip_set_eexist(ret, flags))
166 struct hash_ipmark6_elem {
167 union nf_inet_addr ip;
171 /* Common functions */
174 hash_ipmark6_data_equal(const struct hash_ipmark6_elem *ip1,
175 const struct hash_ipmark6_elem *ip2,
178 return ipv6_addr_equal(&ip1->ip.in6, &ip2->ip.in6) &&
179 ip1->mark == ip2->mark;
183 hash_ipmark6_data_list(struct sk_buff *skb,
184 const struct hash_ipmark6_elem *data)
186 if (nla_put_ipaddr6(skb, IPSET_ATTR_IP, &data->ip.in6) ||
187 nla_put_net32(skb, IPSET_ATTR_MARK, htonl(data->mark)))
188 goto nla_put_failure;
196 hash_ipmark6_data_next(struct hash_ipmark6_elem *next,
197 const struct hash_ipmark6_elem *d)
204 #define MTYPE hash_ipmark6
205 #define HOST_MASK 128
206 #define IP_SET_EMIT_CREATE
207 #include "ip_set_hash_gen.h"
210 hash_ipmark6_kadt(struct ip_set *set, const struct sk_buff *skb,
211 const struct xt_action_param *par,
212 enum ipset_adt adt, struct ip_set_adt_opt *opt)
214 const struct hash_ipmark6 *h = set->data;
215 ipset_adtfn adtfn = set->variant->adt[adt];
216 struct hash_ipmark6_elem e = { };
217 struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set);
220 e.mark &= h->markmask;
222 ip6addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &e.ip.in6);
223 return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags);
227 hash_ipmark6_uadt(struct ip_set *set, struct nlattr *tb[],
228 enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
230 const struct hash_ipmark6 *h = set->data;
231 ipset_adtfn adtfn = set->variant->adt[adt];
232 struct hash_ipmark6_elem e = { };
233 struct ip_set_ext ext = IP_SET_INIT_UEXT(set);
236 if (tb[IPSET_ATTR_LINENO])
237 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
239 if (unlikely(!tb[IPSET_ATTR_IP] ||
240 !ip_set_attr_netorder(tb, IPSET_ATTR_MARK)))
241 return -IPSET_ERR_PROTOCOL;
242 if (unlikely(tb[IPSET_ATTR_IP_TO]))
243 return -IPSET_ERR_HASH_RANGE_UNSUPPORTED;
244 if (unlikely(tb[IPSET_ATTR_CIDR])) {
245 u8 cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
247 if (cidr != HOST_MASK)
248 return -IPSET_ERR_INVALID_CIDR;
251 ret = ip_set_get_ipaddr6(tb[IPSET_ATTR_IP], &e.ip);
255 ret = ip_set_get_extensions(set, tb, &ext);
259 e.mark = ntohl(nla_get_be32(tb[IPSET_ATTR_MARK]));
260 e.mark &= h->markmask;
262 if (adt == IPSET_TEST) {
263 ret = adtfn(set, &e, &ext, &ext, flags);
264 return ip_set_eexist(ret, flags) ? 0 : ret;
267 ret = adtfn(set, &e, &ext, &ext, flags);
268 if (ret && !ip_set_eexist(ret, flags))
274 static struct ip_set_type hash_ipmark_type __read_mostly = {
275 .name = "hash:ip,mark",
276 .protocol = IPSET_PROTOCOL,
277 .features = IPSET_TYPE_IP | IPSET_TYPE_MARK,
278 .dimension = IPSET_DIM_TWO,
279 .family = NFPROTO_UNSPEC,
280 .revision_min = IPSET_TYPE_REV_MIN,
281 .revision_max = IPSET_TYPE_REV_MAX,
282 .create = hash_ipmark_create,
284 [IPSET_ATTR_MARKMASK] = { .type = NLA_U32 },
285 [IPSET_ATTR_HASHSIZE] = { .type = NLA_U32 },
286 [IPSET_ATTR_MAXELEM] = { .type = NLA_U32 },
287 [IPSET_ATTR_PROBES] = { .type = NLA_U8 },
288 [IPSET_ATTR_RESIZE] = { .type = NLA_U8 },
289 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
290 [IPSET_ATTR_CADT_FLAGS] = { .type = NLA_U32 },
293 [IPSET_ATTR_IP] = { .type = NLA_NESTED },
294 [IPSET_ATTR_IP_TO] = { .type = NLA_NESTED },
295 [IPSET_ATTR_MARK] = { .type = NLA_U32 },
296 [IPSET_ATTR_CIDR] = { .type = NLA_U8 },
297 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
298 [IPSET_ATTR_LINENO] = { .type = NLA_U32 },
299 [IPSET_ATTR_BYTES] = { .type = NLA_U64 },
300 [IPSET_ATTR_PACKETS] = { .type = NLA_U64 },
301 [IPSET_ATTR_COMMENT] = { .type = NLA_NUL_STRING,
302 .len = IPSET_MAX_COMMENT_SIZE },
303 [IPSET_ATTR_SKBMARK] = { .type = NLA_U64 },
304 [IPSET_ATTR_SKBPRIO] = { .type = NLA_U32 },
305 [IPSET_ATTR_SKBQUEUE] = { .type = NLA_U16 },
311 hash_ipmark_init(void)
313 return ip_set_type_register(&hash_ipmark_type);
317 hash_ipmark_fini(void)
320 ip_set_type_unregister(&hash_ipmark_type);
323 module_init(hash_ipmark_init);
324 module_exit(hash_ipmark_fini);