xref: /netbsd-src/external/bsd/tcpdump/dist/print-rip.c (revision b7b7574d3bf8eeb51a1fa3977b59142ec6434a55)
1 /*
2  * Copyright (c) 1989, 1990, 1991, 1993, 1994, 1996
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 #if 0
25 static const char rcsid[] _U_ =
26     "@(#) Header: /tcpdump/master/tcpdump/print-rip.c,v 1.59 2006-03-23 14:58:44 hannes Exp  (LBL)";
27 #else
28 __RCSID("$NetBSD: print-rip.c,v 1.4 2013/12/31 17:33:31 christos Exp $");
29 #endif
30 #endif
31 
32 #ifdef HAVE_CONFIG_H
33 #include "config.h"
34 #endif
35 
36 #include <tcpdump-stdinc.h>
37 
38 #include <stdio.h>
39 #include <string.h>
40 
41 #include "interface.h"
42 #include "addrtoname.h"
43 #include "extract.h"			/* must come after interface.h */
44 
45 #include "af.h"
46 
47 struct rip {
48 	u_int8_t rip_cmd;		/* request/response */
49 	u_int8_t rip_vers;		/* protocol version # */
50 	u_int8_t unused[2];		/* unused */
51 };
52 
53 #define	RIPCMD_REQUEST		1	/* want info */
54 #define	RIPCMD_RESPONSE		2	/* responding to request */
55 #define	RIPCMD_TRACEON		3	/* turn tracing on */
56 #define	RIPCMD_TRACEOFF		4	/* turn it off */
57 #define	RIPCMD_POLL		5	/* want info from everybody */
58 #define	RIPCMD_POLLENTRY	6	/* poll for entry */
59 
60 static const struct tok rip_cmd_values[] = {
61     { RIPCMD_REQUEST,	        "Request" },
62     { RIPCMD_RESPONSE,	        "Response" },
63     { RIPCMD_TRACEON,	        "Trace on" },
64     { RIPCMD_TRACEOFF,	        "Trace off" },
65     { RIPCMD_POLL,	        "Poll" },
66     { RIPCMD_POLLENTRY,	        "Poll Entry" },
67     { 0, NULL}
68 };
69 
70 #define RIP_AUTHLEN  16
71 #define RIP_ROUTELEN 20
72 
73 /*
74  * rfc 1723
75  *
76  *  0                   1                   2                   3 3
77  *  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
78  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
79  * | Command (1)   | Version (1)   |           unused              |
80  * +---------------+---------------+-------------------------------+
81  * | Address Family Identifier (2) |        Route Tag (2)          |
82  * +-------------------------------+-------------------------------+
83  * |                         IP Address (4)                        |
84  * +---------------------------------------------------------------+
85  * |                         Subnet Mask (4)                       |
86  * +---------------------------------------------------------------+
87  * |                         Next Hop (4)                          |
88  * +---------------------------------------------------------------+
89  * |                         Metric (4)                            |
90  * +---------------------------------------------------------------+
91  *
92  */
93 
94 struct rip_netinfo {
95 	u_int16_t rip_family;
96 	u_int16_t rip_tag;
97 	u_int32_t rip_dest;
98 	u_int32_t rip_dest_mask;
99 	u_int32_t rip_router;
100 	u_int32_t rip_metric;		/* cost of route */
101 };
102 
103 static void
104 rip_entry_print_v1(register const struct rip_netinfo *ni)
105 {
106 	register u_short family;
107 
108 	/* RFC 1058 */
109 	family = EXTRACT_16BITS(&ni->rip_family);
110 	if (family != BSD_AFNUM_INET && family != 0) {
111 		printf("\n\t AFI %s, ", tok2str(bsd_af_values, "Unknown (%u)", family));
112                 print_unknown_data((u_int8_t *)&ni->rip_family,"\n\t  ",RIP_ROUTELEN);
113 		return;
114 	}
115 	if (EXTRACT_16BITS(&ni->rip_tag) ||
116 	    EXTRACT_32BITS(&ni->rip_dest_mask) ||
117 	    EXTRACT_32BITS(&ni->rip_router)) {
118 		/* MBZ fields not zero */
119                 print_unknown_data((u_int8_t *)&ni->rip_family,"\n\t  ",RIP_ROUTELEN);
120 		return;
121 	}
122 	if (family == 0) {
123 		printf("\n\t  AFI 0, %s, metric: %u",
124 			ipaddr_string(&ni->rip_dest),
125 			EXTRACT_32BITS(&ni->rip_metric));
126 		return;
127 	} /* BSD_AFNUM_INET */
128 	printf("\n\t  %s, metric: %u",
129                ipaddr_string(&ni->rip_dest),
130 	       EXTRACT_32BITS(&ni->rip_metric));
131 }
132 
133 static unsigned
134 rip_entry_print_v2(register const struct rip_netinfo *ni, const unsigned remaining)
135 {
136 	register u_short family;
137 
138 	family = EXTRACT_16BITS(&ni->rip_family);
139 	if (family == 0xFFFF) { /* variable-sized authentication structures */
140 		u_int16_t auth_type = EXTRACT_16BITS(&ni->rip_tag);
141 		if (auth_type == 2) {
142 			register u_char *p = (u_char *)&ni->rip_dest;
143 			u_int i = 0;
144 			printf("\n\t  Simple Text Authentication data: ");
145 			for (; i < RIP_AUTHLEN; p++, i++)
146 				putchar (isprint(*p) ? *p : '.');
147 		} else if (auth_type == 3) {
148 			printf("\n\t  Auth header:");
149 			printf(" Packet Len %u,", EXTRACT_16BITS((u_int8_t *)ni + 4));
150 			printf(" Key-ID %u,", *((u_int8_t *)ni + 6));
151 			printf(" Auth Data Len %u,", *((u_int8_t *)ni + 7));
152 			printf(" SeqNo %u,", EXTRACT_32BITS(&ni->rip_dest_mask));
153 			printf(" MBZ %u,", EXTRACT_32BITS(&ni->rip_router));
154 			printf(" MBZ %u", EXTRACT_32BITS(&ni->rip_metric));
155 		} else if (auth_type == 1) {
156 			printf("\n\t  Auth trailer:");
157 			print_unknown_data((u_int8_t *)&ni->rip_dest,"\n\t  ",remaining);
158 			return remaining; /* AT spans till the packet end */
159                 } else {
160 			printf("\n\t  Unknown (%u) Authentication data:",
161 			       EXTRACT_16BITS(&ni->rip_tag));
162 			print_unknown_data((u_int8_t *)&ni->rip_dest,"\n\t  ",remaining);
163 		}
164 	} else if (family != BSD_AFNUM_INET && family != 0) {
165 		printf("\n\t  AFI %s", tok2str(bsd_af_values, "Unknown (%u)", family));
166                 print_unknown_data((u_int8_t *)&ni->rip_tag,"\n\t  ",RIP_ROUTELEN-2);
167 	} else { /* BSD_AFNUM_INET or AFI 0 */
168 		printf("\n\t  AFI %s, %15s/%-2d, tag 0x%04x, metric: %u, next-hop: ",
169                        tok2str(bsd_af_values, "%u", family),
170                        ipaddr_string(&ni->rip_dest),
171 		       mask2plen(EXTRACT_32BITS(&ni->rip_dest_mask)),
172                        EXTRACT_16BITS(&ni->rip_tag),
173                        EXTRACT_32BITS(&ni->rip_metric));
174 		if (EXTRACT_32BITS(&ni->rip_router))
175 			printf("%s", ipaddr_string(&ni->rip_router));
176                 else
177                     printf("self");
178 	}
179 	return sizeof (*ni);
180 }
181 
182 void
183 rip_print(const u_char *dat, u_int length)
184 {
185 	register const struct rip *rp;
186 	register const struct rip_netinfo *ni;
187 	register u_int i, j;
188 
189 	if (snapend < dat) {
190 		printf(" [|rip]");
191 		return;
192 	}
193 	i = snapend - dat;
194 	if (i > length)
195 		i = length;
196 	if (i < sizeof(*rp)) {
197 		printf(" [|rip]");
198 		return;
199 	}
200 	i -= sizeof(*rp);
201 
202 	rp = (struct rip *)dat;
203 
204         printf("%sRIPv%u",
205                (vflag >= 1) ? "\n\t" : "",
206                rp->rip_vers);
207 
208 	switch (rp->rip_vers) {
209 	case 0:
210 		/*
211 		 * RFC 1058.
212 		 *
213 		 * XXX - RFC 1058 says
214 		 *
215 		 * 0  Datagrams whose version number is zero are to be ignored.
216 		 *    These are from a previous version of the protocol, whose
217 		 *    packet format was machine-specific.
218 		 *
219 		 * so perhaps we should just dump the packet, in hex.
220 		 */
221                 print_unknown_data((u_int8_t *)&rp->rip_cmd,"\n\t",length);
222 		break;
223 	default:
224                 /* dump version and lets see if we know the commands name*/
225                 printf(", %s, length: %u",
226                        tok2str(rip_cmd_values,
227                                "unknown command (%u)",
228                                rp->rip_cmd),
229                        length);
230 
231                 if (vflag < 1)
232                     return;
233 
234 		switch (rp->rip_cmd) {
235 		case RIPCMD_REQUEST:
236 		case RIPCMD_RESPONSE:
237 			j = length / sizeof(*ni);
238                         printf(", routes: %u%s", j, rp->rip_vers == 2 ? " or less" : "");
239 			ni = (struct rip_netinfo *)(rp + 1);
240 			for (; i >= sizeof(*ni); ++ni) {
241 				if (rp->rip_vers == 1)
242 				{
243 					rip_entry_print_v1(ni);
244 					i -= sizeof(*ni);
245 				}
246 				else if (rp->rip_vers == 2)
247 					i -= rip_entry_print_v2(ni, i);
248                                 else
249                                     break;
250 			}
251 			if (i)
252 				printf("[|rip]");
253 			break;
254 
255 		case RIPCMD_TRACEOFF:
256 		case RIPCMD_POLL:
257 		case RIPCMD_POLLENTRY:
258 			break;
259 
260 		case RIPCMD_TRACEON:
261                     /* fall through */
262 	        default:
263                     if (vflag <= 1) {
264                         if(!print_unknown_data((u_int8_t *)rp,"\n\t",length))
265                             return;
266                     }
267                     break;
268                 }
269                 /* do we want to see an additionally hexdump ? */
270                 if (vflag> 1) {
271                     if(!print_unknown_data((u_int8_t *)rp,"\n\t",length))
272                         return;
273                 }
274         }
275 }
276 
277 
278