blob: fe06e74da3724ed4e0d97b4f6d9ec620919f5c5e [file] [log] [blame]
paul718e3742002-12-13 20:15:29 +00001/*
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>
paul269c67c2003-05-24 22:50:31 +000024#include "log.h"
pauledd7c242003-06-04 13:59:38 +000025#include "prefix.h"
paul718e3742002-12-13 20:15:29 +000026
paul269c67c2003-05-24 22:50:31 +000027/*
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/*
pauledd7c242003-06-04 13:59:38 +000037
38extern struct zebra_privs_t zserv_privs;
39
paul269c67c2003-05-24 22:50:31 +000040** 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*/
paul718e3742002-12-13 20:15:29 +000046
paul269c67c2003-05-24 22:50:31 +000047static int
48solaris_nd(const int cmd, const char* parameter, const int value)
paul718e3742002-12-13 20:15:29 +000049{
paul269c67c2003-05-24 22:50:31 +000050#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__);
paul718e3742002-12-13 20:15:29 +000068 return -1;
69 }
pauledd7c242003-06-04 13:59:38 +000070
paul269c67c2003-05-24 22:50:31 +000071 strioctl.ic_cmd = cmd;
72 strioctl.ic_timout = 0;
73 strioctl.ic_len = ND_BUFFER_SIZE;
74 strioctl.ic_dp = nd_buf;
pauledd7c242003-06-04 13:59:38 +000075
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;
paul269c67c2003-05-24 22:50:31 +000084 }
pauledd7c242003-06-04 13:59:38 +000085 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 }
paul269c67c2003-05-24 22:50:31 +0000112 return retval;
113}
paul718e3742002-12-13 20:15:29 +0000114
paul269c67c2003-05-24 22:50:31 +0000115static int
116solaris_nd_set(const char* parameter, const int value) {
117 return solaris_nd(ND_SET, parameter, value);
118}
119static int
120solaris_nd_get(const char* parameter) {
121 return solaris_nd(ND_GET, parameter, 0);
122}
123int
124ipforward()
125{
126 return solaris_nd_get("ip_forwarding");
paul718e3742002-12-13 20:15:29 +0000127}
128
129int
130ipforward_on ()
131{
paul269c67c2003-05-24 22:50:31 +0000132 (void) solaris_nd_set("ip_forwarding", 1);
133 return ipforward();
paul718e3742002-12-13 20:15:29 +0000134}
135
136int
137ipforward_off ()
138{
paul269c67c2003-05-24 22:50:31 +0000139 (void) solaris_nd_set("ip_forwarding", 0);
140 return ipforward();
paul718e3742002-12-13 20:15:29 +0000141}
paul269c67c2003-05-24 22:50:31 +0000142#ifdef HAVE_IPV6
143int ipforward_ipv6()
144{
145 return solaris_nd_get("ip6_fowarding");
146}
147int
148ipforward_ipv6_on ()
149{
150 (void) solaris_nd_set("ip6_forwarding", 1);
151 return ipforward_ipv6();
152}
153int
154ipforward_ipv6_off ()
155{
156 (void) solaris_nd_set("ip6_forwarding", 0);
157 return ipforward_ipv6();
158}
159#endif /* HAVE_IPV6 */