xref: /openbsd-src/usr.sbin/mopd/mopprobe/mopprobe.c (revision 4c1e55dc91edd6e69ccc60ce855900fbc12cf34f)
1 /*	$OpenBSD: mopprobe.c,v 1.13 2010/05/01 08:14:26 mk 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 /*
28  * mopprobe - MOP Probe Utility
29  *
30  * Usage:	mopprobe [-3 | -4] [-aov] interface
31  */
32 
33 #include "os.h"
34 #include "common/common.h"
35 #include "common/mopdef.h"
36 #include "common/device.h"
37 #include "common/print.h"
38 #include "common/get.h"
39 #include "common/cmp.h"
40 #include "common/pf.h"
41 #include "common/nmadef.h"
42 
43 /*
44  * The list of all interfaces that are being listened to.
45  */
46 struct if_info *iflist;
47 
48 __dead void   Loop(void);
49 void   Usage(void);
50 void   mopProcess(struct if_info *, u_char *);
51 
52 struct once {
53 	u_char	eaddr[6];		/* Ethernet addr */
54 	struct once *next;		/* Next one */
55 };
56 
57 int     AllFlag = 0;		/* listen on "all" interfaces  */
58 int	Not3Flag = 0;		/* Not MOP V3 messages         */
59 int	Not4Flag = 0;		/* Not MOP V4 messages         */
60 int	VerboseFlag = 0;	/* Print All Announces	       */
61 int     OnceFlag = 0;		/* print only once             */
62 int	promisc = 1;		/* Need promisc mode           */
63 extern char *__progname;
64 struct once *root = NULL;
65 
66 int
67 main(int argc, char *argv[])
68 {
69 	int     op;
70 	char   *interface;
71 
72 	/* All error reporting is done through syslogs. */
73 	openlog(__progname, LOG_PID | LOG_CONS, LOG_DAEMON);
74 
75 	opterr = 0;
76 	while ((op = getopt(argc, argv, "34aov")) != -1) {
77 		switch (op) {
78 		case '3':
79 			Not3Flag++;
80 			break;
81 		case '4':
82 			Not4Flag++;
83 			break;
84 		case 'a':
85 			AllFlag++;
86 			break;
87 		case 'o':
88 			OnceFlag++;
89 			break;
90 		case 'v':
91 			VerboseFlag++;
92 			break;
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 }
113 
114 void
115 Usage()
116 {
117 	fprintf(stderr, "usage: %s [-3 | -4] [-aov] interface\n", __progname);
118 	exit(1);
119 }
120 
121 /*
122  * Process incoming packages.
123  */
124 void
125 mopProcess(struct if_info *ii, u_char *pkt)
126 {
127 	u_char	*dst, *src, mopcode, tmpc, device, ilen;
128 	u_short	 ptype, moplen = 0, itype;
129 	int	 idx, trans, len, i, hwa = 0;
130 	struct once *o = NULL;
131 
132 	/* We don't known with transport, Guess! */
133 
134 	trans = mopGetTrans(pkt, 0);
135 
136 	/* Ok, return if we don't wan't this message */
137 
138 	if ((trans == TRANS_ETHER) && Not3Flag) return;
139 	if ((trans == TRANS_8023) && Not4Flag)	return;
140 
141 	idx = 0;
142 	mopGetHeader(pkt, &idx, &dst, &src, &ptype, &len, trans);
143 
144 	/* Ignore our own transmissions */
145 
146 	if (mopCmpEAddr(ii->eaddr,src) == 0)
147 		return;
148 
149 	/* Just check multicast */
150 
151 	if (mopCmpEAddr(rc_mcst,dst) != 0) {
152 		return;
153 	}
154 
155 	switch(ptype) {
156 	case MOP_K_PROTO_RC:
157 		break;
158 	default:
159 		return;
160 	}
161 
162 	if (OnceFlag) {
163 		o = root;
164 		while (o != NULL) {
165 			if (mopCmpEAddr(o->eaddr,src) == 0)
166 				return;
167 			o = o->next;
168 		}
169 		o = (struct once *)malloc(sizeof(*o));
170 		o->eaddr[0] = src[0];
171 		o->eaddr[1] = src[1];
172 		o->eaddr[2] = src[2];
173 		o->eaddr[3] = src[3];
174 		o->eaddr[4] = src[4];
175 		o->eaddr[5] = src[5];
176 		o->next = root;
177 		root = o;
178 	}
179 
180 	moplen  = mopGetLength(pkt, trans);
181 	mopcode	= mopGetChar(pkt,&idx);
182 
183 	/* Just process System Information */
184 
185 	if (mopcode != MOP_K_CODE_SID) {
186 		return;
187 	}
188 
189 	mopGetChar(pkt,&idx);			/* Reserved */
190 	mopGetShort(pkt,&idx);			/* Receipt # */
191 
192 	device = 0;
193 
194 	switch(trans) {
195 	case TRANS_ETHER:
196 		moplen = moplen + 16;
197 		break;
198 	case TRANS_8023:
199 		moplen = moplen + 14;
200 		break;
201 	}
202 
203 	itype = mopGetShort(pkt,&idx);
204 
205 	while (idx < (int)(moplen)) {
206 		ilen  = mopGetChar(pkt,&idx);
207 		switch (itype) {
208 		case 0:
209 			tmpc  = mopGetChar(pkt,&idx);
210 			idx = idx + tmpc;
211 			break;
212 		case MOP_K_INFO_VER:
213 			idx = idx + 3;
214 			break;
215 		case MOP_K_INFO_MFCT:
216 		case MOP_K_INFO_RTM:
217 		case MOP_K_INFO_CSZ:
218 		case MOP_K_INFO_RSZ:
219 			idx = idx + 2;
220 			break;
221 		case MOP_K_INFO_HWA:
222 			hwa = idx;
223 			/* FALLTHROUGH */
224 		case MOP_K_INFO_CNU:
225 			idx = idx + 6;
226 			break;
227 		case MOP_K_INFO_TIME:
228 			idx = idx + 10;
229 			break;
230 	        case MOP_K_INFO_SOFD:
231 			device = mopGetChar(pkt,&idx);
232 			if (VerboseFlag &&
233 			    (device != NMA_C_SOFD_LCS) &&   /* DECserver 100 */
234 			    (device != NMA_C_SOFD_DS2) &&   /* DECserver 200 */
235 			    (device != NMA_C_SOFD_DP2) &&   /* DECserver 250 */
236 			    (device != NMA_C_SOFD_DS3))     /* DECserver 300 */
237 			{
238 				mopPrintHWA(stdout, src);
239 				fprintf(stdout," # ");
240 				mopPrintDevice(stdout, device);
241 				fprintf(stdout," ");
242 				mopPrintHWA(stdout, &pkt[hwa]);
243 				fprintf(stdout,"\n");
244 			}
245 			break;
246 		case MOP_K_INFO_SFID:
247 			tmpc = mopGetChar(pkt,&idx);
248 			if ((tmpc > 0) && (tmpc < 17))
249 				idx = idx + tmpc;
250 			break;
251 		case MOP_K_INFO_PRTY:
252 			idx = idx + 1;
253 			break;
254 		case MOP_K_INFO_DLTY:
255 			idx = idx + 1;
256 			break;
257 	        case MOP_K_INFO_DLBSZ:
258 			idx = idx + 2;
259 			break;
260 		default:
261 			if (((device == NMA_C_SOFD_LCS) ||  /* DECserver 100 */
262 			     (device == NMA_C_SOFD_DS2) ||  /* DECserver 200 */
263 			     (device == NMA_C_SOFD_DP2) ||  /* DECserver 250 */
264 			     (device == NMA_C_SOFD_DS3)) && /* DECserver 300 */
265 			    ((itype > 101) && (itype < 107)))
266 			{
267 				switch (itype) {
268 				case 102:
269 				case 103:
270 				case 106:
271 					idx = idx + ilen;
272 					break;
273 				case 104:
274 					idx = idx + 2;
275 					break;
276 				case 105:
277 					mopPrintHWA(stdout, src);
278 					fprintf(stdout," ");
279 					for (i = 0; i < ilen; i++) {
280 						fprintf(stdout, "%c",pkt[idx+i]);
281 					}
282 					idx = idx + ilen;
283 					fprintf(stdout, "\n");
284 					break;
285 				};
286 			} else {
287 				idx = idx + ilen;
288 			};
289 		}
290 		itype = mopGetShort(pkt,&idx);
291 	}
292 }
293