+ IPSEC_STAT_INCREMENT(ipsecstat.in_inval);
+ goto bad;
+ }
+
+ if (ip_doscopedroute) {
+ bzero(&addr, sizeof(addr));
+ ipaddr = (__typeof__(ipaddr))&addr;
+ ipaddr->sin_family = AF_INET;
+ ipaddr->sin_len = sizeof(*ipaddr);
+ ipaddr->sin_addr = ip->ip_dst;
+ }
+#if INET6
+ } else if (ifamily == AF_INET6) {
+ struct sockaddr_in6 *ip6addr;
+
+ /*
+ * m_pullup is prohibited in KAME IPv6 input processing
+ * but there's no other way!
+ */
+ if (m->m_len < sizeof(*ip6)) {
+ m = m_pullup(m, sizeof(*ip6));
+ if (!m) {
+ IPSEC_STAT_INCREMENT(ipsecstat.in_inval);
+ goto bad;
+ }
+ }
+
+ /*
+ * Expect 32-bit aligned data pointer on strict-align
+ * platforms.
+ */
+ MBUF_STRICT_DATA_ALIGNMENT_CHECK_32(m);
+
+ ip6 = mtod(m, struct ip6_hdr *);
+
+ /* ECN consideration. */
+ /* XXX To be fixed later if needed */
+ // ip_ecn_egress(ip4_ipsec_ecn, &tos, &ip->ip_tos);
+
+ if (!key_checktunnelsanity(sav, AF_INET6,
+ (caddr_t)&ip6->ip6_src, (caddr_t)&ip6->ip6_dst)) {
+ ipseclog((LOG_ERR, "ipsec tunnel address mismatch "
+ "in ESP input: %s %s\n",
+ ipsec6_logpacketstr(ip6, spi), ipsec_logsastr(sav)));
+ IPSEC_STAT_INCREMENT(ipsecstat.in_inval);
+ goto bad;
+ }
+
+ if (ip6_doscopedroute) {
+ bzero(&addr, sizeof(addr));
+ ip6addr = (__typeof__(ip6addr))&addr;
+ ip6addr->sin6_family = AF_INET6;
+ ip6addr->sin6_len = sizeof(*ip6addr);
+ ip6addr->sin6_addr = ip6->ip6_dst;
+ }
+#endif /* INET6 */
+ } else {
+ ipseclog((LOG_ERR, "ipsec tunnel unsupported address family "
+ "in ESP input\n"));