]> git.proxmox.com Git - mirror_frr.git/blob - zebra/ipforward_proc.c
2004-11-19 Andrew J. Schorr <ajschorr@alumni.princeton.edu>
[mirror_frr.git] / zebra / ipforward_proc.c
1 /*
2 * Fetch ipforward value by reading /proc filesystem.
3 * Copyright (C) 1997 Kunihiro Ishiguro
4 *
5 * This file is part of GNU Zebra.
6 *
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.
11 *
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.
16 *
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.
21 */
22
23 #include <zebra.h>
24
25 #include "log.h"
26 #include "privs.h"
27
28 extern struct zebra_privs_t zserv_privs;
29
30 char proc_net_snmp[] = "/proc/net/snmp";
31
32 static void
33 dropline (FILE *fp)
34 {
35 int c;
36
37 while ((c = getc (fp)) != '\n')
38 ;
39 }
40
41 int
42 ipforward ()
43 {
44 FILE *fp;
45 int ipforwarding = 0;
46 char *pnt;
47 char buf[10];
48
49 fp = fopen (proc_net_snmp, "r");
50
51 if (fp == NULL)
52 return -1;
53
54 /* We don't care about the first line. */
55 dropline (fp);
56
57 /* Get ip_statistics.IpForwarding :
58 1 => ip forwarding enabled
59 2 => ip forwarding off. */
60 pnt = fgets (buf, 6, fp);
61 sscanf (buf, "Ip: %d", &ipforwarding);
62
63 fclose(fp);
64
65 if (ipforwarding == 1)
66 return 1;
67
68 return 0;
69 }
70
71 /* char proc_ipv4_forwarding[] = "/proc/sys/net/ipv4/conf/all/forwarding"; */
72 char proc_ipv4_forwarding[] = "/proc/sys/net/ipv4/ip_forward";
73
74 int
75 ipforward_on ()
76 {
77 FILE *fp;
78
79 if ( zserv_privs.change(ZPRIVS_RAISE) )
80 zlog_err ("Can't raise privileges, %s", safe_strerror (errno) );
81
82 fp = fopen (proc_ipv4_forwarding, "w");
83
84 if (fp == NULL) {
85 if ( zserv_privs.change(ZPRIVS_LOWER) )
86 zlog_err ("Can't lower privileges, %s", safe_strerror (errno));
87 return -1;
88 }
89
90 fprintf (fp, "1\n");
91
92 fclose (fp);
93
94 if ( zserv_privs.change(ZPRIVS_LOWER) )
95 zlog_err ("Can't lower privileges, %s", safe_strerror (errno));
96
97 return ipforward ();
98 }
99
100 int
101 ipforward_off ()
102 {
103 FILE *fp;
104
105 if ( zserv_privs.change(ZPRIVS_RAISE) )
106 zlog_err ("Can't raise privileges, %s", safe_strerror (errno));
107
108 fp = fopen (proc_ipv4_forwarding, "w");
109
110 if (fp == NULL) {
111 if ( zserv_privs.change(ZPRIVS_LOWER) )
112 zlog_err ("Can't lower privileges, %s", safe_strerror (errno));
113 return -1;
114 }
115
116 fprintf (fp, "0\n");
117
118 fclose (fp);
119
120 if ( zserv_privs.change(ZPRIVS_LOWER) )
121 zlog_err ("Can't lower privileges, %s", safe_strerror (errno));
122
123 return ipforward ();
124 }
125 #ifdef HAVE_IPV6
126
127 char proc_ipv6_forwarding[] = "/proc/sys/net/ipv6/conf/all/forwarding";
128
129 int
130 ipforward_ipv6 ()
131 {
132 FILE *fp;
133 char buf[5];
134 int ipforwarding = 0;
135
136 fp = fopen (proc_ipv6_forwarding, "r");
137
138 if (fp == NULL)
139 return -1;
140
141 fgets (buf, 2, fp);
142 sscanf (buf, "%d", &ipforwarding);
143
144 return ipforwarding;
145 }
146
147 int
148 ipforward_ipv6_on ()
149 {
150 FILE *fp;
151
152 if ( zserv_privs.change(ZPRIVS_RAISE) )
153 zlog_err ("Can't raise privileges, %s", safe_strerror (errno));
154
155 fp = fopen (proc_ipv6_forwarding, "w");
156
157 if (fp == NULL) {
158 if ( zserv_privs.change(ZPRIVS_LOWER) )
159 zlog_err ("Can't lower privileges, %s", safe_strerror (errno));
160 return -1;
161 }
162
163 fprintf (fp, "1\n");
164
165 fclose (fp);
166
167 if ( zserv_privs.change(ZPRIVS_LOWER) )
168 zlog_err ("Can't lower privileges, %s", safe_strerror (errno));
169
170 return ipforward_ipv6 ();
171 }
172
173 int
174 ipforward_ipv6_off ()
175 {
176 FILE *fp;
177
178 if ( zserv_privs.change(ZPRIVS_RAISE) )
179 zlog_err ("Can't raise privileges, %s", safe_strerror (errno));
180
181 fp = fopen (proc_ipv6_forwarding, "w");
182
183 if (fp == NULL) {
184 if ( zserv_privs.change(ZPRIVS_LOWER) )
185 zlog_err ("Can't lower privileges, %s", safe_strerror (errno));
186 return -1;
187 }
188
189 fprintf (fp, "0\n");
190
191 fclose (fp);
192
193 if ( zserv_privs.change(ZPRIVS_LOWER) )
194 zlog_err ("Can't lower privileges, %s", safe_strerror (errno));
195
196 return ipforward_ipv6 ();
197 }
198 #endif /* HAVE_IPV6 */