91-full-ethernet.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. #include "limbo/arp/arp_request.hpp"
  2. #include "limbo/arp/arp_response.hpp"
  3. #include "limbo/arp/arp_table.hpp"
  4. #include "limbo/arp/state.hpp"
  5. #include "limbo/errc.h"
  6. #include "limbo/ethernet/state.hpp"
  7. #include "limbo/icmp/destination_unreachable.hpp"
  8. #include "limbo/icmp/echo_request.hpp"
  9. #include "limbo/icmp/echo_response.hpp"
  10. #include "limbo/icmp/state.hpp"
  11. #include "limbo/ip/state.hpp"
  12. #include "limbo/udp/connection.hpp"
  13. #include "limbo/udp/receiver.hpp"
  14. #include "limbo/udp/state.hpp"
  15. #include "limbo/stack.hpp"
  16. #include "test-utils.h"
  17. using namespace limbo;
  18. using Eth = ethernet::State<void>;
  19. using Arp = arp::State<Eth>;
  20. using ArpRequest = arp::ArpRequest<Arp>;
  21. using ArpResponse = arp::ArpResponse<Arp>;
  22. using IP = ip::State<Eth>;
  23. using ICMP = icmp::State<IP>;
  24. using UDP = udp::State<IP>;
  25. using EchoReq = icmp::EchoRequest<ICMP>;
  26. using EchoRes = icmp::EchoResponse<ICMP>;
  27. using Unreach = icmp::DestinationUnreacheable<ICMP>;
  28. using Receiver = udp::Receiver<UDP>;
  29. using Connection = udp::Connection<UDP>;
  30. using ArpCache = arp::ArpTable<2>;
  31. using MyStack1 =
  32. Stack<Layer<Receiver, EchoReq, EchoRes, Unreach>, /* app */
  33. Layer<UDP, ICMP, ArpRequest, ArpResponse>, /* transport */
  34. Layer<IP, Arp>, /* network */
  35. Layer<Eth> /* link */
  36. >;
  37. using MyStack2 =
  38. Stack<Layer<Connection, EchoReq, EchoRes, Unreach>, /* app */
  39. Layer<UDP, ICMP, ArpRequest, ArpResponse>, /* transport */
  40. Layer<IP, Arp>, /* network */
  41. Layer<Eth> /* link */
  42. >;
  43. uint8_t mac_raw_1[] = {0x48, 0xba, 0x4e, 0x51, 0x39, 0xbb};
  44. uint8_t mac_raw_2[] = {0x48, 0xba, 0x4e, 0x51, 0x39, 0xbc};
  45. auto ip_1 = make_address("192.168.100.1");
  46. auto ip_2 = make_address("192.168.100.2");
  47. auto ep_1 = ip::IPv4Endpoint{ip_1, 2000};
  48. auto ep_2 = ip::IPv4Endpoint{ip_2, 43210};
  49. auto ep_x = ip::IPv4Endpoint{ip_1, 2001};
  50. auto mac_1 = ethernet::MacAddress(mac_raw_1);
  51. auto mac_2 = ethernet::MacAddress(mac_raw_2);
  52. TEST_CASE("udp connection over ethernet", "[scenario]") {
  53. char buff_raw[1500];
  54. auto buff = Chunk(buff_raw, sizeof(buff_raw));
  55. char ping_raw[] = "ping";
  56. auto ping = Chunk(ping_raw, sizeof(ping_raw));
  57. char pong_raw[] = "pong";
  58. auto pong = Chunk(pong_raw, sizeof(pong_raw));
  59. MyStack1 stack1;
  60. MyStack2 stack2;
  61. auto &eth_2 = stack2.get<3, 0>();
  62. auto &arp_req_2 = stack2.get<1, 2>();
  63. auto &arp_res_2 = stack2.get<1, 3>();
  64. auto &arp_req_1 = stack1.get<1, 2>();
  65. auto &arp_res_1 = stack1.get<1, 3>();
  66. auto &udp_recv_1 = stack1.get<0, 0>();
  67. auto &udp_conn_2 = stack2.get<0, 0>();
  68. udp_recv_1.init(ep_1);
  69. udp_conn_2.init(ep_2, ep_1);
  70. arp_req_1.init(mac_1, ip_1);
  71. arp_res_1.init(mac_1, ip_1);
  72. arp_req_2.init(mac_2, ip_2);
  73. arp_res_2.init(mac_2, ip_2);
  74. // step 0, attempt to send ping, while arp is not known
  75. auto r2 = udp_conn_2.send(stack2, buff, ping);
  76. REQUIRE(!r2);
  77. REQUIRE(r2.state() == &eth_2);
  78. CHECK(r2.error_code() == (uint32_t)Errc::ethernet_unknown_mac);
  79. // step 1, exchange macs
  80. ArpCache cache1, cache2;
  81. r2 = arp_req_2.send(stack2, buff, ip_1);
  82. REQUIRE(r2);
  83. auto r1 = stack1.recv(r2.consumed(), nullptr);
  84. REQUIRE(r1);
  85. REQUIRE(r1.state() == &arp_req_1);
  86. auto &arp_packet_1 = arp_req_1.get_parsed();
  87. cache1.update(arp_packet_1.sender_mac, arp_packet_1.sender_ip, 5);
  88. r1 = arp_res_1.send(stack1, buff, arp_req_1);
  89. REQUIRE(r1);
  90. r2 = stack2.recv(r1.consumed(), nullptr);
  91. REQUIRE(r2);
  92. REQUIRE(r2.state() == &arp_res_2);
  93. auto &arp_packet_2 = arp_res_2.get_parsed();
  94. cache2.update(arp_packet_2.sender_mac, arp_packet_2.sender_ip, 5);
  95. REQUIRE(*cache1.get(mac_2, 2) == ip_2);
  96. REQUIRE(*cache2.get(mac_1, 2) == ip_1);
  97. // step 2, send "pong", receive "ping"
  98. auto &link_ctx_2 = get_link_context(udp_conn_2);
  99. link_ctx_2.source = mac_2;
  100. link_ctx_2.destination = mac_1;
  101. link_ctx_2.type = ethernet::EtherType::ipv4;
  102. r2 = udp_conn_2.send(stack2, buff, ping);
  103. REQUIRE(r2);
  104. r1 = stack1.recv(r2.consumed(), nullptr);
  105. REQUIRE(r1);
  106. REQUIRE(r1.state() == &udp_recv_1);
  107. auto &udp_packet_1 = udp_recv_1.get_parsed();
  108. CHECK(udp_packet_1.payload == ping);
  109. auto &udp_1_mac = get_link_context(udp_packet_1).source;
  110. auto &udp_1_ip = get_ip_context(udp_packet_1).source;
  111. cache1.update(udp_1_mac, udp_1_ip, 6);
  112. auto link_ctx_1 =
  113. Eth::Context{mac_1, udp_1_mac, ethernet::EtherType::ipv4, nullptr};
  114. get_ip_context(udp_recv_1).link_context = &link_ctx_1;
  115. r1 = udp_recv_1.send(stack1, buff, ep_2, pong);
  116. REQUIRE(r1);
  117. r2 = stack2.recv(r1.consumed(), nullptr);
  118. REQUIRE(r2);
  119. REQUIRE(r2.state() == &udp_conn_2);
  120. auto &udp_packet_2 = udp_conn_2.get_parsed();
  121. CHECK(udp_packet_2.payload == pong);
  122. auto &udp_2_mac = get_link_context(udp_packet_2).source;
  123. auto &udp_2_ip = get_ip_context(udp_packet_2).source;
  124. cache2.update(udp_2_mac, udp_2_ip, 6);
  125. }
  126. TEST_CASE("wrong connection to dup, reply back with arp", "[scenario]") {
  127. char buff_raw[1500];
  128. auto buff = Chunk(buff_raw, sizeof(buff_raw));
  129. char buff_raw_2[1500];
  130. auto buff_2 = Chunk(buff_raw_2, sizeof(buff_raw_2));
  131. char ping_raw[] = "ping";
  132. auto ping = Chunk(ping_raw, sizeof(ping_raw));
  133. MyStack1 stack1;
  134. MyStack2 stack2;
  135. auto &arp_req_2 = stack2.get<1, 2>();
  136. auto &arp_res_2 = stack2.get<1, 3>();
  137. auto &arp_req_1 = stack1.get<1, 2>();
  138. auto &arp_res_1 = stack1.get<1, 3>();
  139. auto &udp_1 = stack1.get<1, 0>();
  140. auto &udp_conn_2 = stack2.get<0, 0>();
  141. auto &unreach_1 = stack1.get<0, 3>();
  142. auto &unreach_2 = stack2.get<0, 3>();
  143. udp_conn_2.init(ep_2, ep_x);
  144. arp_req_1.init(mac_1, ip_1);
  145. arp_res_1.init(mac_1, ip_1);
  146. arp_req_2.init(mac_2, ip_2);
  147. arp_res_2.init(mac_2, ip_2);
  148. unreach_1.init(ip_1);
  149. unreach_2.init(ip_2);
  150. // step 1, exchange macs
  151. ArpCache cache1, cache2;
  152. auto r2 = arp_req_2.send(stack2, buff, ip_1);
  153. REQUIRE(r2);
  154. auto r1 = stack1.recv(r2.consumed(), nullptr);
  155. REQUIRE(r1);
  156. REQUIRE(r1.state() == &arp_req_1);
  157. auto &arp_packet_1 = arp_req_1.get_parsed();
  158. cache1.update(arp_packet_1.sender_mac, arp_packet_1.sender_ip, 5);
  159. r1 = arp_res_1.send(stack1, buff, arp_req_1);
  160. REQUIRE(r1);
  161. r2 = stack2.recv(r1.consumed(), nullptr);
  162. REQUIRE(r2);
  163. REQUIRE(r2.state() == &arp_res_2);
  164. auto &arp_packet_2 = arp_res_2.get_parsed();
  165. cache2.update(arp_packet_2.sender_mac, arp_packet_2.sender_ip, 5);
  166. REQUIRE(*cache1.get(mac_2, 2) == ip_2);
  167. REQUIRE(*cache2.get(mac_1, 2) == ip_1);
  168. // step 2, send "pong", receive bare udp
  169. auto &link_ctx_2 = get_link_context(udp_conn_2);
  170. link_ctx_2.source = mac_2;
  171. link_ctx_2.destination = mac_1;
  172. link_ctx_2.type = ethernet::EtherType::ipv4;
  173. r2 = udp_conn_2.send(stack2, buff, ping);
  174. REQUIRE(r2);
  175. r1 = stack1.recv(r2.consumed(), nullptr);
  176. REQUIRE(r1);
  177. REQUIRE(r1.state() == &udp_1);
  178. auto &udp_packet_1 = udp_1.get_parsed();
  179. CHECK(udp_packet_1.payload == ping);
  180. auto &udp_1_mac = get_link_context(udp_packet_1).source;
  181. auto &udp_1_ip = get_ip_context(udp_packet_1).source;
  182. cache1.update(udp_1_mac, udp_1_ip, 6);
  183. // step 3, send & receive ICMP destination unreacheable
  184. auto &link_ctx_1 = get_link_context(unreach_1);
  185. link_ctx_1.source = mac_1;
  186. link_ctx_1.destination = mac_2;
  187. link_ctx_1.type = ethernet::EtherType::ipv4;
  188. /* buff_1 cannot be used, as we take ip from here */
  189. r1 = unreach_1.send(stack1, buff_2, *udp_packet_1.container, 3);
  190. REQUIRE(r1);
  191. r2 = stack2.recv(r1.consumed(), nullptr);
  192. REQUIRE(r2);
  193. REQUIRE(r2.state() == &unreach_2);
  194. auto &icmp_packet = unreach_2.get_parsed();
  195. REQUIRE(icmp_packet.type == icmp::Type::destination_unreachable);
  196. REQUIRE(icmp_packet.code == 3);
  197. }
  198. TEST_CASE("icmp echo req/res", "[scenario]") {
  199. char buff_raw[1500];
  200. auto buff = Chunk(buff_raw, sizeof(buff_raw));
  201. char ping_raw[] = "ping";
  202. auto ping = Chunk(ping_raw, sizeof(ping_raw));
  203. MyStack1 stack1;
  204. MyStack2 stack2;
  205. auto &arp_req_2 = stack2.get<1, 2>();
  206. auto &arp_res_2 = stack2.get<1, 3>();
  207. auto &arp_req_1 = stack1.get<1, 2>();
  208. auto &arp_res_1 = stack1.get<1, 3>();
  209. auto &echo_res_1 = stack1.get<0, 2>();
  210. auto &echo_res_2 = stack2.get<0, 2>();
  211. auto &echo_req_1 = stack1.get<0, 1>();
  212. auto &echo_req_2 = stack2.get<0, 1>();
  213. arp_req_1.init(mac_1, ip_1);
  214. arp_res_1.init(mac_1, ip_1);
  215. arp_req_2.init(mac_2, ip_2);
  216. arp_res_2.init(mac_2, ip_2);
  217. echo_req_1.init(ip_1);
  218. echo_req_2.init(ip_2);
  219. echo_res_1.init(ip_1);
  220. echo_res_2.init(ip_2);
  221. // step 1, exchange macs
  222. ArpCache cache1, cache2;
  223. auto r2 = arp_req_2.send(stack2, buff, ip_1);
  224. REQUIRE(r2);
  225. auto r1 = stack1.recv(r2.consumed(), nullptr);
  226. REQUIRE(r1);
  227. REQUIRE(r1.state() == &arp_req_1);
  228. auto &arp_packet_1 = arp_req_1.get_parsed();
  229. cache1.update(arp_packet_1.sender_mac, arp_packet_1.sender_ip, 5);
  230. r1 = arp_res_1.send(stack1, buff, arp_req_1);
  231. REQUIRE(r1);
  232. r2 = stack2.recv(r1.consumed(), nullptr);
  233. REQUIRE(r2);
  234. REQUIRE(r2.state() == &arp_res_2);
  235. auto &arp_packet_2 = arp_res_2.get_parsed();
  236. cache2.update(arp_packet_2.sender_mac, arp_packet_2.sender_ip, 5);
  237. REQUIRE(*cache1.get(mac_2, 2) == ip_2);
  238. REQUIRE(*cache2.get(mac_1, 2) == ip_1);
  239. // step 2, send & receive echo request
  240. auto &link_ctx_2 = get_link_context(echo_req_2);
  241. link_ctx_2.source = mac_2;
  242. link_ctx_2.destination = mac_1;
  243. link_ctx_2.type = ethernet::EtherType::ipv4;
  244. r2 = echo_req_2.send(stack2, buff, ip_1, ping);
  245. REQUIRE(r2);
  246. r1 = stack1.recv(r2.consumed(), nullptr);
  247. REQUIRE(r1);
  248. REQUIRE(r1.state() == &echo_req_1);
  249. auto &echo_packet_1 = echo_req_1.get_parsed();
  250. auto &ip_packet = *echo_packet_1.container->container;
  251. CHECK(ip_packet.source == ip_2);
  252. CHECK(ip_packet.destination == ip_1);
  253. REQUIRE(echo_packet_1.payload == ping);
  254. // step 3, send & receve echo response
  255. auto &link_ctx_1 = get_link_context(echo_res_1);
  256. link_ctx_1.source = mac_1;
  257. link_ctx_1.destination = mac_2;
  258. link_ctx_1.type = ethernet::EtherType::ipv4;
  259. r1 = echo_res_1.send(stack1, buff, echo_packet_1);
  260. REQUIRE(r1);
  261. r2 = stack2.recv(r1.consumed(), nullptr);
  262. REQUIRE(r2);
  263. REQUIRE(r2.state() == &echo_res_2);
  264. auto &echo_packet_2 = echo_res_2.get_parsed();
  265. ip_packet = *echo_packet_2.container->container;
  266. CHECK(ip_packet.source == ip_1);
  267. CHECK(ip_packet.destination == ip_2);
  268. REQUIRE(echo_packet_2.payload == ping);
  269. }