Sync our ieee80211*.9 manual pages with the recent upgrade.
[dragonfly.git] / sys / netproto / ipsec / ipsec_input.c
1 /*      $FreeBSD: src/sys/netipsec/ipsec_input.c,v 1.2.4.2 2003/03/28 20:32:53 sam Exp $        */
2 /*      $DragonFly: src/sys/netproto/ipsec/ipsec_input.c,v 1.10 2006/01/14 13:36:40 swildner Exp $      */
3 /*      $OpenBSD: ipsec_input.c,v 1.63 2003/02/20 18:35:43 deraadt Exp $        */
4 /*
5  * The authors of this code are John Ioannidis (ji@tla.org),
6  * Angelos D. Keromytis (kermit@csd.uch.gr) and
7  * Niels Provos (provos@physnet.uni-hamburg.de).
8  *
9  * This code was written by John Ioannidis for BSD/OS in Athens, Greece,
10  * in November 1995.
11  *
12  * Ported to OpenBSD and NetBSD, with additional transforms, in December 1996,
13  * by Angelos D. Keromytis.
14  *
15  * Additional transforms and features in 1997 and 1998 by Angelos D. Keromytis
16  * and Niels Provos.
17  *
18  * Additional features in 1999 by Angelos D. Keromytis.
19  *
20  * Copyright (C) 1995, 1996, 1997, 1998, 1999 by John Ioannidis,
21  * Angelos D. Keromytis and Niels Provos.
22  * Copyright (c) 2001, Angelos D. Keromytis.
23  *
24  * Permission to use, copy, and modify this software with or without fee
25  * is hereby granted, provided that this entire notice is included in
26  * all copies of any software which is or includes a copy or
27  * modification of this software.
28  * You may use this code under the GNU public license if you so wish. Please
29  * contribute changes back to the authors under this freer than GPL license
30  * so that we may further the use of strong encryption without limitations to
31  * all.
32  *
33  * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR
34  * IMPLIED WARRANTY. IN PARTICULAR, NONE OF THE AUTHORS MAKES ANY
35  * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE
36  * MERCHANTABILITY OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR
37  * PURPOSE.
38  */
39
40 /*
41  * IPsec input processing.
42  */
43
44 #include "opt_inet.h"
45 #include "opt_inet6.h"
46 #include "opt_ipsec.h"
47
48 #include <sys/param.h>
49 #include <sys/systm.h>
50 #include <sys/malloc.h>
51 #include <sys/mbuf.h>
52 #include <sys/domain.h>
53 #include <sys/protosw.h>
54 #include <sys/socket.h>
55 #include <sys/errno.h>
56 #include <sys/syslog.h>
57 #include <sys/in_cksum.h>
58
59 #include <net/if.h>
60 #include <net/route.h>
61 #include <net/netisr.h>
62
63 #include <netinet/in.h>
64 #include <netinet/in_systm.h>
65 #include <netinet/ip.h>
66 #include <netinet/ip_var.h>
67 #include <netinet/in_var.h>
68
69 #include <netinet/ip6.h>
70 #ifdef INET6
71 #include <netinet6/ip6_var.h>
72 #endif
73 #include <netinet/in_pcb.h>
74 #ifdef INET6
75 #include <netinet/icmp6.h>
76 #endif
77
78 #include <netproto/ipsec/ipsec.h>
79 #ifdef INET6
80 #include <netproto/ipsec/ipsec6.h>
81 #endif
82 #include <netproto/ipsec/ah_var.h>
83 #include <netproto/ipsec/esp.h>
84 #include <netproto/ipsec/esp_var.h>
85 #include <netproto/ipsec/ipcomp_var.h>
86
87 #include <netproto/ipsec/key.h>
88 #include <netproto/ipsec/keydb.h>
89
90 #include <netproto/ipsec/xform.h>
91 #include <netinet6/ip6protosw.h>
92
93 #include <machine/stdarg.h>
94
95 #include <net/net_osdep.h>
96
97 #define IPSEC_ISTAT(p,x,y,z) ((p) == IPPROTO_ESP ? (x)++ : \
98                             (p) == IPPROTO_AH ? (y)++ : (z)++)
99
100 /*
101  * ipsec_common_input gets called when an IPsec-protected packet
102  * is received by IPv4 or IPv6.  It's job is to find the right SA
103  # and call the appropriate transform.  The transform callback
104  * takes care of further processing (like ingress filtering).
105  */
106 static int
107 ipsec_common_input(struct mbuf *m, int skip, int protoff, int af, int sproto)
108 {
109         union sockaddr_union dst_address;
110         struct secasvar *sav;
111         u_int32_t spi;
112         int error;
113
114         IPSEC_ISTAT(sproto, espstat.esps_input, ahstat.ahs_input,
115                 ipcompstat.ipcomps_input);
116
117         KASSERT(m != NULL, ("ipsec_common_input: null packet"));
118
119         if ((sproto == IPPROTO_ESP && !esp_enable) ||
120             (sproto == IPPROTO_AH && !ah_enable) ||
121             (sproto == IPPROTO_IPCOMP && !ipcomp_enable)) {
122                 m_freem(m);
123                 IPSEC_ISTAT(sproto, espstat.esps_pdrops, ahstat.ahs_pdrops,
124                     ipcompstat.ipcomps_pdrops);
125                 return EOPNOTSUPP;
126         }
127
128         if (m->m_pkthdr.len - skip < 2 * sizeof (u_int32_t)) {
129                 m_freem(m);
130                 IPSEC_ISTAT(sproto, espstat.esps_hdrops, ahstat.ahs_hdrops,
131                     ipcompstat.ipcomps_hdrops);
132                 DPRINTF(("ipsec_common_input: packet too small\n"));
133                 return EINVAL;
134         }
135
136         /* Retrieve the SPI from the relevant IPsec header */
137         if (sproto == IPPROTO_ESP)
138                 m_copydata(m, skip, sizeof(u_int32_t), (caddr_t) &spi);
139         else if (sproto == IPPROTO_AH)
140                 m_copydata(m, skip + sizeof(u_int32_t), sizeof(u_int32_t),
141                     (caddr_t) &spi);
142         else if (sproto == IPPROTO_IPCOMP) {
143                 u_int16_t cpi;
144                 m_copydata(m, skip + sizeof(u_int16_t), sizeof(u_int16_t),
145                     (caddr_t) &cpi);
146                 spi = ntohl(htons(cpi));
147         }
148
149         /*
150          * Find the SA and (indirectly) call the appropriate
151          * kernel crypto routine. The resulting mbuf chain is a valid
152          * IP packet ready to go through input processing.
153          */
154         bzero(&dst_address, sizeof (dst_address));
155         dst_address.sa.sa_family = af;
156         switch (af) {
157 #ifdef INET
158         case AF_INET:
159                 dst_address.sin.sin_len = sizeof(struct sockaddr_in);
160                 m_copydata(m, offsetof(struct ip, ip_dst),
161                     sizeof(struct in_addr),
162                     (caddr_t) &dst_address.sin.sin_addr);
163                 break;
164 #endif /* INET */
165 #ifdef INET6
166         case AF_INET6:
167                 dst_address.sin6.sin6_len = sizeof(struct sockaddr_in6);
168                 m_copydata(m, offsetof(struct ip6_hdr, ip6_dst),
169                     sizeof(struct in6_addr),
170                     (caddr_t) &dst_address.sin6.sin6_addr);
171                 break;
172 #endif /* INET6 */
173         default:
174                 DPRINTF(("ipsec_common_input: unsupported protocol "
175                         "family %u\n", af));
176                 m_freem(m);
177                 IPSEC_ISTAT(sproto, espstat.esps_nopf, ahstat.ahs_nopf,
178                     ipcompstat.ipcomps_nopf);
179                 return EPFNOSUPPORT;
180         }
181
182         crit_enter();
183
184         /* NB: only pass dst since key_allocsa follows RFC2401 */
185         sav = KEY_ALLOCSA(&dst_address, sproto, spi);
186         if (sav == NULL) {
187                 DPRINTF(("ipsec_common_input: no key association found for"
188                           " SA %s/%08lx/%u\n",
189                           ipsec_address(&dst_address),
190                           (u_long) ntohl(spi), sproto));
191                 IPSEC_ISTAT(sproto, espstat.esps_notdb, ahstat.ahs_notdb,
192                     ipcompstat.ipcomps_notdb);
193                 crit_exit();
194                 m_freem(m);
195                 return ENOENT;
196         }
197
198         if (sav->tdb_xform == NULL) {
199                 DPRINTF(("ipsec_common_input: attempted to use uninitialized"
200                          " SA %s/%08lx/%u\n",
201                          ipsec_address(&dst_address),
202                          (u_long) ntohl(spi), sproto));
203                 IPSEC_ISTAT(sproto, espstat.esps_noxform, ahstat.ahs_noxform,
204                     ipcompstat.ipcomps_noxform);
205                 KEY_FREESAV(&sav);
206                 crit_exit();
207                 m_freem(m);
208                 return ENXIO;
209         }
210
211         /*
212          * Call appropriate transform and return -- callback takes care of
213          * everything else.
214          */
215         error = (*sav->tdb_xform->xf_input)(m, sav, skip, protoff);
216         KEY_FREESAV(&sav);
217         crit_exit();
218         return error;
219 }
220
221 #ifdef INET
222 /*
223  * Common input handler for IPv4 AH, ESP, and IPCOMP.
224  */
225 int
226 ipsec4_common_input(struct mbuf *m, ...)
227 {
228         __va_list ap;
229         int off, nxt;
230
231         __va_start(ap, m);
232         off = __va_arg(ap, int);
233         nxt = __va_arg(ap, int);
234         __va_end(ap);
235
236         return ipsec_common_input(m, off, offsetof(struct ip, ip_p),
237                                   AF_INET, nxt);
238 }
239
240 /*
241  * IPsec input callback for INET protocols.
242  * This routine is called as the transform callback.
243  * Takes care of filtering and other sanity checks on
244  * the processed packet.
245  */
246 int
247 ipsec4_common_input_cb(struct mbuf *m, struct secasvar *sav,
248                         int skip, int protoff, struct m_tag *mt)
249 {
250         int prot, af, sproto;
251         struct ip *ip;
252         struct m_tag *mtag;
253         struct tdb_ident *tdbi;
254         struct secasindex *saidx;
255         int error;
256
257         KASSERT(m != NULL, ("ipsec4_common_input_cb: null mbuf"));
258         KASSERT(sav != NULL, ("ipsec4_common_input_cb: null SA"));
259         KASSERT(sav->sah != NULL, ("ipsec4_common_input_cb: null SAH"));
260         saidx = &sav->sah->saidx;
261         af = saidx->dst.sa.sa_family;
262         KASSERT(af == AF_INET, ("ipsec4_common_input_cb: unexpected af %u",af));
263         sproto = saidx->proto;
264         KASSERT(sproto == IPPROTO_ESP || sproto == IPPROTO_AH ||
265                 sproto == IPPROTO_IPCOMP,
266                 ("ipsec4_common_input_cb: unexpected security protocol %u",
267                 sproto));
268
269         /* Sanity check */
270         if (m == NULL) {
271                 DPRINTF(("ipsec4_common_input_cb: null mbuf"));
272                 IPSEC_ISTAT(sproto, espstat.esps_badkcr, ahstat.ahs_badkcr,
273                     ipcompstat.ipcomps_badkcr);
274                 KEY_FREESAV(&sav);
275                 return EINVAL;
276         }
277
278         if (skip != 0) {
279                 /* Fix IPv4 header */
280                 if (m->m_len < skip && (m = m_pullup(m, skip)) == NULL) {
281                         DPRINTF(("ipsec4_common_input_cb: processing failed "
282                             "for SA %s/%08lx\n",
283                             ipsec_address(&sav->sah->saidx.dst),
284                             (u_long) ntohl(sav->spi)));
285                         IPSEC_ISTAT(sproto, espstat.esps_hdrops, ahstat.ahs_hdrops,
286                             ipcompstat.ipcomps_hdrops);
287                         error = ENOBUFS;
288                         goto bad;
289                 }
290
291                 ip = mtod(m, struct ip *);
292                 ip->ip_len = htons(m->m_pkthdr.len);
293                 ip->ip_off = htons(ip->ip_off);
294                 ip->ip_sum = 0;
295                 ip->ip_sum = in_cksum(m, ip->ip_hl << 2);
296         } else {
297                 ip = mtod(m, struct ip *);
298         }
299         prot = ip->ip_p;
300
301         /* IP-in-IP encapsulation */
302         if (prot == IPPROTO_IPIP) {
303                 struct ip ipn;
304
305                 /* ipn will now contain the inner IPv4 header */
306                 m_copydata(m, ip->ip_hl << 2, sizeof(struct ip),
307                     (caddr_t) &ipn);
308
309 #ifdef notyet
310                 /* XXX PROXY address isn't recorded in SAH */
311                 /*
312                  * Check that the inner source address is the same as
313                  * the proxy address, if available.
314                  */
315                 if ((saidx->proxy.sa.sa_family == AF_INET &&
316                     saidx->proxy.sin.sin_addr.s_addr !=
317                     INADDR_ANY &&
318                     ipn.ip_src.s_addr !=
319                     saidx->proxy.sin.sin_addr.s_addr) ||
320                     (saidx->proxy.sa.sa_family != AF_INET &&
321                         saidx->proxy.sa.sa_family != 0)) {
322
323                         DPRINTF(("ipsec4_common_input_cb: inner "
324                             "source address %s doesn't correspond to "
325                             "expected proxy source %s, SA %s/%08lx\n",
326                             inet_ntoa4(ipn.ip_src),
327                             ipsp_address(saidx->proxy),
328                             ipsp_address(saidx->dst),
329                             (u_long) ntohl(sav->spi)));
330
331                         IPSEC_ISTAT(sproto, espstat.esps_pdrops,
332                             ahstat.ahs_pdrops,
333                             ipcompstat.ipcomps_pdrops);
334                         error = EACCES;
335                         goto bad;
336                 }
337 #endif /*XXX*/
338         }
339 #if INET6
340         /* IPv6-in-IP encapsulation. */
341         if (prot == IPPROTO_IPV6) {
342                 struct ip6_hdr ip6n;
343
344                 /* ip6n will now contain the inner IPv6 header. */
345                 m_copydata(m, ip->ip_hl << 2, sizeof(struct ip6_hdr),
346                     (caddr_t) &ip6n);
347
348 #ifdef notyet
349                 /*
350                  * Check that the inner source address is the same as
351                  * the proxy address, if available.
352                  */
353                 if ((saidx->proxy.sa.sa_family == AF_INET6 &&
354                     !IN6_IS_ADDR_UNSPECIFIED(&saidx->proxy.sin6.sin6_addr) &&
355                     !IN6_ARE_ADDR_EQUAL(&ip6n.ip6_src,
356                         &saidx->proxy.sin6.sin6_addr)) ||
357                     (saidx->proxy.sa.sa_family != AF_INET6 &&
358                         saidx->proxy.sa.sa_family != 0)) {
359
360                         DPRINTF(("ipsec4_common_input_cb: inner "
361                             "source address %s doesn't correspond to "
362                             "expected proxy source %s, SA %s/%08lx\n",
363                             ip6_sprintf(&ip6n.ip6_src),
364                             ipsec_address(&saidx->proxy),
365                             ipsec_address(&saidx->dst),
366                             (u_long) ntohl(sav->spi)));
367
368                         IPSEC_ISTAT(sproto, espstat.esps_pdrops,
369                             ahstat.ahs_pdrops,
370                             ipcompstat.ipcomps_pdrops);
371                         error = EACCES;
372                         goto bad;
373                 }
374 #endif /*XXX*/
375         }
376 #endif /* INET6 */
377
378         /*
379          * Record what we've done to the packet (under what SA it was
380          * processed). If we've been passed an mtag, it means the packet
381          * was already processed by an ethernet/crypto combo card and
382          * thus has a tag attached with all the right information, but
383          * with a PACKET_TAG_IPSEC_IN_CRYPTO_DONE as opposed to
384          * PACKET_TAG_IPSEC_IN_DONE type; in that case, just change the type.
385          */
386         if (mt == NULL && sproto != IPPROTO_IPCOMP) {
387                 mtag = m_tag_get(PACKET_TAG_IPSEC_IN_DONE,
388                                 sizeof(struct tdb_ident), M_NOWAIT);
389                 if (mtag == NULL) {
390                         DPRINTF(("ipsec4_common_input_cb: failed to get tag\n"));
391                         IPSEC_ISTAT(sproto, espstat.esps_hdrops,
392                             ahstat.ahs_hdrops, ipcompstat.ipcomps_hdrops);
393                         error = ENOMEM;
394                         goto bad;
395                 }
396
397                 tdbi = (struct tdb_ident *)m_tag_data(mtag);
398                 bcopy(&saidx->dst, &tdbi->dst, saidx->dst.sa.sa_len);
399                 tdbi->proto = sproto;
400                 tdbi->spi = sav->spi;
401
402                 m_tag_prepend(m, mtag);
403         } else {
404                 mt->m_tag_id = PACKET_TAG_IPSEC_IN_DONE;
405                 /* XXX do we need to mark m_flags??? */
406         }
407
408         key_sa_recordxfer(sav, m);              /* record data transfer */
409
410         /*
411          * Re-dispatch via software interrupt.
412          */
413         if (netisr_queue(NETISR_IP, m)) {
414                 IPSEC_ISTAT(sproto, espstat.esps_qfull, ahstat.ahs_qfull,
415                             ipcompstat.ipcomps_qfull);
416
417                 DPRINTF(("ipsec4_common_input_cb: queue full; "
418                         "proto %u packet dropped\n", sproto));
419                 return ENOBUFS;
420         }
421         return 0;
422 bad:
423         m_freem(m);
424         return error;
425 }
426 #endif /* INET */
427
428 #ifdef INET6
429 /* IPv6 AH wrapper. */
430 int
431 ipsec6_common_input(struct mbuf **mp, int *offp, int proto)
432 {
433         int l = 0;
434         int protoff;
435         struct ip6_ext ip6e;
436
437         if (*offp < sizeof(struct ip6_hdr)) {
438                 DPRINTF(("ipsec6_common_input: bad offset %u\n", *offp));
439                 return IPPROTO_DONE;
440         } else if (*offp == sizeof(struct ip6_hdr)) {
441                 protoff = offsetof(struct ip6_hdr, ip6_nxt);
442         } else {
443                 /* Chase down the header chain... */
444                 protoff = sizeof(struct ip6_hdr);
445
446                 do {
447                         protoff += l;
448                         m_copydata(*mp, protoff, sizeof(ip6e),
449                             (caddr_t) &ip6e);
450
451                         if (ip6e.ip6e_nxt == IPPROTO_AH)
452                                 l = (ip6e.ip6e_len + 2) << 2;
453                         else
454                                 l = (ip6e.ip6e_len + 1) << 3;
455                         KASSERT(l > 0, ("ah6_input: l went zero or negative"));
456                 } while (protoff + l < *offp);
457
458                 /* Malformed packet check */
459                 if (protoff + l != *offp) {
460                         DPRINTF(("ipsec6_common_input: bad packet header chain, "
461                                 "protoff %u, l %u, off %u\n", protoff, l, *offp));
462                         IPSEC_ISTAT(proto, espstat.esps_hdrops,
463                                     ahstat.ahs_hdrops,
464                                     ipcompstat.ipcomps_hdrops);
465                         m_freem(*mp);
466                         *mp = NULL;
467                         return IPPROTO_DONE;
468                 }
469                 protoff += offsetof(struct ip6_ext, ip6e_nxt);
470         }
471         ipsec_common_input(*mp, *offp, protoff, AF_INET6, proto);
472         return IPPROTO_DONE;
473 }
474
475 void
476 esp6_ctlinput(int cmd, struct sockaddr *sa, void *d)
477 {
478         if (sa->sa_family != AF_INET6 ||
479             sa->sa_len != sizeof(struct sockaddr_in6))
480                 return;
481         if ((unsigned)cmd >= PRC_NCMDS)
482                 return;
483
484         /* if the parameter is from icmp6, decode it. */
485         if (d !=  NULL) {
486                 struct ip6ctlparam *ip6cp = (struct ip6ctlparam *)d;
487                 struct mbuf *m = ip6cp->ip6c_m;
488                 int off = ip6cp->ip6c_off;
489
490                 struct ip6ctlparam ip6cp1;
491
492                 /*
493                  * Notify the error to all possible sockets via pfctlinput2.
494                  * Since the upper layer information (such as protocol type,
495                  * source and destination ports) is embedded in the encrypted
496                  * data and might have been cut, we can't directly call
497                  * an upper layer ctlinput function. However, the pcbnotify
498                  * function will consider source and destination addresses
499                  * as well as the flow info value, and may be able to find
500                  * some PCB that should be notified.
501                  * Although pfctlinput2 will call esp6_ctlinput(), there is
502                  * no possibility of an infinite loop of function calls,
503                  * because we don't pass the inner IPv6 header.
504                  */
505                 bzero(&ip6cp1, sizeof(ip6cp1));
506                 ip6cp1.ip6c_src = ip6cp->ip6c_src;
507                 pfctlinput2(cmd, sa, (void *)&ip6cp1);
508
509                 /*
510                  * Then go to special cases that need ESP header information.
511                  * XXX: We assume that when ip6 is non NULL,
512                  * M and OFF are valid.
513                  */
514
515                 if (cmd == PRC_MSGSIZE) {
516                         struct secasvar *sav;
517                         u_int32_t spi;
518                         int valid;
519
520                         /* check header length before using m_copydata */
521                         if (m->m_pkthdr.len < off + sizeof (struct esp))
522                                 return;
523                         m_copydata(m, off + offsetof(struct esp, esp_spi),
524                                 sizeof(u_int32_t), (caddr_t) &spi);
525                         /*
526                          * Check to see if we have a valid SA corresponding to
527                          * the address in the ICMP message payload.
528                          */
529                         sav = KEY_ALLOCSA((union sockaddr_union *)sa,
530                                         IPPROTO_ESP, spi);
531                         valid = (sav != NULL);
532                         if (sav)
533                                 KEY_FREESAV(&sav);
534
535                         /* XXX Further validation? */
536
537                         /*
538                          * Depending on whether the SA is "valid" and
539                          * routing table size (mtudisc_{hi,lo}wat), we will:
540                          * - recalcurate the new MTU and create the
541                          *   corresponding routing entry, or
542                          * - ignore the MTU change notification.
543                          */
544                         icmp6_mtudisc_update(ip6cp, valid);
545                 }
546         } else {
547                 /* we normally notify any pcb here */
548         }
549 }
550
551 extern  struct ip6protosw inet6sw[];
552 extern  u_char ip6_protox[];
553
554 /*
555  * IPsec input callback, called by the transform callback. Takes care of
556  * filtering and other sanity checks on the processed packet.
557  */
558 int
559 ipsec6_common_input_cb(struct mbuf *m, struct secasvar *sav, int skip, int protoff,
560     struct m_tag *mt)
561 {
562         int prot, af, sproto;
563         struct ip6_hdr *ip6;
564         struct m_tag *mtag;
565         struct tdb_ident *tdbi;
566         struct secasindex *saidx;
567         int nxt;
568         u_int8_t nxt8;
569         int error, nest;
570
571         KASSERT(m != NULL, ("ipsec6_common_input_cb: null mbuf"));
572         KASSERT(sav != NULL, ("ipsec6_common_input_cb: null SA"));
573         KASSERT(sav->sah != NULL, ("ipsec6_common_input_cb: null SAH"));
574         saidx = &sav->sah->saidx;
575         af = saidx->dst.sa.sa_family;
576         KASSERT(af == AF_INET6,
577                 ("ipsec6_common_input_cb: unexpected af %u", af));
578         sproto = saidx->proto;
579         KASSERT(sproto == IPPROTO_ESP || sproto == IPPROTO_AH ||
580                 sproto == IPPROTO_IPCOMP,
581                 ("ipsec6_common_input_cb: unexpected security protocol %u",
582                 sproto));
583
584         /* Sanity check */
585         if (m == NULL) {
586                 DPRINTF(("ipsec4_common_input_cb: null mbuf"));
587                 IPSEC_ISTAT(sproto, espstat.esps_badkcr, ahstat.ahs_badkcr,
588                     ipcompstat.ipcomps_badkcr);
589                 error = EINVAL;
590                 goto bad;
591         }
592
593         /* Fix IPv6 header */
594         if (m->m_len < sizeof(struct ip6_hdr) &&
595             (m = m_pullup(m, sizeof(struct ip6_hdr))) == NULL) {
596
597                 DPRINTF(("ipsec_common_input_cb: processing failed "
598                     "for SA %s/%08lx\n", ipsec_address(&sav->sah->saidx.dst),
599                     (u_long) ntohl(sav->spi)));
600
601                 IPSEC_ISTAT(sproto, espstat.esps_hdrops, ahstat.ahs_hdrops,
602                     ipcompstat.ipcomps_hdrops);
603                 error = EACCES;
604                 goto bad;
605         }
606
607         ip6 = mtod(m, struct ip6_hdr *);
608         ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(struct ip6_hdr));
609
610         /* Save protocol */
611         m_copydata(m, protoff, 1, (unsigned char *) &prot);
612
613 #ifdef INET
614         /* IP-in-IP encapsulation */
615         if (prot == IPPROTO_IPIP) {
616                 struct ip ipn;
617
618                 /* ipn will now contain the inner IPv4 header */
619                 m_copydata(m, skip, sizeof(struct ip), (caddr_t) &ipn);
620
621 #ifdef notyet
622                 /*
623                  * Check that the inner source address is the same as
624                  * the proxy address, if available.
625                  */
626                 if ((saidx->proxy.sa.sa_family == AF_INET &&
627                     saidx->proxy.sin.sin_addr.s_addr != INADDR_ANY &&
628                     ipn.ip_src.s_addr != saidx->proxy.sin.sin_addr.s_addr) ||
629                     (saidx->proxy.sa.sa_family != AF_INET &&
630                         saidx->proxy.sa.sa_family != 0)) {
631
632                         DPRINTF(("ipsec_common_input_cb: inner "
633                             "source address %s doesn't correspond to "
634                             "expected proxy source %s, SA %s/%08lx\n",
635                             inet_ntoa4(ipn.ip_src),
636                             ipsec_address(&saidx->proxy),
637                             ipsec_address(&saidx->dst),
638                             (u_long) ntohl(sav->spi)));
639
640                         IPSEC_ISTATsproto, (espstat.esps_pdrops,
641                             ahstat.ahs_pdrops, ipcompstat.ipcomps_pdrops);
642                         error = EACCES;
643                         goto bad;
644                 }
645 #endif /*XXX*/
646         }
647 #endif /* INET */
648
649         /* IPv6-in-IP encapsulation */
650         if (prot == IPPROTO_IPV6) {
651                 struct ip6_hdr ip6n;
652
653                 /* ip6n will now contain the inner IPv6 header. */
654                 m_copydata(m, skip, sizeof(struct ip6_hdr),
655                     (caddr_t) &ip6n);
656
657 #ifdef notyet
658                 /*
659                  * Check that the inner source address is the same as
660                  * the proxy address, if available.
661                  */
662                 if ((saidx->proxy.sa.sa_family == AF_INET6 &&
663                     !IN6_IS_ADDR_UNSPECIFIED(&saidx->proxy.sin6.sin6_addr) &&
664                     !IN6_ARE_ADDR_EQUAL(&ip6n.ip6_src,
665                         &saidx->proxy.sin6.sin6_addr)) ||
666                     (saidx->proxy.sa.sa_family != AF_INET6 &&
667                         saidx->proxy.sa.sa_family != 0)) {
668
669                         DPRINTF(("ipsec_common_input_cb: inner "
670                             "source address %s doesn't correspond to "
671                             "expected proxy source %s, SA %s/%08lx\n",
672                             ip6_sprintf(&ip6n.ip6_src),
673                             ipsec_address(&saidx->proxy),
674                             ipsec_address(&saidx->dst),
675                             (u_long) ntohl(sav->spi)));
676
677                         IPSEC_ISTAT(sproto, espstat.esps_pdrops,
678                             ahstat.ahs_pdrops, ipcompstat.ipcomps_pdrops);
679                         error = EACCES;
680                         goto bad;
681                 }
682 #endif /*XXX*/
683         }
684
685         /*
686          * Record what we've done to the packet (under what SA it was
687          * processed). If we've been passed an mtag, it means the packet
688          * was already processed by an ethernet/crypto combo card and
689          * thus has a tag attached with all the right information, but
690          * with a PACKET_TAG_IPSEC_IN_CRYPTO_DONE as opposed to
691          * PACKET_TAG_IPSEC_IN_DONE type; in that case, just change the type.
692          */
693         if (mt == NULL && sproto != IPPROTO_IPCOMP) {
694                 mtag = m_tag_get(PACKET_TAG_IPSEC_IN_DONE,
695                                 sizeof(struct tdb_ident), M_NOWAIT);
696                 if (mtag == NULL) {
697                         DPRINTF(("ipsec_common_input_cb: failed to "
698                             "get tag\n"));
699                         IPSEC_ISTAT(sproto, espstat.esps_hdrops,
700                             ahstat.ahs_hdrops, ipcompstat.ipcomps_hdrops);
701                         error = ENOMEM;
702                         goto bad;
703                 }
704
705                 tdbi = (struct tdb_ident *)m_tag_data(mtag);
706                 bcopy(&saidx->dst, &tdbi->dst, sizeof(union sockaddr_union));
707                 tdbi->proto = sproto;
708                 tdbi->spi = sav->spi;
709
710                 m_tag_prepend(m, mtag);
711         } else {
712                 mt->m_tag_id = PACKET_TAG_IPSEC_IN_DONE;
713                 /* XXX do we need to mark m_flags??? */
714         }
715
716         key_sa_recordxfer(sav, m);
717
718         /* Retrieve new protocol */
719         m_copydata(m, protoff, sizeof(u_int8_t), (caddr_t) &nxt8);
720
721         /*
722          * See the end of ip6_input for this logic.
723          * IPPROTO_IPV[46] case will be processed just like other ones
724          */
725         nest = 0;
726         nxt = nxt8;
727         while (nxt != IPPROTO_DONE) {
728                 if (ip6_hdrnestlimit && (++nest > ip6_hdrnestlimit)) {
729                         ip6stat.ip6s_toomanyhdr++;
730                         error = EINVAL;
731                         goto bad;
732                 }
733
734                 /*
735                  * Protection against faulty packet - there should be
736                  * more sanity checks in header chain processing.
737                  */
738                 if (m->m_pkthdr.len < skip) {
739                         ip6stat.ip6s_tooshort++;
740                         in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_truncated);
741                         error = EINVAL;
742                         goto bad;
743                 }
744                 /*
745                  * Enforce IPsec policy checking if we are seeing last header.
746                  * note that we do not visit this with protocols with pcb layer
747                  * code - like udp/tcp/raw ip.
748                  */
749                 if ((inet6sw[ip6_protox[nxt]].pr_flags & PR_LASTHDR) != 0 &&
750                     ipsec6_in_reject(m, NULL)) {
751                         error = EINVAL;
752                         goto bad;
753                 }
754                 nxt = (*inet6sw[ip6_protox[nxt]].pr_input)(&m, &skip, nxt);
755         }
756         return 0;
757 bad:
758         if (m)
759                 m_freem(m);
760         return error;
761 }
762 #endif /* INET6 */