2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2012 Chelsio Communications, Inc.
6 * Written by: Navdeep Parhar <np@FreeBSD.org>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
34 #include "opt_inet6.h"
36 #include <sys/param.h>
37 #include <sys/eventhandler.h>
38 #include <sys/kernel.h>
39 #include <sys/systm.h>
40 #include <sys/malloc.h>
42 #include <sys/module.h>
43 #include <sys/types.h>
44 #include <sys/sockopt.h>
45 #include <sys/sysctl.h>
46 #include <sys/socket.h>
48 #include <net/ethernet.h>
50 #include <net/if_var.h>
51 #include <net/if_types.h>
52 #include <net/if_vlan_var.h>
53 #include <net/if_llatbl.h>
54 #include <net/route.h>
56 #include <netinet/if_ether.h>
57 #include <netinet/in.h>
58 #include <netinet/in_pcb.h>
59 #include <netinet/in_var.h>
60 #include <netinet6/in6_var.h>
61 #include <netinet6/in6_pcb.h>
62 #include <netinet6/nd6.h>
64 #include <netinet/tcp.h>
65 #include <netinet/tcp_fsm.h>
66 #include <netinet/tcp_timer.h>
67 #include <netinet/tcp_var.h>
68 #include <netinet/tcp_syncache.h>
69 #include <netinet/tcp_offload.h>
70 #include <netinet/toecore.h>
72 static struct mtx toedev_lock;
73 static TAILQ_HEAD(, toedev) toedev_list;
74 static eventhandler_tag listen_start_eh;
75 static eventhandler_tag listen_stop_eh;
76 static eventhandler_tag lle_event_eh;
79 toedev_connect(struct toedev *tod __unused, struct socket *so __unused,
80 struct nhop_object *nh __unused, struct sockaddr *nam __unused)
87 toedev_listen_start(struct toedev *tod __unused, struct tcpcb *tp __unused)
94 toedev_listen_stop(struct toedev *tod __unused, struct tcpcb *tp __unused)
101 toedev_input(struct toedev *tod __unused, struct tcpcb *tp __unused,
110 toedev_rcvd(struct toedev *tod __unused, struct tcpcb *tp __unused)
117 toedev_output(struct toedev *tod __unused, struct tcpcb *tp __unused)
124 toedev_pcb_detach(struct toedev *tod __unused, struct tcpcb *tp __unused)
131 toedev_l2_update(struct toedev *tod __unused, struct ifnet *ifp __unused,
132 struct sockaddr *sa __unused, uint8_t *lladdr __unused,
133 uint16_t vtag __unused)
140 toedev_route_redirect(struct toedev *tod __unused, struct ifnet *ifp __unused,
141 struct nhop_object *nh0 __unused, struct nhop_object *nh1 __unused)
148 toedev_syncache_added(struct toedev *tod __unused, void *ctx __unused)
155 toedev_syncache_removed(struct toedev *tod __unused, void *ctx __unused)
162 toedev_syncache_respond(struct toedev *tod __unused, void *ctx __unused,
171 toedev_offload_socket(struct toedev *tod __unused, void *ctx __unused,
172 struct socket *so __unused)
179 toedev_ctloutput(struct toedev *tod __unused, struct tcpcb *tp __unused,
180 int sopt_dir __unused, int sopt_name __unused)
187 toedev_tcp_info(struct toedev *tod __unused, struct tcpcb *tp __unused,
188 struct tcp_info *ti __unused)
195 toedev_alloc_tls_session(struct toedev *tod __unused, struct tcpcb *tp __unused,
196 struct ktls_session *tls __unused, int direction __unused)
203 toedev_pmtu_update(struct toedev *tod __unused, struct tcpcb *tp __unused,
204 tcp_seq seq __unused, int mtu __unused)
211 * Inform one or more TOE devices about a listening socket.
214 toe_listen_start(struct inpcb *inp, void *arg)
216 struct toedev *t, *tod;
219 INP_WLOCK_ASSERT(inp);
220 KASSERT(inp->inp_pcbinfo == &V_tcbinfo,
221 ("%s: inp is not a TCP inp", __func__));
223 if (inp->inp_flags & (INP_DROPPED | INP_TIMEWAIT))
227 if (tp->t_state != TCPS_LISTEN)
231 mtx_lock(&toedev_lock);
232 TAILQ_FOREACH(tod, &toedev_list, link) {
233 if (t == NULL || t == tod)
234 tod->tod_listen_start(tod, tp);
236 mtx_unlock(&toedev_lock);
240 toe_listen_start_event(void *arg __unused, struct tcpcb *tp)
242 struct inpcb *inp = tp->t_inpcb;
244 INP_WLOCK_ASSERT(inp);
245 KASSERT(tp->t_state == TCPS_LISTEN,
246 ("%s: t_state %s", __func__, tcpstates[tp->t_state]));
248 toe_listen_start(inp, NULL);
252 toe_listen_stop_event(void *arg __unused, struct tcpcb *tp)
256 struct inpcb *inp = tp->t_inpcb;
259 INP_WLOCK_ASSERT(inp);
260 KASSERT(tp->t_state == TCPS_LISTEN,
261 ("%s: t_state %s", __func__, tcpstates[tp->t_state]));
263 mtx_lock(&toedev_lock);
264 TAILQ_FOREACH(tod, &toedev_list, link)
265 tod->tod_listen_stop(tod, tp);
266 mtx_unlock(&toedev_lock);
270 * Fill up a freshly allocated toedev struct with reasonable defaults.
273 init_toedev(struct toedev *tod)
276 tod->tod_softc = NULL;
279 * Provide no-op defaults so that the kernel can call any toedev
280 * function without having to check whether the TOE driver supplied one
283 tod->tod_connect = toedev_connect;
284 tod->tod_listen_start = toedev_listen_start;
285 tod->tod_listen_stop = toedev_listen_stop;
286 tod->tod_input = toedev_input;
287 tod->tod_rcvd = toedev_rcvd;
288 tod->tod_output = toedev_output;
289 tod->tod_send_rst = toedev_output;
290 tod->tod_send_fin = toedev_output;
291 tod->tod_pcb_detach = toedev_pcb_detach;
292 tod->tod_l2_update = toedev_l2_update;
293 tod->tod_route_redirect = toedev_route_redirect;
294 tod->tod_syncache_added = toedev_syncache_added;
295 tod->tod_syncache_removed = toedev_syncache_removed;
296 tod->tod_syncache_respond = toedev_syncache_respond;
297 tod->tod_offload_socket = toedev_offload_socket;
298 tod->tod_ctloutput = toedev_ctloutput;
299 tod->tod_tcp_info = toedev_tcp_info;
300 tod->tod_alloc_tls_session = toedev_alloc_tls_session;
301 tod->tod_pmtu_update = toedev_pmtu_update;
305 * Register an active TOE device with the system. This allows it to receive
306 * notifications from the kernel.
309 register_toedev(struct toedev *tod)
313 mtx_lock(&toedev_lock);
314 TAILQ_FOREACH(t, &toedev_list, link) {
316 mtx_unlock(&toedev_lock);
321 TAILQ_INSERT_TAIL(&toedev_list, tod, link);
322 registered_toedevs++;
323 mtx_unlock(&toedev_lock);
325 inp_apply_all(toe_listen_start, tod);
331 * Remove the TOE device from the global list of active TOE devices. It is the
332 * caller's responsibility to ensure that the TOE device is quiesced prior to
336 unregister_toedev(struct toedev *tod)
338 struct toedev *t, *t2;
341 mtx_lock(&toedev_lock);
342 TAILQ_FOREACH_SAFE(t, &toedev_list, link, t2) {
344 TAILQ_REMOVE(&toedev_list, tod, link);
345 registered_toedevs--;
350 KASSERT(registered_toedevs >= 0,
351 ("%s: registered_toedevs (%d) < 0", __func__, registered_toedevs));
352 mtx_unlock(&toedev_lock);
357 toe_syncache_add(struct in_conninfo *inc, struct tcpopt *to, struct tcphdr *th,
358 struct inpcb *inp, void *tod, void *todctx, uint8_t iptos)
361 INP_RLOCK_ASSERT(inp);
363 (void )syncache_add(inc, to, th, inp, inp->inp_socket, NULL, tod,
364 todctx, iptos, htons(0));
368 toe_syncache_expand(struct in_conninfo *inc, struct tcpopt *to,
369 struct tcphdr *th, struct socket **lsop)
374 return (syncache_expand(inc, to, th, lsop, NULL, htons(0)));
378 * General purpose check to see if a 4-tuple is in use by the kernel. If a TCP
379 * header (presumably for an incoming SYN) is also provided, an existing 4-tuple
380 * in TIME_WAIT may be assassinated freeing it up for re-use.
382 * Note that the TCP header must have been run through tcp_fields_to_host() or
386 toe_4tuple_check(struct in_conninfo *inc, struct tcphdr *th, struct ifnet *ifp)
390 if (inc->inc_flags & INC_ISIPV6) {
391 inp = in6_pcblookup(&V_tcbinfo, &inc->inc6_faddr,
392 inc->inc_fport, &inc->inc6_laddr, inc->inc_lport,
393 INPLOOKUP_RLOCKPCB, ifp);
395 inp = in_pcblookup(&V_tcbinfo, inc->inc_faddr, inc->inc_fport,
396 inc->inc_laddr, inc->inc_lport, INPLOOKUP_RLOCKPCB, ifp);
399 INP_RLOCK_ASSERT(inp);
401 if ((inp->inp_flags & INP_TIMEWAIT) && th != NULL) {
402 if (!tcp_twcheck(inp, NULL, th, NULL, 0))
414 toe_lle_event(void *arg __unused, struct llentry *lle, int evt)
422 struct sockaddr_in6 sin6;
424 LLE_WLOCK_ASSERT(lle);
426 ifp = lltable_get_ifp(lle->lle_tbl);
427 family = lltable_get_af(lle->lle_tbl);
429 if (family != AF_INET && family != AF_INET6)
432 * Not interested if the interface's TOE capability is not enabled.
434 if ((family == AF_INET && !(ifp->if_capenable & IFCAP_TOE4)) ||
435 (family == AF_INET6 && !(ifp->if_capenable & IFCAP_TOE6)))
442 sa = (struct sockaddr *)&sin6;
443 lltable_fill_sa_entry(lle, sa);
447 if (evt != LLENTRY_RESOLVED) {
449 * LLENTRY_TIMEDOUT, LLENTRY_DELETED, LLENTRY_EXPIRED all mean
450 * this entry is going to be deleted.
455 KASSERT(lle->la_flags & LLE_VALID,
456 ("%s: %p resolved but not valid?", __func__, lle));
458 lladdr = (uint8_t *)lle->ll_addr;
463 tod->tod_l2_update(tod, ifp, sa, lladdr, EVL_MAKETAG(vid, pcp, 0));
467 * Returns 0 or EWOULDBLOCK on success (any other value is an error). 0 means
468 * lladdr and vtag are valid on return, EWOULDBLOCK means the TOE driver's
469 * tod_l2_update will be called later, when the entry is resolved or times out.
472 toe_l2_resolve(struct toedev *tod, struct ifnet *ifp, struct sockaddr *sa,
473 uint8_t *lladdr, uint16_t *vtag)
478 switch (sa->sa_family) {
481 rc = arpresolve(ifp, 0, NULL, sa, lladdr, NULL, NULL);
486 rc = nd6_resolve(ifp, LLE_SF(AF_INET6, 0), NULL, sa, lladdr,
491 return (EPROTONOSUPPORT);
497 if (ifp->if_type == IFT_L2VLAN) {
500 } else if (ifp->if_pcp != IFNET_PCP_NONE) {
504 *vtag = EVL_MAKETAG(vid, pcp, 0);
511 toe_connect_failed(struct toedev *tod, struct inpcb *inp, int err)
515 INP_WLOCK_ASSERT(inp);
517 if (!(inp->inp_flags & INP_DROPPED)) {
518 struct tcpcb *tp = intotcpcb(inp);
520 KASSERT(tp->t_flags & TF_TOE,
521 ("%s: tp %p not offloaded.", __func__, tp));
525 * Temporary failure during offload, take this PCB back.
526 * Detach from the TOE driver and do the rest of what
527 * TCP's pru_connect would have done if the connection
531 tod->tod_pcb_detach(tod, tp);
532 KASSERT(!(tp->t_flags & TF_TOE),
533 ("%s: tp %p still offloaded.", __func__, tp));
534 tcp_timer_activate(tp, TT_KEEP, TP_KEEPINIT(tp));
535 if (tcp_output(tp) < 0)
536 INP_WLOCK(inp); /* re-acquire */
538 tp = tcp_drop(tp, err);
540 INP_WLOCK(inp); /* re-acquire */
543 INP_WLOCK_ASSERT(inp);
550 mtx_init(&toedev_lock, "toedev lock", NULL, MTX_DEF);
551 TAILQ_INIT(&toedev_list);
553 listen_start_eh = EVENTHANDLER_REGISTER(tcp_offload_listen_start,
554 toe_listen_start_event, NULL, EVENTHANDLER_PRI_ANY);
555 listen_stop_eh = EVENTHANDLER_REGISTER(tcp_offload_listen_stop,
556 toe_listen_stop_event, NULL, EVENTHANDLER_PRI_ANY);
557 lle_event_eh = EVENTHANDLER_REGISTER(lle_event, toe_lle_event, NULL,
558 EVENTHANDLER_PRI_ANY);
567 mtx_lock(&toedev_lock);
568 if (!TAILQ_EMPTY(&toedev_list)) {
569 mtx_unlock(&toedev_lock);
573 EVENTHANDLER_DEREGISTER(tcp_offload_listen_start, listen_start_eh);
574 EVENTHANDLER_DEREGISTER(tcp_offload_listen_stop, listen_stop_eh);
575 EVENTHANDLER_DEREGISTER(lle_event, lle_event_eh);
577 mtx_unlock(&toedev_lock);
578 mtx_destroy(&toedev_lock);
584 toecore_mod_handler(module_t mod, int cmd, void *arg)
588 return (toecore_load());
590 if (cmd == MOD_UNLOAD)
591 return (toecore_unload());
596 static moduledata_t mod_data= {
602 MODULE_VERSION(toecore, 1);
603 DECLARE_MODULE(toecore, mod_data, SI_SUB_EXEC, SI_ORDER_ANY);