rtread_getmsg.c 6.3 KB

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