2 * Copyright (c) 1982, 1986, 1988, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
32 #include "opt_ipsec.h"
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/malloc.h>
39 #include <sys/protosw.h>
40 #include <sys/socket.h>
41 #include <sys/socketvar.h>
42 #include <sys/sysctl.h>
45 #include <net/route.h>
47 #include <netinet/in.h>
48 #include <netinet/in_systm.h>
49 #include <netinet/in_var.h>
50 #include <netinet/ip.h>
51 #include <netinet/in_pcb.h>
52 #include <netinet/ip_var.h>
53 #include <netinet/ip_options.h>
54 #include <netinet/ip_ipsec.h>
56 #include <machine/in_cksum.h>
59 #include <netinet6/ipsec.h>
60 #include <netkey/key.h>
62 #include <netkey/key_debug.h>
64 #define KEYDEBUG(lev,arg)
69 #include <netipsec/ipsec.h>
70 #include <netipsec/xform.h>
71 #include <netipsec/key.h>
74 extern struct protosw inetsw[];
77 * Check if we have to jump over firewall processing for this packet.
78 * Called from ip_input().
79 * 1 = jump over firewall, 0 = packet goes through firewall.
82 ip_ipsec_filtergif(struct mbuf *m)
84 #if defined(IPSEC) && !defined(IPSEC_FILTERGIF)
86 * Bypass packet filtering for packets from a tunnel (gif).
88 if (ipsec_getnhist(m))
91 #if defined(FAST_IPSEC) && !defined(IPSEC_FILTERGIF)
93 * Bypass packet filtering for packets from a tunnel (gif).
95 if (m_tag_find(m, PACKET_TAG_IPSEC_IN_DONE, NULL) != NULL)
102 * Check if this packet has an active SA and needs to be dropped instead
104 * Called from ip_input().
105 * 1 = drop packet, 0 = forward packet.
108 ip_ipsec_fwd(struct mbuf *m)
112 struct tdb_ident *tdbi;
113 struct secpolicy *sp;
115 #endif /* FAST_IPSEC */
118 * Enforce inbound IPsec SPD.
120 if (ipsec4_in_reject(m, NULL)) {
121 ipsecstat.in_polvio++;
126 mtag = m_tag_find(m, PACKET_TAG_IPSEC_IN_DONE, NULL);
129 tdbi = (struct tdb_ident *)(mtag + 1);
130 sp = ipsec_getpolicy(tdbi, IPSEC_DIR_INBOUND);
132 sp = ipsec_getpolicybyaddr(m, IPSEC_DIR_INBOUND,
133 IP_FORWARDING, &error);
135 if (sp == NULL) { /* NB: can happen if error */
137 /*XXX error stat???*/
138 DPRINTF(("ip_input: no SP for forwarding\n")); /*XXX*/
143 * Check security policy against packet attributes.
145 error = ipsec_in_reject(sp, m);
149 ipstat.ips_cantforward++;
152 #endif /* FAST_IPSEC */
157 * Check if protocol type doesn't have a further header and do IPSEC
158 * decryption or reject right now. Protocols with further headers get
159 * their IPSEC treatment within the protocol specific processing.
160 * Called from ip_input().
161 * 1 = drop packet, 0 = continue processing packet.
164 ip_ipsec_input(struct mbuf *m)
166 struct ip *ip = mtod(m, struct ip *);
169 struct tdb_ident *tdbi;
170 struct secpolicy *sp;
172 #endif /* FAST_IPSEC */
175 * enforce IPsec policy checking if we are seeing last header.
176 * note that we do not visit this with protocols with pcb layer
177 * code - like udp/tcp/raw ip.
179 if ((inetsw[ip_protox[ip->ip_p]].pr_flags & PR_LASTHDR) != 0 &&
180 ipsec4_in_reject(m, NULL)) {
181 ipsecstat.in_polvio++;
187 * enforce IPsec policy checking if we are seeing last header.
188 * note that we do not visit this with protocols with pcb layer
189 * code - like udp/tcp/raw ip.
191 if ((inetsw[ip_protox[ip->ip_p]].pr_flags & PR_LASTHDR) != 0) {
193 * Check if the packet has already had IPsec processing
194 * done. If so, then just pass it along. This tag gets
195 * set during AH, ESP, etc. input handling, before the
196 * packet is returned to the ip input queue for delivery.
198 mtag = m_tag_find(m, PACKET_TAG_IPSEC_IN_DONE, NULL);
201 tdbi = (struct tdb_ident *)(mtag + 1);
202 sp = ipsec_getpolicy(tdbi, IPSEC_DIR_INBOUND);
204 sp = ipsec_getpolicybyaddr(m, IPSEC_DIR_INBOUND,
205 IP_FORWARDING, &error);
209 * Check security policy against packet attributes.
211 error = ipsec_in_reject(sp, m);
214 /* XXX error stat??? */
216 DPRINTF(("ip_input: no SP, packet discarded\n"));/*XXX*/
223 #endif /* FAST_IPSEC */
228 * Compute the MTU for a forwarded packet that gets IPSEC encapsulated.
229 * Called from ip_forward().
230 * Returns MTU suggestion for ICMP needfrag reply.
233 ip_ipsec_mtu(struct mbuf *m)
237 * If the packet is routed over IPsec tunnel, tell the
238 * originator the tunnel MTU.
239 * tunnel MTU = if MTU - sizeof(IP) - ESP/AH hdrsiz
242 struct secpolicy *sp = NULL;
247 sp = ipsec4_getpolicybyaddr(m,
251 #else /* FAST_IPSEC */
252 sp = ipsec_getpolicybyaddr(m,
258 /* count IPsec header size */
259 ipsechdr = ipsec4_hdrsiz(m,
264 * find the correct route for outer IPv4
265 * header, compute tunnel MTU.
267 if (sp->req != NULL &&
268 sp->req->sav != NULL &&
269 sp->req->sav->sah != NULL) {
270 ro = &sp->req->sav->sah->sa_route;
271 if (ro->ro_rt && ro->ro_rt->rt_ifp) {
273 ro->ro_rt->rt_rmx.rmx_mtu ?
274 ro->ro_rt->rt_rmx.rmx_mtu :
275 ro->ro_rt->rt_ifp->if_mtu;
281 #else /* FAST_IPSEC */
290 * Called from ip_output().
291 * 1 = drop packet, 0 = continue processing packet,
292 * -1 = packet was reinjected and stop processing packet (FAST_IPSEC only)
295 ip_ipsec_output(struct mbuf **m, struct inpcb *inp, int *flags, int *error,
296 struct route **ro, struct route *iproute, struct sockaddr_in **dst,
297 struct in_ifaddr **ia, struct ifnet **ifp)
299 struct secpolicy *sp = NULL;
300 struct ip *ip = mtod(*m, struct ip *);
302 struct ipsec_output_state state;
305 struct tdb_ident *tdbi;
308 #endif /* FAST_IPSEC */
310 /* get SP for this packet */
312 sp = ipsec4_getpolicybyaddr(*m, IPSEC_DIR_OUTBOUND,
315 sp = ipsec4_getpolicybypcb(*m, IPSEC_DIR_OUTBOUND, inp, error);
318 ipsecstat.out_inval++;
323 switch (sp->policy) {
324 case IPSEC_POLICY_DISCARD:
326 * This packet is just discarded.
328 ipsecstat.out_polvio++;
331 case IPSEC_POLICY_BYPASS:
332 case IPSEC_POLICY_NONE:
333 case IPSEC_POLICY_TCP:
334 /* no need to do IPsec. */
337 case IPSEC_POLICY_IPSEC:
338 if (sp->req == NULL) {
339 /* acquire a policy */
340 *error = key_spdacquire(sp);
345 case IPSEC_POLICY_ENTRUST:
347 printf("%s: Invalid policy found. %d\n", __func__, sp->policy);
350 bzero(&state, sizeof(state));
352 if (*flags & IP_ROUTETOIF) {
354 bzero(iproute, sizeof(iproute));
357 state.dst = (struct sockaddr *)(*dst);
363 * delayed checksums are not currently compatible with IPsec
365 if ((*m)->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
366 in_delayed_cksum(*m);
367 (*m)->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
370 ip->ip_len = htons(ip->ip_len);
371 ip->ip_off = htons(ip->ip_off);
373 *error = ipsec4_output(&state, sp, *flags);
376 if (*flags & IP_ROUTETOIF) {
378 * if we have tunnel mode SA, we may need to ignore
381 if (state.ro != iproute || state.ro->ro_rt != NULL) {
382 *flags &= ~IP_ROUTETOIF;
387 *dst = (struct sockaddr_in *)state.dst;
389 /* mbuf is already reclaimed in ipsec4_output. */
399 printf("ip4_output (ipsec): error code %d\n", *error);
402 /* don't show these error codes to the user */
409 /* be sure to update variables that are affected by ipsec4_output() */
410 if ((*ro)->ro_rt == NULL) {
411 if ((*flags & IP_ROUTETOIF) == 0) {
413 "can't update route after IPsec processing\n");
414 *error = EHOSTUNREACH; /*XXX*/
419 *ia = ifatoia((*ro)->ro_rt->rt_ifa);
420 *ifp = (*ro)->ro_rt->rt_ifp;
423 ip = mtod(*m, struct ip *);
425 /* make it flipped, again. */
426 ip->ip_len = ntohs(ip->ip_len);
427 ip->ip_off = ntohs(ip->ip_off);
431 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
432 printf("DP ip_output call free SP:%p\n", sp));
438 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
439 printf("DP ip_output call free SP:%p\n", sp));
446 * Check the security policy (SP) for the packet and, if
447 * required, do IPsec-related processing. There are two
448 * cases here; the first time a packet is sent through
449 * it will be untagged and handled by ipsec4_checkpolicy.
450 * If the packet is resubmitted to ip_output (e.g. after
451 * AH, ESP, etc. processing), there will be a tag to bypass
452 * the lookup and related policy checking.
454 mtag = m_tag_find(*m, PACKET_TAG_IPSEC_PENDING_TDB, NULL);
457 tdbi = (struct tdb_ident *)(mtag + 1);
458 sp = ipsec_getpolicy(tdbi, IPSEC_DIR_OUTBOUND);
460 *error = -EINVAL; /* force silent drop */
461 m_tag_delete(*m, mtag);
463 sp = ipsec4_checkpolicy(*m, IPSEC_DIR_OUTBOUND, *flags,
467 * There are four return cases:
468 * sp != NULL apply IPsec policy
469 * sp == NULL, error == 0 no IPsec handling needed
470 * sp == NULL, error == -EINVAL discard packet w/o error
471 * sp == NULL, error != 0 discard packet, report error
474 /* Loop detection, check if ipsec processing already done */
475 KASSERT(sp->req != NULL, ("ip_output: no ipsec request"));
476 for (mtag = m_tag_first(*m); mtag != NULL;
477 mtag = m_tag_next(*m, mtag)) {
478 if (mtag->m_tag_cookie != MTAG_ABI_COMPAT)
480 if (mtag->m_tag_id != PACKET_TAG_IPSEC_OUT_DONE &&
481 mtag->m_tag_id != PACKET_TAG_IPSEC_OUT_CRYPTO_NEEDED)
484 * Check if policy has an SA associated with it.
485 * This can happen when an SP has yet to acquire
486 * an SA; e.g. on first reference. If it occurs,
487 * then we let ipsec4_process_packet do its thing.
489 if (sp->req->sav == NULL)
491 tdbi = (struct tdb_ident *)(mtag + 1);
492 if (tdbi->spi == sp->req->sav->spi &&
493 tdbi->proto == sp->req->sav->sah->saidx.proto &&
494 bcmp(&tdbi->dst, &sp->req->sav->sah->saidx.dst,
495 sizeof (union sockaddr_union)) == 0) {
497 * No IPsec processing is needed, free
500 * NB: null pointer to avoid free at
503 KEY_FREESP(&sp), sp = NULL;
510 * Do delayed checksums now because we send before
511 * this is done in the normal processing path.
513 if ((*m)->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
514 in_delayed_cksum(*m);
515 (*m)->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
518 ip->ip_len = htons(ip->ip_len);
519 ip->ip_off = htons(ip->ip_off);
521 /* NB: callee frees mbuf */
522 *error = ipsec4_process_packet(*m, sp->req, *flags, 0);
524 * Preserve KAME behaviour: ENOENT can be returned
525 * when an SA acquire is in progress. Don't propagate
526 * this to user-level; it confuses applications.
528 * XXX this will go away when the SADB is redone.
530 if (*error == ENOENT)
534 } else { /* sp == NULL */
539 * Hack: -EINVAL is used to signal that a packet
540 * should be silently discarded. This is typically
541 * because we asked key management for an SA and
542 * it was delayed (e.g. kicked up to IKE).
544 if (*error == -EINVAL)
548 /* No IPsec processing for this packet. */
552 * If deferred crypto processing is needed, check that
553 * the interface supports it.
555 mtag = m_tag_find(*m, PACKET_TAG_IPSEC_OUT_CRYPTO_NEEDED, NULL);
556 if (mtag != NULL && ((*ifp)->if_capenable & IFCAP_IPSEC) == 0) {
557 /* notify IPsec to do its own crypto */
558 ipsp_skipcrypto_unmark((struct tdb_ident *)(mtag + 1));
559 *error = EHOSTUNREACH;
576 #endif /* FAST_IPSEC */