1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
26 /*	  All Rights Reserved	*/
27 
28 #include "defs.h"
29 #include "ifconfig.h"
30 #include <sys/types.h>
31 #include <libdlpi.h>
32 #include <sys/sysmacros.h>
33 #include <sys/time.h>
34 #include <deflt.h>
35 
36 #define	IPADDRL		sizeof (struct in_addr)
37 #define	RARPRETRIES	5
38 
39 /*
40  * The following value (8) is determined to work reliably in switched 10/100MB
41  * ethernet environments. Use caution if you plan on decreasing it.
42  */
43 #define	RARPTIMEOUT	8
44 
45 static char	defaultfile[] = "/etc/inet/rarp";
46 static char	retries_var[] = "RARP_RETRIES=";
47 static int rarp_timeout = RARPTIMEOUT;
48 static int rarp_retries = RARPRETRIES;
49 
50 static dlpi_handle_t rarp_open(const char *, size_t *, uchar_t *, uchar_t *);
51 static int rarp_recv(dlpi_handle_t, struct arphdr *, size_t, size_t, int64_t);
52 
53 int
doifrevarp(const char * linkname,struct sockaddr_in * laddr)54 doifrevarp(const char *linkname, struct sockaddr_in *laddr)
55 {
56 	int			s, retval;
57 	struct arphdr		*req, *ans;
58 	struct in_addr		from;
59 	struct in_addr		answer;
60 	struct lifreq		lifr;
61 	int			tries_left;
62 	size_t			physaddrlen, ifrarplen;
63 	uchar_t			my_macaddr[DLPI_PHYSADDR_MAX];
64 	uchar_t 		my_broadcast[DLPI_PHYSADDR_MAX];
65 	dlpi_handle_t		dh;
66 
67 	if (linkname[0] == '\0') {
68 		(void) fprintf(stderr, "ifconfig: doifrevarp: name not set\n");
69 		exit(1);
70 	}
71 
72 	if (debug)
73 		(void) printf("doifrevarp interface %s\n", linkname);
74 
75 	if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
76 		Perror0_exit("socket");
77 
78 	(void) strlcpy(lifr.lifr_name, linkname, sizeof (lifr.lifr_name));
79 	if (ioctl(s, SIOCGLIFFLAGS, (char *)&lifr) < 0) {
80 		(void) close(s);
81 		Perror0_exit("SIOCGLIFFLAGS");
82 	}
83 
84 	/* don't try to revarp if we know it won't work */
85 	if ((lifr.lifr_flags & IFF_LOOPBACK) ||
86 	    (lifr.lifr_flags & IFF_NOARP) ||
87 	    (lifr.lifr_flags & IFF_IPMP) ||
88 	    (lifr.lifr_flags & IFF_POINTOPOINT)) {
89 		(void) close(s);
90 		return (0);
91 	}
92 
93 	/* open rarp interface */
94 	dh = rarp_open(linkname, &physaddrlen, my_macaddr, my_broadcast);
95 	if (dh == NULL) {
96 		(void) close(s);
97 		return (0);
98 	}
99 
100 	/*
101 	 * RARP looks at /etc/ethers and NIS, which only works
102 	 * with 6 byte addresses currently.
103 	 */
104 	if (physaddrlen != ETHERADDRL) {
105 		dlpi_close(dh);
106 		(void) close(s);
107 		return (0);
108 	}
109 
110 	ifrarplen = sizeof (struct arphdr) + (2 * IPADDRL) + (2 * physaddrlen);
111 
112 	/* look for adjustments to rarp_retries in the RARP defaults file */
113 	if (defopen(defaultfile) == 0) {
114 		char	*cp;
115 
116 		if (cp = defread(retries_var)) {
117 			int	ntries;
118 
119 			ntries = atoi(cp);
120 			if (ntries > 0)
121 				rarp_retries = ntries;
122 		}
123 		(void) defopen(NULL);	/* close default file */
124 	}
125 
126 	/* allocate request and response buffers */
127 	if (((req = malloc(ifrarplen)) == NULL) ||
128 	    ((ans = malloc(ifrarplen)) == NULL)) {
129 		dlpi_close(dh);
130 		(void) close(s);
131 		free(req);
132 		return (0);
133 	}
134 
135 	/* create rarp request */
136 	(void) memset(req, 0, ifrarplen);
137 	req->ar_hrd = htons(ARPHRD_ETHER);
138 	req->ar_pro = htons(ETHERTYPE_IP);
139 	req->ar_hln = physaddrlen;
140 	req->ar_pln = IPADDRL;
141 	req->ar_op = htons(REVARP_REQUEST);
142 
143 	(void) memcpy(&req[1], my_macaddr, physaddrlen);
144 	(void) memcpy((uchar_t *)req + sizeof (struct arphdr) + IPADDRL +
145 	    physaddrlen, my_macaddr, physaddrlen);
146 
147 	for (tries_left = rarp_retries; tries_left > 0; --tries_left) {
148 		/* send the request */
149 		retval = dlpi_send(dh, my_broadcast, physaddrlen, req,
150 		    ifrarplen, NULL);
151 		if (retval != DLPI_SUCCESS) {
152 			Perrdlpi("doifrevarp: cannot send rarp request",
153 			    linkname, retval);
154 			break;
155 		}
156 
157 		if (debug)
158 			(void) printf("rarp sent\n");
159 
160 		retval = rarp_recv(dh, ans, ifrarplen, physaddrlen,
161 		    rarp_timeout * MILLISEC);
162 
163 		if (retval != DLPI_ETIMEDOUT)
164 			break;
165 
166 		if (debug)
167 			(void) printf("rarp retry\n");
168 	}
169 
170 	if (retval == DLPI_SUCCESS) {
171 		(void) memcpy(&answer, (uchar_t *)ans +
172 		    sizeof (struct arphdr) + (2 * physaddrlen) + IPADDRL,
173 		    sizeof (answer));
174 		(void) memcpy(&from, (uchar_t *)ans + physaddrlen +
175 		    sizeof (struct arphdr), sizeof (from));
176 
177 		if (debug) {
178 			(void) printf("answer: %s", inet_ntoa(answer));
179 			(void) printf(" [from %s]\n", inet_ntoa(from));
180 		}
181 		laddr->sin_addr = answer;
182 	} else if (debug) {
183 		Perrdlpi("doifrevarp: could not receive rarp reply",
184 		    linkname, retval);
185 	}
186 
187 	dlpi_close(dh);
188 	(void) close(s);
189 	free(req);
190 	free(ans);
191 	return (retval == DLPI_SUCCESS);
192 }
193 
194 /*
195  * Open the datalink provider device and bind to the REVARP type.
196  * Return the resulting DLPI handle.
197  */
198 static	dlpi_handle_t
rarp_open(const char * linkname,size_t * alen,uchar_t * myaddr,uchar_t * mybaddr)199 rarp_open(const char *linkname, size_t *alen, uchar_t *myaddr, uchar_t *mybaddr)
200 {
201 	int		retval;
202 	char		*physaddr, *bcastaddr;
203 	dlpi_info_t	dlinfo;
204 	dlpi_handle_t	dh;
205 
206 	if (debug)
207 		(void) printf("rarp_open %s\n", linkname);
208 
209 	if ((retval = dlpi_open(linkname, &dh, 0)) != DLPI_SUCCESS) {
210 		Perrdlpi("rarp_open: dlpi_open failed", linkname, retval);
211 		return (NULL);
212 	}
213 
214 	if ((retval = dlpi_bind(dh, ETHERTYPE_REVARP, NULL)) != DLPI_SUCCESS) {
215 		Perrdlpi("rarp_open: dlpi_bind failed", linkname, retval);
216 		goto failed;
217 	}
218 
219 	if ((retval = dlpi_info(dh, &dlinfo, 0)) != DLPI_SUCCESS) {
220 		Perrdlpi("rarp_open: dlpi_info failed", linkname, retval);
221 		goto failed;
222 	}
223 
224 	if (dlinfo.di_bcastaddrlen == 0) {
225 		(void) fprintf(stderr, "ifconfig: rarp_open: %s broadcast "
226 		    "not supported\n", linkname);
227 		goto failed;
228 	}
229 
230 	/* we assume the following are equal and fill in 'alen' */
231 	assert(dlinfo.di_bcastaddrlen == dlinfo.di_physaddrlen);
232 
233 	(void) memcpy(mybaddr, dlinfo.di_bcastaddr, dlinfo.di_bcastaddrlen);
234 
235 	*alen = dlinfo.di_physaddrlen;
236 
237 	(void) memcpy(myaddr, dlinfo.di_physaddr, dlinfo.di_physaddrlen);
238 
239 	if (debug) {
240 		bcastaddr = _link_ntoa(mybaddr, NULL, dlinfo.di_bcastaddrlen,
241 		    IFT_OTHER);
242 
243 		physaddr = _link_ntoa(myaddr, NULL, dlinfo.di_physaddrlen,
244 		    IFT_OTHER);
245 
246 		if (physaddr != NULL && bcastaddr != NULL) {
247 			(void) printf("device %s: broadcast address %s, mac "
248 			    "address %s\n", linkname, bcastaddr, physaddr);
249 		}
250 
251 		free(physaddr);
252 		free(bcastaddr);
253 
254 		(void) printf("rarp_open: addr length = %d\n",
255 		    dlinfo.di_physaddrlen);
256 	}
257 
258 	return (dh);
259 
260 failed:
261 	dlpi_close(dh);
262 	return (NULL);
263 }
264 
265 /*
266  * Read reply for RARP request. If a reply is received within waitms,
267  * validate the reply. If it is a correct RARP reply return DLPI_SUCCESS,
268  * otherwise return DLPI_ETIMEDOUT. If there is an error while reading retrun
269  * the error code.
270  */
271 static int
rarp_recv(dlpi_handle_t dh,struct arphdr * ans,size_t msglen,size_t physaddrlen,int64_t waitms)272 rarp_recv(dlpi_handle_t dh, struct arphdr *ans, size_t msglen,
273     size_t physaddrlen, int64_t waitms)
274 {
275 	int		retval;
276 	char		*cause;
277 	size_t		anslen = msglen;
278 	hrtime_t	endtime = gethrtime() + MSEC2NSEC(waitms);
279 	hrtime_t	currtime;
280 
281 	while ((currtime = gethrtime()) < endtime) {
282 		waitms = NSEC2MSEC(endtime - currtime);
283 		retval = dlpi_recv(dh, NULL, NULL, ans, &anslen, waitms, NULL);
284 		if (retval == DLPI_SUCCESS) {
285 			cause = NULL;
286 
287 			if (anslen < msglen)
288 				cause = "short packet";
289 			else if (ans->ar_hrd != htons(ARPHRD_ETHER))
290 				cause = "hardware type not Ethernet";
291 			else if (ans->ar_pro != htons(ETHERTYPE_IP))
292 				cause = "protocol type not IP";
293 			else if (ans->ar_hln != physaddrlen)
294 				cause = "unexpected hardware address length";
295 			else if (ans->ar_pln != IPADDRL)
296 				cause = "unexpected protocol address length";
297 			if (cause != NULL) {
298 				(void) fprintf(stderr, "RARP packet received "
299 				    "but discarded (%s)\n", cause);
300 				continue;
301 			}
302 			switch (ntohs(ans->ar_op)) {
303 			case REVARP_REQUEST:
304 				if (debug)
305 					(void) printf("Got a rarp request.\n");
306 				break;
307 
308 			case REVARP_REPLY:
309 				return (DLPI_SUCCESS);
310 
311 			default:
312 				(void) fprintf(stderr, "ifconfig: unknown "
313 				    "RARP opcode 0x%x\n", ans->ar_op);
314 				break;
315 			}
316 		} else if (retval != DLPI_ETIMEDOUT) {
317 			Perrdlpi("doifrevarp: dlpi_recv failed",
318 			    dlpi_linkname(dh), retval);
319 			return (retval);
320 		}
321 	}
322 
323 	return (DLPI_ETIMEDOUT);
324 }
325 
326 void
dlpi_print_address(const char * linkname)327 dlpi_print_address(const char *linkname)
328 {
329 	uint_t	physaddrlen = DLPI_PHYSADDR_MAX;
330 	uchar_t	physaddr[DLPI_PHYSADDR_MAX];
331 	char	*str;
332 	int	retv;
333 	dlpi_handle_t	dh;
334 	dlpi_info_t	dlinfo;
335 
336 	if (dlpi_open(linkname, &dh, 0) != DLPI_SUCCESS) {
337 		/* Do not report an error */
338 		return;
339 	}
340 
341 	retv = dlpi_get_physaddr(dh, DL_CURR_PHYS_ADDR, physaddr, &physaddrlen);
342 	if (retv != DLPI_SUCCESS) {
343 		Perrdlpi("dlpi_get_physaddr failed", linkname, retv);
344 		dlpi_close(dh);
345 		return;
346 	}
347 
348 	retv = dlpi_info(dh, &dlinfo, 0);
349 	if (retv != DLPI_SUCCESS) {
350 		Perrdlpi("dlpi_info failed", linkname, retv);
351 		dlpi_close(dh);
352 		return;
353 	}
354 	dlpi_close(dh);
355 
356 	str = _link_ntoa(physaddr, NULL, physaddrlen, IFT_OTHER);
357 
358 	if (str != NULL && physaddrlen != 0) {
359 		switch (dlinfo.di_mactype) {
360 			case DL_IB:
361 				(void) printf("\tipib %s \n", str);
362 				break;
363 			default:
364 				(void) printf("\tether %s \n", str);
365 				break;
366 		}
367 		free(str);
368 	}
369 }
370