ip_set_hash_ipmark.c 8.1 KB

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