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