ip_set_bitmap_ipmac.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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. * Martin Josefsson <gandalf@wlug.westbo.se>
  5. */
  6. /* Kernel module implementing an IP set type: the bitmap:ip,mac type */
  7. #include <linux/module.h>
  8. #include <linux/ip.h>
  9. #include <linux/etherdevice.h>
  10. #include <linux/skbuff.h>
  11. #include <linux/errno.h>
  12. #include <linux/if_ether.h>
  13. #include <linux/netlink.h>
  14. #include <linux/jiffies.h>
  15. #include <linux/timer.h>
  16. #include <net/netlink.h>
  17. #include <linux/netfilter/ipset/pfxlen.h>
  18. #include <linux/netfilter/ipset/ip_set.h>
  19. #include <linux/netfilter/ipset/ip_set_bitmap.h>
  20. #define IPSET_TYPE_REV_MIN 0
  21. /* 1 Counter support added */
  22. /* 2 Comment support added */
  23. #define IPSET_TYPE_REV_MAX 3 /* skbinfo support added */
  24. MODULE_LICENSE("GPL");
  25. MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@netfilter.org>");
  26. IP_SET_MODULE_DESC("bitmap:ip,mac", IPSET_TYPE_REV_MIN, IPSET_TYPE_REV_MAX);
  27. MODULE_ALIAS("ip_set_bitmap:ip,mac");
  28. #define MTYPE bitmap_ipmac
  29. #define HOST_MASK 32
  30. #define IP_SET_BITMAP_STORED_TIMEOUT
  31. enum {
  32. MAC_UNSET, /* element is set, without MAC */
  33. MAC_FILLED, /* element is set with MAC */
  34. };
  35. /* Type structure */
  36. struct bitmap_ipmac {
  37. unsigned long *members; /* the set members */
  38. u32 first_ip; /* host byte order, included in range */
  39. u32 last_ip; /* host byte order, included in range */
  40. u32 elements; /* number of max elements in the set */
  41. size_t memsize; /* members size */
  42. struct timer_list gc; /* garbage collector */
  43. struct ip_set *set; /* attached to this ip_set */
  44. unsigned char extensions[] /* MAC + data extensions */
  45. __aligned(__alignof__(u64));
  46. };
  47. /* ADT structure for generic function args */
  48. struct bitmap_ipmac_adt_elem {
  49. unsigned char ether[ETH_ALEN] __aligned(2);
  50. u16 id;
  51. u16 add_mac;
  52. };
  53. struct bitmap_ipmac_elem {
  54. unsigned char ether[ETH_ALEN];
  55. unsigned char filled;
  56. } __aligned(__alignof__(u64));
  57. static u32
  58. ip_to_id(const struct bitmap_ipmac *m, u32 ip)
  59. {
  60. return ip - m->first_ip;
  61. }
  62. #define get_elem(extensions, id, dsize) \
  63. (struct bitmap_ipmac_elem *)(extensions + (id) * (dsize))
  64. #define get_const_elem(extensions, id, dsize) \
  65. (const struct bitmap_ipmac_elem *)(extensions + (id) * (dsize))
  66. /* Common functions */
  67. static int
  68. bitmap_ipmac_do_test(const struct bitmap_ipmac_adt_elem *e,
  69. const struct bitmap_ipmac *map, size_t dsize)
  70. {
  71. const struct bitmap_ipmac_elem *elem;
  72. if (!test_bit(e->id, map->members))
  73. return 0;
  74. elem = get_const_elem(map->extensions, e->id, dsize);
  75. if (e->add_mac && elem->filled == MAC_FILLED)
  76. return ether_addr_equal(e->ether, elem->ether);
  77. /* Trigger kernel to fill out the ethernet address */
  78. return -EAGAIN;
  79. }
  80. static int
  81. bitmap_ipmac_gc_test(u16 id, const struct bitmap_ipmac *map, size_t dsize)
  82. {
  83. const struct bitmap_ipmac_elem *elem;
  84. if (!test_bit(id, map->members))
  85. return 0;
  86. elem = get_const_elem(map->extensions, id, dsize);
  87. /* Timer not started for the incomplete elements */
  88. return elem->filled == MAC_FILLED;
  89. }
  90. static int
  91. bitmap_ipmac_is_filled(const struct bitmap_ipmac_elem *elem)
  92. {
  93. return elem->filled == MAC_FILLED;
  94. }
  95. static int
  96. bitmap_ipmac_add_timeout(unsigned long *timeout,
  97. const struct bitmap_ipmac_adt_elem *e,
  98. const struct ip_set_ext *ext, struct ip_set *set,
  99. struct bitmap_ipmac *map, int mode)
  100. {
  101. u32 t = ext->timeout;
  102. if (mode == IPSET_ADD_START_STORED_TIMEOUT) {
  103. if (t == set->timeout)
  104. /* Timeout was not specified, get stored one */
  105. t = *timeout;
  106. ip_set_timeout_set(timeout, t);
  107. } else {
  108. /* If MAC is unset yet, we store plain timeout value
  109. * because the timer is not activated yet
  110. * and we can reuse it later when MAC is filled out,
  111. * possibly by the kernel
  112. */
  113. if (e->add_mac)
  114. ip_set_timeout_set(timeout, t);
  115. else
  116. *timeout = t;
  117. }
  118. return 0;
  119. }
  120. static int
  121. bitmap_ipmac_do_add(const struct bitmap_ipmac_adt_elem *e,
  122. struct bitmap_ipmac *map, u32 flags, size_t dsize)
  123. {
  124. struct bitmap_ipmac_elem *elem;
  125. elem = get_elem(map->extensions, e->id, dsize);
  126. if (test_bit(e->id, map->members)) {
  127. if (elem->filled == MAC_FILLED) {
  128. if (e->add_mac &&
  129. (flags & IPSET_FLAG_EXIST) &&
  130. !ether_addr_equal(e->ether, elem->ether)) {
  131. /* memcpy isn't atomic */
  132. clear_bit(e->id, map->members);
  133. smp_mb__after_atomic();
  134. ether_addr_copy(elem->ether, e->ether);
  135. }
  136. return IPSET_ADD_FAILED;
  137. } else if (!e->add_mac)
  138. /* Already added without ethernet address */
  139. return IPSET_ADD_FAILED;
  140. /* Fill the MAC address and trigger the timer activation */
  141. clear_bit(e->id, map->members);
  142. smp_mb__after_atomic();
  143. ether_addr_copy(elem->ether, e->ether);
  144. elem->filled = MAC_FILLED;
  145. return IPSET_ADD_START_STORED_TIMEOUT;
  146. } else if (e->add_mac) {
  147. /* We can store MAC too */
  148. ether_addr_copy(elem->ether, e->ether);
  149. elem->filled = MAC_FILLED;
  150. return 0;
  151. }
  152. elem->filled = MAC_UNSET;
  153. /* MAC is not stored yet, don't start timer */
  154. return IPSET_ADD_STORE_PLAIN_TIMEOUT;
  155. }
  156. static int
  157. bitmap_ipmac_do_del(const struct bitmap_ipmac_adt_elem *e,
  158. struct bitmap_ipmac *map)
  159. {
  160. return !test_and_clear_bit(e->id, map->members);
  161. }
  162. static int
  163. bitmap_ipmac_do_list(struct sk_buff *skb, const struct bitmap_ipmac *map,
  164. u32 id, size_t dsize)
  165. {
  166. const struct bitmap_ipmac_elem *elem =
  167. get_const_elem(map->extensions, id, dsize);
  168. return nla_put_ipaddr4(skb, IPSET_ATTR_IP,
  169. htonl(map->first_ip + id)) ||
  170. (elem->filled == MAC_FILLED &&
  171. nla_put(skb, IPSET_ATTR_ETHER, ETH_ALEN, elem->ether));
  172. }
  173. static int
  174. bitmap_ipmac_do_head(struct sk_buff *skb, const struct bitmap_ipmac *map)
  175. {
  176. return nla_put_ipaddr4(skb, IPSET_ATTR_IP, htonl(map->first_ip)) ||
  177. nla_put_ipaddr4(skb, IPSET_ATTR_IP_TO, htonl(map->last_ip));
  178. }
  179. static int
  180. bitmap_ipmac_kadt(struct ip_set *set, const struct sk_buff *skb,
  181. const struct xt_action_param *par,
  182. enum ipset_adt adt, struct ip_set_adt_opt *opt)
  183. {
  184. struct bitmap_ipmac *map = set->data;
  185. ipset_adtfn adtfn = set->variant->adt[adt];
  186. struct bitmap_ipmac_adt_elem e = { .id = 0, .add_mac = 1 };
  187. struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set);
  188. u32 ip;
  189. ip = ntohl(ip4addr(skb, opt->flags & IPSET_DIM_ONE_SRC));
  190. if (ip < map->first_ip || ip > map->last_ip)
  191. return -IPSET_ERR_BITMAP_RANGE;
  192. /* Backward compatibility: we don't check the second flag */
  193. if (skb_mac_header(skb) < skb->head ||
  194. (skb_mac_header(skb) + ETH_HLEN) > skb->data)
  195. return -EINVAL;
  196. e.id = ip_to_id(map, ip);
  197. if (opt->flags & IPSET_DIM_TWO_SRC)
  198. ether_addr_copy(e.ether, eth_hdr(skb)->h_source);
  199. else
  200. ether_addr_copy(e.ether, eth_hdr(skb)->h_dest);
  201. if (is_zero_ether_addr(e.ether))
  202. return -EINVAL;
  203. return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags);
  204. }
  205. static int
  206. bitmap_ipmac_uadt(struct ip_set *set, struct nlattr *tb[],
  207. enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
  208. {
  209. const struct bitmap_ipmac *map = set->data;
  210. ipset_adtfn adtfn = set->variant->adt[adt];
  211. struct bitmap_ipmac_adt_elem e = { .id = 0 };
  212. struct ip_set_ext ext = IP_SET_INIT_UEXT(set);
  213. u32 ip = 0;
  214. int ret = 0;
  215. if (tb[IPSET_ATTR_LINENO])
  216. *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
  217. if (unlikely(!tb[IPSET_ATTR_IP]))
  218. return -IPSET_ERR_PROTOCOL;
  219. ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP], &ip);
  220. if (ret)
  221. return ret;
  222. ret = ip_set_get_extensions(set, tb, &ext);
  223. if (ret)
  224. return ret;
  225. if (ip < map->first_ip || ip > map->last_ip)
  226. return -IPSET_ERR_BITMAP_RANGE;
  227. e.id = ip_to_id(map, ip);
  228. if (tb[IPSET_ATTR_ETHER]) {
  229. if (nla_len(tb[IPSET_ATTR_ETHER]) != ETH_ALEN)
  230. return -IPSET_ERR_PROTOCOL;
  231. memcpy(e.ether, nla_data(tb[IPSET_ATTR_ETHER]), ETH_ALEN);
  232. e.add_mac = 1;
  233. }
  234. ret = adtfn(set, &e, &ext, &ext, flags);
  235. return ip_set_eexist(ret, flags) ? 0 : ret;
  236. }
  237. static bool
  238. bitmap_ipmac_same_set(const struct ip_set *a, const struct ip_set *b)
  239. {
  240. const struct bitmap_ipmac *x = a->data;
  241. const struct bitmap_ipmac *y = b->data;
  242. return x->first_ip == y->first_ip &&
  243. x->last_ip == y->last_ip &&
  244. a->timeout == b->timeout &&
  245. a->extensions == b->extensions;
  246. }
  247. /* Plain variant */
  248. #include "ip_set_bitmap_gen.h"
  249. /* Create bitmap:ip,mac type of sets */
  250. static bool
  251. init_map_ipmac(struct ip_set *set, struct bitmap_ipmac *map,
  252. u32 first_ip, u32 last_ip, u32 elements)
  253. {
  254. map->members = bitmap_zalloc(elements, GFP_KERNEL | __GFP_NOWARN);
  255. if (!map->members)
  256. return false;
  257. map->first_ip = first_ip;
  258. map->last_ip = last_ip;
  259. map->elements = elements;
  260. set->timeout = IPSET_NO_TIMEOUT;
  261. map->set = set;
  262. set->data = map;
  263. set->family = NFPROTO_IPV4;
  264. return true;
  265. }
  266. static int
  267. bitmap_ipmac_create(struct net *net, struct ip_set *set, struct nlattr *tb[],
  268. u32 flags)
  269. {
  270. u32 first_ip = 0, last_ip = 0;
  271. u64 elements;
  272. struct bitmap_ipmac *map;
  273. int ret;
  274. if (unlikely(!tb[IPSET_ATTR_IP] ||
  275. !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT) ||
  276. !ip_set_optattr_netorder(tb, IPSET_ATTR_CADT_FLAGS)))
  277. return -IPSET_ERR_PROTOCOL;
  278. ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP], &first_ip);
  279. if (ret)
  280. return ret;
  281. if (tb[IPSET_ATTR_IP_TO]) {
  282. ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP_TO], &last_ip);
  283. if (ret)
  284. return ret;
  285. if (first_ip > last_ip)
  286. swap(first_ip, last_ip);
  287. } else if (tb[IPSET_ATTR_CIDR]) {
  288. u8 cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
  289. if (cidr >= HOST_MASK)
  290. return -IPSET_ERR_INVALID_CIDR;
  291. ip_set_mask_from_to(first_ip, last_ip, cidr);
  292. } else {
  293. return -IPSET_ERR_PROTOCOL;
  294. }
  295. elements = (u64)last_ip - first_ip + 1;
  296. if (elements > IPSET_BITMAP_MAX_RANGE + 1)
  297. return -IPSET_ERR_BITMAP_RANGE_SIZE;
  298. set->dsize = ip_set_elem_len(set, tb,
  299. sizeof(struct bitmap_ipmac_elem),
  300. __alignof__(struct bitmap_ipmac_elem));
  301. map = ip_set_alloc(sizeof(*map) + elements * set->dsize);
  302. if (!map)
  303. return -ENOMEM;
  304. map->memsize = BITS_TO_LONGS(elements) * sizeof(unsigned long);
  305. set->variant = &bitmap_ipmac;
  306. if (!init_map_ipmac(set, map, first_ip, last_ip, elements)) {
  307. ip_set_free(map);
  308. return -ENOMEM;
  309. }
  310. if (tb[IPSET_ATTR_TIMEOUT]) {
  311. set->timeout = ip_set_timeout_uget(tb[IPSET_ATTR_TIMEOUT]);
  312. bitmap_ipmac_gc_init(set, bitmap_ipmac_gc);
  313. }
  314. return 0;
  315. }
  316. static struct ip_set_type bitmap_ipmac_type = {
  317. .name = "bitmap:ip,mac",
  318. .protocol = IPSET_PROTOCOL,
  319. .features = IPSET_TYPE_IP | IPSET_TYPE_MAC,
  320. .dimension = IPSET_DIM_TWO,
  321. .family = NFPROTO_IPV4,
  322. .revision_min = IPSET_TYPE_REV_MIN,
  323. .revision_max = IPSET_TYPE_REV_MAX,
  324. .create = bitmap_ipmac_create,
  325. .create_policy = {
  326. [IPSET_ATTR_IP] = { .type = NLA_NESTED },
  327. [IPSET_ATTR_IP_TO] = { .type = NLA_NESTED },
  328. [IPSET_ATTR_CIDR] = { .type = NLA_U8 },
  329. [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
  330. [IPSET_ATTR_CADT_FLAGS] = { .type = NLA_U32 },
  331. },
  332. .adt_policy = {
  333. [IPSET_ATTR_IP] = { .type = NLA_NESTED },
  334. [IPSET_ATTR_ETHER] = { .type = NLA_BINARY,
  335. .len = ETH_ALEN },
  336. [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
  337. [IPSET_ATTR_LINENO] = { .type = NLA_U32 },
  338. [IPSET_ATTR_BYTES] = { .type = NLA_U64 },
  339. [IPSET_ATTR_PACKETS] = { .type = NLA_U64 },
  340. [IPSET_ATTR_COMMENT] = { .type = NLA_NUL_STRING,
  341. .len = IPSET_MAX_COMMENT_SIZE },
  342. [IPSET_ATTR_SKBMARK] = { .type = NLA_U64 },
  343. [IPSET_ATTR_SKBPRIO] = { .type = NLA_U32 },
  344. [IPSET_ATTR_SKBQUEUE] = { .type = NLA_U16 },
  345. },
  346. .me = THIS_MODULE,
  347. };
  348. static int __init
  349. bitmap_ipmac_init(void)
  350. {
  351. return ip_set_type_register(&bitmap_ipmac_type);
  352. }
  353. static void __exit
  354. bitmap_ipmac_fini(void)
  355. {
  356. rcu_barrier();
  357. ip_set_type_unregister(&bitmap_ipmac_type);
  358. }
  359. module_init(bitmap_ipmac_init);
  360. module_exit(bitmap_ipmac_fini);