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>
38 #include <sys/malloc.h>
40 #include <sys/protosw.h>
41 #include <sys/socket.h>
42 #include <sys/socketvar.h>
43 #include <sys/sysctl.h>
46 #include <net/route.h>
48 #include <netinet/in.h>
49 #include <netinet/in_systm.h>
50 #include <netinet/in_var.h>
51 #include <netinet/ip.h>
52 #include <netinet/in_pcb.h>
53 #include <netinet/ip_var.h>
54 #include <netinet/ip_options.h>
55 #include <netinet/ip_ipsec.h>
57 #include <machine/in_cksum.h>
60 #include <netinet6/ipsec.h>
61 #include <netkey/key.h>
63 #include <netkey/key_debug.h>
65 #define KEYDEBUG(lev,arg)
70 #include <netipsec/ipsec.h>
71 #include <netipsec/xform.h>
72 #include <netipsec/key.h>
75 extern struct protosw inetsw[];
78 * Check if we have to jump over firewall processing for this packet.
79 * Called from ip_input().
80 * 1 = jump over firewall, 0 = packet goes through firewall.
83 ip_ipsec_filtergif(struct mbuf *m)
85 #if defined(IPSEC) && !defined(IPSEC_FILTERGIF)
87 * Bypass packet filtering for packets from a tunnel (gif).
89 if (ipsec_getnhist(m))
92 #if defined(FAST_IPSEC) && !defined(IPSEC_FILTERGIF)
94 * Bypass packet filtering for packets from a tunnel (gif).
96 if (m_tag_find(m, PACKET_TAG_IPSEC_IN_DONE, NULL) != NULL)
103 * Check if this packet has an active SA and needs to be dropped instead
105 * Called from ip_input().
106 * 1 = drop packet, 0 = forward packet.
109 ip_ipsec_fwd(struct mbuf *m)
113 struct tdb_ident *tdbi;
114 struct secpolicy *sp;
116 #endif /* FAST_IPSEC */
119 * Enforce inbound IPsec SPD.
121 if (ipsec4_in_reject(m, NULL)) {
122 ipsecstat.in_polvio++;
127 mtag = m_tag_find(m, PACKET_TAG_IPSEC_IN_DONE, NULL);
130 tdbi = (struct tdb_ident *)(mtag + 1);
131 sp = ipsec_getpolicy(tdbi, IPSEC_DIR_INBOUND);
133 sp = ipsec_getpolicybyaddr(m, IPSEC_DIR_INBOUND,
134 IP_FORWARDING, &error);
136 if (sp == NULL) { /* NB: can happen if error */
138 /*XXX error stat???*/
139 DPRINTF(("ip_input: no SP for forwarding\n")); /*XXX*/
144 * Check security policy against packet attributes.
146 error = ipsec_in_reject(sp, m);
150 ipstat.ips_cantforward++;
153 #endif /* FAST_IPSEC */
158 * Check if protocol type doesn't have a further header and do IPSEC
159 * decryption or reject right now. Protocols with further headers get
160 * their IPSEC treatment within the protocol specific processing.
161 * Called from ip_input().
162 * 1 = drop packet, 0 = continue processing packet.
165 ip_ipsec_input(struct mbuf *m)
167 struct ip *ip = mtod(m, struct ip *);
170 struct tdb_ident *tdbi;
171 struct secpolicy *sp;
173 #endif /* FAST_IPSEC */
176 * enforce IPsec policy checking if we are seeing last header.
177 * note that we do not visit this with protocols with pcb layer
178 * code - like udp/tcp/raw ip.
180 if ((inetsw[ip_protox[ip->ip_p]].pr_flags & PR_LASTHDR) != 0 &&
181 ipsec4_in_reject(m, NULL)) {
182 ipsecstat.in_polvio++;
188 * enforce IPsec policy checking if we are seeing last header.
189 * note that we do not visit this with protocols with pcb layer
190 * code - like udp/tcp/raw ip.
192 if ((inetsw[ip_protox[ip->ip_p]].pr_flags & PR_LASTHDR) != 0) {
194 * Check if the packet has already had IPsec processing
195 * done. If so, then just pass it along. This tag gets
196 * set during AH, ESP, etc. input handling, before the
197 * packet is returned to the ip input queue for delivery.
199 mtag = m_tag_find(m, PACKET_TAG_IPSEC_IN_DONE, NULL);
202 tdbi = (struct tdb_ident *)(mtag + 1);
203 sp = ipsec_getpolicy(tdbi, IPSEC_DIR_INBOUND);
205 sp = ipsec_getpolicybyaddr(m, IPSEC_DIR_INBOUND,
206 IP_FORWARDING, &error);
210 * Check security policy against packet attributes.
212 error = ipsec_in_reject(sp, m);
215 /* XXX error stat??? */
217 DPRINTF(("ip_input: no SP, packet discarded\n"));/*XXX*/
224 #endif /* FAST_IPSEC */
229 * Compute the MTU for a forwarded packet that gets IPSEC encapsulated.
230 * Called from ip_forward().
231 * Returns MTU suggestion for ICMP needfrag reply.
234 ip_ipsec_mtu(struct mbuf *m)
238 * If the packet is routed over IPsec tunnel, tell the
239 * originator the tunnel MTU.
240 * tunnel MTU = if MTU - sizeof(IP) - ESP/AH hdrsiz
243 struct secpolicy *sp = NULL;
248 sp = ipsec4_getpolicybyaddr(m,
252 #else /* FAST_IPSEC */
253 sp = ipsec_getpolicybyaddr(m,
259 /* count IPsec header size */
260 ipsechdr = ipsec4_hdrsiz(m,
265 * find the correct route for outer IPv4
266 * header, compute tunnel MTU.
268 if (sp->req != NULL &&
269 sp->req->sav != NULL &&
270 sp->req->sav->sah != NULL) {
271 ro = &sp->req->sav->sah->sa_route;
272 if (ro->ro_rt && ro->ro_rt->rt_ifp) {
274 ro->ro_rt->rt_rmx.rmx_mtu ?
275 ro->ro_rt->rt_rmx.rmx_mtu :
276 ro->ro_rt->rt_ifp->if_mtu;
282 #else /* FAST_IPSEC */
291 * Called from ip_output().
292 * 1 = drop packet, 0 = continue processing packet,
293 * -1 = packet was reinjected and stop processing packet (FAST_IPSEC only)
296 ip_ipsec_output(struct mbuf **m, struct inpcb *inp, int *flags, int *error,
297 struct route **ro, struct route *iproute, struct sockaddr_in **dst,
298 struct in_ifaddr **ia, struct ifnet **ifp)
300 struct secpolicy *sp = NULL;
301 struct ip *ip = mtod(*m, struct ip *);
303 struct ipsec_output_state state;
306 struct tdb_ident *tdbi;
309 #endif /* FAST_IPSEC */
311 /* get SP for this packet */
313 sp = ipsec4_getpolicybyaddr(*m, IPSEC_DIR_OUTBOUND,
316 sp = ipsec4_getpolicybypcb(*m, IPSEC_DIR_OUTBOUND, inp, error);
319 ipsecstat.out_inval++;
324 switch (sp->policy) {
325 case IPSEC_POLICY_DISCARD:
327 * This packet is just discarded.
329 ipsecstat.out_polvio++;
332 case IPSEC_POLICY_BYPASS:
333 case IPSEC_POLICY_NONE:
334 case IPSEC_POLICY_TCP:
335 /* no need to do IPsec. */
338 case IPSEC_POLICY_IPSEC:
339 if (sp->req == NULL) {
340 /* acquire a policy */
341 *error = key_spdacquire(sp);
346 case IPSEC_POLICY_ENTRUST:
348 printf("%s: Invalid policy found. %d\n", __func__, sp->policy);
351 bzero(&state, sizeof(state));
353 if (*flags & IP_ROUTETOIF) {
355 bzero(iproute, sizeof(iproute));
358 state.dst = (struct sockaddr *)(*dst);
364 * delayed checksums are not currently compatible with IPsec
366 if ((*m)->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
367 in_delayed_cksum(*m);
368 (*m)->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
371 ip->ip_len = htons(ip->ip_len);
372 ip->ip_off = htons(ip->ip_off);
374 *error = ipsec4_output(&state, sp, *flags);
377 if (*flags & IP_ROUTETOIF) {
379 * if we have tunnel mode SA, we may need to ignore
382 if (state.ro != iproute || state.ro->ro_rt != NULL) {
383 *flags &= ~IP_ROUTETOIF;
388 *dst = (struct sockaddr_in *)state.dst;
390 /* mbuf is already reclaimed in ipsec4_output. */
400 printf("ip4_output (ipsec): error code %d\n", *error);
403 /* don't show these error codes to the user */
410 /* be sure to update variables that are affected by ipsec4_output() */
411 if ((*ro)->ro_rt == NULL) {
412 if ((*flags & IP_ROUTETOIF) == 0) {
414 "can't update route after IPsec processing\n");
415 *error = EHOSTUNREACH; /*XXX*/
420 *ia = ifatoia((*ro)->ro_rt->rt_ifa);
421 *ifp = (*ro)->ro_rt->rt_ifp;
424 ip = mtod(*m, struct ip *);
426 /* make it flipped, again. */
427 ip->ip_len = ntohs(ip->ip_len);
428 ip->ip_off = ntohs(ip->ip_off);
432 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
433 printf("DP ip_output call free SP:%p\n", sp));
439 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
440 printf("DP ip_output call free SP:%p\n", sp));
447 * Check the security policy (SP) for the packet and, if
448 * required, do IPsec-related processing. There are two
449 * cases here; the first time a packet is sent through
450 * it will be untagged and handled by ipsec4_checkpolicy.
451 * If the packet is resubmitted to ip_output (e.g. after
452 * AH, ESP, etc. processing), there will be a tag to bypass
453 * the lookup and related policy checking.
455 mtag = m_tag_find(*m, PACKET_TAG_IPSEC_PENDING_TDB, NULL);
458 tdbi = (struct tdb_ident *)(mtag + 1);
459 sp = ipsec_getpolicy(tdbi, IPSEC_DIR_OUTBOUND);
461 *error = -EINVAL; /* force silent drop */
462 m_tag_delete(*m, mtag);
464 sp = ipsec4_checkpolicy(*m, IPSEC_DIR_OUTBOUND, *flags,
468 * There are four return cases:
469 * sp != NULL apply IPsec policy
470 * sp == NULL, error == 0 no IPsec handling needed
471 * sp == NULL, error == -EINVAL discard packet w/o error
472 * sp == NULL, error != 0 discard packet, report error
475 /* Loop detection, check if ipsec processing already done */
476 KASSERT(sp->req != NULL, ("ip_output: no ipsec request"));
477 for (mtag = m_tag_first(*m); mtag != NULL;
478 mtag = m_tag_next(*m, mtag)) {
479 if (mtag->m_tag_cookie != MTAG_ABI_COMPAT)
481 if (mtag->m_tag_id != PACKET_TAG_IPSEC_OUT_DONE &&
482 mtag->m_tag_id != PACKET_TAG_IPSEC_OUT_CRYPTO_NEEDED)
485 * Check if policy has an SA associated with it.
486 * This can happen when an SP has yet to acquire
487 * an SA; e.g. on first reference. If it occurs,
488 * then we let ipsec4_process_packet do its thing.
490 if (sp->req->sav == NULL)
492 tdbi = (struct tdb_ident *)(mtag + 1);
493 if (tdbi->spi == sp->req->sav->spi &&
494 tdbi->proto == sp->req->sav->sah->saidx.proto &&
495 bcmp(&tdbi->dst, &sp->req->sav->sah->saidx.dst,
496 sizeof (union sockaddr_union)) == 0) {
498 * No IPsec processing is needed, free
501 * NB: null pointer to avoid free at
504 KEY_FREESP(&sp), sp = NULL;
511 * Do delayed checksums now because we send before
512 * this is done in the normal processing path.
514 if ((*m)->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
515 in_delayed_cksum(*m);
516 (*m)->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
519 ip->ip_len = htons(ip->ip_len);
520 ip->ip_off = htons(ip->ip_off);
522 /* NB: callee frees mbuf */
523 *error = ipsec4_process_packet(*m, sp->req, *flags, 0);
525 * Preserve KAME behaviour: ENOENT can be returned
526 * when an SA acquire is in progress. Don't propagate
527 * this to user-level; it confuses applications.
529 * XXX this will go away when the SADB is redone.
531 if (*error == ENOENT)
535 } else { /* sp == NULL */
540 * Hack: -EINVAL is used to signal that a packet
541 * should be silently discarded. This is typically
542 * because we asked key management for an SA and
543 * it was delayed (e.g. kicked up to IKE).
545 if (*error == -EINVAL)
549 /* No IPsec processing for this packet. */
553 * If deferred crypto processing is needed, check that
554 * the interface supports it.
556 mtag = m_tag_find(*m, PACKET_TAG_IPSEC_OUT_CRYPTO_NEEDED, NULL);
557 if (mtag != NULL && ((*ifp)->if_capenable & IFCAP_IPSEC) == 0) {
558 /* notify IPsec to do its own crypto */
559 ipsp_skipcrypto_unmark((struct tdb_ident *)(mtag + 1));
560 *error = EHOSTUNREACH;
577 #endif /* FAST_IPSEC */