]> git.proxmox.com Git - mirror_frr.git/blame - zebra/ipforward_proc.c
Merge pull request #964 from opensourcerouting/plist-trie-corruption-3.0
[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 *
896014f4
DL
17 * You should have received a copy of the GNU General Public License along
18 * with this program; see the file COPYING; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
718e3744 20 */
21
22#include <zebra.h>
23
ddfeb486
DL
24#ifdef GNU_LINUX
25
edd7c245 26#include "log.h"
27#include "privs.h"
28
a1ac18c4 29#include "zebra/ipforward.h"
30
edd7c245 31extern struct zebra_privs_t zserv_privs;
32
718e3744 33char proc_net_snmp[] = "/proc/net/snmp";
34
d62a17ae 35static void dropline(FILE *fp)
718e3744 36{
d62a17ae 37 int c;
718e3744 38
d62a17ae 39 while ((c = getc(fp)) != '\n')
40 ;
718e3744 41}
42
d62a17ae 43int ipforward(void)
718e3744 44{
d62a17ae 45 FILE *fp;
46 int ipforwarding = 0;
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 if (fgets(buf, 6, fp))
61 sscanf(buf, "Ip: %d", &ipforwarding);
62
63 fclose(fp);
64
65 if (ipforwarding == 1)
66 return 1;
67
68 return 0;
718e3744 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
d62a17ae 74int ipforward_on(void)
718e3744 75{
d62a17ae 76 FILE *fp;
77
78 if (zserv_privs.change(ZPRIVS_RAISE))
79 zlog_err("Can't raise privileges, %s", safe_strerror(errno));
718e3744 80
d62a17ae 81 fp = fopen(proc_ipv4_forwarding, "w");
edd7c245 82
d62a17ae 83 if (fp == NULL) {
84 if (zserv_privs.change(ZPRIVS_LOWER))
85 zlog_err("Can't lower privileges, %s",
86 safe_strerror(errno));
87 return -1;
88 }
718e3744 89
d62a17ae 90 fprintf(fp, "1\n");
718e3744 91
d62a17ae 92 fclose(fp);
718e3744 93
d62a17ae 94 if (zserv_privs.change(ZPRIVS_LOWER))
95 zlog_err("Can't lower privileges, %s", safe_strerror(errno));
41d3fc96 96
d62a17ae 97 return ipforward();
718e3744 98}
99
d62a17ae 100int ipforward_off(void)
718e3744 101{
d62a17ae 102 FILE *fp;
718e3744 103
d62a17ae 104 if (zserv_privs.change(ZPRIVS_RAISE))
105 zlog_err("Can't raise privileges, %s", safe_strerror(errno));
edd7c245 106
d62a17ae 107 fp = fopen(proc_ipv4_forwarding, "w");
edd7c245 108
d62a17ae 109 if (fp == NULL) {
110 if (zserv_privs.change(ZPRIVS_LOWER))
111 zlog_err("Can't lower privileges, %s",
112 safe_strerror(errno));
113 return -1;
114 }
718e3744 115
d62a17ae 116 fprintf(fp, "0\n");
718e3744 117
d62a17ae 118 fclose(fp);
718e3744 119
d62a17ae 120 if (zserv_privs.change(ZPRIVS_LOWER))
121 zlog_err("Can't lower privileges, %s", safe_strerror(errno));
41d3fc96 122
d62a17ae 123 return ipforward();
718e3744 124}
718e3744 125
126char proc_ipv6_forwarding[] = "/proc/sys/net/ipv6/conf/all/forwarding";
127
d62a17ae 128int ipforward_ipv6(void)
718e3744 129{
d62a17ae 130 FILE *fp;
131 char buf[5];
132 int ipforwarding = 0;
718e3744 133
d62a17ae 134 fp = fopen(proc_ipv6_forwarding, "r");
718e3744 135
d62a17ae 136 if (fp == NULL)
137 return -1;
718e3744 138
d62a17ae 139 if (fgets(buf, 2, fp))
140 sscanf(buf, "%d", &ipforwarding);
718e3744 141
d62a17ae 142 fclose(fp);
143 return ipforwarding;
718e3744 144}
145
d62a17ae 146int ipforward_ipv6_on(void)
718e3744 147{
d62a17ae 148 FILE *fp;
718e3744 149
d62a17ae 150 if (zserv_privs.change(ZPRIVS_RAISE))
151 zlog_err("Can't raise privileges, %s", safe_strerror(errno));
edd7c245 152
d62a17ae 153 fp = fopen(proc_ipv6_forwarding, "w");
edd7c245 154
d62a17ae 155 if (fp == NULL) {
156 if (zserv_privs.change(ZPRIVS_LOWER))
157 zlog_err("Can't lower privileges, %s",
158 safe_strerror(errno));
159 return -1;
160 }
718e3744 161
d62a17ae 162 fprintf(fp, "1\n");
718e3744 163
d62a17ae 164 fclose(fp);
718e3744 165
d62a17ae 166 if (zserv_privs.change(ZPRIVS_LOWER))
167 zlog_err("Can't lower privileges, %s", safe_strerror(errno));
41d3fc96 168
d62a17ae 169 return ipforward_ipv6();
718e3744 170}
171
56c1f7d8 172
d62a17ae 173int ipforward_ipv6_off(void)
718e3744 174{
d62a17ae 175 FILE *fp;
718e3744 176
d62a17ae 177 if (zserv_privs.change(ZPRIVS_RAISE))
178 zlog_err("Can't raise privileges, %s", safe_strerror(errno));
edd7c245 179
d62a17ae 180 fp = fopen(proc_ipv6_forwarding, "w");
edd7c245 181
d62a17ae 182 if (fp == NULL) {
183 if (zserv_privs.change(ZPRIVS_LOWER))
184 zlog_err("Can't lower privileges, %s",
185 safe_strerror(errno));
186 return -1;
187 }
718e3744 188
d62a17ae 189 fprintf(fp, "0\n");
718e3744 190
d62a17ae 191 fclose(fp);
718e3744 192
d62a17ae 193 if (zserv_privs.change(ZPRIVS_LOWER))
194 zlog_err("Can't lower privileges, %s", safe_strerror(errno));
41d3fc96 195
d62a17ae 196 return ipforward_ipv6();
718e3744 197}
ddfeb486
DL
198
199#endif /* GNU_LINUX */