blob: 2876eded378048b95039d2b93a8e47a0ca1abaac [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
paula1ac18c2005-06-28 17:17:12 +000028#include "zebra/ipforward.h"
29
pauledd7c242003-06-04 13:59:38 +000030extern struct zebra_privs_t zserv_privs;
31
paul718e3742002-12-13 20:15:29 +000032char proc_net_snmp[] = "/proc/net/snmp";
33
34static void
35dropline (FILE *fp)
36{
37 int c;
38
39 while ((c = getc (fp)) != '\n')
40 ;
41}
42
43int
paula1ac18c2005-06-28 17:17:12 +000044ipforward (void)
paul718e3742002-12-13 20:15:29 +000045{
46 FILE *fp;
47 int ipforwarding = 0;
paul718e3742002-12-13 20:15:29 +000048 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. */
Stephen Hemmingerd7a08382011-12-07 00:09:56 +040061 if (fgets (buf, 6, fp))
62 sscanf (buf, "Ip: %d", &ipforwarding);
paul718e3742002-12-13 20:15:29 +000063
paul44ebf842003-10-07 05:58:48 +000064 fclose(fp);
65
paul718e3742002-12-13 20:15:29 +000066 if (ipforwarding == 1)
67 return 1;
68
69 return 0;
70}
71
72/* char proc_ipv4_forwarding[] = "/proc/sys/net/ipv4/conf/all/forwarding"; */
73char proc_ipv4_forwarding[] = "/proc/sys/net/ipv4/ip_forward";
74
75int
paula1ac18c2005-06-28 17:17:12 +000076ipforward_on (void)
paul718e3742002-12-13 20:15:29 +000077{
78 FILE *fp;
pauledd7c242003-06-04 13:59:38 +000079
80 if ( zserv_privs.change(ZPRIVS_RAISE) )
ajs6099b3b2004-11-20 02:06:59 +000081 zlog_err ("Can't raise privileges, %s", safe_strerror (errno) );
paul718e3742002-12-13 20:15:29 +000082
83 fp = fopen (proc_ipv4_forwarding, "w");
pauledd7c242003-06-04 13:59:38 +000084
hasso41d3fc92004-04-06 11:59:00 +000085 if (fp == NULL) {
86 if ( zserv_privs.change(ZPRIVS_LOWER) )
ajs6099b3b2004-11-20 02:06:59 +000087 zlog_err ("Can't lower privileges, %s", safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +000088 return -1;
hasso41d3fc92004-04-06 11:59:00 +000089 }
paul718e3742002-12-13 20:15:29 +000090
91 fprintf (fp, "1\n");
92
93 fclose (fp);
94
hasso41d3fc92004-04-06 11:59:00 +000095 if ( zserv_privs.change(ZPRIVS_LOWER) )
ajs6099b3b2004-11-20 02:06:59 +000096 zlog_err ("Can't lower privileges, %s", safe_strerror (errno));
hasso41d3fc92004-04-06 11:59:00 +000097
paul718e3742002-12-13 20:15:29 +000098 return ipforward ();
99}
100
101int
paula1ac18c2005-06-28 17:17:12 +0000102ipforward_off (void)
paul718e3742002-12-13 20:15:29 +0000103{
104 FILE *fp;
105
pauledd7c242003-06-04 13:59:38 +0000106 if ( zserv_privs.change(ZPRIVS_RAISE) )
ajs6099b3b2004-11-20 02:06:59 +0000107 zlog_err ("Can't raise privileges, %s", safe_strerror (errno));
pauledd7c242003-06-04 13:59:38 +0000108
paul718e3742002-12-13 20:15:29 +0000109 fp = fopen (proc_ipv4_forwarding, "w");
pauledd7c242003-06-04 13:59:38 +0000110
hasso41d3fc92004-04-06 11:59:00 +0000111 if (fp == NULL) {
112 if ( zserv_privs.change(ZPRIVS_LOWER) )
ajs6099b3b2004-11-20 02:06:59 +0000113 zlog_err ("Can't lower privileges, %s", safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +0000114 return -1;
hasso41d3fc92004-04-06 11:59:00 +0000115 }
paul718e3742002-12-13 20:15:29 +0000116
117 fprintf (fp, "0\n");
118
119 fclose (fp);
120
hasso41d3fc92004-04-06 11:59:00 +0000121 if ( zserv_privs.change(ZPRIVS_LOWER) )
ajs6099b3b2004-11-20 02:06:59 +0000122 zlog_err ("Can't lower privileges, %s", safe_strerror (errno));
hasso41d3fc92004-04-06 11:59:00 +0000123
paul718e3742002-12-13 20:15:29 +0000124 return ipforward ();
125}
126#ifdef HAVE_IPV6
127
128char proc_ipv6_forwarding[] = "/proc/sys/net/ipv6/conf/all/forwarding";
129
130int
paula1ac18c2005-06-28 17:17:12 +0000131ipforward_ipv6 (void)
paul718e3742002-12-13 20:15:29 +0000132{
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
Stephen Hemmingerd7a08382011-12-07 00:09:56 +0400142 if (fgets (buf, 2, fp))
143 sscanf (buf, "%d", &ipforwarding);
paul718e3742002-12-13 20:15:29 +0000144
hasso42a66d72005-03-07 08:19:44 +0000145 fclose (fp);
paul718e3742002-12-13 20:15:29 +0000146 return ipforwarding;
147}
148
149int
paula1ac18c2005-06-28 17:17:12 +0000150ipforward_ipv6_on (void)
paul718e3742002-12-13 20:15:29 +0000151{
152 FILE *fp;
153
pauledd7c242003-06-04 13:59:38 +0000154 if ( zserv_privs.change(ZPRIVS_RAISE) )
ajs6099b3b2004-11-20 02:06:59 +0000155 zlog_err ("Can't raise privileges, %s", safe_strerror (errno));
pauledd7c242003-06-04 13:59:38 +0000156
paul718e3742002-12-13 20:15:29 +0000157 fp = fopen (proc_ipv6_forwarding, "w");
pauledd7c242003-06-04 13:59:38 +0000158
hasso41d3fc92004-04-06 11:59:00 +0000159 if (fp == NULL) {
160 if ( zserv_privs.change(ZPRIVS_LOWER) )
ajs6099b3b2004-11-20 02:06:59 +0000161 zlog_err ("Can't lower privileges, %s", safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +0000162 return -1;
hasso41d3fc92004-04-06 11:59:00 +0000163 }
paul718e3742002-12-13 20:15:29 +0000164
165 fprintf (fp, "1\n");
166
167 fclose (fp);
168
hasso41d3fc92004-04-06 11:59:00 +0000169 if ( zserv_privs.change(ZPRIVS_LOWER) )
ajs6099b3b2004-11-20 02:06:59 +0000170 zlog_err ("Can't lower privileges, %s", safe_strerror (errno));
hasso41d3fc92004-04-06 11:59:00 +0000171
paul718e3742002-12-13 20:15:29 +0000172 return ipforward_ipv6 ();
173}
174
175int
paula1ac18c2005-06-28 17:17:12 +0000176ipforward_ipv6_off (void)
paul718e3742002-12-13 20:15:29 +0000177{
178 FILE *fp;
179
pauledd7c242003-06-04 13:59:38 +0000180 if ( zserv_privs.change(ZPRIVS_RAISE) )
ajs6099b3b2004-11-20 02:06:59 +0000181 zlog_err ("Can't raise privileges, %s", safe_strerror (errno));
pauledd7c242003-06-04 13:59:38 +0000182
paul718e3742002-12-13 20:15:29 +0000183 fp = fopen (proc_ipv6_forwarding, "w");
pauledd7c242003-06-04 13:59:38 +0000184
hasso41d3fc92004-04-06 11:59:00 +0000185 if (fp == NULL) {
186 if ( zserv_privs.change(ZPRIVS_LOWER) )
ajs6099b3b2004-11-20 02:06:59 +0000187 zlog_err ("Can't lower privileges, %s", safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +0000188 return -1;
hasso41d3fc92004-04-06 11:59:00 +0000189 }
paul718e3742002-12-13 20:15:29 +0000190
191 fprintf (fp, "0\n");
192
193 fclose (fp);
194
hasso41d3fc92004-04-06 11:59:00 +0000195 if ( zserv_privs.change(ZPRIVS_LOWER) )
ajs6099b3b2004-11-20 02:06:59 +0000196 zlog_err ("Can't lower privileges, %s", safe_strerror (errno));
hasso41d3fc92004-04-06 11:59:00 +0000197
paul718e3742002-12-13 20:15:29 +0000198 return ipforward_ipv6 ();
199}
200#endif /* HAVE_IPV6 */