xref: /openbsd/usr.sbin/tcpdump/print-rt6.c (revision 3a50f0a9)
1 /*	$OpenBSD: print-rt6.c,v 1.10 2022/12/28 21:30:19 jmc 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
rt6_print(const u_char * bp,const u_char * bp2)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 u_char *ep;
53 	int i, len;
54 
55 	dp = (struct ip6_rthdr *)bp;
56 	len = dp->ip6r_len;
57 
58 	/* 'ep' points to the end of available data. */
59 	ep = snapend;
60 
61 #if 0
62 	printf("%s > %s: ",
63 	       ip6addr_string(&ip->ip6_src),
64 	       ip6addr_string(&ip->ip6_dst));
65 #endif
66 
67 	TCHECK(dp->ip6r_segleft);
68 
69 	printf("srcrt (len=%d, ", dp->ip6r_len);
70 	printf("type=%d, ", dp->ip6r_type);
71 	printf("segleft=%d", dp->ip6r_segleft);
72 
73 	switch (dp->ip6r_type) {
74 	case IPV6_RTHDR_TYPE_0:
75 		dp0 = (struct ip6_rthdr0 *)dp;
76 
77 		TCHECK(dp0->ip6r0_reserved);
78 		if (dp0->ip6r0_reserved || vflag) {
79 			printf(", rsv=0x%0x",
80 				(u_int32_t)ntohl(dp0->ip6r0_reserved));
81 		}
82 
83 		if (len % 2 == 1)
84 			goto trunc;
85 		len >>= 1;
86 		for (i = 0; i < len; i++) {
87 			struct in6_addr *addr;
88 
89 			addr = ((struct in6_addr *)(dp0 + 1)) + i;
90 			if ((u_char *)addr > ep - sizeof(*addr))
91 				goto trunc;
92 
93 			printf(", [%d]%s", i, ip6addr_string((u_char *)addr));
94 		}
95 		printf(")");
96 		return((dp0->ip6r0_len + 1) << 3);
97 	default:
98 		if (bp + ((dp->ip6r_len + 1) << 3) > ep)
99 			goto trunc;
100 		printf(")");
101 		return((dp->ip6r_len + 1) << 3);
102 	}
103 
104  trunc:
105 	printf(", [|srcrt]");
106 	return 65535;		/* XXX */
107 }
108