blob: 3837c897832eaf6c325f06f86f16edf18775956a [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001/* SPDX-License-Identifier: GPL-2.0-or-later */
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -08002/*
3 * Copyright (c) 2014 Mahesh Bandewar <maheshb@google.com>
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -08004 */
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>
Mahesh Bandewar4fbae7d2016-09-16 12:59:19 -070021#include <linux/netfilter.h>
Mahesh Bandewar265de6d2014-11-26 21:13:45 -080022#include <net/ip.h>
23#include <net/ip6_route.h>
Florian Westphal31338222017-04-20 18:08:15 +020024#include <net/netns/generic.h>
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -080025#include <net/rtnetlink.h>
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -080026#include <net/route.h>
27#include <net/addrconf.h>
Mahesh Bandewar4fbae7d2016-09-16 12:59:19 -070028#include <net/l3mdev.h>
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -080029
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
Mahesh Bandewarba35f852015-05-04 17:06:03 -070040#define IPVLAN_QBACKLOG_LIMIT 1000
41
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -080042typedef enum {
43 IPVL_IPV6 = 0,
44 IPVL_ICMPV6,
45 IPVL_IPV4,
46 IPVL_ARP,
47} ipvl_hdr_type;
48
49struct ipvl_pcpu_stats {
50 u64 rx_pkts;
51 u64 rx_bytes;
52 u64 rx_mcast;
53 u64 tx_pkts;
54 u64 tx_bytes;
55 struct u64_stats_sync syncp;
56 u32 rx_errs;
57 u32 tx_drps;
58};
59
60struct ipvl_port;
61
62struct 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;
Eric Dumazet6aa63952015-02-11 19:51:46 -080068 struct ipvl_pcpu_stats __percpu *pcpu_stats;
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -080069 DECLARE_BITMAP(mac_filters, IPVLAN_MAC_FILTER_SIZE);
70 netdev_features_t sfeatures;
71 u32 msg_enable;
Paolo Abeni82308192018-02-28 10:59:27 +010072 spinlock_t addrs_lock;
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -080073};
74
75struct 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 */
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -080085 ipvl_hdr_type atype;
Mahesh Bandewarab5b7012016-02-20 19:31:41 -080086 struct rcu_head rcu;
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -080087};
88
89struct ipvl_port {
90 struct net_device *dev;
Florian Westphal31338222017-04-20 18:08:15 +020091 possible_net_t pnet;
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -080092 struct hlist_head hlhead[IPVLAN_HASH_SIZE];
93 struct list_head ipvlans;
Mahesh Bandewarab5b7012016-02-20 19:31:41 -080094 u16 mode;
Mahesh Bandewara190d042017-10-26 15:09:21 -070095 u16 flags;
Mahesh Bandewarda36e132017-01-09 15:05:54 -080096 u16 dev_id_start;
Mahesh Bandewarba35f852015-05-04 17:06:03 -070097 struct work_struct wq;
98 struct sk_buff_head backlog;
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -080099 int count;
Mahesh Bandewar009146d2017-01-03 12:47:16 -0800100 struct ida ida;
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800101};
102
Mahesh Bandeware2525362016-12-21 17:30:16 -0800103struct ipvl_skb_cb {
104 bool tx_pkt;
105};
106#define IPVL_SKB_CB(_skb) ((struct ipvl_skb_cb *)&((_skb)->cb[0]))
107
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800108static inline struct ipvl_port *ipvlan_port_get_rcu(const struct net_device *d)
109{
110 return rcu_dereference(d->rx_handler_data);
111}
112
WANG Cong0fba37a2015-07-14 16:35:54 +0300113static inline struct ipvl_port *ipvlan_port_get_rcu_bh(const struct net_device *d)
114{
115 return rcu_dereference_bh(d->rx_handler_data);
116}
117
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800118static inline struct ipvl_port *ipvlan_port_get_rtnl(const struct net_device *d)
119{
120 return rtnl_dereference(d->rx_handler_data);
121}
122
Mahesh Bandewara190d042017-10-26 15:09:21 -0700123static inline bool ipvlan_is_private(const struct ipvl_port *port)
124{
125 return !!(port->flags & IPVLAN_F_PRIVATE);
126}
127
128static inline void ipvlan_mark_private(struct ipvl_port *port)
129{
130 port->flags |= IPVLAN_F_PRIVATE;
131}
132
133static inline void ipvlan_clear_private(struct ipvl_port *port)
134{
135 port->flags &= ~IPVLAN_F_PRIVATE;
136}
137
Mahesh Bandewarfe89aa62017-10-26 15:09:25 -0700138static inline bool ipvlan_is_vepa(const struct ipvl_port *port)
139{
140 return !!(port->flags & IPVLAN_F_VEPA);
141}
142
143static inline void ipvlan_mark_vepa(struct ipvl_port *port)
144{
145 port->flags |= IPVLAN_F_VEPA;
146}
147
148static inline void ipvlan_clear_vepa(struct ipvl_port *port)
149{
150 port->flags &= ~IPVLAN_F_VEPA;
151}
152
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800153void ipvlan_init_secret(void);
154unsigned int ipvlan_mac_hash(const unsigned char *addr);
155rx_handler_result_t ipvlan_handle_frame(struct sk_buff **pskb);
Mahesh Bandewarba35f852015-05-04 17:06:03 -0700156void ipvlan_process_multicast(struct work_struct *work);
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800157int ipvlan_queue_xmit(struct sk_buff *skb, struct net_device *dev);
158void ipvlan_ht_addr_add(struct ipvl_dev *ipvlan, struct ipvl_addr *addr);
Jiri Bence9997c22015-03-28 19:13:25 +0100159struct ipvl_addr *ipvlan_find_addr(const struct ipvl_dev *ipvlan,
160 const void *iaddr, bool is_v6);
161bool ipvlan_addr_busy(struct ipvl_port *port, void *iaddr, bool is_v6);
Konstantin Khlebnikov6640e672015-07-14 16:35:53 +0300162void ipvlan_ht_addr_del(struct ipvl_addr *addr);
Daniel Borkmannc675e062019-02-08 13:55:31 +0100163struct ipvl_addr *ipvlan_addr_lookup(struct ipvl_port *port, void *lyr3h,
164 int addr_type, bool use_dest);
165void *ipvlan_get_L3_hdr(struct ipvl_port *port, struct sk_buff *skb, int *type);
Sainath Grandhi235a9d82017-02-10 16:03:52 -0800166void ipvlan_count_rx(const struct ipvl_dev *ipvlan,
167 unsigned int len, bool success, bool mcast);
168int ipvlan_link_new(struct net *src_net, struct net_device *dev,
Matthias Schiffer7a3f4a12017-06-25 23:55:59 +0200169 struct nlattr *tb[], struct nlattr *data[],
170 struct netlink_ext_ack *extack);
Sainath Grandhi235a9d82017-02-10 16:03:52 -0800171void ipvlan_link_delete(struct net_device *dev, struct list_head *head);
172void ipvlan_link_setup(struct net_device *dev);
173int ipvlan_link_register(struct rtnl_link_ops *ops);
Daniel Borkmannc675e062019-02-08 13:55:31 +0100174#ifdef CONFIG_IPVLAN_L3S
175int ipvlan_l3s_register(struct ipvl_port *port);
176void ipvlan_l3s_unregister(struct ipvl_port *port);
177void ipvlan_migrate_l3s_hook(struct net *oldnet, struct net *newnet);
178int ipvlan_l3s_init(void);
179void ipvlan_l3s_cleanup(void);
180#else
181static inline int ipvlan_l3s_register(struct ipvl_port *port)
182{
183 return -ENOTSUPP;
184}
185
186static inline void ipvlan_l3s_unregister(struct ipvl_port *port)
187{
188}
189
190static inline void ipvlan_migrate_l3s_hook(struct net *oldnet,
191 struct net *newnet)
192{
193}
194
195static inline int ipvlan_l3s_init(void)
196{
197 return 0;
198}
199
200static inline void ipvlan_l3s_cleanup(void)
201{
202}
203#endif /* CONFIG_IPVLAN_L3S */
Paolo Abeni1ec54cb2018-03-06 10:56:31 +0100204
205static inline bool netif_is_ipvlan_port(const struct net_device *dev)
206{
Paolo Abeniae5799d2018-03-08 10:29:30 +0100207 return rcu_access_pointer(dev->rx_handler) == ipvlan_handle_frame;
Paolo Abeni1ec54cb2018-03-06 10:56:31 +0100208}
209
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800210#endif /* __IPVLAN_H */