]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/netipsec/ipsec_input.c
Oops - use the wrong array offset.
[FreeBSD/FreeBSD.git] / sys / netipsec / ipsec_input.c
1 /*      $FreeBSD$       */
2 /*      $OpenBSD: ipsec_input.c,v 1.63 2003/02/20 18:35:43 deraadt Exp $        */
3 /*-
4  * The authors of this code are John Ioannidis (ji@tla.org),
5  * Angelos D. Keromytis (kermit@csd.uch.gr) and
6  * Niels Provos (provos@physnet.uni-hamburg.de).
7  *
8  * This code was written by John Ioannidis for BSD/OS in Athens, Greece,
9  * in November 1995.
10  *
11  * Ported to OpenBSD and NetBSD, with additional transforms, in December 1996,
12  * by Angelos D. Keromytis.
13  *
14  * Additional transforms and features in 1997 and 1998 by Angelos D. Keromytis
15  * and Niels Provos.
16  *
17  * Additional features in 1999 by Angelos D. Keromytis.
18  *
19  * Copyright (C) 1995, 1996, 1997, 1998, 1999 by John Ioannidis,
20  * Angelos D. Keromytis and Niels Provos.
21  * Copyright (c) 2001, Angelos D. Keromytis.
22  *
23  * Permission to use, copy, and modify this software with or without fee
24  * is hereby granted, provided that this entire notice is included in
25  * all copies of any software which is or includes a copy or
26  * modification of this software.
27  * You may use this code under the GNU public license if you so wish. Please
28  * contribute changes back to the authors under this freer than GPL license
29  * so that we may further the use of strong encryption without limitations to
30  * all.
31  *
32  * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR
33  * IMPLIED WARRANTY. IN PARTICULAR, NONE OF THE AUTHORS MAKES ANY
34  * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE
35  * MERCHANTABILITY OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR
36  * PURPOSE.
37  */
38
39 /*
40  * IPsec input processing.
41  */
42
43 #include "opt_inet.h"
44 #include "opt_inet6.h"
45 #include "opt_ipsec.h"
46 #include "opt_enc.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
58 #include <net/if.h>
59 #include <net/if_var.h>
60 #include <net/pfil.h>
61 #include <net/netisr.h>
62 #include <net/vnet.h>
63
64 #include <netinet/in.h>
65 #include <netinet/in_systm.h>
66 #include <netinet/ip.h>
67 #include <netinet/ip_var.h>
68 #include <netinet/in_var.h>
69
70 #include <netinet/ip6.h>
71 #ifdef INET6
72 #include <netinet6/ip6_var.h>
73 #endif
74 #include <netinet/in_pcb.h>
75 #ifdef INET6
76 #include <netinet/icmp6.h>
77 #endif
78
79 #include <netipsec/ipsec.h>
80 #ifdef INET6
81 #include <netipsec/ipsec6.h>
82 #endif
83 #include <netipsec/ah_var.h>
84 #include <netipsec/esp.h>
85 #include <netipsec/esp_var.h>
86 #include <netipsec/ipcomp_var.h>
87
88 #include <netipsec/key.h>
89 #include <netipsec/keydb.h>
90
91 #include <netipsec/xform.h>
92 #include <netinet6/ip6protosw.h>
93
94 #include <machine/in_cksum.h>
95 #include <machine/stdarg.h>
96
97 #ifdef DEV_ENC
98 #include <net/if_enc.h>
99 #endif
100
101
102 #define IPSEC_ISTAT(proto, name)        do {    \
103         if ((proto) == IPPROTO_ESP)             \
104                 ESPSTAT_INC(esps_##name);       \
105         else if ((proto) == IPPROTO_AH)         \
106                 AHSTAT_INC(ahs_##name);         \
107         else                                    \
108                 IPCOMPSTAT_INC(ipcomps_##name); \
109 } while (0)
110
111 #ifdef INET
112 static void ipsec4_common_ctlinput(int, struct sockaddr *, void *, int);
113 #endif
114
115 /*
116  * ipsec_common_input gets called when an IPsec-protected packet
117  * is received by IPv4 or IPv6.  Its job is to find the right SA
118  * and call the appropriate transform.  The transform callback
119  * takes care of further processing (like ingress filtering).
120  */
121 int
122 ipsec_common_input(struct mbuf *m, int skip, int protoff, int af, int sproto)
123 {
124         char buf[INET6_ADDRSTRLEN];
125         union sockaddr_union dst_address;
126         struct secasvar *sav;
127         u_int32_t spi;
128         int error;
129 #ifdef INET
130 #ifdef IPSEC_NAT_T
131         struct m_tag *tag;
132 #endif
133 #endif
134
135         IPSEC_ISTAT(sproto, input);
136
137         IPSEC_ASSERT(m != NULL, ("null packet"));
138
139         IPSEC_ASSERT(sproto == IPPROTO_ESP || sproto == IPPROTO_AH ||
140                 sproto == IPPROTO_IPCOMP,
141                 ("unexpected security protocol %u", sproto));
142
143         if ((sproto == IPPROTO_ESP && !V_esp_enable) ||
144             (sproto == IPPROTO_AH && !V_ah_enable) ||
145             (sproto == IPPROTO_IPCOMP && !V_ipcomp_enable)) {
146                 m_freem(m);
147                 IPSEC_ISTAT(sproto, pdrops);
148                 return EOPNOTSUPP;
149         }
150
151         if (m->m_pkthdr.len - skip < 2 * sizeof (u_int32_t)) {
152                 m_freem(m);
153                 IPSEC_ISTAT(sproto, hdrops);
154                 DPRINTF(("%s: packet too small\n", __func__));
155                 return EINVAL;
156         }
157
158         /* Retrieve the SPI from the relevant IPsec header */
159         if (sproto == IPPROTO_ESP)
160                 m_copydata(m, skip, sizeof(u_int32_t), (caddr_t) &spi);
161         else if (sproto == IPPROTO_AH)
162                 m_copydata(m, skip + sizeof(u_int32_t), sizeof(u_int32_t),
163                     (caddr_t) &spi);
164         else if (sproto == IPPROTO_IPCOMP) {
165                 u_int16_t cpi;
166                 m_copydata(m, skip + sizeof(u_int16_t), sizeof(u_int16_t),
167                     (caddr_t) &cpi);
168                 spi = ntohl(htons(cpi));
169         }
170
171         /*
172          * Find the SA and (indirectly) call the appropriate
173          * kernel crypto routine. The resulting mbuf chain is a valid
174          * IP packet ready to go through input processing.
175          */
176         bzero(&dst_address, sizeof (dst_address));
177         dst_address.sa.sa_family = af;
178         switch (af) {
179 #ifdef INET
180         case AF_INET:
181                 dst_address.sin.sin_len = sizeof(struct sockaddr_in);
182                 m_copydata(m, offsetof(struct ip, ip_dst),
183                     sizeof(struct in_addr),
184                     (caddr_t) &dst_address.sin.sin_addr);
185 #ifdef IPSEC_NAT_T
186                 /* Find the source port for NAT-T; see udp*_espdecap. */
187                 tag = m_tag_find(m, PACKET_TAG_IPSEC_NAT_T_PORTS, NULL);
188                 if (tag != NULL)
189                         dst_address.sin.sin_port = ((u_int16_t *)(tag + 1))[1];
190 #endif /* IPSEC_NAT_T */
191                 break;
192 #endif /* INET */
193 #ifdef INET6
194         case AF_INET6:
195                 dst_address.sin6.sin6_len = sizeof(struct sockaddr_in6);
196                 m_copydata(m, offsetof(struct ip6_hdr, ip6_dst),
197                     sizeof(struct in6_addr),
198                     (caddr_t) &dst_address.sin6.sin6_addr);
199                 /* We keep addresses in SADB without embedded scope id */
200                 if (IN6_IS_SCOPE_LINKLOCAL(&dst_address.sin6.sin6_addr)) {
201                         /* XXX: sa6_recoverscope() */
202                         dst_address.sin6.sin6_scope_id =
203                             ntohs(dst_address.sin6.sin6_addr.s6_addr16[1]);
204                         dst_address.sin6.sin6_addr.s6_addr16[1] = 0;
205                 }
206                 break;
207 #endif /* INET6 */
208         default:
209                 DPRINTF(("%s: unsupported protocol family %u\n", __func__, af));
210                 m_freem(m);
211                 IPSEC_ISTAT(sproto, nopf);
212                 return EPFNOSUPPORT;
213         }
214
215         /* NB: only pass dst since key_allocsa follows RFC2401 */
216         sav = KEY_ALLOCSA(&dst_address, sproto, spi);
217         if (sav == NULL) {
218                 DPRINTF(("%s: no key association found for SA %s/%08lx/%u\n",
219                     __func__, ipsec_address(&dst_address, buf, sizeof(buf)),
220                     (u_long) ntohl(spi), sproto));
221                 IPSEC_ISTAT(sproto, notdb);
222                 m_freem(m);
223                 return ENOENT;
224         }
225
226         if (sav->tdb_xform == NULL) {
227                 DPRINTF(("%s: attempted to use uninitialized SA %s/%08lx/%u\n",
228                     __func__, ipsec_address(&dst_address, buf, sizeof(buf)),
229                     (u_long) ntohl(spi), sproto));
230                 IPSEC_ISTAT(sproto, noxform);
231                 KEY_FREESAV(&sav);
232                 m_freem(m);
233                 return ENXIO;
234         }
235
236         /*
237          * Call appropriate transform and return -- callback takes care of
238          * everything else.
239          */
240         error = (*sav->tdb_xform->xf_input)(m, sav, skip, protoff);
241         KEY_FREESAV(&sav);
242         return error;
243 }
244
245 #ifdef INET
246 int
247 ah4_input(struct mbuf **mp, int *offp, int proto)
248 {
249         struct mbuf *m;
250         int off;
251
252         m = *mp;
253         off = *offp;
254         *mp = NULL;
255
256         ipsec_common_input(m, off, offsetof(struct ip, ip_p),
257                                 AF_INET, IPPROTO_AH);
258         return (IPPROTO_DONE);
259 }
260 void
261 ah4_ctlinput(int cmd, struct sockaddr *sa, void *v)
262 {
263         if (sa->sa_family == AF_INET &&
264             sa->sa_len == sizeof(struct sockaddr_in))
265                 ipsec4_common_ctlinput(cmd, sa, v, IPPROTO_AH);
266 }
267
268 int
269 esp4_input(struct mbuf **mp, int *offp, int proto)
270 {
271         struct mbuf *m;
272         int off;
273
274         m = *mp;
275         off = *offp;
276         mp = NULL;
277
278         ipsec_common_input(m, off, offsetof(struct ip, ip_p),
279                                 AF_INET, IPPROTO_ESP);
280         return (IPPROTO_DONE);
281 }
282
283 void
284 esp4_ctlinput(int cmd, struct sockaddr *sa, void *v)
285 {
286         if (sa->sa_family == AF_INET &&
287             sa->sa_len == sizeof(struct sockaddr_in))
288                 ipsec4_common_ctlinput(cmd, sa, v, IPPROTO_ESP);
289 }
290
291 int
292 ipcomp4_input(struct mbuf **mp, int *offp, int proto)
293 {
294         struct mbuf *m;
295         int off;
296
297         m = *mp;
298         off = *offp;
299         mp = NULL;
300
301         ipsec_common_input(m, off, offsetof(struct ip, ip_p),
302                                 AF_INET, IPPROTO_IPCOMP);
303         return (IPPROTO_DONE);
304 }
305
306 /*
307  * IPsec input callback for INET protocols.
308  * This routine is called as the transform callback.
309  * Takes care of filtering and other sanity checks on
310  * the processed packet.
311  */
312 int
313 ipsec4_common_input_cb(struct mbuf *m, struct secasvar *sav, int skip,
314     int protoff)
315 {
316         char buf[INET6_ADDRSTRLEN];
317         int prot, af, sproto, isr_prot;
318         struct ip *ip;
319         struct m_tag *mtag;
320         struct tdb_ident *tdbi;
321         struct secasindex *saidx;
322         int error;
323 #ifdef INET6
324 #ifdef notyet
325         char ip6buf[INET6_ADDRSTRLEN];
326 #endif
327 #endif
328
329         IPSEC_ASSERT(m != NULL, ("null mbuf"));
330         IPSEC_ASSERT(sav != NULL, ("null SA"));
331         IPSEC_ASSERT(sav->sah != NULL, ("null SAH"));
332         saidx = &sav->sah->saidx;
333         af = saidx->dst.sa.sa_family;
334         IPSEC_ASSERT(af == AF_INET, ("unexpected af %u", af));
335         sproto = saidx->proto;
336         IPSEC_ASSERT(sproto == IPPROTO_ESP || sproto == IPPROTO_AH ||
337                 sproto == IPPROTO_IPCOMP,
338                 ("unexpected security protocol %u", sproto));
339
340         /* Sanity check */
341         if (m == NULL) {
342                 DPRINTF(("%s: null mbuf", __func__));
343                 IPSEC_ISTAT(sproto, badkcr);
344                 KEY_FREESAV(&sav);
345                 return EINVAL;
346         }
347
348         if (skip != 0) {
349                 /*
350                  * Fix IPv4 header
351                  * XXXGL: do we need this entire block?
352                  */
353                 if (m->m_len < skip && (m = m_pullup(m, skip)) == NULL) {
354                         DPRINTF(("%s: processing failed for SA %s/%08lx\n",
355                             __func__, ipsec_address(&sav->sah->saidx.dst,
356                             buf, sizeof(buf)), (u_long) ntohl(sav->spi)));
357                         IPSEC_ISTAT(sproto, hdrops);
358                         error = ENOBUFS;
359                         goto bad;
360                 }
361
362                 ip = mtod(m, struct ip *);
363                 ip->ip_len = htons(m->m_pkthdr.len);
364                 ip->ip_sum = 0;
365                 ip->ip_sum = in_cksum(m, ip->ip_hl << 2);
366         } else {
367                 ip = mtod(m, struct ip *);
368         }
369         prot = ip->ip_p;
370
371 #ifdef DEV_ENC
372         if_inc_counter(encif, IFCOUNTER_IPACKETS, 1);
373         if_inc_counter(encif, IFCOUNTER_IBYTES, m->m_pkthdr.len);
374
375         /* Pass the mbuf to enc0 for bpf and pfil. */
376         ipsec_bpf(m, sav, AF_INET, ENC_IN|ENC_BEFORE);
377         if ((error = ipsec_filter(&m, PFIL_IN, ENC_IN|ENC_BEFORE)) != 0)
378                 return (error);
379         ip = mtod(m, struct ip *);
380 #endif /* DEV_ENC */
381
382         /* IP-in-IP encapsulation */
383         if (prot == IPPROTO_IPIP &&
384             saidx->mode != IPSEC_MODE_TRANSPORT) {
385
386                 if (m->m_pkthdr.len - skip < sizeof(struct ip)) {
387                         IPSEC_ISTAT(sproto, hdrops);
388                         error = EINVAL;
389                         goto bad;
390                 }
391                 /* enc0: strip outer IPv4 header */
392                 m_striphdr(m, 0, ip->ip_hl << 2);
393
394 #ifdef notyet
395                 /* XXX PROXY address isn't recorded in SAH */
396                 /*
397                  * Check that the inner source address is the same as
398                  * the proxy address, if available.
399                  */
400                 if ((saidx->proxy.sa.sa_family == AF_INET &&
401                     saidx->proxy.sin.sin_addr.s_addr !=
402                     INADDR_ANY &&
403                     ipn.ip_src.s_addr !=
404                     saidx->proxy.sin.sin_addr.s_addr) ||
405                     (saidx->proxy.sa.sa_family != AF_INET &&
406                         saidx->proxy.sa.sa_family != 0)) {
407
408                         DPRINTF(("%s: inner source address %s doesn't "
409                             "correspond to expected proxy source %s, "
410                             "SA %s/%08lx\n", __func__,
411                             inet_ntoa4(ipn.ip_src),
412                             ipsp_address(saidx->proxy),
413                             ipsp_address(saidx->dst),
414                             (u_long) ntohl(sav->spi)));
415
416                         IPSEC_ISTAT(sproto, pdrops);
417                         error = EACCES;
418                         goto bad;
419                 }
420 #endif /* notyet */
421         }
422 #ifdef INET6
423         /* IPv6-in-IP encapsulation. */
424         else if (prot == IPPROTO_IPV6 &&
425             saidx->mode != IPSEC_MODE_TRANSPORT) {
426
427                 if (m->m_pkthdr.len - skip < sizeof(struct ip6_hdr)) {
428                         IPSEC_ISTAT(sproto, hdrops);
429                         error = EINVAL;
430                         goto bad;
431                 }
432                 /* enc0: strip IPv4 header, keep IPv6 header only */
433                 m_striphdr(m, 0, ip->ip_hl << 2);
434 #ifdef notyet 
435                 /*
436                  * Check that the inner source address is the same as
437                  * the proxy address, if available.
438                  */
439                 if ((saidx->proxy.sa.sa_family == AF_INET6 &&
440                     !IN6_IS_ADDR_UNSPECIFIED(&saidx->proxy.sin6.sin6_addr) &&
441                     !IN6_ARE_ADDR_EQUAL(&ip6n.ip6_src,
442                         &saidx->proxy.sin6.sin6_addr)) ||
443                     (saidx->proxy.sa.sa_family != AF_INET6 &&
444                         saidx->proxy.sa.sa_family != 0)) {
445
446                         DPRINTF(("%s: inner source address %s doesn't "
447                             "correspond to expected proxy source %s, "
448                             "SA %s/%08lx\n", __func__,
449                             ip6_sprintf(ip6buf, &ip6n.ip6_src),
450                             ipsec_address(&saidx->proxy),
451                             ipsec_address(&saidx->dst),
452                             (u_long) ntohl(sav->spi)));
453
454                         IPSEC_ISTAT(sproto, pdrops);
455                         error = EACCES;
456                         goto bad;
457                 }
458 #endif /* notyet */
459         }
460 #endif /* INET6 */
461         else if (prot != IPPROTO_IPV6 && saidx->mode == IPSEC_MODE_ANY) {
462                 /*
463                  * When mode is wildcard, inner protocol is IPv6 and
464                  * we have no INET6 support - drop this packet a bit later.
465                  * In other cases we assume transport mode and outer
466                  * header was already stripped in xform_xxx_cb.
467                  */
468                 prot = IPPROTO_IPIP;
469         }
470
471         /*
472          * Record what we've done to the packet (under what SA it was
473          * processed).
474          */
475         if (sproto != IPPROTO_IPCOMP) {
476                 mtag = m_tag_get(PACKET_TAG_IPSEC_IN_DONE,
477                     sizeof(struct tdb_ident), M_NOWAIT);
478                 if (mtag == NULL) {
479                         DPRINTF(("%s: failed to get tag\n", __func__));
480                         IPSEC_ISTAT(sproto, hdrops);
481                         error = ENOMEM;
482                         goto bad;
483                 }
484
485                 tdbi = (struct tdb_ident *)(mtag + 1);
486                 bcopy(&saidx->dst, &tdbi->dst, saidx->dst.sa.sa_len);
487                 tdbi->proto = sproto;
488                 tdbi->spi = sav->spi;
489                 /* Cache those two for enc(4) in xform_ipip. */
490                 tdbi->alg_auth = sav->alg_auth;
491                 tdbi->alg_enc = sav->alg_enc;
492
493                 m_tag_prepend(m, mtag);
494         }
495
496         key_sa_recordxfer(sav, m);              /* record data transfer */
497
498         /*
499          * In transport mode requeue decrypted mbuf back to IPv4 protocol
500          * handler. This is necessary to correctly expose rcvif.
501          */
502         if (saidx->mode == IPSEC_MODE_TRANSPORT)
503                 prot = IPPROTO_IPIP;
504 #ifdef DEV_ENC
505         /*
506          * Pass the mbuf to enc0 for bpf and pfil.
507          */
508         if (prot == IPPROTO_IPIP)
509                 ipsec_bpf(m, sav, AF_INET, ENC_IN|ENC_AFTER);
510 #ifdef INET6
511         if (prot == IPPROTO_IPV6)
512                 ipsec_bpf(m, sav, AF_INET6, ENC_IN|ENC_AFTER);
513 #endif
514
515         if ((error = ipsec_filter(&m, PFIL_IN, ENC_IN|ENC_AFTER)) != 0)
516                 return (error);
517 #endif /* DEV_ENC */
518
519         /*
520          * Re-dispatch via software interrupt.
521          */
522
523         switch (prot) {
524         case IPPROTO_IPIP:
525                 isr_prot = NETISR_IP;
526                 break;
527 #ifdef INET6
528         case IPPROTO_IPV6:
529                 isr_prot = NETISR_IPV6;
530                 break;
531 #endif
532         default:
533                 DPRINTF(("%s: cannot handle inner ip proto %d\n",
534                             __func__, prot));
535                 IPSEC_ISTAT(sproto, nopf);
536                 error = EPFNOSUPPORT;
537                 goto bad;
538         }
539
540         error = netisr_queue_src(isr_prot, (uintptr_t)sav->spi, m);
541         if (error) {
542                 IPSEC_ISTAT(sproto, qfull);
543                 DPRINTF(("%s: queue full; proto %u packet dropped\n",
544                         __func__, sproto));
545                 return error;
546         }
547         return 0;
548 bad:
549         m_freem(m);
550         return error;
551 }
552
553 void
554 ipsec4_common_ctlinput(int cmd, struct sockaddr *sa, void *v, int proto)
555 {
556         /* XXX nothing just yet */
557 }
558 #endif /* INET */
559
560 #ifdef INET6
561 /* IPv6 AH wrapper. */
562 int
563 ipsec6_common_input(struct mbuf **mp, int *offp, int proto)
564 {
565         int l = 0;
566         int protoff;
567         struct ip6_ext ip6e;
568
569         if (*offp < sizeof(struct ip6_hdr)) {
570                 DPRINTF(("%s: bad offset %u\n", __func__, *offp));
571                 return IPPROTO_DONE;
572         } else if (*offp == sizeof(struct ip6_hdr)) {
573                 protoff = offsetof(struct ip6_hdr, ip6_nxt);
574         } else {
575                 /* Chase down the header chain... */
576                 protoff = sizeof(struct ip6_hdr);
577
578                 do {
579                         protoff += l;
580                         m_copydata(*mp, protoff, sizeof(ip6e),
581                             (caddr_t) &ip6e);
582
583                         if (ip6e.ip6e_nxt == IPPROTO_AH)
584                                 l = (ip6e.ip6e_len + 2) << 2;
585                         else
586                                 l = (ip6e.ip6e_len + 1) << 3;
587                         IPSEC_ASSERT(l > 0, ("l went zero or negative"));
588                 } while (protoff + l < *offp);
589
590                 /* Malformed packet check */
591                 if (protoff + l != *offp) {
592                         DPRINTF(("%s: bad packet header chain, protoff %u, "
593                                 "l %u, off %u\n", __func__, protoff, l, *offp));
594                         IPSEC_ISTAT(proto, hdrops);
595                         m_freem(*mp);
596                         *mp = NULL;
597                         return IPPROTO_DONE;
598                 }
599                 protoff += offsetof(struct ip6_ext, ip6e_nxt);
600         }
601         (void) ipsec_common_input(*mp, *offp, protoff, AF_INET6, proto);
602         return IPPROTO_DONE;
603 }
604
605 /*
606  * IPsec input callback, called by the transform callback. Takes care of
607  * filtering and other sanity checks on the processed packet.
608  */
609 int
610 ipsec6_common_input_cb(struct mbuf *m, struct secasvar *sav, int skip,
611     int protoff)
612 {
613         char buf[INET6_ADDRSTRLEN];
614         int prot, af, sproto;
615         struct ip6_hdr *ip6;
616         struct m_tag *mtag;
617         struct tdb_ident *tdbi;
618         struct secasindex *saidx;
619         int nxt, isr_prot;
620         u_int8_t nxt8;
621         int error, nest;
622 #ifdef notyet
623         char ip6buf[INET6_ADDRSTRLEN];
624 #endif
625
626         IPSEC_ASSERT(m != NULL, ("null mbuf"));
627         IPSEC_ASSERT(sav != NULL, ("null SA"));
628         IPSEC_ASSERT(sav->sah != NULL, ("null SAH"));
629         saidx = &sav->sah->saidx;
630         af = saidx->dst.sa.sa_family;
631         IPSEC_ASSERT(af == AF_INET6, ("unexpected af %u", af));
632         sproto = saidx->proto;
633         IPSEC_ASSERT(sproto == IPPROTO_ESP || sproto == IPPROTO_AH ||
634                 sproto == IPPROTO_IPCOMP,
635                 ("unexpected security protocol %u", sproto));
636
637         /* Sanity check */
638         if (m == NULL) {
639                 DPRINTF(("%s: null mbuf", __func__));
640                 IPSEC_ISTAT(sproto, badkcr);
641                 error = EINVAL;
642                 goto bad;
643         }
644
645         /* Fix IPv6 header */
646         if (m->m_len < sizeof(struct ip6_hdr) &&
647             (m = m_pullup(m, sizeof(struct ip6_hdr))) == NULL) {
648
649                 DPRINTF(("%s: processing failed for SA %s/%08lx\n",
650                     __func__, ipsec_address(&sav->sah->saidx.dst, buf,
651                     sizeof(buf)), (u_long) ntohl(sav->spi)));
652
653                 IPSEC_ISTAT(sproto, hdrops);
654                 error = EACCES;
655                 goto bad;
656         }
657
658         ip6 = mtod(m, struct ip6_hdr *);
659         ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(struct ip6_hdr));
660
661         /* Save protocol */
662         m_copydata(m, protoff, 1, &nxt8);
663         prot = nxt8;
664
665 #ifdef DEV_ENC
666         if_inc_counter(encif, IFCOUNTER_IPACKETS, 1);
667         if_inc_counter(encif, IFCOUNTER_IBYTES, m->m_pkthdr.len);
668
669         /* Pass the mbuf to enc0 for bpf and pfil. */
670         ipsec_bpf(m, sav, AF_INET6, ENC_IN|ENC_BEFORE);
671         if ((error = ipsec_filter(&m, PFIL_IN, ENC_IN|ENC_BEFORE)) != 0)
672                 return (error);
673 #endif /* DEV_ENC */
674
675         /* IPv6-in-IP encapsulation */
676         if (prot == IPPROTO_IPV6 &&
677             saidx->mode != IPSEC_MODE_TRANSPORT) {
678                 if (m->m_pkthdr.len - skip < sizeof(struct ip6_hdr)) {
679                         IPSEC_ISTAT(sproto, hdrops);
680                         error = EINVAL;
681                         goto bad;
682                 }
683                 /* ip6n will now contain the inner IPv6 header. */
684                 m_striphdr(m, 0, skip);
685                 skip = 0;
686 #ifdef notyet
687                 /*
688                  * Check that the inner source address is the same as
689                  * the proxy address, if available.
690                  */
691                 if ((saidx->proxy.sa.sa_family == AF_INET6 &&
692                     !IN6_IS_ADDR_UNSPECIFIED(&saidx->proxy.sin6.sin6_addr) &&
693                     !IN6_ARE_ADDR_EQUAL(&ip6n.ip6_src,
694                         &saidx->proxy.sin6.sin6_addr)) ||
695                     (saidx->proxy.sa.sa_family != AF_INET6 &&
696                         saidx->proxy.sa.sa_family != 0)) {
697
698                         DPRINTF(("%s: inner source address %s doesn't "
699                             "correspond to expected proxy source %s, "
700                             "SA %s/%08lx\n", __func__,
701                             ip6_sprintf(ip6buf, &ip6n.ip6_src),
702                             ipsec_address(&saidx->proxy),
703                             ipsec_address(&saidx->dst),
704                             (u_long) ntohl(sav->spi)));
705
706                         IPSEC_ISTAT(sproto, pdrops);
707                         error = EACCES;
708                         goto bad;
709                 }
710 #endif /* notyet */
711         }
712 #ifdef INET
713         /* IP-in-IP encapsulation */
714         else if (prot == IPPROTO_IPIP &&
715             saidx->mode != IPSEC_MODE_TRANSPORT) {
716                 if (m->m_pkthdr.len - skip < sizeof(struct ip)) {
717                         IPSEC_ISTAT(sproto, hdrops);
718                         error = EINVAL;
719                         goto bad;
720                 }
721                 /* ipn will now contain the inner IPv4 header */
722                 m_striphdr(m, 0, skip);
723                 skip = 0;
724 #ifdef notyet
725                 /*
726                  * Check that the inner source address is the same as
727                  * the proxy address, if available.
728                  */
729                 if ((saidx->proxy.sa.sa_family == AF_INET &&
730                     saidx->proxy.sin.sin_addr.s_addr != INADDR_ANY &&
731                     ipn.ip_src.s_addr != saidx->proxy.sin.sin_addr.s_addr) ||
732                     (saidx->proxy.sa.sa_family != AF_INET &&
733                         saidx->proxy.sa.sa_family != 0)) {
734
735                         DPRINTF(("%s: inner source address %s doesn't "
736                             "correspond to expected proxy source %s, "
737                             "SA %s/%08lx\n", __func__,
738                             inet_ntoa4(ipn.ip_src),
739                             ipsec_address(&saidx->proxy),
740                             ipsec_address(&saidx->dst),
741                             (u_long) ntohl(sav->spi)));
742
743                         IPSEC_ISTAT(sproto, pdrops);
744                         error = EACCES;
745                         goto bad;
746                 }
747 #endif /* notyet */
748         }
749 #endif /* INET */
750         else {
751                 prot = IPPROTO_IPV6; /* for correct BPF processing */
752         }
753
754         /*
755          * Record what we've done to the packet (under what SA it was
756          * processed).
757          */
758         if (sproto != IPPROTO_IPCOMP) {
759                 mtag = m_tag_get(PACKET_TAG_IPSEC_IN_DONE,
760                     sizeof(struct tdb_ident), M_NOWAIT);
761                 if (mtag == NULL) {
762                         DPRINTF(("%s: failed to get tag\n", __func__));
763                         IPSEC_ISTAT(sproto, hdrops);
764                         error = ENOMEM;
765                         goto bad;
766                 }
767
768                 tdbi = (struct tdb_ident *)(mtag + 1);
769                 bcopy(&saidx->dst, &tdbi->dst, sizeof(union sockaddr_union));
770                 tdbi->proto = sproto;
771                 tdbi->spi = sav->spi;
772                 /* Cache those two for enc(4) in xform_ipip. */
773                 tdbi->alg_auth = sav->alg_auth;
774                 tdbi->alg_enc = sav->alg_enc;
775
776                 m_tag_prepend(m, mtag);
777         }
778
779         key_sa_recordxfer(sav, m);
780
781 #ifdef DEV_ENC
782         /*
783          * Pass the mbuf to enc0 for bpf and pfil.
784          */
785 #ifdef INET
786         if (prot == IPPROTO_IPIP)
787                 ipsec_bpf(m, sav, AF_INET, ENC_IN|ENC_AFTER);
788 #endif
789         if (prot == IPPROTO_IPV6)
790                 ipsec_bpf(m, sav, AF_INET6, ENC_IN|ENC_AFTER);
791
792         if ((error = ipsec_filter(&m, PFIL_IN, ENC_IN|ENC_AFTER)) != 0)
793                 return (error);
794 #endif /* DEV_ENC */
795         if (skip == 0) {
796                 /*
797                  * We stripped outer IPv6 header.
798                  * Now we should requeue decrypted packet via netisr.
799                  */
800                 switch (prot) {
801 #ifdef INET
802                 case IPPROTO_IPIP:
803                         isr_prot = NETISR_IP;
804                         break;
805 #endif
806                 case IPPROTO_IPV6:
807                         isr_prot = NETISR_IPV6;
808                         break;
809                 default:
810                         DPRINTF(("%s: cannot handle inner ip proto %d\n",
811                             __func__, prot));
812                         IPSEC_ISTAT(sproto, nopf);
813                         error = EPFNOSUPPORT;
814                         goto bad;
815                 }
816                 error = netisr_queue_src(isr_prot, (uintptr_t)sav->spi, m);
817                 if (error) {
818                         IPSEC_ISTAT(sproto, qfull);
819                         DPRINTF(("%s: queue full; proto %u packet dropped\n",
820                             __func__, sproto));
821                 }
822                 return (error);
823         }
824         /*
825          * See the end of ip6_input for this logic.
826          * IPPROTO_IPV[46] case will be processed just like other ones
827          */
828         nest = 0;
829         nxt = nxt8;
830         while (nxt != IPPROTO_DONE) {
831                 if (V_ip6_hdrnestlimit && (++nest > V_ip6_hdrnestlimit)) {
832                         IP6STAT_INC(ip6s_toomanyhdr);
833                         error = EINVAL;
834                         goto bad;
835                 }
836
837                 /*
838                  * Protection against faulty packet - there should be
839                  * more sanity checks in header chain processing.
840                  */
841                 if (m->m_pkthdr.len < skip) {
842                         IP6STAT_INC(ip6s_tooshort);
843                         in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_truncated);
844                         error = EINVAL;
845                         goto bad;
846                 }
847                 /*
848                  * Enforce IPsec policy checking if we are seeing last header.
849                  * note that we do not visit this with protocols with pcb layer
850                  * code - like udp/tcp/raw ip.
851                  */
852                 if ((inet6sw[ip6_protox[nxt]].pr_flags & PR_LASTHDR) != 0 &&
853                     ipsec6_in_reject(m, NULL)) {
854                         error = EINVAL;
855                         goto bad;
856                 }
857                 nxt = (*inet6sw[ip6_protox[nxt]].pr_input)(&m, &skip, nxt);
858         }
859         return 0;
860 bad:
861         if (m)
862                 m_freem(m);
863         return error;
864 }
865
866 void
867 esp6_ctlinput(int cmd, struct sockaddr *sa, void *d)
868 {
869         struct ip6ctlparam *ip6cp = NULL;
870         struct mbuf *m = NULL;
871         struct ip6_hdr *ip6;
872         int off;
873
874         if (sa->sa_family != AF_INET6 ||
875             sa->sa_len != sizeof(struct sockaddr_in6))
876                 return;
877         if ((unsigned)cmd >= PRC_NCMDS)
878                 return;
879
880         /* if the parameter is from icmp6, decode it. */
881         if (d != NULL) {
882                 ip6cp = (struct ip6ctlparam *)d;
883                 m = ip6cp->ip6c_m;
884                 ip6 = ip6cp->ip6c_ip6;
885                 off = ip6cp->ip6c_off;
886         } else {
887                 m = NULL;
888                 ip6 = NULL;
889                 off = 0;        /* calm gcc */
890         }
891
892         if (ip6 != NULL) {
893
894                 struct ip6ctlparam ip6cp1;
895
896                 /*
897                  * Notify the error to all possible sockets via pfctlinput2.
898                  * Since the upper layer information (such as protocol type,
899                  * source and destination ports) is embedded in the encrypted
900                  * data and might have been cut, we can't directly call
901                  * an upper layer ctlinput function. However, the pcbnotify
902                  * function will consider source and destination addresses
903                  * as well as the flow info value, and may be able to find
904                  * some PCB that should be notified.
905                  * Although pfctlinput2 will call esp6_ctlinput(), there is
906                  * no possibility of an infinite loop of function calls,
907                  * because we don't pass the inner IPv6 header.
908                  */
909                 bzero(&ip6cp1, sizeof(ip6cp1));
910                 ip6cp1.ip6c_src = ip6cp->ip6c_src;
911                 pfctlinput2(cmd, sa, (void *)&ip6cp1);
912
913                 /*
914                  * Then go to special cases that need ESP header information.
915                  * XXX: We assume that when ip6 is non NULL,
916                  * M and OFF are valid.
917                  */
918
919                 if (cmd == PRC_MSGSIZE) {
920                         struct secasvar *sav;
921                         u_int32_t spi;
922                         int valid;
923
924                         /* check header length before using m_copydata */
925                         if (m->m_pkthdr.len < off + sizeof (struct esp))
926                                 return;
927                         m_copydata(m, off + offsetof(struct esp, esp_spi),
928                                 sizeof(u_int32_t), (caddr_t) &spi);
929                         /*
930                          * Check to see if we have a valid SA corresponding to
931                          * the address in the ICMP message payload.
932                          */
933                         sav = KEY_ALLOCSA((union sockaddr_union *)sa,
934                                         IPPROTO_ESP, spi);
935                         valid = (sav != NULL);
936                         if (sav)
937                                 KEY_FREESAV(&sav);
938
939                         /* XXX Further validation? */
940
941                         /*
942                          * Depending on whether the SA is "valid" and
943                          * routing table size (mtudisc_{hi,lo}wat), we will:
944                          * - recalcurate the new MTU and create the
945                          *   corresponding routing entry, or
946                          * - ignore the MTU change notification.
947                          */
948                         icmp6_mtudisc_update(ip6cp, valid);
949                 }
950         } else {
951                 /* we normally notify any pcb here */
952         }
953 }
954 #endif /* INET6 */