ip_set_hash_ipport.c 9.8 KB

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