1 /*        $NetBSD: mopprobe.c,v 1.17 2025/05/04 19:54:05 rillig 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 "port.h"
28 __RCSID("$NetBSD: mopprobe.c,v 1.17 2025/05/04 19:54:05 rillig Exp $");
29 
30 #include "os.h"
31 #include "cmp.h"
32 #include "common.h"
33 #include "device.h"
34 #include "get.h"
35 #include "mopdef.h"
36 #include "nmadef.h"
37 #include "pf.h"
38 #include "print.h"
39 #include "log.h"
40 
41 /*
42  * The list of all interfaces that are being listened to.  rarp_loop()
43  * "selects" on the descriptors in this list.
44  */
45 extern struct if_info *iflist;
46 
47 __dead static void  Usage(void);
48 void      mopProcess(struct if_info *, u_char *);
49 
50 int       promisc = 1;                  /* Need promisc mode           */
51 
52 int
main(int argc,char ** argv)53 main(int argc, char  **argv)
54 {
55           int     op, AllFlag = 0;
56           char   *interface;
57 
58           mopInteractive = 1;
59 
60           while ((op = getopt(argc, argv, "a")) != -1) {
61                     switch (op) {
62                     case 'a':
63                               AllFlag++;
64                               break;
65 
66                     default:
67                               Usage();
68                     }
69           }
70           interface = argv[optind++];
71 
72           if ((AllFlag != 0) == (interface != NULL))
73                     Usage();
74 
75           if (AllFlag)
76                     deviceInitAll();
77           else
78                     deviceInitOne(interface);
79 
80           Loop();
81 }
82 
83 static void
Usage(void)84 Usage(void)
85 {
86           (void)fprintf(stderr, "usage: %s -a|interface\n", getprogname());
87           exit(1);
88 }
89 
90 /*
91  * Process incoming packages.
92  */
93 void
mopProcess(struct if_info * ii,u_char * pkt)94 mopProcess(struct if_info *ii, u_char *pkt)
95 {
96           u_char  *dst, *src, *p, mopcode, tmpc, ilen;
97           u_short *ptype, moplen, itype, len;
98           int       idx, i, trans;
99 
100           dst       = pkt;
101           src       = pkt+6;
102           ptype   = (u_short *)(pkt+12);
103           idx   = 0;
104 
105           if (*ptype < 1600) {
106                     len = *ptype;
107                     trans = TRANS_8023;
108                     ptype = (u_short *)(pkt+20);
109                     p = pkt+22;
110           } else {
111                     len = 0;
112                     trans = TRANS_ETHER;
113                     p = pkt+14;
114           }
115 
116           /* Ignore our own messages */
117 
118           if (mopCmpEAddr(ii->eaddr,src) == 0) {
119                     return;
120           }
121 
122           /* Just check multicast */
123 
124           if (mopCmpEAddr(rc_mcst,dst) != 0) {
125                     return;
126           }
127 
128           switch (trans) {
129           case TRANS_8023:
130                     moplen = len;
131                     break;
132           default:
133                     moplen = mopGetShort(pkt,&idx);
134           }
135           mopcode   = mopGetChar(p,&idx);
136 
137           /* Just process System Information */
138 
139           if (mopcode != MOP_K_CODE_SID) {
140                     return;
141           }
142 
143           tmpc      = mopGetChar(pkt,&idx);                 /* Reserved  */
144           (void)mopGetShort(pkt,&idx);            /* Receipt # */
145 
146           itype     = mopGetShort(pkt,&idx);
147 
148           while (idx < (int)(moplen + 2)) {
149                     ilen      = mopGetChar(pkt,&idx);
150                     switch (itype) {
151                     case 0:
152                               tmpc  = mopGetChar(pkt,&idx);
153                               idx = idx + tmpc;
154                               break;
155                   case MOP_K_INFO_VER:
156                               idx = idx + 3;
157                               break;
158                     case MOP_K_INFO_MFCT:
159                               idx = idx + 2;
160                               break;
161                     case MOP_K_INFO_CNU:
162                               idx = idx + 6;
163                               break;
164                     case MOP_K_INFO_RTM:
165                               idx = idx + 2;
166                               break;
167                     case MOP_K_INFO_CSZ:
168                               idx = idx + 2;
169                               break;
170                     case MOP_K_INFO_RSZ:
171                               idx = idx + 2;
172                               break;
173                     case MOP_K_INFO_HWA:
174                               idx = idx + 6;
175                               break;
176                     case MOP_K_INFO_TIME:
177                               idx = idx + 10;
178                               break;
179                   case MOP_K_INFO_SOFD:
180                               (void)mopGetChar(pkt, &idx);
181                               break;
182                     case MOP_K_INFO_SFID:
183                               tmpc = mopGetChar(pkt,&idx);
184                               if ((idx > 0) && (idx < 17))
185                                 idx = idx + tmpc;
186                               break;
187                     case MOP_K_INFO_PRTY:
188                               idx = idx + 1;
189                               break;
190                     case MOP_K_INFO_DLTY:
191                               idx = idx + 1;
192                               break;
193                   case MOP_K_INFO_DLBSZ:
194                               idx = idx + 2;
195                               break;
196                     default:
197                               if (itype > 101 && itype < 107) {
198                                         switch (itype) {
199                                         case 102:
200                                                   idx = idx + ilen;
201                                                   break;
202                                         case 103:
203                                                   idx = idx + ilen;
204                                                   break;
205                                         case 104:
206                                                   idx = idx + 2;
207                                                   break;
208                                         case 105:
209                                                   (void)fprintf(stdout,"%x:%x:%x:%x:%x:%x\t",
210                                                                   src[0],src[1],src[2],src[3],src[4],src[5]);
211                                                   for (i = 0; i < ilen; i++) {
212                                                     (void)fprintf(stdout, "%c",pkt[idx+i]);
213                                                   }
214                                                   idx = idx + ilen;
215                                                   (void)fprintf(stdout, "\n");
216                                                   break;
217                                         case 106:
218                                                   idx = idx + ilen;
219                                                   break;
220                                         }
221                               } else {
222                                         idx = idx + ilen;
223                               }
224                     }
225                     itype = mopGetShort(pkt,&idx);
226           }
227 }
228