2 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
3 * Use is subject to license terms.
6 * Copyright (c) 1984 Regents of the University of California.
9 * This code is derived from software contributed to Berkeley by
10 * Sun Microsystems, Inc.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by the University of
23 * California, Berkeley and its contributors.
24 * 4. Neither the name of the University nor the names of its contributors
25 * may be used to endorse or promote products derived from this software
26 * without specific prior written permission.
28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
43 * arp - display, set, and delete arp table entries
47 #include <sys/types.h>
48 #include <sys/socket.h>
49 #include <netinet/in.h>
50 #include <sys/ioctl.h>
54 #include <net/if_arp.h>
58 #include <arpa/inet.h>
59 #include <net/if_types.h>
60 #include <net/if_dl.h>
62 static int file(char *);
63 static int set(int, char *[]);
64 static void get(char *);
65 static void delete(char *);
66 static void usage(void);
69 main(int argc
, char *argv
[])
71 int c
, nflags
= 0, argsleft
;
72 int n_flag
, a_flag
, d_flag
, f_flag
, s_flag
;
74 n_flag
= a_flag
= d_flag
= f_flag
= s_flag
= 0;
86 while ((c
= getopt(argc
, argv
, "nadfs")) != EOF
) {
106 * -n only allowed with -a
108 if (n_flag
&& !a_flag
) {
113 argsleft
= argc
- optind
;
115 if (a_flag
&& (argsleft
== 0)) {
117 * the easiest way to get the complete arp table
118 * is to let netstat, which prints it as part of
119 * the MIB statistics, do it.
121 (void) execl("/usr/bin/netstat", "netstat",
122 (n_flag
? "-np" : "-p"),
123 "-f", "inet", (char *)0);
124 (void) fprintf(stderr
, "failed to exec netstat: %s\n",
128 } else if (s_flag
&& (argsleft
>= 2)) {
129 if (set(argsleft
, &argv
[optind
]) != 0)
132 } else if (d_flag
&& (argsleft
== 1)) {
133 delete(argv
[optind
]);
135 } else if (f_flag
&& (argsleft
== 1)) {
136 if (file(argv
[optind
]) != 0)
139 } else if ((nflags
== 0) && (argsleft
== 1)) {
150 * Process a file to set standard arp entries
156 * A line of input can be:
157 * <hostname> <macaddr> ["temp"] ["pub"] ["trail"] ["permanent"]
159 #define MAX_LINE_LEN (MAXHOSTNAMELEN + \
160 sizeof (" xx:xx:xx:xx:xx:xx temp pub trail permanent\n"))
165 char line
[MAX_LINE_LEN
];
168 if ((fp
= fopen(name
, "r")) == NULL
) {
169 (void) fprintf(stderr
, "arp: cannot open %s\n", name
);
174 while (fgets(line
, MAX_LINE_LEN
, fp
) != NULL
) {
175 char line_copy
[MAX_LINE_LEN
];
176 char *args
[MAX_ARGS
];
181 * Keep a copy of the un-altered line for error
184 (void) strlcpy(line_copy
, line
, MAX_LINE_LEN
);
187 for (i
= 0; i
< MAX_ARGS
; i
++) {
188 if ((args
[i
] = strtok(start
, " \t\n")) == NULL
)
195 (void) fprintf(stderr
, "arp: bad line: %s\n",
201 if (set(i
, args
) != 0)
214 * Set an individual arp entry
217 set(int argc
, char *argv
[])
221 struct sockaddr_in
*sin
;
224 char *host
= argv
[0], *eaddr
= argv
[1];
228 (void) memset(&ar
, 0, sizeof (ar
));
229 sin
= (struct sockaddr_in
*)&ar
.xarp_pa
;
230 sin
->sin_family
= AF_INET
;
231 sin
->sin_addr
.s_addr
= inet_addr(host
);
232 if (sin
->sin_addr
.s_addr
== (in_addr_t
)-1) {
233 hp
= gethostbyname(host
);
235 (void) fprintf(stderr
, "arp: %s: unknown host\n",
239 (void) memcpy(&sin
->sin_addr
, hp
->h_addr
,
240 sizeof (sin
->sin_addr
));
242 ea
= _link_aton(eaddr
, &s
);
245 (void) fprintf(stderr
,
246 "arp: invalid link layer address '%s'\n", eaddr
);
249 perror("arp: nomem");
252 ar
.xarp_ha
.sdl_alen
= s
;
253 (void) memcpy(LLADDR(&ar
.xarp_ha
), ea
, ar
.xarp_ha
.sdl_alen
);
255 ar
.xarp_ha
.sdl_family
= AF_LINK
;
256 ar
.xarp_flags
= ATF_PERM
;
258 if (strncmp(argv
[0], "temp", 4) == 0) {
259 ar
.xarp_flags
&= ~ATF_PERM
;
260 } else if (strncmp(argv
[0], "pub", 3) == 0) {
261 ar
.xarp_flags
|= ATF_PUBL
;
262 } else if (strncmp(argv
[0], "trail", 5) == 0) {
263 ar
.xarp_flags
|= ATF_USETRAILERS
;
264 } else if (strcmp(argv
[0], "permanent") == 0) {
265 ar
.xarp_flags
|= ATF_AUTHORITY
;
267 (void) fprintf(stderr
,
268 "arp: unknown keyword '%s'\n", argv
[0]);
274 if ((ar
.xarp_flags
& (ATF_PERM
|ATF_AUTHORITY
)) == ATF_AUTHORITY
) {
275 (void) fprintf(stderr
, "arp: 'temp' and 'permanent' flags are "
276 "not usable together.\n");
280 s
= socket(AF_INET
, SOCK_DGRAM
, 0);
282 perror("arp: socket");
285 if (ioctl(s
, SIOCSXARP
, (caddr_t
)&ar
) < 0) {
294 * Display an individual arp entry
301 struct sockaddr_in
*sin
;
306 (void) memset(&ar
, 0, sizeof (ar
));
307 sin
= (struct sockaddr_in
*)&ar
.xarp_pa
;
308 sin
->sin_family
= AF_INET
;
309 sin
->sin_addr
.s_addr
= inet_addr(host
);
310 if (sin
->sin_addr
.s_addr
== (in_addr_t
)-1) {
311 hp
= gethostbyname(host
);
313 (void) fprintf(stderr
, "arp: %s: unknown host\n",
317 (void) memcpy(&sin
->sin_addr
, hp
->h_addr
,
318 sizeof (sin
->sin_addr
));
320 s
= socket(AF_INET
, SOCK_DGRAM
, 0);
322 perror("arp: socket");
325 ar
.xarp_ha
.sdl_family
= AF_LINK
;
326 if (ioctl(s
, SIOCGXARP
, (caddr_t
)&ar
) < 0) {
328 (void) printf("%s (%s) -- no entry\n",
329 host
, inet_ntoa(sin
->sin_addr
));
335 ea
= (uchar_t
*)LLADDR(&ar
.xarp_ha
);
336 if (ar
.xarp_flags
& ATF_COM
) {
337 str
= _link_ntoa(ea
, str
, ar
.xarp_ha
.sdl_alen
, IFT_OTHER
);
339 (void) printf("%s (%s) at %s", host
,
340 inet_ntoa(sin
->sin_addr
), str
);
343 perror("arp: nomem");
347 (void) printf("%s (%s) at (incomplete)", host
,
348 inet_ntoa(sin
->sin_addr
));
350 if (!(ar
.xarp_flags
& ATF_PERM
))
351 (void) printf(" temp");
352 if (ar
.xarp_flags
& ATF_PUBL
)
353 (void) printf(" pub");
354 if (ar
.xarp_flags
& ATF_USETRAILERS
)
355 (void) printf(" trail");
356 if (ar
.xarp_flags
& ATF_AUTHORITY
)
357 (void) printf(" permanent");
362 * Delete an arp entry
369 struct sockaddr_in
*sin
;
372 (void) memset(&ar
, 0, sizeof (ar
));
373 sin
= (struct sockaddr_in
*)&ar
.xarp_pa
;
374 sin
->sin_family
= AF_INET
;
375 sin
->sin_addr
.s_addr
= inet_addr(host
);
376 if (sin
->sin_addr
.s_addr
== (in_addr_t
)-1) {
377 hp
= gethostbyname(host
);
379 (void) fprintf(stderr
, "arp: %s: unknown host\n",
383 (void) memcpy(&sin
->sin_addr
, hp
->h_addr
,
384 sizeof (sin
->sin_addr
));
386 s
= socket(AF_INET
, SOCK_DGRAM
, 0);
388 perror("arp: socket");
391 ar
.xarp_ha
.sdl_family
= AF_LINK
;
392 if (ioctl(s
, SIOCDXARP
, (caddr_t
)&ar
) < 0) {
394 (void) printf("%s (%s) -- no entry\n",
395 host
, inet_ntoa(sin
->sin_addr
));
401 (void) printf("%s (%s) deleted\n", host
, inet_ntoa(sin
->sin_addr
));
407 (void) printf("Usage: arp hostname\n");
408 (void) printf(" arp -a [-n]\n");
409 (void) printf(" arp -d hostname\n");
410 (void) printf(" arp -s hostname ether_addr "
411 "[temp] [pub] [trail] [permanent]\n");
412 (void) printf(" arp -f filename\n");