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