Debian Codegraph @

agent/mibgroup/ip-mib/data_access/scalars_sysctl.c

raw
1 /*
2 * IP-MIB architecture support
3 *
4 * $Id$
5 */
6 #include <net-snmp/net-snmp-config.h>
7 #include <net-snmp/net-snmp-includes.h>
8
9 #include <net-snmp/agent/net-snmp-agent-includes.h>
10 #include <net-snmp/data_access/ip_scalars.h>
11
12 #include <sys/types.h>
13 #include <sys/protosw.h>
14 #include <sys/sysctl.h>
15 #include <netinet/in_systm.h>
16 #include <netinet/ip.h>
17 #include <errno.h>
18 #include <stdlib.h>
19
20 /* XXX: the values passed to netsnmp_arch_ip_scalars(..) may or may not be
21 * portable to the other BSDs -- it seems to be portable back to FreeBSD 4.x
22 * (Darwin 10.8.0) at least.
23 */
24
25 static int
26 netsnmp_arch_ip_scalars_sysctl(const char *access_module,
27 int mib[], size_t mib_len,
28 u_long *old_value, u_long *new_value)
29 {
30 int newint, oldint;
31 size_t needed;
32 int rc;
33
34 needed = sizeof(oldint);
35 if (new_value)
36 newint = *new_value;
37
38 rc = sysctl(mib, mib_len, &oldint, &needed,
39 new_value ? &newint : NULL, (new_value ? sizeof(newint) : 0));
40 if (rc == -1) {
41 DEBUGMSGTL((access_module, "sysctl %s failed - %s\n",
42 (new_value == NULL ? "get" : "set"),
43 strerror(errno)));
44 }
45 *old_value = oldint;
46 return rc;
47 }
48
49 int ipDefaultTTL_mib[] = {
50 CTL_NET,
51 PF_INET,
52 IPPROTO_IP,
53 IPCTL_DEFTTL
54 };
55
56 int ipForwarding_mib[] = {
57 CTL_NET,
58 PF_INET,
59 IPPROTO_IP,
60 IPCTL_FORWARDING
61 };
62
63 int ipv6IpDefaultHopLimit_mib[] = {
64 CTL_NET,
65 PF_INET6,
66 IPPROTO_IP,
67 IPV6CTL_DEFHLIM
68 };
69
70 int ipv6IpForwarding_mib[] = {
71 CTL_NET,
72 PF_INET6,
73 IPPROTO_IP,
74 IPV6CTL_FORWARDING
75 };
76
77 #define MIB_LEN(a) (sizeof(a) / sizeof(*a))
78
79 #define IPDEFAULTTTL_LEN MIB_LEN(ipDefaultTTL_mib)
80 #define IPFORWARDING_LEN MIB_LEN(ipForwarding_mib)
81 #define IPV6IPDEFAULTHOPLIMIT_LEN MIB_LEN(ipv6IpDefaultHopLimit_mib)
82 #define IPV6IPFORWARDING_LEN MIB_LEN(ipv6IpForwarding_mib)
83
84 int
85 netsnmp_arch_ip_scalars_ipDefaultTTL_get(u_long *value)
86 {
87 int rc;
88
89 if (NULL == value)
90 return -1;
91
92 rc = netsnmp_arch_ip_scalars_sysctl("access:ipDefaultTTL",
93 ipDefaultTTL_mib,
94 IPDEFAULTTTL_LEN,
95 value, NULL);
96 if (rc != 0)
97 return -2;
98
99 return 0;
100 }
101
102 int
103 netsnmp_arch_ip_scalars_ipDefaultTTL_set(u_long value)
104 {
105 int rc;
106
107 if (1 == value)
108 ;
109 else if (2 == value)
110 value = 0;
111 else {
112 DEBUGMSGTL(("access:ipDefaultTTL", "bad value %ld\n",
113 value));
114 return SNMP_ERR_WRONGVALUE;
115 }
116
117 rc = netsnmp_arch_ip_scalars_sysctl("access:ipForwarding",
118 ipForwarding_mib,
119 IPFORWARDING_LEN,
120 NULL, &value);
121 if (rc != 0)
122 return -1;
123
124 return 0;
125 }
126
127 int
128 netsnmp_arch_ip_scalars_ipForwarding_get(u_long *value)
129 {
130 int rc;
131
132 if (NULL == value)
133 return -1;
134
135 rc = netsnmp_arch_ip_scalars_sysctl("access:ipForwarding",
136 ipForwarding_mib,
137 IPFORWARDING_LEN,
138 value, NULL);
139 if (rc != 0)
140 return -2;
141
142 /* On FreeBSD 7.2 at least, the value passed to the sysctl can be coerced
143 * into a non-zero value; convert it into a value that's sane per the
144 * IP-MIB definition.
145 */
146 if (*value != 0)
147 *value = 1;
148
149 return 0;
150 }
151
152 int
153 netsnmp_arch_ip_scalars_ipForwarding_set(u_long value)
154 {
155 int rc;
156
157 if (1 == value)
158 ;
159 else if (2 == value)
160 value = 0;
161 else {
162 DEBUGMSGTL(("access:ipForwarding", "bad value %ld\n",
163 value));
164 return SNMP_ERR_WRONGVALUE;
165 }
166
167 rc = netsnmp_arch_ip_scalars_sysctl("access:ipForwarding",
168 ipForwarding_mib,
169 IPFORWARDING_LEN,
170 NULL, &value);
171 if (rc != 0)
172 return -1;
173
174 return 0;
175 }
176
177 int
178 netsnmp_arch_ip_scalars_ipv6IpDefaultHopLimit_get(u_long *value)
179 {
180 int rc;
181
182 if (NULL == value)
183 return -1;
184
185 rc = netsnmp_arch_ip_scalars_sysctl("access:ipv6IpDefaultHopLimit",
186 ipv6IpDefaultHopLimit_mib,
187 IPV6IPDEFAULTHOPLIMIT_LEN,
188 value, NULL);
189 if (rc != 0)
190 return -2;
191
192 return 0;
193 }
194
195 int
196 netsnmp_arch_ip_scalars_ipv6IpDefaultHopLimit_set(u_long value)
197 {
198 int rc;
199
200 if (1 == value)
201 ;
202 else if (2 == value)
203 value = 0;
204 else {
205 DEBUGMSGTL(("access:ipv6IpDefaultHopLimit", "bad value %ld\n",
206 value));
207 return SNMP_ERR_WRONGVALUE;
208 }
209
210 rc = netsnmp_arch_ip_scalars_sysctl("access:ipForwarding",
211 ipv6IpDefaultHopLimit_mib,
212 IPV6IPDEFAULTHOPLIMIT_LEN,
213 NULL, &value);
214 if (rc != 0)
215 return -1;
216
217 return 0;
218 }
219
220 int
221 netsnmp_arch_ip_scalars_ipv6IpForwarding_get(u_long *value)
222 {
223 int rc;
224
225 if (NULL == value)
226 return -1;
227
228 rc = netsnmp_arch_ip_scalars_sysctl("access:ipv6IpForwarding",
229 ipv6IpForwarding_mib,
230 IPV6IPFORWARDING_LEN,
231 value, NULL);
232 if (rc != 0)
233 return -2;
234
235 /* On FreeBSD 7.2 at least, the value passed to the sysctl can be coerced
236 * into a non-zero value; convert it into a value that's sane per the
237 * IP-MIB definition.
238 */
239 if (*value != 0)
240 *value = 1;
241
242 return 0;
243 }
244
245 int
246 netsnmp_arch_ip_scalars_ipv6IpForwarding_set(u_long value)
247 {
248 int rc;
249
250 if (1 == value)
251 ;
252 else if (2 == value)
253 value = 0;
254 else {
255 DEBUGMSGTL(("access:ipForwarding", "bad value %ld\n",
256 value));
257 return SNMP_ERR_WRONGVALUE;
258 }
259
260 rc = netsnmp_arch_ip_scalars_sysctl("access:ipv6IpForwarding",
261 ipv6IpForwarding_mib,
262 IPV6IPFORWARDING_LEN,
263 NULL, &value);
264 if (rc != 0)
265 return -1;
266
267 return 0;
268 }
269
270 long netsnmp_arch_ip_scalars_ipReasmTimeout_get(void)
271 {
272 #ifdef freebsd14
273 #define FRAGTTL_CTL "net.inet.ip.fragttl"
274 int intval;
275
276 if (sysctlbyname(FRAGTTL_CTL, &intval, &(size_t){sizeof(int)}, NULL, 0) <
277 0) {
278 DEBUGMSGTL(("access::ipReasmTimeout", "sysctl %s failed - %s\n",
279 FRAGTTL_CTL,
280 strerror(errno)));
281 return -1;
282 }
283 return intval;
284 #else
285 /*
286 * This value is static at compile time on FreeBSD; it really should be a
287 * probed via either sysctl or sysconf at runtime as the compiled value and
288 * the runtime value compiled into the kernel can vary.
289 *
290 * Please refer to sys/protosw.h for more details on what this value is (in
291 * particular PR_SLOWHZ).
292 */
293 return IPFRAGTTL / PR_SLOWHZ;
294 #endif
295 }
296
297 void
298 netsnmp_arch_ip_scalars_register_handlers(void)
299 {
300 }