Home | History | Annotate | Line # | Download | only in npf
npf_sendpkt.c revision 1.6
      1  1.6  zoltan /*	$NetBSD: npf_sendpkt.c,v 1.6 2011/11/05 10:23:26 zoltan Exp $	*/
      2  1.1   rmind 
      3  1.1   rmind /*-
      4  1.1   rmind  * Copyright (c) 2010 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.6  zoltan __KERNEL_RCSID(0, "$NetBSD: npf_sendpkt.c,v 1.6 2011/11/05 10:23:26 zoltan Exp $");
     38  1.1   rmind 
     39  1.1   rmind #include <sys/param.h>
     40  1.1   rmind #include <sys/kernel.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.5  zoltan #include <netinet/ip6.h>
     49  1.5  zoltan #include <netinet/icmp6.h>
     50  1.5  zoltan #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.1   rmind /*
     58  1.1   rmind  * npf_return_tcp: return a TCP reset (RST) packet.
     59  1.1   rmind  */
     60  1.1   rmind static int
     61  1.1   rmind npf_return_tcp(npf_cache_t *npc, nbuf_t *nbuf)
     62  1.1   rmind {
     63  1.1   rmind 	struct mbuf *m;
     64  1.5  zoltan 	struct ip *ip = NULL;
     65  1.5  zoltan 	struct ip6_hdr *ip6 = NULL;
     66  1.3   rmind 	struct tcphdr *oth, *th;
     67  1.1   rmind 	tcp_seq seq, ack;
     68  1.3   rmind 	int tcpdlen, len;
     69  1.3   rmind 	uint32_t win;
     70  1.1   rmind 
     71  1.1   rmind 	/* Fetch relevant data. */
     72  1.5  zoltan 	KASSERT(npf_iscached(npc, NPC_IP46));
     73  1.5  zoltan 	KASSERT(npf_iscached(npc, NPC_LAYER4));
     74  1.5  zoltan 	tcpdlen = npf_tcpsaw(npc, nbuf, &seq, &ack, &win);
     75  1.3   rmind 	oth = &npc->npc_l4.tcp;
     76  1.3   rmind 
     77  1.3   rmind 	if (oth->th_flags & TH_RST) {
     78  1.1   rmind 		return 0;
     79  1.1   rmind 	}
     80  1.1   rmind 
     81  1.1   rmind 	/* Create and setup a network buffer. */
     82  1.6  zoltan 	if (npf_iscached(npc, NPC_IP4))
     83  1.5  zoltan 		len = sizeof(struct ip) + sizeof(struct tcphdr);
     84  1.6  zoltan 	else {
     85  1.6  zoltan 		KASSERT(npf_iscached(npc, NPC_IP6));
     86  1.5  zoltan 		len = sizeof(struct ip6_hdr) + sizeof(struct tcphdr);
     87  1.6  zoltan 	}
     88  1.5  zoltan 
     89  1.1   rmind 	m = m_gethdr(M_DONTWAIT, MT_HEADER);
     90  1.1   rmind 	if (m == NULL) {
     91  1.1   rmind 		return ENOMEM;
     92  1.1   rmind 	}
     93  1.1   rmind 	m->m_data += max_linkhdr;
     94  1.1   rmind 	m->m_len = len;
     95  1.1   rmind 	m->m_pkthdr.len = len;
     96  1.1   rmind 
     97  1.6  zoltan 	if (npf_iscached(npc, NPC_IP4)) {
     98  1.5  zoltan 		struct ip *oip = &npc->npc_ip.v4;
     99  1.5  zoltan 		ip = mtod(m, struct ip *);
    100  1.5  zoltan 		memset(ip, 0, len);
    101  1.5  zoltan 
    102  1.5  zoltan 		/*
    103  1.5  zoltan 	 	* First fill of IPv4 header, for TCP checksum.
    104  1.5  zoltan 	 	* Note: IP length contains TCP header length.
    105  1.5  zoltan 	 	*/
    106  1.5  zoltan 		ip->ip_p = IPPROTO_TCP;
    107  1.5  zoltan 		ip->ip_src.s_addr = oip->ip_dst.s_addr;
    108  1.5  zoltan 		ip->ip_dst.s_addr = oip->ip_src.s_addr;
    109  1.5  zoltan 		ip->ip_len = htons(sizeof(struct tcphdr));
    110  1.5  zoltan 
    111  1.5  zoltan 		th = (struct tcphdr *)(ip + 1);
    112  1.5  zoltan 	} else {
    113  1.6  zoltan 		KASSERT(npf_iscached(npc, NPC_IP6));
    114  1.5  zoltan 		struct ip6_hdr *oip = &npc->npc_ip.v6;
    115  1.5  zoltan 		ip6 = mtod(m, struct ip6_hdr *);
    116  1.5  zoltan 		memset(ip6, 0, len);
    117  1.5  zoltan 
    118  1.5  zoltan 		ip6->ip6_nxt = IPPROTO_TCP;
    119  1.5  zoltan 		ip6->ip6_hlim = IPV6_DEFHLIM;
    120  1.5  zoltan 		memcpy(&ip6->ip6_src, &oip->ip6_dst, sizeof(struct in6_addr));
    121  1.5  zoltan 		memcpy(&ip6->ip6_dst, &oip->ip6_src, sizeof(struct in6_addr));
    122  1.5  zoltan 		ip6->ip6_plen = htons(len);
    123  1.5  zoltan 		ip6->ip6_vfc = IPV6_VERSION;
    124  1.1   rmind 
    125  1.5  zoltan 		th = (struct tcphdr *)(ip6 + 1);
    126  1.5  zoltan 	}
    127  1.1   rmind 
    128  1.1   rmind 	/* Construct TCP header and compute the checksum. */
    129  1.3   rmind 	th->th_sport = oth->th_dport;
    130  1.3   rmind 	th->th_dport = oth->th_sport;
    131  1.1   rmind 	th->th_seq = htonl(ack);
    132  1.3   rmind 	if (oth->th_flags & TH_SYN) {
    133  1.1   rmind 		tcpdlen++;
    134  1.1   rmind 	}
    135  1.1   rmind 	th->th_ack = htonl(seq + tcpdlen);
    136  1.1   rmind 	th->th_off = sizeof(struct tcphdr) >> 2;
    137  1.1   rmind 	th->th_flags = TH_ACK | TH_RST;
    138  1.1   rmind 
    139  1.6  zoltan 	if (npf_iscached(npc, NPC_IP4)) {
    140  1.5  zoltan 		th->th_sum = in_cksum(m, len);
    141  1.5  zoltan 
    142  1.5  zoltan 		 /* Second fill of IPv4 header, fill correct IP length. */
    143  1.5  zoltan 		ip->ip_v = IPVERSION;
    144  1.5  zoltan 		ip->ip_hl = sizeof(struct ip) >> 2;
    145  1.5  zoltan 		ip->ip_tos = IPTOS_LOWDELAY;
    146  1.5  zoltan 		ip->ip_len = htons(len);
    147  1.5  zoltan 		ip->ip_ttl = DEFAULT_IP_TTL;
    148  1.5  zoltan 	} else {
    149  1.6  zoltan 		KASSERT(npf_iscached(npc, NPC_IP6));
    150  1.6  zoltan #ifdef INET6
    151  1.5  zoltan 		th->th_sum = in6_cksum(m, IPPROTO_TCP, sizeof(struct ip6_hdr), sizeof(struct tcphdr));
    152  1.6  zoltan #else
    153  1.6  zoltan 		KASSERT(false);
    154  1.6  zoltan #endif
    155  1.5  zoltan 	}
    156  1.1   rmind 
    157  1.1   rmind 	/* Pass to IP layer. */
    158  1.5  zoltan 	if (npc->npc_info & NPC_IP4) {
    159  1.5  zoltan 		return ip_output(m, NULL, NULL, IP_FORWARDING, NULL, NULL);
    160  1.5  zoltan 	} else {
    161  1.6  zoltan #ifdef INET6
    162  1.5  zoltan 		return ip6_output(m, NULL, NULL, IPV6_FORWARDING, NULL, NULL, NULL);
    163  1.6  zoltan #else
    164  1.6  zoltan 		KASSERT(false);
    165  1.6  zoltan 		return 0;
    166  1.6  zoltan #endif
    167  1.5  zoltan 	}
    168  1.1   rmind }
    169  1.1   rmind 
    170  1.1   rmind /*
    171  1.1   rmind  * npf_return_icmp: return an ICMP error.
    172  1.1   rmind  */
    173  1.1   rmind static int
    174  1.5  zoltan npf_return_icmp(npf_cache_t *npc, nbuf_t *nbuf)
    175  1.1   rmind {
    176  1.1   rmind 	struct mbuf *m = nbuf;
    177  1.1   rmind 
    178  1.5  zoltan 	if (npf_iscached(npc, NPC_IP4)) {
    179  1.5  zoltan 		icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_ADMIN_PROHIBIT, 0, 0);
    180  1.5  zoltan 	} else {
    181  1.5  zoltan 		KASSERT(npf_iscached(npc, NPC_IP6));
    182  1.6  zoltan #ifdef INET6
    183  1.5  zoltan 		icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADMIN, 0);
    184  1.6  zoltan #else
    185  1.6  zoltan 		KASSERT(false);
    186  1.6  zoltan #endif
    187  1.5  zoltan 	}
    188  1.1   rmind 	return 0;
    189  1.1   rmind }
    190  1.1   rmind 
    191  1.1   rmind /*
    192  1.1   rmind  * npf_return_block: return TCP reset or ICMP host unreachable packet.
    193  1.5  zoltan  * TODO: user should be able to specify exact ICMP error codes in config
    194  1.1   rmind  */
    195  1.1   rmind void
    196  1.1   rmind npf_return_block(npf_cache_t *npc, nbuf_t *nbuf, const int retfl)
    197  1.1   rmind {
    198  1.1   rmind 	void *n_ptr = nbuf_dataptr(nbuf);
    199  1.1   rmind 
    200  1.3   rmind 	if (!npf_iscached(npc, NPC_IP46) && !npf_fetch_ip(npc, nbuf, n_ptr)) {
    201  1.1   rmind 		return;
    202  1.1   rmind 	}
    203  1.3   rmind 	switch (npf_cache_ipproto(npc)) {
    204  1.1   rmind 	case IPPROTO_TCP:
    205  1.3   rmind 		if (retfl & NPF_RULE_RETRST) {
    206  1.3   rmind 			if (!npf_fetch_tcp(npc, nbuf, n_ptr)) {
    207  1.3   rmind 				return;
    208  1.3   rmind 			}
    209  1.3   rmind 			(void)npf_return_tcp(npc, nbuf);
    210  1.3   rmind 		}
    211  1.1   rmind 		break;
    212  1.1   rmind 	case IPPROTO_UDP:
    213  1.3   rmind 		if (retfl & NPF_RULE_RETICMP) {
    214  1.5  zoltan 			(void)npf_return_icmp(npc, nbuf);
    215  1.3   rmind 		}
    216  1.1   rmind 		break;
    217  1.1   rmind 	}
    218  1.1   rmind }
    219