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
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
28 #include "zebra/ipforward.h"
30 extern struct zebra_privs_t zserv_privs;
32 char proc_net_snmp[] = "/proc/net/snmp";
39 while ((c = getc (fp)) != '\n')
50 fp = fopen (proc_net_snmp, "r");
55 /* We don't care about the first line. */
58 /* Get ip_statistics.IpForwarding :
59 1 => ip forwarding enabled
60 2 => ip forwarding off. */
61 if (fgets (buf, 6, fp))
62 sscanf (buf, "Ip: %d", &ipforwarding);
66 if (ipforwarding == 1)
72 /* char proc_ipv4_forwarding[] = "/proc/sys/net/ipv4/conf/all/forwarding"; */
73 char proc_ipv4_forwarding[] = "/proc/sys/net/ipv4/ip_forward";
80 if ( zserv_privs.change(ZPRIVS_RAISE) )
81 zlog_err ("Can't raise privileges, %s", safe_strerror (errno) );
83 fp = fopen (proc_ipv4_forwarding, "w");
86 if ( zserv_privs.change(ZPRIVS_LOWER) )
87 zlog_err ("Can't lower privileges, %s", safe_strerror (errno));
95 if ( zserv_privs.change(ZPRIVS_LOWER) )
96 zlog_err ("Can't lower privileges, %s", safe_strerror (errno));
106 if ( zserv_privs.change(ZPRIVS_RAISE) )
107 zlog_err ("Can't raise privileges, %s", safe_strerror (errno));
109 fp = fopen (proc_ipv4_forwarding, "w");
112 if ( zserv_privs.change(ZPRIVS_LOWER) )
113 zlog_err ("Can't lower privileges, %s", safe_strerror (errno));
121 if ( zserv_privs.change(ZPRIVS_LOWER) )
122 zlog_err ("Can't lower privileges, %s", safe_strerror (errno));
128 char proc_ipv6_forwarding[] = "/proc/sys/net/ipv6/conf/all/forwarding";
131 ipforward_ipv6 (void)
135 int ipforwarding = 0;
137 fp = fopen (proc_ipv6_forwarding, "r");
142 if (fgets (buf, 2, fp))
143 sscanf (buf, "%d", &ipforwarding);
150 ipforward_ipv6_on (void)
154 if ( zserv_privs.change(ZPRIVS_RAISE) )
155 zlog_err ("Can't raise privileges, %s", safe_strerror (errno));
157 fp = fopen (proc_ipv6_forwarding, "w");
160 if ( zserv_privs.change(ZPRIVS_LOWER) )
161 zlog_err ("Can't lower privileges, %s", safe_strerror (errno));
169 if ( zserv_privs.change(ZPRIVS_LOWER) )
170 zlog_err ("Can't lower privileges, %s", safe_strerror (errno));
172 return ipforward_ipv6 ();
176 ipforward_ipv6_off (void)
180 if ( zserv_privs.change(ZPRIVS_RAISE) )
181 zlog_err ("Can't raise privileges, %s", safe_strerror (errno));
183 fp = fopen (proc_ipv6_forwarding, "w");
186 if ( zserv_privs.change(ZPRIVS_LOWER) )
187 zlog_err ("Can't lower privileges, %s", safe_strerror (errno));
195 if ( zserv_privs.change(ZPRIVS_LOWER) )
196 zlog_err ("Can't lower privileges, %s", safe_strerror (errno));
198 return ipforward_ipv6 ();
200 #endif /* HAVE_IPV6 */