]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/netinet/ip_ipsec.c
This commit was generated by cvs2svn to compensate for changes in r166124,
[FreeBSD/FreeBSD.git] / sys / netinet / ip_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  * $FreeBSD$
30  */
31
32 #include "opt_ipsec.h"
33
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/malloc.h>
38 #include <sys/mbuf.h>
39 #include <sys/protosw.h>
40 #include <sys/socket.h>
41 #include <sys/socketvar.h>
42 #include <sys/sysctl.h>
43
44 #include <net/if.h>
45 #include <net/route.h>
46
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>
55
56 #include <machine/in_cksum.h>
57
58 #ifdef IPSEC
59 #include <netinet6/ipsec.h>
60 #include <netkey/key.h>
61 #ifdef IPSEC_DEBUG
62 #include <netkey/key_debug.h>
63 #else
64 #define KEYDEBUG(lev,arg)
65 #endif
66 #endif /*IPSEC*/
67
68 #ifdef FAST_IPSEC
69 #include <netipsec/ipsec.h>
70 #include <netipsec/xform.h>
71 #include <netipsec/key.h>
72 #endif /*FAST_IPSEC*/
73
74 extern  struct protosw inetsw[];
75
76 /*
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.
80  */
81 int
82 ip_ipsec_filtergif(struct mbuf *m)
83 {
84 #if defined(IPSEC) && !defined(IPSEC_FILTERGIF)
85         /*
86          * Bypass packet filtering for packets from a tunnel (gif).
87          */
88         if (ipsec_getnhist(m))
89                 return 1;
90 #endif
91 #if defined(FAST_IPSEC) && !defined(IPSEC_FILTERGIF)
92         /*
93          * Bypass packet filtering for packets from a tunnel (gif).
94          */
95         if (m_tag_find(m, PACKET_TAG_IPSEC_IN_DONE, NULL) != NULL)
96                 return 1;
97 #endif
98         return 0;
99 }
100
101 /*
102  * Check if this packet has an active SA and needs to be dropped instead
103  * of forwarded.
104  * Called from ip_input().
105  * 1 = drop packet, 0 = forward packet.
106  */
107 int
108 ip_ipsec_fwd(struct mbuf *m)
109 {
110 #ifdef FAST_IPSEC
111         struct m_tag *mtag;
112         struct tdb_ident *tdbi;
113         struct secpolicy *sp;
114         int s, error;
115 #endif /* FAST_IPSEC */
116 #ifdef IPSEC
117         /*
118          * Enforce inbound IPsec SPD.
119          */
120         if (ipsec4_in_reject(m, NULL)) {
121                 ipsecstat.in_polvio++;
122                 return 1;
123         }
124 #endif /* IPSEC */
125 #ifdef FAST_IPSEC
126         mtag = m_tag_find(m, PACKET_TAG_IPSEC_IN_DONE, NULL);
127         s = splnet();
128         if (mtag != NULL) {
129                 tdbi = (struct tdb_ident *)(mtag + 1);
130                 sp = ipsec_getpolicy(tdbi, IPSEC_DIR_INBOUND);
131         } else {
132                 sp = ipsec_getpolicybyaddr(m, IPSEC_DIR_INBOUND,
133                                            IP_FORWARDING, &error);   
134         }
135         if (sp == NULL) {       /* NB: can happen if error */
136                 splx(s);
137                 /*XXX error stat???*/
138                 DPRINTF(("ip_input: no SP for forwarding\n"));  /*XXX*/
139                 return 1;
140         }
141
142         /*
143          * Check security policy against packet attributes.
144          */
145         error = ipsec_in_reject(sp, m);
146         KEY_FREESP(&sp);
147         splx(s);
148         if (error) {
149                 ipstat.ips_cantforward++;
150                 return 1;
151         }
152 #endif /* FAST_IPSEC */
153         return 0;
154 }
155
156 /*
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.
162  */
163 int
164 ip_ipsec_input(struct mbuf *m)
165 {
166         struct ip *ip = mtod(m, struct ip *);
167 #ifdef FAST_IPSEC
168         struct m_tag *mtag;
169         struct tdb_ident *tdbi;
170         struct secpolicy *sp;
171         int s, error;
172 #endif /* FAST_IPSEC */
173 #ifdef IPSEC
174         /*
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.
178          */
179         if ((inetsw[ip_protox[ip->ip_p]].pr_flags & PR_LASTHDR) != 0 &&
180             ipsec4_in_reject(m, NULL)) {
181                 ipsecstat.in_polvio++;
182                 return 1;
183         }
184 #endif
185 #ifdef FAST_IPSEC
186         /*
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.
190          */
191         if ((inetsw[ip_protox[ip->ip_p]].pr_flags & PR_LASTHDR) != 0) {
192                 /*
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.
197                  */ 
198                 mtag = m_tag_find(m, PACKET_TAG_IPSEC_IN_DONE, NULL);
199                 s = splnet();
200                 if (mtag != NULL) {
201                         tdbi = (struct tdb_ident *)(mtag + 1);
202                         sp = ipsec_getpolicy(tdbi, IPSEC_DIR_INBOUND);
203                 } else {
204                         sp = ipsec_getpolicybyaddr(m, IPSEC_DIR_INBOUND,
205                                                    IP_FORWARDING, &error);   
206                 }
207                 if (sp != NULL) {
208                         /*
209                          * Check security policy against packet attributes.
210                          */
211                         error = ipsec_in_reject(sp, m);
212                         KEY_FREESP(&sp);
213                 } else {
214                         /* XXX error stat??? */
215                         error = EINVAL;
216                         DPRINTF(("ip_input: no SP, packet discarded\n"));/*XXX*/
217                         return 1;
218                 }
219                 splx(s);
220                 if (error)
221                         return 1;
222         }
223 #endif /* FAST_IPSEC */
224         return 0;
225 }
226
227 /*
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.
231  */
232 int
233 ip_ipsec_mtu(struct mbuf *m)
234 {
235         int mtu = 0;
236         /*
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
240          * XXX quickhack!!!
241          */
242         struct secpolicy *sp = NULL;
243         int ipsecerror;
244         int ipsechdr;
245         struct route *ro;
246 #ifdef IPSEC
247         sp = ipsec4_getpolicybyaddr(m,
248                                     IPSEC_DIR_OUTBOUND,
249                                     IP_FORWARDING,
250                                     &ipsecerror);
251 #else /* FAST_IPSEC */
252         sp = ipsec_getpolicybyaddr(m,
253                                    IPSEC_DIR_OUTBOUND,
254                                    IP_FORWARDING,
255                                    &ipsecerror);
256 #endif
257         if (sp != NULL) {
258                 /* count IPsec header size */
259                 ipsechdr = ipsec4_hdrsiz(m,
260                                          IPSEC_DIR_OUTBOUND,
261                                          NULL);
262
263                 /*
264                  * find the correct route for outer IPv4
265                  * header, compute tunnel MTU.
266                  */
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) {
272                                 mtu =
273                                     ro->ro_rt->rt_rmx.rmx_mtu ?
274                                     ro->ro_rt->rt_rmx.rmx_mtu :
275                                     ro->ro_rt->rt_ifp->if_mtu;
276                                 mtu -= ipsechdr;
277                         }
278                 }
279 #ifdef IPSEC
280                 key_freesp(sp);
281 #else /* FAST_IPSEC */
282                 KEY_FREESP(&sp);
283 #endif
284         }
285         return mtu;
286 }
287
288 /*
289  * 
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)
293  */
294 int
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)
298 {
299         struct secpolicy *sp = NULL;
300         struct ip *ip = mtod(*m, struct ip *);
301 #ifdef IPSEC
302         struct ipsec_output_state state;
303 #endif
304 #ifdef FAST_IPSEC
305         struct tdb_ident *tdbi;
306         struct m_tag *mtag;
307         int s;
308 #endif /* FAST_IPSEC */
309 #ifdef IPSEC
310         /* get SP for this packet */
311         if (inp == NULL)
312                 sp = ipsec4_getpolicybyaddr(*m, IPSEC_DIR_OUTBOUND,
313                     *flags, error);
314         else
315                 sp = ipsec4_getpolicybypcb(*m, IPSEC_DIR_OUTBOUND, inp, error);
316
317         if (sp == NULL) {
318                 ipsecstat.out_inval++;
319                 goto bad;
320         }
321
322         /* check policy */
323         switch (sp->policy) {
324         case IPSEC_POLICY_DISCARD:
325                 /*
326                  * This packet is just discarded.
327                  */
328                 ipsecstat.out_polvio++;
329                 goto bad;
330
331         case IPSEC_POLICY_BYPASS:
332         case IPSEC_POLICY_NONE:
333         case IPSEC_POLICY_TCP:
334                 /* no need to do IPsec. */
335                 goto done;
336         
337         case IPSEC_POLICY_IPSEC:
338                 if (sp->req == NULL) {
339                         /* acquire a policy */
340                         *error = key_spdacquire(sp);
341                         goto bad;
342                 }
343                 break;
344
345         case IPSEC_POLICY_ENTRUST:
346         default:
347                 printf("%s: Invalid policy found. %d\n", __func__, sp->policy);
348         }
349
350         bzero(&state, sizeof(state));
351         state.m = *m;
352         if (*flags & IP_ROUTETOIF) {
353                 state.ro = iproute;
354                 bzero(iproute, sizeof(iproute));
355         } else
356                 state.ro = *ro;
357         state.dst = (struct sockaddr *)(*dst);
358
359         ip->ip_sum = 0;
360
361         /*
362          * XXX
363          * delayed checksums are not currently compatible with IPsec
364          */
365         if ((*m)->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
366                 in_delayed_cksum(*m);
367                 (*m)->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
368         }
369
370         ip->ip_len = htons(ip->ip_len);
371         ip->ip_off = htons(ip->ip_off);
372
373         *error = ipsec4_output(&state, sp, *flags);
374
375         *m = state.m;
376         if (*flags & IP_ROUTETOIF) {
377                 /*
378                  * if we have tunnel mode SA, we may need to ignore
379                  * IP_ROUTETOIF.
380                  */
381                 if (state.ro != iproute || state.ro->ro_rt != NULL) {
382                         *flags &= ~IP_ROUTETOIF;
383                         *ro = state.ro;
384                 }
385         } else
386                 *ro = state.ro;
387         *dst = (struct sockaddr_in *)state.dst;
388         if (*error != 0) {
389                 /* mbuf is already reclaimed in ipsec4_output. */
390                 *m = NULL;
391                 switch (*error) {
392                 case EHOSTUNREACH:
393                 case ENETUNREACH:
394                 case EMSGSIZE:
395                 case ENOBUFS:
396                 case ENOMEM:
397                         break;
398                 default:
399                         printf("ip4_output (ipsec): error code %d\n", *error);
400                         /*fall through*/
401                 case ENOENT:
402                         /* don't show these error codes to the user */
403                         *error = 0;
404                         break;
405                 }
406                 goto bad;
407         }
408
409         /* be sure to update variables that are affected by ipsec4_output() */
410         if ((*ro)->ro_rt == NULL) {
411                 if ((*flags & IP_ROUTETOIF) == 0) {
412                         printf("ip_output: "
413                                 "can't update route after IPsec processing\n");
414                         *error = EHOSTUNREACH;  /*XXX*/
415                         goto bad;
416                 }
417         } else {
418                 if (state.encap) {
419                         *ia = ifatoia((*ro)->ro_rt->rt_ifa);
420                         *ifp = (*ro)->ro_rt->rt_ifp;
421                 }
422         }
423         ip = mtod(*m, struct ip *);
424
425         /* make it flipped, again. */
426         ip->ip_len = ntohs(ip->ip_len);
427         ip->ip_off = ntohs(ip->ip_off);
428
429 done:
430         if (sp != NULL) {
431                 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
432                         printf("DP ip_output call free SP:%p\n", sp));
433                 key_freesp(sp);
434         }
435         return 0;
436 bad:
437         if (sp != NULL) {
438                 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
439                         printf("DP ip_output call free SP:%p\n", sp));
440                 key_freesp(sp);
441         }
442         return 1;
443 #endif /*IPSEC*/
444 #ifdef FAST_IPSEC
445         /*
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.
453          */
454         mtag = m_tag_find(*m, PACKET_TAG_IPSEC_PENDING_TDB, NULL);
455         s = splnet();
456         if (mtag != NULL) {
457                 tdbi = (struct tdb_ident *)(mtag + 1);
458                 sp = ipsec_getpolicy(tdbi, IPSEC_DIR_OUTBOUND);
459                 if (sp == NULL)
460                         *error = -EINVAL;       /* force silent drop */
461                 m_tag_delete(*m, mtag);
462         } else {
463                 sp = ipsec4_checkpolicy(*m, IPSEC_DIR_OUTBOUND, *flags,
464                                         error, inp);
465         }
466         /*
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
472          */
473         if (sp != NULL) {
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)
479                                 continue;
480                         if (mtag->m_tag_id != PACKET_TAG_IPSEC_OUT_DONE &&
481                             mtag->m_tag_id != PACKET_TAG_IPSEC_OUT_CRYPTO_NEEDED)
482                                 continue;
483                         /*
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.
488                          */
489                         if (sp->req->sav == NULL)
490                                 break;
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) {
496                                 /*
497                                  * No IPsec processing is needed, free
498                                  * reference to SP.
499                                  *
500                                  * NB: null pointer to avoid free at
501                                  *     done: below.
502                                  */
503                                 KEY_FREESP(&sp), sp = NULL;
504                                 splx(s);
505                                 goto done;
506                         }
507                 }
508
509                 /*
510                  * Do delayed checksums now because we send before
511                  * this is done in the normal processing path.
512                  */
513                 if ((*m)->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
514                         in_delayed_cksum(*m);
515                         (*m)->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
516                 }
517
518                 ip->ip_len = htons(ip->ip_len);
519                 ip->ip_off = htons(ip->ip_off);
520
521                 /* NB: callee frees mbuf */
522                 *error = ipsec4_process_packet(*m, sp->req, *flags, 0);
523                 /*
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.
527                  *
528                  * XXX this will go away when the SADB is redone.
529                  */
530                 if (*error == ENOENT)
531                         *error = 0;
532                 splx(s);
533                 goto reinjected;
534         } else {        /* sp == NULL */
535                 splx(s);
536
537                 if (*error != 0) {
538                         /*
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).
543                          */
544                         if (*error == -EINVAL)
545                                 *error = 0;
546                         goto bad;
547                 } else {
548                         /* No IPsec processing for this packet. */
549                 }
550 #ifdef notyet
551                 /*
552                  * If deferred crypto processing is needed, check that
553                  * the interface supports it.
554                  */ 
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;
560                         goto bad;
561                 }
562 #endif
563         }
564 done:
565         if (sp != NULL)
566                 KEY_FREESP(&sp);
567         return 0;
568 reinjected:
569         if (sp != NULL)
570                 KEY_FREESP(&sp);
571         return -1;
572 bad:
573         if (sp != NULL)
574                 KEY_FREESP(&sp);
575         return 1;
576 #endif /* FAST_IPSEC */
577         return 0;
578 }
579