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