npf_sendpkt.c revision 1.1 1 /* $NetBSD: npf_sendpkt.c,v 1.1 2010/09/16 04:53:27 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 #ifdef _KERNEL
37 #include <sys/cdefs.h>
38 __KERNEL_RCSID(0, "$NetBSD: npf_sendpkt.c,v 1.1 2010/09/16 04:53:27 rmind Exp $");
39
40 #include <sys/param.h>
41 #include <sys/kernel.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 #endif
50 #include <sys/mbuf.h>
51
52 #include "npf_impl.h"
53
54 #define DEFAULT_IP_TTL (ip_defttl)
55
56 /*
57 * npf_fetch_seqack: fetch TCP data length, SEQ and ACK numbers.
58 *
59 * NOTE: Returns in host byte-order.
60 */
61 static inline bool
62 npf_fetch_seqack(nbuf_t *nbuf, npf_cache_t *npc,
63 tcp_seq *seq, tcp_seq *ack, size_t *tcpdlen)
64 {
65 void *n_ptr = nbuf_dataptr(nbuf);
66 u_int offby;
67 tcp_seq seqack[2];
68 uint16_t iplen;
69 uint8_t toff;
70
71 /* Fetch total length of IP. */
72 offby = offsetof(struct ip, ip_len);
73 if ((n_ptr = nbuf_advance(&nbuf, n_ptr, offby)) == NULL)
74 return false;
75 if (nbuf_fetch_datum(nbuf, n_ptr, sizeof(uint16_t), &iplen))
76 return false;
77
78 /* Fetch SEQ and ACK numbers. */
79 offby = (npc->npc_hlen - offby) + offsetof(struct tcphdr, th_seq);
80 if ((n_ptr = nbuf_advance(&nbuf, n_ptr, offby)) == NULL)
81 return false;
82 if (nbuf_fetch_datum(nbuf, n_ptr, sizeof(seqack), seqack))
83 return false;
84
85 /* Fetch TCP data offset (header length) value. */
86 offby = sizeof(seqack);
87 if ((n_ptr = nbuf_advance(&nbuf, n_ptr, offby)) == NULL)
88 return false;
89 if (nbuf_fetch_datum(nbuf, n_ptr, sizeof(uint8_t), &toff))
90 return false;
91 toff >>= 4;
92
93 *seq = ntohl(seqack[0]);
94 *ack = ntohl(seqack[1]);
95 *tcpdlen = ntohs(iplen) - npc->npc_hlen - (toff << 2);
96 return true;
97 }
98
99 /*
100 * npf_return_tcp: return a TCP reset (RST) packet.
101 */
102 static int
103 npf_return_tcp(npf_cache_t *npc, nbuf_t *nbuf)
104 {
105 struct mbuf *m;
106 struct ip *ip;
107 struct tcphdr *th;
108 tcp_seq seq, ack;
109 size_t tcpdlen, len;
110
111 /* Fetch relevant data. */
112 if (!npf_iscached(npc, NPC_IP46 | NPC_ADDRS | NPC_PORTS) ||
113 !npf_fetch_seqack(nbuf, npc, &seq, &ack, &tcpdlen)) {
114 return EBADMSG;
115 }
116 if (npc->npc_tcp_flags & TH_RST) {
117 return 0;
118 }
119
120 /* Create and setup a network buffer. */
121 len = sizeof(struct ip) + sizeof(struct tcphdr);
122 m = m_gethdr(M_DONTWAIT, MT_HEADER);
123 if (m == NULL) {
124 return ENOMEM;
125 }
126 m->m_data += max_linkhdr;
127 m->m_len = len;
128 m->m_pkthdr.len = len;
129
130 ip = mtod(m, struct ip *);
131 memset(ip, 0, len);
132
133 /*
134 * First fill of IPv4 header, for TCP checksum.
135 * Note: IP length contains TCP header length.
136 */
137 ip->ip_p = IPPROTO_TCP;
138 ip->ip_src.s_addr = npc->npc_dstip;
139 ip->ip_dst.s_addr = npc->npc_srcip;
140 ip->ip_len = htons(sizeof(struct tcphdr));
141
142 /* Construct TCP header and compute the checksum. */
143 th = (struct tcphdr *)(ip + 1);
144 th->th_sport = npc->npc_dport;
145 th->th_dport = npc->npc_sport;
146 th->th_seq = htonl(ack);
147 if (npc->npc_tcp_flags & TH_SYN) {
148 tcpdlen++;
149 }
150 th->th_ack = htonl(seq + tcpdlen);
151 th->th_off = sizeof(struct tcphdr) >> 2;
152 th->th_flags = TH_ACK | TH_RST;
153 th->th_sum = in_cksum(m, len);
154
155 /* Second fill of IPv4 header, fill correct IP length. */
156 ip->ip_v = IPVERSION;
157 ip->ip_hl = sizeof(struct ip) >> 2;
158 ip->ip_tos = IPTOS_LOWDELAY;
159 ip->ip_len = htons(len);
160 ip->ip_off = htons(IP_DF);
161 ip->ip_ttl = DEFAULT_IP_TTL;
162
163 /* Pass to IP layer. */
164 return ip_output(m, NULL, NULL, IP_FORWARDING, NULL, NULL);
165 }
166
167 /*
168 * npf_return_icmp: return an ICMP error.
169 */
170 static int
171 npf_return_icmp(nbuf_t *nbuf)
172 {
173 struct mbuf *m = nbuf;
174
175 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_ADMIN_PROHIBIT, 0, 0);
176 return 0;
177 }
178
179 /*
180 * npf_return_block: return TCP reset or ICMP host unreachable packet.
181 */
182 void
183 npf_return_block(npf_cache_t *npc, nbuf_t *nbuf, const int retfl)
184 {
185 void *n_ptr = nbuf_dataptr(nbuf);
186 const int proto = npc->npc_proto;
187
188 if (!npf_iscached(npc, NPC_IP46) && !npf_ip4_proto(npc, nbuf, n_ptr))
189 return;
190 if ((proto == IPPROTO_TCP && (retfl & NPF_RULE_RETRST) == 0) ||
191 (proto == IPPROTO_UDP && (retfl & NPF_RULE_RETICMP) == 0)) {
192 return;
193 }
194 switch (proto) {
195 case IPPROTO_TCP:
196 (void)npf_return_tcp(npc, nbuf);
197 break;
198 case IPPROTO_UDP:
199 (void)npf_return_icmp(nbuf);
200 break;
201 }
202 }
203