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