Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    3)  * GENEVE: Generic Network Virtualization Encapsulation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    5)  * Copyright (c) 2015 Red Hat, Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    6)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    7) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    8) #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    9) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   10) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   11) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   12) #include <linux/etherdevice.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   13) #include <linux/hash.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   14) #include <net/ipv6_stubs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   15) #include <net/dst_metadata.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   16) #include <net/gro_cells.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   17) #include <net/rtnetlink.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   18) #include <net/geneve.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   19) #include <net/protocol.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   20) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   21) #define GENEVE_NETDEV_VER	"0.6"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   22) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   23) #define GENEVE_N_VID		(1u << 24)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   24) #define GENEVE_VID_MASK		(GENEVE_N_VID - 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   25) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   26) #define VNI_HASH_BITS		10
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   27) #define VNI_HASH_SIZE		(1<<VNI_HASH_BITS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   28) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   29) static bool log_ecn_error = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   30) module_param(log_ecn_error, bool, 0644);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   31) MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   32) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   33) #define GENEVE_VER 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   34) #define GENEVE_BASE_HLEN (sizeof(struct udphdr) + sizeof(struct genevehdr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   35) #define GENEVE_IPV4_HLEN (ETH_HLEN + sizeof(struct iphdr) + GENEVE_BASE_HLEN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   36) #define GENEVE_IPV6_HLEN (ETH_HLEN + sizeof(struct ipv6hdr) + GENEVE_BASE_HLEN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   37) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   38) /* per-network namespace private data for this module */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   39) struct geneve_net {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   40) 	struct list_head	geneve_list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   41) 	struct list_head	sock_list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   42) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   43) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   44) static unsigned int geneve_net_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   45) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   46) struct geneve_dev_node {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   47) 	struct hlist_node hlist;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   48) 	struct geneve_dev *geneve;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   49) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   50) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   51) struct geneve_config {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   52) 	struct ip_tunnel_info	info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   53) 	bool			collect_md;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   54) 	bool			use_udp6_rx_checksums;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   55) 	bool			ttl_inherit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   56) 	enum ifla_geneve_df	df;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   57) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   58) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   59) /* Pseudo network device */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   60) struct geneve_dev {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   61) 	struct geneve_dev_node hlist4;	/* vni hash table for IPv4 socket */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   62) #if IS_ENABLED(CONFIG_IPV6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   63) 	struct geneve_dev_node hlist6;	/* vni hash table for IPv6 socket */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   64) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   65) 	struct net	   *net;	/* netns for packet i/o */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   66) 	struct net_device  *dev;	/* netdev for geneve tunnel */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   67) 	struct geneve_sock __rcu *sock4;	/* IPv4 socket used for geneve tunnel */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   68) #if IS_ENABLED(CONFIG_IPV6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   69) 	struct geneve_sock __rcu *sock6;	/* IPv6 socket used for geneve tunnel */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   70) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   71) 	struct list_head   next;	/* geneve's per namespace list */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   72) 	struct gro_cells   gro_cells;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   73) 	struct geneve_config cfg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   74) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   75) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   76) struct geneve_sock {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   77) 	bool			collect_md;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   78) 	struct list_head	list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   79) 	struct socket		*sock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   80) 	struct rcu_head		rcu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   81) 	int			refcnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   82) 	struct hlist_head	vni_list[VNI_HASH_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   83) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   84) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   85) static inline __u32 geneve_net_vni_hash(u8 vni[3])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   86) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   87) 	__u32 vnid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   88) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   89) 	vnid = (vni[0] << 16) | (vni[1] << 8) | vni[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   90) 	return hash_32(vnid, VNI_HASH_BITS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   91) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   92) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   93) static __be64 vni_to_tunnel_id(const __u8 *vni)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   94) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   95) #ifdef __BIG_ENDIAN
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   96) 	return (vni[0] << 16) | (vni[1] << 8) | vni[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   97) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   98) 	return (__force __be64)(((__force u64)vni[0] << 40) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   99) 				((__force u64)vni[1] << 48) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  100) 				((__force u64)vni[2] << 56));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  101) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  102) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  103) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  104) /* Convert 64 bit tunnel ID to 24 bit VNI. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  105) static void tunnel_id_to_vni(__be64 tun_id, __u8 *vni)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  106) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  107) #ifdef __BIG_ENDIAN
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  108) 	vni[0] = (__force __u8)(tun_id >> 16);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  109) 	vni[1] = (__force __u8)(tun_id >> 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  110) 	vni[2] = (__force __u8)tun_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  111) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  112) 	vni[0] = (__force __u8)((__force u64)tun_id >> 40);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  113) 	vni[1] = (__force __u8)((__force u64)tun_id >> 48);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  114) 	vni[2] = (__force __u8)((__force u64)tun_id >> 56);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  115) #endif
^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 bool eq_tun_id_and_vni(u8 *tun_id, u8 *vni)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  119) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  120) 	return !memcmp(vni, &tun_id[5], 3);
^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 sa_family_t geneve_get_sk_family(struct geneve_sock *gs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  124) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  125) 	return gs->sock->sk->sk_family;
^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 struct geneve_dev *geneve_lookup(struct geneve_sock *gs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  129) 					__be32 addr, u8 vni[])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  130) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  131) 	struct hlist_head *vni_list_head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  132) 	struct geneve_dev_node *node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  133) 	__u32 hash;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  134) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  135) 	/* Find the device for this VNI */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  136) 	hash = geneve_net_vni_hash(vni);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  137) 	vni_list_head = &gs->vni_list[hash];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  138) 	hlist_for_each_entry_rcu(node, vni_list_head, hlist) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  139) 		if (eq_tun_id_and_vni((u8 *)&node->geneve->cfg.info.key.tun_id, vni) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  140) 		    addr == node->geneve->cfg.info.key.u.ipv4.dst)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  141) 			return node->geneve;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  142) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  143) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  144) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  145) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  146) #if IS_ENABLED(CONFIG_IPV6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  147) static struct geneve_dev *geneve6_lookup(struct geneve_sock *gs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  148) 					 struct in6_addr addr6, u8 vni[])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  149) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  150) 	struct hlist_head *vni_list_head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  151) 	struct geneve_dev_node *node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  152) 	__u32 hash;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  153) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  154) 	/* Find the device for this VNI */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  155) 	hash = geneve_net_vni_hash(vni);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  156) 	vni_list_head = &gs->vni_list[hash];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  157) 	hlist_for_each_entry_rcu(node, vni_list_head, hlist) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  158) 		if (eq_tun_id_and_vni((u8 *)&node->geneve->cfg.info.key.tun_id, vni) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  159) 		    ipv6_addr_equal(&addr6, &node->geneve->cfg.info.key.u.ipv6.dst))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  160) 			return node->geneve;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  161) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  162) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  163) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  164) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  165) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  166) static inline struct genevehdr *geneve_hdr(const struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  167) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  168) 	return (struct genevehdr *)(udp_hdr(skb) + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  169) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  170) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  171) static struct geneve_dev *geneve_lookup_skb(struct geneve_sock *gs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  172) 					    struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  173) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  174) 	static u8 zero_vni[3];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  175) 	u8 *vni;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  176) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  177) 	if (geneve_get_sk_family(gs) == AF_INET) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  178) 		struct iphdr *iph;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  179) 		__be32 addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  180) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  181) 		iph = ip_hdr(skb); /* outer IP header... */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  182) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  183) 		if (gs->collect_md) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  184) 			vni = zero_vni;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  185) 			addr = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  186) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  187) 			vni = geneve_hdr(skb)->vni;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  188) 			addr = iph->saddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  189) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  190) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  191) 		return geneve_lookup(gs, addr, vni);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  192) #if IS_ENABLED(CONFIG_IPV6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  193) 	} else if (geneve_get_sk_family(gs) == AF_INET6) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  194) 		static struct in6_addr zero_addr6;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  195) 		struct ipv6hdr *ip6h;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  196) 		struct in6_addr addr6;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  197) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  198) 		ip6h = ipv6_hdr(skb); /* outer IPv6 header... */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  199) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  200) 		if (gs->collect_md) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  201) 			vni = zero_vni;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  202) 			addr6 = zero_addr6;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  203) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  204) 			vni = geneve_hdr(skb)->vni;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  205) 			addr6 = ip6h->saddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  206) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  207) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  208) 		return geneve6_lookup(gs, addr6, vni);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  209) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  210) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  211) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  212) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  213) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  214) /* geneve receive/decap routine */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  215) static void geneve_rx(struct geneve_dev *geneve, struct geneve_sock *gs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  216) 		      struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  217) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  218) 	struct genevehdr *gnvh = geneve_hdr(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  219) 	struct metadata_dst *tun_dst = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  220) 	unsigned int len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  221) 	int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  222) 	void *oiph;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  223) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  224) 	if (ip_tunnel_collect_metadata() || gs->collect_md) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  225) 		__be16 flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  226) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  227) 		flags = TUNNEL_KEY | (gnvh->oam ? TUNNEL_OAM : 0) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  228) 			(gnvh->critical ? TUNNEL_CRIT_OPT : 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  229) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  230) 		tun_dst = udp_tun_rx_dst(skb, geneve_get_sk_family(gs), flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  231) 					 vni_to_tunnel_id(gnvh->vni),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  232) 					 gnvh->opt_len * 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  233) 		if (!tun_dst) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  234) 			geneve->dev->stats.rx_dropped++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  235) 			goto drop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  236) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  237) 		/* Update tunnel dst according to Geneve options. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  238) 		ip_tunnel_info_opts_set(&tun_dst->u.tun_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  239) 					gnvh->options, gnvh->opt_len * 4,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  240) 					TUNNEL_GENEVE_OPT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  241) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  242) 		/* Drop packets w/ critical options,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  243) 		 * since we don't support any...
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  244) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  245) 		if (gnvh->critical) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  246) 			geneve->dev->stats.rx_frame_errors++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  247) 			geneve->dev->stats.rx_errors++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  248) 			goto drop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  249) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  250) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  251) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  252) 	skb_reset_mac_header(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  253) 	skb->protocol = eth_type_trans(skb, geneve->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  254) 	skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  255) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  256) 	if (tun_dst)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  257) 		skb_dst_set(skb, &tun_dst->dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  258) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  259) 	/* Ignore packet loops (and multicast echo) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  260) 	if (ether_addr_equal(eth_hdr(skb)->h_source, geneve->dev->dev_addr)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  261) 		geneve->dev->stats.rx_errors++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  262) 		goto drop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  263) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  264) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  265) 	oiph = skb_network_header(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  266) 	skb_reset_network_header(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  267) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  268) 	if (geneve_get_sk_family(gs) == AF_INET)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  269) 		err = IP_ECN_decapsulate(oiph, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  270) #if IS_ENABLED(CONFIG_IPV6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  271) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  272) 		err = IP6_ECN_decapsulate(oiph, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  273) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  274) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  275) 	if (unlikely(err)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  276) 		if (log_ecn_error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  277) 			if (geneve_get_sk_family(gs) == AF_INET)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  278) 				net_info_ratelimited("non-ECT from %pI4 "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  279) 						     "with TOS=%#x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  280) 						     &((struct iphdr *)oiph)->saddr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  281) 						     ((struct iphdr *)oiph)->tos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  282) #if IS_ENABLED(CONFIG_IPV6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  283) 			else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  284) 				net_info_ratelimited("non-ECT from %pI6\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  285) 						     &((struct ipv6hdr *)oiph)->saddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  286) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  287) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  288) 		if (err > 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  289) 			++geneve->dev->stats.rx_frame_errors;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  290) 			++geneve->dev->stats.rx_errors;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  291) 			goto drop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  292) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  293) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  294) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  295) 	len = skb->len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  296) 	err = gro_cells_receive(&geneve->gro_cells, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  297) 	if (likely(err == NET_RX_SUCCESS))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  298) 		dev_sw_netstats_rx_add(geneve->dev, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  299) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  300) 	return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  301) drop:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  302) 	/* Consume bad packet */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  303) 	kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  304) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  305) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  306) /* Setup stats when device is created */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  307) static int geneve_init(struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  308) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  309) 	struct geneve_dev *geneve = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  310) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  311) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  312) 	dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  313) 	if (!dev->tstats)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  314) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  315) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  316) 	err = gro_cells_init(&geneve->gro_cells, dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  317) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  318) 		free_percpu(dev->tstats);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  319) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  320) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  321) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  322) 	err = dst_cache_init(&geneve->cfg.info.dst_cache, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  323) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  324) 		free_percpu(dev->tstats);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  325) 		gro_cells_destroy(&geneve->gro_cells);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  326) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  327) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  328) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  329) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  330) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  331) static void geneve_uninit(struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  332) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  333) 	struct geneve_dev *geneve = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  334) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  335) 	dst_cache_destroy(&geneve->cfg.info.dst_cache);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  336) 	gro_cells_destroy(&geneve->gro_cells);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  337) 	free_percpu(dev->tstats);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  338) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  339) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  340) /* Callback from net/ipv4/udp.c to receive packets */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  341) static int geneve_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  342) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  343) 	struct genevehdr *geneveh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  344) 	struct geneve_dev *geneve;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  345) 	struct geneve_sock *gs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  346) 	int opts_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  347) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  348) 	/* Need UDP and Geneve header to be present */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  349) 	if (unlikely(!pskb_may_pull(skb, GENEVE_BASE_HLEN)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  350) 		goto drop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  351) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  352) 	/* Return packets with reserved bits set */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  353) 	geneveh = geneve_hdr(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  354) 	if (unlikely(geneveh->ver != GENEVE_VER))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  355) 		goto drop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  356) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  357) 	if (unlikely(geneveh->proto_type != htons(ETH_P_TEB)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  358) 		goto drop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  359) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  360) 	gs = rcu_dereference_sk_user_data(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  361) 	if (!gs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  362) 		goto drop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  363) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  364) 	geneve = geneve_lookup_skb(gs, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  365) 	if (!geneve)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  366) 		goto drop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  367) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  368) 	opts_len = geneveh->opt_len * 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  369) 	if (iptunnel_pull_header(skb, GENEVE_BASE_HLEN + opts_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  370) 				 htons(ETH_P_TEB),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  371) 				 !net_eq(geneve->net, dev_net(geneve->dev)))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  372) 		geneve->dev->stats.rx_dropped++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  373) 		goto drop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  374) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  375) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  376) 	geneve_rx(geneve, gs, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  377) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  378) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  379) drop:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  380) 	/* Consume bad packet */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  381) 	kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  382) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  383) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  384) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  385) /* Callback from net/ipv{4,6}/udp.c to check that we have a tunnel for errors */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  386) static int geneve_udp_encap_err_lookup(struct sock *sk, struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  387) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  388) 	struct genevehdr *geneveh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  389) 	struct geneve_sock *gs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  390) 	u8 zero_vni[3] = { 0 };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  391) 	u8 *vni = zero_vni;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  392) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  393) 	if (!pskb_may_pull(skb, skb_transport_offset(skb) + GENEVE_BASE_HLEN))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  394) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  395) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  396) 	geneveh = geneve_hdr(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  397) 	if (geneveh->ver != GENEVE_VER)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  398) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  399) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  400) 	if (geneveh->proto_type != htons(ETH_P_TEB))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  401) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  402) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  403) 	gs = rcu_dereference_sk_user_data(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  404) 	if (!gs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  405) 		return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  406) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  407) 	if (geneve_get_sk_family(gs) == AF_INET) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  408) 		struct iphdr *iph = ip_hdr(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  409) 		__be32 addr4 = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  410) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  411) 		if (!gs->collect_md) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  412) 			vni = geneve_hdr(skb)->vni;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  413) 			addr4 = iph->daddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  414) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  415) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  416) 		return geneve_lookup(gs, addr4, vni) ? 0 : -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  417) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  418) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  419) #if IS_ENABLED(CONFIG_IPV6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  420) 	if (geneve_get_sk_family(gs) == AF_INET6) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  421) 		struct ipv6hdr *ip6h = ipv6_hdr(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  422) 		struct in6_addr addr6;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  423) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  424) 		memset(&addr6, 0, sizeof(struct in6_addr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  425) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  426) 		if (!gs->collect_md) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  427) 			vni = geneve_hdr(skb)->vni;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  428) 			addr6 = ip6h->daddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  429) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  430) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  431) 		return geneve6_lookup(gs, addr6, vni) ? 0 : -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  432) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  433) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  434) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  435) 	return -EPFNOSUPPORT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  436) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  437) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  438) static struct socket *geneve_create_sock(struct net *net, bool ipv6,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  439) 					 __be16 port, bool ipv6_rx_csum)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  440) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  441) 	struct socket *sock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  442) 	struct udp_port_cfg udp_conf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  443) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  444) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  445) 	memset(&udp_conf, 0, sizeof(udp_conf));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  446) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  447) 	if (ipv6) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  448) 		udp_conf.family = AF_INET6;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  449) 		udp_conf.ipv6_v6only = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  450) 		udp_conf.use_udp6_rx_checksums = ipv6_rx_csum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  451) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  452) 		udp_conf.family = AF_INET;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  453) 		udp_conf.local_ip.s_addr = htonl(INADDR_ANY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  454) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  455) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  456) 	udp_conf.local_udp_port = port;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  457) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  458) 	/* Open UDP socket */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  459) 	err = udp_sock_create(net, &udp_conf, &sock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  460) 	if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  461) 		return ERR_PTR(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  462) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  463) 	return sock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  464) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  465) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  466) static int geneve_hlen(struct genevehdr *gh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  467) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  468) 	return sizeof(*gh) + gh->opt_len * 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  469) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  470) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  471) static struct sk_buff *geneve_gro_receive(struct sock *sk,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  472) 					  struct list_head *head,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  473) 					  struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  474) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  475) 	struct sk_buff *pp = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  476) 	struct sk_buff *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  477) 	struct genevehdr *gh, *gh2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  478) 	unsigned int hlen, gh_len, off_gnv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  479) 	const struct packet_offload *ptype;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  480) 	__be16 type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  481) 	int flush = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  482) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  483) 	off_gnv = skb_gro_offset(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  484) 	hlen = off_gnv + sizeof(*gh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  485) 	gh = skb_gro_header_fast(skb, off_gnv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  486) 	if (skb_gro_header_hard(skb, hlen)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  487) 		gh = skb_gro_header_slow(skb, hlen, off_gnv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  488) 		if (unlikely(!gh))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  489) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  490) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  491) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  492) 	if (gh->ver != GENEVE_VER || gh->oam)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  493) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  494) 	gh_len = geneve_hlen(gh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  495) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  496) 	hlen = off_gnv + gh_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  497) 	if (skb_gro_header_hard(skb, hlen)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  498) 		gh = skb_gro_header_slow(skb, hlen, off_gnv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  499) 		if (unlikely(!gh))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  500) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  501) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  502) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  503) 	list_for_each_entry(p, head, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  504) 		if (!NAPI_GRO_CB(p)->same_flow)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  505) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  506) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  507) 		gh2 = (struct genevehdr *)(p->data + off_gnv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  508) 		if (gh->opt_len != gh2->opt_len ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  509) 		    memcmp(gh, gh2, gh_len)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  510) 			NAPI_GRO_CB(p)->same_flow = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  511) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  512) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  513) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  514) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  515) 	type = gh->proto_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  516) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  517) 	rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  518) 	ptype = gro_find_receive_by_type(type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  519) 	if (!ptype)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  520) 		goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  521) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  522) 	skb_gro_pull(skb, gh_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  523) 	skb_gro_postpull_rcsum(skb, gh, gh_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  524) 	pp = call_gro_receive(ptype->callbacks.gro_receive, head, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  525) 	flush = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  526) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  527) out_unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  528) 	rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  529) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  530) 	skb_gro_flush_final(skb, pp, flush);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  531) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  532) 	return pp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  533) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  534) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  535) static int geneve_gro_complete(struct sock *sk, struct sk_buff *skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  536) 			       int nhoff)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  537) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  538) 	struct genevehdr *gh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  539) 	struct packet_offload *ptype;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  540) 	__be16 type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  541) 	int gh_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  542) 	int err = -ENOSYS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  543) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  544) 	gh = (struct genevehdr *)(skb->data + nhoff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  545) 	gh_len = geneve_hlen(gh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  546) 	type = gh->proto_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  547) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  548) 	rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  549) 	ptype = gro_find_complete_by_type(type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  550) 	if (ptype)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  551) 		err = ptype->callbacks.gro_complete(skb, nhoff + gh_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  552) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  553) 	rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  554) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  555) 	skb_set_inner_mac_header(skb, nhoff + gh_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  556) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  557) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  558) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  559) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  560) /* Create new listen socket if needed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  561) static struct geneve_sock *geneve_socket_create(struct net *net, __be16 port,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  562) 						bool ipv6, bool ipv6_rx_csum)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  563) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  564) 	struct geneve_net *gn = net_generic(net, geneve_net_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  565) 	struct geneve_sock *gs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  566) 	struct socket *sock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  567) 	struct udp_tunnel_sock_cfg tunnel_cfg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  568) 	int h;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  569) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  570) 	gs = kzalloc(sizeof(*gs), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  571) 	if (!gs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  572) 		return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  573) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  574) 	sock = geneve_create_sock(net, ipv6, port, ipv6_rx_csum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  575) 	if (IS_ERR(sock)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  576) 		kfree(gs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  577) 		return ERR_CAST(sock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  578) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  579) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  580) 	gs->sock = sock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  581) 	gs->refcnt = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  582) 	for (h = 0; h < VNI_HASH_SIZE; ++h)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  583) 		INIT_HLIST_HEAD(&gs->vni_list[h]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  584) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  585) 	/* Initialize the geneve udp offloads structure */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  586) 	udp_tunnel_notify_add_rx_port(gs->sock, UDP_TUNNEL_TYPE_GENEVE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  587) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  588) 	/* Mark socket as an encapsulation socket */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  589) 	memset(&tunnel_cfg, 0, sizeof(tunnel_cfg));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  590) 	tunnel_cfg.sk_user_data = gs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  591) 	tunnel_cfg.encap_type = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  592) 	tunnel_cfg.gro_receive = geneve_gro_receive;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  593) 	tunnel_cfg.gro_complete = geneve_gro_complete;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  594) 	tunnel_cfg.encap_rcv = geneve_udp_encap_recv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  595) 	tunnel_cfg.encap_err_lookup = geneve_udp_encap_err_lookup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  596) 	tunnel_cfg.encap_destroy = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  597) 	setup_udp_tunnel_sock(net, sock, &tunnel_cfg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  598) 	list_add(&gs->list, &gn->sock_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  599) 	return gs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  600) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  601) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  602) static void __geneve_sock_release(struct geneve_sock *gs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  603) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  604) 	if (!gs || --gs->refcnt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  605) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  606) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  607) 	list_del(&gs->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  608) 	udp_tunnel_notify_del_rx_port(gs->sock, UDP_TUNNEL_TYPE_GENEVE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  609) 	udp_tunnel_sock_release(gs->sock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  610) 	kfree_rcu(gs, rcu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  611) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  612) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  613) static void geneve_sock_release(struct geneve_dev *geneve)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  614) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  615) 	struct geneve_sock *gs4 = rtnl_dereference(geneve->sock4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  616) #if IS_ENABLED(CONFIG_IPV6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  617) 	struct geneve_sock *gs6 = rtnl_dereference(geneve->sock6);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  618) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  619) 	rcu_assign_pointer(geneve->sock6, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  620) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  621) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  622) 	rcu_assign_pointer(geneve->sock4, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  623) 	synchronize_net();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  624) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  625) 	__geneve_sock_release(gs4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  626) #if IS_ENABLED(CONFIG_IPV6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  627) 	__geneve_sock_release(gs6);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  628) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  629) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  630) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  631) static struct geneve_sock *geneve_find_sock(struct geneve_net *gn,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  632) 					    sa_family_t family,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  633) 					    __be16 dst_port)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  634) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  635) 	struct geneve_sock *gs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  636) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  637) 	list_for_each_entry(gs, &gn->sock_list, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  638) 		if (inet_sk(gs->sock->sk)->inet_sport == dst_port &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  639) 		    geneve_get_sk_family(gs) == family) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  640) 			return gs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  641) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  642) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  643) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  644) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  645) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  646) static int geneve_sock_add(struct geneve_dev *geneve, bool ipv6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  647) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  648) 	struct net *net = geneve->net;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  649) 	struct geneve_net *gn = net_generic(net, geneve_net_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  650) 	struct geneve_dev_node *node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  651) 	struct geneve_sock *gs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  652) 	__u8 vni[3];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  653) 	__u32 hash;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  654) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  655) 	gs = geneve_find_sock(gn, ipv6 ? AF_INET6 : AF_INET, geneve->cfg.info.key.tp_dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  656) 	if (gs) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  657) 		gs->refcnt++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  658) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  659) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  660) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  661) 	gs = geneve_socket_create(net, geneve->cfg.info.key.tp_dst, ipv6,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  662) 				  geneve->cfg.use_udp6_rx_checksums);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  663) 	if (IS_ERR(gs))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  664) 		return PTR_ERR(gs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  665) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  666) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  667) 	gs->collect_md = geneve->cfg.collect_md;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  668) #if IS_ENABLED(CONFIG_IPV6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  669) 	if (ipv6) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  670) 		rcu_assign_pointer(geneve->sock6, gs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  671) 		node = &geneve->hlist6;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  672) 	} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  673) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  674) 	{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  675) 		rcu_assign_pointer(geneve->sock4, gs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  676) 		node = &geneve->hlist4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  677) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  678) 	node->geneve = geneve;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  679) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  680) 	tunnel_id_to_vni(geneve->cfg.info.key.tun_id, vni);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  681) 	hash = geneve_net_vni_hash(vni);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  682) 	hlist_add_head_rcu(&node->hlist, &gs->vni_list[hash]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  683) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  684) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  685) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  686) static int geneve_open(struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  687) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  688) 	struct geneve_dev *geneve = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  689) 	bool metadata = geneve->cfg.collect_md;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  690) 	bool ipv4, ipv6;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  691) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  692) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  693) 	ipv6 = geneve->cfg.info.mode & IP_TUNNEL_INFO_IPV6 || metadata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  694) 	ipv4 = !ipv6 || metadata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  695) #if IS_ENABLED(CONFIG_IPV6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  696) 	if (ipv6) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  697) 		ret = geneve_sock_add(geneve, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  698) 		if (ret < 0 && ret != -EAFNOSUPPORT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  699) 			ipv4 = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  700) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  701) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  702) 	if (ipv4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  703) 		ret = geneve_sock_add(geneve, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  704) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  705) 		geneve_sock_release(geneve);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  706) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  707) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  708) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  709) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  710) static int geneve_stop(struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  711) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  712) 	struct geneve_dev *geneve = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  713) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  714) 	hlist_del_init_rcu(&geneve->hlist4.hlist);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  715) #if IS_ENABLED(CONFIG_IPV6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  716) 	hlist_del_init_rcu(&geneve->hlist6.hlist);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  717) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  718) 	geneve_sock_release(geneve);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  719) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  720) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  721) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  722) static void geneve_build_header(struct genevehdr *geneveh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  723) 				const struct ip_tunnel_info *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  724) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  725) 	geneveh->ver = GENEVE_VER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  726) 	geneveh->opt_len = info->options_len / 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  727) 	geneveh->oam = !!(info->key.tun_flags & TUNNEL_OAM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  728) 	geneveh->critical = !!(info->key.tun_flags & TUNNEL_CRIT_OPT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  729) 	geneveh->rsvd1 = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  730) 	tunnel_id_to_vni(info->key.tun_id, geneveh->vni);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  731) 	geneveh->proto_type = htons(ETH_P_TEB);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  732) 	geneveh->rsvd2 = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  733) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  734) 	if (info->key.tun_flags & TUNNEL_GENEVE_OPT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  735) 		ip_tunnel_info_opts_get(geneveh->options, info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  736) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  737) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  738) static int geneve_build_skb(struct dst_entry *dst, struct sk_buff *skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  739) 			    const struct ip_tunnel_info *info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  740) 			    bool xnet, int ip_hdr_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  741) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  742) 	bool udp_sum = !!(info->key.tun_flags & TUNNEL_CSUM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  743) 	struct genevehdr *gnvh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  744) 	int min_headroom;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  745) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  746) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  747) 	skb_reset_mac_header(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  748) 	skb_scrub_packet(skb, xnet);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  749) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  750) 	min_headroom = LL_RESERVED_SPACE(dst->dev) + dst->header_len +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  751) 		       GENEVE_BASE_HLEN + info->options_len + ip_hdr_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  752) 	err = skb_cow_head(skb, min_headroom);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  753) 	if (unlikely(err))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  754) 		goto free_dst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  755) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  756) 	err = udp_tunnel_handle_offloads(skb, udp_sum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  757) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  758) 		goto free_dst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  759) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  760) 	gnvh = __skb_push(skb, sizeof(*gnvh) + info->options_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  761) 	geneve_build_header(gnvh, info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  762) 	skb_set_inner_protocol(skb, htons(ETH_P_TEB));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  763) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  764) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  765) free_dst:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  766) 	dst_release(dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  767) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  768) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  769) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  770) static struct rtable *geneve_get_v4_rt(struct sk_buff *skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  771) 				       struct net_device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  772) 				       struct geneve_sock *gs4,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  773) 				       struct flowi4 *fl4,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  774) 				       const struct ip_tunnel_info *info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  775) 				       __be16 dport, __be16 sport)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  776) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  777) 	bool use_cache = ip_tunnel_dst_cache_usable(skb, info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  778) 	struct geneve_dev *geneve = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  779) 	struct dst_cache *dst_cache;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  780) 	struct rtable *rt = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  781) 	__u8 tos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  782) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  783) 	if (!gs4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  784) 		return ERR_PTR(-EIO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  785) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  786) 	memset(fl4, 0, sizeof(*fl4));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  787) 	fl4->flowi4_mark = skb->mark;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  788) 	fl4->flowi4_proto = IPPROTO_UDP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  789) 	fl4->daddr = info->key.u.ipv4.dst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  790) 	fl4->saddr = info->key.u.ipv4.src;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  791) 	fl4->fl4_dport = dport;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  792) 	fl4->fl4_sport = sport;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  793) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  794) 	tos = info->key.tos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  795) 	if ((tos == 1) && !geneve->cfg.collect_md) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  796) 		tos = ip_tunnel_get_dsfield(ip_hdr(skb), skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  797) 		use_cache = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  798) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  799) 	fl4->flowi4_tos = RT_TOS(tos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  800) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  801) 	dst_cache = (struct dst_cache *)&info->dst_cache;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  802) 	if (use_cache) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  803) 		rt = dst_cache_get_ip4(dst_cache, &fl4->saddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  804) 		if (rt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  805) 			return rt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  806) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  807) 	rt = ip_route_output_key(geneve->net, fl4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  808) 	if (IS_ERR(rt)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  809) 		netdev_dbg(dev, "no route to %pI4\n", &fl4->daddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  810) 		return ERR_PTR(-ENETUNREACH);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  811) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  812) 	if (rt->dst.dev == dev) { /* is this necessary? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  813) 		netdev_dbg(dev, "circular route to %pI4\n", &fl4->daddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  814) 		ip_rt_put(rt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  815) 		return ERR_PTR(-ELOOP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  816) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  817) 	if (use_cache)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  818) 		dst_cache_set_ip4(dst_cache, &rt->dst, fl4->saddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  819) 	return rt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  820) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  821) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  822) #if IS_ENABLED(CONFIG_IPV6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  823) static struct dst_entry *geneve_get_v6_dst(struct sk_buff *skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  824) 					   struct net_device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  825) 					   struct geneve_sock *gs6,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  826) 					   struct flowi6 *fl6,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  827) 					   const struct ip_tunnel_info *info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  828) 					   __be16 dport, __be16 sport)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  829) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  830) 	bool use_cache = ip_tunnel_dst_cache_usable(skb, info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  831) 	struct geneve_dev *geneve = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  832) 	struct dst_entry *dst = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  833) 	struct dst_cache *dst_cache;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  834) 	__u8 prio;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  835) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  836) 	if (!gs6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  837) 		return ERR_PTR(-EIO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  838) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  839) 	memset(fl6, 0, sizeof(*fl6));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  840) 	fl6->flowi6_mark = skb->mark;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  841) 	fl6->flowi6_proto = IPPROTO_UDP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  842) 	fl6->daddr = info->key.u.ipv6.dst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  843) 	fl6->saddr = info->key.u.ipv6.src;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  844) 	fl6->fl6_dport = dport;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  845) 	fl6->fl6_sport = sport;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  846) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  847) 	prio = info->key.tos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  848) 	if ((prio == 1) && !geneve->cfg.collect_md) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  849) 		prio = ip_tunnel_get_dsfield(ip_hdr(skb), skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  850) 		use_cache = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  851) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  852) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  853) 	fl6->flowlabel = ip6_make_flowinfo(RT_TOS(prio),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  854) 					   info->key.label);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  855) 	dst_cache = (struct dst_cache *)&info->dst_cache;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  856) 	if (use_cache) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  857) 		dst = dst_cache_get_ip6(dst_cache, &fl6->saddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  858) 		if (dst)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  859) 			return dst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  860) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  861) 	dst = ipv6_stub->ipv6_dst_lookup_flow(geneve->net, gs6->sock->sk, fl6,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  862) 					      NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  863) 	if (IS_ERR(dst)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  864) 		netdev_dbg(dev, "no route to %pI6\n", &fl6->daddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  865) 		return ERR_PTR(-ENETUNREACH);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  866) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  867) 	if (dst->dev == dev) { /* is this necessary? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  868) 		netdev_dbg(dev, "circular route to %pI6\n", &fl6->daddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  869) 		dst_release(dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  870) 		return ERR_PTR(-ELOOP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  871) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  872) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  873) 	if (use_cache)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  874) 		dst_cache_set_ip6(dst_cache, dst, &fl6->saddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  875) 	return dst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  876) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  877) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  878) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  879) static int geneve_xmit_skb(struct sk_buff *skb, struct net_device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  880) 			   struct geneve_dev *geneve,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  881) 			   const struct ip_tunnel_info *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  882) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  883) 	bool xnet = !net_eq(geneve->net, dev_net(geneve->dev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  884) 	struct geneve_sock *gs4 = rcu_dereference(geneve->sock4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  885) 	const struct ip_tunnel_key *key = &info->key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  886) 	struct rtable *rt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  887) 	struct flowi4 fl4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  888) 	__u8 tos, ttl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  889) 	__be16 df = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  890) 	__be16 sport;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  891) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  892) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  893) 	if (!pskb_inet_may_pull(skb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  894) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  895) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  896) 	sport = udp_flow_src_port(geneve->net, skb, 1, USHRT_MAX, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  897) 	rt = geneve_get_v4_rt(skb, dev, gs4, &fl4, info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  898) 			      geneve->cfg.info.key.tp_dst, sport);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  899) 	if (IS_ERR(rt))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  900) 		return PTR_ERR(rt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  901) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  902) 	err = skb_tunnel_check_pmtu(skb, &rt->dst,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  903) 				    GENEVE_IPV4_HLEN + info->options_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  904) 				    netif_is_any_bridge_port(dev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  905) 	if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  906) 		dst_release(&rt->dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  907) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  908) 	} else if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  909) 		struct ip_tunnel_info *info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  910) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  911) 		info = skb_tunnel_info(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  912) 		if (info) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  913) 			struct ip_tunnel_info *unclone;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  914) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  915) 			unclone = skb_tunnel_info_unclone(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  916) 			if (unlikely(!unclone)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  917) 				dst_release(&rt->dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  918) 				return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  919) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  920) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  921) 			unclone->key.u.ipv4.dst = fl4.saddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  922) 			unclone->key.u.ipv4.src = fl4.daddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  923) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  924) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  925) 		if (!pskb_may_pull(skb, ETH_HLEN)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  926) 			dst_release(&rt->dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  927) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  928) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  929) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  930) 		skb->protocol = eth_type_trans(skb, geneve->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  931) 		netif_rx(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  932) 		dst_release(&rt->dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  933) 		return -EMSGSIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  934) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  935) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  936) 	if (geneve->cfg.collect_md) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  937) 		tos = ip_tunnel_ecn_encap(key->tos, ip_hdr(skb), skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  938) 		ttl = key->ttl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  939) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  940) 		df = key->tun_flags & TUNNEL_DONT_FRAGMENT ? htons(IP_DF) : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  941) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  942) 		tos = ip_tunnel_ecn_encap(fl4.flowi4_tos, ip_hdr(skb), skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  943) 		if (geneve->cfg.ttl_inherit)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  944) 			ttl = ip_tunnel_get_ttl(ip_hdr(skb), skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  945) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  946) 			ttl = key->ttl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  947) 		ttl = ttl ? : ip4_dst_hoplimit(&rt->dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  948) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  949) 		if (geneve->cfg.df == GENEVE_DF_SET) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  950) 			df = htons(IP_DF);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  951) 		} else if (geneve->cfg.df == GENEVE_DF_INHERIT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  952) 			struct ethhdr *eth = eth_hdr(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  953) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  954) 			if (ntohs(eth->h_proto) == ETH_P_IPV6) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  955) 				df = htons(IP_DF);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  956) 			} else if (ntohs(eth->h_proto) == ETH_P_IP) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  957) 				struct iphdr *iph = ip_hdr(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  958) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  959) 				if (iph->frag_off & htons(IP_DF))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  960) 					df = htons(IP_DF);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  961) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  962) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  963) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  964) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  965) 	err = geneve_build_skb(&rt->dst, skb, info, xnet, sizeof(struct iphdr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  966) 	if (unlikely(err))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  967) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  968) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  969) 	udp_tunnel_xmit_skb(rt, gs4->sock->sk, skb, fl4.saddr, fl4.daddr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  970) 			    tos, ttl, df, sport, geneve->cfg.info.key.tp_dst,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  971) 			    !net_eq(geneve->net, dev_net(geneve->dev)),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  972) 			    !(info->key.tun_flags & TUNNEL_CSUM));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  973) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  974) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  975) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  976) #if IS_ENABLED(CONFIG_IPV6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  977) static int geneve6_xmit_skb(struct sk_buff *skb, struct net_device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  978) 			    struct geneve_dev *geneve,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  979) 			    const struct ip_tunnel_info *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  980) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  981) 	bool xnet = !net_eq(geneve->net, dev_net(geneve->dev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  982) 	struct geneve_sock *gs6 = rcu_dereference(geneve->sock6);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  983) 	const struct ip_tunnel_key *key = &info->key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  984) 	struct dst_entry *dst = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  985) 	struct flowi6 fl6;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  986) 	__u8 prio, ttl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  987) 	__be16 sport;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  988) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  989) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  990) 	if (!pskb_inet_may_pull(skb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  991) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  992) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  993) 	sport = udp_flow_src_port(geneve->net, skb, 1, USHRT_MAX, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  994) 	dst = geneve_get_v6_dst(skb, dev, gs6, &fl6, info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  995) 				geneve->cfg.info.key.tp_dst, sport);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  996) 	if (IS_ERR(dst))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  997) 		return PTR_ERR(dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  998) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  999) 	err = skb_tunnel_check_pmtu(skb, dst,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) 				    GENEVE_IPV6_HLEN + info->options_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) 				    netif_is_any_bridge_port(dev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) 	if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) 		dst_release(dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) 	} else if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) 		struct ip_tunnel_info *info = skb_tunnel_info(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) 		if (info) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) 			struct ip_tunnel_info *unclone;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) 			unclone = skb_tunnel_info_unclone(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) 			if (unlikely(!unclone)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) 				dst_release(dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) 				return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) 			unclone->key.u.ipv6.dst = fl6.saddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) 			unclone->key.u.ipv6.src = fl6.daddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) 		if (!pskb_may_pull(skb, ETH_HLEN)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) 			dst_release(dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) 		skb->protocol = eth_type_trans(skb, geneve->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) 		netif_rx(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) 		dst_release(dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) 		return -EMSGSIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) 	if (geneve->cfg.collect_md) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) 		prio = ip_tunnel_ecn_encap(key->tos, ip_hdr(skb), skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) 		ttl = key->ttl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) 		prio = ip_tunnel_ecn_encap(ip6_tclass(fl6.flowlabel),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) 					   ip_hdr(skb), skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) 		if (geneve->cfg.ttl_inherit)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) 			ttl = ip_tunnel_get_ttl(ip_hdr(skb), skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) 			ttl = key->ttl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) 		ttl = ttl ? : ip6_dst_hoplimit(dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) 	err = geneve_build_skb(dst, skb, info, xnet, sizeof(struct ipv6hdr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) 	if (unlikely(err))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) 	udp_tunnel6_xmit_skb(dst, gs6->sock->sk, skb, dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) 			     &fl6.saddr, &fl6.daddr, prio, ttl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) 			     info->key.label, sport, geneve->cfg.info.key.tp_dst,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) 			     !(info->key.tun_flags & TUNNEL_CSUM));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) static netdev_tx_t geneve_xmit(struct sk_buff *skb, struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) 	struct geneve_dev *geneve = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) 	struct ip_tunnel_info *info = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) 	if (geneve->cfg.collect_md) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) 		info = skb_tunnel_info(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) 		if (unlikely(!info || !(info->mode & IP_TUNNEL_INFO_TX))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) 			netdev_dbg(dev, "no tunnel metadata\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) 			dev_kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) 			dev->stats.tx_dropped++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) 			return NETDEV_TX_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) 		info = &geneve->cfg.info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) 	rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) #if IS_ENABLED(CONFIG_IPV6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) 	if (info->mode & IP_TUNNEL_INFO_IPV6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077) 		err = geneve6_xmit_skb(skb, dev, geneve, info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) 		err = geneve_xmit_skb(skb, dev, geneve, info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) 	rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) 	if (likely(!err))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) 		return NETDEV_TX_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) 	if (err != -EMSGSIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) 		dev_kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) 	if (err == -ELOOP)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) 		dev->stats.collisions++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091) 	else if (err == -ENETUNREACH)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) 		dev->stats.tx_carrier_errors++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) 	dev->stats.tx_errors++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) 	return NETDEV_TX_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) static int geneve_change_mtu(struct net_device *dev, int new_mtu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100) 	if (new_mtu > dev->max_mtu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) 		new_mtu = dev->max_mtu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102) 	else if (new_mtu < dev->min_mtu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103) 		new_mtu = dev->min_mtu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105) 	dev->mtu = new_mtu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109) static int geneve_fill_metadata_dst(struct net_device *dev, struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) 	struct ip_tunnel_info *info = skb_tunnel_info(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112) 	struct geneve_dev *geneve = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113) 	__be16 sport;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115) 	if (ip_tunnel_info_af(info) == AF_INET) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116) 		struct rtable *rt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117) 		struct flowi4 fl4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119) 		struct geneve_sock *gs4 = rcu_dereference(geneve->sock4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) 		sport = udp_flow_src_port(geneve->net, skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) 					  1, USHRT_MAX, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) 		rt = geneve_get_v4_rt(skb, dev, gs4, &fl4, info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) 				      geneve->cfg.info.key.tp_dst, sport);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125) 		if (IS_ERR(rt))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126) 			return PTR_ERR(rt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128) 		ip_rt_put(rt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) 		info->key.u.ipv4.src = fl4.saddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130) #if IS_ENABLED(CONFIG_IPV6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) 	} else if (ip_tunnel_info_af(info) == AF_INET6) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) 		struct dst_entry *dst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) 		struct flowi6 fl6;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135) 		struct geneve_sock *gs6 = rcu_dereference(geneve->sock6);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) 		sport = udp_flow_src_port(geneve->net, skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) 					  1, USHRT_MAX, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) 		dst = geneve_get_v6_dst(skb, dev, gs6, &fl6, info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140) 					geneve->cfg.info.key.tp_dst, sport);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) 		if (IS_ERR(dst))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142) 			return PTR_ERR(dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144) 		dst_release(dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145) 		info->key.u.ipv6.src = fl6.saddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151) 	info->key.tp_src = sport;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152) 	info->key.tp_dst = geneve->cfg.info.key.tp_dst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156) static const struct net_device_ops geneve_netdev_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157) 	.ndo_init		= geneve_init,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158) 	.ndo_uninit		= geneve_uninit,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159) 	.ndo_open		= geneve_open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160) 	.ndo_stop		= geneve_stop,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161) 	.ndo_start_xmit		= geneve_xmit,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162) 	.ndo_get_stats64	= ip_tunnel_get_stats64,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163) 	.ndo_change_mtu		= geneve_change_mtu,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164) 	.ndo_validate_addr	= eth_validate_addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165) 	.ndo_set_mac_address	= eth_mac_addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166) 	.ndo_fill_metadata_dst	= geneve_fill_metadata_dst,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169) static void geneve_get_drvinfo(struct net_device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170) 			       struct ethtool_drvinfo *drvinfo)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172) 	strlcpy(drvinfo->version, GENEVE_NETDEV_VER, sizeof(drvinfo->version));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173) 	strlcpy(drvinfo->driver, "geneve", sizeof(drvinfo->driver));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176) static const struct ethtool_ops geneve_ethtool_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177) 	.get_drvinfo	= geneve_get_drvinfo,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1178) 	.get_link	= ethtool_op_get_link,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1179) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1180) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1181) /* Info for udev, that this is a virtual tunnel endpoint */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1182) static struct device_type geneve_type = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1183) 	.name = "geneve",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1184) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1185) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1186) /* Calls the ndo_udp_tunnel_add of the caller in order to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1187)  * supply the listening GENEVE udp ports. Callers are expected
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1188)  * to implement the ndo_udp_tunnel_add.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1189)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1190) static void geneve_offload_rx_ports(struct net_device *dev, bool push)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1191) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1192) 	struct net *net = dev_net(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1193) 	struct geneve_net *gn = net_generic(net, geneve_net_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1194) 	struct geneve_sock *gs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1195) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1196) 	rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1197) 	list_for_each_entry_rcu(gs, &gn->sock_list, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1198) 		if (push) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1199) 			udp_tunnel_push_rx_port(dev, gs->sock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1200) 						UDP_TUNNEL_TYPE_GENEVE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1201) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1202) 			udp_tunnel_drop_rx_port(dev, gs->sock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1203) 						UDP_TUNNEL_TYPE_GENEVE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1204) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1205) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1206) 	rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1207) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1208) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1209) /* Initialize the device structure. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1210) static void geneve_setup(struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1211) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1212) 	ether_setup(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1213) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1214) 	dev->netdev_ops = &geneve_netdev_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1215) 	dev->ethtool_ops = &geneve_ethtool_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1216) 	dev->needs_free_netdev = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1217) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1218) 	SET_NETDEV_DEVTYPE(dev, &geneve_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1219) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1220) 	dev->features    |= NETIF_F_LLTX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1221) 	dev->features    |= NETIF_F_SG | NETIF_F_HW_CSUM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1222) 	dev->features    |= NETIF_F_RXCSUM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1223) 	dev->features    |= NETIF_F_GSO_SOFTWARE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1224) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1225) 	dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1226) 	dev->hw_features |= NETIF_F_GSO_SOFTWARE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1227) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1228) 	/* MTU range: 68 - (something less than 65535) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1229) 	dev->min_mtu = ETH_MIN_MTU;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1230) 	/* The max_mtu calculation does not take account of GENEVE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1231) 	 * options, to avoid excluding potentially valid
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1232) 	 * configurations. This will be further reduced by IPvX hdr size.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1233) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1234) 	dev->max_mtu = IP_MAX_MTU - GENEVE_BASE_HLEN - dev->hard_header_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1235) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1236) 	netif_keep_dst(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1237) 	dev->priv_flags &= ~IFF_TX_SKB_SHARING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1238) 	dev->priv_flags |= IFF_LIVE_ADDR_CHANGE | IFF_NO_QUEUE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1239) 	eth_hw_addr_random(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1240) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1241) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1242) static const struct nla_policy geneve_policy[IFLA_GENEVE_MAX + 1] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1243) 	[IFLA_GENEVE_ID]		= { .type = NLA_U32 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1244) 	[IFLA_GENEVE_REMOTE]		= { .len = sizeof_field(struct iphdr, daddr) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1245) 	[IFLA_GENEVE_REMOTE6]		= { .len = sizeof(struct in6_addr) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1246) 	[IFLA_GENEVE_TTL]		= { .type = NLA_U8 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1247) 	[IFLA_GENEVE_TOS]		= { .type = NLA_U8 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1248) 	[IFLA_GENEVE_LABEL]		= { .type = NLA_U32 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1249) 	[IFLA_GENEVE_PORT]		= { .type = NLA_U16 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1250) 	[IFLA_GENEVE_COLLECT_METADATA]	= { .type = NLA_FLAG },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1251) 	[IFLA_GENEVE_UDP_CSUM]		= { .type = NLA_U8 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1252) 	[IFLA_GENEVE_UDP_ZERO_CSUM6_TX]	= { .type = NLA_U8 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1253) 	[IFLA_GENEVE_UDP_ZERO_CSUM6_RX]	= { .type = NLA_U8 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1254) 	[IFLA_GENEVE_TTL_INHERIT]	= { .type = NLA_U8 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1255) 	[IFLA_GENEVE_DF]		= { .type = NLA_U8 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1256) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1257) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1258) static int geneve_validate(struct nlattr *tb[], struct nlattr *data[],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1259) 			   struct netlink_ext_ack *extack)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1260) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1261) 	if (tb[IFLA_ADDRESS]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1262) 		if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1263) 			NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_ADDRESS],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1264) 					    "Provided link layer address is not Ethernet");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1265) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1266) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1267) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1268) 		if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1269) 			NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_ADDRESS],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1270) 					    "Provided Ethernet address is not unicast");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1271) 			return -EADDRNOTAVAIL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1272) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1273) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1274) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1275) 	if (!data) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1276) 		NL_SET_ERR_MSG(extack,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1277) 			       "Not enough attributes provided to perform the operation");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1278) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1279) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1280) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1281) 	if (data[IFLA_GENEVE_ID]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1282) 		__u32 vni =  nla_get_u32(data[IFLA_GENEVE_ID]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1283) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1284) 		if (vni >= GENEVE_N_VID) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1285) 			NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_ID],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1286) 					    "Geneve ID must be lower than 16777216");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1287) 			return -ERANGE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1288) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1289) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1290) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1291) 	if (data[IFLA_GENEVE_DF]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1292) 		enum ifla_geneve_df df = nla_get_u8(data[IFLA_GENEVE_DF]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1293) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1294) 		if (df < 0 || df > GENEVE_DF_MAX) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1295) 			NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_DF],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1296) 					    "Invalid DF attribute");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1297) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1298) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1299) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1300) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1301) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1302) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1303) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1304) static struct geneve_dev *geneve_find_dev(struct geneve_net *gn,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1305) 					  const struct ip_tunnel_info *info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1306) 					  bool *tun_on_same_port,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1307) 					  bool *tun_collect_md)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1308) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1309) 	struct geneve_dev *geneve, *t = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1310) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1311) 	*tun_on_same_port = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1312) 	*tun_collect_md = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1313) 	list_for_each_entry(geneve, &gn->geneve_list, next) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1314) 		if (info->key.tp_dst == geneve->cfg.info.key.tp_dst) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1315) 			*tun_collect_md = geneve->cfg.collect_md;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1316) 			*tun_on_same_port = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1317) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1318) 		if (info->key.tun_id == geneve->cfg.info.key.tun_id &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1319) 		    info->key.tp_dst == geneve->cfg.info.key.tp_dst &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1320) 		    !memcmp(&info->key.u, &geneve->cfg.info.key.u, sizeof(info->key.u)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1321) 			t = geneve;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1322) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1323) 	return t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1324) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1325) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1326) static bool is_tnl_info_zero(const struct ip_tunnel_info *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1327) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1328) 	return !(info->key.tun_id || info->key.tun_flags || info->key.tos ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1329) 		 info->key.ttl || info->key.label || info->key.tp_src ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1330) 		 memchr_inv(&info->key.u, 0, sizeof(info->key.u)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1331) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1332) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1333) static bool geneve_dst_addr_equal(struct ip_tunnel_info *a,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1334) 				  struct ip_tunnel_info *b)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1335) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1336) 	if (ip_tunnel_info_af(a) == AF_INET)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1337) 		return a->key.u.ipv4.dst == b->key.u.ipv4.dst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1338) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1339) 		return ipv6_addr_equal(&a->key.u.ipv6.dst, &b->key.u.ipv6.dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1340) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1341) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1342) static int geneve_configure(struct net *net, struct net_device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1343) 			    struct netlink_ext_ack *extack,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1344) 			    const struct geneve_config *cfg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1345) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1346) 	struct geneve_net *gn = net_generic(net, geneve_net_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1347) 	struct geneve_dev *t, *geneve = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1348) 	const struct ip_tunnel_info *info = &cfg->info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1349) 	bool tun_collect_md, tun_on_same_port;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1350) 	int err, encap_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1351) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1352) 	if (cfg->collect_md && !is_tnl_info_zero(info)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1353) 		NL_SET_ERR_MSG(extack,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1354) 			       "Device is externally controlled, so attributes (VNI, Port, and so on) must not be specified");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1355) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1356) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1357) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1358) 	geneve->net = net;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1359) 	geneve->dev = dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1360) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1361) 	t = geneve_find_dev(gn, info, &tun_on_same_port, &tun_collect_md);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1362) 	if (t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1363) 		return -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1364) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1365) 	/* make enough headroom for basic scenario */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1366) 	encap_len = GENEVE_BASE_HLEN + ETH_HLEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1367) 	if (!cfg->collect_md && ip_tunnel_info_af(info) == AF_INET) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1368) 		encap_len += sizeof(struct iphdr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1369) 		dev->max_mtu -= sizeof(struct iphdr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1370) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1371) 		encap_len += sizeof(struct ipv6hdr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1372) 		dev->max_mtu -= sizeof(struct ipv6hdr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1373) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1374) 	dev->needed_headroom = encap_len + ETH_HLEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1375) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1376) 	if (cfg->collect_md) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1377) 		if (tun_on_same_port) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1378) 			NL_SET_ERR_MSG(extack,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1379) 				       "There can be only one externally controlled device on a destination port");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1380) 			return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1381) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1382) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1383) 		if (tun_collect_md) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1384) 			NL_SET_ERR_MSG(extack,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1385) 				       "There already exists an externally controlled device on this destination port");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1386) 			return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1387) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1388) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1389) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1390) 	dst_cache_reset(&geneve->cfg.info.dst_cache);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1391) 	memcpy(&geneve->cfg, cfg, sizeof(*cfg));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1392) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1393) 	err = register_netdevice(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1394) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1395) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1396) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1397) 	list_add(&geneve->next, &gn->geneve_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1398) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1399) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1400) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1401) static void init_tnl_info(struct ip_tunnel_info *info, __u16 dst_port)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1402) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1403) 	memset(info, 0, sizeof(*info));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1404) 	info->key.tp_dst = htons(dst_port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1405) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1406) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1407) static int geneve_nl2info(struct nlattr *tb[], struct nlattr *data[],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1408) 			  struct netlink_ext_ack *extack,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1409) 			  struct geneve_config *cfg, bool changelink)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1410) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1411) 	struct ip_tunnel_info *info = &cfg->info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1412) 	int attrtype;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1413) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1414) 	if (data[IFLA_GENEVE_REMOTE] && data[IFLA_GENEVE_REMOTE6]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1415) 		NL_SET_ERR_MSG(extack,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1416) 			       "Cannot specify both IPv4 and IPv6 Remote addresses");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1417) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1418) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1419) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1420) 	if (data[IFLA_GENEVE_REMOTE]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1421) 		if (changelink && (ip_tunnel_info_af(info) == AF_INET6)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1422) 			attrtype = IFLA_GENEVE_REMOTE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1423) 			goto change_notsup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1424) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1425) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1426) 		info->key.u.ipv4.dst =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1427) 			nla_get_in_addr(data[IFLA_GENEVE_REMOTE]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1428) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1429) 		if (ipv4_is_multicast(info->key.u.ipv4.dst)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1430) 			NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_REMOTE],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1431) 					    "Remote IPv4 address cannot be Multicast");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1432) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1433) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1434) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1435) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1436) 	if (data[IFLA_GENEVE_REMOTE6]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1437) #if IS_ENABLED(CONFIG_IPV6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1438) 		if (changelink && (ip_tunnel_info_af(info) == AF_INET)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1439) 			attrtype = IFLA_GENEVE_REMOTE6;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1440) 			goto change_notsup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1441) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1442) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1443) 		info->mode = IP_TUNNEL_INFO_IPV6;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1444) 		info->key.u.ipv6.dst =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1445) 			nla_get_in6_addr(data[IFLA_GENEVE_REMOTE6]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1446) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1447) 		if (ipv6_addr_type(&info->key.u.ipv6.dst) &
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1448) 		    IPV6_ADDR_LINKLOCAL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1449) 			NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_REMOTE6],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1450) 					    "Remote IPv6 address cannot be link-local");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1451) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1452) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1453) 		if (ipv6_addr_is_multicast(&info->key.u.ipv6.dst)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1454) 			NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_REMOTE6],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1455) 					    "Remote IPv6 address cannot be Multicast");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1456) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1457) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1458) 		info->key.tun_flags |= TUNNEL_CSUM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1459) 		cfg->use_udp6_rx_checksums = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1460) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1461) 		NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_REMOTE6],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1462) 				    "IPv6 support not enabled in the kernel");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1463) 		return -EPFNOSUPPORT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1464) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1465) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1466) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1467) 	if (data[IFLA_GENEVE_ID]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1468) 		__u32 vni;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1469) 		__u8 tvni[3];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1470) 		__be64 tunid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1471) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1472) 		vni = nla_get_u32(data[IFLA_GENEVE_ID]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1473) 		tvni[0] = (vni & 0x00ff0000) >> 16;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1474) 		tvni[1] = (vni & 0x0000ff00) >> 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1475) 		tvni[2] =  vni & 0x000000ff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1476) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1477) 		tunid = vni_to_tunnel_id(tvni);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1478) 		if (changelink && (tunid != info->key.tun_id)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1479) 			attrtype = IFLA_GENEVE_ID;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1480) 			goto change_notsup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1481) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1482) 		info->key.tun_id = tunid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1483) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1484) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1485) 	if (data[IFLA_GENEVE_TTL_INHERIT]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1486) 		if (nla_get_u8(data[IFLA_GENEVE_TTL_INHERIT]))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1487) 			cfg->ttl_inherit = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1488) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1489) 			cfg->ttl_inherit = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1490) 	} else if (data[IFLA_GENEVE_TTL]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1491) 		info->key.ttl = nla_get_u8(data[IFLA_GENEVE_TTL]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1492) 		cfg->ttl_inherit = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1493) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1494) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1495) 	if (data[IFLA_GENEVE_TOS])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1496) 		info->key.tos = nla_get_u8(data[IFLA_GENEVE_TOS]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1497) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1498) 	if (data[IFLA_GENEVE_DF])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1499) 		cfg->df = nla_get_u8(data[IFLA_GENEVE_DF]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1500) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1501) 	if (data[IFLA_GENEVE_LABEL]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1502) 		info->key.label = nla_get_be32(data[IFLA_GENEVE_LABEL]) &
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1503) 				  IPV6_FLOWLABEL_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1504) 		if (info->key.label && (!(info->mode & IP_TUNNEL_INFO_IPV6))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1505) 			NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_LABEL],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1506) 					    "Label attribute only applies for IPv6 Geneve devices");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1507) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1508) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1509) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1510) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1511) 	if (data[IFLA_GENEVE_PORT]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1512) 		if (changelink) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1513) 			attrtype = IFLA_GENEVE_PORT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1514) 			goto change_notsup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1515) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1516) 		info->key.tp_dst = nla_get_be16(data[IFLA_GENEVE_PORT]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1517) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1518) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1519) 	if (data[IFLA_GENEVE_COLLECT_METADATA]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1520) 		if (changelink) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1521) 			attrtype = IFLA_GENEVE_COLLECT_METADATA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1522) 			goto change_notsup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1523) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1524) 		cfg->collect_md = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1525) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1526) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1527) 	if (data[IFLA_GENEVE_UDP_CSUM]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1528) 		if (changelink) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1529) 			attrtype = IFLA_GENEVE_UDP_CSUM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1530) 			goto change_notsup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1531) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1532) 		if (nla_get_u8(data[IFLA_GENEVE_UDP_CSUM]))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1533) 			info->key.tun_flags |= TUNNEL_CSUM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1534) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1535) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1536) 	if (data[IFLA_GENEVE_UDP_ZERO_CSUM6_TX]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1537) #if IS_ENABLED(CONFIG_IPV6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1538) 		if (changelink) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1539) 			attrtype = IFLA_GENEVE_UDP_ZERO_CSUM6_TX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1540) 			goto change_notsup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1541) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1542) 		if (nla_get_u8(data[IFLA_GENEVE_UDP_ZERO_CSUM6_TX]))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1543) 			info->key.tun_flags &= ~TUNNEL_CSUM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1544) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1545) 		NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_UDP_ZERO_CSUM6_TX],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1546) 				    "IPv6 support not enabled in the kernel");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1547) 		return -EPFNOSUPPORT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1548) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1549) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1550) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1551) 	if (data[IFLA_GENEVE_UDP_ZERO_CSUM6_RX]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1552) #if IS_ENABLED(CONFIG_IPV6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1553) 		if (changelink) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1554) 			attrtype = IFLA_GENEVE_UDP_ZERO_CSUM6_RX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1555) 			goto change_notsup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1556) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1557) 		if (nla_get_u8(data[IFLA_GENEVE_UDP_ZERO_CSUM6_RX]))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1558) 			cfg->use_udp6_rx_checksums = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1559) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1560) 		NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_UDP_ZERO_CSUM6_RX],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1561) 				    "IPv6 support not enabled in the kernel");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1562) 		return -EPFNOSUPPORT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1563) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1564) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1565) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1566) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1567) change_notsup:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1568) 	NL_SET_ERR_MSG_ATTR(extack, data[attrtype],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1569) 			    "Changing VNI, Port, endpoint IP address family, external, and UDP checksum attributes are not supported");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1570) 	return -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1571) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1572) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1573) static void geneve_link_config(struct net_device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1574) 			       struct ip_tunnel_info *info, struct nlattr *tb[])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1575) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1576) 	struct geneve_dev *geneve = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1577) 	int ldev_mtu = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1578) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1579) 	if (tb[IFLA_MTU]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1580) 		geneve_change_mtu(dev, nla_get_u32(tb[IFLA_MTU]));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1581) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1582) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1583) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1584) 	switch (ip_tunnel_info_af(info)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1585) 	case AF_INET: {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1586) 		struct flowi4 fl4 = { .daddr = info->key.u.ipv4.dst };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1587) 		struct rtable *rt = ip_route_output_key(geneve->net, &fl4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1588) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1589) 		if (!IS_ERR(rt) && rt->dst.dev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1590) 			ldev_mtu = rt->dst.dev->mtu - GENEVE_IPV4_HLEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1591) 			ip_rt_put(rt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1592) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1593) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1594) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1595) #if IS_ENABLED(CONFIG_IPV6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1596) 	case AF_INET6: {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1597) 		struct rt6_info *rt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1598) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1599) 		if (!__in6_dev_get(dev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1600) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1601) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1602) 		rt = rt6_lookup(geneve->net, &info->key.u.ipv6.dst, NULL, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1603) 				NULL, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1604) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1605) 		if (rt && rt->dst.dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1606) 			ldev_mtu = rt->dst.dev->mtu - GENEVE_IPV6_HLEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1607) 		ip6_rt_put(rt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1608) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1609) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1610) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1611) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1612) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1613) 	if (ldev_mtu <= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1614) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1615) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1616) 	geneve_change_mtu(dev, ldev_mtu - info->options_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1617) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1618) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1619) static int geneve_newlink(struct net *net, struct net_device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1620) 			  struct nlattr *tb[], struct nlattr *data[],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1621) 			  struct netlink_ext_ack *extack)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1622) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1623) 	struct geneve_config cfg = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1624) 		.df = GENEVE_DF_UNSET,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1625) 		.use_udp6_rx_checksums = false,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1626) 		.ttl_inherit = false,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1627) 		.collect_md = false,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1628) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1629) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1630) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1631) 	init_tnl_info(&cfg.info, GENEVE_UDP_PORT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1632) 	err = geneve_nl2info(tb, data, extack, &cfg, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1633) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1634) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1635) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1636) 	err = geneve_configure(net, dev, extack, &cfg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1637) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1638) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1639) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1640) 	geneve_link_config(dev, &cfg.info, tb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1641) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1642) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1643) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1644) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1645) /* Quiesces the geneve device data path for both TX and RX.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1646)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1647)  * On transmit geneve checks for non-NULL geneve_sock before it proceeds.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1648)  * So, if we set that socket to NULL under RCU and wait for synchronize_net()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1649)  * to complete for the existing set of in-flight packets to be transmitted,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1650)  * then we would have quiesced the transmit data path. All the future packets
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1651)  * will get dropped until we unquiesce the data path.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1652)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1653)  * On receive geneve dereference the geneve_sock stashed in the socket. So,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1654)  * if we set that to NULL under RCU and wait for synchronize_net() to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1655)  * complete, then we would have quiesced the receive data path.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1656)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1657) static void geneve_quiesce(struct geneve_dev *geneve, struct geneve_sock **gs4,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1658) 			   struct geneve_sock **gs6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1659) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1660) 	*gs4 = rtnl_dereference(geneve->sock4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1661) 	rcu_assign_pointer(geneve->sock4, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1662) 	if (*gs4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1663) 		rcu_assign_sk_user_data((*gs4)->sock->sk, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1664) #if IS_ENABLED(CONFIG_IPV6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1665) 	*gs6 = rtnl_dereference(geneve->sock6);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1666) 	rcu_assign_pointer(geneve->sock6, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1667) 	if (*gs6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1668) 		rcu_assign_sk_user_data((*gs6)->sock->sk, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1669) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1670) 	*gs6 = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1671) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1672) 	synchronize_net();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1673) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1674) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1675) /* Resumes the geneve device data path for both TX and RX. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1676) static void geneve_unquiesce(struct geneve_dev *geneve, struct geneve_sock *gs4,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1677) 			     struct geneve_sock __maybe_unused *gs6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1678) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1679) 	rcu_assign_pointer(geneve->sock4, gs4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1680) 	if (gs4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1681) 		rcu_assign_sk_user_data(gs4->sock->sk, gs4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1682) #if IS_ENABLED(CONFIG_IPV6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1683) 	rcu_assign_pointer(geneve->sock6, gs6);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1684) 	if (gs6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1685) 		rcu_assign_sk_user_data(gs6->sock->sk, gs6);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1686) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1687) 	synchronize_net();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1688) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1689) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1690) static int geneve_changelink(struct net_device *dev, struct nlattr *tb[],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1691) 			     struct nlattr *data[],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1692) 			     struct netlink_ext_ack *extack)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1693) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1694) 	struct geneve_dev *geneve = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1695) 	struct geneve_sock *gs4, *gs6;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1696) 	struct geneve_config cfg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1697) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1698) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1699) 	/* If the geneve device is configured for metadata (or externally
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1700) 	 * controlled, for example, OVS), then nothing can be changed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1701) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1702) 	if (geneve->cfg.collect_md)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1703) 		return -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1704) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1705) 	/* Start with the existing info. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1706) 	memcpy(&cfg, &geneve->cfg, sizeof(cfg));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1707) 	err = geneve_nl2info(tb, data, extack, &cfg, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1708) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1709) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1710) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1711) 	if (!geneve_dst_addr_equal(&geneve->cfg.info, &cfg.info)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1712) 		dst_cache_reset(&cfg.info.dst_cache);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1713) 		geneve_link_config(dev, &cfg.info, tb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1714) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1715) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1716) 	geneve_quiesce(geneve, &gs4, &gs6);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1717) 	memcpy(&geneve->cfg, &cfg, sizeof(cfg));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1718) 	geneve_unquiesce(geneve, gs4, gs6);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1719) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1720) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1721) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1722) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1723) static void geneve_dellink(struct net_device *dev, struct list_head *head)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1724) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1725) 	struct geneve_dev *geneve = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1726) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1727) 	list_del(&geneve->next);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1728) 	unregister_netdevice_queue(dev, head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1729) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1730) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1731) static size_t geneve_get_size(const struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1732) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1733) 	return nla_total_size(sizeof(__u32)) +	/* IFLA_GENEVE_ID */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1734) 		nla_total_size(sizeof(struct in6_addr)) + /* IFLA_GENEVE_REMOTE{6} */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1735) 		nla_total_size(sizeof(__u8)) +  /* IFLA_GENEVE_TTL */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1736) 		nla_total_size(sizeof(__u8)) +  /* IFLA_GENEVE_TOS */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1737) 		nla_total_size(sizeof(__u8)) +	/* IFLA_GENEVE_DF */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1738) 		nla_total_size(sizeof(__be32)) +  /* IFLA_GENEVE_LABEL */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1739) 		nla_total_size(sizeof(__be16)) +  /* IFLA_GENEVE_PORT */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1740) 		nla_total_size(0) +	 /* IFLA_GENEVE_COLLECT_METADATA */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1741) 		nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_UDP_CSUM */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1742) 		nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_UDP_ZERO_CSUM6_TX */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1743) 		nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_UDP_ZERO_CSUM6_RX */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1744) 		nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_TTL_INHERIT */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1745) 		0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1746) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1747) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1748) static int geneve_fill_info(struct sk_buff *skb, const struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1749) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1750) 	struct geneve_dev *geneve = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1751) 	struct ip_tunnel_info *info = &geneve->cfg.info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1752) 	bool ttl_inherit = geneve->cfg.ttl_inherit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1753) 	bool metadata = geneve->cfg.collect_md;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1754) 	__u8 tmp_vni[3];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1755) 	__u32 vni;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1756) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1757) 	tunnel_id_to_vni(info->key.tun_id, tmp_vni);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1758) 	vni = (tmp_vni[0] << 16) | (tmp_vni[1] << 8) | tmp_vni[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1759) 	if (nla_put_u32(skb, IFLA_GENEVE_ID, vni))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1760) 		goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1761) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1762) 	if (!metadata && ip_tunnel_info_af(info) == AF_INET) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1763) 		if (nla_put_in_addr(skb, IFLA_GENEVE_REMOTE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1764) 				    info->key.u.ipv4.dst))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1765) 			goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1766) 		if (nla_put_u8(skb, IFLA_GENEVE_UDP_CSUM,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1767) 			       !!(info->key.tun_flags & TUNNEL_CSUM)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1768) 			goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1769) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1770) #if IS_ENABLED(CONFIG_IPV6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1771) 	} else if (!metadata) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1772) 		if (nla_put_in6_addr(skb, IFLA_GENEVE_REMOTE6,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1773) 				     &info->key.u.ipv6.dst))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1774) 			goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1775) 		if (nla_put_u8(skb, IFLA_GENEVE_UDP_ZERO_CSUM6_TX,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1776) 			       !(info->key.tun_flags & TUNNEL_CSUM)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1777) 			goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1778) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1779) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1780) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1781) 	if (nla_put_u8(skb, IFLA_GENEVE_TTL, info->key.ttl) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1782) 	    nla_put_u8(skb, IFLA_GENEVE_TOS, info->key.tos) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1783) 	    nla_put_be32(skb, IFLA_GENEVE_LABEL, info->key.label))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1784) 		goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1785) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1786) 	if (nla_put_u8(skb, IFLA_GENEVE_DF, geneve->cfg.df))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1787) 		goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1788) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1789) 	if (nla_put_be16(skb, IFLA_GENEVE_PORT, info->key.tp_dst))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1790) 		goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1791) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1792) 	if (metadata && nla_put_flag(skb, IFLA_GENEVE_COLLECT_METADATA))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1793) 		goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1794) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1795) #if IS_ENABLED(CONFIG_IPV6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1796) 	if (nla_put_u8(skb, IFLA_GENEVE_UDP_ZERO_CSUM6_RX,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1797) 		       !geneve->cfg.use_udp6_rx_checksums))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1798) 		goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1799) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1800) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1801) 	if (nla_put_u8(skb, IFLA_GENEVE_TTL_INHERIT, ttl_inherit))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1802) 		goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1803) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1804) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1805) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1806) nla_put_failure:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1807) 	return -EMSGSIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1808) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1809) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1810) static struct rtnl_link_ops geneve_link_ops __read_mostly = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1811) 	.kind		= "geneve",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1812) 	.maxtype	= IFLA_GENEVE_MAX,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1813) 	.policy		= geneve_policy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1814) 	.priv_size	= sizeof(struct geneve_dev),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1815) 	.setup		= geneve_setup,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1816) 	.validate	= geneve_validate,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1817) 	.newlink	= geneve_newlink,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1818) 	.changelink	= geneve_changelink,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1819) 	.dellink	= geneve_dellink,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1820) 	.get_size	= geneve_get_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1821) 	.fill_info	= geneve_fill_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1822) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1823) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1824) struct net_device *geneve_dev_create_fb(struct net *net, const char *name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1825) 					u8 name_assign_type, u16 dst_port)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1826) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1827) 	struct nlattr *tb[IFLA_MAX + 1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1828) 	struct net_device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1829) 	LIST_HEAD(list_kill);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1830) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1831) 	struct geneve_config cfg = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1832) 		.df = GENEVE_DF_UNSET,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1833) 		.use_udp6_rx_checksums = true,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1834) 		.ttl_inherit = false,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1835) 		.collect_md = true,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1836) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1837) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1838) 	memset(tb, 0, sizeof(tb));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1839) 	dev = rtnl_create_link(net, name, name_assign_type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1840) 			       &geneve_link_ops, tb, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1841) 	if (IS_ERR(dev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1842) 		return dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1843) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1844) 	init_tnl_info(&cfg.info, dst_port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1845) 	err = geneve_configure(net, dev, NULL, &cfg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1846) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1847) 		free_netdev(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1848) 		return ERR_PTR(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1849) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1850) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1851) 	/* openvswitch users expect packet sizes to be unrestricted,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1852) 	 * so set the largest MTU we can.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1853) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1854) 	err = geneve_change_mtu(dev, IP_MAX_MTU);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1855) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1856) 		goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1857) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1858) 	err = rtnl_configure_link(dev, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1859) 	if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1860) 		goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1861) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1862) 	return dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1863) err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1864) 	geneve_dellink(dev, &list_kill);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1865) 	unregister_netdevice_many(&list_kill);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1866) 	return ERR_PTR(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1867) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1868) EXPORT_SYMBOL_GPL(geneve_dev_create_fb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1869) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1870) static int geneve_netdevice_event(struct notifier_block *unused,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1871) 				  unsigned long event, void *ptr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1872) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1873) 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1874) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1875) 	if (event == NETDEV_UDP_TUNNEL_PUSH_INFO ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1876) 	    event == NETDEV_UDP_TUNNEL_DROP_INFO) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1877) 		geneve_offload_rx_ports(dev, event == NETDEV_UDP_TUNNEL_PUSH_INFO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1878) 	} else if (event == NETDEV_UNREGISTER) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1879) 		if (!dev->udp_tunnel_nic_info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1880) 			geneve_offload_rx_ports(dev, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1881) 	} else if (event == NETDEV_REGISTER) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1882) 		if (!dev->udp_tunnel_nic_info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1883) 			geneve_offload_rx_ports(dev, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1884) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1885) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1886) 	return NOTIFY_DONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1887) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1888) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1889) static struct notifier_block geneve_notifier_block __read_mostly = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1890) 	.notifier_call = geneve_netdevice_event,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1891) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1892) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1893) static __net_init int geneve_init_net(struct net *net)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1894) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1895) 	struct geneve_net *gn = net_generic(net, geneve_net_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1896) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1897) 	INIT_LIST_HEAD(&gn->geneve_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1898) 	INIT_LIST_HEAD(&gn->sock_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1899) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1900) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1901) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1902) static void geneve_destroy_tunnels(struct net *net, struct list_head *head)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1903) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1904) 	struct geneve_net *gn = net_generic(net, geneve_net_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1905) 	struct geneve_dev *geneve, *next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1906) 	struct net_device *dev, *aux;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1907) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1908) 	/* gather any geneve devices that were moved into this ns */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1909) 	for_each_netdev_safe(net, dev, aux)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1910) 		if (dev->rtnl_link_ops == &geneve_link_ops)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1911) 			unregister_netdevice_queue(dev, head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1912) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1913) 	/* now gather any other geneve devices that were created in this ns */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1914) 	list_for_each_entry_safe(geneve, next, &gn->geneve_list, next) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1915) 		/* If geneve->dev is in the same netns, it was already added
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1916) 		 * to the list by the previous loop.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1917) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1918) 		if (!net_eq(dev_net(geneve->dev), net))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1919) 			unregister_netdevice_queue(geneve->dev, head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1920) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1921) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1922) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1923) static void __net_exit geneve_exit_batch_net(struct list_head *net_list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1924) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1925) 	struct net *net;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1926) 	LIST_HEAD(list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1927) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1928) 	rtnl_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1929) 	list_for_each_entry(net, net_list, exit_list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1930) 		geneve_destroy_tunnels(net, &list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1931) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1932) 	/* unregister the devices gathered above */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1933) 	unregister_netdevice_many(&list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1934) 	rtnl_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1935) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1936) 	list_for_each_entry(net, net_list, exit_list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1937) 		const struct geneve_net *gn = net_generic(net, geneve_net_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1938) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1939) 		WARN_ON_ONCE(!list_empty(&gn->sock_list));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1940) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1941) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1942) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1943) static struct pernet_operations geneve_net_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1944) 	.init = geneve_init_net,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1945) 	.exit_batch = geneve_exit_batch_net,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1946) 	.id   = &geneve_net_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1947) 	.size = sizeof(struct geneve_net),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1948) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1949) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1950) static int __init geneve_init_module(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1951) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1952) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1953) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1954) 	rc = register_pernet_subsys(&geneve_net_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1955) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1956) 		goto out1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1957) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1958) 	rc = register_netdevice_notifier(&geneve_notifier_block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1959) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1960) 		goto out2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1961) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1962) 	rc = rtnl_link_register(&geneve_link_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1963) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1964) 		goto out3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1965) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1966) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1967) out3:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1968) 	unregister_netdevice_notifier(&geneve_notifier_block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1969) out2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1970) 	unregister_pernet_subsys(&geneve_net_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1971) out1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1972) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1973) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1974) late_initcall(geneve_init_module);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1975) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1976) static void __exit geneve_cleanup_module(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1977) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1978) 	rtnl_link_unregister(&geneve_link_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1979) 	unregister_netdevice_notifier(&geneve_notifier_block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1980) 	unregister_pernet_subsys(&geneve_net_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1981) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1982) module_exit(geneve_cleanup_module);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1983) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1984) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1985) MODULE_VERSION(GENEVE_NETDEV_VER);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1986) MODULE_AUTHOR("John W. Linville <linville@tuxdriver.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1987) MODULE_DESCRIPTION("Interface driver for GENEVE encapsulated traffic");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1988) MODULE_ALIAS_RTNL_LINK("geneve");