ip_set_hash_ipportip.c 11 KB

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