]>
Commit | Line | Data |
---|---|---|
718e3744 | 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 | ||
edd7c245 | 25 | #include "log.h" |
26 | #include "privs.h" | |
27 | ||
28 | extern struct zebra_privs_t zserv_privs; | |
29 | ||
718e3744 | 30 | char proc_net_snmp[] = "/proc/net/snmp"; |
31 | ||
32 | static void | |
33 | dropline (FILE *fp) | |
34 | { | |
35 | int c; | |
36 | ||
37 | while ((c = getc (fp)) != '\n') | |
38 | ; | |
39 | } | |
40 | ||
41 | int | |
42 | ipforward () | |
43 | { | |
44 | FILE *fp; | |
45 | int ipforwarding = 0; | |
46 | char *pnt; | |
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 | pnt = fgets (buf, 6, fp); | |
61 | sscanf (buf, "Ip: %d", &ipforwarding); | |
62 | ||
44ebf843 | 63 | fclose(fp); |
64 | ||
718e3744 | 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 | |
75 | ipforward_on () | |
76 | { | |
77 | FILE *fp; | |
edd7c245 | 78 | |
79 | if ( zserv_privs.change(ZPRIVS_RAISE) ) | |
80 | zlog_err ("Can't raise privileges, %s", strerror (errno) ); | |
718e3744 | 81 | |
82 | fp = fopen (proc_ipv4_forwarding, "w"); | |
edd7c245 | 83 | |
84 | if ( zserv_privs.change(ZPRIVS_LOWER) ) | |
85 | zlog_err ("Can't lower privileges, %s", strerror (errno)); | |
86 | ||
718e3744 | 87 | if (fp == NULL) |
88 | return -1; | |
89 | ||
90 | fprintf (fp, "1\n"); | |
91 | ||
92 | fclose (fp); | |
93 | ||
94 | return ipforward (); | |
95 | } | |
96 | ||
97 | int | |
98 | ipforward_off () | |
99 | { | |
100 | FILE *fp; | |
101 | ||
edd7c245 | 102 | if ( zserv_privs.change(ZPRIVS_RAISE) ) |
103 | zlog_err ("Can't raise privileges, %s", strerror (errno)); | |
104 | ||
718e3744 | 105 | fp = fopen (proc_ipv4_forwarding, "w"); |
edd7c245 | 106 | |
107 | if ( zserv_privs.change(ZPRIVS_LOWER) ) | |
108 | zlog_err ("Can't lower privileges, %s", strerror (errno)); | |
109 | ||
718e3744 | 110 | |
111 | if (fp == NULL) | |
112 | return -1; | |
113 | ||
114 | fprintf (fp, "0\n"); | |
115 | ||
116 | fclose (fp); | |
117 | ||
118 | return ipforward (); | |
119 | } | |
120 | #ifdef HAVE_IPV6 | |
121 | ||
122 | char proc_ipv6_forwarding[] = "/proc/sys/net/ipv6/conf/all/forwarding"; | |
123 | ||
124 | int | |
125 | ipforward_ipv6 () | |
126 | { | |
127 | FILE *fp; | |
128 | char buf[5]; | |
129 | int ipforwarding = 0; | |
130 | ||
131 | fp = fopen (proc_ipv6_forwarding, "r"); | |
132 | ||
133 | if (fp == NULL) | |
134 | return -1; | |
135 | ||
136 | fgets (buf, 2, fp); | |
137 | sscanf (buf, "%d", &ipforwarding); | |
138 | ||
139 | return ipforwarding; | |
140 | } | |
141 | ||
142 | int | |
143 | ipforward_ipv6_on () | |
144 | { | |
145 | FILE *fp; | |
146 | ||
edd7c245 | 147 | if ( zserv_privs.change(ZPRIVS_RAISE) ) |
148 | zlog_err ("Can't raise privileges, %s", strerror (errno)); | |
149 | ||
718e3744 | 150 | fp = fopen (proc_ipv6_forwarding, "w"); |
edd7c245 | 151 | |
152 | if ( zserv_privs.change(ZPRIVS_LOWER) ) | |
153 | zlog_err ("Can't lower privileges, %s", strerror (errno)); | |
718e3744 | 154 | |
155 | if (fp == NULL) | |
156 | return -1; | |
157 | ||
158 | fprintf (fp, "1\n"); | |
159 | ||
160 | fclose (fp); | |
161 | ||
162 | return ipforward_ipv6 (); | |
163 | } | |
164 | ||
165 | int | |
166 | ipforward_ipv6_off () | |
167 | { | |
168 | FILE *fp; | |
169 | ||
edd7c245 | 170 | if ( zserv_privs.change(ZPRIVS_RAISE) ) |
171 | zlog_err ("Can't raise privileges, %s", strerror (errno)); | |
172 | ||
718e3744 | 173 | fp = fopen (proc_ipv6_forwarding, "w"); |
edd7c245 | 174 | |
175 | if ( zserv_privs.change(ZPRIVS_LOWER) ) | |
176 | zlog_err ("Can't lower privileges, %s", strerror (errno)); | |
718e3744 | 177 | |
178 | if (fp == NULL) | |
179 | return -1; | |
180 | ||
181 | fprintf (fp, "0\n"); | |
182 | ||
183 | fclose (fp); | |
184 | ||
185 | return ipforward_ipv6 (); | |
186 | } | |
187 | #endif /* HAVE_IPV6 */ |