rtread_proc.c 4.4 KB

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