File:  [ELWIX - Embedded LightWeight unIX -] / embedaddon / quagga / zebra / rtread_getmsg.c
Revision 1.1.1.2 (vendor branch): download - view: text, annotated - select for diffs - revision graph
Tue Oct 9 09:22:29 2012 UTC (11 years, 9 months ago) by misho
Branches: quagga, MAIN
CVS tags: v0_99_22p0, v0_99_22, v0_99_21, HEAD
quagga

    1: /*
    2:  * Kernel routing table readup by getmsg(2)
    3:  * Copyright (C) 1999 Michael Handler
    4:  *
    5:  * This file is part of GNU Zebra.
    6:  *
    7:  * GNU Zebra is free software; you can redistribute it and/or modify it
    8:  * under the terms of the GNU General Public License as published by the
    9:  * Free Software Foundation; either version 2, or (at your option) any
   10:  * later version.
   11:  *
   12:  * GNU Zebra is distributed in the hope that it will be useful, but
   13:  * WITHOUT ANY WARRANTY; without even the implied warranty of
   14:  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   15:  * General Public License for more details.
   16:  *
   17:  * You should have received a copy of the GNU General Public License
   18:  * along with GNU Zebra; see the file COPYING.  If not, write to the Free
   19:  * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
   20:  * 02111-1307, USA.  
   21:  */
   22: 
   23: #include <zebra.h>
   24: 
   25: #include "prefix.h"
   26: #include "log.h"
   27: #include "if.h"
   28: 
   29: #include "zebra/rib.h"
   30: #include "zebra/zserv.h"
   31: 
   32: #include <sys/stream.h>
   33: #include <sys/tihdr.h>
   34: 
   35: /* Solaris defines these in both <netinet/in.h> and <inet/in.h>, sigh */
   36: #ifdef SUNOS_5
   37: #include <sys/tiuser.h>
   38: #ifndef T_CURRENT
   39: #define T_CURRENT       MI_T_CURRENT
   40: #endif /* T_CURRENT */
   41: #ifndef IRE_CACHE
   42: #define IRE_CACHE               0x0020  /* Cached Route entry */
   43: #endif /* IRE_CACHE */
   44: #ifndef IRE_HOST_REDIRECT
   45: #define IRE_HOST_REDIRECT       0x0200  /* Host route entry from redirects */
   46: #endif /* IRE_HOST_REDIRECT */
   47: #ifndef IRE_CACHETABLE
   48: #define IRE_CACHETABLE          (IRE_CACHE | IRE_BROADCAST | IRE_LOCAL | \
   49:                                  IRE_LOOPBACK)
   50: #endif /* IRE_CACHETABLE */
   51: #undef IPOPT_EOL
   52: #undef IPOPT_NOP
   53: #undef IPOPT_LSRR
   54: #undef IPOPT_RR
   55: #undef IPOPT_SSRR
   56: #endif /* SUNOS_5 */
   57: 
   58: #include <inet/common.h>
   59: #include <inet/ip.h>
   60: #include <inet/mib2.h>
   61: 
   62: /* device to read IP routing table from */
   63: #ifndef _PATH_GETMSG_ROUTE
   64: #define _PATH_GETMSG_ROUTE	"/dev/ip"
   65: #endif /* _PATH_GETMSG_ROUTE */
   66: 
   67: #define RT_BUFSIZ		8192
   68: 
   69: static void 
   70: handle_route_entry (mib2_ipRouteEntry_t *routeEntry)
   71: {
   72: 	struct prefix_ipv4	prefix;
   73:  	struct in_addr		tmpaddr, gateway;
   74: 	u_char			zebra_flags = 0;
   75: 
   76: 	if (routeEntry->ipRouteInfo.re_ire_type & IRE_CACHETABLE)
   77: 		return;
   78: 
   79: 	if (routeEntry->ipRouteInfo.re_ire_type & IRE_HOST_REDIRECT)
   80: 		zebra_flags |= ZEBRA_FLAG_SELFROUTE;
   81: 
   82: 	prefix.family = AF_INET;
   83: 
   84: 	tmpaddr.s_addr = routeEntry->ipRouteDest;
   85: 	prefix.prefix = tmpaddr;
   86: 
   87: 	tmpaddr.s_addr = routeEntry->ipRouteMask;
   88: 	prefix.prefixlen = ip_masklen (tmpaddr);
   89: 
   90: 	gateway.s_addr = routeEntry->ipRouteNextHop;
   91: 
   92: 	rib_add_ipv4 (ZEBRA_ROUTE_KERNEL, zebra_flags, &prefix,
   93: 		      &gateway, NULL, 0, 0, 0, 0, SAFI_UNICAST);
   94: }
   95: 
   96: void
   97: route_read (void)
   98: {
   99: 	char 			storage[RT_BUFSIZ];
  100: 
  101: 	struct T_optmgmt_req	*TLIreq = (struct T_optmgmt_req *) storage;
  102: 	struct T_optmgmt_ack	*TLIack = (struct T_optmgmt_ack *) storage;
  103: 	struct T_error_ack	*TLIerr = (struct T_error_ack *) storage;
  104: 
  105: 	struct opthdr		*MIB2hdr;
  106: 
  107: 	mib2_ipRouteEntry_t	*routeEntry, *lastRouteEntry;
  108: 
  109: 	struct strbuf		msgdata;
  110: 	int			flags, dev, retval, process;
  111: 
  112: 	if ((dev = open (_PATH_GETMSG_ROUTE, O_RDWR)) == -1) {
  113: 		zlog_warn ("can't open %s: %s", _PATH_GETMSG_ROUTE,
  114: 			safe_strerror (errno));
  115: 		return;
  116: 	}
  117: 
  118: 	TLIreq->PRIM_type = T_OPTMGMT_REQ;
  119: 	TLIreq->OPT_offset = sizeof (struct T_optmgmt_req);
  120: 	TLIreq->OPT_length = sizeof (struct opthdr);
  121: 	TLIreq->MGMT_flags = T_CURRENT;
  122: 
  123: 	MIB2hdr = (struct opthdr *) &TLIreq[1];
  124: 
  125: 	MIB2hdr->level = MIB2_IP;
  126: 	MIB2hdr->name = 0;
  127: 	MIB2hdr->len = 0;
  128: 	
  129: 	msgdata.buf = storage;
  130: 	msgdata.len = sizeof (struct T_optmgmt_req) + sizeof (struct opthdr);
  131: 
  132: 	flags = 0;
  133: 
  134: 	if (putmsg (dev, &msgdata, NULL, flags) == -1) {
  135: 		zlog_warn ("putmsg failed: %s", safe_strerror (errno));
  136: 		goto exit;
  137: 	}
  138: 
  139: 	MIB2hdr = (struct opthdr *) &TLIack[1];
  140: 	msgdata.maxlen = sizeof (storage);
  141: 
  142: 	while (1) {
  143: 		flags = 0;
  144: 		retval = getmsg (dev, &msgdata, NULL, &flags);
  145: 
  146: 		if (retval == -1) {
  147: 			zlog_warn ("getmsg(ctl) failed: %s", safe_strerror (errno));
  148: 			goto exit;
  149: 		}
  150: 
  151: 		/* This is normal loop termination */
  152: 		if (retval == 0 &&
  153: 			msgdata.len >= sizeof (struct T_optmgmt_ack) &&
  154: 			TLIack->PRIM_type == T_OPTMGMT_ACK &&
  155: 			TLIack->MGMT_flags == T_SUCCESS &&
  156: 			MIB2hdr->len == 0)
  157: 			break;
  158: 
  159: 		if (msgdata.len >= sizeof (struct T_error_ack) &&
  160: 			TLIerr->PRIM_type == T_ERROR_ACK) {
  161: 			zlog_warn ("getmsg(ctl) returned T_ERROR_ACK: %s",
  162: 				safe_strerror ((TLIerr->TLI_error == TSYSERR)
  163: 				? TLIerr->UNIX_error : EPROTO));
  164: 			break;
  165: 		}
  166: 
  167: 		/* should dump more debugging info to the log statement,
  168: 		   like what GateD does in this instance, but not
  169: 		   critical yet. */
  170: 		if (retval != MOREDATA ||
  171: 			msgdata.len < sizeof (struct T_optmgmt_ack) ||
  172: 			TLIack->PRIM_type != T_OPTMGMT_ACK ||
  173: 			TLIack->MGMT_flags != T_SUCCESS) {
  174: 			errno = ENOMSG;
  175: 			zlog_warn ("getmsg(ctl) returned bizarreness");
  176: 			break;
  177: 		}
  178: 
  179: 		/* MIB2_IP_21 is the the pseudo-MIB2 ipRouteTable
  180: 		   entry, see <inet/mib2.h>. "This isn't the MIB data
  181: 		   you're looking for." */
  182: 		process = (MIB2hdr->level == MIB2_IP &&
  183: 			MIB2hdr->name == MIB2_IP_21) ? 1 : 0;
  184: 
  185: 		/* getmsg writes the data buffer out completely, not
  186: 		   to the closest smaller multiple. Unless reassembling
  187: 		   data structures across buffer boundaries is your idea
  188: 		   of a good time, set maxlen to the closest smaller
  189: 		   multiple of the size of the datastructure you're
  190: 		   retrieving. */
  191: 		msgdata.maxlen = sizeof (storage) - (sizeof (storage)
  192: 			% sizeof (mib2_ipRouteEntry_t));
  193: 
  194: 		msgdata.len = 0;
  195: 		flags = 0;
  196: 
  197: 		do {
  198: 			retval = getmsg (dev, NULL, &msgdata, &flags);
  199: 
  200: 			if (retval == -1) {
  201: 				zlog_warn ("getmsg(data) failed: %s",
  202: 					safe_strerror (errno));
  203: 				goto exit;
  204: 			}
  205: 
  206: 			if (!(retval == 0 || retval == MOREDATA)) {
  207: 				zlog_warn ("getmsg(data) returned %d", retval);
  208: 				goto exit;
  209: 			}
  210: 
  211: 			if (process) {
  212: 				if (msgdata.len %
  213: 					sizeof (mib2_ipRouteEntry_t) != 0) {
  214: 					zlog_warn ("getmsg(data) returned "
  215: "msgdata.len = %d (%% sizeof (mib2_ipRouteEntry_t) != 0)", msgdata.len);
  216: 					goto exit;
  217: 				}
  218: 
  219: 				routeEntry = (mib2_ipRouteEntry_t *)
  220: 					msgdata.buf;
  221: 				lastRouteEntry = (mib2_ipRouteEntry_t *)
  222: 					(msgdata.buf + msgdata.len);
  223: 				do {
  224: 					handle_route_entry (routeEntry);
  225: 				} while (++routeEntry < lastRouteEntry);
  226: 			}
  227: 		} while (retval == MOREDATA);
  228: 	}
  229: 
  230: exit:
  231: 	close (dev);
  232: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>