blob: befa2369ecbe290fb6efdf4328d9fc7bebdaa554 [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) )
80 zlog_err ("Can't raise privileges, %s", strerror (errno) );
paul718e3742002-12-13 20:15:29 +000081
82 fp = fopen (proc_ipv4_forwarding, "w");
pauledd7c242003-06-04 13:59:38 +000083
84 if ( zserv_privs.change(ZPRIVS_LOWER) )
85 zlog_err ("Can't lower privileges, %s", strerror (errno));
86
paul718e3742002-12-13 20:15:29 +000087 if (fp == NULL)
88 return -1;
89
90 fprintf (fp, "1\n");
91
92 fclose (fp);
93
94 return ipforward ();
95}
96
97int
98ipforward_off ()
99{
100 FILE *fp;
101
pauledd7c242003-06-04 13:59:38 +0000102 if ( zserv_privs.change(ZPRIVS_RAISE) )
103 zlog_err ("Can't raise privileges, %s", strerror (errno));
104
paul718e3742002-12-13 20:15:29 +0000105 fp = fopen (proc_ipv4_forwarding, "w");
pauledd7c242003-06-04 13:59:38 +0000106
107 if ( zserv_privs.change(ZPRIVS_LOWER) )
108 zlog_err ("Can't lower privileges, %s", strerror (errno));
109
paul718e3742002-12-13 20:15:29 +0000110
111 if (fp == NULL)
112 return -1;
113
114 fprintf (fp, "0\n");
115
116 fclose (fp);
117
118 return ipforward ();
119}
120#ifdef HAVE_IPV6
121
122char proc_ipv6_forwarding[] = "/proc/sys/net/ipv6/conf/all/forwarding";
123
124int
125ipforward_ipv6 ()
126{
127 FILE *fp;
128 char buf[5];
129 int ipforwarding = 0;
130
131 fp = fopen (proc_ipv6_forwarding, "r");
132
133 if (fp == NULL)
134 return -1;
135
136 fgets (buf, 2, fp);
137 sscanf (buf, "%d", &ipforwarding);
138
139 return ipforwarding;
140}
141
142int
143ipforward_ipv6_on ()
144{
145 FILE *fp;
146
pauledd7c242003-06-04 13:59:38 +0000147 if ( zserv_privs.change(ZPRIVS_RAISE) )
148 zlog_err ("Can't raise privileges, %s", strerror (errno));
149
paul718e3742002-12-13 20:15:29 +0000150 fp = fopen (proc_ipv6_forwarding, "w");
pauledd7c242003-06-04 13:59:38 +0000151
152 if ( zserv_privs.change(ZPRIVS_LOWER) )
153 zlog_err ("Can't lower privileges, %s", strerror (errno));
paul718e3742002-12-13 20:15:29 +0000154
155 if (fp == NULL)
156 return -1;
157
158 fprintf (fp, "1\n");
159
160 fclose (fp);
161
162 return ipforward_ipv6 ();
163}
164
165int
166ipforward_ipv6_off ()
167{
168 FILE *fp;
169
pauledd7c242003-06-04 13:59:38 +0000170 if ( zserv_privs.change(ZPRIVS_RAISE) )
171 zlog_err ("Can't raise privileges, %s", strerror (errno));
172
paul718e3742002-12-13 20:15:29 +0000173 fp = fopen (proc_ipv6_forwarding, "w");
pauledd7c242003-06-04 13:59:38 +0000174
175 if ( zserv_privs.change(ZPRIVS_LOWER) )
176 zlog_err ("Can't lower privileges, %s", strerror (errno));
paul718e3742002-12-13 20:15:29 +0000177
178 if (fp == NULL)
179 return -1;
180
181 fprintf (fp, "0\n");
182
183 fclose (fp);
184
185 return ipforward_ipv6 ();
186}
187#endif /* HAVE_IPV6 */