1 | /* |
---|
2 | * |
---|
3 | * Copyright (c) 2007-2016 The University of Waikato, Hamilton, New Zealand. |
---|
4 | * All rights reserved. |
---|
5 | * |
---|
6 | * This file is part of libtrace. |
---|
7 | * |
---|
8 | * This code has been developed by the University of Waikato WAND |
---|
9 | * research group. For further information please see http://www.wand.net.nz/ |
---|
10 | * |
---|
11 | * libtrace is free software; you can redistribute it and/or modify |
---|
12 | * it under the terms of the GNU Lesser General Public License as published by |
---|
13 | * the Free Software Foundation; either version 3 of the License, or |
---|
14 | * (at your option) any later version. |
---|
15 | * |
---|
16 | * libtrace is distributed in the hope that it will be useful, |
---|
17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
19 | * GNU Lesser General Public License for more details. |
---|
20 | * |
---|
21 | * You should have received a copy of the GNU Lesser General Public License |
---|
22 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
---|
23 | * |
---|
24 | * |
---|
25 | */ |
---|
26 | #include <stdio.h> |
---|
27 | #include "libpacketdump.h" |
---|
28 | |
---|
29 | #define LE(lhs,n) \ |
---|
30 | do { \ |
---|
31 | uint64_t num=0; \ |
---|
32 | int size=0; \ |
---|
33 | if ((offset+n)>len*8) return; \ |
---|
34 | if (n>16) { \ |
---|
35 | num=htonl(*(uint32_t*)(packet+offset/8));\ |
---|
36 | size = 32;\ |
---|
37 | } else if (n>8) { \ |
---|
38 | num=htons(*(uint16_t*)(packet+offset/8));\ |
---|
39 | size = 16; \ |
---|
40 | } else { \ |
---|
41 | num=*(uint8_t*)(packet+offset/8); \ |
---|
42 | size = 8; \ |
---|
43 | } \ |
---|
44 | num=num>>(size - (n + (offset % 8))); \ |
---|
45 | offset+=n; \ |
---|
46 | lhs=num&((1<<(n))-1); \ |
---|
47 | } while(0) |
---|
48 | |
---|
49 | DLLEXPORT void decode(int link_type UNUSED,const char *packet,unsigned len) |
---|
50 | { |
---|
51 | unsigned int offset=0; |
---|
52 | int value; |
---|
53 | int more = 0; |
---|
54 | LE(value,20); printf(" MPLS: Label: %d\n",value); |
---|
55 | LE(value,3); printf(" MPLS: Class of service: %d\n",value); |
---|
56 | LE(value,1); printf(" MPLS: Stack: %s\n",value?"Last" :"More"); |
---|
57 | if (value == 0) more = 1; |
---|
58 | LE(value,8); printf(" MPLS: TTL: %d\n",value); |
---|
59 | |
---|
60 | /* MPLS doesn't say what it's encapsulating, so we make an educated |
---|
61 | * guess and pray. |
---|
62 | */ |
---|
63 | if (more) |
---|
64 | decode_next(packet+offset/8,len-4,"eth",0x8847); |
---|
65 | else if ((*(packet+4)&0xF0) == 0x40) |
---|
66 | decode_next(packet+offset/8,len-4,"eth",0x0800); |
---|
67 | else if ((*(packet+4)&0xF0) == 0x60) |
---|
68 | decode_next(packet+offset/8,len-4,"eth",0x86DD); |
---|
69 | else |
---|
70 | decode_next(packet+offset/8,len-4,"link",1); |
---|
71 | |
---|
72 | return; |
---|
73 | } |
---|