ip_set_hash_netportnet.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611
  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,net 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. /* 0 Comments support added */
  25. /* 1 Forceadd support added */
  26. #define IPSET_TYPE_REV_MAX 2 /* skbinfo support added */
  27. MODULE_LICENSE("GPL");
  28. MODULE_AUTHOR("Oliver Smith <oliver@8.c.9.b.0.7.4.0.1.0.0.2.ip6.arpa>");
  29. IP_SET_MODULE_DESC("hash:net,port,net", IPSET_TYPE_REV_MIN, IPSET_TYPE_REV_MAX);
  30. MODULE_ALIAS("ip_set_hash:net,port,net");
  31. /* Type specific function prefix */
  32. #define HTYPE hash_netportnet
  33. #define IP_SET_HASH_WITH_PROTO
  34. #define IP_SET_HASH_WITH_NETS
  35. #define IPSET_NET_COUNT 2
  36. /* IPv4 variant */
  37. /* Member elements */
  38. struct hash_netportnet4_elem {
  39. union {
  40. __be32 ip[2];
  41. __be64 ipcmp;
  42. };
  43. __be16 port;
  44. union {
  45. u8 cidr[2];
  46. u16 ccmp;
  47. };
  48. u16 padding;
  49. u8 nomatch;
  50. u8 proto;
  51. };
  52. /* Common functions */
  53. static inline bool
  54. hash_netportnet4_data_equal(const struct hash_netportnet4_elem *ip1,
  55. const struct hash_netportnet4_elem *ip2,
  56. u32 *multi)
  57. {
  58. return ip1->ipcmp == ip2->ipcmp &&
  59. ip1->ccmp == ip2->ccmp &&
  60. ip1->port == ip2->port &&
  61. ip1->proto == ip2->proto;
  62. }
  63. static inline int
  64. hash_netportnet4_do_data_match(const struct hash_netportnet4_elem *elem)
  65. {
  66. return elem->nomatch ? -ENOTEMPTY : 1;
  67. }
  68. static inline void
  69. hash_netportnet4_data_set_flags(struct hash_netportnet4_elem *elem, u32 flags)
  70. {
  71. elem->nomatch = !!((flags >> 16) & IPSET_FLAG_NOMATCH);
  72. }
  73. static inline void
  74. hash_netportnet4_data_reset_flags(struct hash_netportnet4_elem *elem, u8 *flags)
  75. {
  76. swap(*flags, elem->nomatch);
  77. }
  78. static inline void
  79. hash_netportnet4_data_reset_elem(struct hash_netportnet4_elem *elem,
  80. struct hash_netportnet4_elem *orig)
  81. {
  82. elem->ip[1] = orig->ip[1];
  83. }
  84. static inline void
  85. hash_netportnet4_data_netmask(struct hash_netportnet4_elem *elem,
  86. u8 cidr, bool inner)
  87. {
  88. if (inner) {
  89. elem->ip[1] &= ip_set_netmask(cidr);
  90. elem->cidr[1] = cidr;
  91. } else {
  92. elem->ip[0] &= ip_set_netmask(cidr);
  93. elem->cidr[0] = cidr;
  94. }
  95. }
  96. static bool
  97. hash_netportnet4_data_list(struct sk_buff *skb,
  98. const struct hash_netportnet4_elem *data)
  99. {
  100. u32 flags = data->nomatch ? IPSET_FLAG_NOMATCH : 0;
  101. if (nla_put_ipaddr4(skb, IPSET_ATTR_IP, data->ip[0]) ||
  102. nla_put_ipaddr4(skb, IPSET_ATTR_IP2, data->ip[1]) ||
  103. nla_put_net16(skb, IPSET_ATTR_PORT, data->port) ||
  104. nla_put_u8(skb, IPSET_ATTR_CIDR, data->cidr[0]) ||
  105. nla_put_u8(skb, IPSET_ATTR_CIDR2, data->cidr[1]) ||
  106. nla_put_u8(skb, IPSET_ATTR_PROTO, data->proto) ||
  107. (flags &&
  108. nla_put_net32(skb, IPSET_ATTR_CADT_FLAGS, htonl(flags))))
  109. goto nla_put_failure;
  110. return false;
  111. nla_put_failure:
  112. return true;
  113. }
  114. static inline void
  115. hash_netportnet4_data_next(struct hash_netportnet4_elem *next,
  116. const struct hash_netportnet4_elem *d)
  117. {
  118. next->ipcmp = d->ipcmp;
  119. next->port = d->port;
  120. }
  121. #define MTYPE hash_netportnet4
  122. #define HOST_MASK 32
  123. #include "ip_set_hash_gen.h"
  124. static void
  125. hash_netportnet4_init(struct hash_netportnet4_elem *e)
  126. {
  127. e->cidr[0] = HOST_MASK;
  128. e->cidr[1] = HOST_MASK;
  129. }
  130. static int
  131. hash_netportnet4_kadt(struct ip_set *set, const struct sk_buff *skb,
  132. const struct xt_action_param *par,
  133. enum ipset_adt adt, struct ip_set_adt_opt *opt)
  134. {
  135. const struct hash_netportnet *h = set->data;
  136. ipset_adtfn adtfn = set->variant->adt[adt];
  137. struct hash_netportnet4_elem e = { };
  138. struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set);
  139. e.cidr[0] = INIT_CIDR(h->nets[0].cidr[0], HOST_MASK);
  140. e.cidr[1] = INIT_CIDR(h->nets[0].cidr[1], HOST_MASK);
  141. if (adt == IPSET_TEST)
  142. e.ccmp = (HOST_MASK << (sizeof(e.cidr[0]) * 8)) | HOST_MASK;
  143. if (!ip_set_get_ip4_port(skb, opt->flags & IPSET_DIM_TWO_SRC,
  144. &e.port, &e.proto))
  145. return -EINVAL;
  146. ip4addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &e.ip[0]);
  147. ip4addrptr(skb, opt->flags & IPSET_DIM_THREE_SRC, &e.ip[1]);
  148. e.ip[0] &= ip_set_netmask(e.cidr[0]);
  149. e.ip[1] &= ip_set_netmask(e.cidr[1]);
  150. return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags);
  151. }
  152. static int
  153. hash_netportnet4_uadt(struct ip_set *set, struct nlattr *tb[],
  154. enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
  155. {
  156. const struct hash_netportnet *h = set->data;
  157. ipset_adtfn adtfn = set->variant->adt[adt];
  158. struct hash_netportnet4_elem e = { };
  159. struct ip_set_ext ext = IP_SET_INIT_UEXT(set);
  160. u32 ip = 0, ip_to = 0, ip_last, p = 0, port, port_to;
  161. u32 ip2_from = 0, ip2_to = 0, ip2_last, ip2;
  162. bool with_ports = false;
  163. int ret;
  164. if (tb[IPSET_ATTR_LINENO])
  165. *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
  166. hash_netportnet4_init(&e);
  167. if (unlikely(!tb[IPSET_ATTR_IP] || !tb[IPSET_ATTR_IP2] ||
  168. !ip_set_attr_netorder(tb, IPSET_ATTR_PORT) ||
  169. !ip_set_optattr_netorder(tb, IPSET_ATTR_PORT_TO) ||
  170. !ip_set_optattr_netorder(tb, IPSET_ATTR_CADT_FLAGS)))
  171. return -IPSET_ERR_PROTOCOL;
  172. ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP], &ip);
  173. if (ret)
  174. return ret;
  175. ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP2], &ip2_from);
  176. if (ret)
  177. return ret;
  178. ret = ip_set_get_extensions(set, tb, &ext);
  179. if (ret)
  180. return ret;
  181. if (tb[IPSET_ATTR_CIDR]) {
  182. e.cidr[0] = nla_get_u8(tb[IPSET_ATTR_CIDR]);
  183. if (e.cidr[0] > HOST_MASK)
  184. return -IPSET_ERR_INVALID_CIDR;
  185. }
  186. if (tb[IPSET_ATTR_CIDR2]) {
  187. e.cidr[1] = nla_get_u8(tb[IPSET_ATTR_CIDR2]);
  188. if (e.cidr[1] > HOST_MASK)
  189. return -IPSET_ERR_INVALID_CIDR;
  190. }
  191. e.port = nla_get_be16(tb[IPSET_ATTR_PORT]);
  192. if (tb[IPSET_ATTR_PROTO]) {
  193. e.proto = nla_get_u8(tb[IPSET_ATTR_PROTO]);
  194. with_ports = ip_set_proto_with_ports(e.proto);
  195. if (e.proto == 0)
  196. return -IPSET_ERR_INVALID_PROTO;
  197. } else {
  198. return -IPSET_ERR_MISSING_PROTO;
  199. }
  200. if (!(with_ports || e.proto == IPPROTO_ICMP))
  201. e.port = 0;
  202. if (tb[IPSET_ATTR_CADT_FLAGS]) {
  203. u32 cadt_flags = ip_set_get_h32(tb[IPSET_ATTR_CADT_FLAGS]);
  204. if (cadt_flags & IPSET_FLAG_NOMATCH)
  205. flags |= (IPSET_FLAG_NOMATCH << 16);
  206. }
  207. with_ports = with_ports && tb[IPSET_ATTR_PORT_TO];
  208. if (adt == IPSET_TEST ||
  209. !(tb[IPSET_ATTR_IP_TO] || with_ports || tb[IPSET_ATTR_IP2_TO])) {
  210. e.ip[0] = htonl(ip & ip_set_hostmask(e.cidr[0]));
  211. e.ip[1] = htonl(ip2_from & ip_set_hostmask(e.cidr[1]));
  212. ret = adtfn(set, &e, &ext, &ext, flags);
  213. return ip_set_enomatch(ret, flags, adt, set) ? -ret :
  214. ip_set_eexist(ret, flags) ? 0 : ret;
  215. }
  216. ip_to = ip;
  217. if (tb[IPSET_ATTR_IP_TO]) {
  218. ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP_TO], &ip_to);
  219. if (ret)
  220. return ret;
  221. if (ip > ip_to)
  222. swap(ip, ip_to);
  223. if (unlikely(ip + UINT_MAX == ip_to))
  224. return -IPSET_ERR_HASH_RANGE;
  225. } else {
  226. ip_set_mask_from_to(ip, ip_to, e.cidr[0]);
  227. }
  228. port_to = port = ntohs(e.port);
  229. if (tb[IPSET_ATTR_PORT_TO]) {
  230. port_to = ip_set_get_h16(tb[IPSET_ATTR_PORT_TO]);
  231. if (port > port_to)
  232. swap(port, port_to);
  233. }
  234. ip2_to = ip2_from;
  235. if (tb[IPSET_ATTR_IP2_TO]) {
  236. ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP2_TO], &ip2_to);
  237. if (ret)
  238. return ret;
  239. if (ip2_from > ip2_to)
  240. swap(ip2_from, ip2_to);
  241. if (unlikely(ip2_from + UINT_MAX == ip2_to))
  242. return -IPSET_ERR_HASH_RANGE;
  243. } else {
  244. ip_set_mask_from_to(ip2_from, ip2_to, e.cidr[1]);
  245. }
  246. if (retried)
  247. ip = ntohl(h->next.ip[0]);
  248. while (!after(ip, ip_to)) {
  249. e.ip[0] = htonl(ip);
  250. ip_last = ip_set_range_to_cidr(ip, ip_to, &e.cidr[0]);
  251. p = retried && ip == ntohl(h->next.ip[0]) ? ntohs(h->next.port)
  252. : port;
  253. for (; p <= port_to; p++) {
  254. e.port = htons(p);
  255. ip2 = (retried && ip == ntohl(h->next.ip[0]) &&
  256. p == ntohs(h->next.port)) ? ntohl(h->next.ip[1])
  257. : ip2_from;
  258. while (!after(ip2, ip2_to)) {
  259. e.ip[1] = htonl(ip2);
  260. ip2_last = ip_set_range_to_cidr(ip2, ip2_to,
  261. &e.cidr[1]);
  262. ret = adtfn(set, &e, &ext, &ext, flags);
  263. if (ret && !ip_set_eexist(ret, flags))
  264. return ret;
  265. ret = 0;
  266. ip2 = ip2_last + 1;
  267. }
  268. }
  269. ip = ip_last + 1;
  270. }
  271. return ret;
  272. }
  273. /* IPv6 variant */
  274. struct hash_netportnet6_elem {
  275. union nf_inet_addr ip[2];
  276. __be16 port;
  277. union {
  278. u8 cidr[2];
  279. u16 ccmp;
  280. };
  281. u16 padding;
  282. u8 nomatch;
  283. u8 proto;
  284. };
  285. /* Common functions */
  286. static inline bool
  287. hash_netportnet6_data_equal(const struct hash_netportnet6_elem *ip1,
  288. const struct hash_netportnet6_elem *ip2,
  289. u32 *multi)
  290. {
  291. return ipv6_addr_equal(&ip1->ip[0].in6, &ip2->ip[0].in6) &&
  292. ipv6_addr_equal(&ip1->ip[1].in6, &ip2->ip[1].in6) &&
  293. ip1->ccmp == ip2->ccmp &&
  294. ip1->port == ip2->port &&
  295. ip1->proto == ip2->proto;
  296. }
  297. static inline int
  298. hash_netportnet6_do_data_match(const struct hash_netportnet6_elem *elem)
  299. {
  300. return elem->nomatch ? -ENOTEMPTY : 1;
  301. }
  302. static inline void
  303. hash_netportnet6_data_set_flags(struct hash_netportnet6_elem *elem, u32 flags)
  304. {
  305. elem->nomatch = !!((flags >> 16) & IPSET_FLAG_NOMATCH);
  306. }
  307. static inline void
  308. hash_netportnet6_data_reset_flags(struct hash_netportnet6_elem *elem, u8 *flags)
  309. {
  310. swap(*flags, elem->nomatch);
  311. }
  312. static inline void
  313. hash_netportnet6_data_reset_elem(struct hash_netportnet6_elem *elem,
  314. struct hash_netportnet6_elem *orig)
  315. {
  316. elem->ip[1] = orig->ip[1];
  317. }
  318. static inline void
  319. hash_netportnet6_data_netmask(struct hash_netportnet6_elem *elem,
  320. u8 cidr, bool inner)
  321. {
  322. if (inner) {
  323. ip6_netmask(&elem->ip[1], cidr);
  324. elem->cidr[1] = cidr;
  325. } else {
  326. ip6_netmask(&elem->ip[0], cidr);
  327. elem->cidr[0] = cidr;
  328. }
  329. }
  330. static bool
  331. hash_netportnet6_data_list(struct sk_buff *skb,
  332. const struct hash_netportnet6_elem *data)
  333. {
  334. u32 flags = data->nomatch ? IPSET_FLAG_NOMATCH : 0;
  335. if (nla_put_ipaddr6(skb, IPSET_ATTR_IP, &data->ip[0].in6) ||
  336. nla_put_ipaddr6(skb, IPSET_ATTR_IP2, &data->ip[1].in6) ||
  337. nla_put_net16(skb, IPSET_ATTR_PORT, data->port) ||
  338. nla_put_u8(skb, IPSET_ATTR_CIDR, data->cidr[0]) ||
  339. nla_put_u8(skb, IPSET_ATTR_CIDR2, data->cidr[1]) ||
  340. nla_put_u8(skb, IPSET_ATTR_PROTO, data->proto) ||
  341. (flags &&
  342. nla_put_net32(skb, IPSET_ATTR_CADT_FLAGS, htonl(flags))))
  343. goto nla_put_failure;
  344. return false;
  345. nla_put_failure:
  346. return true;
  347. }
  348. static inline void
  349. hash_netportnet6_data_next(struct hash_netportnet4_elem *next,
  350. const struct hash_netportnet6_elem *d)
  351. {
  352. next->port = d->port;
  353. }
  354. #undef MTYPE
  355. #undef HOST_MASK
  356. #define MTYPE hash_netportnet6
  357. #define HOST_MASK 128
  358. #define IP_SET_EMIT_CREATE
  359. #include "ip_set_hash_gen.h"
  360. static void
  361. hash_netportnet6_init(struct hash_netportnet6_elem *e)
  362. {
  363. e->cidr[0] = HOST_MASK;
  364. e->cidr[1] = HOST_MASK;
  365. }
  366. static int
  367. hash_netportnet6_kadt(struct ip_set *set, const struct sk_buff *skb,
  368. const struct xt_action_param *par,
  369. enum ipset_adt adt, struct ip_set_adt_opt *opt)
  370. {
  371. const struct hash_netportnet *h = set->data;
  372. ipset_adtfn adtfn = set->variant->adt[adt];
  373. struct hash_netportnet6_elem e = { };
  374. struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set);
  375. e.cidr[0] = INIT_CIDR(h->nets[0].cidr[0], HOST_MASK);
  376. e.cidr[1] = INIT_CIDR(h->nets[0].cidr[1], HOST_MASK);
  377. if (adt == IPSET_TEST)
  378. e.ccmp = (HOST_MASK << (sizeof(u8) * 8)) | HOST_MASK;
  379. if (!ip_set_get_ip6_port(skb, opt->flags & IPSET_DIM_TWO_SRC,
  380. &e.port, &e.proto))
  381. return -EINVAL;
  382. ip6addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &e.ip[0].in6);
  383. ip6addrptr(skb, opt->flags & IPSET_DIM_THREE_SRC, &e.ip[1].in6);
  384. ip6_netmask(&e.ip[0], e.cidr[0]);
  385. ip6_netmask(&e.ip[1], e.cidr[1]);
  386. return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags);
  387. }
  388. static int
  389. hash_netportnet6_uadt(struct ip_set *set, struct nlattr *tb[],
  390. enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
  391. {
  392. const struct hash_netportnet *h = set->data;
  393. ipset_adtfn adtfn = set->variant->adt[adt];
  394. struct hash_netportnet6_elem e = { };
  395. struct ip_set_ext ext = IP_SET_INIT_UEXT(set);
  396. u32 port, port_to;
  397. bool with_ports = false;
  398. int ret;
  399. if (tb[IPSET_ATTR_LINENO])
  400. *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
  401. hash_netportnet6_init(&e);
  402. if (unlikely(!tb[IPSET_ATTR_IP] || !tb[IPSET_ATTR_IP2] ||
  403. !ip_set_attr_netorder(tb, IPSET_ATTR_PORT) ||
  404. !ip_set_optattr_netorder(tb, IPSET_ATTR_PORT_TO) ||
  405. !ip_set_optattr_netorder(tb, IPSET_ATTR_CADT_FLAGS)))
  406. return -IPSET_ERR_PROTOCOL;
  407. if (unlikely(tb[IPSET_ATTR_IP_TO] || tb[IPSET_ATTR_IP2_TO]))
  408. return -IPSET_ERR_HASH_RANGE_UNSUPPORTED;
  409. ret = ip_set_get_ipaddr6(tb[IPSET_ATTR_IP], &e.ip[0]);
  410. if (ret)
  411. return ret;
  412. ret = ip_set_get_ipaddr6(tb[IPSET_ATTR_IP2], &e.ip[1]);
  413. if (ret)
  414. return ret;
  415. ret = ip_set_get_extensions(set, tb, &ext);
  416. if (ret)
  417. return ret;
  418. if (tb[IPSET_ATTR_CIDR]) {
  419. e.cidr[0] = nla_get_u8(tb[IPSET_ATTR_CIDR]);
  420. if (e.cidr[0] > HOST_MASK)
  421. return -IPSET_ERR_INVALID_CIDR;
  422. }
  423. if (tb[IPSET_ATTR_CIDR2]) {
  424. e.cidr[1] = nla_get_u8(tb[IPSET_ATTR_CIDR2]);
  425. if (e.cidr[1] > HOST_MASK)
  426. return -IPSET_ERR_INVALID_CIDR;
  427. }
  428. ip6_netmask(&e.ip[0], e.cidr[0]);
  429. ip6_netmask(&e.ip[1], e.cidr[1]);
  430. e.port = nla_get_be16(tb[IPSET_ATTR_PORT]);
  431. if (tb[IPSET_ATTR_PROTO]) {
  432. e.proto = nla_get_u8(tb[IPSET_ATTR_PROTO]);
  433. with_ports = ip_set_proto_with_ports(e.proto);
  434. if (e.proto == 0)
  435. return -IPSET_ERR_INVALID_PROTO;
  436. } else {
  437. return -IPSET_ERR_MISSING_PROTO;
  438. }
  439. if (!(with_ports || e.proto == IPPROTO_ICMPV6))
  440. e.port = 0;
  441. if (tb[IPSET_ATTR_CADT_FLAGS]) {
  442. u32 cadt_flags = ip_set_get_h32(tb[IPSET_ATTR_CADT_FLAGS]);
  443. if (cadt_flags & IPSET_FLAG_NOMATCH)
  444. flags |= (IPSET_FLAG_NOMATCH << 16);
  445. }
  446. if (adt == IPSET_TEST || !with_ports || !tb[IPSET_ATTR_PORT_TO]) {
  447. ret = adtfn(set, &e, &ext, &ext, flags);
  448. return ip_set_enomatch(ret, flags, adt, set) ? -ret :
  449. ip_set_eexist(ret, flags) ? 0 : ret;
  450. }
  451. port = ntohs(e.port);
  452. port_to = ip_set_get_h16(tb[IPSET_ATTR_PORT_TO]);
  453. if (port > port_to)
  454. swap(port, port_to);
  455. if (retried)
  456. port = ntohs(h->next.port);
  457. for (; port <= port_to; port++) {
  458. e.port = htons(port);
  459. ret = adtfn(set, &e, &ext, &ext, flags);
  460. if (ret && !ip_set_eexist(ret, flags))
  461. return ret;
  462. ret = 0;
  463. }
  464. return ret;
  465. }
  466. static struct ip_set_type hash_netportnet_type __read_mostly = {
  467. .name = "hash:net,port,net",
  468. .protocol = IPSET_PROTOCOL,
  469. .features = IPSET_TYPE_IP | IPSET_TYPE_PORT | IPSET_TYPE_IP2 |
  470. IPSET_TYPE_NOMATCH,
  471. .dimension = IPSET_DIM_THREE,
  472. .family = NFPROTO_UNSPEC,
  473. .revision_min = IPSET_TYPE_REV_MIN,
  474. .revision_max = IPSET_TYPE_REV_MAX,
  475. .create = hash_netportnet_create,
  476. .create_policy = {
  477. [IPSET_ATTR_HASHSIZE] = { .type = NLA_U32 },
  478. [IPSET_ATTR_MAXELEM] = { .type = NLA_U32 },
  479. [IPSET_ATTR_PROBES] = { .type = NLA_U8 },
  480. [IPSET_ATTR_RESIZE] = { .type = NLA_U8 },
  481. [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
  482. [IPSET_ATTR_CADT_FLAGS] = { .type = NLA_U32 },
  483. },
  484. .adt_policy = {
  485. [IPSET_ATTR_IP] = { .type = NLA_NESTED },
  486. [IPSET_ATTR_IP_TO] = { .type = NLA_NESTED },
  487. [IPSET_ATTR_IP2] = { .type = NLA_NESTED },
  488. [IPSET_ATTR_IP2_TO] = { .type = NLA_NESTED },
  489. [IPSET_ATTR_PORT] = { .type = NLA_U16 },
  490. [IPSET_ATTR_PORT_TO] = { .type = NLA_U16 },
  491. [IPSET_ATTR_CIDR] = { .type = NLA_U8 },
  492. [IPSET_ATTR_CIDR2] = { .type = NLA_U8 },
  493. [IPSET_ATTR_PROTO] = { .type = NLA_U8 },
  494. [IPSET_ATTR_CADT_FLAGS] = { .type = NLA_U32 },
  495. [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
  496. [IPSET_ATTR_LINENO] = { .type = NLA_U32 },
  497. [IPSET_ATTR_BYTES] = { .type = NLA_U64 },
  498. [IPSET_ATTR_PACKETS] = { .type = NLA_U64 },
  499. [IPSET_ATTR_COMMENT] = { .type = NLA_NUL_STRING,
  500. .len = IPSET_MAX_COMMENT_SIZE },
  501. [IPSET_ATTR_SKBMARK] = { .type = NLA_U64 },
  502. [IPSET_ATTR_SKBPRIO] = { .type = NLA_U32 },
  503. [IPSET_ATTR_SKBQUEUE] = { .type = NLA_U16 },
  504. },
  505. .me = THIS_MODULE,
  506. };
  507. static int __init
  508. hash_netportnet_init(void)
  509. {
  510. return ip_set_type_register(&hash_netportnet_type);
  511. }
  512. static void __exit
  513. hash_netportnet_fini(void)
  514. {
  515. rcu_barrier();
  516. ip_set_type_unregister(&hash_netportnet_type);
  517. }
  518. module_init(hash_netportnet_init);
  519. module_exit(hash_netportnet_fini);