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
27 #include "zebra/ipforward.h"
29 extern struct zebra_privs_t zserv_privs
;
31 char proc_net_snmp
[] = "/proc/net/snmp";
38 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";
79 if ( zserv_privs
.change(ZPRIVS_RAISE
) )
80 zlog_err ("Can't raise privileges, %s", safe_strerror (errno
) );
82 fp
= fopen (proc_ipv4_forwarding
, "w");
85 if ( zserv_privs
.change(ZPRIVS_LOWER
) )
86 zlog_err ("Can't lower privileges, %s", safe_strerror (errno
));
94 if ( zserv_privs
.change(ZPRIVS_LOWER
) )
95 zlog_err ("Can't lower privileges, %s", safe_strerror (errno
));
105 if ( zserv_privs
.change(ZPRIVS_RAISE
) )
106 zlog_err ("Can't raise privileges, %s", safe_strerror (errno
));
108 fp
= fopen (proc_ipv4_forwarding
, "w");
111 if ( zserv_privs
.change(ZPRIVS_LOWER
) )
112 zlog_err ("Can't lower privileges, %s", 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";
129 ipforward_ipv6 (void)
133 int ipforwarding
= 0;
135 fp
= fopen (proc_ipv6_forwarding
, "r");
140 if (fgets (buf
, 2, fp
))
141 sscanf (buf
, "%d", &ipforwarding
);
148 ipforward_ipv6_on (void)
152 if ( zserv_privs
.change(ZPRIVS_RAISE
) )
153 zlog_err ("Can't raise privileges, %s", safe_strerror (errno
));
155 fp
= fopen (proc_ipv6_forwarding
, "w");
158 if ( zserv_privs
.change(ZPRIVS_LOWER
) )
159 zlog_err ("Can't lower privileges, %s", safe_strerror (errno
));
167 if ( zserv_privs
.change(ZPRIVS_LOWER
) )
168 zlog_err ("Can't lower privileges, %s", safe_strerror (errno
));
170 return ipforward_ipv6 ();
175 ipforward_ipv6_off (void)
179 if ( zserv_privs
.change(ZPRIVS_RAISE
) )
180 zlog_err ("Can't raise privileges, %s", safe_strerror (errno
));
182 fp
= fopen (proc_ipv6_forwarding
, "w");
185 if ( zserv_privs
.change(ZPRIVS_LOWER
) )
186 zlog_err ("Can't lower privileges, %s", safe_strerror (errno
));
194 if ( zserv_privs
.change(ZPRIVS_LOWER
) )
195 zlog_err ("Can't lower privileges, %s", safe_strerror (errno
));
197 return ipforward_ipv6 ();