Move /var/svc/log to /var/log/svc
[unleashed/lotheac.git] / usr / src / cmd / rpcsvc / rpc.bootparamd / bootparam_ip_route.c
blob27e1a3ce2c74fc1a2f60335716b3aa927b823d9a
1 /*
2 * CDDL HEADER START
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
7 * with the License.
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]
20 * CDDL HEADER END
23 * Copyright 1991-2003 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <unistd.h>
31 #include <errno.h>
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>
43 #include <net/if.h>
45 #include <inet/common.h>
46 #include <inet/mib2.h>
47 #include <inet/ip.h>
48 #include <netinet/igmp_var.h>
49 #include <netinet/ip_mroute.h>
51 #include <arpa/inet.h>
53 #include <netdb.h>
54 #include <nss_dbdefs.h>
55 #include <fcntl.h>
56 #include <stropts.h>
58 #include "bootparam_private.h"
60 typedef struct mib_item_s {
61 struct mib_item_s *next_item;
62 long group;
63 long mib_id;
64 long length;
65 char *valp;
66 } mib_item_t;
68 static void free_itemlist(mib_item_t *);
70 static mib_item_t *
71 mibget(int sd)
73 char buf[512];
74 int flags;
75 int i, j, getcode;
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;
80 struct opthdr *req;
81 mib_item_t *first_item = nilp(mib_item_t);
82 mib_item_t *last_item = nilp(mib_item_t);
83 mib_item_t *temp;
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 */
91 req->name = 0;
92 req->len = 0;
94 ctlbuf.buf = buf;
95 ctlbuf.len = tor->OPT_length + tor->OPT_offset;
96 flags = 0;
97 if (putmsg(sd, &ctlbuf, nilp(struct strbuf), flags) == -1) {
98 perror("mibget: putmsg(ctl) failed");
99 goto error_exit;
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);
109 for (j = 1; ; j++) {
110 flags = 0;
111 getcode = getmsg(sd, &ctlbuf, nilp(struct strbuf), &flags);
112 if (getcode == -1) {
113 perror("mibget getmsg(ctl) failed");
114 if (debug) {
115 msgout("# level name len");
116 i = 0;
117 for (last_item = first_item; last_item;
118 last_item = last_item->next_item)
119 msgout("%d %4ld %5ld %ld", ++i,
120 last_item->group,
121 last_item->mib_id,
122 last_item->length);
124 goto error_exit;
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) &&
130 (req->len == 0)) {
131 if (debug)
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;
145 goto error_exit;
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, "
157 "req->len = %lu",
158 toa->MGMT_flags, req->len);
159 errno = ENOMSG;
160 goto error_exit;
163 temp = (mib_item_t *)malloc(sizeof (mib_item_t));
164 if (!temp) {
165 perror("mibget malloc failed");
166 goto error_exit;
168 if (last_item)
169 last_item->next_item = temp;
170 else
171 first_item = temp;
172 last_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);
178 if (debug)
179 msgout(
180 "msg %d: group = %4ld mib_id = %5ld length = %ld",
181 j, last_item->group, last_item->mib_id,
182 last_item->length);
184 databuf.maxlen = last_item->length;
185 databuf.buf = last_item->valp;
186 databuf.len = 0;
187 flags = 0;
188 getcode = getmsg(sd, nilp(struct strbuf), &databuf, &flags);
189 if (getcode == -1) {
190 perror("mibget getmsg(data) failed");
191 goto error_exit;
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);
196 goto error_exit;
200 error_exit:
201 free_itemlist(first_item);
202 return (NULL);
205 static void
206 free_itemlist(mib_item_t *item_list)
208 mib_item_t *item;
210 while (item_list) {
211 item = item_list;
212 item_list = item->next_item;
213 free(item->valp);
214 free(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.
227 in_addr_t
228 get_ip_route(struct in_addr client_addr)
230 boolean_t found;
231 mib_item_t *item_list;
232 mib_item_t *item;
233 int sd;
234 mib2_ip_t *mip;
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);
251 if (sd == -1) {
252 perror("ip open");
253 (void) close(sd);
254 msgout("can't open mib stream");
255 return (0);
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");
261 (void) close(sd);
262 return (0);
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;
281 break;
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;
299 int ifflags;
301 ifflags = map->ipAdEntInfo.ae_flags;
302 addr = map->ipAdEntAddr;
303 mask = map->ipAdEntNetMask;
304 net = addr & mask;
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;
311 found = B_TRUE;
312 break;
314 map++;
320 * If this exercise found no interface on the same subnet as
321 * the client, then we can't suggest any router address to
322 * use.
324 if (interface_addr.s_addr == 0) {
325 if (debug)
326 msgout("get_ip_route: no interface on same net "
327 "as client");
328 (void) close(sd);
329 free_itemlist(item_list);
330 return (0);
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) {
338 if (debug)
339 msgout("get_ip_route: returning local addr %s",
340 inet_ntoa(interface_addr));
341 (void) close(sd);
342 free_itemlist(item_list);
343 return (interface_addr.s_addr);
346 if (debug) {
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)) {
357 if (debug)
358 msgout("%lu records for ipRouteEntryTable",
359 item->length /
360 sizeof (mib2_ipRouteEntry_t));
362 for (rp = (mib2_ipRouteEntry_t *)(void *)item->valp;
363 (char *)rp < item->valp + item->length;
364 rp++) {
365 if (debug >= 2)
366 msgout("ire_type = %d, next_hop = 0x%x",
367 rp->ipRouteInfo.re_ire_type,
368 rp->ipRouteNextHop);
371 * We are only interested in real
372 * gateway routes.
374 if ((rp->ipRouteInfo.re_ire_type !=
375 IRE_DEFAULT) &&
376 (rp->ipRouteInfo.re_ire_type !=
377 IRE_PREFIX) &&
378 (rp->ipRouteInfo.re_ire_type !=
379 IRE_HOST) &&
380 (rp->ipRouteInfo.re_ire_type !=
381 IRE_HOST_REDIRECT))
382 continue;
385 * We are only interested in routes with
386 * a next hop on the same subnet as
387 * the client.
389 if ((rp->ipRouteNextHop &
390 interface_mask.s_addr) !=
391 (interface_addr.s_addr &
392 interface_mask.s_addr))
393 continue;
396 * We have a valid route. Give preference
397 * to default routes.
399 if ((rp->ipRouteDest == 0) ||
400 (best_router.s_addr == 0))
401 best_router.s_addr =
402 rp->ipRouteNextHop;
407 if (debug && (best_router.s_addr == 0))
408 msgout("get_ip_route: no route found for client");
410 (void) close(sd);
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.
423 in_addr_t
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];
429 int err;
430 struct in_addr client_addr;
431 mib_item_t *item_list;
432 mib_item_t *item;
433 int sd;
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);
443 if (hp == NULL)
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);
449 if (sd == -1) {
450 perror("ip open");
451 (void) close(sd);
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");
459 (void) close(sd);
460 return (server_addr);
462 (void) close(sd);
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;
479 int ifflags;
481 ifflags = map->ipAdEntInfo.ae_flags;
482 addr = map->ipAdEntAddr;
483 mask = map->ipAdEntNetMask;
484 net = addr & mask;
486 if ((ifflags & IFF_LOOPBACK|IFF_UP) == IFF_UP &&
487 addr != INADDR_ANY &&
488 (client_addr.s_addr & mask) == net) {
489 client_net = net;
490 client_mask = mask;
491 found_client_int = B_TRUE;
492 break;
494 map++;
500 * If we found the interface check which is the best IP address.
502 if (found_client_int) {
503 while (*addr_list != NULL) {
504 in_addr_t addr;
506 (void) memcpy(&addr, *addr_list, sizeof (in_addr_t));
507 if ((addr & client_mask) == client_net) {
508 server_addr = addr;
509 break;
511 addr_list++;
515 if (debug && server_addr == 0)
516 msgout("No usable interface for returning reply");
518 free_itemlist(item_list);
519 return (server_addr);