]>
Commit | Line | Data |
---|---|---|
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 | int ret = 0; | |
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 | ret = sscanf(buf, "Ip: %d", &ipforwarding); | |
63 | ||
64 | fclose(fp); | |
65 | ||
66 | if (ret == 1 && 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 ipforward_on(void) | |
76 | { | |
77 | FILE *fp; | |
78 | ||
79 | frr_elevate_privs(&zserv_privs) { | |
80 | ||
81 | fp = fopen(proc_ipv4_forwarding, "w"); | |
82 | ||
83 | if (fp == NULL) { | |
84 | return -1; | |
85 | } | |
86 | ||
87 | fprintf(fp, "1\n"); | |
88 | ||
89 | fclose(fp); | |
90 | ||
91 | } | |
92 | ||
93 | return ipforward(); | |
94 | } | |
95 | ||
96 | int ipforward_off(void) | |
97 | { | |
98 | FILE *fp; | |
99 | ||
100 | frr_elevate_privs(&zserv_privs) { | |
101 | ||
102 | fp = fopen(proc_ipv4_forwarding, "w"); | |
103 | ||
104 | if (fp == NULL) { | |
105 | return -1; | |
106 | } | |
107 | ||
108 | fprintf(fp, "0\n"); | |
109 | ||
110 | fclose(fp); | |
111 | ||
112 | } | |
113 | ||
114 | return ipforward(); | |
115 | } | |
116 | ||
117 | char proc_ipv6_forwarding[] = "/proc/sys/net/ipv6/conf/all/forwarding"; | |
118 | ||
119 | int ipforward_ipv6(void) | |
120 | { | |
121 | int ret = 0; | |
122 | FILE *fp; | |
123 | char buf[5]; | |
124 | int ipforwarding = 0; | |
125 | ||
126 | fp = fopen(proc_ipv6_forwarding, "r"); | |
127 | ||
128 | if (fp == NULL) | |
129 | return -1; | |
130 | ||
131 | if (fgets(buf, 2, fp)) | |
132 | ret = sscanf(buf, "%d", &ipforwarding); | |
133 | ||
134 | fclose(fp); | |
135 | ||
136 | if (ret != 1) | |
137 | return 0; | |
138 | ||
139 | return ipforwarding; | |
140 | } | |
141 | ||
142 | int ipforward_ipv6_on(void) | |
143 | { | |
144 | FILE *fp; | |
145 | ||
146 | frr_elevate_privs(&zserv_privs) { | |
147 | ||
148 | fp = fopen(proc_ipv6_forwarding, "w"); | |
149 | ||
150 | if (fp == NULL) { | |
151 | return -1; | |
152 | } | |
153 | ||
154 | fprintf(fp, "1\n"); | |
155 | ||
156 | fclose(fp); | |
157 | ||
158 | } | |
159 | ||
160 | return ipforward_ipv6(); | |
161 | } | |
162 | ||
163 | ||
164 | int ipforward_ipv6_off(void) | |
165 | { | |
166 | FILE *fp; | |
167 | ||
168 | frr_elevate_privs(&zserv_privs) { | |
169 | ||
170 | fp = fopen(proc_ipv6_forwarding, "w"); | |
171 | ||
172 | if (fp == NULL) { | |
173 | return -1; | |
174 | } | |
175 | ||
176 | fprintf(fp, "0\n"); | |
177 | ||
178 | fclose(fp); | |
179 | ||
180 | } | |
181 | ||
182 | return ipforward_ipv6(); | |
183 | } | |
184 | ||
185 | #endif /* GNU_LINUX */ |