10Sstevel@tonic-gate /* 20Sstevel@tonic-gate * CDDL HEADER START 30Sstevel@tonic-gate * 40Sstevel@tonic-gate * The contents of this file are subject to the terms of the 53628Sss150715 * Common Development and Distribution License (the "License"). 63628Sss150715 * You may not use this file except in compliance with the License. 70Sstevel@tonic-gate * 80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 100Sstevel@tonic-gate * See the License for the specific language governing permissions 110Sstevel@tonic-gate * and limitations under the License. 120Sstevel@tonic-gate * 130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 180Sstevel@tonic-gate * 190Sstevel@tonic-gate * CDDL HEADER END 200Sstevel@tonic-gate */ 210Sstevel@tonic-gate /* 22*6353Sdr146992 * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 230Sstevel@tonic-gate * Use is subject to license terms. 240Sstevel@tonic-gate */ 250Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 260Sstevel@tonic-gate /* All Rights Reserved */ 270Sstevel@tonic-gate 280Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 290Sstevel@tonic-gate 300Sstevel@tonic-gate #include "defs.h" 310Sstevel@tonic-gate #include "ifconfig.h" 320Sstevel@tonic-gate #include <sys/types.h> 330Sstevel@tonic-gate #include <libdlpi.h> 340Sstevel@tonic-gate #include <sys/sysmacros.h> 353628Sss150715 #include <sys/time.h> 360Sstevel@tonic-gate #include <deflt.h> 370Sstevel@tonic-gate 380Sstevel@tonic-gate #define IPADDRL sizeof (struct in_addr) 390Sstevel@tonic-gate #define RARPRETRIES 5 403628Sss150715 #define MSEC2NSEC(msec) ((msec) * 1000000) 413628Sss150715 #define NSEC2MSEC(nsec) ((nsec) / 1000000) 420Sstevel@tonic-gate 430Sstevel@tonic-gate /* 440Sstevel@tonic-gate * The following value (8) is determined to work reliably in switched 10/100MB 450Sstevel@tonic-gate * ethernet environments. Use caution if you plan on decreasing it. 460Sstevel@tonic-gate */ 470Sstevel@tonic-gate #define RARPTIMEOUT 8 480Sstevel@tonic-gate 490Sstevel@tonic-gate static char defaultfile[] = "/etc/inet/rarp"; 500Sstevel@tonic-gate static char retries_var[] = "RARP_RETRIES="; 510Sstevel@tonic-gate static int rarp_timeout = RARPTIMEOUT; 520Sstevel@tonic-gate static int rarp_retries = RARPRETRIES; 530Sstevel@tonic-gate 543628Sss150715 static dlpi_handle_t rarp_open(const char *, size_t *, uchar_t *, uchar_t *); 553628Sss150715 static int rarp_recv(dlpi_handle_t, struct arphdr *, size_t, size_t, int64_t); 560Sstevel@tonic-gate 570Sstevel@tonic-gate int 583628Sss150715 doifrevarp(const char *linkname, struct sockaddr_in *laddr) 590Sstevel@tonic-gate { 603628Sss150715 int s, retval; 610Sstevel@tonic-gate struct arphdr *req, *ans; 620Sstevel@tonic-gate struct in_addr from; 630Sstevel@tonic-gate struct in_addr answer; 640Sstevel@tonic-gate struct lifreq lifr; 650Sstevel@tonic-gate int tries_left; 663628Sss150715 size_t physaddrlen, ifrarplen; 673628Sss150715 uchar_t my_macaddr[DLPI_PHYSADDR_MAX]; 683628Sss150715 uchar_t my_broadcast[DLPI_PHYSADDR_MAX]; 693628Sss150715 dlpi_handle_t dh; 700Sstevel@tonic-gate 713628Sss150715 if (linkname[0] == '\0') { 720Sstevel@tonic-gate (void) fprintf(stderr, "ifconfig: doifrevarp: name not set\n"); 730Sstevel@tonic-gate exit(1); 740Sstevel@tonic-gate } 750Sstevel@tonic-gate 760Sstevel@tonic-gate if (debug) 773628Sss150715 (void) printf("doifrevarp interface %s\n", linkname); 780Sstevel@tonic-gate 793628Sss150715 if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) 800Sstevel@tonic-gate Perror0_exit("socket"); 813628Sss150715 823628Sss150715 (void) strlcpy(lifr.lifr_name, linkname, sizeof (lifr.lifr_name)); 833628Sss150715 if (ioctl(s, SIOCGLIFFLAGS, (char *)&lifr) < 0) { 843628Sss150715 (void) close(s); 853628Sss150715 Perror0_exit("SIOCGLIFFLAGS"); 860Sstevel@tonic-gate } 870Sstevel@tonic-gate 880Sstevel@tonic-gate /* don't try to revarp if we know it won't work */ 890Sstevel@tonic-gate if ((lifr.lifr_flags & IFF_LOOPBACK) || 900Sstevel@tonic-gate (lifr.lifr_flags & IFF_NOARP) || 913628Sss150715 (lifr.lifr_flags & IFF_POINTOPOINT)) { 923628Sss150715 (void) close(s); 930Sstevel@tonic-gate return (0); 943628Sss150715 } 950Sstevel@tonic-gate 960Sstevel@tonic-gate /* open rarp interface */ 973628Sss150715 dh = rarp_open(linkname, &physaddrlen, my_macaddr, my_broadcast); 983628Sss150715 if (dh == NULL) { 993628Sss150715 (void) close(s); 1000Sstevel@tonic-gate return (0); 1013628Sss150715 } 1020Sstevel@tonic-gate 1030Sstevel@tonic-gate /* 1040Sstevel@tonic-gate * RARP looks at /etc/ethers and NIS, which only works 1050Sstevel@tonic-gate * with 6 byte addresses currently. 1060Sstevel@tonic-gate */ 1073628Sss150715 if (physaddrlen != ETHERADDRL) { 1083628Sss150715 dlpi_close(dh); 1093628Sss150715 (void) close(s); 1100Sstevel@tonic-gate return (0); 1110Sstevel@tonic-gate } 1120Sstevel@tonic-gate 1133628Sss150715 ifrarplen = sizeof (struct arphdr) + (2 * IPADDRL) + (2 * physaddrlen); 1140Sstevel@tonic-gate 1150Sstevel@tonic-gate /* look for adjustments to rarp_retries in the RARP defaults file */ 1160Sstevel@tonic-gate if (defopen(defaultfile) == 0) { 1170Sstevel@tonic-gate char *cp; 1180Sstevel@tonic-gate 1190Sstevel@tonic-gate if (cp = defread(retries_var)) { 1200Sstevel@tonic-gate int ntries; 1210Sstevel@tonic-gate 1220Sstevel@tonic-gate ntries = atoi(cp); 1230Sstevel@tonic-gate if (ntries > 0) 1240Sstevel@tonic-gate rarp_retries = ntries; 1250Sstevel@tonic-gate } 1260Sstevel@tonic-gate (void) defopen(NULL); /* close default file */ 1270Sstevel@tonic-gate } 1280Sstevel@tonic-gate 1290Sstevel@tonic-gate /* allocate request and response buffers */ 1303628Sss150715 if (((req = malloc(ifrarplen)) == NULL) || 1313628Sss150715 ((ans = malloc(ifrarplen)) == NULL)) { 1323628Sss150715 dlpi_close(dh); 1333628Sss150715 (void) close(s); 1340Sstevel@tonic-gate free(req); 1350Sstevel@tonic-gate return (0); 1360Sstevel@tonic-gate } 1370Sstevel@tonic-gate 1380Sstevel@tonic-gate /* create rarp request */ 1390Sstevel@tonic-gate (void) memset(req, 0, ifrarplen); 1400Sstevel@tonic-gate req->ar_hrd = htons(ARPHRD_ETHER); 1410Sstevel@tonic-gate req->ar_pro = htons(ETHERTYPE_IP); 1423628Sss150715 req->ar_hln = physaddrlen; 1430Sstevel@tonic-gate req->ar_pln = IPADDRL; 1440Sstevel@tonic-gate req->ar_op = htons(REVARP_REQUEST); 1450Sstevel@tonic-gate 1463628Sss150715 (void) memcpy(&req[1], my_macaddr, physaddrlen); 1470Sstevel@tonic-gate (void) memcpy((uchar_t *)req + sizeof (struct arphdr) + IPADDRL + 1483628Sss150715 physaddrlen, my_macaddr, physaddrlen); 1490Sstevel@tonic-gate 1503628Sss150715 for (tries_left = rarp_retries; tries_left > 0; --tries_left) { 1513628Sss150715 /* send the request */ 1523628Sss150715 retval = dlpi_send(dh, my_broadcast, physaddrlen, req, 1533628Sss150715 ifrarplen, NULL); 1543628Sss150715 if (retval != DLPI_SUCCESS) { 1553628Sss150715 Perrdlpi("doifrevarp: cannot send rarp request", 1563628Sss150715 linkname, retval); 1573628Sss150715 break; 1580Sstevel@tonic-gate } 1590Sstevel@tonic-gate 1603628Sss150715 if (debug) 1613628Sss150715 (void) printf("rarp sent\n"); 1623628Sss150715 1633628Sss150715 retval = rarp_recv(dh, ans, ifrarplen, physaddrlen, 1643628Sss150715 rarp_timeout * MILLISEC); 1653628Sss150715 1663628Sss150715 if (retval != DLPI_ETIMEDOUT) 1673628Sss150715 break; 1680Sstevel@tonic-gate 1693628Sss150715 if (debug) 1703628Sss150715 (void) printf("rarp retry\n"); 1713628Sss150715 } 1723628Sss150715 1733628Sss150715 if (retval == DLPI_SUCCESS) { 1743628Sss150715 (void) memcpy(&answer, (uchar_t *)ans + 1753628Sss150715 sizeof (struct arphdr) + (2 * physaddrlen) + IPADDRL, 1763628Sss150715 sizeof (answer)); 1773628Sss150715 (void) memcpy(&from, (uchar_t *)ans + physaddrlen + 1783628Sss150715 sizeof (struct arphdr), sizeof (from)); 1790Sstevel@tonic-gate 1800Sstevel@tonic-gate if (debug) { 1813628Sss150715 (void) printf("answer: %s", inet_ntoa(answer)); 1823628Sss150715 (void) printf(" [from %s]\n", inet_ntoa(from)); 1830Sstevel@tonic-gate } 1843628Sss150715 laddr->sin_addr = answer; 1853628Sss150715 } else if (debug) { 1863628Sss150715 Perrdlpi("doifrevarp: could not receive rarp reply", 1873628Sss150715 linkname, retval); 1883628Sss150715 } 1890Sstevel@tonic-gate 1903628Sss150715 dlpi_close(dh); 1913628Sss150715 (void) close(s); 1920Sstevel@tonic-gate free(req); 1930Sstevel@tonic-gate free(ans); 1943628Sss150715 return (retval == DLPI_SUCCESS); 1950Sstevel@tonic-gate } 1960Sstevel@tonic-gate 1970Sstevel@tonic-gate /* 1980Sstevel@tonic-gate * Open the datalink provider device and bind to the REVARP type. 1993628Sss150715 * Return the resulting DLPI handle. 2000Sstevel@tonic-gate */ 2013628Sss150715 static dlpi_handle_t 2023628Sss150715 rarp_open(const char *linkname, size_t *alen, uchar_t *myaddr, uchar_t *mybaddr) 2030Sstevel@tonic-gate { 2043628Sss150715 int retval; 2053628Sss150715 char *physaddr, *bcastaddr; 2063628Sss150715 dlpi_info_t dlinfo; 2073628Sss150715 dlpi_handle_t dh; 2080Sstevel@tonic-gate 2090Sstevel@tonic-gate if (debug) 2103628Sss150715 (void) printf("rarp_open %s\n", linkname); 2110Sstevel@tonic-gate 2123628Sss150715 if ((retval = dlpi_open(linkname, &dh, 0)) != DLPI_SUCCESS) { 2133628Sss150715 Perrdlpi("rarp_open: dlpi_open failed", linkname, retval); 2143628Sss150715 return (NULL); 2150Sstevel@tonic-gate } 2160Sstevel@tonic-gate 2173628Sss150715 if ((retval = dlpi_bind(dh, ETHERTYPE_REVARP, NULL)) != DLPI_SUCCESS) { 2183628Sss150715 Perrdlpi("rarp_open: dlpi_bind failed", linkname, retval); 2190Sstevel@tonic-gate goto failed; 2200Sstevel@tonic-gate } 2210Sstevel@tonic-gate 2223628Sss150715 if ((retval = dlpi_info(dh, &dlinfo, 0)) != DLPI_SUCCESS) { 2233628Sss150715 Perrdlpi("rarp_open: dlpi_info failed", linkname, retval); 2240Sstevel@tonic-gate goto failed; 2250Sstevel@tonic-gate } 2260Sstevel@tonic-gate 2273628Sss150715 if (dlinfo.di_bcastaddrlen == 0) { 2283628Sss150715 (void) fprintf(stderr, "ifconfig: rarp_open: %s broadcast " 2293628Sss150715 "not supported\n", linkname); 2300Sstevel@tonic-gate goto failed; 2310Sstevel@tonic-gate } 2320Sstevel@tonic-gate 2333628Sss150715 /* we assume the following are equal and fill in 'alen' */ 2343628Sss150715 assert(dlinfo.di_bcastaddrlen == dlinfo.di_physaddrlen); 2353628Sss150715 2363628Sss150715 (void) memcpy(mybaddr, dlinfo.di_bcastaddr, dlinfo.di_bcastaddrlen); 2373628Sss150715 2383628Sss150715 *alen = dlinfo.di_physaddrlen; 2393628Sss150715 2403628Sss150715 (void) memcpy(myaddr, dlinfo.di_physaddr, dlinfo.di_physaddrlen); 2413628Sss150715 2420Sstevel@tonic-gate if (debug) { 2433628Sss150715 bcastaddr = _link_ntoa(mybaddr, NULL, dlinfo.di_bcastaddrlen, 2443628Sss150715 IFT_OTHER); 2453628Sss150715 2463628Sss150715 physaddr = _link_ntoa(myaddr, NULL, dlinfo.di_physaddrlen, 2473628Sss150715 IFT_OTHER); 2483628Sss150715 2493628Sss150715 if (physaddr != NULL && bcastaddr != NULL) { 2503628Sss150715 (void) printf("device %s: broadcast address %s, mac " 2513628Sss150715 "address %s\n", linkname, bcastaddr, physaddr); 2523628Sss150715 } 2533628Sss150715 2543628Sss150715 free(physaddr); 2553628Sss150715 free(bcastaddr); 2563628Sss150715 2573628Sss150715 (void) printf("rarp_open: addr length = %d\n", 2583628Sss150715 dlinfo.di_physaddrlen); 2593628Sss150715 } 2603628Sss150715 2613628Sss150715 return (dh); 2623628Sss150715 2633628Sss150715 failed: 2643628Sss150715 dlpi_close(dh); 2653628Sss150715 return (NULL); 2663628Sss150715 } 2673628Sss150715 2683628Sss150715 /* 2693628Sss150715 * Read reply for RARP request. If a reply is received within waitms, 2703628Sss150715 * validate the reply. If it is a correct RARP reply return DLPI_SUCCESS, 2713628Sss150715 * otherwise return DLPI_ETIMEDOUT. If there is an error while reading retrun 2723628Sss150715 * the error code. 2733628Sss150715 */ 2743628Sss150715 static int 2753628Sss150715 rarp_recv(dlpi_handle_t dh, struct arphdr *ans, size_t msglen, 2763628Sss150715 size_t physaddrlen, int64_t waitms) 2773628Sss150715 { 2783628Sss150715 int retval; 2793628Sss150715 char *cause; 2803628Sss150715 size_t anslen = msglen; 2813628Sss150715 hrtime_t endtime = gethrtime() + MSEC2NSEC(waitms); 2823628Sss150715 hrtime_t currtime; 2833628Sss150715 2843628Sss150715 while ((currtime = gethrtime()) < endtime) { 2853628Sss150715 waitms = NSEC2MSEC(endtime - currtime); 2863628Sss150715 retval = dlpi_recv(dh, NULL, NULL, ans, &anslen, waitms, NULL); 2873628Sss150715 if (retval == DLPI_SUCCESS) { 2883628Sss150715 cause = NULL; 2893628Sss150715 2903628Sss150715 if (anslen < msglen) 2913628Sss150715 cause = "short packet"; 2923628Sss150715 else if (ans->ar_hrd != htons(ARPHRD_ETHER)) 2933628Sss150715 cause = "hardware type not Ethernet"; 2943628Sss150715 else if (ans->ar_pro != htons(ETHERTYPE_IP)) 2953628Sss150715 cause = "protocol type not IP"; 2963628Sss150715 else if (ans->ar_hln != physaddrlen) 2973628Sss150715 cause = "unexpected hardware address length"; 2983628Sss150715 else if (ans->ar_pln != IPADDRL) 2993628Sss150715 cause = "unexpected protocol address length"; 3003628Sss150715 if (cause != NULL) { 3013628Sss150715 (void) fprintf(stderr, "RARP packet received " 3023628Sss150715 "but discarded (%s)\n", cause); 3033628Sss150715 continue; 3043628Sss150715 } 3053628Sss150715 switch (ntohs(ans->ar_op)) { 3063628Sss150715 case REVARP_REQUEST: 3073628Sss150715 if (debug) 3083628Sss150715 (void) printf("Got a rarp request.\n"); 3093628Sss150715 break; 3103628Sss150715 3113628Sss150715 case REVARP_REPLY: 3123628Sss150715 return (DLPI_SUCCESS); 3133628Sss150715 3143628Sss150715 default: 3153628Sss150715 (void) fprintf(stderr, "ifconfig: unknown " 3163628Sss150715 "RARP opcode 0x%x\n", ans->ar_op); 3173628Sss150715 break; 3183628Sss150715 } 3193628Sss150715 } else if (retval != DLPI_ETIMEDOUT) { 3203628Sss150715 Perrdlpi("doifrevarp: dlpi_recv failed", 3213628Sss150715 dlpi_linkname(dh), retval); 3223628Sss150715 return (retval); 3230Sstevel@tonic-gate } 3240Sstevel@tonic-gate } 3250Sstevel@tonic-gate 3263628Sss150715 return (DLPI_ETIMEDOUT); 3270Sstevel@tonic-gate } 3280Sstevel@tonic-gate 3290Sstevel@tonic-gate int 3303628Sss150715 dlpi_set_address(const char *linkname, uchar_t *physaddr, uint_t physaddrlen) 3310Sstevel@tonic-gate { 3323628Sss150715 int retval; 3333628Sss150715 dlpi_handle_t dh; 3340Sstevel@tonic-gate 3353628Sss150715 if ((retval = dlpi_open(linkname, &dh, 0)) != DLPI_SUCCESS) { 3363628Sss150715 Perrdlpi("dlpi_open failed", linkname, retval); 3370Sstevel@tonic-gate return (-1); 3380Sstevel@tonic-gate } 3390Sstevel@tonic-gate 3403628Sss150715 if ((retval = dlpi_set_physaddr(dh, DL_CURR_PHYS_ADDR, physaddr, 3413628Sss150715 physaddrlen)) != DLPI_SUCCESS) { 3423628Sss150715 Perrdlpi("dlpi_set_physaddr failed", linkname, retval); 3433628Sss150715 dlpi_close(dh); 3440Sstevel@tonic-gate return (-1); 3450Sstevel@tonic-gate } 3460Sstevel@tonic-gate 3473628Sss150715 dlpi_close(dh); 3480Sstevel@tonic-gate return (0); 3490Sstevel@tonic-gate } 3500Sstevel@tonic-gate 3510Sstevel@tonic-gate void 3523628Sss150715 dlpi_print_address(const char *linkname) 3530Sstevel@tonic-gate { 3543628Sss150715 uint_t physaddrlen = DLPI_PHYSADDR_MAX; 3553628Sss150715 uchar_t physaddr[DLPI_PHYSADDR_MAX]; 3563628Sss150715 char *str; 3573628Sss150715 int retv; 3583628Sss150715 dlpi_handle_t dh; 3593628Sss150715 dlpi_info_t dlinfo; 3600Sstevel@tonic-gate 3613628Sss150715 if (dlpi_open(linkname, &dh, 0) != DLPI_SUCCESS) { 3620Sstevel@tonic-gate /* Do not report an error */ 3630Sstevel@tonic-gate return; 3640Sstevel@tonic-gate } 3650Sstevel@tonic-gate 3663628Sss150715 retv = dlpi_get_physaddr(dh, DL_CURR_PHYS_ADDR, physaddr, &physaddrlen); 3673628Sss150715 if (retv != DLPI_SUCCESS) { 3683628Sss150715 Perrdlpi("dlpi_get_physaddr failed", linkname, retv); 3693628Sss150715 dlpi_close(dh); 37013Syw138387 return; 3710Sstevel@tonic-gate } 3720Sstevel@tonic-gate 3733628Sss150715 retv = dlpi_info(dh, &dlinfo, 0); 3743628Sss150715 if (retv != DLPI_SUCCESS) { 3753628Sss150715 Perrdlpi("dlpi_info failed", linkname, retv); 3763628Sss150715 dlpi_close(dh); 3773628Sss150715 return; 3780Sstevel@tonic-gate } 3793628Sss150715 dlpi_close(dh); 3800Sstevel@tonic-gate 3813628Sss150715 str = _link_ntoa(physaddr, NULL, physaddrlen, IFT_OTHER); 3820Sstevel@tonic-gate 383*6353Sdr146992 if (str != NULL && physaddrlen != 0) { 3843628Sss150715 switch (dlinfo.di_mactype) { 3850Sstevel@tonic-gate case DL_IB: 3860Sstevel@tonic-gate (void) printf("\tipib %s \n", str); 3870Sstevel@tonic-gate break; 3880Sstevel@tonic-gate default: 3890Sstevel@tonic-gate (void) printf("\tether %s \n", str); 3900Sstevel@tonic-gate break; 3910Sstevel@tonic-gate } 3920Sstevel@tonic-gate free(str); 3930Sstevel@tonic-gate } 3940Sstevel@tonic-gate } 395