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