rtread_getmsg.c 6.0 KB

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