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