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