2017-01-29 03:15:54 +03:00
|
|
|
/* $NetBSD: npf_ext_normalize.c,v 1.5 2017/01/29 00:15:54 christos Exp $ */
|
2012-09-16 17:47:41 +04:00
|
|
|
|
|
|
|
/*-
|
|
|
|
* Copyright (c) 2009-2012 The NetBSD Foundation, Inc.
|
|
|
|
* 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 NETBSD FOUNDATION, INC. AND CONTRIBUTORS
|
|
|
|
* ``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 FOUNDATION OR CONTRIBUTORS
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2016-12-27 02:05:05 +03:00
|
|
|
#ifdef _KERNEL
|
2012-09-16 17:47:41 +04:00
|
|
|
#include <sys/cdefs.h>
|
2017-01-29 03:15:54 +03:00
|
|
|
__KERNEL_RCSID(0, "$NetBSD: npf_ext_normalize.c,v 1.5 2017/01/29 00:15:54 christos Exp $");
|
2012-09-16 17:47:41 +04:00
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/module.h>
|
|
|
|
#include <sys/kmem.h>
|
|
|
|
|
|
|
|
#include <net/if.h>
|
|
|
|
#include <netinet/in_systm.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <netinet/in_var.h>
|
2016-12-27 02:05:05 +03:00
|
|
|
#endif
|
2012-09-16 17:47:41 +04:00
|
|
|
|
|
|
|
#include "npf.h"
|
|
|
|
#include "npf_impl.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* NPF extension module definition and the identifier.
|
|
|
|
*/
|
2013-03-13 00:47:48 +04:00
|
|
|
NPF_EXT_MODULE(npf_ext_normalize, "");
|
2012-09-16 17:47:41 +04:00
|
|
|
|
2013-03-13 00:47:48 +04:00
|
|
|
#define NPFEXT_NORMALIZE_VER 1
|
2012-09-16 17:47:41 +04:00
|
|
|
|
2013-03-13 00:47:48 +04:00
|
|
|
static void * npf_ext_normalize_id;
|
2012-09-16 17:47:41 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Normalisation parameters.
|
|
|
|
*/
|
|
|
|
typedef struct {
|
|
|
|
u_int n_minttl;
|
|
|
|
u_int n_maxmss;
|
|
|
|
bool n_random_id;
|
|
|
|
bool n_no_df;
|
2013-03-13 00:47:48 +04:00
|
|
|
} npf_normalize_t;
|
2012-09-16 17:47:41 +04:00
|
|
|
|
|
|
|
/*
|
2013-03-13 00:47:48 +04:00
|
|
|
* npf_normalize_ctor: a constructor for the normalisation rule procedure
|
2012-09-16 17:47:41 +04:00
|
|
|
* with the given parameters.
|
|
|
|
*/
|
|
|
|
static int
|
2013-03-13 00:47:48 +04:00
|
|
|
npf_normalize_ctor(npf_rproc_t *rp, prop_dictionary_t params)
|
2012-09-16 17:47:41 +04:00
|
|
|
{
|
2013-03-13 00:47:48 +04:00
|
|
|
npf_normalize_t *np;
|
2012-09-16 17:47:41 +04:00
|
|
|
|
|
|
|
/* Create a structure for normalisation parameters. */
|
2013-03-13 00:47:48 +04:00
|
|
|
np = kmem_zalloc(sizeof(npf_normalize_t), KM_SLEEP);
|
2012-09-16 17:47:41 +04:00
|
|
|
|
|
|
|
/* IP ID randomisation and IP_DF flag cleansing. */
|
|
|
|
prop_dictionary_get_bool(params, "random-id", &np->n_random_id);
|
|
|
|
prop_dictionary_get_bool(params, "no-df", &np->n_no_df);
|
|
|
|
|
|
|
|
/* Minimum IP TTL and maximum TCP MSS. */
|
|
|
|
prop_dictionary_get_uint32(params, "min-ttl", &np->n_minttl);
|
|
|
|
prop_dictionary_get_uint32(params, "max-mss", &np->n_maxmss);
|
|
|
|
|
|
|
|
/* Assign the parameters for this rule procedure. */
|
|
|
|
npf_rproc_assign(rp, np);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2013-03-13 00:47:48 +04:00
|
|
|
* npf_normalize_dtor: a destructor for a normalisation rule procedure.
|
2012-09-16 17:47:41 +04:00
|
|
|
*/
|
|
|
|
static void
|
2013-03-13 00:47:48 +04:00
|
|
|
npf_normalize_dtor(npf_rproc_t *rp, void *params)
|
2012-09-16 17:47:41 +04:00
|
|
|
{
|
|
|
|
/* Free our meta-data, associated with the procedure. */
|
2013-03-13 00:47:48 +04:00
|
|
|
kmem_free(params, sizeof(npf_normalize_t));
|
2012-09-16 17:47:41 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2013-03-13 00:47:48 +04:00
|
|
|
* npf_normalize_ip4: routine to normalize IPv4 header (randomise ID,
|
2012-09-16 17:47:41 +04:00
|
|
|
* clear "don't fragment" and/or enforce minimum TTL).
|
|
|
|
*/
|
2012-12-24 23:05:42 +04:00
|
|
|
static inline void
|
2013-03-13 00:47:48 +04:00
|
|
|
npf_normalize_ip4(npf_cache_t *npc, npf_normalize_t *np)
|
2012-09-16 17:47:41 +04:00
|
|
|
{
|
2012-12-24 23:05:42 +04:00
|
|
|
struct ip *ip = npc->npc_ip.v4;
|
2012-09-16 17:47:41 +04:00
|
|
|
uint16_t cksum = ip->ip_sum;
|
|
|
|
uint16_t ip_off = ip->ip_off;
|
|
|
|
uint8_t ttl = ip->ip_ttl;
|
|
|
|
u_int minttl = np->n_minttl;
|
|
|
|
|
|
|
|
KASSERT(np->n_random_id || np->n_no_df || minttl);
|
|
|
|
|
|
|
|
/* Randomise IPv4 ID. */
|
|
|
|
if (np->n_random_id) {
|
|
|
|
uint16_t oid = ip->ip_id, nid;
|
|
|
|
|
|
|
|
nid = htons(ip_randomid(ip_ids, 0));
|
|
|
|
cksum = npf_fixup16_cksum(cksum, oid, nid);
|
|
|
|
ip->ip_id = nid;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* IP_DF flag cleansing. */
|
|
|
|
if (np->n_no_df && (ip_off & htons(IP_DF)) != 0) {
|
|
|
|
uint16_t nip_off = ip_off & ~htons(IP_DF);
|
|
|
|
|
|
|
|
cksum = npf_fixup16_cksum(cksum, ip_off, nip_off);
|
|
|
|
ip->ip_off = nip_off;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Enforce minimum TTL. */
|
|
|
|
if (minttl && ttl < minttl) {
|
|
|
|
cksum = npf_fixup16_cksum(cksum, ttl, minttl);
|
|
|
|
ip->ip_ttl = minttl;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Update IPv4 checksum. */
|
|
|
|
ip->ip_sum = cksum;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2013-03-13 00:47:48 +04:00
|
|
|
* npf_normalize: the main routine to normalize IPv4 and/or TCP headers.
|
2012-09-16 17:47:41 +04:00
|
|
|
*/
|
2014-05-19 22:45:51 +04:00
|
|
|
static bool
|
2017-01-29 03:15:54 +03:00
|
|
|
npf_normalize(npf_cache_t *npc, void *params, const npf_match_info_t *mi,
|
|
|
|
int *decision)
|
2012-09-16 17:47:41 +04:00
|
|
|
{
|
2013-03-13 00:47:48 +04:00
|
|
|
npf_normalize_t *np = params;
|
2012-12-24 23:05:42 +04:00
|
|
|
struct tcphdr *th = npc->npc_l4.tcp;
|
|
|
|
uint16_t cksum, mss, maxmss = np->n_maxmss;
|
2012-09-16 17:47:41 +04:00
|
|
|
int wscale;
|
|
|
|
|
|
|
|
/* Skip, if already blocking. */
|
|
|
|
if (*decision == NPF_DECISION_BLOCK) {
|
2014-05-19 22:45:51 +04:00
|
|
|
return true;
|
2012-09-16 17:47:41 +04:00
|
|
|
}
|
|
|
|
|
2012-12-24 23:05:42 +04:00
|
|
|
/* Normalise IPv4. Nothing to do for IPv6. */
|
2012-09-16 17:47:41 +04:00
|
|
|
if (npf_iscached(npc, NPC_IP4) && (np->n_random_id || np->n_minttl)) {
|
2013-03-13 00:47:48 +04:00
|
|
|
npf_normalize_ip4(npc, np);
|
2012-09-16 17:47:41 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* TCP Maximum Segment Size (MSS) "clamping". Only if SYN packet.
|
|
|
|
* Fetch MSS and check whether rewrite to lower is needed.
|
|
|
|
*/
|
|
|
|
if (maxmss == 0 || !npf_iscached(npc, NPC_TCP) ||
|
|
|
|
(th->th_flags & TH_SYN) == 0) {
|
|
|
|
/* Not required; done. */
|
2014-05-19 22:45:51 +04:00
|
|
|
return true;
|
2012-09-16 17:47:41 +04:00
|
|
|
}
|
|
|
|
mss = 0;
|
2014-07-20 04:37:41 +04:00
|
|
|
if (!npf_fetch_tcpopts(npc, &mss, &wscale)) {
|
2014-05-19 22:45:51 +04:00
|
|
|
return true;
|
2012-09-16 17:47:41 +04:00
|
|
|
}
|
|
|
|
if (ntohs(mss) <= maxmss) {
|
|
|
|
/* Nothing else to do. */
|
2014-05-19 22:45:51 +04:00
|
|
|
return true;
|
2012-09-16 17:47:41 +04:00
|
|
|
}
|
|
|
|
maxmss = htons(maxmss);
|
2012-12-24 23:05:42 +04:00
|
|
|
|
|
|
|
/* Store new MSS, calculate TCP checksum and update it. */
|
2014-07-20 04:37:41 +04:00
|
|
|
if (npf_fetch_tcpopts(npc, &maxmss, &wscale)) {
|
2012-12-24 23:05:42 +04:00
|
|
|
cksum = npf_fixup16_cksum(th->th_sum, mss, maxmss);
|
|
|
|
th->th_sum = cksum;
|
2012-09-16 17:47:41 +04:00
|
|
|
}
|
2014-05-19 22:45:51 +04:00
|
|
|
|
|
|
|
return true;
|
2012-09-16 17:47:41 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2013-03-13 00:47:48 +04:00
|
|
|
npf_ext_normalize_modcmd(modcmd_t cmd, void *arg)
|
2012-09-16 17:47:41 +04:00
|
|
|
{
|
2013-03-13 00:47:48 +04:00
|
|
|
static const npf_ext_ops_t npf_normalize_ops = {
|
|
|
|
.version = NPFEXT_NORMALIZE_VER,
|
2012-09-16 17:47:41 +04:00
|
|
|
.ctx = NULL,
|
2013-03-13 00:47:48 +04:00
|
|
|
.ctor = npf_normalize_ctor,
|
|
|
|
.dtor = npf_normalize_dtor,
|
|
|
|
.proc = npf_normalize
|
2012-09-16 17:47:41 +04:00
|
|
|
};
|
2016-12-27 02:05:05 +03:00
|
|
|
npf_t *npf = npf_getkernctx();
|
2012-09-16 17:47:41 +04:00
|
|
|
|
|
|
|
switch (cmd) {
|
|
|
|
case MODULE_CMD_INIT:
|
|
|
|
/*
|
2013-03-13 00:47:48 +04:00
|
|
|
* Initialise normalisation module. Register the "normalize"
|
2012-09-16 17:47:41 +04:00
|
|
|
* extension and its calls.
|
|
|
|
*/
|
2013-03-13 00:47:48 +04:00
|
|
|
npf_ext_normalize_id =
|
2016-12-27 02:05:05 +03:00
|
|
|
npf_ext_register(npf, "normalize", &npf_normalize_ops);
|
2013-03-13 00:47:48 +04:00
|
|
|
return npf_ext_normalize_id ? 0 : EEXIST;
|
2012-09-16 17:47:41 +04:00
|
|
|
|
|
|
|
case MODULE_CMD_FINI:
|
|
|
|
/* Unregister the normalisation rule procedure. */
|
2016-12-27 02:05:05 +03:00
|
|
|
return npf_ext_unregister(npf, npf_ext_normalize_id);
|
2012-09-16 17:47:41 +04:00
|
|
|
|
|
|
|
case MODULE_CMD_AUTOUNLOAD:
|
|
|
|
return npf_autounload_p() ? 0 : EBUSY;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return ENOTTY;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|