rtread_proc.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. * Kernel routing readup by /proc filesystem
  3. * Copyright (C) 1997 Kunihiro Ishiguro
  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 "rib.h"
  27. /* Proc file system to read IPv4 routing table. */
  28. #ifndef _PATH_PROCNET_ROUTE
  29. #define _PATH_PROCNET_ROUTE "/proc/net/route"
  30. #endif /* _PATH_PROCNET_ROUTE */
  31. /* Proc file system to read IPv6 routing table. */
  32. #ifndef _PATH_PROCNET_ROUTE6
  33. #define _PATH_PROCNET_ROUTE6 "/proc/net/ipv6_route"
  34. #endif /* _PATH_PROCNET_ROUTE6 */
  35. /* To read interface's name */
  36. #define INTERFACE_NAMSIZ 20
  37. /* Reading buffer for one routing entry. */
  38. #define RT_BUFSIZ 1024
  39. /* Kernel routing table read up by /proc filesystem. */
  40. int
  41. proc_route_read ()
  42. {
  43. FILE *fp;
  44. char buf[RT_BUFSIZ];
  45. char iface[INTERFACE_NAMSIZ], dest[9], gate[9], mask[9];
  46. int flags, refcnt, use, metric, mtu, window, rtt;
  47. /* Open /proc filesystem */
  48. fp = fopen (_PATH_PROCNET_ROUTE, "r");
  49. if (fp == NULL)
  50. {
  51. zlog_warn ("Can't open %s : %s\n", _PATH_PROCNET_ROUTE, strerror (errno));
  52. return -1;
  53. }
  54. /* Drop first label line. */
  55. fgets (buf, RT_BUFSIZ, fp);
  56. while (fgets (buf, RT_BUFSIZ, fp) != NULL)
  57. {
  58. int n;
  59. struct prefix_ipv4 p;
  60. struct in_addr tmpmask;
  61. struct in_addr gateway;
  62. u_char zebra_flags = 0;
  63. n = sscanf (buf, "%s %s %s %x %d %d %d %s %d %d %d",
  64. iface, dest, gate, &flags, &refcnt, &use, &metric,
  65. mask, &mtu, &window, &rtt);
  66. if (n != 11)
  67. {
  68. zlog_warn ("can't read all of routing information\n");
  69. continue;
  70. }
  71. if (! (flags & RTF_UP))
  72. continue;
  73. if (! (flags & RTF_GATEWAY))
  74. continue;
  75. if (flags & RTF_DYNAMIC)
  76. zebra_flags |= ZEBRA_FLAG_SELFROUTE;
  77. p.family = AF_INET;
  78. sscanf (dest, "%lX", (unsigned long *)&p.prefix);
  79. sscanf (mask, "%lX", (unsigned long *)&tmpmask);
  80. p.prefixlen = ip_masklen (tmpmask);
  81. sscanf (gate, "%lX", (unsigned long *)&gateway);
  82. rib_add_ipv4 (ZEBRA_ROUTE_KERNEL, zebra_flags, &p, &gateway, 0, 0, 0, 0);
  83. }
  84. return 0;
  85. }
  86. #ifdef HAVE_IPV6
  87. int
  88. proc_ipv6_route_read ()
  89. {
  90. FILE *fp;
  91. char buf [RT_BUFSIZ];
  92. /* Open /proc filesystem */
  93. fp = fopen (_PATH_PROCNET_ROUTE6, "r");
  94. if (fp == NULL)
  95. {
  96. zlog_warn ("Can't open %s : %s", _PATH_PROCNET_ROUTE6,
  97. strerror (errno));
  98. return -1;
  99. }
  100. /* There is no title line, so we don't drop first line. */
  101. while (fgets (buf, RT_BUFSIZ, fp) != NULL)
  102. {
  103. int n;
  104. char dest[33], src[33], gate[33];
  105. char iface[INTERFACE_NAMSIZ];
  106. int dest_plen, src_plen;
  107. int metric, use, refcnt, flags;
  108. struct prefix_ipv6 p;
  109. struct in6_addr gateway;
  110. u_char zebra_flags = 0;
  111. /* Linux 2.1.x write this information at net/ipv6/route.c
  112. rt6_info_node () */
  113. n = sscanf (buf, "%32s %02x %32s %02x %32s %08x %08x %08x %08x %s",
  114. dest, &dest_plen, src, &src_plen, gate,
  115. &metric, &use, &refcnt, &flags, iface);
  116. if (n != 10)
  117. {
  118. /* zlog_warn ("can't read all of routing information %d\n%s\n", n, buf); */
  119. continue;
  120. }
  121. if (! (flags & RTF_UP))
  122. continue;
  123. if (! (flags & RTF_GATEWAY))
  124. continue;
  125. if (flags & RTF_DYNAMIC)
  126. zebra_flags |= ZEBRA_FLAG_SELFROUTE;
  127. p.family = AF_INET6;
  128. str2in6_addr (dest, &p.prefix);
  129. str2in6_addr (gate, &gateway);
  130. p.prefixlen = dest_plen;
  131. rib_add_ipv6 (ZEBRA_ROUTE_KERNEL, zebra_flags, &p, &gateway, 0, 0);
  132. }
  133. return 0;
  134. }
  135. #endif /* HAVE_IPV6 */
  136. void
  137. route_read ()
  138. {
  139. proc_route_read ();
  140. #ifdef HAVE_IPV6
  141. proc_ipv6_route_read ();
  142. #endif /* HAVE_IPV6 */
  143. }