]> The Tcpdump Group git mirrors - tcpdump/blob - print-rt6.c
Fix a typo
[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 #include <config.h>
25
26 #include "netdissect-stdinc.h"
27
28 #include "netdissect.h"
29 #include "addrtoname.h"
30 #include "extract.h"
31
32 #include "ip6.h"
33
34 static const struct tok srh_tlv_type[] = {
35 { IPV6_SRH_TLV_PAD1, "Pad1"},
36 { IPV6_SRH_TLV_PADN, "PadN"},
37 { IPV6_SRH_TLV_HMAC, "HMAC"},
38 { 0, NULL }
39 };
40
41 static int
42 srh_tlv_print(netdissect_options *ndo, const u_char *p, u_int bytes_left)
43 {
44 u_int tlv_type, tlv_len;
45 while (bytes_left != 0) {
46 tlv_type = GET_U_1(p);
47 ND_PRINT(", TLV-type=%s(%u)",
48 tok2str(srh_tlv_type, "Unknown", tlv_type),
49 tlv_type);
50 ND_ICHECKMSG_U("remaining length", bytes_left, <, 1);
51 p += 1;
52 bytes_left -= 1;
53 if (bytes_left == 0)
54 break;
55 if (tlv_type == IPV6_SRH_TLV_PAD1)
56 continue;
57
58 tlv_len = GET_U_1(p);
59 ND_PRINT(", TLV-len=%u", tlv_len);
60 ND_ICHECKMSG_U("remaining length", bytes_left, <, 1);
61 p += 1;
62 bytes_left -= 1;
63
64 switch (tlv_type) {
65 case IPV6_SRH_TLV_PADN:
66 ND_ICHECKMSG_U("PadN length", tlv_len, >, 5); /* RFC 8754 */
67 ND_ICHECKMSG_U("remaining length", bytes_left, <, tlv_len);
68 p += tlv_len;
69 bytes_left -= tlv_len;
70 break;
71 case IPV6_SRH_TLV_HMAC:
72 ND_ICHECKMSG_U("remaining length", bytes_left, <, 6);
73 uint16_t reserved;
74 uint32_t key_id;
75 uint8_t hmac_byte;
76 reserved = GET_BE_U_2(p);
77 p += 2;
78 if (ndo->ndo_vflag)
79 ND_PRINT(", D=%u", reserved >> 15);
80 if (ndo->ndo_vflag && (reserved & 0x7fff) != 0)
81 ND_PRINT(", reserved MBZ %u", reserved & 0x7fff);
82 key_id = GET_BE_U_4(p);
83 p += 4;
84 if (ndo->ndo_vflag)
85 ND_PRINT(", HMAC-key-ID=0x%02x", key_id);
86 bytes_left -= 6;
87 if (ndo->ndo_vflag)
88 ND_PRINT(", HMAC=0x");
89 for (u_int i = 0; i < tlv_len; i++) {
90 hmac_byte = GET_U_1(p);
91 ND_ICHECKMSG_U("remaining length", bytes_left, <, 1);
92 p += 1;
93 bytes_left -= 1;
94 if (ndo->ndo_vflag)
95 ND_PRINT("%02x", hmac_byte);
96 }
97 break;
98 default: /* Unknown type */
99 if (ndo->ndo_vflag)
100 ND_PRINT(", TLV-value=0x");
101 ND_ICHECKMSG_U("remaining length", bytes_left, <, tlv_len);
102 uint8_t tlv_byte;
103 for (u_int i = 0; i < tlv_len; i++) {
104 tlv_byte = GET_U_1(p);
105 p += 1;
106 bytes_left -= 1;
107 if (ndo->ndo_vflag)
108 ND_PRINT("%02x", tlv_byte);
109 }
110 break;
111 }
112 }
113 return 0;
114
115 invalid:
116 return -1;
117 }
118
119
120 int
121 rt6_print(netdissect_options *ndo, const u_char *bp, const u_char *bp2 _U_)
122 {
123 const struct ip6_rthdr *dp;
124 const struct ip6_rthdr0 *dp0;
125 const struct ip6_srh *srh;
126 u_int i, len, type, seg_list_len, last_entry;
127 int err;
128 const u_char *p;
129
130 ndo->ndo_protocol = "rt6";
131
132 nd_print_protocol_caps(ndo);
133 dp = (const struct ip6_rthdr *)bp;
134
135 len = GET_U_1(dp->ip6r_len);
136 ND_PRINT(" (len=%u", len); /*)*/
137 type = GET_U_1(dp->ip6r_type);
138 ND_PRINT(", type=%u", type);
139 if (type == IPV6_RTHDR_TYPE_0)
140 ND_PRINT(" [Deprecated]");
141 ND_PRINT(", segleft=%u", GET_U_1(dp->ip6r_segleft));
142
143 switch (type) {
144 case IPV6_RTHDR_TYPE_0:
145 case IPV6_RTHDR_TYPE_2: /* Mobile IPv6 ID-20 */
146 dp0 = (const struct ip6_rthdr0 *)dp;
147
148 if (GET_BE_U_4(dp0->ip6r0_reserved) || ndo->ndo_vflag) {
149 ND_PRINT(", rsv=0x%0x",
150 GET_BE_U_4(dp0->ip6r0_reserved));
151 }
152
153 if (len % 2 == 1) {
154 ND_PRINT(" [length %u]", len);
155 goto invalid;
156 }
157 len >>= 1;
158 p = (const u_char *) dp0->ip6r0_addr;
159 for (i = 0; i < len; i++) {
160 ND_PRINT(", [%u]%s", i, GET_IP6ADDR_STRING(p));
161 p += 16;
162 }
163 /*(*/
164 ND_PRINT(") ");
165 return((GET_U_1(dp0->ip6r0_len) + 1) << 3);
166 case IPV6_RTHDR_TYPE_4:
167 srh = (const struct ip6_srh *)dp;
168 last_entry = GET_U_1(srh->srh_last_ent);
169 ND_PRINT(", last-entry=%u", last_entry);
170
171 if (GET_U_1(srh->srh_flags) || ndo->ndo_vflag) {
172 ND_PRINT(", flags=0x%0x",
173 GET_U_1(srh->srh_flags));
174 }
175
176 ND_PRINT(", tag=%x", GET_BE_U_2(srh->srh_tag));
177 p = (const u_char *) srh->srh_segments;
178 for (i = 0; i < last_entry + 1; i++) {
179 ND_PRINT(", [%u]%s", i, GET_IP6ADDR_STRING(p));
180 p += 16;
181 }
182 seg_list_len = (last_entry + 1) * 2;
183 if (seg_list_len < len) {
184 /* there is TLV */
185 u_int bytes_left;
186 bytes_left = (len - seg_list_len) * 8;
187 err = srh_tlv_print(ndo, p, bytes_left);
188 if (err)
189 goto invalid;
190 }
191
192 /*(*/
193 ND_PRINT(") ");
194 return((GET_U_1(srh->srh_len) + 1) << 3);
195 default:
196 ND_PRINT(" (unknown type)");
197 goto invalid;
198 }
199
200 invalid:
201 nd_print_invalid(ndo);
202 return -1;
203 }