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";
33 static void dropline(FILE *fp
)
37 while ((c
= getc(fp
)) != '\n')
47 fp
= fopen(proc_net_snmp
, "r");
52 /* We don't care about the first line. */
55 /* Get ip_statistics.IpForwarding :
56 1 => ip forwarding enabled
57 2 => ip forwarding off. */
58 if (fgets(buf
, 6, fp
))
59 sscanf(buf
, "Ip: %d", &ipforwarding
);
63 if (ipforwarding
== 1)
69 /* char proc_ipv4_forwarding[] = "/proc/sys/net/ipv4/conf/all/forwarding"; */
70 char proc_ipv4_forwarding
[] = "/proc/sys/net/ipv4/ip_forward";
72 int ipforward_on(void)
76 if (zserv_privs
.change(ZPRIVS_RAISE
))
77 zlog_err("Can't raise privileges, %s", safe_strerror(errno
));
79 fp
= fopen(proc_ipv4_forwarding
, "w");
82 if (zserv_privs
.change(ZPRIVS_LOWER
))
83 zlog_err("Can't lower privileges, %s",
84 safe_strerror(errno
));
92 if (zserv_privs
.change(ZPRIVS_LOWER
))
93 zlog_err("Can't lower privileges, %s", safe_strerror(errno
));
98 int ipforward_off(void)
102 if (zserv_privs
.change(ZPRIVS_RAISE
))
103 zlog_err("Can't raise privileges, %s", safe_strerror(errno
));
105 fp
= fopen(proc_ipv4_forwarding
, "w");
108 if (zserv_privs
.change(ZPRIVS_LOWER
))
109 zlog_err("Can't lower privileges, %s",
110 safe_strerror(errno
));
118 if (zserv_privs
.change(ZPRIVS_LOWER
))
119 zlog_err("Can't lower privileges, %s", safe_strerror(errno
));
124 char proc_ipv6_forwarding
[] = "/proc/sys/net/ipv6/conf/all/forwarding";
126 int ipforward_ipv6(void)
130 int ipforwarding
= 0;
132 fp
= fopen(proc_ipv6_forwarding
, "r");
137 if (fgets(buf
, 2, fp
))
138 sscanf(buf
, "%d", &ipforwarding
);
144 int ipforward_ipv6_on(void)
148 if (zserv_privs
.change(ZPRIVS_RAISE
))
149 zlog_err("Can't raise privileges, %s", safe_strerror(errno
));
151 fp
= fopen(proc_ipv6_forwarding
, "w");
154 if (zserv_privs
.change(ZPRIVS_LOWER
))
155 zlog_err("Can't lower privileges, %s",
156 safe_strerror(errno
));
164 if (zserv_privs
.change(ZPRIVS_LOWER
))
165 zlog_err("Can't lower privileges, %s", safe_strerror(errno
));
167 return ipforward_ipv6();
171 int ipforward_ipv6_off(void)
175 if (zserv_privs
.change(ZPRIVS_RAISE
))
176 zlog_err("Can't raise privileges, %s", safe_strerror(errno
));
178 fp
= fopen(proc_ipv6_forwarding
, "w");
181 if (zserv_privs
.change(ZPRIVS_LOWER
))
182 zlog_err("Can't lower privileges, %s",
183 safe_strerror(errno
));
191 if (zserv_privs
.change(ZPRIVS_LOWER
))
192 zlog_err("Can't lower privileges, %s", safe_strerror(errno
));
194 return ipforward_ipv6();