]> git.proxmox.com Git - mirror_frr.git/blame - zebra/ipforward_proc.c
Merge pull request #3502 from donaldsharp/socket_to_me_baby
[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{
116e176d 45 int ret = 0;
d62a17ae 46 FILE *fp;
47 int ipforwarding = 0;
48 char buf[10];
49
50 fp = fopen(proc_net_snmp, "r");
51
52 if (fp == NULL)
53 return -1;
54
55 /* We don't care about the first line. */
56 dropline(fp);
57
58 /* Get ip_statistics.IpForwarding :
59 1 => ip forwarding enabled
60 2 => ip forwarding off. */
61 if (fgets(buf, 6, fp))
116e176d 62 ret = sscanf(buf, "Ip: %d", &ipforwarding);
d62a17ae 63
64 fclose(fp);
65
116e176d 66 if (ret == 1 && ipforwarding == 1)
d62a17ae 67 return 1;
68
69 return 0;
718e3744 70}
71
72/* char proc_ipv4_forwarding[] = "/proc/sys/net/ipv4/conf/all/forwarding"; */
73char proc_ipv4_forwarding[] = "/proc/sys/net/ipv4/ip_forward";
74
d62a17ae 75int ipforward_on(void)
718e3744 76{
d62a17ae 77 FILE *fp;
78
01b9e3fd 79 frr_elevate_privs(&zserv_privs) {
718e3744 80
01b9e3fd 81 fp = fopen(proc_ipv4_forwarding, "w");
edd7c245 82
01b9e3fd
DL
83 if (fp == NULL) {
84 return -1;
85 }
718e3744 86
01b9e3fd 87 fprintf(fp, "1\n");
718e3744 88
01b9e3fd 89 fclose(fp);
718e3744 90
01b9e3fd 91 }
41d3fc96 92
d62a17ae 93 return ipforward();
718e3744 94}
95
d62a17ae 96int ipforward_off(void)
718e3744 97{
d62a17ae 98 FILE *fp;
718e3744 99
01b9e3fd 100 frr_elevate_privs(&zserv_privs) {
edd7c245 101
01b9e3fd 102 fp = fopen(proc_ipv4_forwarding, "w");
edd7c245 103
01b9e3fd
DL
104 if (fp == NULL) {
105 return -1;
106 }
718e3744 107
01b9e3fd 108 fprintf(fp, "0\n");
718e3744 109
01b9e3fd 110 fclose(fp);
718e3744 111
01b9e3fd 112 }
41d3fc96 113
d62a17ae 114 return ipforward();
718e3744 115}
718e3744 116
117char proc_ipv6_forwarding[] = "/proc/sys/net/ipv6/conf/all/forwarding";
118
d62a17ae 119int ipforward_ipv6(void)
718e3744 120{
116e176d 121 int ret = 0;
d62a17ae 122 FILE *fp;
123 char buf[5];
124 int ipforwarding = 0;
718e3744 125
d62a17ae 126 fp = fopen(proc_ipv6_forwarding, "r");
718e3744 127
d62a17ae 128 if (fp == NULL)
129 return -1;
718e3744 130
d62a17ae 131 if (fgets(buf, 2, fp))
116e176d 132 ret = sscanf(buf, "%d", &ipforwarding);
718e3744 133
d62a17ae 134 fclose(fp);
116e176d
DS
135
136 if (ret != 1)
137 return 0;
138
d62a17ae 139 return ipforwarding;
718e3744 140}
141
d62a17ae 142int ipforward_ipv6_on(void)
718e3744 143{
d62a17ae 144 FILE *fp;
718e3744 145
01b9e3fd 146 frr_elevate_privs(&zserv_privs) {
edd7c245 147
01b9e3fd 148 fp = fopen(proc_ipv6_forwarding, "w");
edd7c245 149
01b9e3fd
DL
150 if (fp == NULL) {
151 return -1;
152 }
718e3744 153
01b9e3fd 154 fprintf(fp, "1\n");
718e3744 155
01b9e3fd 156 fclose(fp);
718e3744 157
01b9e3fd 158 }
41d3fc96 159
d62a17ae 160 return ipforward_ipv6();
718e3744 161}
162
56c1f7d8 163
d62a17ae 164int ipforward_ipv6_off(void)
718e3744 165{
d62a17ae 166 FILE *fp;
718e3744 167
01b9e3fd 168 frr_elevate_privs(&zserv_privs) {
edd7c245 169
01b9e3fd 170 fp = fopen(proc_ipv6_forwarding, "w");
edd7c245 171
01b9e3fd
DL
172 if (fp == NULL) {
173 return -1;
174 }
718e3744 175
01b9e3fd 176 fprintf(fp, "0\n");
718e3744 177
01b9e3fd 178 fclose(fp);
718e3744 179
01b9e3fd 180 }
41d3fc96 181
d62a17ae 182 return ipforward_ipv6();
718e3744 183}
ddfeb486
DL
184
185#endif /* GNU_LINUX */