-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathph_icmp6.cc
339 lines (249 loc) · 9.73 KB
/
ph_icmp6.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
/*
* Copyright (c) 2001 - 2003
* Sebastian Majewski. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by Sebastian Majewski
* and his contributors.
* 4. Neither the name of Sebastian Majewski nor the names of his contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <netz.h>
#include "ph_packet.h"
#include "support.h"
c_string print_icmp6_info(u_int, u_int);
c_string icmp6_packet_handler(c_packet_info packet_info)
{
c_string output_string;
c_icmp6_header header(packet_info.packet);
output_string.add((char*)"ICMPv6\t");
output_string.add((char*)"TYPE %u CODE %u '",
header.get_type(), header.get_code());
output_string += print_icmp6_info(header.get_type(), header.get_code());
output_string.add((char*)"'\n");
output_string.add((char*)"\t(LEN %u) CKSUM %u ",
packet_info.packet_len, header.get_cksum());
c_pseudo_header pseudo_header;
if(packet_info.previous_packet_type == PACKET_TYPE_IP)
{
c_ipp_header ipp_header(c_ip_header(packet_info.previous_packet));
pseudo_header = ipp_header.get_pseudo_header();
}
if(packet_info.previous_packet_type == PACKET_TYPE_IP6)
{
c_ip6p_header ip6p_header(c_ip6_header(packet_info.previous_packet));
pseudo_header = ip6p_header.get_pseudo_header();
}
if(!cksum(packet_info.packet, packet_info.packet_len,
pseudo_header))
{
output_string.add((char*)"(OK)");
}
else
{
output_string.add((char*)"(BAD)");
}
output_string.add((char*)" ");
switch(header.get_type())
{
case ICMP6_PACKET_TOO_BIG: {
c_icmp6_pkttoobig body(header);
output_string.add((char*)"MTU %u\n",
body.get_mtu());
break;
}
case ICMP6_PARAMPROB: {
c_icmp6_paramprob body(header);
output_string.add((char*)"PTR %u\n",
body.get_pointer());
break;
}
case ICMP6_ECHO_REQUEST: {
c_icmp6_echorequest body(header);
output_string.add((char*)"ID %u SQN %u\n",
body.get_id(),
body.get_seqnumber());
break;
}
case ICMP6_ECHO_REPLY: {
c_icmp6_echoreply body(header);
output_string.add((char*)"ID %u SQN %u\n",
body.get_id(),
body.get_seqnumber());
break;
}
case ICMP6_NEIGHBOR_SOLICITATION: {
c_icmp6_nbsolicit body(header);
string addr_string[40];
output_string.add((char*)"\n\tTARGET %s\n", conv_ip6_str(addr_string,
body.get_target()));
break;
}
case ICMP6_NEIGHBOR_ADVERTISEMENT: {
c_icmp6_nbadvert body(header);
output_string.add((char*)"\n\tFLAGS |");
if(body.get_flag_router())
{
output_string.add((char*)"R|");
}
else
{
output_string.add((char*)" |");
}
if(body.get_flag_solicited())
{
output_string.add((char*)"S|");
}
else
{
output_string.add((char*)" |");
}
if(body.get_flag_override())
{
output_string.add((char*)"O|");
}
else
{
output_string.add((char*)" |");
}
string addr_string[40];
output_string.add((char*)" TARGET %s\n", conv_ip6_str(addr_string,
body.get_target()));
break;
}
case ICMP6_ROUTER_ADVERTISEMENT: {
c_icmp6_routeradvert body(header);
output_string.add((char*)"\n\tHLIMIT %u RTRLT %u FLAGS |",
body.get_hoplimit(),
body.get_lifetime());
if(body.get_flag_mac())
{
output_string.add((char*)"M|");
}
else
{
output_string.add((char*)" |");
}
if(body.get_flag_osc())
{
output_string.add((char*)"O|");
}
else
{
output_string.add((char*)" |");
}
output_string.add((char*)" REACHT %u RETRT %u\n",
body.get_reachtimer(),
body.get_retrtimer());
break;
}
default:
output_string.add((char*)"\n");
}
output_string += debug(packet_info);
output_string += print_line();
return output_string;
}
c_string print_icmp6_info(u_int type, u_int code)
{
switch(type)
{
case ICMP6_UNREACH:
switch(code)
{
case ICMP6_UNREACH_NOROUTE:
return c_string((char*)"No Route to Destination");
case ICMP6_UNREACH_DSTPROHIB:
return c_string((char*)"Communication with Destination "
"Administratively Prohibited");
case ICMP6_UNREACH_NOTNEIGHBOR:
return c_string((char*)"No Route to Destination - Not a Neighbor");
case ICMP6_UNREACH_BADADDR:
return c_string((char*)"Destination Unreachable - Bad Address");
case ICMP6_UNREACH_BADPORT:
return c_string((char*)"Destination Unreachable - Bad Port");
}
case ICMP6_PACKET_TOO_BIG:
return c_string((char*)"Packet is Larger than the MTU of the Outgoing Link");
case ICMP6_TIMEXCEED:
switch(code)
{
case ICMP6_TIMEXCEED_TRANSIT:
return c_string((char*)"Hop Limit Exceeded in Transit");
case ICMP6_TIMEXCEED_REASSEMBLY:
return c_string((char*)"Fragment Reassembly Time Exceeded");
}
case ICMP6_PARAMPROB:
switch(code)
{
case ICMP6_PARAMPROB_HEADER:
return c_string((char*)"Erroneous Header Field Encountered");
case ICMP6_PARAMPROB_NEXTHEADER:
return c_string((char*)"Unrecognized Next Header Type Encountered");
case ICMP6_PARAMPROB_OPTION:
return c_string((char*)"Unrecognized IPv6 Option Encountered");
}
case ICMP6_ECHO_REQUEST:
return c_string((char*)"Echo Request");
case ICMP6_ECHO_REPLY:
return c_string((char*)"Echo Reply");
case ICMP6_MEMBERSHIP_QUERY:
return c_string((char*)"Membership Query");
case ICMP6_MEMBERSHIP_REPORT:
return c_string((char*)"Membership Report");
case ICMP6_MEMBERSHIP_REDUCTION:
return c_string((char*)"Membership Reduction");
case ICMP6_ROUTER_SOLICITATION:
return c_string((char*)"Router Solicitation");
case ICMP6_ROUTER_ADVERTISEMENT:
return c_string((char*)"Router Advertisement");
case ICMP6_NEIGHBOR_SOLICITATION:
return c_string((char*)"Neighbor Solicitation");
case ICMP6_NEIGHBOR_ADVERTISEMENT:
return c_string((char*)"Neighbor Advertisement");
case ICMP6_REDIRECT:
return c_string((char*)"Redirect");
case ICMP6_ROUTER_RENUM:
switch(code)
{
case ICMP6_ROUTER_RENUM_COMMAND:
return c_string((char*)"Router Renumbering Command");
case ICMP6_ROUTER_RENUM_RESULT:
return c_string((char*)"Router Renumbering Result");
case ICMP6_ROUTER_RENUM_SNRESET:
return c_string((char*)"Sequence Number Reset");
}
case ICMP6_NODE_INFORMATION_QUERRY:
return c_string((char*)"ICMP Node Information Query");
case ICMP6_NODE_INFORMATION_RESPONSE:
return c_string((char*)"ICMP Node Information Response");
case ICMP6_INV_NEIGHBOR_SOLICICATION:
return c_string((char*)"Inverse Neighbor Discovery Solicitation");
case ICMP6_INV_NEIGHBOR_ADVERTISEMENT:
return c_string((char*)"Inverse Neighbor Discovery Advertisement");
default:
return c_string((char*)"Unknown");
}
}