Index: sbin/ipfw/ipfw.8 =================================================================== --- sbin/ipfw/ipfw.8 +++ sbin/ipfw/ipfw.8 @@ -1118,6 +1118,20 @@ keyword with setdscp. If the tablearg value is not within the 0..64 range, lower 6 bits of supplied value are used. +.It Cm tcp-setmss Ar mss +Set the Maximum Segment Size (MSS) in the TCP segment to value +.Ar mss . +The kernel module +.Cm ipfw_pmod +should be loaded or kernel should have +.Cm options IPFIREWALL_PMOD +to be able use this action. +This command does not change a packet if original MSS value is lower than +specified value. +Both TCP over IPv4 and over IPv6 are supported. +Regardless of matched a packet or not by the +.Cm tcp-setmss +rule, the search continues with the next rule. .It Cm reass Queue and reassemble IP fragments. If the packet is not fragmented, counters are updated and Index: sbin/ipfw/ipfw2.h =================================================================== --- sbin/ipfw/ipfw2.h +++ sbin/ipfw/ipfw2.h @@ -284,6 +284,8 @@ TOK_INTPREFIX, TOK_EXTPREFIX, TOK_PREFIXLEN, + + TOK_TCPSETMSS, }; /* Index: sbin/ipfw/ipfw2.c =================================================================== --- sbin/ipfw/ipfw2.c +++ sbin/ipfw/ipfw2.c @@ -36,6 +36,7 @@ #include #include #include +#include #include #include #include @@ -238,6 +239,7 @@ { "nat64lsn", TOK_NAT64LSN }, { "nat64stl", TOK_NAT64STL }, { "nptv6", TOK_NPTV6 }, + { "tcp-setmss", TOK_TCPSETMSS }, { NULL, 0 } /* terminator */ }; @@ -272,6 +274,7 @@ { "call", TOK_CALL }, { "return", TOK_RETURN }, { "eaction", TOK_EACTION }, + { "tcp-setmss", TOK_TCPSETMSS }, { NULL, 0 } /* terminator */ }; @@ -1642,6 +1645,22 @@ break; } + case O_EXTERNAL_DATA: { + if (has_eaction == NULL) + break; + /* + * Currently we support data formatting only for + * external data with datalen u16. For unknown data + * print its size in bytes. + */ + if (cmd->len == F_INSN_SIZE(ipfw_insn)) + bprintf(bp, " %u", cmd->arg1); + else + bprintf(bp, " %ubytes", + cmd->len * sizeof(uint32_t)); + break; + } + case O_SETDSCP: { const char *code; @@ -3991,6 +4010,26 @@ fill_cmd(action, O_CALLRETURN, F_NOT, 0); break; + case TOK_TCPSETMSS: { + u_long mss; + uint16_t idx; + + idx = pack_object(tstate, "tcp-setmss", IPFW_TLV_EACTION); + if (idx == 0) + errx(EX_DATAERR, "pack_object failed"); + fill_cmd(action, O_EXTERNAL_ACTION, 0, idx); + NEED1("Missing MSS value"); + action = next_cmd(action, &ablen); + action->len = 1; + CHECK_ACTLEN; + mss = strtoul(*av, NULL, 10); + if (mss == 0 || mss > UINT16_MAX) + errx(EX_USAGE, "invalid MSS value %s", *av); + fill_cmd(action, O_EXTERNAL_DATA, 0, (uint16_t)mss); + av++; + break; + } + default: av--; if (match_token(rule_eactions, *av) == -1) Index: sys/conf/NOTES =================================================================== --- sys/conf/NOTES +++ sys/conf/NOTES @@ -971,6 +971,9 @@ # # IPFIREWALL_NPTV6 adds support for in kernel NPTv6 in ipfw. # +# IPFIREWALL_PMOD adds support for protocols modification module. Currently +# it supports only TCP MSS modification. +# # IPSTEALTH enables code to support stealth forwarding (i.e., forwarding # packets without touching the TTL). This can be useful to hide firewalls # from traceroute and similar tools. Index: sys/conf/files =================================================================== --- sys/conf/files +++ sys/conf/files @@ -4226,6 +4226,8 @@ ipfirewall_nptv6 netpfil/ipfw/nptv6/nptv6.c optional inet inet6 ipfirewall \ ipfirewall_nptv6 +netpfil/ipfw/pmod/ip_fw_pmod.c optional inet ipfirewall_pmod +netpfil/ipfw/pmod/tcpmod.c optional inet ipfirewall_pmod netpfil/pf/if_pflog.c optional pflog pf inet netpfil/pf/if_pfsync.c optional pfsync pf inet netpfil/pf/pf.c optional pf inet Index: sys/conf/options =================================================================== --- sys/conf/options +++ sys/conf/options @@ -426,6 +426,7 @@ IPFIREWALL_NPTV6 opt_ipfw.h IPFIREWALL_VERBOSE opt_ipfw.h IPFIREWALL_VERBOSE_LIMIT opt_ipfw.h +IPFIREWALL_PMOD opt_ipfw.h IPSEC opt_ipsec.h IPSEC_DEBUG opt_ipsec.h IPSEC_SUPPORT opt_ipsec.h Index: sys/modules/Makefile =================================================================== --- sys/modules/Makefile +++ sys/modules/Makefile @@ -173,6 +173,7 @@ ipfw_nat \ ${_ipfw_nat64} \ ${_ipfw_nptv6} \ + ${_ipfw_pmod} \ ${_ipmi} \ ip6_mroute_mod \ ip_mroute_mod \ @@ -448,6 +449,7 @@ _if_enc= if_enc _if_gif= if_gif _if_gre= if_gre +_ipfw_pmod= ipfw_pmod .if ${MK_IPSEC_SUPPORT} != "no" _ipsec= ipsec _tcpmd5= tcp/tcpmd5 Index: sys/modules/ipfw_pmod/Makefile =================================================================== --- /dev/null +++ sys/modules/ipfw_pmod/Makefile @@ -0,0 +1,8 @@ +# $FreeBSD$ + +.PATH: ${SRCTOP}/sys/netpfil/ipfw/pmod + +KMOD= ipfw_pmod +SRCS= ip_fw_pmod.c tcpmod.c opt_inet.h opt_inet6.h + +.include Index: sys/netinet/ip_fw.h =================================================================== --- sys/netinet/ip_fw.h +++ sys/netinet/ip_fw.h @@ -281,6 +281,7 @@ O_EXTERNAL_ACTION, /* arg1=id of external action handler */ O_EXTERNAL_INSTANCE, /* arg1=id of eaction handler instance */ + O_EXTERNAL_DATA, /* variable length data */ O_LAST_OPCODE /* not an opcode! */ }; Index: sys/netpfil/ipfw/ip_fw_eaction.c =================================================================== --- sys/netpfil/ipfw/ip_fw_eaction.c +++ sys/netpfil/ipfw/ip_fw_eaction.c @@ -57,7 +57,7 @@ * rules. * Module should implement opcode handler with type ipfw_eaction_t. * This handler will be called by ipfw_chk() function when - * O_EXTERNAL_ACTION opcode will be matched. The handler must return + * O_EXTERNAL_ACTION opcode is matched. The handler must return * value used as return value in ipfw_chk(), i.e. IP_FW_PASS, * IP_FW_DENY (see ip_fw_private.h). * Also the last argument must be set by handler. If it is zero, @@ -69,9 +69,12 @@ * This function will return eaction_id, that can be used by module. * * It is possible to pass some additional information to external - * action handler via the O_EXTERNAL_INSTANCE opcode. This opcode - * will be next after the O_EXTERNAL_ACTION opcode. cmd->arg1 will - * contain index of named object related to instance of external action. + * action handler using O_EXTERNAL_INSTANCE and O_EXTERNAL_DATA opcodes. + * Such opcodes should be next after the O_EXTERNAL_ACTION opcode. + * For the O_EXTERNAL_INSTANCE opcode the cmd->arg1 contains index of named + * object related to an instance of external action. + * For the O_EXTERNAL_DATA opcode the cmd contains the data that can be used + * by external action handler without needing to create named instance. * * In case when eaction module uses named instances, it should register * opcode rewriting routines for O_EXTERNAL_INSTANCE opcode. The @@ -284,11 +287,13 @@ /* * Since named_object related to this instance will be * also destroyed, truncate the chain of opcodes to - * remove O_EXTERNAL_INSTANCE opcode. + * remove the rest of cmd chain just after O_EXTERNAL_ACTION + * opcode. */ if (rule->act_ofs < rule->cmd_len - 1) { - EACTION_DEBUG("truncate rule %d", rule->rulenum); - rule->cmd_len--; + EACTION_DEBUG("truncate rule %d: len %u -> %u", + rule->rulenum, rule->cmd_len, rule->act_ofs + 1); + rule->cmd_len = rule->act_ofs + 1; } } IPFW_WUNLOCK(ch); Index: sys/netpfil/ipfw/ip_fw_sockopt.c =================================================================== --- sys/netpfil/ipfw/ip_fw_sockopt.c +++ sys/netpfil/ipfw/ip_fw_sockopt.c @@ -1736,11 +1736,16 @@ return (EINVAL); } ci->object_opcodes++; - /* Do we have O_EXTERNAL_INSTANCE opcode? */ + /* + * Do we have O_EXTERNAL_INSTANCE or O_EXTERNAL_DATA + * opcode? + */ if (l != cmdlen) { l -= cmdlen; cmd += cmdlen; cmdlen = F_LEN(cmd); + if (cmd->opcode == O_EXTERNAL_DATA) + goto check_action; if (cmd->opcode != O_EXTERNAL_INSTANCE) { printf("ipfw: invalid opcode " "next to external action %u\n", Index: sys/netpfil/ipfw/pmod/ip_fw_pmod.c =================================================================== --- /dev/null +++ sys/netpfil/ipfw/pmod/ip_fw_pmod.c @@ -0,0 +1,101 @@ +/*- + * Copyright (c) 2017 Yandex LLC + * Copyright (c) 2017 Andrey V. Elsukov + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include +__FBSDID("$FreeBSD$"); + +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +#include +#include +#include + +#include +#include + +static int +vnet_ipfw_pmod_init(const void *arg __unused) +{ + int error; + + error = tcpmod_init(&V_layer3_chain, IS_DEFAULT_VNET(curvnet)); + return (error); +} + +static int +vnet_ipfw_pmod_uninit(const void *arg __unused) +{ + + tcpmod_uninit(&V_layer3_chain, IS_DEFAULT_VNET(curvnet)); + return (0); +} + +static int +ipfw_pmod_modevent(module_t mod, int type, void *unused) +{ + + switch (type) { + case MOD_LOAD: + case MOD_UNLOAD: + break; + default: + return (EOPNOTSUPP); + } + return (0); +} + +static moduledata_t ipfw_pmod_mod = { + "ipfw_pmod", + ipfw_pmod_modevent, + 0 +}; + +/* Define startup order. */ +#define IPFW_PMOD_SI_SUB_FIREWALL SI_SUB_PROTO_IFATTACHDOMAIN +#define IPFW_PMOD_MODEVENT_ORDER (SI_ORDER_ANY - 128) /* after ipfw */ +#define IPFW_PMOD_MODULE_ORDER (IPFW_PMOD_MODEVENT_ORDER + 1) +#define IPFW_PMOD_VNET_ORDER (IPFW_PMOD_MODEVENT_ORDER + 2) + +DECLARE_MODULE(ipfw_pmod, ipfw_pmod_mod, IPFW_PMOD_SI_SUB_FIREWALL, + IPFW_PMOD_MODULE_ORDER); +MODULE_DEPEND(ipfw_pmod, ipfw, 3, 3, 3); +MODULE_VERSION(ipfw_pmod, 1); + +VNET_SYSINIT(vnet_ipfw_pmod_init, IPFW_PMOD_SI_SUB_FIREWALL, + IPFW_PMOD_VNET_ORDER, vnet_ipfw_pmod_init, NULL); +VNET_SYSUNINIT(vnet_ipfw_pmod_uninit, IPFW_PMOD_SI_SUB_FIREWALL, + IPFW_PMOD_VNET_ORDER, vnet_ipfw_pmod_uninit, NULL); Index: sys/netpfil/ipfw/pmod/pmod.h =================================================================== --- /dev/null +++ sys/netpfil/ipfw/pmod/pmod.h @@ -0,0 +1,36 @@ +/*- + * Copyright (c) 2017 Yandex LLC + * Copyright (c) 2017 Andrey V. Elsukov + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * $FreeBSD$ + */ + +#ifndef _IP_FW_PMOD_H_ +#define _IP_FW_PMOD_H_ + +int tcpmod_init(struct ip_fw_chain *ch, int first); +void tcpmod_uninit(struct ip_fw_chain *ch, int last); +#endif /* _IP_FW_PMOD_H_ */ + Index: sys/netpfil/ipfw/pmod/tcpmod.c =================================================================== --- /dev/null +++ sys/netpfil/ipfw/pmod/tcpmod.c @@ -0,0 +1,247 @@ +/*- + * Copyright (c) 2017 Yandex LLC + * Copyright (c) 2017 Andrey V. Elsukov + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "opt_inet.h" +#include "opt_inet6.h" + +#include +__FBSDID("$FreeBSD$"); + +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include + +#include +#include + +#include + +static VNET_DEFINE(uint16_t, tcpmod_setmss_eid) = 0; +#define V_tcpmod_setmss_eid VNET(tcpmod_setmss_eid) +#define IPFW_TLV_TCPMOD_NAME IPFW_TLV_EACTION_NAME(V_tcpmod_setmss_eid) + +static int +tcpmod_setmss(struct mbuf **mp, struct tcphdr *tcp, int tlen, uint16_t mss) +{ + struct mbuf *m; + u_char *cp; + int optlen, ret; + uint16_t oldmss, csum; + + m = *mp; + ret = IP_FW_DENY; + if (m->m_len < m->m_pkthdr.len) { + /* + * We shouldn't have any data, IP packet contains only + * TCP header with options. + */ + *mp = m = m_pullup(m, m->m_pkthdr.len); + if (m == NULL) + return (ret); + } + /* Parse TCP options. */ + for (tlen -= sizeof(struct tcphdr), cp = (u_char *)(tcp + 1); + tlen > 0; tlen -= optlen, cp += optlen) { + if (cp[0] == TCPOPT_EOL) + break; + if (cp[0] == TCPOPT_NOP) { + optlen = 1; + continue; + } + if (tlen < 2) + break; + optlen = cp[1]; + if (optlen < 2 || optlen > tlen) + break; + if (cp[0] == TCPOPT_MAXSEG) { + if (optlen != TCPOLEN_MAXSEG) + break; + ret = 0; /* report success */ + bcopy(cp + 2, &oldmss, sizeof(oldmss)); + /* Do not update lower MSS value */ + if (oldmss <= mss) + break; + bcopy(&mss, cp + 2, sizeof(mss)); + /* Update checksum if it is not delayed. */ + if ((m->m_pkthdr.csum_flags & + (CSUM_TCP | CSUM_TCP_IPV6)) == 0) { + bcopy(&tcp->th_sum, &csum, sizeof(csum)); + csum = cksum_adjust(csum, oldmss, mss); + bcopy(&csum, &tcp->th_sum, sizeof(csum)); + } + break; + } + } + + return (ret); +} + +#ifdef INET6 +static int +tcpmod_ipv6_setmss(struct mbuf **mp, uint16_t mss) +{ + struct ip6_hdr *ip6; + struct ip6_hbh *hbh; + struct tcphdr *tcp; + int hlen, plen, proto; + + ip6 = mtod(*mp, struct ip6_hdr *); + hlen = sizeof(*ip6); + proto = ip6->ip6_nxt; + /* + * Skip IPv6 extension headers and get the TCP header. + * ipfw_chk() has already done this work. So we are sure that + * we will not do an access to the out of bounds. For this + * reason we skip some checks here. + */ + while (proto == IPPROTO_HOPOPTS || proto == IPPROTO_ROUTING || + proto == IPPROTO_DSTOPTS) { + hbh = mtodo(*mp, hlen); + proto = hbh->ip6h_nxt; + hlen += hbh->ip6h_len << 3; + } + tcp = mtodo(*mp, hlen); + plen = (*mp)->m_pkthdr.len - hlen; + hlen = tcp->th_off << 2; + /* We must have TCP options and enough data in a packet. */ + if (hlen <= sizeof(struct tcphdr) || hlen > plen) + return (IP_FW_DENY); + return (tcpmod_setmss(mp, tcp, hlen, mss)); +} +#endif /* INET6 */ + +#ifdef INET +static int +tcpmod_ipv4_setmss(struct mbuf **mp, uint16_t mss) +{ + struct tcphdr *tcp; + struct ip *ip; + int hlen, plen; + + ip = mtod(*mp, struct ip *); + hlen = ip->ip_hl << 2; + tcp = mtodo(*mp, hlen); + plen = (*mp)->m_pkthdr.len - hlen; + hlen = tcp->th_off << 2; + /* We must have TCP options and enough data in a packet. */ + if (hlen <= sizeof(struct tcphdr) || hlen > plen) + return (IP_FW_DENY); + return (tcpmod_setmss(mp, tcp, hlen, mss)); +} +#endif /* INET */ + +/* + * ipfw external action handler. + */ +static int +ipfw_tcpmod(struct ip_fw_chain *chain, struct ip_fw_args *args, + ipfw_insn *cmd, int *done) +{ + ipfw_insn *icmd; + int ret; + + *done = 0; /* try next rule if not matched */ + ret = IP_FW_DENY; + icmd = cmd + 1; + if (cmd->opcode != O_EXTERNAL_ACTION || + cmd->arg1 != V_tcpmod_setmss_eid || + icmd->opcode != O_EXTERNAL_DATA || + icmd->len != F_INSN_SIZE(ipfw_insn)) + return (ret); + + /* + * NOTE: ipfw_chk() can set f_id.proto from IPv6 fragment header, + * but f_id._flags can be filled only from real TCP header. + * + * NOTE: ipfw_chk() drops very short packets in the PULLUP_TO() + * macro. But we need to check that mbuf is contiguous more than + * IP+IP_options/IP_extensions+tcphdr length, because TCP header + * must have TCP options, and ipfw_chk() does PULLUP_TO() size of + * struct tcphdr. + * + * NOTE: we require only the presence of SYN flag. User should + * properly configure the rule to select the direction of packets, + * that should be modified. + */ + if (args->f_id.proto != IPPROTO_TCP || + (args->f_id._flags & TH_SYN) == 0) + return (ret); + + switch (args->f_id.addr_type) { +#ifdef INET + case 4: + ret = tcpmod_ipv4_setmss(&args->m, htons(icmd->arg1)); + break; +#endif +#ifdef INET6 + case 6: + ret = tcpmod_ipv6_setmss(&args->m, htons(icmd->arg1)); + break; +#endif + } + /* + * We return zero in both @ret and @done on success, and ipfw_chk() + * will update rule counters. Otherwise a packet will not be matched + * by rule. + */ + return (ret); +} + +int +tcpmod_init(struct ip_fw_chain *ch, int first) +{ + + V_tcpmod_setmss_eid = ipfw_add_eaction(ch, ipfw_tcpmod, "tcp-setmss"); + if (V_tcpmod_setmss_eid == 0) + return (ENXIO); + return (0); +} + +void +tcpmod_uninit(struct ip_fw_chain *ch, int last) +{ + + ipfw_del_eaction(ch, V_tcpmod_setmss_eid); + V_tcpmod_setmss_eid = 0; +} +