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 | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 25 | |
paul | 269c67c | 2003-05-24 22:50:31 +0000 | [diff] [blame] | 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 | /* |
| 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 | */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 42 | |
paul | 269c67c | 2003-05-24 22:50:31 +0000 | [diff] [blame] | 43 | static int |
| 44 | solaris_nd(const int cmd, const char* parameter, const int value) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 45 | { |
paul | 269c67c | 2003-05-24 22:50:31 +0000 | [diff] [blame] | 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 solaris_nd()%s:%d", __FILE__, __LINE__); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 64 | return -1; |
| 65 | } |
paul | 269c67c | 2003-05-24 22:50:31 +0000 | [diff] [blame] | 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 ((fd = open (device, O_RDWR)) < 0) { |
| 71 | zlog_warn("failed to open device %s - %s", device, strerror(errno)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 72 | return -1; |
| 73 | } |
paul | 269c67c | 2003-05-24 22:50:31 +0000 | [diff] [blame] | 74 | if (ioctl (fd, I_STR, &strioctl) < 0) { |
| 75 | close (fd); |
| 76 | zlog_warn("ioctl I_STR failed on device %s - %s", device,strerror(errno)); |
| 77 | return -1; |
| 78 | } |
| 79 | close(fd); |
| 80 | if (cmd == ND_GET) { |
| 81 | errno = 0; |
| 82 | retval = atoi(nd_buf); |
| 83 | if (errno) { |
| 84 | zlog_warn("failed to convert returned value to integer - %s",strerror(errno)); |
| 85 | retval = -1; |
| 86 | } |
| 87 | } else { |
| 88 | retval = 0; |
| 89 | } |
| 90 | return retval; |
| 91 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 92 | |
paul | 269c67c | 2003-05-24 22:50:31 +0000 | [diff] [blame] | 93 | static int |
| 94 | solaris_nd_set(const char* parameter, const int value) { |
| 95 | return solaris_nd(ND_SET, parameter, value); |
| 96 | } |
| 97 | static int |
| 98 | solaris_nd_get(const char* parameter) { |
| 99 | return solaris_nd(ND_GET, parameter, 0); |
| 100 | } |
| 101 | int |
| 102 | ipforward() |
| 103 | { |
| 104 | return solaris_nd_get("ip_forwarding"); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | int |
| 108 | ipforward_on () |
| 109 | { |
paul | 269c67c | 2003-05-24 22:50:31 +0000 | [diff] [blame] | 110 | (void) solaris_nd_set("ip_forwarding", 1); |
| 111 | return ipforward(); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | int |
| 115 | ipforward_off () |
| 116 | { |
paul | 269c67c | 2003-05-24 22:50:31 +0000 | [diff] [blame] | 117 | (void) solaris_nd_set("ip_forwarding", 0); |
| 118 | return ipforward(); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 119 | } |
paul | 269c67c | 2003-05-24 22:50:31 +0000 | [diff] [blame] | 120 | #ifdef HAVE_IPV6 |
| 121 | int ipforward_ipv6() |
| 122 | { |
| 123 | return solaris_nd_get("ip6_fowarding"); |
| 124 | } |
| 125 | int |
| 126 | ipforward_ipv6_on () |
| 127 | { |
| 128 | (void) solaris_nd_set("ip6_forwarding", 1); |
| 129 | return ipforward_ipv6(); |
| 130 | } |
| 131 | int |
| 132 | ipforward_ipv6_off () |
| 133 | { |
| 134 | (void) solaris_nd_set("ip6_forwarding", 0); |
| 135 | return ipforward_ipv6(); |
| 136 | } |
| 137 | #endif /* HAVE_IPV6 */ |