npf_sendpkt.c revision 1.5 1 /* $NetBSD: npf_sendpkt.c,v 1.5 2011/11/04 01:00:27 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.5 2011/11/04 01:00:27 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 (npc->npc_info & NPC_IP4)
83 len = sizeof(struct ip) + sizeof(struct tcphdr);
84 else
85 len = sizeof(struct ip6_hdr) + sizeof(struct tcphdr);
86
87 m = m_gethdr(M_DONTWAIT, MT_HEADER);
88 if (m == NULL) {
89 return ENOMEM;
90 }
91 m->m_data += max_linkhdr;
92 m->m_len = len;
93 m->m_pkthdr.len = len;
94
95 if (npc->npc_info & NPC_IP4) {
96 struct ip *oip = &npc->npc_ip.v4;
97 ip = mtod(m, struct ip *);
98 memset(ip, 0, len);
99
100 /*
101 * First fill of IPv4 header, for TCP checksum.
102 * Note: IP length contains TCP header length.
103 */
104 ip->ip_p = IPPROTO_TCP;
105 ip->ip_src.s_addr = oip->ip_dst.s_addr;
106 ip->ip_dst.s_addr = oip->ip_src.s_addr;
107 ip->ip_len = htons(sizeof(struct tcphdr));
108
109 th = (struct tcphdr *)(ip + 1);
110 } else {
111 struct ip6_hdr *oip = &npc->npc_ip.v6;
112 ip6 = mtod(m, struct ip6_hdr *);
113 memset(ip6, 0, len);
114
115 ip6->ip6_nxt = IPPROTO_TCP;
116 ip6->ip6_hlim = IPV6_DEFHLIM;
117 memcpy(&ip6->ip6_src, &oip->ip6_dst, sizeof(struct in6_addr));
118 memcpy(&ip6->ip6_dst, &oip->ip6_src, sizeof(struct in6_addr));
119 ip6->ip6_plen = htons(len);
120 ip6->ip6_vfc = IPV6_VERSION;
121
122 th = (struct tcphdr *)(ip6 + 1);
123 }
124
125 /* Construct TCP header and compute the checksum. */
126 th->th_sport = oth->th_dport;
127 th->th_dport = oth->th_sport;
128 th->th_seq = htonl(ack);
129 if (oth->th_flags & TH_SYN) {
130 tcpdlen++;
131 }
132 th->th_ack = htonl(seq + tcpdlen);
133 th->th_off = sizeof(struct tcphdr) >> 2;
134 th->th_flags = TH_ACK | TH_RST;
135
136 if (npc->npc_info & NPC_IP4) {
137 th->th_sum = in_cksum(m, len);
138
139 /* Second fill of IPv4 header, fill correct IP length. */
140 ip->ip_v = IPVERSION;
141 ip->ip_hl = sizeof(struct ip) >> 2;
142 ip->ip_tos = IPTOS_LOWDELAY;
143 ip->ip_len = htons(len);
144 ip->ip_ttl = DEFAULT_IP_TTL;
145 } else {
146 th->th_sum = in6_cksum(m, IPPROTO_TCP, sizeof(struct ip6_hdr), sizeof(struct tcphdr));
147 }
148
149 /* Pass to IP layer. */
150 if (npc->npc_info & NPC_IP4) {
151 return ip_output(m, NULL, NULL, IP_FORWARDING, NULL, NULL);
152 } else {
153 return ip6_output(m, NULL, NULL, IPV6_FORWARDING, NULL, NULL, NULL);
154 }
155 }
156
157 /*
158 * npf_return_icmp: return an ICMP error.
159 */
160 static int
161 npf_return_icmp(npf_cache_t *npc, nbuf_t *nbuf)
162 {
163 struct mbuf *m = nbuf;
164
165 if (npf_iscached(npc, NPC_IP4)) {
166 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_ADMIN_PROHIBIT, 0, 0);
167 } else {
168 KASSERT(npf_iscached(npc, NPC_IP6));
169 icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADMIN, 0);
170 }
171 return 0;
172 }
173
174 /*
175 * npf_return_block: return TCP reset or ICMP host unreachable packet.
176 * TODO: user should be able to specify exact ICMP error codes in config
177 */
178 void
179 npf_return_block(npf_cache_t *npc, nbuf_t *nbuf, const int retfl)
180 {
181 void *n_ptr = nbuf_dataptr(nbuf);
182
183 if (!npf_iscached(npc, NPC_IP46) && !npf_fetch_ip(npc, nbuf, n_ptr)) {
184 return;
185 }
186 switch (npf_cache_ipproto(npc)) {
187 case IPPROTO_TCP:
188 if (retfl & NPF_RULE_RETRST) {
189 if (!npf_fetch_tcp(npc, nbuf, n_ptr)) {
190 return;
191 }
192 (void)npf_return_tcp(npc, nbuf);
193 }
194 break;
195 case IPPROTO_UDP:
196 if (retfl & NPF_RULE_RETICMP) {
197 (void)npf_return_icmp(npc, nbuf);
198 }
199 break;
200 }
201 }
202