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