ip_set_hash_ip.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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. /* Kernel module implementing an IP set type: the hash:ip type */
  8. #include <linux/jhash.h>
  9. #include <linux/module.h>
  10. #include <linux/ip.h>
  11. #include <linux/skbuff.h>
  12. #include <linux/errno.h>
  13. #include <linux/random.h>
  14. #include <net/ip.h>
  15. #include <net/ipv6.h>
  16. #include <net/netlink.h>
  17. #include <net/tcp.h>
  18. #include <linux/netfilter.h>
  19. #include <linux/netfilter/ipset/pfxlen.h>
  20. #include <linux/netfilter/ipset/ip_set.h>
  21. #include <linux/netfilter/ipset/ip_set_hash.h>
  22. #define IPSET_TYPE_REV_MIN 0
  23. /* 1 Counters support */
  24. /* 2 Comments support */
  25. /* 3 Forceadd support */
  26. #define IPSET_TYPE_REV_MAX 4 /* skbinfo support */
  27. MODULE_LICENSE("GPL");
  28. MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
  29. IP_SET_MODULE_DESC("hash:ip", IPSET_TYPE_REV_MIN, IPSET_TYPE_REV_MAX);
  30. MODULE_ALIAS("ip_set_hash:ip");
  31. /* Type specific function prefix */
  32. #define HTYPE hash_ip
  33. #define IP_SET_HASH_WITH_NETMASK
  34. /* IPv4 variant */
  35. /* Member elements */
  36. struct hash_ip4_elem {
  37. /* Zero valued IP addresses cannot be stored */
  38. __be32 ip;
  39. };
  40. /* Common functions */
  41. static inline bool
  42. hash_ip4_data_equal(const struct hash_ip4_elem *e1,
  43. const struct hash_ip4_elem *e2,
  44. u32 *multi)
  45. {
  46. return e1->ip == e2->ip;
  47. }
  48. static bool
  49. hash_ip4_data_list(struct sk_buff *skb, const struct hash_ip4_elem *e)
  50. {
  51. if (nla_put_ipaddr4(skb, IPSET_ATTR_IP, e->ip))
  52. goto nla_put_failure;
  53. return false;
  54. nla_put_failure:
  55. return true;
  56. }
  57. static inline void
  58. hash_ip4_data_next(struct hash_ip4_elem *next, const struct hash_ip4_elem *e)
  59. {
  60. next->ip = e->ip;
  61. }
  62. #define MTYPE hash_ip4
  63. #define HOST_MASK 32
  64. #include "ip_set_hash_gen.h"
  65. static int
  66. hash_ip4_kadt(struct ip_set *set, const struct sk_buff *skb,
  67. const struct xt_action_param *par,
  68. enum ipset_adt adt, struct ip_set_adt_opt *opt)
  69. {
  70. const struct hash_ip *h = set->data;
  71. ipset_adtfn adtfn = set->variant->adt[adt];
  72. struct hash_ip4_elem e = { 0 };
  73. struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set);
  74. __be32 ip;
  75. ip4addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &ip);
  76. ip &= ip_set_netmask(h->netmask);
  77. if (ip == 0)
  78. return -EINVAL;
  79. e.ip = ip;
  80. return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags);
  81. }
  82. static int
  83. hash_ip4_uadt(struct ip_set *set, struct nlattr *tb[],
  84. enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
  85. {
  86. const struct hash_ip *h = set->data;
  87. ipset_adtfn adtfn = set->variant->adt[adt];
  88. struct hash_ip4_elem e = { 0 };
  89. struct ip_set_ext ext = IP_SET_INIT_UEXT(set);
  90. u32 ip = 0, ip_to = 0, hosts;
  91. int ret = 0;
  92. if (tb[IPSET_ATTR_LINENO])
  93. *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
  94. if (unlikely(!tb[IPSET_ATTR_IP]))
  95. return -IPSET_ERR_PROTOCOL;
  96. ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP], &ip);
  97. if (ret)
  98. return ret;
  99. ret = ip_set_get_extensions(set, tb, &ext);
  100. if (ret)
  101. return ret;
  102. ip &= ip_set_hostmask(h->netmask);
  103. if (adt == IPSET_TEST) {
  104. e.ip = htonl(ip);
  105. if (e.ip == 0)
  106. return -IPSET_ERR_HASH_ELEM;
  107. return adtfn(set, &e, &ext, &ext, flags);
  108. }
  109. ip_to = ip;
  110. if (tb[IPSET_ATTR_IP_TO]) {
  111. ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP_TO], &ip_to);
  112. if (ret)
  113. return ret;
  114. if (ip > ip_to)
  115. swap(ip, ip_to);
  116. } else if (tb[IPSET_ATTR_CIDR]) {
  117. u8 cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
  118. if (!cidr || cidr > HOST_MASK)
  119. return -IPSET_ERR_INVALID_CIDR;
  120. ip_set_mask_from_to(ip, ip_to, cidr);
  121. }
  122. hosts = h->netmask == 32 ? 1 : 2 << (32 - h->netmask - 1);
  123. if (retried)
  124. ip = ntohl(h->next.ip);
  125. for (; !before(ip_to, ip); ip += hosts) {
  126. e.ip = htonl(ip);
  127. if (e.ip == 0)
  128. return -IPSET_ERR_HASH_ELEM;
  129. ret = adtfn(set, &e, &ext, &ext, flags);
  130. if (ret && !ip_set_eexist(ret, flags))
  131. return ret;
  132. ret = 0;
  133. }
  134. return ret;
  135. }
  136. /* IPv6 variant */
  137. /* Member elements */
  138. struct hash_ip6_elem {
  139. union nf_inet_addr ip;
  140. };
  141. /* Common functions */
  142. static inline bool
  143. hash_ip6_data_equal(const struct hash_ip6_elem *ip1,
  144. const struct hash_ip6_elem *ip2,
  145. u32 *multi)
  146. {
  147. return ipv6_addr_equal(&ip1->ip.in6, &ip2->ip.in6);
  148. }
  149. static inline void
  150. hash_ip6_netmask(union nf_inet_addr *ip, u8 prefix)
  151. {
  152. ip6_netmask(ip, prefix);
  153. }
  154. static bool
  155. hash_ip6_data_list(struct sk_buff *skb, const struct hash_ip6_elem *e)
  156. {
  157. if (nla_put_ipaddr6(skb, IPSET_ATTR_IP, &e->ip.in6))
  158. goto nla_put_failure;
  159. return false;
  160. nla_put_failure:
  161. return true;
  162. }
  163. static inline void
  164. hash_ip6_data_next(struct hash_ip4_elem *next, const struct hash_ip6_elem *e)
  165. {
  166. }
  167. #undef MTYPE
  168. #undef HOST_MASK
  169. #define MTYPE hash_ip6
  170. #define HOST_MASK 128
  171. #define IP_SET_EMIT_CREATE
  172. #include "ip_set_hash_gen.h"
  173. static int
  174. hash_ip6_kadt(struct ip_set *set, const struct sk_buff *skb,
  175. const struct xt_action_param *par,
  176. enum ipset_adt adt, struct ip_set_adt_opt *opt)
  177. {
  178. const struct hash_ip *h = set->data;
  179. ipset_adtfn adtfn = set->variant->adt[adt];
  180. struct hash_ip6_elem e = { { .all = { 0 } } };
  181. struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set);
  182. ip6addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &e.ip.in6);
  183. hash_ip6_netmask(&e.ip, h->netmask);
  184. if (ipv6_addr_any(&e.ip.in6))
  185. return -EINVAL;
  186. return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags);
  187. }
  188. static int
  189. hash_ip6_uadt(struct ip_set *set, struct nlattr *tb[],
  190. enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
  191. {
  192. const struct hash_ip *h = set->data;
  193. ipset_adtfn adtfn = set->variant->adt[adt];
  194. struct hash_ip6_elem e = { { .all = { 0 } } };
  195. struct ip_set_ext ext = IP_SET_INIT_UEXT(set);
  196. int ret;
  197. if (tb[IPSET_ATTR_LINENO])
  198. *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
  199. if (unlikely(!tb[IPSET_ATTR_IP]))
  200. return -IPSET_ERR_PROTOCOL;
  201. if (unlikely(tb[IPSET_ATTR_IP_TO]))
  202. return -IPSET_ERR_HASH_RANGE_UNSUPPORTED;
  203. if (unlikely(tb[IPSET_ATTR_CIDR])) {
  204. u8 cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
  205. if (cidr != HOST_MASK)
  206. return -IPSET_ERR_INVALID_CIDR;
  207. }
  208. ret = ip_set_get_ipaddr6(tb[IPSET_ATTR_IP], &e.ip);
  209. if (ret)
  210. return ret;
  211. ret = ip_set_get_extensions(set, tb, &ext);
  212. if (ret)
  213. return ret;
  214. hash_ip6_netmask(&e.ip, h->netmask);
  215. if (ipv6_addr_any(&e.ip.in6))
  216. return -IPSET_ERR_HASH_ELEM;
  217. ret = adtfn(set, &e, &ext, &ext, flags);
  218. return ip_set_eexist(ret, flags) ? 0 : ret;
  219. }
  220. static struct ip_set_type hash_ip_type __read_mostly = {
  221. .name = "hash:ip",
  222. .protocol = IPSET_PROTOCOL,
  223. .features = IPSET_TYPE_IP,
  224. .dimension = IPSET_DIM_ONE,
  225. .family = NFPROTO_UNSPEC,
  226. .revision_min = IPSET_TYPE_REV_MIN,
  227. .revision_max = IPSET_TYPE_REV_MAX,
  228. .create = hash_ip_create,
  229. .create_policy = {
  230. [IPSET_ATTR_HASHSIZE] = { .type = NLA_U32 },
  231. [IPSET_ATTR_MAXELEM] = { .type = NLA_U32 },
  232. [IPSET_ATTR_PROBES] = { .type = NLA_U8 },
  233. [IPSET_ATTR_RESIZE] = { .type = NLA_U8 },
  234. [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
  235. [IPSET_ATTR_NETMASK] = { .type = NLA_U8 },
  236. [IPSET_ATTR_CADT_FLAGS] = { .type = NLA_U32 },
  237. },
  238. .adt_policy = {
  239. [IPSET_ATTR_IP] = { .type = NLA_NESTED },
  240. [IPSET_ATTR_IP_TO] = { .type = NLA_NESTED },
  241. [IPSET_ATTR_CIDR] = { .type = NLA_U8 },
  242. [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
  243. [IPSET_ATTR_LINENO] = { .type = NLA_U32 },
  244. [IPSET_ATTR_BYTES] = { .type = NLA_U64 },
  245. [IPSET_ATTR_PACKETS] = { .type = NLA_U64 },
  246. [IPSET_ATTR_COMMENT] = { .type = NLA_NUL_STRING,
  247. .len = IPSET_MAX_COMMENT_SIZE },
  248. [IPSET_ATTR_SKBMARK] = { .type = NLA_U64 },
  249. [IPSET_ATTR_SKBPRIO] = { .type = NLA_U32 },
  250. [IPSET_ATTR_SKBQUEUE] = { .type = NLA_U16 },
  251. },
  252. .me = THIS_MODULE,
  253. };
  254. static int __init
  255. hash_ip_init(void)
  256. {
  257. return ip_set_type_register(&hash_ip_type);
  258. }
  259. static void __exit
  260. hash_ip_fini(void)
  261. {
  262. rcu_barrier();
  263. ip_set_type_unregister(&hash_ip_type);
  264. }
  265. module_init(hash_ip_init);
  266. module_exit(hash_ip_fini);