[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ]

FreeBSD/Linux Kernel Cross Reference
sys/netinet6/ip6_ipsec.c

Version: -  FREEBSD  -  FREEBSD7  -  FREEBSD70  -  FREEBSD6  -  FREEBSD64  -  FREEBSD63  -  FREEBSD62  -  FREEBSD61  -  FREEBSD60  -  FREEBSD5  -  FREEBSD55  -  FREEBSD54  -  FREEBSD53  -  FREEBSD52  -  FREEBSD51  -  FREEBSD50  -  FREEBSD4  -  FREEBSD3  -  FREEBSD22  -  linux-2.6  -  linux-2.4.22  -  MK83  -  MK84  -  PLAN9  -  DFBSD  -  NETBSD  -  NETBSD5  -  NETBSD4  -  NETBSD3  -  NETBSD20  -  OPENBSD  -  xnu-517  -  xnu-792  -  xnu-792.6.70  -  xnu-1228  -  OPENSOLARIS  -  minix-3-1-1  -  TRUSTEDBSD-SEBSD  -  FREEBSD-LIBC  -  FREEBSD7-LIBC  -  FREEBSD6-LIBC  -  GLIBC27 
SearchContext: -  none  -  excerpts  -  bigexcerpts 

  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: src/sys/netinet6/ip6_ipsec.c,v 1.14 2008/12/02 21:37:28 bz Exp $");
 32 
 33 #include "opt_ipsec.h"
 34 
 35 #include <sys/param.h>
 36 #include <sys/systm.h>
 37 #include <sys/kernel.h>
 38 #include <sys/mac.h>
 39 #include <sys/malloc.h>
 40 #include <sys/mbuf.h>
 41 #include <sys/protosw.h>
 42 #include <sys/socket.h>
 43 #include <sys/socketvar.h>
 44 #include <sys/sysctl.h>
 45 #include <sys/vimage.h>
 46 
 47 #include <net/if.h>
 48 #include <net/route.h>
 49 
 50 #include <netinet/in.h>
 51 #include <netinet/in_systm.h>
 52 #include <netinet/in_var.h>
 53 #include <netinet/ip.h>
 54 #include <netinet/ip6.h>
 55 #include <netinet/in_pcb.h>
 56 #include <netinet/ip_var.h>
 57 #include <netinet/ip_options.h>
 58 
 59 #include <machine/in_cksum.h>
 60 
 61 #ifdef IPSEC
 62 #include <netipsec/ipsec.h>
 63 #include <netipsec/ipsec6.h>
 64 #include <netipsec/xform.h>
 65 #include <netipsec/key.h>
 66 #ifdef IPSEC_DEBUG
 67 #include <netipsec/key_debug.h>
 68 #else
 69 #define KEYDEBUG(lev,arg)
 70 #endif
 71 #endif /*IPSEC*/
 72 
 73 #include <netinet6/ip6_ipsec.h>
 74 #include <netinet6/ip6_var.h>
 75 #include <netinet6/vinet6.h>
 76 
 77 extern  struct protosw inet6sw[];
 78 
 79 /*
 80  * Check if we have to jump over firewall processing for this packet.
 81  * Called from ip_input().
 82  * 1 = jump over firewall, 0 = packet goes through firewall.
 83  */
 84 int
 85 ip6_ipsec_filtertunnel(struct mbuf *m)
 86 {
 87 #if defined(IPSEC) && !defined(IPSEC_FILTERTUNNEL)
 88         /*
 89          * Bypass packet filtering for packets from a tunnel.
 90          */
 91         if (m_tag_find(m, PACKET_TAG_IPSEC_IN_DONE, NULL) != NULL)
 92                 return 1;
 93 #endif
 94         return 0;
 95 }
 96 
 97 /*
 98  * Check if this packet has an active SA and needs to be dropped instead
 99  * of forwarded.
100  * Called from ip_input().
101  * 1 = drop packet, 0 = forward packet.
102  */
103 int
104 ip6_ipsec_fwd(struct mbuf *m)
105 {
106 #ifdef IPSEC
107         INIT_VNET_INET6(curvnet);
108         INIT_VNET_IPSEC(curvnet);
109         struct m_tag *mtag;
110         struct tdb_ident *tdbi;
111         struct secpolicy *sp;
112         int s, error;
113         mtag = m_tag_find(m, PACKET_TAG_IPSEC_IN_DONE, NULL);
114         s = splnet();
115         if (mtag != NULL) {
116                 tdbi = (struct tdb_ident *)(mtag + 1);
117                 sp = ipsec_getpolicy(tdbi, IPSEC_DIR_INBOUND);
118         } else {
119                 sp = ipsec_getpolicybyaddr(m, IPSEC_DIR_INBOUND,
120                                            IP_FORWARDING, &error);
121         }
122         if (sp == NULL) {       /* NB: can happen if error */
123                 splx(s);
124                 /*XXX error stat???*/
125                 DPRINTF(("ip_input: no SP for forwarding\n"));  /*XXX*/
126                 return 1;
127         }
128 
129         /*
130          * Check security policy against packet attributes.
131          */
132         error = ipsec_in_reject(sp, m);
133         KEY_FREESP(&sp);
134         splx(s);
135         if (error) {
136                 V_ip6stat.ip6s_cantforward++;
137                 return 1;
138         }
139 #endif /* IPSEC */
140         return 0;
141 }
142 
143 /*
144  * Check if protocol type doesn't have a further header and do IPSEC
145  * decryption or reject right now.  Protocols with further headers get
146  * their IPSEC treatment within the protocol specific processing.
147  * Called from ip_input().
148  * 1 = drop packet, 0 = continue processing packet.
149  */
150 int
151 ip6_ipsec_input(struct mbuf *m, int nxt)
152 {
153 #ifdef IPSEC
154         INIT_VNET_IPSEC(curvnet);
155         struct m_tag *mtag;
156         struct tdb_ident *tdbi;
157         struct secpolicy *sp;
158         int s, error;
159         /*
160          * enforce IPsec policy checking if we are seeing last header.
161          * note that we do not visit this with protocols with pcb layer
162          * code - like udp/tcp/raw ip.
163          */
164         if ((inet6sw[ip6_protox[nxt]].pr_flags & PR_LASTHDR) != 0 &&
165             ipsec6_in_reject(m, NULL)) {
166 
167                 /*
168                  * Check if the packet has already had IPsec processing
169                  * done.  If so, then just pass it along.  This tag gets
170                  * set during AH, ESP, etc. input handling, before the
171                  * packet is returned to the ip input queue for delivery.
172                  */
173                 mtag = m_tag_find(m, PACKET_TAG_IPSEC_IN_DONE, NULL);
174                 s = splnet();
175                 if (mtag != NULL) {
176                         tdbi = (struct tdb_ident *)(mtag + 1);
177                         sp = ipsec_getpolicy(tdbi, IPSEC_DIR_INBOUND);
178                 } else {
179                         sp = ipsec_getpolicybyaddr(m, IPSEC_DIR_INBOUND,
180                                                    IP_FORWARDING, &error);
181                 }
182                 if (sp != NULL) {
183                         /*
184                          * Check security policy against packet attributes.
185                          */
186                         error = ipsec_in_reject(sp, m);
187                         KEY_FREESP(&sp);
188                 } else {
189                         /* XXX error stat??? */
190                         error = EINVAL;
191                         DPRINTF(("ip_input: no SP, packet discarded\n"));/*XXX*/
192                         return 1;
193                 }
194                 splx(s);
195                 if (error)
196                         return 1;
197         }
198 #endif /* IPSEC */
199         return 0;
200 }
201 
202 /*
203  * Called from ip6_output().
204  * 1 = drop packet, 0 = continue processing packet,
205  * -1 = packet was reinjected and stop processing packet
206  */
207 
208 int
209 ip6_ipsec_output(struct mbuf **m, struct inpcb *inp, int *flags, int *error,
210     struct ifnet **ifp, struct secpolicy **sp)
211 {
212 #ifdef IPSEC
213         struct tdb_ident *tdbi;
214         struct m_tag *mtag;
215         /* XXX int s; */
216         if (sp == NULL)
217                 return 1;
218         mtag = m_tag_find(*m, PACKET_TAG_IPSEC_PENDING_TDB, NULL);
219         if (mtag != NULL) {
220                 tdbi = (struct tdb_ident *)(mtag + 1);
221                 *sp = ipsec_getpolicy(tdbi, IPSEC_DIR_OUTBOUND);
222                 if (*sp == NULL)
223                         *error = -EINVAL;       /* force silent drop */
224                 m_tag_delete(*m, mtag);
225         } else {
226                 *sp = ipsec4_checkpolicy(*m, IPSEC_DIR_OUTBOUND, *flags,
227                                         error, inp);
228         }
229 
230         /*
231          * There are four return cases:
232          *    sp != NULL                    apply IPsec policy
233          *    sp == NULL, error == 0        no IPsec handling needed
234          *    sp == NULL, error == -EINVAL  discard packet w/o error
235          *    sp == NULL, error != 0        discard packet, report error
236          */
237         if (*sp != NULL) {
238                 /* Loop detection, check if ipsec processing already done */
239                 KASSERT((*sp)->req != NULL, ("ip_output: no ipsec request"));
240                 for (mtag = m_tag_first(*m); mtag != NULL;
241                      mtag = m_tag_next(*m, mtag)) {
242                         if (mtag->m_tag_cookie != MTAG_ABI_COMPAT)
243                                 continue;
244                         if (mtag->m_tag_id != PACKET_TAG_IPSEC_OUT_DONE &&
245                             mtag->m_tag_id != PACKET_TAG_IPSEC_OUT_CRYPTO_NEEDED)
246                                 continue;
247                         /*
248                          * Check if policy has an SA associated with it.
249                          * This can happen when an SP has yet to acquire
250                          * an SA; e.g. on first reference.  If it occurs,
251                          * then we let ipsec4_process_packet do its thing.
252                          */
253                         if ((*sp)->req->sav == NULL)
254                                 break;
255                         tdbi = (struct tdb_ident *)(mtag + 1);
256                         if (tdbi->spi == (*sp)->req->sav->spi &&
257                             tdbi->proto == (*sp)->req->sav->sah->saidx.proto &&
258                             bcmp(&tdbi->dst, &(*sp)->req->sav->sah->saidx.dst,
259                                  sizeof (union sockaddr_union)) == 0) {
260                                 /*
261                                  * No IPsec processing is needed, free
262                                  * reference to SP.
263                                  *
264                                  * NB: null pointer to avoid free at
265                                  *     done: below.
266                                  */
267                                 KEY_FREESP(sp), *sp = NULL;
268                                 /* XXX splx(s); */
269                                 goto done;
270                         }
271                 }
272 
273                 /*
274                  * Do delayed checksums now because we send before
275                  * this is done in the normal processing path.
276                  */
277                 if ((*m)->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
278                         in_delayed_cksum(*m);
279                         (*m)->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
280                 }
281 
282                 /*
283                  * Preserve KAME behaviour: ENOENT can be returned
284                  * when an SA acquire is in progress.  Don't propagate
285                  * this to user-level; it confuses applications.
286                  *
287                  * XXX this will go away when the SADB is redone.
288                  */
289                 if (*error == ENOENT)
290                         *error = 0;
291                 goto do_ipsec;
292         } else {        /* sp == NULL */
293                 if (*error != 0) {
294                         /*
295                          * Hack: -EINVAL is used to signal that a packet
296                          * should be silently discarded.  This is typically
297                          * because we asked key management for an SA and
298                          * it was delayed (e.g. kicked up to IKE).
299                          */
300                         if (*error == -EINVAL)
301                                 *error = 0;
302                         goto bad;
303                 } else {
304                         /* No IPsec processing for this packet. */
305                 }
306         }
307 done:
308         return 0;
309 do_ipsec:
310         return -1;
311 bad:
312         return 1;
313 #endif /* IPSEC */
314         return 0;
315 }
316 
317 #if 0
318 /*
319  * Compute the MTU for a forwarded packet that gets IPSEC encapsulated.
320  * Called from ip_forward().
321  * Returns MTU suggestion for ICMP needfrag reply.
322  */
323 int
324 ip6_ipsec_mtu(struct mbuf *m)
325 {
326         int mtu = 0;
327         /*
328          * If the packet is routed over IPsec tunnel, tell the
329          * originator the tunnel MTU.
330          *      tunnel MTU = if MTU - sizeof(IP) - ESP/AH hdrsiz
331          * XXX quickhack!!!
332          */
333 #ifdef IPSEC
334         struct secpolicy *sp = NULL;
335         int ipsecerror;
336         int ipsechdr;
337         struct route *ro;
338         sp = ipsec_getpolicybyaddr(m,
339                                    IPSEC_DIR_OUTBOUND,
340                                    IP_FORWARDING,
341                                    &ipsecerror);
342         if (sp != NULL) {
343                 /* count IPsec header size */
344                 ipsechdr = ipsec4_hdrsiz(m,
345                                          IPSEC_DIR_OUTBOUND,
346                                          NULL);
347 
348                 /*
349                  * find the correct route for outer IPv4
350                  * header, compute tunnel MTU.
351                  */
352                 if (sp->req != NULL &&
353                     sp->req->sav != NULL &&
354                     sp->req->sav->sah != NULL) {
355                         ro = &sp->req->sav->sah->sa_route;
356                         if (ro->ro_rt && ro->ro_rt->rt_ifp) {
357                                 mtu =
358                                     ro->ro_rt->rt_rmx.rmx_mtu ?
359                                     ro->ro_rt->rt_rmx.rmx_mtu :
360                                     ro->ro_rt->rt_ifp->if_mtu;
361                                 mtu -= ipsechdr;
362                         }
363                 }
364                 KEY_FREESP(&sp);
365         }
366 #endif /* IPSEC */
367         /* XXX else case missing. */
368         return mtu;
369 }
370 #endif
371 

[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.