efi_selftest_snp.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. /*
  2. * efi_selftest_snp
  3. *
  4. * Copyright (c) 2017 Heinrich Schuchardt <xypron.glpk@gmx.de>
  5. *
  6. * SPDX-License-Identifier: GPL-2.0+
  7. *
  8. * This unit test covers the Simple Network Protocol as well as
  9. * the CopyMem and SetMem boottime services.
  10. *
  11. * A DHCP discover message is sent. The test is successful if a
  12. * DHCP reply is received.
  13. *
  14. * TODO: Once ConnectController and DisconnectController are implemented
  15. * we should connect our code as controller.
  16. */
  17. #include <efi_selftest.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_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. /*
  216. * Initialize network adapter.
  217. */
  218. ret = net->initialize(net, 0, 0);
  219. if (ret != EFI_SUCCESS) {
  220. efi_st_error("Failed to initialize network adapter\n");
  221. return EFI_ST_FAILURE;
  222. }
  223. /*
  224. * Start network adapter.
  225. */
  226. ret = net->start(net);
  227. if (ret != EFI_SUCCESS) {
  228. efi_st_error("Failed to start network adapter\n");
  229. return EFI_ST_FAILURE;
  230. }
  231. return EFI_ST_SUCCESS;
  232. }
  233. /*
  234. * Execute unit test.
  235. *
  236. * A DHCP discover message is sent. The test is successful if a
  237. * DHCP reply is received within 10 seconds.
  238. *
  239. * @return: EFI_ST_SUCCESS for success
  240. */
  241. static int execute(void)
  242. {
  243. efi_status_t ret;
  244. struct efi_event *events[2];
  245. efi_uintn_t index;
  246. union {
  247. struct dhcp p;
  248. u8 b[PKTSIZE];
  249. } buffer;
  250. struct efi_mac_address srcaddr;
  251. struct efi_mac_address destaddr;
  252. size_t buffer_size;
  253. u8 *addr;
  254. /*
  255. * The timeout is to occur after 10 s.
  256. */
  257. unsigned int timeout = 10;
  258. /* Setup may have failed */
  259. if (!net || !timer) {
  260. efi_st_error("Cannot execute test after setup failure\n");
  261. return EFI_ST_FAILURE;
  262. }
  263. /*
  264. * Send DHCP discover message
  265. */
  266. ret = send_dhcp_discover();
  267. if (ret != EFI_SUCCESS)
  268. return EFI_ST_FAILURE;
  269. /*
  270. * If we would call WaitForEvent only with the WaitForPacket event,
  271. * our code would block until a packet is received which might never
  272. * occur. By calling WaitFor event with both a timer event and the
  273. * WaitForPacket event we can escape this blocking situation.
  274. *
  275. * If the timer event occurs before we have received a DHCP reply
  276. * a further DHCP discover message is sent.
  277. */
  278. events[0] = timer;
  279. events[1] = net->wait_for_packet;
  280. for (;;) {
  281. /*
  282. * Wait for packet to be received or timer event.
  283. */
  284. boottime->wait_for_event(2, events, &index);
  285. if (index == 0) {
  286. /*
  287. * The timer event occurred. Check for timeout.
  288. */
  289. --timeout;
  290. if (!timeout) {
  291. efi_st_error("Timeout occurred\n");
  292. return EFI_ST_FAILURE;
  293. }
  294. /*
  295. * Send further DHCP discover message
  296. */
  297. ret = send_dhcp_discover();
  298. if (ret != EFI_SUCCESS)
  299. return EFI_ST_FAILURE;
  300. continue;
  301. }
  302. /*
  303. * Receive packet
  304. */
  305. buffer_size = sizeof(buffer);
  306. net->receive(net, NULL, &buffer_size, &buffer,
  307. &srcaddr, &destaddr, NULL);
  308. if (ret != EFI_SUCCESS) {
  309. efi_st_error("Failed to receive packet");
  310. return EFI_ST_FAILURE;
  311. }
  312. /*
  313. * Check the packet is meant for this system.
  314. * Unfortunately QEMU ignores the broadcast flag.
  315. * So we have to check for broadcasts too.
  316. */
  317. if (efi_st_memcmp(&destaddr, &net->mode->current_address,
  318. ARP_HLEN) &&
  319. efi_st_memcmp(&destaddr, BROADCAST_MAC, ARP_HLEN))
  320. continue;
  321. /*
  322. * Check this is a DHCP reply
  323. */
  324. if (buffer.p.eth_hdr.et_protlen != ntohs(PROT_IP) ||
  325. buffer.p.ip_udp.ip_hl_v != 0x45 ||
  326. buffer.p.ip_udp.ip_p != IPPROTO_UDP ||
  327. buffer.p.ip_udp.udp_src != ntohs(67) ||
  328. buffer.p.ip_udp.udp_dst != ntohs(68) ||
  329. buffer.p.dhcp_hdr.op != BOOTREPLY)
  330. continue;
  331. /*
  332. * We successfully received a DHCP reply.
  333. */
  334. break;
  335. }
  336. /*
  337. * Write a log message.
  338. */
  339. addr = (u8 *)&buffer.p.ip_udp.ip_src;
  340. efi_st_printf("DHCP reply received from %u.%u.%u.%u (%pm) ",
  341. addr[0], addr[1], addr[2], addr[3], &srcaddr);
  342. if (!efi_st_memcmp(&destaddr, BROADCAST_MAC, ARP_HLEN))
  343. efi_st_printf("as broadcast message.\n");
  344. else
  345. efi_st_printf("as unicast message.\n");
  346. return EFI_ST_SUCCESS;
  347. }
  348. /*
  349. * Tear down unit test.
  350. *
  351. * Close the timer event created in setup.
  352. * Shut down the network adapter.
  353. *
  354. * @return: EFI_ST_SUCCESS for success
  355. */
  356. static int teardown(void)
  357. {
  358. efi_status_t ret;
  359. int exit_status = EFI_ST_SUCCESS;
  360. if (timer) {
  361. /*
  362. * Stop timer.
  363. */
  364. ret = boottime->set_timer(timer, EFI_TIMER_STOP, 0);
  365. if (ret != EFI_SUCCESS) {
  366. efi_st_error("Failed to stop timer");
  367. exit_status = EFI_ST_FAILURE;
  368. }
  369. /*
  370. * Close timer event.
  371. */
  372. ret = boottime->close_event(timer);
  373. if (ret != EFI_SUCCESS) {
  374. efi_st_error("Failed to close event");
  375. exit_status = EFI_ST_FAILURE;
  376. }
  377. }
  378. if (net) {
  379. /*
  380. * Stop network adapter.
  381. */
  382. ret = net->stop(net);
  383. if (ret != EFI_SUCCESS) {
  384. efi_st_error("Failed to stop network adapter\n");
  385. exit_status = EFI_ST_FAILURE;
  386. }
  387. /*
  388. * Shut down network adapter.
  389. */
  390. ret = net->shutdown(net);
  391. if (ret != EFI_SUCCESS) {
  392. efi_st_error("Failed to shut down network adapter\n");
  393. exit_status = EFI_ST_FAILURE;
  394. }
  395. }
  396. return exit_status;
  397. }
  398. EFI_UNIT_TEST(snp) = {
  399. .name = "simple network protocol",
  400. .phase = EFI_EXECUTE_BEFORE_BOOTTIME_EXIT,
  401. .setup = setup,
  402. .execute = execute,
  403. .teardown = teardown,
  404. };