unsigned-md5.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Author: Dmitry Safonov <dima@arista.com> */
  3. #include <inttypes.h>
  4. #include "aolib.h"
  5. #define fault(type) (inj == FAULT_ ## type)
  6. static const char *md5_password = "Some evil genius, enemy to mankind, must have been the first contriver.";
  7. static const char *ao_password = DEFAULT_TEST_PASSWORD;
  8. static volatile int sk_pair;
  9. static union tcp_addr client2;
  10. static union tcp_addr client3;
  11. static const int test_vrf_ifindex = 200;
  12. static const uint8_t test_vrf_tabid = 42;
  13. static void setup_vrfs(void)
  14. {
  15. int err;
  16. if (!kernel_config_has(KCONFIG_NET_VRF))
  17. return;
  18. err = add_vrf("ksft-vrf", test_vrf_tabid, test_vrf_ifindex, -1);
  19. if (err)
  20. test_error("Failed to add a VRF: %d", err);
  21. err = link_set_up("ksft-vrf");
  22. if (err)
  23. test_error("Failed to bring up a VRF");
  24. err = ip_route_add_vrf(veth_name, TEST_FAMILY,
  25. this_ip_addr, this_ip_dest, test_vrf_tabid);
  26. if (err)
  27. test_error("Failed to add a route to VRF: %d", err);
  28. }
  29. static void try_accept(const char *tst_name, unsigned int port,
  30. union tcp_addr *md5_addr, uint8_t md5_prefix,
  31. union tcp_addr *ao_addr, uint8_t ao_prefix,
  32. bool set_ao_required,
  33. uint8_t sndid, uint8_t rcvid, uint8_t vrf,
  34. const char *cnt_name, test_cnt cnt_expected,
  35. int needs_tcp_md5, fault_t inj)
  36. {
  37. struct tcp_counters cnt1, cnt2;
  38. uint64_t before_cnt = 0, after_cnt = 0; /* silence GCC */
  39. test_cnt poll_cnt = (cnt_expected == TEST_CNT_GOOD) ? 0 : cnt_expected;
  40. int lsk, err, sk = -1;
  41. if (needs_tcp_md5 && should_skip_test(tst_name, KCONFIG_TCP_MD5))
  42. return;
  43. lsk = test_listen_socket(this_ip_addr, port, 1);
  44. if (md5_addr && test_set_md5(lsk, *md5_addr, md5_prefix, -1, md5_password))
  45. test_error("setsockopt(TCP_MD5SIG_EXT)");
  46. if (ao_addr && test_add_key(lsk, ao_password,
  47. *ao_addr, ao_prefix, sndid, rcvid))
  48. test_error("setsockopt(TCP_AO_ADD_KEY)");
  49. if (set_ao_required && test_set_ao_flags(lsk, true, false))
  50. test_error("setsockopt(TCP_AO_INFO)");
  51. if (cnt_name)
  52. before_cnt = netstat_get_one(cnt_name, NULL);
  53. if (ao_addr && test_get_tcp_counters(lsk, &cnt1))
  54. test_error("test_get_tcp_counters()");
  55. synchronize_threads(); /* preparations done */
  56. err = test_skpair_wait_poll(lsk, 0, poll_cnt, &sk_pair);
  57. synchronize_threads(); /* connect()/accept() timeouts */
  58. if (err == -ETIMEDOUT) {
  59. sk_pair = err;
  60. if (!fault(TIMEOUT))
  61. test_fail("%s: timed out for accept()", tst_name);
  62. } else if (err == -EKEYREJECTED) {
  63. if (!fault(KEYREJECT))
  64. test_fail("%s: key was rejected", tst_name);
  65. } else if (err < 0) {
  66. test_error("test_skpair_wait_poll()");
  67. } else {
  68. if (fault(TIMEOUT))
  69. test_fail("%s: ready to accept", tst_name);
  70. sk = accept(lsk, NULL, NULL);
  71. if (sk < 0) {
  72. test_error("accept()");
  73. } else {
  74. if (fault(TIMEOUT))
  75. test_fail("%s: accepted", tst_name);
  76. }
  77. }
  78. if (ao_addr && test_get_tcp_counters(lsk, &cnt2))
  79. test_error("test_get_tcp_counters()");
  80. close(lsk);
  81. if (!cnt_name) {
  82. test_ok("%s: no counter checks", tst_name);
  83. goto out;
  84. }
  85. after_cnt = netstat_get_one(cnt_name, NULL);
  86. if (after_cnt <= before_cnt) {
  87. test_fail("%s: %s counter did not increase: %" PRIu64 " <= %" PRIu64,
  88. tst_name, cnt_name, after_cnt, before_cnt);
  89. } else {
  90. test_ok("%s: counter %s increased %" PRIu64 " => %" PRIu64,
  91. tst_name, cnt_name, before_cnt, after_cnt);
  92. }
  93. if (ao_addr)
  94. test_assert_counters(tst_name, &cnt1, &cnt2, cnt_expected);
  95. out:
  96. synchronize_threads(); /* test_kill_sk() */
  97. if (sk >= 0)
  98. test_kill_sk(sk);
  99. }
  100. static void server_add_routes(void)
  101. {
  102. int family = TEST_FAMILY;
  103. synchronize_threads(); /* client_add_ips() */
  104. if (ip_route_add(veth_name, family, this_ip_addr, client2))
  105. test_error("Failed to add route");
  106. if (ip_route_add(veth_name, family, this_ip_addr, client3))
  107. test_error("Failed to add route");
  108. }
  109. static void server_add_fail_tests(unsigned int *port)
  110. {
  111. union tcp_addr addr_any = {};
  112. try_accept("TCP-AO established: add TCP-MD5 key", (*port)++, NULL, 0,
  113. &addr_any, 0, 0, 100, 100, 0, "TCPAOGood", TEST_CNT_GOOD,
  114. 1, 0);
  115. try_accept("TCP-MD5 established: add TCP-AO key", (*port)++, &addr_any,
  116. 0, NULL, 0, 0, 0, 0, 0, NULL, 0, 1, 0);
  117. try_accept("non-signed established: add TCP-AO key", (*port)++, NULL, 0,
  118. NULL, 0, 0, 0, 0, 0, "CurrEstab", 0, 0, 0);
  119. }
  120. static void server_vrf_tests(unsigned int *port)
  121. {
  122. setup_vrfs();
  123. }
  124. static void *server_fn(void *arg)
  125. {
  126. unsigned int port = test_server_port;
  127. union tcp_addr addr_any = {};
  128. server_add_routes();
  129. try_accept("[server] AO server (INADDR_ANY): AO client", port++, NULL, 0,
  130. &addr_any, 0, 0, 100, 100, 0, "TCPAOGood",
  131. TEST_CNT_GOOD, 0, 0);
  132. try_accept("[server] AO server (INADDR_ANY): MD5 client", port++, NULL, 0,
  133. &addr_any, 0, 0, 100, 100, 0, "TCPMD5Unexpected",
  134. TEST_CNT_NS_MD5_UNEXPECTED, 1, FAULT_TIMEOUT);
  135. try_accept("[server] AO server (INADDR_ANY): no sign client", port++, NULL, 0,
  136. &addr_any, 0, 0, 100, 100, 0, "TCPAORequired",
  137. TEST_CNT_AO_REQUIRED, 0, FAULT_TIMEOUT);
  138. try_accept("[server] AO server (AO_REQUIRED): AO client", port++, NULL, 0,
  139. &this_ip_dest, TEST_PREFIX, true,
  140. 100, 100, 0, "TCPAOGood", TEST_CNT_GOOD, 0, 0);
  141. try_accept("[server] AO server (AO_REQUIRED): unsigned client", port++, NULL, 0,
  142. &this_ip_dest, TEST_PREFIX, true,
  143. 100, 100, 0, "TCPAORequired",
  144. TEST_CNT_AO_REQUIRED, 0, FAULT_TIMEOUT);
  145. try_accept("[server] MD5 server (INADDR_ANY): AO client", port++, &addr_any, 0,
  146. NULL, 0, 0, 0, 0, 0, "TCPAOKeyNotFound",
  147. TEST_CNT_NS_KEY_NOT_FOUND, 1, FAULT_TIMEOUT);
  148. try_accept("[server] MD5 server (INADDR_ANY): MD5 client", port++, &addr_any, 0,
  149. NULL, 0, 0, 0, 0, 0, NULL, 0, 1, 0);
  150. try_accept("[server] MD5 server (INADDR_ANY): no sign client", port++, &addr_any,
  151. 0, NULL, 0, 0, 0, 0, 0, "TCPMD5NotFound",
  152. TEST_CNT_NS_MD5_NOT_FOUND, 1, FAULT_TIMEOUT);
  153. try_accept("[server] no sign server: AO client", port++, NULL, 0,
  154. NULL, 0, 0, 0, 0, 0, "TCPAOKeyNotFound",
  155. TEST_CNT_NS_KEY_NOT_FOUND, 0, FAULT_TIMEOUT);
  156. try_accept("[server] no sign server: MD5 client", port++, NULL, 0,
  157. NULL, 0, 0, 0, 0, 0, "TCPMD5Unexpected",
  158. TEST_CNT_NS_MD5_UNEXPECTED, 1, FAULT_TIMEOUT);
  159. try_accept("[server] no sign server: no sign client", port++, NULL, 0,
  160. NULL, 0, 0, 0, 0, 0, "CurrEstab", 0, 0, 0);
  161. try_accept("[server] AO+MD5 server: AO client (matching)", port++,
  162. &this_ip_dest, TEST_PREFIX, &client2, TEST_PREFIX, 0,
  163. 100, 100, 0, "TCPAOGood", TEST_CNT_GOOD, 1, 0);
  164. try_accept("[server] AO+MD5 server: AO client (misconfig, matching MD5)", port++,
  165. &this_ip_dest, TEST_PREFIX, &client2, TEST_PREFIX, 0,
  166. 100, 100, 0, "TCPAOKeyNotFound", TEST_CNT_AO_KEY_NOT_FOUND,
  167. 1, FAULT_TIMEOUT);
  168. try_accept("[server] AO+MD5 server: AO client (misconfig, non-matching)", port++,
  169. &this_ip_dest, TEST_PREFIX, &client2, TEST_PREFIX, 0,
  170. 100, 100, 0, "TCPAOKeyNotFound", TEST_CNT_AO_KEY_NOT_FOUND,
  171. 1, FAULT_TIMEOUT);
  172. try_accept("[server] AO+MD5 server: MD5 client (matching)", port++,
  173. &this_ip_dest, TEST_PREFIX, &client2, TEST_PREFIX, 0,
  174. 100, 100, 0, NULL, 0, 1, 0);
  175. try_accept("[server] AO+MD5 server: MD5 client (misconfig, matching AO)", port++,
  176. &this_ip_dest, TEST_PREFIX, &client2, TEST_PREFIX, 0,
  177. 100, 100, 0, "TCPMD5Unexpected",
  178. TEST_CNT_NS_MD5_UNEXPECTED, 1, FAULT_TIMEOUT);
  179. try_accept("[server] AO+MD5 server: MD5 client (misconfig, non-matching)", port++,
  180. &this_ip_dest, TEST_PREFIX, &client2, TEST_PREFIX, 0,
  181. 100, 100, 0, "TCPMD5Unexpected",
  182. TEST_CNT_NS_MD5_UNEXPECTED, 1, FAULT_TIMEOUT);
  183. try_accept("[server] AO+MD5 server: no sign client (unmatched)", port++,
  184. &this_ip_dest, TEST_PREFIX, &client2, TEST_PREFIX, 0,
  185. 100, 100, 0, "CurrEstab", 0, 1, 0);
  186. try_accept("[server] AO+MD5 server: no sign client (misconfig, matching AO)",
  187. port++, &this_ip_dest, TEST_PREFIX, &client2, TEST_PREFIX, 0,
  188. 100, 100, 0, "TCPAORequired",
  189. TEST_CNT_AO_REQUIRED, 1, FAULT_TIMEOUT);
  190. try_accept("[server] AO+MD5 server: no sign client (misconfig, matching MD5)",
  191. port++, &this_ip_dest, TEST_PREFIX, &client2, TEST_PREFIX, 0,
  192. 100, 100, 0, "TCPMD5NotFound",
  193. TEST_CNT_NS_MD5_NOT_FOUND, 1, FAULT_TIMEOUT);
  194. /* Key rejected by the other side, failing short through skpair */
  195. try_accept("[server] AO+MD5 server: client with both [TCP-MD5] and TCP-AO keys",
  196. port++, &this_ip_dest, TEST_PREFIX, &client2, TEST_PREFIX, 0,
  197. 100, 100, 0, NULL, 0, 1, FAULT_KEYREJECT);
  198. try_accept("[server] AO+MD5 server: client with both TCP-MD5 and [TCP-AO] keys",
  199. port++, &this_ip_dest, TEST_PREFIX, &client2, TEST_PREFIX, 0,
  200. 100, 100, 0, NULL, 0, 1, FAULT_KEYREJECT);
  201. server_add_fail_tests(&port);
  202. server_vrf_tests(&port);
  203. /* client exits */
  204. synchronize_threads();
  205. return NULL;
  206. }
  207. static int client_bind(int sk, union tcp_addr bind_addr)
  208. {
  209. #ifdef IPV6_TEST
  210. struct sockaddr_in6 addr = {
  211. .sin6_family = AF_INET6,
  212. .sin6_port = 0,
  213. .sin6_addr = bind_addr.a6,
  214. };
  215. #else
  216. struct sockaddr_in addr = {
  217. .sin_family = AF_INET,
  218. .sin_port = 0,
  219. .sin_addr = bind_addr.a4,
  220. };
  221. #endif
  222. return bind(sk, &addr, sizeof(addr));
  223. }
  224. static void try_connect(const char *tst_name, unsigned int port,
  225. union tcp_addr *md5_addr, uint8_t md5_prefix,
  226. union tcp_addr *ao_addr, uint8_t ao_prefix,
  227. uint8_t sndid, uint8_t rcvid, uint8_t vrf,
  228. fault_t inj, int needs_tcp_md5, union tcp_addr *bind_addr)
  229. {
  230. int sk, ret;
  231. if (needs_tcp_md5 && should_skip_test(tst_name, KCONFIG_TCP_MD5))
  232. return;
  233. sk = socket(test_family, SOCK_STREAM, IPPROTO_TCP);
  234. if (sk < 0)
  235. test_error("socket()");
  236. if (bind_addr && client_bind(sk, *bind_addr))
  237. test_error("bind()");
  238. if (md5_addr && test_set_md5(sk, *md5_addr, md5_prefix, -1, md5_password))
  239. test_error("setsockopt(TCP_MD5SIG_EXT)");
  240. if (ao_addr && test_add_key(sk, ao_password, *ao_addr,
  241. ao_prefix, sndid, rcvid))
  242. test_error("setsockopt(TCP_AO_ADD_KEY)");
  243. synchronize_threads(); /* preparations done */
  244. ret = test_skpair_connect_poll(sk, this_ip_dest, port, 0, &sk_pair);
  245. synchronize_threads(); /* connect()/accept() timeouts */
  246. if (ret < 0) {
  247. sk_pair = ret;
  248. if (fault(KEYREJECT) && ret == -EKEYREJECTED)
  249. test_ok("%s: connect() was prevented", tst_name);
  250. else if (ret == -ETIMEDOUT && fault(TIMEOUT))
  251. test_ok("%s", tst_name);
  252. else if (ret == -ECONNREFUSED &&
  253. (fault(TIMEOUT) || fault(KEYREJECT)))
  254. test_ok("%s: refused to connect", tst_name);
  255. else
  256. test_error("%s: connect() returned %d", tst_name, ret);
  257. goto out;
  258. }
  259. if (fault(TIMEOUT) || fault(KEYREJECT))
  260. test_fail("%s: connected", tst_name);
  261. else
  262. test_ok("%s: connected", tst_name);
  263. out:
  264. synchronize_threads(); /* test_kill_sk() */
  265. if (ret > 0) /* test_skpair_connect_poll() cleans up on failure */
  266. test_kill_sk(sk);
  267. }
  268. #define PREINSTALL_MD5_FIRST BIT(0)
  269. #define PREINSTALL_AO BIT(1)
  270. #define POSTINSTALL_AO BIT(2)
  271. #define PREINSTALL_MD5 BIT(3)
  272. #define POSTINSTALL_MD5 BIT(4)
  273. static int try_add_key_vrf(int sk, union tcp_addr in_addr, uint8_t prefix,
  274. int vrf, uint8_t sndid, uint8_t rcvid,
  275. bool set_ao_required)
  276. {
  277. uint8_t keyflags = 0;
  278. if (vrf >= 0)
  279. keyflags |= TCP_AO_KEYF_IFINDEX;
  280. else
  281. vrf = 0;
  282. if (set_ao_required) {
  283. int err = test_set_ao_flags(sk, true, 0);
  284. if (err)
  285. return err;
  286. }
  287. return test_add_key_vrf(sk, ao_password, keyflags, in_addr, prefix,
  288. (uint8_t)vrf, sndid, rcvid);
  289. }
  290. static bool test_continue(const char *tst_name, int err,
  291. fault_t inj, bool added_ao)
  292. {
  293. bool expected_to_fail;
  294. expected_to_fail = fault(PREINSTALL_AO) && added_ao;
  295. expected_to_fail |= fault(PREINSTALL_MD5) && !added_ao;
  296. if (!err) {
  297. if (!expected_to_fail)
  298. return true;
  299. test_fail("%s: setsockopt()s were expected to fail", tst_name);
  300. return false;
  301. }
  302. if (err != -EKEYREJECTED || !expected_to_fail) {
  303. test_error("%s: setsockopt(%s) = %d", tst_name,
  304. added_ao ? "TCP_AO_ADD_KEY" : "TCP_MD5SIG_EXT", err);
  305. return false;
  306. }
  307. test_ok("%s: prefailed as expected: %m", tst_name);
  308. return false;
  309. }
  310. static int open_add(const char *tst_name, unsigned int port,
  311. unsigned int strategy,
  312. union tcp_addr md5_addr, uint8_t md5_prefix, int md5_vrf,
  313. union tcp_addr ao_addr, uint8_t ao_prefix,
  314. int ao_vrf, bool set_ao_required,
  315. uint8_t sndid, uint8_t rcvid,
  316. fault_t inj)
  317. {
  318. int sk;
  319. sk = socket(test_family, SOCK_STREAM, IPPROTO_TCP);
  320. if (sk < 0)
  321. test_error("socket()");
  322. if (client_bind(sk, this_ip_addr))
  323. test_error("bind()");
  324. if (strategy & PREINSTALL_MD5_FIRST) {
  325. if (test_set_md5(sk, md5_addr, md5_prefix, md5_vrf, md5_password))
  326. test_error("setsockopt(TCP_MD5SIG_EXT)");
  327. }
  328. if (strategy & PREINSTALL_AO) {
  329. int err = try_add_key_vrf(sk, ao_addr, ao_prefix, ao_vrf,
  330. sndid, rcvid, set_ao_required);
  331. if (!test_continue(tst_name, err, inj, true)) {
  332. close(sk);
  333. return -1;
  334. }
  335. }
  336. if (strategy & PREINSTALL_MD5) {
  337. errno = 0;
  338. test_set_md5(sk, md5_addr, md5_prefix, md5_vrf, md5_password);
  339. if (!test_continue(tst_name, -errno, inj, false)) {
  340. close(sk);
  341. return -1;
  342. }
  343. }
  344. return sk;
  345. }
  346. static void try_to_preadd(const char *tst_name, unsigned int port,
  347. unsigned int strategy,
  348. union tcp_addr md5_addr, uint8_t md5_prefix,
  349. int md5_vrf,
  350. union tcp_addr ao_addr, uint8_t ao_prefix,
  351. int ao_vrf, bool set_ao_required,
  352. uint8_t sndid, uint8_t rcvid,
  353. int needs_tcp_md5, int needs_vrf, fault_t inj)
  354. {
  355. int sk;
  356. if (needs_tcp_md5 && should_skip_test(tst_name, KCONFIG_TCP_MD5))
  357. return;
  358. if (needs_vrf && should_skip_test(tst_name, KCONFIG_NET_VRF))
  359. return;
  360. sk = open_add(tst_name, port, strategy, md5_addr, md5_prefix, md5_vrf,
  361. ao_addr, ao_prefix, ao_vrf, set_ao_required,
  362. sndid, rcvid, inj);
  363. if (sk < 0)
  364. return;
  365. test_ok("%s", tst_name);
  366. close(sk);
  367. }
  368. static void try_to_add(const char *tst_name, unsigned int port,
  369. unsigned int strategy,
  370. union tcp_addr md5_addr, uint8_t md5_prefix,
  371. int md5_vrf,
  372. union tcp_addr ao_addr, uint8_t ao_prefix,
  373. int ao_vrf, uint8_t sndid, uint8_t rcvid,
  374. int needs_tcp_md5, fault_t inj)
  375. {
  376. int sk, ret;
  377. if (needs_tcp_md5 && should_skip_test(tst_name, KCONFIG_TCP_MD5))
  378. return;
  379. sk = open_add(tst_name, port, strategy, md5_addr, md5_prefix, md5_vrf,
  380. ao_addr, ao_prefix, ao_vrf, 0, sndid, rcvid, inj);
  381. if (sk < 0)
  382. return;
  383. synchronize_threads(); /* preparations done */
  384. ret = test_skpair_connect_poll(sk, this_ip_dest, port, 0, &sk_pair);
  385. synchronize_threads(); /* connect()/accept() timeouts */
  386. if (ret < 0) {
  387. test_error("%s: connect() returned %d", tst_name, ret);
  388. goto out;
  389. }
  390. if (strategy & POSTINSTALL_MD5) {
  391. if (test_set_md5(sk, md5_addr, md5_prefix, md5_vrf, md5_password)) {
  392. if (fault(POSTINSTALL)) {
  393. test_ok("%s: postfailed as expected", tst_name);
  394. goto out;
  395. } else {
  396. test_error("setsockopt(TCP_MD5SIG_EXT)");
  397. }
  398. } else if (fault(POSTINSTALL)) {
  399. test_fail("%s: post setsockopt() was expected to fail", tst_name);
  400. goto out;
  401. }
  402. }
  403. if (strategy & POSTINSTALL_AO) {
  404. if (try_add_key_vrf(sk, ao_addr, ao_prefix, ao_vrf,
  405. sndid, rcvid, 0)) {
  406. if (fault(POSTINSTALL)) {
  407. test_ok("%s: postfailed as expected", tst_name);
  408. goto out;
  409. } else {
  410. test_error("setsockopt(TCP_AO_ADD_KEY)");
  411. }
  412. } else if (fault(POSTINSTALL)) {
  413. test_fail("%s: post setsockopt() was expected to fail", tst_name);
  414. goto out;
  415. }
  416. }
  417. out:
  418. synchronize_threads(); /* test_kill_sk() */
  419. if (ret > 0) /* test_skpair_connect_poll() cleans up on failure */
  420. test_kill_sk(sk);
  421. }
  422. static void client_add_ip(union tcp_addr *client, const char *ip)
  423. {
  424. int err, family = TEST_FAMILY;
  425. if (inet_pton(family, ip, client) != 1)
  426. test_error("Can't convert ip address %s", ip);
  427. err = ip_addr_add(veth_name, family, *client, TEST_PREFIX);
  428. if (err)
  429. test_error("Failed to add ip address: %d", err);
  430. }
  431. static void client_add_ips(void)
  432. {
  433. client_add_ip(&client2, __TEST_CLIENT_IP(2));
  434. client_add_ip(&client3, __TEST_CLIENT_IP(3));
  435. synchronize_threads(); /* server_add_routes() */
  436. }
  437. static void client_add_fail_tests(unsigned int *port)
  438. {
  439. try_to_add("TCP-AO established: add TCP-MD5 key",
  440. (*port)++, POSTINSTALL_MD5 | PREINSTALL_AO,
  441. this_ip_dest, TEST_PREFIX, -1, this_ip_dest, TEST_PREFIX, 0,
  442. 100, 100, 1, FAULT_POSTINSTALL);
  443. try_to_add("TCP-MD5 established: add TCP-AO key",
  444. (*port)++, PREINSTALL_MD5 | POSTINSTALL_AO,
  445. this_ip_dest, TEST_PREFIX, -1, this_ip_dest, TEST_PREFIX, 0,
  446. 100, 100, 1, FAULT_POSTINSTALL);
  447. try_to_add("non-signed established: add TCP-AO key",
  448. (*port)++, POSTINSTALL_AO,
  449. this_ip_dest, TEST_PREFIX, -1, this_ip_dest, TEST_PREFIX, 0,
  450. 100, 100, 0, FAULT_POSTINSTALL);
  451. try_to_add("TCP-AO key intersects with existing TCP-MD5 key",
  452. (*port)++, PREINSTALL_MD5_FIRST | PREINSTALL_AO,
  453. this_ip_addr, TEST_PREFIX, -1, this_ip_addr, TEST_PREFIX, -1,
  454. 100, 100, 1, FAULT_PREINSTALL_AO);
  455. try_to_add("TCP-MD5 key intersects with existing TCP-AO key",
  456. (*port)++, PREINSTALL_MD5 | PREINSTALL_AO,
  457. this_ip_addr, TEST_PREFIX, -1, this_ip_addr, TEST_PREFIX, -1,
  458. 100, 100, 1, FAULT_PREINSTALL_MD5);
  459. try_to_preadd("TCP-MD5 key + TCP-AO required",
  460. (*port)++, PREINSTALL_MD5_FIRST | PREINSTALL_AO,
  461. this_ip_addr, TEST_PREFIX, -1,
  462. this_ip_addr, TEST_PREFIX, -1, true,
  463. 100, 100, 1, 0, FAULT_PREINSTALL_AO);
  464. try_to_preadd("TCP-AO required on socket + TCP-MD5 key",
  465. (*port)++, PREINSTALL_MD5 | PREINSTALL_AO,
  466. this_ip_addr, TEST_PREFIX, -1,
  467. this_ip_addr, TEST_PREFIX, -1, true,
  468. 100, 100, 1, 0, FAULT_PREINSTALL_MD5);
  469. }
  470. static void client_vrf_tests(unsigned int *port)
  471. {
  472. setup_vrfs();
  473. /* The following restrictions for setsockopt()s are expected:
  474. *
  475. * |--------------|-----------------|-------------|-------------|
  476. * | | MD5 key without | MD5 key | MD5 key |
  477. * | | l3index | l3index=0 | l3index=N |
  478. * |--------------|-----------------|-------------|-------------|
  479. * | TCP-AO key | | | |
  480. * | without | reject | reject | reject |
  481. * | l3index | | | |
  482. * |--------------|-----------------|-------------|-------------|
  483. * | TCP-AO key | | | |
  484. * | l3index=0 | reject | reject | allow |
  485. * |--------------|-----------------|-------------|-------------|
  486. * | TCP-AO key | | | |
  487. * | l3index=N | reject | allow | reject |
  488. * |--------------|-----------------|-------------|-------------|
  489. */
  490. try_to_preadd("VRF: TCP-AO key (no l3index) + TCP-MD5 key (no l3index)",
  491. (*port)++, PREINSTALL_MD5 | PREINSTALL_AO,
  492. this_ip_addr, TEST_PREFIX, -1,
  493. this_ip_addr, TEST_PREFIX, -1, 0, 100, 100,
  494. 1, 1, FAULT_PREINSTALL_MD5);
  495. try_to_preadd("VRF: TCP-MD5 key (no l3index) + TCP-AO key (no l3index)",
  496. (*port)++, PREINSTALL_MD5_FIRST | PREINSTALL_AO,
  497. this_ip_addr, TEST_PREFIX, -1,
  498. this_ip_addr, TEST_PREFIX, -1, 0, 100, 100,
  499. 1, 1, FAULT_PREINSTALL_AO);
  500. try_to_preadd("VRF: TCP-AO key (no l3index) + TCP-MD5 key (l3index=0)",
  501. (*port)++, PREINSTALL_MD5 | PREINSTALL_AO,
  502. this_ip_addr, TEST_PREFIX, 0,
  503. this_ip_addr, TEST_PREFIX, -1, 0, 100, 100,
  504. 1, 1, FAULT_PREINSTALL_MD5);
  505. try_to_preadd("VRF: TCP-MD5 key (l3index=0) + TCP-AO key (no l3index)",
  506. (*port)++, PREINSTALL_MD5_FIRST | PREINSTALL_AO,
  507. this_ip_addr, TEST_PREFIX, 0,
  508. this_ip_addr, TEST_PREFIX, -1, 0, 100, 100,
  509. 1, 1, FAULT_PREINSTALL_AO);
  510. try_to_preadd("VRF: TCP-AO key (no l3index) + TCP-MD5 key (l3index=N)",
  511. (*port)++, PREINSTALL_MD5 | PREINSTALL_AO,
  512. this_ip_addr, TEST_PREFIX, test_vrf_ifindex,
  513. this_ip_addr, TEST_PREFIX, -1, 0, 100, 100,
  514. 1, 1, FAULT_PREINSTALL_MD5);
  515. try_to_preadd("VRF: TCP-MD5 key (l3index=N) + TCP-AO key (no l3index)",
  516. (*port)++, PREINSTALL_MD5_FIRST | PREINSTALL_AO,
  517. this_ip_addr, TEST_PREFIX, test_vrf_ifindex,
  518. this_ip_addr, TEST_PREFIX, -1, 0, 100, 100,
  519. 1, 1, FAULT_PREINSTALL_AO);
  520. try_to_preadd("VRF: TCP-AO key (l3index=0) + TCP-MD5 key (no l3index)",
  521. (*port)++, PREINSTALL_MD5 | PREINSTALL_AO,
  522. this_ip_addr, TEST_PREFIX, -1,
  523. this_ip_addr, TEST_PREFIX, 0, 0, 100, 100,
  524. 1, 1, FAULT_PREINSTALL_MD5);
  525. try_to_preadd("VRF: TCP-MD5 key (no l3index) + TCP-AO key (l3index=0)",
  526. (*port)++, PREINSTALL_MD5_FIRST | PREINSTALL_AO,
  527. this_ip_addr, TEST_PREFIX, -1,
  528. this_ip_addr, TEST_PREFIX, 0, 0, 100, 100,
  529. 1, 1, FAULT_PREINSTALL_AO);
  530. try_to_preadd("VRF: TCP-AO key (l3index=0) + TCP-MD5 key (l3index=0)",
  531. (*port)++, PREINSTALL_MD5 | PREINSTALL_AO,
  532. this_ip_addr, TEST_PREFIX, 0,
  533. this_ip_addr, TEST_PREFIX, 0, 0, 100, 100,
  534. 1, 1, FAULT_PREINSTALL_MD5);
  535. try_to_preadd("VRF: TCP-MD5 key (l3index=0) + TCP-AO key (l3index=0)",
  536. (*port)++, PREINSTALL_MD5_FIRST | PREINSTALL_AO,
  537. this_ip_addr, TEST_PREFIX, 0,
  538. this_ip_addr, TEST_PREFIX, 0, 0, 100, 100,
  539. 1, 1, FAULT_PREINSTALL_AO);
  540. try_to_preadd("VRF: TCP-AO key (l3index=0) + TCP-MD5 key (l3index=N)",
  541. (*port)++, PREINSTALL_MD5 | PREINSTALL_AO,
  542. this_ip_addr, TEST_PREFIX, test_vrf_ifindex,
  543. this_ip_addr, TEST_PREFIX, 0, 0, 100, 100,
  544. 1, 1, 0);
  545. try_to_preadd("VRF: TCP-MD5 key (l3index=N) + TCP-AO key (l3index=0)",
  546. (*port)++, PREINSTALL_MD5_FIRST | PREINSTALL_AO,
  547. this_ip_addr, TEST_PREFIX, test_vrf_ifindex,
  548. this_ip_addr, TEST_PREFIX, 0, 0, 100, 100,
  549. 1, 1, 0);
  550. try_to_preadd("VRF: TCP-AO key (l3index=N) + TCP-MD5 key (no l3index)",
  551. (*port)++, PREINSTALL_MD5 | PREINSTALL_AO,
  552. this_ip_addr, TEST_PREFIX, test_vrf_ifindex,
  553. this_ip_addr, TEST_PREFIX, -1, 0, 100, 100,
  554. 1, 1, FAULT_PREINSTALL_MD5);
  555. try_to_preadd("VRF: TCP-MD5 key (no l3index) + TCP-AO key (l3index=N)",
  556. (*port)++, PREINSTALL_MD5_FIRST | PREINSTALL_AO,
  557. this_ip_addr, TEST_PREFIX, -1,
  558. this_ip_addr, TEST_PREFIX, test_vrf_ifindex, 0, 100, 100,
  559. 1, 1, FAULT_PREINSTALL_AO);
  560. try_to_preadd("VRF: TCP-AO key (l3index=N) + TCP-MD5 key (l3index=0)",
  561. (*port)++, PREINSTALL_MD5 | PREINSTALL_AO,
  562. this_ip_addr, TEST_PREFIX, 0,
  563. this_ip_addr, TEST_PREFIX, test_vrf_ifindex, 0, 100, 100,
  564. 1, 1, 0);
  565. try_to_preadd("VRF: TCP-MD5 key (l3index=0) + TCP-AO key (l3index=N)",
  566. (*port)++, PREINSTALL_MD5_FIRST | PREINSTALL_AO,
  567. this_ip_addr, TEST_PREFIX, 0,
  568. this_ip_addr, TEST_PREFIX, test_vrf_ifindex, 0, 100, 100,
  569. 1, 1, 0);
  570. try_to_preadd("VRF: TCP-AO key (l3index=N) + TCP-MD5 key (l3index=N)",
  571. (*port)++, PREINSTALL_MD5 | PREINSTALL_AO,
  572. this_ip_addr, TEST_PREFIX, test_vrf_ifindex,
  573. this_ip_addr, TEST_PREFIX, test_vrf_ifindex, 0, 100, 100,
  574. 1, 1, FAULT_PREINSTALL_MD5);
  575. try_to_preadd("VRF: TCP-MD5 key (l3index=N) + TCP-AO key (l3index=N)",
  576. (*port)++, PREINSTALL_MD5_FIRST | PREINSTALL_AO,
  577. this_ip_addr, TEST_PREFIX, test_vrf_ifindex,
  578. this_ip_addr, TEST_PREFIX, test_vrf_ifindex, 0, 100, 100,
  579. 1, 1, FAULT_PREINSTALL_AO);
  580. }
  581. static void *client_fn(void *arg)
  582. {
  583. unsigned int port = test_server_port;
  584. union tcp_addr addr_any = {};
  585. client_add_ips();
  586. try_connect("AO server (INADDR_ANY): AO client", port++, NULL, 0,
  587. &addr_any, 0, 100, 100, 0, 0, 0, &this_ip_addr);
  588. trace_hash_event_expect(TCP_HASH_MD5_UNEXPECTED, this_ip_addr,
  589. this_ip_dest, -1, port, 0, 0, 1, 0, 0, 0);
  590. try_connect("AO server (INADDR_ANY): MD5 client", port++, &addr_any, 0,
  591. NULL, 0, 100, 100, 0, FAULT_TIMEOUT, 1, &this_ip_addr);
  592. trace_hash_event_expect(TCP_HASH_AO_REQUIRED, this_ip_addr,
  593. this_ip_dest, -1, port, 0, 0, 1, 0, 0, 0);
  594. try_connect("AO server (INADDR_ANY): unsigned client", port++, NULL, 0,
  595. NULL, 0, 100, 100, 0, FAULT_TIMEOUT, 0, &this_ip_addr);
  596. try_connect("AO server (AO_REQUIRED): AO client", port++, NULL, 0,
  597. &addr_any, 0, 100, 100, 0, 0, 0, &this_ip_addr);
  598. trace_hash_event_expect(TCP_HASH_AO_REQUIRED, client2,
  599. this_ip_dest, -1, port, 0, 0, 1, 0, 0, 0);
  600. try_connect("AO server (AO_REQUIRED): unsigned client", port++, NULL, 0,
  601. NULL, 0, 100, 100, 0, FAULT_TIMEOUT, 0, &client2);
  602. trace_ao_event_expect(TCP_AO_KEY_NOT_FOUND, this_ip_addr, this_ip_dest,
  603. -1, port, 0, 0, 1, 0, 0, 0, 100, 100, -1);
  604. try_connect("MD5 server (INADDR_ANY): AO client", port++, NULL, 0,
  605. &addr_any, 0, 100, 100, 0, FAULT_TIMEOUT, 1, &this_ip_addr);
  606. try_connect("MD5 server (INADDR_ANY): MD5 client", port++, &addr_any, 0,
  607. NULL, 0, 100, 100, 0, 0, 1, &this_ip_addr);
  608. trace_hash_event_expect(TCP_HASH_MD5_REQUIRED, this_ip_addr,
  609. this_ip_dest, -1, port, 0, 0, 1, 0, 0, 0);
  610. try_connect("MD5 server (INADDR_ANY): no sign client", port++, NULL, 0,
  611. NULL, 0, 100, 100, 0, FAULT_TIMEOUT, 1, &this_ip_addr);
  612. trace_ao_event_expect(TCP_AO_KEY_NOT_FOUND, this_ip_addr, this_ip_dest,
  613. -1, port, 0, 0, 1, 0, 0, 0, 100, 100, -1);
  614. try_connect("no sign server: AO client", port++, NULL, 0,
  615. &addr_any, 0, 100, 100, 0, FAULT_TIMEOUT, 0, &this_ip_addr);
  616. trace_hash_event_expect(TCP_HASH_MD5_UNEXPECTED, this_ip_addr,
  617. this_ip_dest, -1, port, 0, 0, 1, 0, 0, 0);
  618. try_connect("no sign server: MD5 client", port++, &addr_any, 0,
  619. NULL, 0, 100, 100, 0, FAULT_TIMEOUT, 1, &this_ip_addr);
  620. try_connect("no sign server: no sign client", port++, NULL, 0,
  621. NULL, 0, 100, 100, 0, 0, 0, &this_ip_addr);
  622. try_connect("AO+MD5 server: AO client (matching)", port++, NULL, 0,
  623. &addr_any, 0, 100, 100, 0, 0, 1, &client2);
  624. trace_ao_event_expect(TCP_AO_KEY_NOT_FOUND, this_ip_addr, this_ip_dest,
  625. -1, port, 0, 0, 1, 0, 0, 0, 100, 100, -1);
  626. try_connect("AO+MD5 server: AO client (misconfig, matching MD5)",
  627. port++, NULL, 0, &addr_any, 0, 100, 100, 0,
  628. FAULT_TIMEOUT, 1, &this_ip_addr);
  629. trace_ao_event_expect(TCP_AO_KEY_NOT_FOUND, client3, this_ip_dest,
  630. -1, port, 0, 0, 1, 0, 0, 0, 100, 100, -1);
  631. try_connect("AO+MD5 server: AO client (misconfig, non-matching)",
  632. port++, NULL, 0, &addr_any, 0, 100, 100, 0,
  633. FAULT_TIMEOUT, 1, &client3);
  634. try_connect("AO+MD5 server: MD5 client (matching)", port++, &addr_any, 0,
  635. NULL, 0, 100, 100, 0, 0, 1, &this_ip_addr);
  636. trace_hash_event_expect(TCP_HASH_MD5_UNEXPECTED, client2,
  637. this_ip_dest, -1, port, 0, 0, 1, 0, 0, 0);
  638. try_connect("AO+MD5 server: MD5 client (misconfig, matching AO)",
  639. port++, &addr_any, 0, NULL, 0, 100, 100, 0, FAULT_TIMEOUT,
  640. 1, &client2);
  641. trace_hash_event_expect(TCP_HASH_MD5_UNEXPECTED, client3,
  642. this_ip_dest, -1, port, 0, 0, 1, 0, 0, 0);
  643. try_connect("AO+MD5 server: MD5 client (misconfig, non-matching)",
  644. port++, &addr_any, 0, NULL, 0, 100, 100, 0, FAULT_TIMEOUT,
  645. 1, &client3);
  646. try_connect("AO+MD5 server: no sign client (unmatched)",
  647. port++, NULL, 0, NULL, 0, 100, 100, 0, 0, 1, &client3);
  648. trace_hash_event_expect(TCP_HASH_AO_REQUIRED, client2,
  649. this_ip_dest, -1, port, 0, 0, 1, 0, 0, 0);
  650. try_connect("AO+MD5 server: no sign client (misconfig, matching AO)",
  651. port++, NULL, 0, NULL, 0, 100, 100, 0, FAULT_TIMEOUT,
  652. 1, &client2);
  653. trace_hash_event_expect(TCP_HASH_MD5_REQUIRED, this_ip_addr,
  654. this_ip_dest, -1, port, 0, 0, 1, 0, 0, 0);
  655. try_connect("AO+MD5 server: no sign client (misconfig, matching MD5)",
  656. port++, NULL, 0, NULL, 0, 100, 100, 0, FAULT_TIMEOUT,
  657. 1, &this_ip_addr);
  658. try_connect("AO+MD5 server: client with both [TCP-MD5] and TCP-AO keys",
  659. port++, &this_ip_addr, TEST_PREFIX,
  660. &client2, TEST_PREFIX, 100, 100, 0, FAULT_KEYREJECT,
  661. 1, &this_ip_addr);
  662. try_connect("AO+MD5 server: client with both TCP-MD5 and [TCP-AO] keys",
  663. port++, &this_ip_addr, TEST_PREFIX,
  664. &client2, TEST_PREFIX, 100, 100, 0, FAULT_KEYREJECT,
  665. 1, &client2);
  666. client_add_fail_tests(&port);
  667. client_vrf_tests(&port);
  668. return NULL;
  669. }
  670. int main(int argc, char *argv[])
  671. {
  672. test_init(73, server_fn, client_fn);
  673. return 0;
  674. }