blob: 63d1110cbcf864b2fe184998996ee4c5b7b5b916 [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"
paul718e3742002-12-13 20:15:29 +000025
paul269c67c2003-05-24 22:50:31 +000026/*
27** Solaris should define IP_DEV_NAME in <inet/ip.h>, but we'll save
28** configure.in changes for another day. We can use the same device
29** for both IPv4 and IPv6.
30*/
31/* #include <inet/ip.h> */
32#ifndef IP_DEV_NAME
33#define IP_DEV_NAME "/dev/ip"
34#endif
35/*
36** This is a limited ndd style function that operates one integer
37** value only. Errors return -1. ND_SET commands return 0 on
38** success. ND_GET commands return the value on success (which could
39** be -1 and be confused for an error). The parameter is the string
40** name of the parameter being referenced.
41*/
paul718e3742002-12-13 20:15:29 +000042
paul269c67c2003-05-24 22:50:31 +000043static int
44solaris_nd(const int cmd, const char* parameter, const int value)
paul718e3742002-12-13 20:15:29 +000045{
paul269c67c2003-05-24 22:50:31 +000046#define ND_BUFFER_SIZE 1024
47 int fd;
48 char nd_buf[ND_BUFFER_SIZE];
49 struct strioctl strioctl;
50 const char* device = IP_DEV_NAME;
51 int retval;
52 memset(nd_buf, '\0', ND_BUFFER_SIZE);
53 /*
54 ** ND_SET takes a NULL delimited list of strings further terminated
55 ** buy a NULL. ND_GET returns a list in a similar layout, although
56 ** here we only use the first result.
57 */
58 if (cmd == ND_SET) {
59 snprintf(nd_buf, ND_BUFFER_SIZE, "%s%c%d%c", parameter, '\0', value,'\0');
60 } else if (cmd == ND_GET) {
61 snprintf(nd_buf, ND_BUFFER_SIZE, "%s", parameter);
62 } else {
63 zlog_err("internal error - inappropriate command given to solaris_nd()%s:%d", __FILE__, __LINE__);
paul718e3742002-12-13 20:15:29 +000064 return -1;
65 }
paul269c67c2003-05-24 22:50:31 +000066 strioctl.ic_cmd = cmd;
67 strioctl.ic_timout = 0;
68 strioctl.ic_len = ND_BUFFER_SIZE;
69 strioctl.ic_dp = nd_buf;
70 if ((fd = open (device, O_RDWR)) < 0) {
71 zlog_warn("failed to open device %s - %s", device, strerror(errno));
paul718e3742002-12-13 20:15:29 +000072 return -1;
73 }
paul269c67c2003-05-24 22:50:31 +000074 if (ioctl (fd, I_STR, &strioctl) < 0) {
75 close (fd);
76 zlog_warn("ioctl I_STR failed on device %s - %s", device,strerror(errno));
77 return -1;
78 }
79 close(fd);
80 if (cmd == ND_GET) {
81 errno = 0;
82 retval = atoi(nd_buf);
83 if (errno) {
84 zlog_warn("failed to convert returned value to integer - %s",strerror(errno));
85 retval = -1;
86 }
87 } else {
88 retval = 0;
89 }
90 return retval;
91}
paul718e3742002-12-13 20:15:29 +000092
paul269c67c2003-05-24 22:50:31 +000093static int
94solaris_nd_set(const char* parameter, const int value) {
95 return solaris_nd(ND_SET, parameter, value);
96}
97static int
98solaris_nd_get(const char* parameter) {
99 return solaris_nd(ND_GET, parameter, 0);
100}
101int
102ipforward()
103{
104 return solaris_nd_get("ip_forwarding");
paul718e3742002-12-13 20:15:29 +0000105}
106
107int
108ipforward_on ()
109{
paul269c67c2003-05-24 22:50:31 +0000110 (void) solaris_nd_set("ip_forwarding", 1);
111 return ipforward();
paul718e3742002-12-13 20:15:29 +0000112}
113
114int
115ipforward_off ()
116{
paul269c67c2003-05-24 22:50:31 +0000117 (void) solaris_nd_set("ip_forwarding", 0);
118 return ipforward();
paul718e3742002-12-13 20:15:29 +0000119}
paul269c67c2003-05-24 22:50:31 +0000120#ifdef HAVE_IPV6
121int ipforward_ipv6()
122{
123 return solaris_nd_get("ip6_fowarding");
124}
125int
126ipforward_ipv6_on ()
127{
128 (void) solaris_nd_set("ip6_forwarding", 1);
129 return ipforward_ipv6();
130}
131int
132ipforward_ipv6_off ()
133{
134 (void) solaris_nd_set("ip6_forwarding", 0);
135 return ipforward_ipv6();
136}
137#endif /* HAVE_IPV6 */