libcxgb_cm.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. * Copyright (c) 2016 Chelsio Communications, Inc. All rights reserved.
  3. *
  4. * This software is available to you under a choice of one of two
  5. * licenses. You may choose to be licensed under the terms of the GNU
  6. * General Public License (GPL) Version 2, available from the file
  7. * COPYING in the main directory of this source tree, or the
  8. * OpenIB.org BSD license below:
  9. *
  10. * Redistribution and use in source and binary forms, with or
  11. * without modification, are permitted provided that the following
  12. * conditions are met:
  13. *
  14. * - Redistributions of source code must retain the above
  15. * copyright notice, this list of conditions and the following
  16. * disclaimer.
  17. *
  18. * - Redistributions in binary form must reproduce the above
  19. * copyright notice, this list of conditions and the following
  20. * disclaimer in the documentation and/or other materials
  21. * provided with the distribution.
  22. *
  23. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  27. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  28. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  29. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  30. * SOFTWARE.
  31. */
  32. #include <linux/tcp.h>
  33. #include <linux/ipv6.h>
  34. #include <net/route.h>
  35. #include <net/ip6_route.h>
  36. #include "libcxgb_cm.h"
  37. void
  38. cxgb_get_4tuple(struct cpl_pass_accept_req *req, enum chip_type type,
  39. int *iptype, __u8 *local_ip, __u8 *peer_ip,
  40. __be16 *local_port, __be16 *peer_port)
  41. {
  42. int eth_len = (CHELSIO_CHIP_VERSION(type) <= CHELSIO_T5) ?
  43. ETH_HDR_LEN_G(be32_to_cpu(req->hdr_len)) :
  44. T6_ETH_HDR_LEN_G(be32_to_cpu(req->hdr_len));
  45. int ip_len = (CHELSIO_CHIP_VERSION(type) <= CHELSIO_T5) ?
  46. IP_HDR_LEN_G(be32_to_cpu(req->hdr_len)) :
  47. T6_IP_HDR_LEN_G(be32_to_cpu(req->hdr_len));
  48. struct iphdr *ip = (struct iphdr *)((u8 *)(req + 1) + eth_len);
  49. struct ipv6hdr *ip6 = (struct ipv6hdr *)((u8 *)(req + 1) + eth_len);
  50. struct tcphdr *tcp = (struct tcphdr *)
  51. ((u8 *)(req + 1) + eth_len + ip_len);
  52. if (ip->version == 4) {
  53. pr_debug("%s saddr 0x%x daddr 0x%x sport %u dport %u\n",
  54. __func__, ntohl(ip->saddr), ntohl(ip->daddr),
  55. ntohs(tcp->source), ntohs(tcp->dest));
  56. *iptype = 4;
  57. memcpy(peer_ip, &ip->saddr, 4);
  58. memcpy(local_ip, &ip->daddr, 4);
  59. } else {
  60. pr_debug("%s saddr %pI6 daddr %pI6 sport %u dport %u\n",
  61. __func__, ip6->saddr.s6_addr, ip6->daddr.s6_addr,
  62. ntohs(tcp->source), ntohs(tcp->dest));
  63. *iptype = 6;
  64. memcpy(peer_ip, ip6->saddr.s6_addr, 16);
  65. memcpy(local_ip, ip6->daddr.s6_addr, 16);
  66. }
  67. *peer_port = tcp->source;
  68. *local_port = tcp->dest;
  69. }
  70. EXPORT_SYMBOL(cxgb_get_4tuple);
  71. static bool
  72. cxgb_our_interface(struct cxgb4_lld_info *lldi,
  73. struct net_device *(*get_real_dev)(struct net_device *),
  74. struct net_device *egress_dev)
  75. {
  76. int i;
  77. egress_dev = get_real_dev(egress_dev);
  78. for (i = 0; i < lldi->nports; i++)
  79. if (lldi->ports[i] == egress_dev)
  80. return true;
  81. return false;
  82. }
  83. struct dst_entry *
  84. cxgb_find_route(struct cxgb4_lld_info *lldi,
  85. struct net_device *(*get_real_dev)(struct net_device *),
  86. __be32 local_ip, __be32 peer_ip, __be16 local_port,
  87. __be16 peer_port, u8 tos)
  88. {
  89. struct rtable *rt;
  90. struct flowi4 fl4;
  91. struct neighbour *n;
  92. rt = ip_route_output_ports(&init_net, &fl4, NULL, peer_ip, local_ip,
  93. peer_port, local_port, IPPROTO_TCP,
  94. tos, 0);
  95. if (IS_ERR(rt))
  96. return NULL;
  97. n = dst_neigh_lookup(&rt->dst, &peer_ip);
  98. if (!n)
  99. return NULL;
  100. if (!cxgb_our_interface(lldi, get_real_dev, n->dev) &&
  101. !(n->dev->flags & IFF_LOOPBACK)) {
  102. neigh_release(n);
  103. dst_release(&rt->dst);
  104. return NULL;
  105. }
  106. neigh_release(n);
  107. return &rt->dst;
  108. }
  109. EXPORT_SYMBOL(cxgb_find_route);
  110. struct dst_entry *
  111. cxgb_find_route6(struct cxgb4_lld_info *lldi,
  112. struct net_device *(*get_real_dev)(struct net_device *),
  113. __u8 *local_ip, __u8 *peer_ip, __be16 local_port,
  114. __be16 peer_port, u8 tos, __u32 sin6_scope_id)
  115. {
  116. struct dst_entry *dst = NULL;
  117. if (IS_ENABLED(CONFIG_IPV6)) {
  118. struct flowi6 fl6;
  119. memset(&fl6, 0, sizeof(fl6));
  120. memcpy(&fl6.daddr, peer_ip, 16);
  121. memcpy(&fl6.saddr, local_ip, 16);
  122. if (ipv6_addr_type(&fl6.daddr) & IPV6_ADDR_LINKLOCAL)
  123. fl6.flowi6_oif = sin6_scope_id;
  124. dst = ip6_route_output(&init_net, NULL, &fl6);
  125. if (dst->error ||
  126. (!cxgb_our_interface(lldi, get_real_dev,
  127. ip6_dst_idev(dst)->dev) &&
  128. !(ip6_dst_idev(dst)->dev->flags & IFF_LOOPBACK))) {
  129. dst_release(dst);
  130. return NULL;
  131. }
  132. }
  133. return dst;
  134. }
  135. EXPORT_SYMBOL(cxgb_find_route6);