ipforward_solaris.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. * ipforward value get function for solaris.
  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 "prefix.h"
  25. #include "privs.h"
  26. /*
  27. ** Solaris should define IP_DEV_NAME in <inet/ip.h>, but we'll save
  28. ** configure.in changes for another day. We can use the same device
  29. ** for both IPv4 and IPv6.
  30. */
  31. /* #include <inet/ip.h> */
  32. #ifndef IP_DEV_NAME
  33. #define IP_DEV_NAME "/dev/ip"
  34. #endif
  35. extern struct zebra_privs_t zserv_privs;
  36. /* This is a limited ndd style function that operates one integer
  37. ** value only. Errors return -1. ND_SET commands return 0 on
  38. ** success. ND_GET commands return the value on success (which could
  39. ** be -1 and be confused for an error). The parameter is the string
  40. ** name of the parameter being referenced.
  41. */
  42. static int
  43. solaris_nd(const int cmd, const char* parameter, const int value)
  44. {
  45. #define ND_BUFFER_SIZE 1024
  46. int fd;
  47. char nd_buf[ND_BUFFER_SIZE];
  48. struct strioctl strioctl;
  49. const char* device = IP_DEV_NAME;
  50. int retval;
  51. memset(nd_buf, '\0', ND_BUFFER_SIZE);
  52. /*
  53. ** ND_SET takes a NULL delimited list of strings further terminated
  54. ** buy a NULL. ND_GET returns a list in a similar layout, although
  55. ** here we only use the first result.
  56. */
  57. if (cmd == ND_SET)
  58. snprintf(nd_buf, ND_BUFFER_SIZE, "%s%c%d%c", parameter, '\0', value,'\0');
  59. else if (cmd == ND_GET)
  60. snprintf(nd_buf, ND_BUFFER_SIZE, "%s", parameter);
  61. else {
  62. zlog_err("internal error - inappropriate command given to "
  63. "solaris_nd()%s:%d", __FILE__, __LINE__);
  64. return -1;
  65. }
  66. strioctl.ic_cmd = cmd;
  67. strioctl.ic_timout = 0;
  68. strioctl.ic_len = ND_BUFFER_SIZE;
  69. strioctl.ic_dp = nd_buf;
  70. if ( zserv_privs.change (ZPRIVS_RAISE) )
  71. zlog_err ("solaris_nd: Can't raise privileges");
  72. if ((fd = open (device, O_RDWR)) < 0)
  73. {
  74. zlog_warn("failed to open device %s - %s", device, strerror(errno));
  75. if ( zserv_privs.change (ZPRIVS_LOWER) )
  76. zlog_err ("solaris_nd: Can't lower privileges");
  77. return -1;
  78. }
  79. if (ioctl (fd, I_STR, &strioctl) < 0)
  80. {
  81. if ( zserv_privs.change (ZPRIVS_LOWER) )
  82. zlog_err ("solaris_nd: Can't lower privileges");
  83. close (fd);
  84. zlog_warn("ioctl I_STR failed on device %s - %s", device,strerror(errno));
  85. return -1;
  86. }
  87. close(fd);
  88. if ( zserv_privs.change (ZPRIVS_LOWER) )
  89. zlog_err ("solaris_nd: Can't lower privileges");
  90. if (cmd == ND_GET)
  91. {
  92. errno = 0;
  93. retval = atoi(nd_buf);
  94. if (errno)
  95. {
  96. zlog_warn("failed to convert returned value to integer - %s",
  97. strerror(errno));
  98. retval = -1;
  99. }
  100. }
  101. else
  102. {
  103. retval = 0;
  104. }
  105. return retval;
  106. }
  107. static int
  108. solaris_nd_set(const char* parameter, const int value) {
  109. return solaris_nd(ND_SET, parameter, value);
  110. }
  111. static int
  112. solaris_nd_get(const char* parameter) {
  113. return solaris_nd(ND_GET, parameter, 0);
  114. }
  115. int
  116. ipforward()
  117. {
  118. return solaris_nd_get("ip_forwarding");
  119. }
  120. int
  121. ipforward_on ()
  122. {
  123. (void) solaris_nd_set("ip_forwarding", 1);
  124. return ipforward();
  125. }
  126. int
  127. ipforward_off ()
  128. {
  129. (void) solaris_nd_set("ip_forwarding", 0);
  130. return ipforward();
  131. }
  132. #ifdef HAVE_IPV6
  133. int ipforward_ipv6()
  134. {
  135. return solaris_nd_get("ip6_forwarding");
  136. }
  137. int
  138. ipforward_ipv6_on ()
  139. {
  140. (void) solaris_nd_set("ip6_forwarding", 1);
  141. return ipforward_ipv6();
  142. }
  143. int
  144. ipforward_ipv6_off ()
  145. {
  146. (void) solaris_nd_set("ip6_forwarding", 0);
  147. return ipforward_ipv6();
  148. }
  149. #endif /* HAVE_IPV6 */