blob: a31ec84b67081bed85495e9598aaf303878a5aba [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
63 if (ipforwarding == 1)
64 return 1;
65
66 return 0;
67}
68
69/* char proc_ipv4_forwarding[] = "/proc/sys/net/ipv4/conf/all/forwarding"; */
70char proc_ipv4_forwarding[] = "/proc/sys/net/ipv4/ip_forward";
71
72int
73ipforward_on ()
74{
75 FILE *fp;
pauledd7c242003-06-04 13:59:38 +000076
77 if ( zserv_privs.change(ZPRIVS_RAISE) )
78 zlog_err ("Can't raise privileges, %s", strerror (errno) );
paul718e3742002-12-13 20:15:29 +000079
80 fp = fopen (proc_ipv4_forwarding, "w");
pauledd7c242003-06-04 13:59:38 +000081
82 if ( zserv_privs.change(ZPRIVS_LOWER) )
83 zlog_err ("Can't lower privileges, %s", strerror (errno));
84
paul718e3742002-12-13 20:15:29 +000085 if (fp == NULL)
86 return -1;
87
88 fprintf (fp, "1\n");
89
90 fclose (fp);
91
92 return ipforward ();
93}
94
95int
96ipforward_off ()
97{
98 FILE *fp;
99
pauledd7c242003-06-04 13:59:38 +0000100 if ( zserv_privs.change(ZPRIVS_RAISE) )
101 zlog_err ("Can't raise privileges, %s", strerror (errno));
102
paul718e3742002-12-13 20:15:29 +0000103 fp = fopen (proc_ipv4_forwarding, "w");
pauledd7c242003-06-04 13:59:38 +0000104
105 if ( zserv_privs.change(ZPRIVS_LOWER) )
106 zlog_err ("Can't lower privileges, %s", strerror (errno));
107
paul718e3742002-12-13 20:15:29 +0000108
109 if (fp == NULL)
110 return -1;
111
112 fprintf (fp, "0\n");
113
114 fclose (fp);
115
116 return ipforward ();
117}
118#ifdef HAVE_IPV6
119
120char proc_ipv6_forwarding[] = "/proc/sys/net/ipv6/conf/all/forwarding";
121
122int
123ipforward_ipv6 ()
124{
125 FILE *fp;
126 char buf[5];
127 int ipforwarding = 0;
128
129 fp = fopen (proc_ipv6_forwarding, "r");
130
131 if (fp == NULL)
132 return -1;
133
134 fgets (buf, 2, fp);
135 sscanf (buf, "%d", &ipforwarding);
136
137 return ipforwarding;
138}
139
140int
141ipforward_ipv6_on ()
142{
143 FILE *fp;
144
pauledd7c242003-06-04 13:59:38 +0000145 if ( zserv_privs.change(ZPRIVS_RAISE) )
146 zlog_err ("Can't raise privileges, %s", strerror (errno));
147
paul718e3742002-12-13 20:15:29 +0000148 fp = fopen (proc_ipv6_forwarding, "w");
pauledd7c242003-06-04 13:59:38 +0000149
150 if ( zserv_privs.change(ZPRIVS_LOWER) )
151 zlog_err ("Can't lower privileges, %s", strerror (errno));
paul718e3742002-12-13 20:15:29 +0000152
153 if (fp == NULL)
154 return -1;
155
156 fprintf (fp, "1\n");
157
158 fclose (fp);
159
160 return ipforward_ipv6 ();
161}
162
163int
164ipforward_ipv6_off ()
165{
166 FILE *fp;
167
pauledd7c242003-06-04 13:59:38 +0000168 if ( zserv_privs.change(ZPRIVS_RAISE) )
169 zlog_err ("Can't raise privileges, %s", strerror (errno));
170
paul718e3742002-12-13 20:15:29 +0000171 fp = fopen (proc_ipv6_forwarding, "w");
pauledd7c242003-06-04 13:59:38 +0000172
173 if ( zserv_privs.change(ZPRIVS_LOWER) )
174 zlog_err ("Can't lower privileges, %s", strerror (errno));
paul718e3742002-12-13 20:15:29 +0000175
176 if (fp == NULL)
177 return -1;
178
179 fprintf (fp, "0\n");
180
181 fclose (fp);
182
183 return ipforward_ipv6 ();
184}
185#endif /* HAVE_IPV6 */