xref: /minix/external/bsd/tcpdump/dist/print-ripng.c (revision fb9c64b2)
1 /*
2  * Copyright (c) 1989, 1990, 1991, 1993, 1994
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 #include <sys/cdefs.h>
23 #ifndef lint
24 __RCSID("$NetBSD: print-ripng.c,v 1.4 2014/11/20 03:05:03 christos Exp $");
25 #endif
26 
27 #define NETDISSECT_REWORKED
28 #ifdef HAVE_CONFIG_H
29 #include "config.h"
30 #endif
31 
32 #ifdef INET6
33 
34 #include <tcpdump-stdinc.h>
35 
36 #include "interface.h"
37 #include "addrtoname.h"
38 #include "extract.h"
39 
40 /*
41  * Copyright (C) 1995, 1996, 1997 and 1998 WIDE Project.
42  * All rights reserved.
43  *
44  * Redistribution and use in source and binary forms, with or without
45  * modification, are permitted provided that the following conditions
46  * are met:
47  * 1. Redistributions of source code must retain the above copyright
48  *    notice, this list of conditions and the following disclaimer.
49  * 2. Redistributions in binary form must reproduce the above copyright
50  *    notice, this list of conditions and the following disclaimer in the
51  *    documentation and/or other materials provided with the distribution.
52  * 3. Neither the name of the project nor the names of its contributors
53  *    may be used to endorse or promote products derived from this software
54  *    without specific prior written permission.
55  *
56  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
57  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
58  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
59  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
60  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
61  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
62  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
63  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
64  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
65  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
66  * SUCH DAMAGE.
67  */
68 #define	RIP6_VERSION	1
69 
70 #define	RIP6_REQUEST	1
71 #define	RIP6_RESPONSE	2
72 
73 struct netinfo6 {
74 	struct in6_addr	rip6_dest;
75 	uint16_t	rip6_tag;
76 	uint8_t		rip6_plen;
77 	uint8_t		rip6_metric;
78 };
79 
80 struct	rip6 {
81 	uint8_t		rip6_cmd;
82 	uint8_t		rip6_vers;
83 	uint8_t		rip6_res1[2];
84 	union {
85 		struct	netinfo6	ru6_nets[1];
86 		char	ru6_tracefile[1];
87 	} rip6un;
88 #define	rip6_nets	rip6un.ru6_nets
89 #define	rip6_tracefile	rip6un.ru6_tracefile
90 };
91 
92 #define	HOPCNT_INFINITY6	16
93 
94 #if !defined(IN6_IS_ADDR_UNSPECIFIED) && !defined(_MSC_VER) /* MSVC inline */
95 static int IN6_IS_ADDR_UNSPECIFIED(const struct in6_addr *addr)
96 {
97     static const struct in6_addr in6addr_any;        /* :: */
98     return (memcmp(addr, &in6addr_any, sizeof(*addr)) == 0);
99 }
100 #endif
101 
102 static int
103 rip6_entry_print(netdissect_options *ndo, register const struct netinfo6 *ni, int metric)
104 {
105 	int l;
106 	l = ND_PRINT((ndo, "%s/%d", ip6addr_string(ndo, &ni->rip6_dest), ni->rip6_plen));
107 	if (ni->rip6_tag)
108 		l += ND_PRINT((ndo, " [%d]", EXTRACT_16BITS(&ni->rip6_tag)));
109 	if (metric)
110 		l += ND_PRINT((ndo, " (%d)", ni->rip6_metric));
111 	return l;
112 }
113 
114 void
115 ripng_print(netdissect_options *ndo, const u_char *dat, unsigned int length)
116 {
117 	register const struct rip6 *rp = (struct rip6 *)dat;
118 	register const struct netinfo6 *ni;
119 	register u_int amt;
120 	register u_int i;
121 	int j;
122 	int trunc;
123 
124 	if (ndo->ndo_snapend < dat)
125 		return;
126 	amt = ndo->ndo_snapend - dat;
127 	i = min(length, amt);
128 	if (i < (sizeof(struct rip6) - sizeof(struct netinfo6)))
129 		return;
130 	i -= (sizeof(struct rip6) - sizeof(struct netinfo6));
131 
132 	switch (rp->rip6_cmd) {
133 
134 	case RIP6_REQUEST:
135 		j = length / sizeof(*ni);
136 		if (j == 1
137 		    &&  rp->rip6_nets->rip6_metric == HOPCNT_INFINITY6
138 		    &&  IN6_IS_ADDR_UNSPECIFIED(&rp->rip6_nets->rip6_dest)) {
139 			ND_PRINT((ndo, " ripng-req dump"));
140 			break;
141 		}
142 		if (j * sizeof(*ni) != length - 4)
143 			ND_PRINT((ndo, " ripng-req %d[%u]:", j, length));
144 		else
145 			ND_PRINT((ndo, " ripng-req %d:", j));
146 		trunc = ((i / sizeof(*ni)) * sizeof(*ni) != i);
147 		for (ni = rp->rip6_nets; i >= sizeof(*ni);
148 		    i -= sizeof(*ni), ++ni) {
149 			if (ndo->ndo_vflag > 1)
150 				ND_PRINT((ndo, "\n\t"));
151 			else
152 				ND_PRINT((ndo, " "));
153 			rip6_entry_print(ndo, ni, 0);
154 		}
155 		break;
156 	case RIP6_RESPONSE:
157 		j = length / sizeof(*ni);
158 		if (j * sizeof(*ni) != length - 4)
159 			ND_PRINT((ndo, " ripng-resp %d[%u]:", j, length));
160 		else
161 			ND_PRINT((ndo, " ripng-resp %d:", j));
162 		trunc = ((i / sizeof(*ni)) * sizeof(*ni) != i);
163 		for (ni = rp->rip6_nets; i >= sizeof(*ni);
164 		    i -= sizeof(*ni), ++ni) {
165 			if (ndo->ndo_vflag > 1)
166 				ND_PRINT((ndo, "\n\t"));
167 			else
168 				ND_PRINT((ndo, " "));
169 			rip6_entry_print(ndo, ni, ni->rip6_metric);
170 		}
171 		if (trunc)
172 			ND_PRINT((ndo, "[|ripng]"));
173 		break;
174 	default:
175 		ND_PRINT((ndo, " ripng-%d ?? %u", rp->rip6_cmd, length));
176 		break;
177 	}
178 	if (rp->rip6_vers != RIP6_VERSION)
179 		ND_PRINT((ndo, " [vers %d]", rp->rip6_vers));
180 }
181 #endif /* INET6 */
182