1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 2012 Chelsio Communications, Inc. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28 29 #include "opt_inet.h" 30 #include "opt_inet6.h" 31 32 #include <sys/param.h> 33 #include <sys/systm.h> 34 #include <sys/eventhandler.h> 35 #include <sys/mbuf.h> 36 #include <sys/socket.h> 37 #include <sys/socketvar.h> 38 #include <sys/sockopt.h> 39 #include <net/if.h> 40 #include <net/if_var.h> 41 #include <net/if_private.h> 42 #include <net/route.h> 43 #include <net/route/nhop.h> 44 #include <netinet/in.h> 45 #include <netinet/in_pcb.h> 46 #include <netinet/in_fib.h> 47 #include <netinet6/in6_fib.h> 48 #include <netinet/tcp.h> 49 #include <netinet/tcp_offload.h> 50 #define TCPOUTFLAGS 51 #include <netinet/tcp_fsm.h> 52 #include <netinet/tcp_var.h> 53 #include <netinet/toecore.h> 54 55 int registered_toedevs; 56 57 /* 58 * Provide an opportunity for a TOE driver to offload. 59 */ 60 int 61 tcp_offload_connect(struct socket *so, struct sockaddr *nam) 62 { 63 struct ifnet *ifp; 64 struct toedev *tod; 65 struct nhop_object *nh; 66 struct epoch_tracker et; 67 int error = EOPNOTSUPP; 68 69 INP_WLOCK_ASSERT(sotoinpcb(so)); 70 KASSERT(nam->sa_family == AF_INET || nam->sa_family == AF_INET6, 71 ("%s: called with sa_family %d", __func__, nam->sa_family)); 72 73 if (registered_toedevs == 0) 74 return (error); 75 76 NET_EPOCH_ENTER(et); 77 nh = NULL; 78 #ifdef INET 79 if (nam->sa_family == AF_INET) 80 nh = fib4_lookup(0, ((struct sockaddr_in *)nam)->sin_addr, 81 NHR_NONE, 0, 0); 82 #endif 83 #if defined(INET) && defined(INET6) 84 else 85 #endif 86 #ifdef INET6 87 if (nam->sa_family == AF_INET6) 88 nh = fib6_lookup(0, &((struct sockaddr_in6 *)nam)->sin6_addr, 89 NHR_NONE, 0, 0); 90 #endif 91 if (nh == NULL) { 92 NET_EPOCH_EXIT(et); 93 return (EHOSTUNREACH); 94 } 95 96 ifp = nh->nh_ifp; 97 98 if (nam->sa_family == AF_INET && !(ifp->if_capenable & IFCAP_TOE4)) 99 goto done; 100 if (nam->sa_family == AF_INET6 && !(ifp->if_capenable & IFCAP_TOE6)) 101 goto done; 102 103 tod = TOEDEV(ifp); 104 if (tod != NULL) 105 error = tod->tod_connect(tod, so, nh, nam); 106 done: 107 NET_EPOCH_EXIT(et); 108 return (error); 109 } 110 111 void 112 tcp_offload_listen_start(struct tcpcb *tp) 113 { 114 115 INP_WLOCK_ASSERT(tptoinpcb(tp)); 116 117 EVENTHANDLER_INVOKE(tcp_offload_listen_start, tp); 118 } 119 120 void 121 tcp_offload_listen_stop(struct tcpcb *tp) 122 { 123 124 INP_WLOCK_ASSERT(tptoinpcb(tp)); 125 126 EVENTHANDLER_INVOKE(tcp_offload_listen_stop, tp); 127 } 128 129 void 130 tcp_offload_input(struct tcpcb *tp, struct mbuf *m) 131 { 132 struct toedev *tod = tp->tod; 133 134 KASSERT(tod != NULL, ("%s: tp->tod is NULL, tp %p", __func__, tp)); 135 INP_WLOCK_ASSERT(tptoinpcb(tp)); 136 137 tod->tod_input(tod, tp, m); 138 } 139 140 int 141 tcp_offload_output(struct tcpcb *tp) 142 { 143 struct toedev *tod = tp->tod; 144 int error, flags; 145 146 KASSERT(tod != NULL, ("%s: tp->tod is NULL, tp %p", __func__, tp)); 147 INP_WLOCK_ASSERT(tptoinpcb(tp)); 148 149 flags = tcp_outflags[tp->t_state]; 150 151 if (flags & TH_RST) { 152 /* XXX: avoid repeated calls like we do for FIN */ 153 error = tod->tod_send_rst(tod, tp); 154 } else if ((flags & TH_FIN || tp->t_flags & TF_NEEDFIN) && 155 (tp->t_flags & TF_SENTFIN) == 0) { 156 error = tod->tod_send_fin(tod, tp); 157 if (error == 0) 158 tp->t_flags |= TF_SENTFIN; 159 } else 160 error = tod->tod_output(tod, tp); 161 162 return (error); 163 } 164 165 void 166 tcp_offload_rcvd(struct tcpcb *tp) 167 { 168 struct toedev *tod = tp->tod; 169 170 KASSERT(tod != NULL, ("%s: tp->tod is NULL, tp %p", __func__, tp)); 171 INP_WLOCK_ASSERT(tptoinpcb(tp)); 172 173 tod->tod_rcvd(tod, tp); 174 } 175 176 void 177 tcp_offload_ctloutput(struct tcpcb *tp, int sopt_dir, int sopt_name) 178 { 179 struct toedev *tod = tp->tod; 180 181 KASSERT(tod != NULL, ("%s: tp->tod is NULL, tp %p", __func__, tp)); 182 INP_WLOCK_ASSERT(tptoinpcb(tp)); 183 184 tod->tod_ctloutput(tod, tp, sopt_dir, sopt_name); 185 } 186 187 void 188 tcp_offload_tcp_info(const struct tcpcb *tp, struct tcp_info *ti) 189 { 190 struct toedev *tod = tp->tod; 191 192 KASSERT(tod != NULL, ("%s: tp->tod is NULL, tp %p", __func__, tp)); 193 INP_LOCK_ASSERT(tptoinpcb(tp)); 194 195 tod->tod_tcp_info(tod, tp, ti); 196 } 197 198 int 199 tcp_offload_alloc_tls_session(struct tcpcb *tp, struct ktls_session *tls, 200 int direction) 201 { 202 struct toedev *tod = tp->tod; 203 204 KASSERT(tod != NULL, ("%s: tp->tod is NULL, tp %p", __func__, tp)); 205 INP_WLOCK_ASSERT(tptoinpcb(tp)); 206 207 return (tod->tod_alloc_tls_session(tod, tp, tls, direction)); 208 } 209 210 void 211 tcp_offload_detach(struct tcpcb *tp) 212 { 213 struct toedev *tod = tp->tod; 214 215 KASSERT(tod != NULL, ("%s: tp->tod is NULL, tp %p", __func__, tp)); 216 INP_WLOCK_ASSERT(tptoinpcb(tp)); 217 218 tod->tod_pcb_detach(tod, tp); 219 } 220 221 void 222 tcp_offload_pmtu_update(struct tcpcb *tp, tcp_seq seq, int mtu) 223 { 224 struct toedev *tod = tp->tod; 225 226 KASSERT(tod != NULL, ("%s: tp->tod is NULL, tp %p", __func__, tp)); 227 INP_WLOCK_ASSERT(tptoinpcb(tp)); 228 229 tod->tod_pmtu_update(tod, tp, seq, mtu); 230 } 231