xref: /openbsd/usr.sbin/tcpdump/print-rt6.c (revision 73471bf0)
1 /*	$OpenBSD: print-rt6.c,v 1.8 2020/01/24 22:46:37 procter Exp $	*/
2 
3 
4 /*
5  * Copyright (c) 1988, 1989, 1990, 1991, 1993, 1994
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that: (1) source code distributions
10  * retain the above copyright notice and this paragraph in its entirety, (2)
11  * distributions including binary code include the above copyright notice and
12  * this paragraph in its entirety in the documentation or other materials
13  * provided with the distribution, and (3) all advertising materials mentioning
14  * features or use of this software display the following acknowledgement:
15  * ``This product includes software developed by the University of California,
16  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
17  * the University nor the names of its contributors may be used to endorse
18  * or promote products derived from this software without specific prior
19  * written permission.
20  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
21  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
22  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
23  */
24 
25 #include <sys/time.h>
26 #include <sys/types.h>
27 #include <sys/socket.h>
28 
29 #include <net/if.h>
30 
31 #include <netinet/in.h>
32 #include <netinet/if_ether.h>
33 #include <netinet/ip.h>
34 #include <netinet/ip_icmp.h>
35 #include <netinet/ip_var.h>
36 #include <netinet/udp.h>
37 #include <netinet/udp_var.h>
38 #include <netinet/tcp.h>
39 
40 #include <stdio.h>
41 
42 #include <netinet/ip6.h>
43 
44 #include "interface.h"
45 #include "addrtoname.h"
46 
47 int
48 rt6_print(const u_char *bp, const u_char *bp2)
49 {
50 	const struct ip6_rthdr *dp;
51 	const struct ip6_rthdr0 *dp0;
52 	const struct ip6_hdr *ip;
53 	const u_char *ep;
54 	int i, len;
55 
56 	dp = (struct ip6_rthdr *)bp;
57 	ip = (struct ip6_hdr *)bp2;
58 	len = dp->ip6r_len;
59 
60 	/* 'ep' points to the end of avaible data. */
61 	ep = snapend;
62 
63 #if 0
64 	printf("%s > %s: ",
65 	       ip6addr_string(&ip->ip6_src),
66 	       ip6addr_string(&ip->ip6_dst));
67 #endif
68 
69 	TCHECK(dp->ip6r_segleft);
70 
71 	printf("srcrt (len=%d, ", dp->ip6r_len);
72 	printf("type=%d, ", dp->ip6r_type);
73 	printf("segleft=%d", dp->ip6r_segleft);
74 
75 	switch (dp->ip6r_type) {
76 	case IPV6_RTHDR_TYPE_0:
77 		dp0 = (struct ip6_rthdr0 *)dp;
78 
79 		TCHECK(dp0->ip6r0_reserved);
80 		if (dp0->ip6r0_reserved || vflag) {
81 			printf(", rsv=0x%0x",
82 				(u_int32_t)ntohl(dp0->ip6r0_reserved));
83 		}
84 
85 		if (len % 2 == 1)
86 			goto trunc;
87 		len >>= 1;
88 		for (i = 0; i < len; i++) {
89 			struct in6_addr *addr;
90 
91 			addr = ((struct in6_addr *)(dp0 + 1)) + i;
92 			if ((u_char *)addr > ep - sizeof(*addr))
93 				goto trunc;
94 
95 			printf(", [%d]%s", i, ip6addr_string((u_char *)addr));
96 		}
97 		printf(")");
98 		return((dp0->ip6r0_len + 1) << 3);
99 	default:
100 		if (bp + ((dp->ip6r_len + 1) << 3) > ep)
101 			goto trunc;
102 		printf(")");
103 		return((dp->ip6r_len + 1) << 3);
104 	}
105 
106  trunc:
107 	printf(", [|srcrt]");
108 	return 65535;		/* XXX */
109 }
110