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