blob: 8d6fd98b8c6b626c55e7378f9f0b007ab5e0c13e [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"
28
paul269c67c2003-05-24 22:50:31 +000029/*
30** Solaris should define IP_DEV_NAME in <inet/ip.h>, but we'll save
31** configure.in changes for another day. We can use the same device
32** for both IPv4 and IPv6.
33*/
34/* #include <inet/ip.h> */
35#ifndef IP_DEV_NAME
36#define IP_DEV_NAME "/dev/ip"
37#endif
paul9bcdb632003-07-08 08:09:45 +000038
pauledd7c242003-06-04 13:59:38 +000039
40extern struct zebra_privs_t zserv_privs;
41
paul9bcdb632003-07-08 08:09:45 +000042/* This is a limited ndd style function that operates one integer
paul269c67c2003-05-24 22:50:31 +000043** value only. Errors return -1. ND_SET commands return 0 on
44** success. ND_GET commands return the value on success (which could
45** be -1 and be confused for an error). The parameter is the string
46** name of the parameter being referenced.
47*/
paul718e3742002-12-13 20:15:29 +000048
paul269c67c2003-05-24 22:50:31 +000049static int
50solaris_nd(const int cmd, const char* parameter, const int value)
paul718e3742002-12-13 20:15:29 +000051{
paul269c67c2003-05-24 22:50:31 +000052#define ND_BUFFER_SIZE 1024
53 int fd;
54 char nd_buf[ND_BUFFER_SIZE];
55 struct strioctl strioctl;
56 const char* device = IP_DEV_NAME;
57 int retval;
58 memset(nd_buf, '\0', ND_BUFFER_SIZE);
59 /*
60 ** ND_SET takes a NULL delimited list of strings further terminated
61 ** buy a NULL. ND_GET returns a list in a similar layout, although
62 ** here we only use the first result.
63 */
paul66e94882004-05-09 17:19:24 +000064 if (cmd == ND_SET)
paul269c67c2003-05-24 22:50:31 +000065 snprintf(nd_buf, ND_BUFFER_SIZE, "%s%c%d%c", parameter, '\0', value,'\0');
paul66e94882004-05-09 17:19:24 +000066 else if (cmd == ND_GET)
paul269c67c2003-05-24 22:50:31 +000067 snprintf(nd_buf, ND_BUFFER_SIZE, "%s", parameter);
paul66e94882004-05-09 17:19:24 +000068 else {
69 zlog_err("internal error - inappropriate command given to "
70 "solaris_nd()%s:%d", __FILE__, __LINE__);
paul718e3742002-12-13 20:15:29 +000071 return -1;
72 }
pauledd7c242003-06-04 13:59:38 +000073
paul269c67c2003-05-24 22:50:31 +000074 strioctl.ic_cmd = cmd;
75 strioctl.ic_timout = 0;
76 strioctl.ic_len = ND_BUFFER_SIZE;
77 strioctl.ic_dp = nd_buf;
pauledd7c242003-06-04 13:59:38 +000078
79 if ( zserv_privs.change (ZPRIVS_RAISE) )
80 zlog_err ("solaris_nd: Can't raise privileges");
81 if ((fd = open (device, O_RDWR)) < 0)
82 {
83 zlog_warn("failed to open device %s - %s", device, strerror(errno));
84 if ( zserv_privs.change (ZPRIVS_LOWER) )
85 zlog_err ("solaris_nd: Can't lower privileges");
86 return -1;
paul269c67c2003-05-24 22:50:31 +000087 }
pauledd7c242003-06-04 13:59:38 +000088 if (ioctl (fd, I_STR, &strioctl) < 0)
89 {
90 if ( zserv_privs.change (ZPRIVS_LOWER) )
91 zlog_err ("solaris_nd: Can't lower privileges");
92 close (fd);
93 zlog_warn("ioctl I_STR failed on device %s - %s", device,strerror(errno));
94 return -1;
95 }
96 close(fd);
97 if ( zserv_privs.change (ZPRIVS_LOWER) )
98 zlog_err ("solaris_nd: Can't lower privileges");
99
100 if (cmd == ND_GET)
101 {
102 errno = 0;
103 retval = atoi(nd_buf);
104 if (errno)
105 {
106 zlog_warn("failed to convert returned value to integer - %s",
107 strerror(errno));
108 retval = -1;
109 }
110 }
111 else
112 {
113 retval = 0;
114 }
paul269c67c2003-05-24 22:50:31 +0000115 return retval;
116}
paul718e3742002-12-13 20:15:29 +0000117
paul269c67c2003-05-24 22:50:31 +0000118static int
119solaris_nd_set(const char* parameter, const int value) {
120 return solaris_nd(ND_SET, parameter, value);
121}
122static int
123solaris_nd_get(const char* parameter) {
124 return solaris_nd(ND_GET, parameter, 0);
125}
126int
127ipforward()
128{
129 return solaris_nd_get("ip_forwarding");
paul718e3742002-12-13 20:15:29 +0000130}
131
132int
133ipforward_on ()
134{
paul269c67c2003-05-24 22:50:31 +0000135 (void) solaris_nd_set("ip_forwarding", 1);
136 return ipforward();
paul718e3742002-12-13 20:15:29 +0000137}
138
139int
140ipforward_off ()
141{
paul269c67c2003-05-24 22:50:31 +0000142 (void) solaris_nd_set("ip_forwarding", 0);
143 return ipforward();
paul718e3742002-12-13 20:15:29 +0000144}
paul269c67c2003-05-24 22:50:31 +0000145#ifdef HAVE_IPV6
146int ipforward_ipv6()
147{
paul66e94882004-05-09 17:19:24 +0000148 return solaris_nd_get("ip6_forwarding");
paul269c67c2003-05-24 22:50:31 +0000149}
150int
151ipforward_ipv6_on ()
152{
153 (void) solaris_nd_set("ip6_forwarding", 1);
154 return ipforward_ipv6();
155}
156int
157ipforward_ipv6_off ()
158{
159 (void) solaris_nd_set("ip6_forwarding", 0);
160 return ipforward_ipv6();
161}
162#endif /* HAVE_IPV6 */