/contrib/bsnmp/snmp_mibII/mibII_ip.c

https://bitbucket.org/freebsd/freebsd-head/ · C · 493 lines · 363 code · 88 blank · 42 comment · 64 complexity · 3a95c10394f38cac5edc1214559f4e05 MD5 · raw file

  1. /*
  2. * Copyright (c) 2001-2003
  3. * Fraunhofer Institute for Open Communication Systems (FhG Fokus).
  4. * All rights reserved.
  5. *
  6. * Author: Harti Brandt <harti@freebsd.org>
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  18. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
  21. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  23. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  24. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  26. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  27. * SUCH DAMAGE.
  28. *
  29. * $Begemot: bsnmp/snmp_mibII/mibII_ip.c,v 1.11 2005/05/23 09:03:40 brandt_h Exp $
  30. *
  31. * ip group scalars.
  32. */
  33. #include "mibII.h"
  34. #include "mibII_oid.h"
  35. #include <netinet/in_systm.h>
  36. #include <netinet/ip.h>
  37. #include <netinet/ip_var.h>
  38. #include <netinet/ip_icmp.h>
  39. #include <netinet/icmp_var.h>
  40. static struct ipstat ipstat;
  41. static u_int ip_idrop;
  42. static struct icmpstat icmpstat;
  43. static int ip_forwarding;
  44. static int ip_defttl;
  45. static uint64_t ip_tick;
  46. static uint64_t ipstat_tick;
  47. static int
  48. fetch_ipstat(void)
  49. {
  50. size_t len;
  51. len = sizeof(ipstat);
  52. if (sysctlbyname("net.inet.ip.stats", &ipstat, &len, NULL, 0) == -1) {
  53. syslog(LOG_ERR, "net.inet.ip.stats: %m");
  54. return (-1);
  55. }
  56. if (len != sizeof(ipstat)) {
  57. syslog(LOG_ERR, "net.inet.ip.stats: wrong size");
  58. return (-1);
  59. }
  60. len = sizeof(ip_idrop);
  61. if (sysctlbyname("net.inet.ip.intr_queue_drops", &ip_idrop, &len, NULL, 0) == -1)
  62. syslog(LOG_WARNING, "net.inet.ip.intr_queue_drops: %m");
  63. if (len != sizeof(ip_idrop)) {
  64. syslog(LOG_WARNING, "net.inet.ip.intr_queue_drops: wrong size");
  65. ip_idrop = 0;
  66. }
  67. len = sizeof(icmpstat);
  68. if (sysctlbyname("net.inet.icmp.stats", &icmpstat, &len, NULL, 0) == -1) {
  69. syslog(LOG_ERR, "net.inet.icmp.stats: %m");
  70. return (-1);
  71. }
  72. if (len != sizeof(icmpstat)) {
  73. syslog(LOG_ERR, "net.inet.icmp.stats: wrong size");
  74. return (-1);
  75. }
  76. ipstat_tick = get_ticks();
  77. return (0);
  78. }
  79. static int
  80. fetch_ip(void)
  81. {
  82. size_t len;
  83. len = sizeof(ip_forwarding);
  84. if (sysctlbyname("net.inet.ip.forwarding", &ip_forwarding, &len,
  85. NULL, 0) == -1) {
  86. syslog(LOG_ERR, "net.inet.ip.forwarding: %m");
  87. return (-1);
  88. }
  89. if (len != sizeof(ip_forwarding)) {
  90. syslog(LOG_ERR, "net.inet.ip.forwarding: wrong size");
  91. return (-1);
  92. }
  93. len = sizeof(ip_defttl);
  94. if (sysctlbyname("net.inet.ip.ttl", &ip_defttl, &len,
  95. NULL, 0) == -1) {
  96. syslog(LOG_ERR, "net.inet.ip.ttl: %m");
  97. return (-1);
  98. }
  99. if (len != sizeof(ip_defttl)) {
  100. syslog(LOG_ERR, "net.inet.ip.ttl: wrong size");
  101. return (-1);
  102. }
  103. ip_tick = get_ticks();
  104. return (0);
  105. }
  106. static int
  107. ip_forward(int forw, int *old)
  108. {
  109. size_t olen;
  110. olen = sizeof(*old);
  111. if (sysctlbyname("net.inet.ip.forwarding", old, old ? &olen : NULL,
  112. &forw, sizeof(forw)) == -1) {
  113. syslog(LOG_ERR, "set net.inet.ip.forwarding: %m");
  114. return (-1);
  115. }
  116. ip_forwarding = forw;
  117. return (0);
  118. }
  119. static int
  120. ip_setttl(int ttl, int *old)
  121. {
  122. size_t olen;
  123. olen = sizeof(*old);
  124. if (sysctlbyname("net.inet.ip.ttl", old, old ? &olen : NULL,
  125. &ttl, sizeof(ttl)) == -1) {
  126. syslog(LOG_ERR, "set net.inet.ip.ttl: %m");
  127. return (-1);
  128. }
  129. ip_defttl = ttl;
  130. return (0);
  131. }
  132. /*
  133. * READ/WRITE ip group.
  134. */
  135. int
  136. op_ip(struct snmp_context *ctx, struct snmp_value *value,
  137. u_int sub, u_int idx __unused, enum snmp_op op)
  138. {
  139. int old;
  140. switch (op) {
  141. case SNMP_OP_GETNEXT:
  142. abort();
  143. case SNMP_OP_GET:
  144. break;
  145. case SNMP_OP_SET:
  146. if (ip_tick < this_tick)
  147. if (fetch_ip() == -1)
  148. return (SNMP_ERR_GENERR);
  149. switch (value->var.subs[sub - 1]) {
  150. case LEAF_ipForwarding:
  151. ctx->scratch->int1 = ip_forwarding ? 1 : 2;
  152. ctx->scratch->int2 = value->v.integer;
  153. if (value->v.integer == 1) {
  154. if (!ip_forwarding && ip_forward(1, &old))
  155. return (SNMP_ERR_GENERR);
  156. ctx->scratch->int1 = old ? 1 : 2;
  157. } else if (value->v.integer == 2) {
  158. if (ip_forwarding && ip_forward(0, &old))
  159. return (SNMP_ERR_GENERR);
  160. ctx->scratch->int1 = old;
  161. } else
  162. return (SNMP_ERR_WRONG_VALUE);
  163. break;
  164. case LEAF_ipDefaultTTL:
  165. ctx->scratch->int1 = ip_defttl;
  166. ctx->scratch->int2 = value->v.integer;
  167. if (value->v.integer < 1 || value->v.integer > 255)
  168. return (SNMP_ERR_WRONG_VALUE);
  169. if (ip_defttl != value->v.integer &&
  170. ip_setttl(value->v.integer, &old))
  171. return (SNMP_ERR_GENERR);
  172. ctx->scratch->int1 = old;
  173. break;
  174. }
  175. return (SNMP_ERR_NOERROR);
  176. case SNMP_OP_ROLLBACK:
  177. switch (value->var.subs[sub - 1]) {
  178. case LEAF_ipForwarding:
  179. if (ctx->scratch->int1 == 1) {
  180. if (ctx->scratch->int2 == 2)
  181. (void)ip_forward(1, NULL);
  182. } else {
  183. if (ctx->scratch->int2 == 1)
  184. (void)ip_forward(0, NULL);
  185. }
  186. break;
  187. case LEAF_ipDefaultTTL:
  188. if (ctx->scratch->int1 != ctx->scratch->int2)
  189. (void)ip_setttl(ctx->scratch->int1, NULL);
  190. break;
  191. }
  192. return (SNMP_ERR_NOERROR);
  193. case SNMP_OP_COMMIT:
  194. return (SNMP_ERR_NOERROR);
  195. }
  196. if (ip_tick < this_tick)
  197. if (fetch_ip() == -1)
  198. return (SNMP_ERR_GENERR);
  199. switch (value->var.subs[sub - 1]) {
  200. case LEAF_ipForwarding:
  201. value->v.integer = ip_forwarding ? 1 : 2;
  202. break;
  203. case LEAF_ipDefaultTTL:
  204. value->v.integer = ip_defttl;
  205. break;
  206. }
  207. return (SNMP_ERR_NOERROR);
  208. }
  209. /*
  210. * READ-ONLY statistics ip group.
  211. */
  212. int
  213. op_ipstat(struct snmp_context *ctx __unused, struct snmp_value *value,
  214. u_int sub, u_int idx __unused, enum snmp_op op)
  215. {
  216. switch (op) {
  217. case SNMP_OP_GETNEXT:
  218. abort();
  219. case SNMP_OP_GET:
  220. break;
  221. case SNMP_OP_SET:
  222. return (SNMP_ERR_NOT_WRITEABLE);
  223. case SNMP_OP_ROLLBACK:
  224. case SNMP_OP_COMMIT:
  225. abort();
  226. }
  227. if (ipstat_tick < this_tick)
  228. fetch_ipstat();
  229. switch (value->var.subs[sub - 1]) {
  230. case LEAF_ipInReceives:
  231. value->v.uint32 = ipstat.ips_total;
  232. break;
  233. case LEAF_ipInHdrErrors:
  234. value->v.uint32 = ipstat.ips_badsum + ipstat.ips_tooshort
  235. + ipstat.ips_toosmall + ipstat.ips_badhlen
  236. + ipstat.ips_badlen + ipstat.ips_badvers +
  237. + ipstat.ips_toolong;
  238. break;
  239. case LEAF_ipInAddrErrors:
  240. value->v.uint32 = ipstat.ips_cantforward;
  241. break;
  242. case LEAF_ipForwDatagrams:
  243. value->v.uint32 = ipstat.ips_forward;
  244. break;
  245. case LEAF_ipInUnknownProtos:
  246. value->v.uint32 = ipstat.ips_noproto;
  247. break;
  248. case LEAF_ipInDiscards:
  249. value->v.uint32 = ip_idrop;
  250. break;
  251. case LEAF_ipInDelivers:
  252. value->v.uint32 = ipstat.ips_delivered;
  253. break;
  254. case LEAF_ipOutRequests:
  255. value->v.uint32 = ipstat.ips_localout;
  256. break;
  257. case LEAF_ipOutDiscards:
  258. value->v.uint32 = ipstat.ips_odropped;
  259. break;
  260. case LEAF_ipOutNoRoutes:
  261. value->v.uint32 = ipstat.ips_noroute;
  262. break;
  263. case LEAF_ipReasmTimeout:
  264. value->v.integer = IPFRAGTTL;
  265. break;
  266. case LEAF_ipReasmReqds:
  267. value->v.uint32 = ipstat.ips_fragments;
  268. break;
  269. case LEAF_ipReasmOKs:
  270. value->v.uint32 = ipstat.ips_reassembled;
  271. break;
  272. case LEAF_ipReasmFails:
  273. value->v.uint32 = ipstat.ips_fragdropped
  274. + ipstat.ips_fragtimeout;
  275. break;
  276. case LEAF_ipFragOKs:
  277. value->v.uint32 = ipstat.ips_fragmented;
  278. break;
  279. case LEAF_ipFragFails:
  280. value->v.uint32 = ipstat.ips_cantfrag;
  281. break;
  282. case LEAF_ipFragCreates:
  283. value->v.uint32 = ipstat.ips_ofragments;
  284. break;
  285. }
  286. return (SNMP_ERR_NOERROR);
  287. }
  288. /*
  289. * READ-ONLY statistics icmp group.
  290. */
  291. int
  292. op_icmpstat(struct snmp_context *ctx __unused, struct snmp_value *value,
  293. u_int sub, u_int idx __unused, enum snmp_op op)
  294. {
  295. u_int i;
  296. switch (op) {
  297. case SNMP_OP_GETNEXT:
  298. abort();
  299. case SNMP_OP_GET:
  300. break;
  301. case SNMP_OP_SET:
  302. return (SNMP_ERR_NOT_WRITEABLE);
  303. case SNMP_OP_ROLLBACK:
  304. case SNMP_OP_COMMIT:
  305. abort();
  306. }
  307. if (ipstat_tick < this_tick)
  308. fetch_ipstat();
  309. switch (value->var.subs[sub - 1]) {
  310. case LEAF_icmpInMsgs:
  311. value->v.integer = 0;
  312. for (i = 0; i <= ICMP_MAXTYPE; i++)
  313. value->v.integer += icmpstat.icps_inhist[i];
  314. value->v.integer += icmpstat.icps_tooshort +
  315. icmpstat.icps_checksum;
  316. /* missing: bad type and packets on faith */
  317. break;
  318. case LEAF_icmpInErrors:
  319. value->v.integer = icmpstat.icps_tooshort +
  320. icmpstat.icps_checksum +
  321. icmpstat.icps_badlen +
  322. icmpstat.icps_badcode +
  323. icmpstat.icps_bmcastecho +
  324. icmpstat.icps_bmcasttstamp;
  325. break;
  326. case LEAF_icmpInDestUnreachs:
  327. value->v.integer = icmpstat.icps_inhist[ICMP_UNREACH];
  328. break;
  329. case LEAF_icmpInTimeExcds:
  330. value->v.integer = icmpstat.icps_inhist[ICMP_TIMXCEED];
  331. break;
  332. case LEAF_icmpInParmProbs:
  333. value->v.integer = icmpstat.icps_inhist[ICMP_PARAMPROB];
  334. break;
  335. case LEAF_icmpInSrcQuenchs:
  336. value->v.integer = icmpstat.icps_inhist[ICMP_SOURCEQUENCH];
  337. break;
  338. case LEAF_icmpInRedirects:
  339. value->v.integer = icmpstat.icps_inhist[ICMP_REDIRECT];
  340. break;
  341. case LEAF_icmpInEchos:
  342. value->v.integer = icmpstat.icps_inhist[ICMP_ECHO];
  343. break;
  344. case LEAF_icmpInEchoReps:
  345. value->v.integer = icmpstat.icps_inhist[ICMP_ECHOREPLY];
  346. break;
  347. case LEAF_icmpInTimestamps:
  348. value->v.integer = icmpstat.icps_inhist[ICMP_TSTAMP];
  349. break;
  350. case LEAF_icmpInTimestampReps:
  351. value->v.integer = icmpstat.icps_inhist[ICMP_TSTAMPREPLY];
  352. break;
  353. case LEAF_icmpInAddrMasks:
  354. value->v.integer = icmpstat.icps_inhist[ICMP_MASKREQ];
  355. break;
  356. case LEAF_icmpInAddrMaskReps:
  357. value->v.integer = icmpstat.icps_inhist[ICMP_MASKREPLY];
  358. break;
  359. case LEAF_icmpOutMsgs:
  360. value->v.integer = 0;
  361. for (i = 0; i <= ICMP_MAXTYPE; i++)
  362. value->v.integer += icmpstat.icps_outhist[i];
  363. value->v.integer += icmpstat.icps_badaddr +
  364. icmpstat.icps_noroute;
  365. break;
  366. case LEAF_icmpOutErrors:
  367. value->v.integer = icmpstat.icps_badaddr +
  368. icmpstat.icps_noroute;
  369. break;
  370. case LEAF_icmpOutDestUnreachs:
  371. value->v.integer = icmpstat.icps_outhist[ICMP_UNREACH];
  372. break;
  373. case LEAF_icmpOutTimeExcds:
  374. value->v.integer = icmpstat.icps_outhist[ICMP_TIMXCEED];
  375. break;
  376. case LEAF_icmpOutParmProbs:
  377. value->v.integer = icmpstat.icps_outhist[ICMP_PARAMPROB];
  378. break;
  379. case LEAF_icmpOutSrcQuenchs:
  380. value->v.integer = icmpstat.icps_outhist[ICMP_SOURCEQUENCH];
  381. break;
  382. case LEAF_icmpOutRedirects:
  383. value->v.integer = icmpstat.icps_outhist[ICMP_REDIRECT];
  384. break;
  385. case LEAF_icmpOutEchos:
  386. value->v.integer = icmpstat.icps_outhist[ICMP_ECHO];
  387. break;
  388. case LEAF_icmpOutEchoReps:
  389. value->v.integer = icmpstat.icps_outhist[ICMP_ECHOREPLY];
  390. break;
  391. case LEAF_icmpOutTimestamps:
  392. value->v.integer = icmpstat.icps_outhist[ICMP_TSTAMP];
  393. break;
  394. case LEAF_icmpOutTimestampReps:
  395. value->v.integer = icmpstat.icps_outhist[ICMP_TSTAMPREPLY];
  396. break;
  397. case LEAF_icmpOutAddrMasks:
  398. value->v.integer = icmpstat.icps_outhist[ICMP_MASKREQ];
  399. break;
  400. case LEAF_icmpOutAddrMaskReps:
  401. value->v.integer = icmpstat.icps_outhist[ICMP_MASKREPLY];
  402. break;
  403. }
  404. return (SNMP_ERR_NOERROR);
  405. }