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