ip_set_hash_ipmac.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* Copyright (C) 2016 Tomasz Chilinski <tomasz.chilinski@chilan.com>
  3. */
  4. /* Kernel module implementing an IP set type: the hash:ip,mac type */
  5. #include <linux/jhash.h>
  6. #include <linux/module.h>
  7. #include <linux/ip.h>
  8. #include <linux/etherdevice.h>
  9. #include <linux/skbuff.h>
  10. #include <linux/errno.h>
  11. #include <linux/random.h>
  12. #include <linux/if_ether.h>
  13. #include <net/ip.h>
  14. #include <net/ipv6.h>
  15. #include <net/netlink.h>
  16. #include <net/tcp.h>
  17. #include <linux/netfilter.h>
  18. #include <linux/netfilter/ipset/pfxlen.h>
  19. #include <linux/netfilter/ipset/ip_set.h>
  20. #include <linux/netfilter/ipset/ip_set_hash.h>
  21. #define IPSET_TYPE_REV_MIN 0
  22. #define IPSET_TYPE_REV_MAX 0
  23. MODULE_LICENSE("GPL");
  24. MODULE_AUTHOR("Tomasz Chilinski <tomasz.chilinski@chilan.com>");
  25. IP_SET_MODULE_DESC("hash:ip,mac", IPSET_TYPE_REV_MIN, IPSET_TYPE_REV_MAX);
  26. MODULE_ALIAS("ip_set_hash:ip,mac");
  27. /* Type specific function prefix */
  28. #define HTYPE hash_ipmac
  29. /* IPv4 variant */
  30. /* Member elements */
  31. struct hash_ipmac4_elem {
  32. /* Zero valued IP addresses cannot be stored */
  33. __be32 ip;
  34. union {
  35. unsigned char ether[ETH_ALEN];
  36. __be32 foo[2];
  37. };
  38. };
  39. /* Common functions */
  40. static bool
  41. hash_ipmac4_data_equal(const struct hash_ipmac4_elem *e1,
  42. const struct hash_ipmac4_elem *e2,
  43. u32 *multi)
  44. {
  45. return e1->ip == e2->ip && ether_addr_equal(e1->ether, e2->ether);
  46. }
  47. static bool
  48. hash_ipmac4_data_list(struct sk_buff *skb, const struct hash_ipmac4_elem *e)
  49. {
  50. if (nla_put_ipaddr4(skb, IPSET_ATTR_IP, e->ip) ||
  51. nla_put(skb, IPSET_ATTR_ETHER, ETH_ALEN, e->ether))
  52. goto nla_put_failure;
  53. return false;
  54. nla_put_failure:
  55. return true;
  56. }
  57. static void
  58. hash_ipmac4_data_next(struct hash_ipmac4_elem *next,
  59. const struct hash_ipmac4_elem *e)
  60. {
  61. next->ip = e->ip;
  62. }
  63. #define MTYPE hash_ipmac4
  64. #define PF 4
  65. #define HOST_MASK 32
  66. #define HKEY_DATALEN sizeof(struct hash_ipmac4_elem)
  67. #include "ip_set_hash_gen.h"
  68. static int
  69. hash_ipmac4_kadt(struct ip_set *set, const struct sk_buff *skb,
  70. const struct xt_action_param *par,
  71. enum ipset_adt adt, struct ip_set_adt_opt *opt)
  72. {
  73. ipset_adtfn adtfn = set->variant->adt[adt];
  74. struct hash_ipmac4_elem e = { .ip = 0, { .foo[0] = 0, .foo[1] = 0 } };
  75. struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set);
  76. if (skb_mac_header(skb) < skb->head ||
  77. (skb_mac_header(skb) + ETH_HLEN) > skb->data)
  78. return -EINVAL;
  79. if (opt->flags & IPSET_DIM_TWO_SRC)
  80. ether_addr_copy(e.ether, eth_hdr(skb)->h_source);
  81. else
  82. ether_addr_copy(e.ether, eth_hdr(skb)->h_dest);
  83. if (is_zero_ether_addr(e.ether))
  84. return -EINVAL;
  85. ip4addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &e.ip);
  86. return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags);
  87. }
  88. static int
  89. hash_ipmac4_uadt(struct ip_set *set, struct nlattr *tb[],
  90. enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
  91. {
  92. ipset_adtfn adtfn = set->variant->adt[adt];
  93. struct hash_ipmac4_elem e = { .ip = 0, { .foo[0] = 0, .foo[1] = 0 } };
  94. struct ip_set_ext ext = IP_SET_INIT_UEXT(set);
  95. int ret;
  96. if (unlikely(!tb[IPSET_ATTR_IP] ||
  97. !tb[IPSET_ATTR_ETHER] ||
  98. nla_len(tb[IPSET_ATTR_ETHER]) != ETH_ALEN ||
  99. !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT) ||
  100. !ip_set_optattr_netorder(tb, IPSET_ATTR_PACKETS) ||
  101. !ip_set_optattr_netorder(tb, IPSET_ATTR_BYTES) ||
  102. !ip_set_optattr_netorder(tb, IPSET_ATTR_SKBMARK) ||
  103. !ip_set_optattr_netorder(tb, IPSET_ATTR_SKBPRIO) ||
  104. !ip_set_optattr_netorder(tb, IPSET_ATTR_SKBQUEUE)))
  105. return -IPSET_ERR_PROTOCOL;
  106. if (tb[IPSET_ATTR_LINENO])
  107. *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
  108. ret = ip_set_get_ipaddr4(tb[IPSET_ATTR_IP], &e.ip) ||
  109. ip_set_get_extensions(set, tb, &ext);
  110. if (ret)
  111. return ret;
  112. memcpy(e.ether, nla_data(tb[IPSET_ATTR_ETHER]), ETH_ALEN);
  113. if (is_zero_ether_addr(e.ether))
  114. return -IPSET_ERR_HASH_ELEM;
  115. return adtfn(set, &e, &ext, &ext, flags);
  116. }
  117. /* IPv6 variant */
  118. /* Member elements */
  119. struct hash_ipmac6_elem {
  120. /* Zero valued IP addresses cannot be stored */
  121. union nf_inet_addr ip;
  122. union {
  123. unsigned char ether[ETH_ALEN];
  124. __be32 foo[2];
  125. };
  126. };
  127. /* Common functions */
  128. static bool
  129. hash_ipmac6_data_equal(const struct hash_ipmac6_elem *e1,
  130. const struct hash_ipmac6_elem *e2,
  131. u32 *multi)
  132. {
  133. return ipv6_addr_equal(&e1->ip.in6, &e2->ip.in6) &&
  134. ether_addr_equal(e1->ether, e2->ether);
  135. }
  136. static bool
  137. hash_ipmac6_data_list(struct sk_buff *skb, const struct hash_ipmac6_elem *e)
  138. {
  139. if (nla_put_ipaddr6(skb, IPSET_ATTR_IP, &e->ip.in6) ||
  140. nla_put(skb, IPSET_ATTR_ETHER, ETH_ALEN, e->ether))
  141. goto nla_put_failure;
  142. return false;
  143. nla_put_failure:
  144. return true;
  145. }
  146. static void
  147. hash_ipmac6_data_next(struct hash_ipmac6_elem *next,
  148. const struct hash_ipmac6_elem *e)
  149. {
  150. }
  151. #undef MTYPE
  152. #undef PF
  153. #undef HOST_MASK
  154. #undef HKEY_DATALEN
  155. #define MTYPE hash_ipmac6
  156. #define PF 6
  157. #define HOST_MASK 128
  158. #define HKEY_DATALEN sizeof(struct hash_ipmac6_elem)
  159. #define IP_SET_EMIT_CREATE
  160. #include "ip_set_hash_gen.h"
  161. static int
  162. hash_ipmac6_kadt(struct ip_set *set, const struct sk_buff *skb,
  163. const struct xt_action_param *par,
  164. enum ipset_adt adt, struct ip_set_adt_opt *opt)
  165. {
  166. ipset_adtfn adtfn = set->variant->adt[adt];
  167. struct hash_ipmac6_elem e = {
  168. { .all = { 0 } },
  169. { .foo[0] = 0, .foo[1] = 0 }
  170. };
  171. struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set);
  172. if (skb_mac_header(skb) < skb->head ||
  173. (skb_mac_header(skb) + ETH_HLEN) > skb->data)
  174. return -EINVAL;
  175. if (opt->flags & IPSET_DIM_TWO_SRC)
  176. ether_addr_copy(e.ether, eth_hdr(skb)->h_source);
  177. else
  178. ether_addr_copy(e.ether, eth_hdr(skb)->h_dest);
  179. if (is_zero_ether_addr(e.ether))
  180. return -EINVAL;
  181. ip6addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &e.ip.in6);
  182. return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags);
  183. }
  184. static int
  185. hash_ipmac6_uadt(struct ip_set *set, struct nlattr *tb[],
  186. enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
  187. {
  188. ipset_adtfn adtfn = set->variant->adt[adt];
  189. struct hash_ipmac6_elem e = {
  190. { .all = { 0 } },
  191. { .foo[0] = 0, .foo[1] = 0 }
  192. };
  193. struct ip_set_ext ext = IP_SET_INIT_UEXT(set);
  194. int ret;
  195. if (unlikely(!tb[IPSET_ATTR_IP] ||
  196. !tb[IPSET_ATTR_ETHER] ||
  197. nla_len(tb[IPSET_ATTR_ETHER]) != ETH_ALEN ||
  198. !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT) ||
  199. !ip_set_optattr_netorder(tb, IPSET_ATTR_PACKETS) ||
  200. !ip_set_optattr_netorder(tb, IPSET_ATTR_BYTES) ||
  201. !ip_set_optattr_netorder(tb, IPSET_ATTR_SKBMARK) ||
  202. !ip_set_optattr_netorder(tb, IPSET_ATTR_SKBPRIO) ||
  203. !ip_set_optattr_netorder(tb, IPSET_ATTR_SKBQUEUE)))
  204. return -IPSET_ERR_PROTOCOL;
  205. if (tb[IPSET_ATTR_LINENO])
  206. *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
  207. ret = ip_set_get_ipaddr6(tb[IPSET_ATTR_IP], &e.ip) ||
  208. ip_set_get_extensions(set, tb, &ext);
  209. if (ret)
  210. return ret;
  211. memcpy(e.ether, nla_data(tb[IPSET_ATTR_ETHER]), ETH_ALEN);
  212. if (is_zero_ether_addr(e.ether))
  213. return -IPSET_ERR_HASH_ELEM;
  214. return adtfn(set, &e, &ext, &ext, flags);
  215. }
  216. static struct ip_set_type hash_ipmac_type __read_mostly = {
  217. .name = "hash:ip,mac",
  218. .protocol = IPSET_PROTOCOL,
  219. .features = IPSET_TYPE_IP | IPSET_TYPE_MAC,
  220. .dimension = IPSET_DIM_TWO,
  221. .family = NFPROTO_UNSPEC,
  222. .revision_min = IPSET_TYPE_REV_MIN,
  223. .revision_max = IPSET_TYPE_REV_MAX,
  224. .create = hash_ipmac_create,
  225. .create_policy = {
  226. [IPSET_ATTR_HASHSIZE] = { .type = NLA_U32 },
  227. [IPSET_ATTR_MAXELEM] = { .type = NLA_U32 },
  228. [IPSET_ATTR_PROBES] = { .type = NLA_U8 },
  229. [IPSET_ATTR_RESIZE] = { .type = NLA_U8 },
  230. [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
  231. [IPSET_ATTR_CADT_FLAGS] = { .type = NLA_U32 },
  232. },
  233. .adt_policy = {
  234. [IPSET_ATTR_IP] = { .type = NLA_NESTED },
  235. [IPSET_ATTR_ETHER] = { .type = NLA_BINARY,
  236. .len = ETH_ALEN },
  237. [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
  238. [IPSET_ATTR_LINENO] = { .type = NLA_U32 },
  239. [IPSET_ATTR_BYTES] = { .type = NLA_U64 },
  240. [IPSET_ATTR_PACKETS] = { .type = NLA_U64 },
  241. [IPSET_ATTR_COMMENT] = { .type = NLA_NUL_STRING },
  242. [IPSET_ATTR_SKBMARK] = { .type = NLA_U64 },
  243. [IPSET_ATTR_SKBPRIO] = { .type = NLA_U32 },
  244. [IPSET_ATTR_SKBQUEUE] = { .type = NLA_U16 },
  245. },
  246. .me = THIS_MODULE,
  247. };
  248. static int __init
  249. hash_ipmac_init(void)
  250. {
  251. return ip_set_type_register(&hash_ipmac_type);
  252. }
  253. static void __exit
  254. hash_ipmac_fini(void)
  255. {
  256. ip_set_type_unregister(&hash_ipmac_type);
  257. }
  258. module_init(hash_ipmac_init);
  259. module_exit(hash_ipmac_fini);