blob: f83d88c3322c55e4406e02047e9987af4861e8c5 [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
hasso42a66d72005-03-07 08:19:44 +0000144 fclose (fp);
paul718e3742002-12-13 20:15:29 +0000145 return ipforwarding;
146}
147
148int
149ipforward_ipv6_on ()
150{
151 FILE *fp;
152
pauledd7c242003-06-04 13:59:38 +0000153 if ( zserv_privs.change(ZPRIVS_RAISE) )
ajs6099b3b2004-11-20 02:06:59 +0000154 zlog_err ("Can't raise privileges, %s", safe_strerror (errno));
pauledd7c242003-06-04 13:59:38 +0000155
paul718e3742002-12-13 20:15:29 +0000156 fp = fopen (proc_ipv6_forwarding, "w");
pauledd7c242003-06-04 13:59:38 +0000157
hasso41d3fc92004-04-06 11:59:00 +0000158 if (fp == NULL) {
159 if ( zserv_privs.change(ZPRIVS_LOWER) )
ajs6099b3b2004-11-20 02:06:59 +0000160 zlog_err ("Can't lower privileges, %s", safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +0000161 return -1;
hasso41d3fc92004-04-06 11:59:00 +0000162 }
paul718e3742002-12-13 20:15:29 +0000163
164 fprintf (fp, "1\n");
165
166 fclose (fp);
167
hasso41d3fc92004-04-06 11:59:00 +0000168 if ( zserv_privs.change(ZPRIVS_LOWER) )
ajs6099b3b2004-11-20 02:06:59 +0000169 zlog_err ("Can't lower privileges, %s", safe_strerror (errno));
hasso41d3fc92004-04-06 11:59:00 +0000170
paul718e3742002-12-13 20:15:29 +0000171 return ipforward_ipv6 ();
172}
173
174int
175ipforward_ipv6_off ()
176{
177 FILE *fp;
178
pauledd7c242003-06-04 13:59:38 +0000179 if ( zserv_privs.change(ZPRIVS_RAISE) )
ajs6099b3b2004-11-20 02:06:59 +0000180 zlog_err ("Can't raise privileges, %s", safe_strerror (errno));
pauledd7c242003-06-04 13:59:38 +0000181
paul718e3742002-12-13 20:15:29 +0000182 fp = fopen (proc_ipv6_forwarding, "w");
pauledd7c242003-06-04 13:59:38 +0000183
hasso41d3fc92004-04-06 11:59:00 +0000184 if (fp == NULL) {
185 if ( zserv_privs.change(ZPRIVS_LOWER) )
ajs6099b3b2004-11-20 02:06:59 +0000186 zlog_err ("Can't lower privileges, %s", safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +0000187 return -1;
hasso41d3fc92004-04-06 11:59:00 +0000188 }
paul718e3742002-12-13 20:15:29 +0000189
190 fprintf (fp, "0\n");
191
192 fclose (fp);
193
hasso41d3fc92004-04-06 11:59:00 +0000194 if ( zserv_privs.change(ZPRIVS_LOWER) )
ajs6099b3b2004-11-20 02:06:59 +0000195 zlog_err ("Can't lower privileges, %s", safe_strerror (errno));
hasso41d3fc92004-04-06 11:59:00 +0000196
paul718e3742002-12-13 20:15:29 +0000197 return ipforward_ipv6 ();
198}
199#endif /* HAVE_IPV6 */