]> git.proxmox.com Git - mirror_frr.git/blob - zebra/ipforward_proc.c
Merge remote-tracking branch 'origin/stable/3.0'
[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 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
20 */
21
22 #include <zebra.h>
23
24 #include "log.h"
25 #include "privs.h"
26
27 #include "zebra/ipforward.h"
28
29 extern struct zebra_privs_t zserv_privs;
30
31 char proc_net_snmp[] = "/proc/net/snmp";
32
33 static void
34 dropline (FILE *fp)
35 {
36 int c;
37
38 while ((c = getc (fp)) != '\n')
39 ;
40 }
41
42 int
43 ipforward (void)
44 {
45 FILE *fp;
46 int ipforwarding = 0;
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 if (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 (void)
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 (void)
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
126 char proc_ipv6_forwarding[] = "/proc/sys/net/ipv6/conf/all/forwarding";
127
128 int
129 ipforward_ipv6 (void)
130 {
131 FILE *fp;
132 char buf[5];
133 int ipforwarding = 0;
134
135 fp = fopen (proc_ipv6_forwarding, "r");
136
137 if (fp == NULL)
138 return -1;
139
140 if (fgets (buf, 2, fp))
141 sscanf (buf, "%d", &ipforwarding);
142
143 fclose (fp);
144 return ipforwarding;
145 }
146
147 int
148 ipforward_ipv6_on (void)
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
174 int
175 ipforward_ipv6_off (void)
176 {
177 FILE *fp;
178
179 if ( zserv_privs.change(ZPRIVS_RAISE) )
180 zlog_err ("Can't raise privileges, %s", safe_strerror (errno));
181
182 fp = fopen (proc_ipv6_forwarding, "w");
183
184 if (fp == NULL) {
185 if ( zserv_privs.change(ZPRIVS_LOWER) )
186 zlog_err ("Can't lower privileges, %s", safe_strerror (errno));
187 return -1;
188 }
189
190 fprintf (fp, "0\n");
191
192 fclose (fp);
193
194 if ( zserv_privs.change(ZPRIVS_LOWER) )
195 zlog_err ("Can't lower privileges, %s", safe_strerror (errno));
196
197 return ipforward_ipv6 ();
198 }