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