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