ip_set_hash_ipmark.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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,mark 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 Forceadd support */
  20. #define IPSET_TYPE_REV_MAX 2 /* skbinfo support */
  21. MODULE_LICENSE("GPL");
  22. MODULE_AUTHOR("Vytas Dauksa <vytas.dauksa@smoothwall.net>");
  23. IP_SET_MODULE_DESC("hash:ip,mark", IPSET_TYPE_REV_MIN, IPSET_TYPE_REV_MAX);
  24. MODULE_ALIAS("ip_set_hash:ip,mark");
  25. /* Type specific function prefix */
  26. #define HTYPE hash_ipmark
  27. #define IP_SET_HASH_WITH_MARKMASK
  28. /* IPv4 variant */
  29. /* Member elements */
  30. struct hash_ipmark4_elem {
  31. __be32 ip;
  32. __u32 mark;
  33. };
  34. /* Common functions */
  35. static bool
  36. hash_ipmark4_data_equal(const struct hash_ipmark4_elem *ip1,
  37. const struct hash_ipmark4_elem *ip2,
  38. u32 *multi)
  39. {
  40. return ip1->ip == ip2->ip &&
  41. ip1->mark == ip2->mark;
  42. }
  43. static bool
  44. hash_ipmark4_data_list(struct sk_buff *skb,
  45. const struct hash_ipmark4_elem *data)
  46. {
  47. if (nla_put_ipaddr4(skb, IPSET_ATTR_IP, data->ip) ||
  48. nla_put_net32(skb, IPSET_ATTR_MARK, htonl(data->mark)))
  49. goto nla_put_failure;
  50. return false;
  51. nla_put_failure:
  52. return true;
  53. }
  54. static void
  55. hash_ipmark4_data_next(struct hash_ipmark4_elem *next,
  56. const struct hash_ipmark4_elem *d)
  57. {
  58. next->ip = d->ip;
  59. }
  60. #define MTYPE hash_ipmark4
  61. #define HOST_MASK 32
  62. #include "ip_set_hash_gen.h"
  63. static int
  64. hash_ipmark4_kadt(struct ip_set *set, const struct sk_buff *skb,
  65. const struct xt_action_param *par,
  66. enum ipset_adt adt, struct ip_set_adt_opt *opt)
  67. {
  68. const struct hash_ipmark4 *h = set->data;
  69. ipset_adtfn adtfn = set->variant->adt[adt];
  70. struct hash_ipmark4_elem e = { };
  71. struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set);
  72. e.mark = skb->mark;
  73. e.mark &= h->markmask;
  74. ip4addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &e.ip);
  75. return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags);
  76. }
  77. static int
  78. hash_ipmark4_uadt(struct ip_set *set, struct nlattr *tb[],
  79. enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
  80. {
  81. const struct hash_ipmark4 *h = set->data;
  82. ipset_adtfn adtfn = set->variant->adt[adt];
  83. struct hash_ipmark4_elem e = { };
  84. struct ip_set_ext ext = IP_SET_INIT_UEXT(set);
  85. u32 ip, ip_to = 0;
  86. int ret;
  87. if (tb[IPSET_ATTR_LINENO])
  88. *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
  89. if (unlikely(!tb[IPSET_ATTR_IP] ||
  90. !ip_set_attr_netorder(tb, IPSET_ATTR_MARK)))
  91. return -IPSET_ERR_PROTOCOL;
  92. ret = ip_set_get_ipaddr4(tb[IPSET_ATTR_IP], &e.ip);
  93. if (ret)
  94. return ret;
  95. ret = ip_set_get_extensions(set, tb, &ext);
  96. if (ret)
  97. return ret;
  98. e.mark = ntohl(nla_get_be32(tb[IPSET_ATTR_MARK]));
  99. e.mark &= h->markmask;
  100. if (adt == IPSET_TEST ||
  101. !(tb[IPSET_ATTR_IP_TO] || tb[IPSET_ATTR_CIDR])) {
  102. ret = adtfn(set, &e, &ext, &ext, flags);
  103. return ip_set_eexist(ret, flags) ? 0 : ret;
  104. }
  105. ip_to = ip = ntohl(e.ip);
  106. if (tb[IPSET_ATTR_IP_TO]) {
  107. ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP_TO], &ip_to);
  108. if (ret)
  109. return ret;
  110. if (ip > ip_to)
  111. swap(ip, ip_to);
  112. } else if (tb[IPSET_ATTR_CIDR]) {
  113. u8 cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
  114. if (!cidr || cidr > HOST_MASK)
  115. return -IPSET_ERR_INVALID_CIDR;
  116. ip_set_mask_from_to(ip, ip_to, cidr);
  117. }
  118. if (retried)
  119. ip = ntohl(h->next.ip);
  120. for (; ip <= ip_to; ip++) {
  121. e.ip = htonl(ip);
  122. ret = adtfn(set, &e, &ext, &ext, flags);
  123. if (ret && !ip_set_eexist(ret, flags))
  124. return ret;
  125. ret = 0;
  126. }
  127. return ret;
  128. }
  129. /* IPv6 variant */
  130. struct hash_ipmark6_elem {
  131. union nf_inet_addr ip;
  132. __u32 mark;
  133. };
  134. /* Common functions */
  135. static bool
  136. hash_ipmark6_data_equal(const struct hash_ipmark6_elem *ip1,
  137. const struct hash_ipmark6_elem *ip2,
  138. u32 *multi)
  139. {
  140. return ipv6_addr_equal(&ip1->ip.in6, &ip2->ip.in6) &&
  141. ip1->mark == ip2->mark;
  142. }
  143. static bool
  144. hash_ipmark6_data_list(struct sk_buff *skb,
  145. const struct hash_ipmark6_elem *data)
  146. {
  147. if (nla_put_ipaddr6(skb, IPSET_ATTR_IP, &data->ip.in6) ||
  148. nla_put_net32(skb, IPSET_ATTR_MARK, htonl(data->mark)))
  149. goto nla_put_failure;
  150. return false;
  151. nla_put_failure:
  152. return true;
  153. }
  154. static void
  155. hash_ipmark6_data_next(struct hash_ipmark6_elem *next,
  156. const struct hash_ipmark6_elem *d)
  157. {
  158. }
  159. #undef MTYPE
  160. #undef HOST_MASK
  161. #define MTYPE hash_ipmark6
  162. #define HOST_MASK 128
  163. #define IP_SET_EMIT_CREATE
  164. #include "ip_set_hash_gen.h"
  165. static int
  166. hash_ipmark6_kadt(struct ip_set *set, const struct sk_buff *skb,
  167. const struct xt_action_param *par,
  168. enum ipset_adt adt, struct ip_set_adt_opt *opt)
  169. {
  170. const struct hash_ipmark6 *h = set->data;
  171. ipset_adtfn adtfn = set->variant->adt[adt];
  172. struct hash_ipmark6_elem e = { };
  173. struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set);
  174. e.mark = skb->mark;
  175. e.mark &= h->markmask;
  176. ip6addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &e.ip.in6);
  177. return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags);
  178. }
  179. static int
  180. hash_ipmark6_uadt(struct ip_set *set, struct nlattr *tb[],
  181. enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
  182. {
  183. const struct hash_ipmark6 *h = set->data;
  184. ipset_adtfn adtfn = set->variant->adt[adt];
  185. struct hash_ipmark6_elem e = { };
  186. struct ip_set_ext ext = IP_SET_INIT_UEXT(set);
  187. int ret;
  188. if (tb[IPSET_ATTR_LINENO])
  189. *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
  190. if (unlikely(!tb[IPSET_ATTR_IP] ||
  191. !ip_set_attr_netorder(tb, IPSET_ATTR_MARK)))
  192. return -IPSET_ERR_PROTOCOL;
  193. if (unlikely(tb[IPSET_ATTR_IP_TO]))
  194. return -IPSET_ERR_HASH_RANGE_UNSUPPORTED;
  195. if (unlikely(tb[IPSET_ATTR_CIDR])) {
  196. u8 cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
  197. if (cidr != HOST_MASK)
  198. return -IPSET_ERR_INVALID_CIDR;
  199. }
  200. ret = ip_set_get_ipaddr6(tb[IPSET_ATTR_IP], &e.ip);
  201. if (ret)
  202. return ret;
  203. ret = ip_set_get_extensions(set, tb, &ext);
  204. if (ret)
  205. return ret;
  206. e.mark = ntohl(nla_get_be32(tb[IPSET_ATTR_MARK]));
  207. e.mark &= h->markmask;
  208. if (adt == IPSET_TEST) {
  209. ret = adtfn(set, &e, &ext, &ext, flags);
  210. return ip_set_eexist(ret, flags) ? 0 : ret;
  211. }
  212. ret = adtfn(set, &e, &ext, &ext, flags);
  213. if (ret && !ip_set_eexist(ret, flags))
  214. return ret;
  215. return 0;
  216. }
  217. static struct ip_set_type hash_ipmark_type __read_mostly = {
  218. .name = "hash:ip,mark",
  219. .protocol = IPSET_PROTOCOL,
  220. .features = IPSET_TYPE_IP | IPSET_TYPE_MARK,
  221. .dimension = IPSET_DIM_TWO,
  222. .family = NFPROTO_UNSPEC,
  223. .revision_min = IPSET_TYPE_REV_MIN,
  224. .revision_max = IPSET_TYPE_REV_MAX,
  225. .create = hash_ipmark_create,
  226. .create_policy = {
  227. [IPSET_ATTR_MARKMASK] = { .type = NLA_U32 },
  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_CADT_FLAGS] = { .type = NLA_U32 },
  234. },
  235. .adt_policy = {
  236. [IPSET_ATTR_IP] = { .type = NLA_NESTED },
  237. [IPSET_ATTR_IP_TO] = { .type = NLA_NESTED },
  238. [IPSET_ATTR_MARK] = { .type = NLA_U32 },
  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_ipmark_init(void)
  254. {
  255. return ip_set_type_register(&hash_ipmark_type);
  256. }
  257. static void __exit
  258. hash_ipmark_fini(void)
  259. {
  260. rcu_barrier();
  261. ip_set_type_unregister(&hash_ipmark_type);
  262. }
  263. module_init(hash_ipmark_init);
  264. module_exit(hash_ipmark_fini);