rtread_getmsg.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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 (struct zebra_vrf *zvrf)
  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 (zvrf->vrf_id != VRF_DEFAULT) {
  93. return;
  94. }
  95. if ((dev = open (_PATH_GETMSG_ROUTE, O_RDWR)) == -1) {
  96. zlog_warn ("can't open %s: %s", _PATH_GETMSG_ROUTE,
  97. safe_strerror (errno));
  98. return;
  99. }
  100. TLIreq->PRIM_type = T_OPTMGMT_REQ;
  101. TLIreq->OPT_offset = sizeof (struct T_optmgmt_req);
  102. TLIreq->OPT_length = sizeof (struct opthdr);
  103. TLIreq->MGMT_flags = T_CURRENT;
  104. MIB2hdr = (struct opthdr *) &TLIreq[1];
  105. MIB2hdr->level = MIB2_IP;
  106. MIB2hdr->name = 0;
  107. MIB2hdr->len = 0;
  108. msgdata.buf = storage;
  109. msgdata.len = sizeof (struct T_optmgmt_req) + sizeof (struct opthdr);
  110. flags = 0;
  111. if (putmsg (dev, &msgdata, NULL, flags) == -1) {
  112. zlog_warn ("putmsg failed: %s", safe_strerror (errno));
  113. goto exit;
  114. }
  115. MIB2hdr = (struct opthdr *) &TLIack[1];
  116. msgdata.maxlen = sizeof (storage);
  117. while (1) {
  118. flags = 0;
  119. retval = getmsg (dev, &msgdata, NULL, &flags);
  120. if (retval == -1) {
  121. zlog_warn ("getmsg(ctl) failed: %s", safe_strerror (errno));
  122. goto exit;
  123. }
  124. /* This is normal loop termination */
  125. if (retval == 0 &&
  126. msgdata.len >= sizeof (struct T_optmgmt_ack) &&
  127. TLIack->PRIM_type == T_OPTMGMT_ACK &&
  128. TLIack->MGMT_flags == T_SUCCESS &&
  129. MIB2hdr->len == 0)
  130. break;
  131. if (msgdata.len >= sizeof (struct T_error_ack) &&
  132. TLIerr->PRIM_type == T_ERROR_ACK) {
  133. zlog_warn ("getmsg(ctl) returned T_ERROR_ACK: %s",
  134. safe_strerror ((TLIerr->TLI_error == TSYSERR)
  135. ? TLIerr->UNIX_error : EPROTO));
  136. break;
  137. }
  138. /* should dump more debugging info to the log statement,
  139. like what GateD does in this instance, but not
  140. critical yet. */
  141. if (retval != MOREDATA ||
  142. msgdata.len < sizeof (struct T_optmgmt_ack) ||
  143. TLIack->PRIM_type != T_OPTMGMT_ACK ||
  144. TLIack->MGMT_flags != T_SUCCESS) {
  145. errno = ENOMSG;
  146. zlog_warn ("getmsg(ctl) returned bizarreness");
  147. break;
  148. }
  149. /* MIB2_IP_21 is the the pseudo-MIB2 ipRouteTable
  150. entry, see <inet/mib2.h>. "This isn't the MIB data
  151. you're looking for." */
  152. process = (MIB2hdr->level == MIB2_IP &&
  153. MIB2hdr->name == MIB2_IP_21) ? 1 : 0;
  154. /* getmsg writes the data buffer out completely, not
  155. to the closest smaller multiple. Unless reassembling
  156. data structures across buffer boundaries is your idea
  157. of a good time, set maxlen to the closest smaller
  158. multiple of the size of the datastructure you're
  159. retrieving. */
  160. msgdata.maxlen = sizeof (storage) - (sizeof (storage)
  161. % sizeof (mib2_ipRouteEntry_t));
  162. msgdata.len = 0;
  163. flags = 0;
  164. do {
  165. retval = getmsg (dev, NULL, &msgdata, &flags);
  166. if (retval == -1) {
  167. zlog_warn ("getmsg(data) failed: %s",
  168. safe_strerror (errno));
  169. goto exit;
  170. }
  171. if (!(retval == 0 || retval == MOREDATA)) {
  172. zlog_warn ("getmsg(data) returned %d", retval);
  173. goto exit;
  174. }
  175. if (process) {
  176. if (msgdata.len %
  177. sizeof (mib2_ipRouteEntry_t) != 0) {
  178. zlog_warn ("getmsg(data) returned "
  179. "msgdata.len = %d (%% sizeof (mib2_ipRouteEntry_t) != 0)", msgdata.len);
  180. goto exit;
  181. }
  182. routeEntry = (mib2_ipRouteEntry_t *)
  183. msgdata.buf;
  184. lastRouteEntry = (mib2_ipRouteEntry_t *)
  185. (msgdata.buf + msgdata.len);
  186. do {
  187. handle_route_entry (routeEntry);
  188. } while (++routeEntry < lastRouteEntry);
  189. }
  190. } while (retval == MOREDATA);
  191. }
  192. exit:
  193. close (dev);
  194. }