vxlan_private.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Vxlan private header file
  4. *
  5. */
  6. #ifndef _VXLAN_PRIVATE_H
  7. #define _VXLAN_PRIVATE_H
  8. #include <linux/rhashtable.h>
  9. extern unsigned int vxlan_net_id;
  10. extern const u8 all_zeros_mac[ETH_ALEN + 2];
  11. extern const struct rhashtable_params vxlan_vni_rht_params;
  12. #define PORT_HASH_BITS 8
  13. #define PORT_HASH_SIZE (1 << PORT_HASH_BITS)
  14. /* per-network namespace private data for this module */
  15. struct vxlan_net {
  16. struct list_head vxlan_list;
  17. struct hlist_head sock_list[PORT_HASH_SIZE];
  18. spinlock_t sock_lock;
  19. struct notifier_block nexthop_notifier_block;
  20. };
  21. /* Forwarding table entry */
  22. struct vxlan_fdb {
  23. struct hlist_node hlist; /* linked list of entries */
  24. struct rcu_head rcu;
  25. unsigned long updated; /* jiffies */
  26. unsigned long used;
  27. struct list_head remotes;
  28. u8 eth_addr[ETH_ALEN];
  29. u16 state; /* see ndm_state */
  30. __be32 vni;
  31. u16 flags; /* see ndm_flags and below */
  32. struct list_head nh_list;
  33. struct nexthop __rcu *nh;
  34. struct vxlan_dev __rcu *vdev;
  35. };
  36. #define NTF_VXLAN_ADDED_BY_USER 0x100
  37. /* Virtual Network hash table head */
  38. static inline struct hlist_head *vni_head(struct vxlan_sock *vs, __be32 vni)
  39. {
  40. return &vs->vni_list[hash_32((__force u32)vni, VNI_HASH_BITS)];
  41. }
  42. /* Socket hash table head */
  43. static inline struct hlist_head *vs_head(struct net *net, __be16 port)
  44. {
  45. struct vxlan_net *vn = net_generic(net, vxlan_net_id);
  46. return &vn->sock_list[hash_32(ntohs(port), PORT_HASH_BITS)];
  47. }
  48. /* First remote destination for a forwarding entry.
  49. * Guaranteed to be non-NULL because remotes are never deleted.
  50. */
  51. static inline struct vxlan_rdst *first_remote_rcu(struct vxlan_fdb *fdb)
  52. {
  53. if (rcu_access_pointer(fdb->nh))
  54. return NULL;
  55. return list_entry_rcu(fdb->remotes.next, struct vxlan_rdst, list);
  56. }
  57. static inline struct vxlan_rdst *first_remote_rtnl(struct vxlan_fdb *fdb)
  58. {
  59. if (rcu_access_pointer(fdb->nh))
  60. return NULL;
  61. return list_first_entry(&fdb->remotes, struct vxlan_rdst, list);
  62. }
  63. #if IS_ENABLED(CONFIG_IPV6)
  64. static inline
  65. bool vxlan_addr_equal(const union vxlan_addr *a, const union vxlan_addr *b)
  66. {
  67. if (a->sa.sa_family != b->sa.sa_family)
  68. return false;
  69. if (a->sa.sa_family == AF_INET6)
  70. return ipv6_addr_equal(&a->sin6.sin6_addr, &b->sin6.sin6_addr);
  71. else
  72. return a->sin.sin_addr.s_addr == b->sin.sin_addr.s_addr;
  73. }
  74. #else /* !CONFIG_IPV6 */
  75. static inline
  76. bool vxlan_addr_equal(const union vxlan_addr *a, const union vxlan_addr *b)
  77. {
  78. return a->sin.sin_addr.s_addr == b->sin.sin_addr.s_addr;
  79. }
  80. #endif
  81. static inline struct vxlan_vni_node *
  82. vxlan_vnifilter_lookup(struct vxlan_dev *vxlan, __be32 vni)
  83. {
  84. struct vxlan_vni_group *vg;
  85. vg = rcu_dereference_rtnl(vxlan->vnigrp);
  86. if (!vg)
  87. return NULL;
  88. return rhashtable_lookup_fast(&vg->vni_hash, &vni,
  89. vxlan_vni_rht_params);
  90. }
  91. /* vxlan_core.c */
  92. int vxlan_fdb_create(struct vxlan_dev *vxlan,
  93. const u8 *mac, union vxlan_addr *ip,
  94. __u16 state, __be16 port, __be32 src_vni,
  95. __be32 vni, __u32 ifindex, __u16 ndm_flags,
  96. u32 nhid, struct vxlan_fdb **fdb,
  97. struct netlink_ext_ack *extack);
  98. int __vxlan_fdb_delete(struct vxlan_dev *vxlan,
  99. const unsigned char *addr, union vxlan_addr ip,
  100. __be16 port, __be32 src_vni, __be32 vni,
  101. u32 ifindex, bool swdev_notify);
  102. u32 eth_vni_hash(const unsigned char *addr, __be32 vni);
  103. u32 fdb_head_index(struct vxlan_dev *vxlan, const u8 *mac, __be32 vni);
  104. int vxlan_fdb_update(struct vxlan_dev *vxlan,
  105. const u8 *mac, union vxlan_addr *ip,
  106. __u16 state, __u16 flags,
  107. __be16 port, __be32 src_vni, __be32 vni,
  108. __u32 ifindex, __u16 ndm_flags, u32 nhid,
  109. bool swdev_notify, struct netlink_ext_ack *extack);
  110. int vxlan_vni_in_use(struct net *src_net, struct vxlan_dev *vxlan,
  111. struct vxlan_config *conf, __be32 vni);
  112. /* vxlan_vnifilter.c */
  113. int vxlan_vnigroup_init(struct vxlan_dev *vxlan);
  114. void vxlan_vnigroup_uninit(struct vxlan_dev *vxlan);
  115. void vxlan_vnifilter_init(void);
  116. void vxlan_vnifilter_uninit(void);
  117. void vxlan_vnifilter_count(struct vxlan_dev *vxlan, __be32 vni,
  118. struct vxlan_vni_node *vninode,
  119. int type, unsigned int len);
  120. void vxlan_vs_add_vnigrp(struct vxlan_dev *vxlan,
  121. struct vxlan_sock *vs,
  122. bool ipv6);
  123. void vxlan_vs_del_vnigrp(struct vxlan_dev *vxlan);
  124. int vxlan_vnilist_update_group(struct vxlan_dev *vxlan,
  125. union vxlan_addr *old_remote_ip,
  126. union vxlan_addr *new_remote_ip,
  127. struct netlink_ext_ack *extack);
  128. /* vxlan_multicast.c */
  129. int vxlan_multicast_join(struct vxlan_dev *vxlan);
  130. int vxlan_multicast_leave(struct vxlan_dev *vxlan);
  131. bool vxlan_group_used(struct vxlan_net *vn, struct vxlan_dev *dev,
  132. __be32 vni, union vxlan_addr *rip, int rifindex);
  133. int vxlan_igmp_join(struct vxlan_dev *vxlan, union vxlan_addr *rip,
  134. int rifindex);
  135. int vxlan_igmp_leave(struct vxlan_dev *vxlan, union vxlan_addr *rip,
  136. int rifindex);
  137. #endif