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')
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 ret
= sscanf(buf
, "Ip: %d", &ipforwarding
);
66 if (ret
== 1 && 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";
75 int ipforward_on(void)
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",
87 safe_strerror(errno
));
95 if (zserv_privs
.change(ZPRIVS_LOWER
))
96 zlog_err("Can't lower privileges, %s", safe_strerror(errno
));
101 int ipforward_off(void)
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",
113 safe_strerror(errno
));
121 if (zserv_privs
.change(ZPRIVS_LOWER
))
122 zlog_err("Can't lower privileges, %s", safe_strerror(errno
));
127 char proc_ipv6_forwarding
[] = "/proc/sys/net/ipv6/conf/all/forwarding";
129 int ipforward_ipv6(void)
134 int ipforwarding
= 0;
136 fp
= fopen(proc_ipv6_forwarding
, "r");
141 if (fgets(buf
, 2, fp
))
142 ret
= sscanf(buf
, "%d", &ipforwarding
);
152 int ipforward_ipv6_on(void)
156 if (zserv_privs
.change(ZPRIVS_RAISE
))
157 zlog_err("Can't raise privileges, %s", safe_strerror(errno
));
159 fp
= fopen(proc_ipv6_forwarding
, "w");
162 if (zserv_privs
.change(ZPRIVS_LOWER
))
163 zlog_err("Can't lower privileges, %s",
164 safe_strerror(errno
));
172 if (zserv_privs
.change(ZPRIVS_LOWER
))
173 zlog_err("Can't lower privileges, %s", safe_strerror(errno
));
175 return ipforward_ipv6();
179 int ipforward_ipv6_off(void)
183 if (zserv_privs
.change(ZPRIVS_RAISE
))
184 zlog_err("Can't raise privileges, %s", safe_strerror(errno
));
186 fp
= fopen(proc_ipv6_forwarding
, "w");
189 if (zserv_privs
.change(ZPRIVS_LOWER
))
190 zlog_err("Can't lower privileges, %s",
191 safe_strerror(errno
));
199 if (zserv_privs
.change(ZPRIVS_LOWER
))
200 zlog_err("Can't lower privileges, %s", safe_strerror(errno
));
202 return ipforward_ipv6();
205 #endif /* GNU_LINUX */