Home | History | Annotate | Line # | Download | only in dist
print-bootp.c revision 1.1
      1  1.1  christos /*
      2  1.1  christos  * Copyright (c) 1990, 1991, 1993, 1994, 1995, 1996, 1997
      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  * Format and print bootp packets.
     22  1.1  christos  */
     23  1.1  christos #ifndef lint
     24  1.1  christos static const char rcsid[] _U_ =
     25  1.1  christos     "@(#) Header: /tcpdump/master/tcpdump/print-bootp.c,v 1.89 2008-04-22 09:45:08 hannes Exp (LBL)";
     26  1.1  christos #endif
     27  1.1  christos 
     28  1.1  christos #ifdef HAVE_CONFIG_H
     29  1.1  christos #include "config.h"
     30  1.1  christos #endif
     31  1.1  christos 
     32  1.1  christos #include <tcpdump-stdinc.h>
     33  1.1  christos 
     34  1.1  christos #include <stdio.h>
     35  1.1  christos #include <string.h>
     36  1.1  christos 
     37  1.1  christos #include "interface.h"
     38  1.1  christos #include "addrtoname.h"
     39  1.1  christos #include "extract.h"
     40  1.1  christos #include "ether.h"
     41  1.1  christos #include "bootp.h"
     42  1.1  christos 
     43  1.1  christos static void rfc1048_print(const u_char *);
     44  1.1  christos static void cmu_print(const u_char *);
     45  1.1  christos static char *client_fqdn_flags(u_int flags);
     46  1.1  christos 
     47  1.1  christos static char tstr[] = " [|bootp]";
     48  1.1  christos 
     49  1.1  christos static const struct tok bootp_flag_values[] = {
     50  1.1  christos     { 0x8000,                   "Broadcast" },
     51  1.1  christos     { 0, NULL}
     52  1.1  christos };
     53  1.1  christos 
     54  1.1  christos static const struct tok bootp_op_values[] = {
     55  1.1  christos     { BOOTPREQUEST,             "Request" },
     56  1.1  christos     { BOOTPREPLY,               "Reply" },
     57  1.1  christos     { 0, NULL}
     58  1.1  christos };
     59  1.1  christos 
     60  1.1  christos /*
     61  1.1  christos  * Print bootp requests
     62  1.1  christos  */
     63  1.1  christos void
     64  1.1  christos bootp_print(register const u_char *cp, u_int length)
     65  1.1  christos {
     66  1.1  christos 	register const struct bootp *bp;
     67  1.1  christos 	static const u_char vm_cmu[4] = VM_CMU;
     68  1.1  christos 	static const u_char vm_rfc1048[4] = VM_RFC1048;
     69  1.1  christos 
     70  1.1  christos 	bp = (const struct bootp *)cp;
     71  1.1  christos 	TCHECK(bp->bp_op);
     72  1.1  christos 
     73  1.1  christos         printf("BOOTP/DHCP, %s",
     74  1.1  christos 	       tok2str(bootp_op_values, "unknown (0x%02x)", bp->bp_op));
     75  1.1  christos 
     76  1.1  christos 	if (bp->bp_htype == 1 && bp->bp_hlen == 6 && bp->bp_op == BOOTPREQUEST) {
     77  1.1  christos 		TCHECK2(bp->bp_chaddr[0], 6);
     78  1.1  christos 		printf(" from %s", etheraddr_string(bp->bp_chaddr));
     79  1.1  christos 	}
     80  1.1  christos 
     81  1.1  christos         printf(", length %u", length);
     82  1.1  christos 
     83  1.1  christos         if (!vflag)
     84  1.1  christos             return;
     85  1.1  christos 
     86  1.1  christos 	TCHECK(bp->bp_secs);
     87  1.1  christos 
     88  1.1  christos 	/* The usual hardware address type is 1 (10Mb Ethernet) */
     89  1.1  christos 	if (bp->bp_htype != 1)
     90  1.1  christos 		printf(", htype %d", bp->bp_htype);
     91  1.1  christos 
     92  1.1  christos 	/* The usual length for 10Mb Ethernet address is 6 bytes */
     93  1.1  christos 	if (bp->bp_htype != 1 || bp->bp_hlen != 6)
     94  1.1  christos 		printf(", hlen %d", bp->bp_hlen);
     95  1.1  christos 
     96  1.1  christos 	/* Only print interesting fields */
     97  1.1  christos 	if (bp->bp_hops)
     98  1.1  christos 		printf(", hops %d", bp->bp_hops);
     99  1.1  christos 	if (bp->bp_xid)
    100  1.1  christos 		printf(", xid 0x%x", EXTRACT_32BITS(&bp->bp_xid));
    101  1.1  christos 	if (bp->bp_secs)
    102  1.1  christos 		printf(", secs %d", EXTRACT_16BITS(&bp->bp_secs));
    103  1.1  christos 
    104  1.1  christos 	printf(", Flags [%s]",
    105  1.1  christos 		bittok2str(bootp_flag_values, "none", EXTRACT_16BITS(&bp->bp_flags)));
    106  1.1  christos 	if (vflag > 1)
    107  1.1  christos 		printf(" (0x%04x)", EXTRACT_16BITS(&bp->bp_flags));
    108  1.1  christos 
    109  1.1  christos 	/* Client's ip address */
    110  1.1  christos 	TCHECK(bp->bp_ciaddr);
    111  1.1  christos 	if (bp->bp_ciaddr.s_addr)
    112  1.1  christos 		printf("\n\t  Client-IP %s", ipaddr_string(&bp->bp_ciaddr));
    113  1.1  christos 
    114  1.1  christos 	/* 'your' ip address (bootp client) */
    115  1.1  christos 	TCHECK(bp->bp_yiaddr);
    116  1.1  christos 	if (bp->bp_yiaddr.s_addr)
    117  1.1  christos 		printf("\n\t  Your-IP %s", ipaddr_string(&bp->bp_yiaddr));
    118  1.1  christos 
    119  1.1  christos 	/* Server's ip address */
    120  1.1  christos 	TCHECK(bp->bp_siaddr);
    121  1.1  christos 	if (bp->bp_siaddr.s_addr)
    122  1.1  christos 		printf("\n\t  Server-IP %s", ipaddr_string(&bp->bp_siaddr));
    123  1.1  christos 
    124  1.1  christos 	/* Gateway's ip address */
    125  1.1  christos 	TCHECK(bp->bp_giaddr);
    126  1.1  christos 	if (bp->bp_giaddr.s_addr)
    127  1.1  christos 		printf("\n\t  Gateway-IP %s", ipaddr_string(&bp->bp_giaddr));
    128  1.1  christos 
    129  1.1  christos 	/* Client's Ethernet address */
    130  1.1  christos 	if (bp->bp_htype == 1 && bp->bp_hlen == 6) {
    131  1.1  christos 		TCHECK2(bp->bp_chaddr[0], 6);
    132  1.1  christos 		printf("\n\t  Client-Ethernet-Address %s", etheraddr_string(bp->bp_chaddr));
    133  1.1  christos 	}
    134  1.1  christos 
    135  1.1  christos 	TCHECK2(bp->bp_sname[0], 1);		/* check first char only */
    136  1.1  christos 	if (*bp->bp_sname) {
    137  1.1  christos 		printf("\n\t  sname \"");
    138  1.1  christos 		if (fn_print(bp->bp_sname, snapend)) {
    139  1.1  christos 			putchar('"');
    140  1.1  christos 			fputs(tstr + 1, stdout);
    141  1.1  christos 			return;
    142  1.1  christos 		}
    143  1.1  christos 		putchar('"');
    144  1.1  christos 	}
    145  1.1  christos 	TCHECK2(bp->bp_file[0], 1);		/* check first char only */
    146  1.1  christos 	if (*bp->bp_file) {
    147  1.1  christos 		printf("\n\t  file \"");
    148  1.1  christos 		if (fn_print(bp->bp_file, snapend)) {
    149  1.1  christos 			putchar('"');
    150  1.1  christos 			fputs(tstr + 1, stdout);
    151  1.1  christos 			return;
    152  1.1  christos 		}
    153  1.1  christos 		putchar('"');
    154  1.1  christos 	}
    155  1.1  christos 
    156  1.1  christos 	/* Decode the vendor buffer */
    157  1.1  christos 	TCHECK(bp->bp_vend[0]);
    158  1.1  christos 	if (memcmp((const char *)bp->bp_vend, vm_rfc1048,
    159  1.1  christos 		 sizeof(u_int32_t)) == 0)
    160  1.1  christos 		rfc1048_print(bp->bp_vend);
    161  1.1  christos 	else if (memcmp((const char *)bp->bp_vend, vm_cmu,
    162  1.1  christos 		      sizeof(u_int32_t)) == 0)
    163  1.1  christos 		cmu_print(bp->bp_vend);
    164  1.1  christos 	else {
    165  1.1  christos 		u_int32_t ul;
    166  1.1  christos 
    167  1.1  christos 		ul = EXTRACT_32BITS(&bp->bp_vend);
    168  1.1  christos 		if (ul != 0)
    169  1.1  christos 			printf("\n\t  Vendor-#0x%x", ul);
    170  1.1  christos 	}
    171  1.1  christos 
    172  1.1  christos 	return;
    173  1.1  christos trunc:
    174  1.1  christos 	fputs(tstr, stdout);
    175  1.1  christos }
    176  1.1  christos 
    177  1.1  christos /*
    178  1.1  christos  * The first character specifies the format to print:
    179  1.1  christos  *     i - ip address (32 bits)
    180  1.1  christos  *     p - ip address pairs (32 bits + 32 bits)
    181  1.1  christos  *     l - long (32 bits)
    182  1.1  christos  *     L - unsigned long (32 bits)
    183  1.1  christos  *     s - short (16 bits)
    184  1.1  christos  *     b - period-seperated decimal bytes (variable length)
    185  1.1  christos  *     x - colon-seperated hex bytes (variable length)
    186  1.1  christos  *     a - ascii string (variable length)
    187  1.1  christos  *     B - on/off (8 bits)
    188  1.1  christos  *     $ - special (explicit code to handle)
    189  1.1  christos  */
    190  1.1  christos static struct tok tag2str[] = {
    191  1.1  christos /* RFC1048 tags */
    192  1.1  christos 	{ TAG_PAD,		" PAD" },
    193  1.1  christos 	{ TAG_SUBNET_MASK,	"iSubnet-Mask" },	/* subnet mask (RFC950) */
    194  1.1  christos 	{ TAG_TIME_OFFSET,	"LTime-Zone" },	/* seconds from UTC */
    195  1.1  christos 	{ TAG_GATEWAY,		"iDefault-Gateway" },	/* default gateway */
    196  1.1  christos 	{ TAG_TIME_SERVER,	"iTime-Server" },	/* time servers (RFC868) */
    197  1.1  christos 	{ TAG_NAME_SERVER,	"iIEN-Name-Server" },	/* IEN name servers (IEN116) */
    198  1.1  christos 	{ TAG_DOMAIN_SERVER,	"iDomain-Name-Server" },	/* domain name (RFC1035) */
    199  1.1  christos 	{ TAG_LOG_SERVER,	"iLOG" },	/* MIT log servers */
    200  1.1  christos 	{ TAG_COOKIE_SERVER,	"iCS" },	/* cookie servers (RFC865) */
    201  1.1  christos 	{ TAG_LPR_SERVER,	"iLPR-Server" },	/* lpr server (RFC1179) */
    202  1.1  christos 	{ TAG_IMPRESS_SERVER,	"iIM" },	/* impress servers (Imagen) */
    203  1.1  christos 	{ TAG_RLP_SERVER,	"iRL" },	/* resource location (RFC887) */
    204  1.1  christos 	{ TAG_HOSTNAME,		"aHostname" },	/* ascii hostname */
    205  1.1  christos 	{ TAG_BOOTSIZE,		"sBS" },	/* 512 byte blocks */
    206  1.1  christos 	{ TAG_END,		" END" },
    207  1.1  christos /* RFC1497 tags */
    208  1.1  christos 	{ TAG_DUMPPATH,		"aDP" },
    209  1.1  christos 	{ TAG_DOMAINNAME,	"aDomain-Name" },
    210  1.1  christos 	{ TAG_SWAP_SERVER,	"iSS" },
    211  1.1  christos 	{ TAG_ROOTPATH,		"aRP" },
    212  1.1  christos 	{ TAG_EXTPATH,		"aEP" },
    213  1.1  christos /* RFC2132 tags */
    214  1.1  christos 	{ TAG_IP_FORWARD,	"BIPF" },
    215  1.1  christos 	{ TAG_NL_SRCRT,		"BSRT" },
    216  1.1  christos 	{ TAG_PFILTERS,		"pPF" },
    217  1.1  christos 	{ TAG_REASS_SIZE,	"sRSZ" },
    218  1.1  christos 	{ TAG_DEF_TTL,		"bTTL" },
    219  1.1  christos 	{ TAG_MTU_TIMEOUT,	"lMTU-Timeout" },
    220  1.1  christos 	{ TAG_MTU_TABLE,	"sMTU-Table" },
    221  1.1  christos 	{ TAG_INT_MTU,		"sMTU" },
    222  1.1  christos 	{ TAG_LOCAL_SUBNETS,	"BLSN" },
    223  1.1  christos 	{ TAG_BROAD_ADDR,	"iBR" },
    224  1.1  christos 	{ TAG_DO_MASK_DISC,	"BMD" },
    225  1.1  christos 	{ TAG_SUPPLY_MASK,	"BMS" },
    226  1.1  christos 	{ TAG_DO_RDISC,		"BRouter-Discovery" },
    227  1.1  christos 	{ TAG_RTR_SOL_ADDR,	"iRSA" },
    228  1.1  christos 	{ TAG_STATIC_ROUTE,	"pStatic-Route" },
    229  1.1  christos 	{ TAG_USE_TRAILERS,	"BUT" },
    230  1.1  christos 	{ TAG_ARP_TIMEOUT,	"lAT" },
    231  1.1  christos 	{ TAG_ETH_ENCAP,	"BIE" },
    232  1.1  christos 	{ TAG_TCP_TTL,		"bTT" },
    233  1.1  christos 	{ TAG_TCP_KEEPALIVE,	"lKI" },
    234  1.1  christos 	{ TAG_KEEPALIVE_GO,	"BKG" },
    235  1.1  christos 	{ TAG_NIS_DOMAIN,	"aYD" },
    236  1.1  christos 	{ TAG_NIS_SERVERS,	"iYS" },
    237  1.1  christos 	{ TAG_NTP_SERVERS,	"iNTP" },
    238  1.1  christos 	{ TAG_VENDOR_OPTS,	"bVendor-Option" },
    239  1.1  christos 	{ TAG_NETBIOS_NS,	"iNetbios-Name-Server" },
    240  1.1  christos 	{ TAG_NETBIOS_DDS,	"iWDD" },
    241  1.1  christos 	{ TAG_NETBIOS_NODE,	"$Netbios-Node" },
    242  1.1  christos 	{ TAG_NETBIOS_SCOPE,	"aNetbios-Scope" },
    243  1.1  christos 	{ TAG_XWIN_FS,		"iXFS" },
    244  1.1  christos 	{ TAG_XWIN_DM,		"iXDM" },
    245  1.1  christos 	{ TAG_NIS_P_DOMAIN,	"sN+D" },
    246  1.1  christos 	{ TAG_NIS_P_SERVERS,	"iN+S" },
    247  1.1  christos 	{ TAG_MOBILE_HOME,	"iMH" },
    248  1.1  christos 	{ TAG_SMPT_SERVER,	"iSMTP" },
    249  1.1  christos 	{ TAG_POP3_SERVER,	"iPOP3" },
    250  1.1  christos 	{ TAG_NNTP_SERVER,	"iNNTP" },
    251  1.1  christos 	{ TAG_WWW_SERVER,	"iWWW" },
    252  1.1  christos 	{ TAG_FINGER_SERVER,	"iFG" },
    253  1.1  christos 	{ TAG_IRC_SERVER,	"iIRC" },
    254  1.1  christos 	{ TAG_STREETTALK_SRVR,	"iSTS" },
    255  1.1  christos 	{ TAG_STREETTALK_STDA,	"iSTDA" },
    256  1.1  christos 	{ TAG_REQUESTED_IP,	"iRequested-IP" },
    257  1.1  christos 	{ TAG_IP_LEASE,		"lLease-Time" },
    258  1.1  christos 	{ TAG_OPT_OVERLOAD,	"$OO" },
    259  1.1  christos 	{ TAG_TFTP_SERVER,	"aTFTP" },
    260  1.1  christos 	{ TAG_BOOTFILENAME,	"aBF" },
    261  1.1  christos 	{ TAG_DHCP_MESSAGE,	" DHCP-Message" },
    262  1.1  christos 	{ TAG_SERVER_ID,	"iServer-ID" },
    263  1.1  christos 	{ TAG_PARM_REQUEST,	"bParameter-Request" },
    264  1.1  christos 	{ TAG_MESSAGE,		"aMSG" },
    265  1.1  christos 	{ TAG_MAX_MSG_SIZE,	"sMSZ" },
    266  1.1  christos 	{ TAG_RENEWAL_TIME,	"lRN" },
    267  1.1  christos 	{ TAG_REBIND_TIME,	"lRB" },
    268  1.1  christos 	{ TAG_VENDOR_CLASS,	"aVendor-Class" },
    269  1.1  christos 	{ TAG_CLIENT_ID,	"$Client-ID" },
    270  1.1  christos /* RFC 2485 */
    271  1.1  christos 	{ TAG_OPEN_GROUP_UAP,	"aUAP" },
    272  1.1  christos /* RFC 2563 */
    273  1.1  christos 	{ TAG_DISABLE_AUTOCONF,	"BNOAUTO" },
    274  1.1  christos /* RFC 2610 */
    275  1.1  christos 	{ TAG_SLP_DA,		"bSLP-DA" },	/*"b" is a little wrong */
    276  1.1  christos 	{ TAG_SLP_SCOPE,	"bSLP-SCOPE" },	/*"b" is a little wrong */
    277  1.1  christos /* RFC 2937 */
    278  1.1  christos 	{ TAG_NS_SEARCH,	"sNSSEARCH" },	/* XXX 's' */
    279  1.1  christos /* RFC 3011 */
    280  1.1  christos 	{ TAG_IP4_SUBNET_SELECT, "iSUBNET" },
    281  1.1  christos /* RFC 3442 */
    282  1.1  christos 	{ TAG_CLASSLESS_STATIC_RT, "$Classless-Static-Route" },
    283  1.1  christos 	{ TAG_CLASSLESS_STA_RT_MS, "$Classless-Static-Route-Microsoft" },
    284  1.1  christos /* http://www.iana.org/assignments/bootp-dhcp-extensions/index.htm */
    285  1.1  christos 	{ TAG_USER_CLASS,	"aCLASS" },
    286  1.1  christos 	{ TAG_SLP_NAMING_AUTH,	"aSLP-NA" },
    287  1.1  christos 	{ TAG_CLIENT_FQDN,	"$FQDN" },
    288  1.1  christos 	{ TAG_AGENT_CIRCUIT,	"$Agent-Information" },
    289  1.1  christos 	{ TAG_AGENT_REMOTE,	"bARMT" },
    290  1.1  christos 	{ TAG_AGENT_MASK,	"bAMSK" },
    291  1.1  christos 	{ TAG_TZ_STRING,	"aTZSTR" },
    292  1.1  christos 	{ TAG_FQDN_OPTION,	"bFQDNS" },	/* XXX 'b' */
    293  1.1  christos 	{ TAG_AUTH,		"bAUTH" },	/* XXX 'b' */
    294  1.1  christos 	{ TAG_VINES_SERVERS,	"iVINES" },
    295  1.1  christos 	{ TAG_SERVER_RANK,	"sRANK" },
    296  1.1  christos 	{ TAG_CLIENT_ARCH,	"sARCH" },
    297  1.1  christos 	{ TAG_CLIENT_NDI,	"bNDI" },	/* XXX 'b' */
    298  1.1  christos 	{ TAG_CLIENT_GUID,	"bGUID" },	/* XXX 'b' */
    299  1.1  christos 	{ TAG_LDAP_URL,		"aLDAP" },
    300  1.1  christos 	{ TAG_6OVER4,		"i6o4" },
    301  1.1  christos 	{ TAG_PRINTER_NAME,	"aPRTR" },
    302  1.1  christos 	{ TAG_MDHCP_SERVER,	"bMDHCP" },	/* XXX 'b' */
    303  1.1  christos 	{ TAG_IPX_COMPAT,	"bIPX" },	/* XXX 'b' */
    304  1.1  christos 	{ TAG_NETINFO_PARENT,	"iNI" },
    305  1.1  christos 	{ TAG_NETINFO_PARENT_TAG, "aNITAG" },
    306  1.1  christos 	{ TAG_URL,		"aURL" },
    307  1.1  christos 	{ TAG_FAILOVER,		"bFAIL" },	/* XXX 'b' */
    308  1.1  christos 	{ 0,			NULL }
    309  1.1  christos };
    310  1.1  christos /* 2-byte extended tags */
    311  1.1  christos static struct tok xtag2str[] = {
    312  1.1  christos 	{ 0,			NULL }
    313  1.1  christos };
    314  1.1  christos 
    315  1.1  christos /* DHCP "options overload" types */
    316  1.1  christos static struct tok oo2str[] = {
    317  1.1  christos 	{ 1,			"file" },
    318  1.1  christos 	{ 2,			"sname" },
    319  1.1  christos 	{ 3,			"file+sname" },
    320  1.1  christos 	{ 0,			NULL }
    321  1.1  christos };
    322  1.1  christos 
    323  1.1  christos /* NETBIOS over TCP/IP node type options */
    324  1.1  christos static struct tok nbo2str[] = {
    325  1.1  christos 	{ 0x1,			"b-node" },
    326  1.1  christos 	{ 0x2,			"p-node" },
    327  1.1  christos 	{ 0x4,			"m-node" },
    328  1.1  christos 	{ 0x8,			"h-node" },
    329  1.1  christos 	{ 0,			NULL }
    330  1.1  christos };
    331  1.1  christos 
    332  1.1  christos /* ARP Hardware types, for Client-ID option */
    333  1.1  christos static struct tok arp2str[] = {
    334  1.1  christos 	{ 0x1,			"ether" },
    335  1.1  christos 	{ 0x6,			"ieee802" },
    336  1.1  christos 	{ 0x7,			"arcnet" },
    337  1.1  christos 	{ 0xf,			"frelay" },
    338  1.1  christos 	{ 0x17,			"strip" },
    339  1.1  christos 	{ 0x18,			"ieee1394" },
    340  1.1  christos 	{ 0,			NULL }
    341  1.1  christos };
    342  1.1  christos 
    343  1.1  christos static struct tok dhcp_msg_values[] = {
    344  1.1  christos         { DHCPDISCOVER, "Discover" },
    345  1.1  christos         { DHCPOFFER, "Offer" },
    346  1.1  christos         { DHCPREQUEST, "Request" },
    347  1.1  christos         { DHCPDECLINE, "Decline" },
    348  1.1  christos         { DHCPACK, "ACK" },
    349  1.1  christos         { DHCPNAK, "NACK" },
    350  1.1  christos         { DHCPRELEASE, "Release" },
    351  1.1  christos         { DHCPINFORM, "Inform" },
    352  1.1  christos         { 0,			NULL }
    353  1.1  christos };
    354  1.1  christos 
    355  1.1  christos #define AGENT_SUBOPTION_CIRCUIT_ID 	1	/* RFC 3046 */
    356  1.1  christos #define AGENT_SUBOPTION_REMOTE_ID  	2	/* RFC 3046 */
    357  1.1  christos #define AGENT_SUBOPTION_SUBSCRIBER_ID 	6	/* RFC 3993 */
    358  1.1  christos static struct tok agent_suboption_values[] = {
    359  1.1  christos         { AGENT_SUBOPTION_CIRCUIT_ID, "Circuit-ID" },
    360  1.1  christos         { AGENT_SUBOPTION_REMOTE_ID, "Remote-ID" },
    361  1.1  christos         { AGENT_SUBOPTION_SUBSCRIBER_ID, "Subscriber-ID" },
    362  1.1  christos         { 0,			NULL }
    363  1.1  christos };
    364  1.1  christos 
    365  1.1  christos 
    366  1.1  christos static void
    367  1.1  christos rfc1048_print(register const u_char *bp)
    368  1.1  christos {
    369  1.1  christos 	register u_int16_t tag;
    370  1.1  christos 	register u_int len;
    371  1.1  christos 	register const char *cp;
    372  1.1  christos 	register char c;
    373  1.1  christos 	int first, idx;
    374  1.1  christos 	u_int32_t ul;
    375  1.1  christos 	u_int16_t us;
    376  1.1  christos 	u_int8_t uc, subopt, suboptlen;
    377  1.1  christos 
    378  1.1  christos 	printf("\n\t  Vendor-rfc1048 Extensions");
    379  1.1  christos 
    380  1.1  christos 	/* Step over magic cookie */
    381  1.1  christos         printf("\n\t    Magic Cookie 0x%08x", EXTRACT_32BITS(bp));
    382  1.1  christos 	bp += sizeof(int32_t);
    383  1.1  christos 
    384  1.1  christos 	/* Loop while we there is a tag left in the buffer */
    385  1.1  christos 	while (TTEST2(*bp, 1)) {
    386  1.1  christos 		tag = *bp++;
    387  1.1  christos 		if (tag == TAG_PAD && vflag < 3)
    388  1.1  christos 			continue;
    389  1.1  christos 		if (tag == TAG_END && vflag < 3)
    390  1.1  christos 			return;
    391  1.1  christos 		if (tag == TAG_EXTENDED_OPTION) {
    392  1.1  christos 			TCHECK2(*(bp + 1), 2);
    393  1.1  christos 			tag = EXTRACT_16BITS(bp + 1);
    394  1.1  christos 			/* XXX we don't know yet if the IANA will
    395  1.1  christos 			 * preclude overlap of 1-byte and 2-byte spaces.
    396  1.1  christos 			 * If not, we need to offset tag after this step.
    397  1.1  christos 			 */
    398  1.1  christos 			cp = tok2str(xtag2str, "?xT%u", tag);
    399  1.1  christos 		} else
    400  1.1  christos 			cp = tok2str(tag2str, "?T%u", tag);
    401  1.1  christos 		c = *cp++;
    402  1.1  christos 
    403  1.1  christos 		if (tag == TAG_PAD || tag == TAG_END)
    404  1.1  christos 			len = 0;
    405  1.1  christos 		else {
    406  1.1  christos 			/* Get the length; check for truncation */
    407  1.1  christos 			TCHECK2(*bp, 1);
    408  1.1  christos 			len = *bp++;
    409  1.1  christos 		}
    410  1.1  christos 
    411  1.1  christos 		printf("\n\t    %s Option %u, length %u%s", cp, tag, len,
    412  1.1  christos 		    len > 0 ? ": " : "");
    413  1.1  christos 
    414  1.1  christos 		if (tag == TAG_PAD && vflag > 2) {
    415  1.1  christos 			u_int ntag = 1;
    416  1.1  christos 			while (TTEST2(*bp, 1) && *bp == TAG_PAD) {
    417  1.1  christos 				bp++;
    418  1.1  christos 				ntag++;
    419  1.1  christos 			}
    420  1.1  christos 			if (ntag > 1)
    421  1.1  christos 				printf(", occurs %u", ntag);
    422  1.1  christos 		}
    423  1.1  christos 
    424  1.1  christos 		if (!TTEST2(*bp, len)) {
    425  1.1  christos 			printf("[|rfc1048 %u]", len);
    426  1.1  christos 			return;
    427  1.1  christos 		}
    428  1.1  christos 
    429  1.1  christos 		if (tag == TAG_DHCP_MESSAGE && len == 1) {
    430  1.1  christos 			uc = *bp++;
    431  1.1  christos                         printf("%s", tok2str(dhcp_msg_values, "Unknown (%u)", uc));
    432  1.1  christos                         continue;
    433  1.1  christos 		}
    434  1.1  christos 
    435  1.1  christos 		if (tag == TAG_PARM_REQUEST) {
    436  1.1  christos 			idx = 0;
    437  1.1  christos 			while (len-- > 0) {
    438  1.1  christos 				uc = *bp++;
    439  1.1  christos 				cp = tok2str(tag2str, "?Option %u", uc);
    440  1.1  christos 				if (idx % 4 == 0)
    441  1.1  christos 					printf("\n\t      ");
    442  1.1  christos 				else
    443  1.1  christos 					printf(", ");
    444  1.1  christos 				printf("%s", cp + 1);
    445  1.1  christos 				idx++;
    446  1.1  christos 			}
    447  1.1  christos 			continue;
    448  1.1  christos 		}
    449  1.1  christos 
    450  1.1  christos 		if (tag == TAG_EXTENDED_REQUEST) {
    451  1.1  christos 			first = 1;
    452  1.1  christos 			while (len > 1) {
    453  1.1  christos 				len -= 2;
    454  1.1  christos 				us = EXTRACT_16BITS(bp);
    455  1.1  christos 				bp += 2;
    456  1.1  christos 				cp = tok2str(xtag2str, "?xT%u", us);
    457  1.1  christos 				if (!first)
    458  1.1  christos 					putchar('+');
    459  1.1  christos 				printf("%s", cp + 1);
    460  1.1  christos 				first = 0;
    461  1.1  christos 			}
    462  1.1  christos 			continue;
    463  1.1  christos 		}
    464  1.1  christos 
    465  1.1  christos 		/* Print data */
    466  1.1  christos 		if (c == '?') {
    467  1.1  christos 			/* Base default formats for unknown tags on data size */
    468  1.1  christos 			if (len & 1)
    469  1.1  christos 				c = 'b';
    470  1.1  christos 			else if (len & 2)
    471  1.1  christos 				c = 's';
    472  1.1  christos 			else
    473  1.1  christos 				c = 'l';
    474  1.1  christos 		}
    475  1.1  christos 		first = 1;
    476  1.1  christos 		switch (c) {
    477  1.1  christos 
    478  1.1  christos 		case 'a':
    479  1.1  christos 			/* ascii strings */
    480  1.1  christos 			putchar('"');
    481  1.1  christos 			if (fn_printn(bp, len, snapend)) {
    482  1.1  christos 				putchar('"');
    483  1.1  christos 				goto trunc;
    484  1.1  christos 			}
    485  1.1  christos 			putchar('"');
    486  1.1  christos 			bp += len;
    487  1.1  christos 			len = 0;
    488  1.1  christos 			break;
    489  1.1  christos 
    490  1.1  christos 		case 'i':
    491  1.1  christos 		case 'l':
    492  1.1  christos 		case 'L':
    493  1.1  christos 			/* ip addresses/32-bit words */
    494  1.1  christos 			while (len >= sizeof(ul)) {
    495  1.1  christos 				if (!first)
    496  1.1  christos 					putchar(',');
    497  1.1  christos 				ul = EXTRACT_32BITS(bp);
    498  1.1  christos 				if (c == 'i') {
    499  1.1  christos 					ul = htonl(ul);
    500  1.1  christos 					printf("%s", ipaddr_string(&ul));
    501  1.1  christos 				} else if (c == 'L')
    502  1.1  christos 					printf("%d", ul);
    503  1.1  christos 				else
    504  1.1  christos 					printf("%u", ul);
    505  1.1  christos 				bp += sizeof(ul);
    506  1.1  christos 				len -= sizeof(ul);
    507  1.1  christos 				first = 0;
    508  1.1  christos 			}
    509  1.1  christos 			break;
    510  1.1  christos 
    511  1.1  christos 		case 'p':
    512  1.1  christos 			/* IP address pairs */
    513  1.1  christos 			while (len >= 2*sizeof(ul)) {
    514  1.1  christos 				if (!first)
    515  1.1  christos 					putchar(',');
    516  1.1  christos 				memcpy((char *)&ul, (const char *)bp, sizeof(ul));
    517  1.1  christos 				printf("(%s:", ipaddr_string(&ul));
    518  1.1  christos 				bp += sizeof(ul);
    519  1.1  christos 				memcpy((char *)&ul, (const char *)bp, sizeof(ul));
    520  1.1  christos 				printf("%s)", ipaddr_string(&ul));
    521  1.1  christos 				bp += sizeof(ul);
    522  1.1  christos 				len -= 2*sizeof(ul);
    523  1.1  christos 				first = 0;
    524  1.1  christos 			}
    525  1.1  christos 			break;
    526  1.1  christos 
    527  1.1  christos 		case 's':
    528  1.1  christos 			/* shorts */
    529  1.1  christos 			while (len >= sizeof(us)) {
    530  1.1  christos 				if (!first)
    531  1.1  christos 					putchar(',');
    532  1.1  christos 				us = EXTRACT_16BITS(bp);
    533  1.1  christos 				printf("%u", us);
    534  1.1  christos 				bp += sizeof(us);
    535  1.1  christos 				len -= sizeof(us);
    536  1.1  christos 				first = 0;
    537  1.1  christos 			}
    538  1.1  christos 			break;
    539  1.1  christos 
    540  1.1  christos 		case 'B':
    541  1.1  christos 			/* boolean */
    542  1.1  christos 			while (len > 0) {
    543  1.1  christos 				if (!first)
    544  1.1  christos 					putchar(',');
    545  1.1  christos 				switch (*bp) {
    546  1.1  christos 				case 0:
    547  1.1  christos 					putchar('N');
    548  1.1  christos 					break;
    549  1.1  christos 				case 1:
    550  1.1  christos 					putchar('Y');
    551  1.1  christos 					break;
    552  1.1  christos 				default:
    553  1.1  christos 					printf("%u?", *bp);
    554  1.1  christos 					break;
    555  1.1  christos 				}
    556  1.1  christos 				++bp;
    557  1.1  christos 				--len;
    558  1.1  christos 				first = 0;
    559  1.1  christos 			}
    560  1.1  christos 			break;
    561  1.1  christos 
    562  1.1  christos 		case 'b':
    563  1.1  christos 		case 'x':
    564  1.1  christos 		default:
    565  1.1  christos 			/* Bytes */
    566  1.1  christos 			while (len > 0) {
    567  1.1  christos 				if (!first)
    568  1.1  christos 					putchar(c == 'x' ? ':' : '.');
    569  1.1  christos 				if (c == 'x')
    570  1.1  christos 					printf("%02x", *bp);
    571  1.1  christos 				else
    572  1.1  christos 					printf("%u", *bp);
    573  1.1  christos 				++bp;
    574  1.1  christos 				--len;
    575  1.1  christos 				first = 0;
    576  1.1  christos 			}
    577  1.1  christos 			break;
    578  1.1  christos 
    579  1.1  christos 		case '$':
    580  1.1  christos 			/* Guys we can't handle with one of the usual cases */
    581  1.1  christos 			switch (tag) {
    582  1.1  christos 
    583  1.1  christos 			case TAG_NETBIOS_NODE:
    584  1.1  christos 				/* this option should be at least 1 byte long */
    585  1.1  christos 				if (len < 1)  {
    586  1.1  christos 					printf("ERROR: option %u len %u < 1 bytes",
    587  1.1  christos 					    TAG_NETBIOS_NODE, len);
    588  1.1  christos 					break;
    589  1.1  christos 				}
    590  1.1  christos 				tag = *bp++;
    591  1.1  christos 				--len;
    592  1.1  christos 				fputs(tok2str(nbo2str, NULL, tag), stdout);
    593  1.1  christos 				break;
    594  1.1  christos 
    595  1.1  christos 			case TAG_OPT_OVERLOAD:
    596  1.1  christos 				/* this option should be at least 1 byte long */
    597  1.1  christos 				if (len < 1)  {
    598  1.1  christos 					printf("ERROR: option %u len %u < 1 bytes",
    599  1.1  christos 					    TAG_OPT_OVERLOAD, len);
    600  1.1  christos 					break;
    601  1.1  christos 				}
    602  1.1  christos 				tag = *bp++;
    603  1.1  christos 				--len;
    604  1.1  christos 				fputs(tok2str(oo2str, NULL, tag), stdout);
    605  1.1  christos 				break;
    606  1.1  christos 
    607  1.1  christos 			case TAG_CLIENT_FQDN:
    608  1.1  christos 				/* this option should be at least 3 bytes long */
    609  1.1  christos 				if (len < 3)  {
    610  1.1  christos 					printf("ERROR: option %u len %u < 3 bytes",
    611  1.1  christos 					    TAG_CLIENT_FQDN, len);
    612  1.1  christos 					bp += len;
    613  1.1  christos 					len = 0;
    614  1.1  christos 					break;
    615  1.1  christos 				}
    616  1.1  christos 				if (*bp)
    617  1.1  christos 					printf("[%s] ", client_fqdn_flags(*bp));
    618  1.1  christos 				bp++;
    619  1.1  christos 				if (*bp || *(bp+1))
    620  1.1  christos 					printf("%u/%u ", *bp, *(bp+1));
    621  1.1  christos 				bp += 2;
    622  1.1  christos 				putchar('"');
    623  1.1  christos 				if (fn_printn(bp, len - 3, snapend)) {
    624  1.1  christos 					putchar('"');
    625  1.1  christos 					goto trunc;
    626  1.1  christos 				}
    627  1.1  christos 				putchar('"');
    628  1.1  christos 				bp += len - 3;
    629  1.1  christos 				len = 0;
    630  1.1  christos 				break;
    631  1.1  christos 
    632  1.1  christos 			case TAG_CLIENT_ID:
    633  1.1  christos 			    {	int type;
    634  1.1  christos 
    635  1.1  christos 				/* this option should be at least 1 byte long */
    636  1.1  christos 				if (len < 1)  {
    637  1.1  christos 					printf("ERROR: option %u len %u < 1 bytes",
    638  1.1  christos 					    TAG_CLIENT_ID, len);
    639  1.1  christos 					break;
    640  1.1  christos 				}
    641  1.1  christos 				type = *bp++;
    642  1.1  christos 				len--;
    643  1.1  christos 				if (type == 0) {
    644  1.1  christos 					putchar('"');
    645  1.1  christos 					if (fn_printn(bp, len, snapend)) {
    646  1.1  christos 						putchar('"');
    647  1.1  christos 						goto trunc;
    648  1.1  christos 					}
    649  1.1  christos 					putchar('"');
    650  1.1  christos 					bp += len;
    651  1.1  christos 					len = 0;
    652  1.1  christos 					break;
    653  1.1  christos 				} else {
    654  1.1  christos 					printf("%s ", tok2str(arp2str, "hardware-type %u,", type));
    655  1.1  christos 					while (len > 0) {
    656  1.1  christos 						if (!first)
    657  1.1  christos 							putchar(':');
    658  1.1  christos 						printf("%02x", *bp);
    659  1.1  christos 						++bp;
    660  1.1  christos 						--len;
    661  1.1  christos 						first = 0;
    662  1.1  christos 					}
    663  1.1  christos 				}
    664  1.1  christos 				break;
    665  1.1  christos 			    }
    666  1.1  christos 
    667  1.1  christos 			case TAG_AGENT_CIRCUIT:
    668  1.1  christos 				while (len >= 2) {
    669  1.1  christos 					subopt = *bp++;
    670  1.1  christos 					suboptlen = *bp++;
    671  1.1  christos 					len -= 2;
    672  1.1  christos 					if (suboptlen > len) {
    673  1.1  christos 						printf("\n\t      %s SubOption %u, length %u: length goes past end of option",
    674  1.1  christos 						   tok2str(agent_suboption_values, "Unknown", subopt),
    675  1.1  christos 						   subopt,
    676  1.1  christos 						   suboptlen);
    677  1.1  christos 						bp += len;
    678  1.1  christos 						len = 0;
    679  1.1  christos 						break;
    680  1.1  christos 					}
    681  1.1  christos 					printf("\n\t      %s SubOption %u, length %u: ",
    682  1.1  christos 					   tok2str(agent_suboption_values, "Unknown", subopt),
    683  1.1  christos 					   subopt,
    684  1.1  christos 					   suboptlen);
    685  1.1  christos 					switch (subopt) {
    686  1.1  christos 
    687  1.1  christos                                         case AGENT_SUBOPTION_CIRCUIT_ID: /* fall through */
    688  1.1  christos                                         case AGENT_SUBOPTION_REMOTE_ID:
    689  1.1  christos                                         case AGENT_SUBOPTION_SUBSCRIBER_ID:
    690  1.1  christos                                                 fn_printn(bp, suboptlen, NULL);
    691  1.1  christos                                                 break;
    692  1.1  christos 
    693  1.1  christos 					default:
    694  1.1  christos 						print_unknown_data(bp, "\n\t\t", suboptlen);
    695  1.1  christos 					}
    696  1.1  christos 
    697  1.1  christos 					len -= suboptlen;
    698  1.1  christos 					bp += suboptlen;
    699  1.1  christos 			    }
    700  1.1  christos 			    break;
    701  1.1  christos 
    702  1.1  christos 			case TAG_CLASSLESS_STATIC_RT:
    703  1.1  christos 			case TAG_CLASSLESS_STA_RT_MS:
    704  1.1  christos 			{
    705  1.1  christos 				u_int mask_width, significant_octets, i;
    706  1.1  christos 
    707  1.1  christos 				/* this option should be at least 5 bytes long */
    708  1.1  christos 				if (len < 5)  {
    709  1.1  christos 					printf("ERROR: option %u len %u < 5 bytes",
    710  1.1  christos 					    TAG_CLASSLESS_STATIC_RT, len);
    711  1.1  christos 					bp += len;
    712  1.1  christos 					len = 0;
    713  1.1  christos 					break;
    714  1.1  christos 				}
    715  1.1  christos 				while (len > 0) {
    716  1.1  christos 					if (!first)
    717  1.1  christos 						putchar(',');
    718  1.1  christos 					mask_width = *bp++;
    719  1.1  christos 					len--;
    720  1.1  christos 					/* mask_width <= 32 */
    721  1.1  christos 					if (mask_width > 32) {
    722  1.1  christos 						printf("[ERROR: Mask width (%d) > 32]",  mask_width);
    723  1.1  christos 						bp += len;
    724  1.1  christos 						len = 0;
    725  1.1  christos 						break;
    726  1.1  christos 					}
    727  1.1  christos 					significant_octets = (mask_width + 7) / 8;
    728  1.1  christos 					/* significant octets + router(4) */
    729  1.1  christos 					if (len < significant_octets + 4) {
    730  1.1  christos 						printf("[ERROR: Remaining length (%u) < %u bytes]",  len, significant_octets + 4);
    731  1.1  christos 						bp += len;
    732  1.1  christos 						len = 0;
    733  1.1  christos 						break;
    734  1.1  christos 					}
    735  1.1  christos 					putchar('(');
    736  1.1  christos 					if (mask_width == 0)
    737  1.1  christos 						printf("default");
    738  1.1  christos 					else {
    739  1.1  christos 						for (i = 0; i < significant_octets ; i++) {
    740  1.1  christos 							if (i > 0)
    741  1.1  christos 								putchar('.');
    742  1.1  christos 							printf("%d", *bp++);
    743  1.1  christos 						}
    744  1.1  christos 						for (i = significant_octets ; i < 4 ; i++)
    745  1.1  christos 							printf(".0");
    746  1.1  christos 						printf("/%d", mask_width);
    747  1.1  christos 					}
    748  1.1  christos 					memcpy((char *)&ul, (const char *)bp, sizeof(ul));
    749  1.1  christos 					printf(":%s)", ipaddr_string(&ul));
    750  1.1  christos 					bp += sizeof(ul);
    751  1.1  christos 					len -= (significant_octets + 4);
    752  1.1  christos 					first = 0;
    753  1.1  christos 				}
    754  1.1  christos 			}
    755  1.1  christos 			break;
    756  1.1  christos 
    757  1.1  christos 			default:
    758  1.1  christos 				printf("[unknown special tag %u, size %u]",
    759  1.1  christos 				    tag, len);
    760  1.1  christos 				bp += len;
    761  1.1  christos 				len = 0;
    762  1.1  christos 				break;
    763  1.1  christos 			}
    764  1.1  christos 			break;
    765  1.1  christos 		}
    766  1.1  christos 		/* Data left over? */
    767  1.1  christos 		if (len) {
    768  1.1  christos 			printf("\n\t  trailing data length %u", len);
    769  1.1  christos 			bp += len;
    770  1.1  christos 		}
    771  1.1  christos 	}
    772  1.1  christos 	return;
    773  1.1  christos trunc:
    774  1.1  christos 	printf("|[rfc1048]");
    775  1.1  christos }
    776  1.1  christos 
    777  1.1  christos static void
    778  1.1  christos cmu_print(register const u_char *bp)
    779  1.1  christos {
    780  1.1  christos 	register const struct cmu_vend *cmu;
    781  1.1  christos 
    782  1.1  christos #define PRINTCMUADDR(m, s) { TCHECK(cmu->m); \
    783  1.1  christos     if (cmu->m.s_addr != 0) \
    784  1.1  christos 	printf(" %s:%s", s, ipaddr_string(&cmu->m.s_addr)); }
    785  1.1  christos 
    786  1.1  christos 	printf(" vend-cmu");
    787  1.1  christos 	cmu = (const struct cmu_vend *)bp;
    788  1.1  christos 
    789  1.1  christos 	/* Only print if there are unknown bits */
    790  1.1  christos 	TCHECK(cmu->v_flags);
    791  1.1  christos 	if ((cmu->v_flags & ~(VF_SMASK)) != 0)
    792  1.1  christos 		printf(" F:0x%x", cmu->v_flags);
    793  1.1  christos 	PRINTCMUADDR(v_dgate, "DG");
    794  1.1  christos 	PRINTCMUADDR(v_smask, cmu->v_flags & VF_SMASK ? "SM" : "SM*");
    795  1.1  christos 	PRINTCMUADDR(v_dns1, "NS1");
    796  1.1  christos 	PRINTCMUADDR(v_dns2, "NS2");
    797  1.1  christos 	PRINTCMUADDR(v_ins1, "IEN1");
    798  1.1  christos 	PRINTCMUADDR(v_ins2, "IEN2");
    799  1.1  christos 	PRINTCMUADDR(v_ts1, "TS1");
    800  1.1  christos 	PRINTCMUADDR(v_ts2, "TS2");
    801  1.1  christos 	return;
    802  1.1  christos 
    803  1.1  christos trunc:
    804  1.1  christos 	fputs(tstr, stdout);
    805  1.1  christos #undef PRINTCMUADDR
    806  1.1  christos }
    807  1.1  christos 
    808  1.1  christos static char *
    809  1.1  christos client_fqdn_flags(u_int flags)
    810  1.1  christos {
    811  1.1  christos 	static char buf[8+1];
    812  1.1  christos 	int i = 0;
    813  1.1  christos 
    814  1.1  christos 	if (flags & CLIENT_FQDN_FLAGS_S)
    815  1.1  christos 		buf[i++] = 'S';
    816  1.1  christos 	if (flags & CLIENT_FQDN_FLAGS_O)
    817  1.1  christos 		buf[i++] = 'O';
    818  1.1  christos 	if (flags & CLIENT_FQDN_FLAGS_E)
    819  1.1  christos 		buf[i++] = 'E';
    820  1.1  christos 	if (flags & CLIENT_FQDN_FLAGS_N)
    821  1.1  christos 		buf[i++] = 'N';
    822  1.1  christos 	buf[i] = '\0';
    823  1.1  christos 
    824  1.1  christos 	return buf;
    825  1.1  christos }
    826