zebra: make declaration const in rtm_flag_dump()
[jleu-quagga.git] / zebra / ipforward_proc.c
blob73846137a8b8188f5a9dd48e46dbb70af100530a
1 /*
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
10 * later version.
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
20 * 02111-1307, USA.
23 #include <zebra.h>
25 #include "log.h"
26 #include "privs.h"
28 #include "zebra/ipforward.h"
30 extern struct zebra_privs_t zserv_privs;
32 char proc_net_snmp[] = "/proc/net/snmp";
34 static void
35 dropline (FILE *fp)
37 int c;
39 while ((c = getc (fp)) != '\n')
43 int
44 ipforward (void)
46 FILE *fp;
47 int ipforwarding = 0;
48 char *pnt;
49 char buf[10];
51 fp = fopen (proc_net_snmp, "r");
53 if (fp == NULL)
54 return -1;
56 /* We don't care about the first line. */
57 dropline (fp);
59 /* Get ip_statistics.IpForwarding :
60 1 => ip forwarding enabled
61 2 => ip forwarding off. */
62 pnt = fgets (buf, 6, fp);
63 sscanf (buf, "Ip: %d", &ipforwarding);
65 fclose(fp);
67 if (ipforwarding == 1)
68 return 1;
70 return 0;
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
77 ipforward_on (void)
79 FILE *fp;
81 if ( zserv_privs.change(ZPRIVS_RAISE) )
82 zlog_err ("Can't raise privileges, %s", safe_strerror (errno) );
84 fp = fopen (proc_ipv4_forwarding, "w");
86 if (fp == NULL) {
87 if ( zserv_privs.change(ZPRIVS_LOWER) )
88 zlog_err ("Can't lower privileges, %s", safe_strerror (errno));
89 return -1;
92 fprintf (fp, "1\n");
94 fclose (fp);
96 if ( zserv_privs.change(ZPRIVS_LOWER) )
97 zlog_err ("Can't lower privileges, %s", safe_strerror (errno));
99 return ipforward ();
103 ipforward_off (void)
105 FILE *fp;
107 if ( zserv_privs.change(ZPRIVS_RAISE) )
108 zlog_err ("Can't raise privileges, %s", safe_strerror (errno));
110 fp = fopen (proc_ipv4_forwarding, "w");
112 if (fp == NULL) {
113 if ( zserv_privs.change(ZPRIVS_LOWER) )
114 zlog_err ("Can't lower privileges, %s", safe_strerror (errno));
115 return -1;
118 fprintf (fp, "0\n");
120 fclose (fp);
122 if ( zserv_privs.change(ZPRIVS_LOWER) )
123 zlog_err ("Can't lower privileges, %s", safe_strerror (errno));
125 return ipforward ();
127 #ifdef HAVE_IPV6
129 char proc_ipv6_forwarding[] = "/proc/sys/net/ipv6/conf/all/forwarding";
132 ipforward_ipv6 (void)
134 FILE *fp;
135 char buf[5];
136 int ipforwarding = 0;
138 fp = fopen (proc_ipv6_forwarding, "r");
140 if (fp == NULL)
141 return -1;
143 fgets (buf, 2, fp);
144 sscanf (buf, "%d", &ipforwarding);
146 fclose (fp);
147 return ipforwarding;
151 ipforward_ipv6_on (void)
153 FILE *fp;
155 if ( zserv_privs.change(ZPRIVS_RAISE) )
156 zlog_err ("Can't raise privileges, %s", safe_strerror (errno));
158 fp = fopen (proc_ipv6_forwarding, "w");
160 if (fp == NULL) {
161 if ( zserv_privs.change(ZPRIVS_LOWER) )
162 zlog_err ("Can't lower privileges, %s", safe_strerror (errno));
163 return -1;
166 fprintf (fp, "1\n");
168 fclose (fp);
170 if ( zserv_privs.change(ZPRIVS_LOWER) )
171 zlog_err ("Can't lower privileges, %s", safe_strerror (errno));
173 return ipforward_ipv6 ();
177 ipforward_ipv6_off (void)
179 FILE *fp;
181 if ( zserv_privs.change(ZPRIVS_RAISE) )
182 zlog_err ("Can't raise privileges, %s", safe_strerror (errno));
184 fp = fopen (proc_ipv6_forwarding, "w");
186 if (fp == NULL) {
187 if ( zserv_privs.change(ZPRIVS_LOWER) )
188 zlog_err ("Can't lower privileges, %s", safe_strerror (errno));
189 return -1;
192 fprintf (fp, "0\n");
194 fclose (fp);
196 if ( zserv_privs.change(ZPRIVS_LOWER) )
197 zlog_err ("Can't lower privileges, %s", safe_strerror (errno));
199 return ipforward_ipv6 ();
201 #endif /* HAVE_IPV6 */