paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Kernel routing table read 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 "memory.h" |
| 26 | #include "log.h" |
Feng Lu | 758fb8f | 2014-07-03 18:23:09 +0800 | [diff] [blame] | 27 | #include "vrf.h" |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 28 | |
paul | 6621ca8 | 2005-11-23 13:02:08 +0000 | [diff] [blame] | 29 | #include "zebra/zserv.h" |
| 30 | #include "zebra/rt.h" |
paul | ec1a428 | 2005-11-24 15:15:17 +0000 | [diff] [blame] | 31 | #include "zebra/kernel_socket.h" |
paul | 6621ca8 | 2005-11-23 13:02:08 +0000 | [diff] [blame] | 32 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 33 | /* Kernel routing table read up by sysctl function. */ |
paul | 6621ca8 | 2005-11-23 13:02:08 +0000 | [diff] [blame] | 34 | void |
Feng Lu | 758fb8f | 2014-07-03 18:23:09 +0800 | [diff] [blame] | 35 | route_read (struct zebra_vrf *zvrf) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 36 | { |
| 37 | caddr_t buf, end, ref; |
| 38 | size_t bufsiz; |
| 39 | struct rt_msghdr *rtm; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 40 | |
| 41 | #define MIBSIZ 6 |
| 42 | int mib[MIBSIZ] = |
| 43 | { |
| 44 | CTL_NET, |
| 45 | PF_ROUTE, |
| 46 | 0, |
| 47 | 0, |
| 48 | NET_RT_DUMP, |
| 49 | 0 |
| 50 | }; |
Feng Lu | 758fb8f | 2014-07-03 18:23:09 +0800 | [diff] [blame] | 51 | |
| 52 | if (zvrf->vrf_id != VRF_DEFAULT) |
| 53 | return; |
| 54 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 55 | /* Get 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 fail: %s", safe_strerror (errno)); |
paul | ec1a428 | 2005-11-24 15:15:17 +0000 | [diff] [blame] | 59 | return; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | /* Allocate buffer. */ |
| 63 | ref = buf = XMALLOC (MTYPE_TMP, bufsiz); |
| 64 | |
| 65 | /* Read routing table information by calling sysctl(). */ |
| 66 | if (sysctl (mib, MIBSIZ, buf, &bufsiz, NULL, 0) < 0) |
| 67 | { |
ajs | 6099b3b | 2004-11-20 02:06:59 +0000 | [diff] [blame] | 68 | zlog_warn ("sysctl() fail by %s", safe_strerror (errno)); |
paul | ec1a428 | 2005-11-24 15:15:17 +0000 | [diff] [blame] | 69 | return; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | for (end = buf + bufsiz; buf < end; buf += rtm->rtm_msglen) |
| 73 | { |
| 74 | rtm = (struct rt_msghdr *) buf; |
Denis Ovsienko | 882968e | 2007-09-06 14:34:41 +0000 | [diff] [blame] | 75 | /* We must set RTF_DONE here, so rtm_read() doesn't ignore the message. */ |
| 76 | SET_FLAG (rtm->rtm_flags, RTF_DONE); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 77 | rtm_read (rtm); |
| 78 | } |
| 79 | |
| 80 | /* Free buffer. */ |
| 81 | XFREE (MTYPE_TMP, ref); |
| 82 | |
paul | ec1a428 | 2005-11-24 15:15:17 +0000 | [diff] [blame] | 83 | return; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 84 | } |