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