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.
27 #pragma ident "%Z%%M% %I% %E% SMI"
34 #include <sys/utsname.h>
35 #include <sys/systeminfo.h>
37 #include <sys/types.h>
38 #include <sys/param.h>
39 #include <sys/errno.h>
41 #include <sys/ioctl.h>
42 #include <sys/signal.h>
45 #include <sys/socket.h>
46 #include <sys/stropts.h>
47 #include <sys/resource.h>
49 #include <net/if_arp.h>
50 #include <sys/stream.h>
51 #include <net/route.h>
52 #include <netinet/in.h>
53 #include <arpa/inet.h>
54 #include <netinet/if_ether.h>
55 #include <netinet/ip_var.h>
56 #include <netinet/udp.h>
57 #include <netinet/udp_var.h>
59 #include <rpcsvc/bootparam_prot.h>
63 /* command line flags */
64 int debug
= 0; /* do debug printfs */
65 int echo_host
= 0; /* just echo hostname, don't set it */
66 int verbose
= 0; /* do verbose printfs */
67 int safe
= 0; /* don't change anything */
68 int multiple
= 0; /* take multiple replies */
70 static ulong_t if_netmask
;
72 void notsupported(), usage(), bp_whoami();
73 int get_ifdata(); /* get IP addr, subnet mask from IF */
74 extern char *inet_ntoa();
75 extern int getopt(), setdomainname();
81 { "bootparams", bp_whoami
},
82 { "bootp", notsupported
},
89 * usage: hostconfig [-p <protocol>] [-v] [-n] [-h] [<ifname>] [-f <hostname>]
94 * -n Don't change anything.
95 * -h Don't set hostname, just echo to standard out.
96 * -m Wait for multiple answers (best used with the "-n"
98 * -f <hostname> Fake mode - get bootparams for <hostname> (also
99 * best used with the "-n" and "-v" flags).
100 * <ifname> Use IP address of <interface> in whoami request.
102 * If no interface name is specified, bp_whoami will cycle through the
103 * interfaces, using the IP address of each in turn until an answer is
104 * received. Note that rpc_broadcast() broadcasts the RPC call on all
105 * interfaces, so the <ifname> argument doesn't restrict the request
106 * to that interface, it just uses that interface to determine the IP
107 * address to put into the request. If "-f <hostname>" is specified,
108 * we put the IP address of <hostname> in the whoami request. Otherwise,
109 * we put the IP address of the interface in the whoami request.
119 struct ifreq
*reqbuf
;
122 struct in_addr targetaddr
;
124 char *targethost
= NULL
;
128 struct prototab
*ptp
;
129 void (*protofunc
)() = NULL
;
138 while ((c
= getopt(argc
, argv
, "dhvnmf:p:")) != -1) {
166 for (ptp
= &prototab
[0]; ptp
->func
; ptp
++)
167 if (strcmp(optarg
, ptp
->name
) == 0) {
168 protofunc
= ptp
->func
;
171 if (protofunc
== NULL
)
180 if (protofunc
== NULL
)
184 /* we are faking it */
186 fprintf(stdout
, "targethost = %s\n", targethost
);
188 if ((hp
= gethostbyname(targethost
)) == NULL
) {
189 if ((targetaddr
.s_addr
= inet_addr(targethost
)) ==
191 (void) fprintf(stderr
,
192 "%s: cannot get IP address for %s\n",
193 cmdname
, targethost
);
197 if (hp
->h_length
!= sizeof (targetaddr
)) {
198 (void) fprintf(stderr
,
199 "%s: cannot find host entry for %s\n",
200 cmdname
, targethost
);
203 (void) memcpy(&targetaddr
.s_addr
, hp
->h_addr
,
204 sizeof (targetaddr
));
207 targetaddr
.s_addr
= 0;
210 /* interface names were specified */
211 for (; optind
< argc
; optind
++) {
213 fprintf(stdout
, "Trying arg %s\n",
215 (*protofunc
)(argv
[optind
], targetaddr
);
218 /* no interface names specified - try them all */
219 int ifcount
= 0; /* count of useable interfaces */
222 if ((s
= socket(AF_INET
, SOCK_DGRAM
, 0)) < 0) {
227 if (ioctl(s
, SIOCGIFNUM
, (char *)&numifs
) < 0) {
233 bufsize
= numifs
* sizeof (struct ifreq
);
234 reqbuf
= (struct ifreq
*)malloc(bufsize
);
235 if (reqbuf
== NULL
) {
236 fprintf(stderr
, "out of memory\n");
239 ifc
.ifc_buf
= (caddr_t
)&reqbuf
[0];
240 ifc
.ifc_len
= bufsize
;
241 if (ioctl(s
, SIOCGIFCONF
, (char *)&ifc
) < 0) {
242 perror("ioctl(SIOCGIFCONF)");
246 n
= ifc
.ifc_len
/sizeof (struct ifreq
);
247 for (; n
> 0; n
--, ifr
++) {
248 if (ioctl(s
, SIOCGIFFLAGS
, (char *)ifr
) < 0) {
249 perror("ioctl(SIOCGIFFLAGS)");
252 if ((ifr
->ifr_flags
& IFF_LOOPBACK
) ||
253 !(ifr
->ifr_flags
& IFF_BROADCAST
) ||
254 !(ifr
->ifr_flags
& IFF_UP
) ||
255 (ifr
->ifr_flags
& IFF_NOARP
) ||
256 (ifr
->ifr_flags
& IFF_POINTOPOINT
)) {
258 fprintf(stdout
, "If %s not suitable\n",
263 fprintf(stdout
, "Trying device %s\n",
265 (*protofunc
)(ifr
->ifr_name
, targetaddr
);
269 if (verbose
&& ifcount
== 0) {
270 fprintf(stderr
, "No useable interfaces found.\n");
274 (void) free((char *)reqbuf
);
281 add_default_route(router_addr
)
282 struct in_addr router_addr
;
284 struct rtentry route
;
285 struct sockaddr_in
*sin
;
288 (void) memset(&route
, 0, sizeof (route
));
290 /* route destination is "default" - zero */
291 /* LINTED - alignment OK (32bit) */
292 sin
= (struct sockaddr_in
*)&route
.rt_dst
;
293 sin
->sin_family
= AF_INET
;
295 /* LINTED - alignment OK (32bit) */
296 sin
= (struct sockaddr_in
*)&route
.rt_gateway
;
297 sin
->sin_family
= AF_INET
;
298 sin
->sin_addr
.s_addr
= router_addr
.s_addr
;
300 route
.rt_flags
= RTF_GATEWAY
| RTF_UP
;
302 if ((s
= socket(AF_INET
, SOCK_DGRAM
, 0)) < 0) {
306 if (ioctl(s
, SIOCADDRT
, (char *)&route
) == -1) {
307 perror("add default route");
316 struct bp_whoami_res
*res
;
319 struct in_addr router_addr
;
322 char errbuf
[MAX_MACHINE_NAME
+ 28];
323 /* MAX_MACHINE_NAME + strlen ("sysinfo(SI_SET_HOSTNAME)()") + null */
325 (void) memcpy(&router_addr
, &res
->router_address
.bp_address_u
.ip_addr
,
326 sizeof (router_addr
));
329 struct sockaddr_in
*addr
;
332 /* LINTED - alignment (32bit) OK */
333 addr
= (struct sockaddr_in
*)nb
->buf
;
334 fprintf(stdout
, "From [%s]: ",
335 inet_ntoa(addr
->sin_addr
));
337 fprintf(stdout
, "Reply:\\t\\t");
339 fprintf(stdout
, "hostname = %s\n", res
->client_name
);
340 fprintf(stdout
, "\t\typdomain = %s\n", res
->domain_name
);
341 fprintf(stdout
, "\t\trouter = %s\n", inet_ntoa(router_addr
));
346 * Stuff the values from the RPC reply into the kernel.
347 * Only allow one pass through this code; There's no reason
348 * why all replies should tweak the kernel.
352 len
= strlen(res
->client_name
);
355 if (sysinfo(SI_SET_HOSTNAME
, res
->client_name
,
357 (void) snprintf(errbuf
, sizeof (errbuf
),
358 "sysinfo(SI_SET_HOSTNAME)(%s)",
363 (void) fprintf(stdout
, "%s\n",
367 len
= strlen(res
->domain_name
);
369 if (setdomainname(res
->domain_name
, len
) == -1) {
370 (void) snprintf(errbuf
, sizeof (errbuf
),
371 "setdomainname(%s)", res
->domain_name
);
376 /* we really should validate this router value */
377 if (router_addr
.s_addr
!= 0)
378 add_default_route(router_addr
);
384 /* our job is done */
390 bp_whoami(device
, addr
)
394 struct bp_whoami_arg req
;
395 struct bp_whoami_res res
;
396 struct in_addr lookupaddr
;
401 fprintf(stdout
, "bp_whoami on interface %s addr %s\n", device
,
404 if (addr
.s_addr
== 0) {
405 if (get_ifdata(device
, &lookupaddr
, &if_netmask
) == -1)
408 (void) memcpy(&lookupaddr
, &addr
, sizeof (addr
));
410 lookupaddr
.s_addr
= ntohl(lookupaddr
.s_addr
);
413 fprintf(stdout
, "lookup address is %s\n",
414 inet_ntoa(lookupaddr
));
416 (void) memset(&req
, 0, sizeof (req
));
417 (void) memset(&res
, 0, sizeof (res
));
419 req
.client_address
.address_type
= IP_ADDR_TYPE
;
420 (void) memcpy(&req
.client_address
.bp_address_u
.ip_addr
, &lookupaddr
,
421 sizeof (lookupaddr
));
424 * Broadcast using portmap version number 2 ONLY to
425 * prevent broadcast storm
428 (void) __rpc_control(CLCR_SET_LOWVERS
, &val
);
430 stat
= rpc_broadcast(BOOTPARAMPROG
, BOOTPARAMVERS
, BOOTPARAMPROC_WHOAMI
,
431 xdr_bp_whoami_arg
, (caddr_t
)&req
, xdr_bp_whoami_res
, (caddr_t
)&res
,
432 (resultproc_t
)bpanswer
, "udp");
434 /* Now try version 3 as well */
437 (void) __rpc_control(CLCR_SET_LOWVERS
, &val
);
439 stat
= rpc_broadcast(BOOTPARAMPROG
, BOOTPARAMVERS
,
440 BOOTPARAMPROC_WHOAMI
, xdr_bp_whoami_arg
, (caddr_t
)&req
,
441 xdr_bp_whoami_res
, (caddr_t
)&res
, (resultproc_t
)bpanswer
, "udp");
443 if (stat
!= RPC_SUCCESS
) {
451 * Get IP address of an interface. As long as we are looking, get the
455 get_ifdata(dev
, ipp
, maskp
)
457 ulong_t
*ipp
, *maskp
;
460 /* LINTED - alignment OK (32bit) */
461 struct sockaddr_in
*sin
= (struct sockaddr_in
*)&ifr
.ifr_addr
;
464 if ((s
= socket(AF_INET
, SOCK_DGRAM
, 0)) < 0) {
469 if (strlcpy(ifr
.ifr_name
, dev
, sizeof (ifr
.ifr_name
)) >=
470 sizeof (ifr
.ifr_name
)) {
471 (void) fprintf(stderr
, "Device name too long %s\n",
477 if (ioctl(s
, SIOCGIFADDR
, (caddr_t
)&ifr
) < 0) {
478 perror("ioctl(SIOCGIFADDR)");
481 *ipp
= ntohl(sin
->sin_addr
.s_addr
);
484 (void) fprintf(stderr
, "Interface '%s' address %s\n",
485 dev
, inet_ntoa(sin
->sin_addr
));
489 if (ioctl(s
, SIOCGIFNETMASK
, (caddr_t
)&ifr
) < 0) {
490 perror("SIOCGIFNETMASK");
493 *maskp
= ntohl(sin
->sin_addr
.s_addr
);
496 (void) fprintf(stderr
,
497 "Interface '%s' subnet mask %s\n", dev
,
498 inet_ntoa(sin
->sin_addr
));
508 fprintf(stderr
, "requested protocol is not supported\n");
516 (void) fprintf(stderr
, "usage: %s [-v] [-n] [-m] [-h] [<ifname>] "
517 "[-f <hostname>] -p bootparams|bootp\n", cmdname
);
518 (void) fflush(stderr
);