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