xref: /netbsd-src/usr.sbin/mopd/mopprobe/mopprobe.c (revision b1c86f5f087524e68db12794ee9c3e3da1ab17a0)
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  */
54 struct if_info *iflist;
55 
56 void	Usage __P((void));
57 int	main __P((int, char **));
58 void	mopProcess __P((struct if_info *, u_char *));
59 
60 int     AllFlag = 0;		/* listen on "all" interfaces  */
61 int     DebugFlag = 0;		/* print debugging messages    */
62 int	Not3Flag = 0;		/* Not MOP V3 messages         */
63 int	Not4Flag = 0;		/* Not MOP V4 messages         */
64 int     oflag = 0;		/* print only once             */
65 int	promisc = 1;		/* Need promisc mode           */
66 
67 int
68 main(argc, argv)
69 	int     argc;
70 	char  **argv;
71 {
72 	int     op;
73 	char   *interface;
74 
75 	mopInteractive = 1;
76 
77 	opterr = 0;
78 	while ((op = getopt(argc, argv, "ado")) != -1) {
79 		switch (op) {
80 		case '3':
81 			Not3Flag++;
82 			break;
83 		case '4':
84 			Not4Flag++;
85 			break;
86 		case 'a':
87 			AllFlag++;
88 			break;
89 		case 'd':
90 			DebugFlag++;
91 			break;
92 		case 'o':
93 			oflag++;
94 			break;
95 
96 		default:
97 			Usage();
98 			/* NOTREACHED */
99 		}
100 	}
101 	interface = argv[optind++];
102 
103 	if ((AllFlag && interface) ||
104 	    (!AllFlag && interface == 0) ||
105 	    (Not3Flag && Not4Flag))
106 		Usage();
107 
108 	if (AllFlag)
109  		deviceInitAll();
110 	else
111 		deviceInitOne(interface);
112 
113 	Loop();
114 	/* NOTREACHED */
115 	return (0);
116 }
117 
118 void
119 Usage()
120 {
121 	(void) fprintf(stderr, "usage: %s -a [ -3 | -4 ]\n", getprogname());
122 	(void) fprintf(stderr, "       %s [ -3 | -4 ] interface\n",
123 	    getprogname());
124 	exit(1);
125 }
126 
127 /*
128  * Process incomming packages.
129  */
130 void
131 mopProcess(ii, pkt)
132 	struct if_info *ii;
133 	u_char *pkt;
134 {
135 	u_char  *dst, *src, *p, mopcode, tmpc, ilen;
136 	u_short *ptype, moplen, tmps, itype, len;
137 	int	idx, i, device, trans;
138 
139 	dst	= pkt;
140 	src	= pkt+6;
141 	ptype   = (u_short *)(pkt+12);
142 	idx   = 0;
143 
144 	if (*ptype < 1600) {
145 		len = *ptype;
146 		trans = TRANS_8023;
147 		ptype = (u_short *)(pkt+20);
148 		p = pkt+22;
149 		if (Not4Flag) return;
150 	} else {
151 		len = 0;
152 		trans = TRANS_ETHER;
153 		p = pkt+14;
154 		if (Not3Flag) return;
155 	}
156 
157 	/* Ignore our own messages */
158 
159 	if (mopCmpEAddr(ii->eaddr,src) == 0) {
160 		return;
161 	}
162 
163 	/* Just check multicast */
164 
165 	if (mopCmpEAddr(rc_mcst,dst) != 0) {
166 		return;
167 	}
168 
169 	switch (trans) {
170 	case TRANS_8023:
171 		moplen = len;
172 		break;
173 	default:
174 		moplen = mopGetShort(pkt,&idx);
175 	}
176 	mopcode	= mopGetChar(p,&idx);
177 
178 	/* Just process System Information */
179 
180 	if (mopcode != MOP_K_CODE_SID) {
181 		return;
182 	}
183 
184 	tmpc	= mopGetChar(pkt,&idx);		/* Reserved  */
185 	tmps	= mopGetShort(pkt,&idx);		/* Receipt # */
186 
187 	device	= 0;					/* Unknown Device */
188 
189 	itype	= mopGetShort(pkt,&idx);
190 
191 	while (idx < (int)(moplen + 2)) {
192 		ilen	= mopGetChar(pkt,&idx);
193 		switch (itype) {
194 		case 0:
195 			tmpc  = mopGetChar(pkt,&idx);
196 			idx = idx + tmpc;
197 			break;
198 	        case MOP_K_INFO_VER:
199 			idx = idx + 3;
200 			break;
201 		case MOP_K_INFO_MFCT:
202 			idx = idx + 2;
203 			break;
204 		case MOP_K_INFO_CNU:
205 			idx = idx + 6;
206 			break;
207 		case MOP_K_INFO_RTM:
208 			idx = idx + 2;
209 			break;
210 		case MOP_K_INFO_CSZ:
211 			idx = idx + 2;
212 			break;
213 		case MOP_K_INFO_RSZ:
214 			idx = idx + 2;
215 			break;
216 		case MOP_K_INFO_HWA:
217 			idx = idx + 6;
218 			break;
219 		case MOP_K_INFO_TIME:
220 			idx = idx + 10;
221 			break;
222 	        case MOP_K_INFO_SOFD:
223 			device = mopGetChar(pkt,&idx);
224 			break;
225 		case MOP_K_INFO_SFID:
226 			tmpc = mopGetChar(pkt,&idx);
227 			if ((idx > 0) && (idx < 17))
228 			  idx = idx + tmpc;
229 			break;
230 		case MOP_K_INFO_PRTY:
231 			idx = idx + 1;
232 			break;
233 		case MOP_K_INFO_DLTY:
234 			idx = idx + 1;
235 			break;
236 	        case MOP_K_INFO_DLBSZ:
237 			idx = idx + 2;
238 			break;
239 		default:
240 			if (((device = NMA_C_SOFD_LCS) ||   /* DECserver 100 */
241 			     (device = NMA_C_SOFD_DS2) ||   /* DECserver 200 */
242 			     (device = NMA_C_SOFD_DP2) ||   /* DECserver 250 */
243 			     (device = NMA_C_SOFD_DS3)) &&  /* DECserver 300 */
244 			    ((itype > 101) && (itype < 107)))
245 			{
246 				switch (itype) {
247 				case 102:
248 					idx = idx + ilen;
249 					break;
250 				case 103:
251 					idx = idx + ilen;
252 					break;
253 				case 104:
254 					idx = idx + 2;
255 					break;
256 				case 105:
257 					(void)fprintf(stdout,"%x:%x:%x:%x:%x:%x\t",
258 						      src[0],src[1],src[2],src[3],src[4],src[5]);
259 					for (i = 0; i < ilen; i++) {
260 					  (void)fprintf(stdout, "%c",pkt[idx+i]);
261 					}
262 					idx = idx + ilen;
263 					(void)fprintf(stdout, "\n");
264 					break;
265 				case 106:
266 					idx = idx + ilen;
267 					break;
268 				};
269 			} else {
270 				idx = idx + ilen;
271 			};
272 		}
273 		itype = mopGetShort(pkt,&idx);
274 	}
275 
276 }
277 
278