blob: 4aa1b797afcf87e656d8aaf73d66af6e430bfb6e [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
paul9bcdb632003-07-08 08:09:45 +000027#include "privs.h"
paula1ac18c2005-06-28 17:17:12 +000028#include "zebra/ipforward.h"
paul9bcdb632003-07-08 08:09:45 +000029
paul269c67c2003-05-24 22:50:31 +000030/*
31** Solaris should define IP_DEV_NAME in <inet/ip.h>, but we'll save
32** configure.in changes for another day. We can use the same device
33** for both IPv4 and IPv6.
34*/
35/* #include <inet/ip.h> */
36#ifndef IP_DEV_NAME
37#define IP_DEV_NAME "/dev/ip"
38#endif
paul9bcdb632003-07-08 08:09:45 +000039
pauledd7c242003-06-04 13:59:38 +000040
41extern struct zebra_privs_t zserv_privs;
42
paul9bcdb632003-07-08 08:09:45 +000043/* This is a limited ndd style function that operates one integer
paul269c67c2003-05-24 22:50:31 +000044** value only. Errors return -1. ND_SET commands return 0 on
45** success. ND_GET commands return the value on success (which could
46** be -1 and be confused for an error). The parameter is the string
47** name of the parameter being referenced.
48*/
paul718e3742002-12-13 20:15:29 +000049
paul269c67c2003-05-24 22:50:31 +000050static int
51solaris_nd(const int cmd, const char* parameter, const int value)
paul718e3742002-12-13 20:15:29 +000052{
paul269c67c2003-05-24 22:50:31 +000053#define ND_BUFFER_SIZE 1024
54 int fd;
55 char nd_buf[ND_BUFFER_SIZE];
56 struct strioctl strioctl;
57 const char* device = IP_DEV_NAME;
58 int retval;
59 memset(nd_buf, '\0', ND_BUFFER_SIZE);
60 /*
61 ** ND_SET takes a NULL delimited list of strings further terminated
62 ** buy a NULL. ND_GET returns a list in a similar layout, although
63 ** here we only use the first result.
64 */
paul66e94882004-05-09 17:19:24 +000065 if (cmd == ND_SET)
paul269c67c2003-05-24 22:50:31 +000066 snprintf(nd_buf, ND_BUFFER_SIZE, "%s%c%d%c", parameter, '\0', value,'\0');
paul66e94882004-05-09 17:19:24 +000067 else if (cmd == ND_GET)
paul269c67c2003-05-24 22:50:31 +000068 snprintf(nd_buf, ND_BUFFER_SIZE, "%s", parameter);
paul66e94882004-05-09 17:19:24 +000069 else {
70 zlog_err("internal error - inappropriate command given to "
71 "solaris_nd()%s:%d", __FILE__, __LINE__);
paul718e3742002-12-13 20:15:29 +000072 return -1;
73 }
pauledd7c242003-06-04 13:59:38 +000074
paul269c67c2003-05-24 22:50:31 +000075 strioctl.ic_cmd = cmd;
76 strioctl.ic_timout = 0;
77 strioctl.ic_len = ND_BUFFER_SIZE;
78 strioctl.ic_dp = nd_buf;
pauledd7c242003-06-04 13:59:38 +000079
80 if ( zserv_privs.change (ZPRIVS_RAISE) )
81 zlog_err ("solaris_nd: Can't raise privileges");
82 if ((fd = open (device, O_RDWR)) < 0)
83 {
ajs6099b3b2004-11-20 02:06:59 +000084 zlog_warn("failed to open device %s - %s", device, safe_strerror(errno));
pauledd7c242003-06-04 13:59:38 +000085 if ( zserv_privs.change (ZPRIVS_LOWER) )
86 zlog_err ("solaris_nd: Can't lower privileges");
87 return -1;
paul269c67c2003-05-24 22:50:31 +000088 }
pauledd7c242003-06-04 13:59:38 +000089 if (ioctl (fd, I_STR, &strioctl) < 0)
90 {
ajs4460e7a2005-01-29 17:07:40 +000091 int save_errno = errno;
pauledd7c242003-06-04 13:59:38 +000092 if ( zserv_privs.change (ZPRIVS_LOWER) )
93 zlog_err ("solaris_nd: Can't lower privileges");
94 close (fd);
ajs4460e7a2005-01-29 17:07:40 +000095 zlog_warn("ioctl I_STR failed on device %s - %s",
96 device, safe_strerror(save_errno));
pauledd7c242003-06-04 13:59:38 +000097 return -1;
98 }
99 close(fd);
100 if ( zserv_privs.change (ZPRIVS_LOWER) )
101 zlog_err ("solaris_nd: Can't lower privileges");
102
103 if (cmd == ND_GET)
104 {
105 errno = 0;
106 retval = atoi(nd_buf);
107 if (errno)
108 {
109 zlog_warn("failed to convert returned value to integer - %s",
ajs6099b3b2004-11-20 02:06:59 +0000110 safe_strerror(errno));
pauledd7c242003-06-04 13:59:38 +0000111 retval = -1;
112 }
113 }
114 else
115 {
116 retval = 0;
117 }
paul269c67c2003-05-24 22:50:31 +0000118 return retval;
119}
paul718e3742002-12-13 20:15:29 +0000120
paul269c67c2003-05-24 22:50:31 +0000121static int
122solaris_nd_set(const char* parameter, const int value) {
123 return solaris_nd(ND_SET, parameter, value);
124}
125static int
126solaris_nd_get(const char* parameter) {
127 return solaris_nd(ND_GET, parameter, 0);
128}
129int
paula1ac18c2005-06-28 17:17:12 +0000130ipforward(void)
paul269c67c2003-05-24 22:50:31 +0000131{
132 return solaris_nd_get("ip_forwarding");
paul718e3742002-12-13 20:15:29 +0000133}
134
135int
paula1ac18c2005-06-28 17:17:12 +0000136ipforward_on (void)
paul718e3742002-12-13 20:15:29 +0000137{
paul269c67c2003-05-24 22:50:31 +0000138 (void) solaris_nd_set("ip_forwarding", 1);
139 return ipforward();
paul718e3742002-12-13 20:15:29 +0000140}
141
142int
paula1ac18c2005-06-28 17:17:12 +0000143ipforward_off (void)
paul718e3742002-12-13 20:15:29 +0000144{
paul269c67c2003-05-24 22:50:31 +0000145 (void) solaris_nd_set("ip_forwarding", 0);
146 return ipforward();
paul718e3742002-12-13 20:15:29 +0000147}
paul269c67c2003-05-24 22:50:31 +0000148#ifdef HAVE_IPV6
paula1ac18c2005-06-28 17:17:12 +0000149int ipforward_ipv6(void)
paul269c67c2003-05-24 22:50:31 +0000150{
paul66e94882004-05-09 17:19:24 +0000151 return solaris_nd_get("ip6_forwarding");
paul269c67c2003-05-24 22:50:31 +0000152}
153int
paula1ac18c2005-06-28 17:17:12 +0000154ipforward_ipv6_on (void)
paul269c67c2003-05-24 22:50:31 +0000155{
156 (void) solaris_nd_set("ip6_forwarding", 1);
157 return ipforward_ipv6();
158}
159int
paula1ac18c2005-06-28 17:17:12 +0000160ipforward_ipv6_off (void)
paul269c67c2003-05-24 22:50:31 +0000161{
162 (void) solaris_nd_set("ip6_forwarding", 0);
163 return ipforward_ipv6();
164}
165#endif /* HAVE_IPV6 */