]>
Commit | Line | Data |
---|---|---|
718e3744 | 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 | |
ac4d0be5 | 19 | * 02111-1307, USA. |
718e3744 | 20 | */ |
21 | ||
22 | #include <zebra.h> | |
edd7c245 | 23 | #include "privs.h" |
a1ac18c4 | 24 | #include "zebra/ipforward.h" |
718e3744 | 25 | |
718e3744 | 26 | #include "log.h" |
27 | ||
28 | #define MIB_SIZ 4 | |
29 | ||
edd7c245 | 30 | extern struct zebra_privs_t zserv_privs; |
31 | ||
718e3744 | 32 | /* IPv4 forwarding control MIB. */ |
ac4d0be5 | 33 | int mib[MIB_SIZ] = {CTL_NET, PF_INET, IPPROTO_IP, IPCTL_FORWARDING}; |
718e3744 | 34 | |
ac4d0be5 | 35 | int ipforward(void) |
718e3744 | 36 | { |
ac4d0be5 | 37 | size_t len; |
38 | int ipforwarding = 0; | |
39 | ||
40 | len = sizeof ipforwarding; | |
41 | if (sysctl(mib, MIB_SIZ, &ipforwarding, &len, 0, 0) < 0) { | |
42 | zlog_warn("Can't get ipforwarding value"); | |
43 | return -1; | |
44 | } | |
45 | return ipforwarding; | |
718e3744 | 46 | } |
47 | ||
ac4d0be5 | 48 | int ipforward_on(void) |
718e3744 | 49 | { |
ac4d0be5 | 50 | size_t len; |
51 | int ipforwarding = 1; | |
52 | ||
53 | len = sizeof ipforwarding; | |
54 | if (zserv_privs.change(ZPRIVS_RAISE)) | |
55 | zlog_err("Can't raise privileges"); | |
56 | if (sysctl(mib, MIB_SIZ, NULL, NULL, &ipforwarding, len) < 0) { | |
57 | if (zserv_privs.change(ZPRIVS_LOWER)) | |
58 | zlog_err("Can't lower privileges"); | |
59 | zlog_warn("Can't set ipforwarding on"); | |
60 | return -1; | |
61 | } | |
62 | if (zserv_privs.change(ZPRIVS_LOWER)) | |
63 | zlog_err("Can't lower privileges"); | |
64 | return ipforwarding; | |
718e3744 | 65 | } |
66 | ||
ac4d0be5 | 67 | int ipforward_off(void) |
718e3744 | 68 | { |
ac4d0be5 | 69 | size_t len; |
70 | int ipforwarding = 0; | |
71 | ||
72 | len = sizeof ipforwarding; | |
73 | if (zserv_privs.change(ZPRIVS_RAISE)) | |
74 | zlog_err("Can't raise privileges"); | |
75 | if (sysctl(mib, MIB_SIZ, NULL, NULL, &ipforwarding, len) < 0) { | |
76 | if (zserv_privs.change(ZPRIVS_LOWER)) | |
77 | zlog_err("Can't lower privileges"); | |
78 | zlog_warn("Can't set ipforwarding on"); | |
79 | return -1; | |
80 | } | |
81 | if (zserv_privs.change(ZPRIVS_LOWER)) | |
82 | zlog_err("Can't lower privileges"); | |
83 | return ipforwarding; | |
718e3744 | 84 | } |
85 | ||
718e3744 | 86 | /* IPv6 forwarding control MIB. */ |
ac4d0be5 | 87 | int mib_ipv6[MIB_SIZ] = {CTL_NET, PF_INET6, |
1cbb5dfc | 88 | #if defined(KAME) |
ac4d0be5 | 89 | IPPROTO_IPV6, IPV6CTL_FORWARDING |
90 | #else /* NOT KAME */ | |
91 | IPPROTO_IP, IP6CTL_FORWARDING | |
718e3744 | 92 | #endif /* KAME */ |
ac4d0be5 | 93 | }; |
718e3744 | 94 | |
ac4d0be5 | 95 | int ipforward_ipv6(void) |
718e3744 | 96 | { |
ac4d0be5 | 97 | size_t len; |
98 | int ip6forwarding = 0; | |
99 | ||
100 | len = sizeof ip6forwarding; | |
101 | if (zserv_privs.change(ZPRIVS_RAISE)) | |
102 | zlog_err("Can't raise privileges"); | |
103 | if (sysctl(mib_ipv6, MIB_SIZ, &ip6forwarding, &len, 0, 0) < 0) { | |
104 | if (zserv_privs.change(ZPRIVS_LOWER)) | |
105 | zlog_err("Can't lower privileges"); | |
106 | zlog_warn("can't get ip6forwarding value"); | |
107 | return -1; | |
108 | } | |
109 | if (zserv_privs.change(ZPRIVS_LOWER)) | |
110 | zlog_err("Can't lower privileges"); | |
111 | return ip6forwarding; | |
718e3744 | 112 | } |
113 | ||
ac4d0be5 | 114 | int ipforward_ipv6_on(void) |
718e3744 | 115 | { |
ac4d0be5 | 116 | size_t len; |
117 | int ip6forwarding = 1; | |
118 | ||
119 | len = sizeof ip6forwarding; | |
120 | if (zserv_privs.change(ZPRIVS_RAISE)) | |
121 | zlog_err("Can't raise privileges"); | |
122 | if (sysctl(mib_ipv6, MIB_SIZ, NULL, NULL, &ip6forwarding, len) < 0) { | |
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; | |
718e3744 | 131 | } |
132 | ||
ac4d0be5 | 133 | int ipforward_ipv6_off(void) |
718e3744 | 134 | { |
ac4d0be5 | 135 | size_t len; |
136 | int ip6forwarding = 0; | |
137 | ||
138 | len = sizeof ip6forwarding; | |
139 | if (zserv_privs.change(ZPRIVS_RAISE)) | |
140 | zlog_err("Can't raise privileges"); | |
141 | if (sysctl(mib_ipv6, MIB_SIZ, NULL, NULL, &ip6forwarding, len) < 0) { | |
142 | if (zserv_privs.change(ZPRIVS_LOWER)) | |
143 | zlog_err("Can't lower privileges"); | |
144 | zlog_warn("can't get ip6forwarding value"); | |
145 | return -1; | |
146 | } | |
147 | if (zserv_privs.change(ZPRIVS_LOWER)) | |
148 | zlog_err("Can't lower privileges"); | |
149 | return ip6forwarding; | |
718e3744 | 150 | } |