ipvlan.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. /*
  3. * Copyright (c) 2014 Mahesh Bandewar <[email protected]>
  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_stats_t rx_pkts;
  44. u64_stats_t rx_bytes;
  45. u64_stats_t rx_mcast;
  46. u64_stats_t tx_pkts;
  47. u64_stats_t 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. netdevice_tracker dev_tracker;
  91. };
  92. struct ipvl_skb_cb {
  93. bool tx_pkt;
  94. };
  95. #define IPVL_SKB_CB(_skb) ((struct ipvl_skb_cb *)&((_skb)->cb[0]))
  96. static inline struct ipvl_port *ipvlan_port_get_rcu(const struct net_device *d)
  97. {
  98. return rcu_dereference(d->rx_handler_data);
  99. }
  100. static inline struct ipvl_port *ipvlan_port_get_rcu_bh(const struct net_device *d)
  101. {
  102. return rcu_dereference_bh(d->rx_handler_data);
  103. }
  104. static inline struct ipvl_port *ipvlan_port_get_rtnl(const struct net_device *d)
  105. {
  106. return rtnl_dereference(d->rx_handler_data);
  107. }
  108. static inline bool ipvlan_is_private(const struct ipvl_port *port)
  109. {
  110. return !!(port->flags & IPVLAN_F_PRIVATE);
  111. }
  112. static inline void ipvlan_mark_private(struct ipvl_port *port)
  113. {
  114. port->flags |= IPVLAN_F_PRIVATE;
  115. }
  116. static inline void ipvlan_clear_private(struct ipvl_port *port)
  117. {
  118. port->flags &= ~IPVLAN_F_PRIVATE;
  119. }
  120. static inline bool ipvlan_is_vepa(const struct ipvl_port *port)
  121. {
  122. return !!(port->flags & IPVLAN_F_VEPA);
  123. }
  124. static inline void ipvlan_mark_vepa(struct ipvl_port *port)
  125. {
  126. port->flags |= IPVLAN_F_VEPA;
  127. }
  128. static inline void ipvlan_clear_vepa(struct ipvl_port *port)
  129. {
  130. port->flags &= ~IPVLAN_F_VEPA;
  131. }
  132. void ipvlan_init_secret(void);
  133. unsigned int ipvlan_mac_hash(const unsigned char *addr);
  134. rx_handler_result_t ipvlan_handle_frame(struct sk_buff **pskb);
  135. void ipvlan_process_multicast(struct work_struct *work);
  136. int ipvlan_queue_xmit(struct sk_buff *skb, struct net_device *dev);
  137. void ipvlan_ht_addr_add(struct ipvl_dev *ipvlan, struct ipvl_addr *addr);
  138. struct ipvl_addr *ipvlan_find_addr(const struct ipvl_dev *ipvlan,
  139. const void *iaddr, bool is_v6);
  140. bool ipvlan_addr_busy(struct ipvl_port *port, void *iaddr, bool is_v6);
  141. void ipvlan_ht_addr_del(struct ipvl_addr *addr);
  142. struct ipvl_addr *ipvlan_addr_lookup(struct ipvl_port *port, void *lyr3h,
  143. int addr_type, bool use_dest);
  144. void *ipvlan_get_L3_hdr(struct ipvl_port *port, struct sk_buff *skb, int *type);
  145. void ipvlan_count_rx(const struct ipvl_dev *ipvlan,
  146. unsigned int len, bool success, bool mcast);
  147. int ipvlan_link_new(struct net *src_net, struct net_device *dev,
  148. struct nlattr *tb[], struct nlattr *data[],
  149. struct netlink_ext_ack *extack);
  150. void ipvlan_link_delete(struct net_device *dev, struct list_head *head);
  151. void ipvlan_link_setup(struct net_device *dev);
  152. int ipvlan_link_register(struct rtnl_link_ops *ops);
  153. #ifdef CONFIG_IPVLAN_L3S
  154. int ipvlan_l3s_register(struct ipvl_port *port);
  155. void ipvlan_l3s_unregister(struct ipvl_port *port);
  156. void ipvlan_migrate_l3s_hook(struct net *oldnet, struct net *newnet);
  157. int ipvlan_l3s_init(void);
  158. void ipvlan_l3s_cleanup(void);
  159. #else
  160. static inline int ipvlan_l3s_register(struct ipvl_port *port)
  161. {
  162. return -ENOTSUPP;
  163. }
  164. static inline void ipvlan_l3s_unregister(struct ipvl_port *port)
  165. {
  166. }
  167. static inline void ipvlan_migrate_l3s_hook(struct net *oldnet,
  168. struct net *newnet)
  169. {
  170. }
  171. static inline int ipvlan_l3s_init(void)
  172. {
  173. return 0;
  174. }
  175. static inline void ipvlan_l3s_cleanup(void)
  176. {
  177. }
  178. #endif /* CONFIG_IPVLAN_L3S */
  179. static inline bool netif_is_ipvlan_port(const struct net_device *dev)
  180. {
  181. return rcu_access_pointer(dev->rx_handler) == ipvlan_handle_frame;
  182. }
  183. #endif /* __IPVLAN_H */