print-mptcp.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. /**
  2. * Copyright (c) 2012
  3. *
  4. * Gregory Detal <gregory.detal@uclouvain.be>
  5. * Christoph Paasch <christoph.paasch@uclouvain.be>
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. *
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in the
  16. * documentation and/or other materials provided with the distribution.
  17. *
  18. * 3. Neither the name of the University nor of the Laboratory may be used
  19. * to endorse or promote products derived from this software without
  20. * specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  23. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  26. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  27. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  28. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  29. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  30. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  31. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  32. * SUCH DAMAGE.
  33. */
  34. /* \summary: Multipath TCP (MPTCP) printer */
  35. /* specification: RFC 6824 */
  36. #ifdef HAVE_CONFIG_H
  37. #include "config.h"
  38. #endif
  39. #include <netdissect-stdinc.h>
  40. #include "netdissect.h"
  41. #include "extract.h"
  42. #include "addrtoname.h"
  43. #include "tcp.h"
  44. #define MPTCP_SUB_CAPABLE 0x0
  45. #define MPTCP_SUB_JOIN 0x1
  46. #define MPTCP_SUB_DSS 0x2
  47. #define MPTCP_SUB_ADD_ADDR 0x3
  48. #define MPTCP_SUB_REMOVE_ADDR 0x4
  49. #define MPTCP_SUB_PRIO 0x5
  50. #define MPTCP_SUB_FAIL 0x6
  51. #define MPTCP_SUB_FCLOSE 0x7
  52. struct mptcp_option {
  53. uint8_t kind;
  54. uint8_t len;
  55. uint8_t sub_etc; /* subtype upper 4 bits, other stuff lower 4 bits */
  56. };
  57. #define MPTCP_OPT_SUBTYPE(sub_etc) (((sub_etc) >> 4) & 0xF)
  58. struct mp_capable {
  59. uint8_t kind;
  60. uint8_t len;
  61. uint8_t sub_ver;
  62. uint8_t flags;
  63. uint8_t sender_key[8];
  64. uint8_t receiver_key[8];
  65. };
  66. #define MP_CAPABLE_OPT_VERSION(sub_ver) (((sub_ver) >> 0) & 0xF)
  67. #define MP_CAPABLE_C 0x80
  68. #define MP_CAPABLE_S 0x01
  69. struct mp_join {
  70. uint8_t kind;
  71. uint8_t len;
  72. uint8_t sub_b;
  73. uint8_t addr_id;
  74. union {
  75. struct {
  76. uint8_t token[4];
  77. uint8_t nonce[4];
  78. } syn;
  79. struct {
  80. uint8_t mac[8];
  81. uint8_t nonce[4];
  82. } synack;
  83. struct {
  84. uint8_t mac[20];
  85. } ack;
  86. } u;
  87. };
  88. #define MP_JOIN_B 0x01
  89. struct mp_dss {
  90. uint8_t kind;
  91. uint8_t len;
  92. uint8_t sub;
  93. uint8_t flags;
  94. };
  95. #define MP_DSS_F 0x10
  96. #define MP_DSS_m 0x08
  97. #define MP_DSS_M 0x04
  98. #define MP_DSS_a 0x02
  99. #define MP_DSS_A 0x01
  100. struct mp_add_addr {
  101. uint8_t kind;
  102. uint8_t len;
  103. uint8_t sub_ipver;
  104. uint8_t addr_id;
  105. union {
  106. struct {
  107. uint8_t addr[4];
  108. uint8_t port[2];
  109. } v4;
  110. struct {
  111. uint8_t addr[16];
  112. uint8_t port[2];
  113. } v6;
  114. } u;
  115. };
  116. #define MP_ADD_ADDR_IPVER(sub_ipver) (((sub_ipver) >> 0) & 0xF)
  117. struct mp_remove_addr {
  118. uint8_t kind;
  119. uint8_t len;
  120. uint8_t sub;
  121. /* list of addr_id */
  122. uint8_t addrs_id;
  123. };
  124. struct mp_fail {
  125. uint8_t kind;
  126. uint8_t len;
  127. uint8_t sub;
  128. uint8_t resv;
  129. uint8_t data_seq[8];
  130. };
  131. struct mp_close {
  132. uint8_t kind;
  133. uint8_t len;
  134. uint8_t sub;
  135. uint8_t rsv;
  136. uint8_t key[8];
  137. };
  138. struct mp_prio {
  139. uint8_t kind;
  140. uint8_t len;
  141. uint8_t sub_b;
  142. uint8_t addr_id;
  143. };
  144. #define MP_PRIO_B 0x01
  145. static int
  146. dummy_print(netdissect_options *ndo _U_,
  147. const u_char *opt _U_, u_int opt_len _U_, u_char flags _U_)
  148. {
  149. return 1;
  150. }
  151. static int
  152. mp_capable_print(netdissect_options *ndo,
  153. const u_char *opt, u_int opt_len, u_char flags)
  154. {
  155. const struct mp_capable *mpc = (const struct mp_capable *) opt;
  156. if (!(opt_len == 12 && (flags & TH_SYN)) &&
  157. !(opt_len == 20 && (flags & (TH_SYN | TH_ACK)) == TH_ACK))
  158. return 0;
  159. if (MP_CAPABLE_OPT_VERSION(mpc->sub_ver) != 0) {
  160. ND_PRINT((ndo, " Unknown Version (%d)", MP_CAPABLE_OPT_VERSION(mpc->sub_ver)));
  161. return 1;
  162. }
  163. if (mpc->flags & MP_CAPABLE_C)
  164. ND_PRINT((ndo, " csum"));
  165. ND_PRINT((ndo, " {0x%" PRIx64, EXTRACT_64BITS(mpc->sender_key)));
  166. if (opt_len == 20) /* ACK */
  167. ND_PRINT((ndo, ",0x%" PRIx64, EXTRACT_64BITS(mpc->receiver_key)));
  168. ND_PRINT((ndo, "}"));
  169. return 1;
  170. }
  171. static int
  172. mp_join_print(netdissect_options *ndo,
  173. const u_char *opt, u_int opt_len, u_char flags)
  174. {
  175. const struct mp_join *mpj = (const struct mp_join *) opt;
  176. if (!(opt_len == 12 && (flags & TH_SYN)) &&
  177. !(opt_len == 16 && (flags & (TH_SYN | TH_ACK)) == (TH_SYN | TH_ACK)) &&
  178. !(opt_len == 24 && (flags & TH_ACK)))
  179. return 0;
  180. if (opt_len != 24) {
  181. if (mpj->sub_b & MP_JOIN_B)
  182. ND_PRINT((ndo, " backup"));
  183. ND_PRINT((ndo, " id %u", mpj->addr_id));
  184. }
  185. switch (opt_len) {
  186. case 12: /* SYN */
  187. ND_PRINT((ndo, " token 0x%x" " nonce 0x%x",
  188. EXTRACT_32BITS(mpj->u.syn.token),
  189. EXTRACT_32BITS(mpj->u.syn.nonce)));
  190. break;
  191. case 16: /* SYN/ACK */
  192. ND_PRINT((ndo, " hmac 0x%" PRIx64 " nonce 0x%x",
  193. EXTRACT_64BITS(mpj->u.synack.mac),
  194. EXTRACT_32BITS(mpj->u.synack.nonce)));
  195. break;
  196. case 24: {/* ACK */
  197. size_t i;
  198. ND_PRINT((ndo, " hmac 0x"));
  199. for (i = 0; i < sizeof(mpj->u.ack.mac); ++i)
  200. ND_PRINT((ndo, "%02x", mpj->u.ack.mac[i]));
  201. }
  202. default:
  203. break;
  204. }
  205. return 1;
  206. }
  207. static int
  208. mp_dss_print(netdissect_options *ndo,
  209. const u_char *opt, u_int opt_len, u_char flags)
  210. {
  211. const struct mp_dss *mdss = (const struct mp_dss *) opt;
  212. /* We need the flags, at a minimum. */
  213. if (opt_len < 4)
  214. return 0;
  215. if (flags & TH_SYN)
  216. return 0;
  217. if (mdss->flags & MP_DSS_F)
  218. ND_PRINT((ndo, " fin"));
  219. opt += 4;
  220. opt_len -= 4;
  221. if (mdss->flags & MP_DSS_A) {
  222. /* Ack present */
  223. ND_PRINT((ndo, " ack "));
  224. /*
  225. * If the a flag is set, we have an 8-byte ack; if it's
  226. * clear, we have a 4-byte ack.
  227. */
  228. if (mdss->flags & MP_DSS_a) {
  229. if (opt_len < 8)
  230. return 0;
  231. ND_PRINT((ndo, "%" PRIu64, EXTRACT_64BITS(opt)));
  232. opt += 8;
  233. opt_len -= 8;
  234. } else {
  235. if (opt_len < 4)
  236. return 0;
  237. ND_PRINT((ndo, "%u", EXTRACT_32BITS(opt)));
  238. opt += 4;
  239. opt_len -= 4;
  240. }
  241. }
  242. if (mdss->flags & MP_DSS_M) {
  243. /*
  244. * Data Sequence Number (DSN), Subflow Sequence Number (SSN),
  245. * Data-Level Length present, and Checksum possibly present.
  246. */
  247. ND_PRINT((ndo, " seq "));
  248. /*
  249. * If the m flag is set, we have an 8-byte NDS; if it's clear,
  250. * we have a 4-byte DSN.
  251. */
  252. if (mdss->flags & MP_DSS_m) {
  253. if (opt_len < 8)
  254. return 0;
  255. ND_PRINT((ndo, "%" PRIu64, EXTRACT_64BITS(opt)));
  256. opt += 8;
  257. opt_len -= 8;
  258. } else {
  259. if (opt_len < 4)
  260. return 0;
  261. ND_PRINT((ndo, "%u", EXTRACT_32BITS(opt)));
  262. opt += 4;
  263. opt_len -= 4;
  264. }
  265. if (opt_len < 4)
  266. return 0;
  267. ND_PRINT((ndo, " subseq %u", EXTRACT_32BITS(opt)));
  268. opt += 4;
  269. opt_len -= 4;
  270. if (opt_len < 2)
  271. return 0;
  272. ND_PRINT((ndo, " len %u", EXTRACT_16BITS(opt)));
  273. opt += 2;
  274. opt_len -= 2;
  275. /*
  276. * The Checksum is present only if negotiated.
  277. * If there are at least 2 bytes left, process the next 2
  278. * bytes as the Checksum.
  279. */
  280. if (opt_len >= 2) {
  281. ND_PRINT((ndo, " csum 0x%x", EXTRACT_16BITS(opt)));
  282. opt_len -= 2;
  283. }
  284. }
  285. if (opt_len != 0)
  286. return 0;
  287. return 1;
  288. }
  289. static int
  290. add_addr_print(netdissect_options *ndo,
  291. const u_char *opt, u_int opt_len, u_char flags _U_)
  292. {
  293. const struct mp_add_addr *add_addr = (const struct mp_add_addr *) opt;
  294. u_int ipver = MP_ADD_ADDR_IPVER(add_addr->sub_ipver);
  295. if (!((opt_len == 8 || opt_len == 10) && ipver == 4) &&
  296. !((opt_len == 20 || opt_len == 22) && ipver == 6))
  297. return 0;
  298. ND_PRINT((ndo, " id %u", add_addr->addr_id));
  299. switch (ipver) {
  300. case 4:
  301. ND_PRINT((ndo, " %s", ipaddr_string(ndo, add_addr->u.v4.addr)));
  302. if (opt_len == 10)
  303. ND_PRINT((ndo, ":%u", EXTRACT_16BITS(add_addr->u.v4.port)));
  304. break;
  305. case 6:
  306. ND_PRINT((ndo, " %s", ip6addr_string(ndo, add_addr->u.v6.addr)));
  307. if (opt_len == 22)
  308. ND_PRINT((ndo, ":%u", EXTRACT_16BITS(add_addr->u.v6.port)));
  309. break;
  310. default:
  311. return 0;
  312. }
  313. return 1;
  314. }
  315. static int
  316. remove_addr_print(netdissect_options *ndo,
  317. const u_char *opt, u_int opt_len, u_char flags _U_)
  318. {
  319. const struct mp_remove_addr *remove_addr = (const struct mp_remove_addr *) opt;
  320. const uint8_t *addr_id = &remove_addr->addrs_id;
  321. if (opt_len < 4)
  322. return 0;
  323. opt_len -= 3;
  324. ND_PRINT((ndo, " id"));
  325. while (opt_len--)
  326. ND_PRINT((ndo, " %u", *addr_id++));
  327. return 1;
  328. }
  329. static int
  330. mp_prio_print(netdissect_options *ndo,
  331. const u_char *opt, u_int opt_len, u_char flags _U_)
  332. {
  333. const struct mp_prio *mpp = (const struct mp_prio *) opt;
  334. if (opt_len != 3 && opt_len != 4)
  335. return 0;
  336. if (mpp->sub_b & MP_PRIO_B)
  337. ND_PRINT((ndo, " backup"));
  338. else
  339. ND_PRINT((ndo, " non-backup"));
  340. if (opt_len == 4)
  341. ND_PRINT((ndo, " id %u", mpp->addr_id));
  342. return 1;
  343. }
  344. static int
  345. mp_fail_print(netdissect_options *ndo,
  346. const u_char *opt, u_int opt_len, u_char flags _U_)
  347. {
  348. if (opt_len != 12)
  349. return 0;
  350. ND_PRINT((ndo, " seq %" PRIu64, EXTRACT_64BITS(opt + 4)));
  351. return 1;
  352. }
  353. static int
  354. mp_fast_close_print(netdissect_options *ndo,
  355. const u_char *opt, u_int opt_len, u_char flags _U_)
  356. {
  357. if (opt_len != 12)
  358. return 0;
  359. ND_PRINT((ndo, " key 0x%" PRIx64, EXTRACT_64BITS(opt + 4)));
  360. return 1;
  361. }
  362. static const struct {
  363. const char *name;
  364. int (*print)(netdissect_options *, const u_char *, u_int, u_char);
  365. } mptcp_options[] = {
  366. { "capable", mp_capable_print},
  367. { "join", mp_join_print },
  368. { "dss", mp_dss_print },
  369. { "add-addr", add_addr_print },
  370. { "rem-addr", remove_addr_print },
  371. { "prio", mp_prio_print },
  372. { "fail", mp_fail_print },
  373. { "fast-close", mp_fast_close_print },
  374. { "unknown", dummy_print },
  375. };
  376. int
  377. mptcp_print(netdissect_options *ndo,
  378. const u_char *cp, u_int len, u_char flags)
  379. {
  380. const struct mptcp_option *opt;
  381. u_int subtype;
  382. if (len < 3)
  383. return 0;
  384. opt = (const struct mptcp_option *) cp;
  385. subtype = min(MPTCP_OPT_SUBTYPE(opt->sub_etc), MPTCP_SUB_FCLOSE + 1);
  386. ND_PRINT((ndo, " %s", mptcp_options[subtype].name));
  387. return mptcp_options[subtype].print(ndo, cp, len, flags);
  388. }