mtu_kvm.c 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /* MTU get using kvm_read.
  2. * Copyright (C) 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. #include <zebra.h>
  22. #include <kvm.h>
  23. #include <limits.h>
  24. #include <fcntl.h>
  25. #include "if.h"
  26. /* get interface MTU to use kvm_read */
  27. void
  28. if_kvm_get_mtu (struct interface *ifp)
  29. {
  30. kvm_t *kvmd;
  31. struct ifnet ifnet;
  32. unsigned long ifnetaddr;
  33. int len;
  34. char ifname[IFNAMSIZ];
  35. char tname[INTERFACE_NAMSIZ + 1];
  36. char buf[_POSIX2_LINE_MAX];
  37. struct nlist nl[] =
  38. {
  39. {"_ifnet"},
  40. {""}
  41. };
  42. ifp->mtu6 = ifp->mtu = -1;
  43. kvmd = kvm_openfiles (NULL, NULL, NULL, O_RDONLY, buf);
  44. if (kvmd == NULL)
  45. return ;
  46. kvm_nlist(kvmd, nl);
  47. ifnetaddr = nl[0].n_value;
  48. if (kvm_read(kvmd, ifnetaddr, (char *)&ifnetaddr, sizeof ifnetaddr) < 0)
  49. {
  50. kvm_close (kvmd);
  51. return ;
  52. }
  53. while(ifnetaddr != 0)
  54. {
  55. if (kvm_read (kvmd, ifnetaddr, (char *)&ifnet, sizeof ifnet) < 0)
  56. {
  57. kvm_close (kvmd);
  58. return ;
  59. }
  60. if (kvm_read (kvmd, (u_long)ifnet.if_name, ifname, IFNAMSIZ) < 0)
  61. {
  62. kvm_close (kvmd);
  63. return ;
  64. }
  65. len = snprintf (tname, INTERFACE_NAMSIZ + 1,
  66. "%s%d", ifname, ifnet.if_unit);
  67. if (strncmp (tname, ifp->name, len) == 0)
  68. break;
  69. ifnetaddr = (u_long)ifnet.if_next;
  70. }
  71. kvm_close (kvmd);
  72. if (ifnetaddr == 0)
  73. {
  74. return ;
  75. }
  76. ifp->mtu6 = ifp->mtu = ifnet.if_mtu;
  77. }