Home | History | Annotate | Line # | Download | only in dist
print-rip.c revision 1.7.12.1
      1       1.1  christos /*
      2       1.1  christos  * Copyright (c) 1989, 1990, 1991, 1993, 1994, 1996
      3       1.1  christos  *	The Regents of the University of California.  All rights reserved.
      4       1.1  christos  *
      5       1.1  christos  * Redistribution and use in source and binary forms, with or without
      6       1.1  christos  * modification, are permitted provided that: (1) source code distributions
      7       1.1  christos  * retain the above copyright notice and this paragraph in its entirety, (2)
      8       1.1  christos  * distributions including binary code include the above copyright notice and
      9       1.1  christos  * this paragraph in its entirety in the documentation or other materials
     10       1.1  christos  * provided with the distribution, and (3) all advertising materials mentioning
     11       1.1  christos  * features or use of this software display the following acknowledgement:
     12       1.1  christos  * ``This product includes software developed by the University of California,
     13       1.1  christos  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
     14       1.1  christos  * the University nor the names of its contributors may be used to endorse
     15       1.1  christos  * or promote products derived from this software without specific prior
     16       1.1  christos  * written permission.
     17       1.1  christos  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
     18       1.1  christos  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
     19       1.1  christos  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
     20       1.1  christos  */
     21       1.1  christos 
     22       1.2  christos #include <sys/cdefs.h>
     23       1.1  christos #ifndef lint
     24  1.7.12.1    martin __RCSID("$NetBSD: print-rip.c,v 1.7.12.1 2020/04/08 14:04:14 martin Exp $");
     25       1.1  christos #endif
     26       1.1  christos 
     27       1.7       spz /* \summary: Routing Information Protocol (RIP) printer */
     28       1.7       spz 
     29       1.1  christos #ifdef HAVE_CONFIG_H
     30       1.1  christos #include "config.h"
     31       1.1  christos #endif
     32       1.1  christos 
     33       1.6  christos #include <netdissect-stdinc.h>
     34       1.1  christos 
     35       1.1  christos #include <stdio.h>
     36       1.1  christos 
     37       1.6  christos #include "netdissect.h"
     38       1.1  christos #include "addrtoname.h"
     39       1.6  christos #include "extract.h"
     40       1.1  christos 
     41       1.1  christos #include "af.h"
     42       1.1  christos 
     43       1.5  christos static const char tstr[] = "[|rip]";
     44       1.5  christos 
     45       1.1  christos struct rip {
     46       1.5  christos 	uint8_t rip_cmd;		/* request/response */
     47       1.5  christos 	uint8_t rip_vers;		/* protocol version # */
     48       1.5  christos 	uint8_t unused[2];		/* unused */
     49       1.1  christos };
     50       1.1  christos 
     51       1.1  christos #define	RIPCMD_REQUEST		1	/* want info */
     52       1.1  christos #define	RIPCMD_RESPONSE		2	/* responding to request */
     53       1.1  christos #define	RIPCMD_TRACEON		3	/* turn tracing on */
     54       1.1  christos #define	RIPCMD_TRACEOFF		4	/* turn it off */
     55       1.1  christos #define	RIPCMD_POLL		5	/* want info from everybody */
     56       1.1  christos #define	RIPCMD_POLLENTRY	6	/* poll for entry */
     57       1.1  christos 
     58       1.1  christos static const struct tok rip_cmd_values[] = {
     59       1.1  christos     { RIPCMD_REQUEST,	        "Request" },
     60       1.1  christos     { RIPCMD_RESPONSE,	        "Response" },
     61       1.1  christos     { RIPCMD_TRACEON,	        "Trace on" },
     62       1.1  christos     { RIPCMD_TRACEOFF,	        "Trace off" },
     63       1.1  christos     { RIPCMD_POLL,	        "Poll" },
     64       1.1  christos     { RIPCMD_POLLENTRY,	        "Poll Entry" },
     65       1.1  christos     { 0, NULL}
     66       1.1  christos };
     67       1.1  christos 
     68       1.1  christos #define RIP_AUTHLEN  16
     69       1.1  christos #define RIP_ROUTELEN 20
     70       1.1  christos 
     71       1.1  christos /*
     72       1.1  christos  * rfc 1723
     73       1.5  christos  *
     74       1.1  christos  *  0                   1                   2                   3 3
     75       1.1  christos  *  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
     76       1.1  christos  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
     77       1.1  christos  * | Command (1)   | Version (1)   |           unused              |
     78       1.1  christos  * +---------------+---------------+-------------------------------+
     79       1.1  christos  * | Address Family Identifier (2) |        Route Tag (2)          |
     80       1.1  christos  * +-------------------------------+-------------------------------+
     81       1.1  christos  * |                         IP Address (4)                        |
     82       1.1  christos  * +---------------------------------------------------------------+
     83       1.1  christos  * |                         Subnet Mask (4)                       |
     84       1.1  christos  * +---------------------------------------------------------------+
     85       1.1  christos  * |                         Next Hop (4)                          |
     86       1.1  christos  * +---------------------------------------------------------------+
     87       1.1  christos  * |                         Metric (4)                            |
     88       1.1  christos  * +---------------------------------------------------------------+
     89       1.1  christos  *
     90       1.1  christos  */
     91       1.1  christos 
     92       1.1  christos struct rip_netinfo {
     93       1.5  christos 	uint16_t rip_family;
     94       1.5  christos 	uint16_t rip_tag;
     95       1.5  christos 	uint32_t rip_dest;
     96       1.5  christos 	uint32_t rip_dest_mask;
     97       1.5  christos 	uint32_t rip_router;
     98       1.5  christos 	uint32_t rip_metric;		/* cost of route */
     99       1.1  christos };
    100       1.1  christos 
    101  1.7.12.1    martin UNALIGNED_OK
    102       1.1  christos static void
    103       1.5  christos rip_entry_print_v1(netdissect_options *ndo,
    104       1.5  christos                    register const struct rip_netinfo *ni)
    105       1.1  christos {
    106       1.1  christos 	register u_short family;
    107       1.1  christos 
    108       1.1  christos 	/* RFC 1058 */
    109       1.1  christos 	family = EXTRACT_16BITS(&ni->rip_family);
    110       1.4  christos 	if (family != BSD_AFNUM_INET && family != 0) {
    111       1.5  christos 		ND_PRINT((ndo, "\n\t AFI %s, ", tok2str(bsd_af_values, "Unknown (%u)", family)));
    112       1.6  christos 		print_unknown_data(ndo, (const uint8_t *)&ni->rip_family, "\n\t  ", RIP_ROUTELEN);
    113       1.1  christos 		return;
    114       1.1  christos 	}
    115       1.1  christos 	if (EXTRACT_16BITS(&ni->rip_tag) ||
    116       1.1  christos 	    EXTRACT_32BITS(&ni->rip_dest_mask) ||
    117       1.1  christos 	    EXTRACT_32BITS(&ni->rip_router)) {
    118       1.1  christos 		/* MBZ fields not zero */
    119       1.6  christos                 print_unknown_data(ndo, (const uint8_t *)&ni->rip_family, "\n\t  ", RIP_ROUTELEN);
    120       1.1  christos 		return;
    121       1.4  christos 	}
    122       1.4  christos 	if (family == 0) {
    123       1.5  christos 		ND_PRINT((ndo, "\n\t  AFI 0, %s, metric: %u",
    124       1.5  christos 			ipaddr_string(ndo, &ni->rip_dest),
    125       1.5  christos 			EXTRACT_32BITS(&ni->rip_metric)));
    126       1.4  christos 		return;
    127       1.1  christos 	} /* BSD_AFNUM_INET */
    128       1.5  christos 	ND_PRINT((ndo, "\n\t  %s, metric: %u",
    129       1.5  christos                ipaddr_string(ndo, &ni->rip_dest),
    130       1.5  christos 	       EXTRACT_32BITS(&ni->rip_metric)));
    131       1.1  christos }
    132       1.1  christos 
    133  1.7.12.1    martin UNALIGNED_OK
    134       1.4  christos static unsigned
    135       1.5  christos rip_entry_print_v2(netdissect_options *ndo,
    136       1.5  christos                    register const struct rip_netinfo *ni, const unsigned remaining)
    137       1.1  christos {
    138       1.1  christos 	register u_short family;
    139       1.1  christos 
    140       1.1  christos 	family = EXTRACT_16BITS(&ni->rip_family);
    141       1.4  christos 	if (family == 0xFFFF) { /* variable-sized authentication structures */
    142       1.5  christos 		uint16_t auth_type = EXTRACT_16BITS(&ni->rip_tag);
    143       1.4  christos 		if (auth_type == 2) {
    144       1.6  christos 			register const u_char *p = (const u_char *)&ni->rip_dest;
    145       1.4  christos 			u_int i = 0;
    146       1.5  christos 			ND_PRINT((ndo, "\n\t  Simple Text Authentication data: "));
    147       1.4  christos 			for (; i < RIP_AUTHLEN; p++, i++)
    148       1.5  christos 				ND_PRINT((ndo, "%c", ND_ISPRINT(*p) ? *p : '.'));
    149       1.4  christos 		} else if (auth_type == 3) {
    150       1.5  christos 			ND_PRINT((ndo, "\n\t  Auth header:"));
    151       1.6  christos 			ND_PRINT((ndo, " Packet Len %u,", EXTRACT_16BITS((const uint8_t *)ni + 4)));
    152       1.6  christos 			ND_PRINT((ndo, " Key-ID %u,", *((const uint8_t *)ni + 6)));
    153       1.6  christos 			ND_PRINT((ndo, " Auth Data Len %u,", *((const uint8_t *)ni + 7)));
    154       1.5  christos 			ND_PRINT((ndo, " SeqNo %u,", EXTRACT_32BITS(&ni->rip_dest_mask)));
    155       1.5  christos 			ND_PRINT((ndo, " MBZ %u,", EXTRACT_32BITS(&ni->rip_router)));
    156       1.5  christos 			ND_PRINT((ndo, " MBZ %u", EXTRACT_32BITS(&ni->rip_metric)));
    157       1.4  christos 		} else if (auth_type == 1) {
    158       1.5  christos 			ND_PRINT((ndo, "\n\t  Auth trailer:"));
    159       1.6  christos 			print_unknown_data(ndo, (const uint8_t *)&ni->rip_dest, "\n\t  ", remaining);
    160       1.4  christos 			return remaining; /* AT spans till the packet end */
    161       1.5  christos 		} else {
    162       1.5  christos 			ND_PRINT((ndo, "\n\t  Unknown (%u) Authentication data:",
    163       1.5  christos 			       EXTRACT_16BITS(&ni->rip_tag)));
    164       1.6  christos 			print_unknown_data(ndo, (const uint8_t *)&ni->rip_dest, "\n\t  ", remaining);
    165       1.1  christos 		}
    166       1.4  christos 	} else if (family != BSD_AFNUM_INET && family != 0) {
    167       1.5  christos 		ND_PRINT((ndo, "\n\t  AFI %s", tok2str(bsd_af_values, "Unknown (%u)", family)));
    168       1.6  christos                 print_unknown_data(ndo, (const uint8_t *)&ni->rip_tag, "\n\t  ", RIP_ROUTELEN-2);
    169       1.4  christos 	} else { /* BSD_AFNUM_INET or AFI 0 */
    170       1.5  christos 		ND_PRINT((ndo, "\n\t  AFI %s, %15s/%-2d, tag 0x%04x, metric: %u, next-hop: ",
    171       1.4  christos                        tok2str(bsd_af_values, "%u", family),
    172       1.5  christos                        ipaddr_string(ndo, &ni->rip_dest),
    173       1.1  christos 		       mask2plen(EXTRACT_32BITS(&ni->rip_dest_mask)),
    174       1.1  christos                        EXTRACT_16BITS(&ni->rip_tag),
    175       1.5  christos                        EXTRACT_32BITS(&ni->rip_metric)));
    176       1.1  christos 		if (EXTRACT_32BITS(&ni->rip_router))
    177       1.5  christos 			ND_PRINT((ndo, "%s", ipaddr_string(ndo, &ni->rip_router)));
    178       1.5  christos 		else
    179       1.5  christos 			ND_PRINT((ndo, "self"));
    180       1.1  christos 	}
    181       1.4  christos 	return sizeof (*ni);
    182       1.1  christos }
    183       1.1  christos 
    184       1.1  christos void
    185       1.5  christos rip_print(netdissect_options *ndo,
    186       1.5  christos           const u_char *dat, u_int length)
    187       1.1  christos {
    188       1.1  christos 	register const struct rip *rp;
    189       1.1  christos 	register const struct rip_netinfo *ni;
    190       1.1  christos 	register u_int i, j;
    191       1.1  christos 
    192       1.5  christos 	if (ndo->ndo_snapend < dat) {
    193       1.5  christos 		ND_PRINT((ndo, " %s", tstr));
    194       1.1  christos 		return;
    195       1.1  christos 	}
    196       1.5  christos 	i = ndo->ndo_snapend - dat;
    197       1.1  christos 	if (i > length)
    198       1.1  christos 		i = length;
    199       1.1  christos 	if (i < sizeof(*rp)) {
    200       1.5  christos 		ND_PRINT((ndo, " %s", tstr));
    201       1.1  christos 		return;
    202       1.1  christos 	}
    203       1.1  christos 	i -= sizeof(*rp);
    204       1.1  christos 
    205       1.6  christos 	rp = (const struct rip *)dat;
    206       1.1  christos 
    207       1.5  christos 	ND_PRINT((ndo, "%sRIPv%u",
    208       1.5  christos                (ndo->ndo_vflag >= 1) ? "\n\t" : "",
    209       1.5  christos                rp->rip_vers));
    210       1.1  christos 
    211       1.1  christos 	switch (rp->rip_vers) {
    212       1.1  christos 	case 0:
    213       1.1  christos 		/*
    214       1.1  christos 		 * RFC 1058.
    215       1.1  christos 		 *
    216       1.1  christos 		 * XXX - RFC 1058 says
    217       1.1  christos 		 *
    218       1.1  christos 		 * 0  Datagrams whose version number is zero are to be ignored.
    219       1.1  christos 		 *    These are from a previous version of the protocol, whose
    220       1.1  christos 		 *    packet format was machine-specific.
    221       1.1  christos 		 *
    222       1.1  christos 		 * so perhaps we should just dump the packet, in hex.
    223       1.1  christos 		 */
    224       1.6  christos                 print_unknown_data(ndo, (const uint8_t *)&rp->rip_cmd, "\n\t", length);
    225       1.1  christos 		break;
    226       1.1  christos 	default:
    227       1.1  christos                 /* dump version and lets see if we know the commands name*/
    228       1.5  christos                 ND_PRINT((ndo, ", %s, length: %u",
    229       1.1  christos                        tok2str(rip_cmd_values,
    230       1.1  christos                                "unknown command (%u)",
    231       1.1  christos                                rp->rip_cmd),
    232       1.5  christos                        length));
    233       1.1  christos 
    234       1.5  christos                 if (ndo->ndo_vflag < 1)
    235       1.1  christos                     return;
    236       1.1  christos 
    237       1.1  christos 		switch (rp->rip_cmd) {
    238       1.4  christos 		case RIPCMD_REQUEST:
    239       1.1  christos 		case RIPCMD_RESPONSE:
    240       1.1  christos 			j = length / sizeof(*ni);
    241       1.5  christos 			ND_PRINT((ndo, ", routes: %u%s", j, rp->rip_vers == 2 ? " or less" : ""));
    242       1.6  christos 			ni = (const struct rip_netinfo *)(rp + 1);
    243       1.1  christos 			for (; i >= sizeof(*ni); ++ni) {
    244       1.1  christos 				if (rp->rip_vers == 1)
    245       1.4  christos 				{
    246       1.5  christos 					rip_entry_print_v1(ndo, ni);
    247       1.4  christos 					i -= sizeof(*ni);
    248       1.4  christos 				}
    249       1.1  christos 				else if (rp->rip_vers == 2)
    250       1.5  christos 					i -= rip_entry_print_v2(ndo, ni, i);
    251       1.1  christos                                 else
    252       1.1  christos                                     break;
    253       1.1  christos 			}
    254       1.4  christos 			if (i)
    255       1.5  christos 				ND_PRINT((ndo, "%s", tstr));
    256       1.1  christos 			break;
    257       1.1  christos 
    258       1.1  christos 		case RIPCMD_TRACEOFF:
    259       1.1  christos 		case RIPCMD_POLL:
    260       1.1  christos 		case RIPCMD_POLLENTRY:
    261       1.1  christos 			break;
    262       1.1  christos 
    263       1.1  christos 		case RIPCMD_TRACEON:
    264       1.1  christos                     /* fall through */
    265       1.1  christos 	        default:
    266       1.5  christos                     if (ndo->ndo_vflag <= 1) {
    267       1.6  christos                         if(!print_unknown_data(ndo, (const uint8_t *)rp, "\n\t", length))
    268       1.1  christos                             return;
    269       1.1  christos                     }
    270       1.1  christos                     break;
    271       1.1  christos                 }
    272       1.1  christos                 /* do we want to see an additionally hexdump ? */
    273       1.5  christos                 if (ndo->ndo_vflag> 1) {
    274       1.6  christos                     if(!print_unknown_data(ndo, (const uint8_t *)rp, "\n\t", length))
    275       1.1  christos                         return;
    276       1.1  christos                 }
    277       1.1  christos         }
    278       1.1  christos }
    279