efi_selftest_snp.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * efi_selftest_snp
  4. *
  5. * Copyright (c) 2017 Heinrich Schuchardt <xypron.glpk@gmx.de>
  6. *
  7. * This unit test covers the Simple Network Protocol as well as
  8. * the CopyMem and SetMem boottime services.
  9. *
  10. * A DHCP discover message is sent. The test is successful if a
  11. * DHCP reply is received.
  12. *
  13. * TODO: Once ConnectController and DisconnectController are implemented
  14. * we should connect our code as controller.
  15. */
  16. #include <efi_selftest.h>
  17. #include <net.h>
  18. /*
  19. * MAC address for broadcasts
  20. */
  21. static const u8 BROADCAST_MAC[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
  22. struct dhcp_hdr {
  23. u8 op;
  24. #define BOOTREQUEST 1
  25. #define BOOTREPLY 2
  26. u8 htype;
  27. # define HWT_ETHER 1
  28. u8 hlen;
  29. # define HWL_ETHER 6
  30. u8 hops;
  31. u32 xid;
  32. u16 secs;
  33. u16 flags;
  34. #define DHCP_FLAGS_UNICAST 0x0000
  35. #define DHCP_FLAGS_BROADCAST 0x0080
  36. u32 ciaddr;
  37. u32 yiaddr;
  38. u32 siaddr;
  39. u32 giaddr;
  40. u8 chaddr[16];
  41. u8 sname[64];
  42. u8 file[128];
  43. };
  44. /*
  45. * Message type option.
  46. */
  47. #define DHCP_MESSAGE_TYPE 0x35
  48. #define DHCPDISCOVER 1
  49. #define DHCPOFFER 2
  50. #define DHCPREQUEST 3
  51. #define DHCPDECLINE 4
  52. #define DHCPACK 5
  53. #define DHCPNAK 6
  54. #define DHCPRELEASE 7
  55. struct dhcp {
  56. struct ethernet_hdr eth_hdr;
  57. struct ip_udp_hdr ip_udp;
  58. struct dhcp_hdr dhcp_hdr;
  59. u8 opt[128];
  60. } __packed;
  61. static struct efi_boot_services *boottime;
  62. static struct efi_simple_network *net;
  63. static struct efi_event *timer;
  64. static const efi_guid_t efi_net_guid = EFI_SIMPLE_NETWORK_PROTOCOL_GUID;
  65. /* IP packet ID */
  66. static unsigned int net_ip_id;
  67. /*
  68. * Compute the checksum of the IP header. We cover even values of length only.
  69. * We cannot use net/checksum.c due to different CFLAGS values.
  70. *
  71. * @buf: IP header
  72. * @len: length of header in bytes
  73. * @return: checksum
  74. */
  75. static unsigned int efi_ip_checksum(const void *buf, size_t len)
  76. {
  77. size_t i;
  78. u32 sum = 0;
  79. const u16 *pos = buf;
  80. for (i = 0; i < len; i += 2)
  81. sum += *pos++;
  82. sum = (sum >> 16) + (sum & 0xffff);
  83. sum += sum >> 16;
  84. sum = ~sum & 0xffff;
  85. return sum;
  86. }
  87. /*
  88. * Transmit a DHCPDISCOVER message.
  89. */
  90. static efi_status_t send_dhcp_discover(void)
  91. {
  92. efi_status_t ret;
  93. struct dhcp p = {};
  94. /*
  95. * Fill Ethernet header
  96. */
  97. boottime->copy_mem(p.eth_hdr.et_dest, (void *)BROADCAST_MAC, ARP_HLEN);
  98. boottime->copy_mem(p.eth_hdr.et_src, &net->mode->current_address,
  99. ARP_HLEN);
  100. p.eth_hdr.et_protlen = htons(PROT_IP);
  101. /*
  102. * Fill IP header
  103. */
  104. p.ip_udp.ip_hl_v = 0x45;
  105. p.ip_udp.ip_len = htons(sizeof(struct dhcp) -
  106. sizeof(struct ethernet_hdr));
  107. p.ip_udp.ip_id = htons(++net_ip_id);
  108. p.ip_udp.ip_off = htons(IP_FLAGS_DFRAG);
  109. p.ip_udp.ip_ttl = 0xff; /* time to live */
  110. p.ip_udp.ip_p = IPPROTO_UDP;
  111. boottime->set_mem(&p.ip_udp.ip_dst, 4, 0xff);
  112. p.ip_udp.ip_sum = efi_ip_checksum(&p.ip_udp, IP_HDR_SIZE);
  113. /*
  114. * Fill UDP header
  115. */
  116. p.ip_udp.udp_src = htons(68);
  117. p.ip_udp.udp_dst = htons(67);
  118. p.ip_udp.udp_len = htons(sizeof(struct dhcp) -
  119. sizeof(struct ethernet_hdr) -
  120. sizeof(struct ip_hdr));
  121. /*
  122. * Fill DHCP header
  123. */
  124. p.dhcp_hdr.op = BOOTREQUEST;
  125. p.dhcp_hdr.htype = HWT_ETHER;
  126. p.dhcp_hdr.hlen = HWL_ETHER;
  127. p.dhcp_hdr.flags = htons(DHCP_FLAGS_UNICAST);
  128. boottime->copy_mem(&p.dhcp_hdr.chaddr,
  129. &net->mode->current_address, ARP_HLEN);
  130. /*
  131. * Fill options
  132. */
  133. p.opt[0] = 0x63; /* DHCP magic cookie */
  134. p.opt[1] = 0x82;
  135. p.opt[2] = 0x53;
  136. p.opt[3] = 0x63;
  137. p.opt[4] = DHCP_MESSAGE_TYPE;
  138. p.opt[5] = 0x01; /* length */
  139. p.opt[6] = DHCPDISCOVER;
  140. p.opt[7] = 0x39; /* maximum message size */
  141. p.opt[8] = 0x02; /* length */
  142. p.opt[9] = 0x02; /* 576 bytes */
  143. p.opt[10] = 0x40;
  144. p.opt[11] = 0xff; /* end of options */
  145. /*
  146. * Transmit DHCPDISCOVER message.
  147. */
  148. ret = net->transmit(net, 0, sizeof(struct dhcp), &p, NULL, NULL, 0);
  149. if (ret != EFI_SUCCESS)
  150. efi_st_error("Sending a DHCP request failed\n");
  151. else
  152. efi_st_printf("DHCP Discover\n");
  153. return ret;
  154. }
  155. /*
  156. * Setup unit test.
  157. *
  158. * Create a 1 s periodic timer.
  159. * Start the network driver.
  160. *
  161. * @handle: handle of the loaded image
  162. * @systable: system table
  163. * @return: EFI_ST_SUCCESS for success
  164. */
  165. static int setup(const efi_handle_t handle,
  166. const struct efi_system_table *systable)
  167. {
  168. efi_status_t ret;
  169. boottime = systable->boottime;
  170. /*
  171. * Create a timer event.
  172. */
  173. ret = boottime->create_event(EVT_TIMER, TPL_CALLBACK, NULL, NULL,
  174. &timer);
  175. if (ret != EFI_SUCCESS) {
  176. efi_st_error("Failed to create event\n");
  177. return EFI_ST_FAILURE;
  178. }
  179. /*
  180. * Set timer period to 1s.
  181. */
  182. ret = boottime->set_timer(timer, EFI_TIMER_PERIODIC, 10000000);
  183. if (ret != EFI_SUCCESS) {
  184. efi_st_error("Failed to set timer\n");
  185. return EFI_ST_FAILURE;
  186. }
  187. /*
  188. * Find an interface implementing the SNP protocol.
  189. */
  190. ret = boottime->locate_protocol(&efi_net_guid, NULL, (void **)&net);
  191. if (ret != EFI_SUCCESS) {
  192. net = NULL;
  193. efi_st_error("Failed to locate simple network protocol\n");
  194. return EFI_ST_FAILURE;
  195. }
  196. /*
  197. * Check hardware address size.
  198. */
  199. if (!net->mode) {
  200. efi_st_error("Mode not provided\n");
  201. return EFI_ST_FAILURE;
  202. }
  203. if (net->mode->hwaddr_size != ARP_HLEN) {
  204. efi_st_error("HwAddressSize = %u, expected %u\n",
  205. net->mode->hwaddr_size, ARP_HLEN);
  206. return EFI_ST_FAILURE;
  207. }
  208. /*
  209. * Check that WaitForPacket event exists.
  210. */
  211. if (!net->wait_for_packet) {
  212. efi_st_error("WaitForPacket event missing\n");
  213. return EFI_ST_FAILURE;
  214. }
  215. if (net->mode->state == EFI_NETWORK_INITIALIZED) {
  216. /*
  217. * Shut down network adapter.
  218. */
  219. ret = net->shutdown(net);
  220. if (ret != EFI_SUCCESS) {
  221. efi_st_error("Failed to shut down network adapter\n");
  222. return EFI_ST_FAILURE;
  223. }
  224. }
  225. if (net->mode->state == EFI_NETWORK_STARTED) {
  226. /*
  227. * Stop network adapter.
  228. */
  229. ret = net->stop(net);
  230. if (ret != EFI_SUCCESS) {
  231. efi_st_error("Failed to stop network adapter\n");
  232. return EFI_ST_FAILURE;
  233. }
  234. }
  235. /*
  236. * Start network adapter.
  237. */
  238. ret = net->start(net);
  239. if (ret != EFI_SUCCESS && ret != EFI_ALREADY_STARTED) {
  240. efi_st_error("Failed to start network adapter\n");
  241. return EFI_ST_FAILURE;
  242. }
  243. if (net->mode->state != EFI_NETWORK_STARTED) {
  244. efi_st_error("Failed to start network adapter\n");
  245. return EFI_ST_FAILURE;
  246. }
  247. /*
  248. * Initialize network adapter.
  249. */
  250. ret = net->initialize(net, 0, 0);
  251. if (ret != EFI_SUCCESS) {
  252. efi_st_error("Failed to initialize network adapter\n");
  253. return EFI_ST_FAILURE;
  254. }
  255. if (net->mode->state != EFI_NETWORK_INITIALIZED) {
  256. efi_st_error("Failed to initialize network adapter\n");
  257. return EFI_ST_FAILURE;
  258. }
  259. return EFI_ST_SUCCESS;
  260. }
  261. /*
  262. * Execute unit test.
  263. *
  264. * A DHCP discover message is sent. The test is successful if a
  265. * DHCP reply is received within 10 seconds.
  266. *
  267. * @return: EFI_ST_SUCCESS for success
  268. */
  269. static int execute(void)
  270. {
  271. efi_status_t ret;
  272. struct efi_event *events[2];
  273. efi_uintn_t index;
  274. union {
  275. struct dhcp p;
  276. u8 b[PKTSIZE];
  277. } buffer;
  278. struct efi_mac_address srcaddr;
  279. struct efi_mac_address destaddr;
  280. size_t buffer_size;
  281. u8 *addr;
  282. /*
  283. * The timeout is to occur after 10 s.
  284. */
  285. unsigned int timeout = 10;
  286. /* Setup may have failed */
  287. if (!net || !timer) {
  288. efi_st_error("Cannot execute test after setup failure\n");
  289. return EFI_ST_FAILURE;
  290. }
  291. /*
  292. * Send DHCP discover message
  293. */
  294. ret = send_dhcp_discover();
  295. if (ret != EFI_SUCCESS)
  296. return EFI_ST_FAILURE;
  297. /*
  298. * If we would call WaitForEvent only with the WaitForPacket event,
  299. * our code would block until a packet is received which might never
  300. * occur. By calling WaitFor event with both a timer event and the
  301. * WaitForPacket event we can escape this blocking situation.
  302. *
  303. * If the timer event occurs before we have received a DHCP reply
  304. * a further DHCP discover message is sent.
  305. */
  306. events[0] = timer;
  307. events[1] = net->wait_for_packet;
  308. for (;;) {
  309. u32 int_status;
  310. /*
  311. * Wait for packet to be received or timer event.
  312. */
  313. boottime->wait_for_event(2, events, &index);
  314. if (index == 0) {
  315. /*
  316. * The timer event occurred. Check for timeout.
  317. */
  318. --timeout;
  319. if (!timeout) {
  320. efi_st_error("Timeout occurred\n");
  321. return EFI_ST_FAILURE;
  322. }
  323. /*
  324. * Send further DHCP discover message
  325. */
  326. ret = send_dhcp_discover();
  327. if (ret != EFI_SUCCESS)
  328. return EFI_ST_FAILURE;
  329. continue;
  330. }
  331. /*
  332. * Receive packet
  333. */
  334. buffer_size = sizeof(buffer);
  335. ret = net->get_status(net, &int_status, NULL);
  336. if (ret != EFI_SUCCESS) {
  337. efi_st_error("Failed to get status");
  338. return EFI_ST_FAILURE;
  339. }
  340. if (!(int_status & EFI_SIMPLE_NETWORK_RECEIVE_INTERRUPT)) {
  341. efi_st_error("RX interrupt not set");
  342. return EFI_ST_FAILURE;
  343. }
  344. ret = net->receive(net, NULL, &buffer_size, &buffer,
  345. &srcaddr, &destaddr, NULL);
  346. if (ret != EFI_SUCCESS) {
  347. efi_st_error("Failed to receive packet");
  348. return EFI_ST_FAILURE;
  349. }
  350. /*
  351. * Check the packet is meant for this system.
  352. * Unfortunately QEMU ignores the broadcast flag.
  353. * So we have to check for broadcasts too.
  354. */
  355. if (memcmp(&destaddr, &net->mode->current_address, ARP_HLEN) &&
  356. memcmp(&destaddr, BROADCAST_MAC, ARP_HLEN))
  357. continue;
  358. /*
  359. * Check this is a DHCP reply
  360. */
  361. if (buffer.p.eth_hdr.et_protlen != ntohs(PROT_IP) ||
  362. buffer.p.ip_udp.ip_hl_v != 0x45 ||
  363. buffer.p.ip_udp.ip_p != IPPROTO_UDP ||
  364. buffer.p.ip_udp.udp_src != ntohs(67) ||
  365. buffer.p.ip_udp.udp_dst != ntohs(68) ||
  366. buffer.p.dhcp_hdr.op != BOOTREPLY)
  367. continue;
  368. /*
  369. * We successfully received a DHCP reply.
  370. */
  371. break;
  372. }
  373. /*
  374. * Write a log message.
  375. */
  376. addr = (u8 *)&buffer.p.ip_udp.ip_src;
  377. efi_st_printf("DHCP reply received from %u.%u.%u.%u (%pm) ",
  378. addr[0], addr[1], addr[2], addr[3], &srcaddr);
  379. if (!memcmp(&destaddr, BROADCAST_MAC, ARP_HLEN))
  380. efi_st_printf("as broadcast message.\n");
  381. else
  382. efi_st_printf("as unicast message.\n");
  383. return EFI_ST_SUCCESS;
  384. }
  385. /*
  386. * Tear down unit test.
  387. *
  388. * Close the timer event created in setup.
  389. * Shut down the network adapter.
  390. *
  391. * @return: EFI_ST_SUCCESS for success
  392. */
  393. static int teardown(void)
  394. {
  395. efi_status_t ret;
  396. int exit_status = EFI_ST_SUCCESS;
  397. if (timer) {
  398. /*
  399. * Stop timer.
  400. */
  401. ret = boottime->set_timer(timer, EFI_TIMER_STOP, 0);
  402. if (ret != EFI_SUCCESS) {
  403. efi_st_error("Failed to stop timer");
  404. exit_status = EFI_ST_FAILURE;
  405. }
  406. /*
  407. * Close timer event.
  408. */
  409. ret = boottime->close_event(timer);
  410. if (ret != EFI_SUCCESS) {
  411. efi_st_error("Failed to close event");
  412. exit_status = EFI_ST_FAILURE;
  413. }
  414. }
  415. if (net) {
  416. /*
  417. * Shut down network adapter.
  418. */
  419. ret = net->shutdown(net);
  420. if (ret != EFI_SUCCESS) {
  421. efi_st_error("Failed to shut down network adapter\n");
  422. exit_status = EFI_ST_FAILURE;
  423. }
  424. if (net->mode->state != EFI_NETWORK_STARTED) {
  425. efi_st_error("Failed to shutdown network adapter\n");
  426. return EFI_ST_FAILURE;
  427. }
  428. /*
  429. * Stop network adapter.
  430. */
  431. ret = net->stop(net);
  432. if (ret != EFI_SUCCESS) {
  433. efi_st_error("Failed to stop network adapter\n");
  434. exit_status = EFI_ST_FAILURE;
  435. }
  436. if (net->mode->state != EFI_NETWORK_STOPPED) {
  437. efi_st_error("Failed to stop network adapter\n");
  438. return EFI_ST_FAILURE;
  439. }
  440. }
  441. return exit_status;
  442. }
  443. EFI_UNIT_TEST(snp) = {
  444. .name = "simple network protocol",
  445. .phase = EFI_EXECUTE_BEFORE_BOOTTIME_EXIT,
  446. .setup = setup,
  447. .execute = execute,
  448. .teardown = teardown,
  449. #ifdef CONFIG_SANDBOX
  450. /*
  451. * Running this test on the sandbox requires setting environment
  452. * variable ethact to a network interface connected to a DHCP server and
  453. * ethrotate to 'no'.
  454. */
  455. .on_request = true,
  456. #endif
  457. };