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: #ifdef HAVE_IPV6
127:
128: char proc_ipv6_forwarding[] = "/proc/sys/net/ipv6/conf/all/forwarding";
129:
130: int
131: ipforward_ipv6 (void)
132: {
133: FILE *fp;
134: char buf[5];
135: int ipforwarding = 0;
136:
137: fp = fopen (proc_ipv6_forwarding, "r");
138:
139: if (fp == NULL)
140: return -1;
141:
142: if (fgets (buf, 2, fp))
143: sscanf (buf, "%d", &ipforwarding);
144:
145: fclose (fp);
146: return ipforwarding;
147: }
148:
149: int
150: ipforward_ipv6_on (void)
151: {
152: FILE *fp;
153:
154: if ( zserv_privs.change(ZPRIVS_RAISE) )
155: zlog_err ("Can't raise privileges, %s", safe_strerror (errno));
156:
157: fp = fopen (proc_ipv6_forwarding, "w");
158:
159: if (fp == NULL) {
160: if ( zserv_privs.change(ZPRIVS_LOWER) )
161: zlog_err ("Can't lower privileges, %s", safe_strerror (errno));
162: return -1;
163: }
164:
165: fprintf (fp, "1\n");
166:
167: fclose (fp);
168:
169: if ( zserv_privs.change(ZPRIVS_LOWER) )
170: zlog_err ("Can't lower privileges, %s", safe_strerror (errno));
171:
172: return ipforward_ipv6 ();
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: }
200: #endif /* HAVE_IPV6 */
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>