]>
Commit | Line | Data |
---|---|---|
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 | 31 | extern struct zebra_privs_t zserv_privs; |
32 | ||
2b64873d | 33 | static const char proc_net_snmp[] = "/proc/net/snmp"; |
718e3744 | 34 | |
d62a17ae | 35 | static void dropline(FILE *fp) |
718e3744 | 36 | { |
d62a17ae | 37 | int c; |
718e3744 | 38 | |
d62a17ae | 39 | while ((c = getc(fp)) != '\n') |
40 | ; | |
718e3744 | 41 | } |
42 | ||
d62a17ae | 43 | int 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"; */ | |
2b64873d | 73 | static const char proc_ipv4_forwarding[] = "/proc/sys/net/ipv4/ip_forward"; |
718e3744 | 74 | |
d62a17ae | 75 | int ipforward_on(void) |
718e3744 | 76 | { |
d62a17ae | 77 | FILE *fp; |
78 | ||
0cf6db21 | 79 | frr_with_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 | 96 | int ipforward_off(void) |
718e3744 | 97 | { |
d62a17ae | 98 | FILE *fp; |
718e3744 | 99 | |
0cf6db21 | 100 | frr_with_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 | |
2b64873d DL |
117 | static const char proc_ipv6_forwarding[] = |
118 | "/proc/sys/net/ipv6/conf/all/forwarding"; | |
718e3744 | 119 | |
d62a17ae | 120 | int ipforward_ipv6(void) |
718e3744 | 121 | { |
116e176d | 122 | int ret = 0; |
d62a17ae | 123 | FILE *fp; |
124 | char buf[5]; | |
125 | int ipforwarding = 0; | |
718e3744 | 126 | |
d62a17ae | 127 | fp = fopen(proc_ipv6_forwarding, "r"); |
718e3744 | 128 | |
d62a17ae | 129 | if (fp == NULL) |
130 | return -1; | |
718e3744 | 131 | |
d62a17ae | 132 | if (fgets(buf, 2, fp)) |
116e176d | 133 | ret = sscanf(buf, "%d", &ipforwarding); |
718e3744 | 134 | |
d62a17ae | 135 | fclose(fp); |
116e176d DS |
136 | |
137 | if (ret != 1) | |
138 | return 0; | |
139 | ||
d62a17ae | 140 | return ipforwarding; |
718e3744 | 141 | } |
142 | ||
d62a17ae | 143 | int ipforward_ipv6_on(void) |
718e3744 | 144 | { |
d62a17ae | 145 | FILE *fp; |
718e3744 | 146 | |
0cf6db21 | 147 | frr_with_privs(&zserv_privs) { |
edd7c245 | 148 | |
01b9e3fd | 149 | fp = fopen(proc_ipv6_forwarding, "w"); |
edd7c245 | 150 | |
01b9e3fd DL |
151 | if (fp == NULL) { |
152 | return -1; | |
153 | } | |
718e3744 | 154 | |
01b9e3fd | 155 | fprintf(fp, "1\n"); |
718e3744 | 156 | |
01b9e3fd | 157 | fclose(fp); |
718e3744 | 158 | |
01b9e3fd | 159 | } |
41d3fc96 | 160 | |
d62a17ae | 161 | return ipforward_ipv6(); |
718e3744 | 162 | } |
163 | ||
56c1f7d8 | 164 | |
d62a17ae | 165 | int ipforward_ipv6_off(void) |
718e3744 | 166 | { |
d62a17ae | 167 | FILE *fp; |
718e3744 | 168 | |
0cf6db21 | 169 | frr_with_privs(&zserv_privs) { |
edd7c245 | 170 | |
01b9e3fd | 171 | fp = fopen(proc_ipv6_forwarding, "w"); |
edd7c245 | 172 | |
01b9e3fd DL |
173 | if (fp == NULL) { |
174 | return -1; | |
175 | } | |
718e3744 | 176 | |
01b9e3fd | 177 | fprintf(fp, "0\n"); |
718e3744 | 178 | |
01b9e3fd | 179 | fclose(fp); |
718e3744 | 180 | |
01b9e3fd | 181 | } |
41d3fc96 | 182 | |
d62a17ae | 183 | return ipforward_ipv6(); |
718e3744 | 184 | } |
ddfeb486 DL |
185 | |
186 | #endif /* GNU_LINUX */ |