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