ipforward_solaris.c 4.3 KB

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