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 1991-2003 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
33 #include <sys/types.h>
34 #include <sys/stream.h>
35 #include <sys/stropts.h>
36 #include <sys/tihdr.h>
37 #include <sys/tiuser.h>
38 #include <sys/timod.h>
40 #include <sys/socket.h>
41 #include <sys/sockio.h>
42 #include <netinet/in.h>
45 #include <inet/common.h>
46 #include <inet/mib2.h>
48 #include <netinet/igmp_var.h>
49 #include <netinet/ip_mroute.h>
51 #include <arpa/inet.h>
54 #include <nss_dbdefs.h>
58 #include "bootparam_private.h"
60 typedef struct mib_item_s
{
61 struct mib_item_s
*next_item
;
68 static void free_itemlist(mib_item_t
*);
76 struct strbuf ctlbuf
, databuf
;
77 struct T_optmgmt_req
*tor
= (struct T_optmgmt_req
*)(void *)buf
;
78 struct T_optmgmt_ack
*toa
= (struct T_optmgmt_ack
*)(void *)buf
;
79 struct T_error_ack
*tea
= (struct T_error_ack
*)(void *)buf
;
81 mib_item_t
*first_item
= nilp(mib_item_t
);
82 mib_item_t
*last_item
= nilp(mib_item_t
);
85 tor
->PRIM_type
= T_SVR4_OPTMGMT_REQ
;
86 tor
->OPT_offset
= sizeof (struct T_optmgmt_req
);
87 tor
->OPT_length
= sizeof (struct opthdr
);
88 tor
->MGMT_flags
= T_CURRENT
;
89 req
= (struct opthdr
*)&tor
[1];
90 req
->level
= MIB2_IP
; /* any MIB2_xxx value ok here */
95 ctlbuf
.len
= tor
->OPT_length
+ tor
->OPT_offset
;
97 if (putmsg(sd
, &ctlbuf
, nilp(struct strbuf
), flags
) == -1) {
98 perror("mibget: putmsg(ctl) failed");
102 * each reply consists of a ctl part for one fixed structure
103 * or table, as defined in mib2.h. The format is a T_OPTMGMT_ACK,
104 * containing an opthdr structure. level/name identify the entry,
105 * len is the size of the data part of the message.
107 req
= (struct opthdr
*)&toa
[1];
108 ctlbuf
.maxlen
= sizeof (buf
);
111 getcode
= getmsg(sd
, &ctlbuf
, nilp(struct strbuf
), &flags
);
113 perror("mibget getmsg(ctl) failed");
115 msgout("# level name len");
117 for (last_item
= first_item
; last_item
;
118 last_item
= last_item
->next_item
)
119 msgout("%d %4ld %5ld %ld", ++i
,
126 if ((getcode
== 0) &&
127 (ctlbuf
.len
>= sizeof (struct T_optmgmt_ack
))&&
128 (toa
->PRIM_type
== T_OPTMGMT_ACK
) &&
129 (toa
->MGMT_flags
== T_SUCCESS
) &&
132 msgout("mibget getmsg() %d returned EOD "
133 "(level %lu, name %lu)",
134 j
, req
->level
, req
->name
);
135 return (first_item
); /* this is EOD msg */
138 if (ctlbuf
.len
>= sizeof (struct T_error_ack
) &&
139 tea
->PRIM_type
== T_ERROR_ACK
) {
140 msgout("mibget %d gives T_ERROR_ACK: "
141 "TLI_error = 0x%lx, UNIX_error = 0x%lx",
142 j
, tea
->TLI_error
, tea
->UNIX_error
);
143 errno
= (tea
->TLI_error
== TSYSERR
)
144 ? tea
->UNIX_error
: EPROTO
;
148 if (getcode
!= MOREDATA
||
149 ctlbuf
.len
< sizeof (struct T_optmgmt_ack
) ||
150 toa
->PRIM_type
!= T_OPTMGMT_ACK
||
151 toa
->MGMT_flags
!= T_SUCCESS
) {
152 msgout("mibget getmsg(ctl) %d returned %d, "
153 "ctlbuf.len = %d, PRIM_type = %ld",
154 j
, getcode
, ctlbuf
.len
, toa
->PRIM_type
);
155 if (toa
->PRIM_type
== T_OPTMGMT_ACK
)
156 msgout("T_OPTMGMT_ACK: MGMT_flags = 0x%lx, "
158 toa
->MGMT_flags
, req
->len
);
163 temp
= (mib_item_t
*)malloc(sizeof (mib_item_t
));
165 perror("mibget malloc failed");
169 last_item
->next_item
= temp
;
173 last_item
->next_item
= nilp(mib_item_t
);
174 last_item
->group
= req
->level
;
175 last_item
->mib_id
= req
->name
;
176 last_item
->length
= req
->len
;
177 last_item
->valp
= (char *)malloc(req
->len
);
180 "msg %d: group = %4ld mib_id = %5ld length = %ld",
181 j
, last_item
->group
, last_item
->mib_id
,
184 databuf
.maxlen
= last_item
->length
;
185 databuf
.buf
= last_item
->valp
;
188 getcode
= getmsg(sd
, nilp(struct strbuf
), &databuf
, &flags
);
190 perror("mibget getmsg(data) failed");
192 } else if (getcode
!= 0) {
193 msgout("xmibget getmsg(data) returned %d, "
194 "databuf.maxlen = %d, databuf.len = %d",
195 getcode
, databuf
.maxlen
, databuf
.len
);
201 free_itemlist(first_item
);
206 free_itemlist(mib_item_t
*item_list
)
212 item_list
= item
->next_item
;
219 * If we are a router, return address of interface closest to client.
220 * If we are not a router, look through our routing table and return
221 * address of "best" router that is on same net as client.
223 * We expect the router flag to show up first, followed by interface
224 * addr group, followed by the routing table.
228 get_ip_route(struct in_addr client_addr
)
231 mib_item_t
*item_list
;
235 mib2_ipAddrEntry_t
*map
;
236 mib2_ipRouteEntry_t
*rp
;
237 int ip_forwarding
= 2; /* off */
238 /* mask of interface used to route to client and best_router */
239 struct in_addr interface_mask
;
240 /* address of interface used to route to client and best_router */
241 struct in_addr interface_addr
;
242 /* address of "best router"; i.e. the answer */
243 struct in_addr best_router
;
245 interface_mask
.s_addr
= 0L;
246 interface_addr
.s_addr
= 0L;
247 best_router
.s_addr
= 0L;
249 /* open a stream to IP */
250 sd
= open("/dev/ip", O_RDWR
);
254 msgout("can't open mib stream");
258 /* send down a request and suck up all the mib info from IP */
259 if ((item_list
= mibget(sd
)) == nilp(mib_item_t
)) {
260 msgout("mibget() failed");
266 * We make three passes through the list of collected IP mib
267 * information. First we figure out if we are a router. Next,
268 * we find which of our interfaces is on the same subnet as
269 * the client. Third, we paw through our own routing table
270 * looking for a useful router address.
274 * The general IP group.
276 for (item
= item_list
; item
; item
= item
->next_item
) {
277 if ((item
->group
== MIB2_IP
) && (item
->mib_id
== 0)) {
278 /* are we an IP router? */
279 mip
= (mib2_ip_t
*)(void *)item
->valp
;
280 ip_forwarding
= mip
->ipForwarding
;
286 * The interface group.
288 for (item
= item_list
, found
= B_FALSE
; item
!= NULL
&& !found
;
289 item
= item
->next_item
) {
290 if ((item
->group
== MIB2_IP
) && (item
->mib_id
== MIB2_IP_20
)) {
292 * Try to find out which interface is up, configured,
293 * not loopback, and on the same subnet as the client.
294 * Save its address and netmask.
296 map
= (mib2_ipAddrEntry_t
*)(void *)item
->valp
;
297 while ((char *)map
< item
->valp
+ item
->length
) {
298 in_addr_t addr
, mask
, net
;
301 ifflags
= map
->ipAdEntInfo
.ae_flags
;
302 addr
= map
->ipAdEntAddr
;
303 mask
= map
->ipAdEntNetMask
;
306 if ((ifflags
& IFF_LOOPBACK
| IFF_UP
) ==
307 IFF_UP
&& addr
!= INADDR_ANY
&&
308 net
== (client_addr
.s_addr
& mask
)) {
309 interface_addr
.s_addr
= addr
;
310 interface_mask
.s_addr
= mask
;
320 * If this exercise found no interface on the same subnet as
321 * the client, then we can't suggest any router address to
324 if (interface_addr
.s_addr
== 0) {
326 msgout("get_ip_route: no interface on same net "
329 free_itemlist(item_list
);
334 * If we are a router, we return to client the address of our
335 * interface on the same net as the client.
337 if (ip_forwarding
== 1) {
339 msgout("get_ip_route: returning local addr %s",
340 inet_ntoa(interface_addr
));
342 free_itemlist(item_list
);
343 return (interface_addr
.s_addr
);
347 msgout("interface_addr = %s.", inet_ntoa(interface_addr
));
348 msgout("interface_mask = %s", inet_ntoa(interface_mask
));
353 * The routing table group.
355 for (item
= item_list
; item
; item
= item
->next_item
) {
356 if ((item
->group
== MIB2_IP
) && (item
->mib_id
== MIB2_IP_21
)) {
358 msgout("%lu records for ipRouteEntryTable",
360 sizeof (mib2_ipRouteEntry_t
));
362 for (rp
= (mib2_ipRouteEntry_t
*)(void *)item
->valp
;
363 (char *)rp
< item
->valp
+ item
->length
;
366 msgout("ire_type = %d, next_hop = 0x%x",
367 rp
->ipRouteInfo
.re_ire_type
,
371 * We are only interested in real
374 if ((rp
->ipRouteInfo
.re_ire_type
!=
376 (rp
->ipRouteInfo
.re_ire_type
!=
378 (rp
->ipRouteInfo
.re_ire_type
!=
380 (rp
->ipRouteInfo
.re_ire_type
!=
385 * We are only interested in routes with
386 * a next hop on the same subnet as
389 if ((rp
->ipRouteNextHop
&
390 interface_mask
.s_addr
) !=
391 (interface_addr
.s_addr
&
392 interface_mask
.s_addr
))
396 * We have a valid route. Give preference
399 if ((rp
->ipRouteDest
== 0) ||
400 (best_router
.s_addr
== 0))
407 if (debug
&& (best_router
.s_addr
== 0))
408 msgout("get_ip_route: no route found for client");
411 free_itemlist(item_list
);
412 return (best_router
.s_addr
);
416 * Return address of server interface closest to client.
418 * If the server has only a single IP address return it. Otherwise check
419 * if the server has an interface on the same subnet as the client and
420 * return the address of that interface.
424 find_best_server_int(char **addr_list
, char *client_name
)
426 in_addr_t server_addr
= 0;
427 struct hostent h
, *hp
;
428 char hbuf
[NSS_BUFLEN_HOSTS
];
430 struct in_addr client_addr
;
431 mib_item_t
*item_list
;
434 mib2_ipAddrEntry_t
*map
;
435 in_addr_t client_net
= 0, client_mask
= 0;
436 boolean_t found_client_int
;
438 (void) memcpy(&server_addr
, addr_list
[0], sizeof (in_addr_t
));
439 if (addr_list
[1] == NULL
)
440 return (server_addr
);
442 hp
= gethostbyname_r(client_name
, &h
, hbuf
, sizeof (hbuf
), &err
);
444 return (server_addr
);
445 (void) memcpy(&client_addr
, hp
->h_addr_list
[0], sizeof (client_addr
));
447 /* open a stream to IP */
448 sd
= open("/dev/ip", O_RDWR
);
452 msgout("can't open mib stream");
453 return (server_addr
);
456 /* send down a request and suck up all the mib info from IP */
457 if ((item_list
= mibget(sd
)) == nilp(mib_item_t
)) {
458 msgout("mibget() failed");
460 return (server_addr
);
465 * Search through the list for our interface which is on the same
466 * subnet as the client and get the netmask.
468 for (item
= item_list
, found_client_int
= B_FALSE
;
469 item
!= NULL
&& !found_client_int
; item
= item
->next_item
) {
470 if ((item
->group
== MIB2_IP
) && (item
->mib_id
== MIB2_IP_20
)) {
472 * Try to find out which interface is up, configured,
473 * not loopback, and on the same subnet as the client.
474 * Save its address and netmask.
476 map
= (mib2_ipAddrEntry_t
*)(void *)item
->valp
;
477 while ((char *)map
< item
->valp
+ item
->length
) {
478 in_addr_t addr
, mask
, net
;
481 ifflags
= map
->ipAdEntInfo
.ae_flags
;
482 addr
= map
->ipAdEntAddr
;
483 mask
= map
->ipAdEntNetMask
;
486 if ((ifflags
& IFF_LOOPBACK
|IFF_UP
) == IFF_UP
&&
487 addr
!= INADDR_ANY
&&
488 (client_addr
.s_addr
& mask
) == net
) {
491 found_client_int
= B_TRUE
;
500 * If we found the interface check which is the best IP address.
502 if (found_client_int
) {
503 while (*addr_list
!= NULL
) {
506 (void) memcpy(&addr
, *addr_list
, sizeof (in_addr_t
));
507 if ((addr
& client_mask
) == client_net
) {
515 if (debug
&& server_addr
== 0)
516 msgout("No usable interface for returning reply");
518 free_itemlist(item_list
);
519 return (server_addr
);