2013-06-02 06:20:04 +04:00
|
|
|
/* $NetBSD: npf_alg_icmp.c,v 1.17 2013/06/02 02:20:04 rmind Exp $ */
|
2010-08-22 22:56:18 +04:00
|
|
|
|
|
|
|
/*-
|
|
|
|
* Copyright (c) 2010 The NetBSD Foundation, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This material is based upon work partially supported by The
|
|
|
|
* NetBSD Foundation under a contract with Mindaugas Rasiukevicius.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* NPF ALG for ICMP and traceroute translations.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/cdefs.h>
|
2013-06-02 06:20:04 +04:00
|
|
|
__KERNEL_RCSID(0, "$NetBSD: npf_alg_icmp.c,v 1.17 2013/06/02 02:20:04 rmind Exp $");
|
2010-08-22 22:56:18 +04:00
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/module.h>
|
|
|
|
|
|
|
|
#include <netinet/in_systm.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <netinet/ip.h>
|
|
|
|
#include <netinet/tcp.h>
|
|
|
|
#include <netinet/udp.h>
|
|
|
|
#include <netinet/ip_icmp.h>
|
2012-07-20 01:52:29 +04:00
|
|
|
#include <netinet/icmp6.h>
|
2010-08-22 22:56:18 +04:00
|
|
|
#include <net/pfil.h>
|
|
|
|
|
|
|
|
#include "npf_impl.h"
|
|
|
|
|
|
|
|
MODULE(MODULE_CLASS_MISC, npf_alg_icmp, "npf");
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Traceroute criteria.
|
|
|
|
*
|
|
|
|
* IANA assigned base port: 33434. However, common practice is to increase
|
2012-12-24 23:05:42 +04:00
|
|
|
* the port, thus monitor [33434-33484] range. Additional filter is low TTL.
|
2010-08-22 22:56:18 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
#define TR_BASE_PORT 33434
|
|
|
|
#define TR_PORT_RANGE 33484
|
2012-12-24 23:05:42 +04:00
|
|
|
#define TR_MAX_TTL 48
|
2010-08-22 22:56:18 +04:00
|
|
|
|
2011-01-18 23:33:45 +03:00
|
|
|
static npf_alg_t * alg_icmp __read_mostly;
|
2010-08-22 22:56:18 +04:00
|
|
|
|
2012-12-24 23:05:42 +04:00
|
|
|
static bool npfa_icmp_match(npf_cache_t *, nbuf_t *, npf_nat_t *, int);
|
|
|
|
static bool npfa_icmp_nat(npf_cache_t *, nbuf_t *, npf_nat_t *, int);
|
|
|
|
static npf_session_t *npfa_icmp_session(npf_cache_t *, nbuf_t *, int);
|
2010-08-22 22:56:18 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* npf_alg_icmp_{init,fini,modcmd}: ICMP ALG initialization, destruction
|
|
|
|
* and module interface.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static int
|
|
|
|
npf_alg_icmp_init(void)
|
|
|
|
{
|
2013-03-20 04:29:46 +04:00
|
|
|
alg_icmp = npf_alg_register("icmp", npfa_icmp_match,
|
2012-12-24 23:05:42 +04:00
|
|
|
npfa_icmp_nat, npfa_icmp_session);
|
2013-06-02 06:20:04 +04:00
|
|
|
return alg_icmp ? 0 : ENOMEM;
|
2010-08-22 22:56:18 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
npf_alg_icmp_fini(void)
|
|
|
|
{
|
|
|
|
KASSERT(alg_icmp != NULL);
|
|
|
|
return npf_alg_unregister(alg_icmp);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
npf_alg_icmp_modcmd(modcmd_t cmd, void *arg)
|
|
|
|
{
|
|
|
|
switch (cmd) {
|
|
|
|
case MODULE_CMD_INIT:
|
|
|
|
return npf_alg_icmp_init();
|
|
|
|
case MODULE_CMD_FINI:
|
|
|
|
return npf_alg_icmp_fini();
|
2012-07-15 04:22:58 +04:00
|
|
|
case MODULE_CMD_AUTOUNLOAD:
|
|
|
|
return EBUSY;
|
2010-08-22 22:56:18 +04:00
|
|
|
default:
|
|
|
|
return ENOTTY;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2010-11-11 09:30:39 +03:00
|
|
|
* npfa_icmp_match: ALG matching inspector - determines ALG case and
|
|
|
|
* associates ALG with NAT entry.
|
2010-08-22 22:56:18 +04:00
|
|
|
*/
|
|
|
|
static bool
|
2012-12-24 23:05:42 +04:00
|
|
|
npfa_icmp_match(npf_cache_t *npc, nbuf_t *nbuf, npf_nat_t *nt, int di)
|
2010-08-22 22:56:18 +04:00
|
|
|
{
|
2013-02-09 07:35:31 +04:00
|
|
|
const int proto = npc->npc_proto;
|
2012-12-24 23:05:42 +04:00
|
|
|
const struct ip *ip = npc->npc_ip.v4;
|
2010-11-11 09:30:39 +03:00
|
|
|
in_port_t dport;
|
|
|
|
|
2011-11-04 05:00:27 +04:00
|
|
|
KASSERT(npf_iscached(npc, NPC_IP46));
|
|
|
|
KASSERT(npf_iscached(npc, NPC_LAYER4));
|
2010-11-11 09:30:39 +03:00
|
|
|
|
2011-01-18 23:33:45 +03:00
|
|
|
/* Check for low TTL. */
|
|
|
|
if (ip->ip_ttl > TR_MAX_TTL) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-12-24 23:05:42 +04:00
|
|
|
switch (proto) {
|
|
|
|
case IPPROTO_TCP: {
|
|
|
|
const struct tcphdr *th = npc->npc_l4.tcp;
|
2010-11-11 09:30:39 +03:00
|
|
|
dport = ntohs(th->th_dport);
|
2012-12-24 23:05:42 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case IPPROTO_UDP: {
|
|
|
|
const struct udphdr *uh = npc->npc_l4.udp;
|
2010-11-11 09:30:39 +03:00
|
|
|
dport = ntohs(uh->uh_dport);
|
2012-12-24 23:05:42 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case IPPROTO_ICMP:
|
|
|
|
case IPPROTO_ICMPV6:
|
|
|
|
/* Just to pass the test below. */
|
|
|
|
dport = TR_BASE_PORT;
|
|
|
|
break;
|
|
|
|
default:
|
2010-08-22 22:56:18 +04:00
|
|
|
return false;
|
|
|
|
}
|
2010-11-11 09:30:39 +03:00
|
|
|
|
|
|
|
/* Handle TCP/UDP traceroute - check for port range. */
|
2010-08-22 22:56:18 +04:00
|
|
|
if (dport < TR_BASE_PORT || dport > TR_PORT_RANGE) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Associate ALG with translation entry. */
|
|
|
|
npf_nat_setalg(nt, alg_icmp, 0);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2012-12-24 23:05:42 +04:00
|
|
|
* npfa_icmp{4,6}_inspect: retrieve unique identifiers - either ICMP query
|
|
|
|
* ID or TCP/UDP ports of the original packet, which is embedded.
|
2010-08-22 22:56:18 +04:00
|
|
|
*/
|
2012-09-16 17:44:14 +04:00
|
|
|
|
2010-12-18 04:07:25 +03:00
|
|
|
static bool
|
2012-12-24 23:05:42 +04:00
|
|
|
npfa_icmp4_inspect(const int type, npf_cache_t *npc, nbuf_t *nbuf)
|
2010-08-22 22:56:18 +04:00
|
|
|
{
|
2012-09-16 17:44:14 +04:00
|
|
|
u_int offby;
|
|
|
|
|
|
|
|
/* Per RFC 792. */
|
|
|
|
switch (type) {
|
|
|
|
case ICMP_UNREACH:
|
|
|
|
case ICMP_SOURCEQUENCH:
|
|
|
|
case ICMP_REDIRECT:
|
|
|
|
case ICMP_TIMXCEED:
|
|
|
|
case ICMP_PARAMPROB:
|
2012-12-24 23:05:42 +04:00
|
|
|
if (npc == NULL) {
|
2012-09-16 17:44:14 +04:00
|
|
|
return false;
|
|
|
|
}
|
2012-12-24 23:05:42 +04:00
|
|
|
/* Should contain original IP header. */
|
|
|
|
if (!nbuf_advance(nbuf, offsetof(struct icmp, icmp_ip), 0)) {
|
2012-09-16 17:44:14 +04:00
|
|
|
return false;
|
2010-08-22 22:56:18 +04:00
|
|
|
}
|
2012-12-24 23:05:42 +04:00
|
|
|
return (npf_cache_all(npc, nbuf) & NPC_LAYER4) != 0;
|
2012-09-16 17:44:14 +04:00
|
|
|
|
|
|
|
case ICMP_ECHOREPLY:
|
|
|
|
case ICMP_ECHO:
|
|
|
|
case ICMP_TSTAMP:
|
|
|
|
case ICMP_TSTAMPREPLY:
|
|
|
|
case ICMP_IREQ:
|
|
|
|
case ICMP_IREQREPLY:
|
2012-12-24 23:05:42 +04:00
|
|
|
/* Should contain ICMP query ID - ensure. */
|
2012-09-16 17:44:14 +04:00
|
|
|
offby = offsetof(struct icmp, icmp_id);
|
2012-12-24 23:05:42 +04:00
|
|
|
if (!nbuf_advance(nbuf, offby, sizeof(uint16_t))) {
|
2012-09-16 17:44:14 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
npc->npc_info |= NPC_ICMP_ID;
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
break;
|
2012-07-20 01:52:29 +04:00
|
|
|
}
|
2012-09-16 17:44:14 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
2012-12-24 23:05:42 +04:00
|
|
|
npfa_icmp6_inspect(const int type, npf_cache_t *npc, nbuf_t *nbuf)
|
2012-09-16 17:44:14 +04:00
|
|
|
{
|
|
|
|
u_int offby;
|
|
|
|
|
|
|
|
/* Per RFC 4443. */
|
|
|
|
switch (type) {
|
|
|
|
case ICMP6_DST_UNREACH:
|
|
|
|
case ICMP6_PACKET_TOO_BIG:
|
|
|
|
case ICMP6_TIME_EXCEEDED:
|
|
|
|
case ICMP6_PARAM_PROB:
|
2012-12-24 23:05:42 +04:00
|
|
|
if (npc == NULL) {
|
2012-09-16 17:44:14 +04:00
|
|
|
return false;
|
|
|
|
}
|
2012-12-24 23:05:42 +04:00
|
|
|
/* Should contain original IP header. */
|
|
|
|
if (!nbuf_advance(nbuf, sizeof(struct icmp6_hdr), 0)) {
|
2012-09-16 17:44:14 +04:00
|
|
|
return false;
|
2010-08-22 22:56:18 +04:00
|
|
|
}
|
2012-12-24 23:05:42 +04:00
|
|
|
return (npf_cache_all(npc, nbuf) & NPC_LAYER4) != 0;
|
2012-09-16 17:44:14 +04:00
|
|
|
|
|
|
|
case ICMP6_ECHO_REQUEST:
|
|
|
|
case ICMP6_ECHO_REPLY:
|
2012-12-24 23:05:42 +04:00
|
|
|
/* Should contain ICMP query ID - ensure. */
|
2012-09-16 17:44:14 +04:00
|
|
|
offby = offsetof(struct icmp6_hdr, icmp6_id);
|
2012-12-24 23:05:42 +04:00
|
|
|
if (!nbuf_advance(nbuf, offby, sizeof(uint16_t))) {
|
2012-09-16 17:44:14 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
npc->npc_info |= NPC_ICMP_ID;
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
break;
|
2010-08-22 22:56:18 +04:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2012-12-24 23:05:42 +04:00
|
|
|
* npfa_icmp_session: ALG ICMP inspector.
|
|
|
|
*
|
|
|
|
* => Returns true if "enpc" is filled.
|
2010-08-22 22:56:18 +04:00
|
|
|
*/
|
|
|
|
static bool
|
2012-12-24 23:05:42 +04:00
|
|
|
npfa_icmp_inspect(npf_cache_t *npc, nbuf_t *nbuf, npf_cache_t *enpc)
|
2010-08-22 22:56:18 +04:00
|
|
|
{
|
2012-09-16 17:44:14 +04:00
|
|
|
bool ret;
|
|
|
|
|
2012-12-24 23:05:42 +04:00
|
|
|
KASSERT(npf_iscached(npc, NPC_IP46));
|
2010-11-11 09:30:39 +03:00
|
|
|
KASSERT(npf_iscached(npc, NPC_ICMP));
|
2010-08-22 22:56:18 +04:00
|
|
|
|
|
|
|
/* Advance to ICMP header. */
|
2012-12-24 23:05:42 +04:00
|
|
|
nbuf_reset(nbuf);
|
2013-02-09 07:35:31 +04:00
|
|
|
if (!nbuf_advance(nbuf, npc->npc_hlen, 0)) {
|
2010-08-22 22:56:18 +04:00
|
|
|
return false;
|
|
|
|
}
|
2012-12-24 23:05:42 +04:00
|
|
|
enpc->npc_info = 0;
|
2010-08-22 22:56:18 +04:00
|
|
|
|
2012-09-16 17:44:14 +04:00
|
|
|
/*
|
2012-12-24 23:05:42 +04:00
|
|
|
* Inspect the ICMP packet. The relevant data might be in the
|
|
|
|
* embedded packet. Fill the "enpc" cache, if so.
|
2012-09-16 17:44:14 +04:00
|
|
|
*/
|
|
|
|
if (npf_iscached(npc, NPC_IP4)) {
|
2012-12-24 23:05:42 +04:00
|
|
|
const struct icmp *ic = npc->npc_l4.icmp;
|
|
|
|
ret = npfa_icmp4_inspect(ic->icmp_type, enpc, nbuf);
|
2012-09-16 17:44:14 +04:00
|
|
|
} else if (npf_iscached(npc, NPC_IP6)) {
|
2012-12-24 23:05:42 +04:00
|
|
|
const struct icmp6_hdr *ic6 = npc->npc_l4.icmp6;
|
|
|
|
ret = npfa_icmp6_inspect(ic6->icmp6_type, enpc, nbuf);
|
2012-09-16 17:44:14 +04:00
|
|
|
} else {
|
|
|
|
ret = false;
|
|
|
|
}
|
|
|
|
if (!ret) {
|
2010-08-22 22:56:18 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-12-24 23:05:42 +04:00
|
|
|
/* ICMP ID is the original packet, just indicate it. */
|
|
|
|
if (npf_iscached(enpc, NPC_ICMP_ID)) {
|
2010-11-11 09:30:39 +03:00
|
|
|
npc->npc_info |= NPC_ICMP_ID;
|
|
|
|
return false;
|
2010-08-22 22:56:18 +04:00
|
|
|
}
|
2010-11-11 09:30:39 +03:00
|
|
|
|
2012-12-24 23:05:42 +04:00
|
|
|
/* Indicate that embedded packet is in the cache. */
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static npf_session_t *
|
|
|
|
npfa_icmp_session(npf_cache_t *npc, nbuf_t *nbuf, int di)
|
|
|
|
{
|
|
|
|
npf_cache_t enpc;
|
|
|
|
|
|
|
|
/* Inspect ICMP packet for an embedded packet. */
|
|
|
|
if (!npf_iscached(npc, NPC_ICMP))
|
|
|
|
return NULL;
|
|
|
|
if (!npfa_icmp_inspect(npc, nbuf, &enpc))
|
|
|
|
return NULL;
|
|
|
|
|
2010-11-11 09:30:39 +03:00
|
|
|
/*
|
2012-12-24 23:05:42 +04:00
|
|
|
* Invert the identifiers of the embedded packet.
|
|
|
|
* If it is ICMP, then ensure ICMP ID.
|
2010-11-11 09:30:39 +03:00
|
|
|
*/
|
2012-12-24 23:05:42 +04:00
|
|
|
union l4 {
|
|
|
|
struct tcphdr th;
|
|
|
|
struct udphdr uh;
|
|
|
|
} l4;
|
|
|
|
bool ret, forw;
|
|
|
|
|
|
|
|
#define SWAP(type, x, y) { type tmp = x; x = y; y = tmp; }
|
|
|
|
SWAP(npf_addr_t *, enpc.npc_srcip, enpc.npc_dstip);
|
|
|
|
|
2013-02-09 07:35:31 +04:00
|
|
|
switch (enpc.npc_proto) {
|
2012-12-24 23:05:42 +04:00
|
|
|
case IPPROTO_TCP:
|
|
|
|
l4.th.th_sport = enpc.npc_l4.tcp->th_dport;
|
|
|
|
l4.th.th_dport = enpc.npc_l4.tcp->th_sport;
|
|
|
|
enpc.npc_l4.tcp = &l4.th;
|
|
|
|
break;
|
|
|
|
case IPPROTO_UDP:
|
|
|
|
l4.uh.uh_sport = enpc.npc_l4.udp->uh_dport;
|
|
|
|
l4.uh.uh_dport = enpc.npc_l4.udp->uh_sport;
|
|
|
|
enpc.npc_l4.udp = &l4.uh;
|
|
|
|
break;
|
|
|
|
case IPPROTO_ICMP: {
|
|
|
|
const struct icmp *ic = enpc.npc_l4.icmp;
|
|
|
|
ret = npfa_icmp4_inspect(ic->icmp_type, &enpc, nbuf);
|
|
|
|
if (!ret || !npf_iscached(&enpc, NPC_ICMP_ID))
|
|
|
|
return false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case IPPROTO_ICMPV6: {
|
|
|
|
const struct icmp6_hdr *ic6 = enpc.npc_l4.icmp6;
|
|
|
|
ret = npfa_icmp6_inspect(ic6->icmp6_type, &enpc, nbuf);
|
|
|
|
if (!ret || !npf_iscached(&enpc, NPC_ICMP_ID))
|
|
|
|
return false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
2010-11-11 09:30:39 +03:00
|
|
|
|
2012-12-24 23:05:42 +04:00
|
|
|
/* Lookup for a session using embedded packet. */
|
|
|
|
return npf_session_lookup(&enpc, nbuf, di, &forw);
|
2010-08-22 22:56:18 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2012-12-24 23:05:42 +04:00
|
|
|
* npfa_icmp_nat: ALG inbound translation inspector, rewrite IP address
|
2010-08-22 22:56:18 +04:00
|
|
|
* in the IP header, which is embedded in ICMP packet.
|
|
|
|
*/
|
|
|
|
static bool
|
2012-12-24 23:05:42 +04:00
|
|
|
npfa_icmp_nat(npf_cache_t *npc, nbuf_t *nbuf, npf_nat_t *nt, int di)
|
2010-08-22 22:56:18 +04:00
|
|
|
{
|
2012-12-24 23:05:42 +04:00
|
|
|
npf_cache_t enpc;
|
2010-08-22 22:56:18 +04:00
|
|
|
|
2012-12-24 23:05:42 +04:00
|
|
|
if (di != PFIL_IN || !npf_iscached(npc, NPC_ICMP))
|
2010-08-22 22:56:18 +04:00
|
|
|
return false;
|
2012-12-24 23:05:42 +04:00
|
|
|
if (!npfa_icmp_inspect(npc, nbuf, &enpc))
|
|
|
|
return false;
|
|
|
|
|
2011-11-04 05:00:27 +04:00
|
|
|
KASSERT(npf_iscached(&enpc, NPC_IP46));
|
|
|
|
KASSERT(npf_iscached(&enpc, NPC_LAYER4));
|
2012-12-24 23:05:42 +04:00
|
|
|
|
|
|
|
struct icmp *ic = npc->npc_l4.icmp;
|
|
|
|
uint16_t cksum = ic->icmp_cksum;
|
|
|
|
|
|
|
|
CTASSERT(offsetof(struct icmp, icmp_cksum) ==
|
|
|
|
offsetof(struct icmp6_hdr, icmp6_cksum));
|
2010-08-22 22:56:18 +04:00
|
|
|
|
2011-01-18 23:33:45 +03:00
|
|
|
/*
|
2012-12-24 23:05:42 +04:00
|
|
|
* Retrieve the original address and port, then calculate ICMP
|
|
|
|
* checksum for these changes in the embedded packet. While data
|
|
|
|
* is not rewritten in the cache, save IP and TCP/UDP checksums.
|
2011-01-18 23:33:45 +03:00
|
|
|
*/
|
2013-02-09 07:35:31 +04:00
|
|
|
const int proto = enpc.npc_proto;
|
2012-12-24 23:05:42 +04:00
|
|
|
uint16_t ipcksum = 0, l4cksum = 0;
|
2011-01-18 23:33:45 +03:00
|
|
|
npf_addr_t *addr;
|
|
|
|
in_port_t port;
|
|
|
|
|
|
|
|
npf_nat_getorig(nt, &addr, &port);
|
2010-11-11 09:30:39 +03:00
|
|
|
|
2012-12-24 23:05:42 +04:00
|
|
|
if (npf_iscached(&enpc, NPC_IP4)) {
|
|
|
|
const struct ip *eip = enpc.npc_ip.v4;
|
|
|
|
ipcksum = eip->ip_sum;
|
|
|
|
}
|
|
|
|
cksum = npf_addr_cksum(cksum, enpc.npc_alen, enpc.npc_srcip, addr);
|
|
|
|
|
|
|
|
switch (proto) {
|
|
|
|
case IPPROTO_TCP: {
|
|
|
|
const struct tcphdr *th = enpc.npc_l4.tcp;
|
2011-01-18 23:33:45 +03:00
|
|
|
cksum = npf_fixup16_cksum(cksum, th->th_sport, port);
|
2010-11-11 09:30:39 +03:00
|
|
|
l4cksum = th->th_sum;
|
2012-12-24 23:05:42 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case IPPROTO_UDP: {
|
|
|
|
const struct udphdr *uh = enpc.npc_l4.udp;
|
2011-01-18 23:33:45 +03:00
|
|
|
cksum = npf_fixup16_cksum(cksum, uh->uh_sport, port);
|
2010-11-11 09:30:39 +03:00
|
|
|
l4cksum = uh->uh_sum;
|
2012-12-24 23:05:42 +04:00
|
|
|
break;
|
2010-11-11 09:30:39 +03:00
|
|
|
}
|
2012-12-24 23:05:42 +04:00
|
|
|
case IPPROTO_ICMP:
|
|
|
|
case IPPROTO_ICMPV6:
|
|
|
|
break;
|
|
|
|
default:
|
2010-08-22 22:56:18 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-11-11 09:30:39 +03:00
|
|
|
/*
|
2012-12-24 23:05:42 +04:00
|
|
|
* Rewrite the source IP address and port of the embedded IP header,
|
|
|
|
* which represents the original packet, therefore passing PFIL_OUT.
|
|
|
|
* This updates the checksums in the embedded packet.
|
2010-11-11 09:30:39 +03:00
|
|
|
*/
|
2012-12-24 23:05:42 +04:00
|
|
|
if (npf_nat_translate(&enpc, nbuf, nt, false, PFIL_OUT)) {
|
2010-08-22 22:56:18 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2012-12-24 23:05:42 +04:00
|
|
|
* Finish calculation of the ICMP checksum: include the checksum
|
|
|
|
* change in the embedded packet.
|
2010-08-22 22:56:18 +04:00
|
|
|
*/
|
2012-12-24 23:05:42 +04:00
|
|
|
if (npf_iscached(&enpc, NPC_IP4)) {
|
|
|
|
const struct ip *eip = enpc.npc_ip.v4;
|
|
|
|
cksum = npf_fixup16_cksum(cksum, ipcksum, eip->ip_sum);
|
|
|
|
}
|
|
|
|
switch (proto) {
|
|
|
|
case IPPROTO_TCP: {
|
|
|
|
const struct tcphdr *th = enpc.npc_l4.tcp;
|
2010-11-11 09:30:39 +03:00
|
|
|
cksum = npf_fixup16_cksum(cksum, l4cksum, th->th_sum);
|
2012-12-24 23:05:42 +04:00
|
|
|
break;
|
2010-08-22 22:56:18 +04:00
|
|
|
}
|
2012-12-24 23:05:42 +04:00
|
|
|
case IPPROTO_UDP:
|
|
|
|
if (l4cksum) {
|
|
|
|
const struct udphdr *uh = enpc.npc_l4.udp;
|
|
|
|
cksum = npf_fixup16_cksum(cksum, l4cksum, uh->uh_sum);
|
|
|
|
}
|
|
|
|
break;
|
2011-01-18 23:33:45 +03:00
|
|
|
}
|
2012-12-24 23:05:42 +04:00
|
|
|
ic->icmp_cksum = cksum;
|
2011-01-18 23:33:45 +03:00
|
|
|
return true;
|
2010-08-22 22:56:18 +04:00
|
|
|
}
|