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