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