]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/netinet6/ip6_ipsec.c
Update libucl to git version 8d3b186
[FreeBSD/FreeBSD.git] / sys / netinet6 / ip6_ipsec.c
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$");
32
33 #include "opt_inet.h"
34 #include "opt_inet6.h"
35 #include "opt_ipsec.h"
36
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
40 #include <sys/mac.h>
41 #include <sys/malloc.h>
42 #include <sys/mbuf.h>
43 #include <sys/protosw.h>
44 #include <sys/socket.h>
45 #include <sys/socketvar.h>
46 #include <sys/sysctl.h>
47 #include <sys/syslog.h>
48
49 #include <net/if.h>
50 #include <net/if_var.h>
51 #include <net/vnet.h>
52
53 #include <netinet/in.h>
54 #include <netinet/in_systm.h>
55 #include <netinet/in_var.h>
56 #include <netinet/ip.h>
57 #include <netinet/ip6.h>
58 #include <netinet/in_pcb.h>
59 #include <netinet/ip_var.h>
60 #include <netinet/ip_options.h>
61
62 #include <machine/in_cksum.h>
63
64 #ifdef IPSEC
65 #include <netipsec/ipsec.h>
66 #include <netipsec/ipsec6.h>
67 #include <netipsec/xform.h>
68 #include <netipsec/key.h>
69 #ifdef IPSEC_DEBUG
70 #include <netipsec/key_debug.h>
71 #else
72 #define KEYDEBUG(lev,arg)
73 #endif
74 #endif /*IPSEC*/
75
76 #include <netinet6/ip6_ipsec.h>
77 #include <netinet6/ip6_var.h>
78
79 extern  struct protosw inet6sw[];
80
81
82 #ifdef INET6 
83 #ifdef IPSEC
84 #ifdef IPSEC_FILTERTUNNEL
85 static VNET_DEFINE(int, ip6_ipsec6_filtertunnel) = 1;
86 #else
87 static VNET_DEFINE(int, ip6_ipsec6_filtertunnel) = 0;
88 #endif
89 #define V_ip6_ipsec6_filtertunnel       VNET(ip6_ipsec6_filtertunnel)
90
91 SYSCTL_DECL(_net_inet6_ipsec6);
92 SYSCTL_INT(_net_inet6_ipsec6, OID_AUTO, filtertunnel,
93         CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ip6_ipsec6_filtertunnel),  0,
94         "If set filter packets from an IPsec tunnel.");
95 #endif /* IPSEC */
96 #endif /* INET6 */
97
98 /*
99  * Check if we have to jump over firewall processing for this packet.
100  * Called from ip6_input().
101  * 1 = jump over firewall, 0 = packet goes through firewall.
102  */
103 int
104 ip6_ipsec_filtertunnel(struct mbuf *m)
105 {
106 #ifdef IPSEC
107
108         /*
109          * Bypass packet filtering for packets previously handled by IPsec.
110          */
111         if (!V_ip6_ipsec6_filtertunnel &&
112             m_tag_find(m, PACKET_TAG_IPSEC_IN_DONE, NULL) != NULL)
113                 return 1;
114 #endif
115         return 0;
116 }
117
118 /*
119  * Check if this packet has an active SA and needs to be dropped instead
120  * of forwarded.
121  * Called from ip6_forward().
122  * 1 = drop packet, 0 = forward packet.
123  */
124 int
125 ip6_ipsec_fwd(struct mbuf *m)
126 {
127
128 #ifdef IPSEC
129         return (ipsec6_in_reject(m, NULL));
130 #else
131         return (0);
132 #endif /* !IPSEC */
133 }
134
135 /*
136  * Check if protocol type doesn't have a further header and do IPSEC
137  * decryption or reject right now.  Protocols with further headers get
138  * their IPSEC treatment within the protocol specific processing.
139  * Called from ip6_input().
140  * 1 = drop packet, 0 = continue processing packet.
141  */
142 int
143 ip6_ipsec_input(struct mbuf *m, int nxt)
144 {
145
146 #ifdef IPSEC
147         /*
148          * enforce IPsec policy checking if we are seeing last header.
149          * note that we do not visit this with protocols with pcb layer
150          * code - like udp/tcp/raw ip.
151          */
152         if ((inet6sw[ip6_protox[nxt]].pr_flags & PR_LASTHDR) != 0)
153                 return (ipsec6_in_reject(m, NULL));
154 #endif /* IPSEC */
155         return (0);
156 }
157
158 /*
159  * Called from ip6_output().
160  * 1 = drop packet, 0 = continue processing packet,
161  * -1 = packet was reinjected and stop processing packet
162  */
163
164 int
165 ip6_ipsec_output(struct mbuf **m, struct inpcb *inp, int *error)
166 {
167 #ifdef IPSEC
168         struct secpolicy *sp;
169
170         /*
171          * Check the security policy (SP) for the packet and, if
172          * required, do IPsec-related processing.  There are two
173          * cases here; the first time a packet is sent through
174          * it will be untagged and handled by ipsec4_checkpolicy.
175          * If the packet is resubmitted to ip6_output (e.g. after
176          * AH, ESP, etc. processing), there will be a tag to bypass
177          * the lookup and related policy checking.
178          */
179         if (m_tag_find(*m, PACKET_TAG_IPSEC_OUT_DONE, NULL) != NULL) {
180                 *error = 0;
181                 return (0);
182         }
183         sp = ipsec4_checkpolicy(*m, IPSEC_DIR_OUTBOUND, error, inp);
184         /*
185          * There are four return cases:
186          *    sp != NULL                    apply IPsec policy
187          *    sp == NULL, error == 0        no IPsec handling needed
188          *    sp == NULL, error == -EINVAL  discard packet w/o error
189          *    sp == NULL, error != 0        discard packet, report error
190          */
191         if (sp != NULL) {
192                 /*
193                  * Do delayed checksums now because we send before
194                  * this is done in the normal processing path.
195                  */
196 #ifdef INET
197                 if ((*m)->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
198                         in_delayed_cksum(*m);
199                         (*m)->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
200                 }
201 #endif
202                 if ((*m)->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) {
203                         in6_delayed_cksum(*m, (*m)->m_pkthdr.len - sizeof(struct ip6_hdr),
204                                                         sizeof(struct ip6_hdr));
205                         (*m)->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA_IPV6;
206                 }
207 #ifdef SCTP
208                 if ((*m)->m_pkthdr.csum_flags & CSUM_SCTP_IPV6) {
209                         sctp_delayed_cksum(*m, sizeof(struct ip6_hdr));
210                         (*m)->m_pkthdr.csum_flags &= ~CSUM_SCTP_IPV6;
211                 }
212 #endif
213
214                 /* NB: callee frees mbuf */
215                 *error = ipsec6_process_packet(*m, sp->req);
216
217                 if (*error == EJUSTRETURN) {
218                         /*
219                          * We had a SP with a level of 'use' and no SA. We
220                          * will just continue to process the packet without
221                          * IPsec processing.
222                          */
223                         *error = 0;
224                         goto done;
225                 }
226
227                 /*
228                  * Preserve KAME behaviour: ENOENT can be returned
229                  * when an SA acquire is in progress.  Don't propagate
230                  * this to user-level; it confuses applications.
231                  *
232                  * XXX this will go away when the SADB is redone.
233                  */
234                 if (*error == ENOENT)
235                         *error = 0;
236                 goto reinjected;
237         } else {        /* sp == NULL */
238                 if (*error != 0) {
239                         /*
240                          * Hack: -EINVAL is used to signal that a packet
241                          * should be silently discarded.  This is typically
242                          * because we asked key management for an SA and
243                          * it was delayed (e.g. kicked up to IKE).
244                          */
245                         if (*error == -EINVAL)
246                                 *error = 0;
247                         goto bad;
248                 }
249                 /* No IPsec processing for this packet. */
250         }
251 done:
252         if (sp != NULL)
253                 KEY_FREESP(&sp);
254         return 0;
255 reinjected:
256         if (sp != NULL)
257                 KEY_FREESP(&sp);
258         return -1;
259 bad:
260         if (sp != NULL)
261                 KEY_FREESP(&sp);
262         return 1;
263 #endif /* IPSEC */
264         return 0;
265 }
266
267 #if 0
268 /*
269  * Compute the MTU for a forwarded packet that gets IPSEC encapsulated.
270  * Called from ip_forward().
271  * Returns MTU suggestion for ICMP needfrag reply.
272  */
273 int
274 ip6_ipsec_mtu(struct mbuf *m)
275 {
276         int mtu = 0;
277         /*
278          * If the packet is routed over IPsec tunnel, tell the
279          * originator the tunnel MTU.
280          *      tunnel MTU = if MTU - sizeof(IP) - ESP/AH hdrsiz
281          * XXX quickhack!!!
282          */
283 #ifdef IPSEC
284         struct secpolicy *sp = NULL;
285         int ipsecerror;
286         int ipsechdr;
287         struct route *ro;
288         sp = ipsec_getpolicybyaddr(m,
289                                    IPSEC_DIR_OUTBOUND,
290                                    IP_FORWARDING,
291                                    &ipsecerror);
292         if (sp != NULL) {
293                 /* count IPsec header size */
294                 ipsechdr = ipsec_hdrsiz(m, IPSEC_DIR_OUTBOUND, NULL);
295
296                 /*
297                  * find the correct route for outer IPv4
298                  * header, compute tunnel MTU.
299                  */
300                 if (sp->req != NULL &&
301                     sp->req->sav != NULL &&
302                     sp->req->sav->sah != NULL) {
303                         ro = &sp->req->sav->sah->route_cache.sa_route;
304                         if (ro->ro_rt && ro->ro_rt->rt_ifp) {
305                                 mtu = ro->ro_rt->rt_mtu ? ro->ro_rt->rt_mtu :
306                                     ro->ro_rt->rt_ifp->if_mtu;
307                                 mtu -= ipsechdr;
308                         }
309                 }
310                 KEY_FREESP(&sp);
311         }
312 #endif /* IPSEC */
313         /* XXX else case missing. */
314         return mtu;
315 }
316 #endif