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 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_hash.h>
26 #define IPSET_TYPE_REV_MIN 0
27 /* 1 Counters support */
28 /* 2 Comments support */
29 /* 3 Forceadd support */
30 #define IPSET_TYPE_REV_MAX 4 /* skbinfo support */
32 MODULE_LICENSE("GPL");
33 MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
34 IP_SET_MODULE_DESC("hash:ip", IPSET_TYPE_REV_MIN
, IPSET_TYPE_REV_MAX
);
35 MODULE_ALIAS("ip_set_hash:ip");
37 /* Type specific function prefix */
39 #define IP_SET_HASH_WITH_NETMASK
44 struct hash_ip4_elem
{
45 /* Zero valued IP addresses cannot be stored */
49 /* Common functions */
52 hash_ip4_data_equal(const struct hash_ip4_elem
*e1
,
53 const struct hash_ip4_elem
*e2
,
56 return e1
->ip
== e2
->ip
;
60 hash_ip4_data_list(struct sk_buff
*skb
, const struct hash_ip4_elem
*e
)
62 if (nla_put_ipaddr4(skb
, IPSET_ATTR_IP
, e
->ip
))
71 hash_ip4_data_next(struct hash_ip4_elem
*next
, const struct hash_ip4_elem
*e
)
76 #define MTYPE hash_ip4
78 #include "ip_set_hash_gen.h"
81 hash_ip4_kadt(struct ip_set
*set
, const struct sk_buff
*skb
,
82 const struct xt_action_param
*par
,
83 enum ipset_adt adt
, struct ip_set_adt_opt
*opt
)
85 const struct hash_ip4
*h
= set
->data
;
86 ipset_adtfn adtfn
= set
->variant
->adt
[adt
];
87 struct hash_ip4_elem e
= { 0 };
88 struct ip_set_ext ext
= IP_SET_INIT_KEXT(skb
, opt
, set
);
91 ip4addrptr(skb
, opt
->flags
& IPSET_DIM_ONE_SRC
, &ip
);
92 ip
&= ip_set_netmask(h
->netmask
);
97 return adtfn(set
, &e
, &ext
, &opt
->ext
, opt
->cmdflags
);
101 hash_ip4_uadt(struct ip_set
*set
, struct nlattr
*tb
[],
102 enum ipset_adt adt
, u32
*lineno
, u32 flags
, bool retried
)
104 const struct hash_ip4
*h
= set
->data
;
105 ipset_adtfn adtfn
= set
->variant
->adt
[adt
];
106 struct hash_ip4_elem e
= { 0 };
107 struct ip_set_ext ext
= IP_SET_INIT_UEXT(set
);
108 u32 ip
= 0, ip_to
= 0, hosts
;
111 if (tb
[IPSET_ATTR_LINENO
])
112 *lineno
= nla_get_u32(tb
[IPSET_ATTR_LINENO
]);
114 if (unlikely(!tb
[IPSET_ATTR_IP
]))
115 return -IPSET_ERR_PROTOCOL
;
117 ret
= ip_set_get_hostipaddr4(tb
[IPSET_ATTR_IP
], &ip
);
121 ret
= ip_set_get_extensions(set
, tb
, &ext
);
125 ip
&= ip_set_hostmask(h
->netmask
);
128 return -IPSET_ERR_HASH_ELEM
;
130 if (adt
== IPSET_TEST
)
131 return adtfn(set
, &e
, &ext
, &ext
, flags
);
134 if (tb
[IPSET_ATTR_IP_TO
]) {
135 ret
= ip_set_get_hostipaddr4(tb
[IPSET_ATTR_IP_TO
], &ip_to
);
140 } else if (tb
[IPSET_ATTR_CIDR
]) {
141 u8 cidr
= nla_get_u8(tb
[IPSET_ATTR_CIDR
]);
143 if (!cidr
|| cidr
> HOST_MASK
)
144 return -IPSET_ERR_INVALID_CIDR
;
145 ip_set_mask_from_to(ip
, ip_to
, cidr
);
148 hosts
= h
->netmask
== 32 ? 1 : 2 << (32 - h
->netmask
- 1);
151 ip
= ntohl(h
->next
.ip
);
154 for (; ip
<= ip_to
;) {
155 ret
= adtfn(set
, &e
, &ext
, &ext
, flags
);
156 if (ret
&& !ip_set_eexist(ret
, flags
))
171 /* Member elements */
172 struct hash_ip6_elem
{
173 union nf_inet_addr ip
;
176 /* Common functions */
179 hash_ip6_data_equal(const struct hash_ip6_elem
*ip1
,
180 const struct hash_ip6_elem
*ip2
,
183 return ipv6_addr_equal(&ip1
->ip
.in6
, &ip2
->ip
.in6
);
187 hash_ip6_netmask(union nf_inet_addr
*ip
, u8 prefix
)
189 ip6_netmask(ip
, prefix
);
193 hash_ip6_data_list(struct sk_buff
*skb
, const struct hash_ip6_elem
*e
)
195 if (nla_put_ipaddr6(skb
, IPSET_ATTR_IP
, &e
->ip
.in6
))
196 goto nla_put_failure
;
204 hash_ip6_data_next(struct hash_ip6_elem
*next
, const struct hash_ip6_elem
*e
)
211 #define MTYPE hash_ip6
212 #define HOST_MASK 128
214 #define IP_SET_EMIT_CREATE
215 #include "ip_set_hash_gen.h"
218 hash_ip6_kadt(struct ip_set
*set
, const struct sk_buff
*skb
,
219 const struct xt_action_param
*par
,
220 enum ipset_adt adt
, struct ip_set_adt_opt
*opt
)
222 const struct hash_ip6
*h
= set
->data
;
223 ipset_adtfn adtfn
= set
->variant
->adt
[adt
];
224 struct hash_ip6_elem e
= { { .all
= { 0 } } };
225 struct ip_set_ext ext
= IP_SET_INIT_KEXT(skb
, opt
, set
);
227 ip6addrptr(skb
, opt
->flags
& IPSET_DIM_ONE_SRC
, &e
.ip
.in6
);
228 hash_ip6_netmask(&e
.ip
, h
->netmask
);
229 if (ipv6_addr_any(&e
.ip
.in6
))
232 return adtfn(set
, &e
, &ext
, &opt
->ext
, opt
->cmdflags
);
236 hash_ip6_uadt(struct ip_set
*set
, struct nlattr
*tb
[],
237 enum ipset_adt adt
, u32
*lineno
, u32 flags
, bool retried
)
239 const struct hash_ip6
*h
= set
->data
;
240 ipset_adtfn adtfn
= set
->variant
->adt
[adt
];
241 struct hash_ip6_elem e
= { { .all
= { 0 } } };
242 struct ip_set_ext ext
= IP_SET_INIT_UEXT(set
);
245 if (tb
[IPSET_ATTR_LINENO
])
246 *lineno
= nla_get_u32(tb
[IPSET_ATTR_LINENO
]);
248 if (unlikely(!tb
[IPSET_ATTR_IP
]))
249 return -IPSET_ERR_PROTOCOL
;
250 if (unlikely(tb
[IPSET_ATTR_IP_TO
]))
251 return -IPSET_ERR_HASH_RANGE_UNSUPPORTED
;
252 if (unlikely(tb
[IPSET_ATTR_CIDR
])) {
253 u8 cidr
= nla_get_u8(tb
[IPSET_ATTR_CIDR
]);
255 if (cidr
!= HOST_MASK
)
256 return -IPSET_ERR_INVALID_CIDR
;
259 ret
= ip_set_get_ipaddr6(tb
[IPSET_ATTR_IP
], &e
.ip
);
263 ret
= ip_set_get_extensions(set
, tb
, &ext
);
267 hash_ip6_netmask(&e
.ip
, h
->netmask
);
268 if (ipv6_addr_any(&e
.ip
.in6
))
269 return -IPSET_ERR_HASH_ELEM
;
271 ret
= adtfn(set
, &e
, &ext
, &ext
, flags
);
273 return ip_set_eexist(ret
, flags
) ? 0 : ret
;
276 static struct ip_set_type hash_ip_type __read_mostly
= {
278 .protocol
= IPSET_PROTOCOL
,
279 .features
= IPSET_TYPE_IP
,
280 .dimension
= IPSET_DIM_ONE
,
281 .family
= NFPROTO_UNSPEC
,
282 .revision_min
= IPSET_TYPE_REV_MIN
,
283 .revision_max
= IPSET_TYPE_REV_MAX
,
284 .create
= hash_ip_create
,
286 [IPSET_ATTR_HASHSIZE
] = { .type
= NLA_U32
},
287 [IPSET_ATTR_MAXELEM
] = { .type
= NLA_U32
},
288 [IPSET_ATTR_PROBES
] = { .type
= NLA_U8
},
289 [IPSET_ATTR_RESIZE
] = { .type
= NLA_U8
},
290 [IPSET_ATTR_TIMEOUT
] = { .type
= NLA_U32
},
291 [IPSET_ATTR_NETMASK
] = { .type
= NLA_U8
},
292 [IPSET_ATTR_CADT_FLAGS
] = { .type
= NLA_U32
},
295 [IPSET_ATTR_IP
] = { .type
= NLA_NESTED
},
296 [IPSET_ATTR_IP_TO
] = { .type
= NLA_NESTED
},
297 [IPSET_ATTR_CIDR
] = { .type
= NLA_U8
},
298 [IPSET_ATTR_TIMEOUT
] = { .type
= NLA_U32
},
299 [IPSET_ATTR_LINENO
] = { .type
= NLA_U32
},
300 [IPSET_ATTR_BYTES
] = { .type
= NLA_U64
},
301 [IPSET_ATTR_PACKETS
] = { .type
= NLA_U64
},
302 [IPSET_ATTR_COMMENT
] = { .type
= NLA_NUL_STRING
,
303 .len
= IPSET_MAX_COMMENT_SIZE
},
304 [IPSET_ATTR_SKBMARK
] = { .type
= NLA_U64
},
305 [IPSET_ATTR_SKBPRIO
] = { .type
= NLA_U32
},
306 [IPSET_ATTR_SKBQUEUE
] = { .type
= NLA_U16
},
314 return ip_set_type_register(&hash_ip_type
);
321 ip_set_type_unregister(&hash_ip_type
);
324 module_init(hash_ip_init
);
325 module_exit(hash_ip_fini
);