1 1.82 knakahar /* $NetBSD: ipsec_input.c,v 1.82 2025/08/12 04:45:48 knakahara Exp $ */ 2 1.66 maxv /* $FreeBSD: ipsec_input.c,v 1.2.4.2 2003/03/28 20:32:53 sam Exp $ */ 3 1.7 thorpej /* $OpenBSD: ipsec_input.c,v 1.63 2003/02/20 18:35:43 deraadt Exp $ */ 4 1.7 thorpej 5 1.7 thorpej /* 6 1.7 thorpej * The authors of this code are John Ioannidis (ji (at) tla.org), 7 1.7 thorpej * Angelos D. Keromytis (kermit (at) csd.uch.gr) and 8 1.7 thorpej * Niels Provos (provos (at) physnet.uni-hamburg.de). 9 1.7 thorpej * 10 1.7 thorpej * This code was written by John Ioannidis for BSD/OS in Athens, Greece, 11 1.7 thorpej * in November 1995. 12 1.7 thorpej * 13 1.7 thorpej * Ported to OpenBSD and NetBSD, with additional transforms, in December 1996, 14 1.7 thorpej * by Angelos D. Keromytis. 15 1.7 thorpej * 16 1.7 thorpej * Additional transforms and features in 1997 and 1998 by Angelos D. Keromytis 17 1.7 thorpej * and Niels Provos. 18 1.7 thorpej * 19 1.7 thorpej * Additional features in 1999 by Angelos D. Keromytis. 20 1.7 thorpej * 21 1.7 thorpej * Copyright (C) 1995, 1996, 1997, 1998, 1999 by John Ioannidis, 22 1.7 thorpej * Angelos D. Keromytis and Niels Provos. 23 1.7 thorpej * Copyright (c) 2001, Angelos D. Keromytis. 24 1.7 thorpej * 25 1.7 thorpej * Permission to use, copy, and modify this software with or without fee 26 1.7 thorpej * is hereby granted, provided that this entire notice is included in 27 1.7 thorpej * all copies of any software which is or includes a copy or 28 1.7 thorpej * modification of this software. 29 1.7 thorpej * You may use this code under the GNU public license if you so wish. Please 30 1.7 thorpej * contribute changes back to the authors under this freer than GPL license 31 1.7 thorpej * so that we may further the use of strong encryption without limitations to 32 1.7 thorpej * all. 33 1.7 thorpej * 34 1.7 thorpej * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR 35 1.7 thorpej * IMPLIED WARRANTY. IN PARTICULAR, NONE OF THE AUTHORS MAKES ANY 36 1.7 thorpej * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE 37 1.7 thorpej * MERCHANTABILITY OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR 38 1.7 thorpej * PURPOSE. 39 1.7 thorpej */ 40 1.1 jonathan 41 1.1 jonathan #include <sys/cdefs.h> 42 1.82 knakahar __KERNEL_RCSID(0, "$NetBSD: ipsec_input.c,v 1.82 2025/08/12 04:45:48 knakahara Exp $"); 43 1.1 jonathan 44 1.1 jonathan /* 45 1.1 jonathan * IPsec input processing. 46 1.1 jonathan */ 47 1.1 jonathan 48 1.38 ozaki #if defined(_KERNEL_OPT) 49 1.1 jonathan #include "opt_inet.h" 50 1.38 ozaki #endif 51 1.1 jonathan 52 1.1 jonathan #include <sys/param.h> 53 1.1 jonathan #include <sys/systm.h> 54 1.1 jonathan #include <sys/mbuf.h> 55 1.1 jonathan #include <sys/domain.h> 56 1.1 jonathan #include <sys/protosw.h> 57 1.1 jonathan #include <sys/socket.h> 58 1.1 jonathan #include <sys/errno.h> 59 1.1 jonathan #include <sys/syslog.h> 60 1.1 jonathan 61 1.1 jonathan #include <net/if.h> 62 1.1 jonathan #include <net/route.h> 63 1.1 jonathan 64 1.1 jonathan #include <netinet/in.h> 65 1.1 jonathan #include <netinet/in_systm.h> 66 1.1 jonathan #include <netinet/ip.h> 67 1.1 jonathan #include <netinet/ip_var.h> 68 1.1 jonathan #include <netinet/in_var.h> 69 1.29 drochner #include <netinet/in_proto.h> 70 1.44 christos #include <netinet/udp.h> 71 1.44 christos #include <netinet/tcp.h> 72 1.1 jonathan 73 1.1 jonathan #include <netinet/ip6.h> 74 1.1 jonathan #ifdef INET6 75 1.69 maxv #include <netinet6/in6.h> 76 1.1 jonathan #include <netinet6/ip6_var.h> 77 1.19 thorpej #include <netinet6/ip6_private.h> 78 1.26 drochner #include <netinet6/scope6_var.h> 79 1.1 jonathan #endif 80 1.1 jonathan #include <netinet/in_pcb.h> 81 1.1 jonathan 82 1.1 jonathan #include <netipsec/ipsec.h> 83 1.20 thorpej #include <netipsec/ipsec_private.h> 84 1.1 jonathan #ifdef INET6 85 1.1 jonathan #include <netipsec/ipsec6.h> 86 1.1 jonathan #endif 87 1.1 jonathan #include <netipsec/ah_var.h> 88 1.1 jonathan #include <netipsec/esp.h> 89 1.1 jonathan #include <netipsec/esp_var.h> 90 1.1 jonathan #include <netipsec/ipcomp_var.h> 91 1.1 jonathan 92 1.6 tls #include <netipsec/key.h> 93 1.6 tls #include <netipsec/keydb.h> 94 1.1 jonathan 95 1.1 jonathan #include <netipsec/xform.h> 96 1.1 jonathan #include <netinet6/ip6protosw.h> 97 1.1 jonathan 98 1.20 thorpej #define IPSEC_ISTAT(p, x, y, z) \ 99 1.20 thorpej do { \ 100 1.20 thorpej switch (p) { \ 101 1.20 thorpej case IPPROTO_ESP: \ 102 1.20 thorpej ESP_STATINC(x); \ 103 1.20 thorpej break; \ 104 1.20 thorpej case IPPROTO_AH: \ 105 1.20 thorpej AH_STATINC(y); \ 106 1.20 thorpej break; \ 107 1.20 thorpej default: \ 108 1.20 thorpej IPCOMP_STATINC(z); \ 109 1.20 thorpej break; \ 110 1.20 thorpej } \ 111 1.20 thorpej } while (/*CONSTCOND*/0) 112 1.1 jonathan 113 1.1 jonathan /* 114 1.44 christos * fixup TCP/UDP checksum 115 1.44 christos * 116 1.44 christos * XXX: if we have NAT-OA payload from IKE server, 117 1.44 christos * we must do the differential update of checksum. 118 1.44 christos * 119 1.77 andvar * XXX: NAT-OAi/NAT-OAr derived from IKE initiator/responder. 120 1.44 christos * how to know the IKE side from kernel? 121 1.44 christos */ 122 1.44 christos static struct mbuf * 123 1.44 christos ipsec4_fixup_checksum(struct mbuf *m) 124 1.44 christos { 125 1.52 ozaki struct ip *ip; 126 1.52 ozaki struct tcphdr *th; 127 1.52 ozaki struct udphdr *uh; 128 1.52 ozaki int poff, off; 129 1.52 ozaki int plen; 130 1.52 ozaki 131 1.53 ozaki if (m->m_len < sizeof(*ip)) { 132 1.52 ozaki m = m_pullup(m, sizeof(*ip)); 133 1.53 ozaki if (m == NULL) 134 1.53 ozaki return NULL; 135 1.53 ozaki } 136 1.59 maxv ip = mtod(m, struct ip *); 137 1.52 ozaki poff = ip->ip_hl << 2; 138 1.52 ozaki plen = ntohs(ip->ip_len) - poff; 139 1.52 ozaki 140 1.52 ozaki switch (ip->ip_p) { 141 1.52 ozaki case IPPROTO_TCP: 142 1.70 maxv M_REGION_GET(th, struct tcphdr *, m, poff, sizeof(*th)); 143 1.52 ozaki if (th == NULL) 144 1.52 ozaki return NULL; 145 1.52 ozaki off = th->th_off << 2; 146 1.52 ozaki if (off < sizeof(*th) || off > plen) { 147 1.52 ozaki m_freem(m); 148 1.52 ozaki return NULL; 149 1.52 ozaki } 150 1.52 ozaki th->th_sum = 0; 151 1.52 ozaki th->th_sum = in4_cksum(m, IPPROTO_TCP, poff, plen); 152 1.52 ozaki break; 153 1.52 ozaki case IPPROTO_UDP: 154 1.70 maxv M_REGION_GET(uh, struct udphdr *, m, poff, sizeof(*uh)); 155 1.52 ozaki if (uh == NULL) 156 1.52 ozaki return NULL; 157 1.59 maxv off = sizeof(*uh); 158 1.59 maxv if (off > plen) { 159 1.52 ozaki m_freem(m); 160 1.52 ozaki return NULL; 161 1.52 ozaki } 162 1.52 ozaki uh->uh_sum = 0; 163 1.52 ozaki uh->uh_sum = in4_cksum(m, IPPROTO_UDP, poff, plen); 164 1.52 ozaki break; 165 1.52 ozaki default: 166 1.59 maxv /* no checksum */ 167 1.52 ozaki return m; 168 1.52 ozaki } 169 1.44 christos 170 1.52 ozaki return m; 171 1.44 christos } 172 1.44 christos 173 1.72 maxv static void 174 1.72 maxv nat_t_ports_get(struct mbuf *m, uint16_t *dport, uint16_t *sport) 175 1.72 maxv { 176 1.72 maxv struct m_tag *tag; 177 1.72 maxv 178 1.73 maxv if ((tag = m_tag_find(m, PACKET_TAG_IPSEC_NAT_T_PORTS))) { 179 1.72 maxv *sport = ((uint16_t *)(tag + 1))[0]; 180 1.72 maxv *dport = ((uint16_t *)(tag + 1))[1]; 181 1.72 maxv } else 182 1.72 maxv *sport = *dport = 0; 183 1.72 maxv } 184 1.72 maxv 185 1.76 christos static uint32_t 186 1.76 christos spi_get(struct mbuf *m, int sproto, int skip) 187 1.76 christos { 188 1.76 christos uint32_t spi; 189 1.76 christos uint16_t cpi; 190 1.76 christos 191 1.76 christos switch (sproto) { 192 1.76 christos case IPPROTO_ESP: 193 1.76 christos m_copydata(m, skip, sizeof(spi), &spi); 194 1.76 christos return spi; 195 1.76 christos case IPPROTO_AH: 196 1.76 christos m_copydata(m, skip + sizeof(spi), sizeof(spi), &spi); 197 1.76 christos return spi; 198 1.76 christos case IPPROTO_IPCOMP: 199 1.76 christos m_copydata(m, skip + sizeof(cpi), sizeof(cpi), &cpi); 200 1.76 christos return htonl(ntohs(cpi)); 201 1.76 christos default: 202 1.76 christos panic("%s called with bad protocol number: %d\n", __func__, 203 1.76 christos sproto); 204 1.76 christos } 205 1.76 christos } 206 1.76 christos 207 1.76 christos 208 1.44 christos /* 209 1.1 jonathan * ipsec_common_input gets called when an IPsec-protected packet 210 1.64 maxv * is received by IPv4 or IPv6. Its job is to find the right SA 211 1.64 maxv * and call the appropriate transform. The transform callback 212 1.1 jonathan * takes care of further processing (like ingress filtering). 213 1.1 jonathan */ 214 1.1 jonathan static int 215 1.1 jonathan ipsec_common_input(struct mbuf *m, int skip, int protoff, int af, int sproto) 216 1.1 jonathan { 217 1.78 knakahar char buf[IPSEC_ADDRSTRLEN], buf2[IPSEC_ADDRSTRLEN]; 218 1.78 knakahar union sockaddr_union src_address, dst_address; 219 1.1 jonathan struct secasvar *sav; 220 1.1 jonathan u_int32_t spi; 221 1.30 christos u_int16_t sport; 222 1.30 christos u_int16_t dport; 223 1.79 ozaki int error; 224 1.1 jonathan 225 1.20 thorpej IPSEC_ISTAT(sproto, ESP_STAT_INPUT, AH_STAT_INPUT, 226 1.20 thorpej IPCOMP_STAT_INPUT); 227 1.1 jonathan 228 1.40 ozaki KASSERT(m != NULL); 229 1.1 jonathan 230 1.1 jonathan if ((sproto == IPPROTO_ESP && !esp_enable) || 231 1.1 jonathan (sproto == IPPROTO_AH && !ah_enable) || 232 1.1 jonathan (sproto == IPPROTO_IPCOMP && !ipcomp_enable)) { 233 1.1 jonathan m_freem(m); 234 1.20 thorpej IPSEC_ISTAT(sproto, ESP_STAT_PDROPS, AH_STAT_PDROPS, 235 1.20 thorpej IPCOMP_STAT_PDROPS); 236 1.1 jonathan return EOPNOTSUPP; 237 1.1 jonathan } 238 1.1 jonathan 239 1.59 maxv if (m->m_pkthdr.len - skip < 2 * sizeof(u_int32_t)) { 240 1.1 jonathan m_freem(m); 241 1.20 thorpej IPSEC_ISTAT(sproto, ESP_STAT_HDROPS, AH_STAT_HDROPS, 242 1.20 thorpej IPCOMP_STAT_HDROPS); 243 1.43 ozaki IPSECLOG(LOG_DEBUG, "packet too small\n"); 244 1.1 jonathan return EINVAL; 245 1.1 jonathan } 246 1.1 jonathan 247 1.1 jonathan /* Retrieve the SPI from the relevant IPsec header */ 248 1.76 christos spi = spi_get(m, sproto, skip); 249 1.17 degroote 250 1.17 degroote /* find the source port for NAT-T */ 251 1.30 christos nat_t_ports_get(m, &dport, &sport); 252 1.1 jonathan 253 1.1 jonathan /* 254 1.1 jonathan * Find the SA and (indirectly) call the appropriate 255 1.1 jonathan * kernel crypto routine. The resulting mbuf chain is a valid 256 1.1 jonathan * IP packet ready to go through input processing. 257 1.1 jonathan */ 258 1.78 knakahar memset(&src_address, 0, sizeof (src_address)); 259 1.59 maxv memset(&dst_address, 0, sizeof(dst_address)); 260 1.78 knakahar src_address.sa.sa_family = af; 261 1.1 jonathan dst_address.sa.sa_family = af; 262 1.1 jonathan switch (af) { 263 1.1 jonathan #ifdef INET 264 1.1 jonathan case AF_INET: 265 1.78 knakahar src_address.sin.sin_len = sizeof(struct sockaddr_in); 266 1.1 jonathan dst_address.sin.sin_len = sizeof(struct sockaddr_in); 267 1.78 knakahar m_copydata(m, offsetof(struct ip, ip_src), 268 1.78 knakahar sizeof(struct in_addr), 269 1.78 knakahar &src_address.sin.sin_addr); 270 1.1 jonathan m_copydata(m, offsetof(struct ip, ip_dst), 271 1.1 jonathan sizeof(struct in_addr), 272 1.16 degroote &dst_address.sin.sin_addr); 273 1.1 jonathan break; 274 1.59 maxv #endif 275 1.1 jonathan #ifdef INET6 276 1.1 jonathan case AF_INET6: 277 1.78 knakahar src_address.sin6.sin6_len = sizeof(struct sockaddr_in6); 278 1.1 jonathan dst_address.sin6.sin6_len = sizeof(struct sockaddr_in6); 279 1.78 knakahar m_copydata(m, offsetof(struct ip6_hdr, ip6_src), 280 1.78 knakahar sizeof(struct in6_addr), 281 1.78 knakahar &src_address.sin6.sin6_addr); 282 1.1 jonathan m_copydata(m, offsetof(struct ip6_hdr, ip6_dst), 283 1.1 jonathan sizeof(struct in6_addr), 284 1.16 degroote &dst_address.sin6.sin6_addr); 285 1.26 drochner if (sa6_recoverscope(&dst_address.sin6)) { 286 1.26 drochner m_freem(m); 287 1.26 drochner return EINVAL; 288 1.26 drochner } 289 1.1 jonathan break; 290 1.59 maxv #endif 291 1.1 jonathan default: 292 1.43 ozaki IPSECLOG(LOG_DEBUG, "unsupported protocol family %u\n", af); 293 1.1 jonathan m_freem(m); 294 1.20 thorpej IPSEC_ISTAT(sproto, ESP_STAT_NOPF, AH_STAT_NOPF, 295 1.20 thorpej IPCOMP_STAT_NOPF); 296 1.1 jonathan return EPFNOSUPPORT; 297 1.1 jonathan } 298 1.1 jonathan 299 1.47 ozaki /* NB: only pass dst since key_lookup_sa follows RFC2401 */ 300 1.47 ozaki sav = KEY_LOOKUP_SA(&dst_address, sproto, spi, sport, dport); 301 1.1 jonathan if (sav == NULL) { 302 1.78 knakahar static struct timeval lasttime = {0, 0}; 303 1.78 knakahar static int curpps = 0; 304 1.78 knakahar 305 1.78 knakahar if (!ipsec_debug && ppsratecheck(&lasttime, &curpps, 1)) { 306 1.78 knakahar if (sport || dport) { 307 1.78 knakahar log(LOG_INFO, 308 1.78 knakahar "no key association found for SA" 309 1.78 knakahar " %s[%u]-%s[%u]/SPI 0x%08lx\n", 310 1.78 knakahar ipsec_address(&src_address, buf, sizeof(buf)), 311 1.78 knakahar ntohs(sport), 312 1.78 knakahar ipsec_address(&dst_address, buf2, sizeof(buf2)), 313 1.78 knakahar ntohs(dport), 314 1.78 knakahar (u_long) ntohl(spi)); 315 1.78 knakahar } else { 316 1.78 knakahar log(LOG_INFO, 317 1.78 knakahar "no key association found for" 318 1.78 knakahar " SA %s-%s/SPI 0x%08lx\n", 319 1.78 knakahar ipsec_address(&src_address, buf, sizeof(buf)), 320 1.82 knakahar ipsec_address(&dst_address, buf2, sizeof(buf2)), 321 1.78 knakahar (u_long) ntohl(spi)); 322 1.78 knakahar } 323 1.78 knakahar } else if (ipsec_debug) { 324 1.78 knakahar IPSECLOG(LOG_DEBUG, 325 1.78 knakahar "no key association found for SA " 326 1.78 knakahar "%s-%s/SPI 0x%08lx/PROTO %u/PORT %u-%u\n", 327 1.78 knakahar ipsec_address(&src_address, buf, sizeof(buf)), 328 1.78 knakahar ipsec_address(&dst_address, buf2, sizeof(buf2)), 329 1.78 knakahar (u_long) ntohl(spi), sproto, ntohs(dport), ntohs(sport)); 330 1.78 knakahar } 331 1.20 thorpej IPSEC_ISTAT(sproto, ESP_STAT_NOTDB, AH_STAT_NOTDB, 332 1.20 thorpej IPCOMP_STAT_NOTDB); 333 1.1 jonathan m_freem(m); 334 1.1 jonathan return ENOENT; 335 1.1 jonathan } 336 1.1 jonathan 337 1.46 ozaki KASSERT(sav->tdb_xform != NULL); 338 1.1 jonathan 339 1.1 jonathan /* 340 1.1 jonathan * Call appropriate transform and return -- callback takes care of 341 1.1 jonathan * everything else. 342 1.1 jonathan */ 343 1.1 jonathan error = (*sav->tdb_xform->xf_input)(m, sav, skip, protoff); 344 1.51 ozaki KEY_SA_UNREF(&sav); 345 1.1 jonathan return error; 346 1.1 jonathan } 347 1.1 jonathan 348 1.1 jonathan #ifdef INET 349 1.1 jonathan /* 350 1.1 jonathan * Common input handler for IPv4 AH, ESP, and IPCOMP. 351 1.1 jonathan */ 352 1.2 jonathan void 353 1.71 maxv ipsec4_common_input(struct mbuf *m, int off, int proto) 354 1.1 jonathan { 355 1.59 maxv (void)ipsec_common_input(m, off, offsetof(struct ip, ip_p), 356 1.71 maxv AF_INET, proto); 357 1.1 jonathan } 358 1.1 jonathan 359 1.1 jonathan /* 360 1.1 jonathan * IPsec input callback for INET protocols. 361 1.1 jonathan * This routine is called as the transform callback. 362 1.1 jonathan * Takes care of filtering and other sanity checks on 363 1.1 jonathan * the processed packet. 364 1.1 jonathan */ 365 1.1 jonathan int 366 1.1 jonathan ipsec4_common_input_cb(struct mbuf *m, struct secasvar *sav, 367 1.45 ozaki int skip, int protoff) 368 1.1 jonathan { 369 1.31 mrg int prot, af __diagused, sproto; 370 1.1 jonathan struct ip *ip; 371 1.1 jonathan struct secasindex *saidx; 372 1.1 jonathan int error; 373 1.1 jonathan 374 1.60 maxv if (__predict_false(m == NULL)) { 375 1.60 maxv panic("%s: NULL mbuf", __func__); 376 1.60 maxv } 377 1.61 maxv if (__predict_false(skip < sizeof(struct ip))) { 378 1.61 maxv panic("%s: short skip", __func__); 379 1.61 maxv } 380 1.60 maxv 381 1.40 ozaki KASSERT(sav != NULL); 382 1.1 jonathan saidx = &sav->sah->saidx; 383 1.1 jonathan af = saidx->dst.sa.sa_family; 384 1.40 ozaki KASSERTMSG(af == AF_INET, "unexpected af %u", af); 385 1.1 jonathan sproto = saidx->proto; 386 1.40 ozaki KASSERTMSG(sproto == IPPROTO_ESP || sproto == IPPROTO_AH || 387 1.40 ozaki sproto == IPPROTO_IPCOMP, 388 1.40 ozaki "unexpected security protocol %u", sproto); 389 1.1 jonathan 390 1.61 maxv /* 391 1.61 maxv * Update the IPv4 header. The length of the packet may have changed, 392 1.61 maxv * so fix it, and recompute the checksum. 393 1.61 maxv */ 394 1.61 maxv if (m->m_len < skip && (m = m_pullup(m, skip)) == NULL) { 395 1.61 maxv char buf[IPSEC_ADDRSTRLEN]; 396 1.44 christos cantpull: 397 1.61 maxv IPSECLOG(LOG_DEBUG, 398 1.61 maxv "processing failed for SA %s/%08lx\n", 399 1.61 maxv ipsec_address(&sav->sah->saidx.dst, buf, 400 1.61 maxv sizeof(buf)), (u_long) ntohl(sav->spi)); 401 1.61 maxv IPSEC_ISTAT(sproto, ESP_STAT_HDROPS, AH_STAT_HDROPS, 402 1.61 maxv IPCOMP_STAT_HDROPS); 403 1.61 maxv error = ENOBUFS; 404 1.61 maxv goto bad; 405 1.29 drochner } 406 1.61 maxv ip = mtod(m, struct ip *); 407 1.61 maxv ip->ip_len = htons(m->m_pkthdr.len); 408 1.61 maxv ip->ip_sum = 0; 409 1.61 maxv ip->ip_sum = in_cksum(m, ip->ip_hl << 2); 410 1.1 jonathan 411 1.44 christos /* 412 1.44 christos * Update TCP/UDP checksum 413 1.44 christos * XXX: should only do it in NAT-T case 414 1.44 christos * XXX: should do it incrementally, see FreeBSD code. 415 1.44 christos */ 416 1.44 christos m = ipsec4_fixup_checksum(m); 417 1.44 christos if (m == NULL) 418 1.44 christos goto cantpull; 419 1.54 maxv ip = mtod(m, struct ip *); 420 1.44 christos 421 1.1 jonathan prot = ip->ip_p; 422 1.1 jonathan 423 1.63 maxv M_VERIFY_PACKET(m); 424 1.63 maxv 425 1.69 maxv key_sa_recordxfer(sav, m); 426 1.1 jonathan 427 1.35 riastrad if ((inetsw[ip_protox[prot]].pr_flags & PR_LASTHDR) != 0 && 428 1.62 maxv ipsec_in_reject(m, NULL)) { 429 1.29 drochner error = EINVAL; 430 1.29 drochner goto bad; 431 1.1 jonathan } 432 1.74 knakahar 433 1.74 knakahar /* 434 1.80 andvar * There is no struct ifnet for tunnel mode IP-IP tunnel connection, 435 1.74 knakahar * so we cannot write filtering rule to the inner packet. 436 1.74 knakahar */ 437 1.74 knakahar if (saidx->mode == IPSEC_MODE_TUNNEL) 438 1.74 knakahar m->m_pkthdr.pkthdr_flags |= PKTHDR_FLAG_IPSEC_SKIP_PFIL; 439 1.74 knakahar 440 1.35 riastrad (*inetsw[ip_protox[prot]].pr_input)(m, skip, prot); 441 1.1 jonathan return 0; 442 1.59 maxv 443 1.1 jonathan bad: 444 1.1 jonathan m_freem(m); 445 1.1 jonathan return error; 446 1.1 jonathan } 447 1.1 jonathan #endif /* INET */ 448 1.1 jonathan 449 1.1 jonathan #ifdef INET6 450 1.1 jonathan int 451 1.1 jonathan ipsec6_common_input(struct mbuf **mp, int *offp, int proto) 452 1.1 jonathan { 453 1.1 jonathan int l = 0; 454 1.27 drochner int protoff, nxt; 455 1.1 jonathan struct ip6_ext ip6e; 456 1.1 jonathan 457 1.1 jonathan if (*offp < sizeof(struct ip6_hdr)) { 458 1.43 ozaki IPSECLOG(LOG_DEBUG, "bad offset %u\n", *offp); 459 1.25 drochner IPSEC_ISTAT(proto, ESP_STAT_HDROPS, AH_STAT_HDROPS, 460 1.25 drochner IPCOMP_STAT_HDROPS); 461 1.25 drochner m_freem(*mp); 462 1.1 jonathan return IPPROTO_DONE; 463 1.1 jonathan } else if (*offp == sizeof(struct ip6_hdr)) { 464 1.1 jonathan protoff = offsetof(struct ip6_hdr, ip6_nxt); 465 1.1 jonathan } else { 466 1.1 jonathan /* Chase down the header chain... */ 467 1.1 jonathan protoff = sizeof(struct ip6_hdr); 468 1.27 drochner nxt = (mtod(*mp, struct ip6_hdr *))->ip6_nxt; 469 1.1 jonathan 470 1.1 jonathan do { 471 1.1 jonathan protoff += l; 472 1.16 degroote m_copydata(*mp, protoff, sizeof(ip6e), &ip6e); 473 1.1 jonathan 474 1.27 drochner if (nxt == IPPROTO_AH) 475 1.1 jonathan l = (ip6e.ip6e_len + 2) << 2; 476 1.55 maxv else if (nxt == IPPROTO_FRAGMENT) 477 1.55 maxv l = sizeof(struct ip6_frag); 478 1.1 jonathan else 479 1.1 jonathan l = (ip6e.ip6e_len + 1) << 3; 480 1.40 ozaki KASSERT(l > 0); 481 1.27 drochner 482 1.27 drochner nxt = ip6e.ip6e_nxt; 483 1.1 jonathan } while (protoff + l < *offp); 484 1.1 jonathan 485 1.1 jonathan /* Malformed packet check */ 486 1.1 jonathan if (protoff + l != *offp) { 487 1.43 ozaki IPSECLOG(LOG_DEBUG, "bad packet header chain, " 488 1.43 ozaki "protoff %u, l %u, off %u\n", protoff, l, *offp); 489 1.20 thorpej IPSEC_ISTAT(proto, ESP_STAT_HDROPS, 490 1.20 thorpej AH_STAT_HDROPS, 491 1.20 thorpej IPCOMP_STAT_HDROPS); 492 1.1 jonathan m_freem(*mp); 493 1.1 jonathan *mp = NULL; 494 1.1 jonathan return IPPROTO_DONE; 495 1.1 jonathan } 496 1.1 jonathan protoff += offsetof(struct ip6_ext, ip6e_nxt); 497 1.1 jonathan } 498 1.1 jonathan (void) ipsec_common_input(*mp, *offp, protoff, AF_INET6, proto); 499 1.1 jonathan return IPPROTO_DONE; 500 1.1 jonathan } 501 1.1 jonathan 502 1.1 jonathan /* 503 1.1 jonathan * IPsec input callback, called by the transform callback. Takes care of 504 1.1 jonathan * filtering and other sanity checks on the processed packet. 505 1.1 jonathan */ 506 1.1 jonathan int 507 1.45 ozaki ipsec6_common_input_cb(struct mbuf *m, struct secasvar *sav, int skip, 508 1.45 ozaki int protoff) 509 1.1 jonathan { 510 1.32 ozaki int af __diagused, sproto; 511 1.1 jonathan struct ip6_hdr *ip6; 512 1.1 jonathan struct secasindex *saidx; 513 1.1 jonathan int nxt; 514 1.69 maxv u_int8_t prot; 515 1.1 jonathan int error, nest; 516 1.1 jonathan 517 1.60 maxv if (__predict_false(m == NULL)) { 518 1.60 maxv panic("%s: NULL mbuf", __func__); 519 1.60 maxv } 520 1.60 maxv 521 1.40 ozaki KASSERT(sav != NULL); 522 1.1 jonathan saidx = &sav->sah->saidx; 523 1.1 jonathan af = saidx->dst.sa.sa_family; 524 1.40 ozaki KASSERTMSG(af == AF_INET6, "unexpected af %u", af); 525 1.1 jonathan sproto = saidx->proto; 526 1.40 ozaki KASSERTMSG(sproto == IPPROTO_ESP || sproto == IPPROTO_AH || 527 1.40 ozaki sproto == IPPROTO_IPCOMP, 528 1.40 ozaki "unexpected security protocol %u", sproto); 529 1.1 jonathan 530 1.1 jonathan /* Fix IPv6 header */ 531 1.1 jonathan if (m->m_len < sizeof(struct ip6_hdr) && 532 1.1 jonathan (m = m_pullup(m, sizeof(struct ip6_hdr))) == NULL) { 533 1.42 ryo char buf[IPSEC_ADDRSTRLEN]; 534 1.43 ozaki IPSECLOG(LOG_DEBUG, "processing failed for SA %s/%08lx\n", 535 1.43 ozaki ipsec_address(&sav->sah->saidx.dst, 536 1.43 ozaki buf, sizeof(buf)), (u_long) ntohl(sav->spi)); 537 1.20 thorpej IPSEC_ISTAT(sproto, ESP_STAT_HDROPS, AH_STAT_HDROPS, 538 1.20 thorpej IPCOMP_STAT_HDROPS); 539 1.1 jonathan error = EACCES; 540 1.1 jonathan goto bad; 541 1.1 jonathan } 542 1.1 jonathan 543 1.1 jonathan ip6 = mtod(m, struct ip6_hdr *); 544 1.1 jonathan ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(struct ip6_hdr)); 545 1.1 jonathan 546 1.69 maxv m_copydata(m, protoff, sizeof(prot), &prot); 547 1.1 jonathan 548 1.1 jonathan key_sa_recordxfer(sav, m); 549 1.1 jonathan 550 1.1 jonathan /* 551 1.1 jonathan * See the end of ip6_input for this logic. 552 1.1 jonathan * IPPROTO_IPV[46] case will be processed just like other ones 553 1.1 jonathan */ 554 1.1 jonathan nest = 0; 555 1.69 maxv nxt = prot; 556 1.1 jonathan while (nxt != IPPROTO_DONE) { 557 1.1 jonathan if (ip6_hdrnestlimit && (++nest > ip6_hdrnestlimit)) { 558 1.19 thorpej IP6_STATINC(IP6_STAT_TOOMANYHDR); 559 1.1 jonathan error = EINVAL; 560 1.1 jonathan goto bad; 561 1.1 jonathan } 562 1.1 jonathan 563 1.63 maxv M_VERIFY_PACKET(m); 564 1.63 maxv 565 1.1 jonathan /* 566 1.1 jonathan * Protection against faulty packet - there should be 567 1.1 jonathan * more sanity checks in header chain processing. 568 1.1 jonathan */ 569 1.1 jonathan if (m->m_pkthdr.len < skip) { 570 1.19 thorpej IP6_STATINC(IP6_STAT_TOOSHORT); 571 1.36 ozaki in6_ifstat_inc(m_get_rcvif_NOMPSAFE(m), 572 1.59 maxv ifs6_in_truncated); 573 1.1 jonathan error = EINVAL; 574 1.1 jonathan goto bad; 575 1.1 jonathan } 576 1.59 maxv 577 1.1 jonathan /* 578 1.1 jonathan * Enforce IPsec policy checking if we are seeing last header. 579 1.64 maxv * Note that we do not visit this with protocols with pcb layer 580 1.1 jonathan * code - like udp/tcp/raw ip. 581 1.1 jonathan */ 582 1.1 jonathan if ((inet6sw[ip6_protox[nxt]].pr_flags & PR_LASTHDR) != 0 && 583 1.62 maxv ipsec_in_reject(m, NULL)) { 584 1.1 jonathan error = EINVAL; 585 1.1 jonathan goto bad; 586 1.1 jonathan } 587 1.74 knakahar 588 1.74 knakahar /* 589 1.80 andvar * There is no struct ifnet for tunnel mode IP-IP tunnel connection, 590 1.74 knakahar * so we cannot write filtering rule to the inner packet. 591 1.74 knakahar */ 592 1.74 knakahar if (saidx->mode == IPSEC_MODE_TUNNEL) 593 1.74 knakahar m->m_pkthdr.pkthdr_flags |= PKTHDR_FLAG_IPSEC_SKIP_PFIL; 594 1.74 knakahar 595 1.1 jonathan nxt = (*inet6sw[ip6_protox[nxt]].pr_input)(&m, &skip, nxt); 596 1.1 jonathan } 597 1.1 jonathan return 0; 598 1.61 maxv 599 1.1 jonathan bad: 600 1.81 rin m_freem(m); 601 1.1 jonathan return error; 602 1.1 jonathan } 603 1.1 jonathan #endif /* INET6 */ 604