github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/test/syscalls/linux/packet_socket.cc (about) 1 // Copyright 2019 The gVisor Authors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 #include <arpa/inet.h> 16 #include <ifaddrs.h> 17 #include <linux/capability.h> 18 #include <linux/if_arp.h> 19 #include <linux/if_packet.h> 20 #include <net/ethernet.h> 21 #include <netinet/in.h> 22 #include <netinet/ip.h> 23 #include <netinet/udp.h> 24 #include <poll.h> 25 #include <sys/ioctl.h> 26 #include <sys/socket.h> 27 #include <sys/types.h> 28 #include <unistd.h> 29 30 #include "gtest/gtest.h" 31 #include "absl/base/internal/endian.h" 32 #include "test/syscalls/linux/socket_test_util.h" 33 #include "test/syscalls/linux/unix_domain_socket_test_util.h" 34 #include "test/util/capability_util.h" 35 #include "test/util/file_descriptor.h" 36 #include "test/util/test_util.h" 37 38 // Some of these tests involve sending packets via AF_PACKET sockets and the 39 // loopback interface. Because AF_PACKET circumvents so much of the networking 40 // stack, Linux sees these packets as "martian", i.e. they claim to be to/from 41 // localhost but don't have the usual associated data. Thus Linux drops them by 42 // default. You can see where this happens by following the code at: 43 // 44 // - net/ipv4/ip_input.c:ip_rcv_finish, which calls 45 // - net/ipv4/route.c:ip_route_input_noref, which calls 46 // - net/ipv4/route.c:ip_route_input_slow, which finds and drops martian 47 // packets. 48 // 49 // To tell Linux not to drop these packets, you need to tell it to accept our 50 // funny packets (which are completely valid and correct, but lack associated 51 // in-kernel data because we use AF_PACKET): 52 // 53 // echo 1 >> /proc/sys/net/ipv4/conf/lo/accept_local 54 // echo 1 >> /proc/sys/net/ipv4/conf/lo/route_localnet 55 // 56 // These tests require CAP_NET_RAW to run. 57 58 namespace gvisor { 59 namespace testing { 60 61 namespace { 62 63 using ::testing::AnyOf; 64 using ::testing::Eq; 65 66 constexpr char kMessage[] = "soweoneul malhaebwa"; 67 constexpr in_port_t kPort = 0x409c; // htons(40000) 68 69 // 70 // "Cooked" tests. Cooked AF_PACKET sockets do not contain link layer 71 // headers, and provide link layer destination/source information via a 72 // returned struct sockaddr_ll. 73 // 74 75 // Send kMessage via sock to loopback 76 void SendUDPMessage(int sock) { 77 struct sockaddr_in dest = {}; 78 dest.sin_port = kPort; 79 dest.sin_addr.s_addr = htonl(INADDR_LOOPBACK); 80 dest.sin_family = AF_INET; 81 EXPECT_THAT(sendto(sock, kMessage, sizeof(kMessage), 0, 82 reinterpret_cast<struct sockaddr*>(&dest), sizeof(dest)), 83 SyscallSucceedsWithValue(sizeof(kMessage))); 84 } 85 86 // Send an IP packet and make sure ETH_P_<something else> doesn't pick it up. 87 TEST(BasicCookedPacketTest, WrongType) { 88 if (!ASSERT_NO_ERRNO_AND_VALUE(HaveCapability(CAP_NET_RAW))) { 89 ASSERT_THAT(socket(AF_PACKET, SOCK_DGRAM, ETH_P_PUP), 90 SyscallFailsWithErrno(EPERM)); 91 GTEST_SKIP(); 92 } 93 94 FileDescriptor sock = ASSERT_NO_ERRNO_AND_VALUE( 95 Socket(AF_PACKET, SOCK_DGRAM, htons(ETH_P_PUP))); 96 97 // Let's use a simple IP payload: a UDP datagram. 98 FileDescriptor udp_sock = 99 ASSERT_NO_ERRNO_AND_VALUE(Socket(AF_INET, SOCK_DGRAM, 0)); 100 SendUDPMessage(udp_sock.get()); 101 102 // Wait and make sure the socket never becomes readable. 103 struct pollfd pfd = {}; 104 pfd.fd = sock.get(); 105 pfd.events = POLLIN; 106 EXPECT_THAT(RetryEINTR(poll)(&pfd, 1, 1000), SyscallSucceedsWithValue(0)); 107 } 108 109 // Tests for "cooked" (SOCK_DGRAM) packet(7) sockets. 110 class CookedPacketTest : public ::testing::TestWithParam<int> { 111 protected: 112 // Creates a socket to be used in tests. 113 void SetUp() override; 114 115 // Closes the socket created by SetUp(). 116 void TearDown() override; 117 118 // Gets the device index of the loopback device. 119 int GetLoopbackIndex(); 120 121 // The socket used for both reading and writing. 122 int socket_; 123 }; 124 125 void CookedPacketTest::SetUp() { 126 if (!ASSERT_NO_ERRNO_AND_VALUE(HaveCapability(CAP_NET_RAW))) { 127 ASSERT_THAT(socket(AF_PACKET, SOCK_DGRAM, htons(GetParam())), 128 SyscallFailsWithErrno(EPERM)); 129 GTEST_SKIP(); 130 } 131 132 if (!IsRunningOnGvisor()) { 133 FileDescriptor acceptLocal = ASSERT_NO_ERRNO_AND_VALUE( 134 Open("/proc/sys/net/ipv4/conf/lo/accept_local", O_RDONLY)); 135 FileDescriptor routeLocalnet = ASSERT_NO_ERRNO_AND_VALUE( 136 Open("/proc/sys/net/ipv4/conf/lo/route_localnet", O_RDONLY)); 137 char enabled; 138 ASSERT_THAT(read(acceptLocal.get(), &enabled, 1), 139 SyscallSucceedsWithValue(1)); 140 ASSERT_EQ(enabled, '1'); 141 ASSERT_THAT(read(routeLocalnet.get(), &enabled, 1), 142 SyscallSucceedsWithValue(1)); 143 ASSERT_EQ(enabled, '1'); 144 } 145 146 ASSERT_THAT(socket_ = socket(AF_PACKET, SOCK_DGRAM, htons(GetParam())), 147 SyscallSucceeds()); 148 } 149 150 void CookedPacketTest::TearDown() { 151 // TearDown will be run even if we skip the test. 152 if (ASSERT_NO_ERRNO_AND_VALUE(HaveCapability(CAP_NET_RAW))) { 153 EXPECT_THAT(close(socket_), SyscallSucceeds()); 154 } 155 } 156 157 int CookedPacketTest::GetLoopbackIndex() { 158 struct ifreq ifr; 159 snprintf(ifr.ifr_name, IFNAMSIZ, "lo"); 160 EXPECT_THAT(ioctl(socket_, SIOCGIFINDEX, &ifr), SyscallSucceeds()); 161 EXPECT_NE(ifr.ifr_ifindex, 0); 162 return ifr.ifr_ifindex; 163 } 164 165 // Receive and verify the message via packet socket on interface. 166 void ReceiveMessage(int sock, int ifindex) { 167 // Wait for the socket to become readable. 168 struct pollfd pfd = {}; 169 pfd.fd = sock; 170 pfd.events = POLLIN; 171 EXPECT_THAT(RetryEINTR(poll)(&pfd, 1, 2000), SyscallSucceedsWithValue(1)); 172 173 // Read and verify the data. 174 constexpr size_t packet_size = 175 sizeof(struct iphdr) + sizeof(struct udphdr) + sizeof(kMessage); 176 char buf[64]; 177 struct sockaddr_ll src = {}; 178 socklen_t src_len = sizeof(src); 179 ASSERT_THAT(recvfrom(sock, buf, sizeof(buf), 0, 180 reinterpret_cast<struct sockaddr*>(&src), &src_len), 181 SyscallSucceedsWithValue(packet_size)); 182 183 // sockaddr_ll ends with an 8 byte physical address field, but ethernet 184 // addresses only use 6 bytes. Linux used to return sizeof(sockaddr_ll)-2 185 // here, but since commit b2cf86e1563e33a14a1c69b3e508d15dc12f804c returns 186 // sizeof(sockaddr_ll). 187 ASSERT_THAT(src_len, AnyOf(Eq(sizeof(src)), Eq(sizeof(src) - 2))); 188 189 // Verify the source address. 190 EXPECT_EQ(src.sll_family, AF_PACKET); 191 EXPECT_EQ(src.sll_ifindex, ifindex); 192 EXPECT_EQ(src.sll_halen, ETH_ALEN); 193 EXPECT_EQ(ntohs(src.sll_protocol), ETH_P_IP); 194 // This came from the loopback device, so the address is all 0s. 195 for (int i = 0; i < src.sll_halen; i++) { 196 EXPECT_EQ(src.sll_addr[i], 0); 197 } 198 199 // Verify the IP header. We memcpy to deal with pointer aligment. 200 struct iphdr ip = {}; 201 memcpy(&ip, buf, sizeof(ip)); 202 EXPECT_EQ(ip.ihl, 5); 203 EXPECT_EQ(ip.version, 4); 204 EXPECT_EQ(ip.tot_len, htons(packet_size)); 205 EXPECT_EQ(ip.protocol, IPPROTO_UDP); 206 EXPECT_EQ(ip.daddr, htonl(INADDR_LOOPBACK)); 207 EXPECT_EQ(ip.saddr, htonl(INADDR_LOOPBACK)); 208 209 // Verify the UDP header. We memcpy to deal with pointer aligment. 210 struct udphdr udp = {}; 211 memcpy(&udp, buf + sizeof(iphdr), sizeof(udp)); 212 EXPECT_EQ(udp.dest, kPort); 213 EXPECT_EQ(udp.len, htons(sizeof(udphdr) + sizeof(kMessage))); 214 215 // Verify the payload. 216 char* payload = reinterpret_cast<char*>(buf + sizeof(iphdr) + sizeof(udphdr)); 217 EXPECT_EQ(strncmp(payload, kMessage, sizeof(kMessage)), 0); 218 } 219 220 // Receive via a packet socket. 221 TEST_P(CookedPacketTest, Receive) { 222 // Let's use a simple IP payload: a UDP datagram. 223 FileDescriptor udp_sock = 224 ASSERT_NO_ERRNO_AND_VALUE(Socket(AF_INET, SOCK_DGRAM, 0)); 225 SendUDPMessage(udp_sock.get()); 226 227 // Receive and verify the data. 228 int loopback_index = GetLoopbackIndex(); 229 ReceiveMessage(socket_, loopback_index); 230 } 231 232 // Send via a packet socket. 233 TEST_P(CookedPacketTest, Send) { 234 // Let's send a UDP packet and receive it using a regular UDP socket. 235 FileDescriptor udp_sock = 236 ASSERT_NO_ERRNO_AND_VALUE(Socket(AF_INET, SOCK_DGRAM, 0)); 237 struct sockaddr_in bind_addr = {}; 238 bind_addr.sin_family = AF_INET; 239 bind_addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK); 240 bind_addr.sin_port = kPort; 241 ASSERT_THAT( 242 bind(udp_sock.get(), reinterpret_cast<struct sockaddr*>(&bind_addr), 243 sizeof(bind_addr)), 244 SyscallSucceeds()); 245 246 // Set up the destination physical address. 247 struct sockaddr_ll dest = {}; 248 dest.sll_family = AF_PACKET; 249 dest.sll_halen = ETH_ALEN; 250 dest.sll_ifindex = GetLoopbackIndex(); 251 dest.sll_protocol = htons(ETH_P_IP); 252 // We're sending to the loopback device, so the address is all 0s. 253 memset(dest.sll_addr, 0x00, ETH_ALEN); 254 255 // Set up the IP header. 256 struct iphdr iphdr = {0}; 257 iphdr.ihl = 5; 258 iphdr.version = 4; 259 iphdr.tos = 0; 260 iphdr.tot_len = 261 htons(sizeof(struct iphdr) + sizeof(struct udphdr) + sizeof(kMessage)); 262 // Get a pseudo-random ID. If we clash with an in-use ID the test will fail, 263 // but we have no way of getting an ID we know to be good. 264 srand(*reinterpret_cast<unsigned int*>(&iphdr)); 265 iphdr.id = rand(); 266 // Linux sets this bit ("do not fragment") for small packets. 267 iphdr.frag_off = 1 << 6; 268 iphdr.ttl = 64; 269 iphdr.protocol = IPPROTO_UDP; 270 iphdr.daddr = htonl(INADDR_LOOPBACK); 271 iphdr.saddr = htonl(INADDR_LOOPBACK); 272 iphdr.check = IPChecksum(iphdr); 273 274 // Set up the UDP header. 275 struct udphdr udphdr = {}; 276 udphdr.source = kPort; 277 udphdr.dest = kPort; 278 udphdr.len = htons(sizeof(udphdr) + sizeof(kMessage)); 279 udphdr.check = UDPChecksum(iphdr, udphdr, kMessage, sizeof(kMessage)); 280 281 // Copy both headers and the payload into our packet buffer. 282 char send_buf[sizeof(iphdr) + sizeof(udphdr) + sizeof(kMessage)]; 283 memcpy(send_buf, &iphdr, sizeof(iphdr)); 284 memcpy(send_buf + sizeof(iphdr), &udphdr, sizeof(udphdr)); 285 memcpy(send_buf + sizeof(iphdr) + sizeof(udphdr), kMessage, sizeof(kMessage)); 286 287 // We don't implement writing to packet sockets on gVisor. 288 if (IsRunningOnGvisor()) { 289 ASSERT_THAT(sendto(socket_, send_buf, sizeof(send_buf), 0, 290 reinterpret_cast<struct sockaddr*>(&dest), sizeof(dest)), 291 SyscallFailsWithErrno(EINVAL)); 292 GTEST_SKIP(); 293 } 294 295 // Send it. 296 ASSERT_THAT(sendto(socket_, send_buf, sizeof(send_buf), 0, 297 reinterpret_cast<struct sockaddr*>(&dest), sizeof(dest)), 298 SyscallSucceedsWithValue(sizeof(send_buf))); 299 300 // Wait for the packet to become available on both sockets. 301 struct pollfd pfd = {}; 302 pfd.fd = udp_sock.get(); 303 pfd.events = POLLIN; 304 ASSERT_THAT(RetryEINTR(poll)(&pfd, 1, 5000), SyscallSucceedsWithValue(1)); 305 pfd.fd = socket_; 306 pfd.events = POLLIN; 307 ASSERT_THAT(RetryEINTR(poll)(&pfd, 1, 5000), SyscallSucceedsWithValue(1)); 308 309 // Receive on the packet socket. 310 char recv_buf[sizeof(send_buf)]; 311 ASSERT_THAT(recv(socket_, recv_buf, sizeof(recv_buf), 0), 312 SyscallSucceedsWithValue(sizeof(recv_buf))); 313 ASSERT_EQ(memcmp(recv_buf, send_buf, sizeof(send_buf)), 0); 314 315 // Receive on the UDP socket. 316 struct sockaddr_in src; 317 socklen_t src_len = sizeof(src); 318 ASSERT_THAT(recvfrom(udp_sock.get(), recv_buf, sizeof(recv_buf), MSG_DONTWAIT, 319 reinterpret_cast<struct sockaddr*>(&src), &src_len), 320 SyscallSucceedsWithValue(sizeof(kMessage))); 321 // Check src and payload. 322 EXPECT_EQ(strncmp(recv_buf, kMessage, sizeof(kMessage)), 0); 323 EXPECT_EQ(src.sin_family, AF_INET); 324 EXPECT_EQ(src.sin_port, kPort); 325 EXPECT_EQ(src.sin_addr.s_addr, htonl(INADDR_LOOPBACK)); 326 } 327 328 // Bind and receive via packet socket. 329 TEST_P(CookedPacketTest, BindReceive) { 330 struct sockaddr_ll bind_addr = {}; 331 bind_addr.sll_family = AF_PACKET; 332 bind_addr.sll_protocol = htons(GetParam()); 333 bind_addr.sll_ifindex = GetLoopbackIndex(); 334 335 ASSERT_THAT(bind(socket_, reinterpret_cast<struct sockaddr*>(&bind_addr), 336 sizeof(bind_addr)), 337 SyscallSucceeds()); 338 339 // Let's use a simple IP payload: a UDP datagram. 340 FileDescriptor udp_sock = 341 ASSERT_NO_ERRNO_AND_VALUE(Socket(AF_INET, SOCK_DGRAM, 0)); 342 SendUDPMessage(udp_sock.get()); 343 344 // Receive and verify the data. 345 ReceiveMessage(socket_, bind_addr.sll_ifindex); 346 } 347 348 // Double Bind socket. 349 TEST_P(CookedPacketTest, DoubleBindSucceeds) { 350 struct sockaddr_ll bind_addr = {}; 351 bind_addr.sll_family = AF_PACKET; 352 bind_addr.sll_protocol = htons(GetParam()); 353 bind_addr.sll_ifindex = GetLoopbackIndex(); 354 355 ASSERT_THAT(bind(socket_, reinterpret_cast<struct sockaddr*>(&bind_addr), 356 sizeof(bind_addr)), 357 SyscallSucceeds()); 358 359 // Binding socket again should fail. 360 ASSERT_THAT(bind(socket_, reinterpret_cast<struct sockaddr*>(&bind_addr), 361 sizeof(bind_addr)), 362 // Linux 4.09 returns EINVAL here, but some time before 4.19 it 363 // switched to EADDRINUSE. 364 SyscallSucceeds()); 365 } 366 367 // Bind and verify we do not receive data on interface which is not bound 368 TEST_P(CookedPacketTest, BindDrop) { 369 // Let's use a simple IP payload: a UDP datagram. 370 FileDescriptor udp_sock = 371 ASSERT_NO_ERRNO_AND_VALUE(Socket(AF_INET, SOCK_DGRAM, 0)); 372 373 struct ifaddrs* if_addr_list = nullptr; 374 auto cleanup = Cleanup([&if_addr_list]() { freeifaddrs(if_addr_list); }); 375 376 ASSERT_THAT(getifaddrs(&if_addr_list), SyscallSucceeds()); 377 378 // Get interface other than loopback. 379 struct ifreq ifr = {}; 380 for (struct ifaddrs* i = if_addr_list; i; i = i->ifa_next) { 381 if (strcmp(i->ifa_name, "lo") != 0) { 382 strncpy(ifr.ifr_name, i->ifa_name, sizeof(ifr.ifr_name)); 383 break; 384 } 385 } 386 387 // Skip if no interface is available other than loopback. 388 if (strlen(ifr.ifr_name) == 0) { 389 GTEST_SKIP(); 390 } 391 392 // Get interface index. 393 EXPECT_THAT(ioctl(socket_, SIOCGIFINDEX, &ifr), SyscallSucceeds()); 394 EXPECT_NE(ifr.ifr_ifindex, 0); 395 396 // Bind to packet socket requires only family, protocol and ifindex. 397 struct sockaddr_ll bind_addr = {}; 398 bind_addr.sll_family = AF_PACKET; 399 bind_addr.sll_protocol = htons(GetParam()); 400 bind_addr.sll_ifindex = ifr.ifr_ifindex; 401 402 ASSERT_THAT(bind(socket_, reinterpret_cast<struct sockaddr*>(&bind_addr), 403 sizeof(bind_addr)), 404 SyscallSucceeds()); 405 406 // Send to loopback interface. 407 struct sockaddr_in dest = {}; 408 dest.sin_addr.s_addr = htonl(INADDR_LOOPBACK); 409 dest.sin_family = AF_INET; 410 dest.sin_port = kPort; 411 EXPECT_THAT(sendto(udp_sock.get(), kMessage, sizeof(kMessage), 0, 412 reinterpret_cast<struct sockaddr*>(&dest), sizeof(dest)), 413 SyscallSucceedsWithValue(sizeof(kMessage))); 414 415 // Wait and make sure the socket never receives any data. 416 struct pollfd pfd = {}; 417 pfd.fd = socket_; 418 pfd.events = POLLIN; 419 EXPECT_THAT(RetryEINTR(poll)(&pfd, 1, 1000), SyscallSucceedsWithValue(0)); 420 } 421 422 // Verify that we receive outbound packets. This test requires at least one 423 // non loopback interface so that we can actually capture an outgoing packet. 424 TEST_P(CookedPacketTest, ReceiveOutbound) { 425 // Only ETH_P_ALL sockets can receive outbound packets on linux. 426 SKIP_IF(GetParam() != ETH_P_ALL); 427 428 // Let's use a simple IP payload: a UDP datagram. 429 FileDescriptor udp_sock = 430 ASSERT_NO_ERRNO_AND_VALUE(Socket(AF_INET, SOCK_DGRAM, 0)); 431 432 struct ifaddrs* if_addr_list = nullptr; 433 auto cleanup = Cleanup([&if_addr_list]() { freeifaddrs(if_addr_list); }); 434 435 ASSERT_THAT(getifaddrs(&if_addr_list), SyscallSucceeds()); 436 437 // Get interface other than loopback. 438 struct ifreq ifr = {}; 439 for (struct ifaddrs* i = if_addr_list; i; i = i->ifa_next) { 440 if (strcmp(i->ifa_name, "lo") != 0) { 441 strncpy(ifr.ifr_name, i->ifa_name, sizeof(ifr.ifr_name)); 442 break; 443 } 444 } 445 446 // Skip if no interface is available other than loopback. 447 if (strlen(ifr.ifr_name) == 0) { 448 GTEST_SKIP(); 449 } 450 451 // Get interface index and name. 452 EXPECT_THAT(ioctl(socket_, SIOCGIFINDEX, &ifr), SyscallSucceeds()); 453 EXPECT_NE(ifr.ifr_ifindex, 0); 454 int ifindex = ifr.ifr_ifindex; 455 456 constexpr int kMACSize = 6; 457 char hwaddr[kMACSize]; 458 // Get interface address. 459 ASSERT_THAT(ioctl(socket_, SIOCGIFHWADDR, &ifr), SyscallSucceeds()); 460 ASSERT_THAT(ifr.ifr_hwaddr.sa_family, 461 AnyOf(Eq(ARPHRD_NONE), Eq(ARPHRD_ETHER))); 462 memcpy(hwaddr, ifr.ifr_hwaddr.sa_data, kMACSize); 463 464 // Just send it to the google dns server 8.8.8.8. It's UDP we don't care 465 // if it actually gets to the DNS Server we just want to see that we receive 466 // it on our AF_PACKET socket. 467 // 468 // NOTE: We just want to pick an IP that is non-local to avoid having to 469 // handle ARP as this should cause the UDP packet to be sent to the default 470 // gateway configured for the system under test. Otherwise the only packet we 471 // will see is the ARP query unless we picked an IP which will actually 472 // resolve. The test is a bit brittle but this was the best compromise for 473 // now. 474 struct sockaddr_in dest = {}; 475 ASSERT_EQ(inet_pton(AF_INET, "8.8.8.8", &dest.sin_addr.s_addr), 1); 476 dest.sin_family = AF_INET; 477 dest.sin_port = kPort; 478 EXPECT_THAT(sendto(udp_sock.get(), kMessage, sizeof(kMessage), 0, 479 reinterpret_cast<struct sockaddr*>(&dest), sizeof(dest)), 480 SyscallSucceedsWithValue(sizeof(kMessage))); 481 482 // Wait and make sure the socket receives the data. 483 struct pollfd pfd = {}; 484 pfd.fd = socket_; 485 pfd.events = POLLIN; 486 EXPECT_THAT(RetryEINTR(poll)(&pfd, 1, 1000), SyscallSucceedsWithValue(1)); 487 488 // Now read and check that the packet is the one we just sent. 489 // Read and verify the data. 490 constexpr size_t packet_size = 491 sizeof(struct iphdr) + sizeof(struct udphdr) + sizeof(kMessage); 492 char buf[64]; 493 struct sockaddr_ll src = {}; 494 socklen_t src_len = sizeof(src); 495 ASSERT_THAT(recvfrom(socket_, buf, sizeof(buf), 0, 496 reinterpret_cast<struct sockaddr*>(&src), &src_len), 497 SyscallSucceedsWithValue(packet_size)); 498 499 // sockaddr_ll ends with an 8 byte physical address field, but ethernet 500 // addresses only use 6 bytes. Linux used to return sizeof(sockaddr_ll)-2 501 // here, but since commit b2cf86e1563e33a14a1c69b3e508d15dc12f804c returns 502 // sizeof(sockaddr_ll). 503 ASSERT_THAT(src_len, AnyOf(Eq(sizeof(src)), Eq(sizeof(src) - 2))); 504 505 // Verify the source address. 506 EXPECT_EQ(src.sll_family, AF_PACKET); 507 EXPECT_EQ(src.sll_ifindex, ifindex); 508 EXPECT_EQ(src.sll_halen, ETH_ALEN); 509 EXPECT_EQ(ntohs(src.sll_protocol), ETH_P_IP); 510 EXPECT_EQ(src.sll_pkttype, PACKET_OUTGOING); 511 // Verify the link address of the interface matches that of the non 512 // non loopback interface address we stored above. 513 for (int i = 0; i < src.sll_halen; i++) { 514 EXPECT_EQ(src.sll_addr[i], hwaddr[i]); 515 } 516 517 // Verify the IP header. 518 struct iphdr ip = {}; 519 memcpy(&ip, buf, sizeof(ip)); 520 EXPECT_EQ(ip.ihl, 5); 521 EXPECT_EQ(ip.version, 4); 522 EXPECT_EQ(ip.tot_len, htons(packet_size)); 523 EXPECT_EQ(ip.protocol, IPPROTO_UDP); 524 EXPECT_EQ(ip.daddr, dest.sin_addr.s_addr); 525 EXPECT_NE(ip.saddr, htonl(INADDR_LOOPBACK)); 526 527 // Verify the UDP header. 528 struct udphdr udp = {}; 529 memcpy(&udp, buf + sizeof(iphdr), sizeof(udp)); 530 EXPECT_EQ(udp.dest, kPort); 531 EXPECT_EQ(udp.len, htons(sizeof(udphdr) + sizeof(kMessage))); 532 533 // Verify the payload. 534 char* payload = reinterpret_cast<char*>(buf + sizeof(iphdr) + sizeof(udphdr)); 535 EXPECT_EQ(strncmp(payload, kMessage, sizeof(kMessage)), 0); 536 } 537 538 // Bind with invalid address. 539 TEST_P(CookedPacketTest, BindFail) { 540 // Null address. 541 ASSERT_THAT( 542 bind(socket_, nullptr, sizeof(struct sockaddr)), 543 AnyOf(SyscallFailsWithErrno(EFAULT), SyscallFailsWithErrno(EINVAL))); 544 545 // Address of size 1. 546 uint8_t addr = 0; 547 ASSERT_THAT( 548 bind(socket_, reinterpret_cast<struct sockaddr*>(&addr), sizeof(addr)), 549 SyscallFailsWithErrno(EINVAL)); 550 } 551 552 INSTANTIATE_TEST_SUITE_P(AllInetTests, CookedPacketTest, 553 ::testing::Values(ETH_P_IP, ETH_P_ALL)); 554 555 } // namespace 556 557 } // namespace testing 558 } // namespace gvisor