ip_set_hash_mac.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /* Copyright (C) 2014 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:mac type */
  8. #include <linux/jhash.h>
  9. #include <linux/module.h>
  10. #include <linux/etherdevice.h>
  11. #include <linux/skbuff.h>
  12. #include <linux/errno.h>
  13. #include <linux/if_ether.h>
  14. #include <net/netlink.h>
  15. #include <linux/netfilter.h>
  16. #include <linux/netfilter/ipset/ip_set.h>
  17. #include <linux/netfilter/ipset/ip_set_hash.h>
  18. #define IPSET_TYPE_REV_MIN 0
  19. #define IPSET_TYPE_REV_MAX 0
  20. MODULE_LICENSE("GPL");
  21. MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
  22. IP_SET_MODULE_DESC("hash:mac", IPSET_TYPE_REV_MIN, IPSET_TYPE_REV_MAX);
  23. MODULE_ALIAS("ip_set_hash:mac");
  24. /* Type specific function prefix */
  25. #define HTYPE hash_mac
  26. /* Member elements */
  27. struct hash_mac4_elem {
  28. /* Zero valued IP addresses cannot be stored */
  29. union {
  30. unsigned char ether[ETH_ALEN];
  31. __be32 foo[2];
  32. };
  33. };
  34. /* Common functions */
  35. static inline bool
  36. hash_mac4_data_equal(const struct hash_mac4_elem *e1,
  37. const struct hash_mac4_elem *e2,
  38. u32 *multi)
  39. {
  40. return ether_addr_equal(e1->ether, e2->ether);
  41. }
  42. static inline bool
  43. hash_mac4_data_list(struct sk_buff *skb, const struct hash_mac4_elem *e)
  44. {
  45. if (nla_put(skb, IPSET_ATTR_ETHER, ETH_ALEN, e->ether))
  46. goto nla_put_failure;
  47. return false;
  48. nla_put_failure:
  49. return true;
  50. }
  51. static inline void
  52. hash_mac4_data_next(struct hash_mac4_elem *next,
  53. const struct hash_mac4_elem *e)
  54. {
  55. }
  56. #define MTYPE hash_mac4
  57. #define HOST_MASK 32
  58. #define IP_SET_EMIT_CREATE
  59. #define IP_SET_PROTO_UNDEF
  60. #include "ip_set_hash_gen.h"
  61. /* Zero valued element is not supported */
  62. static const unsigned char invalid_ether[ETH_ALEN] = { 0 };
  63. static int
  64. hash_mac4_kadt(struct ip_set *set, const struct sk_buff *skb,
  65. const struct xt_action_param *par,
  66. enum ipset_adt adt, struct ip_set_adt_opt *opt)
  67. {
  68. ipset_adtfn adtfn = set->variant->adt[adt];
  69. struct hash_mac4_elem e = { { .foo[0] = 0, .foo[1] = 0 } };
  70. struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set);
  71. /* MAC can be src only */
  72. if (!(opt->flags & IPSET_DIM_ONE_SRC))
  73. return 0;
  74. if (skb_mac_header(skb) < skb->head ||
  75. (skb_mac_header(skb) + ETH_HLEN) > skb->data)
  76. return -EINVAL;
  77. ether_addr_copy(e.ether, eth_hdr(skb)->h_source);
  78. if (memcmp(e.ether, invalid_ether, ETH_ALEN) == 0)
  79. return -EINVAL;
  80. return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags);
  81. }
  82. static int
  83. hash_mac4_uadt(struct ip_set *set, struct nlattr *tb[],
  84. enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
  85. {
  86. ipset_adtfn adtfn = set->variant->adt[adt];
  87. struct hash_mac4_elem e = { { .foo[0] = 0, .foo[1] = 0 } };
  88. struct ip_set_ext ext = IP_SET_INIT_UEXT(set);
  89. int ret;
  90. if (tb[IPSET_ATTR_LINENO])
  91. *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
  92. if (unlikely(!tb[IPSET_ATTR_ETHER]))
  93. return -IPSET_ERR_PROTOCOL;
  94. ret = ip_set_get_extensions(set, tb, &ext);
  95. if (ret)
  96. return ret;
  97. ether_addr_copy(e.ether, nla_data(tb[IPSET_ATTR_ETHER]));
  98. if (memcmp(e.ether, invalid_ether, ETH_ALEN) == 0)
  99. return -IPSET_ERR_HASH_ELEM;
  100. return adtfn(set, &e, &ext, &ext, flags);
  101. }
  102. static struct ip_set_type hash_mac_type __read_mostly = {
  103. .name = "hash:mac",
  104. .protocol = IPSET_PROTOCOL,
  105. .features = IPSET_TYPE_MAC,
  106. .dimension = IPSET_DIM_ONE,
  107. .family = NFPROTO_UNSPEC,
  108. .revision_min = IPSET_TYPE_REV_MIN,
  109. .revision_max = IPSET_TYPE_REV_MAX,
  110. .create = hash_mac_create,
  111. .create_policy = {
  112. [IPSET_ATTR_HASHSIZE] = { .type = NLA_U32 },
  113. [IPSET_ATTR_MAXELEM] = { .type = NLA_U32 },
  114. [IPSET_ATTR_PROBES] = { .type = NLA_U8 },
  115. [IPSET_ATTR_RESIZE] = { .type = NLA_U8 },
  116. [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
  117. [IPSET_ATTR_CADT_FLAGS] = { .type = NLA_U32 },
  118. },
  119. .adt_policy = {
  120. [IPSET_ATTR_ETHER] = { .type = NLA_BINARY,
  121. .len = ETH_ALEN },
  122. [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
  123. [IPSET_ATTR_LINENO] = { .type = NLA_U32 },
  124. [IPSET_ATTR_BYTES] = { .type = NLA_U64 },
  125. [IPSET_ATTR_PACKETS] = { .type = NLA_U64 },
  126. [IPSET_ATTR_COMMENT] = { .type = NLA_NUL_STRING,
  127. .len = IPSET_MAX_COMMENT_SIZE },
  128. [IPSET_ATTR_SKBMARK] = { .type = NLA_U64 },
  129. [IPSET_ATTR_SKBPRIO] = { .type = NLA_U32 },
  130. [IPSET_ATTR_SKBQUEUE] = { .type = NLA_U16 },
  131. },
  132. .me = THIS_MODULE,
  133. };
  134. static int __init
  135. hash_mac_init(void)
  136. {
  137. return ip_set_type_register(&hash_mac_type);
  138. }
  139. static void __exit
  140. hash_mac_fini(void)
  141. {
  142. rcu_barrier();
  143. ip_set_type_unregister(&hash_mac_type);
  144. }
  145. module_init(hash_mac_init);
  146. module_exit(hash_mac_fini);