ipvlan.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. /*
  3. * Copyright (c) 2014 Mahesh Bandewar <maheshb@google.com>
  4. */
  5. #ifndef __IPVLAN_H
  6. #define __IPVLAN_H
  7. #include <linux/kernel.h>
  8. #include <linux/types.h>
  9. #include <linux/module.h>
  10. #include <linux/init.h>
  11. #include <linux/rculist.h>
  12. #include <linux/notifier.h>
  13. #include <linux/netdevice.h>
  14. #include <linux/etherdevice.h>
  15. #include <linux/if_arp.h>
  16. #include <linux/if_link.h>
  17. #include <linux/if_vlan.h>
  18. #include <linux/ip.h>
  19. #include <linux/inetdevice.h>
  20. #include <linux/netfilter.h>
  21. #include <net/ip.h>
  22. #include <net/ip6_route.h>
  23. #include <net/netns/generic.h>
  24. #include <net/rtnetlink.h>
  25. #include <net/route.h>
  26. #include <net/addrconf.h>
  27. #include <net/l3mdev.h>
  28. #define IPVLAN_DRV "ipvlan"
  29. #define IPV_DRV_VER "0.1"
  30. #define IPVLAN_HASH_SIZE (1 << BITS_PER_BYTE)
  31. #define IPVLAN_HASH_MASK (IPVLAN_HASH_SIZE - 1)
  32. #define IPVLAN_MAC_FILTER_BITS 8
  33. #define IPVLAN_MAC_FILTER_SIZE (1 << IPVLAN_MAC_FILTER_BITS)
  34. #define IPVLAN_MAC_FILTER_MASK (IPVLAN_MAC_FILTER_SIZE - 1)
  35. #define IPVLAN_QBACKLOG_LIMIT 1000
  36. typedef enum {
  37. IPVL_IPV6 = 0,
  38. IPVL_ICMPV6,
  39. IPVL_IPV4,
  40. IPVL_ARP,
  41. } ipvl_hdr_type;
  42. struct ipvl_pcpu_stats {
  43. u64 rx_pkts;
  44. u64 rx_bytes;
  45. u64 rx_mcast;
  46. u64 tx_pkts;
  47. u64 tx_bytes;
  48. struct u64_stats_sync syncp;
  49. u32 rx_errs;
  50. u32 tx_drps;
  51. };
  52. struct ipvl_port;
  53. struct ipvl_dev {
  54. struct net_device *dev;
  55. struct list_head pnode;
  56. struct ipvl_port *port;
  57. struct net_device *phy_dev;
  58. struct list_head addrs;
  59. struct ipvl_pcpu_stats __percpu *pcpu_stats;
  60. DECLARE_BITMAP(mac_filters, IPVLAN_MAC_FILTER_SIZE);
  61. netdev_features_t sfeatures;
  62. u32 msg_enable;
  63. spinlock_t addrs_lock;
  64. };
  65. struct ipvl_addr {
  66. struct ipvl_dev *master; /* Back pointer to master */
  67. union {
  68. struct in6_addr ip6; /* IPv6 address on logical interface */
  69. struct in_addr ip4; /* IPv4 address on logical interface */
  70. } ipu;
  71. #define ip6addr ipu.ip6
  72. #define ip4addr ipu.ip4
  73. struct hlist_node hlnode; /* Hash-table linkage */
  74. struct list_head anode; /* logical-interface linkage */
  75. ipvl_hdr_type atype;
  76. struct rcu_head rcu;
  77. };
  78. struct ipvl_port {
  79. struct net_device *dev;
  80. possible_net_t pnet;
  81. struct hlist_head hlhead[IPVLAN_HASH_SIZE];
  82. struct list_head ipvlans;
  83. u16 mode;
  84. u16 flags;
  85. u16 dev_id_start;
  86. struct work_struct wq;
  87. struct sk_buff_head backlog;
  88. int count;
  89. struct ida ida;
  90. };
  91. struct ipvl_skb_cb {
  92. bool tx_pkt;
  93. };
  94. #define IPVL_SKB_CB(_skb) ((struct ipvl_skb_cb *)&((_skb)->cb[0]))
  95. static inline struct ipvl_port *ipvlan_port_get_rcu(const struct net_device *d)
  96. {
  97. return rcu_dereference(d->rx_handler_data);
  98. }
  99. static inline struct ipvl_port *ipvlan_port_get_rcu_bh(const struct net_device *d)
  100. {
  101. return rcu_dereference_bh(d->rx_handler_data);
  102. }
  103. static inline struct ipvl_port *ipvlan_port_get_rtnl(const struct net_device *d)
  104. {
  105. return rtnl_dereference(d->rx_handler_data);
  106. }
  107. static inline bool ipvlan_is_private(const struct ipvl_port *port)
  108. {
  109. return !!(port->flags & IPVLAN_F_PRIVATE);
  110. }
  111. static inline void ipvlan_mark_private(struct ipvl_port *port)
  112. {
  113. port->flags |= IPVLAN_F_PRIVATE;
  114. }
  115. static inline void ipvlan_clear_private(struct ipvl_port *port)
  116. {
  117. port->flags &= ~IPVLAN_F_PRIVATE;
  118. }
  119. static inline bool ipvlan_is_vepa(const struct ipvl_port *port)
  120. {
  121. return !!(port->flags & IPVLAN_F_VEPA);
  122. }
  123. static inline void ipvlan_mark_vepa(struct ipvl_port *port)
  124. {
  125. port->flags |= IPVLAN_F_VEPA;
  126. }
  127. static inline void ipvlan_clear_vepa(struct ipvl_port *port)
  128. {
  129. port->flags &= ~IPVLAN_F_VEPA;
  130. }
  131. void ipvlan_init_secret(void);
  132. unsigned int ipvlan_mac_hash(const unsigned char *addr);
  133. rx_handler_result_t ipvlan_handle_frame(struct sk_buff **pskb);
  134. void ipvlan_process_multicast(struct work_struct *work);
  135. int ipvlan_queue_xmit(struct sk_buff *skb, struct net_device *dev);
  136. void ipvlan_ht_addr_add(struct ipvl_dev *ipvlan, struct ipvl_addr *addr);
  137. struct ipvl_addr *ipvlan_find_addr(const struct ipvl_dev *ipvlan,
  138. const void *iaddr, bool is_v6);
  139. bool ipvlan_addr_busy(struct ipvl_port *port, void *iaddr, bool is_v6);
  140. void ipvlan_ht_addr_del(struct ipvl_addr *addr);
  141. struct ipvl_addr *ipvlan_addr_lookup(struct ipvl_port *port, void *lyr3h,
  142. int addr_type, bool use_dest);
  143. void *ipvlan_get_L3_hdr(struct ipvl_port *port, struct sk_buff *skb, int *type);
  144. void ipvlan_count_rx(const struct ipvl_dev *ipvlan,
  145. unsigned int len, bool success, bool mcast);
  146. int ipvlan_link_new(struct net *src_net, struct net_device *dev,
  147. struct nlattr *tb[], struct nlattr *data[],
  148. struct netlink_ext_ack *extack);
  149. void ipvlan_link_delete(struct net_device *dev, struct list_head *head);
  150. void ipvlan_link_setup(struct net_device *dev);
  151. int ipvlan_link_register(struct rtnl_link_ops *ops);
  152. #ifdef CONFIG_IPVLAN_L3S
  153. int ipvlan_l3s_register(struct ipvl_port *port);
  154. void ipvlan_l3s_unregister(struct ipvl_port *port);
  155. void ipvlan_migrate_l3s_hook(struct net *oldnet, struct net *newnet);
  156. int ipvlan_l3s_init(void);
  157. void ipvlan_l3s_cleanup(void);
  158. #else
  159. static inline int ipvlan_l3s_register(struct ipvl_port *port)
  160. {
  161. return -ENOTSUPP;
  162. }
  163. static inline void ipvlan_l3s_unregister(struct ipvl_port *port)
  164. {
  165. }
  166. static inline void ipvlan_migrate_l3s_hook(struct net *oldnet,
  167. struct net *newnet)
  168. {
  169. }
  170. static inline int ipvlan_l3s_init(void)
  171. {
  172. return 0;
  173. }
  174. static inline void ipvlan_l3s_cleanup(void)
  175. {
  176. }
  177. #endif /* CONFIG_IPVLAN_L3S */
  178. static inline bool netif_is_ipvlan_port(const struct net_device *dev)
  179. {
  180. return rcu_access_pointer(dev->rx_handler) == ipvlan_handle_frame;
  181. }
  182. #endif /* __IPVLAN_H */