1/*	$NetBSD: mopprobe.c,v 1.10 2009/10/20 00:51:13 snj Exp $	*/
2
3/*
4 * Copyright (c) 1993-96 Mats O Jansson.  All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#include <sys/cdefs.h>
28#ifndef lint
29__RCSID("$NetBSD: mopprobe.c,v 1.10 2009/10/20 00:51:13 snj Exp $");
30#endif
31
32/*
33 * mopprobe - MOP Probe Utility
34 *
35 * Usage:	mopprobe -a [ -3 | -4 ]
36 *		mopprobe [ -3 | -4 ] interface
37 */
38
39#include "os.h"
40#include "cmp.h"
41#include "common.h"
42#include "device.h"
43#include "get.h"
44#include "mopdef.h"
45#include "nmadef.h"
46#include "pf.h"
47#include "print.h"
48#include "log.h"
49
50/*
51 * The list of all interfaces that are being listened to.  rarp_loop()
52 * "selects" on the descriptors in this list.
53 */
54struct if_info *iflist;
55
56__dead static void	Usage(void);
57void	mopProcess(struct if_info *, u_char *);
58
59int     AllFlag = 0;		/* listen on "all" interfaces  */
60int     DebugFlag = 0;		/* print debugging messages    */
61int	Not3Flag = 0;		/* Not MOP V3 messages         */
62int	Not4Flag = 0;		/* Not MOP V4 messages         */
63int     oflag = 0;		/* print only once             */
64int	promisc = 1;		/* Need promisc mode           */
65
66int
67main(int argc, char  **argv)
68{
69	int     op;
70	char   *interface;
71
72	mopInteractive = 1;
73
74	opterr = 0;
75	while ((op = getopt(argc, argv, "ado")) != -1) {
76		switch (op) {
77		case '3':
78			Not3Flag++;
79			break;
80		case '4':
81			Not4Flag++;
82			break;
83		case 'a':
84			AllFlag++;
85			break;
86		case 'd':
87			DebugFlag++;
88			break;
89		case 'o':
90			oflag++;
91			break;
92
93		default:
94			Usage();
95			/* NOTREACHED */
96		}
97	}
98	interface = argv[optind++];
99
100	if ((AllFlag && interface) ||
101	    (!AllFlag && interface == 0) ||
102	    (Not3Flag && Not4Flag))
103		Usage();
104
105	if (AllFlag)
106 		deviceInitAll();
107	else
108		deviceInitOne(interface);
109
110	Loop();
111	/* NOTREACHED */
112	return (0);
113}
114
115static void
116Usage(void)
117{
118	(void) fprintf(stderr, "usage: %s -a [ -3 | -4 ]\n", getprogname());
119	(void) fprintf(stderr, "       %s [ -3 | -4 ] interface\n",
120	    getprogname());
121	exit(1);
122}
123
124/*
125 * Process incomming packages.
126 */
127void
128mopProcess(struct if_info *ii, u_char *pkt)
129{
130	u_char  *dst, *src, *p, mopcode, tmpc, ilen;
131	u_short *ptype, moplen, tmps, itype, len;
132	int	idx, i, device, trans;
133
134	dst	= pkt;
135	src	= pkt+6;
136	ptype   = (u_short *)(pkt+12);
137	idx   = 0;
138
139	if (*ptype < 1600) {
140		len = *ptype;
141		trans = TRANS_8023;
142		ptype = (u_short *)(pkt+20);
143		p = pkt+22;
144		if (Not4Flag) return;
145	} else {
146		len = 0;
147		trans = TRANS_ETHER;
148		p = pkt+14;
149		if (Not3Flag) return;
150	}
151
152	/* Ignore our own messages */
153
154	if (mopCmpEAddr(ii->eaddr,src) == 0) {
155		return;
156	}
157
158	/* Just check multicast */
159
160	if (mopCmpEAddr(rc_mcst,dst) != 0) {
161		return;
162	}
163
164	switch (trans) {
165	case TRANS_8023:
166		moplen = len;
167		break;
168	default:
169		moplen = mopGetShort(pkt,&idx);
170	}
171	mopcode	= mopGetChar(p,&idx);
172
173	/* Just process System Information */
174
175	if (mopcode != MOP_K_CODE_SID) {
176		return;
177	}
178
179	tmpc	= mopGetChar(pkt,&idx);		/* Reserved  */
180	tmps	= mopGetShort(pkt,&idx);		/* Receipt # */
181
182	device	= 0;					/* Unknown Device */
183
184	itype	= mopGetShort(pkt,&idx);
185
186	while (idx < (int)(moplen + 2)) {
187		ilen	= mopGetChar(pkt,&idx);
188		switch (itype) {
189		case 0:
190			tmpc  = mopGetChar(pkt,&idx);
191			idx = idx + tmpc;
192			break;
193	        case MOP_K_INFO_VER:
194			idx = idx + 3;
195			break;
196		case MOP_K_INFO_MFCT:
197			idx = idx + 2;
198			break;
199		case MOP_K_INFO_CNU:
200			idx = idx + 6;
201			break;
202		case MOP_K_INFO_RTM:
203			idx = idx + 2;
204			break;
205		case MOP_K_INFO_CSZ:
206			idx = idx + 2;
207			break;
208		case MOP_K_INFO_RSZ:
209			idx = idx + 2;
210			break;
211		case MOP_K_INFO_HWA:
212			idx = idx + 6;
213			break;
214		case MOP_K_INFO_TIME:
215			idx = idx + 10;
216			break;
217	        case MOP_K_INFO_SOFD:
218			device = mopGetChar(pkt,&idx);
219			break;
220		case MOP_K_INFO_SFID:
221			tmpc = mopGetChar(pkt,&idx);
222			if ((idx > 0) && (idx < 17))
223			  idx = idx + tmpc;
224			break;
225		case MOP_K_INFO_PRTY:
226			idx = idx + 1;
227			break;
228		case MOP_K_INFO_DLTY:
229			idx = idx + 1;
230			break;
231	        case MOP_K_INFO_DLBSZ:
232			idx = idx + 2;
233			break;
234		default:
235			if (((device = NMA_C_SOFD_LCS) ||   /* DECserver 100 */
236			     (device = NMA_C_SOFD_DS2) ||   /* DECserver 200 */
237			     (device = NMA_C_SOFD_DP2) ||   /* DECserver 250 */
238			     (device = NMA_C_SOFD_DS3)) &&  /* DECserver 300 */
239			    ((itype > 101) && (itype < 107)))
240			{
241				switch (itype) {
242				case 102:
243					idx = idx + ilen;
244					break;
245				case 103:
246					idx = idx + ilen;
247					break;
248				case 104:
249					idx = idx + 2;
250					break;
251				case 105:
252					(void)fprintf(stdout,"%x:%x:%x:%x:%x:%x\t",
253						      src[0],src[1],src[2],src[3],src[4],src[5]);
254					for (i = 0; i < ilen; i++) {
255					  (void)fprintf(stdout, "%c",pkt[idx+i]);
256					}
257					idx = idx + ilen;
258					(void)fprintf(stdout, "\n");
259					break;
260				case 106:
261					idx = idx + ilen;
262					break;
263				};
264			} else {
265				idx = idx + ilen;
266			};
267		}
268		itype = mopGetShort(pkt,&idx);
269	}
270
271}
272
273