1119 lines · 28.3 KB · C
RawDownload
// SPDX-License-Identifier: GPL-2.0-only/* * Copyright (c) 2007-2014 Nicira, Inc. */ #include <linux/uaccess.h>#include <linux/netdevice.h>#include <linux/etherdevice.h>#include <linux/if_ether.h>#include <linux/if_vlan.h>#include <net/llc_pdu.h>#include <linux/kernel.h>#include <linux/jhash.h>#include <linux/jiffies.h>#include <linux/llc.h>#include <linux/module.h>#include <linux/in.h>#include <linux/rcupdate.h>#include <linux/cpumask.h>#include <linux/if_arp.h>#include <linux/ip.h>#include <linux/ipv6.h>#include <linux/mpls.h>#include <linux/sctp.h>#include <linux/smp.h>#include <linux/tcp.h>#include <linux/udp.h>#include <linux/icmp.h>#include <linux/icmpv6.h>#include <linux/rculist.h>#include <net/ip.h>#include <net/ip_tunnels.h>#include <net/ipv6.h>#include <net/mpls.h>#include <net/ndisc.h>#include <net/nsh.h>#include <net/pkt_cls.h>#include <net/netfilter/nf_conntrack_zones.h> #include "conntrack.h"#include "datapath.h"#include "flow.h"#include "flow_netlink.h"#include "vport.h" u64 ovs_flow_used_time(unsigned long flow_jiffies){	struct timespec64 cur_ts;	u64 cur_ms, idle_ms; 	ktime_get_ts64(&cur_ts);	idle_ms = jiffies_to_msecs(jiffies - flow_jiffies);	cur_ms = (u64)(u32)cur_ts.tv_sec * MSEC_PER_SEC +		 cur_ts.tv_nsec / NSEC_PER_MSEC; 	return cur_ms - idle_ms;} #define TCP_FLAGS_BE16(tp) (*(__be16 *)&tcp_flag_word(tp) & htons(0x0FFF)) void ovs_flow_stats_update(struct sw_flow *flow, __be16 tcp_flags,			   const struct sk_buff *skb){	struct sw_flow_stats *stats;	unsigned int cpu = smp_processor_id();	int len = skb->len + (skb_vlan_tag_present(skb) ? VLAN_HLEN : 0); 	stats = rcu_dereference(flow->stats[cpu]); 	/* Check if already have CPU-specific stats. */	if (likely(stats)) {		spin_lock(&stats->lock);		/* Mark if we write on the pre-allocated stats. */		if (cpu == 0 && unlikely(flow->stats_last_writer != cpu))			flow->stats_last_writer = cpu;	} else {		stats = rcu_dereference(flow->stats[0]); /* Pre-allocated. */		spin_lock(&stats->lock); 		/* If the current CPU is the only writer on the		 * pre-allocated stats keep using them.		 */		if (unlikely(flow->stats_last_writer != cpu)) {			/* A previous locker may have already allocated the			 * stats, so we need to check again.  If CPU-specific			 * stats were already allocated, we update the pre-			 * allocated stats as we have already locked them.			 */			if (likely(flow->stats_last_writer != -1) &&			    likely(!rcu_access_pointer(flow->stats[cpu]))) {				/* Try to allocate CPU-specific stats. */				struct sw_flow_stats *new_stats; 				new_stats =					kmem_cache_alloc_node(flow_stats_cache,							      GFP_NOWAIT |							      __GFP_THISNODE |							      __GFP_NOWARN |							      __GFP_NOMEMALLOC,							      numa_node_id());				if (likely(new_stats)) {					new_stats->used = jiffies;					new_stats->packet_count = 1;					new_stats->byte_count = len;					new_stats->tcp_flags = tcp_flags;					spin_lock_init(&new_stats->lock); 					rcu_assign_pointer(flow->stats[cpu],							   new_stats);					cpumask_set_cpu(cpu,							flow->cpu_used_mask);					goto unlock;				}			}			flow->stats_last_writer = cpu;		}	} 	stats->used = jiffies;	stats->packet_count++;	stats->byte_count += len;	stats->tcp_flags |= tcp_flags;unlock:	spin_unlock(&stats->lock);} /* Must be called with rcu_read_lock or ovs_mutex. */void ovs_flow_stats_get(const struct sw_flow *flow,			struct ovs_flow_stats *ovs_stats,			unsigned long *used, __be16 *tcp_flags){	unsigned int cpu; 	*used = 0;	*tcp_flags = 0;	memset(ovs_stats, 0, sizeof(*ovs_stats)); 	for_each_cpu(cpu, flow->cpu_used_mask) {		struct sw_flow_stats *stats = rcu_dereference_ovsl(flow->stats[cpu]); 		if (stats) {			/* Local CPU may write on non-local stats, so we must			 * block bottom-halves here.			 */			spin_lock_bh(&stats->lock);			if (!*used || time_after(stats->used, *used))				*used = stats->used;			*tcp_flags |= stats->tcp_flags;			ovs_stats->n_packets += stats->packet_count;			ovs_stats->n_bytes += stats->byte_count;			spin_unlock_bh(&stats->lock);		}	}} /* Called with ovs_mutex. */void ovs_flow_stats_clear(struct sw_flow *flow){	unsigned int cpu; 	for_each_cpu(cpu, flow->cpu_used_mask) {		struct sw_flow_stats *stats = ovsl_dereference(flow->stats[cpu]); 		if (stats) {			spin_lock_bh(&stats->lock);			stats->used = 0;			stats->packet_count = 0;			stats->byte_count = 0;			stats->tcp_flags = 0;			spin_unlock_bh(&stats->lock);		}	}} static int check_header(struct sk_buff *skb, int len){	if (unlikely(skb->len < len))		return -EINVAL;	if (unlikely(!pskb_may_pull(skb, len)))		return -ENOMEM;	return 0;} static bool arphdr_ok(struct sk_buff *skb){	return pskb_may_pull(skb, skb_network_offset(skb) +				  sizeof(struct arp_eth_header));} static int check_iphdr(struct sk_buff *skb){	unsigned int nh_ofs = skb_network_offset(skb);	unsigned int ip_len;	int err; 	err = check_header(skb, nh_ofs + sizeof(struct iphdr));	if (unlikely(err))		return err; 	ip_len = ip_hdrlen(skb);	if (unlikely(ip_len < sizeof(struct iphdr) ||		     skb->len < nh_ofs + ip_len))		return -EINVAL; 	skb_set_transport_header(skb, nh_ofs + ip_len);	return 0;} static bool tcphdr_ok(struct sk_buff *skb){	int th_ofs = skb_transport_offset(skb);	int tcp_len; 	if (unlikely(!pskb_may_pull(skb, th_ofs + sizeof(struct tcphdr))))		return false; 	tcp_len = tcp_hdrlen(skb);	if (unlikely(tcp_len < sizeof(struct tcphdr) ||		     skb->len < th_ofs + tcp_len))		return false; 	return true;} static bool udphdr_ok(struct sk_buff *skb){	return pskb_may_pull(skb, skb_transport_offset(skb) +				  sizeof(struct udphdr));} static bool sctphdr_ok(struct sk_buff *skb){	return pskb_may_pull(skb, skb_transport_offset(skb) +				  sizeof(struct sctphdr));} static bool icmphdr_ok(struct sk_buff *skb){	return pskb_may_pull(skb, skb_transport_offset(skb) +				  sizeof(struct icmphdr));} /** * get_ipv6_ext_hdrs() - Parses packet and sets IPv6 extension header flags. * * @skb: buffer where extension header data starts in packet * @nh: ipv6 header * @ext_hdrs: flags are stored here * * OFPIEH12_UNREP is set if more than one of a given IPv6 extension header * is unexpectedly encountered. (Two destination options headers may be * expected and would not cause this bit to be set.) * * OFPIEH12_UNSEQ is set if IPv6 extension headers were not in the order * preferred (but not required) by RFC 2460: * * When more than one extension header is used in the same packet, it is * recommended that those headers appear in the following order: *      IPv6 header *      Hop-by-Hop Options header *      Destination Options header *      Routing header *      Fragment header *      Authentication header *      Encapsulating Security Payload header *      Destination Options header *      upper-layer header */static void get_ipv6_ext_hdrs(struct sk_buff *skb, struct ipv6hdr *nh,			      u16 *ext_hdrs){	u8 next_type = nh->nexthdr;	unsigned int start = skb_network_offset(skb) + sizeof(struct ipv6hdr);	int dest_options_header_count = 0; 	*ext_hdrs = 0; 	while (ipv6_ext_hdr(next_type)) {		struct ipv6_opt_hdr _hdr, *hp; 		switch (next_type) {		case IPPROTO_NONE:			*ext_hdrs |= OFPIEH12_NONEXT;			/* stop parsing */			return; 		case IPPROTO_ESP:			if (*ext_hdrs & OFPIEH12_ESP)				*ext_hdrs |= OFPIEH12_UNREP;			if ((*ext_hdrs & ~(OFPIEH12_HOP | OFPIEH12_DEST |					   OFPIEH12_ROUTER | IPPROTO_FRAGMENT |					   OFPIEH12_AUTH | OFPIEH12_UNREP)) ||			    dest_options_header_count >= 2) {				*ext_hdrs |= OFPIEH12_UNSEQ;			}			*ext_hdrs |= OFPIEH12_ESP;			break; 		case IPPROTO_AH:			if (*ext_hdrs & OFPIEH12_AUTH)				*ext_hdrs |= OFPIEH12_UNREP;			if ((*ext_hdrs &			     ~(OFPIEH12_HOP | OFPIEH12_DEST | OFPIEH12_ROUTER |			       IPPROTO_FRAGMENT | OFPIEH12_UNREP)) ||			    dest_options_header_count >= 2) {				*ext_hdrs |= OFPIEH12_UNSEQ;			}			*ext_hdrs |= OFPIEH12_AUTH;			break; 		case IPPROTO_DSTOPTS:			if (dest_options_header_count == 0) {				if (*ext_hdrs &				    ~(OFPIEH12_HOP | OFPIEH12_UNREP))					*ext_hdrs |= OFPIEH12_UNSEQ;				*ext_hdrs |= OFPIEH12_DEST;			} else if (dest_options_header_count == 1) {				if (*ext_hdrs &				    ~(OFPIEH12_HOP | OFPIEH12_DEST |				      OFPIEH12_ROUTER | OFPIEH12_FRAG |				      OFPIEH12_AUTH | OFPIEH12_ESP |				      OFPIEH12_UNREP)) {					*ext_hdrs |= OFPIEH12_UNSEQ;				}			} else {				*ext_hdrs |= OFPIEH12_UNREP;			}			dest_options_header_count++;			break; 		case IPPROTO_FRAGMENT:			if (*ext_hdrs & OFPIEH12_FRAG)				*ext_hdrs |= OFPIEH12_UNREP;			if ((*ext_hdrs & ~(OFPIEH12_HOP |					   OFPIEH12_DEST |					   OFPIEH12_ROUTER |					   OFPIEH12_UNREP)) ||			    dest_options_header_count >= 2) {				*ext_hdrs |= OFPIEH12_UNSEQ;			}			*ext_hdrs |= OFPIEH12_FRAG;			break; 		case IPPROTO_ROUTING:			if (*ext_hdrs & OFPIEH12_ROUTER)				*ext_hdrs |= OFPIEH12_UNREP;			if ((*ext_hdrs & ~(OFPIEH12_HOP |					   OFPIEH12_DEST |					   OFPIEH12_UNREP)) ||			    dest_options_header_count >= 2) {				*ext_hdrs |= OFPIEH12_UNSEQ;			}			*ext_hdrs |= OFPIEH12_ROUTER;			break; 		case IPPROTO_HOPOPTS:			if (*ext_hdrs & OFPIEH12_HOP)				*ext_hdrs |= OFPIEH12_UNREP;			/* OFPIEH12_HOP is set to 1 if a hop-by-hop IPv6			 * extension header is present as the first			 * extension header in the packet.			 */			if (*ext_hdrs == 0)				*ext_hdrs |= OFPIEH12_HOP;			else				*ext_hdrs |= OFPIEH12_UNSEQ;			break; 		default:			return;		} 		hp = skb_header_pointer(skb, start, sizeof(_hdr), &_hdr);		if (!hp)			break;		next_type = hp->nexthdr;		start += ipv6_optlen(hp);	}} static int parse_ipv6hdr(struct sk_buff *skb, struct sw_flow_key *key){	unsigned short frag_off;	unsigned int payload_ofs = 0;	unsigned int nh_ofs = skb_network_offset(skb);	unsigned int nh_len;	struct ipv6hdr *nh;	int err, nexthdr, flags = 0; 	err = check_header(skb, nh_ofs + sizeof(*nh));	if (unlikely(err))		return err; 	nh = ipv6_hdr(skb); 	get_ipv6_ext_hdrs(skb, nh, &key->ipv6.exthdrs); 	key->ip.proto = NEXTHDR_NONE;	key->ip.tos = ipv6_get_dsfield(nh);	key->ip.ttl = nh->hop_limit;	key->ipv6.label = *(__be32 *)nh & htonl(IPV6_FLOWINFO_FLOWLABEL);	key->ipv6.addr.src = nh->saddr;	key->ipv6.addr.dst = nh->daddr; 	nexthdr = ipv6_find_hdr(skb, &payload_ofs, -1, &frag_off, &flags);	if (flags & IP6_FH_F_FRAG) {		if (frag_off) {			key->ip.frag = OVS_FRAG_TYPE_LATER;			key->ip.proto = NEXTHDR_FRAGMENT;			return 0;		}		key->ip.frag = OVS_FRAG_TYPE_FIRST;	} else {		key->ip.frag = OVS_FRAG_TYPE_NONE;	} 	/* Delayed handling of error in ipv6_find_hdr() as it	 * always sets flags and frag_off to a valid value which may be	 * used to set key->ip.frag above.	 */	if (unlikely(nexthdr < 0))		return -EPROTO; 	nh_len = payload_ofs - nh_ofs;	skb_set_transport_header(skb, nh_ofs + nh_len);	key->ip.proto = nexthdr;	return nh_len;} static bool icmp6hdr_ok(struct sk_buff *skb){	return pskb_may_pull(skb, skb_transport_offset(skb) +				  sizeof(struct icmp6hdr));} /** * parse_vlan_tag - Parse vlan tag from vlan header. * @skb: skb containing frame to parse * @key_vh: pointer to parsed vlan tag * @untag_vlan: should the vlan header be removed from the frame * * Return: ERROR on memory error. * %0 if it encounters a non-vlan or incomplete packet. * %1 after successfully parsing vlan tag. */static int parse_vlan_tag(struct sk_buff *skb, struct vlan_head *key_vh,			  bool untag_vlan){	struct vlan_head *vh = (struct vlan_head *)skb->data; 	if (likely(!eth_type_vlan(vh->tpid)))		return 0; 	if (unlikely(skb->len < sizeof(struct vlan_head) + sizeof(__be16)))		return 0; 	if (unlikely(!pskb_may_pull(skb, sizeof(struct vlan_head) +				 sizeof(__be16))))		return -ENOMEM; 	vh = (struct vlan_head *)skb->data;	key_vh->tci = vh->tci | htons(VLAN_CFI_MASK);	key_vh->tpid = vh->tpid; 	if (unlikely(untag_vlan)) {		int offset = skb->data - skb_mac_header(skb);		u16 tci;		int err; 		__skb_push(skb, offset);		err = __skb_vlan_pop(skb, &tci);		__skb_pull(skb, offset);		if (err)			return err;		__vlan_hwaccel_put_tag(skb, key_vh->tpid, tci);	} else {		__skb_pull(skb, sizeof(struct vlan_head));	}	return 1;} static void clear_vlan(struct sw_flow_key *key){	key->eth.vlan.tci = 0;	key->eth.vlan.tpid = 0;	key->eth.cvlan.tci = 0;	key->eth.cvlan.tpid = 0;} static int parse_vlan(struct sk_buff *skb, struct sw_flow_key *key){	int res; 	if (skb_vlan_tag_present(skb)) {		key->eth.vlan.tci = htons(skb->vlan_tci) | htons(VLAN_CFI_MASK);		key->eth.vlan.tpid = skb->vlan_proto;	} else {		/* Parse outer vlan tag in the non-accelerated case. */		res = parse_vlan_tag(skb, &key->eth.vlan, true);		if (res <= 0)

Lines 1–500 of 1119.