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