rtread_getmsg.c 6.2 KB

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