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
28 #include "lib_errors.h"
30 #include "zebra/ipforward.h"
32 extern struct zebra_privs_t zserv_privs
;
34 char proc_net_snmp
[] = "/proc/net/snmp";
36 static void dropline(FILE *fp
)
40 while ((c
= getc(fp
)) != '\n')
51 fp
= fopen(proc_net_snmp
, "r");
56 /* We don't care about the first line. */
59 /* Get ip_statistics.IpForwarding :
60 1 => ip forwarding enabled
61 2 => ip forwarding off. */
62 if (fgets(buf
, 6, fp
))
63 ret
= sscanf(buf
, "Ip: %d", &ipforwarding
);
67 if (ret
== 1 && ipforwarding
== 1)
73 /* char proc_ipv4_forwarding[] = "/proc/sys/net/ipv4/conf/all/forwarding"; */
74 char proc_ipv4_forwarding
[] = "/proc/sys/net/ipv4/ip_forward";
76 int ipforward_on(void)
80 if (zserv_privs
.change(ZPRIVS_RAISE
))
81 zlog_ferr(LIB_ERR_PRIVILEGES
, "Can't raise privileges, %s",
82 safe_strerror(errno
));
84 fp
= fopen(proc_ipv4_forwarding
, "w");
87 if (zserv_privs
.change(ZPRIVS_LOWER
))
88 zlog_ferr(LIB_ERR_PRIVILEGES
,
89 "Can't lower privileges, %s",
90 safe_strerror(errno
));
98 if (zserv_privs
.change(ZPRIVS_LOWER
))
99 zlog_ferr(LIB_ERR_PRIVILEGES
, "Can't lower privileges, %s",
100 safe_strerror(errno
));
105 int ipforward_off(void)
109 if (zserv_privs
.change(ZPRIVS_RAISE
))
110 zlog_ferr(LIB_ERR_PRIVILEGES
, "Can't raise privileges, %s",
111 safe_strerror(errno
));
113 fp
= fopen(proc_ipv4_forwarding
, "w");
116 if (zserv_privs
.change(ZPRIVS_LOWER
))
117 zlog_ferr(LIB_ERR_PRIVILEGES
,
118 "Can't lower privileges, %s",
119 safe_strerror(errno
));
127 if (zserv_privs
.change(ZPRIVS_LOWER
))
128 zlog_ferr(LIB_ERR_PRIVILEGES
, "Can't lower privileges, %s",
129 safe_strerror(errno
));
134 char proc_ipv6_forwarding
[] = "/proc/sys/net/ipv6/conf/all/forwarding";
136 int ipforward_ipv6(void)
141 int ipforwarding
= 0;
143 fp
= fopen(proc_ipv6_forwarding
, "r");
148 if (fgets(buf
, 2, fp
))
149 ret
= sscanf(buf
, "%d", &ipforwarding
);
159 int ipforward_ipv6_on(void)
163 if (zserv_privs
.change(ZPRIVS_RAISE
))
164 zlog_ferr(LIB_ERR_PRIVILEGES
, "Can't raise privileges, %s",
165 safe_strerror(errno
));
167 fp
= fopen(proc_ipv6_forwarding
, "w");
170 if (zserv_privs
.change(ZPRIVS_LOWER
))
171 zlog_ferr(LIB_ERR_PRIVILEGES
,
172 "Can't lower privileges, %s",
173 safe_strerror(errno
));
181 if (zserv_privs
.change(ZPRIVS_LOWER
))
182 zlog_ferr(LIB_ERR_PRIVILEGES
, "Can't lower privileges, %s",
183 safe_strerror(errno
));
185 return ipforward_ipv6();
189 int ipforward_ipv6_off(void)
193 if (zserv_privs
.change(ZPRIVS_RAISE
))
194 zlog_ferr(LIB_ERR_PRIVILEGES
, "Can't raise privileges, %s",
195 safe_strerror(errno
));
197 fp
= fopen(proc_ipv6_forwarding
, "w");
200 if (zserv_privs
.change(ZPRIVS_LOWER
))
201 zlog_ferr(LIB_ERR_PRIVILEGES
,
202 "Can't lower privileges, %s",
203 safe_strerror(errno
));
211 if (zserv_privs
.change(ZPRIVS_LOWER
))
212 zlog_ferr(LIB_ERR_PRIVILEGES
, "Can't lower privileges, %s",
213 safe_strerror(errno
));
215 return ipforward_ipv6();
218 #endif /* GNU_LINUX */