]> git.proxmox.com Git - mirror_frr.git/blame - zebra/ipforward_proc.c
2004-11-19 Andrew J. Schorr <ajschorr@alumni.princeton.edu>
[mirror_frr.git] / zebra / ipforward_proc.c
CommitLineData
718e3744 1/*
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
edd7c245 25#include "log.h"
26#include "privs.h"
27
28extern struct zebra_privs_t zserv_privs;
29
718e3744 30char 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
44ebf843 63 fclose(fp);
64
718e3744 65 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;
edd7c245 78
79 if ( zserv_privs.change(ZPRIVS_RAISE) )
6099b3b5 80 zlog_err ("Can't raise privileges, %s", safe_strerror (errno) );
718e3744 81
82 fp = fopen (proc_ipv4_forwarding, "w");
edd7c245 83
41d3fc96 84 if (fp == NULL) {
85 if ( zserv_privs.change(ZPRIVS_LOWER) )
6099b3b5 86 zlog_err ("Can't lower privileges, %s", safe_strerror (errno));
718e3744 87 return -1;
41d3fc96 88 }
718e3744 89
90 fprintf (fp, "1\n");
91
92 fclose (fp);
93
41d3fc96 94 if ( zserv_privs.change(ZPRIVS_LOWER) )
6099b3b5 95 zlog_err ("Can't lower privileges, %s", safe_strerror (errno));
41d3fc96 96
718e3744 97 return ipforward ();
98}
99
100int
101ipforward_off ()
102{
103 FILE *fp;
104
edd7c245 105 if ( zserv_privs.change(ZPRIVS_RAISE) )
6099b3b5 106 zlog_err ("Can't raise privileges, %s", safe_strerror (errno));
edd7c245 107
718e3744 108 fp = fopen (proc_ipv4_forwarding, "w");
edd7c245 109
41d3fc96 110 if (fp == NULL) {
111 if ( zserv_privs.change(ZPRIVS_LOWER) )
6099b3b5 112 zlog_err ("Can't lower privileges, %s", safe_strerror (errno));
718e3744 113 return -1;
41d3fc96 114 }
718e3744 115
116 fprintf (fp, "0\n");
117
118 fclose (fp);
119
41d3fc96 120 if ( zserv_privs.change(ZPRIVS_LOWER) )
6099b3b5 121 zlog_err ("Can't lower privileges, %s", safe_strerror (errno));
41d3fc96 122
718e3744 123 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
144 return ipforwarding;
145}
146
147int
148ipforward_ipv6_on ()
149{
150 FILE *fp;
151
edd7c245 152 if ( zserv_privs.change(ZPRIVS_RAISE) )
6099b3b5 153 zlog_err ("Can't raise privileges, %s", safe_strerror (errno));
edd7c245 154
718e3744 155 fp = fopen (proc_ipv6_forwarding, "w");
edd7c245 156
41d3fc96 157 if (fp == NULL) {
158 if ( zserv_privs.change(ZPRIVS_LOWER) )
6099b3b5 159 zlog_err ("Can't lower privileges, %s", safe_strerror (errno));
718e3744 160 return -1;
41d3fc96 161 }
718e3744 162
163 fprintf (fp, "1\n");
164
165 fclose (fp);
166
41d3fc96 167 if ( zserv_privs.change(ZPRIVS_LOWER) )
6099b3b5 168 zlog_err ("Can't lower privileges, %s", safe_strerror (errno));
41d3fc96 169
718e3744 170 return ipforward_ipv6 ();
171}
172
173int
174ipforward_ipv6_off ()
175{
176 FILE *fp;
177
edd7c245 178 if ( zserv_privs.change(ZPRIVS_RAISE) )
6099b3b5 179 zlog_err ("Can't raise privileges, %s", safe_strerror (errno));
edd7c245 180
718e3744 181 fp = fopen (proc_ipv6_forwarding, "w");
edd7c245 182
41d3fc96 183 if (fp == NULL) {
184 if ( zserv_privs.change(ZPRIVS_LOWER) )
6099b3b5 185 zlog_err ("Can't lower privileges, %s", safe_strerror (errno));
718e3744 186 return -1;
41d3fc96 187 }
718e3744 188
189 fprintf (fp, "0\n");
190
191 fclose (fp);
192
41d3fc96 193 if ( zserv_privs.change(ZPRIVS_LOWER) )
6099b3b5 194 zlog_err ("Can't lower privileges, %s", safe_strerror (errno));
41d3fc96 195
718e3744 196 return ipforward_ipv6 ();
197}
198#endif /* HAVE_IPV6 */