]> git.proxmox.com Git - mirror_frr.git/blob - zebra/ipforward_proc.c
*: use frr_elevate_privs() (1/2: coccinelle)
[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 #ifdef GNU_LINUX
25
26 #include "log.h"
27 #include "privs.h"
28 #include "lib_errors.h"
29
30 #include "zebra/ipforward.h"
31
32 extern struct zebra_privs_t zserv_privs;
33
34 char proc_net_snmp[] = "/proc/net/snmp";
35
36 static void dropline(FILE *fp)
37 {
38 int c;
39
40 while ((c = getc(fp)) != '\n')
41 ;
42 }
43
44 int ipforward(void)
45 {
46 int ret = 0;
47 FILE *fp;
48 int ipforwarding = 0;
49 char buf[10];
50
51 fp = fopen(proc_net_snmp, "r");
52
53 if (fp == NULL)
54 return -1;
55
56 /* We don't care about the first line. */
57 dropline(fp);
58
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);
64
65 fclose(fp);
66
67 if (ret == 1 && ipforwarding == 1)
68 return 1;
69
70 return 0;
71 }
72
73 /* char proc_ipv4_forwarding[] = "/proc/sys/net/ipv4/conf/all/forwarding"; */
74 char proc_ipv4_forwarding[] = "/proc/sys/net/ipv4/ip_forward";
75
76 int ipforward_on(void)
77 {
78 FILE *fp;
79
80 frr_elevate_privs(&zserv_privs) {
81
82 fp = fopen(proc_ipv4_forwarding, "w");
83
84 if (fp == NULL) {
85 return -1;
86 }
87
88 fprintf(fp, "1\n");
89
90 fclose(fp);
91
92 }
93
94 return ipforward();
95 }
96
97 int ipforward_off(void)
98 {
99 FILE *fp;
100
101 frr_elevate_privs(&zserv_privs) {
102
103 fp = fopen(proc_ipv4_forwarding, "w");
104
105 if (fp == NULL) {
106 return -1;
107 }
108
109 fprintf(fp, "0\n");
110
111 fclose(fp);
112
113 }
114
115 return ipforward();
116 }
117
118 char proc_ipv6_forwarding[] = "/proc/sys/net/ipv6/conf/all/forwarding";
119
120 int ipforward_ipv6(void)
121 {
122 int ret = 0;
123 FILE *fp;
124 char buf[5];
125 int ipforwarding = 0;
126
127 fp = fopen(proc_ipv6_forwarding, "r");
128
129 if (fp == NULL)
130 return -1;
131
132 if (fgets(buf, 2, fp))
133 ret = sscanf(buf, "%d", &ipforwarding);
134
135 fclose(fp);
136
137 if (ret != 1)
138 return 0;
139
140 return ipforwarding;
141 }
142
143 int ipforward_ipv6_on(void)
144 {
145 FILE *fp;
146
147 frr_elevate_privs(&zserv_privs) {
148
149 fp = fopen(proc_ipv6_forwarding, "w");
150
151 if (fp == NULL) {
152 return -1;
153 }
154
155 fprintf(fp, "1\n");
156
157 fclose(fp);
158
159 }
160
161 return ipforward_ipv6();
162 }
163
164
165 int ipforward_ipv6_off(void)
166 {
167 FILE *fp;
168
169 frr_elevate_privs(&zserv_privs) {
170
171 fp = fopen(proc_ipv6_forwarding, "w");
172
173 if (fp == NULL) {
174 return -1;
175 }
176
177 fprintf(fp, "0\n");
178
179 fclose(fp);
180
181 }
182
183 return ipforward_ipv6();
184 }
185
186 #endif /* GNU_LINUX */