npf_sendpkt.c revision 1.6 1 /* $NetBSD: npf_sendpkt.c,v 1.6 2011/11/05 10:23:26 zoltan 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.6 2011/11/05 10:23:26 zoltan 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 ip = mtod(m, struct ip *);
100 memset(ip, 0, len);
101
102 /*
103 * First fill of IPv4 header, for TCP checksum.
104 * Note: IP length contains TCP header length.
105 */
106 ip->ip_p = IPPROTO_TCP;
107 ip->ip_src.s_addr = oip->ip_dst.s_addr;
108 ip->ip_dst.s_addr = oip->ip_src.s_addr;
109 ip->ip_len = htons(sizeof(struct tcphdr));
110
111 th = (struct tcphdr *)(ip + 1);
112 } else {
113 KASSERT(npf_iscached(npc, NPC_IP6));
114 struct ip6_hdr *oip = &npc->npc_ip.v6;
115 ip6 = mtod(m, struct ip6_hdr *);
116 memset(ip6, 0, len);
117
118 ip6->ip6_nxt = IPPROTO_TCP;
119 ip6->ip6_hlim = IPV6_DEFHLIM;
120 memcpy(&ip6->ip6_src, &oip->ip6_dst, sizeof(struct in6_addr));
121 memcpy(&ip6->ip6_dst, &oip->ip6_src, sizeof(struct in6_addr));
122 ip6->ip6_plen = htons(len);
123 ip6->ip6_vfc = IPV6_VERSION;
124
125 th = (struct tcphdr *)(ip6 + 1);
126 }
127
128 /* Construct TCP header and compute the checksum. */
129 th->th_sport = oth->th_dport;
130 th->th_dport = oth->th_sport;
131 th->th_seq = htonl(ack);
132 if (oth->th_flags & TH_SYN) {
133 tcpdlen++;
134 }
135 th->th_ack = htonl(seq + tcpdlen);
136 th->th_off = sizeof(struct tcphdr) >> 2;
137 th->th_flags = TH_ACK | TH_RST;
138
139 if (npf_iscached(npc, NPC_IP4)) {
140 th->th_sum = in_cksum(m, len);
141
142 /* Second fill of IPv4 header, fill correct IP length. */
143 ip->ip_v = IPVERSION;
144 ip->ip_hl = sizeof(struct ip) >> 2;
145 ip->ip_tos = IPTOS_LOWDELAY;
146 ip->ip_len = htons(len);
147 ip->ip_ttl = DEFAULT_IP_TTL;
148 } else {
149 KASSERT(npf_iscached(npc, NPC_IP6));
150 #ifdef INET6
151 th->th_sum = in6_cksum(m, IPPROTO_TCP, sizeof(struct ip6_hdr), sizeof(struct tcphdr));
152 #else
153 KASSERT(false);
154 #endif
155 }
156
157 /* Pass to IP layer. */
158 if (npc->npc_info & NPC_IP4) {
159 return ip_output(m, NULL, NULL, IP_FORWARDING, NULL, NULL);
160 } else {
161 #ifdef INET6
162 return ip6_output(m, NULL, NULL, IPV6_FORWARDING, NULL, NULL, NULL);
163 #else
164 KASSERT(false);
165 return 0;
166 #endif
167 }
168 }
169
170 /*
171 * npf_return_icmp: return an ICMP error.
172 */
173 static int
174 npf_return_icmp(npf_cache_t *npc, nbuf_t *nbuf)
175 {
176 struct mbuf *m = nbuf;
177
178 if (npf_iscached(npc, NPC_IP4)) {
179 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_ADMIN_PROHIBIT, 0, 0);
180 } else {
181 KASSERT(npf_iscached(npc, NPC_IP6));
182 #ifdef INET6
183 icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADMIN, 0);
184 #else
185 KASSERT(false);
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