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