4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
32 #include <sys/utsname.h>
33 #include <sys/systeminfo.h>
35 #include <sys/types.h>
36 #include <sys/param.h>
37 #include <sys/errno.h>
39 #include <sys/ioctl.h>
40 #include <sys/signal.h>
43 #include <sys/socket.h>
44 #include <sys/stropts.h>
45 #include <sys/resource.h>
47 #include <net/if_arp.h>
48 #include <sys/stream.h>
49 #include <net/route.h>
50 #include <netinet/in.h>
51 #include <arpa/inet.h>
52 #include <netinet/if_ether.h>
53 #include <netinet/ip_var.h>
54 #include <netinet/udp.h>
55 #include <netinet/udp_var.h>
57 #include <rpcsvc/bootparam_prot.h>
61 /* command line flags */
62 int debug
= 0; /* do debug printfs */
63 int echo_host
= 0; /* just echo hostname, don't set it */
64 int verbose
= 0; /* do verbose printfs */
65 int safe
= 0; /* don't change anything */
66 int multiple
= 0; /* take multiple replies */
68 static ulong_t if_netmask
;
70 void notsupported(), usage(), bp_whoami();
71 int get_ifdata(); /* get IP addr, subnet mask from IF */
72 extern char *inet_ntoa();
73 extern int getopt(), setdomainname();
79 { "bootparams", bp_whoami
},
80 { "bootp", notsupported
},
87 * usage: hostconfig [-p <protocol>] [-v] [-n] [-h] [<ifname>] [-f <hostname>]
92 * -n Don't change anything.
93 * -h Don't set hostname, just echo to standard out.
94 * -m Wait for multiple answers (best used with the "-n"
96 * -f <hostname> Fake mode - get bootparams for <hostname> (also
97 * best used with the "-n" and "-v" flags).
98 * <ifname> Use IP address of <interface> in whoami request.
100 * If no interface name is specified, bp_whoami will cycle through the
101 * interfaces, using the IP address of each in turn until an answer is
102 * received. Note that rpc_broadcast() broadcasts the RPC call on all
103 * interfaces, so the <ifname> argument doesn't restrict the request
104 * to that interface, it just uses that interface to determine the IP
105 * address to put into the request. If "-f <hostname>" is specified,
106 * we put the IP address of <hostname> in the whoami request. Otherwise,
107 * we put the IP address of the interface in the whoami request.
117 struct ifreq
*reqbuf
;
120 struct in_addr targetaddr
;
122 char *targethost
= NULL
;
126 struct prototab
*ptp
;
127 void (*protofunc
)() = NULL
;
136 while ((c
= getopt(argc
, argv
, "dhvnmf:p:")) != -1) {
164 for (ptp
= &prototab
[0]; ptp
->func
; ptp
++)
165 if (strcmp(optarg
, ptp
->name
) == 0) {
166 protofunc
= ptp
->func
;
169 if (protofunc
== NULL
)
178 if (protofunc
== NULL
)
182 /* we are faking it */
184 fprintf(stdout
, "targethost = %s\n", targethost
);
186 if ((hp
= gethostbyname(targethost
)) == NULL
) {
187 if ((targetaddr
.s_addr
= inet_addr(targethost
)) ==
189 (void) fprintf(stderr
,
190 "%s: cannot get IP address for %s\n",
191 cmdname
, targethost
);
195 if (hp
->h_length
!= sizeof (targetaddr
)) {
196 (void) fprintf(stderr
,
197 "%s: cannot find host entry for %s\n",
198 cmdname
, targethost
);
201 (void) memcpy(&targetaddr
.s_addr
, hp
->h_addr
,
202 sizeof (targetaddr
));
205 targetaddr
.s_addr
= 0;
208 /* interface names were specified */
209 for (; optind
< argc
; optind
++) {
211 fprintf(stdout
, "Trying arg %s\n",
213 (*protofunc
)(argv
[optind
], targetaddr
);
216 /* no interface names specified - try them all */
217 int ifcount
= 0; /* count of useable interfaces */
220 if ((s
= socket(AF_INET
, SOCK_DGRAM
, 0)) < 0) {
225 if (ioctl(s
, SIOCGIFNUM
, (char *)&numifs
) < 0) {
231 bufsize
= numifs
* sizeof (struct ifreq
);
232 reqbuf
= (struct ifreq
*)malloc(bufsize
);
233 if (reqbuf
== NULL
) {
234 fprintf(stderr
, "out of memory\n");
237 ifc
.ifc_buf
= (caddr_t
)&reqbuf
[0];
238 ifc
.ifc_len
= bufsize
;
239 if (ioctl(s
, SIOCGIFCONF
, (char *)&ifc
) < 0) {
240 perror("ioctl(SIOCGIFCONF)");
244 n
= ifc
.ifc_len
/sizeof (struct ifreq
);
245 for (; n
> 0; n
--, ifr
++) {
246 if (ioctl(s
, SIOCGIFFLAGS
, (char *)ifr
) < 0) {
247 perror("ioctl(SIOCGIFFLAGS)");
250 if ((ifr
->ifr_flags
& IFF_LOOPBACK
) ||
251 !(ifr
->ifr_flags
& IFF_BROADCAST
) ||
252 !(ifr
->ifr_flags
& IFF_UP
) ||
253 (ifr
->ifr_flags
& IFF_NOARP
) ||
254 (ifr
->ifr_flags
& IFF_POINTOPOINT
)) {
256 fprintf(stdout
, "If %s not suitable\n",
261 fprintf(stdout
, "Trying device %s\n",
263 (*protofunc
)(ifr
->ifr_name
, targetaddr
);
267 if (verbose
&& ifcount
== 0) {
268 fprintf(stderr
, "No useable interfaces found.\n");
272 (void) free((char *)reqbuf
);
279 add_default_route(router_addr
)
280 struct in_addr router_addr
;
282 struct rtentry route
;
283 struct sockaddr_in
*sin
;
286 (void) memset(&route
, 0, sizeof (route
));
288 /* route destination is "default" - zero */
289 /* LINTED - alignment OK (32bit) */
290 sin
= (struct sockaddr_in
*)&route
.rt_dst
;
291 sin
->sin_family
= AF_INET
;
293 /* LINTED - alignment OK (32bit) */
294 sin
= (struct sockaddr_in
*)&route
.rt_gateway
;
295 sin
->sin_family
= AF_INET
;
296 sin
->sin_addr
.s_addr
= router_addr
.s_addr
;
298 route
.rt_flags
= RTF_GATEWAY
| RTF_UP
;
300 if ((s
= socket(AF_INET
, SOCK_DGRAM
, 0)) < 0) {
304 if (ioctl(s
, SIOCADDRT
, (char *)&route
) == -1) {
305 perror("add default route");
314 struct bp_whoami_res
*res
;
317 struct in_addr router_addr
;
320 char errbuf
[MAX_MACHINE_NAME
+ 28];
321 /* MAX_MACHINE_NAME + strlen ("sysinfo(SI_SET_HOSTNAME)()") + null */
323 (void) memcpy(&router_addr
, &res
->router_address
.bp_address_u
.ip_addr
,
324 sizeof (router_addr
));
327 struct sockaddr_in
*addr
;
330 /* LINTED - alignment (32bit) OK */
331 addr
= (struct sockaddr_in
*)nb
->buf
;
332 fprintf(stdout
, "From [%s]: ",
333 inet_ntoa(addr
->sin_addr
));
335 fprintf(stdout
, "Reply:\\t\\t");
337 fprintf(stdout
, "hostname = %s\n", res
->client_name
);
338 fprintf(stdout
, "\t\typdomain = %s\n", res
->domain_name
);
339 fprintf(stdout
, "\t\trouter = %s\n", inet_ntoa(router_addr
));
344 * Stuff the values from the RPC reply into the kernel.
345 * Only allow one pass through this code; There's no reason
346 * why all replies should tweak the kernel.
350 len
= strlen(res
->client_name
);
353 if (sysinfo(SI_SET_HOSTNAME
, res
->client_name
,
355 (void) snprintf(errbuf
, sizeof (errbuf
),
356 "sysinfo(SI_SET_HOSTNAME)(%s)",
361 (void) fprintf(stdout
, "%s\n",
365 len
= strlen(res
->domain_name
);
367 if (setdomainname(res
->domain_name
, len
) == -1) {
368 (void) snprintf(errbuf
, sizeof (errbuf
),
369 "setdomainname(%s)", res
->domain_name
);
374 /* we really should validate this router value */
375 if (router_addr
.s_addr
!= 0)
376 add_default_route(router_addr
);
382 /* our job is done */
388 bp_whoami(device
, addr
)
392 struct bp_whoami_arg req
;
393 struct bp_whoami_res res
;
394 struct in_addr lookupaddr
;
399 fprintf(stdout
, "bp_whoami on interface %s addr %s\n", device
,
402 if (addr
.s_addr
== 0) {
403 if (get_ifdata(device
, &lookupaddr
, &if_netmask
) == -1)
406 (void) memcpy(&lookupaddr
, &addr
, sizeof (addr
));
408 lookupaddr
.s_addr
= ntohl(lookupaddr
.s_addr
);
411 fprintf(stdout
, "lookup address is %s\n",
412 inet_ntoa(lookupaddr
));
414 (void) memset(&req
, 0, sizeof (req
));
415 (void) memset(&res
, 0, sizeof (res
));
417 req
.client_address
.address_type
= IP_ADDR_TYPE
;
418 (void) memcpy(&req
.client_address
.bp_address_u
.ip_addr
, &lookupaddr
,
419 sizeof (lookupaddr
));
422 * Broadcast using portmap version number 2 ONLY to
423 * prevent broadcast storm
426 (void) __rpc_control(CLCR_SET_LOWVERS
, &val
);
428 stat
= rpc_broadcast(BOOTPARAMPROG
, BOOTPARAMVERS
, BOOTPARAMPROC_WHOAMI
,
429 xdr_bp_whoami_arg
, (caddr_t
)&req
, xdr_bp_whoami_res
, (caddr_t
)&res
,
430 (resultproc_t
)bpanswer
, "udp");
432 /* Now try version 3 as well */
435 (void) __rpc_control(CLCR_SET_LOWVERS
, &val
);
437 stat
= rpc_broadcast(BOOTPARAMPROG
, BOOTPARAMVERS
,
438 BOOTPARAMPROC_WHOAMI
, xdr_bp_whoami_arg
, (caddr_t
)&req
,
439 xdr_bp_whoami_res
, (caddr_t
)&res
, (resultproc_t
)bpanswer
, "udp");
441 if (stat
!= RPC_SUCCESS
) {
449 * Get IP address of an interface. As long as we are looking, get the
453 get_ifdata(dev
, ipp
, maskp
)
455 ulong_t
*ipp
, *maskp
;
458 /* LINTED - alignment OK (32bit) */
459 struct sockaddr_in
*sin
= (struct sockaddr_in
*)&ifr
.ifr_addr
;
462 if ((s
= socket(AF_INET
, SOCK_DGRAM
, 0)) < 0) {
467 if (strlcpy(ifr
.ifr_name
, dev
, sizeof (ifr
.ifr_name
)) >=
468 sizeof (ifr
.ifr_name
)) {
469 (void) fprintf(stderr
, "Device name too long %s\n",
475 if (ioctl(s
, SIOCGIFADDR
, (caddr_t
)&ifr
) < 0) {
476 perror("ioctl(SIOCGIFADDR)");
479 *ipp
= ntohl(sin
->sin_addr
.s_addr
);
482 (void) fprintf(stderr
, "Interface '%s' address %s\n",
483 dev
, inet_ntoa(sin
->sin_addr
));
487 if (ioctl(s
, SIOCGIFNETMASK
, (caddr_t
)&ifr
) < 0) {
488 perror("SIOCGIFNETMASK");
491 *maskp
= ntohl(sin
->sin_addr
.s_addr
);
494 (void) fprintf(stderr
,
495 "Interface '%s' subnet mask %s\n", dev
,
496 inet_ntoa(sin
->sin_addr
));
506 fprintf(stderr
, "requested protocol is not supported\n");
514 (void) fprintf(stderr
, "usage: %s [-v] [-n] [-m] [-h] [<ifname>] "
515 "[-f <hostname>] -p bootparams|bootp\n", cmdname
);
516 (void) fflush(stderr
);