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