ip_set_bitmap_ip.c 9.6 KB

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