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