Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0-or-later */
0002 /*
0003  * Copyright (c) 2014 Mahesh Bandewar <maheshb@google.com>
0004  */
0005 #ifndef __IPVLAN_H
0006 #define __IPVLAN_H
0007 
0008 #include <linux/kernel.h>
0009 #include <linux/types.h>
0010 #include <linux/module.h>
0011 #include <linux/init.h>
0012 #include <linux/rculist.h>
0013 #include <linux/notifier.h>
0014 #include <linux/netdevice.h>
0015 #include <linux/etherdevice.h>
0016 #include <linux/if_arp.h>
0017 #include <linux/if_link.h>
0018 #include <linux/if_vlan.h>
0019 #include <linux/ip.h>
0020 #include <linux/inetdevice.h>
0021 #include <linux/netfilter.h>
0022 #include <net/ip.h>
0023 #include <net/ip6_route.h>
0024 #include <net/netns/generic.h>
0025 #include <net/rtnetlink.h>
0026 #include <net/route.h>
0027 #include <net/addrconf.h>
0028 #include <net/l3mdev.h>
0029 
0030 #define IPVLAN_DRV  "ipvlan"
0031 #define IPV_DRV_VER "0.1"
0032 
0033 #define IPVLAN_HASH_SIZE    (1 << BITS_PER_BYTE)
0034 #define IPVLAN_HASH_MASK    (IPVLAN_HASH_SIZE - 1)
0035 
0036 #define IPVLAN_MAC_FILTER_BITS  8
0037 #define IPVLAN_MAC_FILTER_SIZE  (1 << IPVLAN_MAC_FILTER_BITS)
0038 #define IPVLAN_MAC_FILTER_MASK  (IPVLAN_MAC_FILTER_SIZE - 1)
0039 
0040 #define IPVLAN_QBACKLOG_LIMIT   1000
0041 
0042 typedef enum {
0043     IPVL_IPV6 = 0,
0044     IPVL_ICMPV6,
0045     IPVL_IPV4,
0046     IPVL_ARP,
0047 } ipvl_hdr_type;
0048 
0049 struct ipvl_pcpu_stats {
0050     u64_stats_t     rx_pkts;
0051     u64_stats_t     rx_bytes;
0052     u64_stats_t     rx_mcast;
0053     u64_stats_t     tx_pkts;
0054     u64_stats_t     tx_bytes;
0055     struct u64_stats_sync   syncp;
0056     u32         rx_errs;
0057     u32         tx_drps;
0058 };
0059 
0060 struct ipvl_port;
0061 
0062 struct ipvl_dev {
0063     struct net_device   *dev;
0064     struct list_head    pnode;
0065     struct ipvl_port    *port;
0066     struct net_device   *phy_dev;
0067     struct list_head    addrs;
0068     struct ipvl_pcpu_stats  __percpu *pcpu_stats;
0069     DECLARE_BITMAP(mac_filters, IPVLAN_MAC_FILTER_SIZE);
0070     netdev_features_t   sfeatures;
0071     u32         msg_enable;
0072     spinlock_t      addrs_lock;
0073 };
0074 
0075 struct ipvl_addr {
0076     struct ipvl_dev     *master; /* Back pointer to master */
0077     union {
0078         struct in6_addr ip6;     /* IPv6 address on logical interface */
0079         struct in_addr  ip4;     /* IPv4 address on logical interface */
0080     } ipu;
0081 #define ip6addr ipu.ip6
0082 #define ip4addr ipu.ip4
0083     struct hlist_node   hlnode;  /* Hash-table linkage */
0084     struct list_head    anode;   /* logical-interface linkage */
0085     ipvl_hdr_type       atype;
0086     struct rcu_head     rcu;
0087 };
0088 
0089 struct ipvl_port {
0090     struct net_device   *dev;
0091     possible_net_t      pnet;
0092     struct hlist_head   hlhead[IPVLAN_HASH_SIZE];
0093     struct list_head    ipvlans;
0094     u16         mode;
0095     u16         flags;
0096     u16         dev_id_start;
0097     struct work_struct  wq;
0098     struct sk_buff_head backlog;
0099     int         count;
0100     struct ida      ida;
0101 };
0102 
0103 struct ipvl_skb_cb {
0104     bool tx_pkt;
0105 };
0106 #define IPVL_SKB_CB(_skb) ((struct ipvl_skb_cb *)&((_skb)->cb[0]))
0107 
0108 static inline struct ipvl_port *ipvlan_port_get_rcu(const struct net_device *d)
0109 {
0110     return rcu_dereference(d->rx_handler_data);
0111 }
0112 
0113 static inline struct ipvl_port *ipvlan_port_get_rcu_bh(const struct net_device *d)
0114 {
0115     return rcu_dereference_bh(d->rx_handler_data);
0116 }
0117 
0118 static inline struct ipvl_port *ipvlan_port_get_rtnl(const struct net_device *d)
0119 {
0120     return rtnl_dereference(d->rx_handler_data);
0121 }
0122 
0123 static inline bool ipvlan_is_private(const struct ipvl_port *port)
0124 {
0125     return !!(port->flags & IPVLAN_F_PRIVATE);
0126 }
0127 
0128 static inline void ipvlan_mark_private(struct ipvl_port *port)
0129 {
0130     port->flags |= IPVLAN_F_PRIVATE;
0131 }
0132 
0133 static inline void ipvlan_clear_private(struct ipvl_port *port)
0134 {
0135     port->flags &= ~IPVLAN_F_PRIVATE;
0136 }
0137 
0138 static inline bool ipvlan_is_vepa(const struct ipvl_port *port)
0139 {
0140     return !!(port->flags & IPVLAN_F_VEPA);
0141 }
0142 
0143 static inline void ipvlan_mark_vepa(struct ipvl_port *port)
0144 {
0145     port->flags |= IPVLAN_F_VEPA;
0146 }
0147 
0148 static inline void ipvlan_clear_vepa(struct ipvl_port *port)
0149 {
0150     port->flags &= ~IPVLAN_F_VEPA;
0151 }
0152 
0153 void ipvlan_init_secret(void);
0154 unsigned int ipvlan_mac_hash(const unsigned char *addr);
0155 rx_handler_result_t ipvlan_handle_frame(struct sk_buff **pskb);
0156 void ipvlan_process_multicast(struct work_struct *work);
0157 int ipvlan_queue_xmit(struct sk_buff *skb, struct net_device *dev);
0158 void ipvlan_ht_addr_add(struct ipvl_dev *ipvlan, struct ipvl_addr *addr);
0159 struct ipvl_addr *ipvlan_find_addr(const struct ipvl_dev *ipvlan,
0160                    const void *iaddr, bool is_v6);
0161 bool ipvlan_addr_busy(struct ipvl_port *port, void *iaddr, bool is_v6);
0162 void ipvlan_ht_addr_del(struct ipvl_addr *addr);
0163 struct ipvl_addr *ipvlan_addr_lookup(struct ipvl_port *port, void *lyr3h,
0164                      int addr_type, bool use_dest);
0165 void *ipvlan_get_L3_hdr(struct ipvl_port *port, struct sk_buff *skb, int *type);
0166 void ipvlan_count_rx(const struct ipvl_dev *ipvlan,
0167              unsigned int len, bool success, bool mcast);
0168 int ipvlan_link_new(struct net *src_net, struct net_device *dev,
0169             struct nlattr *tb[], struct nlattr *data[],
0170             struct netlink_ext_ack *extack);
0171 void ipvlan_link_delete(struct net_device *dev, struct list_head *head);
0172 void ipvlan_link_setup(struct net_device *dev);
0173 int ipvlan_link_register(struct rtnl_link_ops *ops);
0174 #ifdef CONFIG_IPVLAN_L3S
0175 int ipvlan_l3s_register(struct ipvl_port *port);
0176 void ipvlan_l3s_unregister(struct ipvl_port *port);
0177 void ipvlan_migrate_l3s_hook(struct net *oldnet, struct net *newnet);
0178 int ipvlan_l3s_init(void);
0179 void ipvlan_l3s_cleanup(void);
0180 #else
0181 static inline int ipvlan_l3s_register(struct ipvl_port *port)
0182 {
0183     return -ENOTSUPP;
0184 }
0185 
0186 static inline void ipvlan_l3s_unregister(struct ipvl_port *port)
0187 {
0188 }
0189 
0190 static inline void ipvlan_migrate_l3s_hook(struct net *oldnet,
0191                        struct net *newnet)
0192 {
0193 }
0194 
0195 static inline int ipvlan_l3s_init(void)
0196 {
0197     return 0;
0198 }
0199 
0200 static inline void ipvlan_l3s_cleanup(void)
0201 {
0202 }
0203 #endif /* CONFIG_IPVLAN_L3S */
0204 
0205 static inline bool netif_is_ipvlan_port(const struct net_device *dev)
0206 {
0207     return rcu_access_pointer(dev->rx_handler) == ipvlan_handle_frame;
0208 }
0209 
0210 #endif /* __IPVLAN_H */