ipforward_proc.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /*
  2. * Fetch ipforward value by reading /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 "log.h"
  24. #include "privs.h"
  25. #include "zebra/ipforward.h"
  26. extern struct zebra_privs_t zserv_privs;
  27. char proc_net_snmp[] = "/proc/net/snmp";
  28. static void
  29. dropline (FILE *fp)
  30. {
  31. int c;
  32. while ((c = getc (fp)) != '\n')
  33. ;
  34. }
  35. int
  36. ipforward (void)
  37. {
  38. FILE *fp;
  39. int ipforwarding = 0;
  40. char buf[10];
  41. fp = fopen (proc_net_snmp, "r");
  42. if (fp == NULL)
  43. return -1;
  44. /* We don't care about the first line. */
  45. dropline (fp);
  46. /* Get ip_statistics.IpForwarding :
  47. 1 => ip forwarding enabled
  48. 2 => ip forwarding off. */
  49. if (fgets (buf, 6, fp))
  50. sscanf (buf, "Ip: %d", &ipforwarding);
  51. fclose(fp);
  52. if (ipforwarding == 1)
  53. return 1;
  54. return 0;
  55. }
  56. /* char proc_ipv4_forwarding[] = "/proc/sys/net/ipv4/conf/all/forwarding"; */
  57. char proc_ipv4_forwarding[] = "/proc/sys/net/ipv4/ip_forward";
  58. int
  59. ipforward_on (void)
  60. {
  61. FILE *fp;
  62. if ( zserv_privs.change(ZPRIVS_RAISE) )
  63. zlog_err ("Can't raise privileges, %s", safe_strerror (errno) );
  64. fp = fopen (proc_ipv4_forwarding, "w");
  65. if (fp == NULL) {
  66. if ( zserv_privs.change(ZPRIVS_LOWER) )
  67. zlog_err ("Can't lower privileges, %s", safe_strerror (errno));
  68. return -1;
  69. }
  70. fprintf (fp, "1\n");
  71. fclose (fp);
  72. if ( zserv_privs.change(ZPRIVS_LOWER) )
  73. zlog_err ("Can't lower privileges, %s", safe_strerror (errno));
  74. return ipforward ();
  75. }
  76. int
  77. ipforward_off (void)
  78. {
  79. FILE *fp;
  80. if ( zserv_privs.change(ZPRIVS_RAISE) )
  81. zlog_err ("Can't raise privileges, %s", safe_strerror (errno));
  82. fp = fopen (proc_ipv4_forwarding, "w");
  83. if (fp == NULL) {
  84. if ( zserv_privs.change(ZPRIVS_LOWER) )
  85. zlog_err ("Can't lower privileges, %s", safe_strerror (errno));
  86. return -1;
  87. }
  88. fprintf (fp, "0\n");
  89. fclose (fp);
  90. if ( zserv_privs.change(ZPRIVS_LOWER) )
  91. zlog_err ("Can't lower privileges, %s", safe_strerror (errno));
  92. return ipforward ();
  93. }
  94. #ifdef HAVE_IPV6
  95. char proc_ipv6_forwarding[] = "/proc/sys/net/ipv6/conf/all/forwarding";
  96. int
  97. ipforward_ipv6 (void)
  98. {
  99. FILE *fp;
  100. char buf[5];
  101. int ipforwarding = 0;
  102. fp = fopen (proc_ipv6_forwarding, "r");
  103. if (fp == NULL)
  104. return -1;
  105. if (fgets (buf, 2, fp))
  106. sscanf (buf, "%d", &ipforwarding);
  107. fclose (fp);
  108. return ipforwarding;
  109. }
  110. int
  111. ipforward_ipv6_on (void)
  112. {
  113. FILE *fp;
  114. if ( zserv_privs.change(ZPRIVS_RAISE) )
  115. zlog_err ("Can't raise privileges, %s", safe_strerror (errno));
  116. fp = fopen (proc_ipv6_forwarding, "w");
  117. if (fp == NULL) {
  118. if ( zserv_privs.change(ZPRIVS_LOWER) )
  119. zlog_err ("Can't lower privileges, %s", safe_strerror (errno));
  120. return -1;
  121. }
  122. fprintf (fp, "1\n");
  123. fclose (fp);
  124. if ( zserv_privs.change(ZPRIVS_LOWER) )
  125. zlog_err ("Can't lower privileges, %s", safe_strerror (errno));
  126. return ipforward_ipv6 ();
  127. }
  128. int
  129. ipforward_ipv6_off (void)
  130. {
  131. FILE *fp;
  132. if ( zserv_privs.change(ZPRIVS_RAISE) )
  133. zlog_err ("Can't raise privileges, %s", safe_strerror (errno));
  134. fp = fopen (proc_ipv6_forwarding, "w");
  135. if (fp == NULL) {
  136. if ( zserv_privs.change(ZPRIVS_LOWER) )
  137. zlog_err ("Can't lower privileges, %s", safe_strerror (errno));
  138. return -1;
  139. }
  140. fprintf (fp, "0\n");
  141. fclose (fp);
  142. if ( zserv_privs.change(ZPRIVS_LOWER) )
  143. zlog_err ("Can't lower privileges, %s", safe_strerror (errno));
  144. return ipforward_ipv6 ();
  145. }
  146. #endif /* HAVE_IPV6 */