ip_set_hash_netport.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  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,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 <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_getport.h>
  17. #include <linux/netfilter/ipset/ip_set_hash.h>
  18. #define IPSET_TYPE_REV_MIN 0
  19. /* 1 SCTP and UDPLITE support added */
  20. /* 2 Range as input support for IPv4 added */
  21. /* 3 nomatch flag support added */
  22. /* 4 Counters support added */
  23. /* 5 Comments support added */
  24. /* 6 Forceadd support added */
  25. #define IPSET_TYPE_REV_MAX 7 /* skbinfo support added */
  26. MODULE_LICENSE("GPL");
  27. MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@netfilter.org>");
  28. IP_SET_MODULE_DESC("hash:net,port", IPSET_TYPE_REV_MIN, IPSET_TYPE_REV_MAX);
  29. MODULE_ALIAS("ip_set_hash:net,port");
  30. /* Type specific function prefix */
  31. #define HTYPE hash_netport
  32. #define IP_SET_HASH_WITH_PROTO
  33. #define IP_SET_HASH_WITH_NETS
  34. /* We squeeze the "nomatch" flag into cidr: we don't support cidr == 0
  35. * However this way we have to store internally cidr - 1,
  36. * dancing back and forth.
  37. */
  38. #define IP_SET_HASH_WITH_NETS_PACKED
  39. /* IPv4 variant */
  40. /* Member elements */
  41. struct hash_netport4_elem {
  42. __be32 ip;
  43. __be16 port;
  44. u8 proto;
  45. u8 cidr:7;
  46. u8 nomatch:1;
  47. };
  48. /* Common functions */
  49. static bool
  50. hash_netport4_data_equal(const struct hash_netport4_elem *ip1,
  51. const struct hash_netport4_elem *ip2,
  52. u32 *multi)
  53. {
  54. return ip1->ip == ip2->ip &&
  55. ip1->port == ip2->port &&
  56. ip1->proto == ip2->proto &&
  57. ip1->cidr == ip2->cidr;
  58. }
  59. static int
  60. hash_netport4_do_data_match(const struct hash_netport4_elem *elem)
  61. {
  62. return elem->nomatch ? -ENOTEMPTY : 1;
  63. }
  64. static void
  65. hash_netport4_data_set_flags(struct hash_netport4_elem *elem, u32 flags)
  66. {
  67. elem->nomatch = !!((flags >> 16) & IPSET_FLAG_NOMATCH);
  68. }
  69. static void
  70. hash_netport4_data_reset_flags(struct hash_netport4_elem *elem, u8 *flags)
  71. {
  72. swap(*flags, elem->nomatch);
  73. }
  74. static void
  75. hash_netport4_data_netmask(struct hash_netport4_elem *elem, u8 cidr)
  76. {
  77. elem->ip &= ip_set_netmask(cidr);
  78. elem->cidr = cidr - 1;
  79. }
  80. static bool
  81. hash_netport4_data_list(struct sk_buff *skb,
  82. const struct hash_netport4_elem *data)
  83. {
  84. u32 flags = data->nomatch ? IPSET_FLAG_NOMATCH : 0;
  85. if (nla_put_ipaddr4(skb, IPSET_ATTR_IP, data->ip) ||
  86. nla_put_net16(skb, IPSET_ATTR_PORT, data->port) ||
  87. nla_put_u8(skb, IPSET_ATTR_CIDR, data->cidr + 1) ||
  88. nla_put_u8(skb, IPSET_ATTR_PROTO, data->proto) ||
  89. (flags &&
  90. nla_put_net32(skb, IPSET_ATTR_CADT_FLAGS, htonl(flags))))
  91. goto nla_put_failure;
  92. return false;
  93. nla_put_failure:
  94. return true;
  95. }
  96. static void
  97. hash_netport4_data_next(struct hash_netport4_elem *next,
  98. const struct hash_netport4_elem *d)
  99. {
  100. next->ip = d->ip;
  101. next->port = d->port;
  102. }
  103. #define MTYPE hash_netport4
  104. #define HOST_MASK 32
  105. #include "ip_set_hash_gen.h"
  106. static int
  107. hash_netport4_kadt(struct ip_set *set, const struct sk_buff *skb,
  108. const struct xt_action_param *par,
  109. enum ipset_adt adt, struct ip_set_adt_opt *opt)
  110. {
  111. const struct hash_netport4 *h = set->data;
  112. ipset_adtfn adtfn = set->variant->adt[adt];
  113. struct hash_netport4_elem e = {
  114. .cidr = INIT_CIDR(h->nets[0].cidr[0], HOST_MASK),
  115. };
  116. struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set);
  117. if (adt == IPSET_TEST)
  118. e.cidr = HOST_MASK - 1;
  119. if (!ip_set_get_ip4_port(skb, opt->flags & IPSET_DIM_TWO_SRC,
  120. &e.port, &e.proto))
  121. return -EINVAL;
  122. ip4addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &e.ip);
  123. e.ip &= ip_set_netmask(e.cidr + 1);
  124. return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags);
  125. }
  126. static int
  127. hash_netport4_uadt(struct ip_set *set, struct nlattr *tb[],
  128. enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
  129. {
  130. const struct hash_netport4 *h = set->data;
  131. ipset_adtfn adtfn = set->variant->adt[adt];
  132. struct hash_netport4_elem e = { .cidr = HOST_MASK - 1 };
  133. struct ip_set_ext ext = IP_SET_INIT_UEXT(set);
  134. u32 port, port_to, p = 0, ip = 0, ip_to = 0;
  135. bool with_ports = false;
  136. u8 cidr;
  137. int ret;
  138. if (tb[IPSET_ATTR_LINENO])
  139. *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
  140. if (unlikely(!tb[IPSET_ATTR_IP] ||
  141. !ip_set_attr_netorder(tb, IPSET_ATTR_PORT) ||
  142. !ip_set_optattr_netorder(tb, IPSET_ATTR_PORT_TO) ||
  143. !ip_set_optattr_netorder(tb, IPSET_ATTR_CADT_FLAGS)))
  144. return -IPSET_ERR_PROTOCOL;
  145. ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP], &ip);
  146. if (ret)
  147. return ret;
  148. ret = ip_set_get_extensions(set, tb, &ext);
  149. if (ret)
  150. return ret;
  151. if (tb[IPSET_ATTR_CIDR]) {
  152. cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
  153. if (!cidr || cidr > HOST_MASK)
  154. return -IPSET_ERR_INVALID_CIDR;
  155. e.cidr = cidr - 1;
  156. }
  157. e.port = nla_get_be16(tb[IPSET_ATTR_PORT]);
  158. if (tb[IPSET_ATTR_PROTO]) {
  159. e.proto = nla_get_u8(tb[IPSET_ATTR_PROTO]);
  160. with_ports = ip_set_proto_with_ports(e.proto);
  161. if (e.proto == 0)
  162. return -IPSET_ERR_INVALID_PROTO;
  163. } else {
  164. return -IPSET_ERR_MISSING_PROTO;
  165. }
  166. if (!(with_ports || e.proto == IPPROTO_ICMP))
  167. e.port = 0;
  168. with_ports = with_ports && tb[IPSET_ATTR_PORT_TO];
  169. if (tb[IPSET_ATTR_CADT_FLAGS]) {
  170. u32 cadt_flags = ip_set_get_h32(tb[IPSET_ATTR_CADT_FLAGS]);
  171. if (cadt_flags & IPSET_FLAG_NOMATCH)
  172. flags |= (IPSET_FLAG_NOMATCH << 16);
  173. }
  174. if (adt == IPSET_TEST || !(with_ports || tb[IPSET_ATTR_IP_TO])) {
  175. e.ip = htonl(ip & ip_set_hostmask(e.cidr + 1));
  176. ret = adtfn(set, &e, &ext, &ext, flags);
  177. return ip_set_enomatch(ret, flags, adt, set) ? -ret :
  178. ip_set_eexist(ret, flags) ? 0 : ret;
  179. }
  180. port = port_to = ntohs(e.port);
  181. if (tb[IPSET_ATTR_PORT_TO]) {
  182. port_to = ip_set_get_h16(tb[IPSET_ATTR_PORT_TO]);
  183. if (port_to < port)
  184. swap(port, port_to);
  185. }
  186. if (tb[IPSET_ATTR_IP_TO]) {
  187. ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP_TO], &ip_to);
  188. if (ret)
  189. return ret;
  190. if (ip_to < ip)
  191. swap(ip, ip_to);
  192. if (ip + UINT_MAX == ip_to)
  193. return -IPSET_ERR_HASH_RANGE;
  194. } else {
  195. ip_set_mask_from_to(ip, ip_to, e.cidr + 1);
  196. }
  197. if (retried) {
  198. ip = ntohl(h->next.ip);
  199. p = ntohs(h->next.port);
  200. } else {
  201. p = port;
  202. }
  203. do {
  204. e.ip = htonl(ip);
  205. ip = ip_set_range_to_cidr(ip, ip_to, &cidr);
  206. e.cidr = cidr - 1;
  207. for (; p <= port_to; p++) {
  208. e.port = htons(p);
  209. ret = adtfn(set, &e, &ext, &ext, flags);
  210. if (ret && !ip_set_eexist(ret, flags))
  211. return ret;
  212. ret = 0;
  213. }
  214. p = port;
  215. } while (ip++ < ip_to);
  216. return ret;
  217. }
  218. /* IPv6 variant */
  219. struct hash_netport6_elem {
  220. union nf_inet_addr ip;
  221. __be16 port;
  222. u8 proto;
  223. u8 cidr:7;
  224. u8 nomatch:1;
  225. };
  226. /* Common functions */
  227. static bool
  228. hash_netport6_data_equal(const struct hash_netport6_elem *ip1,
  229. const struct hash_netport6_elem *ip2,
  230. u32 *multi)
  231. {
  232. return ipv6_addr_equal(&ip1->ip.in6, &ip2->ip.in6) &&
  233. ip1->port == ip2->port &&
  234. ip1->proto == ip2->proto &&
  235. ip1->cidr == ip2->cidr;
  236. }
  237. static int
  238. hash_netport6_do_data_match(const struct hash_netport6_elem *elem)
  239. {
  240. return elem->nomatch ? -ENOTEMPTY : 1;
  241. }
  242. static void
  243. hash_netport6_data_set_flags(struct hash_netport6_elem *elem, u32 flags)
  244. {
  245. elem->nomatch = !!((flags >> 16) & IPSET_FLAG_NOMATCH);
  246. }
  247. static void
  248. hash_netport6_data_reset_flags(struct hash_netport6_elem *elem, u8 *flags)
  249. {
  250. swap(*flags, elem->nomatch);
  251. }
  252. static void
  253. hash_netport6_data_netmask(struct hash_netport6_elem *elem, u8 cidr)
  254. {
  255. ip6_netmask(&elem->ip, cidr);
  256. elem->cidr = cidr - 1;
  257. }
  258. static bool
  259. hash_netport6_data_list(struct sk_buff *skb,
  260. const struct hash_netport6_elem *data)
  261. {
  262. u32 flags = data->nomatch ? IPSET_FLAG_NOMATCH : 0;
  263. if (nla_put_ipaddr6(skb, IPSET_ATTR_IP, &data->ip.in6) ||
  264. nla_put_net16(skb, IPSET_ATTR_PORT, data->port) ||
  265. nla_put_u8(skb, IPSET_ATTR_CIDR, data->cidr + 1) ||
  266. nla_put_u8(skb, IPSET_ATTR_PROTO, data->proto) ||
  267. (flags &&
  268. nla_put_net32(skb, IPSET_ATTR_CADT_FLAGS, htonl(flags))))
  269. goto nla_put_failure;
  270. return false;
  271. nla_put_failure:
  272. return true;
  273. }
  274. static void
  275. hash_netport6_data_next(struct hash_netport6_elem *next,
  276. const struct hash_netport6_elem *d)
  277. {
  278. next->port = d->port;
  279. }
  280. #undef MTYPE
  281. #undef HOST_MASK
  282. #define MTYPE hash_netport6
  283. #define HOST_MASK 128
  284. #define IP_SET_EMIT_CREATE
  285. #include "ip_set_hash_gen.h"
  286. static int
  287. hash_netport6_kadt(struct ip_set *set, const struct sk_buff *skb,
  288. const struct xt_action_param *par,
  289. enum ipset_adt adt, struct ip_set_adt_opt *opt)
  290. {
  291. const struct hash_netport6 *h = set->data;
  292. ipset_adtfn adtfn = set->variant->adt[adt];
  293. struct hash_netport6_elem e = {
  294. .cidr = INIT_CIDR(h->nets[0].cidr[0], HOST_MASK),
  295. };
  296. struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set);
  297. if (adt == IPSET_TEST)
  298. e.cidr = HOST_MASK - 1;
  299. if (!ip_set_get_ip6_port(skb, opt->flags & IPSET_DIM_TWO_SRC,
  300. &e.port, &e.proto))
  301. return -EINVAL;
  302. ip6addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &e.ip.in6);
  303. ip6_netmask(&e.ip, e.cidr + 1);
  304. return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags);
  305. }
  306. static int
  307. hash_netport6_uadt(struct ip_set *set, struct nlattr *tb[],
  308. enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
  309. {
  310. const struct hash_netport6 *h = set->data;
  311. ipset_adtfn adtfn = set->variant->adt[adt];
  312. struct hash_netport6_elem e = { .cidr = HOST_MASK - 1 };
  313. struct ip_set_ext ext = IP_SET_INIT_UEXT(set);
  314. u32 port, port_to;
  315. bool with_ports = false;
  316. u8 cidr;
  317. int ret;
  318. if (tb[IPSET_ATTR_LINENO])
  319. *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
  320. if (unlikely(!tb[IPSET_ATTR_IP] ||
  321. !ip_set_attr_netorder(tb, IPSET_ATTR_PORT) ||
  322. !ip_set_optattr_netorder(tb, IPSET_ATTR_PORT_TO) ||
  323. !ip_set_optattr_netorder(tb, IPSET_ATTR_CADT_FLAGS)))
  324. return -IPSET_ERR_PROTOCOL;
  325. if (unlikely(tb[IPSET_ATTR_IP_TO]))
  326. return -IPSET_ERR_HASH_RANGE_UNSUPPORTED;
  327. ret = ip_set_get_ipaddr6(tb[IPSET_ATTR_IP], &e.ip);
  328. if (ret)
  329. return ret;
  330. ret = ip_set_get_extensions(set, tb, &ext);
  331. if (ret)
  332. return ret;
  333. if (tb[IPSET_ATTR_CIDR]) {
  334. cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
  335. if (!cidr || cidr > HOST_MASK)
  336. return -IPSET_ERR_INVALID_CIDR;
  337. e.cidr = cidr - 1;
  338. }
  339. ip6_netmask(&e.ip, e.cidr + 1);
  340. e.port = nla_get_be16(tb[IPSET_ATTR_PORT]);
  341. if (tb[IPSET_ATTR_PROTO]) {
  342. e.proto = nla_get_u8(tb[IPSET_ATTR_PROTO]);
  343. with_ports = ip_set_proto_with_ports(e.proto);
  344. if (e.proto == 0)
  345. return -IPSET_ERR_INVALID_PROTO;
  346. } else {
  347. return -IPSET_ERR_MISSING_PROTO;
  348. }
  349. if (!(with_ports || e.proto == IPPROTO_ICMPV6))
  350. e.port = 0;
  351. if (tb[IPSET_ATTR_CADT_FLAGS]) {
  352. u32 cadt_flags = ip_set_get_h32(tb[IPSET_ATTR_CADT_FLAGS]);
  353. if (cadt_flags & IPSET_FLAG_NOMATCH)
  354. flags |= (IPSET_FLAG_NOMATCH << 16);
  355. }
  356. if (adt == IPSET_TEST || !with_ports || !tb[IPSET_ATTR_PORT_TO]) {
  357. ret = adtfn(set, &e, &ext, &ext, flags);
  358. return ip_set_enomatch(ret, flags, adt, set) ? -ret :
  359. ip_set_eexist(ret, flags) ? 0 : ret;
  360. }
  361. port = ntohs(e.port);
  362. port_to = ip_set_get_h16(tb[IPSET_ATTR_PORT_TO]);
  363. if (port > port_to)
  364. swap(port, port_to);
  365. if (retried)
  366. port = ntohs(h->next.port);
  367. for (; port <= port_to; port++) {
  368. e.port = htons(port);
  369. ret = adtfn(set, &e, &ext, &ext, flags);
  370. if (ret && !ip_set_eexist(ret, flags))
  371. return ret;
  372. ret = 0;
  373. }
  374. return ret;
  375. }
  376. static struct ip_set_type hash_netport_type __read_mostly = {
  377. .name = "hash:net,port",
  378. .protocol = IPSET_PROTOCOL,
  379. .features = IPSET_TYPE_IP | IPSET_TYPE_PORT | IPSET_TYPE_NOMATCH,
  380. .dimension = IPSET_DIM_TWO,
  381. .family = NFPROTO_UNSPEC,
  382. .revision_min = IPSET_TYPE_REV_MIN,
  383. .revision_max = IPSET_TYPE_REV_MAX,
  384. .create = hash_netport_create,
  385. .create_policy = {
  386. [IPSET_ATTR_HASHSIZE] = { .type = NLA_U32 },
  387. [IPSET_ATTR_MAXELEM] = { .type = NLA_U32 },
  388. [IPSET_ATTR_PROBES] = { .type = NLA_U8 },
  389. [IPSET_ATTR_RESIZE] = { .type = NLA_U8 },
  390. [IPSET_ATTR_PROTO] = { .type = NLA_U8 },
  391. [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
  392. [IPSET_ATTR_CADT_FLAGS] = { .type = NLA_U32 },
  393. },
  394. .adt_policy = {
  395. [IPSET_ATTR_IP] = { .type = NLA_NESTED },
  396. [IPSET_ATTR_IP_TO] = { .type = NLA_NESTED },
  397. [IPSET_ATTR_PORT] = { .type = NLA_U16 },
  398. [IPSET_ATTR_PORT_TO] = { .type = NLA_U16 },
  399. [IPSET_ATTR_PROTO] = { .type = NLA_U8 },
  400. [IPSET_ATTR_CIDR] = { .type = NLA_U8 },
  401. [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
  402. [IPSET_ATTR_LINENO] = { .type = NLA_U32 },
  403. [IPSET_ATTR_CADT_FLAGS] = { .type = NLA_U32 },
  404. [IPSET_ATTR_BYTES] = { .type = NLA_U64 },
  405. [IPSET_ATTR_PACKETS] = { .type = NLA_U64 },
  406. [IPSET_ATTR_COMMENT] = { .type = NLA_NUL_STRING,
  407. .len = IPSET_MAX_COMMENT_SIZE },
  408. [IPSET_ATTR_SKBMARK] = { .type = NLA_U64 },
  409. [IPSET_ATTR_SKBPRIO] = { .type = NLA_U32 },
  410. [IPSET_ATTR_SKBQUEUE] = { .type = NLA_U16 },
  411. },
  412. .me = THIS_MODULE,
  413. };
  414. static int __init
  415. hash_netport_init(void)
  416. {
  417. return ip_set_type_register(&hash_netport_type);
  418. }
  419. static void __exit
  420. hash_netport_fini(void)
  421. {
  422. rcu_barrier();
  423. ip_set_type_unregister(&hash_netport_type);
  424. }
  425. module_init(hash_netport_init);
  426. module_exit(hash_netport_fini);