ip_set_hash_net.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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:net 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 <linux/netfilter.h>
  14. #include <linux/netfilter/ipset/pfxlen.h>
  15. #include <linux/netfilter/ipset/ip_set.h>
  16. #include <linux/netfilter/ipset/ip_set_hash.h>
  17. #define IPSET_TYPE_REV_MIN 0
  18. /* 1 Range as input support for IPv4 added */
  19. /* 2 nomatch flag support added */
  20. /* 3 Counters support added */
  21. /* 4 Comments support added */
  22. /* 5 Forceadd support added */
  23. #define IPSET_TYPE_REV_MAX 6 /* skbinfo mapping support added */
  24. MODULE_LICENSE("GPL");
  25. MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@netfilter.org>");
  26. IP_SET_MODULE_DESC("hash:net", IPSET_TYPE_REV_MIN, IPSET_TYPE_REV_MAX);
  27. MODULE_ALIAS("ip_set_hash:net");
  28. /* Type specific function prefix */
  29. #define HTYPE hash_net
  30. #define IP_SET_HASH_WITH_NETS
  31. /* IPv4 variant */
  32. /* Member elements */
  33. struct hash_net4_elem {
  34. __be32 ip;
  35. u16 padding0;
  36. u8 nomatch;
  37. u8 cidr;
  38. };
  39. /* Common functions */
  40. static bool
  41. hash_net4_data_equal(const struct hash_net4_elem *ip1,
  42. const struct hash_net4_elem *ip2,
  43. u32 *multi)
  44. {
  45. return ip1->ip == ip2->ip &&
  46. ip1->cidr == ip2->cidr;
  47. }
  48. static int
  49. hash_net4_do_data_match(const struct hash_net4_elem *elem)
  50. {
  51. return elem->nomatch ? -ENOTEMPTY : 1;
  52. }
  53. static void
  54. hash_net4_data_set_flags(struct hash_net4_elem *elem, u32 flags)
  55. {
  56. elem->nomatch = (flags >> 16) & IPSET_FLAG_NOMATCH;
  57. }
  58. static void
  59. hash_net4_data_reset_flags(struct hash_net4_elem *elem, u8 *flags)
  60. {
  61. swap(*flags, elem->nomatch);
  62. }
  63. static void
  64. hash_net4_data_netmask(struct hash_net4_elem *elem, u8 cidr)
  65. {
  66. elem->ip &= ip_set_netmask(cidr);
  67. elem->cidr = cidr;
  68. }
  69. static bool
  70. hash_net4_data_list(struct sk_buff *skb, const struct hash_net4_elem *data)
  71. {
  72. u32 flags = data->nomatch ? IPSET_FLAG_NOMATCH : 0;
  73. if (nla_put_ipaddr4(skb, IPSET_ATTR_IP, data->ip) ||
  74. nla_put_u8(skb, IPSET_ATTR_CIDR, data->cidr) ||
  75. (flags &&
  76. nla_put_net32(skb, IPSET_ATTR_CADT_FLAGS, htonl(flags))))
  77. goto nla_put_failure;
  78. return false;
  79. nla_put_failure:
  80. return true;
  81. }
  82. static void
  83. hash_net4_data_next(struct hash_net4_elem *next,
  84. const struct hash_net4_elem *d)
  85. {
  86. next->ip = d->ip;
  87. }
  88. #define MTYPE hash_net4
  89. #define HOST_MASK 32
  90. #include "ip_set_hash_gen.h"
  91. static int
  92. hash_net4_kadt(struct ip_set *set, const struct sk_buff *skb,
  93. const struct xt_action_param *par,
  94. enum ipset_adt adt, struct ip_set_adt_opt *opt)
  95. {
  96. const struct hash_net4 *h = set->data;
  97. ipset_adtfn adtfn = set->variant->adt[adt];
  98. struct hash_net4_elem e = {
  99. .cidr = INIT_CIDR(h->nets[0].cidr[0], HOST_MASK),
  100. };
  101. struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set);
  102. if (e.cidr == 0)
  103. return -EINVAL;
  104. if (adt == IPSET_TEST)
  105. e.cidr = HOST_MASK;
  106. ip4addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &e.ip);
  107. e.ip &= ip_set_netmask(e.cidr);
  108. return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags);
  109. }
  110. static int
  111. hash_net4_uadt(struct ip_set *set, struct nlattr *tb[],
  112. enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
  113. {
  114. const struct hash_net4 *h = set->data;
  115. ipset_adtfn adtfn = set->variant->adt[adt];
  116. struct hash_net4_elem e = { .cidr = HOST_MASK };
  117. struct ip_set_ext ext = IP_SET_INIT_UEXT(set);
  118. u32 ip = 0, ip_to = 0;
  119. int ret;
  120. if (tb[IPSET_ATTR_LINENO])
  121. *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
  122. if (unlikely(!tb[IPSET_ATTR_IP] ||
  123. !ip_set_optattr_netorder(tb, IPSET_ATTR_CADT_FLAGS)))
  124. return -IPSET_ERR_PROTOCOL;
  125. ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP], &ip);
  126. if (ret)
  127. return ret;
  128. ret = ip_set_get_extensions(set, tb, &ext);
  129. if (ret)
  130. return ret;
  131. if (tb[IPSET_ATTR_CIDR]) {
  132. e.cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
  133. if (!e.cidr || e.cidr > HOST_MASK)
  134. return -IPSET_ERR_INVALID_CIDR;
  135. }
  136. if (tb[IPSET_ATTR_CADT_FLAGS]) {
  137. u32 cadt_flags = ip_set_get_h32(tb[IPSET_ATTR_CADT_FLAGS]);
  138. if (cadt_flags & IPSET_FLAG_NOMATCH)
  139. flags |= (IPSET_FLAG_NOMATCH << 16);
  140. }
  141. if (adt == IPSET_TEST || !tb[IPSET_ATTR_IP_TO]) {
  142. e.ip = htonl(ip & ip_set_hostmask(e.cidr));
  143. ret = adtfn(set, &e, &ext, &ext, flags);
  144. return ip_set_enomatch(ret, flags, adt, set) ? -ret :
  145. ip_set_eexist(ret, flags) ? 0 : ret;
  146. }
  147. ip_to = ip;
  148. if (tb[IPSET_ATTR_IP_TO]) {
  149. ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP_TO], &ip_to);
  150. if (ret)
  151. return ret;
  152. if (ip_to < ip)
  153. swap(ip, ip_to);
  154. if (ip + UINT_MAX == ip_to)
  155. return -IPSET_ERR_HASH_RANGE;
  156. }
  157. if (retried)
  158. ip = ntohl(h->next.ip);
  159. do {
  160. e.ip = htonl(ip);
  161. ip = ip_set_range_to_cidr(ip, ip_to, &e.cidr);
  162. ret = adtfn(set, &e, &ext, &ext, flags);
  163. if (ret && !ip_set_eexist(ret, flags))
  164. return ret;
  165. ret = 0;
  166. } while (ip++ < ip_to);
  167. return ret;
  168. }
  169. /* IPv6 variant */
  170. struct hash_net6_elem {
  171. union nf_inet_addr ip;
  172. u16 padding0;
  173. u8 nomatch;
  174. u8 cidr;
  175. };
  176. /* Common functions */
  177. static bool
  178. hash_net6_data_equal(const struct hash_net6_elem *ip1,
  179. const struct hash_net6_elem *ip2,
  180. u32 *multi)
  181. {
  182. return ipv6_addr_equal(&ip1->ip.in6, &ip2->ip.in6) &&
  183. ip1->cidr == ip2->cidr;
  184. }
  185. static int
  186. hash_net6_do_data_match(const struct hash_net6_elem *elem)
  187. {
  188. return elem->nomatch ? -ENOTEMPTY : 1;
  189. }
  190. static void
  191. hash_net6_data_set_flags(struct hash_net6_elem *elem, u32 flags)
  192. {
  193. elem->nomatch = (flags >> 16) & IPSET_FLAG_NOMATCH;
  194. }
  195. static void
  196. hash_net6_data_reset_flags(struct hash_net6_elem *elem, u8 *flags)
  197. {
  198. swap(*flags, elem->nomatch);
  199. }
  200. static void
  201. hash_net6_data_netmask(struct hash_net6_elem *elem, u8 cidr)
  202. {
  203. ip6_netmask(&elem->ip, cidr);
  204. elem->cidr = cidr;
  205. }
  206. static bool
  207. hash_net6_data_list(struct sk_buff *skb, const struct hash_net6_elem *data)
  208. {
  209. u32 flags = data->nomatch ? IPSET_FLAG_NOMATCH : 0;
  210. if (nla_put_ipaddr6(skb, IPSET_ATTR_IP, &data->ip.in6) ||
  211. nla_put_u8(skb, IPSET_ATTR_CIDR, data->cidr) ||
  212. (flags &&
  213. nla_put_net32(skb, IPSET_ATTR_CADT_FLAGS, htonl(flags))))
  214. goto nla_put_failure;
  215. return false;
  216. nla_put_failure:
  217. return true;
  218. }
  219. static void
  220. hash_net6_data_next(struct hash_net6_elem *next,
  221. const struct hash_net6_elem *d)
  222. {
  223. }
  224. #undef MTYPE
  225. #undef HOST_MASK
  226. #define MTYPE hash_net6
  227. #define HOST_MASK 128
  228. #define IP_SET_EMIT_CREATE
  229. #include "ip_set_hash_gen.h"
  230. static int
  231. hash_net6_kadt(struct ip_set *set, const struct sk_buff *skb,
  232. const struct xt_action_param *par,
  233. enum ipset_adt adt, struct ip_set_adt_opt *opt)
  234. {
  235. const struct hash_net6 *h = set->data;
  236. ipset_adtfn adtfn = set->variant->adt[adt];
  237. struct hash_net6_elem e = {
  238. .cidr = INIT_CIDR(h->nets[0].cidr[0], HOST_MASK),
  239. };
  240. struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set);
  241. if (e.cidr == 0)
  242. return -EINVAL;
  243. if (adt == IPSET_TEST)
  244. e.cidr = HOST_MASK;
  245. ip6addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &e.ip.in6);
  246. ip6_netmask(&e.ip, e.cidr);
  247. return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags);
  248. }
  249. static int
  250. hash_net6_uadt(struct ip_set *set, struct nlattr *tb[],
  251. enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
  252. {
  253. ipset_adtfn adtfn = set->variant->adt[adt];
  254. struct hash_net6_elem e = { .cidr = HOST_MASK };
  255. struct ip_set_ext ext = IP_SET_INIT_UEXT(set);
  256. int ret;
  257. if (tb[IPSET_ATTR_LINENO])
  258. *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
  259. if (unlikely(!tb[IPSET_ATTR_IP] ||
  260. !ip_set_optattr_netorder(tb, IPSET_ATTR_CADT_FLAGS)))
  261. return -IPSET_ERR_PROTOCOL;
  262. if (unlikely(tb[IPSET_ATTR_IP_TO]))
  263. return -IPSET_ERR_HASH_RANGE_UNSUPPORTED;
  264. ret = ip_set_get_ipaddr6(tb[IPSET_ATTR_IP], &e.ip);
  265. if (ret)
  266. return ret;
  267. ret = ip_set_get_extensions(set, tb, &ext);
  268. if (ret)
  269. return ret;
  270. if (tb[IPSET_ATTR_CIDR]) {
  271. e.cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
  272. if (!e.cidr || e.cidr > HOST_MASK)
  273. return -IPSET_ERR_INVALID_CIDR;
  274. }
  275. ip6_netmask(&e.ip, e.cidr);
  276. if (tb[IPSET_ATTR_CADT_FLAGS]) {
  277. u32 cadt_flags = ip_set_get_h32(tb[IPSET_ATTR_CADT_FLAGS]);
  278. if (cadt_flags & IPSET_FLAG_NOMATCH)
  279. flags |= (IPSET_FLAG_NOMATCH << 16);
  280. }
  281. ret = adtfn(set, &e, &ext, &ext, flags);
  282. return ip_set_enomatch(ret, flags, adt, set) ? -ret :
  283. ip_set_eexist(ret, flags) ? 0 : ret;
  284. }
  285. static struct ip_set_type hash_net_type __read_mostly = {
  286. .name = "hash:net",
  287. .protocol = IPSET_PROTOCOL,
  288. .features = IPSET_TYPE_IP | IPSET_TYPE_NOMATCH,
  289. .dimension = IPSET_DIM_ONE,
  290. .family = NFPROTO_UNSPEC,
  291. .revision_min = IPSET_TYPE_REV_MIN,
  292. .revision_max = IPSET_TYPE_REV_MAX,
  293. .create = hash_net_create,
  294. .create_policy = {
  295. [IPSET_ATTR_HASHSIZE] = { .type = NLA_U32 },
  296. [IPSET_ATTR_MAXELEM] = { .type = NLA_U32 },
  297. [IPSET_ATTR_PROBES] = { .type = NLA_U8 },
  298. [IPSET_ATTR_RESIZE] = { .type = NLA_U8 },
  299. [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
  300. [IPSET_ATTR_CADT_FLAGS] = { .type = NLA_U32 },
  301. },
  302. .adt_policy = {
  303. [IPSET_ATTR_IP] = { .type = NLA_NESTED },
  304. [IPSET_ATTR_IP_TO] = { .type = NLA_NESTED },
  305. [IPSET_ATTR_CIDR] = { .type = NLA_U8 },
  306. [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
  307. [IPSET_ATTR_LINENO] = { .type = NLA_U32 },
  308. [IPSET_ATTR_CADT_FLAGS] = { .type = NLA_U32 },
  309. [IPSET_ATTR_BYTES] = { .type = NLA_U64 },
  310. [IPSET_ATTR_PACKETS] = { .type = NLA_U64 },
  311. [IPSET_ATTR_COMMENT] = { .type = NLA_NUL_STRING,
  312. .len = IPSET_MAX_COMMENT_SIZE },
  313. [IPSET_ATTR_SKBMARK] = { .type = NLA_U64 },
  314. [IPSET_ATTR_SKBPRIO] = { .type = NLA_U32 },
  315. [IPSET_ATTR_SKBQUEUE] = { .type = NLA_U16 },
  316. },
  317. .me = THIS_MODULE,
  318. };
  319. static int __init
  320. hash_net_init(void)
  321. {
  322. return ip_set_type_register(&hash_net_type);
  323. }
  324. static void __exit
  325. hash_net_fini(void)
  326. {
  327. rcu_barrier();
  328. ip_set_type_unregister(&hash_net_type);
  329. }
  330. module_init(hash_net_init);
  331. module_exit(hash_net_fini);