blob: 73846137a8b8188f5a9dd48e46dbb70af100530a [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;
48 char *pnt;
49 char buf[10];
50
51 fp = fopen (proc_net_snmp, "r");
52
53 if (fp == NULL)
54 return -1;
55
56 /* We don't care about the first line. */
57 dropline (fp);
58
59 /* Get ip_statistics.IpForwarding :
60 1 => ip forwarding enabled
61 2 => ip forwarding off. */
62 pnt = fgets (buf, 6, fp);
63 sscanf (buf, "Ip: %d", &ipforwarding);
64
paul44ebf842003-10-07 05:58:48 +000065 fclose(fp);
66
paul718e3742002-12-13 20:15:29 +000067 if (ipforwarding == 1)
68 return 1;
69
70 return 0;
71}
72
73/* char proc_ipv4_forwarding[] = "/proc/sys/net/ipv4/conf/all/forwarding"; */
74char proc_ipv4_forwarding[] = "/proc/sys/net/ipv4/ip_forward";
75
76int
paula1ac18c2005-06-28 17:17:12 +000077ipforward_on (void)
paul718e3742002-12-13 20:15:29 +000078{
79 FILE *fp;
pauledd7c242003-06-04 13:59:38 +000080
81 if ( zserv_privs.change(ZPRIVS_RAISE) )
ajs6099b3b2004-11-20 02:06:59 +000082 zlog_err ("Can't raise privileges, %s", safe_strerror (errno) );
paul718e3742002-12-13 20:15:29 +000083
84 fp = fopen (proc_ipv4_forwarding, "w");
pauledd7c242003-06-04 13:59:38 +000085
hasso41d3fc92004-04-06 11:59:00 +000086 if (fp == NULL) {
87 if ( zserv_privs.change(ZPRIVS_LOWER) )
ajs6099b3b2004-11-20 02:06:59 +000088 zlog_err ("Can't lower privileges, %s", safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +000089 return -1;
hasso41d3fc92004-04-06 11:59:00 +000090 }
paul718e3742002-12-13 20:15:29 +000091
92 fprintf (fp, "1\n");
93
94 fclose (fp);
95
hasso41d3fc92004-04-06 11:59:00 +000096 if ( zserv_privs.change(ZPRIVS_LOWER) )
ajs6099b3b2004-11-20 02:06:59 +000097 zlog_err ("Can't lower privileges, %s", safe_strerror (errno));
hasso41d3fc92004-04-06 11:59:00 +000098
paul718e3742002-12-13 20:15:29 +000099 return ipforward ();
100}
101
102int
paula1ac18c2005-06-28 17:17:12 +0000103ipforward_off (void)
paul718e3742002-12-13 20:15:29 +0000104{
105 FILE *fp;
106
pauledd7c242003-06-04 13:59:38 +0000107 if ( zserv_privs.change(ZPRIVS_RAISE) )
ajs6099b3b2004-11-20 02:06:59 +0000108 zlog_err ("Can't raise privileges, %s", safe_strerror (errno));
pauledd7c242003-06-04 13:59:38 +0000109
paul718e3742002-12-13 20:15:29 +0000110 fp = fopen (proc_ipv4_forwarding, "w");
pauledd7c242003-06-04 13:59:38 +0000111
hasso41d3fc92004-04-06 11:59:00 +0000112 if (fp == NULL) {
113 if ( zserv_privs.change(ZPRIVS_LOWER) )
ajs6099b3b2004-11-20 02:06:59 +0000114 zlog_err ("Can't lower privileges, %s", safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +0000115 return -1;
hasso41d3fc92004-04-06 11:59:00 +0000116 }
paul718e3742002-12-13 20:15:29 +0000117
118 fprintf (fp, "0\n");
119
120 fclose (fp);
121
hasso41d3fc92004-04-06 11:59:00 +0000122 if ( zserv_privs.change(ZPRIVS_LOWER) )
ajs6099b3b2004-11-20 02:06:59 +0000123 zlog_err ("Can't lower privileges, %s", safe_strerror (errno));
hasso41d3fc92004-04-06 11:59:00 +0000124
paul718e3742002-12-13 20:15:29 +0000125 return ipforward ();
126}
127#ifdef HAVE_IPV6
128
129char proc_ipv6_forwarding[] = "/proc/sys/net/ipv6/conf/all/forwarding";
130
131int
paula1ac18c2005-06-28 17:17:12 +0000132ipforward_ipv6 (void)
paul718e3742002-12-13 20:15:29 +0000133{
134 FILE *fp;
135 char buf[5];
136 int ipforwarding = 0;
137
138 fp = fopen (proc_ipv6_forwarding, "r");
139
140 if (fp == NULL)
141 return -1;
142
143 fgets (buf, 2, fp);
144 sscanf (buf, "%d", &ipforwarding);
145
hasso42a66d72005-03-07 08:19:44 +0000146 fclose (fp);
paul718e3742002-12-13 20:15:29 +0000147 return ipforwarding;
148}
149
150int
paula1ac18c2005-06-28 17:17:12 +0000151ipforward_ipv6_on (void)
paul718e3742002-12-13 20:15:29 +0000152{
153 FILE *fp;
154
pauledd7c242003-06-04 13:59:38 +0000155 if ( zserv_privs.change(ZPRIVS_RAISE) )
ajs6099b3b2004-11-20 02:06:59 +0000156 zlog_err ("Can't raise privileges, %s", safe_strerror (errno));
pauledd7c242003-06-04 13:59:38 +0000157
paul718e3742002-12-13 20:15:29 +0000158 fp = fopen (proc_ipv6_forwarding, "w");
pauledd7c242003-06-04 13:59:38 +0000159
hasso41d3fc92004-04-06 11:59:00 +0000160 if (fp == NULL) {
161 if ( zserv_privs.change(ZPRIVS_LOWER) )
ajs6099b3b2004-11-20 02:06:59 +0000162 zlog_err ("Can't lower privileges, %s", safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +0000163 return -1;
hasso41d3fc92004-04-06 11:59:00 +0000164 }
paul718e3742002-12-13 20:15:29 +0000165
166 fprintf (fp, "1\n");
167
168 fclose (fp);
169
hasso41d3fc92004-04-06 11:59:00 +0000170 if ( zserv_privs.change(ZPRIVS_LOWER) )
ajs6099b3b2004-11-20 02:06:59 +0000171 zlog_err ("Can't lower privileges, %s", safe_strerror (errno));
hasso41d3fc92004-04-06 11:59:00 +0000172
paul718e3742002-12-13 20:15:29 +0000173 return ipforward_ipv6 ();
174}
175
176int
paula1ac18c2005-06-28 17:17:12 +0000177ipforward_ipv6_off (void)
paul718e3742002-12-13 20:15:29 +0000178{
179 FILE *fp;
180
pauledd7c242003-06-04 13:59:38 +0000181 if ( zserv_privs.change(ZPRIVS_RAISE) )
ajs6099b3b2004-11-20 02:06:59 +0000182 zlog_err ("Can't raise privileges, %s", safe_strerror (errno));
pauledd7c242003-06-04 13:59:38 +0000183
paul718e3742002-12-13 20:15:29 +0000184 fp = fopen (proc_ipv6_forwarding, "w");
pauledd7c242003-06-04 13:59:38 +0000185
hasso41d3fc92004-04-06 11:59:00 +0000186 if (fp == NULL) {
187 if ( zserv_privs.change(ZPRIVS_LOWER) )
ajs6099b3b2004-11-20 02:06:59 +0000188 zlog_err ("Can't lower privileges, %s", safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +0000189 return -1;
hasso41d3fc92004-04-06 11:59:00 +0000190 }
paul718e3742002-12-13 20:15:29 +0000191
192 fprintf (fp, "0\n");
193
194 fclose (fp);
195
hasso41d3fc92004-04-06 11:59:00 +0000196 if ( zserv_privs.change(ZPRIVS_LOWER) )
ajs6099b3b2004-11-20 02:06:59 +0000197 zlog_err ("Can't lower privileges, %s", safe_strerror (errno));
hasso41d3fc92004-04-06 11:59:00 +0000198
paul718e3742002-12-13 20:15:29 +0000199 return ipforward_ipv6 ();
200}
201#endif /* HAVE_IPV6 */