2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2012 Chelsio Communications, Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
33 #include "opt_inet6.h"
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/eventhandler.h>
39 #include <sys/socket.h>
40 #include <sys/socketvar.h>
41 #include <sys/sockopt.h>
43 #include <net/if_var.h>
44 #include <net/if_private.h>
45 #include <net/route.h>
46 #include <net/route/nhop.h>
47 #include <netinet/in.h>
48 #include <netinet/in_pcb.h>
49 #include <netinet/in_fib.h>
50 #include <netinet6/in6_fib.h>
51 #include <netinet/tcp.h>
52 #include <netinet/tcp_offload.h>
54 #include <netinet/tcp_fsm.h>
55 #include <netinet/tcp_var.h>
56 #include <netinet/toecore.h>
58 int registered_toedevs;
61 * Provide an opportunity for a TOE driver to offload.
64 tcp_offload_connect(struct socket *so, struct sockaddr *nam)
68 struct nhop_object *nh;
69 struct epoch_tracker et;
70 int error = EOPNOTSUPP;
72 INP_WLOCK_ASSERT(sotoinpcb(so));
73 KASSERT(nam->sa_family == AF_INET || nam->sa_family == AF_INET6,
74 ("%s: called with sa_family %d", __func__, nam->sa_family));
76 if (registered_toedevs == 0)
82 if (nam->sa_family == AF_INET)
83 nh = fib4_lookup(0, ((struct sockaddr_in *)nam)->sin_addr,
86 #if defined(INET) && defined(INET6)
90 if (nam->sa_family == AF_INET6)
91 nh = fib6_lookup(0, &((struct sockaddr_in6 *)nam)->sin6_addr,
96 return (EHOSTUNREACH);
101 if (nam->sa_family == AF_INET && !(ifp->if_capenable & IFCAP_TOE4))
103 if (nam->sa_family == AF_INET6 && !(ifp->if_capenable & IFCAP_TOE6))
108 error = tod->tod_connect(tod, so, nh, nam);
115 tcp_offload_listen_start(struct tcpcb *tp)
118 INP_WLOCK_ASSERT(tptoinpcb(tp));
120 EVENTHANDLER_INVOKE(tcp_offload_listen_start, tp);
124 tcp_offload_listen_stop(struct tcpcb *tp)
127 INP_WLOCK_ASSERT(tptoinpcb(tp));
129 EVENTHANDLER_INVOKE(tcp_offload_listen_stop, tp);
133 tcp_offload_input(struct tcpcb *tp, struct mbuf *m)
135 struct toedev *tod = tp->tod;
137 KASSERT(tod != NULL, ("%s: tp->tod is NULL, tp %p", __func__, tp));
138 INP_WLOCK_ASSERT(tptoinpcb(tp));
140 tod->tod_input(tod, tp, m);
144 tcp_offload_output(struct tcpcb *tp)
146 struct toedev *tod = tp->tod;
149 KASSERT(tod != NULL, ("%s: tp->tod is NULL, tp %p", __func__, tp));
150 INP_WLOCK_ASSERT(tptoinpcb(tp));
152 flags = tcp_outflags[tp->t_state];
154 if (flags & TH_RST) {
155 /* XXX: avoid repeated calls like we do for FIN */
156 error = tod->tod_send_rst(tod, tp);
157 } else if ((flags & TH_FIN || tp->t_flags & TF_NEEDFIN) &&
158 (tp->t_flags & TF_SENTFIN) == 0) {
159 error = tod->tod_send_fin(tod, tp);
161 tp->t_flags |= TF_SENTFIN;
163 error = tod->tod_output(tod, tp);
169 tcp_offload_rcvd(struct tcpcb *tp)
171 struct toedev *tod = tp->tod;
173 KASSERT(tod != NULL, ("%s: tp->tod is NULL, tp %p", __func__, tp));
174 INP_WLOCK_ASSERT(tptoinpcb(tp));
176 tod->tod_rcvd(tod, tp);
180 tcp_offload_ctloutput(struct tcpcb *tp, int sopt_dir, int sopt_name)
182 struct toedev *tod = tp->tod;
184 KASSERT(tod != NULL, ("%s: tp->tod is NULL, tp %p", __func__, tp));
185 INP_WLOCK_ASSERT(tptoinpcb(tp));
187 tod->tod_ctloutput(tod, tp, sopt_dir, sopt_name);
191 tcp_offload_tcp_info(struct tcpcb *tp, struct tcp_info *ti)
193 struct toedev *tod = tp->tod;
195 KASSERT(tod != NULL, ("%s: tp->tod is NULL, tp %p", __func__, tp));
196 INP_WLOCK_ASSERT(tptoinpcb(tp));
198 tod->tod_tcp_info(tod, tp, ti);
202 tcp_offload_alloc_tls_session(struct tcpcb *tp, struct ktls_session *tls,
205 struct toedev *tod = tp->tod;
207 KASSERT(tod != NULL, ("%s: tp->tod is NULL, tp %p", __func__, tp));
208 INP_WLOCK_ASSERT(tptoinpcb(tp));
210 return (tod->tod_alloc_tls_session(tod, tp, tls, direction));
214 tcp_offload_detach(struct tcpcb *tp)
216 struct toedev *tod = tp->tod;
218 KASSERT(tod != NULL, ("%s: tp->tod is NULL, tp %p", __func__, tp));
219 INP_WLOCK_ASSERT(tptoinpcb(tp));
221 tod->tod_pcb_detach(tod, tp);
225 tcp_offload_pmtu_update(struct tcpcb *tp, tcp_seq seq, int mtu)
227 struct toedev *tod = tp->tod;
229 KASSERT(tod != NULL, ("%s: tp->tod is NULL, tp %p", __func__, tp));
230 INP_WLOCK_ASSERT(tptoinpcb(tp));
232 tod->tod_pmtu_update(tod, tp, seq, mtu);