/*-1* Copyright (c) 2017 Yandex LLC2* Copyright (c) 2017 Andrey V. Elsukov <[email protected]>3* All rights reserved.4*5* Redistribution and use in source and binary forms, with or without6* modification, are permitted provided that the following conditions7* are met:8*9* 1. Redistributions of source code must retain the above copyright10* notice, this list of conditions and the following disclaimer.11* 2. Redistributions in binary form must reproduce the above copyright12* notice, this list of conditions and the following disclaimer in the13* documentation and/or other materials provided with the distribution.14*15* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR16* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES17* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.18* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,19* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT20* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,21* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY22* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT23* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF24* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.25*/2627#include "opt_inet.h"28#include "opt_inet6.h"2930#include <sys/param.h>31#include <sys/systm.h>32#include <sys/errno.h>33#include <sys/kernel.h>34#include <sys/mbuf.h>35#include <sys/module.h>36#include <sys/socket.h>3738#include <net/if.h>39#include <net/if_var.h>40#include <net/pfil.h>41#include <net/vnet.h>4243#include <netinet/in.h>44#include <netinet/ip.h>45#include <netinet/ip_var.h>46#include <netinet/tcp.h>47#include <netinet/ip_fw.h>48#include <netinet/ip6.h>4950#include <netpfil/ipfw/ip_fw_private.h>51#include <netpfil/ipfw/pmod/pmod.h>5253#include <machine/in_cksum.h>5455VNET_DEFINE_STATIC(uint32_t, tcpmod_setmss_eid) = 0;56#define V_tcpmod_setmss_eid VNET(tcpmod_setmss_eid)5758static int59tcpmod_setmss(struct mbuf **mp, struct tcphdr *tcp, int tlen, uint16_t mss,60int *done)61{62struct mbuf *m;63u_char *cp;64int optlen, ret;65uint16_t oldmss, csum;6667m = *mp;68ret = IP_FW_DENY;69if (m->m_len < m->m_pkthdr.len) {70/*71* We shouldn't have any data, IP packet contains only72* TCP header with options.73*/74*mp = m = m_pullup(m, m->m_pkthdr.len);75if (m == NULL) {76*done = 1;77return (ret);78}79}80/* Parse TCP options. */81for (tlen -= sizeof(struct tcphdr), cp = (u_char *)(tcp + 1);82tlen > 0; tlen -= optlen, cp += optlen) {83if (cp[0] == TCPOPT_EOL)84break;85if (cp[0] == TCPOPT_NOP) {86optlen = 1;87continue;88}89if (tlen < 2)90break;91optlen = cp[1];92if (optlen < 2 || optlen > tlen)93break;94if (cp[0] == TCPOPT_MAXSEG) {95if (optlen != TCPOLEN_MAXSEG)96break;97ret = 0; /* report success */98bcopy(cp + 2, &oldmss, sizeof(oldmss));99/* Do not update lower MSS value */100if (ntohs(oldmss) <= ntohs(mss))101break;102bcopy(&mss, cp + 2, sizeof(mss));103/* Update checksum if it is not delayed. */104if ((m->m_pkthdr.csum_flags &105(CSUM_TCP | CSUM_TCP_IPV6)) == 0) {106bcopy(&tcp->th_sum, &csum, sizeof(csum));107csum = cksum_adjust(csum, oldmss, mss);108bcopy(&csum, &tcp->th_sum, sizeof(csum));109}110break;111}112}113114return (ret);115}116117#ifdef INET6118static int119tcpmod_ipv6_setmss(struct mbuf **mp, uint16_t mss, int *done)120{121struct ip6_hdr *ip6;122struct ip6_hbh *hbh;123struct tcphdr *tcp;124int hlen, plen, proto;125126ip6 = mtod(*mp, struct ip6_hdr *);127hlen = sizeof(*ip6);128proto = ip6->ip6_nxt;129/*130* Skip IPv6 extension headers and get the TCP header.131* ipfw_chk() has already done this work. So we are sure that132* we will not do an access to the out of bounds. For this133* reason we skip some checks here.134*/135while (proto == IPPROTO_HOPOPTS || proto == IPPROTO_ROUTING ||136proto == IPPROTO_DSTOPTS) {137hbh = mtodo(*mp, hlen);138proto = hbh->ip6h_nxt;139hlen += (hbh->ip6h_len + 1) << 3;140}141tcp = mtodo(*mp, hlen);142plen = (*mp)->m_pkthdr.len - hlen;143hlen = tcp->th_off << 2;144/* We must have TCP options and enough data in a packet. */145if (hlen <= sizeof(struct tcphdr) || hlen > plen)146return (IP_FW_DENY);147return (tcpmod_setmss(mp, tcp, hlen, mss, done));148}149#endif /* INET6 */150151#ifdef INET152static int153tcpmod_ipv4_setmss(struct mbuf **mp, uint16_t mss, int *done)154{155struct tcphdr *tcp;156struct ip *ip;157int hlen, plen;158159ip = mtod(*mp, struct ip *);160hlen = ip->ip_hl << 2;161tcp = mtodo(*mp, hlen);162plen = (*mp)->m_pkthdr.len - hlen;163hlen = tcp->th_off << 2;164/* We must have TCP options and enough data in a packet. */165if (hlen <= sizeof(struct tcphdr) || hlen > plen)166return (IP_FW_DENY);167return (tcpmod_setmss(mp, tcp, hlen, mss, done));168}169#endif /* INET */170171/*172* ipfw external action handler.173*/174static int175ipfw_tcpmod(struct ip_fw_chain *chain, struct ip_fw_args *args,176ipfw_insn *cmd, int *done)177{178ipfw_insn *icmd;179int ret;180181*done = 0; /* try next rule if not matched */182ret = IP_FW_DENY;183icmd = cmd + F_LEN(cmd);184if (cmd->opcode != O_EXTERNAL_ACTION ||185insntod(cmd, kidx)->kidx != V_tcpmod_setmss_eid ||186icmd->opcode != O_EXTERNAL_DATA ||187icmd->len != F_INSN_SIZE(ipfw_insn))188return (ret);189190/*191* NOTE: ipfw_chk() can set f_id.proto from IPv6 fragment header,192* but f_id._flags can be filled only from real TCP header.193*194* NOTE: ipfw_chk() drops very short packets in the PULLUP_TO()195* macro. But we need to check that mbuf is contiguous more than196* IP+IP_options/IP_extensions+tcphdr length, because TCP header197* must have TCP options, and ipfw_chk() does PULLUP_TO() size of198* struct tcphdr.199*200* NOTE: we require only the presence of SYN flag. User should201* properly configure the rule to select the direction of packets,202* that should be modified.203*/204if (args->f_id.proto != IPPROTO_TCP ||205(args->f_id._flags & TH_SYN) == 0)206return (ret);207208switch (args->f_id.addr_type) {209#ifdef INET210case 4:211ret = tcpmod_ipv4_setmss(&args->m, htons(icmd->arg1),212done);213break;214#endif215#ifdef INET6216case 6:217ret = tcpmod_ipv6_setmss(&args->m, htons(icmd->arg1),218done);219break;220#endif221}222/*223* We return zero in both @ret and @done on success, and ipfw_chk()224* will update rule counters. Otherwise a packet will not be matched225* by rule. We passed @done around above in case we hit a fatal error226* somewhere, we'll return non-zero but signal that rule processing227* cannot succeed.228*/229return (ret);230}231232int233tcpmod_init(struct ip_fw_chain *ch, int first)234{235236V_tcpmod_setmss_eid = ipfw_add_eaction(ch, ipfw_tcpmod, "tcp-setmss");237if (V_tcpmod_setmss_eid == 0)238return (ENXIO);239return (0);240}241242void243tcpmod_uninit(struct ip_fw_chain *ch, int last)244{245246ipfw_del_eaction(ch, V_tcpmod_setmss_eid);247V_tcpmod_setmss_eid = 0;248}249250251