]> git.proxmox.com Git - mirror_frr.git/blob - zebra/ipforward_proc.c
08fbfede42aff314518df36463ec7a7b44883c81
[mirror_frr.git] / zebra / ipforward_proc.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Fetch ipforward value by reading /proc filesystem.
4 * Copyright (C) 1997 Kunihiro Ishiguro
5 */
6
7 #include <zebra.h>
8
9 #ifdef GNU_LINUX
10
11 #include "log.h"
12 #include "privs.h"
13
14 #include "zebra/ipforward.h"
15
16 extern struct zebra_privs_t zserv_privs;
17
18 static const char proc_net_snmp[] = "/proc/net/snmp";
19
20 static void dropline(FILE *fp)
21 {
22 while (getc(fp) != '\n')
23 ;
24 }
25
26 int ipforward(void)
27 {
28 int ret = 0;
29 FILE *fp;
30 int ipforwarding = 0;
31 char buf[10];
32
33 fp = fopen(proc_net_snmp, "r");
34
35 if (fp == NULL)
36 return -1;
37
38 /* We don't care about the first line. */
39 dropline(fp);
40
41 /* Get ip_statistics.IpForwarding :
42 1 => ip forwarding enabled
43 2 => ip forwarding off. */
44 if (fgets(buf, 6, fp))
45 ret = sscanf(buf, "Ip: %d", &ipforwarding);
46
47 fclose(fp);
48
49 if (ret == 1 && ipforwarding == 1)
50 return 1;
51
52 return 0;
53 }
54
55 /* char proc_ipv4_forwarding[] = "/proc/sys/net/ipv4/conf/all/forwarding"; */
56 static const char proc_ipv4_forwarding[] = "/proc/sys/net/ipv4/ip_forward";
57
58 int ipforward_on(void)
59 {
60 FILE *fp;
61
62 frr_with_privs(&zserv_privs) {
63
64 fp = fopen(proc_ipv4_forwarding, "w");
65
66 if (fp == NULL) {
67 return -1;
68 }
69
70 fprintf(fp, "1\n");
71
72 fclose(fp);
73
74 }
75
76 return ipforward();
77 }
78
79 int ipforward_off(void)
80 {
81 FILE *fp;
82
83 frr_with_privs(&zserv_privs) {
84
85 fp = fopen(proc_ipv4_forwarding, "w");
86
87 if (fp == NULL) {
88 return -1;
89 }
90
91 fprintf(fp, "0\n");
92
93 fclose(fp);
94
95 }
96
97 return ipforward();
98 }
99
100 static const char proc_ipv6_forwarding[] =
101 "/proc/sys/net/ipv6/conf/all/forwarding";
102
103 int ipforward_ipv6(void)
104 {
105 int ret = 0;
106 FILE *fp;
107 char buf[5];
108 int ipforwarding = 0;
109
110 fp = fopen(proc_ipv6_forwarding, "r");
111
112 if (fp == NULL)
113 return -1;
114
115 if (fgets(buf, 2, fp))
116 ret = sscanf(buf, "%d", &ipforwarding);
117
118 fclose(fp);
119
120 if (ret != 1)
121 return 0;
122
123 return ipforwarding;
124 }
125
126 int ipforward_ipv6_on(void)
127 {
128 FILE *fp;
129
130 frr_with_privs(&zserv_privs) {
131
132 fp = fopen(proc_ipv6_forwarding, "w");
133
134 if (fp == NULL) {
135 return -1;
136 }
137
138 fprintf(fp, "1\n");
139
140 fclose(fp);
141
142 }
143
144 return ipforward_ipv6();
145 }
146
147
148 int ipforward_ipv6_off(void)
149 {
150 FILE *fp;
151
152 frr_with_privs(&zserv_privs) {
153
154 fp = fopen(proc_ipv6_forwarding, "w");
155
156 if (fp == NULL) {
157 return -1;
158 }
159
160 fprintf(fp, "0\n");
161
162 fclose(fp);
163
164 }
165
166 return ipforward_ipv6();
167 }
168
169 #endif /* GNU_LINUX */