1/*
2 * Copyright (c) 1994, 1995, 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 * Format and print Novell IPX packets.
22 * Contributed by Brad Parker (brad@fcr.com).
23 */
24
25#include <sys/cdefs.h>
26#ifndef lint
27#if 0
28static const char rcsid[] _U_ =
29    "@(#) Header: /tcpdump/master/tcpdump/print-ipx.c,v 1.42 2005-05-06 08:26:44 guy Exp";
30#else
31__RCSID("$NetBSD: print-ipx.c,v 1.2 2010/12/05 05:11:30 christos Exp $");
32#endif
33#endif
34
35#ifdef HAVE_CONFIG_H
36#include "config.h"
37#endif
38
39#include <tcpdump-stdinc.h>
40
41#include <stdlib.h>
42#include <stdio.h>
43#include <string.h>
44
45#include "interface.h"
46#include "addrtoname.h"
47#include "ipx.h"
48#include "extract.h"
49
50
51static const char *ipxaddr_string(u_int32_t, const u_char *);
52void ipx_decode(const struct ipxHdr *, const u_char *, u_int);
53void ipx_sap_print(const u_short *, u_int);
54void ipx_rip_print(const u_short *, u_int);
55
56/*
57 * Print IPX datagram packets.
58 */
59void
60ipx_print(const u_char *p, u_int length)
61{
62	const struct ipxHdr *ipx = (const struct ipxHdr *)p;
63
64	if (!eflag)
65		printf("IPX ");
66
67	TCHECK(ipx->srcSkt);
68	(void)printf("%s.%04x > ",
69		     ipxaddr_string(EXTRACT_32BITS(ipx->srcNet), ipx->srcNode),
70		     EXTRACT_16BITS(&ipx->srcSkt));
71
72	(void)printf("%s.%04x: ",
73		     ipxaddr_string(EXTRACT_32BITS(ipx->dstNet), ipx->dstNode),
74		     EXTRACT_16BITS(&ipx->dstSkt));
75
76	/* take length from ipx header */
77	TCHECK(ipx->length);
78	length = EXTRACT_16BITS(&ipx->length);
79
80	ipx_decode(ipx, (u_char *)ipx + ipxSize, length - ipxSize);
81	return;
82trunc:
83	printf("[|ipx %d]", length);
84}
85
86static const char *
87ipxaddr_string(u_int32_t net, const u_char *node)
88{
89    static char line[256];
90
91    snprintf(line, sizeof(line), "%08x.%02x:%02x:%02x:%02x:%02x:%02x",
92	    net, node[0], node[1], node[2], node[3], node[4], node[5]);
93
94    return line;
95}
96
97void
98ipx_decode(const struct ipxHdr *ipx, const u_char *datap, u_int length)
99{
100    register u_short dstSkt;
101
102    dstSkt = EXTRACT_16BITS(&ipx->dstSkt);
103    switch (dstSkt) {
104      case IPX_SKT_NCP:
105	(void)printf("ipx-ncp %d", length);
106	break;
107      case IPX_SKT_SAP:
108	ipx_sap_print((u_short *)datap, length);
109	break;
110      case IPX_SKT_RIP:
111	ipx_rip_print((u_short *)datap, length);
112	break;
113      case IPX_SKT_NETBIOS:
114	(void)printf("ipx-netbios %d", length);
115#ifdef TCPDUMP_DO_SMB
116	ipx_netbios_print(datap, length);
117#endif
118	break;
119      case IPX_SKT_DIAGNOSTICS:
120	(void)printf("ipx-diags %d", length);
121	break;
122      case IPX_SKT_NWLINK_DGM:
123	(void)printf("ipx-nwlink-dgm %d", length);
124#ifdef TCPDUMP_DO_SMB
125	ipx_netbios_print(datap, length);
126#endif
127	break;
128      case IPX_SKT_EIGRP:
129	eigrp_print(datap, length);
130	break;
131      default:
132	(void)printf("ipx-#%x %d", dstSkt, length);
133	break;
134    }
135}
136
137void
138ipx_sap_print(const u_short *ipx, u_int length)
139{
140    int command, i;
141
142    TCHECK(ipx[0]);
143    command = EXTRACT_16BITS(ipx);
144    ipx++;
145    length -= 2;
146
147    switch (command) {
148      case 1:
149      case 3:
150	if (command == 1)
151	    (void)printf("ipx-sap-req");
152	else
153	    (void)printf("ipx-sap-nearest-req");
154
155	TCHECK(ipx[0]);
156	(void)printf(" %s", ipxsap_string(htons(EXTRACT_16BITS(&ipx[0]))));
157	break;
158
159      case 2:
160      case 4:
161	if (command == 2)
162	    (void)printf("ipx-sap-resp");
163	else
164	    (void)printf("ipx-sap-nearest-resp");
165
166	for (i = 0; i < 8 && length > 0; i++) {
167	    TCHECK(ipx[0]);
168	    (void)printf(" %s '", ipxsap_string(htons(EXTRACT_16BITS(&ipx[0]))));
169	    if (fn_printzp((u_char *)&ipx[1], 48, snapend)) {
170		printf("'");
171		goto trunc;
172	    }
173	    TCHECK2(ipx[25], 10);
174	    printf("' addr %s",
175		ipxaddr_string(EXTRACT_32BITS(&ipx[25]), (u_char *)&ipx[27]));
176	    ipx += 32;
177	    length -= 64;
178	}
179	break;
180      default:
181	(void)printf("ipx-sap-?%x", command);
182	break;
183    }
184    return;
185trunc:
186    printf("[|ipx %d]", length);
187}
188
189void
190ipx_rip_print(const u_short *ipx, u_int length)
191{
192    int command, i;
193
194    TCHECK(ipx[0]);
195    command = EXTRACT_16BITS(ipx);
196    ipx++;
197    length -= 2;
198
199    switch (command) {
200      case 1:
201	(void)printf("ipx-rip-req");
202	if (length > 0) {
203	    TCHECK(ipx[3]);
204	    (void)printf(" %u/%d.%d", EXTRACT_32BITS(&ipx[0]),
205			 EXTRACT_16BITS(&ipx[2]), EXTRACT_16BITS(&ipx[3]));
206	}
207	break;
208      case 2:
209	(void)printf("ipx-rip-resp");
210	for (i = 0; i < 50 && length > 0; i++) {
211	    TCHECK(ipx[3]);
212	    (void)printf(" %u/%d.%d", EXTRACT_32BITS(&ipx[0]),
213			 EXTRACT_16BITS(&ipx[2]), EXTRACT_16BITS(&ipx[3]));
214
215	    ipx += 4;
216	    length -= 8;
217	}
218	break;
219      default:
220	(void)printf("ipx-rip-?%x", command);
221	break;
222    }
223    return;
224trunc:
225    printf("[|ipx %d]", length);
226}
227