blob: 443cb1c6e2921fb2c7c107c39607feffcba6ce8e [file] [log] [blame]
paul718e3742002-12-13 20:15:29 +00001/*
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
pauledd7c242003-06-04 13:59:38 +000025#include "log.h"
26#include "privs.h"
27
28extern struct zebra_privs_t zserv_privs;
29
paul718e3742002-12-13 20:15:29 +000030char proc_net_snmp[] = "/proc/net/snmp";
31
32static void
33dropline (FILE *fp)
34{
35 int c;
36
37 while ((c = getc (fp)) != '\n')
38 ;
39}
40
41int
42ipforward ()
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
paul44ebf842003-10-07 05:58:48 +000063 fclose(fp);
64
paul718e3742002-12-13 20:15:29 +000065 if (ipforwarding == 1)
66 return 1;
67
68 return 0;
69}
70
71/* char proc_ipv4_forwarding[] = "/proc/sys/net/ipv4/conf/all/forwarding"; */
72char proc_ipv4_forwarding[] = "/proc/sys/net/ipv4/ip_forward";
73
74int
75ipforward_on ()
76{
77 FILE *fp;
pauledd7c242003-06-04 13:59:38 +000078
79 if ( zserv_privs.change(ZPRIVS_RAISE) )
ajs6099b3b2004-11-20 02:06:59 +000080 zlog_err ("Can't raise privileges, %s", safe_strerror (errno) );
paul718e3742002-12-13 20:15:29 +000081
82 fp = fopen (proc_ipv4_forwarding, "w");
pauledd7c242003-06-04 13:59:38 +000083
hasso41d3fc92004-04-06 11:59:00 +000084 if (fp == NULL) {
85 if ( zserv_privs.change(ZPRIVS_LOWER) )
ajs6099b3b2004-11-20 02:06:59 +000086 zlog_err ("Can't lower privileges, %s", safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +000087 return -1;
hasso41d3fc92004-04-06 11:59:00 +000088 }
paul718e3742002-12-13 20:15:29 +000089
90 fprintf (fp, "1\n");
91
92 fclose (fp);
93
hasso41d3fc92004-04-06 11:59:00 +000094 if ( zserv_privs.change(ZPRIVS_LOWER) )
ajs6099b3b2004-11-20 02:06:59 +000095 zlog_err ("Can't lower privileges, %s", safe_strerror (errno));
hasso41d3fc92004-04-06 11:59:00 +000096
paul718e3742002-12-13 20:15:29 +000097 return ipforward ();
98}
99
100int
101ipforward_off ()
102{
103 FILE *fp;
104
pauledd7c242003-06-04 13:59:38 +0000105 if ( zserv_privs.change(ZPRIVS_RAISE) )
ajs6099b3b2004-11-20 02:06:59 +0000106 zlog_err ("Can't raise privileges, %s", safe_strerror (errno));
pauledd7c242003-06-04 13:59:38 +0000107
paul718e3742002-12-13 20:15:29 +0000108 fp = fopen (proc_ipv4_forwarding, "w");
pauledd7c242003-06-04 13:59:38 +0000109
hasso41d3fc92004-04-06 11:59:00 +0000110 if (fp == NULL) {
111 if ( zserv_privs.change(ZPRIVS_LOWER) )
ajs6099b3b2004-11-20 02:06:59 +0000112 zlog_err ("Can't lower privileges, %s", safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +0000113 return -1;
hasso41d3fc92004-04-06 11:59:00 +0000114 }
paul718e3742002-12-13 20:15:29 +0000115
116 fprintf (fp, "0\n");
117
118 fclose (fp);
119
hasso41d3fc92004-04-06 11:59:00 +0000120 if ( zserv_privs.change(ZPRIVS_LOWER) )
ajs6099b3b2004-11-20 02:06:59 +0000121 zlog_err ("Can't lower privileges, %s", safe_strerror (errno));
hasso41d3fc92004-04-06 11:59:00 +0000122
paul718e3742002-12-13 20:15:29 +0000123 return ipforward ();
124}
125#ifdef HAVE_IPV6
126
127char proc_ipv6_forwarding[] = "/proc/sys/net/ipv6/conf/all/forwarding";
128
129int
130ipforward_ipv6 ()
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 fgets (buf, 2, fp);
142 sscanf (buf, "%d", &ipforwarding);
143
144 return ipforwarding;
145}
146
147int
148ipforward_ipv6_on ()
149{
150 FILE *fp;
151
pauledd7c242003-06-04 13:59:38 +0000152 if ( zserv_privs.change(ZPRIVS_RAISE) )
ajs6099b3b2004-11-20 02:06:59 +0000153 zlog_err ("Can't raise privileges, %s", safe_strerror (errno));
pauledd7c242003-06-04 13:59:38 +0000154
paul718e3742002-12-13 20:15:29 +0000155 fp = fopen (proc_ipv6_forwarding, "w");
pauledd7c242003-06-04 13:59:38 +0000156
hasso41d3fc92004-04-06 11:59:00 +0000157 if (fp == NULL) {
158 if ( zserv_privs.change(ZPRIVS_LOWER) )
ajs6099b3b2004-11-20 02:06:59 +0000159 zlog_err ("Can't lower privileges, %s", safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +0000160 return -1;
hasso41d3fc92004-04-06 11:59:00 +0000161 }
paul718e3742002-12-13 20:15:29 +0000162
163 fprintf (fp, "1\n");
164
165 fclose (fp);
166
hasso41d3fc92004-04-06 11:59:00 +0000167 if ( zserv_privs.change(ZPRIVS_LOWER) )
ajs6099b3b2004-11-20 02:06:59 +0000168 zlog_err ("Can't lower privileges, %s", safe_strerror (errno));
hasso41d3fc92004-04-06 11:59:00 +0000169
paul718e3742002-12-13 20:15:29 +0000170 return ipforward_ipv6 ();
171}
172
173int
174ipforward_ipv6_off ()
175{
176 FILE *fp;
177
pauledd7c242003-06-04 13:59:38 +0000178 if ( zserv_privs.change(ZPRIVS_RAISE) )
ajs6099b3b2004-11-20 02:06:59 +0000179 zlog_err ("Can't raise privileges, %s", safe_strerror (errno));
pauledd7c242003-06-04 13:59:38 +0000180
paul718e3742002-12-13 20:15:29 +0000181 fp = fopen (proc_ipv6_forwarding, "w");
pauledd7c242003-06-04 13:59:38 +0000182
hasso41d3fc92004-04-06 11:59:00 +0000183 if (fp == NULL) {
184 if ( zserv_privs.change(ZPRIVS_LOWER) )
ajs6099b3b2004-11-20 02:06:59 +0000185 zlog_err ("Can't lower privileges, %s", safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +0000186 return -1;
hasso41d3fc92004-04-06 11:59:00 +0000187 }
paul718e3742002-12-13 20:15:29 +0000188
189 fprintf (fp, "0\n");
190
191 fclose (fp);
192
hasso41d3fc92004-04-06 11:59:00 +0000193 if ( zserv_privs.change(ZPRIVS_LOWER) )
ajs6099b3b2004-11-20 02:06:59 +0000194 zlog_err ("Can't lower privileges, %s", safe_strerror (errno));
hasso41d3fc92004-04-06 11:59:00 +0000195
paul718e3742002-12-13 20:15:29 +0000196 return ipforward_ipv6 ();
197}
198#endif /* HAVE_IPV6 */