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