]> git.proxmox.com Git - mirror_frr.git/blob - zebra/ipforward_sysctl.c
Merge branch 'stable/2.0'
[mirror_frr.git] / zebra / ipforward_sysctl.c
1 /* IP forward control by sysctl function.
2 * Copyright (C) 1997, 1999 Kunihiro Ishiguro
3 *
4 * This file is part of GNU Zebra.
5 *
6 * GNU Zebra is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2, or (at your option) any
9 * later version.
10 *
11 * GNU Zebra is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with GNU Zebra; see the file COPYING. If not, write to the Free
18 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
19 * 02111-1307, USA.
20 */
21
22 #include <zebra.h>
23 #include "privs.h"
24 #include "zebra/ipforward.h"
25
26 #include "log.h"
27
28 #define MIB_SIZ 4
29
30 extern struct zebra_privs_t zserv_privs;
31
32 /* IPv4 forwarding control MIB. */
33 int mib[MIB_SIZ] =
34 {
35 CTL_NET,
36 PF_INET,
37 IPPROTO_IP,
38 IPCTL_FORWARDING
39 };
40
41 int
42 ipforward (void)
43 {
44 size_t len;
45 int ipforwarding = 0;
46
47 len = sizeof ipforwarding;
48 if (sysctl (mib, MIB_SIZ, &ipforwarding, &len, 0, 0) < 0)
49 {
50 zlog_warn ("Can't get ipforwarding value");
51 return -1;
52 }
53 return ipforwarding;
54 }
55
56 int
57 ipforward_on (void)
58 {
59 size_t len;
60 int ipforwarding = 1;
61
62 len = sizeof ipforwarding;
63 if (zserv_privs.change(ZPRIVS_RAISE))
64 zlog_err("Can't raise privileges");
65 if (sysctl (mib, MIB_SIZ, NULL, NULL, &ipforwarding, len) < 0)
66 {
67 if (zserv_privs.change(ZPRIVS_LOWER))
68 zlog_err("Can't lower privileges");
69 zlog_warn ("Can't set ipforwarding on");
70 return -1;
71 }
72 if (zserv_privs.change(ZPRIVS_LOWER))
73 zlog_err("Can't lower privileges");
74 return ipforwarding;
75 }
76
77 int
78 ipforward_off (void)
79 {
80 size_t len;
81 int ipforwarding = 0;
82
83 len = sizeof ipforwarding;
84 if (zserv_privs.change(ZPRIVS_RAISE))
85 zlog_err("Can't raise privileges");
86 if (sysctl (mib, MIB_SIZ, NULL, NULL, &ipforwarding, len) < 0)
87 {
88 if (zserv_privs.change(ZPRIVS_LOWER))
89 zlog_err("Can't lower privileges");
90 zlog_warn ("Can't set ipforwarding on");
91 return -1;
92 }
93 if (zserv_privs.change(ZPRIVS_LOWER))
94 zlog_err("Can't lower privileges");
95 return ipforwarding;
96 }
97
98 /* IPv6 forwarding control MIB. */
99 int mib_ipv6[MIB_SIZ] =
100 {
101 CTL_NET,
102 PF_INET6,
103 #if defined(KAME)
104 IPPROTO_IPV6,
105 IPV6CTL_FORWARDING
106 #else /* NOT KAME */
107 IPPROTO_IP,
108 IP6CTL_FORWARDING
109 #endif /* KAME */
110 };
111
112 int
113 ipforward_ipv6 (void)
114 {
115 size_t len;
116 int ip6forwarding = 0;
117
118 len = sizeof ip6forwarding;
119 if (zserv_privs.change(ZPRIVS_RAISE))
120 zlog_err("Can't raise privileges");
121 if (sysctl (mib_ipv6, MIB_SIZ, &ip6forwarding, &len, 0, 0) < 0)
122 {
123 if (zserv_privs.change(ZPRIVS_LOWER))
124 zlog_err("Can't lower privileges");
125 zlog_warn ("can't get ip6forwarding value");
126 return -1;
127 }
128 if (zserv_privs.change(ZPRIVS_LOWER))
129 zlog_err("Can't lower privileges");
130 return ip6forwarding;
131 }
132
133 int
134 ipforward_ipv6_on (void)
135 {
136 size_t len;
137 int ip6forwarding = 1;
138
139 len = sizeof ip6forwarding;
140 if (zserv_privs.change(ZPRIVS_RAISE))
141 zlog_err("Can't raise privileges");
142 if (sysctl (mib_ipv6, MIB_SIZ, NULL, NULL, &ip6forwarding, len) < 0)
143 {
144 if (zserv_privs.change(ZPRIVS_LOWER))
145 zlog_err("Can't lower privileges");
146 zlog_warn ("can't get ip6forwarding value");
147 return -1;
148 }
149 if (zserv_privs.change(ZPRIVS_LOWER))
150 zlog_err("Can't lower privileges");
151 return ip6forwarding;
152 }
153
154 int
155 ipforward_ipv6_off (void)
156 {
157 size_t len;
158 int ip6forwarding = 0;
159
160 len = sizeof ip6forwarding;
161 if (zserv_privs.change(ZPRIVS_RAISE))
162 zlog_err("Can't raise privileges");
163 if (sysctl (mib_ipv6, MIB_SIZ, NULL, NULL, &ip6forwarding, len) < 0)
164 {
165 if (zserv_privs.change(ZPRIVS_LOWER))
166 zlog_err("Can't lower privileges");
167 zlog_warn ("can't get ip6forwarding value");
168 return -1;
169 }
170 if (zserv_privs.change(ZPRIVS_LOWER))
171 zlog_err("Can't lower privileges");
172 return ip6forwarding;
173 }