ip_set_hash_ip.c 7.5 KB

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