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