Print this page
OS-4807 arp(1M) and ndp(1M) should use zone_get_nroot()
| Split |
Close |
| Expand all |
| Collapse all |
--- old/usr/src/cmd/cmd-inet/usr.sbin/arp.c
+++ new/usr/src/cmd/cmd-inet/usr.sbin/arp.c
1 1 /*
2 2 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
3 3 * Use is subject to license terms.
4 4 */
5 5 /*
6 6 * Copyright (c) 1984 Regents of the University of California.
7 7 * All rights reserved.
8 8 *
9 9 * This code is derived from software contributed to Berkeley by
10 10 * Sun Microsystems, Inc.
11 11 *
12 12 * Redistribution and use in source and binary forms, with or without
13 13 * modification, are permitted provided that the following conditions
14 14 * are met:
15 15 * 1. Redistributions of source code must retain the above copyright
16 16 * notice, this list of conditions and the following disclaimer.
17 17 * 2. Redistributions in binary form must reproduce the above copyright
18 18 * notice, this list of conditions and the following disclaimer in the
19 19 * documentation and/or other materials provided with the distribution.
20 20 * 3. All advertising materials mentioning features or use of this software
21 21 * must display the following acknowledgement:
22 22 * This product includes software developed by the University of
23 23 * California, Berkeley and its contributors.
24 24 * 4. Neither the name of the University nor the names of its contributors
25 25 * may be used to endorse or promote products derived from this software
26 26 * without specific prior written permission.
27 27 *
28 28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 38 * SUCH DAMAGE.
39 39 */
40 40
41 41
42 42 /*
43 43 * arp - display, set, and delete arp table entries
44 44 */
45 45
46 46 #include <stdio.h>
47 47 #include <sys/types.h>
48 48 #include <sys/socket.h>
49 49 #include <netinet/in.h>
50 50 #include <sys/ioctl.h>
|
↓ open down ↓ |
50 lines elided |
↑ open up ↑ |
51 51 #include <errno.h>
52 52 #include <netdb.h>
53 53 #include <net/if.h>
54 54 #include <net/if_arp.h>
55 55 #include <stdlib.h>
56 56 #include <unistd.h>
57 57 #include <string.h>
58 58 #include <arpa/inet.h>
59 59 #include <net/if_types.h>
60 60 #include <net/if_dl.h>
61 +#include <zone.h>
61 62
62 63 static int file(char *);
63 64 static int set(int, char *[]);
64 65 static void get(char *);
65 66 static void delete(char *);
66 67 static void usage(void);
67 68
68 69 int
69 70 main(int argc, char *argv[])
70 71 {
71 72 int c, nflags = 0, argsleft;
72 73 int n_flag, a_flag, d_flag, f_flag, s_flag;
73 74
74 75 n_flag = a_flag = d_flag = f_flag = s_flag = 0;
75 76
76 77 #define CASE(x, y) \
77 78 case x: \
78 79 if (nflags > 0) { \
79 80 usage(); \
80 81 exit(1); \
81 82 } else \
82 83 y##_flag = 1; \
83 84 nflags++; \
84 85 break
85 86
86 87 while ((c = getopt(argc, argv, "nadfs")) != EOF) {
87 88 switch (c) {
88 89 case '?':
89 90 usage();
90 91 exit(1);
91 92 /* NOTREACHED */
92 93 break;
93 94 case 'n':
94 95 n_flag = 1;
95 96 break;
96 97 CASE('a', a);
97 98 CASE('d', d);
98 99 CASE('f', f);
99 100 CASE('s', s);
100 101 }
101 102 }
102 103
103 104 #undef CASE
104 105
105 106 /*
106 107 * -n only allowed with -a
107 108 */
108 109 if (n_flag && !a_flag) {
109 110 usage();
110 111 exit(1);
|
↓ open down ↓ |
40 lines elided |
↑ open up ↑ |
111 112 }
112 113
113 114 argsleft = argc - optind;
114 115
115 116 if (a_flag && (argsleft == 0)) {
116 117 /*
117 118 * the easiest way to get the complete arp table
118 119 * is to let netstat, which prints it as part of
119 120 * the MIB statistics, do it.
120 121 */
121 - (void) execl("/usr/bin/netstat", "netstat",
122 + char netstat_path[MAXPATHLEN];
123 + const char *zroot = zone_get_nroot();
124 + (void) snprintf(netstat_path, sizeof (netstat_path), "%s%s", zroot != NULL ?
125 + zroot : "", "/usr/bin/netstat");
126 + (void) execl(netstat_path, "netstat",
122 127 (n_flag ? "-np" : "-p"),
123 128 "-f", "inet", (char *)0);
124 - (void) fprintf(stderr, "failed to exec netstat: %s\n",
125 - strerror(errno));
129 + (void) fprintf(stderr, "failed to exec %s: %s\n",
130 + netstat_path, strerror(errno));
126 131 exit(1);
127 132
128 133 } else if (s_flag && (argsleft >= 2)) {
129 134 if (set(argsleft, &argv[optind]) != 0)
130 135 exit(1);
131 136
132 137 } else if (d_flag && (argsleft == 1)) {
133 138 delete(argv[optind]);
134 139
135 140 } else if (f_flag && (argsleft == 1)) {
136 141 if (file(argv[optind]) != 0)
137 142 exit(1);
138 143
139 144 } else if ((nflags == 0) && (argsleft == 1)) {
140 145 get(argv[optind]);
141 146
142 147 } else {
143 148 usage();
144 149 exit(1);
145 150 }
146 151 return (0);
147 152 }
148 153
149 154 /*
150 155 * Process a file to set standard arp entries
151 156 */
152 157 static int
153 158 file(char *name)
154 159 {
155 160 /*
156 161 * A line of input can be:
157 162 * <hostname> <macaddr> ["temp"] ["pub"] ["trail"] ["permanent"]
158 163 */
159 164 #define MAX_LINE_LEN (MAXHOSTNAMELEN + \
160 165 sizeof (" xx:xx:xx:xx:xx:xx temp pub trail permanent\n"))
161 166 #define MIN_ARGS 2
162 167 #define MAX_ARGS 5
163 168
164 169 FILE *fp;
165 170 char line[MAX_LINE_LEN];
166 171 int retval;
167 172
168 173 if ((fp = fopen(name, "r")) == NULL) {
169 174 (void) fprintf(stderr, "arp: cannot open %s\n", name);
170 175 exit(1);
171 176 }
172 177
173 178 retval = 0;
174 179 while (fgets(line, MAX_LINE_LEN, fp) != NULL) {
175 180 char line_copy[MAX_LINE_LEN];
176 181 char *args[MAX_ARGS];
177 182 char *start;
178 183 int i;
179 184
180 185 /*
181 186 * Keep a copy of the un-altered line for error
182 187 * reporting.
183 188 */
184 189 (void) strlcpy(line_copy, line, MAX_LINE_LEN);
185 190
186 191 start = line_copy;
187 192 for (i = 0; i < MAX_ARGS; i++) {
188 193 if ((args[i] = strtok(start, " \t\n")) == NULL)
189 194 break;
190 195
191 196 start = NULL;
192 197 }
193 198
194 199 if (i < MIN_ARGS) {
195 200 (void) fprintf(stderr, "arp: bad line: %s\n",
196 201 line);
197 202 retval = 1;
198 203 continue;
199 204 }
200 205
201 206 if (set(i, args) != 0)
202 207 retval = 1;
203 208 }
204 209
205 210 #undef MAX_LINE_LEN
206 211 #undef MIN_ARGS
207 212 #undef MAX_ARGS
208 213
209 214 (void) fclose(fp);
210 215 return (retval);
211 216 }
212 217
213 218 /*
214 219 * Set an individual arp entry
215 220 */
216 221 static int
217 222 set(int argc, char *argv[])
218 223 {
219 224 struct xarpreq ar;
220 225 struct hostent *hp;
221 226 struct sockaddr_in *sin;
222 227 uchar_t *ea;
223 228 int s;
224 229 char *host = argv[0], *eaddr = argv[1];
225 230
226 231 argc -= 2;
227 232 argv += 2;
228 233 (void) memset(&ar, 0, sizeof (ar));
229 234 sin = (struct sockaddr_in *)&ar.xarp_pa;
230 235 sin->sin_family = AF_INET;
231 236 sin->sin_addr.s_addr = inet_addr(host);
232 237 if (sin->sin_addr.s_addr == (in_addr_t)-1) {
233 238 hp = gethostbyname(host);
234 239 if (hp == NULL) {
235 240 (void) fprintf(stderr, "arp: %s: unknown host\n",
236 241 host);
237 242 return (1);
238 243 }
239 244 (void) memcpy(&sin->sin_addr, hp->h_addr,
240 245 sizeof (sin->sin_addr));
241 246 }
242 247 ea = _link_aton(eaddr, &s);
243 248 if (ea == NULL) {
244 249 if (s == -1) {
245 250 (void) fprintf(stderr,
246 251 "arp: invalid link layer address '%s'\n", eaddr);
247 252 return (1);
248 253 }
249 254 perror("arp: nomem");
250 255 exit(1);
251 256 }
252 257 ar.xarp_ha.sdl_alen = s;
253 258 (void) memcpy(LLADDR(&ar.xarp_ha), ea, ar.xarp_ha.sdl_alen);
254 259 free(ea);
255 260 ar.xarp_ha.sdl_family = AF_LINK;
256 261 ar.xarp_flags = ATF_PERM;
257 262 while (argc-- > 0) {
258 263 if (strncmp(argv[0], "temp", 4) == 0) {
259 264 ar.xarp_flags &= ~ATF_PERM;
260 265 } else if (strncmp(argv[0], "pub", 3) == 0) {
261 266 ar.xarp_flags |= ATF_PUBL;
262 267 } else if (strncmp(argv[0], "trail", 5) == 0) {
263 268 ar.xarp_flags |= ATF_USETRAILERS;
264 269 } else if (strcmp(argv[0], "permanent") == 0) {
265 270 ar.xarp_flags |= ATF_AUTHORITY;
266 271 } else {
267 272 (void) fprintf(stderr,
268 273 "arp: unknown keyword '%s'\n", argv[0]);
269 274 return (1);
270 275 }
271 276 argv++;
272 277 }
273 278
274 279 if ((ar.xarp_flags & (ATF_PERM|ATF_AUTHORITY)) == ATF_AUTHORITY) {
275 280 (void) fprintf(stderr, "arp: 'temp' and 'permanent' flags are "
276 281 "not usable together.\n");
277 282 return (1);
278 283 }
279 284
280 285 s = socket(AF_INET, SOCK_DGRAM, 0);
281 286 if (s < 0) {
282 287 perror("arp: socket");
283 288 exit(1);
284 289 }
285 290 if (ioctl(s, SIOCSXARP, (caddr_t)&ar) < 0) {
286 291 perror(host);
287 292 exit(1);
288 293 }
289 294 (void) close(s);
290 295 return (0);
291 296 }
292 297
293 298 /*
294 299 * Display an individual arp entry
295 300 */
296 301 static void
297 302 get(char *host)
298 303 {
299 304 struct xarpreq ar;
300 305 struct hostent *hp;
301 306 struct sockaddr_in *sin;
302 307 uchar_t *ea;
303 308 int s;
304 309 char *str = NULL;
305 310
306 311 (void) memset(&ar, 0, sizeof (ar));
307 312 sin = (struct sockaddr_in *)&ar.xarp_pa;
308 313 sin->sin_family = AF_INET;
309 314 sin->sin_addr.s_addr = inet_addr(host);
310 315 if (sin->sin_addr.s_addr == (in_addr_t)-1) {
311 316 hp = gethostbyname(host);
312 317 if (hp == NULL) {
313 318 (void) fprintf(stderr, "arp: %s: unknown host\n",
314 319 host);
315 320 exit(1);
316 321 }
317 322 (void) memcpy(&sin->sin_addr, hp->h_addr,
318 323 sizeof (sin->sin_addr));
319 324 }
320 325 s = socket(AF_INET, SOCK_DGRAM, 0);
321 326 if (s < 0) {
322 327 perror("arp: socket");
323 328 exit(1);
324 329 }
325 330 ar.xarp_ha.sdl_family = AF_LINK;
326 331 if (ioctl(s, SIOCGXARP, (caddr_t)&ar) < 0) {
327 332 if (errno == ENXIO)
328 333 (void) printf("%s (%s) -- no entry\n",
329 334 host, inet_ntoa(sin->sin_addr));
330 335 else
331 336 perror("SIOCGXARP");
332 337 exit(1);
333 338 }
334 339 (void) close(s);
335 340 ea = (uchar_t *)LLADDR(&ar.xarp_ha);
336 341 if (ar.xarp_flags & ATF_COM) {
337 342 str = _link_ntoa(ea, str, ar.xarp_ha.sdl_alen, IFT_OTHER);
338 343 if (str != NULL) {
339 344 (void) printf("%s (%s) at %s", host,
340 345 inet_ntoa(sin->sin_addr), str);
341 346 free(str);
342 347 } else {
343 348 perror("arp: nomem");
344 349 exit(1);
345 350 }
346 351 } else {
347 352 (void) printf("%s (%s) at (incomplete)", host,
348 353 inet_ntoa(sin->sin_addr));
349 354 }
350 355 if (!(ar.xarp_flags & ATF_PERM))
351 356 (void) printf(" temp");
352 357 if (ar.xarp_flags & ATF_PUBL)
353 358 (void) printf(" pub");
354 359 if (ar.xarp_flags & ATF_USETRAILERS)
355 360 (void) printf(" trail");
356 361 if (ar.xarp_flags & ATF_AUTHORITY)
357 362 (void) printf(" permanent");
358 363 (void) printf("\n");
359 364 }
360 365
361 366 /*
362 367 * Delete an arp entry
363 368 */
364 369 static void
365 370 delete(char *host)
366 371 {
367 372 struct xarpreq ar;
368 373 struct hostent *hp;
369 374 struct sockaddr_in *sin;
370 375 int s;
371 376
372 377 (void) memset(&ar, 0, sizeof (ar));
373 378 sin = (struct sockaddr_in *)&ar.xarp_pa;
374 379 sin->sin_family = AF_INET;
375 380 sin->sin_addr.s_addr = inet_addr(host);
376 381 if (sin->sin_addr.s_addr == (in_addr_t)-1) {
377 382 hp = gethostbyname(host);
378 383 if (hp == NULL) {
379 384 (void) fprintf(stderr, "arp: %s: unknown host\n",
380 385 host);
381 386 exit(1);
382 387 }
383 388 (void) memcpy(&sin->sin_addr, hp->h_addr,
384 389 sizeof (sin->sin_addr));
385 390 }
386 391 s = socket(AF_INET, SOCK_DGRAM, 0);
387 392 if (s < 0) {
388 393 perror("arp: socket");
389 394 exit(1);
390 395 }
391 396 ar.xarp_ha.sdl_family = AF_LINK;
392 397 if (ioctl(s, SIOCDXARP, (caddr_t)&ar) < 0) {
393 398 if (errno == ENXIO)
394 399 (void) printf("%s (%s) -- no entry\n",
395 400 host, inet_ntoa(sin->sin_addr));
396 401 else
397 402 perror("SIOCDXARP");
398 403 exit(1);
399 404 }
400 405 (void) close(s);
401 406 (void) printf("%s (%s) deleted\n", host, inet_ntoa(sin->sin_addr));
402 407 }
403 408
404 409 static void
405 410 usage(void)
406 411 {
407 412 (void) printf("Usage: arp hostname\n");
408 413 (void) printf(" arp -a [-n]\n");
409 414 (void) printf(" arp -d hostname\n");
410 415 (void) printf(" arp -s hostname ether_addr "
411 416 "[temp] [pub] [trail] [permanent]\n");
412 417 (void) printf(" arp -f filename\n");
413 418 }
|
↓ open down ↓ |
278 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX