]> The Tcpdump Group git mirrors - tcpdump/blob - print-ip.c
SNMP v2c patches from http://www.ibr.cs.tu-bs.de/ietf/snmpv3/tcpdump.shtml
[tcpdump] / print-ip.c
1 /*
2 * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997
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 #ifndef lint
23 static const char rcsid[] =
24 "@(#) $Header: /tcpdump/master/tcpdump/print-ip.c,v 1.66 1999-10-07 23:47:11 mcr Exp $ (LBL)";
25 #endif
26
27 #include <sys/param.h>
28 #include <sys/time.h>
29 #include <sys/socket.h>
30
31 #include <netinet/in.h>
32 #include <netinet/in_systm.h>
33 #include <netinet/ip.h>
34 #include <netinet/ip_var.h>
35 #include <netinet/udp.h>
36 #include <netinet/udp_var.h>
37 #include <netinet/tcp.h>
38 #include <netinet/tcpip.h>
39
40 #ifdef HAVE_MALLOC_H
41 #include <malloc.h>
42 #endif
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <string.h>
46 #include <unistd.h>
47
48 #include "addrtoname.h"
49 #include "interface.h"
50 #include "extract.h" /* must come after interface.h */
51
52 /* Compatibility */
53 #ifndef IPPROTO_ND
54 #define IPPROTO_ND 77
55 #endif
56
57 #ifndef IN_CLASSD
58 #define IN_CLASSD(i) (((int32_t)(i) & 0xf0000000) == 0xe0000000)
59 #endif
60
61 /* (following from ipmulti/mrouted/prune.h) */
62
63 /*
64 * The packet format for a traceroute request.
65 */
66 struct tr_query {
67 u_int tr_src; /* traceroute source */
68 u_int tr_dst; /* traceroute destination */
69 u_int tr_raddr; /* traceroute response address */
70 #ifdef WORDS_BIGENDIAN
71 struct {
72 u_int ttl : 8; /* traceroute response ttl */
73 u_int qid : 24; /* traceroute query id */
74 } q;
75 #else
76 struct {
77 u_int qid : 24; /* traceroute query id */
78 u_int ttl : 8; /* traceroute response ttl */
79 } q;
80 #endif
81 };
82
83 #define tr_rttl q.ttl
84 #define tr_qid q.qid
85
86 /*
87 * Traceroute response format. A traceroute response has a tr_query at the
88 * beginning, followed by one tr_resp for each hop taken.
89 */
90 struct tr_resp {
91 u_int tr_qarr; /* query arrival time */
92 u_int tr_inaddr; /* incoming interface address */
93 u_int tr_outaddr; /* outgoing interface address */
94 u_int tr_rmtaddr; /* parent address in source tree */
95 u_int tr_vifin; /* input packet count on interface */
96 u_int tr_vifout; /* output packet count on interface */
97 u_int tr_pktcnt; /* total incoming packets for src-grp */
98 u_char tr_rproto; /* routing proto deployed on router */
99 u_char tr_fttl; /* ttl required to forward on outvif */
100 u_char tr_smask; /* subnet mask for src addr */
101 u_char tr_rflags; /* forwarding error codes */
102 };
103
104 /* defs within mtrace */
105 #define TR_QUERY 1
106 #define TR_RESP 2
107
108 /* fields for tr_rflags (forwarding error codes) */
109 #define TR_NO_ERR 0
110 #define TR_WRONG_IF 1
111 #define TR_PRUNED 2
112 #define TR_OPRUNED 3
113 #define TR_SCOPED 4
114 #define TR_NO_RTE 5
115 #define TR_NO_FWD 7
116 #define TR_NO_SPACE 0x81
117 #define TR_OLD_ROUTER 0x82
118
119 /* fields for tr_rproto (routing protocol) */
120 #define TR_PROTO_DVMRP 1
121 #define TR_PROTO_MOSPF 2
122 #define TR_PROTO_PIM 3
123 #define TR_PROTO_CBT 4
124
125 static void print_mtrace(register const u_char *bp, register u_int len)
126 {
127 register struct tr_query *tr = (struct tr_query *)(bp + 8);
128
129 printf("mtrace %d: %s to %s reply-to %s", tr->tr_qid,
130 ipaddr_string(&tr->tr_src), ipaddr_string(&tr->tr_dst),
131 ipaddr_string(&tr->tr_raddr));
132 if (IN_CLASSD(ntohl(tr->tr_raddr)))
133 printf(" with-ttl %d", tr->tr_rttl);
134 }
135
136 static void print_mresp(register const u_char *bp, register u_int len)
137 {
138 register struct tr_query *tr = (struct tr_query *)(bp + 8);
139
140 printf("mresp %d: %s to %s reply-to %s", tr->tr_qid,
141 ipaddr_string(&tr->tr_src), ipaddr_string(&tr->tr_dst),
142 ipaddr_string(&tr->tr_raddr));
143 if (IN_CLASSD(ntohl(tr->tr_raddr)))
144 printf(" with-ttl %d", tr->tr_rttl);
145 }
146
147 static void
148 igmp_print(register const u_char *bp, register u_int len,
149 register const u_char *bp2)
150 {
151 register const struct ip *ip;
152
153 ip = (const struct ip *)bp2;
154 (void)printf("%s > %s: ",
155 ipaddr_string(&ip->ip_src),
156 ipaddr_string(&ip->ip_dst));
157
158 TCHECK2(bp[0], 8);
159 switch (bp[0]) {
160 case 0x11:
161 (void)printf("igmp query");
162 if (*(int *)&bp[4])
163 (void)printf(" [gaddr %s]", ipaddr_string(&bp[4]));
164 if (len != 8)
165 (void)printf(" [len %d]", len);
166 break;
167 case 0x12:
168 (void)printf("igmp report %s", ipaddr_string(&bp[4]));
169 if (len != 8)
170 (void)printf(" [len %d]", len);
171 break;
172 case 0x16:
173 (void)printf("igmp nreport %s", ipaddr_string(&bp[4]));
174 break;
175 case 0x17:
176 (void)printf("igmp leave %s", ipaddr_string(&bp[4]));
177 break;
178 case 0x13:
179 (void)printf("igmp dvmrp");
180 if (len < 8)
181 (void)printf(" [len %d]", len);
182 else
183 dvmrp_print(bp, len);
184 break;
185 case 0x14:
186 (void)printf("igmp pim");
187 pim_print(bp, len);
188 break;
189 case 0x1e:
190 print_mresp(bp, len);
191 break;
192 case 0x1f:
193 print_mtrace(bp, len);
194 break;
195 default:
196 (void)printf("igmp-%d", bp[0] & 0xf);
197 break;
198 }
199 if ((bp[0] >> 4) != 1)
200 (void)printf(" [v%d]", bp[0] >> 4);
201
202 TCHECK2(bp[0], len);
203 if (vflag) {
204 /* Check the IGMP checksum */
205 u_int32_t sum = 0;
206 int count;
207 const u_short *sp = (u_short *)bp;
208
209 for (count = len / 2; --count >= 0; )
210 sum += *sp++;
211 if (len & 1)
212 sum += ntohs(*(u_char *) sp << 8);
213 while (sum >> 16)
214 sum = (sum & 0xffff) + (sum >> 16);
215 sum = 0xffff & ~sum;
216 if (sum != 0)
217 printf(" bad igmp cksum %x!", EXTRACT_16BITS(&bp[2]));
218 }
219 return;
220 trunc:
221 fputs("[|igmp]", stdout);
222 }
223
224 /*
225 * print the recorded route in an IP RR, LSRR or SSRR option.
226 */
227 static void
228 ip_printroute(const char *type, register const u_char *cp, u_int length)
229 {
230 register u_int ptr = cp[2] - 1;
231 register u_int len;
232
233 printf(" %s{", type);
234 if ((length + 1) & 3)
235 printf(" [bad length %d]", length);
236 if (ptr < 3 || ((ptr + 1) & 3) || ptr > length + 1)
237 printf(" [bad ptr %d]", cp[2]);
238
239 type = "";
240 for (len = 3; len < length; len += 4) {
241 if (ptr == len)
242 type = "#";
243 printf("%s%s", type, ipaddr_string(&cp[len]));
244 type = " ";
245 }
246 printf("%s}", ptr == len? "#" : "");
247 }
248
249 /*
250 * print IP options.
251 */
252 static void
253 ip_optprint(register const u_char *cp, u_int length)
254 {
255 register u_int len;
256
257 for (; length > 0; cp += len, length -= len) {
258 int tt = *cp;
259
260 len = (tt == IPOPT_NOP || tt == IPOPT_EOL) ? 1 : cp[1];
261 if (len <= 0) {
262 printf("[|ip op len %d]", len);
263 return;
264 }
265 if (&cp[1] >= snapend || cp + len > snapend) {
266 printf("[|ip]");
267 return;
268 }
269 switch (tt) {
270
271 case IPOPT_EOL:
272 printf(" EOL");
273 if (length > 1)
274 printf("-%d", length - 1);
275 return;
276
277 case IPOPT_NOP:
278 printf(" NOP");
279 break;
280
281 case IPOPT_TS:
282 printf(" TS{%d}", len);
283 break;
284
285 case IPOPT_SECURITY:
286 printf(" SECURITY{%d}", len);
287 break;
288
289 case IPOPT_RR:
290 printf(" RR{%d}=", len);
291 ip_printroute("RR", cp, len);
292 break;
293
294 case IPOPT_SSRR:
295 ip_printroute("SSRR", cp, len);
296 break;
297
298 case IPOPT_LSRR:
299 ip_printroute("LSRR", cp, len);
300 break;
301
302 default:
303 printf(" IPOPT-%d{%d}", cp[0], len);
304 break;
305 }
306 }
307 }
308
309 /*
310 * compute an IP header checksum.
311 * don't modifiy the packet.
312 */
313 static int
314 in_cksum(const struct ip *ip)
315 {
316 register const u_short *sp = (u_short *)ip;
317 register u_int32_t sum = 0;
318 register int count;
319
320 /*
321 * No need for endian conversions.
322 */
323 for (count = ip->ip_hl * 2; --count >= 0; )
324 sum += *sp++;
325 while (sum > 0xffff)
326 sum = (sum & 0xffff) + (sum >> 16);
327 sum = ~sum & 0xffff;
328
329 return (sum);
330 }
331
332 /*
333 * print an IP datagram.
334 */
335 void
336 ip_print(register const u_char *bp, register u_int length)
337 {
338 register const struct ip *ip;
339 register u_int hlen, len, off;
340 register const u_char *cp;
341
342 ip = (const struct ip *)bp;
343 #ifdef LBL_ALIGN
344 /*
345 * If the IP header is not aligned, copy into abuf.
346 * This will never happen with BPF. It does happen raw packet
347 * dumps from -r.
348 */
349 if ((long)ip & 3) {
350 static u_char *abuf = NULL;
351 static int didwarn = 0;
352
353 if (abuf == NULL) {
354 abuf = (u_char *)malloc(snaplen);
355 if (abuf == NULL)
356 error("ip_print: malloc");
357 }
358 memcpy((char *)abuf, (char *)ip, min(length, snaplen));
359 snapend += abuf - (u_char *)ip;
360 packetp = abuf;
361 ip = (struct ip *)abuf;
362 /* We really want libpcap to give us aligned packets */
363 if (!didwarn) {
364 warning("compensating for unaligned libpcap packets");
365 ++didwarn;
366 }
367 }
368 #endif
369 if ((u_char *)(ip + 1) > snapend) {
370 printf("[|ip]");
371 return;
372 }
373 if (length < sizeof (struct ip)) {
374 (void)printf("truncated-ip %d", length);
375 return;
376 }
377 hlen = ip->ip_hl * 4;
378
379 len = ntohs(ip->ip_len);
380 if (length < len)
381 (void)printf("truncated-ip - %d bytes missing!",
382 len - length);
383 len -= hlen;
384
385 /*
386 * If this is fragment zero, hand it to the next higher
387 * level protocol.
388 */
389 off = ntohs(ip->ip_off);
390 if ((off & 0x1fff) == 0) {
391 cp = (const u_char *)ip + hlen;
392 switch (ip->ip_p) {
393
394 case IPPROTO_TCP:
395 tcp_print(cp, len, (const u_char *)ip);
396 break;
397
398 case IPPROTO_UDP:
399 udp_print(cp, len, (const u_char *)ip);
400 break;
401
402 case IPPROTO_ICMP:
403 icmp_print(cp, (const u_char *)ip);
404 break;
405
406 #ifndef IPPROTO_IGRP
407 #define IPPROTO_IGRP 9
408 #endif
409 case IPPROTO_IGRP:
410 igrp_print(cp, len, (const u_char *)ip);
411 break;
412
413 case IPPROTO_ND:
414 (void)printf("%s > %s:", ipaddr_string(&ip->ip_src),
415 ipaddr_string(&ip->ip_dst));
416 (void)printf(" nd %d", len);
417 break;
418
419 case IPPROTO_EGP:
420 egp_print(cp, len, (const u_char *)ip);
421 break;
422
423 #ifndef IPPROTO_OSPF
424 #define IPPROTO_OSPF 89
425 #endif
426 case IPPROTO_OSPF:
427 ospf_print(cp, len, (const u_char *)ip);
428 break;
429
430 #ifndef IPPROTO_IGMP
431 #define IPPROTO_IGMP 2
432 #endif
433 case IPPROTO_IGMP:
434 igmp_print(cp, len, (const u_char *)ip);
435 break;
436
437 case 4:
438 /* DVMRP multicast tunnel (ip-in-ip encapsulation) */
439 if (vflag)
440 (void)printf("%s > %s: ",
441 ipaddr_string(&ip->ip_src),
442 ipaddr_string(&ip->ip_dst));
443 ip_print(cp, len);
444 if (! vflag) {
445 printf(" (ipip)");
446 return;
447 }
448 break;
449
450 #ifndef IPPROTO_GRE
451 #define IPPROTO_GRE 47
452 #endif
453 case IPPROTO_GRE:
454 if (vflag)
455 (void)printf("gre %s > %s: ",
456 ipaddr_string(&ip->ip_src),
457 ipaddr_string(&ip->ip_dst));
458 /* do it */
459 gre_print(cp, len);
460 if (! vflag) {
461 printf(" (gre encap)");
462 return;
463 }
464 break;
465
466 default:
467 (void)printf("%s > %s:", ipaddr_string(&ip->ip_src),
468 ipaddr_string(&ip->ip_dst));
469 (void)printf(" ip-proto-%d %d", ip->ip_p, len);
470 break;
471 }
472 }
473 /*
474 * for fragmented datagrams, print id:size@offset. On all
475 * but the last stick a "+". For unfragmented datagrams, note
476 * the don't fragment flag.
477 */
478 if (off & 0x3fff) {
479 /*
480 * if this isn't the first frag, we're missing the
481 * next level protocol header. print the ip addr.
482 */
483 if (off & 0x1fff)
484 (void)printf("%s > %s:", ipaddr_string(&ip->ip_src),
485 ipaddr_string(&ip->ip_dst));
486 (void)printf(" (frag %d:%d@%d%s)", ntohs(ip->ip_id), len,
487 (off & 0x1fff) * 8,
488 (off & IP_MF)? "+" : "");
489 } else if (off & IP_DF)
490 (void)printf(" (DF)");
491
492 if (ip->ip_tos)
493 (void)printf(" [tos 0x%x]", (int)ip->ip_tos);
494 if (ip->ip_ttl <= 1)
495 (void)printf(" [ttl %d]", (int)ip->ip_ttl);
496
497 if (vflag) {
498 int sum;
499 char *sep = "";
500
501 printf(" (");
502 if (ip->ip_ttl > 1) {
503 (void)printf("%sttl %d", sep, (int)ip->ip_ttl);
504 sep = ", ";
505 }
506 if ((off & 0x3fff) == 0) {
507 (void)printf("%sid %d", sep, (int)ntohs(ip->ip_id));
508 sep = ", ";
509 }
510 if ((u_char *)ip + hlen <= snapend) {
511 sum = in_cksum(ip);
512 if (sum != 0) {
513 (void)printf("%sbad cksum %x!", sep,
514 ntohs(ip->ip_sum));
515 sep = ", ";
516 }
517 }
518 if ((hlen -= sizeof(struct ip)) > 0) {
519 (void)printf("%soptlen=%d", sep, hlen);
520 ip_optprint((u_char *)(ip + 1), hlen);
521 }
522 printf(")");
523 }
524 }