vxlan_private.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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. static inline struct vxlan_rdst *first_remote_rcu(struct vxlan_fdb *fdb)
  50. {
  51. if (rcu_access_pointer(fdb->nh))
  52. return NULL;
  53. return list_entry_rcu(fdb->remotes.next, struct vxlan_rdst, list);
  54. }
  55. static inline struct vxlan_rdst *first_remote_rtnl(struct vxlan_fdb *fdb)
  56. {
  57. if (rcu_access_pointer(fdb->nh))
  58. return NULL;
  59. return list_first_entry(&fdb->remotes, struct vxlan_rdst, list);
  60. }
  61. #if IS_ENABLED(CONFIG_IPV6)
  62. static inline
  63. bool vxlan_addr_equal(const union vxlan_addr *a, const union vxlan_addr *b)
  64. {
  65. if (a->sa.sa_family != b->sa.sa_family)
  66. return false;
  67. if (a->sa.sa_family == AF_INET6)
  68. return ipv6_addr_equal(&a->sin6.sin6_addr, &b->sin6.sin6_addr);
  69. else
  70. return a->sin.sin_addr.s_addr == b->sin.sin_addr.s_addr;
  71. }
  72. static inline int vxlan_nla_get_addr(union vxlan_addr *ip,
  73. const struct nlattr *nla)
  74. {
  75. if (nla_len(nla) >= sizeof(struct in6_addr)) {
  76. ip->sin6.sin6_addr = nla_get_in6_addr(nla);
  77. ip->sa.sa_family = AF_INET6;
  78. return 0;
  79. } else if (nla_len(nla) >= sizeof(__be32)) {
  80. ip->sin.sin_addr.s_addr = nla_get_in_addr(nla);
  81. ip->sa.sa_family = AF_INET;
  82. return 0;
  83. } else {
  84. return -EAFNOSUPPORT;
  85. }
  86. }
  87. static inline int vxlan_nla_put_addr(struct sk_buff *skb, int attr,
  88. const union vxlan_addr *ip)
  89. {
  90. if (ip->sa.sa_family == AF_INET6)
  91. return nla_put_in6_addr(skb, attr, &ip->sin6.sin6_addr);
  92. else
  93. return nla_put_in_addr(skb, attr, ip->sin.sin_addr.s_addr);
  94. }
  95. static inline bool vxlan_addr_is_multicast(const union vxlan_addr *ip)
  96. {
  97. if (ip->sa.sa_family == AF_INET6)
  98. return ipv6_addr_is_multicast(&ip->sin6.sin6_addr);
  99. else
  100. return ipv4_is_multicast(ip->sin.sin_addr.s_addr);
  101. }
  102. #else /* !CONFIG_IPV6 */
  103. static inline
  104. bool vxlan_addr_equal(const union vxlan_addr *a, const union vxlan_addr *b)
  105. {
  106. return a->sin.sin_addr.s_addr == b->sin.sin_addr.s_addr;
  107. }
  108. static inline int vxlan_nla_get_addr(union vxlan_addr *ip,
  109. const struct nlattr *nla)
  110. {
  111. if (nla_len(nla) >= sizeof(struct in6_addr)) {
  112. return -EAFNOSUPPORT;
  113. } else if (nla_len(nla) >= sizeof(__be32)) {
  114. ip->sin.sin_addr.s_addr = nla_get_in_addr(nla);
  115. ip->sa.sa_family = AF_INET;
  116. return 0;
  117. } else {
  118. return -EAFNOSUPPORT;
  119. }
  120. }
  121. static inline int vxlan_nla_put_addr(struct sk_buff *skb, int attr,
  122. const union vxlan_addr *ip)
  123. {
  124. return nla_put_in_addr(skb, attr, ip->sin.sin_addr.s_addr);
  125. }
  126. static inline bool vxlan_addr_is_multicast(const union vxlan_addr *ip)
  127. {
  128. return ipv4_is_multicast(ip->sin.sin_addr.s_addr);
  129. }
  130. #endif
  131. static inline size_t vxlan_addr_size(const union vxlan_addr *ip)
  132. {
  133. if (ip->sa.sa_family == AF_INET6)
  134. return sizeof(struct in6_addr);
  135. else
  136. return sizeof(__be32);
  137. }
  138. static inline struct vxlan_vni_node *
  139. vxlan_vnifilter_lookup(struct vxlan_dev *vxlan, __be32 vni)
  140. {
  141. struct vxlan_vni_group *vg;
  142. vg = rcu_dereference_rtnl(vxlan->vnigrp);
  143. if (!vg)
  144. return NULL;
  145. return rhashtable_lookup_fast(&vg->vni_hash, &vni,
  146. vxlan_vni_rht_params);
  147. }
  148. /* vxlan_core.c */
  149. int vxlan_fdb_create(struct vxlan_dev *vxlan,
  150. const u8 *mac, union vxlan_addr *ip,
  151. __u16 state, __be16 port, __be32 src_vni,
  152. __be32 vni, __u32 ifindex, __u16 ndm_flags,
  153. u32 nhid, struct vxlan_fdb **fdb,
  154. struct netlink_ext_ack *extack);
  155. int __vxlan_fdb_delete(struct vxlan_dev *vxlan,
  156. const unsigned char *addr, union vxlan_addr ip,
  157. __be16 port, __be32 src_vni, __be32 vni,
  158. u32 ifindex, bool swdev_notify);
  159. u32 eth_vni_hash(const unsigned char *addr, __be32 vni);
  160. u32 fdb_head_index(struct vxlan_dev *vxlan, const u8 *mac, __be32 vni);
  161. int vxlan_fdb_update(struct vxlan_dev *vxlan,
  162. const u8 *mac, union vxlan_addr *ip,
  163. __u16 state, __u16 flags,
  164. __be16 port, __be32 src_vni, __be32 vni,
  165. __u32 ifindex, __u16 ndm_flags, u32 nhid,
  166. bool swdev_notify, struct netlink_ext_ack *extack);
  167. void vxlan_xmit_one(struct sk_buff *skb, struct net_device *dev,
  168. __be32 default_vni, struct vxlan_rdst *rdst, bool did_rsc);
  169. int vxlan_vni_in_use(struct net *src_net, struct vxlan_dev *vxlan,
  170. struct vxlan_config *conf, __be32 vni);
  171. /* vxlan_vnifilter.c */
  172. int vxlan_vnigroup_init(struct vxlan_dev *vxlan);
  173. void vxlan_vnigroup_uninit(struct vxlan_dev *vxlan);
  174. int vxlan_vnifilter_init(void);
  175. void vxlan_vnifilter_uninit(void);
  176. void vxlan_vnifilter_count(struct vxlan_dev *vxlan, __be32 vni,
  177. struct vxlan_vni_node *vninode,
  178. int type, unsigned int len);
  179. void vxlan_vs_add_vnigrp(struct vxlan_dev *vxlan,
  180. struct vxlan_sock *vs,
  181. bool ipv6);
  182. void vxlan_vs_del_vnigrp(struct vxlan_dev *vxlan);
  183. int vxlan_vnilist_update_group(struct vxlan_dev *vxlan,
  184. union vxlan_addr *old_remote_ip,
  185. union vxlan_addr *new_remote_ip,
  186. struct netlink_ext_ack *extack);
  187. /* vxlan_multicast.c */
  188. int vxlan_multicast_join(struct vxlan_dev *vxlan);
  189. int vxlan_multicast_leave(struct vxlan_dev *vxlan);
  190. bool vxlan_group_used(struct vxlan_net *vn, struct vxlan_dev *dev,
  191. __be32 vni, union vxlan_addr *rip, int rifindex);
  192. int vxlan_igmp_join(struct vxlan_dev *vxlan, union vxlan_addr *rip,
  193. int rifindex);
  194. int vxlan_igmp_leave(struct vxlan_dev *vxlan, union vxlan_addr *rip,
  195. int rifindex);
  196. /* vxlan_mdb.c */
  197. int vxlan_mdb_dump(struct net_device *dev, struct sk_buff *skb,
  198. struct netlink_callback *cb);
  199. int vxlan_mdb_add(struct net_device *dev, struct nlattr *tb[], u16 nlmsg_flags,
  200. struct netlink_ext_ack *extack);
  201. int vxlan_mdb_del(struct net_device *dev, struct nlattr *tb[],
  202. struct netlink_ext_ack *extack);
  203. int vxlan_mdb_del_bulk(struct net_device *dev, struct nlattr *tb[],
  204. struct netlink_ext_ack *extack);
  205. int vxlan_mdb_get(struct net_device *dev, struct nlattr *tb[], u32 portid,
  206. u32 seq, struct netlink_ext_ack *extack);
  207. struct vxlan_mdb_entry *vxlan_mdb_entry_skb_get(struct vxlan_dev *vxlan,
  208. struct sk_buff *skb,
  209. __be32 src_vni);
  210. netdev_tx_t vxlan_mdb_xmit(struct vxlan_dev *vxlan,
  211. const struct vxlan_mdb_entry *mdb_entry,
  212. struct sk_buff *skb);
  213. int vxlan_mdb_init(struct vxlan_dev *vxlan);
  214. void vxlan_mdb_fini(struct vxlan_dev *vxlan);
  215. #endif