]> The Tcpdump Group git mirrors - tcpdump/blob - print-mptcp.c
Revert partially the commit 21b1273
[tcpdump] / print-mptcp.c
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
35 /* \summary: Multipath TCP (MPTCP) printer */
36
37 /* specification: RFC 6824 */
38
39 #ifdef HAVE_CONFIG_H
40 #include <config.h>
41 #endif
42
43 #include "netdissect-stdinc.h"
44
45 #include "netdissect.h"
46 #include "extract.h"
47 #include "addrtoname.h"
48
49 #include "tcp.h"
50
51 #define MPTCP_SUB_CAPABLE 0x0
52 #define MPTCP_SUB_JOIN 0x1
53 #define MPTCP_SUB_DSS 0x2
54 #define MPTCP_SUB_ADD_ADDR 0x3
55 #define MPTCP_SUB_REMOVE_ADDR 0x4
56 #define MPTCP_SUB_PRIO 0x5
57 #define MPTCP_SUB_FAIL 0x6
58 #define MPTCP_SUB_FCLOSE 0x7
59
60 struct mptcp_option {
61 nd_uint8_t kind;
62 nd_uint8_t len;
63 nd_uint8_t sub_etc; /* subtype upper 4 bits, other stuff lower 4 bits */
64 };
65
66 #define MPTCP_OPT_SUBTYPE(sub_etc) ((GET_U_1(sub_etc) >> 4) & 0xF)
67
68 struct mp_capable {
69 nd_uint8_t kind;
70 nd_uint8_t len;
71 nd_uint8_t sub_ver;
72 nd_uint8_t flags;
73 nd_uint64_t sender_key;
74 nd_uint64_t receiver_key;
75 };
76
77 #define MP_CAPABLE_OPT_VERSION(sub_ver) ((GET_U_1(sub_ver) >> 0) & 0xF)
78 #define MP_CAPABLE_C 0x80
79 #define MP_CAPABLE_S 0x01
80
81 struct mp_join {
82 nd_uint8_t kind;
83 nd_uint8_t len;
84 nd_uint8_t sub_b;
85 nd_uint8_t addr_id;
86 union {
87 struct {
88 nd_uint32_t token;
89 nd_uint32_t nonce;
90 } syn;
91 struct {
92 nd_uint64_t mac;
93 nd_uint32_t nonce;
94 } synack;
95 struct {
96 nd_byte mac[20];
97 } ack;
98 } u;
99 };
100
101 #define MP_JOIN_B 0x01
102
103 struct mp_dss {
104 nd_uint8_t kind;
105 nd_uint8_t len;
106 nd_uint8_t sub;
107 nd_uint8_t flags;
108 };
109
110 #define MP_DSS_F 0x10
111 #define MP_DSS_m 0x08
112 #define MP_DSS_M 0x04
113 #define MP_DSS_a 0x02
114 #define MP_DSS_A 0x01
115
116 struct mp_add_addr {
117 nd_uint8_t kind;
118 nd_uint8_t len;
119 nd_uint8_t sub_echo;
120 nd_uint8_t addr_id;
121 union {
122 struct {
123 nd_ipv4 addr;
124 nd_uint16_t port;
125 nd_uint64_t mac;
126 } v4;
127 struct {
128 nd_ipv4 addr;
129 nd_uint64_t mac;
130 } v4np;
131 struct {
132 nd_ipv6 addr;
133 nd_uint16_t port;
134 nd_uint64_t mac;
135 } v6;
136 struct {
137 nd_ipv6 addr;
138 nd_uint64_t mac;
139 } v6np;
140 } u;
141 };
142
143 struct mp_remove_addr {
144 nd_uint8_t kind;
145 nd_uint8_t len;
146 nd_uint8_t sub;
147 /* list of addr_id */
148 nd_uint8_t addrs_id[1];
149 };
150
151 struct mp_fail {
152 nd_uint8_t kind;
153 nd_uint8_t len;
154 nd_uint8_t sub;
155 nd_uint8_t resv;
156 nd_uint64_t data_seq;
157 };
158
159 struct mp_close {
160 nd_uint8_t kind;
161 nd_uint8_t len;
162 nd_uint8_t sub;
163 nd_uint8_t rsv;
164 nd_byte key[8];
165 };
166
167 struct mp_prio {
168 nd_uint8_t kind;
169 nd_uint8_t len;
170 nd_uint8_t sub_b;
171 nd_uint8_t addr_id;
172 };
173
174 #define MP_PRIO_B 0x01
175
176 static int
177 dummy_print(netdissect_options *ndo _U_,
178 const u_char *opt _U_, u_int opt_len _U_, u_char flags _U_)
179 {
180 return 1;
181 }
182
183 static int
184 mp_capable_print(netdissect_options *ndo,
185 const u_char *opt, u_int opt_len, u_char flags)
186 {
187 const struct mp_capable *mpc = (const struct mp_capable *) opt;
188
189 if (!((opt_len == 12 || opt_len == 4) && flags & TH_SYN) &&
190 !((opt_len == 20 || opt_len == 22) && (flags & (TH_SYN | TH_ACK)) ==
191 TH_ACK))
192 return 0;
193
194 switch (MP_CAPABLE_OPT_VERSION(mpc->sub_ver)) {
195 case 0: /* fall through */
196 case 1:
197 ND_PRINT(" v%d", MP_CAPABLE_OPT_VERSION(mpc->sub_ver));
198 break;
199 default:
200 ND_PRINT(" Unknown Version (%d)",
201 MP_CAPABLE_OPT_VERSION(mpc->sub_ver));
202 return 1;
203 }
204
205 if (GET_U_1(mpc->flags) & MP_CAPABLE_C)
206 ND_PRINT(" csum");
207 if (opt_len == 12 || opt_len >= 20) {
208 ND_PRINT(" {0x%" PRIx64, GET_BE_U_8(mpc->sender_key));
209 if (opt_len >= 20)
210 ND_PRINT(",0x%" PRIx64, GET_BE_U_8(mpc->receiver_key));
211 ND_PRINT("}");
212 }
213 return 1;
214 }
215
216 static int
217 mp_join_print(netdissect_options *ndo,
218 const u_char *opt, u_int opt_len, u_char flags)
219 {
220 const struct mp_join *mpj = (const struct mp_join *) opt;
221
222 if (!(opt_len == 12 && (flags & TH_SYN)) &&
223 !(opt_len == 16 && (flags & (TH_SYN | TH_ACK)) == (TH_SYN | TH_ACK)) &&
224 !(opt_len == 24 && (flags & TH_ACK)))
225 return 0;
226
227 if (opt_len != 24) {
228 if (GET_U_1(mpj->sub_b) & MP_JOIN_B)
229 ND_PRINT(" backup");
230 ND_PRINT(" id %u", GET_U_1(mpj->addr_id));
231 }
232
233 switch (opt_len) {
234 case 12: /* SYN */
235 ND_PRINT(" token 0x%x" " nonce 0x%x",
236 GET_BE_U_4(mpj->u.syn.token),
237 GET_BE_U_4(mpj->u.syn.nonce));
238 break;
239 case 16: /* SYN/ACK */
240 ND_PRINT(" hmac 0x%" PRIx64 " nonce 0x%x",
241 GET_BE_U_8(mpj->u.synack.mac),
242 GET_BE_U_4(mpj->u.synack.nonce));
243 break;
244 case 24: {/* ACK */
245 size_t i;
246 ND_PRINT(" hmac 0x");
247 for (i = 0; i < sizeof(mpj->u.ack.mac); ++i)
248 ND_PRINT("%02x", mpj->u.ack.mac[i]);
249 }
250 default:
251 break;
252 }
253 return 1;
254 }
255
256 static int
257 mp_dss_print(netdissect_options *ndo,
258 const u_char *opt, u_int opt_len, u_char flags)
259 {
260 const struct mp_dss *mdss = (const struct mp_dss *) opt;
261 uint8_t mdss_flags;
262
263 /* We need the flags, at a minimum. */
264 if (opt_len < 4)
265 return 0;
266
267 if (flags & TH_SYN)
268 return 0;
269
270 mdss_flags = GET_U_1(mdss->flags);
271 if (mdss_flags & MP_DSS_F)
272 ND_PRINT(" fin");
273
274 opt += 4;
275 opt_len -= 4;
276 if (mdss_flags & MP_DSS_A) {
277 /* Ack present */
278 ND_PRINT(" ack ");
279 /*
280 * If the a flag is set, we have an 8-byte ack; if it's
281 * clear, we have a 4-byte ack.
282 */
283 if (mdss_flags & MP_DSS_a) {
284 if (opt_len < 8)
285 return 0;
286 ND_PRINT("%" PRIu64, GET_BE_U_8(opt));
287 opt += 8;
288 opt_len -= 8;
289 } else {
290 if (opt_len < 4)
291 return 0;
292 ND_PRINT("%u", GET_BE_U_4(opt));
293 opt += 4;
294 opt_len -= 4;
295 }
296 }
297
298 if (mdss_flags & MP_DSS_M) {
299 /*
300 * Data Sequence Number (DSN), Subflow Sequence Number (SSN),
301 * Data-Level Length present, and Checksum possibly present.
302 */
303 ND_PRINT(" seq ");
304 /*
305 * If the m flag is set, we have an 8-byte NDS; if it's clear,
306 * we have a 4-byte DSN.
307 */
308 if (mdss_flags & MP_DSS_m) {
309 if (opt_len < 8)
310 return 0;
311 ND_PRINT("%" PRIu64, GET_BE_U_8(opt));
312 opt += 8;
313 opt_len -= 8;
314 } else {
315 if (opt_len < 4)
316 return 0;
317 ND_PRINT("%u", GET_BE_U_4(opt));
318 opt += 4;
319 opt_len -= 4;
320 }
321 if (opt_len < 4)
322 return 0;
323 ND_PRINT(" subseq %u", GET_BE_U_4(opt));
324 opt += 4;
325 opt_len -= 4;
326 if (opt_len < 2)
327 return 0;
328 ND_PRINT(" len %u", GET_BE_U_2(opt));
329 opt += 2;
330 opt_len -= 2;
331
332 /*
333 * The Checksum is present only if negotiated.
334 * If there are at least 2 bytes left, process the next 2
335 * bytes as the Checksum.
336 */
337 if (opt_len >= 2) {
338 ND_PRINT(" csum 0x%x", GET_BE_U_2(opt));
339 opt_len -= 2;
340 }
341 }
342 if (opt_len != 0)
343 return 0;
344 return 1;
345 }
346
347 static int
348 add_addr_print(netdissect_options *ndo,
349 const u_char *opt, u_int opt_len, u_char flags _U_)
350 {
351 const struct mp_add_addr *add_addr = (const struct mp_add_addr *) opt;
352
353 if (!(opt_len == 8 || opt_len == 10 || opt_len == 16 || opt_len == 18 ||
354 opt_len == 20 || opt_len == 22 || opt_len == 28 || opt_len == 30))
355 return 0;
356
357 ND_PRINT(" id %u", GET_U_1(add_addr->addr_id));
358 if (opt_len == 8 || opt_len == 10 || opt_len == 16 || opt_len == 18) {
359 ND_PRINT(" %s", GET_IPADDR_STRING(add_addr->u.v4.addr));
360 if (opt_len == 10 || opt_len == 18)
361 ND_PRINT(":%u", GET_BE_U_2(add_addr->u.v4.port));
362 if (opt_len == 16)
363 ND_PRINT(" hmac 0x%" PRIx64, GET_BE_U_8(add_addr->u.v4np.mac));
364 if (opt_len == 18)
365 ND_PRINT(" hmac 0x%" PRIx64, GET_BE_U_8(add_addr->u.v4.mac));
366 }
367
368 if (opt_len == 20 || opt_len == 22 || opt_len == 28 || opt_len == 30) {
369 ND_PRINT(" %s", GET_IP6ADDR_STRING(add_addr->u.v6.addr));
370 if (opt_len == 22 || opt_len == 30)
371 ND_PRINT(":%u", GET_BE_U_2(add_addr->u.v6.port));
372 if (opt_len == 28)
373 ND_PRINT(" hmac 0x%" PRIx64, GET_BE_U_8(add_addr->u.v6np.mac));
374 if (opt_len == 30)
375 ND_PRINT(" hmac 0x%" PRIx64, GET_BE_U_8(add_addr->u.v6.mac));
376 }
377
378 return 1;
379 }
380
381 static int
382 remove_addr_print(netdissect_options *ndo,
383 const u_char *opt, u_int opt_len, u_char flags _U_)
384 {
385 const struct mp_remove_addr *remove_addr = (const struct mp_remove_addr *) opt;
386 u_int i;
387
388 if (opt_len < 4)
389 return 0;
390
391 opt_len -= 3;
392 ND_PRINT(" id");
393 for (i = 0; i < opt_len; i++)
394 ND_PRINT(" %u", GET_U_1(remove_addr->addrs_id[i]));
395 return 1;
396 }
397
398 static int
399 mp_prio_print(netdissect_options *ndo,
400 const u_char *opt, u_int opt_len, u_char flags _U_)
401 {
402 const struct mp_prio *mpp = (const struct mp_prio *) opt;
403
404 if (opt_len != 3 && opt_len != 4)
405 return 0;
406
407 if (GET_U_1(mpp->sub_b) & MP_PRIO_B)
408 ND_PRINT(" backup");
409 else
410 ND_PRINT(" non-backup");
411 if (opt_len == 4)
412 ND_PRINT(" id %u", GET_U_1(mpp->addr_id));
413
414 return 1;
415 }
416
417 static int
418 mp_fail_print(netdissect_options *ndo,
419 const u_char *opt, u_int opt_len, u_char flags _U_)
420 {
421 if (opt_len != 12)
422 return 0;
423
424 ND_PRINT(" seq %" PRIu64, GET_BE_U_8(opt + 4));
425 return 1;
426 }
427
428 static int
429 mp_fast_close_print(netdissect_options *ndo,
430 const u_char *opt, u_int opt_len, u_char flags _U_)
431 {
432 if (opt_len != 12)
433 return 0;
434
435 ND_PRINT(" key 0x%" PRIx64, GET_BE_U_8(opt + 4));
436 return 1;
437 }
438
439 static const struct {
440 const char *name;
441 int (*print)(netdissect_options *, const u_char *, u_int, u_char);
442 } mptcp_options[] = {
443 { "capable", mp_capable_print},
444 { "join", mp_join_print },
445 { "dss", mp_dss_print },
446 { "add-addr", add_addr_print },
447 { "rem-addr", remove_addr_print },
448 { "prio", mp_prio_print },
449 { "fail", mp_fail_print },
450 { "fast-close", mp_fast_close_print },
451 { "unknown", dummy_print },
452 };
453
454 int
455 mptcp_print(netdissect_options *ndo,
456 const u_char *cp, u_int len, u_char flags)
457 {
458 const struct mptcp_option *opt;
459 u_int subtype;
460
461 ndo->ndo_protocol = "mptcp";
462 if (len < 3)
463 return 0;
464
465 opt = (const struct mptcp_option *) cp;
466 ND_TCHECK_SIZE(opt);
467 subtype = ND_MIN(MPTCP_OPT_SUBTYPE(opt->sub_etc), MPTCP_SUB_FCLOSE + 1);
468
469 ND_PRINT(" %s", mptcp_options[subtype].name);
470 return mptcp_options[subtype].print(ndo, cp, len, flags);
471
472 trunc:
473 nd_print_trunc(ndo);
474 return 0;
475 }