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" |
David Lamparter | 388f885 | 2015-03-03 08:55:54 +0100 | [diff] [blame] | 32 | #include "interface.h" |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 33 | |
paul | ec1a428 | 2005-11-24 15:15:17 +0000 | [diff] [blame] | 34 | #include "zebra/rt.h" |
| 35 | #include "zebra/kernel_socket.h" |
| 36 | |
Paul Jakma | f28b0e5 | 2006-08-06 15:57:59 +0000 | [diff] [blame] | 37 | void |
| 38 | ifstat_update_sysctl (void) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 39 | { |
| 40 | caddr_t ref, buf, end; |
| 41 | size_t bufsiz; |
| 42 | struct if_msghdr *ifm; |
| 43 | struct interface *ifp; |
| 44 | |
| 45 | #define MIBSIZ 6 |
| 46 | int mib[MIBSIZ] = |
| 47 | { |
| 48 | CTL_NET, |
| 49 | PF_ROUTE, |
| 50 | 0, |
| 51 | 0, /* AF_INET & AF_INET6 */ |
| 52 | NET_RT_IFLIST, |
| 53 | 0 |
| 54 | }; |
| 55 | |
| 56 | /* Query buffer size. */ |
| 57 | if (sysctl (mib, MIBSIZ, NULL, &bufsiz, NULL, 0) < 0) |
| 58 | { |
ajs | 6099b3b | 2004-11-20 02:06:59 +0000 | [diff] [blame] | 59 | zlog_warn ("sysctl() error by %s", safe_strerror (errno)); |
Paul Jakma | f28b0e5 | 2006-08-06 15:57:59 +0000 | [diff] [blame] | 60 | return; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | /* We free this memory at the end of this function. */ |
| 64 | ref = buf = XMALLOC (MTYPE_TMP, bufsiz); |
| 65 | |
| 66 | /* Fetch interface informations into allocated buffer. */ |
| 67 | if (sysctl (mib, MIBSIZ, buf, &bufsiz, NULL, 0) < 0) |
| 68 | { |
ajs | 6099b3b | 2004-11-20 02:06:59 +0000 | [diff] [blame] | 69 | zlog (NULL, LOG_WARNING, "sysctl error by %s", safe_strerror (errno)); |
Paul Jakma | f28b0e5 | 2006-08-06 15:57:59 +0000 | [diff] [blame] | 70 | return; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | /* Parse both interfaces and addresses. */ |
| 74 | for (end = buf + bufsiz; buf < end; buf += ifm->ifm_msglen) |
| 75 | { |
| 76 | ifm = (struct if_msghdr *) buf; |
| 77 | if (ifm->ifm_type == RTM_IFINFO) |
| 78 | { |
| 79 | ifp = if_lookup_by_index (ifm->ifm_index); |
| 80 | if (ifp) |
| 81 | ifp->stats = ifm->ifm_data; |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | /* Free sysctl buffer. */ |
| 86 | XFREE (MTYPE_TMP, ref); |
| 87 | |
Paul Jakma | f28b0e5 | 2006-08-06 15:57:59 +0000 | [diff] [blame] | 88 | return; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | /* Interface listing up function using sysctl(). */ |
| 92 | void |
| 93 | interface_list () |
| 94 | { |
| 95 | caddr_t ref, buf, end; |
| 96 | size_t bufsiz; |
| 97 | struct if_msghdr *ifm; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 98 | |
| 99 | #define MIBSIZ 6 |
| 100 | int mib[MIBSIZ] = |
| 101 | { |
| 102 | CTL_NET, |
| 103 | PF_ROUTE, |
| 104 | 0, |
| 105 | 0, /* AF_INET & AF_INET6 */ |
| 106 | NET_RT_IFLIST, |
| 107 | 0 |
| 108 | }; |
| 109 | |
| 110 | /* Query buffer size. */ |
| 111 | if (sysctl (mib, MIBSIZ, NULL, &bufsiz, NULL, 0) < 0) |
| 112 | { |
ajs | 6099b3b | 2004-11-20 02:06:59 +0000 | [diff] [blame] | 113 | zlog (NULL, LOG_WARNING, "sysctl() error by %s", safe_strerror (errno)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 114 | return; |
| 115 | } |
| 116 | |
| 117 | /* We free this memory at the end of this function. */ |
| 118 | ref = buf = XMALLOC (MTYPE_TMP, bufsiz); |
| 119 | |
| 120 | /* Fetch interface informations into allocated buffer. */ |
| 121 | if (sysctl (mib, MIBSIZ, buf, &bufsiz, NULL, 0) < 0) |
| 122 | { |
ajs | 6099b3b | 2004-11-20 02:06:59 +0000 | [diff] [blame] | 123 | zlog (NULL, LOG_WARNING, "sysctl error by %s", safe_strerror (errno)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 124 | return; |
| 125 | } |
| 126 | |
| 127 | /* Parse both interfaces and addresses. */ |
| 128 | for (end = buf + bufsiz; buf < end; buf += ifm->ifm_msglen) |
| 129 | { |
| 130 | ifm = (struct if_msghdr *) buf; |
| 131 | |
| 132 | switch (ifm->ifm_type) |
| 133 | { |
| 134 | case RTM_IFINFO: |
| 135 | ifm_read (ifm); |
| 136 | break; |
| 137 | case RTM_NEWADDR: |
| 138 | ifam_read ((struct ifa_msghdr *) ifm); |
| 139 | break; |
| 140 | default: |
| 141 | zlog_info ("interfaces_list(): unexpected message type"); |
| 142 | XFREE (MTYPE_TMP, ref); |
| 143 | return; |
| 144 | break; |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | /* Free sysctl buffer. */ |
| 149 | XFREE (MTYPE_TMP, ref); |
| 150 | } |