]> The Tcpdump Group git mirrors - tcpdump/blob - print-rt6.c
rt6: parse TLV
[tcpdump] / print-rt6.c
1 /*
2 * Copyright (c) 1988, 1989, 1990, 1991, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that: (1) source code distributions
7 * retain the above copyright notice and this paragraph in its entirety, (2)
8 * distributions including binary code include the above copyright notice and
9 * this paragraph in its entirety in the documentation or other materials
10 * provided with the distribution, and (3) all advertising materials mentioning
11 * features or use of this software display the following acknowledgement:
12 * ``This product includes software developed by the University of California,
13 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
14 * the University nor the names of its contributors may be used to endorse
15 * or promote products derived from this software without specific prior
16 * written permission.
17 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
18 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
20 */
21
22 /* \summary: IPv6 routing header printer */
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include "netdissect-stdinc.h"
29
30 #include "netdissect.h"
31 #include "addrtoname.h"
32 #include "extract.h"
33
34 #include "ip6.h"
35
36 static int
37 srh_tlv_print(netdissect_options *ndo, const u_char *p, u_int bytes_left)
38 {
39 u_int tlv_type, tlv_len;
40 while (bytes_left != 0) {
41 tlv_type = GET_U_1(p);
42 ND_ICHECKMSG_U("remaining length", bytes_left, <, 1);
43 p += 1;
44 bytes_left -= 1;
45 if (bytes_left == 0)
46 break;
47 if (tlv_type == IPV6_SRH_TLV_PAD1) {
48 ND_PRINT(", TLV-type=Pad1(%u)", tlv_type);
49 continue;
50 }
51
52 tlv_len = GET_U_1(p);
53 ND_ICHECKMSG_U("remaining length", bytes_left, <, 1);
54 p += 1;
55 bytes_left -= 1;
56
57 switch (tlv_type) {
58 case IPV6_SRH_TLV_PADN:
59 ND_PRINT(", TLV-type=PadN(%u)", tlv_type);
60 ND_PRINT(", TLV-len=%u", tlv_len);
61 ND_ICHECKMSG_U("PadN length", tlv_len, >, 5); /* RFC 8754 */
62 ND_ICHECKMSG_U("remaining length", bytes_left, <, tlv_len);
63 p += tlv_len;
64 bytes_left -= tlv_len;
65 break;
66 case IPV6_SRH_TLV_HMAC:
67 ND_PRINT(", TLV-type=HMAC(%u)", tlv_type);
68 ND_PRINT(", TLV-len=%u", tlv_len);
69 ND_ICHECKMSG_U("remaining length", bytes_left, <, 6);
70 uint16_t reserved;
71 uint32_t key_id;
72 uint8_t hmac_byte;
73 reserved = GET_BE_U_2(p);
74 p += 2;
75 if (ndo->ndo_vflag)
76 ND_PRINT(", D=%u", reserved >> 15);
77 key_id = GET_BE_U_4(p);
78 p += 4;
79 if (ndo->ndo_vflag)
80 ND_PRINT(", HMAC-key-ID=0x%02x", key_id);
81 bytes_left -= 6;
82 if (ndo->ndo_vflag)
83 ND_PRINT(", HMAC=0x");
84 for (u_int i = 0; i < tlv_len; i++) {
85 hmac_byte = GET_U_1(p);
86 ND_ICHECKMSG_U("remaining length", bytes_left, <, 1);
87 p += 1;
88 bytes_left -= 1;
89 if (ndo->ndo_vflag)
90 ND_PRINT("%02x", hmac_byte);
91 }
92 break;
93 default: /* Unknown type */
94 ND_PRINT(" Unknown");
95 ND_PRINT(", TLV-len=%u", tlv_len);
96 if (ndo->ndo_vflag)
97 ND_PRINT(", TLV-value=0x");
98 ND_ICHECKMSG_U("remaining length", bytes_left, <, tlv_len);
99 uint8_t tlv_byte;
100 for (u_int i = 0; i < tlv_len; i++) {
101 tlv_byte = GET_U_1(p);
102 p += 1;
103 bytes_left -= 1;
104 if (ndo->ndo_vflag)
105 ND_PRINT("%02x", tlv_byte);
106 }
107 break;
108 }
109 }
110 return 0;
111
112 invalid:
113 return -1;
114 }
115
116
117 int
118 rt6_print(netdissect_options *ndo, const u_char *bp, const u_char *bp2 _U_)
119 {
120 const struct ip6_rthdr *dp;
121 const struct ip6_rthdr0 *dp0;
122 const struct ip6_srh *srh;
123 u_int i, len, type, seg_list_len, last_entry;
124 int err;
125 const u_char *p;
126
127 ndo->ndo_protocol = "rt6";
128
129 nd_print_protocol_caps(ndo);
130 dp = (const struct ip6_rthdr *)bp;
131
132 len = GET_U_1(dp->ip6r_len);
133 ND_PRINT(" (len=%u", len); /*)*/
134 type = GET_U_1(dp->ip6r_type);
135 ND_PRINT(", type=%u", type);
136 if (type == IPV6_RTHDR_TYPE_0)
137 ND_PRINT(" [Deprecated]");
138 ND_PRINT(", segleft=%u", GET_U_1(dp->ip6r_segleft));
139
140 switch (type) {
141 case IPV6_RTHDR_TYPE_0:
142 case IPV6_RTHDR_TYPE_2: /* Mobile IPv6 ID-20 */
143 dp0 = (const struct ip6_rthdr0 *)dp;
144
145 if (GET_BE_U_4(dp0->ip6r0_reserved) || ndo->ndo_vflag) {
146 ND_PRINT(", rsv=0x%0x",
147 GET_BE_U_4(dp0->ip6r0_reserved));
148 }
149
150 if (len % 2 == 1) {
151 ND_PRINT(" (invalid length %u)", len);
152 goto invalid;
153 }
154 len >>= 1;
155 p = (const u_char *) dp0->ip6r0_addr;
156 for (i = 0; i < len; i++) {
157 ND_PRINT(", [%u]%s", i, GET_IP6ADDR_STRING(p));
158 p += 16;
159 }
160 /*(*/
161 ND_PRINT(") ");
162 return((GET_U_1(dp0->ip6r0_len) + 1) << 3);
163 break;
164 case IPV6_RTHDR_TYPE_4:
165 srh = (const struct ip6_srh *)dp;
166 last_entry = GET_U_1(srh->srh_last_ent);
167 ND_PRINT(", last-entry=%u", last_entry);
168
169 if (GET_U_1(srh->srh_flags) || ndo->ndo_vflag) {
170 ND_PRINT(", flags=0x%0x",
171 GET_U_1(srh->srh_flags));
172 }
173
174 ND_PRINT(", tag=%x", GET_BE_U_2(srh->srh_tag));
175 p = (const u_char *) srh->srh_segments;
176 for (i = 0; i < last_entry + 1; i++) {
177 ND_PRINT(", [%u]%s", i, GET_IP6ADDR_STRING(p));
178 p += 16;
179 }
180 seg_list_len = (last_entry + 1) * 2;
181 if (seg_list_len < len) {
182 /* there is TLV */
183 u_int bytes_left;
184 bytes_left = (len - seg_list_len) * 8;
185 err = srh_tlv_print(ndo, p, bytes_left);
186 if (err)
187 goto invalid;
188 }
189
190 /*(*/
191 ND_PRINT(") ");
192 return((GET_U_1(srh->srh_len) + 1) << 3);
193 break;
194 default:
195 ND_PRINT(" (unknown type)");
196 goto invalid;
197 }
198
199 invalid:
200 nd_print_invalid(ndo);
201 return -1;
202 }