1*0Sstevel@tonic-gate /* 2*0Sstevel@tonic-gate * CDDL HEADER START 3*0Sstevel@tonic-gate * 4*0Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*0Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*0Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*0Sstevel@tonic-gate * with the License. 8*0Sstevel@tonic-gate * 9*0Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*0Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*0Sstevel@tonic-gate * See the License for the specific language governing permissions 12*0Sstevel@tonic-gate * and limitations under the License. 13*0Sstevel@tonic-gate * 14*0Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*0Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*0Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*0Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*0Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*0Sstevel@tonic-gate * 20*0Sstevel@tonic-gate * CDDL HEADER END 21*0Sstevel@tonic-gate */ 22*0Sstevel@tonic-gate /* 23*0Sstevel@tonic-gate * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 24*0Sstevel@tonic-gate * Use is subject to license terms. 25*0Sstevel@tonic-gate */ 26*0Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 27*0Sstevel@tonic-gate /* All Rights Reserved */ 28*0Sstevel@tonic-gate 29*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 30*0Sstevel@tonic-gate 31*0Sstevel@tonic-gate #include "defs.h" 32*0Sstevel@tonic-gate #include "ifconfig.h" 33*0Sstevel@tonic-gate #include <sys/types.h> 34*0Sstevel@tonic-gate #include <sys/dlpi.h> 35*0Sstevel@tonic-gate #include <libdlpi.h> 36*0Sstevel@tonic-gate #include <sys/sysmacros.h> 37*0Sstevel@tonic-gate #include <deflt.h> 38*0Sstevel@tonic-gate 39*0Sstevel@tonic-gate #define IPADDRL sizeof (struct in_addr) 40*0Sstevel@tonic-gate #define RARPRETRIES 5 41*0Sstevel@tonic-gate 42*0Sstevel@tonic-gate /* 43*0Sstevel@tonic-gate * The following value (8) is determined to work reliably in switched 10/100MB 44*0Sstevel@tonic-gate * ethernet environments. Use caution if you plan on decreasing it. 45*0Sstevel@tonic-gate */ 46*0Sstevel@tonic-gate #define RARPTIMEOUT 8 47*0Sstevel@tonic-gate 48*0Sstevel@tonic-gate static char defaultfile[] = "/etc/inet/rarp"; 49*0Sstevel@tonic-gate static char retries_var[] = "RARP_RETRIES="; 50*0Sstevel@tonic-gate static int rarp_timeout = RARPTIMEOUT; 51*0Sstevel@tonic-gate static int rarp_retries = RARPRETRIES; 52*0Sstevel@tonic-gate 53*0Sstevel@tonic-gate static int rarp_write(int, struct arphdr *, uchar_t *, size_t, size_t); 54*0Sstevel@tonic-gate static int rarp_open(char *, t_uscalar_t, size_t *, uchar_t **, 55*0Sstevel@tonic-gate uchar_t **); 56*0Sstevel@tonic-gate 57*0Sstevel@tonic-gate /* ARGSUSED */ 58*0Sstevel@tonic-gate int 59*0Sstevel@tonic-gate doifrevarp(char *ifname, struct sockaddr_in *laddr) 60*0Sstevel@tonic-gate { 61*0Sstevel@tonic-gate int if_fd; 62*0Sstevel@tonic-gate struct pollfd pfd; 63*0Sstevel@tonic-gate int s, flags, ret; 64*0Sstevel@tonic-gate char *ctlbuf, *databuf, *cause; 65*0Sstevel@tonic-gate struct strbuf ctl, data; 66*0Sstevel@tonic-gate struct arphdr *req, *ans; 67*0Sstevel@tonic-gate struct in_addr from; 68*0Sstevel@tonic-gate struct in_addr answer; 69*0Sstevel@tonic-gate union DL_primitives *dlp; 70*0Sstevel@tonic-gate struct lifreq lifr; 71*0Sstevel@tonic-gate struct timeval senttime; 72*0Sstevel@tonic-gate struct timeval currenttime; 73*0Sstevel@tonic-gate int waittime; 74*0Sstevel@tonic-gate int tries_left; 75*0Sstevel@tonic-gate size_t ifaddrlen, ifrarplen; 76*0Sstevel@tonic-gate uchar_t *my_macaddr = NULL, *my_broadcast = NULL; 77*0Sstevel@tonic-gate 78*0Sstevel@tonic-gate 79*0Sstevel@tonic-gate if (ifname[0] == '\0') { 80*0Sstevel@tonic-gate (void) fprintf(stderr, "ifconfig: doifrevarp: name not set\n"); 81*0Sstevel@tonic-gate exit(1); 82*0Sstevel@tonic-gate } 83*0Sstevel@tonic-gate 84*0Sstevel@tonic-gate if (debug) 85*0Sstevel@tonic-gate (void) printf("doifrevarp interface %s\n", ifname); 86*0Sstevel@tonic-gate 87*0Sstevel@tonic-gate if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { 88*0Sstevel@tonic-gate Perror0_exit("socket"); 89*0Sstevel@tonic-gate } 90*0Sstevel@tonic-gate (void) strncpy(lifr.lifr_name, ifname, sizeof (lifr.lifr_name)); 91*0Sstevel@tonic-gate if (ioctl(s, SIOCGLIFFLAGS, (char *)&lifr) < 0) 92*0Sstevel@tonic-gate Perror0_exit("SIOCGLIFFLAGS"); 93*0Sstevel@tonic-gate 94*0Sstevel@tonic-gate /* don't try to revarp if we know it won't work */ 95*0Sstevel@tonic-gate if ((lifr.lifr_flags & IFF_LOOPBACK) || 96*0Sstevel@tonic-gate (lifr.lifr_flags & IFF_NOARP) || 97*0Sstevel@tonic-gate (lifr.lifr_flags & IFF_POINTOPOINT)) 98*0Sstevel@tonic-gate return (0); 99*0Sstevel@tonic-gate 100*0Sstevel@tonic-gate /* open rarp interface */ 101*0Sstevel@tonic-gate if_fd = rarp_open(ifname, ETHERTYPE_REVARP, &ifaddrlen, &my_macaddr, 102*0Sstevel@tonic-gate &my_broadcast); 103*0Sstevel@tonic-gate if (if_fd < 0) 104*0Sstevel@tonic-gate return (0); 105*0Sstevel@tonic-gate 106*0Sstevel@tonic-gate /* 107*0Sstevel@tonic-gate * RARP looks at /etc/ethers and NIS, which only works 108*0Sstevel@tonic-gate * with 6 byte addresses currently. 109*0Sstevel@tonic-gate */ 110*0Sstevel@tonic-gate if (ifaddrlen != ETHERADDRL) { 111*0Sstevel@tonic-gate (void) close(if_fd); 112*0Sstevel@tonic-gate free(my_macaddr); 113*0Sstevel@tonic-gate free(my_broadcast); 114*0Sstevel@tonic-gate return (0); 115*0Sstevel@tonic-gate } 116*0Sstevel@tonic-gate 117*0Sstevel@tonic-gate ifrarplen = sizeof (struct arphdr) + (2 * IPADDRL) + (2 * ifaddrlen); 118*0Sstevel@tonic-gate 119*0Sstevel@tonic-gate /* look for adjustments to rarp_retries in the RARP defaults file */ 120*0Sstevel@tonic-gate if (defopen(defaultfile) == 0) { 121*0Sstevel@tonic-gate char *cp; 122*0Sstevel@tonic-gate 123*0Sstevel@tonic-gate if (cp = defread(retries_var)) { 124*0Sstevel@tonic-gate int ntries; 125*0Sstevel@tonic-gate 126*0Sstevel@tonic-gate ntries = atoi(cp); 127*0Sstevel@tonic-gate if (ntries > 0) 128*0Sstevel@tonic-gate rarp_retries = ntries; 129*0Sstevel@tonic-gate } 130*0Sstevel@tonic-gate (void) defopen(NULL); /* close default file */ 131*0Sstevel@tonic-gate } 132*0Sstevel@tonic-gate 133*0Sstevel@tonic-gate /* allocate request and response buffers */ 134*0Sstevel@tonic-gate if (((req = (struct arphdr *)malloc(ifrarplen)) == NULL) || 135*0Sstevel@tonic-gate ((ans = (struct arphdr *)malloc(ifrarplen)) == NULL)) { 136*0Sstevel@tonic-gate (void) close(if_fd); 137*0Sstevel@tonic-gate free(req); 138*0Sstevel@tonic-gate free(my_macaddr); 139*0Sstevel@tonic-gate free(my_broadcast); 140*0Sstevel@tonic-gate return (0); 141*0Sstevel@tonic-gate } 142*0Sstevel@tonic-gate 143*0Sstevel@tonic-gate /* create rarp request */ 144*0Sstevel@tonic-gate (void) memset(req, 0, ifrarplen); 145*0Sstevel@tonic-gate req->ar_hrd = htons(ARPHRD_ETHER); 146*0Sstevel@tonic-gate req->ar_pro = htons(ETHERTYPE_IP); 147*0Sstevel@tonic-gate req->ar_hln = ifaddrlen; 148*0Sstevel@tonic-gate req->ar_pln = IPADDRL; 149*0Sstevel@tonic-gate req->ar_op = htons(REVARP_REQUEST); 150*0Sstevel@tonic-gate 151*0Sstevel@tonic-gate (void) memcpy((uchar_t *)req + sizeof (struct arphdr), my_macaddr, 152*0Sstevel@tonic-gate ifaddrlen); 153*0Sstevel@tonic-gate (void) memcpy((uchar_t *)req + sizeof (struct arphdr) + IPADDRL + 154*0Sstevel@tonic-gate ifaddrlen, my_macaddr, ifaddrlen); 155*0Sstevel@tonic-gate 156*0Sstevel@tonic-gate tries_left = rarp_retries; 157*0Sstevel@tonic-gate rarp_retry: 158*0Sstevel@tonic-gate /* send the request */ 159*0Sstevel@tonic-gate if (rarp_write(if_fd, req, my_broadcast, ifaddrlen, ifrarplen) < 0) 160*0Sstevel@tonic-gate goto fail; 161*0Sstevel@tonic-gate 162*0Sstevel@tonic-gate gettimeofday(&senttime, NULL); 163*0Sstevel@tonic-gate 164*0Sstevel@tonic-gate if (debug) 165*0Sstevel@tonic-gate (void) printf("rarp sent\n"); 166*0Sstevel@tonic-gate 167*0Sstevel@tonic-gate 168*0Sstevel@tonic-gate /* read the answers */ 169*0Sstevel@tonic-gate if ((databuf = malloc(BUFSIZ)) == NULL) { 170*0Sstevel@tonic-gate (void) fprintf(stderr, "ifconfig: malloc() failed\n"); 171*0Sstevel@tonic-gate goto fail; 172*0Sstevel@tonic-gate } 173*0Sstevel@tonic-gate if ((ctlbuf = malloc(BUFSIZ)) == NULL) { 174*0Sstevel@tonic-gate (void) fprintf(stderr, "ifconfig: malloc() failed\n"); 175*0Sstevel@tonic-gate goto fail; 176*0Sstevel@tonic-gate } 177*0Sstevel@tonic-gate for (;;) { 178*0Sstevel@tonic-gate ctl.len = 0; 179*0Sstevel@tonic-gate ctl.maxlen = BUFSIZ; 180*0Sstevel@tonic-gate ctl.buf = ctlbuf; 181*0Sstevel@tonic-gate data.len = 0; 182*0Sstevel@tonic-gate data.maxlen = BUFSIZ; 183*0Sstevel@tonic-gate data.buf = databuf; 184*0Sstevel@tonic-gate flags = 0; 185*0Sstevel@tonic-gate 186*0Sstevel@tonic-gate /* 187*0Sstevel@tonic-gate * Check to see when the packet was last sent. 188*0Sstevel@tonic-gate * If we have not sent a packet in the last 189*0Sstevel@tonic-gate * RARP_TIMEOUT seconds, we should send one now. 190*0Sstevel@tonic-gate * Note that if some other host on the network is 191*0Sstevel@tonic-gate * sending a broadcast packet, poll will return and we 192*0Sstevel@tonic-gate * will find out that it does not match the reply we 193*0Sstevel@tonic-gate * are waiting for and then go back to poll. If the 194*0Sstevel@tonic-gate * frequency of such packets is > rarp_timeout, we don't 195*0Sstevel@tonic-gate * want to just go back to poll. We should send out one 196*0Sstevel@tonic-gate * more RARP request before blocking in poll. 197*0Sstevel@tonic-gate */ 198*0Sstevel@tonic-gate 199*0Sstevel@tonic-gate gettimeofday(¤ttime, NULL); 200*0Sstevel@tonic-gate waittime = rarp_timeout - 201*0Sstevel@tonic-gate (currenttime.tv_sec - senttime.tv_sec); 202*0Sstevel@tonic-gate 203*0Sstevel@tonic-gate if (waittime <= 0) { 204*0Sstevel@tonic-gate if (--tries_left > 0) { 205*0Sstevel@tonic-gate if (debug) 206*0Sstevel@tonic-gate (void) printf("rarp retry\n"); 207*0Sstevel@tonic-gate goto rarp_retry; 208*0Sstevel@tonic-gate } else { 209*0Sstevel@tonic-gate if (debug) 210*0Sstevel@tonic-gate (void) printf("rarp timeout\n"); 211*0Sstevel@tonic-gate goto fail; 212*0Sstevel@tonic-gate } 213*0Sstevel@tonic-gate } 214*0Sstevel@tonic-gate 215*0Sstevel@tonic-gate /* start RARP reply timeout */ 216*0Sstevel@tonic-gate pfd.fd = if_fd; 217*0Sstevel@tonic-gate pfd.events = POLLIN | POLLRDNORM | POLLRDBAND | POLLPRI; 218*0Sstevel@tonic-gate if ((ret = poll(&pfd, 1, waittime * 1000)) == 0) { 219*0Sstevel@tonic-gate if (--tries_left > 0) { 220*0Sstevel@tonic-gate if (debug) 221*0Sstevel@tonic-gate (void) printf("rarp retry\n"); 222*0Sstevel@tonic-gate goto rarp_retry; 223*0Sstevel@tonic-gate } else { 224*0Sstevel@tonic-gate if (debug) 225*0Sstevel@tonic-gate (void) printf("rarp timeout\n"); 226*0Sstevel@tonic-gate goto fail; 227*0Sstevel@tonic-gate } 228*0Sstevel@tonic-gate } else if (ret == -1) { 229*0Sstevel@tonic-gate perror("ifconfig: RARP reply poll"); 230*0Sstevel@tonic-gate goto fail; 231*0Sstevel@tonic-gate } 232*0Sstevel@tonic-gate 233*0Sstevel@tonic-gate /* poll returned > 0 for this fd so getmsg should not block */ 234*0Sstevel@tonic-gate if ((ret = getmsg(if_fd, &ctl, &data, &flags)) < 0) { 235*0Sstevel@tonic-gate perror("ifconfig: RARP reply getmsg"); 236*0Sstevel@tonic-gate goto fail; 237*0Sstevel@tonic-gate } 238*0Sstevel@tonic-gate 239*0Sstevel@tonic-gate if (debug) { 240*0Sstevel@tonic-gate (void) printf("rarp: ret[%d] ctl.len[%d] data.len[%d] " 241*0Sstevel@tonic-gate "flags[%d]\n", ret, ctl.len, data.len, flags); 242*0Sstevel@tonic-gate } 243*0Sstevel@tonic-gate /* Validate DL_UNITDATA_IND. */ 244*0Sstevel@tonic-gate /* LINTED: malloc returns a pointer aligned for any use */ 245*0Sstevel@tonic-gate dlp = (union DL_primitives *)ctlbuf; 246*0Sstevel@tonic-gate if (debug) { 247*0Sstevel@tonic-gate (void) printf("rarp: dl_primitive[%lu]\n", 248*0Sstevel@tonic-gate dlp->dl_primitive); 249*0Sstevel@tonic-gate if (dlp->dl_primitive == DL_ERROR_ACK) { 250*0Sstevel@tonic-gate (void) printf( 251*0Sstevel@tonic-gate "rarp: err ak: dl_errno %lu errno %lu\n", 252*0Sstevel@tonic-gate dlp->error_ack.dl_errno, 253*0Sstevel@tonic-gate dlp->error_ack.dl_unix_errno); 254*0Sstevel@tonic-gate } 255*0Sstevel@tonic-gate if (dlp->dl_primitive == DL_UDERROR_IND) { 256*0Sstevel@tonic-gate (void) printf("rarp: ud err: err[%lu] len[%lu] " 257*0Sstevel@tonic-gate "off[%lu]\n", 258*0Sstevel@tonic-gate dlp->uderror_ind.dl_errno, 259*0Sstevel@tonic-gate dlp->uderror_ind.dl_dest_addr_length, 260*0Sstevel@tonic-gate dlp->uderror_ind.dl_dest_addr_offset); 261*0Sstevel@tonic-gate } 262*0Sstevel@tonic-gate } 263*0Sstevel@tonic-gate (void) memcpy(ans, databuf, ifrarplen); 264*0Sstevel@tonic-gate cause = NULL; 265*0Sstevel@tonic-gate if (ret & MORECTL) 266*0Sstevel@tonic-gate cause = "MORECTL flag"; 267*0Sstevel@tonic-gate else if (ret & MOREDATA) 268*0Sstevel@tonic-gate cause = "MOREDATA flag"; 269*0Sstevel@tonic-gate else if (ctl.len == 0) 270*0Sstevel@tonic-gate cause = "missing control part of message"; 271*0Sstevel@tonic-gate else if (ctl.len < 0) 272*0Sstevel@tonic-gate cause = "short control part of message"; 273*0Sstevel@tonic-gate else if (dlp->dl_primitive != DL_UNITDATA_IND) 274*0Sstevel@tonic-gate cause = "not unitdata_ind"; 275*0Sstevel@tonic-gate else if (ctl.len < DL_UNITDATA_IND_SIZE) 276*0Sstevel@tonic-gate cause = "short unitdata_ind"; 277*0Sstevel@tonic-gate 278*0Sstevel@tonic-gate else if (data.len < ifrarplen) 279*0Sstevel@tonic-gate cause = "short arp"; 280*0Sstevel@tonic-gate else if (ans->ar_hrd != htons(ARPHRD_ETHER)) 281*0Sstevel@tonic-gate cause = "hrd"; 282*0Sstevel@tonic-gate else if (ans->ar_pro != htons(ETHERTYPE_IP)) 283*0Sstevel@tonic-gate cause = "pro"; 284*0Sstevel@tonic-gate else if (ans->ar_hln != ifaddrlen) 285*0Sstevel@tonic-gate cause = "hln"; 286*0Sstevel@tonic-gate else if (ans->ar_pln != IPADDRL) 287*0Sstevel@tonic-gate cause = "pln"; 288*0Sstevel@tonic-gate if (cause) { 289*0Sstevel@tonic-gate (void) fprintf(stderr, 290*0Sstevel@tonic-gate "sanity check failed; cause: %s\n", cause); 291*0Sstevel@tonic-gate continue; 292*0Sstevel@tonic-gate } 293*0Sstevel@tonic-gate 294*0Sstevel@tonic-gate switch (ntohs(ans->ar_op)) { 295*0Sstevel@tonic-gate case ARPOP_REQUEST: 296*0Sstevel@tonic-gate if (debug) 297*0Sstevel@tonic-gate (void) printf("Got an arp request\n"); 298*0Sstevel@tonic-gate break; 299*0Sstevel@tonic-gate 300*0Sstevel@tonic-gate case ARPOP_REPLY: 301*0Sstevel@tonic-gate if (debug) 302*0Sstevel@tonic-gate (void) printf("Got an arp reply.\n"); 303*0Sstevel@tonic-gate break; 304*0Sstevel@tonic-gate 305*0Sstevel@tonic-gate case REVARP_REQUEST: 306*0Sstevel@tonic-gate if (debug) 307*0Sstevel@tonic-gate (void) printf("Got a rarp request.\n"); 308*0Sstevel@tonic-gate break; 309*0Sstevel@tonic-gate 310*0Sstevel@tonic-gate case REVARP_REPLY: 311*0Sstevel@tonic-gate 312*0Sstevel@tonic-gate (void) memcpy(&answer, (uchar_t *)ans + 313*0Sstevel@tonic-gate sizeof (struct arphdr) + (2 * ifaddrlen) + 314*0Sstevel@tonic-gate IPADDRL, sizeof (answer)); 315*0Sstevel@tonic-gate (void) memcpy(&from, (uchar_t *)ans + 316*0Sstevel@tonic-gate sizeof (struct arphdr) + ifaddrlen, sizeof (from)); 317*0Sstevel@tonic-gate if (debug) { 318*0Sstevel@tonic-gate (void) printf("answer: %s", inet_ntoa(answer)); 319*0Sstevel@tonic-gate (void) printf(" [from %s]\n", inet_ntoa(from)); 320*0Sstevel@tonic-gate } 321*0Sstevel@tonic-gate laddr->sin_addr = answer; 322*0Sstevel@tonic-gate (void) close(if_fd); 323*0Sstevel@tonic-gate free(req); 324*0Sstevel@tonic-gate free(ans); 325*0Sstevel@tonic-gate free(my_macaddr); 326*0Sstevel@tonic-gate free(my_broadcast); 327*0Sstevel@tonic-gate return (1); 328*0Sstevel@tonic-gate 329*0Sstevel@tonic-gate default: 330*0Sstevel@tonic-gate (void) fprintf(stderr, 331*0Sstevel@tonic-gate "ifconfig: unknown opcode 0x%xd\n", ans->ar_op); 332*0Sstevel@tonic-gate break; 333*0Sstevel@tonic-gate } 334*0Sstevel@tonic-gate } 335*0Sstevel@tonic-gate /* NOTREACHED */ 336*0Sstevel@tonic-gate fail: 337*0Sstevel@tonic-gate (void) close(if_fd); 338*0Sstevel@tonic-gate free(req); 339*0Sstevel@tonic-gate free(ans); 340*0Sstevel@tonic-gate free(my_macaddr); 341*0Sstevel@tonic-gate free(my_broadcast); 342*0Sstevel@tonic-gate return (0); 343*0Sstevel@tonic-gate } 344*0Sstevel@tonic-gate 345*0Sstevel@tonic-gate /* 346*0Sstevel@tonic-gate * Open the datalink provider device and bind to the REVARP type. 347*0Sstevel@tonic-gate * Return the resulting descriptor. 348*0Sstevel@tonic-gate */ 349*0Sstevel@tonic-gate static int 350*0Sstevel@tonic-gate rarp_open(char *ifname, t_uscalar_t type, size_t *alen, uchar_t **myaddr, 351*0Sstevel@tonic-gate uchar_t **mybaddr) 352*0Sstevel@tonic-gate { 353*0Sstevel@tonic-gate int fd, len; 354*0Sstevel@tonic-gate char *str; 355*0Sstevel@tonic-gate dl_info_ack_t dlinfo; 356*0Sstevel@tonic-gate dlpi_if_attr_t dia; 357*0Sstevel@tonic-gate int i; 358*0Sstevel@tonic-gate 359*0Sstevel@tonic-gate if (debug) 360*0Sstevel@tonic-gate (void) printf("rarp_open %s\n", ifname); 361*0Sstevel@tonic-gate 362*0Sstevel@tonic-gate fd = dlpi_if_open(ifname, &dia, _B_FALSE); 363*0Sstevel@tonic-gate if (fd < 0) { 364*0Sstevel@tonic-gate (void) fprintf(stderr, "ifconfig: could not open device for " 365*0Sstevel@tonic-gate "%s\n", ifname); 366*0Sstevel@tonic-gate return (-1); 367*0Sstevel@tonic-gate } 368*0Sstevel@tonic-gate 369*0Sstevel@tonic-gate if (dlpi_info(fd, -1, &dlinfo, NULL, NULL, NULL, NULL, NULL, 370*0Sstevel@tonic-gate NULL) < 0) { 371*0Sstevel@tonic-gate (void) fprintf(stderr, "ifconfig: info req failed\n"); 372*0Sstevel@tonic-gate goto failed; 373*0Sstevel@tonic-gate } 374*0Sstevel@tonic-gate 375*0Sstevel@tonic-gate if ((*mybaddr = malloc(dlinfo.dl_brdcst_addr_length)) == NULL) { 376*0Sstevel@tonic-gate (void) fprintf(stderr, "rarp_open: malloc() failed\n"); 377*0Sstevel@tonic-gate goto failed; 378*0Sstevel@tonic-gate } 379*0Sstevel@tonic-gate 380*0Sstevel@tonic-gate if (dlpi_info(fd, -1, &dlinfo, NULL, NULL, NULL, NULL, *mybaddr, 381*0Sstevel@tonic-gate NULL) < 0) { 382*0Sstevel@tonic-gate (void) fprintf(stderr, "ifconfig: info req failed\n"); 383*0Sstevel@tonic-gate goto failed; 384*0Sstevel@tonic-gate } 385*0Sstevel@tonic-gate 386*0Sstevel@tonic-gate if (debug) { 387*0Sstevel@tonic-gate (void) printf("broadcast addr: "); 388*0Sstevel@tonic-gate for (i = 0; i < dlinfo.dl_brdcst_addr_length; i++) 389*0Sstevel@tonic-gate (void) printf("%02x", (*mybaddr)[i]); 390*0Sstevel@tonic-gate (void) printf("\n"); 391*0Sstevel@tonic-gate } 392*0Sstevel@tonic-gate 393*0Sstevel@tonic-gate len = *alen = dlinfo.dl_addr_length - abs(dlinfo.dl_sap_length); 394*0Sstevel@tonic-gate 395*0Sstevel@tonic-gate if (debug) 396*0Sstevel@tonic-gate (void) printf("rarp_open: addr length = %d\n", len); 397*0Sstevel@tonic-gate 398*0Sstevel@tonic-gate if ((*myaddr = malloc(len)) == NULL) { 399*0Sstevel@tonic-gate (void) fprintf(stderr, "rarp_open: malloc() failed\n"); 400*0Sstevel@tonic-gate goto failed; 401*0Sstevel@tonic-gate } 402*0Sstevel@tonic-gate 403*0Sstevel@tonic-gate if (dlpi_bind(fd, -1, type, DL_CLDLS, _B_FALSE, NULL, NULL, 404*0Sstevel@tonic-gate *myaddr, NULL) < 0) { 405*0Sstevel@tonic-gate (void) fprintf(stderr, "rarp_open: dlpi_bind failed\n"); 406*0Sstevel@tonic-gate goto failed; 407*0Sstevel@tonic-gate } 408*0Sstevel@tonic-gate 409*0Sstevel@tonic-gate if (debug) { 410*0Sstevel@tonic-gate str = _link_ntoa(*myaddr, str, len, IFT_OTHER); 411*0Sstevel@tonic-gate if (str != NULL) { 412*0Sstevel@tonic-gate (void) printf("device %s mac address %s\n", 413*0Sstevel@tonic-gate ifname, str); 414*0Sstevel@tonic-gate free(str); 415*0Sstevel@tonic-gate } 416*0Sstevel@tonic-gate } 417*0Sstevel@tonic-gate 418*0Sstevel@tonic-gate return (fd); 419*0Sstevel@tonic-gate 420*0Sstevel@tonic-gate failed: 421*0Sstevel@tonic-gate (void) dlpi_close(fd); 422*0Sstevel@tonic-gate free(*mybaddr); 423*0Sstevel@tonic-gate free(*myaddr); 424*0Sstevel@tonic-gate return (-1); 425*0Sstevel@tonic-gate } 426*0Sstevel@tonic-gate 427*0Sstevel@tonic-gate static int 428*0Sstevel@tonic-gate rarp_write(int fd, struct arphdr *ahdr, uchar_t *dhost, size_t maclen, 429*0Sstevel@tonic-gate size_t rarplen) 430*0Sstevel@tonic-gate { 431*0Sstevel@tonic-gate struct strbuf ctl, data; 432*0Sstevel@tonic-gate union DL_primitives *dlp; 433*0Sstevel@tonic-gate char *ctlbuf; 434*0Sstevel@tonic-gate int ret; 435*0Sstevel@tonic-gate ushort_t etype = ETHERTYPE_REVARP; 436*0Sstevel@tonic-gate 437*0Sstevel@tonic-gate /* 438*0Sstevel@tonic-gate * Construct DL_UNITDATA_REQ. Allocate at least BUFSIZ bytes. 439*0Sstevel@tonic-gate */ 440*0Sstevel@tonic-gate ctl.len = DL_UNITDATA_REQ_SIZE + maclen + sizeof (ushort_t); 441*0Sstevel@tonic-gate if ((ctl.buf = ctlbuf = malloc(ctl.len)) == NULL) { 442*0Sstevel@tonic-gate (void) fprintf(stderr, "ifconfig: malloc() failed\n"); 443*0Sstevel@tonic-gate return (-1); 444*0Sstevel@tonic-gate } 445*0Sstevel@tonic-gate /* LINTED: malloc returns a pointer aligned for any use */ 446*0Sstevel@tonic-gate dlp = (union DL_primitives *)ctlbuf; 447*0Sstevel@tonic-gate dlp->unitdata_req.dl_primitive = DL_UNITDATA_REQ; 448*0Sstevel@tonic-gate dlp->unitdata_req.dl_dest_addr_length = maclen + sizeof (ushort_t); 449*0Sstevel@tonic-gate dlp->unitdata_req.dl_dest_addr_offset = DL_UNITDATA_REQ_SIZE; 450*0Sstevel@tonic-gate dlp->unitdata_req.dl_priority.dl_min = 0; 451*0Sstevel@tonic-gate dlp->unitdata_req.dl_priority.dl_max = 0; 452*0Sstevel@tonic-gate 453*0Sstevel@tonic-gate /* 454*0Sstevel@tonic-gate * XXX FIXME Assumes a specific DLPI address format. 455*0Sstevel@tonic-gate */ 456*0Sstevel@tonic-gate (void) memcpy(ctlbuf + DL_UNITDATA_REQ_SIZE, dhost, maclen); 457*0Sstevel@tonic-gate (void) memcpy(ctlbuf + DL_UNITDATA_REQ_SIZE + maclen, &etype, 458*0Sstevel@tonic-gate sizeof (etype)); 459*0Sstevel@tonic-gate 460*0Sstevel@tonic-gate /* Send DL_UNITDATA_REQ. */ 461*0Sstevel@tonic-gate data.len = rarplen; 462*0Sstevel@tonic-gate data.buf = (char *)ahdr; 463*0Sstevel@tonic-gate ret = putmsg(fd, &ctl, &data, 0); 464*0Sstevel@tonic-gate free(ctlbuf); 465*0Sstevel@tonic-gate return (ret); 466*0Sstevel@tonic-gate } 467*0Sstevel@tonic-gate 468*0Sstevel@tonic-gate int 469*0Sstevel@tonic-gate dlpi_set_address(char *ifname, uchar_t *ea, int length) 470*0Sstevel@tonic-gate { 471*0Sstevel@tonic-gate int fd; 472*0Sstevel@tonic-gate dlpi_if_attr_t dia; 473*0Sstevel@tonic-gate 474*0Sstevel@tonic-gate fd = dlpi_if_open(ifname, &dia, _B_FALSE); 475*0Sstevel@tonic-gate if (fd < 0) { 476*0Sstevel@tonic-gate (void) fprintf(stderr, "ifconfig: could not open device for " 477*0Sstevel@tonic-gate "%s\n", ifname); 478*0Sstevel@tonic-gate return (-1); 479*0Sstevel@tonic-gate } 480*0Sstevel@tonic-gate 481*0Sstevel@tonic-gate if (dlpi_set_phys_addr(fd, -1, ea, length) < 0) { 482*0Sstevel@tonic-gate (void) dlpi_close(fd); 483*0Sstevel@tonic-gate return (-1); 484*0Sstevel@tonic-gate } 485*0Sstevel@tonic-gate 486*0Sstevel@tonic-gate (void) dlpi_close(fd); 487*0Sstevel@tonic-gate return (0); 488*0Sstevel@tonic-gate } 489*0Sstevel@tonic-gate 490*0Sstevel@tonic-gate void 491*0Sstevel@tonic-gate dlpi_print_address(char *ifname) 492*0Sstevel@tonic-gate { 493*0Sstevel@tonic-gate int fd, len; 494*0Sstevel@tonic-gate uchar_t *laddr; 495*0Sstevel@tonic-gate dl_info_ack_t dl_info; 496*0Sstevel@tonic-gate char *str = NULL; 497*0Sstevel@tonic-gate dlpi_if_attr_t dia; 498*0Sstevel@tonic-gate 499*0Sstevel@tonic-gate fd = dlpi_if_open(ifname, &dia, _B_FALSE); 500*0Sstevel@tonic-gate if (fd < 0) { 501*0Sstevel@tonic-gate /* Do not report an error */ 502*0Sstevel@tonic-gate return; 503*0Sstevel@tonic-gate } 504*0Sstevel@tonic-gate 505*0Sstevel@tonic-gate if (dlpi_info(fd, -1, &dl_info, NULL, NULL, NULL, NULL, NULL, 506*0Sstevel@tonic-gate NULL) < 0) { 507*0Sstevel@tonic-gate (void) fprintf(stderr, "ifconfig: info req failed\n"); 508*0Sstevel@tonic-gate goto failed; 509*0Sstevel@tonic-gate } 510*0Sstevel@tonic-gate 511*0Sstevel@tonic-gate len = dl_info.dl_addr_length - abs(dl_info.dl_sap_length); 512*0Sstevel@tonic-gate if ((laddr = malloc(len)) == NULL) { 513*0Sstevel@tonic-gate goto failed; 514*0Sstevel@tonic-gate } 515*0Sstevel@tonic-gate 516*0Sstevel@tonic-gate if (dlpi_phys_addr(fd, -1, DL_CURR_PHYS_ADDR, laddr, NULL) < 0) { 517*0Sstevel@tonic-gate (void) fprintf(stderr, "ifconfig: phys_addr failed\n"); 518*0Sstevel@tonic-gate goto failed; 519*0Sstevel@tonic-gate } 520*0Sstevel@tonic-gate 521*0Sstevel@tonic-gate (void) dlpi_close(fd); 522*0Sstevel@tonic-gate str = _link_ntoa(laddr, str, len, IFT_OTHER); 523*0Sstevel@tonic-gate if (str != NULL) { 524*0Sstevel@tonic-gate switch (dl_info.dl_mac_type) { 525*0Sstevel@tonic-gate case DL_IB: 526*0Sstevel@tonic-gate (void) printf("\tipib %s \n", str); 527*0Sstevel@tonic-gate break; 528*0Sstevel@tonic-gate default: 529*0Sstevel@tonic-gate (void) printf("\tether %s \n", str); 530*0Sstevel@tonic-gate break; 531*0Sstevel@tonic-gate } 532*0Sstevel@tonic-gate free(str); 533*0Sstevel@tonic-gate } 534*0Sstevel@tonic-gate free(laddr); 535*0Sstevel@tonic-gate 536*0Sstevel@tonic-gate failed: 537*0Sstevel@tonic-gate free(laddr); 538*0Sstevel@tonic-gate (void) dlpi_close(fd); 539*0Sstevel@tonic-gate } 540