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