1 /* $OpenBSD: pfkeyv2_parsemessage.c,v 1.51 2017/02/28 16:46:27 bluhm Exp $ */ 2 3 /* 4 * @(#)COPYRIGHT 1.1 (NRL) 17 January 1995 5 * 6 * NRL grants permission for redistribution and use in source and binary 7 * forms, with or without modification, of the software and documentation 8 * created at NRL provided that the following conditions are met: 9 * 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgements: 17 * This product includes software developed by the University of 18 * California, Berkeley and its contributors. 19 * This product includes software developed at the Information 20 * Technology Division, US Naval Research Laboratory. 21 * 4. Neither the name of the NRL nor the names of its contributors 22 * may be used to endorse or promote products derived from this software 23 * without specific prior written permission. 24 * 25 * THE SOFTWARE PROVIDED BY NRL IS PROVIDED BY NRL AND CONTRIBUTORS ``AS 26 * IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 27 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 28 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL NRL OR 29 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 30 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 31 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 32 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 34 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 36 * 37 * The views and conclusions contained in the software and documentation 38 * are those of the authors and should not be interpreted as representing 39 * official policies, either expressed or implied, of the US Naval 40 * Research Laboratory (NRL). 41 */ 42 43 /* 44 * Copyright (c) 1995, 1996, 1997, 1998, 1999 Craig Metz. All rights reserved. 45 * 46 * Redistribution and use in source and binary forms, with or without 47 * modification, are permitted provided that the following conditions 48 * are met: 49 * 1. Redistributions of source code must retain the above copyright 50 * notice, this list of conditions and the following disclaimer. 51 * 2. Redistributions in binary form must reproduce the above copyright 52 * notice, this list of conditions and the following disclaimer in the 53 * documentation and/or other materials provided with the distribution. 54 * 3. Neither the name of the author nor the names of any contributors 55 * may be used to endorse or promote products derived from this software 56 * without specific prior written permission. 57 * 58 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 59 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 60 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 61 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 62 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 63 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 64 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 65 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 66 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 67 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 68 * SUCH DAMAGE. 69 */ 70 71 #include "pf.h" 72 73 #include <sys/param.h> 74 #include <sys/systm.h> 75 #include <sys/socket.h> 76 #include <sys/mbuf.h> 77 #include <sys/proc.h> 78 #include <netinet/ip_ipsp.h> 79 #include <netinet/ip_var.h> 80 #include <net/pfkeyv2.h> 81 82 #if NPF > 0 83 #include <net/if.h> 84 #include <net/pfvar.h> 85 #endif 86 87 #ifdef ENCDEBUG 88 #define DPRINTF(x) if (encdebug) printf x 89 #else 90 #define DPRINTF(x) 91 #endif 92 93 #define BITMAP_SA (1LL << SADB_EXT_SA) 94 #define BITMAP_LIFETIME_CURRENT (1LL << SADB_EXT_LIFETIME_CURRENT) 95 #define BITMAP_LIFETIME_HARD (1LL << SADB_EXT_LIFETIME_HARD) 96 #define BITMAP_LIFETIME_SOFT (1LL << SADB_EXT_LIFETIME_SOFT) 97 #define BITMAP_ADDRESS_SRC (1LL << SADB_EXT_ADDRESS_SRC) 98 #define BITMAP_ADDRESS_DST (1LL << SADB_EXT_ADDRESS_DST) 99 #define BITMAP_KEY_AUTH (1LL << SADB_EXT_KEY_AUTH) 100 #define BITMAP_KEY_ENCRYPT (1LL << SADB_EXT_KEY_ENCRYPT) 101 #define BITMAP_IDENTITY_SRC (1LL << SADB_EXT_IDENTITY_SRC) 102 #define BITMAP_IDENTITY_DST (1LL << SADB_EXT_IDENTITY_DST) 103 #define BITMAP_SENSITIVITY (1LL << SADB_EXT_SENSITIVITY) 104 #define BITMAP_PROPOSAL (1LL << SADB_EXT_PROPOSAL) 105 #define BITMAP_SUPPORTED_AUTH (1LL << SADB_EXT_SUPPORTED_AUTH) 106 #define BITMAP_SUPPORTED_ENCRYPT (1LL << SADB_EXT_SUPPORTED_ENCRYPT) 107 #define BITMAP_SPIRANGE (1LL << SADB_EXT_SPIRANGE) 108 #define BITMAP_LIFETIME (BITMAP_LIFETIME_CURRENT | BITMAP_LIFETIME_HARD | BITMAP_LIFETIME_SOFT) 109 #define BITMAP_ADDRESS (BITMAP_ADDRESS_SRC | BITMAP_ADDRESS_DST) 110 #define BITMAP_KEY (BITMAP_KEY_AUTH | BITMAP_KEY_ENCRYPT) 111 #define BITMAP_IDENTITY (BITMAP_IDENTITY_SRC | BITMAP_IDENTITY_DST) 112 #define BITMAP_MSG 1 113 #define BITMAP_X_SRC_MASK (1LL << SADB_X_EXT_SRC_MASK) 114 #define BITMAP_X_DST_MASK (1LL << SADB_X_EXT_DST_MASK) 115 #define BITMAP_X_PROTOCOL (1LL << SADB_X_EXT_PROTOCOL) 116 #define BITMAP_X_SRC_FLOW (1LL << SADB_X_EXT_SRC_FLOW) 117 #define BITMAP_X_DST_FLOW (1LL << SADB_X_EXT_DST_FLOW) 118 #define BITMAP_X_FLOW_TYPE (1LL << SADB_X_EXT_FLOW_TYPE) 119 #define BITMAP_X_SA2 (1LL << SADB_X_EXT_SA2) 120 #define BITMAP_X_DST2 (1LL << SADB_X_EXT_DST2) 121 #define BITMAP_X_POLICY (1LL << SADB_X_EXT_POLICY) 122 #define BITMAP_X_FLOW (BITMAP_X_SRC_MASK | BITMAP_X_DST_MASK | BITMAP_X_PROTOCOL | BITMAP_X_SRC_FLOW | BITMAP_X_DST_FLOW | BITMAP_X_FLOW_TYPE) 123 #define BITMAP_X_SUPPORTED_COMP (1LL << SADB_X_EXT_SUPPORTED_COMP) 124 #define BITMAP_X_UDPENCAP (1LL << SADB_X_EXT_UDPENCAP) 125 #define BITMAP_X_LIFETIME_LASTUSE (1LL << SADB_X_EXT_LIFETIME_LASTUSE) 126 #define BITMAP_X_TAG (1LL << SADB_X_EXT_TAG) 127 #define BITMAP_X_TAP (1LL << SADB_X_EXT_TAP) 128 #define BITMAP_X_SATYPE2 (1LL << SADB_X_EXT_SATYPE2) 129 130 uint64_t sadb_exts_allowed_in[SADB_MAX+1] = 131 { 132 /* RESERVED */ 133 ~0, 134 /* GETSPI */ 135 BITMAP_ADDRESS_SRC | BITMAP_ADDRESS_DST | BITMAP_SPIRANGE, 136 /* UPDATE */ 137 BITMAP_SA | BITMAP_LIFETIME | BITMAP_ADDRESS | BITMAP_KEY | BITMAP_IDENTITY | BITMAP_X_FLOW | BITMAP_X_UDPENCAP | BITMAP_X_TAG | BITMAP_X_TAP, 138 /* ADD */ 139 BITMAP_SA | BITMAP_LIFETIME | BITMAP_ADDRESS | BITMAP_KEY | BITMAP_IDENTITY | BITMAP_X_FLOW | BITMAP_X_UDPENCAP | BITMAP_X_LIFETIME_LASTUSE | BITMAP_X_TAG | BITMAP_X_TAP, 140 /* DELETE */ 141 BITMAP_SA | BITMAP_ADDRESS_SRC | BITMAP_ADDRESS_DST, 142 /* GET */ 143 BITMAP_SA | BITMAP_ADDRESS_SRC | BITMAP_ADDRESS_DST, 144 /* ACQUIRE */ 145 BITMAP_ADDRESS_SRC | BITMAP_ADDRESS_DST | BITMAP_IDENTITY | BITMAP_PROPOSAL, 146 /* REGISTER */ 147 0, 148 /* EXPIRE */ 149 BITMAP_SA | BITMAP_ADDRESS_SRC | BITMAP_ADDRESS_DST, 150 /* FLUSH */ 151 0, 152 /* DUMP */ 153 0, 154 /* X_PROMISC */ 155 0, 156 /* X_ADDFLOW */ 157 BITMAP_ADDRESS_SRC | BITMAP_ADDRESS_DST | BITMAP_IDENTITY_SRC | BITMAP_IDENTITY_DST | BITMAP_X_FLOW, 158 /* X_DELFLOW */ 159 BITMAP_X_FLOW, 160 /* X_GRPSPIS */ 161 BITMAP_SA | BITMAP_X_SA2 | BITMAP_X_DST2 | BITMAP_ADDRESS_DST | BITMAP_X_SATYPE2, 162 /* X_ASKPOLICY */ 163 BITMAP_X_POLICY, 164 }; 165 166 uint64_t sadb_exts_required_in[SADB_MAX+1] = 167 { 168 /* RESERVED */ 169 0, 170 /* GETSPI */ 171 BITMAP_ADDRESS_SRC | BITMAP_ADDRESS_DST | BITMAP_SPIRANGE, 172 /* UPDATE */ 173 BITMAP_SA | BITMAP_ADDRESS_SRC | BITMAP_ADDRESS_DST, 174 /* ADD */ 175 BITMAP_SA | BITMAP_ADDRESS_DST, 176 /* DELETE */ 177 BITMAP_SA | BITMAP_ADDRESS_DST, 178 /* GET */ 179 BITMAP_SA | BITMAP_ADDRESS_DST, 180 /* ACQUIRE */ 181 0, 182 /* REGISTER */ 183 0, 184 /* EXPIRE */ 185 BITMAP_SA | BITMAP_ADDRESS_SRC | BITMAP_ADDRESS_DST, 186 /* FLUSH */ 187 0, 188 /* DUMP */ 189 0, 190 /* X_PROMISC */ 191 0, 192 /* X_ADDFLOW */ 193 BITMAP_X_SRC_MASK | BITMAP_X_DST_MASK | BITMAP_X_SRC_FLOW | BITMAP_X_DST_FLOW | BITMAP_X_FLOW_TYPE, 194 /* X_DELFLOW */ 195 BITMAP_X_SRC_MASK | BITMAP_X_DST_MASK | BITMAP_X_SRC_FLOW | BITMAP_X_DST_FLOW | BITMAP_X_FLOW_TYPE, 196 /* X_GRPSPIS */ 197 BITMAP_SA | BITMAP_X_SA2 | BITMAP_X_DST2 | BITMAP_ADDRESS_DST | BITMAP_X_SATYPE2, 198 /* X_ASKPOLICY */ 199 BITMAP_X_POLICY, 200 }; 201 202 uint64_t sadb_exts_allowed_out[SADB_MAX+1] = 203 { 204 /* RESERVED */ 205 ~0, 206 /* GETSPI */ 207 BITMAP_SA | BITMAP_ADDRESS_SRC | BITMAP_ADDRESS_DST, 208 /* UPDATE */ 209 BITMAP_SA | BITMAP_LIFETIME | BITMAP_ADDRESS | BITMAP_IDENTITY | BITMAP_X_FLOW | BITMAP_X_UDPENCAP | BITMAP_X_TAG | BITMAP_X_TAP, 210 /* ADD */ 211 BITMAP_SA | BITMAP_LIFETIME | BITMAP_ADDRESS | BITMAP_IDENTITY | BITMAP_X_FLOW | BITMAP_X_UDPENCAP | BITMAP_X_TAG | BITMAP_X_TAP, 212 /* DELETE */ 213 BITMAP_SA | BITMAP_ADDRESS_SRC | BITMAP_ADDRESS_DST, 214 /* GET */ 215 BITMAP_SA | BITMAP_LIFETIME | BITMAP_ADDRESS | BITMAP_KEY | BITMAP_IDENTITY | BITMAP_X_UDPENCAP | BITMAP_X_LIFETIME_LASTUSE | BITMAP_X_SRC_MASK | BITMAP_X_DST_MASK | BITMAP_X_PROTOCOL | BITMAP_X_FLOW_TYPE | BITMAP_X_SRC_FLOW | BITMAP_X_DST_FLOW | BITMAP_X_TAG | BITMAP_X_TAP, 216 /* ACQUIRE */ 217 BITMAP_ADDRESS_SRC | BITMAP_ADDRESS_DST | BITMAP_IDENTITY | BITMAP_PROPOSAL, 218 /* REGISTER */ 219 BITMAP_SUPPORTED_AUTH | BITMAP_SUPPORTED_ENCRYPT | BITMAP_X_SUPPORTED_COMP, 220 /* EXPIRE */ 221 BITMAP_SA | BITMAP_LIFETIME | BITMAP_ADDRESS, 222 /* FLUSH */ 223 0, 224 /* DUMP */ 225 BITMAP_SA | BITMAP_LIFETIME | BITMAP_ADDRESS | BITMAP_IDENTITY, 226 /* X_PROMISC */ 227 0, 228 /* X_ADDFLOW */ 229 BITMAP_ADDRESS_SRC | BITMAP_ADDRESS_DST | BITMAP_X_SRC_MASK | BITMAP_X_DST_MASK | BITMAP_X_PROTOCOL | BITMAP_X_SRC_FLOW | BITMAP_X_DST_FLOW | BITMAP_X_FLOW_TYPE | BITMAP_IDENTITY_SRC | BITMAP_IDENTITY_DST, 230 /* X_DELFLOW */ 231 BITMAP_X_SRC_MASK | BITMAP_X_DST_MASK | BITMAP_X_PROTOCOL | BITMAP_X_SRC_FLOW | BITMAP_X_DST_FLOW | BITMAP_X_FLOW_TYPE, 232 /* X_GRPSPIS */ 233 BITMAP_SA | BITMAP_X_SA2 | BITMAP_X_DST2 | BITMAP_ADDRESS_DST | BITMAP_X_SATYPE2, 234 /* X_ASKPOLICY */ 235 BITMAP_X_SRC_FLOW | BITMAP_X_DST_FLOW | BITMAP_X_SRC_MASK | BITMAP_X_DST_MASK | BITMAP_X_FLOW_TYPE | BITMAP_X_POLICY, 236 }; 237 238 uint64_t sadb_exts_required_out[SADB_MAX+1] = 239 { 240 /* RESERVED */ 241 0, 242 /* GETSPI */ 243 BITMAP_SA | BITMAP_ADDRESS_DST, 244 /* UPDATE */ 245 BITMAP_SA | BITMAP_ADDRESS_DST, 246 /* ADD */ 247 BITMAP_SA | BITMAP_ADDRESS_DST, 248 /* DELETE */ 249 BITMAP_SA | BITMAP_ADDRESS_DST, 250 /* GET */ 251 BITMAP_SA | BITMAP_LIFETIME_CURRENT | BITMAP_ADDRESS_DST, 252 /* ACQUIRE */ 253 0, 254 /* REGISTER */ 255 BITMAP_SUPPORTED_AUTH | BITMAP_SUPPORTED_ENCRYPT | BITMAP_X_SUPPORTED_COMP, 256 /* EXPIRE */ 257 BITMAP_SA | BITMAP_ADDRESS_DST, 258 /* FLUSH */ 259 0, 260 /* DUMP */ 261 0, 262 /* X_PROMISC */ 263 0, 264 /* X_ADDFLOW */ 265 BITMAP_X_SRC_MASK | BITMAP_X_DST_MASK | BITMAP_X_SRC_FLOW | BITMAP_X_DST_FLOW | BITMAP_X_FLOW_TYPE, 266 /* X_DELFLOW */ 267 BITMAP_X_SRC_MASK | BITMAP_X_DST_MASK | BITMAP_X_SRC_FLOW | BITMAP_X_DST_FLOW | BITMAP_X_FLOW_TYPE, 268 /* X_GRPSPIS */ 269 BITMAP_SA | BITMAP_X_SA2 | BITMAP_X_DST2 | BITMAP_ADDRESS_DST | BITMAP_X_SATYPE2, 270 /* X_REPPOLICY */ 271 BITMAP_X_SRC_FLOW | BITMAP_X_DST_FLOW | BITMAP_X_SRC_MASK | BITMAP_X_DST_MASK | BITMAP_X_FLOW_TYPE, 272 }; 273 274 int pfkeyv2_parsemessage(void *, int, void **); 275 276 #define RETURN_EINVAL(line) goto einval; 277 278 int 279 pfkeyv2_parsemessage(void *p, int len, void **headers) 280 { 281 struct sadb_ext *sadb_ext; 282 int i, left = len; 283 uint64_t allow, seen = 1; 284 struct sadb_msg *sadb_msg = (struct sadb_msg *) p; 285 286 bzero(headers, (SADB_EXT_MAX + 1) * sizeof(void *)); 287 288 if (left < sizeof(struct sadb_msg)) { 289 DPRINTF(("pfkeyv2_parsemessage: message too short\n")); 290 return (EINVAL); 291 } 292 293 headers[0] = p; 294 295 if (sadb_msg->sadb_msg_len * sizeof(uint64_t) != left) { 296 DPRINTF(("pfkeyv2_parsemessage: length not a multiple of 64\n")); 297 return (EINVAL); 298 } 299 300 p += sizeof(struct sadb_msg); 301 left -= sizeof(struct sadb_msg); 302 303 if (sadb_msg->sadb_msg_reserved) { 304 DPRINTF(("pfkeyv2_parsemessage: message header reserved " 305 "field set\n")); 306 return (EINVAL); 307 } 308 309 if (sadb_msg->sadb_msg_type > SADB_MAX) { 310 DPRINTF(("pfkeyv2_parsemessage: message type > %d\n", 311 SADB_MAX)); 312 return (EINVAL); 313 } 314 315 if (!sadb_msg->sadb_msg_type) { 316 DPRINTF(("pfkeyv2_parsemessage: message type unset\n")); 317 return (EINVAL); 318 } 319 320 if (sadb_msg->sadb_msg_pid != curproc->p_p->ps_pid) { 321 DPRINTF(("pfkeyv2_parsemessage: bad PID value\n")); 322 return (EINVAL); 323 } 324 325 if (sadb_msg->sadb_msg_errno) { 326 if (left) { 327 DPRINTF(("pfkeyv2_parsemessage: too-large error message\n")); 328 return (EINVAL); 329 } 330 return (0); 331 } 332 333 if (sadb_msg->sadb_msg_type == SADB_X_PROMISC) { 334 DPRINTF(("pfkeyv2_parsemessage: message type promiscuous\n")); 335 return (0); 336 } 337 338 allow = sadb_exts_allowed_in[sadb_msg->sadb_msg_type]; 339 340 while (left > 0) { 341 sadb_ext = (struct sadb_ext *)p; 342 if (left < sizeof(struct sadb_ext)) { 343 DPRINTF(("pfkeyv2_parsemessage: extension header too " 344 "short\n")); 345 return (EINVAL); 346 } 347 348 i = sadb_ext->sadb_ext_len * sizeof(uint64_t); 349 if (left < i) { 350 DPRINTF(("pfkeyv2_parsemessage: extension header " 351 "exceeds message length\n")); 352 return (EINVAL); 353 } 354 355 if (sadb_ext->sadb_ext_type > SADB_EXT_MAX) { 356 DPRINTF(("pfkeyv2_parsemessage: unknown extension " 357 "header %d\n", sadb_ext->sadb_ext_type)); 358 return (EINVAL); 359 } 360 361 if (!sadb_ext->sadb_ext_type) { 362 DPRINTF(("pfkeyv2_parsemessage: unset extension " 363 "header\n")); 364 return (EINVAL); 365 } 366 367 if (!(allow & (1LL << sadb_ext->sadb_ext_type))) { 368 DPRINTF(("pfkeyv2_parsemessage: extension header %d " 369 "not permitted on message type %d\n", 370 sadb_ext->sadb_ext_type, sadb_msg->sadb_msg_type)); 371 return (EINVAL); 372 } 373 374 if (headers[sadb_ext->sadb_ext_type]) { 375 DPRINTF(("pfkeyv2_parsemessage: duplicate extension " 376 "header %d\n", sadb_ext->sadb_ext_type)); 377 return (EINVAL); 378 } 379 380 seen |= (1LL << sadb_ext->sadb_ext_type); 381 382 switch (sadb_ext->sadb_ext_type) { 383 case SADB_EXT_SA: 384 case SADB_X_EXT_SA2: 385 { 386 struct sadb_sa *sadb_sa = (struct sadb_sa *)p; 387 388 if (i != sizeof(struct sadb_sa)) { 389 DPRINTF(("pfkeyv2_parsemessage: bad header " 390 "length for SA extension header %d\n", 391 sadb_ext->sadb_ext_type)); 392 return (EINVAL); 393 } 394 395 if (sadb_sa->sadb_sa_state > SADB_SASTATE_MAX) { 396 DPRINTF(("pfkeyv2_parsemessage: unknown SA " 397 "state %d in SA extension header %d\n", 398 sadb_sa->sadb_sa_state, 399 sadb_ext->sadb_ext_type)); 400 return (EINVAL); 401 } 402 403 if (sadb_sa->sadb_sa_state == SADB_SASTATE_DEAD) { 404 DPRINTF(("pfkeyv2_parsemessage: cannot set SA " 405 "state to dead, SA extension header %d\n", 406 sadb_ext->sadb_ext_type)); 407 return (EINVAL); 408 } 409 410 if (sadb_sa->sadb_sa_encrypt > SADB_EALG_MAX) { 411 DPRINTF(("pfkeyv2_parsemessage: unknown " 412 "encryption algorithm %d in SA extension " 413 "header %d\n", sadb_sa->sadb_sa_encrypt, 414 sadb_ext->sadb_ext_type)); 415 return (EINVAL); 416 } 417 418 if (sadb_sa->sadb_sa_auth > SADB_AALG_MAX) { 419 DPRINTF(("pfkeyv2_parsemessage: unknown " 420 "authentication algorithm %d in SA " 421 "extension header %d\n", 422 sadb_sa->sadb_sa_auth, 423 sadb_ext->sadb_ext_type)); 424 return (EINVAL); 425 } 426 427 if (sadb_sa->sadb_sa_replay > 64) { 428 DPRINTF(("pfkeyv2_parsemessage: unsupported " 429 "replay window size %d in SA extension " 430 "header %d\n", sadb_sa->sadb_sa_replay, 431 sadb_ext->sadb_ext_type)); 432 return (EINVAL); 433 } 434 } 435 break; 436 case SADB_X_EXT_PROTOCOL: 437 case SADB_X_EXT_FLOW_TYPE: 438 case SADB_X_EXT_SATYPE2: 439 if (i != sizeof(struct sadb_protocol)) { 440 DPRINTF(("pfkeyv2_parsemessage: bad PROTOCOL/" 441 "FLOW/SATYPE2 header length in extension " 442 "header %d\n", sadb_ext->sadb_ext_type)); 443 return (EINVAL); 444 } 445 break; 446 case SADB_X_EXT_POLICY: 447 if (i != sizeof(struct sadb_x_policy)) { 448 DPRINTF(("pfkeyv2_parsemessage: bad POLICY " 449 "header length\n")); 450 return (EINVAL); 451 } 452 break; 453 case SADB_EXT_LIFETIME_CURRENT: 454 case SADB_EXT_LIFETIME_HARD: 455 case SADB_EXT_LIFETIME_SOFT: 456 case SADB_X_EXT_LIFETIME_LASTUSE: 457 if (i != sizeof(struct sadb_lifetime)) { 458 DPRINTF(("pfkeyv2_parsemessage: bad header " 459 "length for LIFETIME extension header " 460 "%d\n", sadb_ext->sadb_ext_type)); 461 return (EINVAL); 462 } 463 break; 464 case SADB_EXT_ADDRESS_SRC: 465 case SADB_EXT_ADDRESS_DST: 466 case SADB_X_EXT_SRC_MASK: 467 case SADB_X_EXT_DST_MASK: 468 case SADB_X_EXT_SRC_FLOW: 469 case SADB_X_EXT_DST_FLOW: 470 case SADB_X_EXT_DST2: 471 { 472 struct sadb_address *sadb_address = 473 (struct sadb_address *)p; 474 struct sockaddr *sa = (struct sockaddr *)(p + 475 sizeof(struct sadb_address)); 476 477 if (i < sizeof(struct sadb_address) + 478 sizeof(struct sockaddr)) { 479 DPRINTF(("pfkeyv2_parsemessage: bad ADDRESS " 480 "extension header %d length\n", 481 sadb_ext->sadb_ext_type)); 482 return (EINVAL); 483 } 484 485 if (sadb_address->sadb_address_reserved) { 486 DPRINTF(("pfkeyv2_parsemessage: ADDRESS " 487 "extension header %d reserved field set\n", 488 sadb_ext->sadb_ext_type)); 489 return (EINVAL); 490 } 491 if (sa->sa_len && 492 (i != sizeof(struct sadb_address) + 493 PADUP(sa->sa_len))) { 494 DPRINTF(("pfkeyv2_parsemessage: bad sockaddr " 495 "length field in ADDRESS extension " 496 "header %d\n", sadb_ext->sadb_ext_type)); 497 return (EINVAL); 498 } 499 500 switch (sa->sa_family) { 501 case AF_INET: 502 if (sizeof(struct sadb_address) + 503 PADUP(sizeof(struct sockaddr_in)) != i) { 504 DPRINTF(("pfkeyv2_parsemessage: " 505 "invalid ADDRESS extension header " 506 "%d length\n", 507 sadb_ext->sadb_ext_type)); 508 return (EINVAL); 509 } 510 511 if (sa->sa_len != sizeof(struct sockaddr_in)) { 512 DPRINTF(("pfkeyv2_parsemessage: bad " 513 "sockaddr_in length in ADDRESS " 514 "extension header %d\n", 515 sadb_ext->sadb_ext_type)); 516 return (EINVAL); 517 } 518 519 /* Only check the right pieces */ 520 switch (sadb_ext->sadb_ext_type) 521 { 522 case SADB_X_EXT_SRC_MASK: 523 case SADB_X_EXT_DST_MASK: 524 case SADB_X_EXT_SRC_FLOW: 525 case SADB_X_EXT_DST_FLOW: 526 break; 527 528 default: 529 if (((struct sockaddr_in *)sa)->sin_port) { 530 DPRINTF(("pfkeyv2_parsemessage" 531 ": port field set in " 532 "sockaddr_in of ADDRESS " 533 "extension header %d\n", 534 sadb_ext->sadb_ext_type)); 535 return (EINVAL); 536 } 537 break; 538 } 539 540 { 541 char zero[sizeof(((struct sockaddr_in *)sa)->sin_zero)]; 542 bzero(zero, sizeof(zero)); 543 544 if (bcmp(&((struct sockaddr_in *)sa)->sin_zero, zero, sizeof(zero))) { 545 DPRINTF(("pfkeyv2_parsemessage" 546 ": reserved sockaddr_in " 547 "field non-zero'ed in " 548 "ADDRESS extension header " 549 "%d\n", 550 sadb_ext->sadb_ext_type)); 551 return (EINVAL); 552 } 553 } 554 break; 555 #ifdef INET6 556 case AF_INET6: 557 if (i != sizeof(struct sadb_address) + 558 PADUP(sizeof(struct sockaddr_in6))) { 559 DPRINTF(("pfkeyv2_parsemessage: " 560 "invalid sockaddr_in6 length in " 561 "ADDRESS extension header %d\n", 562 sadb_ext->sadb_ext_type)); 563 return (EINVAL); 564 } 565 566 if (sa->sa_len != 567 sizeof(struct sockaddr_in6)) { 568 DPRINTF(("pfkeyv2_parsemessage: bad " 569 "sockaddr_in6 length in ADDRESS " 570 "extension header %d\n", 571 sadb_ext->sadb_ext_type)); 572 return (EINVAL); 573 } 574 575 if (((struct sockaddr_in6 *)sa)->sin6_flowinfo) { 576 DPRINTF(("pfkeyv2_parsemessage: " 577 "flowinfo field set in " 578 "sockaddr_in6 of ADDRESS " 579 "extension header %d\n", 580 sadb_ext->sadb_ext_type)); 581 return (EINVAL); 582 } 583 584 /* Only check the right pieces */ 585 switch (sadb_ext->sadb_ext_type) 586 { 587 case SADB_X_EXT_SRC_MASK: 588 case SADB_X_EXT_DST_MASK: 589 case SADB_X_EXT_SRC_FLOW: 590 case SADB_X_EXT_DST_FLOW: 591 break; 592 593 default: 594 if (((struct sockaddr_in6 *)sa)->sin6_port) { 595 DPRINTF(("pfkeyv2_parsemessage" 596 ": port field set in " 597 "sockaddr_in6 of ADDRESS " 598 "extension header %d\n", 599 sadb_ext->sadb_ext_type)); 600 return (EINVAL); 601 } 602 break; 603 } 604 break; 605 #endif /* INET6 */ 606 default: 607 if (sadb_msg->sadb_msg_satype == 608 SADB_X_SATYPE_TCPSIGNATURE && 609 sa->sa_family == 0) 610 break; 611 DPRINTF(("pfkeyv2_parsemessage: unknown " 612 "address family %d in ADDRESS extension " 613 "header %d\n", 614 sa->sa_family, sadb_ext->sadb_ext_type)); 615 return (EINVAL); 616 } 617 } 618 break; 619 case SADB_EXT_KEY_AUTH: 620 case SADB_EXT_KEY_ENCRYPT: 621 { 622 struct sadb_key *sadb_key = (struct sadb_key *)p; 623 624 if (i < sizeof(struct sadb_key)) { 625 DPRINTF(("pfkeyv2_parsemessage: bad header " 626 "length in KEY extension header %d\n", 627 sadb_ext->sadb_ext_type)); 628 return (EINVAL); 629 } 630 631 if (!sadb_key->sadb_key_bits) { 632 DPRINTF(("pfkeyv2_parsemessage: key length " 633 "unset in KEY extension header %d\n", 634 sadb_ext->sadb_ext_type)); 635 return (EINVAL); 636 } 637 638 if (((sadb_key->sadb_key_bits + 63) / 64) * sizeof(uint64_t) != i - sizeof(struct sadb_key)) { 639 DPRINTF(("pfkeyv2_parsemessage: invalid key " 640 "length in KEY extension header %d\n", 641 sadb_ext->sadb_ext_type)); 642 return (EINVAL); 643 } 644 645 if (sadb_key->sadb_key_reserved) { 646 DPRINTF(("pfkeyv2_parsemessage: reserved field" 647 " set in KEY extension header %d\n", 648 sadb_ext->sadb_ext_type)); 649 return (EINVAL); 650 } 651 } 652 break; 653 case SADB_EXT_IDENTITY_SRC: 654 case SADB_EXT_IDENTITY_DST: 655 { 656 struct sadb_ident *sadb_ident = (struct sadb_ident *)p; 657 658 if (i < sizeof(struct sadb_ident)) { 659 DPRINTF(("pfkeyv2_parsemessage: bad header " 660 "length of IDENTITY extension header %d\n", 661 sadb_ext->sadb_ext_type)); 662 return (EINVAL); 663 } 664 665 if (sadb_ident->sadb_ident_type > SADB_IDENTTYPE_MAX) { 666 DPRINTF(("pfkeyv2_parsemessage: unknown " 667 "identity type %d in IDENTITY extension " 668 "header %d\n", 669 sadb_ident->sadb_ident_type, 670 sadb_ext->sadb_ext_type)); 671 return (EINVAL); 672 } 673 674 if (sadb_ident->sadb_ident_reserved) { 675 DPRINTF(("pfkeyv2_parsemessage: reserved " 676 "field set in IDENTITY extension header " 677 "%d\n", sadb_ext->sadb_ext_type)); 678 return (EINVAL); 679 } 680 681 if (i > sizeof(struct sadb_ident)) { 682 char *c = 683 (char *)(p + sizeof(struct sadb_ident)); 684 int j; 685 686 if (*(char *)(p + i - 1)) { 687 DPRINTF(("pfkeyv2_parsemessage: non " 688 "NUL-terminated identity in " 689 "IDENTITY extension header %d\n", 690 sadb_ext->sadb_ext_type)); 691 return (EINVAL); 692 } 693 694 j = PADUP(strlen(c) + 1) + 695 sizeof(struct sadb_ident); 696 697 if (i != j) { 698 DPRINTF(("pfkeyv2_parsemessage: actual" 699 " identity length does not match " 700 "expected length in identity " 701 "extension header %d\n", 702 sadb_ext->sadb_ext_type)); 703 return (EINVAL); 704 } 705 } 706 } 707 break; 708 case SADB_EXT_SENSITIVITY: 709 { 710 struct sadb_sens *sadb_sens = (struct sadb_sens *)p; 711 712 if (i < sizeof(struct sadb_sens)) { 713 DPRINTF(("pfkeyv2_parsemessage: bad header " 714 "length for SENSITIVITY extension " 715 "header\n")); 716 return (EINVAL); 717 } 718 719 if (i != (sadb_sens->sadb_sens_sens_len + 720 sadb_sens->sadb_sens_integ_len) * 721 sizeof(uint64_t) + 722 sizeof(struct sadb_sens)) { 723 DPRINTF(("pfkeyv2_parsemessage: bad payload " 724 "length for SENSITIVITY extension " 725 "header\n")); 726 return (EINVAL); 727 } 728 } 729 break; 730 case SADB_EXT_PROPOSAL: 731 { 732 struct sadb_prop *sadb_prop = (struct sadb_prop *)p; 733 734 if (i < sizeof(struct sadb_prop)) { 735 DPRINTF(("pfkeyv2_parsemessage: bad PROPOSAL " 736 "header length\n")); 737 return (EINVAL); 738 } 739 740 if (sadb_prop->sadb_prop_reserved) { 741 DPRINTF(("pfkeyv2_parsemessage: reserved field" 742 "set in PROPOSAL extension header\n")); 743 return (EINVAL); 744 } 745 746 if ((i - sizeof(struct sadb_prop)) % 747 sizeof(struct sadb_comb)) { 748 DPRINTF(("pfkeyv2_parsemessage: bad proposal " 749 "length\n")); 750 return (EINVAL); 751 } 752 753 { 754 struct sadb_comb *sadb_comb = 755 (struct sadb_comb *)(p + 756 sizeof(struct sadb_prop)); 757 int j; 758 759 for (j = 0; 760 j < (i - sizeof(struct sadb_prop))/ 761 sizeof(struct sadb_comb); 762 j++) { 763 if (sadb_comb->sadb_comb_auth > 764 SADB_AALG_MAX) { 765 DPRINTF(("pfkeyv2_parsemessage" 766 ": unknown authentication " 767 "algorithm %d in " 768 "PROPOSAL\n", 769 sadb_comb->sadb_comb_auth)); 770 return (EINVAL); 771 } 772 773 if (sadb_comb->sadb_comb_encrypt > 774 SADB_EALG_MAX) { 775 DPRINTF(("pfkeyv2_parsemessage" 776 ": unknown encryption " 777 "algorithm %d in " 778 "PROPOSAL\n", 779 sadb_comb->sadb_comb_encrypt)); 780 return (EINVAL); 781 } 782 783 if (sadb_comb->sadb_comb_reserved) { 784 DPRINTF(("pfkeyv2_parsemessage" 785 ": reserved field set in " 786 "COMB header\n")); 787 return (EINVAL); 788 } 789 } 790 } 791 } 792 break; 793 case SADB_EXT_SUPPORTED_AUTH: 794 case SADB_EXT_SUPPORTED_ENCRYPT: 795 case SADB_X_EXT_SUPPORTED_COMP: 796 { 797 struct sadb_supported *sadb_supported = 798 (struct sadb_supported *)p; 799 int j; 800 801 if (i < sizeof(struct sadb_supported)) { 802 DPRINTF(("pfkeyv2_parsemessage: bad header " 803 "length for SUPPORTED extension header " 804 "%d\n", sadb_ext->sadb_ext_type)); 805 return (EINVAL); 806 } 807 808 if (sadb_supported->sadb_supported_reserved) { 809 DPRINTF(("pfkeyv2_parsemessage: reserved " 810 "field set in SUPPORTED extension " 811 "header %d\n", sadb_ext->sadb_ext_type)); 812 return (EINVAL); 813 } 814 815 { 816 struct sadb_alg *sadb_alg = 817 (struct sadb_alg *)(p + 818 sizeof(struct sadb_supported)); 819 int max_alg; 820 821 max_alg = sadb_ext->sadb_ext_type == 822 SADB_EXT_SUPPORTED_AUTH ? 823 SADB_AALG_MAX : SADB_EXT_SUPPORTED_ENCRYPT ? 824 SADB_EALG_MAX : SADB_X_CALG_MAX; 825 826 for (j = 0; 827 j < sadb_supported->sadb_supported_len - 1; 828 j++) { 829 if (sadb_alg->sadb_alg_id > max_alg) { 830 DPRINTF(("pfkeyv2_parsemessage" 831 ": unknown algorithm %d " 832 "in SUPPORTED extension " 833 "header %d\n", 834 sadb_alg->sadb_alg_id, 835 sadb_ext->sadb_ext_type)); 836 return (EINVAL); 837 } 838 839 if (sadb_alg->sadb_alg_reserved) { 840 DPRINTF(("pfkeyv2_parsemessage" 841 ": reserved field set in " 842 "supported algorithms " 843 "header inside SUPPORTED " 844 "extension header %d\n", 845 sadb_ext->sadb_ext_type)); 846 return (EINVAL); 847 } 848 849 sadb_alg++; 850 } 851 } 852 } 853 break; 854 case SADB_EXT_SPIRANGE: 855 { 856 struct sadb_spirange *sadb_spirange = 857 (struct sadb_spirange *)p; 858 859 if (i != sizeof(struct sadb_spirange)) { 860 DPRINTF(("pfkeyv2_parsemessage: bad header " 861 "length of SPIRANGE extension header\n")); 862 return (EINVAL); 863 } 864 865 if (sadb_spirange->sadb_spirange_min > 866 sadb_spirange->sadb_spirange_max) { 867 DPRINTF(("pfkeyv2_parsemessage: bad SPI " 868 "range\n")); 869 return (EINVAL); 870 } 871 } 872 break; 873 case SADB_X_EXT_UDPENCAP: 874 if (i != sizeof(struct sadb_x_udpencap)) { 875 DPRINTF(("pfkeyv2_parsemessage: bad UDPENCAP " 876 "header length\n")); 877 return (EINVAL); 878 } 879 break; 880 #if NPF > 0 881 case SADB_X_EXT_TAG: 882 if (i < sizeof(struct sadb_x_tag)) { 883 DPRINTF(("pfkeyv2_parsemessage: " 884 "TAG extension header too small")); 885 return (EINVAL); 886 } 887 if (i > (sizeof(struct sadb_x_tag) + 888 PF_TAG_NAME_SIZE)) { 889 DPRINTF(("pfkeyv2_parsemessage: " 890 "TAG extension header too long")); 891 return (EINVAL); 892 } 893 break; 894 case SADB_X_EXT_TAP: 895 if (i < sizeof(struct sadb_x_tap)) { 896 DPRINTF(("pfkeyv2_parsemessage: " 897 "TAP extension header too small")); 898 return (EINVAL); 899 } 900 if (i > sizeof(struct sadb_x_tap)) { 901 DPRINTF(("pfkeyv2_parsemessage: " 902 "TAP extension header too long")); 903 return (EINVAL); 904 } 905 break; 906 #endif 907 default: 908 DPRINTF(("pfkeyv2_parsemessage: unknown extension " 909 "header type %d\n", 910 sadb_ext->sadb_ext_type)); 911 return (EINVAL); 912 } 913 914 headers[sadb_ext->sadb_ext_type] = p; 915 p += i; 916 left -= i; 917 } 918 919 if (left) { 920 DPRINTF(("pfkeyv2_parsemessage: message too long\n")); 921 return (EINVAL); 922 } 923 924 { 925 uint64_t required; 926 927 required = sadb_exts_required_in[sadb_msg->sadb_msg_type]; 928 929 if ((seen & required) != required) { 930 DPRINTF(("pfkeyv2_parsemessage: required fields " 931 "missing\n")); 932 return (EINVAL); 933 } 934 } 935 936 switch (((struct sadb_msg *)headers[0])->sadb_msg_type) { 937 case SADB_UPDATE: 938 if (((struct sadb_sa *)headers[SADB_EXT_SA])->sadb_sa_state != 939 SADB_SASTATE_MATURE) { 940 DPRINTF(("pfkeyv2_parsemessage: updating non-mature " 941 "SA prohibited\n")); 942 return (EINVAL); 943 } 944 break; 945 case SADB_ADD: 946 if (((struct sadb_sa *)headers[SADB_EXT_SA])->sadb_sa_state != 947 SADB_SASTATE_MATURE) { 948 DPRINTF(("pfkeyv2_parsemessage: adding non-mature " 949 "SA prohibited\n")); 950 return (EINVAL); 951 } 952 break; 953 } 954 955 return (0); 956 } 957