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