1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 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 <sys/cdefs.h> 30 __FBSDID("$FreeBSD$"); 31 32 #include "opt_inet.h" 33 34 #include <sys/param.h> 35 #include <sys/systm.h> 36 #include <sys/types.h> 37 #include <sys/mbuf.h> 38 #include <sys/socket.h> 39 #include <sys/socketvar.h> 40 #include <sys/sockopt.h> 41 #include <net/if.h> 42 #include <net/if_var.h> 43 #include <net/route.h> 44 #include <netinet/in.h> 45 #include <netinet/in_pcb.h> 46 #include <netinet/tcp.h> 47 #include <netinet/tcp_offload.h> 48 #include <netinet/tcp_fsm.h> 49 #include <netinet/tcp_var.h> 50 #include <netinet/toecore.h> 51 52 int registered_toedevs; 53 54 /* 55 * Provide an opportunity for a TOE driver to offload. 56 */ 57 int 58 tcp_offload_connect(struct socket *so, struct sockaddr *nam) 59 { 60 struct ifnet *ifp; 61 struct toedev *tod; 62 struct rtentry *rt; 63 int error = EOPNOTSUPP; 64 65 INP_WLOCK_ASSERT(sotoinpcb(so)); 66 KASSERT(nam->sa_family == AF_INET || nam->sa_family == AF_INET6, 67 ("%s: called with sa_family %d", __func__, nam->sa_family)); 68 69 if (registered_toedevs == 0) 70 return (error); 71 72 rt = rtalloc1(nam, 0, 0); 73 if (rt) 74 RT_UNLOCK(rt); 75 else 76 return (EHOSTUNREACH); 77 78 ifp = rt->rt_ifp; 79 80 if (nam->sa_family == AF_INET && !(ifp->if_capenable & IFCAP_TOE4)) 81 goto done; 82 if (nam->sa_family == AF_INET6 && !(ifp->if_capenable & IFCAP_TOE6)) 83 goto done; 84 85 tod = TOEDEV(ifp); 86 if (tod != NULL) 87 error = tod->tod_connect(tod, so, rt, nam); 88 done: 89 RTFREE(rt); 90 return (error); 91 } 92 93 void 94 tcp_offload_listen_start(struct tcpcb *tp) 95 { 96 97 INP_WLOCK_ASSERT(tp->t_inpcb); 98 99 EVENTHANDLER_INVOKE(tcp_offload_listen_start, tp); 100 } 101 102 void 103 tcp_offload_listen_stop(struct tcpcb *tp) 104 { 105 106 INP_WLOCK_ASSERT(tp->t_inpcb); 107 108 EVENTHANDLER_INVOKE(tcp_offload_listen_stop, tp); 109 } 110 111 void 112 tcp_offload_input(struct tcpcb *tp, struct mbuf *m) 113 { 114 struct toedev *tod = tp->tod; 115 116 KASSERT(tod != NULL, ("%s: tp->tod is NULL, tp %p", __func__, tp)); 117 INP_WLOCK_ASSERT(tp->t_inpcb); 118 119 tod->tod_input(tod, tp, m); 120 } 121 122 int 123 tcp_offload_output(struct tcpcb *tp) 124 { 125 struct toedev *tod = tp->tod; 126 int error, flags; 127 128 KASSERT(tod != NULL, ("%s: tp->tod is NULL, tp %p", __func__, tp)); 129 INP_WLOCK_ASSERT(tp->t_inpcb); 130 131 flags = tcp_outflags[tp->t_state]; 132 133 if (flags & TH_RST) { 134 /* XXX: avoid repeated calls like we do for FIN */ 135 error = tod->tod_send_rst(tod, tp); 136 } else if ((flags & TH_FIN || tp->t_flags & TF_NEEDFIN) && 137 (tp->t_flags & TF_SENTFIN) == 0) { 138 error = tod->tod_send_fin(tod, tp); 139 if (error == 0) 140 tp->t_flags |= TF_SENTFIN; 141 } else 142 error = tod->tod_output(tod, tp); 143 144 return (error); 145 } 146 147 void 148 tcp_offload_rcvd(struct tcpcb *tp) 149 { 150 struct toedev *tod = tp->tod; 151 152 KASSERT(tod != NULL, ("%s: tp->tod is NULL, tp %p", __func__, tp)); 153 INP_WLOCK_ASSERT(tp->t_inpcb); 154 155 tod->tod_rcvd(tod, tp); 156 } 157 158 void 159 tcp_offload_ctloutput(struct tcpcb *tp, int sopt_dir, int sopt_name) 160 { 161 struct toedev *tod = tp->tod; 162 163 KASSERT(tod != NULL, ("%s: tp->tod is NULL, tp %p", __func__, tp)); 164 INP_WLOCK_ASSERT(tp->t_inpcb); 165 166 tod->tod_ctloutput(tod, tp, sopt_dir, sopt_name); 167 } 168 169 void 170 tcp_offload_tcp_info(struct tcpcb *tp, struct tcp_info *ti) 171 { 172 struct toedev *tod = tp->tod; 173 174 KASSERT(tod != NULL, ("%s: tp->tod is NULL, tp %p", __func__, tp)); 175 INP_WLOCK_ASSERT(tp->t_inpcb); 176 177 tod->tod_tcp_info(tod, tp, ti); 178 } 179 180 void 181 tcp_offload_detach(struct tcpcb *tp) 182 { 183 struct toedev *tod = tp->tod; 184 185 KASSERT(tod != NULL, ("%s: tp->tod is NULL, tp %p", __func__, tp)); 186 INP_WLOCK_ASSERT(tp->t_inpcb); 187 188 tod->tod_pcb_detach(tod, tp); 189 } 190