xref: /openbmc/linux/drivers/net/ethernet/chelsio/libcxgb/libcxgb_cm.c (revision 03ab8e6297acd1bc0eedaa050e2a1635c576fd11)
185e42b04SVarun Prakash /*
285e42b04SVarun Prakash  * Copyright (c) 2016 Chelsio Communications, Inc. All rights reserved.
385e42b04SVarun Prakash  *
485e42b04SVarun Prakash  * This software is available to you under a choice of one of two
585e42b04SVarun Prakash  * licenses.  You may choose to be licensed under the terms of the GNU
685e42b04SVarun Prakash  * General Public License (GPL) Version 2, available from the file
785e42b04SVarun Prakash  * COPYING in the main directory of this source tree, or the
885e42b04SVarun Prakash  * OpenIB.org BSD license below:
985e42b04SVarun Prakash  *
1085e42b04SVarun Prakash  *     Redistribution and use in source and binary forms, with or
1185e42b04SVarun Prakash  *     without modification, are permitted provided that the following
1285e42b04SVarun Prakash  *     conditions are met:
1385e42b04SVarun Prakash  *
1485e42b04SVarun Prakash  *      - Redistributions of source code must retain the above
1585e42b04SVarun Prakash  *	  copyright notice, this list of conditions and the following
1685e42b04SVarun Prakash  *	  disclaimer.
1785e42b04SVarun Prakash  *
1885e42b04SVarun Prakash  *      - Redistributions in binary form must reproduce the above
1985e42b04SVarun Prakash  *	  copyright notice, this list of conditions and the following
2085e42b04SVarun Prakash  *	  disclaimer in the documentation and/or other materials
2185e42b04SVarun Prakash  *	  provided with the distribution.
2285e42b04SVarun Prakash  *
2385e42b04SVarun Prakash  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
2485e42b04SVarun Prakash  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
2585e42b04SVarun Prakash  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
2685e42b04SVarun Prakash  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
2785e42b04SVarun Prakash  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
2885e42b04SVarun Prakash  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
2985e42b04SVarun Prakash  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
3085e42b04SVarun Prakash  * SOFTWARE.
3185e42b04SVarun Prakash  */
3285e42b04SVarun Prakash 
3385e42b04SVarun Prakash #include <linux/tcp.h>
3485e42b04SVarun Prakash #include <linux/ipv6.h>
35*a915deaaSGuillaume Nault #include <net/inet_ecn.h>
36804c2f3eSVarun Prakash #include <net/route.h>
3795554761SVarun Prakash #include <net/ip6_route.h>
3885e42b04SVarun Prakash 
3985e42b04SVarun Prakash #include "libcxgb_cm.h"
4085e42b04SVarun Prakash 
4185e42b04SVarun Prakash void
cxgb_get_4tuple(struct cpl_pass_accept_req * req,enum chip_type type,int * iptype,__u8 * local_ip,__u8 * peer_ip,__be16 * local_port,__be16 * peer_port)4285e42b04SVarun Prakash cxgb_get_4tuple(struct cpl_pass_accept_req *req, enum chip_type type,
4385e42b04SVarun Prakash 		int *iptype, __u8 *local_ip, __u8 *peer_ip,
4485e42b04SVarun Prakash 		__be16 *local_port, __be16 *peer_port)
4585e42b04SVarun Prakash {
4685e42b04SVarun Prakash 	int eth_len = (CHELSIO_CHIP_VERSION(type) <= CHELSIO_T5) ?
4785e42b04SVarun Prakash 		      ETH_HDR_LEN_G(be32_to_cpu(req->hdr_len)) :
4885e42b04SVarun Prakash 		      T6_ETH_HDR_LEN_G(be32_to_cpu(req->hdr_len));
4985e42b04SVarun Prakash 	int ip_len = (CHELSIO_CHIP_VERSION(type) <= CHELSIO_T5) ?
5085e42b04SVarun Prakash 		     IP_HDR_LEN_G(be32_to_cpu(req->hdr_len)) :
5185e42b04SVarun Prakash 		     T6_IP_HDR_LEN_G(be32_to_cpu(req->hdr_len));
5285e42b04SVarun Prakash 	struct iphdr *ip = (struct iphdr *)((u8 *)(req + 1) + eth_len);
5385e42b04SVarun Prakash 	struct ipv6hdr *ip6 = (struct ipv6hdr *)((u8 *)(req + 1) + eth_len);
5485e42b04SVarun Prakash 	struct tcphdr *tcp = (struct tcphdr *)
5585e42b04SVarun Prakash 			     ((u8 *)(req + 1) + eth_len + ip_len);
5685e42b04SVarun Prakash 
5785e42b04SVarun Prakash 	if (ip->version == 4) {
5885e42b04SVarun Prakash 		pr_debug("%s saddr 0x%x daddr 0x%x sport %u dport %u\n",
5985e42b04SVarun Prakash 			 __func__, ntohl(ip->saddr), ntohl(ip->daddr),
6085e42b04SVarun Prakash 			 ntohs(tcp->source), ntohs(tcp->dest));
6185e42b04SVarun Prakash 		*iptype = 4;
6285e42b04SVarun Prakash 		memcpy(peer_ip, &ip->saddr, 4);
6385e42b04SVarun Prakash 		memcpy(local_ip, &ip->daddr, 4);
6485e42b04SVarun Prakash 	} else {
6585e42b04SVarun Prakash 		pr_debug("%s saddr %pI6 daddr %pI6 sport %u dport %u\n",
6685e42b04SVarun Prakash 			 __func__, ip6->saddr.s6_addr, ip6->daddr.s6_addr,
6785e42b04SVarun Prakash 			 ntohs(tcp->source), ntohs(tcp->dest));
6885e42b04SVarun Prakash 		*iptype = 6;
6985e42b04SVarun Prakash 		memcpy(peer_ip, ip6->saddr.s6_addr, 16);
7085e42b04SVarun Prakash 		memcpy(local_ip, ip6->daddr.s6_addr, 16);
7185e42b04SVarun Prakash 	}
7285e42b04SVarun Prakash 	*peer_port = tcp->source;
7385e42b04SVarun Prakash 	*local_port = tcp->dest;
7485e42b04SVarun Prakash }
7585e42b04SVarun Prakash EXPORT_SYMBOL(cxgb_get_4tuple);
76804c2f3eSVarun Prakash 
77804c2f3eSVarun Prakash static bool
cxgb_our_interface(struct cxgb4_lld_info * lldi,struct net_device * (* get_real_dev)(struct net_device *),struct net_device * egress_dev)78804c2f3eSVarun Prakash cxgb_our_interface(struct cxgb4_lld_info *lldi,
79804c2f3eSVarun Prakash 		   struct net_device *(*get_real_dev)(struct net_device *),
80804c2f3eSVarun Prakash 		   struct net_device *egress_dev)
81804c2f3eSVarun Prakash {
82804c2f3eSVarun Prakash 	int i;
83804c2f3eSVarun Prakash 
84804c2f3eSVarun Prakash 	egress_dev = get_real_dev(egress_dev);
85804c2f3eSVarun Prakash 	for (i = 0; i < lldi->nports; i++)
86804c2f3eSVarun Prakash 		if (lldi->ports[i] == egress_dev)
87804c2f3eSVarun Prakash 			return true;
88804c2f3eSVarun Prakash 	return false;
89804c2f3eSVarun Prakash }
90804c2f3eSVarun Prakash 
91804c2f3eSVarun Prakash struct dst_entry *
cxgb_find_route(struct cxgb4_lld_info * lldi,struct net_device * (* get_real_dev)(struct net_device *),__be32 local_ip,__be32 peer_ip,__be16 local_port,__be16 peer_port,u8 tos)92804c2f3eSVarun Prakash cxgb_find_route(struct cxgb4_lld_info *lldi,
93804c2f3eSVarun Prakash 		struct net_device *(*get_real_dev)(struct net_device *),
94804c2f3eSVarun Prakash 		__be32 local_ip, __be32 peer_ip, __be16 local_port,
95804c2f3eSVarun Prakash 		__be16 peer_port, u8 tos)
96804c2f3eSVarun Prakash {
97804c2f3eSVarun Prakash 	struct rtable *rt;
98804c2f3eSVarun Prakash 	struct flowi4 fl4;
99804c2f3eSVarun Prakash 	struct neighbour *n;
100804c2f3eSVarun Prakash 
101804c2f3eSVarun Prakash 	rt = ip_route_output_ports(&init_net, &fl4, NULL, peer_ip, local_ip,
102804c2f3eSVarun Prakash 				   peer_port, local_port, IPPROTO_TCP,
103*a915deaaSGuillaume Nault 				   tos & ~INET_ECN_MASK, 0);
104804c2f3eSVarun Prakash 	if (IS_ERR(rt))
105804c2f3eSVarun Prakash 		return NULL;
106804c2f3eSVarun Prakash 	n = dst_neigh_lookup(&rt->dst, &peer_ip);
107804c2f3eSVarun Prakash 	if (!n)
108804c2f3eSVarun Prakash 		return NULL;
109804c2f3eSVarun Prakash 	if (!cxgb_our_interface(lldi, get_real_dev, n->dev) &&
110804c2f3eSVarun Prakash 	    !(n->dev->flags & IFF_LOOPBACK)) {
111804c2f3eSVarun Prakash 		neigh_release(n);
112804c2f3eSVarun Prakash 		dst_release(&rt->dst);
113804c2f3eSVarun Prakash 		return NULL;
114804c2f3eSVarun Prakash 	}
115804c2f3eSVarun Prakash 	neigh_release(n);
116804c2f3eSVarun Prakash 	return &rt->dst;
117804c2f3eSVarun Prakash }
118804c2f3eSVarun Prakash EXPORT_SYMBOL(cxgb_find_route);
11995554761SVarun Prakash 
12095554761SVarun Prakash struct dst_entry *
cxgb_find_route6(struct cxgb4_lld_info * lldi,struct net_device * (* get_real_dev)(struct net_device *),__u8 * local_ip,__u8 * peer_ip,__be16 local_port,__be16 peer_port,u8 tos,__u32 sin6_scope_id)12195554761SVarun Prakash cxgb_find_route6(struct cxgb4_lld_info *lldi,
12295554761SVarun Prakash 		 struct net_device *(*get_real_dev)(struct net_device *),
12395554761SVarun Prakash 		 __u8 *local_ip, __u8 *peer_ip, __be16 local_port,
12495554761SVarun Prakash 		 __be16 peer_port, u8 tos, __u32 sin6_scope_id)
12595554761SVarun Prakash {
12695554761SVarun Prakash 	struct dst_entry *dst = NULL;
12795554761SVarun Prakash 
12895554761SVarun Prakash 	if (IS_ENABLED(CONFIG_IPV6)) {
12995554761SVarun Prakash 		struct flowi6 fl6;
13095554761SVarun Prakash 
13195554761SVarun Prakash 		memset(&fl6, 0, sizeof(fl6));
13295554761SVarun Prakash 		memcpy(&fl6.daddr, peer_ip, 16);
13395554761SVarun Prakash 		memcpy(&fl6.saddr, local_ip, 16);
13495554761SVarun Prakash 		if (ipv6_addr_type(&fl6.daddr) & IPV6_ADDR_LINKLOCAL)
13595554761SVarun Prakash 			fl6.flowi6_oif = sin6_scope_id;
13695554761SVarun Prakash 		dst = ip6_route_output(&init_net, NULL, &fl6);
137a9a8cdb3SVarun Prakash 		if (dst->error ||
138a9a8cdb3SVarun Prakash 		    (!cxgb_our_interface(lldi, get_real_dev,
13995554761SVarun Prakash 					 ip6_dst_idev(dst)->dev) &&
140a9a8cdb3SVarun Prakash 		     !(ip6_dst_idev(dst)->dev->flags & IFF_LOOPBACK))) {
14195554761SVarun Prakash 			dst_release(dst);
142a9a8cdb3SVarun Prakash 			return NULL;
14395554761SVarun Prakash 		}
14495554761SVarun Prakash 	}
14595554761SVarun Prakash 
14695554761SVarun Prakash 	return dst;
14795554761SVarun Prakash }
14895554761SVarun Prakash EXPORT_SYMBOL(cxgb_find_route6);
149