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