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