]> git.proxmox.com Git - mirror_frr.git/blob - zebra/ipforward_proc.c
build: zebra: remove *_method Makefile hacks
[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
29 #include "zebra/ipforward.h"
30
31 extern struct zebra_privs_t zserv_privs;
32
33 char proc_net_snmp[] = "/proc/net/snmp";
34
35 static void dropline(FILE *fp)
36 {
37 int c;
38
39 while ((c = getc(fp)) != '\n')
40 ;
41 }
42
43 int 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 ipforward_on(void)
75 {
76 FILE *fp;
77
78 if (zserv_privs.change(ZPRIVS_RAISE))
79 zlog_err("Can't raise privileges, %s", safe_strerror(errno));
80
81 fp = fopen(proc_ipv4_forwarding, "w");
82
83 if (fp == NULL) {
84 if (zserv_privs.change(ZPRIVS_LOWER))
85 zlog_err("Can't lower privileges, %s",
86 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 ipforward_off(void)
101 {
102 FILE *fp;
103
104 if (zserv_privs.change(ZPRIVS_RAISE))
105 zlog_err("Can't raise privileges, %s", safe_strerror(errno));
106
107 fp = fopen(proc_ipv4_forwarding, "w");
108
109 if (fp == NULL) {
110 if (zserv_privs.change(ZPRIVS_LOWER))
111 zlog_err("Can't lower privileges, %s",
112 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 ipforward_ipv6(void)
129 {
130 FILE *fp;
131 char buf[5];
132 int ipforwarding = 0;
133
134 fp = fopen(proc_ipv6_forwarding, "r");
135
136 if (fp == NULL)
137 return -1;
138
139 if (fgets(buf, 2, fp))
140 sscanf(buf, "%d", &ipforwarding);
141
142 fclose(fp);
143 return ipforwarding;
144 }
145
146 int ipforward_ipv6_on(void)
147 {
148 FILE *fp;
149
150 if (zserv_privs.change(ZPRIVS_RAISE))
151 zlog_err("Can't raise privileges, %s", safe_strerror(errno));
152
153 fp = fopen(proc_ipv6_forwarding, "w");
154
155 if (fp == NULL) {
156 if (zserv_privs.change(ZPRIVS_LOWER))
157 zlog_err("Can't lower privileges, %s",
158 safe_strerror(errno));
159 return -1;
160 }
161
162 fprintf(fp, "1\n");
163
164 fclose(fp);
165
166 if (zserv_privs.change(ZPRIVS_LOWER))
167 zlog_err("Can't lower privileges, %s", safe_strerror(errno));
168
169 return ipforward_ipv6();
170 }
171
172
173 int ipforward_ipv6_off(void)
174 {
175 FILE *fp;
176
177 if (zserv_privs.change(ZPRIVS_RAISE))
178 zlog_err("Can't raise privileges, %s", safe_strerror(errno));
179
180 fp = fopen(proc_ipv6_forwarding, "w");
181
182 if (fp == NULL) {
183 if (zserv_privs.change(ZPRIVS_LOWER))
184 zlog_err("Can't lower privileges, %s",
185 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
199 #endif /* GNU_LINUX */