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 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553
|
/*
* Copyright (c) 2019 Nicira, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <config.h>
#include <ctype.h>
#include <errno.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netinet/ip6.h>
#include <netinet/icmp6.h>
#include <string.h>
#include "coverage.h"
#include "csum.h"
#include "ipf.h"
#include "latch.h"
#include "openvswitch/hmap.h"
#include "openvswitch/poll-loop.h"
#include "openvswitch/vlog.h"
#include "ovs-atomic.h"
#include "packets.h"
#include "util.h"
VLOG_DEFINE_THIS_MODULE(ipf);
COVERAGE_DEFINE(ipf_stuck_frag_list_purged);
COVERAGE_DEFINE(ipf_l3csum_err);
enum {
IPV4_PACKET_MAX_HDR_SIZE = 60,
IPV4_PACKET_MAX_SIZE = 65535,
IPV6_PACKET_MAX_DATA = 65535,
};
enum ipf_list_state {
IPF_LIST_STATE_UNUSED,
IPF_LIST_STATE_REASS_FAIL,
IPF_LIST_STATE_OTHER_SEEN,
IPF_LIST_STATE_FIRST_SEEN,
IPF_LIST_STATE_LAST_SEEN,
IPF_LIST_STATE_FIRST_LAST_SEEN,
IPF_LIST_STATE_COMPLETED,
IPF_LIST_STATE_NUM,
};
static char *ipf_state_name[IPF_LIST_STATE_NUM] =
{"unused", "reassemble fail", "other frag", "first frag", "last frag",
"first/last frag", "complete"};
enum ipf_list_type {
IPF_FRAG_COMPLETED_LIST,
IPF_FRAG_EXPIRY_LIST,
};
enum {
IPF_INVALID_IDX = -1,
IPF_V4_FRAG_SIZE_LBOUND = 400,
IPF_V4_FRAG_SIZE_MIN_DEF = 1200,
IPF_V6_FRAG_SIZE_LBOUND = 400, /* Useful for testing. */
IPF_V6_FRAG_SIZE_MIN_DEF = 1280,
IPF_MAX_FRAGS_DEFAULT = 1000,
IPF_NFRAG_UBOUND = 5000,
};
enum ipf_counter_type {
IPF_NFRAGS_ACCEPTED,
IPF_NFRAGS_COMPL_SENT,
IPF_NFRAGS_EXPD_SENT,
IPF_NFRAGS_TOO_SMALL,
IPF_NFRAGS_OVERLAP,
IPF_NFRAGS_PURGED,
IPF_NFRAGS_NUM_CNTS,
};
union ipf_addr {
ovs_be32 ipv4;
struct in6_addr ipv6;
};
/* Represents a single fragment; part of a list of fragments. */
struct ipf_frag {
struct dp_packet *pkt;
uint16_t start_data_byte;
uint16_t end_data_byte;
};
/* The key for a collection of fragments potentially making up an unfragmented
* packet. */
struct ipf_list_key {
/* ipf_list_key_hash() requires 'src_addr' and 'dst_addr' to be the first
* two members. */
union ipf_addr src_addr;
union ipf_addr dst_addr;
uint32_t recirc_id;
ovs_be32 ip_id; /* V6 is 32 bits. */
ovs_be16 dl_type;
uint16_t zone;
uint8_t nw_proto;
};
/* A collection of fragments potentially making up an unfragmented packet. */
struct ipf_list {
struct hmap_node node; /* In struct ipf's 'frag_lists'. */
struct ovs_list list_node; /* In struct ipf's 'frag_exp_list' or
* 'frag_complete_list'. */
struct ipf_frag *frag_list; /* List of fragments for this list. */
struct ipf_list_key key; /* The key for the fragemnt list. */
struct dp_packet *reass_execute_ctx; /* Reassembled packet. */
long long expiration; /* In milliseconds. */
int last_sent_idx; /* Last sent fragment idx. */
int last_inuse_idx; /* Last inuse fragment idx. */
int size; /* Fragment list size. */
uint8_t state; /* Frag list state; see ipf_list_state. */
};
/* Represents a reassambled packet which typically is passed through
* conntrack. */
struct reassembled_pkt {
struct ovs_list rp_list_node; /* In struct ipf's
* 'reassembled_pkt_list'. */
struct dp_packet *pkt;
struct ipf_list *list;
};
struct ipf {
/* The clean thread is used to clean up fragments in the 'ipf'
* module if packet batches are not longer be sent through its user. */
pthread_t ipf_clean_thread;
struct latch ipf_clean_thread_exit;
int max_v4_frag_list_size;
struct ovs_mutex ipf_lock; /* Protects all of the following. */
/* These contain 'struct ipf_list's. */
struct hmap frag_lists OVS_GUARDED;
struct ovs_list frag_exp_list OVS_GUARDED;
struct ovs_list frag_complete_list OVS_GUARDED;
/* Contains 'struct reassembled_pkt's. */
struct ovs_list reassembled_pkt_list OVS_GUARDED;
/* Used to allow disabling fragmentation reassembly. */
atomic_bool ifp_v4_enabled;
atomic_bool ifp_v6_enabled;
/* Will be clamped above 400 bytes; the value chosen should handle
* alg control packets of interest that use string encoding of mutable
* IP fields; meaning, the control packets should not be fragmented. */
atomic_uint min_v4_frag_size;
atomic_uint min_v6_frag_size;
/* Configurable maximum allowable fragments in process. */
atomic_uint nfrag_max;
/* Number of fragments in process. */
atomic_count nfrag;
atomic_uint64_t n4frag_cnt[IPF_NFRAGS_NUM_CNTS];
atomic_uint64_t n6frag_cnt[IPF_NFRAGS_NUM_CNTS];
};
static void
ipf_print_reass_packet(const char *es, const void *pkt)
{
static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10);
if (!VLOG_DROP_WARN(&rl)) {
struct ds ds = DS_EMPTY_INITIALIZER;
ds_put_hex_dump(&ds, pkt, 128, 0, false);
VLOG_WARN("%s\n%s", es, ds_cstr(&ds));
ds_destroy(&ds);
}
}
static void
ipf_count(struct ipf *ipf, bool v6, enum ipf_counter_type cntr)
{
atomic_count_inc64(v6 ? &ipf->n6frag_cnt[cntr] : &ipf->n4frag_cnt[cntr]);
}
static bool
ipf_get_v4_enabled(struct ipf *ipf)
{
bool ifp_v4_enabled_;
atomic_read_relaxed(&ipf->ifp_v4_enabled, &ifp_v4_enabled_);
return ifp_v4_enabled_;
}
static bool
ipf_get_v6_enabled(struct ipf *ipf)
{
bool ifp_v6_enabled_;
atomic_read_relaxed(&ipf->ifp_v6_enabled, &ifp_v6_enabled_);
return ifp_v6_enabled_;
}
static bool
ipf_get_enabled(struct ipf *ipf)
{
return ipf_get_v4_enabled(ipf) || ipf_get_v6_enabled(ipf);
}
static uint32_t
ipf_addr_hash_add(uint32_t hash, const union ipf_addr *addr)
{
BUILD_ASSERT_DECL(sizeof *addr % 4 == 0);
return hash_add_bytes32(hash, (const uint32_t *) addr, sizeof *addr);
}
/* Adds a list of fragments to the list tracking expiry of yet to be
* completed reassembled packets, hence subject to expirty. */
static void
ipf_expiry_list_add(struct ovs_list *frag_exp_list, struct ipf_list *ipf_list,
long long now)
/* OVS_REQUIRES(ipf->ipf_lock) */
{
enum {
IPF_FRAG_LIST_TIMEOUT = 15000,
};
ipf_list->expiration = now + IPF_FRAG_LIST_TIMEOUT;
ovs_list_push_back(frag_exp_list, &ipf_list->list_node);
}
/* Adds a list of fragments to the list of completed packets, which will be
* subsequently transmitted. */
static void
ipf_completed_list_add(struct ovs_list *frag_complete_list,
struct ipf_list *ipf_list)
/* OVS_REQUIRES(ipf_lock) */
{
ovs_list_push_back(frag_complete_list, &ipf_list->list_node);
}
/* Adds a reassmebled packet to the list of reassembled packets, awaiting some
* processing, such as being sent through conntrack. */
static void
ipf_reassembled_list_add(struct ovs_list *reassembled_pkt_list,
struct reassembled_pkt *rp)
/* OVS_REQUIRES(ipf_lock) */
{
ovs_list_push_back(reassembled_pkt_list, &rp->rp_list_node);
}
/* Removed a frag list from tracking datastructures and frees list heap
* memory. */
static void
ipf_list_clean(struct hmap *frag_lists,
struct ipf_list *ipf_list)
/* OVS_REQUIRES(ipf_lock) */
{
ovs_list_remove(&ipf_list->list_node);
hmap_remove(frag_lists, &ipf_list->node);
free(ipf_list->frag_list);
free(ipf_list);
}
/* Removed a frag list sitting on the expiry list from tracking
* datastructures and frees list heap memory. */
static void
ipf_expiry_list_clean(struct hmap *frag_lists,
struct ipf_list *ipf_list)
/* OVS_REQUIRES(ipf_lock) */
{
ipf_list_clean(frag_lists, ipf_list);
}
/* Removed a frag list sitting on the completed list from tracking
* datastructures and frees list heap memory. */
static void
ipf_completed_list_clean(struct hmap *frag_lists,
struct ipf_list *ipf_list)
/* OVS_REQUIRES(ipf_lock) */
{
ipf_list_clean(frag_lists, ipf_list);
}
static void
ipf_expiry_list_remove(struct ipf_list *ipf_list)
/* OVS_REQUIRES(ipf_lock) */
{
ovs_list_remove(&ipf_list->list_node);
}
static void
ipf_reassembled_list_remove(struct reassembled_pkt *rp)
/* OVS_REQUIRES(ipf_lock) */
{
ovs_list_remove(&rp->rp_list_node);
}
/* Symmetric */
static uint32_t
ipf_list_key_hash(const struct ipf_list_key *key, uint32_t basis)
{
uint32_t hsrc, hdst, hash;
hsrc = hdst = basis;
hsrc = ipf_addr_hash_add(hsrc, &key->src_addr);
hdst = ipf_addr_hash_add(hdst, &key->dst_addr);
hash = hsrc ^ hdst;
/* Hash the rest of the key. */
return hash_words((uint32_t *) (&key->dst_addr + 1),
(uint32_t *) (key + 1) -
(uint32_t *) (&key->dst_addr + 1),
hash);
}
static bool
ipf_is_first_v4_frag(const struct dp_packet *pkt)
{
const struct ip_header *l3 = dp_packet_l3(pkt);
if (!(l3->ip_frag_off & htons(IP_FRAG_OFF_MASK)) &&
l3->ip_frag_off & htons(IP_MORE_FRAGMENTS)) {
return true;
}
return false;
}
static bool
ipf_is_last_v4_frag(const struct dp_packet *pkt)
{
const struct ip_header *l3 = dp_packet_l3(pkt);
if (l3->ip_frag_off & htons(IP_FRAG_OFF_MASK) &&
!(l3->ip_frag_off & htons(IP_MORE_FRAGMENTS))) {
return true;
}
return false;
}
static bool
ipf_is_v6_frag(ovs_be16 ip6f_offlg)
{
if (ip6f_offlg & (IP6F_OFF_MASK | IP6F_MORE_FRAG)) {
return true;
}
return false;
}
static bool
ipf_is_first_v6_frag(ovs_be16 ip6f_offlg)
{
if (!(ip6f_offlg & IP6F_OFF_MASK) &&
ip6f_offlg & IP6F_MORE_FRAG) {
return true;
}
return false;
}
static bool
ipf_is_last_v6_frag(ovs_be16 ip6f_offlg)
{
if ((ip6f_offlg & IP6F_OFF_MASK) &&
!(ip6f_offlg & IP6F_MORE_FRAG)) {
return true;
}
return false;
}
/* Checks for a completed packet collection of fragments. */
static bool
ipf_list_complete(const struct ipf_list *ipf_list)
/* OVS_REQUIRES(ipf_lock) */
{
for (int i = 1; i <= ipf_list->last_inuse_idx; i++) {
if (ipf_list->frag_list[i - 1].end_data_byte + 1
!= ipf_list->frag_list[i].start_data_byte) {
return false;
}
}
return true;
}
/* Runs O(n) for a sorted or almost sorted list. */
static void
ipf_sort(struct ipf_frag *frag_list, size_t last_idx)
/* OVS_REQUIRES(ipf_lock) */
{
for (int li = 1; li <= last_idx; li++) {
struct ipf_frag ipf_frag = frag_list[li];
int ci = li - 1;
while (ci >= 0 &&
frag_list[ci].start_data_byte > ipf_frag.start_data_byte) {
frag_list[ci + 1] = frag_list[ci];
ci--;
}
frag_list[ci + 1] = ipf_frag;
}
}
/* Called on a sorted complete list of v4 fragments to reassemble them into
* a single packet that can be processed, such as passing through conntrack.
*/
static struct dp_packet *
ipf_reassemble_v4_frags(struct ipf_list *ipf_list)
/* OVS_REQUIRES(ipf_lock) */
{
struct ipf_frag *frag_list = ipf_list->frag_list;
struct dp_packet *pkt = dp_packet_clone(frag_list[0].pkt);
dp_packet_set_size(pkt, dp_packet_size(pkt) - dp_packet_l2_pad_size(pkt));
struct ip_header *l3 = dp_packet_l3(pkt);
int len = ntohs(l3->ip_tot_len);
int orig_len = dp_packet_size(pkt);
int rest_len = frag_list[ipf_list->last_inuse_idx].end_data_byte -
frag_list[1].start_data_byte + 1;
if (orig_len + rest_len > IPV4_PACKET_MAX_SIZE) {
ipf_print_reass_packet(
"Unsupported big reassembled v4 packet; v4 hdr:", l3);
dp_packet_delete(pkt);
return NULL;
}
dp_packet_prealloc_tailroom(pkt, rest_len);
for (int i = 1; i <= ipf_list->last_inuse_idx; i++) {
size_t add_len = frag_list[i].end_data_byte -
frag_list[i].start_data_byte + 1;
const char *l4 = dp_packet_l4(frag_list[i].pkt);
dp_packet_put(pkt, l4, add_len);
}
len += rest_len;
l3 = dp_packet_l3(pkt);
ovs_be16 new_ip_frag_off = l3->ip_frag_off & ~htons(IP_MORE_FRAGMENTS);
if (dp_packet_hwol_tx_ip_csum(pkt)) {
dp_packet_ol_reset_ip_csum_good(pkt);
} else {
l3->ip_csum = recalc_csum16(l3->ip_csum, l3->ip_frag_off,
new_ip_frag_off);
l3->ip_csum = recalc_csum16(l3->ip_csum, l3->ip_tot_len, htons(len));
}
l3->ip_tot_len = htons(len);
l3->ip_frag_off = new_ip_frag_off;
dp_packet_set_l2_pad_size(pkt, 0);
return pkt;
}
/* Called on a sorted complete list of v6 fragments to reassemble them into
* a single packet that can be processed, such as passing through conntrack.
*/
static struct dp_packet *
ipf_reassemble_v6_frags(struct ipf_list *ipf_list)
/* OVS_REQUIRES(ipf_lock) */
{
struct ipf_frag *frag_list = ipf_list->frag_list;
struct dp_packet *pkt = dp_packet_clone(frag_list[0].pkt);
dp_packet_set_size(pkt, dp_packet_size(pkt) - dp_packet_l2_pad_size(pkt));
struct ovs_16aligned_ip6_hdr *l3 = dp_packet_l3(pkt);
int pl = ntohs(l3->ip6_plen) - sizeof(struct ovs_16aligned_ip6_frag);
int orig_len = dp_packet_size(pkt);
int rest_len = frag_list[ipf_list->last_inuse_idx].end_data_byte -
frag_list[1].start_data_byte + 1;
if (orig_len + rest_len > IPV6_PACKET_MAX_DATA) {
ipf_print_reass_packet(
"Unsupported big reassembled v6 packet; v6 hdr:", l3);
dp_packet_delete(pkt);
return NULL;
}
dp_packet_prealloc_tailroom(pkt, rest_len);
for (int i = 1; i <= ipf_list->last_inuse_idx; i++) {
size_t add_len = frag_list[i].end_data_byte -
frag_list[i].start_data_byte + 1;
const char *l4 = dp_packet_l4(frag_list[i].pkt);
dp_packet_put(pkt, l4, add_len);
}
pl += rest_len;
l3 = dp_packet_l3(pkt);
uint8_t nw_proto = l3->ip6_nxt;
uint8_t nw_frag = 0;
const void *data = l3 + 1;
size_t datasize = pl;
const struct ovs_16aligned_ip6_frag *frag_hdr;
if (!parse_ipv6_ext_hdrs(&data, &datasize, &nw_proto, &nw_frag, &frag_hdr,
NULL) || !nw_frag || !frag_hdr) {
ipf_print_reass_packet("Unparsed reassembled v6 packet; v6 hdr:", l3);
dp_packet_delete(pkt);
return NULL;
}
struct ovs_16aligned_ip6_frag *fh =
CONST_CAST(struct ovs_16aligned_ip6_frag *, frag_hdr);
fh->ip6f_offlg = 0;
l3->ip6_plen = htons(pl);
l3->ip6_ctlun.ip6_un1.ip6_un1_nxt = nw_proto;
dp_packet_set_l2_pad_size(pkt, 0);
return pkt;
}
/* Called when a frag list state transitions to another state. This is
* triggered by new fragment for the list being received. Returns a reassembled
* packet if this fragment has completed one. */
static struct reassembled_pkt *
ipf_list_state_transition(struct ipf *ipf, struct ipf_list *ipf_list,
bool ff, bool lf, bool v6)
OVS_REQUIRES(ipf->ipf_lock)
{
enum ipf_list_state curr_state = ipf_list->state;
struct reassembled_pkt *ret = NULL;
enum ipf_list_state next_state;
switch (curr_state) {
case IPF_LIST_STATE_UNUSED:
case IPF_LIST_STATE_OTHER_SEEN:
if (ff) {
next_state = IPF_LIST_STATE_FIRST_SEEN;
} else if (lf) {
next_state = IPF_LIST_STATE_LAST_SEEN;
} else {
next_state = IPF_LIST_STATE_OTHER_SEEN;
}
break;
case IPF_LIST_STATE_FIRST_SEEN:
if (lf) {
next_state = IPF_LIST_STATE_FIRST_LAST_SEEN;
} else {
next_state = IPF_LIST_STATE_FIRST_SEEN;
}
break;
case IPF_LIST_STATE_LAST_SEEN:
if (ff) {
next_state = IPF_LIST_STATE_FIRST_LAST_SEEN;
} else {
next_state = IPF_LIST_STATE_LAST_SEEN;
}
break;
case IPF_LIST_STATE_FIRST_LAST_SEEN:
next_state = IPF_LIST_STATE_FIRST_LAST_SEEN;
break;
case IPF_LIST_STATE_COMPLETED:
case IPF_LIST_STATE_REASS_FAIL:
case IPF_LIST_STATE_NUM:
default:
OVS_NOT_REACHED();
}
if (next_state == IPF_LIST_STATE_FIRST_LAST_SEEN) {
ipf_sort(ipf_list->frag_list, ipf_list->last_inuse_idx);
if (ipf_list_complete(ipf_list)) {
struct dp_packet *reass_pkt = v6
? ipf_reassemble_v6_frags(ipf_list)
: ipf_reassemble_v4_frags(ipf_list);
if (reass_pkt) {
struct reassembled_pkt *rp = xzalloc(sizeof *rp);
rp->pkt = reass_pkt;
rp->list = ipf_list;
ipf_reassembled_list_add(&ipf->reassembled_pkt_list, rp);
ipf_expiry_list_remove(ipf_list);
next_state = IPF_LIST_STATE_COMPLETED;
ret = rp;
} else {
next_state = IPF_LIST_STATE_REASS_FAIL;
}
}
}
ipf_list->state = next_state;
return ret;
}
/* Some sanity checks are redundant, but prudent, in case code paths for
* fragments change in future. The processing cost for fragments is not
* important. */
static bool
ipf_is_valid_v4_frag(struct ipf *ipf, struct dp_packet *pkt)
{
if (OVS_UNLIKELY(dp_packet_ip_checksum_bad(pkt))) {
COVERAGE_INC(ipf_l3csum_err);
goto invalid_pkt;
}
const struct eth_header *l2 = dp_packet_eth(pkt);
const struct ip_header *l3 = dp_packet_l3(pkt);
if (OVS_UNLIKELY(!l2 || !l3)) {
goto invalid_pkt;
}
size_t l3_size = dp_packet_l3_size(pkt);
if (OVS_UNLIKELY(l3_size < IP_HEADER_LEN)) {
goto invalid_pkt;
}
if (!IP_IS_FRAGMENT(l3->ip_frag_off)) {
return false;
}
uint16_t ip_tot_len = ntohs(l3->ip_tot_len);
if (OVS_UNLIKELY(ip_tot_len != l3_size)) {
goto invalid_pkt;
}
size_t ip_hdr_len = IP_IHL(l3->ip_ihl_ver) * 4;
if (OVS_UNLIKELY(ip_hdr_len < IP_HEADER_LEN)) {
goto invalid_pkt;
}
if (OVS_UNLIKELY(l3_size < ip_hdr_len)) {
goto invalid_pkt;
}
if (OVS_UNLIKELY(!dp_packet_ip_checksum_good(pkt)
&& csum(l3, ip_hdr_len) != 0)) {
COVERAGE_INC(ipf_l3csum_err);
goto invalid_pkt;
}
uint32_t min_v4_frag_size_;
atomic_read_relaxed(&ipf->min_v4_frag_size, &min_v4_frag_size_);
bool lf = ipf_is_last_v4_frag(pkt);
if (OVS_UNLIKELY(!lf && dp_packet_l3_size(pkt) < min_v4_frag_size_)) {
ipf_count(ipf, false, IPF_NFRAGS_TOO_SMALL);
goto invalid_pkt;
}
return true;
invalid_pkt:
pkt->md.ct_state = CS_INVALID;
return false;
}
static bool
ipf_v4_key_extract(struct dp_packet *pkt, ovs_be16 dl_type, uint16_t zone,
struct ipf_list_key *key, uint16_t *start_data_byte,
uint16_t *end_data_byte, bool *ff, bool *lf)
{
const struct ip_header *l3 = dp_packet_l3(pkt);
uint16_t ip_tot_len = ntohs(l3->ip_tot_len);
size_t ip_hdr_len = IP_IHL(l3->ip_ihl_ver) * 4;
*start_data_byte = ntohs(l3->ip_frag_off & htons(IP_FRAG_OFF_MASK)) * 8;
*end_data_byte = *start_data_byte + ip_tot_len - ip_hdr_len - 1;
*ff = ipf_is_first_v4_frag(pkt);
*lf = ipf_is_last_v4_frag(pkt);
memset(key, 0, sizeof *key);
key->ip_id = be16_to_be32(l3->ip_id);
key->dl_type = dl_type;
key->src_addr.ipv4 = get_16aligned_be32(&l3->ip_src);
key->dst_addr.ipv4 = get_16aligned_be32(&l3->ip_dst);
key->nw_proto = l3->ip_proto;
key->zone = zone;
key->recirc_id = pkt->md.recirc_id;
return true;
}
/* Some sanity checks are redundant, but prudent, in case code paths for
* fragments change in future. The processing cost for fragments is not
* important. */
static bool
ipf_is_valid_v6_frag(struct ipf *ipf, struct dp_packet *pkt)
{
const struct eth_header *l2 = dp_packet_eth(pkt);
const struct ovs_16aligned_ip6_hdr *l3 = dp_packet_l3(pkt);
const char *l4 = dp_packet_l4(pkt);
if (OVS_UNLIKELY(!l2 || !l3 || !l4)) {
goto invalid_pkt;
}
size_t l3_size = dp_packet_l3_size(pkt);
size_t l3_hdr_size = sizeof *l3;
if (OVS_UNLIKELY(l3_size < l3_hdr_size)) {
goto invalid_pkt;
}
uint8_t nw_frag = 0;
uint8_t nw_proto = l3->ip6_nxt;
const void *data = l3 + 1;
size_t datasize = l3_size - l3_hdr_size;
const struct ovs_16aligned_ip6_frag *frag_hdr;
if (!parse_ipv6_ext_hdrs(&data, &datasize, &nw_proto, &nw_frag,
&frag_hdr, NULL) || !nw_frag || !frag_hdr) {
return false;
}
int pl = ntohs(l3->ip6_plen);
if (OVS_UNLIKELY(pl + l3_hdr_size != l3_size)) {
goto invalid_pkt;
}
ovs_be16 ip6f_offlg = frag_hdr->ip6f_offlg;
if (OVS_UNLIKELY(!ipf_is_v6_frag(ip6f_offlg))) {
return false;
}
uint32_t min_v6_frag_size_;
atomic_read_relaxed(&ipf->min_v6_frag_size, &min_v6_frag_size_);
bool lf = ipf_is_last_v6_frag(ip6f_offlg);
if (OVS_UNLIKELY(!lf && dp_packet_l3_size(pkt) < min_v6_frag_size_)) {
ipf_count(ipf, true, IPF_NFRAGS_TOO_SMALL);
goto invalid_pkt;
}
return true;
invalid_pkt:
pkt->md.ct_state = CS_INVALID;
return false;
}
static void
ipf_v6_key_extract(struct dp_packet *pkt, ovs_be16 dl_type, uint16_t zone,
struct ipf_list_key *key, uint16_t *start_data_byte,
uint16_t *end_data_byte, bool *ff, bool *lf)
{
const struct ovs_16aligned_ip6_hdr *l3 = dp_packet_l3(pkt);
uint8_t nw_frag = 0;
uint8_t nw_proto = l3->ip6_nxt;
const void *data = l3 + 1;
size_t datasize = dp_packet_l3_size(pkt) - sizeof *l3;
const struct ovs_16aligned_ip6_frag *frag_hdr;
parse_ipv6_ext_hdrs(&data, &datasize, &nw_proto, &nw_frag, &frag_hdr,
NULL);
ovs_assert(nw_frag && frag_hdr);
ovs_be16 ip6f_offlg = frag_hdr->ip6f_offlg;
*start_data_byte = ntohs(ip6f_offlg & IP6F_OFF_MASK) +
sizeof (struct ovs_16aligned_ip6_frag);
*end_data_byte = *start_data_byte + dp_packet_l4_size(pkt) - 1;
*ff = ipf_is_first_v6_frag(ip6f_offlg);
*lf = ipf_is_last_v6_frag(ip6f_offlg);
memset(key, 0, sizeof *key);
key->ip_id = get_16aligned_be32(&frag_hdr->ip6f_ident);
key->dl_type = dl_type;
memcpy(&key->src_addr.ipv6, &l3->ip6_src, sizeof key->src_addr.ipv6);
/* We are not supporting parsing of the routing header to use as the
* dst address part of the key. */
memcpy(&key->dst_addr.ipv6, &l3->ip6_dst, sizeof key->dst_addr.ipv6);
key->nw_proto = 0; /* Not used for key for V6. */
key->zone = zone;
key->recirc_id = pkt->md.recirc_id;
}
static bool
ipf_list_key_eq(const struct ipf_list_key *key1,
const struct ipf_list_key *key2)
/* OVS_REQUIRES(ipf_lock) */
{
if (!memcmp(&key1->src_addr, &key2->src_addr, sizeof key1->src_addr) &&
!memcmp(&key1->dst_addr, &key2->dst_addr, sizeof key1->dst_addr) &&
key1->dl_type == key2->dl_type &&
key1->ip_id == key2->ip_id &&
key1->zone == key2->zone &&
key1->nw_proto == key2->nw_proto &&
key1->recirc_id == key2->recirc_id) {
return true;
}
return false;
}
static struct ipf_list *
ipf_list_key_lookup(struct ipf *ipf, const struct ipf_list_key *key,
uint32_t hash)
OVS_REQUIRES(ipf->ipf_lock)
{
struct ipf_list *ipf_list;
HMAP_FOR_EACH_WITH_HASH (ipf_list, node, hash, &ipf->frag_lists) {
if (ipf_list_key_eq(&ipf_list->key, key)) {
return ipf_list;
}
}
return NULL;
}
static bool
ipf_is_frag_duped(const struct ipf_frag *frag_list, int last_inuse_idx,
size_t start_data_byte, size_t end_data_byte)
/* OVS_REQUIRES(ipf_lock) */
{
for (int i = 0; i <= last_inuse_idx; i++) {
if ((start_data_byte >= frag_list[i].start_data_byte &&
start_data_byte <= frag_list[i].end_data_byte) ||
(end_data_byte >= frag_list[i].start_data_byte &&
end_data_byte <= frag_list[i].end_data_byte)) {
return true;
}
}
return false;
}
/* Adds a fragment to a list of fragments, if the fragment is not a
* duplicate. If the fragment is a duplicate, that fragment is marked
* invalid to avoid the work that conntrack would do to mark the fragment
* as invalid, which it will in all cases. */
static bool
ipf_process_frag(struct ipf *ipf, struct ipf_list *ipf_list,
struct dp_packet *pkt, uint16_t start_data_byte,
uint16_t end_data_byte, bool ff, bool lf, bool v6,
struct reassembled_pkt **rp)
OVS_REQUIRES(ipf->ipf_lock)
{
bool duped_frag = ipf_is_frag_duped(ipf_list->frag_list,
ipf_list->last_inuse_idx, start_data_byte, end_data_byte);
int last_inuse_idx = ipf_list->last_inuse_idx;
if (!duped_frag) {
if (last_inuse_idx < ipf_list->size - 1) {
/* In the case of dpdk, it would be unfortunate if we had
* to create a clone fragment outside the dpdk mp due to the
* mempool size being too limited. We will otherwise need to
* recommend not setting the mempool number of buffers too low
* and also clamp the number of fragments. */
struct ipf_frag *frag = &ipf_list->frag_list[last_inuse_idx + 1];
frag->pkt = pkt;
frag->start_data_byte = start_data_byte;
frag->end_data_byte = end_data_byte;
ipf_list->last_inuse_idx++;
atomic_count_inc(&ipf->nfrag);
ipf_count(ipf, v6, IPF_NFRAGS_ACCEPTED);
*rp = ipf_list_state_transition(ipf, ipf_list, ff, lf, v6);
} else {
OVS_NOT_REACHED();
}
} else {
ipf_count(ipf, v6, IPF_NFRAGS_OVERLAP);
pkt->md.ct_state = CS_INVALID;
return false;
}
return true;
}
static void
ipf_list_init(struct ipf_list *ipf_list, struct ipf_list_key *key,
int max_frag_list_size)
{
ipf_list->key = *key;
ipf_list->last_inuse_idx = IPF_INVALID_IDX;
ipf_list->last_sent_idx = IPF_INVALID_IDX;
ipf_list->reass_execute_ctx = NULL;
ipf_list->state = IPF_LIST_STATE_UNUSED;
ipf_list->size = max_frag_list_size;
ipf_list->frag_list
= xzalloc(ipf_list->size * sizeof *ipf_list->frag_list);
}
/* Generates a fragment list key from a well formed fragment and either starts
* a new fragment list or increases the size of the existing fragment list,
* while checking if the maximum supported fragements are supported or the
* list size is impossibly big. Calls 'ipf_process_frag()' to add a fragment
* to a list of fragemnts. */
static bool
ipf_handle_frag(struct ipf *ipf, struct dp_packet *pkt, ovs_be16 dl_type,
uint16_t zone, long long now, uint32_t hash_basis,
struct reassembled_pkt **rp)
OVS_REQUIRES(ipf->ipf_lock)
{
struct ipf_list_key key;
/* Initialize 4 variables for some versions of GCC. */
uint16_t start_data_byte = 0;
uint16_t end_data_byte = 0;
bool ff = false;
bool lf = false;
bool v6 = dl_type == htons(ETH_TYPE_IPV6);
if (v6 && ipf_get_v6_enabled(ipf)) {
ipf_v6_key_extract(pkt, dl_type, zone, &key, &start_data_byte,
&end_data_byte, &ff, &lf);
} else if (!v6 && ipf_get_v4_enabled(ipf)) {
ipf_v4_key_extract(pkt, dl_type, zone, &key, &start_data_byte,
&end_data_byte, &ff, &lf);
} else {
return false;
}
unsigned int nfrag_max;
atomic_read_relaxed(&ipf->nfrag_max, &nfrag_max);
if (atomic_count_get(&ipf->nfrag) >= nfrag_max) {
return false;
}
uint32_t hash = ipf_list_key_hash(&key, hash_basis);
struct ipf_list *ipf_list = ipf_list_key_lookup(ipf, &key, hash);
enum {
IPF_FRAG_LIST_MIN_INCREMENT = 4,
IPF_IPV6_MAX_FRAG_LIST_SIZE = 65535,
};
int max_frag_list_size;
if (v6) {
/* Because the calculation with extension headers is variable,
* we don't calculate a hard maximum fragment list size upfront. The
* fragment list size is practically limited by the code, however. */
max_frag_list_size = IPF_IPV6_MAX_FRAG_LIST_SIZE;
} else {
max_frag_list_size = ipf->max_v4_frag_list_size;
}
if (!ipf_list) {
ipf_list = xmalloc(sizeof *ipf_list);
ipf_list_init(ipf_list, &key,
MIN(max_frag_list_size, IPF_FRAG_LIST_MIN_INCREMENT));
hmap_insert(&ipf->frag_lists, &ipf_list->node, hash);
ipf_expiry_list_add(&ipf->frag_exp_list, ipf_list, now);
} else if (ipf_list->state == IPF_LIST_STATE_REASS_FAIL ||
ipf_list->state == IPF_LIST_STATE_COMPLETED) {
/* Bail out as early as possible. */
return false;
} else if (ipf_list->last_inuse_idx + 1 >= ipf_list->size) {
int increment = MIN(IPF_FRAG_LIST_MIN_INCREMENT,
max_frag_list_size - ipf_list->size);
/* Enforce limit. */
if (increment > 0) {
ipf_list->frag_list =
xrealloc(ipf_list->frag_list, (ipf_list->size + increment) *
sizeof *ipf_list->frag_list);
ipf_list->size += increment;
} else {
return false;
}
}
return ipf_process_frag(ipf, ipf_list, pkt, start_data_byte,
end_data_byte, ff, lf, v6, rp);
}
/* Filters out fragments from a batch of fragments and adjust the batch. */
static void
ipf_extract_frags_from_batch(struct ipf *ipf, struct dp_packet_batch *pb,
ovs_be16 dl_type, uint16_t zone, long long now,
uint32_t hash_basis)
{
const size_t pb_cnt = dp_packet_batch_size(pb);
int pb_idx; /* Index in a packet batch. */
struct dp_packet *pkt;
DP_PACKET_BATCH_REFILL_FOR_EACH (pb_idx, pb_cnt, pkt, pb) {
if (OVS_UNLIKELY((dl_type == htons(ETH_TYPE_IP) &&
ipf_is_valid_v4_frag(ipf, pkt))
||
(dl_type == htons(ETH_TYPE_IPV6) &&
ipf_is_valid_v6_frag(ipf, pkt)))) {
struct reassembled_pkt *rp = NULL;
ovs_mutex_lock(&ipf->ipf_lock);
if (!ipf_handle_frag(ipf, pkt, dl_type, zone, now, hash_basis,
&rp)) {
dp_packet_batch_refill(pb, pkt, pb_idx);
} else {
if (rp && !dp_packet_batch_is_full(pb)) {
dp_packet_batch_refill(pb, rp->pkt, pb_idx);
rp->list->reass_execute_ctx = rp->pkt;
}
}
ovs_mutex_unlock(&ipf->ipf_lock);
} else {
dp_packet_batch_refill(pb, pkt, pb_idx);
}
}
}
/* In case of DPDK, a memory source check is done, as DPDK memory pool
* management has trouble dealing with multiple source types. The
* check_source paramater is used to indicate when this check is needed. */
static bool
ipf_dp_packet_batch_add(struct dp_packet_batch *pb , struct dp_packet *pkt,
bool check_source OVS_UNUSED)
{
#ifdef DPDK_NETDEV
if ((dp_packet_batch_is_full(pb)) ||
/* DPDK cannot handle multiple sources in a batch. */
(check_source && !dp_packet_batch_is_empty(pb)
&& pb->packets[0]->source != pkt->source)) {
#else
if (dp_packet_batch_is_full(pb)) {
#endif
return false;
}
dp_packet_batch_add(pb, pkt);
return true;
}
/* This would be used in rare cases where a list cannot be sent. One rare
* reason known right now is a mempool source check, which exists due to DPDK
* support, where packets are no longer being received on any port with a
* source matching the fragment. Another reason is a race where all
* conntrack rules are unconfigured when some fragments are yet to be
* flushed.
*
* Returns true if the list was purged. */
static bool
ipf_purge_list_check(struct ipf *ipf, struct ipf_list *ipf_list,
long long now)
OVS_REQUIRES(ipf->ipf_lock)
{
enum {
IPF_FRAG_LIST_PURGE_TIME_ADJ = 10000
};
if (now < ipf_list->expiration + IPF_FRAG_LIST_PURGE_TIME_ADJ) {
return false;
}
while (ipf_list->last_sent_idx < ipf_list->last_inuse_idx) {
struct dp_packet * pkt
= ipf_list->frag_list[ipf_list->last_sent_idx + 1].pkt;
dp_packet_delete(pkt);
atomic_count_dec(&ipf->nfrag);
COVERAGE_INC(ipf_stuck_frag_list_purged);
ipf_count(ipf, ipf_list->key.dl_type == htons(ETH_TYPE_IPV6),
IPF_NFRAGS_PURGED);
ipf_list->last_sent_idx++;
}
return true;
}
/* Does the packet batch management and common accounting work associated
* with 'ipf_send_completed_frags()' and 'ipf_send_expired_frags()'. */
static bool
ipf_send_frags_in_list(struct ipf *ipf, struct ipf_list *ipf_list,
struct dp_packet_batch *pb,
enum ipf_list_type list_type, bool v6, long long now)
OVS_REQUIRES(ipf->ipf_lock)
{
if (ipf_purge_list_check(ipf, ipf_list, now)) {
return true;
}
while (ipf_list->last_sent_idx < ipf_list->last_inuse_idx) {
struct dp_packet *pkt
= ipf_list->frag_list[ipf_list->last_sent_idx + 1].pkt;
if (ipf_dp_packet_batch_add(pb, pkt, true)) {
ipf_list->last_sent_idx++;
atomic_count_dec(&ipf->nfrag);
if (list_type == IPF_FRAG_COMPLETED_LIST) {
ipf_count(ipf, v6, IPF_NFRAGS_COMPL_SENT);
} else {
ipf_count(ipf, v6, IPF_NFRAGS_EXPD_SENT);
pkt->md.ct_state = CS_INVALID;
}
if (ipf_list->last_sent_idx == ipf_list->last_inuse_idx) {
return true;
}
} else {
return false;
}
}
OVS_NOT_REACHED();
}
/* Adds fragments associated with a completed fragment list to a packet batch
* to be processed by the calling application, typically conntrack. Also
* cleans up the list context when it is empty.*/
static void
ipf_send_completed_frags(struct ipf *ipf, struct dp_packet_batch *pb,
long long now, bool v6)
{
if (ovs_list_is_empty(&ipf->frag_complete_list)) {
return;
}
ovs_mutex_lock(&ipf->ipf_lock);
struct ipf_list *ipf_list;
LIST_FOR_EACH_SAFE (ipf_list, list_node, &ipf->frag_complete_list) {
if ((ipf_list->key.dl_type == htons(ETH_TYPE_IPV6)) != v6) {
continue;
}
if (ipf_send_frags_in_list(ipf, ipf_list, pb, IPF_FRAG_COMPLETED_LIST,
v6, now)) {
ipf_completed_list_clean(&ipf->frag_lists, ipf_list);
} else {
break;
}
}
ovs_mutex_unlock(&ipf->ipf_lock);
}
/* Conservatively adds fragments associated with a expired fragment list to
* a packet batch to be processed by the calling application, typically
* conntrack. Also cleans up the list context when it is empty.*/
static void
ipf_send_expired_frags(struct ipf *ipf, struct dp_packet_batch *pb,
long long now, bool v6)
{
enum {
/* Very conservative, due to DOS probability. */
IPF_FRAG_LIST_MAX_EXPIRED = 1,
};
if (ovs_list_is_empty(&ipf->frag_exp_list)) {
return;
}
ovs_mutex_lock(&ipf->ipf_lock);
struct ipf_list *ipf_list;
size_t lists_removed = 0;
LIST_FOR_EACH_SAFE (ipf_list, list_node, &ipf->frag_exp_list) {
if ((ipf_list->key.dl_type == htons(ETH_TYPE_IPV6)) != v6) {
continue;
}
if (now <= ipf_list->expiration ||
lists_removed >= IPF_FRAG_LIST_MAX_EXPIRED) {
break;
}
if (ipf_send_frags_in_list(ipf, ipf_list, pb, IPF_FRAG_EXPIRY_LIST,
v6, now)) {
ipf_expiry_list_clean(&ipf->frag_lists, ipf_list);
lists_removed++;
} else {
break;
}
}
ovs_mutex_unlock(&ipf->ipf_lock);
}
/* Adds a reassmebled packet to a packet batch to be processed by the caller.
*/
static void
ipf_execute_reass_pkts(struct ipf *ipf, struct dp_packet_batch *pb,
ovs_be16 dl_type)
{
if (ovs_list_is_empty(&ipf->reassembled_pkt_list)) {
return;
}
ovs_mutex_lock(&ipf->ipf_lock);
struct reassembled_pkt *rp;
LIST_FOR_EACH_SAFE (rp, rp_list_node, &ipf->reassembled_pkt_list) {
if (!rp->list->reass_execute_ctx &&
rp->list->key.dl_type == dl_type &&
ipf_dp_packet_batch_add(pb, rp->pkt, false)) {
rp->list->reass_execute_ctx = rp->pkt;
}
}
ovs_mutex_unlock(&ipf->ipf_lock);
}
/* Checks for reassembled packets post processing by conntrack and edits the
* fragments if needed based on what conntrack decided. */
static void
ipf_post_execute_reass_pkts(struct ipf *ipf,
struct dp_packet_batch *pb, bool v6)
{
if (ovs_list_is_empty(&ipf->reassembled_pkt_list)) {
return;
}
ovs_mutex_lock(&ipf->ipf_lock);
struct reassembled_pkt *rp;
LIST_FOR_EACH_SAFE (rp, rp_list_node, &ipf->reassembled_pkt_list) {
const size_t pb_cnt = dp_packet_batch_size(pb);
int pb_idx;
struct dp_packet *pkt;
/* Inner batch loop is constant time since batch size is <=
* NETDEV_MAX_BURST. */
DP_PACKET_BATCH_REFILL_FOR_EACH (pb_idx, pb_cnt, pkt, pb) {
if (rp && pkt == rp->list->reass_execute_ctx) {
const struct ipf_frag *frag_0 = &rp->list->frag_list[0];
void *l4_frag = dp_packet_l4(frag_0->pkt);
void *l4_reass = dp_packet_l4(pkt);
memcpy(l4_frag, l4_reass, dp_packet_l4_size(frag_0->pkt));
for (int i = 0; i <= rp->list->last_inuse_idx; i++) {
const struct ipf_frag *frag_i = &rp->list->frag_list[i];
frag_i->pkt->md.ct_label = pkt->md.ct_label;
frag_i->pkt->md.ct_mark = pkt->md.ct_mark;
frag_i->pkt->md.ct_state = pkt->md.ct_state;
frag_i->pkt->md.ct_zone = pkt->md.ct_zone;
frag_i->pkt->md.ct_orig_tuple_ipv6 =
pkt->md.ct_orig_tuple_ipv6;
if (pkt->md.ct_orig_tuple_ipv6) {
frag_i->pkt->md.ct_orig_tuple.ipv6 =
pkt->md.ct_orig_tuple.ipv6;
} else {
frag_i->pkt->md.ct_orig_tuple.ipv4 =
pkt->md.ct_orig_tuple.ipv4;
}
if (v6) {
struct ovs_16aligned_ip6_hdr *l3_frag
= dp_packet_l3(frag_i->pkt);
struct ovs_16aligned_ip6_hdr *l3_reass
= dp_packet_l3(pkt);
l3_frag->ip6_src = l3_reass->ip6_src;
l3_frag->ip6_dst = l3_reass->ip6_dst;
} else {
struct ip_header *l3_frag = dp_packet_l3(frag_i->pkt);
struct ip_header *l3_reass = dp_packet_l3(pkt);
if (dp_packet_hwol_tx_ip_csum(frag_i->pkt)) {
dp_packet_ol_reset_ip_csum_good(frag_i->pkt);
} else {
ovs_be32 reass_ip =
get_16aligned_be32(&l3_reass->ip_src);
ovs_be32 frag_ip =
get_16aligned_be32(&l3_frag->ip_src);
l3_frag->ip_csum = recalc_csum32(l3_frag->ip_csum,
frag_ip,
reass_ip);
reass_ip = get_16aligned_be32(&l3_reass->ip_dst);
frag_ip = get_16aligned_be32(&l3_frag->ip_dst);
l3_frag->ip_csum = recalc_csum32(l3_frag->ip_csum,
frag_ip,
reass_ip);
}
l3_frag->ip_src = l3_reass->ip_src;
l3_frag->ip_dst = l3_reass->ip_dst;
}
}
ipf_completed_list_add(&ipf->frag_complete_list, rp->list);
ipf_reassembled_list_remove(rp);
dp_packet_delete(rp->pkt);
free(rp);
rp = NULL;
} else {
dp_packet_batch_refill(pb, pkt, pb_idx);
}
}
}
ovs_mutex_unlock(&ipf->ipf_lock);
}
/* Extracts any fragments from the batch and reassembles them when a
* complete packet is received. Completed packets are attempted to
* be added to the batch to be sent through conntrack. */
void
ipf_preprocess_conntrack(struct ipf *ipf, struct dp_packet_batch *pb,
long long now, ovs_be16 dl_type, uint16_t zone,
uint32_t hash_basis)
{
if (ipf_get_enabled(ipf)) {
ipf_extract_frags_from_batch(ipf, pb, dl_type, zone, now, hash_basis);
}
if (ipf_get_enabled(ipf) || atomic_count_get(&ipf->nfrag)) {
ipf_execute_reass_pkts(ipf, pb, dl_type);
}
}
/* Updates fragments based on the processing of the reassembled packet sent
* through conntrack and adds these fragments to any batches seen. Expired
* fragments are marked as invalid and also added to the batches seen
* with low priority. Reassembled packets are freed. */
void
ipf_postprocess_conntrack(struct ipf *ipf, struct dp_packet_batch *pb,
long long now, ovs_be16 dl_type)
{
if (ipf_get_enabled(ipf) || atomic_count_get(&ipf->nfrag)) {
bool v6 = dl_type == htons(ETH_TYPE_IPV6);
ipf_post_execute_reass_pkts(ipf, pb, v6);
ipf_send_completed_frags(ipf, pb, now, v6);
ipf_send_expired_frags(ipf, pb, now, v6);
}
}
static void *
ipf_clean_thread_main(void *f)
{
struct ipf *ipf = f;
enum {
IPF_FRAG_LIST_CLEAN_TIMEOUT = 60000,
};
while (!latch_is_set(&ipf->ipf_clean_thread_exit)) {
long long now = time_msec();
if (!ovs_list_is_empty(&ipf->frag_exp_list) ||
!ovs_list_is_empty(&ipf->frag_complete_list)) {
ovs_mutex_lock(&ipf->ipf_lock);
struct ipf_list *ipf_list;
LIST_FOR_EACH_SAFE (ipf_list, list_node,
&ipf->frag_exp_list) {
if (ipf_purge_list_check(ipf, ipf_list, now)) {
ipf_expiry_list_clean(&ipf->frag_lists, ipf_list);
}
}
LIST_FOR_EACH_SAFE (ipf_list, list_node,
&ipf->frag_complete_list) {
if (ipf_purge_list_check(ipf, ipf_list, now)) {
ipf_completed_list_clean(&ipf->frag_lists, ipf_list);
}
}
ovs_mutex_unlock(&ipf->ipf_lock);
}
poll_timer_wait_until(now + IPF_FRAG_LIST_CLEAN_TIMEOUT);
latch_wait(&ipf->ipf_clean_thread_exit);
poll_block();
}
return NULL;
}
struct ipf *
ipf_init(void)
{
struct ipf *ipf = xzalloc(sizeof *ipf);
ovs_mutex_init_adaptive(&ipf->ipf_lock);
ovs_mutex_lock(&ipf->ipf_lock);
hmap_init(&ipf->frag_lists);
ovs_list_init(&ipf->frag_exp_list);
ovs_list_init(&ipf->frag_complete_list);
ovs_list_init(&ipf->reassembled_pkt_list);
atomic_init(&ipf->min_v4_frag_size, IPF_V4_FRAG_SIZE_MIN_DEF);
atomic_init(&ipf->min_v6_frag_size, IPF_V6_FRAG_SIZE_MIN_DEF);
ipf->max_v4_frag_list_size = DIV_ROUND_UP(
IPV4_PACKET_MAX_SIZE - IPV4_PACKET_MAX_HDR_SIZE,
ipf->min_v4_frag_size - IPV4_PACKET_MAX_HDR_SIZE);
ovs_mutex_unlock(&ipf->ipf_lock);
atomic_count_init(&ipf->nfrag, 0);
for (size_t i = 0; i < IPF_NFRAGS_NUM_CNTS; i++) {
atomic_init(&ipf->n4frag_cnt[i], 0);
atomic_init(&ipf->n6frag_cnt[i], 0);
}
atomic_init(&ipf->nfrag_max, IPF_MAX_FRAGS_DEFAULT);
atomic_init(&ipf->ifp_v4_enabled, true);
atomic_init(&ipf->ifp_v6_enabled, true);
latch_init(&ipf->ipf_clean_thread_exit);
ipf->ipf_clean_thread = ovs_thread_create("ipf_clean",
ipf_clean_thread_main, ipf);
return ipf;
}
void
ipf_destroy(struct ipf *ipf)
{
ovs_mutex_lock(&ipf->ipf_lock);
latch_set(&ipf->ipf_clean_thread_exit);
pthread_join(ipf->ipf_clean_thread, NULL);
latch_destroy(&ipf->ipf_clean_thread_exit);
struct ipf_list *ipf_list;
HMAP_FOR_EACH_POP (ipf_list, node, &ipf->frag_lists) {
while (ipf_list->last_sent_idx < ipf_list->last_inuse_idx) {
struct dp_packet *pkt
= ipf_list->frag_list[ipf_list->last_sent_idx + 1].pkt;
dp_packet_delete(pkt);
atomic_count_dec(&ipf->nfrag);
ipf_list->last_sent_idx++;
}
free(ipf_list->frag_list);
free(ipf_list);
}
if (atomic_count_get(&ipf->nfrag)) {
VLOG_WARN("ipf destroy with non-zero fragment count. ");
}
struct reassembled_pkt *rp;
LIST_FOR_EACH_POP (rp, rp_list_node, &ipf->reassembled_pkt_list) {
dp_packet_delete(rp->pkt);
free(rp);
}
hmap_destroy(&ipf->frag_lists);
ovs_list_poison(&ipf->frag_exp_list);
ovs_list_poison(&ipf->frag_complete_list);
ovs_list_poison(&ipf->reassembled_pkt_list);
ovs_mutex_unlock(&ipf->ipf_lock);
ovs_mutex_destroy(&ipf->ipf_lock);
free(ipf);
}
int
ipf_set_enabled(struct ipf *ipf, bool v6, bool enable)
{
atomic_store_relaxed(v6 ? &ipf->ifp_v6_enabled : &ipf->ifp_v4_enabled,
enable);
return 0;
}
int
ipf_set_min_frag(struct ipf *ipf, bool v6, uint32_t value)
{
/* If the user specifies an unreasonably large number, fragmentation
* will not work well but it will not blow up. */
if (value < (v6 ? IPF_V6_FRAG_SIZE_LBOUND : IPF_V4_FRAG_SIZE_LBOUND)) {
return 1;
}
ovs_mutex_lock(&ipf->ipf_lock);
if (v6) {
atomic_store_relaxed(&ipf->min_v6_frag_size, value);
} else {
atomic_store_relaxed(&ipf->min_v4_frag_size, value);
ipf->max_v4_frag_list_size = DIV_ROUND_UP(
IPV4_PACKET_MAX_SIZE - IPV4_PACKET_MAX_HDR_SIZE,
ipf->min_v4_frag_size - IPV4_PACKET_MAX_HDR_SIZE);
}
ovs_mutex_unlock(&ipf->ipf_lock);
return 0;
}
int
ipf_set_max_nfrags(struct ipf *ipf, uint32_t value)
{
if (value > IPF_NFRAG_UBOUND) {
return 1;
}
atomic_store_relaxed(&ipf->nfrag_max, value);
return 0;
}
int
ipf_get_status(struct ipf *ipf, struct ipf_status *ipf_status)
{
ipf_status->nfrag = atomic_count_get(&ipf->nfrag);
atomic_read_relaxed(&ipf->nfrag_max, &ipf_status->nfrag_max);
atomic_read_relaxed(&ipf->ifp_v4_enabled, &ipf_status->v4.enabled);
atomic_read_relaxed(&ipf->min_v4_frag_size,
&ipf_status->v4.min_frag_size);
atomic_read_relaxed(&ipf->n4frag_cnt[IPF_NFRAGS_ACCEPTED],
&ipf_status->v4.nfrag_accepted);
atomic_read_relaxed(&ipf->n4frag_cnt[IPF_NFRAGS_COMPL_SENT],
&ipf_status->v4.nfrag_completed_sent);
atomic_read_relaxed(&ipf->n4frag_cnt[IPF_NFRAGS_EXPD_SENT],
&ipf_status->v4.nfrag_expired_sent);
atomic_read_relaxed(&ipf->n4frag_cnt[IPF_NFRAGS_TOO_SMALL],
&ipf_status->v4.nfrag_too_small);
atomic_read_relaxed(&ipf->n4frag_cnt[IPF_NFRAGS_OVERLAP],
&ipf_status->v4.nfrag_overlap);
atomic_read_relaxed(&ipf->n4frag_cnt[IPF_NFRAGS_PURGED],
&ipf_status->v4.nfrag_purged);
atomic_read_relaxed(&ipf->ifp_v6_enabled, &ipf_status->v6.enabled);
atomic_read_relaxed(&ipf->min_v6_frag_size,
&ipf_status->v6.min_frag_size);
atomic_read_relaxed(&ipf->n6frag_cnt[IPF_NFRAGS_ACCEPTED],
&ipf_status->v6.nfrag_accepted);
atomic_read_relaxed(&ipf->n6frag_cnt[IPF_NFRAGS_COMPL_SENT],
&ipf_status->v6.nfrag_completed_sent);
atomic_read_relaxed(&ipf->n6frag_cnt[IPF_NFRAGS_EXPD_SENT],
&ipf_status->v6.nfrag_expired_sent);
atomic_read_relaxed(&ipf->n6frag_cnt[IPF_NFRAGS_TOO_SMALL],
&ipf_status->v6.nfrag_too_small);
atomic_read_relaxed(&ipf->n6frag_cnt[IPF_NFRAGS_OVERLAP],
&ipf_status->v6.nfrag_overlap);
atomic_read_relaxed(&ipf->n6frag_cnt[IPF_NFRAGS_PURGED],
&ipf_status->v6.nfrag_purged);
return 0;
}
struct ipf_dump_ctx {
struct hmap_position bucket_pos;
};
/* Allocates an 'ipf_dump_ctx' to keep track of an hmap position. The
* caller must call ipf_dump_done() when dumping is finished. */
int
ipf_dump_start(struct ipf_dump_ctx **ipf_dump_ctx)
{
*ipf_dump_ctx = xzalloc(sizeof **ipf_dump_ctx);
return 0;
}
/* Creates a string representation of the state of an 'ipf_list' and puts
* it in 'ds'. */
static void
ipf_dump_create(const struct ipf_list *ipf_list, struct ds *ds)
{
ds_put_cstr(ds, "(");
if (ipf_list->key.dl_type == htons(ETH_TYPE_IP)) {
ds_put_format(ds, "src="IP_FMT",dst="IP_FMT",",
IP_ARGS(ipf_list->key.src_addr.ipv4),
IP_ARGS(ipf_list->key.dst_addr.ipv4));
} else {
ds_put_cstr(ds, "src=");
ipv6_format_addr(&ipf_list->key.src_addr.ipv6, ds);
ds_put_cstr(ds, ",dst=");
ipv6_format_addr(&ipf_list->key.dst_addr.ipv6, ds);
ds_put_cstr(ds, ",");
}
ds_put_format(ds, "recirc_id=%u,ip_id=%u,dl_type=0x%x,zone=%u,nw_proto=%u",
ipf_list->key.recirc_id, ntohl(ipf_list->key.ip_id),
ntohs(ipf_list->key.dl_type), ipf_list->key.zone,
ipf_list->key.nw_proto);
ds_put_format(ds, ",num_fragments=%u,state=%s",
ipf_list->last_inuse_idx + 1,
ipf_state_name[ipf_list->state]);
ds_put_cstr(ds, ")");
}
/* Finds the next ipf list starting from 'ipf_dump_ctx->bucket_pos' and uses
* ipf_dump_create() to create a string representation of the state of an
* ipf list, to which 'dump' is pointed to. Returns EOF when there are no
* more ipf lists. */
int
ipf_dump_next(struct ipf *ipf, struct ipf_dump_ctx *ipf_dump_ctx, char **dump)
{
ovs_mutex_lock(&ipf->ipf_lock);
struct hmap_node *node = hmap_at_position(&ipf->frag_lists,
&ipf_dump_ctx->bucket_pos);
if (!node) {
ovs_mutex_unlock(&ipf->ipf_lock);
return EOF;
} else {
struct ipf_list *ipf_list_;
INIT_CONTAINER(ipf_list_, node, node);
struct ipf_list ipf_list = *ipf_list_;
ovs_mutex_unlock(&ipf->ipf_lock);
struct ds ds = DS_EMPTY_INITIALIZER;
ipf_dump_create(&ipf_list, &ds);
*dump = ds_steal_cstr(&ds);
return 0;
}
}
/* Frees 'ipf_dump_ctx' allocated by ipf_dump_start(). */
int
ipf_dump_done(struct ipf_dump_ctx *ipf_dump_ctx)
{
free(ipf_dump_ctx);
return 0;
}
|