ip_set_bitmap_ip.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* Copyright (C) 2000-2002 Joakim Axelsson <gozem@linux.nu>
  3. * Patrick Schaaf <bof@bof.de>
  4. * Copyright (C) 2003-2013 Jozsef Kadlecsik <kadlec@netfilter.org>
  5. */
  6. /* Kernel module implementing an IP set type: the bitmap:ip type */
  7. #include <linux/module.h>
  8. #include <linux/ip.h>
  9. #include <linux/skbuff.h>
  10. #include <linux/errno.h>
  11. #include <linux/bitops.h>
  12. #include <linux/spinlock.h>
  13. #include <linux/netlink.h>
  14. #include <linux/jiffies.h>
  15. #include <linux/timer.h>
  16. #include <net/netlink.h>
  17. #include <net/tcp.h>
  18. #include <linux/netfilter/ipset/pfxlen.h>
  19. #include <linux/netfilter/ipset/ip_set.h>
  20. #include <linux/netfilter/ipset/ip_set_bitmap.h>
  21. #define IPSET_TYPE_REV_MIN 0
  22. /* 1 Counter support added */
  23. /* 2 Comment support added */
  24. #define IPSET_TYPE_REV_MAX 3 /* skbinfo support added */
  25. MODULE_LICENSE("GPL");
  26. MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@netfilter.org>");
  27. IP_SET_MODULE_DESC("bitmap:ip", IPSET_TYPE_REV_MIN, IPSET_TYPE_REV_MAX);
  28. MODULE_ALIAS("ip_set_bitmap:ip");
  29. #define MTYPE bitmap_ip
  30. #define HOST_MASK 32
  31. /* Type structure */
  32. struct bitmap_ip {
  33. unsigned long *members; /* the set members */
  34. u32 first_ip; /* host byte order, included in range */
  35. u32 last_ip; /* host byte order, included in range */
  36. u32 elements; /* number of max elements in the set */
  37. u32 hosts; /* number of hosts in a subnet */
  38. size_t memsize; /* members size */
  39. u8 netmask; /* subnet netmask */
  40. struct timer_list gc; /* garbage collection */
  41. struct ip_set *set; /* attached to this ip_set */
  42. unsigned char extensions[] /* data extensions */
  43. __aligned(__alignof__(u64));
  44. };
  45. /* ADT structure for generic function args */
  46. struct bitmap_ip_adt_elem {
  47. u16 id;
  48. };
  49. static u32
  50. ip_to_id(const struct bitmap_ip *m, u32 ip)
  51. {
  52. return ((ip & ip_set_hostmask(m->netmask)) - m->first_ip) / m->hosts;
  53. }
  54. /* Common functions */
  55. static int
  56. bitmap_ip_do_test(const struct bitmap_ip_adt_elem *e,
  57. struct bitmap_ip *map, size_t dsize)
  58. {
  59. return !!test_bit(e->id, map->members);
  60. }
  61. static int
  62. bitmap_ip_gc_test(u16 id, const struct bitmap_ip *map, size_t dsize)
  63. {
  64. return !!test_bit(id, map->members);
  65. }
  66. static int
  67. bitmap_ip_do_add(const struct bitmap_ip_adt_elem *e, struct bitmap_ip *map,
  68. u32 flags, size_t dsize)
  69. {
  70. return !!test_bit(e->id, map->members);
  71. }
  72. static int
  73. bitmap_ip_do_del(const struct bitmap_ip_adt_elem *e, struct bitmap_ip *map)
  74. {
  75. return !test_and_clear_bit(e->id, map->members);
  76. }
  77. static int
  78. bitmap_ip_do_list(struct sk_buff *skb, const struct bitmap_ip *map, u32 id,
  79. size_t dsize)
  80. {
  81. return nla_put_ipaddr4(skb, IPSET_ATTR_IP,
  82. htonl(map->first_ip + id * map->hosts));
  83. }
  84. static int
  85. bitmap_ip_do_head(struct sk_buff *skb, const struct bitmap_ip *map)
  86. {
  87. return nla_put_ipaddr4(skb, IPSET_ATTR_IP, htonl(map->first_ip)) ||
  88. nla_put_ipaddr4(skb, IPSET_ATTR_IP_TO, htonl(map->last_ip)) ||
  89. (map->netmask != 32 &&
  90. nla_put_u8(skb, IPSET_ATTR_NETMASK, map->netmask));
  91. }
  92. static int
  93. bitmap_ip_kadt(struct ip_set *set, const struct sk_buff *skb,
  94. const struct xt_action_param *par,
  95. enum ipset_adt adt, struct ip_set_adt_opt *opt)
  96. {
  97. struct bitmap_ip *map = set->data;
  98. ipset_adtfn adtfn = set->variant->adt[adt];
  99. struct bitmap_ip_adt_elem e = { .id = 0 };
  100. struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set);
  101. u32 ip;
  102. ip = ntohl(ip4addr(skb, opt->flags & IPSET_DIM_ONE_SRC));
  103. if (ip < map->first_ip || ip > map->last_ip)
  104. return -IPSET_ERR_BITMAP_RANGE;
  105. e.id = ip_to_id(map, ip);
  106. return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags);
  107. }
  108. static int
  109. bitmap_ip_uadt(struct ip_set *set, struct nlattr *tb[],
  110. enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
  111. {
  112. struct bitmap_ip *map = set->data;
  113. ipset_adtfn adtfn = set->variant->adt[adt];
  114. u32 ip = 0, ip_to = 0;
  115. struct bitmap_ip_adt_elem e = { .id = 0 };
  116. struct ip_set_ext ext = IP_SET_INIT_UEXT(set);
  117. int ret = 0;
  118. if (tb[IPSET_ATTR_LINENO])
  119. *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
  120. if (unlikely(!tb[IPSET_ATTR_IP]))
  121. return -IPSET_ERR_PROTOCOL;
  122. ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP], &ip);
  123. if (ret)
  124. return ret;
  125. ret = ip_set_get_extensions(set, tb, &ext);
  126. if (ret)
  127. return ret;
  128. if (ip < map->first_ip || ip > map->last_ip)
  129. return -IPSET_ERR_BITMAP_RANGE;
  130. if (adt == IPSET_TEST) {
  131. e.id = ip_to_id(map, ip);
  132. return adtfn(set, &e, &ext, &ext, flags);
  133. }
  134. if (tb[IPSET_ATTR_IP_TO]) {
  135. ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP_TO], &ip_to);
  136. if (ret)
  137. return ret;
  138. if (ip > ip_to) {
  139. swap(ip, ip_to);
  140. if (ip < map->first_ip)
  141. return -IPSET_ERR_BITMAP_RANGE;
  142. }
  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. } else {
  149. ip_to = ip;
  150. }
  151. if (ip_to > map->last_ip)
  152. return -IPSET_ERR_BITMAP_RANGE;
  153. for (; !before(ip_to, ip); ip += map->hosts) {
  154. e.id = ip_to_id(map, ip);
  155. ret = adtfn(set, &e, &ext, &ext, flags);
  156. if (ret && !ip_set_eexist(ret, flags))
  157. return ret;
  158. ret = 0;
  159. }
  160. return ret;
  161. }
  162. static bool
  163. bitmap_ip_same_set(const struct ip_set *a, const struct ip_set *b)
  164. {
  165. const struct bitmap_ip *x = a->data;
  166. const struct bitmap_ip *y = b->data;
  167. return x->first_ip == y->first_ip &&
  168. x->last_ip == y->last_ip &&
  169. x->netmask == y->netmask &&
  170. a->timeout == b->timeout &&
  171. a->extensions == b->extensions;
  172. }
  173. /* Plain variant */
  174. struct bitmap_ip_elem {
  175. };
  176. #include "ip_set_bitmap_gen.h"
  177. /* Create bitmap:ip type of sets */
  178. static bool
  179. init_map_ip(struct ip_set *set, struct bitmap_ip *map,
  180. u32 first_ip, u32 last_ip,
  181. u32 elements, u32 hosts, u8 netmask)
  182. {
  183. map->members = bitmap_zalloc(elements, GFP_KERNEL | __GFP_NOWARN);
  184. if (!map->members)
  185. return false;
  186. map->first_ip = first_ip;
  187. map->last_ip = last_ip;
  188. map->elements = elements;
  189. map->hosts = hosts;
  190. map->netmask = netmask;
  191. set->timeout = IPSET_NO_TIMEOUT;
  192. map->set = set;
  193. set->data = map;
  194. set->family = NFPROTO_IPV4;
  195. return true;
  196. }
  197. static u32
  198. range_to_mask(u32 from, u32 to, u8 *bits)
  199. {
  200. u32 mask = 0xFFFFFFFE;
  201. *bits = 32;
  202. while (--(*bits) > 0 && mask && (to & mask) != from)
  203. mask <<= 1;
  204. return mask;
  205. }
  206. static int
  207. bitmap_ip_create(struct net *net, struct ip_set *set, struct nlattr *tb[],
  208. u32 flags)
  209. {
  210. struct bitmap_ip *map;
  211. u32 first_ip = 0, last_ip = 0, hosts;
  212. u64 elements;
  213. u8 netmask = 32;
  214. int ret;
  215. if (unlikely(!tb[IPSET_ATTR_IP] ||
  216. !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT) ||
  217. !ip_set_optattr_netorder(tb, IPSET_ATTR_CADT_FLAGS)))
  218. return -IPSET_ERR_PROTOCOL;
  219. ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP], &first_ip);
  220. if (ret)
  221. return ret;
  222. if (tb[IPSET_ATTR_IP_TO]) {
  223. ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP_TO], &last_ip);
  224. if (ret)
  225. return ret;
  226. if (first_ip > last_ip)
  227. swap(first_ip, last_ip);
  228. } else if (tb[IPSET_ATTR_CIDR]) {
  229. u8 cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
  230. if (cidr >= HOST_MASK)
  231. return -IPSET_ERR_INVALID_CIDR;
  232. ip_set_mask_from_to(first_ip, last_ip, cidr);
  233. } else {
  234. return -IPSET_ERR_PROTOCOL;
  235. }
  236. if (tb[IPSET_ATTR_NETMASK]) {
  237. netmask = nla_get_u8(tb[IPSET_ATTR_NETMASK]);
  238. if (netmask > HOST_MASK)
  239. return -IPSET_ERR_INVALID_NETMASK;
  240. first_ip &= ip_set_hostmask(netmask);
  241. last_ip |= ~ip_set_hostmask(netmask);
  242. }
  243. if (netmask == 32) {
  244. hosts = 1;
  245. elements = (u64)last_ip - first_ip + 1;
  246. } else {
  247. u8 mask_bits;
  248. u32 mask;
  249. mask = range_to_mask(first_ip, last_ip, &mask_bits);
  250. if ((!mask && (first_ip || last_ip != 0xFFFFFFFF)) ||
  251. netmask <= mask_bits)
  252. return -IPSET_ERR_BITMAP_RANGE;
  253. pr_debug("mask_bits %u, netmask %u\n", mask_bits, netmask);
  254. hosts = 2 << (32 - netmask - 1);
  255. elements = 2 << (netmask - mask_bits - 1);
  256. }
  257. if (elements > IPSET_BITMAP_MAX_RANGE + 1)
  258. return -IPSET_ERR_BITMAP_RANGE_SIZE;
  259. pr_debug("hosts %u, elements %llu\n",
  260. hosts, (unsigned long long)elements);
  261. set->dsize = ip_set_elem_len(set, tb, 0, 0);
  262. map = ip_set_alloc(sizeof(*map) + elements * set->dsize);
  263. if (!map)
  264. return -ENOMEM;
  265. map->memsize = BITS_TO_LONGS(elements) * sizeof(unsigned long);
  266. set->variant = &bitmap_ip;
  267. if (!init_map_ip(set, map, first_ip, last_ip,
  268. elements, hosts, netmask)) {
  269. ip_set_free(map);
  270. return -ENOMEM;
  271. }
  272. if (tb[IPSET_ATTR_TIMEOUT]) {
  273. set->timeout = ip_set_timeout_uget(tb[IPSET_ATTR_TIMEOUT]);
  274. bitmap_ip_gc_init(set, bitmap_ip_gc);
  275. }
  276. return 0;
  277. }
  278. static struct ip_set_type bitmap_ip_type __read_mostly = {
  279. .name = "bitmap:ip",
  280. .protocol = IPSET_PROTOCOL,
  281. .features = IPSET_TYPE_IP,
  282. .dimension = IPSET_DIM_ONE,
  283. .family = NFPROTO_IPV4,
  284. .revision_min = IPSET_TYPE_REV_MIN,
  285. .revision_max = IPSET_TYPE_REV_MAX,
  286. .create = bitmap_ip_create,
  287. .create_policy = {
  288. [IPSET_ATTR_IP] = { .type = NLA_NESTED },
  289. [IPSET_ATTR_IP_TO] = { .type = NLA_NESTED },
  290. [IPSET_ATTR_CIDR] = { .type = NLA_U8 },
  291. [IPSET_ATTR_NETMASK] = { .type = NLA_U8 },
  292. [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
  293. [IPSET_ATTR_CADT_FLAGS] = { .type = NLA_U32 },
  294. },
  295. .adt_policy = {
  296. [IPSET_ATTR_IP] = { .type = NLA_NESTED },
  297. [IPSET_ATTR_IP_TO] = { .type = NLA_NESTED },
  298. [IPSET_ATTR_CIDR] = { .type = NLA_U8 },
  299. [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
  300. [IPSET_ATTR_LINENO] = { .type = NLA_U32 },
  301. [IPSET_ATTR_BYTES] = { .type = NLA_U64 },
  302. [IPSET_ATTR_PACKETS] = { .type = NLA_U64 },
  303. [IPSET_ATTR_COMMENT] = { .type = NLA_NUL_STRING,
  304. .len = IPSET_MAX_COMMENT_SIZE },
  305. [IPSET_ATTR_SKBMARK] = { .type = NLA_U64 },
  306. [IPSET_ATTR_SKBPRIO] = { .type = NLA_U32 },
  307. [IPSET_ATTR_SKBQUEUE] = { .type = NLA_U16 },
  308. },
  309. .me = THIS_MODULE,
  310. };
  311. static int __init
  312. bitmap_ip_init(void)
  313. {
  314. return ip_set_type_register(&bitmap_ip_type);
  315. }
  316. static void __exit
  317. bitmap_ip_fini(void)
  318. {
  319. rcu_barrier();
  320. ip_set_type_unregister(&bitmap_ip_type);
  321. }
  322. module_init(bitmap_ip_init);
  323. module_exit(bitmap_ip_fini);