rtread_getmsg.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. #include <zebra.h>
  23. #include "prefix.h"
  24. #include "log.h"
  25. #include "if.h"
  26. #include "zebra/rib.h"
  27. #include "zebra/zserv.h"
  28. #include <sys/stream.h>
  29. #include <sys/tihdr.h>
  30. /* Solaris defines these in both <netinet/in.h> and <inet/in.h>, sigh */
  31. #ifdef SUNOS_5
  32. #include <sys/tiuser.h>
  33. #ifndef T_CURRENT
  34. #define T_CURRENT MI_T_CURRENT
  35. #endif /* T_CURRENT */
  36. #ifndef IRE_CACHE
  37. #define IRE_CACHE 0x0020 /* Cached Route entry */
  38. #endif /* IRE_CACHE */
  39. #ifndef IRE_HOST_REDIRECT
  40. #define IRE_HOST_REDIRECT 0x0200 /* Host route entry from redirects */
  41. #endif /* IRE_HOST_REDIRECT */
  42. #ifndef IRE_CACHETABLE
  43. #define IRE_CACHETABLE (IRE_CACHE | IRE_BROADCAST | IRE_LOCAL | \
  44. IRE_LOOPBACK)
  45. #endif /* IRE_CACHETABLE */
  46. #undef IPOPT_EOL
  47. #undef IPOPT_NOP
  48. #undef IPOPT_LSRR
  49. #undef IPOPT_RR
  50. #undef IPOPT_SSRR
  51. #endif /* SUNOS_5 */
  52. #include <inet/common.h>
  53. #include <inet/ip.h>
  54. #include <inet/mib2.h>
  55. /* device to read IP routing table from */
  56. #ifndef _PATH_GETMSG_ROUTE
  57. #define _PATH_GETMSG_ROUTE "/dev/ip"
  58. #endif /* _PATH_GETMSG_ROUTE */
  59. #define RT_BUFSIZ 8192
  60. static void
  61. handle_route_entry (mib2_ipRouteEntry_t *routeEntry)
  62. {
  63. struct prefix_ipv4 prefix;
  64. struct in_addr tmpaddr, gateway;
  65. u_char zebra_flags = 0;
  66. if (routeEntry->ipRouteInfo.re_ire_type & IRE_CACHETABLE)
  67. return;
  68. if (routeEntry->ipRouteInfo.re_ire_type & IRE_HOST_REDIRECT)
  69. zebra_flags |= ZEBRA_FLAG_SELFROUTE;
  70. prefix.family = AF_INET;
  71. tmpaddr.s_addr = routeEntry->ipRouteDest;
  72. prefix.prefix = tmpaddr;
  73. tmpaddr.s_addr = routeEntry->ipRouteMask;
  74. prefix.prefixlen = ip_masklen (tmpaddr);
  75. gateway.s_addr = routeEntry->ipRouteNextHop;
  76. rib_add_ipv4 (ZEBRA_ROUTE_KERNEL, zebra_flags, &prefix,
  77. &gateway, NULL, 0, 0, 0, 0, SAFI_UNICAST);
  78. }
  79. void
  80. route_read (void)
  81. {
  82. char storage[RT_BUFSIZ];
  83. struct T_optmgmt_req *TLIreq = (struct T_optmgmt_req *) storage;
  84. struct T_optmgmt_ack *TLIack = (struct T_optmgmt_ack *) storage;
  85. struct T_error_ack *TLIerr = (struct T_error_ack *) storage;
  86. struct opthdr *MIB2hdr;
  87. mib2_ipRouteEntry_t *routeEntry, *lastRouteEntry;
  88. struct strbuf msgdata;
  89. int flags, dev, retval, process;
  90. if ((dev = open (_PATH_GETMSG_ROUTE, O_RDWR)) == -1) {
  91. zlog_warn ("can't open %s: %s", _PATH_GETMSG_ROUTE,
  92. safe_strerror (errno));
  93. return;
  94. }
  95. TLIreq->PRIM_type = T_OPTMGMT_REQ;
  96. TLIreq->OPT_offset = sizeof (struct T_optmgmt_req);
  97. TLIreq->OPT_length = sizeof (struct opthdr);
  98. TLIreq->MGMT_flags = T_CURRENT;
  99. MIB2hdr = (struct opthdr *) &TLIreq[1];
  100. MIB2hdr->level = MIB2_IP;
  101. MIB2hdr->name = 0;
  102. MIB2hdr->len = 0;
  103. msgdata.buf = storage;
  104. msgdata.len = sizeof (struct T_optmgmt_req) + sizeof (struct opthdr);
  105. flags = 0;
  106. if (putmsg (dev, &msgdata, NULL, flags) == -1) {
  107. zlog_warn ("putmsg failed: %s", safe_strerror (errno));
  108. goto exit;
  109. }
  110. MIB2hdr = (struct opthdr *) &TLIack[1];
  111. msgdata.maxlen = sizeof (storage);
  112. while (1) {
  113. flags = 0;
  114. retval = getmsg (dev, &msgdata, NULL, &flags);
  115. if (retval == -1) {
  116. zlog_warn ("getmsg(ctl) failed: %s", safe_strerror (errno));
  117. goto exit;
  118. }
  119. /* This is normal loop termination */
  120. if (retval == 0 &&
  121. msgdata.len >= sizeof (struct T_optmgmt_ack) &&
  122. TLIack->PRIM_type == T_OPTMGMT_ACK &&
  123. TLIack->MGMT_flags == T_SUCCESS &&
  124. MIB2hdr->len == 0)
  125. break;
  126. if (msgdata.len >= sizeof (struct T_error_ack) &&
  127. TLIerr->PRIM_type == T_ERROR_ACK) {
  128. zlog_warn ("getmsg(ctl) returned T_ERROR_ACK: %s",
  129. safe_strerror ((TLIerr->TLI_error == TSYSERR)
  130. ? TLIerr->UNIX_error : EPROTO));
  131. break;
  132. }
  133. /* should dump more debugging info to the log statement,
  134. like what GateD does in this instance, but not
  135. critical yet. */
  136. if (retval != MOREDATA ||
  137. msgdata.len < sizeof (struct T_optmgmt_ack) ||
  138. TLIack->PRIM_type != T_OPTMGMT_ACK ||
  139. TLIack->MGMT_flags != T_SUCCESS) {
  140. errno = ENOMSG;
  141. zlog_warn ("getmsg(ctl) returned bizarreness");
  142. break;
  143. }
  144. /* MIB2_IP_21 is the the pseudo-MIB2 ipRouteTable
  145. entry, see <inet/mib2.h>. "This isn't the MIB data
  146. you're looking for." */
  147. process = (MIB2hdr->level == MIB2_IP &&
  148. MIB2hdr->name == MIB2_IP_21) ? 1 : 0;
  149. /* getmsg writes the data buffer out completely, not
  150. to the closest smaller multiple. Unless reassembling
  151. data structures across buffer boundaries is your idea
  152. of a good time, set maxlen to the closest smaller
  153. multiple of the size of the datastructure you're
  154. retrieving. */
  155. msgdata.maxlen = sizeof (storage) - (sizeof (storage)
  156. % sizeof (mib2_ipRouteEntry_t));
  157. msgdata.len = 0;
  158. flags = 0;
  159. do {
  160. retval = getmsg (dev, NULL, &msgdata, &flags);
  161. if (retval == -1) {
  162. zlog_warn ("getmsg(data) failed: %s",
  163. safe_strerror (errno));
  164. goto exit;
  165. }
  166. if (!(retval == 0 || retval == MOREDATA)) {
  167. zlog_warn ("getmsg(data) returned %d", retval);
  168. goto exit;
  169. }
  170. if (process) {
  171. if (msgdata.len %
  172. sizeof (mib2_ipRouteEntry_t) != 0) {
  173. zlog_warn ("getmsg(data) returned "
  174. "msgdata.len = %d (%% sizeof (mib2_ipRouteEntry_t) != 0)", msgdata.len);
  175. goto exit;
  176. }
  177. routeEntry = (mib2_ipRouteEntry_t *)
  178. msgdata.buf;
  179. lastRouteEntry = (mib2_ipRouteEntry_t *)
  180. (msgdata.buf + msgdata.len);
  181. do {
  182. handle_route_entry (routeEntry);
  183. } while (++routeEntry < lastRouteEntry);
  184. }
  185. } while (retval == MOREDATA);
  186. }
  187. exit:
  188. close (dev);
  189. }