Home | History | Annotate | Line # | Download | only in dist
print-egp.c revision 1.7
      1  1.1  christos /*
      2  1.1  christos  * Copyright (c) 1991, 1992, 1993, 1994, 1995, 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 are permitted
      6  1.1  christos  * provided that the above copyright notice and this paragraph are
      7  1.1  christos  * duplicated in all such forms and that any documentation,
      8  1.1  christos  * advertising materials, and other materials related to such
      9  1.1  christos  * distribution and use acknowledge that the software was developed
     10  1.1  christos  * by the University of California, Lawrence Berkeley Laboratory,
     11  1.1  christos  * Berkeley, CA.  The name of the University may not be used to
     12  1.1  christos  * endorse or promote products derived from this software without
     13  1.1  christos  * specific prior written permission.
     14  1.1  christos  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
     15  1.1  christos  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
     16  1.1  christos  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
     17  1.1  christos  *
     18  1.1  christos  * Initial contribution from Jeff Honig (jch (at) MITCHELL.CIT.CORNELL.EDU).
     19  1.1  christos  */
     20  1.1  christos 
     21  1.2  christos #include <sys/cdefs.h>
     22  1.1  christos #ifndef lint
     23  1.7  christos __RCSID("$NetBSD: print-egp.c,v 1.7 2023/08/17 20:19:40 christos Exp $");
     24  1.1  christos #endif
     25  1.1  christos 
     26  1.6       spz /* \summary: Exterior Gateway Protocol (EGP) printer */
     27  1.6       spz 
     28  1.7  christos /* specification: RFC 827 */
     29  1.7  christos 
     30  1.1  christos #ifdef HAVE_CONFIG_H
     31  1.7  christos #include <config.h>
     32  1.1  christos #endif
     33  1.1  christos 
     34  1.7  christos #include "netdissect-stdinc.h"
     35  1.1  christos 
     36  1.5  christos #include "netdissect.h"
     37  1.1  christos #include "addrtoname.h"
     38  1.1  christos #include "extract.h"
     39  1.1  christos 
     40  1.1  christos struct egp_packet {
     41  1.7  christos 	nd_uint8_t  egp_version;
     42  1.1  christos #define	EGP_VERSION	2
     43  1.7  christos 	nd_uint8_t  egp_type;
     44  1.1  christos #define  EGPT_ACQUIRE	3
     45  1.1  christos #define  EGPT_REACH	5
     46  1.1  christos #define  EGPT_POLL	2
     47  1.1  christos #define  EGPT_UPDATE	1
     48  1.1  christos #define  EGPT_ERROR	8
     49  1.7  christos 	nd_uint8_t  egp_code;
     50  1.1  christos #define  EGPC_REQUEST	0
     51  1.1  christos #define  EGPC_CONFIRM	1
     52  1.1  christos #define  EGPC_REFUSE	2
     53  1.1  christos #define  EGPC_CEASE	3
     54  1.1  christos #define  EGPC_CEASEACK	4
     55  1.1  christos #define  EGPC_HELLO	0
     56  1.1  christos #define  EGPC_HEARDU	1
     57  1.7  christos 	nd_uint8_t  egp_status;
     58  1.1  christos #define  EGPS_UNSPEC	0
     59  1.1  christos #define  EGPS_ACTIVE	1
     60  1.1  christos #define  EGPS_PASSIVE	2
     61  1.1  christos #define  EGPS_NORES	3
     62  1.1  christos #define  EGPS_ADMIN	4
     63  1.1  christos #define  EGPS_GODOWN	5
     64  1.1  christos #define  EGPS_PARAM	6
     65  1.1  christos #define  EGPS_PROTO	7
     66  1.1  christos #define  EGPS_INDET	0
     67  1.1  christos #define  EGPS_UP	1
     68  1.1  christos #define  EGPS_DOWN	2
     69  1.1  christos #define  EGPS_UNSOL	0x80
     70  1.7  christos 	nd_uint16_t  egp_checksum;
     71  1.7  christos 	nd_uint16_t  egp_as;
     72  1.7  christos 	nd_uint16_t  egp_sequence;
     73  1.1  christos 	union {
     74  1.7  christos 		nd_uint16_t egpu_hello;
     75  1.7  christos 		nd_uint8_t  egpu_gws[2];
     76  1.7  christos 		nd_uint16_t egpu_reason;
     77  1.1  christos #define  EGPR_UNSPEC	0
     78  1.1  christos #define  EGPR_BADHEAD	1
     79  1.1  christos #define  EGPR_BADDATA	2
     80  1.1  christos #define  EGPR_NOREACH	3
     81  1.1  christos #define  EGPR_XSPOLL	4
     82  1.1  christos #define  EGPR_NORESP	5
     83  1.1  christos #define  EGPR_UVERSION	6
     84  1.1  christos 	} egp_handg;
     85  1.1  christos #define  egp_hello  egp_handg.egpu_hello
     86  1.1  christos #define  egp_intgw  egp_handg.egpu_gws[0]
     87  1.1  christos #define  egp_extgw  egp_handg.egpu_gws[1]
     88  1.1  christos #define  egp_reason  egp_handg.egpu_reason
     89  1.1  christos 	union {
     90  1.7  christos 		nd_uint16_t egpu_poll;
     91  1.7  christos 		nd_ipv4 egpu_sourcenet;
     92  1.1  christos 	} egp_pands;
     93  1.1  christos #define  egp_poll  egp_pands.egpu_poll
     94  1.1  christos #define  egp_sourcenet  egp_pands.egpu_sourcenet
     95  1.1  christos };
     96  1.1  christos 
     97  1.4  christos static const char *egp_acquire_codes[] = {
     98  1.1  christos 	"request",
     99  1.1  christos 	"confirm",
    100  1.1  christos 	"refuse",
    101  1.1  christos 	"cease",
    102  1.1  christos 	"cease_ack"
    103  1.1  christos };
    104  1.1  christos 
    105  1.4  christos static const char *egp_acquire_status[] = {
    106  1.1  christos 	"unspecified",
    107  1.1  christos 	"active_mode",
    108  1.1  christos 	"passive_mode",
    109  1.1  christos 	"insufficient_resources",
    110  1.1  christos 	"administratively_prohibited",
    111  1.1  christos 	"going_down",
    112  1.1  christos 	"parameter_violation",
    113  1.1  christos 	"protocol_violation"
    114  1.1  christos };
    115  1.1  christos 
    116  1.4  christos static const char *egp_reach_codes[] = {
    117  1.1  christos 	"hello",
    118  1.1  christos 	"i-h-u"
    119  1.1  christos };
    120  1.1  christos 
    121  1.4  christos static const char *egp_status_updown[] = {
    122  1.1  christos 	"indeterminate",
    123  1.1  christos 	"up",
    124  1.1  christos 	"down"
    125  1.1  christos };
    126  1.1  christos 
    127  1.4  christos static const char *egp_reasons[] = {
    128  1.1  christos 	"unspecified",
    129  1.1  christos 	"bad_EGP_header_format",
    130  1.1  christos 	"bad_EGP_data_field_format",
    131  1.1  christos 	"reachability_info_unavailable",
    132  1.1  christos 	"excessive_polling_rate",
    133  1.1  christos 	"no_response",
    134  1.1  christos 	"unsupported_version"
    135  1.1  christos };
    136  1.1  christos 
    137  1.1  christos static void
    138  1.7  christos egpnr_print(netdissect_options *ndo,
    139  1.7  christos            const struct egp_packet *egp, u_int length)
    140  1.1  christos {
    141  1.7  christos 	const uint8_t *cp;
    142  1.4  christos 	uint32_t addr;
    143  1.7  christos 	uint32_t net;
    144  1.7  christos 	u_int netlen;
    145  1.7  christos 	u_int gateways, distances, networks;
    146  1.7  christos 	u_int intgw, extgw, t_gateways;
    147  1.1  christos 	const char *comma;
    148  1.1  christos 
    149  1.7  christos 	addr = GET_IPV4_TO_NETWORK_ORDER(egp->egp_sourcenet);
    150  1.1  christos 	if (IN_CLASSA(addr)) {
    151  1.1  christos 		net = addr & IN_CLASSA_NET;
    152  1.1  christos 		netlen = 1;
    153  1.1  christos 	} else if (IN_CLASSB(addr)) {
    154  1.1  christos 		net = addr & IN_CLASSB_NET;
    155  1.1  christos 		netlen = 2;
    156  1.1  christos 	} else if (IN_CLASSC(addr)) {
    157  1.1  christos 		net = addr & IN_CLASSC_NET;
    158  1.1  christos 		netlen = 3;
    159  1.1  christos 	} else {
    160  1.1  christos 		net = 0;
    161  1.1  christos 		netlen = 0;
    162  1.1  christos 	}
    163  1.5  christos 	cp = (const uint8_t *)(egp + 1);
    164  1.6       spz 	length -= sizeof(*egp);
    165  1.1  christos 
    166  1.7  christos 	intgw = GET_U_1(egp->egp_intgw);
    167  1.7  christos 	extgw = GET_U_1(egp->egp_extgw);
    168  1.7  christos 	t_gateways = intgw + extgw;
    169  1.1  christos 	for (gateways = 0; gateways < t_gateways; ++gateways) {
    170  1.1  christos 		/* Pickup host part of gateway address */
    171  1.1  christos 		addr = 0;
    172  1.6       spz 		if (length < 4 - netlen)
    173  1.6       spz 			goto trunc;
    174  1.7  christos 		ND_TCHECK_LEN(cp, 4 - netlen);
    175  1.1  christos 		switch (netlen) {
    176  1.1  christos 
    177  1.1  christos 		case 1:
    178  1.7  christos 			addr = GET_U_1(cp);
    179  1.7  christos 			cp++;
    180  1.1  christos 			/* fall through */
    181  1.1  christos 		case 2:
    182  1.7  christos 			addr = (addr << 8) | GET_U_1(cp);
    183  1.7  christos 			cp++;
    184  1.1  christos 			/* fall through */
    185  1.1  christos 		case 3:
    186  1.7  christos 			addr = (addr << 8) | GET_U_1(cp);
    187  1.7  christos 			cp++;
    188  1.7  christos 			break;
    189  1.1  christos 		}
    190  1.1  christos 		addr |= net;
    191  1.6       spz 		length -= 4 - netlen;
    192  1.6       spz 		if (length < 1)
    193  1.6       spz 			goto trunc;
    194  1.7  christos 		distances = GET_U_1(cp);
    195  1.7  christos 		cp++;
    196  1.6       spz 		length--;
    197  1.7  christos 		ND_PRINT(" %s %s ",
    198  1.7  christos 		       gateways < intgw ? "int" : "ext",
    199  1.7  christos 		       ipaddr_string(ndo, (const u_char *)&addr)); /* local buffer, not packet data; don't use GET_IPADDR_STRING() */
    200  1.1  christos 
    201  1.1  christos 		comma = "";
    202  1.7  christos 		ND_PRINT("(");
    203  1.7  christos 		while (distances != 0) {
    204  1.6       spz 			if (length < 2)
    205  1.6       spz 				goto trunc;
    206  1.7  christos 			ND_PRINT("%sd%u:", comma, GET_U_1(cp));
    207  1.7  christos 			cp++;
    208  1.1  christos 			comma = ", ";
    209  1.7  christos 			networks = GET_U_1(cp);
    210  1.7  christos 			cp++;
    211  1.6       spz 			length -= 2;
    212  1.7  christos 			while (networks != 0) {
    213  1.1  christos 				/* Pickup network number */
    214  1.6       spz 				if (length < 1)
    215  1.6       spz 					goto trunc;
    216  1.7  christos 				addr = ((uint32_t) GET_U_1(cp)) << 24;
    217  1.7  christos 				cp++;
    218  1.6       spz 				length--;
    219  1.1  christos 				if (IN_CLASSB(addr)) {
    220  1.6       spz 					if (length < 1)
    221  1.6       spz 						goto trunc;
    222  1.7  christos 					addr |= ((uint32_t) GET_U_1(cp)) << 16;
    223  1.7  christos 					cp++;
    224  1.6       spz 					length--;
    225  1.1  christos 				} else if (!IN_CLASSA(addr)) {
    226  1.6       spz 					if (length < 2)
    227  1.6       spz 						goto trunc;
    228  1.7  christos 					addr |= ((uint32_t) GET_U_1(cp)) << 16;
    229  1.7  christos 					cp++;
    230  1.7  christos 					addr |= ((uint32_t) GET_U_1(cp)) << 8;
    231  1.7  christos 					cp++;
    232  1.6       spz 					length -= 2;
    233  1.1  christos 				}
    234  1.7  christos 				ND_PRINT(" %s", ipaddr_string(ndo, (const u_char *)&addr)); /* local buffer, not packet data; don't use GET_IPADDR_STRING() */
    235  1.7  christos 				networks--;
    236  1.1  christos 			}
    237  1.7  christos 			distances--;
    238  1.1  christos 		}
    239  1.7  christos 		ND_PRINT(")");
    240  1.1  christos 	}
    241  1.1  christos 	return;
    242  1.1  christos trunc:
    243  1.7  christos 	nd_print_trunc(ndo);
    244  1.1  christos }
    245  1.1  christos 
    246  1.1  christos void
    247  1.4  christos egp_print(netdissect_options *ndo,
    248  1.7  christos           const uint8_t *bp, u_int length)
    249  1.1  christos {
    250  1.7  christos 	const struct egp_packet *egp;
    251  1.7  christos 	u_int version;
    252  1.7  christos 	u_int type;
    253  1.7  christos 	u_int code;
    254  1.7  christos 	u_int status;
    255  1.1  christos 
    256  1.7  christos 	ndo->ndo_protocol = "egp";
    257  1.5  christos 	egp = (const struct egp_packet *)bp;
    258  1.7  christos 	if (length < sizeof(*egp) || !ND_TTEST_SIZE(egp)) {
    259  1.7  christos 		nd_print_trunc(ndo);
    260  1.1  christos 		return;
    261  1.1  christos 	}
    262  1.1  christos 
    263  1.7  christos 	version = GET_U_1(egp->egp_version);
    264  1.4  christos         if (!ndo->ndo_vflag) {
    265  1.7  christos             ND_PRINT("EGPv%u, AS %u, seq %u, length %u",
    266  1.7  christos                    version,
    267  1.7  christos                    GET_BE_U_2(egp->egp_as),
    268  1.7  christos                    GET_BE_U_2(egp->egp_sequence),
    269  1.7  christos                    length);
    270  1.1  christos             return;
    271  1.1  christos         } else
    272  1.7  christos             ND_PRINT("EGPv%u, length %u",
    273  1.7  christos                    version,
    274  1.7  christos                    length);
    275  1.1  christos 
    276  1.7  christos 	if (version != EGP_VERSION) {
    277  1.7  christos 		ND_PRINT("[version %u]", version);
    278  1.1  christos 		return;
    279  1.1  christos 	}
    280  1.1  christos 
    281  1.7  christos 	type = GET_U_1(egp->egp_type);
    282  1.7  christos 	code = GET_U_1(egp->egp_code);
    283  1.7  christos 	status = GET_U_1(egp->egp_status);
    284  1.1  christos 
    285  1.1  christos 	switch (type) {
    286  1.1  christos 	case EGPT_ACQUIRE:
    287  1.7  christos 		ND_PRINT(" acquire");
    288  1.1  christos 		switch (code) {
    289  1.1  christos 		case EGPC_REQUEST:
    290  1.1  christos 		case EGPC_CONFIRM:
    291  1.7  christos 			ND_PRINT(" %s", egp_acquire_codes[code]);
    292  1.1  christos 			switch (status) {
    293  1.1  christos 			case EGPS_UNSPEC:
    294  1.1  christos 			case EGPS_ACTIVE:
    295  1.1  christos 			case EGPS_PASSIVE:
    296  1.7  christos 				ND_PRINT(" %s", egp_acquire_status[status]);
    297  1.1  christos 				break;
    298  1.1  christos 
    299  1.1  christos 			default:
    300  1.7  christos 				ND_PRINT(" [status %u]", status);
    301  1.1  christos 				break;
    302  1.1  christos 			}
    303  1.7  christos 			ND_PRINT(" hello:%u poll:%u",
    304  1.7  christos 			       GET_BE_U_2(egp->egp_hello),
    305  1.7  christos 			       GET_BE_U_2(egp->egp_poll));
    306  1.1  christos 			break;
    307  1.1  christos 
    308  1.1  christos 		case EGPC_REFUSE:
    309  1.1  christos 		case EGPC_CEASE:
    310  1.1  christos 		case EGPC_CEASEACK:
    311  1.7  christos 			ND_PRINT(" %s", egp_acquire_codes[code]);
    312  1.1  christos 			switch (status ) {
    313  1.1  christos 			case EGPS_UNSPEC:
    314  1.1  christos 			case EGPS_NORES:
    315  1.1  christos 			case EGPS_ADMIN:
    316  1.1  christos 			case EGPS_GODOWN:
    317  1.1  christos 			case EGPS_PARAM:
    318  1.1  christos 			case EGPS_PROTO:
    319  1.7  christos 				ND_PRINT(" %s", egp_acquire_status[status]);
    320  1.1  christos 				break;
    321  1.1  christos 
    322  1.1  christos 			default:
    323  1.7  christos 				ND_PRINT("[status %u]", status);
    324  1.1  christos 				break;
    325  1.1  christos 			}
    326  1.1  christos 			break;
    327  1.1  christos 
    328  1.1  christos 		default:
    329  1.7  christos 			ND_PRINT("[code %u]", code);
    330  1.1  christos 			break;
    331  1.1  christos 		}
    332  1.1  christos 		break;
    333  1.1  christos 
    334  1.1  christos 	case EGPT_REACH:
    335  1.1  christos 		switch (code) {
    336  1.1  christos 
    337  1.1  christos 		case EGPC_HELLO:
    338  1.1  christos 		case EGPC_HEARDU:
    339  1.7  christos 			ND_PRINT(" %s", egp_reach_codes[code]);
    340  1.1  christos 			if (status <= EGPS_DOWN)
    341  1.7  christos 				ND_PRINT(" state:%s", egp_status_updown[status]);
    342  1.1  christos 			else
    343  1.7  christos 				ND_PRINT(" [status %u]", status);
    344  1.1  christos 			break;
    345  1.1  christos 
    346  1.1  christos 		default:
    347  1.7  christos 			ND_PRINT("[reach code %u]", code);
    348  1.1  christos 			break;
    349  1.1  christos 		}
    350  1.1  christos 		break;
    351  1.1  christos 
    352  1.1  christos 	case EGPT_POLL:
    353  1.7  christos 		ND_PRINT(" poll");
    354  1.7  christos 		if (status <= EGPS_DOWN)
    355  1.7  christos 			ND_PRINT(" state:%s", egp_status_updown[status]);
    356  1.1  christos 		else
    357  1.7  christos 			ND_PRINT(" [status %u]", status);
    358  1.7  christos 		ND_PRINT(" net:%s", GET_IPADDR_STRING(egp->egp_sourcenet));
    359  1.1  christos 		break;
    360  1.1  christos 
    361  1.1  christos 	case EGPT_UPDATE:
    362  1.7  christos 		ND_PRINT(" update");
    363  1.1  christos 		if (status & EGPS_UNSOL) {
    364  1.1  christos 			status &= ~EGPS_UNSOL;
    365  1.7  christos 			ND_PRINT(" unsolicited");
    366  1.1  christos 		}
    367  1.1  christos 		if (status <= EGPS_DOWN)
    368  1.7  christos 			ND_PRINT(" state:%s", egp_status_updown[status]);
    369  1.1  christos 		else
    370  1.7  christos 			ND_PRINT(" [status %u]", status);
    371  1.7  christos 		ND_PRINT(" %s int %u ext %u",
    372  1.7  christos 		       GET_IPADDR_STRING(egp->egp_sourcenet),
    373  1.7  christos 		       GET_U_1(egp->egp_intgw),
    374  1.7  christos 		       GET_U_1(egp->egp_extgw));
    375  1.4  christos 		if (ndo->ndo_vflag)
    376  1.7  christos 			egpnr_print(ndo, egp, length);
    377  1.1  christos 		break;
    378  1.1  christos 
    379  1.1  christos 	case EGPT_ERROR:
    380  1.7  christos 		ND_PRINT(" error");
    381  1.1  christos 		if (status <= EGPS_DOWN)
    382  1.7  christos 			ND_PRINT(" state:%s", egp_status_updown[status]);
    383  1.1  christos 		else
    384  1.7  christos 			ND_PRINT(" [status %u]", status);
    385  1.1  christos 
    386  1.7  christos 		if (GET_BE_U_2(egp->egp_reason) <= EGPR_UVERSION)
    387  1.7  christos 			ND_PRINT(" %s",
    388  1.7  christos 				 egp_reasons[GET_BE_U_2(egp->egp_reason)]);
    389  1.1  christos 		else
    390  1.7  christos 			ND_PRINT(" [reason %u]", GET_BE_U_2(egp->egp_reason));
    391  1.1  christos 		break;
    392  1.1  christos 
    393  1.1  christos 	default:
    394  1.7  christos 		ND_PRINT("[type %u]", type);
    395  1.1  christos 		break;
    396  1.1  christos 	}
    397  1.1  christos }
    398