Home | History | Annotate | Line # | Download | only in npf
npf_sendpkt.c revision 1.2
      1 /*	$NetBSD: npf_sendpkt.c,v 1.2 2010/09/25 00:25:31 rmind Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2010 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This material is based upon work partially supported by The
      8  * NetBSD Foundation under a contract with Mindaugas Rasiukevicius.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 /*
     33  * NPF module for packet construction routines.
     34  */
     35 
     36 #ifdef _KERNEL
     37 #include <sys/cdefs.h>
     38 __KERNEL_RCSID(0, "$NetBSD: npf_sendpkt.c,v 1.2 2010/09/25 00:25:31 rmind Exp $");
     39 
     40 #include <sys/param.h>
     41 #include <sys/kernel.h>
     42 
     43 #include <netinet/in_systm.h>
     44 #include <netinet/in.h>
     45 #include <netinet/ip.h>
     46 #include <netinet/ip_icmp.h>
     47 #include <netinet/ip_var.h>
     48 #include <netinet/tcp.h>
     49 #endif
     50 #include <sys/mbuf.h>
     51 
     52 #include "npf_impl.h"
     53 
     54 #define	DEFAULT_IP_TTL		(ip_defttl)
     55 
     56 /*
     57  * npf_fetch_seqack: fetch TCP data length, SEQ and ACK numbers.
     58  *
     59  * NOTE: Returns in host byte-order.
     60  */
     61 static inline bool
     62 npf_fetch_seqack(nbuf_t *nbuf, npf_cache_t *npc,
     63     tcp_seq *seq, tcp_seq *ack, size_t *tcpdlen)
     64 {
     65 	void *n_ptr = nbuf_dataptr(nbuf);
     66 	u_int offby;
     67 	tcp_seq seqack[2];
     68 	uint16_t iplen;
     69 	uint8_t toff;
     70 
     71 	/* Fetch total length of IP. */
     72 	offby = offsetof(struct ip, ip_len);
     73 	if (nbuf_advfetch(&nbuf, &n_ptr, offby, sizeof(uint16_t), &iplen))
     74 		return false;
     75 
     76 	/* Fetch SEQ and ACK numbers. */
     77 	offby = (npc->npc_hlen - offby) + offsetof(struct tcphdr, th_seq);
     78 	if (nbuf_advfetch(&nbuf, &n_ptr, offby, sizeof(seqack), seqack))
     79 		return false;
     80 
     81 	/* Fetch TCP data offset (header length) value. */
     82 	offby = sizeof(seqack);
     83 	if (nbuf_advfetch(&nbuf, &n_ptr, offby, sizeof(uint8_t), &toff))
     84 		return false;
     85 	toff >>= 4;
     86 
     87 	*seq = ntohl(seqack[0]);
     88 	*ack = ntohl(seqack[1]);
     89 	*tcpdlen = ntohs(iplen) - npc->npc_hlen - (toff << 2);
     90 	return true;
     91 }
     92 
     93 /*
     94  * npf_return_tcp: return a TCP reset (RST) packet.
     95  */
     96 static int
     97 npf_return_tcp(npf_cache_t *npc, nbuf_t *nbuf)
     98 {
     99 	struct mbuf *m;
    100 	struct ip *ip;
    101 	struct tcphdr *th;
    102 	tcp_seq seq, ack;
    103 	size_t tcpdlen, len;
    104 
    105 	/* Fetch relevant data. */
    106 	if (!npf_iscached(npc, NPC_IP46 | NPC_ADDRS | NPC_PORTS) ||
    107 	    !npf_fetch_seqack(nbuf, npc, &seq, &ack, &tcpdlen)) {
    108 		return EBADMSG;
    109 	}
    110 	if (npc->npc_tcp_flags & TH_RST) {
    111 		return 0;
    112 	}
    113 
    114 	/* Create and setup a network buffer. */
    115 	len = sizeof(struct ip) + sizeof(struct tcphdr);
    116 	m = m_gethdr(M_DONTWAIT, MT_HEADER);
    117 	if (m == NULL) {
    118 		return ENOMEM;
    119 	}
    120 	m->m_data += max_linkhdr;
    121 	m->m_len = len;
    122 	m->m_pkthdr.len = len;
    123 
    124 	ip = mtod(m, struct ip *);
    125 	memset(ip, 0, len);
    126 
    127 	/*
    128 	 * First fill of IPv4 header, for TCP checksum.
    129 	 * Note: IP length contains TCP header length.
    130 	 */
    131 	ip->ip_p = IPPROTO_TCP;
    132 	ip->ip_src.s_addr = npc->npc_dstip;
    133 	ip->ip_dst.s_addr = npc->npc_srcip;
    134 	ip->ip_len = htons(sizeof(struct tcphdr));
    135 
    136 	/* Construct TCP header and compute the checksum. */
    137 	th = (struct tcphdr *)(ip + 1);
    138 	th->th_sport = npc->npc_dport;
    139 	th->th_dport = npc->npc_sport;
    140 	th->th_seq = htonl(ack);
    141 	if (npc->npc_tcp_flags & TH_SYN) {
    142 		tcpdlen++;
    143 	}
    144 	th->th_ack = htonl(seq + tcpdlen);
    145 	th->th_off = sizeof(struct tcphdr) >> 2;
    146 	th->th_flags = TH_ACK | TH_RST;
    147 	th->th_sum = in_cksum(m, len);
    148 
    149 	/* Second fill of IPv4 header, fill correct IP length. */
    150 	ip->ip_v = IPVERSION;
    151 	ip->ip_hl = sizeof(struct ip) >> 2;
    152 	ip->ip_tos = IPTOS_LOWDELAY;
    153 	ip->ip_len = htons(len);
    154 	ip->ip_off = htons(IP_DF);
    155 	ip->ip_ttl = DEFAULT_IP_TTL;
    156 
    157 	/* Pass to IP layer. */
    158 	return ip_output(m, NULL, NULL, IP_FORWARDING, NULL, NULL);
    159 }
    160 
    161 /*
    162  * npf_return_icmp: return an ICMP error.
    163  */
    164 static int
    165 npf_return_icmp(nbuf_t *nbuf)
    166 {
    167 	struct mbuf *m = nbuf;
    168 
    169 	icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_ADMIN_PROHIBIT, 0, 0);
    170 	return 0;
    171 }
    172 
    173 /*
    174  * npf_return_block: return TCP reset or ICMP host unreachable packet.
    175  */
    176 void
    177 npf_return_block(npf_cache_t *npc, nbuf_t *nbuf, const int retfl)
    178 {
    179 	void *n_ptr = nbuf_dataptr(nbuf);
    180 	const int proto = npc->npc_proto;
    181 
    182 	if (!npf_iscached(npc, NPC_IP46) && !npf_ip4_proto(npc, nbuf, n_ptr))
    183 		return;
    184 	if ((proto == IPPROTO_TCP && (retfl & NPF_RULE_RETRST) == 0) ||
    185 	    (proto == IPPROTO_UDP && (retfl & NPF_RULE_RETICMP) == 0)) {
    186 		return;
    187 	}
    188 	switch (proto) {
    189 	case IPPROTO_TCP:
    190 		(void)npf_return_tcp(npc, nbuf);
    191 		break;
    192 	case IPPROTO_UDP:
    193 		(void)npf_return_icmp(nbuf);
    194 		break;
    195 	}
    196 }
    197