Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 | // SPDX-License-Identifier: GPL-2.0 /* Copyright (c) 2020 Facebook */ #include <stddef.h> #include <errno.h> #include <stdbool.h> #include <sys/types.h> #include <sys/socket.h> #include <linux/tcp.h> #include <linux/socket.h> #include <linux/bpf.h> #include <linux/types.h> #include <bpf/bpf_helpers.h> #include <bpf/bpf_endian.h> #define BPF_PROG_TEST_TCP_HDR_OPTIONS #include "test_tcp_hdr_options.h" #ifndef sizeof_field #define sizeof_field(TYPE, MEMBER) sizeof((((TYPE *)0)->MEMBER)) #endif __u8 test_kind = TCPOPT_EXP; __u16 test_magic = 0xeB9F; __u32 inherit_cb_flags = 0; struct bpf_test_option passive_synack_out = {}; struct bpf_test_option passive_fin_out = {}; struct bpf_test_option passive_estab_in = {}; struct bpf_test_option passive_fin_in = {}; struct bpf_test_option active_syn_out = {}; struct bpf_test_option active_fin_out = {}; struct bpf_test_option active_estab_in = {}; struct bpf_test_option active_fin_in = {}; struct { __uint(type, BPF_MAP_TYPE_SK_STORAGE); __uint(map_flags, BPF_F_NO_PREALLOC); __type(key, int); __type(value, struct hdr_stg); } hdr_stg_map SEC(".maps"); static bool skops_want_cookie(const struct bpf_sock_ops *skops) { return skops->args[0] == BPF_WRITE_HDR_TCP_SYNACK_COOKIE; } static bool skops_current_mss(const struct bpf_sock_ops *skops) { return skops->args[0] == BPF_WRITE_HDR_TCP_CURRENT_MSS; } static __u8 option_total_len(__u8 flags) { __u8 i, len = 1; /* +1 for flags */ if (!flags) return 0; /* RESEND bit does not use a byte */ for (i = OPTION_RESEND + 1; i < __NR_OPTION_FLAGS; i++) len += !!TEST_OPTION_FLAGS(flags, i); if (test_kind == TCPOPT_EXP) return len + TCP_BPF_EXPOPT_BASE_LEN; else return len + 2; /* +1 kind, +1 kind-len */ } static void write_test_option(const struct bpf_test_option *test_opt, __u8 *data) { __u8 offset = 0; data[offset++] = test_opt->flags; if (TEST_OPTION_FLAGS(test_opt->flags, OPTION_MAX_DELACK_MS)) data[offset++] = test_opt->max_delack_ms; if (TEST_OPTION_FLAGS(test_opt->flags, OPTION_RAND)) data[offset++] = test_opt->rand; } static int store_option(struct bpf_sock_ops *skops, const struct bpf_test_option *test_opt) { union { struct tcp_exprm_opt exprm; struct tcp_opt regular; } write_opt; int err; if (test_kind == TCPOPT_EXP) { write_opt.exprm.kind = TCPOPT_EXP; write_opt.exprm.len = option_total_len(test_opt->flags); write_opt.exprm.magic = __bpf_htons(test_magic); write_opt.exprm.data32 = 0; write_test_option(test_opt, write_opt.exprm.data); err = bpf_store_hdr_opt(skops, &write_opt.exprm, sizeof(write_opt.exprm), 0); } else { write_opt.regular.kind = test_kind; write_opt.regular.len = option_total_len(test_opt->flags); write_opt.regular.data32 = 0; write_test_option(test_opt, write_opt.regular.data); err = bpf_store_hdr_opt(skops, &write_opt.regular, sizeof(write_opt.regular), 0); } if (err) RET_CG_ERR(err); return CG_OK; } static int parse_test_option(struct bpf_test_option *opt, const __u8 *start) { opt->flags = *start++; if (TEST_OPTION_FLAGS(opt->flags, OPTION_MAX_DELACK_MS)) opt->max_delack_ms = *start++; if (TEST_OPTION_FLAGS(opt->flags, OPTION_RAND)) opt->rand = *start++; return 0; } static int load_option(struct bpf_sock_ops *skops, struct bpf_test_option *test_opt, bool from_syn) { union { struct tcp_exprm_opt exprm; struct tcp_opt regular; } search_opt; int ret, load_flags = from_syn ? BPF_LOAD_HDR_OPT_TCP_SYN : 0; if (test_kind == TCPOPT_EXP) { search_opt.exprm.kind = TCPOPT_EXP; search_opt.exprm.len = 4; search_opt.exprm.magic = __bpf_htons(test_magic); search_opt.exprm.data32 = 0; ret = bpf_load_hdr_opt(skops, &search_opt.exprm, sizeof(search_opt.exprm), load_flags); if (ret < 0) return ret; return parse_test_option(test_opt, search_opt.exprm.data); } else { search_opt.regular.kind = test_kind; search_opt.regular.len = 0; search_opt.regular.data32 = 0; ret = bpf_load_hdr_opt(skops, &search_opt.regular, sizeof(search_opt.regular), load_flags); if (ret < 0) return ret; return parse_test_option(test_opt, search_opt.regular.data); } } static int synack_opt_len(struct bpf_sock_ops *skops) { struct bpf_test_option test_opt = {}; __u8 optlen; int err; if (!passive_synack_out.flags) return CG_OK; err = load_option(skops, &test_opt, true); /* bpf_test_option is not found */ if (err == -ENOMSG) return CG_OK; if (err) RET_CG_ERR(err); optlen = option_total_len(passive_synack_out.flags); if (optlen) { err = bpf_reserve_hdr_opt(skops, optlen, 0); if (err) RET_CG_ERR(err); } return CG_OK; } static int write_synack_opt(struct bpf_sock_ops *skops) { struct bpf_test_option opt; if (!passive_synack_out.flags) /* We should not even be called since no header * space has been reserved. */ RET_CG_ERR(0); opt = passive_synack_out; if (skops_want_cookie(skops)) SET_OPTION_FLAGS(opt.flags, OPTION_RESEND); return store_option(skops, &opt); } static int syn_opt_len(struct bpf_sock_ops *skops) { __u8 optlen; int err; if (!active_syn_out.flags) return CG_OK; optlen = option_total_len(active_syn_out.flags); if (optlen) { err = bpf_reserve_hdr_opt(skops, optlen, 0); if (err) RET_CG_ERR(err); } return CG_OK; } static int write_syn_opt(struct bpf_sock_ops *skops) { if (!active_syn_out.flags) RET_CG_ERR(0); return store_option(skops, &active_syn_out); } static int fin_opt_len(struct bpf_sock_ops *skops) { struct bpf_test_option *opt; struct hdr_stg *hdr_stg; __u8 optlen; int err; if (!skops->sk) RET_CG_ERR(0); hdr_stg = bpf_sk_storage_get(&hdr_stg_map, skops->sk, NULL, 0); if (!hdr_stg) RET_CG_ERR(0); if (hdr_stg->active) opt = &active_fin_out; else opt = &passive_fin_out; optlen = option_total_len(opt->flags); if (optlen) { err = bpf_reserve_hdr_opt(skops, optlen, 0); if (err) RET_CG_ERR(err); } return CG_OK; } static int write_fin_opt(struct bpf_sock_ops *skops) { struct bpf_test_option *opt; struct hdr_stg *hdr_stg; if (!skops->sk) RET_CG_ERR(0); hdr_stg = bpf_sk_storage_get(&hdr_stg_map, skops->sk, NULL, 0); if (!hdr_stg) RET_CG_ERR(0); if (hdr_stg->active) opt = &active_fin_out; else opt = &passive_fin_out; if (!opt->flags) RET_CG_ERR(0); return store_option(skops, opt); } static int resend_in_ack(struct bpf_sock_ops *skops) { struct hdr_stg *hdr_stg; if (!skops->sk) return -1; hdr_stg = bpf_sk_storage_get(&hdr_stg_map, skops->sk, NULL, 0); if (!hdr_stg) return -1; return !!hdr_stg->resend_syn; } static int nodata_opt_len(struct bpf_sock_ops *skops) { int resend; resend = resend_in_ack(skops); if (resend < 0) RET_CG_ERR(0); if (resend) return syn_opt_len(skops); return CG_OK; } static int write_nodata_opt(struct bpf_sock_ops *skops) { int resend; resend = resend_in_ack(skops); if (resend < 0) RET_CG_ERR(0); if (resend) return write_syn_opt(skops); return CG_OK; } static int data_opt_len(struct bpf_sock_ops *skops) { /* Same as the nodata version. Mostly to show * an example usage on skops->skb_len. */ return nodata_opt_len(skops); } static int write_data_opt(struct bpf_sock_ops *skops) { return write_nodata_opt(skops); } static int current_mss_opt_len(struct bpf_sock_ops *skops) { /* Reserve maximum that may be needed */ int err; err = bpf_reserve_hdr_opt(skops, option_total_len(OPTION_MASK), 0); if (err) RET_CG_ERR(err); return CG_OK; } static int handle_hdr_opt_len(struct bpf_sock_ops *skops) { __u8 tcp_flags = skops_tcp_flags(skops); if ((tcp_flags & TCPHDR_SYNACK) == TCPHDR_SYNACK) return synack_opt_len(skops); if (tcp_flags & TCPHDR_SYN) return syn_opt_len(skops); if (tcp_flags & TCPHDR_FIN) return fin_opt_len(skops); if (skops_current_mss(skops)) /* The kernel is calculating the MSS */ return current_mss_opt_len(skops); if (skops->skb_len) return data_opt_len(skops); return nodata_opt_len(skops); } static int handle_write_hdr_opt(struct bpf_sock_ops *skops) { __u8 tcp_flags = skops_tcp_flags(skops); struct tcphdr *th; if ((tcp_flags & TCPHDR_SYNACK) == TCPHDR_SYNACK) return write_synack_opt(skops); if (tcp_flags & TCPHDR_SYN) return write_syn_opt(skops); if (tcp_flags & TCPHDR_FIN) return write_fin_opt(skops); th = skops->skb_data; if (th + 1 > skops->skb_data_end) RET_CG_ERR(0); if (skops->skb_len > tcp_hdrlen(th)) return write_data_opt(skops); return write_nodata_opt(skops); } static int set_delack_max(struct bpf_sock_ops *skops, __u8 max_delack_ms) { __u32 max_delack_us = max_delack_ms * 1000; return bpf_setsockopt(skops, SOL_TCP, TCP_BPF_DELACK_MAX, &max_delack_us, sizeof(max_delack_us)); } static int set_rto_min(struct bpf_sock_ops *skops, __u8 peer_max_delack_ms) { __u32 min_rto_us = peer_max_delack_ms * 1000; return bpf_setsockopt(skops, SOL_TCP, TCP_BPF_RTO_MIN, &min_rto_us, sizeof(min_rto_us)); } static int handle_active_estab(struct bpf_sock_ops *skops) { struct hdr_stg init_stg = { .active = true, }; int err; err = load_option(skops, &active_estab_in, false); if (err && err != -ENOMSG) RET_CG_ERR(err); init_stg.resend_syn = TEST_OPTION_FLAGS(active_estab_in.flags, OPTION_RESEND); if (!skops->sk || !bpf_sk_storage_get(&hdr_stg_map, skops->sk, &init_stg, BPF_SK_STORAGE_GET_F_CREATE)) RET_CG_ERR(0); if (init_stg.resend_syn) /* Don't clear the write_hdr cb now because * the ACK may get lost and retransmit may * be needed. * * PARSE_ALL_HDR cb flag is set to learn if this * resend_syn option has received by the peer. * * The header option will be resent until a valid * packet is received at handle_parse_hdr() * and all hdr cb flags will be cleared in * handle_parse_hdr(). */ set_parse_all_hdr_cb_flags(skops); else if (!active_fin_out.flags) /* No options will be written from now */ clear_hdr_cb_flags(skops); if (active_syn_out.max_delack_ms) { err = set_delack_max(skops, active_syn_out.max_delack_ms); if (err) RET_CG_ERR(err); } if (active_estab_in.max_delack_ms) { err = set_rto_min(skops, active_estab_in.max_delack_ms); if (err) RET_CG_ERR(err); } return CG_OK; } static int handle_passive_estab(struct bpf_sock_ops *skops) { struct hdr_stg init_stg = {}; struct tcphdr *th; int err; inherit_cb_flags = skops->bpf_sock_ops_cb_flags; err = load_option(skops, &passive_estab_in, true); if (err == -ENOENT) { /* saved_syn is not found. It was in syncookie mode. * We have asked the active side to resend the options * in ACK, so try to find the bpf_test_option from ACK now. */ err = load_option(skops, &passive_estab_in, false); init_stg.syncookie = true; } /* ENOMSG: The bpf_test_option is not found which is fine. * Bail out now for all other errors. */ if (err && err != -ENOMSG) RET_CG_ERR(err); th = skops->skb_data; if (th + 1 > skops->skb_data_end) RET_CG_ERR(0); if (th->syn) { /* Fastopen */ /* Cannot clear cb_flags to stop write_hdr cb. * synack is not sent yet for fast open. * Even it was, the synack may need to be retransmitted. * * PARSE_ALL_HDR cb flag is set to learn * if synack has reached the peer. * All cb_flags will be cleared in handle_parse_hdr(). */ set_parse_all_hdr_cb_flags(skops); init_stg.fastopen = true; } else if (!passive_fin_out.flags) { /* No options will be written from now */ clear_hdr_cb_flags(skops); } if (!skops->sk || !bpf_sk_storage_get(&hdr_stg_map, skops->sk, &init_stg, BPF_SK_STORAGE_GET_F_CREATE)) RET_CG_ERR(0); if (passive_synack_out.max_delack_ms) { err = set_delack_max(skops, passive_synack_out.max_delack_ms); if (err) RET_CG_ERR(err); } if (passive_estab_in.max_delack_ms) { err = set_rto_min(skops, passive_estab_in.max_delack_ms); if (err) RET_CG_ERR(err); } return CG_OK; } static int handle_parse_hdr(struct bpf_sock_ops *skops) { struct hdr_stg *hdr_stg; struct tcphdr *th; if (!skops->sk) RET_CG_ERR(0); th = skops->skb_data; if (th + 1 > skops->skb_data_end) RET_CG_ERR(0); hdr_stg = bpf_sk_storage_get(&hdr_stg_map, skops->sk, NULL, 0); if (!hdr_stg) RET_CG_ERR(0); if (hdr_stg->resend_syn || hdr_stg->fastopen) /* The PARSE_ALL_HDR cb flag was turned on * to ensure that the previously written * options have reached the peer. * Those previously written option includes: * - Active side: resend_syn in ACK during syncookie * or * - Passive side: SYNACK during fastopen * * A valid packet has been received here after * the 3WHS, so the PARSE_ALL_HDR cb flag * can be cleared now. */ clear_parse_all_hdr_cb_flags(skops); if (hdr_stg->resend_syn && !active_fin_out.flags) /* Active side resent the syn option in ACK * because the server was in syncookie mode. * A valid packet has been received, so * clear header cb flags if there is no * more option to send. */ clear_hdr_cb_flags(skops); if (hdr_stg->fastopen && !passive_fin_out.flags) /* Passive side was in fastopen. * A valid packet has been received, so * the SYNACK has reached the peer. * Clear header cb flags if there is no more * option to send. */ clear_hdr_cb_flags(skops); if (th->fin) { struct bpf_test_option *fin_opt; int err; if (hdr_stg->active) fin_opt = &active_fin_in; else fin_opt = &passive_fin_in; err = load_option(skops, fin_opt, false); if (err && err != -ENOMSG) RET_CG_ERR(err); } return CG_OK; } SEC("sockops") int estab(struct bpf_sock_ops *skops) { int true_val = 1; switch (skops->op) { case BPF_SOCK_OPS_TCP_LISTEN_CB: bpf_setsockopt(skops, SOL_TCP, TCP_SAVE_SYN, &true_val, sizeof(true_val)); set_hdr_cb_flags(skops, BPF_SOCK_OPS_STATE_CB_FLAG); break; case BPF_SOCK_OPS_TCP_CONNECT_CB: set_hdr_cb_flags(skops, 0); break; case BPF_SOCK_OPS_PARSE_HDR_OPT_CB: return handle_parse_hdr(skops); case BPF_SOCK_OPS_HDR_OPT_LEN_CB: return handle_hdr_opt_len(skops); case BPF_SOCK_OPS_WRITE_HDR_OPT_CB: return handle_write_hdr_opt(skops); case BPF_SOCK_OPS_PASSIVE_ESTABLISHED_CB: return handle_passive_estab(skops); case BPF_SOCK_OPS_ACTIVE_ESTABLISHED_CB: return handle_active_estab(skops); } return CG_OK; } char _license[] SEC("license") = "GPL"; |