1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33#include <linux/tcp.h>
34#include <linux/ipv6.h>
35#include <net/route.h>
36#include <net/ip6_route.h>
37
38#include "libcxgb_cm.h"
39
40void
41cxgb_get_4tuple(struct cpl_pass_accept_req *req, enum chip_type type,
42 int *iptype, __u8 *local_ip, __u8 *peer_ip,
43 __be16 *local_port, __be16 *peer_port)
44{
45 int eth_len = (CHELSIO_CHIP_VERSION(type) <= CHELSIO_T5) ?
46 ETH_HDR_LEN_G(be32_to_cpu(req->hdr_len)) :
47 T6_ETH_HDR_LEN_G(be32_to_cpu(req->hdr_len));
48 int ip_len = (CHELSIO_CHIP_VERSION(type) <= CHELSIO_T5) ?
49 IP_HDR_LEN_G(be32_to_cpu(req->hdr_len)) :
50 T6_IP_HDR_LEN_G(be32_to_cpu(req->hdr_len));
51 struct iphdr *ip = (struct iphdr *)((u8 *)(req + 1) + eth_len);
52 struct ipv6hdr *ip6 = (struct ipv6hdr *)((u8 *)(req + 1) + eth_len);
53 struct tcphdr *tcp = (struct tcphdr *)
54 ((u8 *)(req + 1) + eth_len + ip_len);
55
56 if (ip->version == 4) {
57 pr_debug("%s saddr 0x%x daddr 0x%x sport %u dport %u\n",
58 __func__, ntohl(ip->saddr), ntohl(ip->daddr),
59 ntohs(tcp->source), ntohs(tcp->dest));
60 *iptype = 4;
61 memcpy(peer_ip, &ip->saddr, 4);
62 memcpy(local_ip, &ip->daddr, 4);
63 } else {
64 pr_debug("%s saddr %pI6 daddr %pI6 sport %u dport %u\n",
65 __func__, ip6->saddr.s6_addr, ip6->daddr.s6_addr,
66 ntohs(tcp->source), ntohs(tcp->dest));
67 *iptype = 6;
68 memcpy(peer_ip, ip6->saddr.s6_addr, 16);
69 memcpy(local_ip, ip6->daddr.s6_addr, 16);
70 }
71 *peer_port = tcp->source;
72 *local_port = tcp->dest;
73}
74EXPORT_SYMBOL(cxgb_get_4tuple);
75
76static bool
77cxgb_our_interface(struct cxgb4_lld_info *lldi,
78 struct net_device *(*get_real_dev)(struct net_device *),
79 struct net_device *egress_dev)
80{
81 int i;
82
83 egress_dev = get_real_dev(egress_dev);
84 for (i = 0; i < lldi->nports; i++)
85 if (lldi->ports[i] == egress_dev)
86 return true;
87 return false;
88}
89
90struct dst_entry *
91cxgb_find_route(struct cxgb4_lld_info *lldi,
92 struct net_device *(*get_real_dev)(struct net_device *),
93 __be32 local_ip, __be32 peer_ip, __be16 local_port,
94 __be16 peer_port, u8 tos)
95{
96 struct rtable *rt;
97 struct flowi4 fl4;
98 struct neighbour *n;
99
100 rt = ip_route_output_ports(&init_net, &fl4, NULL, peer_ip, local_ip,
101 peer_port, local_port, IPPROTO_TCP,
102 tos, 0);
103 if (IS_ERR(rt))
104 return NULL;
105 n = dst_neigh_lookup(&rt->dst, &peer_ip);
106 if (!n)
107 return NULL;
108 if (!cxgb_our_interface(lldi, get_real_dev, n->dev) &&
109 !(n->dev->flags & IFF_LOOPBACK)) {
110 neigh_release(n);
111 dst_release(&rt->dst);
112 return NULL;
113 }
114 neigh_release(n);
115 return &rt->dst;
116}
117EXPORT_SYMBOL(cxgb_find_route);
118
119struct dst_entry *
120cxgb_find_route6(struct cxgb4_lld_info *lldi,
121 struct net_device *(*get_real_dev)(struct net_device *),
122 __u8 *local_ip, __u8 *peer_ip, __be16 local_port,
123 __be16 peer_port, u8 tos, __u32 sin6_scope_id)
124{
125 struct dst_entry *dst = NULL;
126
127 if (IS_ENABLED(CONFIG_IPV6)) {
128 struct flowi6 fl6;
129
130 memset(&fl6, 0, sizeof(fl6));
131 memcpy(&fl6.daddr, peer_ip, 16);
132 memcpy(&fl6.saddr, local_ip, 16);
133 if (ipv6_addr_type(&fl6.daddr) & IPV6_ADDR_LINKLOCAL)
134 fl6.flowi6_oif = sin6_scope_id;
135 dst = ip6_route_output(&init_net, NULL, &fl6);
136 if (dst->error ||
137 (!cxgb_our_interface(lldi, get_real_dev,
138 ip6_dst_idev(dst)->dev) &&
139 !(ip6_dst_idev(dst)->dev->flags & IFF_LOOPBACK))) {
140 dst_release(dst);
141 return NULL;
142 }
143 }
144
145 return dst;
146}
147EXPORT_SYMBOL(cxgb_find_route6);
148