npf_sendpkt.c revision 1.1 1 1.1 rmind /* $NetBSD: npf_sendpkt.c,v 1.1 2010/09/16 04:53:27 rmind Exp $ */
2 1.1 rmind
3 1.1 rmind /*-
4 1.1 rmind * Copyright (c) 2010 The NetBSD Foundation, Inc.
5 1.1 rmind * All rights reserved.
6 1.1 rmind *
7 1.1 rmind * This material is based upon work partially supported by The
8 1.1 rmind * NetBSD Foundation under a contract with Mindaugas Rasiukevicius.
9 1.1 rmind *
10 1.1 rmind * Redistribution and use in source and binary forms, with or without
11 1.1 rmind * modification, are permitted provided that the following conditions
12 1.1 rmind * are met:
13 1.1 rmind * 1. Redistributions of source code must retain the above copyright
14 1.1 rmind * notice, this list of conditions and the following disclaimer.
15 1.1 rmind * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 rmind * notice, this list of conditions and the following disclaimer in the
17 1.1 rmind * documentation and/or other materials provided with the distribution.
18 1.1 rmind *
19 1.1 rmind * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 rmind * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 rmind * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 rmind * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 rmind * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 rmind * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 rmind * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 rmind * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 rmind * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 rmind * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 rmind * POSSIBILITY OF SUCH DAMAGE.
30 1.1 rmind */
31 1.1 rmind
32 1.1 rmind /*
33 1.1 rmind * NPF module for packet construction routines.
34 1.1 rmind */
35 1.1 rmind
36 1.1 rmind #ifdef _KERNEL
37 1.1 rmind #include <sys/cdefs.h>
38 1.1 rmind __KERNEL_RCSID(0, "$NetBSD: npf_sendpkt.c,v 1.1 2010/09/16 04:53:27 rmind Exp $");
39 1.1 rmind
40 1.1 rmind #include <sys/param.h>
41 1.1 rmind #include <sys/kernel.h>
42 1.1 rmind
43 1.1 rmind #include <netinet/in_systm.h>
44 1.1 rmind #include <netinet/in.h>
45 1.1 rmind #include <netinet/ip.h>
46 1.1 rmind #include <netinet/ip_icmp.h>
47 1.1 rmind #include <netinet/ip_var.h>
48 1.1 rmind #include <netinet/tcp.h>
49 1.1 rmind #endif
50 1.1 rmind #include <sys/mbuf.h>
51 1.1 rmind
52 1.1 rmind #include "npf_impl.h"
53 1.1 rmind
54 1.1 rmind #define DEFAULT_IP_TTL (ip_defttl)
55 1.1 rmind
56 1.1 rmind /*
57 1.1 rmind * npf_fetch_seqack: fetch TCP data length, SEQ and ACK numbers.
58 1.1 rmind *
59 1.1 rmind * NOTE: Returns in host byte-order.
60 1.1 rmind */
61 1.1 rmind static inline bool
62 1.1 rmind npf_fetch_seqack(nbuf_t *nbuf, npf_cache_t *npc,
63 1.1 rmind tcp_seq *seq, tcp_seq *ack, size_t *tcpdlen)
64 1.1 rmind {
65 1.1 rmind void *n_ptr = nbuf_dataptr(nbuf);
66 1.1 rmind u_int offby;
67 1.1 rmind tcp_seq seqack[2];
68 1.1 rmind uint16_t iplen;
69 1.1 rmind uint8_t toff;
70 1.1 rmind
71 1.1 rmind /* Fetch total length of IP. */
72 1.1 rmind offby = offsetof(struct ip, ip_len);
73 1.1 rmind if ((n_ptr = nbuf_advance(&nbuf, n_ptr, offby)) == NULL)
74 1.1 rmind return false;
75 1.1 rmind if (nbuf_fetch_datum(nbuf, n_ptr, sizeof(uint16_t), &iplen))
76 1.1 rmind return false;
77 1.1 rmind
78 1.1 rmind /* Fetch SEQ and ACK numbers. */
79 1.1 rmind offby = (npc->npc_hlen - offby) + offsetof(struct tcphdr, th_seq);
80 1.1 rmind if ((n_ptr = nbuf_advance(&nbuf, n_ptr, offby)) == NULL)
81 1.1 rmind return false;
82 1.1 rmind if (nbuf_fetch_datum(nbuf, n_ptr, sizeof(seqack), seqack))
83 1.1 rmind return false;
84 1.1 rmind
85 1.1 rmind /* Fetch TCP data offset (header length) value. */
86 1.1 rmind offby = sizeof(seqack);
87 1.1 rmind if ((n_ptr = nbuf_advance(&nbuf, n_ptr, offby)) == NULL)
88 1.1 rmind return false;
89 1.1 rmind if (nbuf_fetch_datum(nbuf, n_ptr, sizeof(uint8_t), &toff))
90 1.1 rmind return false;
91 1.1 rmind toff >>= 4;
92 1.1 rmind
93 1.1 rmind *seq = ntohl(seqack[0]);
94 1.1 rmind *ack = ntohl(seqack[1]);
95 1.1 rmind *tcpdlen = ntohs(iplen) - npc->npc_hlen - (toff << 2);
96 1.1 rmind return true;
97 1.1 rmind }
98 1.1 rmind
99 1.1 rmind /*
100 1.1 rmind * npf_return_tcp: return a TCP reset (RST) packet.
101 1.1 rmind */
102 1.1 rmind static int
103 1.1 rmind npf_return_tcp(npf_cache_t *npc, nbuf_t *nbuf)
104 1.1 rmind {
105 1.1 rmind struct mbuf *m;
106 1.1 rmind struct ip *ip;
107 1.1 rmind struct tcphdr *th;
108 1.1 rmind tcp_seq seq, ack;
109 1.1 rmind size_t tcpdlen, len;
110 1.1 rmind
111 1.1 rmind /* Fetch relevant data. */
112 1.1 rmind if (!npf_iscached(npc, NPC_IP46 | NPC_ADDRS | NPC_PORTS) ||
113 1.1 rmind !npf_fetch_seqack(nbuf, npc, &seq, &ack, &tcpdlen)) {
114 1.1 rmind return EBADMSG;
115 1.1 rmind }
116 1.1 rmind if (npc->npc_tcp_flags & TH_RST) {
117 1.1 rmind return 0;
118 1.1 rmind }
119 1.1 rmind
120 1.1 rmind /* Create and setup a network buffer. */
121 1.1 rmind len = sizeof(struct ip) + sizeof(struct tcphdr);
122 1.1 rmind m = m_gethdr(M_DONTWAIT, MT_HEADER);
123 1.1 rmind if (m == NULL) {
124 1.1 rmind return ENOMEM;
125 1.1 rmind }
126 1.1 rmind m->m_data += max_linkhdr;
127 1.1 rmind m->m_len = len;
128 1.1 rmind m->m_pkthdr.len = len;
129 1.1 rmind
130 1.1 rmind ip = mtod(m, struct ip *);
131 1.1 rmind memset(ip, 0, len);
132 1.1 rmind
133 1.1 rmind /*
134 1.1 rmind * First fill of IPv4 header, for TCP checksum.
135 1.1 rmind * Note: IP length contains TCP header length.
136 1.1 rmind */
137 1.1 rmind ip->ip_p = IPPROTO_TCP;
138 1.1 rmind ip->ip_src.s_addr = npc->npc_dstip;
139 1.1 rmind ip->ip_dst.s_addr = npc->npc_srcip;
140 1.1 rmind ip->ip_len = htons(sizeof(struct tcphdr));
141 1.1 rmind
142 1.1 rmind /* Construct TCP header and compute the checksum. */
143 1.1 rmind th = (struct tcphdr *)(ip + 1);
144 1.1 rmind th->th_sport = npc->npc_dport;
145 1.1 rmind th->th_dport = npc->npc_sport;
146 1.1 rmind th->th_seq = htonl(ack);
147 1.1 rmind if (npc->npc_tcp_flags & TH_SYN) {
148 1.1 rmind tcpdlen++;
149 1.1 rmind }
150 1.1 rmind th->th_ack = htonl(seq + tcpdlen);
151 1.1 rmind th->th_off = sizeof(struct tcphdr) >> 2;
152 1.1 rmind th->th_flags = TH_ACK | TH_RST;
153 1.1 rmind th->th_sum = in_cksum(m, len);
154 1.1 rmind
155 1.1 rmind /* Second fill of IPv4 header, fill correct IP length. */
156 1.1 rmind ip->ip_v = IPVERSION;
157 1.1 rmind ip->ip_hl = sizeof(struct ip) >> 2;
158 1.1 rmind ip->ip_tos = IPTOS_LOWDELAY;
159 1.1 rmind ip->ip_len = htons(len);
160 1.1 rmind ip->ip_off = htons(IP_DF);
161 1.1 rmind ip->ip_ttl = DEFAULT_IP_TTL;
162 1.1 rmind
163 1.1 rmind /* Pass to IP layer. */
164 1.1 rmind return ip_output(m, NULL, NULL, IP_FORWARDING, NULL, NULL);
165 1.1 rmind }
166 1.1 rmind
167 1.1 rmind /*
168 1.1 rmind * npf_return_icmp: return an ICMP error.
169 1.1 rmind */
170 1.1 rmind static int
171 1.1 rmind npf_return_icmp(nbuf_t *nbuf)
172 1.1 rmind {
173 1.1 rmind struct mbuf *m = nbuf;
174 1.1 rmind
175 1.1 rmind icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_ADMIN_PROHIBIT, 0, 0);
176 1.1 rmind return 0;
177 1.1 rmind }
178 1.1 rmind
179 1.1 rmind /*
180 1.1 rmind * npf_return_block: return TCP reset or ICMP host unreachable packet.
181 1.1 rmind */
182 1.1 rmind void
183 1.1 rmind npf_return_block(npf_cache_t *npc, nbuf_t *nbuf, const int retfl)
184 1.1 rmind {
185 1.1 rmind void *n_ptr = nbuf_dataptr(nbuf);
186 1.1 rmind const int proto = npc->npc_proto;
187 1.1 rmind
188 1.1 rmind if (!npf_iscached(npc, NPC_IP46) && !npf_ip4_proto(npc, nbuf, n_ptr))
189 1.1 rmind return;
190 1.1 rmind if ((proto == IPPROTO_TCP && (retfl & NPF_RULE_RETRST) == 0) ||
191 1.1 rmind (proto == IPPROTO_UDP && (retfl & NPF_RULE_RETICMP) == 0)) {
192 1.1 rmind return;
193 1.1 rmind }
194 1.1 rmind switch (proto) {
195 1.1 rmind case IPPROTO_TCP:
196 1.1 rmind (void)npf_return_tcp(npc, nbuf);
197 1.1 rmind break;
198 1.1 rmind case IPPROTO_UDP:
199 1.1 rmind (void)npf_return_icmp(nbuf);
200 1.1 rmind break;
201 1.1 rmind }
202 1.1 rmind }
203