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