xref: /titanic_51/usr/src/cmd/cmd-inet/usr.sbin/snoop/snoop_rip6.c (revision 1e49577a7fcde812700ded04431b49d67cc57d6d)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"	/* SunOS */
28 
29 #include <ctype.h>
30 #include <fcntl.h>
31 #include <sys/socket.h>
32 #include <sys/types.h>
33 #include <net/if.h>
34 #include <netinet/in_systm.h>
35 #include <netinet/in.h>
36 #include <netinet/ip.h>
37 #include <inet/led.h>
38 #include <inet/ip6.h>
39 #include <arpa/inet.h>
40 #include <protocols/routed.h>
41 #include <protocols/ripngd.h>
42 #include "snoop.h"
43 
44 extern char *dlc_header;
45 static char *show_cmd6();
46 
47 static struct in6_addr all_zeroes_addr = { {	0x0, 0x0, 0x0, 0x0,
48 						0x0, 0x0, 0x0, 0x0,
49 						0x0, 0x0, 0x0, 0x0,
50 						0x0, 0x0, 0x0, 0x0 } };
51 
52 int
53 interpret_rip6(int flags, struct rip6 *rip6, int fraglen)
54 {
55 	char *p;
56 	struct netinfo6 *n;
57 	int len, count;
58 	struct in6_addr *dst;
59 	int notdefault = 0;
60 	char dststr[INET6_ADDRSTRLEN];
61 
62 	if (flags & F_SUM) {
63 		switch (rip6->rip6_cmd) {
64 		case RIPCMD6_REQUEST:	p = "C";		break;
65 		case RIPCMD6_RESPONSE:	p = "R";		break;
66 		default: p = "?"; break;
67 		}
68 
69 		switch (rip6->rip6_cmd) {
70 		case RIPCMD6_REQUEST:
71 		case RIPCMD6_RESPONSE:
72 			len = fraglen - 4;
73 			count = 0;
74 			for (n = rip6->rip6_nets;
75 			    len >= sizeof (struct netinfo6); n++) {
76 				count++;
77 				len -= sizeof (struct netinfo6);
78 			}
79 			(void) sprintf(get_sum_line(),
80 			    "RIPng %s (%d destinations)", p, count);
81 			break;
82 		default:
83 			(void) sprintf(get_sum_line(), "RIPng %s", p);
84 			break;
85 		}
86 	}
87 
88 	if (flags & F_DTAIL) {
89 
90 		show_header("RIPng:  ", "Routing Information Protocol for IPv6",
91 		    fraglen);
92 		show_space();
93 		(void) sprintf(get_line((char *)(uintptr_t)rip6->rip6_cmd -
94 		    dlc_header, 1), "Opcode = %d (%s)", rip6->rip6_cmd,
95 		    show_cmd6(rip6->rip6_cmd));
96 		(void) sprintf(get_line((char *)(uintptr_t)rip6->rip6_vers -
97 		    dlc_header, 1), "Version = %d", rip6->rip6_vers);
98 
99 		switch (rip6->rip6_cmd) {
100 		case RIPCMD6_REQUEST:
101 		case RIPCMD6_RESPONSE:
102 			show_space();
103 			(void) sprintf(get_line(0, 0), "Address"
104 			    "                       Prefix        Metric");
105 			len = fraglen - 4;
106 			for (n = rip6->rip6_nets;
107 			    len >= sizeof (struct netinfo6); n++) {
108 				if (rip6->rip6_vers > 0) {
109 					n->rip6_metric = n->rip6_metric;
110 				}
111 				dst = &n->rip6_prefix;
112 				notdefault = bcmp((caddr_t *)dst,
113 				    (caddr_t *)&all_zeroes_addr, sizeof (*dst));
114 				(void) inet_ntop(AF_INET6, (char *)dst, dststr,
115 				    INET6_ADDRSTRLEN);
116 				(void) sprintf(get_line((char *)n - dlc_header,
117 				    sizeof (struct netinfo6)),
118 				    "%-30s %-10d     %-5d %s",
119 				    notdefault ? dststr :
120 				    " (default)", n->rip6_prefix_length,
121 				    n->rip6_metric, n->rip6_metric == 16 ?
122 				    " (not reachable)":"");
123 				len -= sizeof (struct netinfo);
124 			}
125 			break;
126 		}
127 	}
128 	return (fraglen);
129 }
130 
131 static char *
132 show_cmd6(c)
133 	int c;
134 {
135 	switch (c) {
136 	case RIPCMD6_REQUEST:	return ("route request");
137 	case RIPCMD6_RESPONSE:	return ("route response");
138 	}
139 	return ("?");
140 }
141