Deleted Added
full compact
ip_ipsec.c (195699) ip_ipsec.c (195727)
1/*-
2 * Copyright (c) 1982, 1986, 1988, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30#include <sys/cdefs.h>
1/*-
2 * Copyright (c) 1982, 1986, 1988, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30#include <sys/cdefs.h>
31__FBSDID("$FreeBSD: head/sys/netinet/ip_ipsec.c 195699 2009-07-14 22:48:30Z rwatson $");
31__FBSDID("$FreeBSD: head/sys/netinet/ip_ipsec.c 195727 2009-07-16 21:13:04Z rwatson $");
32
33#include "opt_ipsec.h"
34#include "opt_sctp.h"
35
36#include <sys/param.h>
37#include <sys/systm.h>
38#include <sys/errno.h>
39#include <sys/kernel.h>
40#include <sys/malloc.h>
41#include <sys/mbuf.h>
42#include <sys/protosw.h>
43#include <sys/socket.h>
44#include <sys/socketvar.h>
45#include <sys/sysctl.h>
46#include <sys/vimage.h>
47
48#include <net/if.h>
49#include <net/route.h>
50#include <net/vnet.h>
51
52#include <netinet/in.h>
53#include <netinet/in_systm.h>
54#include <netinet/in_var.h>
55#include <netinet/ip.h>
56#include <netinet/in_pcb.h>
57#include <netinet/ip_var.h>
58#include <netinet/ip_options.h>
59#include <netinet/ip_ipsec.h>
60#ifdef SCTP
61#include <netinet/sctp_crc32.h>
62#endif
63
64#include <machine/in_cksum.h>
65
66#ifdef IPSEC
67#include <netipsec/ipsec.h>
68#include <netipsec/xform.h>
69#include <netipsec/key.h>
70#endif /*IPSEC*/
71
72extern struct protosw inetsw[];
73
74#ifdef IPSEC
75#ifdef IPSEC_FILTERTUNNEL
76static VNET_DEFINE(int, ip4_ipsec_filtertunnel) = 1;
77#else
78static VNET_DEFINE(int, ip4_ipsec_filtertunnel) = 0;
79#endif
32
33#include "opt_ipsec.h"
34#include "opt_sctp.h"
35
36#include <sys/param.h>
37#include <sys/systm.h>
38#include <sys/errno.h>
39#include <sys/kernel.h>
40#include <sys/malloc.h>
41#include <sys/mbuf.h>
42#include <sys/protosw.h>
43#include <sys/socket.h>
44#include <sys/socketvar.h>
45#include <sys/sysctl.h>
46#include <sys/vimage.h>
47
48#include <net/if.h>
49#include <net/route.h>
50#include <net/vnet.h>
51
52#include <netinet/in.h>
53#include <netinet/in_systm.h>
54#include <netinet/in_var.h>
55#include <netinet/ip.h>
56#include <netinet/in_pcb.h>
57#include <netinet/ip_var.h>
58#include <netinet/ip_options.h>
59#include <netinet/ip_ipsec.h>
60#ifdef SCTP
61#include <netinet/sctp_crc32.h>
62#endif
63
64#include <machine/in_cksum.h>
65
66#ifdef IPSEC
67#include <netipsec/ipsec.h>
68#include <netipsec/xform.h>
69#include <netipsec/key.h>
70#endif /*IPSEC*/
71
72extern struct protosw inetsw[];
73
74#ifdef IPSEC
75#ifdef IPSEC_FILTERTUNNEL
76static VNET_DEFINE(int, ip4_ipsec_filtertunnel) = 1;
77#else
78static VNET_DEFINE(int, ip4_ipsec_filtertunnel) = 0;
79#endif
80#define V_ip4_ipsec_filtertunnel VNET_GET(ip4_ipsec_filtertunnel)
80#define V_ip4_ipsec_filtertunnel VNET(ip4_ipsec_filtertunnel)
81
82SYSCTL_DECL(_net_inet_ipsec);
83SYSCTL_VNET_INT(_net_inet_ipsec, OID_AUTO, filtertunnel,
84 CTLFLAG_RW, &VNET_NAME(ip4_ipsec_filtertunnel), 0,
85 "If set filter packets from an IPsec tunnel.");
86#endif /* IPSEC */
87
88/*
89 * Check if we have to jump over firewall processing for this packet.
90 * Called from ip_input().
91 * 1 = jump over firewall, 0 = packet goes through firewall.
92 */
93int
94ip_ipsec_filtertunnel(struct mbuf *m)
95{
96#if defined(IPSEC)
97
98 /*
99 * Bypass packet filtering for packets from a tunnel.
100 */
101 if (!V_ip4_ipsec_filtertunnel &&
102 m_tag_find(m, PACKET_TAG_IPSEC_IN_DONE, NULL) != NULL)
103 return 1;
104#endif
105 return 0;
106}
107
108/*
109 * Check if this packet has an active SA and needs to be dropped instead
110 * of forwarded.
111 * Called from ip_input().
112 * 1 = drop packet, 0 = forward packet.
113 */
114int
115ip_ipsec_fwd(struct mbuf *m)
116{
117#ifdef IPSEC
118 struct m_tag *mtag;
119 struct tdb_ident *tdbi;
120 struct secpolicy *sp;
121 int s, error;
122
123 mtag = m_tag_find(m, PACKET_TAG_IPSEC_IN_DONE, NULL);
124 s = splnet();
125 if (mtag != NULL) {
126 tdbi = (struct tdb_ident *)(mtag + 1);
127 sp = ipsec_getpolicy(tdbi, IPSEC_DIR_INBOUND);
128 } else {
129 sp = ipsec_getpolicybyaddr(m, IPSEC_DIR_INBOUND,
130 IP_FORWARDING, &error);
131 }
132 if (sp == NULL) { /* NB: can happen if error */
133 splx(s);
134 /*XXX error stat???*/
135 DPRINTF(("ip_input: no SP for forwarding\n")); /*XXX*/
136 return 1;
137 }
138
139 /*
140 * Check security policy against packet attributes.
141 */
142 error = ipsec_in_reject(sp, m);
143 KEY_FREESP(&sp);
144 splx(s);
145 if (error) {
146 IPSTAT_INC(ips_cantforward);
147 return 1;
148 }
149#endif /* IPSEC */
150 return 0;
151}
152
153/*
154 * Check if protocol type doesn't have a further header and do IPSEC
155 * decryption or reject right now. Protocols with further headers get
156 * their IPSEC treatment within the protocol specific processing.
157 * Called from ip_input().
158 * 1 = drop packet, 0 = continue processing packet.
159 */
160int
161ip_ipsec_input(struct mbuf *m)
162{
163#ifdef IPSEC
164 struct ip *ip = mtod(m, struct ip *);
165 struct m_tag *mtag;
166 struct tdb_ident *tdbi;
167 struct secpolicy *sp;
168 int s, error;
169 /*
170 * enforce IPsec policy checking if we are seeing last header.
171 * note that we do not visit this with protocols with pcb layer
172 * code - like udp/tcp/raw ip.
173 */
174 if ((inetsw[ip_protox[ip->ip_p]].pr_flags & PR_LASTHDR) != 0) {
175 /*
176 * Check if the packet has already had IPsec processing
177 * done. If so, then just pass it along. This tag gets
178 * set during AH, ESP, etc. input handling, before the
179 * packet is returned to the ip input queue for delivery.
180 */
181 mtag = m_tag_find(m, PACKET_TAG_IPSEC_IN_DONE, NULL);
182 s = splnet();
183 if (mtag != NULL) {
184 tdbi = (struct tdb_ident *)(mtag + 1);
185 sp = ipsec_getpolicy(tdbi, IPSEC_DIR_INBOUND);
186 } else {
187 sp = ipsec_getpolicybyaddr(m, IPSEC_DIR_INBOUND,
188 IP_FORWARDING, &error);
189 }
190 if (sp != NULL) {
191 /*
192 * Check security policy against packet attributes.
193 */
194 error = ipsec_in_reject(sp, m);
195 KEY_FREESP(&sp);
196 } else {
197 /* XXX error stat??? */
198 error = EINVAL;
199 DPRINTF(("ip_input: no SP, packet discarded\n"));/*XXX*/
200 return 1;
201 }
202 splx(s);
203 if (error)
204 return 1;
205 }
206#endif /* IPSEC */
207 return 0;
208}
209
210/*
211 * Compute the MTU for a forwarded packet that gets IPSEC encapsulated.
212 * Called from ip_forward().
213 * Returns MTU suggestion for ICMP needfrag reply.
214 */
215int
216ip_ipsec_mtu(struct mbuf *m, int mtu)
217{
218 /*
219 * If the packet is routed over IPsec tunnel, tell the
220 * originator the tunnel MTU.
221 * tunnel MTU = if MTU - sizeof(IP) - ESP/AH hdrsiz
222 * XXX quickhack!!!
223 */
224 struct secpolicy *sp = NULL;
225 int ipsecerror;
226 int ipsechdr;
227 struct route *ro;
228 sp = ipsec_getpolicybyaddr(m,
229 IPSEC_DIR_OUTBOUND,
230 IP_FORWARDING,
231 &ipsecerror);
232 if (sp != NULL) {
233 /* count IPsec header size */
234 ipsechdr = ipsec_hdrsiz(m, IPSEC_DIR_OUTBOUND, NULL);
235
236 /*
237 * find the correct route for outer IPv4
238 * header, compute tunnel MTU.
239 */
240 if (sp->req != NULL &&
241 sp->req->sav != NULL &&
242 sp->req->sav->sah != NULL) {
243 ro = &sp->req->sav->sah->sa_route;
244 if (ro->ro_rt && ro->ro_rt->rt_ifp) {
245 mtu =
246 ro->ro_rt->rt_rmx.rmx_mtu ?
247 ro->ro_rt->rt_rmx.rmx_mtu :
248 ro->ro_rt->rt_ifp->if_mtu;
249 mtu -= ipsechdr;
250 }
251 }
252 KEY_FREESP(&sp);
253 }
254 return mtu;
255}
256
257/*
258 *
259 * Called from ip_output().
260 * 1 = drop packet, 0 = continue processing packet,
261 * -1 = packet was reinjected and stop processing packet
262 */
263int
264ip_ipsec_output(struct mbuf **m, struct inpcb *inp, int *flags, int *error,
265 struct ifnet **ifp)
266{
267#ifdef IPSEC
268 struct secpolicy *sp = NULL;
269 struct ip *ip = mtod(*m, struct ip *);
270 struct tdb_ident *tdbi;
271 struct m_tag *mtag;
272 int s;
273 /*
274 * Check the security policy (SP) for the packet and, if
275 * required, do IPsec-related processing. There are two
276 * cases here; the first time a packet is sent through
277 * it will be untagged and handled by ipsec4_checkpolicy.
278 * If the packet is resubmitted to ip_output (e.g. after
279 * AH, ESP, etc. processing), there will be a tag to bypass
280 * the lookup and related policy checking.
281 */
282 mtag = m_tag_find(*m, PACKET_TAG_IPSEC_PENDING_TDB, NULL);
283 s = splnet();
284 if (mtag != NULL) {
285 tdbi = (struct tdb_ident *)(mtag + 1);
286 sp = ipsec_getpolicy(tdbi, IPSEC_DIR_OUTBOUND);
287 if (sp == NULL)
288 *error = -EINVAL; /* force silent drop */
289 m_tag_delete(*m, mtag);
290 } else {
291 sp = ipsec4_checkpolicy(*m, IPSEC_DIR_OUTBOUND, *flags,
292 error, inp);
293 }
294 /*
295 * There are four return cases:
296 * sp != NULL apply IPsec policy
297 * sp == NULL, error == 0 no IPsec handling needed
298 * sp == NULL, error == -EINVAL discard packet w/o error
299 * sp == NULL, error != 0 discard packet, report error
300 */
301 if (sp != NULL) {
302 /* Loop detection, check if ipsec processing already done */
303 KASSERT(sp->req != NULL, ("ip_output: no ipsec request"));
304 for (mtag = m_tag_first(*m); mtag != NULL;
305 mtag = m_tag_next(*m, mtag)) {
306 if (mtag->m_tag_cookie != MTAG_ABI_COMPAT)
307 continue;
308 if (mtag->m_tag_id != PACKET_TAG_IPSEC_OUT_DONE &&
309 mtag->m_tag_id != PACKET_TAG_IPSEC_OUT_CRYPTO_NEEDED)
310 continue;
311 /*
312 * Check if policy has an SA associated with it.
313 * This can happen when an SP has yet to acquire
314 * an SA; e.g. on first reference. If it occurs,
315 * then we let ipsec4_process_packet do its thing.
316 */
317 if (sp->req->sav == NULL)
318 break;
319 tdbi = (struct tdb_ident *)(mtag + 1);
320 if (tdbi->spi == sp->req->sav->spi &&
321 tdbi->proto == sp->req->sav->sah->saidx.proto &&
322 bcmp(&tdbi->dst, &sp->req->sav->sah->saidx.dst,
323 sizeof (union sockaddr_union)) == 0) {
324 /*
325 * No IPsec processing is needed, free
326 * reference to SP.
327 *
328 * NB: null pointer to avoid free at
329 * done: below.
330 */
331 KEY_FREESP(&sp), sp = NULL;
332 splx(s);
333 goto done;
334 }
335 }
336
337 /*
338 * Do delayed checksums now because we send before
339 * this is done in the normal processing path.
340 */
341 if ((*m)->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
342 in_delayed_cksum(*m);
343 (*m)->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
344 }
345#ifdef SCTP
346 if ((*m)->m_pkthdr.csum_flags & CSUM_SCTP) {
347 sctp_delayed_cksum(*m);
348 (*m)->m_pkthdr.csum_flags &= ~CSUM_SCTP;
349 }
350#endif
351 ip->ip_len = htons(ip->ip_len);
352 ip->ip_off = htons(ip->ip_off);
353
354 /* NB: callee frees mbuf */
355 *error = ipsec4_process_packet(*m, sp->req, *flags, 0);
356 if (*error == EJUSTRETURN) {
357 /*
358 * We had a SP with a level of 'use' and no SA. We
359 * will just continue to process the packet without
360 * IPsec processing and return without error.
361 */
362 *error = 0;
363 ip->ip_len = ntohs(ip->ip_len);
364 ip->ip_off = ntohs(ip->ip_off);
365 goto done;
366 }
367 /*
368 * Preserve KAME behaviour: ENOENT can be returned
369 * when an SA acquire is in progress. Don't propagate
370 * this to user-level; it confuses applications.
371 *
372 * XXX this will go away when the SADB is redone.
373 */
374 if (*error == ENOENT)
375 *error = 0;
376 splx(s);
377 goto reinjected;
378 } else { /* sp == NULL */
379 splx(s);
380
381 if (*error != 0) {
382 /*
383 * Hack: -EINVAL is used to signal that a packet
384 * should be silently discarded. This is typically
385 * because we asked key management for an SA and
386 * it was delayed (e.g. kicked up to IKE).
387 */
388 if (*error == -EINVAL)
389 *error = 0;
390 goto bad;
391 } else {
392 /* No IPsec processing for this packet. */
393 }
394#ifdef notyet
395 /*
396 * If deferred crypto processing is needed, check that
397 * the interface supports it.
398 */
399 mtag = m_tag_find(*m, PACKET_TAG_IPSEC_OUT_CRYPTO_NEEDED, NULL);
400 if (mtag != NULL && ifp != NULL &&
401 ((*ifp)->if_capenable & IFCAP_IPSEC) == 0) {
402 /* notify IPsec to do its own crypto */
403 ipsp_skipcrypto_unmark((struct tdb_ident *)(mtag + 1));
404 *error = EHOSTUNREACH;
405 goto bad;
406 }
407#endif
408 }
409done:
410 if (sp != NULL)
411 KEY_FREESP(&sp);
412 return 0;
413reinjected:
414 if (sp != NULL)
415 KEY_FREESP(&sp);
416 return -1;
417bad:
418 if (sp != NULL)
419 KEY_FREESP(&sp);
420 return 1;
421#endif /* IPSEC */
422 return 0;
423}
81
82SYSCTL_DECL(_net_inet_ipsec);
83SYSCTL_VNET_INT(_net_inet_ipsec, OID_AUTO, filtertunnel,
84 CTLFLAG_RW, &VNET_NAME(ip4_ipsec_filtertunnel), 0,
85 "If set filter packets from an IPsec tunnel.");
86#endif /* IPSEC */
87
88/*
89 * Check if we have to jump over firewall processing for this packet.
90 * Called from ip_input().
91 * 1 = jump over firewall, 0 = packet goes through firewall.
92 */
93int
94ip_ipsec_filtertunnel(struct mbuf *m)
95{
96#if defined(IPSEC)
97
98 /*
99 * Bypass packet filtering for packets from a tunnel.
100 */
101 if (!V_ip4_ipsec_filtertunnel &&
102 m_tag_find(m, PACKET_TAG_IPSEC_IN_DONE, NULL) != NULL)
103 return 1;
104#endif
105 return 0;
106}
107
108/*
109 * Check if this packet has an active SA and needs to be dropped instead
110 * of forwarded.
111 * Called from ip_input().
112 * 1 = drop packet, 0 = forward packet.
113 */
114int
115ip_ipsec_fwd(struct mbuf *m)
116{
117#ifdef IPSEC
118 struct m_tag *mtag;
119 struct tdb_ident *tdbi;
120 struct secpolicy *sp;
121 int s, error;
122
123 mtag = m_tag_find(m, PACKET_TAG_IPSEC_IN_DONE, NULL);
124 s = splnet();
125 if (mtag != NULL) {
126 tdbi = (struct tdb_ident *)(mtag + 1);
127 sp = ipsec_getpolicy(tdbi, IPSEC_DIR_INBOUND);
128 } else {
129 sp = ipsec_getpolicybyaddr(m, IPSEC_DIR_INBOUND,
130 IP_FORWARDING, &error);
131 }
132 if (sp == NULL) { /* NB: can happen if error */
133 splx(s);
134 /*XXX error stat???*/
135 DPRINTF(("ip_input: no SP for forwarding\n")); /*XXX*/
136 return 1;
137 }
138
139 /*
140 * Check security policy against packet attributes.
141 */
142 error = ipsec_in_reject(sp, m);
143 KEY_FREESP(&sp);
144 splx(s);
145 if (error) {
146 IPSTAT_INC(ips_cantforward);
147 return 1;
148 }
149#endif /* IPSEC */
150 return 0;
151}
152
153/*
154 * Check if protocol type doesn't have a further header and do IPSEC
155 * decryption or reject right now. Protocols with further headers get
156 * their IPSEC treatment within the protocol specific processing.
157 * Called from ip_input().
158 * 1 = drop packet, 0 = continue processing packet.
159 */
160int
161ip_ipsec_input(struct mbuf *m)
162{
163#ifdef IPSEC
164 struct ip *ip = mtod(m, struct ip *);
165 struct m_tag *mtag;
166 struct tdb_ident *tdbi;
167 struct secpolicy *sp;
168 int s, error;
169 /*
170 * enforce IPsec policy checking if we are seeing last header.
171 * note that we do not visit this with protocols with pcb layer
172 * code - like udp/tcp/raw ip.
173 */
174 if ((inetsw[ip_protox[ip->ip_p]].pr_flags & PR_LASTHDR) != 0) {
175 /*
176 * Check if the packet has already had IPsec processing
177 * done. If so, then just pass it along. This tag gets
178 * set during AH, ESP, etc. input handling, before the
179 * packet is returned to the ip input queue for delivery.
180 */
181 mtag = m_tag_find(m, PACKET_TAG_IPSEC_IN_DONE, NULL);
182 s = splnet();
183 if (mtag != NULL) {
184 tdbi = (struct tdb_ident *)(mtag + 1);
185 sp = ipsec_getpolicy(tdbi, IPSEC_DIR_INBOUND);
186 } else {
187 sp = ipsec_getpolicybyaddr(m, IPSEC_DIR_INBOUND,
188 IP_FORWARDING, &error);
189 }
190 if (sp != NULL) {
191 /*
192 * Check security policy against packet attributes.
193 */
194 error = ipsec_in_reject(sp, m);
195 KEY_FREESP(&sp);
196 } else {
197 /* XXX error stat??? */
198 error = EINVAL;
199 DPRINTF(("ip_input: no SP, packet discarded\n"));/*XXX*/
200 return 1;
201 }
202 splx(s);
203 if (error)
204 return 1;
205 }
206#endif /* IPSEC */
207 return 0;
208}
209
210/*
211 * Compute the MTU for a forwarded packet that gets IPSEC encapsulated.
212 * Called from ip_forward().
213 * Returns MTU suggestion for ICMP needfrag reply.
214 */
215int
216ip_ipsec_mtu(struct mbuf *m, int mtu)
217{
218 /*
219 * If the packet is routed over IPsec tunnel, tell the
220 * originator the tunnel MTU.
221 * tunnel MTU = if MTU - sizeof(IP) - ESP/AH hdrsiz
222 * XXX quickhack!!!
223 */
224 struct secpolicy *sp = NULL;
225 int ipsecerror;
226 int ipsechdr;
227 struct route *ro;
228 sp = ipsec_getpolicybyaddr(m,
229 IPSEC_DIR_OUTBOUND,
230 IP_FORWARDING,
231 &ipsecerror);
232 if (sp != NULL) {
233 /* count IPsec header size */
234 ipsechdr = ipsec_hdrsiz(m, IPSEC_DIR_OUTBOUND, NULL);
235
236 /*
237 * find the correct route for outer IPv4
238 * header, compute tunnel MTU.
239 */
240 if (sp->req != NULL &&
241 sp->req->sav != NULL &&
242 sp->req->sav->sah != NULL) {
243 ro = &sp->req->sav->sah->sa_route;
244 if (ro->ro_rt && ro->ro_rt->rt_ifp) {
245 mtu =
246 ro->ro_rt->rt_rmx.rmx_mtu ?
247 ro->ro_rt->rt_rmx.rmx_mtu :
248 ro->ro_rt->rt_ifp->if_mtu;
249 mtu -= ipsechdr;
250 }
251 }
252 KEY_FREESP(&sp);
253 }
254 return mtu;
255}
256
257/*
258 *
259 * Called from ip_output().
260 * 1 = drop packet, 0 = continue processing packet,
261 * -1 = packet was reinjected and stop processing packet
262 */
263int
264ip_ipsec_output(struct mbuf **m, struct inpcb *inp, int *flags, int *error,
265 struct ifnet **ifp)
266{
267#ifdef IPSEC
268 struct secpolicy *sp = NULL;
269 struct ip *ip = mtod(*m, struct ip *);
270 struct tdb_ident *tdbi;
271 struct m_tag *mtag;
272 int s;
273 /*
274 * Check the security policy (SP) for the packet and, if
275 * required, do IPsec-related processing. There are two
276 * cases here; the first time a packet is sent through
277 * it will be untagged and handled by ipsec4_checkpolicy.
278 * If the packet is resubmitted to ip_output (e.g. after
279 * AH, ESP, etc. processing), there will be a tag to bypass
280 * the lookup and related policy checking.
281 */
282 mtag = m_tag_find(*m, PACKET_TAG_IPSEC_PENDING_TDB, NULL);
283 s = splnet();
284 if (mtag != NULL) {
285 tdbi = (struct tdb_ident *)(mtag + 1);
286 sp = ipsec_getpolicy(tdbi, IPSEC_DIR_OUTBOUND);
287 if (sp == NULL)
288 *error = -EINVAL; /* force silent drop */
289 m_tag_delete(*m, mtag);
290 } else {
291 sp = ipsec4_checkpolicy(*m, IPSEC_DIR_OUTBOUND, *flags,
292 error, inp);
293 }
294 /*
295 * There are four return cases:
296 * sp != NULL apply IPsec policy
297 * sp == NULL, error == 0 no IPsec handling needed
298 * sp == NULL, error == -EINVAL discard packet w/o error
299 * sp == NULL, error != 0 discard packet, report error
300 */
301 if (sp != NULL) {
302 /* Loop detection, check if ipsec processing already done */
303 KASSERT(sp->req != NULL, ("ip_output: no ipsec request"));
304 for (mtag = m_tag_first(*m); mtag != NULL;
305 mtag = m_tag_next(*m, mtag)) {
306 if (mtag->m_tag_cookie != MTAG_ABI_COMPAT)
307 continue;
308 if (mtag->m_tag_id != PACKET_TAG_IPSEC_OUT_DONE &&
309 mtag->m_tag_id != PACKET_TAG_IPSEC_OUT_CRYPTO_NEEDED)
310 continue;
311 /*
312 * Check if policy has an SA associated with it.
313 * This can happen when an SP has yet to acquire
314 * an SA; e.g. on first reference. If it occurs,
315 * then we let ipsec4_process_packet do its thing.
316 */
317 if (sp->req->sav == NULL)
318 break;
319 tdbi = (struct tdb_ident *)(mtag + 1);
320 if (tdbi->spi == sp->req->sav->spi &&
321 tdbi->proto == sp->req->sav->sah->saidx.proto &&
322 bcmp(&tdbi->dst, &sp->req->sav->sah->saidx.dst,
323 sizeof (union sockaddr_union)) == 0) {
324 /*
325 * No IPsec processing is needed, free
326 * reference to SP.
327 *
328 * NB: null pointer to avoid free at
329 * done: below.
330 */
331 KEY_FREESP(&sp), sp = NULL;
332 splx(s);
333 goto done;
334 }
335 }
336
337 /*
338 * Do delayed checksums now because we send before
339 * this is done in the normal processing path.
340 */
341 if ((*m)->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
342 in_delayed_cksum(*m);
343 (*m)->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
344 }
345#ifdef SCTP
346 if ((*m)->m_pkthdr.csum_flags & CSUM_SCTP) {
347 sctp_delayed_cksum(*m);
348 (*m)->m_pkthdr.csum_flags &= ~CSUM_SCTP;
349 }
350#endif
351 ip->ip_len = htons(ip->ip_len);
352 ip->ip_off = htons(ip->ip_off);
353
354 /* NB: callee frees mbuf */
355 *error = ipsec4_process_packet(*m, sp->req, *flags, 0);
356 if (*error == EJUSTRETURN) {
357 /*
358 * We had a SP with a level of 'use' and no SA. We
359 * will just continue to process the packet without
360 * IPsec processing and return without error.
361 */
362 *error = 0;
363 ip->ip_len = ntohs(ip->ip_len);
364 ip->ip_off = ntohs(ip->ip_off);
365 goto done;
366 }
367 /*
368 * Preserve KAME behaviour: ENOENT can be returned
369 * when an SA acquire is in progress. Don't propagate
370 * this to user-level; it confuses applications.
371 *
372 * XXX this will go away when the SADB is redone.
373 */
374 if (*error == ENOENT)
375 *error = 0;
376 splx(s);
377 goto reinjected;
378 } else { /* sp == NULL */
379 splx(s);
380
381 if (*error != 0) {
382 /*
383 * Hack: -EINVAL is used to signal that a packet
384 * should be silently discarded. This is typically
385 * because we asked key management for an SA and
386 * it was delayed (e.g. kicked up to IKE).
387 */
388 if (*error == -EINVAL)
389 *error = 0;
390 goto bad;
391 } else {
392 /* No IPsec processing for this packet. */
393 }
394#ifdef notyet
395 /*
396 * If deferred crypto processing is needed, check that
397 * the interface supports it.
398 */
399 mtag = m_tag_find(*m, PACKET_TAG_IPSEC_OUT_CRYPTO_NEEDED, NULL);
400 if (mtag != NULL && ifp != NULL &&
401 ((*ifp)->if_capenable & IFCAP_IPSEC) == 0) {
402 /* notify IPsec to do its own crypto */
403 ipsp_skipcrypto_unmark((struct tdb_ident *)(mtag + 1));
404 *error = EHOSTUNREACH;
405 goto bad;
406 }
407#endif
408 }
409done:
410 if (sp != NULL)
411 KEY_FREESP(&sp);
412 return 0;
413reinjected:
414 if (sp != NULL)
415 KEY_FREESP(&sp);
416 return -1;
417bad:
418 if (sp != NULL)
419 KEY_FREESP(&sp);
420 return 1;
421#endif /* IPSEC */
422 return 0;
423}