2 * Fetch ipforward value by reading /proc filesystem.
3 * Copyright (C) 1997 Kunihiro Ishiguro
5 * This file is part of GNU Zebra.
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
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.
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
29 #include "zebra/ipforward.h"
31 extern struct zebra_privs_t zserv_privs
;
33 char proc_net_snmp
[] = "/proc/net/snmp";
35 static void dropline(FILE *fp
)
39 while ((c
= getc(fp
)) != '\n')
49 fp
= fopen(proc_net_snmp
, "r");
54 /* We don't care about the first line. */
57 /* Get ip_statistics.IpForwarding :
58 1 => ip forwarding enabled
59 2 => ip forwarding off. */
60 if (fgets(buf
, 6, fp
))
61 sscanf(buf
, "Ip: %d", &ipforwarding
);
65 if (ipforwarding
== 1)
71 /* char proc_ipv4_forwarding[] = "/proc/sys/net/ipv4/conf/all/forwarding"; */
72 char proc_ipv4_forwarding
[] = "/proc/sys/net/ipv4/ip_forward";
74 int ipforward_on(void)
78 if (zserv_privs
.change(ZPRIVS_RAISE
))
79 zlog_err("Can't raise privileges, %s", safe_strerror(errno
));
81 fp
= fopen(proc_ipv4_forwarding
, "w");
84 if (zserv_privs
.change(ZPRIVS_LOWER
))
85 zlog_err("Can't lower privileges, %s",
86 safe_strerror(errno
));
94 if (zserv_privs
.change(ZPRIVS_LOWER
))
95 zlog_err("Can't lower privileges, %s", safe_strerror(errno
));
100 int ipforward_off(void)
104 if (zserv_privs
.change(ZPRIVS_RAISE
))
105 zlog_err("Can't raise privileges, %s", safe_strerror(errno
));
107 fp
= fopen(proc_ipv4_forwarding
, "w");
110 if (zserv_privs
.change(ZPRIVS_LOWER
))
111 zlog_err("Can't lower privileges, %s",
112 safe_strerror(errno
));
120 if (zserv_privs
.change(ZPRIVS_LOWER
))
121 zlog_err("Can't lower privileges, %s", safe_strerror(errno
));
126 char proc_ipv6_forwarding
[] = "/proc/sys/net/ipv6/conf/all/forwarding";
128 int ipforward_ipv6(void)
132 int ipforwarding
= 0;
134 fp
= fopen(proc_ipv6_forwarding
, "r");
139 if (fgets(buf
, 2, fp
))
140 sscanf(buf
, "%d", &ipforwarding
);
146 int ipforward_ipv6_on(void)
150 if (zserv_privs
.change(ZPRIVS_RAISE
))
151 zlog_err("Can't raise privileges, %s", safe_strerror(errno
));
153 fp
= fopen(proc_ipv6_forwarding
, "w");
156 if (zserv_privs
.change(ZPRIVS_LOWER
))
157 zlog_err("Can't lower privileges, %s",
158 safe_strerror(errno
));
166 if (zserv_privs
.change(ZPRIVS_LOWER
))
167 zlog_err("Can't lower privileges, %s", safe_strerror(errno
));
169 return ipforward_ipv6();
173 int ipforward_ipv6_off(void)
177 if (zserv_privs
.change(ZPRIVS_RAISE
))
178 zlog_err("Can't raise privileges, %s", safe_strerror(errno
));
180 fp
= fopen(proc_ipv6_forwarding
, "w");
183 if (zserv_privs
.change(ZPRIVS_LOWER
))
184 zlog_err("Can't lower privileges, %s",
185 safe_strerror(errno
));
193 if (zserv_privs
.change(ZPRIVS_LOWER
))
194 zlog_err("Can't lower privileges, %s", safe_strerror(errno
));
196 return ipforward_ipv6();
199 #endif /* GNU_LINUX */