Home | History | Annotate | Line # | Download | only in dist
print-rip.c revision 1.2.12.1
      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.2.12.1 2013/06/23 06:28:29 tls 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) {
    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 	} /* BSD_AFNUM_INET */
    122 	printf("\n\t  %s, metric: %u",
    123                ipaddr_string(&ni->rip_dest),
    124 	       EXTRACT_32BITS(&ni->rip_metric));
    125 }
    126 
    127 static void
    128 rip_entry_print_v2(register const struct rip_netinfo *ni)
    129 {
    130 	register u_char *p;
    131 	register u_short family;
    132 	u_char buf[RIP_AUTHLEN];
    133 
    134 	family = EXTRACT_16BITS(&ni->rip_family);
    135 	if (family == 0xFFFF) { /* 16 bytes authentication ? */
    136                 if (EXTRACT_16BITS(&ni->rip_tag) == 2) { /* simple text authentication ? */
    137 			memcpy(buf, &ni->rip_dest, sizeof(buf));
    138 			buf[sizeof(buf)-1] = '\0';
    139 			for (p = buf; *p; p++) {
    140 				if (!isprint(*p))
    141 					break;
    142 			}
    143                         printf("\n\t  Simple Text Authentication data: %s", buf);
    144                 } else {
    145 			printf("\n\t  Unknown (%u) Authentication data:",
    146 			       EXTRACT_16BITS(&ni->rip_tag));
    147                         print_unknown_data((u_int8_t *)&ni->rip_dest,"\n\t  ",RIP_AUTHLEN);
    148 		}
    149 	} else if (family != BSD_AFNUM_INET) {
    150 		printf("\n\t  AFI %s", tok2str(bsd_af_values, "Unknown (%u)", family));
    151                 print_unknown_data((u_int8_t *)&ni->rip_tag,"\n\t  ",RIP_ROUTELEN-2);
    152 		return;
    153 	} else { /* BSD_AFNUM_INET */
    154 		printf("\n\t  AFI %s, %15s/%-2d, tag 0x%04x, metric: %u, next-hop: ",
    155                        tok2str(bsd_af_values, "Unknown (%u)", family),
    156                        ipaddr_string(&ni->rip_dest),
    157 		       mask2plen(EXTRACT_32BITS(&ni->rip_dest_mask)),
    158                        EXTRACT_16BITS(&ni->rip_tag),
    159                        EXTRACT_32BITS(&ni->rip_metric));
    160 		if (EXTRACT_32BITS(&ni->rip_router))
    161 			printf("%s", ipaddr_string(&ni->rip_router));
    162                 else
    163                     printf("self");
    164 	}
    165 }
    166 
    167 void
    168 rip_print(const u_char *dat, u_int length)
    169 {
    170 	register const struct rip *rp;
    171 	register const struct rip_netinfo *ni;
    172 	register u_int i, j;
    173 	register int trunc;
    174 
    175 	if (snapend < dat) {
    176 		printf(" [|rip]");
    177 		return;
    178 	}
    179 	i = snapend - dat;
    180 	if (i > length)
    181 		i = length;
    182 	if (i < sizeof(*rp)) {
    183 		printf(" [|rip]");
    184 		return;
    185 	}
    186 	i -= sizeof(*rp);
    187 
    188 	rp = (struct rip *)dat;
    189 
    190         printf("%sRIPv%u",
    191                (vflag >= 1) ? "\n\t" : "",
    192                rp->rip_vers);
    193 
    194 	switch (rp->rip_vers) {
    195 	case 0:
    196 		/*
    197 		 * RFC 1058.
    198 		 *
    199 		 * XXX - RFC 1058 says
    200 		 *
    201 		 * 0  Datagrams whose version number is zero are to be ignored.
    202 		 *    These are from a previous version of the protocol, whose
    203 		 *    packet format was machine-specific.
    204 		 *
    205 		 * so perhaps we should just dump the packet, in hex.
    206 		 */
    207                 print_unknown_data((u_int8_t *)&rp->rip_cmd,"\n\t",length);
    208 		break;
    209 	default:
    210                 /* dump version and lets see if we know the commands name*/
    211                 printf(", %s, length: %u",
    212                        tok2str(rip_cmd_values,
    213                                "unknown command (%u)",
    214                                rp->rip_cmd),
    215                        length);
    216 
    217                 if (vflag < 1)
    218                     return;
    219 
    220 		switch (rp->rip_cmd) {
    221 		case RIPCMD_RESPONSE:
    222 			j = length / sizeof(*ni);
    223                         printf(", routes: %u",j);
    224 			trunc = (i / sizeof(*ni)) != j;
    225 			ni = (struct rip_netinfo *)(rp + 1);
    226 			for (; i >= sizeof(*ni); ++ni) {
    227 				if (rp->rip_vers == 1)
    228 					rip_entry_print_v1(ni);
    229 				else if (rp->rip_vers == 2)
    230 					rip_entry_print_v2(ni);
    231                                 else
    232                                     break;
    233 				i -= sizeof(*ni);
    234 			}
    235 			if (trunc)
    236 				printf("[|rip]");
    237 			break;
    238 
    239 		case RIPCMD_REQUEST:
    240 		case RIPCMD_TRACEOFF:
    241 		case RIPCMD_POLL:
    242 		case RIPCMD_POLLENTRY:
    243 			break;
    244 
    245 		case RIPCMD_TRACEON:
    246                     /* fall through */
    247 	        default:
    248                     if (vflag <= 1) {
    249                         if(!print_unknown_data((u_int8_t *)rp,"\n\t",length))
    250                             return;
    251                     }
    252                     break;
    253                 }
    254                 /* do we want to see an additionally hexdump ? */
    255                 if (vflag> 1) {
    256                     if(!print_unknown_data((u_int8_t *)rp,"\n\t",length))
    257                         return;
    258                 }
    259         }
    260 }
    261 
    262 
    263