paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Get interface's address and mask information by sysctl() function. |
| 3 | * Copyright (C) 1997, 98 Kunihiro Ishiguro |
| 4 | * |
| 5 | * This file is part of GNU Zebra. |
| 6 | * |
| 7 | * GNU Zebra is free software; you can redistribute it and/or modify it |
| 8 | * under the terms of the GNU General Public License as published by the |
| 9 | * Free Software Foundation; either version 2, or (at your option) any |
| 10 | * later version. |
| 11 | * |
| 12 | * GNU Zebra is distributed in the hope that it will be useful, but |
| 13 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | * General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with GNU Zebra; see the file COPYING. If not, write to the Free |
| 19 | * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
| 20 | * 02111-1307, USA. |
| 21 | */ |
| 22 | |
| 23 | #include <zebra.h> |
| 24 | |
| 25 | #include "if.h" |
| 26 | #include "sockunion.h" |
| 27 | #include "prefix.h" |
| 28 | #include "connected.h" |
| 29 | #include "memory.h" |
| 30 | #include "ioctl.h" |
| 31 | #include "log.h" |
| 32 | |
paul | ec1a428 | 2005-11-24 15:15:17 +0000 | [diff] [blame] | 33 | #include "zebra/rt.h" |
| 34 | #include "zebra/kernel_socket.h" |
| 35 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 36 | int |
| 37 | ifstat_update_sysctl () |
| 38 | { |
| 39 | caddr_t ref, buf, end; |
| 40 | size_t bufsiz; |
| 41 | struct if_msghdr *ifm; |
| 42 | struct interface *ifp; |
| 43 | |
| 44 | #define MIBSIZ 6 |
| 45 | int mib[MIBSIZ] = |
| 46 | { |
| 47 | CTL_NET, |
| 48 | PF_ROUTE, |
| 49 | 0, |
| 50 | 0, /* AF_INET & AF_INET6 */ |
| 51 | NET_RT_IFLIST, |
| 52 | 0 |
| 53 | }; |
| 54 | |
| 55 | /* Query buffer size. */ |
| 56 | if (sysctl (mib, MIBSIZ, NULL, &bufsiz, NULL, 0) < 0) |
| 57 | { |
ajs | 6099b3b | 2004-11-20 02:06:59 +0000 | [diff] [blame] | 58 | zlog_warn ("sysctl() error by %s", safe_strerror (errno)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 59 | return -1; |
| 60 | } |
| 61 | |
| 62 | /* We free this memory at the end of this function. */ |
| 63 | ref = buf = XMALLOC (MTYPE_TMP, bufsiz); |
| 64 | |
| 65 | /* Fetch interface informations into allocated buffer. */ |
| 66 | if (sysctl (mib, MIBSIZ, buf, &bufsiz, NULL, 0) < 0) |
| 67 | { |
ajs | 6099b3b | 2004-11-20 02:06:59 +0000 | [diff] [blame] | 68 | zlog (NULL, LOG_WARNING, "sysctl error by %s", safe_strerror (errno)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 69 | return -1; |
| 70 | } |
| 71 | |
| 72 | /* Parse both interfaces and addresses. */ |
| 73 | for (end = buf + bufsiz; buf < end; buf += ifm->ifm_msglen) |
| 74 | { |
| 75 | ifm = (struct if_msghdr *) buf; |
| 76 | if (ifm->ifm_type == RTM_IFINFO) |
| 77 | { |
| 78 | ifp = if_lookup_by_index (ifm->ifm_index); |
| 79 | if (ifp) |
| 80 | ifp->stats = ifm->ifm_data; |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | /* Free sysctl buffer. */ |
| 85 | XFREE (MTYPE_TMP, ref); |
| 86 | |
| 87 | return 0; |
| 88 | } |
| 89 | |
| 90 | /* Interface listing up function using sysctl(). */ |
| 91 | void |
| 92 | interface_list () |
| 93 | { |
| 94 | caddr_t ref, buf, end; |
| 95 | size_t bufsiz; |
| 96 | struct if_msghdr *ifm; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 97 | |
| 98 | #define MIBSIZ 6 |
| 99 | int mib[MIBSIZ] = |
| 100 | { |
| 101 | CTL_NET, |
| 102 | PF_ROUTE, |
| 103 | 0, |
| 104 | 0, /* AF_INET & AF_INET6 */ |
| 105 | NET_RT_IFLIST, |
| 106 | 0 |
| 107 | }; |
| 108 | |
| 109 | /* Query buffer size. */ |
| 110 | if (sysctl (mib, MIBSIZ, NULL, &bufsiz, NULL, 0) < 0) |
| 111 | { |
ajs | 6099b3b | 2004-11-20 02:06:59 +0000 | [diff] [blame] | 112 | zlog (NULL, LOG_WARNING, "sysctl() error by %s", safe_strerror (errno)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 113 | return; |
| 114 | } |
| 115 | |
| 116 | /* We free this memory at the end of this function. */ |
| 117 | ref = buf = XMALLOC (MTYPE_TMP, bufsiz); |
| 118 | |
| 119 | /* Fetch interface informations into allocated buffer. */ |
| 120 | if (sysctl (mib, MIBSIZ, buf, &bufsiz, NULL, 0) < 0) |
| 121 | { |
ajs | 6099b3b | 2004-11-20 02:06:59 +0000 | [diff] [blame] | 122 | zlog (NULL, LOG_WARNING, "sysctl error by %s", safe_strerror (errno)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 123 | return; |
| 124 | } |
| 125 | |
| 126 | /* Parse both interfaces and addresses. */ |
| 127 | for (end = buf + bufsiz; buf < end; buf += ifm->ifm_msglen) |
| 128 | { |
| 129 | ifm = (struct if_msghdr *) buf; |
| 130 | |
| 131 | switch (ifm->ifm_type) |
| 132 | { |
| 133 | case RTM_IFINFO: |
| 134 | ifm_read (ifm); |
| 135 | break; |
| 136 | case RTM_NEWADDR: |
| 137 | ifam_read ((struct ifa_msghdr *) ifm); |
| 138 | break; |
| 139 | default: |
| 140 | zlog_info ("interfaces_list(): unexpected message type"); |
| 141 | XFREE (MTYPE_TMP, ref); |
| 142 | return; |
| 143 | break; |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | /* Free sysctl buffer. */ |
| 148 | XFREE (MTYPE_TMP, ref); |
| 149 | } |