paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1 | /* Kernel communication using routing socket. |
| 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 | |
| 22 | #include <zebra.h> |
| 23 | |
| 24 | #include "if.h" |
| 25 | #include "prefix.h" |
| 26 | #include "sockunion.h" |
| 27 | #include "connected.h" |
| 28 | #include "memory.h" |
| 29 | #include "ioctl.h" |
| 30 | #include "log.h" |
| 31 | #include "str.h" |
| 32 | #include "table.h" |
| 33 | #include "rib.h" |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 34 | #include "privs.h" |
Feng Lu | 0d0686f | 2015-05-22 11:40:02 +0200 | [diff] [blame] | 35 | #include "vrf.h" |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 36 | |
| 37 | #include "zebra/interface.h" |
| 38 | #include "zebra/zserv.h" |
| 39 | #include "zebra/debug.h" |
paul | ec1a428 | 2005-11-24 15:15:17 +0000 | [diff] [blame] | 40 | #include "zebra/kernel_socket.h" |
Feng Lu | 758fb8f | 2014-07-03 18:23:09 +0800 | [diff] [blame] | 41 | #include "zebra/rib.h" |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 42 | |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 43 | extern struct zebra_privs_t zserv_privs; |
paul | 9bcdb63 | 2003-07-08 08:09:45 +0000 | [diff] [blame] | 44 | extern struct zebra_t zebrad; |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 45 | |
gdt | 4bfbea8 | 2004-01-06 01:13:05 +0000 | [diff] [blame] | 46 | /* |
Greg Troxel | 273b1bd | 2014-12-02 14:51:49 -0500 | [diff] [blame] | 47 | * Historically, the BSD routing socket has aligned data following a |
| 48 | * struct sockaddr to sizeof(long), which was 4 bytes on some |
| 49 | * platforms, and 8 bytes on others. NetBSD 6 changed the routing |
| 50 | * socket to align to sizeof(uint64_t), which is 8 bytes. OS X |
| 51 | * appears to align to sizeof(int), which is 4 bytes. |
gdt | 4bfbea8 | 2004-01-06 01:13:05 +0000 | [diff] [blame] | 52 | * |
Greg Troxel | 273b1bd | 2014-12-02 14:51:49 -0500 | [diff] [blame] | 53 | * Alignment of zero-sized sockaddrs is nonsensical, but historically |
| 54 | * BSD defines RT_ROUNDUP(0) to be the alignment interval (rather than |
| 55 | * 0). We follow this practice without questioning it, but it is a |
| 56 | * bug if quagga calls ROUNDUP with 0. |
gdt | 4bfbea8 | 2004-01-06 01:13:05 +0000 | [diff] [blame] | 57 | */ |
Greg Troxel | 273b1bd | 2014-12-02 14:51:49 -0500 | [diff] [blame] | 58 | |
| 59 | /* |
| 60 | * Because of these varying conventions, the only sane approach is for |
| 61 | * the <net/route.h> header to define some flavor of ROUNDUP macro. |
| 62 | */ |
David Lamparter | 7e92322 | 2015-03-03 21:04:20 +0100 | [diff] [blame] | 63 | |
| 64 | #if defined(SA_SIZE) |
| 65 | /* SAROUNDUP is the only thing we need, and SA_SIZE provides that */ |
| 66 | #define SAROUNDUP(a) SA_SIZE(a) |
| 67 | #else /* !SA_SIZE */ |
| 68 | |
Greg Troxel | 273b1bd | 2014-12-02 14:51:49 -0500 | [diff] [blame] | 69 | #if defined(RT_ROUNDUP) |
| 70 | #define ROUNDUP(a) RT_ROUNDUP(a) |
| 71 | #endif /* defined(RT_ROUNDUP) */ |
| 72 | |
David Lamparter | a2b5031 | 2015-09-15 19:35:41 -0700 | [diff] [blame] | 73 | #if defined(SUNOS_5) |
| 74 | /* Solaris has struct sockaddr_in[6] definitions at 16 / 32 bytes size, |
| 75 | * so the whole concept doesn't really apply. */ |
| 76 | #define ROUNDUP(a) (a) |
| 77 | #endif |
| 78 | |
Greg Troxel | 273b1bd | 2014-12-02 14:51:49 -0500 | [diff] [blame] | 79 | /* |
| 80 | * If ROUNDUP has not yet been defined in terms of platform-provided |
| 81 | * defines, attempt to cope with heuristics. |
| 82 | */ |
| 83 | #if !defined(ROUNDUP) |
| 84 | |
| 85 | /* |
| 86 | * It's a bug for a platform not to define rounding/alignment for |
| 87 | * sockaddrs on the routing socket. This warning really is |
| 88 | * intentional, to provoke filing bug reports with operating systems |
| 89 | * that don't define RT_ROUNDUP or equivalent. |
| 90 | */ |
| 91 | #warning "net/route.h does not define RT_ROUNDUP; making unwarranted assumptions!" |
| 92 | |
| 93 | /* OS X (Xcode as of 2014-12) is known not to define RT_ROUNDUP */ |
Doug VanLeuven | 3b33de6 | 2012-10-10 22:10:14 +0000 | [diff] [blame] | 94 | #ifdef __APPLE__ |
Greg Troxel | 273b1bd | 2014-12-02 14:51:49 -0500 | [diff] [blame] | 95 | #define ROUNDUP_TYPE int |
Greg Troxel | 941789e | 2015-03-23 15:16:29 -0400 | [diff] [blame] | 96 | #else |
| 97 | #define ROUNDUP_TYPE long |
Doug VanLeuven | 3b33de6 | 2012-10-10 22:10:14 +0000 | [diff] [blame] | 98 | #endif |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 99 | |
Greg Troxel | 273b1bd | 2014-12-02 14:51:49 -0500 | [diff] [blame] | 100 | #define ROUNDUP(a) \ |
| 101 | ((a) > 0 ? (1 + (((a) - 1) | (sizeof(ROUNDUP_TYPE) - 1))) : sizeof(ROUNDUP_TYPE)) |
| 102 | |
| 103 | #endif /* defined(ROUNDUP) */ |
| 104 | |
gdt | 4bfbea8 | 2004-01-06 01:13:05 +0000 | [diff] [blame] | 105 | /* |
| 106 | * Given a pointer (sockaddr or void *), return the number of bytes |
| 107 | * taken up by the sockaddr and any padding needed for alignment. |
| 108 | */ |
Paul Jakma | 6f0e3f6 | 2007-05-10 02:38:51 +0000 | [diff] [blame] | 109 | #if defined(HAVE_STRUCT_SOCKADDR_SA_LEN) |
gdt | 4bfbea8 | 2004-01-06 01:13:05 +0000 | [diff] [blame] | 110 | #define SAROUNDUP(X) ROUNDUP(((struct sockaddr *)(X))->sa_len) |
paul | 30be802 | 2003-10-22 02:51:38 +0000 | [diff] [blame] | 111 | #elif defined(HAVE_IPV6) |
gdt | 4bfbea8 | 2004-01-06 01:13:05 +0000 | [diff] [blame] | 112 | /* |
| 113 | * One would hope all fixed-size structure definitions are aligned, |
| 114 | * but round them up nonetheless. |
| 115 | */ |
| 116 | #define SAROUNDUP(X) \ |
paul | 3e95a07 | 2003-09-24 00:05:45 +0000 | [diff] [blame] | 117 | (((struct sockaddr *)(X))->sa_family == AF_INET ? \ |
| 118 | ROUNDUP(sizeof(struct sockaddr_in)):\ |
| 119 | (((struct sockaddr *)(X))->sa_family == AF_INET6 ? \ |
| 120 | ROUNDUP(sizeof(struct sockaddr_in6)) : \ |
| 121 | (((struct sockaddr *)(X))->sa_family == AF_LINK ? \ |
paul | c50ae8b | 2004-05-11 11:31:07 +0000 | [diff] [blame] | 122 | ROUNDUP(sizeof(struct sockaddr_dl)) : sizeof(struct sockaddr)))) |
paul | 30be802 | 2003-10-22 02:51:38 +0000 | [diff] [blame] | 123 | #else /* HAVE_IPV6 */ |
gdt | 4bfbea8 | 2004-01-06 01:13:05 +0000 | [diff] [blame] | 124 | #define SAROUNDUP(X) \ |
paul | 30be802 | 2003-10-22 02:51:38 +0000 | [diff] [blame] | 125 | (((struct sockaddr *)(X))->sa_family == AF_INET ? \ |
| 126 | ROUNDUP(sizeof(struct sockaddr_in)):\ |
| 127 | (((struct sockaddr *)(X))->sa_family == AF_LINK ? \ |
| 128 | ROUNDUP(sizeof(struct sockaddr_dl)) : sizeof(struct sockaddr))) |
Paul Jakma | 6f0e3f6 | 2007-05-10 02:38:51 +0000 | [diff] [blame] | 129 | #endif /* HAVE_STRUCT_SOCKADDR_SA_LEN */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 130 | |
David Lamparter | 7e92322 | 2015-03-03 21:04:20 +0100 | [diff] [blame] | 131 | #endif /* !SA_SIZE */ |
| 132 | |
Doug VanLeuven | a05df8f | 2012-10-10 16:11:36 -0700 | [diff] [blame] | 133 | /* |
| 134 | * We use a call to an inline function to copy (PNT) to (DEST) |
| 135 | * 1. Calculating the length of the copy requires an #ifdef to determine |
| 136 | * if sa_len is a field and can't be used directly inside a #define |
| 137 | * 2. So the compiler doesn't complain when DEST is NULL, which is only true |
| 138 | * when we are skipping the copy and incrementing to the next SA |
paul | ec1a428 | 2005-11-24 15:15:17 +0000 | [diff] [blame] | 139 | */ |
David Lamparter | 3e9e2c9 | 2015-04-10 09:14:58 +0200 | [diff] [blame] | 140 | static inline void |
Doug VanLeuven | a05df8f | 2012-10-10 16:11:36 -0700 | [diff] [blame] | 141 | rta_copy (union sockunion *dest, caddr_t src) { |
| 142 | int len; |
David Lamparter | da1b7ea | 2015-09-15 20:36:20 -0700 | [diff] [blame] | 143 | if (!dest) |
| 144 | return; |
Doug VanLeuven | a05df8f | 2012-10-10 16:11:36 -0700 | [diff] [blame] | 145 | #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN |
| 146 | len = (((struct sockaddr *)src)->sa_len > sizeof (*dest)) ? |
| 147 | sizeof (*dest) : ((struct sockaddr *)src)->sa_len ; |
| 148 | #else |
| 149 | len = (SAROUNDUP (src) > sizeof (*dest)) ? |
| 150 | sizeof (*dest) : SAROUNDUP (src) ; |
| 151 | #endif |
| 152 | memcpy (dest, src, len); |
| 153 | } |
| 154 | |
paul | 62debbb | 2005-06-14 14:07:07 +0000 | [diff] [blame] | 155 | #define RTA_ADDR_GET(DEST, RTA, RTMADDRS, PNT) \ |
| 156 | if ((RTMADDRS) & (RTA)) \ |
| 157 | { \ |
| 158 | int len = SAROUNDUP ((PNT)); \ |
David Lamparter | da1b7ea | 2015-09-15 20:36:20 -0700 | [diff] [blame] | 159 | if (af_check (((struct sockaddr *)(PNT))->sa_family)) \ |
Doug VanLeuven | a05df8f | 2012-10-10 16:11:36 -0700 | [diff] [blame] | 160 | rta_copy((DEST), (PNT)); \ |
paul | 62debbb | 2005-06-14 14:07:07 +0000 | [diff] [blame] | 161 | (PNT) += len; \ |
| 162 | } |
| 163 | #define RTA_ATTR_GET(DEST, RTA, RTMADDRS, PNT) \ |
| 164 | if ((RTMADDRS) & (RTA)) \ |
| 165 | { \ |
| 166 | int len = SAROUNDUP ((PNT)); \ |
David Lamparter | da1b7ea | 2015-09-15 20:36:20 -0700 | [diff] [blame] | 167 | rta_copy((DEST), (PNT)); \ |
paul | 62debbb | 2005-06-14 14:07:07 +0000 | [diff] [blame] | 168 | (PNT) += len; \ |
| 169 | } |
| 170 | |
paul | 6fe70d1 | 2005-11-12 22:55:10 +0000 | [diff] [blame] | 171 | #define RTA_NAME_GET(DEST, RTA, RTMADDRS, PNT, LEN) \ |
| 172 | if ((RTMADDRS) & (RTA)) \ |
| 173 | { \ |
paul | ec1a428 | 2005-11-24 15:15:17 +0000 | [diff] [blame] | 174 | u_char *pdest = (u_char *) (DEST); \ |
paul | 6fe70d1 | 2005-11-12 22:55:10 +0000 | [diff] [blame] | 175 | int len = SAROUNDUP ((PNT)); \ |
| 176 | struct sockaddr_dl *sdl = (struct sockaddr_dl *)(PNT); \ |
| 177 | if (IS_ZEBRA_DEBUG_KERNEL) \ |
| 178 | zlog_debug ("%s: RTA_SDL_GET nlen %d, alen %d", \ |
| 179 | __func__, sdl->sdl_nlen, sdl->sdl_alen); \ |
| 180 | if ( ((DEST) != NULL) && (sdl->sdl_family == AF_LINK) \ |
| 181 | && (sdl->sdl_nlen < IFNAMSIZ) && (sdl->sdl_nlen <= len) ) \ |
| 182 | { \ |
paul | ec1a428 | 2005-11-24 15:15:17 +0000 | [diff] [blame] | 183 | memcpy (pdest, sdl->sdl_data, sdl->sdl_nlen); \ |
| 184 | pdest[sdl->sdl_nlen] = '\0'; \ |
paul | 6fe70d1 | 2005-11-12 22:55:10 +0000 | [diff] [blame] | 185 | (LEN) = sdl->sdl_nlen; \ |
| 186 | } \ |
| 187 | (PNT) += len; \ |
| 188 | } \ |
| 189 | else \ |
| 190 | { \ |
| 191 | (LEN) = 0; \ |
| 192 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 193 | /* Routing socket message types. */ |
Stephen Hemminger | 1423c80 | 2008-08-14 17:59:25 +0100 | [diff] [blame] | 194 | const struct message rtm_type_str[] = |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 195 | { |
| 196 | {RTM_ADD, "RTM_ADD"}, |
| 197 | {RTM_DELETE, "RTM_DELETE"}, |
| 198 | {RTM_CHANGE, "RTM_CHANGE"}, |
| 199 | {RTM_GET, "RTM_GET"}, |
| 200 | {RTM_LOSING, "RTM_LOSING"}, |
| 201 | {RTM_REDIRECT, "RTM_REDIRECT"}, |
| 202 | {RTM_MISS, "RTM_MISS"}, |
| 203 | {RTM_LOCK, "RTM_LOCK"}, |
Greg Troxel | 9458b81 | 2006-09-13 12:13:08 +0000 | [diff] [blame] | 204 | #ifdef OLDADD |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 205 | {RTM_OLDADD, "RTM_OLDADD"}, |
Greg Troxel | 9458b81 | 2006-09-13 12:13:08 +0000 | [diff] [blame] | 206 | #endif /* RTM_OLDADD */ |
| 207 | #ifdef RTM_OLDDEL |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 208 | {RTM_OLDDEL, "RTM_OLDDEL"}, |
Greg Troxel | 9458b81 | 2006-09-13 12:13:08 +0000 | [diff] [blame] | 209 | #endif /* RTM_OLDDEL */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 210 | {RTM_RESOLVE, "RTM_RESOLVE"}, |
| 211 | {RTM_NEWADDR, "RTM_NEWADDR"}, |
| 212 | {RTM_DELADDR, "RTM_DELADDR"}, |
| 213 | {RTM_IFINFO, "RTM_IFINFO"}, |
| 214 | #ifdef RTM_OIFINFO |
| 215 | {RTM_OIFINFO, "RTM_OIFINFO"}, |
| 216 | #endif /* RTM_OIFINFO */ |
| 217 | #ifdef RTM_NEWMADDR |
| 218 | {RTM_NEWMADDR, "RTM_NEWMADDR"}, |
| 219 | #endif /* RTM_NEWMADDR */ |
| 220 | #ifdef RTM_DELMADDR |
| 221 | {RTM_DELMADDR, "RTM_DELMADDR"}, |
| 222 | #endif /* RTM_DELMADDR */ |
| 223 | #ifdef RTM_IFANNOUNCE |
| 224 | {RTM_IFANNOUNCE, "RTM_IFANNOUNCE"}, |
| 225 | #endif /* RTM_IFANNOUNCE */ |
| 226 | {0, NULL} |
| 227 | }; |
| 228 | |
Stephen Hemminger | ce0db9c | 2009-05-15 10:47:04 -0700 | [diff] [blame] | 229 | static const struct message rtm_flag_str[] = |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 230 | { |
| 231 | {RTF_UP, "UP"}, |
| 232 | {RTF_GATEWAY, "GATEWAY"}, |
| 233 | {RTF_HOST, "HOST"}, |
| 234 | {RTF_REJECT, "REJECT"}, |
| 235 | {RTF_DYNAMIC, "DYNAMIC"}, |
| 236 | {RTF_MODIFIED, "MODIFIED"}, |
| 237 | {RTF_DONE, "DONE"}, |
| 238 | #ifdef RTF_MASK |
| 239 | {RTF_MASK, "MASK"}, |
| 240 | #endif /* RTF_MASK */ |
David Ward | e6f148e | 2009-12-03 21:43:11 +0300 | [diff] [blame] | 241 | #ifdef RTF_CLONING |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 242 | {RTF_CLONING, "CLONING"}, |
David Ward | e6f148e | 2009-12-03 21:43:11 +0300 | [diff] [blame] | 243 | #endif /* RTF_CLONING */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 244 | {RTF_XRESOLVE, "XRESOLVE"}, |
Lou Berger | b05c6ca | 2016-01-12 13:41:44 -0500 | [diff] [blame] | 245 | #ifdef RTF_LLINFO |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 246 | {RTF_LLINFO, "LLINFO"}, |
Lou Berger | b05c6ca | 2016-01-12 13:41:44 -0500 | [diff] [blame] | 247 | #endif /* RTF_LLINFO */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 248 | {RTF_STATIC, "STATIC"}, |
| 249 | {RTF_BLACKHOLE, "BLACKHOLE"}, |
paul | 6fe70d1 | 2005-11-12 22:55:10 +0000 | [diff] [blame] | 250 | #ifdef RTF_PRIVATE |
| 251 | {RTF_PRIVATE, "PRIVATE"}, |
| 252 | #endif /* RTF_PRIVATE */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 253 | {RTF_PROTO1, "PROTO1"}, |
| 254 | {RTF_PROTO2, "PROTO2"}, |
| 255 | #ifdef RTF_PRCLONING |
| 256 | {RTF_PRCLONING, "PRCLONING"}, |
| 257 | #endif /* RTF_PRCLONING */ |
| 258 | #ifdef RTF_WASCLONED |
| 259 | {RTF_WASCLONED, "WASCLONED"}, |
| 260 | #endif /* RTF_WASCLONED */ |
| 261 | #ifdef RTF_PROTO3 |
| 262 | {RTF_PROTO3, "PROTO3"}, |
| 263 | #endif /* RTF_PROTO3 */ |
| 264 | #ifdef RTF_PINNED |
| 265 | {RTF_PINNED, "PINNED"}, |
| 266 | #endif /* RTF_PINNED */ |
| 267 | #ifdef RTF_LOCAL |
| 268 | {RTF_LOCAL, "LOCAL"}, |
| 269 | #endif /* RTF_LOCAL */ |
| 270 | #ifdef RTF_BROADCAST |
| 271 | {RTF_BROADCAST, "BROADCAST"}, |
| 272 | #endif /* RTF_BROADCAST */ |
| 273 | #ifdef RTF_MULTICAST |
| 274 | {RTF_MULTICAST, "MULTICAST"}, |
| 275 | #endif /* RTF_MULTICAST */ |
paul | 6fe70d1 | 2005-11-12 22:55:10 +0000 | [diff] [blame] | 276 | #ifdef RTF_MULTIRT |
| 277 | {RTF_MULTIRT, "MULTIRT"}, |
| 278 | #endif /* RTF_MULTIRT */ |
| 279 | #ifdef RTF_SETSRC |
| 280 | {RTF_SETSRC, "SETSRC"}, |
| 281 | #endif /* RTF_SETSRC */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 282 | {0, NULL} |
| 283 | }; |
| 284 | |
| 285 | /* Kernel routing update socket. */ |
| 286 | int routing_sock = -1; |
| 287 | |
| 288 | /* Yes I'm checking ugly routing socket behavior. */ |
| 289 | /* #define DEBUG */ |
| 290 | |
| 291 | /* Supported address family check. */ |
David Lamparter | 3e9e2c9 | 2015-04-10 09:14:58 +0200 | [diff] [blame] | 292 | static inline int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 293 | af_check (int family) |
| 294 | { |
| 295 | if (family == AF_INET) |
| 296 | return 1; |
| 297 | #ifdef HAVE_IPV6 |
| 298 | if (family == AF_INET6) |
| 299 | return 1; |
| 300 | #endif /* HAVE_IPV6 */ |
| 301 | return 0; |
| 302 | } |
David Lamparter | 6b0655a | 2014-06-04 06:53:35 +0200 | [diff] [blame] | 303 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 304 | /* Dump routing table flag for debug purpose. */ |
ajs | b617800 | 2004-12-07 21:12:56 +0000 | [diff] [blame] | 305 | static void |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 306 | rtm_flag_dump (int flag) |
| 307 | { |
Tom Goff | 80b2a94 | 2009-12-03 14:53:15 +0300 | [diff] [blame] | 308 | const struct message *mes; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 309 | static char buf[BUFSIZ]; |
| 310 | |
gdt | cced60d | 2004-07-13 16:45:54 +0000 | [diff] [blame] | 311 | buf[0] = '\0'; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 312 | for (mes = rtm_flag_str; mes->key != 0; mes++) |
| 313 | { |
| 314 | if (mes->key & flag) |
| 315 | { |
| 316 | strlcat (buf, mes->str, BUFSIZ); |
| 317 | strlcat (buf, " ", BUFSIZ); |
| 318 | } |
| 319 | } |
ajs | b617800 | 2004-12-07 21:12:56 +0000 | [diff] [blame] | 320 | zlog_debug ("Kernel: %s", buf); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | #ifdef RTM_IFANNOUNCE |
| 324 | /* Interface adding function */ |
paul | 6621ca8 | 2005-11-23 13:02:08 +0000 | [diff] [blame] | 325 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 326 | ifan_read (struct if_announcemsghdr *ifan) |
| 327 | { |
| 328 | struct interface *ifp; |
paul | 6fe70d1 | 2005-11-12 22:55:10 +0000 | [diff] [blame] | 329 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 330 | ifp = if_lookup_by_index (ifan->ifan_index); |
paul | 6fe70d1 | 2005-11-12 22:55:10 +0000 | [diff] [blame] | 331 | |
| 332 | if (ifp) |
| 333 | assert ( (ifp->ifindex == ifan->ifan_index) |
| 334 | || (ifp->ifindex == IFINDEX_INTERNAL) ); |
| 335 | |
paul | ec1a428 | 2005-11-24 15:15:17 +0000 | [diff] [blame] | 336 | if ( (ifp == NULL) |
| 337 | || ((ifp->ifindex == IFINDEX_INTERNAL) |
| 338 | && (ifan->ifan_what == IFAN_ARRIVAL)) ) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 339 | { |
paul | 6fe70d1 | 2005-11-12 22:55:10 +0000 | [diff] [blame] | 340 | if (IS_ZEBRA_DEBUG_KERNEL) |
| 341 | zlog_debug ("%s: creating interface for ifindex %d, name %s", |
| 342 | __func__, ifan->ifan_index, ifan->ifan_name); |
| 343 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 344 | /* Create Interface */ |
ajs | 08dbfb6 | 2005-04-03 03:40:52 +0000 | [diff] [blame] | 345 | ifp = if_get_by_name_len(ifan->ifan_name, |
| 346 | strnlen(ifan->ifan_name, |
| 347 | sizeof(ifan->ifan_name))); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 348 | ifp->ifindex = ifan->ifan_index; |
| 349 | |
Ingo Flaschberger | 1db65fa | 2011-04-17 18:28:20 +0000 | [diff] [blame] | 350 | if_get_metric (ifp); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 351 | if_add_update (ifp); |
| 352 | } |
| 353 | else if (ifp != NULL && ifan->ifan_what == IFAN_DEPARTURE) |
paul | 6eb8827 | 2005-07-29 14:36:00 +0000 | [diff] [blame] | 354 | if_delete_update (ifp); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 355 | |
| 356 | if_get_flags (ifp); |
| 357 | if_get_mtu (ifp); |
| 358 | if_get_metric (ifp); |
| 359 | |
| 360 | if (IS_ZEBRA_DEBUG_KERNEL) |
paul | 6fe70d1 | 2005-11-12 22:55:10 +0000 | [diff] [blame] | 361 | zlog_debug ("%s: interface %s index %d", |
| 362 | __func__, ifan->ifan_name, ifan->ifan_index); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 363 | |
| 364 | return 0; |
| 365 | } |
| 366 | #endif /* RTM_IFANNOUNCE */ |
| 367 | |
Doug VanLeuven | 9234b38 | 2012-10-10 16:12:32 -0700 | [diff] [blame] | 368 | #ifdef HAVE_BSD_IFI_LINK_STATE |
Andrew J. Schorr | c543a17 | 2008-01-10 15:24:32 +0000 | [diff] [blame] | 369 | /* BSD link detect translation */ |
| 370 | static void |
| 371 | bsd_linkdetect_translate (struct if_msghdr *ifm) |
| 372 | { |
Andrew J. Schorr | 55edb0d | 2008-01-11 15:57:13 +0000 | [diff] [blame] | 373 | if ((ifm->ifm_data.ifi_link_state >= LINK_STATE_UP) || |
| 374 | (ifm->ifm_data.ifi_link_state == LINK_STATE_UNKNOWN)) |
Andrew J. Schorr | c543a17 | 2008-01-10 15:24:32 +0000 | [diff] [blame] | 375 | SET_FLAG(ifm->ifm_flags, IFF_RUNNING); |
| 376 | else |
| 377 | UNSET_FLAG(ifm->ifm_flags, IFF_RUNNING); |
| 378 | } |
Doug VanLeuven | 9234b38 | 2012-10-10 16:12:32 -0700 | [diff] [blame] | 379 | #endif /* HAVE_BSD_IFI_LINK_STATE */ |
Andrew J. Schorr | c543a17 | 2008-01-10 15:24:32 +0000 | [diff] [blame] | 380 | |
gdt | da26e3b | 2004-01-05 17:20:59 +0000 | [diff] [blame] | 381 | /* |
| 382 | * Handle struct if_msghdr obtained from reading routing socket or |
| 383 | * sysctl (from interface_list). There may or may not be sockaddrs |
| 384 | * present after the header. |
| 385 | */ |
paul | ec1a428 | 2005-11-24 15:15:17 +0000 | [diff] [blame] | 386 | int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 387 | ifm_read (struct if_msghdr *ifm) |
| 388 | { |
paul | 3e95a07 | 2003-09-24 00:05:45 +0000 | [diff] [blame] | 389 | struct interface *ifp = NULL; |
Tom Goff | a34eb36 | 2009-11-25 20:36:06 +0000 | [diff] [blame] | 390 | struct sockaddr_dl *sdl; |
paul | 6fe70d1 | 2005-11-12 22:55:10 +0000 | [diff] [blame] | 391 | char ifname[IFNAMSIZ]; |
| 392 | short ifnlen = 0; |
Doug VanLeuven | a05df8f | 2012-10-10 16:11:36 -0700 | [diff] [blame] | 393 | caddr_t cp; |
paul | 6fe70d1 | 2005-11-12 22:55:10 +0000 | [diff] [blame] | 394 | |
| 395 | /* terminate ifname at head (for strnlen) and tail (for safety) */ |
| 396 | ifname[IFNAMSIZ - 1] = '\0'; |
| 397 | |
gdt | da26e3b | 2004-01-05 17:20:59 +0000 | [diff] [blame] | 398 | /* paranoia: sanity check structure */ |
| 399 | if (ifm->ifm_msglen < sizeof(struct if_msghdr)) |
| 400 | { |
| 401 | zlog_err ("ifm_read: ifm->ifm_msglen %d too short\n", |
| 402 | ifm->ifm_msglen); |
| 403 | return -1; |
| 404 | } |
| 405 | |
| 406 | /* |
gdt | 4bfbea8 | 2004-01-06 01:13:05 +0000 | [diff] [blame] | 407 | * Check for a sockaddr_dl following the message. First, point to |
| 408 | * where a socakddr might be if one follows the message. |
gdt | da26e3b | 2004-01-05 17:20:59 +0000 | [diff] [blame] | 409 | */ |
gdt | 4bfbea8 | 2004-01-06 01:13:05 +0000 | [diff] [blame] | 410 | cp = (void *)(ifm + 1); |
| 411 | |
paul | 3e95a07 | 2003-09-24 00:05:45 +0000 | [diff] [blame] | 412 | #ifdef SUNOS_5 |
paul | 3e95a07 | 2003-09-24 00:05:45 +0000 | [diff] [blame] | 413 | /* |
gdt | 4bfbea8 | 2004-01-06 01:13:05 +0000 | [diff] [blame] | 414 | * XXX This behavior should be narrowed to only the kernel versions |
| 415 | * for which the structures returned do not match the headers. |
| 416 | * |
paul | 3e95a07 | 2003-09-24 00:05:45 +0000 | [diff] [blame] | 417 | * if_msghdr_t on 64 bit kernels in Solaris 9 and earlier versions |
gdt | 4bfbea8 | 2004-01-06 01:13:05 +0000 | [diff] [blame] | 418 | * is 12 bytes larger than the 32 bit version. |
paul | 3e95a07 | 2003-09-24 00:05:45 +0000 | [diff] [blame] | 419 | */ |
gdt | 4bfbea8 | 2004-01-06 01:13:05 +0000 | [diff] [blame] | 420 | if (((struct sockaddr *) cp)->sa_family == AF_UNSPEC) |
paul | 3e95a07 | 2003-09-24 00:05:45 +0000 | [diff] [blame] | 421 | cp = cp + 12; |
paul | 3e95a07 | 2003-09-24 00:05:45 +0000 | [diff] [blame] | 422 | #endif |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 423 | |
paul | 6fe70d1 | 2005-11-12 22:55:10 +0000 | [diff] [blame] | 424 | RTA_ADDR_GET (NULL, RTA_DST, ifm->ifm_addrs, cp); |
| 425 | RTA_ADDR_GET (NULL, RTA_GATEWAY, ifm->ifm_addrs, cp); |
| 426 | RTA_ATTR_GET (NULL, RTA_NETMASK, ifm->ifm_addrs, cp); |
| 427 | RTA_ADDR_GET (NULL, RTA_GENMASK, ifm->ifm_addrs, cp); |
Tom Goff | a34eb36 | 2009-11-25 20:36:06 +0000 | [diff] [blame] | 428 | sdl = (struct sockaddr_dl *)cp; |
paul | 6fe70d1 | 2005-11-12 22:55:10 +0000 | [diff] [blame] | 429 | RTA_NAME_GET (ifname, RTA_IFP, ifm->ifm_addrs, cp, ifnlen); |
| 430 | RTA_ADDR_GET (NULL, RTA_IFA, ifm->ifm_addrs, cp); |
| 431 | RTA_ADDR_GET (NULL, RTA_AUTHOR, ifm->ifm_addrs, cp); |
| 432 | RTA_ADDR_GET (NULL, RTA_BRD, ifm->ifm_addrs, cp); |
| 433 | |
| 434 | if (IS_ZEBRA_DEBUG_KERNEL) |
| 435 | zlog_debug ("%s: sdl ifname %s", __func__, (ifnlen ? ifname : "(nil)")); |
| 436 | |
gdt | 4bfbea8 | 2004-01-06 01:13:05 +0000 | [diff] [blame] | 437 | /* |
paul | 6fe70d1 | 2005-11-12 22:55:10 +0000 | [diff] [blame] | 438 | * Look up on ifindex first, because ifindices are the primary handle for |
| 439 | * interfaces across the user/kernel boundary, for most systems. (Some |
| 440 | * messages, such as up/down status changes on NetBSD, do not include a |
| 441 | * sockaddr_dl). |
gdt | 4bfbea8 | 2004-01-06 01:13:05 +0000 | [diff] [blame] | 442 | */ |
paul | 6fe70d1 | 2005-11-12 22:55:10 +0000 | [diff] [blame] | 443 | if ( (ifp = if_lookup_by_index (ifm->ifm_index)) != NULL ) |
gdt | 4bfbea8 | 2004-01-06 01:13:05 +0000 | [diff] [blame] | 444 | { |
paul | 6fe70d1 | 2005-11-12 22:55:10 +0000 | [diff] [blame] | 445 | /* we have an ifp, verify that the name matches as some systems, |
| 446 | * eg Solaris, have a 1:many association of ifindex:ifname |
| 447 | * if they dont match, we dont have the correct ifp and should |
| 448 | * set it back to NULL to let next check do lookup by name |
| 449 | */ |
| 450 | if (ifnlen && (strncmp (ifp->name, ifname, IFNAMSIZ) != 0) ) |
gdt | 4bfbea8 | 2004-01-06 01:13:05 +0000 | [diff] [blame] | 451 | { |
paul | 6fe70d1 | 2005-11-12 22:55:10 +0000 | [diff] [blame] | 452 | if (IS_ZEBRA_DEBUG_KERNEL) |
| 453 | zlog_debug ("%s: ifp name %s doesnt match sdl name %s", |
| 454 | __func__, ifp->name, ifname); |
| 455 | ifp = NULL; |
gdt | 4bfbea8 | 2004-01-06 01:13:05 +0000 | [diff] [blame] | 456 | } |
| 457 | } |
paul | 6fe70d1 | 2005-11-12 22:55:10 +0000 | [diff] [blame] | 458 | |
gdt | da26e3b | 2004-01-05 17:20:59 +0000 | [diff] [blame] | 459 | /* |
paul | 6fe70d1 | 2005-11-12 22:55:10 +0000 | [diff] [blame] | 460 | * If we dont have an ifp, try looking up by name. Particularly as some |
| 461 | * systems (Solaris) have a 1:many mapping of ifindex:ifname - the ifname |
| 462 | * is therefore our unique handle to that interface. |
| 463 | * |
| 464 | * Interfaces specified in the configuration file for which the ifindex |
| 465 | * has not been determined will have ifindex == IFINDEX_INTERNAL, and such |
| 466 | * interfaces are found by this search, and then their ifindex values can |
| 467 | * be filled in. |
gdt | da26e3b | 2004-01-05 17:20:59 +0000 | [diff] [blame] | 468 | */ |
paul | 6fe70d1 | 2005-11-12 22:55:10 +0000 | [diff] [blame] | 469 | if ( (ifp == NULL) && ifnlen) |
| 470 | ifp = if_lookup_by_name (ifname); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 471 | |
gdt | da26e3b | 2004-01-05 17:20:59 +0000 | [diff] [blame] | 472 | /* |
paul | 6fe70d1 | 2005-11-12 22:55:10 +0000 | [diff] [blame] | 473 | * If ifp still does not exist or has an invalid index (IFINDEX_INTERNAL), |
| 474 | * create or fill in an interface. |
gdt | da26e3b | 2004-01-05 17:20:59 +0000 | [diff] [blame] | 475 | */ |
ajs | d2fc889 | 2005-04-02 18:38:43 +0000 | [diff] [blame] | 476 | if ((ifp == NULL) || (ifp->ifindex == IFINDEX_INTERNAL)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 477 | { |
gdt | 4bfbea8 | 2004-01-06 01:13:05 +0000 | [diff] [blame] | 478 | /* |
| 479 | * To create or fill in an interface, a sockaddr_dl (via |
| 480 | * RTA_IFP) is required. |
| 481 | */ |
paul | 6fe70d1 | 2005-11-12 22:55:10 +0000 | [diff] [blame] | 482 | if (!ifnlen) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 483 | { |
paul | 6fe70d1 | 2005-11-12 22:55:10 +0000 | [diff] [blame] | 484 | zlog_warn ("Interface index %d (new) missing ifname\n", |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 485 | ifm->ifm_index); |
| 486 | return -1; |
| 487 | } |
paul | 5c78b3d | 2006-01-25 04:31:40 +0000 | [diff] [blame] | 488 | |
| 489 | #ifndef RTM_IFANNOUNCE |
| 490 | /* Down->Down interface should be ignored here. |
| 491 | * See further comment below. |
| 492 | */ |
| 493 | if (!CHECK_FLAG (ifm->ifm_flags, IFF_UP)) |
| 494 | return 0; |
| 495 | #endif /* !RTM_IFANNOUNCE */ |
paul | 6fe70d1 | 2005-11-12 22:55:10 +0000 | [diff] [blame] | 496 | |
paul | 3e95a07 | 2003-09-24 00:05:45 +0000 | [diff] [blame] | 497 | if (ifp == NULL) |
paul | 6fe70d1 | 2005-11-12 22:55:10 +0000 | [diff] [blame] | 498 | { |
| 499 | /* Interface that zebra was not previously aware of, so create. */ |
| 500 | ifp = if_create (ifname, ifnlen); |
| 501 | if (IS_ZEBRA_DEBUG_KERNEL) |
| 502 | zlog_debug ("%s: creating ifp for ifindex %d", |
| 503 | __func__, ifm->ifm_index); |
| 504 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 505 | |
paul | 6fe70d1 | 2005-11-12 22:55:10 +0000 | [diff] [blame] | 506 | if (IS_ZEBRA_DEBUG_KERNEL) |
| 507 | zlog_debug ("%s: updated/created ifp, ifname %s, ifindex %d", |
| 508 | __func__, ifp->name, ifp->ifindex); |
gdt | 4bfbea8 | 2004-01-06 01:13:05 +0000 | [diff] [blame] | 509 | /* |
| 510 | * Fill in newly created interface structure, or larval |
ajs | d2fc889 | 2005-04-02 18:38:43 +0000 | [diff] [blame] | 511 | * structure with ifindex IFINDEX_INTERNAL. |
gdt | 4bfbea8 | 2004-01-06 01:13:05 +0000 | [diff] [blame] | 512 | */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 513 | ifp->ifindex = ifm->ifm_index; |
Andrew J. Schorr | c543a17 | 2008-01-10 15:24:32 +0000 | [diff] [blame] | 514 | |
Doug VanLeuven | 9234b38 | 2012-10-10 16:12:32 -0700 | [diff] [blame] | 515 | #ifdef HAVE_BSD_IFI_LINK_STATE /* translate BSD kernel msg for link-state */ |
Andrew J. Schorr | c543a17 | 2008-01-10 15:24:32 +0000 | [diff] [blame] | 516 | bsd_linkdetect_translate(ifm); |
Doug VanLeuven | 9234b38 | 2012-10-10 16:12:32 -0700 | [diff] [blame] | 517 | #endif /* HAVE_BSD_IFI_LINK_STATE */ |
Andrew J. Schorr | c543a17 | 2008-01-10 15:24:32 +0000 | [diff] [blame] | 518 | |
paul | 5c78b3d | 2006-01-25 04:31:40 +0000 | [diff] [blame] | 519 | if_flags_update (ifp, ifm->ifm_flags); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 520 | #if defined(__bsdi__) |
| 521 | if_kvm_get_mtu (ifp); |
| 522 | #else |
| 523 | if_get_mtu (ifp); |
| 524 | #endif /* __bsdi__ */ |
| 525 | if_get_metric (ifp); |
| 526 | |
Tom Goff | a34eb36 | 2009-11-25 20:36:06 +0000 | [diff] [blame] | 527 | /* |
| 528 | * XXX sockaddr_dl contents can be larger than the structure |
David Lamparter | ca3ccd8 | 2012-09-26 14:52:39 +0200 | [diff] [blame] | 529 | * definition. There are 2 big families here: |
| 530 | * - BSD has sdl_len + sdl_data[16] + overruns sdl_data |
| 531 | * we MUST use sdl_len here or we'll truncate data. |
| 532 | * - Solaris has no sdl_len, but sdl_data[244] |
| 533 | * presumably, it's not going to run past that, so sizeof() |
| 534 | * is fine here. |
Tom Goff | a34eb36 | 2009-11-25 20:36:06 +0000 | [diff] [blame] | 535 | * a nonzero ifnlen from RTA_NAME_GET() means sdl is valid |
| 536 | */ |
| 537 | if (ifnlen) |
David Lamparter | ca3ccd8 | 2012-09-26 14:52:39 +0200 | [diff] [blame] | 538 | { |
| 539 | #ifdef HAVE_STRUCT_SOCKADDR_DL_SDL_LEN |
| 540 | memcpy (&ifp->sdl, sdl, sdl->sdl_len); |
| 541 | #else |
Tom Goff | a34eb36 | 2009-11-25 20:36:06 +0000 | [diff] [blame] | 542 | memcpy (&ifp->sdl, sdl, sizeof (struct sockaddr_dl)); |
David Lamparter | ca3ccd8 | 2012-09-26 14:52:39 +0200 | [diff] [blame] | 543 | #endif /* HAVE_STRUCT_SOCKADDR_DL_SDL_LEN */ |
| 544 | } |
Tom Goff | a34eb36 | 2009-11-25 20:36:06 +0000 | [diff] [blame] | 545 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 546 | if_add_update (ifp); |
| 547 | } |
| 548 | else |
gdt | da26e3b | 2004-01-05 17:20:59 +0000 | [diff] [blame] | 549 | /* |
| 550 | * Interface structure exists. Adjust stored flags from |
| 551 | * notification. If interface has up->down or down->up |
| 552 | * transition, call state change routines (to adjust routes, |
| 553 | * notify routing daemons, etc.). (Other flag changes are stored |
| 554 | * but apparently do not trigger action.) |
| 555 | */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 556 | { |
paul | 6fe70d1 | 2005-11-12 22:55:10 +0000 | [diff] [blame] | 557 | if (ifp->ifindex != ifm->ifm_index) |
| 558 | { |
| 559 | zlog_warn ("%s: index mismatch, ifname %s, ifp index %d, " |
| 560 | "ifm index %d", |
| 561 | __func__, ifp->name, ifp->ifindex, ifm->ifm_index); |
| 562 | return -1; |
| 563 | } |
| 564 | |
Doug VanLeuven | 9234b38 | 2012-10-10 16:12:32 -0700 | [diff] [blame] | 565 | #ifdef HAVE_BSD_IFI_LINK_STATE /* translate BSD kernel msg for link-state */ |
Andrew J. Schorr | c543a17 | 2008-01-10 15:24:32 +0000 | [diff] [blame] | 566 | bsd_linkdetect_translate(ifm); |
Doug VanLeuven | 9234b38 | 2012-10-10 16:12:32 -0700 | [diff] [blame] | 567 | #endif /* HAVE_BSD_IFI_LINK_STATE */ |
Andrew J. Schorr | c543a17 | 2008-01-10 15:24:32 +0000 | [diff] [blame] | 568 | |
paul | 5c78b3d | 2006-01-25 04:31:40 +0000 | [diff] [blame] | 569 | /* update flags and handle operative->inoperative transition, if any */ |
| 570 | if_flags_update (ifp, ifm->ifm_flags); |
| 571 | |
paul | 6eb8827 | 2005-07-29 14:36:00 +0000 | [diff] [blame] | 572 | #ifndef RTM_IFANNOUNCE |
paul | 5c78b3d | 2006-01-25 04:31:40 +0000 | [diff] [blame] | 573 | if (!if_is_up (ifp)) |
| 574 | { |
| 575 | /* No RTM_IFANNOUNCE on this platform, so we can never |
| 576 | * distinguish between ~IFF_UP and delete. We must presume |
| 577 | * it has been deleted. |
| 578 | * Eg, Solaris will not notify us of unplumb. |
| 579 | * |
| 580 | * XXX: Fixme - this should be runtime detected |
| 581 | * So that a binary compiled on a system with IFANNOUNCE |
| 582 | * will still behave correctly if run on a platform without |
| 583 | */ |
| 584 | if_delete_update (ifp); |
| 585 | } |
paul | 6eb8827 | 2005-07-29 14:36:00 +0000 | [diff] [blame] | 586 | #endif /* RTM_IFANNOUNCE */ |
Denis Ovsienko | 1ba2756 | 2007-08-21 16:15:39 +0000 | [diff] [blame] | 587 | if (if_is_up (ifp)) |
| 588 | { |
| 589 | #if defined(__bsdi__) |
| 590 | if_kvm_get_mtu (ifp); |
| 591 | #else |
| 592 | if_get_mtu (ifp); |
| 593 | #endif /* __bsdi__ */ |
| 594 | if_get_metric (ifp); |
| 595 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 596 | } |
paul | 5c78b3d | 2006-01-25 04:31:40 +0000 | [diff] [blame] | 597 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 598 | #ifdef HAVE_NET_RT_IFLIST |
| 599 | ifp->stats = ifm->ifm_data; |
| 600 | #endif /* HAVE_NET_RT_IFLIST */ |
| 601 | |
| 602 | if (IS_ZEBRA_DEBUG_KERNEL) |
paul | 6fe70d1 | 2005-11-12 22:55:10 +0000 | [diff] [blame] | 603 | zlog_debug ("%s: interface %s index %d", |
| 604 | __func__, ifp->name, ifp->ifindex); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 605 | |
| 606 | return 0; |
| 607 | } |
David Lamparter | 6b0655a | 2014-06-04 06:53:35 +0200 | [diff] [blame] | 608 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 609 | /* Address read from struct ifa_msghdr. */ |
paul | 6621ca8 | 2005-11-23 13:02:08 +0000 | [diff] [blame] | 610 | static void |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 611 | ifam_read_mesg (struct ifa_msghdr *ifm, |
| 612 | union sockunion *addr, |
| 613 | union sockunion *mask, |
paul | 6fe70d1 | 2005-11-12 22:55:10 +0000 | [diff] [blame] | 614 | union sockunion *brd, |
| 615 | char *ifname, |
| 616 | short *ifnlen) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 617 | { |
| 618 | caddr_t pnt, end; |
Andrew J. Schorr | 7ab62c5 | 2007-05-17 15:00:41 +0000 | [diff] [blame] | 619 | union sockunion dst; |
| 620 | union sockunion gateway; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 621 | |
| 622 | pnt = (caddr_t)(ifm + 1); |
| 623 | end = ((caddr_t)ifm) + ifm->ifam_msglen; |
| 624 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 625 | /* Be sure structure is cleared */ |
| 626 | memset (mask, 0, sizeof (union sockunion)); |
| 627 | memset (addr, 0, sizeof (union sockunion)); |
paul | 6621ca8 | 2005-11-23 13:02:08 +0000 | [diff] [blame] | 628 | memset (brd, 0, sizeof (union sockunion)); |
Andrew J. Schorr | 7ab62c5 | 2007-05-17 15:00:41 +0000 | [diff] [blame] | 629 | memset (&dst, 0, sizeof (union sockunion)); |
| 630 | memset (&gateway, 0, sizeof (union sockunion)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 631 | |
| 632 | /* We fetch each socket variable into sockunion. */ |
Andrew J. Schorr | 7ab62c5 | 2007-05-17 15:00:41 +0000 | [diff] [blame] | 633 | RTA_ADDR_GET (&dst, RTA_DST, ifm->ifam_addrs, pnt); |
| 634 | RTA_ADDR_GET (&gateway, RTA_GATEWAY, ifm->ifam_addrs, pnt); |
paul | 62debbb | 2005-06-14 14:07:07 +0000 | [diff] [blame] | 635 | RTA_ATTR_GET (mask, RTA_NETMASK, ifm->ifam_addrs, pnt); |
| 636 | RTA_ADDR_GET (NULL, RTA_GENMASK, ifm->ifam_addrs, pnt); |
paul | 6fe70d1 | 2005-11-12 22:55:10 +0000 | [diff] [blame] | 637 | RTA_NAME_GET (ifname, RTA_IFP, ifm->ifam_addrs, pnt, *ifnlen); |
paul | 62debbb | 2005-06-14 14:07:07 +0000 | [diff] [blame] | 638 | RTA_ADDR_GET (addr, RTA_IFA, ifm->ifam_addrs, pnt); |
| 639 | RTA_ADDR_GET (NULL, RTA_AUTHOR, ifm->ifam_addrs, pnt); |
paul | 6fe70d1 | 2005-11-12 22:55:10 +0000 | [diff] [blame] | 640 | RTA_ADDR_GET (brd, RTA_BRD, ifm->ifam_addrs, pnt); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 641 | |
paul | 6fe70d1 | 2005-11-12 22:55:10 +0000 | [diff] [blame] | 642 | if (IS_ZEBRA_DEBUG_KERNEL) |
Andrew J. Schorr | 5519604 | 2006-05-17 15:04:59 +0000 | [diff] [blame] | 643 | { |
Timo Teräs | be6335d | 2015-05-23 11:08:41 +0300 | [diff] [blame] | 644 | int family = sockunion_family(addr); |
| 645 | switch (family) |
Andrew J. Schorr | 5519604 | 2006-05-17 15:04:59 +0000 | [diff] [blame] | 646 | { |
| 647 | case AF_INET: |
Timo Teräs | be6335d | 2015-05-23 11:08:41 +0300 | [diff] [blame] | 648 | #ifdef HAVE_IPV6 |
| 649 | case AF_INET6: |
| 650 | #endif |
Andrew J. Schorr | 5519604 | 2006-05-17 15:04:59 +0000 | [diff] [blame] | 651 | { |
Timo Teräs | be6335d | 2015-05-23 11:08:41 +0300 | [diff] [blame] | 652 | char buf[4][INET6_ADDRSTRLEN]; |
Andrew J. Schorr | 5519604 | 2006-05-17 15:04:59 +0000 | [diff] [blame] | 653 | zlog_debug ("%s: ifindex %d, ifname %s, ifam_addrs 0x%x, " |
Andrew J. Schorr | 7ab62c5 | 2007-05-17 15:00:41 +0000 | [diff] [blame] | 654 | "ifam_flags 0x%x, addr %s/%d broad %s dst %s " |
| 655 | "gateway %s", |
| 656 | __func__, ifm->ifam_index, |
Andrew J. Schorr | 5519604 | 2006-05-17 15:04:59 +0000 | [diff] [blame] | 657 | (ifnlen ? ifname : "(nil)"), ifm->ifam_addrs, |
Andrew J. Schorr | 7ab62c5 | 2007-05-17 15:00:41 +0000 | [diff] [blame] | 658 | ifm->ifam_flags, |
Timo Teräs | be6335d | 2015-05-23 11:08:41 +0300 | [diff] [blame] | 659 | inet_ntop(family,&addr->sin.sin_addr, |
Andrew J. Schorr | 5519604 | 2006-05-17 15:04:59 +0000 | [diff] [blame] | 660 | buf[0],sizeof(buf[0])), |
| 661 | ip_masklen(mask->sin.sin_addr), |
Timo Teräs | be6335d | 2015-05-23 11:08:41 +0300 | [diff] [blame] | 662 | inet_ntop(family,&brd->sin.sin_addr, |
Andrew J. Schorr | 7ab62c5 | 2007-05-17 15:00:41 +0000 | [diff] [blame] | 663 | buf[1],sizeof(buf[1])), |
Timo Teräs | be6335d | 2015-05-23 11:08:41 +0300 | [diff] [blame] | 664 | inet_ntop(family,&dst.sin.sin_addr, |
Andrew J. Schorr | 7ab62c5 | 2007-05-17 15:00:41 +0000 | [diff] [blame] | 665 | buf[2],sizeof(buf[2])), |
Timo Teräs | be6335d | 2015-05-23 11:08:41 +0300 | [diff] [blame] | 666 | inet_ntop(family,&gateway.sin.sin_addr, |
Andrew J. Schorr | 7ab62c5 | 2007-05-17 15:00:41 +0000 | [diff] [blame] | 667 | buf[3],sizeof(buf[3]))); |
Andrew J. Schorr | 5519604 | 2006-05-17 15:04:59 +0000 | [diff] [blame] | 668 | } |
| 669 | break; |
Andrew J. Schorr | 5519604 | 2006-05-17 15:04:59 +0000 | [diff] [blame] | 670 | default: |
| 671 | zlog_debug ("%s: ifindex %d, ifname %s, ifam_addrs 0x%x", |
| 672 | __func__, ifm->ifam_index, |
| 673 | (ifnlen ? ifname : "(nil)"), ifm->ifam_addrs); |
| 674 | break; |
| 675 | } |
| 676 | } |
Andrew J. Schorr | 7ab62c5 | 2007-05-17 15:00:41 +0000 | [diff] [blame] | 677 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 678 | /* Assert read up end point matches to end point */ |
| 679 | if (pnt != end) |
Denis Ovsienko | 85a2ebf | 2011-12-05 19:36:06 +0400 | [diff] [blame] | 680 | zlog_warn ("ifam_read() doesn't read all socket data"); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 681 | } |
| 682 | |
| 683 | /* Interface's address information get. */ |
paul | ec1a428 | 2005-11-24 15:15:17 +0000 | [diff] [blame] | 684 | int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 685 | ifam_read (struct ifa_msghdr *ifam) |
| 686 | { |
paul | 6fe70d1 | 2005-11-12 22:55:10 +0000 | [diff] [blame] | 687 | struct interface *ifp = NULL; |
paul | 0752ef0 | 2005-11-03 12:35:21 +0000 | [diff] [blame] | 688 | union sockunion addr, mask, brd; |
paul | 6fe70d1 | 2005-11-12 22:55:10 +0000 | [diff] [blame] | 689 | char ifname[INTERFACE_NAMSIZ]; |
| 690 | short ifnlen = 0; |
| 691 | char isalias = 0; |
Andrew J. Schorr | 7ab62c5 | 2007-05-17 15:00:41 +0000 | [diff] [blame] | 692 | int flags = 0; |
paul | 6fe70d1 | 2005-11-12 22:55:10 +0000 | [diff] [blame] | 693 | |
| 694 | ifname[0] = ifname[INTERFACE_NAMSIZ - 1] = '\0'; |
| 695 | |
| 696 | /* Allocate and read address information. */ |
| 697 | ifam_read_mesg (ifam, &addr, &mask, &brd, ifname, &ifnlen); |
| 698 | |
| 699 | if ((ifp = if_lookup_by_index(ifam->ifam_index)) == NULL) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 700 | { |
paul | 6fe70d1 | 2005-11-12 22:55:10 +0000 | [diff] [blame] | 701 | zlog_warn ("%s: no interface for ifname %s, index %d", |
| 702 | __func__, ifname, ifam->ifam_index); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 703 | return -1; |
| 704 | } |
paul | 6fe70d1 | 2005-11-12 22:55:10 +0000 | [diff] [blame] | 705 | |
| 706 | if (ifnlen && strncmp (ifp->name, ifname, INTERFACE_NAMSIZ)) |
| 707 | isalias = 1; |
| 708 | |
Andrew J. Schorr | 7ab62c5 | 2007-05-17 15:00:41 +0000 | [diff] [blame] | 709 | /* N.B. The info in ifa_msghdr does not tell us whether the RTA_BRD |
| 710 | field contains a broadcast address or a peer address, so we are forced to |
| 711 | rely upon the interface type. */ |
| 712 | if (if_is_pointopoint(ifp)) |
| 713 | SET_FLAG(flags, ZEBRA_IFA_PEER); |
| 714 | |
Paul Jakma | 6502208 | 2007-03-06 13:43:05 +0000 | [diff] [blame] | 715 | #if 0 |
| 716 | /* it might seem cute to grab the interface metric here, however |
| 717 | * we're processing an address update message, and so some systems |
| 718 | * (e.g. FBSD) dont bother to fill in ifam_metric. Disabled, but left |
| 719 | * in deliberately, as comment. |
| 720 | */ |
paul | d34b899 | 2006-01-17 18:03:04 +0000 | [diff] [blame] | 721 | ifp->metric = ifam->ifam_metric; |
Paul Jakma | 6502208 | 2007-03-06 13:43:05 +0000 | [diff] [blame] | 722 | #endif |
| 723 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 724 | /* Add connected address. */ |
| 725 | switch (sockunion_family (&addr)) |
| 726 | { |
| 727 | case AF_INET: |
| 728 | if (ifam->ifam_type == RTM_NEWADDR) |
Andrew J. Schorr | 7ab62c5 | 2007-05-17 15:00:41 +0000 | [diff] [blame] | 729 | connected_add_ipv4 (ifp, flags, &addr.sin.sin_addr, |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 730 | ip_masklen (mask.sin.sin_addr), |
paul | d34b899 | 2006-01-17 18:03:04 +0000 | [diff] [blame] | 731 | &brd.sin.sin_addr, |
| 732 | (isalias ? ifname : NULL)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 733 | else |
Andrew J. Schorr | 7ab62c5 | 2007-05-17 15:00:41 +0000 | [diff] [blame] | 734 | connected_delete_ipv4 (ifp, flags, &addr.sin.sin_addr, |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 735 | ip_masklen (mask.sin.sin_addr), |
paul | 0752ef0 | 2005-11-03 12:35:21 +0000 | [diff] [blame] | 736 | &brd.sin.sin_addr); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 737 | break; |
| 738 | #ifdef HAVE_IPV6 |
| 739 | case AF_INET6: |
| 740 | /* Unset interface index from link-local address when IPv6 stack |
| 741 | is KAME. */ |
| 742 | if (IN6_IS_ADDR_LINKLOCAL (&addr.sin6.sin6_addr)) |
David Lamparter | da1b7ea | 2015-09-15 20:36:20 -0700 | [diff] [blame] | 743 | { |
| 744 | SET_IN6_LINKLOCAL_IFINDEX (addr.sin6.sin6_addr, 0); |
| 745 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 746 | |
| 747 | if (ifam->ifam_type == RTM_NEWADDR) |
Andrew J. Schorr | 7ab62c5 | 2007-05-17 15:00:41 +0000 | [diff] [blame] | 748 | connected_add_ipv6 (ifp, flags, &addr.sin6.sin6_addr, |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 749 | ip6_masklen (mask.sin6.sin6_addr), |
paul | d34b899 | 2006-01-17 18:03:04 +0000 | [diff] [blame] | 750 | &brd.sin6.sin6_addr, |
| 751 | (isalias ? ifname : NULL)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 752 | else |
| 753 | connected_delete_ipv6 (ifp, |
| 754 | &addr.sin6.sin6_addr, |
| 755 | ip6_masklen (mask.sin6.sin6_addr), |
paul | 0752ef0 | 2005-11-03 12:35:21 +0000 | [diff] [blame] | 756 | &brd.sin6.sin6_addr); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 757 | break; |
| 758 | #endif /* HAVE_IPV6 */ |
| 759 | default: |
| 760 | /* Unsupported family silently ignore... */ |
| 761 | break; |
| 762 | } |
paul | 5c78b3d | 2006-01-25 04:31:40 +0000 | [diff] [blame] | 763 | |
| 764 | /* Check interface flag for implicit up of the interface. */ |
| 765 | if_refresh (ifp); |
| 766 | |
| 767 | #ifdef SUNOS_5 |
| 768 | /* In addition to lacking IFANNOUNCE, on SUNOS IFF_UP is strange. |
| 769 | * See comments for SUNOS_5 in interface.c::if_flags_mangle. |
| 770 | * |
| 771 | * Here we take care of case where the real IFF_UP was previously |
| 772 | * unset (as kept in struct zebra_if.primary_state) and the mangled |
| 773 | * IFF_UP (ie IFF_UP set || listcount(connected) has now transitioned |
| 774 | * to unset due to the lost non-primary address having DELADDR'd. |
| 775 | * |
| 776 | * we must delete the interface, because in between here and next |
| 777 | * event for this interface-name the administrator could unplumb |
| 778 | * and replumb the interface. |
| 779 | */ |
| 780 | if (!if_is_up (ifp)) |
| 781 | if_delete_update (ifp); |
| 782 | #endif /* SUNOS_5 */ |
| 783 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 784 | return 0; |
| 785 | } |
David Lamparter | 6b0655a | 2014-06-04 06:53:35 +0200 | [diff] [blame] | 786 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 787 | /* Interface function for reading kernel routing table information. */ |
paul | 6621ca8 | 2005-11-23 13:02:08 +0000 | [diff] [blame] | 788 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 789 | rtm_read_mesg (struct rt_msghdr *rtm, |
| 790 | union sockunion *dest, |
| 791 | union sockunion *mask, |
paul | 6fe70d1 | 2005-11-12 22:55:10 +0000 | [diff] [blame] | 792 | union sockunion *gate, |
| 793 | char *ifname, |
| 794 | short *ifnlen) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 795 | { |
| 796 | caddr_t pnt, end; |
| 797 | |
| 798 | /* Pnt points out socket data start point. */ |
| 799 | pnt = (caddr_t)(rtm + 1); |
| 800 | end = ((caddr_t)rtm) + rtm->rtm_msglen; |
| 801 | |
| 802 | /* rt_msghdr version check. */ |
| 803 | if (rtm->rtm_version != RTM_VERSION) |
| 804 | zlog (NULL, LOG_WARNING, |
| 805 | "Routing message version different %d should be %d." |
| 806 | "This may cause problem\n", rtm->rtm_version, RTM_VERSION); |
paul | 62debbb | 2005-06-14 14:07:07 +0000 | [diff] [blame] | 807 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 808 | /* Be sure structure is cleared */ |
| 809 | memset (dest, 0, sizeof (union sockunion)); |
| 810 | memset (gate, 0, sizeof (union sockunion)); |
| 811 | memset (mask, 0, sizeof (union sockunion)); |
| 812 | |
| 813 | /* We fetch each socket variable into sockunion. */ |
paul | 62debbb | 2005-06-14 14:07:07 +0000 | [diff] [blame] | 814 | RTA_ADDR_GET (dest, RTA_DST, rtm->rtm_addrs, pnt); |
| 815 | RTA_ADDR_GET (gate, RTA_GATEWAY, rtm->rtm_addrs, pnt); |
| 816 | RTA_ATTR_GET (mask, RTA_NETMASK, rtm->rtm_addrs, pnt); |
| 817 | RTA_ADDR_GET (NULL, RTA_GENMASK, rtm->rtm_addrs, pnt); |
paul | 6fe70d1 | 2005-11-12 22:55:10 +0000 | [diff] [blame] | 818 | RTA_NAME_GET (ifname, RTA_IFP, rtm->rtm_addrs, pnt, *ifnlen); |
paul | 62debbb | 2005-06-14 14:07:07 +0000 | [diff] [blame] | 819 | RTA_ADDR_GET (NULL, RTA_IFA, rtm->rtm_addrs, pnt); |
| 820 | RTA_ADDR_GET (NULL, RTA_AUTHOR, rtm->rtm_addrs, pnt); |
| 821 | RTA_ADDR_GET (NULL, RTA_BRD, rtm->rtm_addrs, pnt); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 822 | |
| 823 | /* If there is netmask information set it's family same as |
| 824 | destination family*/ |
| 825 | if (rtm->rtm_addrs & RTA_NETMASK) |
| 826 | mask->sa.sa_family = dest->sa.sa_family; |
| 827 | |
| 828 | /* Assert read up to the end of pointer. */ |
| 829 | if (pnt != end) |
Denis Ovsienko | 85a2ebf | 2011-12-05 19:36:06 +0400 | [diff] [blame] | 830 | zlog (NULL, LOG_WARNING, "rtm_read() doesn't read all socket data."); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 831 | |
| 832 | return rtm->rtm_flags; |
| 833 | } |
| 834 | |
paul | ec1a428 | 2005-11-24 15:15:17 +0000 | [diff] [blame] | 835 | void |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 836 | rtm_read (struct rt_msghdr *rtm) |
| 837 | { |
| 838 | int flags; |
| 839 | u_char zebra_flags; |
| 840 | union sockunion dest, mask, gate; |
paul | 6fe70d1 | 2005-11-12 22:55:10 +0000 | [diff] [blame] | 841 | char ifname[INTERFACE_NAMSIZ + 1]; |
| 842 | short ifnlen = 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 843 | |
| 844 | zebra_flags = 0; |
| 845 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 846 | /* Read destination and netmask and gateway from rtm message |
| 847 | structure. */ |
paul | 6fe70d1 | 2005-11-12 22:55:10 +0000 | [diff] [blame] | 848 | flags = rtm_read_mesg (rtm, &dest, &mask, &gate, ifname, &ifnlen); |
Denis Ovsienko | 6da5980 | 2007-08-17 14:16:30 +0000 | [diff] [blame] | 849 | if (!(flags & RTF_DONE)) |
| 850 | return; |
Denis Ovsienko | dc95824 | 2007-08-13 16:03:06 +0000 | [diff] [blame] | 851 | if (IS_ZEBRA_DEBUG_KERNEL) |
| 852 | zlog_debug ("%s: got rtm of type %d (%s)", __func__, rtm->rtm_type, |
Denis Ovsienko | 2d84452 | 2007-09-14 11:31:55 +0000 | [diff] [blame] | 853 | lookup (rtm_type_str, rtm->rtm_type)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 854 | |
| 855 | #ifdef RTF_CLONED /*bsdi, netbsd 1.6*/ |
| 856 | if (flags & RTF_CLONED) |
| 857 | return; |
| 858 | #endif |
| 859 | #ifdef RTF_WASCLONED /*freebsd*/ |
| 860 | if (flags & RTF_WASCLONED) |
| 861 | return; |
| 862 | #endif |
| 863 | |
Timo Teräs | 0abf679 | 2016-01-15 17:36:29 +0200 | [diff] [blame] | 864 | if ((rtm->rtm_type == RTM_ADD || rtm->rtm_type == RTM_CHANGE) && ! (flags & RTF_UP)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 865 | return; |
| 866 | |
| 867 | /* This is connected route. */ |
| 868 | if (! (flags & RTF_GATEWAY)) |
| 869 | return; |
| 870 | |
| 871 | if (flags & RTF_PROTO1) |
| 872 | SET_FLAG (zebra_flags, ZEBRA_FLAG_SELFROUTE); |
| 873 | |
| 874 | /* This is persistent route. */ |
| 875 | if (flags & RTF_STATIC) |
| 876 | SET_FLAG (zebra_flags, ZEBRA_FLAG_STATIC); |
| 877 | |
hasso | 81dfcaa | 2003-05-25 19:21:25 +0000 | [diff] [blame] | 878 | /* This is a reject or blackhole route */ |
| 879 | if (flags & RTF_REJECT) |
| 880 | SET_FLAG (zebra_flags, ZEBRA_FLAG_REJECT); |
| 881 | if (flags & RTF_BLACKHOLE) |
| 882 | SET_FLAG (zebra_flags, ZEBRA_FLAG_BLACKHOLE); |
| 883 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 884 | if (dest.sa.sa_family == AF_INET) |
| 885 | { |
| 886 | struct prefix_ipv4 p; |
| 887 | |
| 888 | p.family = AF_INET; |
| 889 | p.prefix = dest.sin.sin_addr; |
| 890 | if (flags & RTF_HOST) |
| 891 | p.prefixlen = IPV4_MAX_PREFIXLEN; |
| 892 | else |
| 893 | p.prefixlen = ip_masklen (mask.sin.sin_addr); |
paul | ca16218 | 2005-09-12 16:58:52 +0000 | [diff] [blame] | 894 | |
Denis Ovsienko | dc95824 | 2007-08-13 16:03:06 +0000 | [diff] [blame] | 895 | /* Catch self originated messages and match them against our current RIB. |
| 896 | * At the same time, ignore unconfirmed messages, they should be tracked |
| 897 | * by rtm_write() and kernel_rtm_ipv4(). |
| 898 | */ |
Denis Ovsienko | 96934e6 | 2007-09-14 14:56:28 +0000 | [diff] [blame] | 899 | if (rtm->rtm_type != RTM_GET && rtm->rtm_pid == pid) |
Denis Ovsienko | dc95824 | 2007-08-13 16:03:06 +0000 | [diff] [blame] | 900 | { |
Timo Teräs | be6335d | 2015-05-23 11:08:41 +0300 | [diff] [blame] | 901 | char buf[PREFIX_STRLEN], gate_buf[INET_ADDRSTRLEN]; |
Denis Ovsienko | dc95824 | 2007-08-13 16:03:06 +0000 | [diff] [blame] | 902 | int ret; |
Denis Ovsienko | dc95824 | 2007-08-13 16:03:06 +0000 | [diff] [blame] | 903 | if (! IS_ZEBRA_DEBUG_RIB) |
| 904 | return; |
Feng Lu | 0d0686f | 2015-05-22 11:40:02 +0200 | [diff] [blame] | 905 | ret = rib_lookup_ipv4_route (&p, &gate, VRF_DEFAULT); |
Timo Teräs | be6335d | 2015-05-23 11:08:41 +0300 | [diff] [blame] | 906 | prefix2str (&p, buf, sizeof(buf)); |
Denis Ovsienko | dc95824 | 2007-08-13 16:03:06 +0000 | [diff] [blame] | 907 | switch (rtm->rtm_type) |
| 908 | { |
| 909 | case RTM_ADD: |
| 910 | case RTM_GET: |
| 911 | case RTM_CHANGE: |
| 912 | /* The kernel notifies us about a new route in FIB created by us. |
| 913 | Do we have a correspondent entry in our RIB? */ |
| 914 | switch (ret) |
| 915 | { |
| 916 | case ZEBRA_RIB_NOTFOUND: |
Timo Teräs | be6335d | 2015-05-23 11:08:41 +0300 | [diff] [blame] | 917 | zlog_debug ("%s: %s %s: desync: RR isn't yet in RIB, while already in FIB", |
| 918 | __func__, lookup (rtm_type_str, rtm->rtm_type), buf); |
Denis Ovsienko | dc95824 | 2007-08-13 16:03:06 +0000 | [diff] [blame] | 919 | break; |
| 920 | case ZEBRA_RIB_FOUND_CONNECTED: |
| 921 | case ZEBRA_RIB_FOUND_NOGATE: |
| 922 | inet_ntop (AF_INET, &gate.sin.sin_addr, gate_buf, INET_ADDRSTRLEN); |
Timo Teräs | be6335d | 2015-05-23 11:08:41 +0300 | [diff] [blame] | 923 | zlog_debug ("%s: %s %s: desync: RR is in RIB, but gate differs (ours is %s)", |
| 924 | __func__, lookup (rtm_type_str, rtm->rtm_type), buf, gate_buf); |
Denis Ovsienko | dc95824 | 2007-08-13 16:03:06 +0000 | [diff] [blame] | 925 | break; |
| 926 | case ZEBRA_RIB_FOUND_EXACT: /* RIB RR == FIB RR */ |
Timo Teräs | be6335d | 2015-05-23 11:08:41 +0300 | [diff] [blame] | 927 | zlog_debug ("%s: %s %s: done Ok", |
| 928 | __func__, lookup (rtm_type_str, rtm->rtm_type), buf); |
Denis Ovsienko | dc95824 | 2007-08-13 16:03:06 +0000 | [diff] [blame] | 929 | rib_lookup_and_dump (&p); |
| 930 | return; |
| 931 | break; |
| 932 | } |
| 933 | break; |
| 934 | case RTM_DELETE: |
| 935 | /* The kernel notifies us about a route deleted by us. Do we still |
| 936 | have it in the RIB? Do we have anything instead? */ |
| 937 | switch (ret) |
| 938 | { |
| 939 | case ZEBRA_RIB_FOUND_EXACT: |
Timo Teräs | be6335d | 2015-05-23 11:08:41 +0300 | [diff] [blame] | 940 | zlog_debug ("%s: %s %s: desync: RR is still in RIB, while already not in FIB", |
| 941 | __func__, lookup (rtm_type_str, rtm->rtm_type), buf); |
Denis Ovsienko | dc95824 | 2007-08-13 16:03:06 +0000 | [diff] [blame] | 942 | rib_lookup_and_dump (&p); |
| 943 | break; |
| 944 | case ZEBRA_RIB_FOUND_CONNECTED: |
| 945 | case ZEBRA_RIB_FOUND_NOGATE: |
Timo Teräs | be6335d | 2015-05-23 11:08:41 +0300 | [diff] [blame] | 946 | zlog_debug ("%s: %s %s: desync: RR is still in RIB, plus gate differs", |
| 947 | __func__, lookup (rtm_type_str, rtm->rtm_type), buf); |
Denis Ovsienko | dc95824 | 2007-08-13 16:03:06 +0000 | [diff] [blame] | 948 | rib_lookup_and_dump (&p); |
| 949 | break; |
| 950 | case ZEBRA_RIB_NOTFOUND: /* RIB RR == FIB RR */ |
Timo Teräs | be6335d | 2015-05-23 11:08:41 +0300 | [diff] [blame] | 951 | zlog_debug ("%s: %s %s: done Ok", |
| 952 | __func__, lookup (rtm_type_str, rtm->rtm_type), buf); |
Denis Ovsienko | dc95824 | 2007-08-13 16:03:06 +0000 | [diff] [blame] | 953 | rib_lookup_and_dump (&p); |
| 954 | return; |
| 955 | break; |
| 956 | } |
| 957 | break; |
| 958 | default: |
Timo Teräs | be6335d | 2015-05-23 11:08:41 +0300 | [diff] [blame] | 959 | zlog_debug ("%s: %s: warning: loopback RTM of type %s received", |
| 960 | __func__, buf, lookup (rtm_type_str, rtm->rtm_type)); |
Denis Ovsienko | dc95824 | 2007-08-13 16:03:06 +0000 | [diff] [blame] | 961 | } |
| 962 | return; |
| 963 | } |
| 964 | |
paul | ca16218 | 2005-09-12 16:58:52 +0000 | [diff] [blame] | 965 | /* Change, delete the old prefix, we have no further information |
| 966 | * to specify the route really |
| 967 | */ |
| 968 | if (rtm->rtm_type == RTM_CHANGE) |
| 969 | rib_delete_ipv4 (ZEBRA_ROUTE_KERNEL, zebra_flags, &p, |
Feng Lu | 0d0686f | 2015-05-22 11:40:02 +0200 | [diff] [blame] | 970 | NULL, 0, VRF_DEFAULT, SAFI_UNICAST); |
paul | ca16218 | 2005-09-12 16:58:52 +0000 | [diff] [blame] | 971 | |
| 972 | if (rtm->rtm_type == RTM_GET |
| 973 | || rtm->rtm_type == RTM_ADD |
| 974 | || rtm->rtm_type == RTM_CHANGE) |
Feng Lu | 0d0686f | 2015-05-22 11:40:02 +0200 | [diff] [blame] | 975 | rib_add_ipv4 (ZEBRA_ROUTE_KERNEL, zebra_flags, &p, &gate.sin.sin_addr, |
Timo Teräs | b11f3b5 | 2015-11-02 16:50:07 +0200 | [diff] [blame] | 976 | NULL, 0, VRF_DEFAULT, 0, 0, 0, 0, SAFI_UNICAST); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 977 | else |
Feng Lu | 0d0686f | 2015-05-22 11:40:02 +0200 | [diff] [blame] | 978 | rib_delete_ipv4 (ZEBRA_ROUTE_KERNEL, zebra_flags, &p, |
| 979 | &gate.sin.sin_addr, 0, VRF_DEFAULT, SAFI_UNICAST); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 980 | } |
| 981 | #ifdef HAVE_IPV6 |
| 982 | if (dest.sa.sa_family == AF_INET6) |
| 983 | { |
Denis Ovsienko | 5619f56 | 2007-10-24 13:13:21 +0000 | [diff] [blame] | 984 | /* One day we might have a debug section here like one in the |
| 985 | * IPv4 case above. Just ignore own messages at the moment. |
| 986 | */ |
| 987 | if (rtm->rtm_type != RTM_GET && rtm->rtm_pid == pid) |
| 988 | return; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 989 | struct prefix_ipv6 p; |
| 990 | unsigned int ifindex = 0; |
| 991 | |
| 992 | p.family = AF_INET6; |
| 993 | p.prefix = dest.sin6.sin6_addr; |
| 994 | if (flags & RTF_HOST) |
| 995 | p.prefixlen = IPV6_MAX_PREFIXLEN; |
| 996 | else |
| 997 | p.prefixlen = ip6_masklen (mask.sin6.sin6_addr); |
| 998 | |
| 999 | #ifdef KAME |
| 1000 | if (IN6_IS_ADDR_LINKLOCAL (&gate.sin6.sin6_addr)) |
| 1001 | { |
| 1002 | ifindex = IN6_LINKLOCAL_IFINDEX (gate.sin6.sin6_addr); |
| 1003 | SET_IN6_LINKLOCAL_IFINDEX (gate.sin6.sin6_addr, 0); |
| 1004 | } |
| 1005 | #endif /* KAME */ |
| 1006 | |
paul | ca16218 | 2005-09-12 16:58:52 +0000 | [diff] [blame] | 1007 | /* CHANGE: delete the old prefix, we have no further information |
| 1008 | * to specify the route really |
| 1009 | */ |
| 1010 | if (rtm->rtm_type == RTM_CHANGE) |
paul | 6621ca8 | 2005-11-23 13:02:08 +0000 | [diff] [blame] | 1011 | rib_delete_ipv6 (ZEBRA_ROUTE_KERNEL, zebra_flags, &p, |
Feng Lu | 0d0686f | 2015-05-22 11:40:02 +0200 | [diff] [blame] | 1012 | NULL, 0, VRF_DEFAULT, SAFI_UNICAST); |
paul | ca16218 | 2005-09-12 16:58:52 +0000 | [diff] [blame] | 1013 | |
| 1014 | if (rtm->rtm_type == RTM_GET |
| 1015 | || rtm->rtm_type == RTM_ADD |
| 1016 | || rtm->rtm_type == RTM_CHANGE) |
Feng Lu | 0d0686f | 2015-05-22 11:40:02 +0200 | [diff] [blame] | 1017 | rib_add_ipv6 (ZEBRA_ROUTE_KERNEL, zebra_flags, &p, &gate.sin6.sin6_addr, |
Timo Teräs | b11f3b5 | 2015-11-02 16:50:07 +0200 | [diff] [blame] | 1018 | ifindex, VRF_DEFAULT, RT_TABLE_MAIN, 0, 0, 0, SAFI_UNICAST); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1019 | else |
Feng Lu | 0d0686f | 2015-05-22 11:40:02 +0200 | [diff] [blame] | 1020 | rib_delete_ipv6 (ZEBRA_ROUTE_KERNEL, zebra_flags, &p, |
| 1021 | &gate.sin6.sin6_addr, ifindex, |
| 1022 | VRF_DEFAULT, SAFI_UNICAST); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1023 | } |
| 1024 | #endif /* HAVE_IPV6 */ |
| 1025 | } |
| 1026 | |
| 1027 | /* Interface function for the kernel routing table updates. Support |
paul | 6621ca8 | 2005-11-23 13:02:08 +0000 | [diff] [blame] | 1028 | * for RTM_CHANGE will be needed. |
| 1029 | * Exported only for rt_socket.c |
| 1030 | */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1031 | int |
| 1032 | rtm_write (int message, |
| 1033 | union sockunion *dest, |
| 1034 | union sockunion *mask, |
| 1035 | union sockunion *gate, |
| 1036 | unsigned int index, |
| 1037 | int zebra_flags, |
| 1038 | int metric) |
| 1039 | { |
| 1040 | int ret; |
| 1041 | caddr_t pnt; |
| 1042 | struct interface *ifp; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1043 | |
| 1044 | /* Sequencial number of routing message. */ |
| 1045 | static int msg_seq = 0; |
| 1046 | |
| 1047 | /* Struct of rt_msghdr and buffer for storing socket's data. */ |
| 1048 | struct |
| 1049 | { |
| 1050 | struct rt_msghdr rtm; |
| 1051 | char buf[512]; |
| 1052 | } msg; |
| 1053 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1054 | if (routing_sock < 0) |
| 1055 | return ZEBRA_ERR_EPERM; |
| 1056 | |
| 1057 | /* Clear and set rt_msghdr values */ |
| 1058 | memset (&msg, 0, sizeof (struct rt_msghdr)); |
| 1059 | msg.rtm.rtm_version = RTM_VERSION; |
| 1060 | msg.rtm.rtm_type = message; |
| 1061 | msg.rtm.rtm_seq = msg_seq++; |
| 1062 | msg.rtm.rtm_addrs = RTA_DST; |
| 1063 | msg.rtm.rtm_addrs |= RTA_GATEWAY; |
| 1064 | msg.rtm.rtm_flags = RTF_UP; |
| 1065 | msg.rtm.rtm_index = index; |
| 1066 | |
| 1067 | if (metric != 0) |
| 1068 | { |
| 1069 | msg.rtm.rtm_rmx.rmx_hopcount = metric; |
| 1070 | msg.rtm.rtm_inits |= RTV_HOPCOUNT; |
| 1071 | } |
| 1072 | |
| 1073 | ifp = if_lookup_by_index (index); |
| 1074 | |
Timo Teräs | 0abf679 | 2016-01-15 17:36:29 +0200 | [diff] [blame] | 1075 | if (gate && (message == RTM_ADD || message == RTM_CHANGE)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1076 | msg.rtm.rtm_flags |= RTF_GATEWAY; |
| 1077 | |
David Ward | e6f148e | 2009-12-03 21:43:11 +0300 | [diff] [blame] | 1078 | /* When RTF_CLONING is unavailable on BSD, should we set some |
| 1079 | * other flag instead? |
| 1080 | */ |
| 1081 | #ifdef RTF_CLONING |
Timo Teräs | 0abf679 | 2016-01-15 17:36:29 +0200 | [diff] [blame] | 1082 | if (! gate && (message == RTM_ADD || message == RTM_CHANGE) && ifp && |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1083 | (ifp->flags & IFF_POINTOPOINT) == 0) |
| 1084 | msg.rtm.rtm_flags |= RTF_CLONING; |
David Ward | e6f148e | 2009-12-03 21:43:11 +0300 | [diff] [blame] | 1085 | #endif /* RTF_CLONING */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1086 | |
| 1087 | /* If no protocol specific gateway is specified, use link |
| 1088 | address for gateway. */ |
| 1089 | if (! gate) |
| 1090 | { |
| 1091 | if (!ifp) |
| 1092 | { |
Denis Ovsienko | dc95824 | 2007-08-13 16:03:06 +0000 | [diff] [blame] | 1093 | char dest_buf[INET_ADDRSTRLEN] = "NULL", mask_buf[INET_ADDRSTRLEN] = "255.255.255.255"; |
| 1094 | if (dest) |
| 1095 | inet_ntop (AF_INET, &dest->sin.sin_addr, dest_buf, INET_ADDRSTRLEN); |
| 1096 | if (mask) |
| 1097 | inet_ntop (AF_INET, &mask->sin.sin_addr, mask_buf, INET_ADDRSTRLEN); |
| 1098 | zlog_warn ("%s: %s/%s: gate == NULL and no gateway found for ifindex %d", |
| 1099 | __func__, dest_buf, mask_buf, index); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1100 | return -1; |
| 1101 | } |
| 1102 | gate = (union sockunion *) & ifp->sdl; |
| 1103 | } |
| 1104 | |
| 1105 | if (mask) |
| 1106 | msg.rtm.rtm_addrs |= RTA_NETMASK; |
Timo Teräs | 0abf679 | 2016-01-15 17:36:29 +0200 | [diff] [blame] | 1107 | else if (message == RTM_ADD || message == RTM_CHANGE) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1108 | msg.rtm.rtm_flags |= RTF_HOST; |
| 1109 | |
| 1110 | /* Tagging route with flags */ |
| 1111 | msg.rtm.rtm_flags |= (RTF_PROTO1); |
| 1112 | |
| 1113 | /* Additional flags. */ |
| 1114 | if (zebra_flags & ZEBRA_FLAG_BLACKHOLE) |
| 1115 | msg.rtm.rtm_flags |= RTF_BLACKHOLE; |
hasso | 81dfcaa | 2003-05-25 19:21:25 +0000 | [diff] [blame] | 1116 | if (zebra_flags & ZEBRA_FLAG_REJECT) |
| 1117 | msg.rtm.rtm_flags |= RTF_REJECT; |
| 1118 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1119 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1120 | #define SOCKADDRSET(X,R) \ |
| 1121 | if (msg.rtm.rtm_addrs & (R)) \ |
| 1122 | { \ |
paul | 6fe70d1 | 2005-11-12 22:55:10 +0000 | [diff] [blame] | 1123 | int len = SAROUNDUP (X); \ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1124 | memcpy (pnt, (caddr_t)(X), len); \ |
| 1125 | pnt += len; \ |
| 1126 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1127 | |
| 1128 | pnt = (caddr_t) msg.buf; |
| 1129 | |
| 1130 | /* Write each socket data into rtm message buffer */ |
| 1131 | SOCKADDRSET (dest, RTA_DST); |
| 1132 | SOCKADDRSET (gate, RTA_GATEWAY); |
| 1133 | SOCKADDRSET (mask, RTA_NETMASK); |
| 1134 | |
| 1135 | msg.rtm.rtm_msglen = pnt - (caddr_t) &msg; |
| 1136 | |
| 1137 | ret = write (routing_sock, &msg, msg.rtm.rtm_msglen); |
| 1138 | |
| 1139 | if (ret != msg.rtm.rtm_msglen) |
| 1140 | { |
| 1141 | if (errno == EEXIST) |
| 1142 | return ZEBRA_ERR_RTEXIST; |
| 1143 | if (errno == ENETUNREACH) |
| 1144 | return ZEBRA_ERR_RTUNREACH; |
Denis Ovsienko | dc95824 | 2007-08-13 16:03:06 +0000 | [diff] [blame] | 1145 | if (errno == ESRCH) |
| 1146 | return ZEBRA_ERR_RTNOEXIST; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1147 | |
Denis Ovsienko | dc95824 | 2007-08-13 16:03:06 +0000 | [diff] [blame] | 1148 | zlog_warn ("%s: write : %s (%d)", __func__, safe_strerror (errno), errno); |
| 1149 | return ZEBRA_ERR_KERNEL; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1150 | } |
Denis Ovsienko | dc95824 | 2007-08-13 16:03:06 +0000 | [diff] [blame] | 1151 | return ZEBRA_ERR_NOERROR; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1152 | } |
| 1153 | |
David Lamparter | 6b0655a | 2014-06-04 06:53:35 +0200 | [diff] [blame] | 1154 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1155 | #include "thread.h" |
| 1156 | #include "zebra/zserv.h" |
| 1157 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1158 | /* For debug purpose. */ |
ajs | b617800 | 2004-12-07 21:12:56 +0000 | [diff] [blame] | 1159 | static void |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1160 | rtmsg_debug (struct rt_msghdr *rtm) |
| 1161 | { |
Denis Ovsienko | 2d84452 | 2007-09-14 11:31:55 +0000 | [diff] [blame] | 1162 | zlog_debug ("Kernel: Len: %d Type: %s", rtm->rtm_msglen, lookup (rtm_type_str, rtm->rtm_type)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1163 | rtm_flag_dump (rtm->rtm_flags); |
ajs | b617800 | 2004-12-07 21:12:56 +0000 | [diff] [blame] | 1164 | zlog_debug ("Kernel: message seq %d", rtm->rtm_seq); |
David Lamparter | da1b7ea | 2015-09-15 20:36:20 -0700 | [diff] [blame] | 1165 | zlog_debug ("Kernel: pid %lld, rtm_addrs 0x%x", |
| 1166 | (long long)rtm->rtm_pid, rtm->rtm_addrs); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1167 | } |
| 1168 | |
| 1169 | /* This is pretty gross, better suggestions welcome -- mhandler */ |
| 1170 | #ifndef RTAX_MAX |
| 1171 | #ifdef RTA_NUMBITS |
| 1172 | #define RTAX_MAX RTA_NUMBITS |
| 1173 | #else |
| 1174 | #define RTAX_MAX 8 |
| 1175 | #endif /* RTA_NUMBITS */ |
| 1176 | #endif /* RTAX_MAX */ |
| 1177 | |
| 1178 | /* Kernel routing table and interface updates via routing socket. */ |
paul | 6621ca8 | 2005-11-23 13:02:08 +0000 | [diff] [blame] | 1179 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1180 | kernel_read (struct thread *thread) |
| 1181 | { |
| 1182 | int sock; |
| 1183 | int nbytes; |
| 1184 | struct rt_msghdr *rtm; |
| 1185 | |
gdt | dbee01f | 2004-01-06 00:36:51 +0000 | [diff] [blame] | 1186 | /* |
| 1187 | * This must be big enough for any message the kernel might send. |
gdt | b27900b | 2004-01-08 15:44:29 +0000 | [diff] [blame] | 1188 | * Rather than determining how many sockaddrs of what size might be |
| 1189 | * in each particular message, just use RTAX_MAX of sockaddr_storage |
| 1190 | * for each. Note that the sockaddrs must be after each message |
| 1191 | * definition, or rather after whichever happens to be the largest, |
| 1192 | * since the buffer needs to be big enough for a message and the |
| 1193 | * sockaddrs together. |
gdt | dbee01f | 2004-01-06 00:36:51 +0000 | [diff] [blame] | 1194 | */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1195 | union |
| 1196 | { |
| 1197 | /* Routing information. */ |
| 1198 | struct |
| 1199 | { |
| 1200 | struct rt_msghdr rtm; |
gdt | b27900b | 2004-01-08 15:44:29 +0000 | [diff] [blame] | 1201 | struct sockaddr_storage addr[RTAX_MAX]; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1202 | } r; |
| 1203 | |
| 1204 | /* Interface information. */ |
| 1205 | struct |
| 1206 | { |
| 1207 | struct if_msghdr ifm; |
gdt | b27900b | 2004-01-08 15:44:29 +0000 | [diff] [blame] | 1208 | struct sockaddr_storage addr[RTAX_MAX]; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1209 | } im; |
| 1210 | |
| 1211 | /* Interface address information. */ |
| 1212 | struct |
| 1213 | { |
| 1214 | struct ifa_msghdr ifa; |
gdt | b27900b | 2004-01-08 15:44:29 +0000 | [diff] [blame] | 1215 | struct sockaddr_storage addr[RTAX_MAX]; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1216 | } ia; |
| 1217 | |
| 1218 | #ifdef RTM_IFANNOUNCE |
| 1219 | /* Interface arrival/departure */ |
| 1220 | struct |
| 1221 | { |
| 1222 | struct if_announcemsghdr ifan; |
gdt | b27900b | 2004-01-08 15:44:29 +0000 | [diff] [blame] | 1223 | struct sockaddr_storage addr[RTAX_MAX]; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1224 | } ian; |
| 1225 | #endif /* RTM_IFANNOUNCE */ |
| 1226 | |
| 1227 | } buf; |
| 1228 | |
| 1229 | /* Fetch routing socket. */ |
| 1230 | sock = THREAD_FD (thread); |
| 1231 | |
| 1232 | nbytes= read (sock, &buf, sizeof buf); |
| 1233 | |
| 1234 | if (nbytes <= 0) |
| 1235 | { |
| 1236 | if (nbytes < 0 && errno != EWOULDBLOCK && errno != EAGAIN) |
ajs | 6099b3b | 2004-11-20 02:06:59 +0000 | [diff] [blame] | 1237 | zlog_warn ("routing socket error: %s", safe_strerror (errno)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1238 | return 0; |
| 1239 | } |
| 1240 | |
paul | 9bcdb63 | 2003-07-08 08:09:45 +0000 | [diff] [blame] | 1241 | thread_add_read (zebrad.master, kernel_read, NULL, sock); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1242 | |
hasso | 726f9b2 | 2003-05-25 21:04:54 +0000 | [diff] [blame] | 1243 | if (IS_ZEBRA_DEBUG_KERNEL) |
| 1244 | rtmsg_debug (&buf.r.rtm); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1245 | |
| 1246 | rtm = &buf.r.rtm; |
| 1247 | |
gdt | b27900b | 2004-01-08 15:44:29 +0000 | [diff] [blame] | 1248 | /* |
| 1249 | * Ensure that we didn't drop any data, so that processing routines |
| 1250 | * can assume they have the whole message. |
| 1251 | */ |
gdt | da26e3b | 2004-01-05 17:20:59 +0000 | [diff] [blame] | 1252 | if (rtm->rtm_msglen != nbytes) |
| 1253 | { |
| 1254 | zlog_warn ("kernel_read: rtm->rtm_msglen %d, nbytes %d, type %d\n", |
| 1255 | rtm->rtm_msglen, nbytes, rtm->rtm_type); |
| 1256 | return -1; |
| 1257 | } |
| 1258 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1259 | switch (rtm->rtm_type) |
| 1260 | { |
| 1261 | case RTM_ADD: |
| 1262 | case RTM_DELETE: |
paul | ca16218 | 2005-09-12 16:58:52 +0000 | [diff] [blame] | 1263 | case RTM_CHANGE: |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1264 | rtm_read (rtm); |
| 1265 | break; |
| 1266 | case RTM_IFINFO: |
| 1267 | ifm_read (&buf.im.ifm); |
| 1268 | break; |
| 1269 | case RTM_NEWADDR: |
| 1270 | case RTM_DELADDR: |
| 1271 | ifam_read (&buf.ia.ifa); |
| 1272 | break; |
| 1273 | #ifdef RTM_IFANNOUNCE |
| 1274 | case RTM_IFANNOUNCE: |
| 1275 | ifan_read (&buf.ian.ifan); |
| 1276 | break; |
| 1277 | #endif /* RTM_IFANNOUNCE */ |
| 1278 | default: |
hasso | 726f9b2 | 2003-05-25 21:04:54 +0000 | [diff] [blame] | 1279 | if (IS_ZEBRA_DEBUG_KERNEL) |
ajs | b617800 | 2004-12-07 21:12:56 +0000 | [diff] [blame] | 1280 | zlog_debug("Unprocessed RTM_type: %d", rtm->rtm_type); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1281 | break; |
| 1282 | } |
| 1283 | return 0; |
| 1284 | } |
| 1285 | |
| 1286 | /* Make routing socket. */ |
paul | 6621ca8 | 2005-11-23 13:02:08 +0000 | [diff] [blame] | 1287 | static void |
Feng Lu | 758fb8f | 2014-07-03 18:23:09 +0800 | [diff] [blame] | 1288 | routing_socket (struct zebra_vrf *zvrf) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1289 | { |
Feng Lu | 758fb8f | 2014-07-03 18:23:09 +0800 | [diff] [blame] | 1290 | if (zvrf->vrf_id != VRF_DEFAULT) |
| 1291 | return; |
| 1292 | |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 1293 | if ( zserv_privs.change (ZPRIVS_RAISE) ) |
| 1294 | zlog_err ("routing_socket: Can't raise privileges"); |
| 1295 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1296 | routing_sock = socket (AF_ROUTE, SOCK_RAW, 0); |
| 1297 | |
| 1298 | if (routing_sock < 0) |
| 1299 | { |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 1300 | if ( zserv_privs.change (ZPRIVS_LOWER) ) |
| 1301 | zlog_err ("routing_socket: Can't lower privileges"); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1302 | zlog_warn ("Can't init kernel routing socket"); |
| 1303 | return; |
| 1304 | } |
| 1305 | |
paul | 865b852 | 2005-01-05 08:30:35 +0000 | [diff] [blame] | 1306 | /* XXX: Socket should be NONBLOCK, however as we currently |
| 1307 | * discard failed writes, this will lead to inconsistencies. |
| 1308 | * For now, socket must be blocking. |
| 1309 | */ |
| 1310 | /*if (fcntl (routing_sock, F_SETFL, O_NONBLOCK) < 0) |
| 1311 | zlog_warn ("Can't set O_NONBLOCK to routing socket");*/ |
| 1312 | |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 1313 | if ( zserv_privs.change (ZPRIVS_LOWER) ) |
| 1314 | zlog_err ("routing_socket: Can't lower privileges"); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1315 | |
| 1316 | /* kernel_read needs rewrite. */ |
paul | 9bcdb63 | 2003-07-08 08:09:45 +0000 | [diff] [blame] | 1317 | thread_add_read (zebrad.master, kernel_read, NULL, routing_sock); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1318 | } |
| 1319 | |
| 1320 | /* Exported interface function. This function simply calls |
| 1321 | routing_socket (). */ |
| 1322 | void |
Feng Lu | 758fb8f | 2014-07-03 18:23:09 +0800 | [diff] [blame] | 1323 | kernel_init (struct zebra_vrf *zvrf) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1324 | { |
Feng Lu | 758fb8f | 2014-07-03 18:23:09 +0800 | [diff] [blame] | 1325 | routing_socket (zvrf); |
| 1326 | } |
| 1327 | |
| 1328 | void |
| 1329 | kernel_terminate (struct zebra_vrf *zvrf) |
| 1330 | { |
| 1331 | return; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1332 | } |