13229Spst/* 23229Spst * hwaddr.c - routines that deal with hardware addresses. 33229Spst * (i.e. Ethernet) 418471Swosch * 550476Speter * $FreeBSD$ 63229Spst */ 73229Spst 83229Spst#include <sys/types.h> 93229Spst#include <sys/param.h> 103229Spst#include <sys/socket.h> 113229Spst#include <sys/ioctl.h> 123229Spst 133229Spst#if defined(SUNOS) || defined(SVR4) 143229Spst#include <sys/sockio.h> 153229Spst#endif 163229Spst#ifdef SVR4 173229Spst#include <sys/stream.h> 183229Spst#include <stropts.h> 193229Spst#include <fcntl.h> 203229Spst#endif 213229Spst 2213572Spst#ifdef _AIX32 2313572Spst#include <sys/time.h> /* for struct timeval in net/if.h */ 2413572Spst#include <net/if.h> /* for struct ifnet in net/if_arp.h */ 2513572Spst#endif 2613572Spst 273229Spst#include <net/if_arp.h> 283229Spst#include <netinet/in.h> 2913572Spst 3013572Spst#ifdef WIN_TCP 3113572Spst#include <netinet/if_ether.h> 3213572Spst#include <sys/dlpi.h> 3313572Spst#endif 3413572Spst 353229Spst#include <stdio.h> 363229Spst#ifndef NO_UNISTD 373229Spst#include <unistd.h> 383229Spst#endif 393229Spst#include <syslog.h> 403229Spst 413229Spst#ifndef USE_BFUNCS 423229Spst/* Yes, memcpy is OK here (no overlapped copies). */ 433229Spst#include <memory.h> 443229Spst#define bcopy(a,b,c) memcpy(b,a,c) 453229Spst#define bzero(p,l) memset(p,0,l) 463229Spst#define bcmp(a,b,c) memcmp(a,b,c) 473229Spst#endif 483229Spst 4913572Spst#ifndef ATF_INUSE /* Not defined on some systems (i.e. Linux) */ 5013572Spst#define ATF_INUSE 0 513229Spst#endif 523229Spst 5313577Spst/* For BSD 4.4, set arp entry by writing to routing socket */ 5413577Spst#if defined(BSD) 5513577Spst#if BSD >= 199306 56104742Salfredextern int bsd_arp_set(struct in_addr *, char *, int); 5713577Spst#endif 5813577Spst#endif 5913577Spst 603229Spst#include "bptypes.h" 613229Spst#include "hwaddr.h" 623229Spst#include "report.h" 633229Spst 643229Spstextern int debug; 653229Spst 663229Spst/* 673229Spst * Hardware address lengths (in bytes) and network name based on hardware 683229Spst * type code. List in order specified by Assigned Numbers RFC; Array index 693229Spst * is hardware type code. Entries marked as zero are unknown to the author 703229Spst * at this time. . . . 713229Spst */ 723229Spst 733229Spststruct hwinfo hwinfolist[] = 743229Spst{ 753229Spst {0, "Reserved"}, /* Type 0: Reserved (don't use this) */ 763229Spst {6, "Ethernet"}, /* Type 1: 10Mb Ethernet (48 bits) */ 773229Spst {1, "3Mb Ethernet"}, /* Type 2: 3Mb Ethernet (8 bits) */ 783229Spst {0, "AX.25"}, /* Type 3: Amateur Radio AX.25 */ 793229Spst {1, "ProNET"}, /* Type 4: Proteon ProNET Token Ring */ 803229Spst {0, "Chaos"}, /* Type 5: Chaos */ 813229Spst {6, "IEEE 802"}, /* Type 6: IEEE 802 Networks */ 823229Spst {0, "ARCNET"} /* Type 7: ARCNET */ 833229Spst}; 843229Spstint hwinfocnt = sizeof(hwinfolist) / sizeof(hwinfolist[0]); 853229Spst 863229Spst 873229Spst/* 883229Spst * Setup the arp cache so that IP address 'ia' will be temporarily 893229Spst * bound to hardware address 'ha' of length 'len'. 903229Spst */ 913229Spstvoid 9213572Spstsetarp(s, ia, hafamily, haddr, halen) 933229Spst int s; /* socket fd */ 9413572Spst struct in_addr *ia; /* protocol address */ 9513572Spst int hafamily; /* HW address family */ 9613572Spst u_char *haddr; /* HW address data */ 9713572Spst int halen; 983229Spst{ 993229Spst#ifdef SIOCSARP 10013572Spst#ifdef WIN_TCP 10113572Spst /* This is an SVR4 with different networking code from 10213572Spst * Wollongong WIN-TCP. Not quite like the Lachman code. 10313572Spst * Code from: drew@drewsun.FEITH.COM (Andrew B. Sudell) 10413572Spst */ 10513572Spst#undef SIOCSARP 10613572Spst#define SIOCSARP ARP_ADD 10713572Spst struct arptab arpreq; /* Arp table entry */ 10813572Spst 10913572Spst bzero((caddr_t) &arpreq, sizeof(arpreq)); 11013572Spst arpreq.at_flags = ATF_COM; 11113572Spst 11213572Spst /* Set up IP address */ 11313572Spst arpreq.at_in = ia->s_addr; 11413572Spst 11513572Spst /* Set up Hardware Address */ 11613572Spst bcopy(haddr, arpreq.at_enaddr, halen); 11713572Spst 11813572Spst /* Set the Date Link type. */ 11913572Spst /* XXX - Translate (hafamily) to dltype somehow? */ 12013572Spst arpreq.at_dltype = DL_ETHER; 12113572Spst 12213572Spst#else /* WIN_TCP */ 12313572Spst /* Good old Berkeley way. */ 1243229Spst struct arpreq arpreq; /* Arp request ioctl block */ 1253229Spst struct sockaddr_in *si; 12613572Spst char *p; 1273229Spst 12813572Spst bzero((caddr_t) &arpreq, sizeof(arpreq)); 1293229Spst arpreq.arp_flags = ATF_INUSE | ATF_COM; 1303229Spst 1313229Spst /* Set up the protocol address. */ 1323229Spst arpreq.arp_pa.sa_family = AF_INET; 1333229Spst si = (struct sockaddr_in *) &arpreq.arp_pa; 1343229Spst si->sin_addr = *ia; 1353229Spst 1363229Spst /* Set up the hardware address. */ 13713572Spst#ifdef __linux__ /* XXX - Do others need this? -gwr */ 13813572Spst /* 13913572Spst * Linux requires the sa_family field set. 14013572Spst * longyear@netcom.com (Al Longyear) 14113572Spst */ 14213572Spst arpreq.arp_ha.sa_family = hafamily; 14313572Spst#endif /* linux */ 1443229Spst 14513572Spst /* This variable is just to help catch type mismatches. */ 14613572Spst p = arpreq.arp_ha.sa_data; 14713572Spst bcopy(haddr, p, halen); 14813572Spst#endif /* WIN_TCP */ 14913572Spst 1503229Spst#ifdef SVR4 1513229Spst /* 1523229Spst * And now the stuff for System V Rel 4.x which does not 1533229Spst * appear to allow SIOCxxx ioctls on a socket descriptor. 1543229Spst * Thanks to several people: (all sent the same fix) 1553229Spst * Barney Wolff <barney@databus.com>, 1563229Spst * bear@upsys.se (Bj|rn Sj|holm), 1573229Spst * Michael Kuschke <Michael.Kuschke@Materna.DE>, 1583229Spst */ 15913572Spst { 16013572Spst int fd; 16113572Spst struct strioctl iocb; 16213572Spst 16313572Spst if ((fd=open("/dev/arp", O_RDWR)) < 0) { 16413572Spst report(LOG_ERR, "open /dev/arp: %s\n", get_errmsg()); 16513572Spst } 16613572Spst iocb.ic_cmd = SIOCSARP; 16713572Spst iocb.ic_timout = 0; 16813572Spst iocb.ic_dp = (char *)&arpreq; 16913572Spst iocb.ic_len = sizeof(arpreq); 17013572Spst if (ioctl(fd, I_STR, (caddr_t)&iocb) < 0) { 17113572Spst report(LOG_ERR, "ioctl I_STR: %s\n", get_errmsg()); 17213572Spst } 17313572Spst close (fd); 1743229Spst } 1753229Spst#else /* SVR4 */ 1763229Spst /* 1773229Spst * On SunOS, the ioctl sometimes returns ENXIO, and it 1783229Spst * appears to happen when the ARP cache entry you tried 1793229Spst * to add is already in the cache. (Sigh...) 1803229Spst * XXX - Should this error simply be ignored? -gwr 1813229Spst */ 18213572Spst if (ioctl(s, SIOCSARP, (caddr_t) &arpreq) < 0) { 1833229Spst report(LOG_ERR, "ioctl SIOCSARP: %s", get_errmsg()); 1843229Spst } 1853229Spst#endif /* SVR4 */ 1863229Spst#else /* SIOCSARP */ 18713577Spst#if defined(BSD) && (BSD >= 199306) 18813577Spst bsd_arp_set(ia, haddr, halen); 18913577Spst#else 1903229Spst /* 1913229Spst * Oh well, SIOCSARP is not defined. Just run arp(8). 19213572Spst * Need to delete partial entry first on some systems. 1933229Spst * XXX - Gag! 1943229Spst */ 19513572Spst int status; 1963229Spst char buf[256]; 19713572Spst char *a; 19813572Spst extern char *inet_ntoa(); 1993229Spst 20013572Spst a = inet_ntoa(*ia); 20131971Simp snprintf(buf, sizeof(buf), "arp -d %s; arp -s %s %s temp", 20213572Spst a, a, haddrtoa(haddr, halen)); 2033229Spst if (debug > 2) 204105041Skris report(LOG_INFO, "%s", buf); 2053229Spst status = system(buf); 2063229Spst if (status) 2073229Spst report(LOG_ERR, "arp failed, exit code=0x%x", status); 2083229Spst return; 20913577Spst#endif /* ! 4.4 BSD */ 2103229Spst#endif /* SIOCSARP */ 2113229Spst} 2123229Spst 2133229Spst 2143229Spst/* 2153229Spst * Convert a hardware address to an ASCII string. 2163229Spst */ 2173229Spstchar * 2183229Spsthaddrtoa(haddr, hlen) 2193229Spst u_char *haddr; 2203229Spst int hlen; 2213229Spst{ 2223229Spst static char haddrbuf[3 * MAXHADDRLEN + 1]; 2233229Spst char *bufptr; 2243229Spst 2253229Spst if (hlen > MAXHADDRLEN) 2263229Spst hlen = MAXHADDRLEN; 2273229Spst 2283229Spst bufptr = haddrbuf; 2293229Spst while (hlen > 0) { 2303229Spst sprintf(bufptr, "%02X:", (unsigned) (*haddr++ & 0xFF)); 2313229Spst bufptr += 3; 2323229Spst hlen--; 2333229Spst } 2343229Spst bufptr[-1] = 0; 2353229Spst return (haddrbuf); 2363229Spst} 2373229Spst 2383229Spst 2393229Spst/* 2403229Spst * haddr_conv802() 2413229Spst * -------------- 2423229Spst * 2433229Spst * Converts a backwards address to a canonical address and a canonical address 2443229Spst * to a backwards address. 2453229Spst * 2463229Spst * INPUTS: 2473229Spst * adr_in - pointer to six byte string to convert (unsigned char *) 2483229Spst * addr_len - how many bytes to convert 2493229Spst * 2503229Spst * OUTPUTS: 2513229Spst * addr_out - The string is updated to contain the converted address. 2523229Spst * 2533229Spst * CALLER: 2543229Spst * many 2553229Spst * 2563229Spst * DATA: 2573229Spst * Uses conv802table to bit-reverse the address bytes. 2583229Spst */ 2593229Spst 2603229Spststatic u_char conv802table[256] = 2613229Spst{ 2623229Spst /* 0x00 */ 0x00, 0x80, 0x40, 0xC0, 0x20, 0xA0, 0x60, 0xE0, 2633229Spst /* 0x08 */ 0x10, 0x90, 0x50, 0xD0, 0x30, 0xB0, 0x70, 0xF0, 2643229Spst /* 0x10 */ 0x08, 0x88, 0x48, 0xC8, 0x28, 0xA8, 0x68, 0xE8, 2653229Spst /* 0x18 */ 0x18, 0x98, 0x58, 0xD8, 0x38, 0xB8, 0x78, 0xF8, 2663229Spst /* 0x20 */ 0x04, 0x84, 0x44, 0xC4, 0x24, 0xA4, 0x64, 0xE4, 2673229Spst /* 0x28 */ 0x14, 0x94, 0x54, 0xD4, 0x34, 0xB4, 0x74, 0xF4, 2683229Spst /* 0x30 */ 0x0C, 0x8C, 0x4C, 0xCC, 0x2C, 0xAC, 0x6C, 0xEC, 2693229Spst /* 0x38 */ 0x1C, 0x9C, 0x5C, 0xDC, 0x3C, 0xBC, 0x7C, 0xFC, 2703229Spst /* 0x40 */ 0x02, 0x82, 0x42, 0xC2, 0x22, 0xA2, 0x62, 0xE2, 2713229Spst /* 0x48 */ 0x12, 0x92, 0x52, 0xD2, 0x32, 0xB2, 0x72, 0xF2, 2723229Spst /* 0x50 */ 0x0A, 0x8A, 0x4A, 0xCA, 0x2A, 0xAA, 0x6A, 0xEA, 2733229Spst /* 0x58 */ 0x1A, 0x9A, 0x5A, 0xDA, 0x3A, 0xBA, 0x7A, 0xFA, 2743229Spst /* 0x60 */ 0x06, 0x86, 0x46, 0xC6, 0x26, 0xA6, 0x66, 0xE6, 2753229Spst /* 0x68 */ 0x16, 0x96, 0x56, 0xD6, 0x36, 0xB6, 0x76, 0xF6, 2763229Spst /* 0x70 */ 0x0E, 0x8E, 0x4E, 0xCE, 0x2E, 0xAE, 0x6E, 0xEE, 2773229Spst /* 0x78 */ 0x1E, 0x9E, 0x5E, 0xDE, 0x3E, 0xBE, 0x7E, 0xFE, 2783229Spst /* 0x80 */ 0x01, 0x81, 0x41, 0xC1, 0x21, 0xA1, 0x61, 0xE1, 2793229Spst /* 0x88 */ 0x11, 0x91, 0x51, 0xD1, 0x31, 0xB1, 0x71, 0xF1, 2803229Spst /* 0x90 */ 0x09, 0x89, 0x49, 0xC9, 0x29, 0xA9, 0x69, 0xE9, 2813229Spst /* 0x98 */ 0x19, 0x99, 0x59, 0xD9, 0x39, 0xB9, 0x79, 0xF9, 2823229Spst /* 0xA0 */ 0x05, 0x85, 0x45, 0xC5, 0x25, 0xA5, 0x65, 0xE5, 2833229Spst /* 0xA8 */ 0x15, 0x95, 0x55, 0xD5, 0x35, 0xB5, 0x75, 0xF5, 2843229Spst /* 0xB0 */ 0x0D, 0x8D, 0x4D, 0xCD, 0x2D, 0xAD, 0x6D, 0xED, 2853229Spst /* 0xB8 */ 0x1D, 0x9D, 0x5D, 0xDD, 0x3D, 0xBD, 0x7D, 0xFD, 2863229Spst /* 0xC0 */ 0x03, 0x83, 0x43, 0xC3, 0x23, 0xA3, 0x63, 0xE3, 2873229Spst /* 0xC8 */ 0x13, 0x93, 0x53, 0xD3, 0x33, 0xB3, 0x73, 0xF3, 2883229Spst /* 0xD0 */ 0x0B, 0x8B, 0x4B, 0xCB, 0x2B, 0xAB, 0x6B, 0xEB, 2893229Spst /* 0xD8 */ 0x1B, 0x9B, 0x5B, 0xDB, 0x3B, 0xBB, 0x7B, 0xFB, 2903229Spst /* 0xE0 */ 0x07, 0x87, 0x47, 0xC7, 0x27, 0xA7, 0x67, 0xE7, 2913229Spst /* 0xE8 */ 0x17, 0x97, 0x57, 0xD7, 0x37, 0xB7, 0x77, 0xF7, 2923229Spst /* 0xF0 */ 0x0F, 0x8F, 0x4F, 0xCF, 0x2F, 0xAF, 0x6F, 0xEF, 2933229Spst /* 0xF8 */ 0x1F, 0x9F, 0x5F, 0xDF, 0x3F, 0xBF, 0x7F, 0xFF, 2943229Spst}; 2953229Spst 2963229Spstvoid 2973229Spsthaddr_conv802(addr_in, addr_out, len) 298297865Spfg u_char *addr_in, *addr_out; 2993229Spst int len; 3003229Spst{ 3013229Spst u_char *lim; 3023229Spst 3033229Spst lim = addr_out + len; 3043229Spst while (addr_out < lim) 3053229Spst *addr_out++ = conv802table[*addr_in++]; 3063229Spst} 3073229Spst 3083229Spst#if 0 3093229Spst/* 3103229Spst * For the record, here is a program to generate the 3113229Spst * bit-reverse table above. 3123229Spst */ 3133229Spststatic int 3143229Spstbitrev(n) 3153229Spst int n; 3163229Spst{ 3173229Spst int i, r; 3183229Spst 3193229Spst r = 0; 3203229Spst for (i = 0; i < 8; i++) { 3213229Spst r <<= 1; 3223229Spst r |= (n & 1); 3233229Spst n >>= 1; 3243229Spst } 3253229Spst return r; 3263229Spst} 3273229Spst 3283229Spstmain() 3293229Spst{ 3303229Spst int i; 3313229Spst for (i = 0; i <= 0xFF; i++) { 3323229Spst if ((i & 7) == 0) 3333229Spst printf("/* 0x%02X */", i); 3343229Spst printf(" 0x%02X,", bitrev(i)); 3353229Spst if ((i & 7) == 7) 3363229Spst printf("\n"); 3373229Spst } 3383229Spst} 3393229Spst 3403229Spst#endif 3413229Spst 3423229Spst/* 3433229Spst * Local Variables: 3443229Spst * tab-width: 4 3453229Spst * c-indent-level: 4 3463229Spst * c-argdecl-indent: 4 3473229Spst * c-continued-statement-offset: 4 3483229Spst * c-continued-brace-offset: -4 3493229Spst * c-label-offset: -4 3503229Spst * c-brace-offset: 0 3513229Spst * End: 3523229Spst */ 353