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 long |
Doug VanLeuven | 3b33de6 | 2012-10-10 22:10:14 +0000 | [diff] [blame] | 88 | #else |
Greg Troxel | 273b1bd | 2014-12-02 14:51:49 -0500 | [diff] [blame] | 89 | #define ROUNDUP_TYPE int |
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 | { |
| 634 | switch (sockunion_family(addr)) |
| 635 | { |
| 636 | case AF_INET: |
| 637 | { |
Andrew J. Schorr | 7ab62c5 | 2007-05-17 15:00:41 +0000 | [diff] [blame] | 638 | char buf[4][INET_ADDRSTRLEN]; |
Andrew J. Schorr | 5519604 | 2006-05-17 15:04:59 +0000 | [diff] [blame] | 639 | zlog_debug ("%s: ifindex %d, ifname %s, ifam_addrs 0x%x, " |
Andrew J. Schorr | 7ab62c5 | 2007-05-17 15:00:41 +0000 | [diff] [blame] | 640 | "ifam_flags 0x%x, addr %s/%d broad %s dst %s " |
| 641 | "gateway %s", |
| 642 | __func__, ifm->ifam_index, |
Andrew J. Schorr | 5519604 | 2006-05-17 15:04:59 +0000 | [diff] [blame] | 643 | (ifnlen ? ifname : "(nil)"), ifm->ifam_addrs, |
Andrew J. Schorr | 7ab62c5 | 2007-05-17 15:00:41 +0000 | [diff] [blame] | 644 | ifm->ifam_flags, |
Andrew J. Schorr | 5519604 | 2006-05-17 15:04:59 +0000 | [diff] [blame] | 645 | inet_ntop(AF_INET,&addr->sin.sin_addr, |
| 646 | buf[0],sizeof(buf[0])), |
| 647 | ip_masklen(mask->sin.sin_addr), |
| 648 | inet_ntop(AF_INET,&brd->sin.sin_addr, |
Andrew J. Schorr | 7ab62c5 | 2007-05-17 15:00:41 +0000 | [diff] [blame] | 649 | buf[1],sizeof(buf[1])), |
| 650 | inet_ntop(AF_INET,&dst.sin.sin_addr, |
| 651 | buf[2],sizeof(buf[2])), |
| 652 | inet_ntop(AF_INET,&gateway.sin.sin_addr, |
| 653 | buf[3],sizeof(buf[3]))); |
Andrew J. Schorr | 5519604 | 2006-05-17 15:04:59 +0000 | [diff] [blame] | 654 | } |
| 655 | break; |
| 656 | #ifdef HAVE_IPV6 |
| 657 | case AF_INET6: |
| 658 | { |
Andrew J. Schorr | 7ab62c5 | 2007-05-17 15:00:41 +0000 | [diff] [blame] | 659 | char buf[4][INET6_ADDRSTRLEN]; |
Andrew J. Schorr | 5519604 | 2006-05-17 15:04:59 +0000 | [diff] [blame] | 660 | zlog_debug ("%s: ifindex %d, ifname %s, ifam_addrs 0x%x, " |
Andrew J. Schorr | 7ab62c5 | 2007-05-17 15:00:41 +0000 | [diff] [blame] | 661 | "ifam_flags 0x%x, addr %s/%d broad %s dst %s " |
| 662 | "gateway %s", |
Andrew J. Schorr | 5519604 | 2006-05-17 15:04:59 +0000 | [diff] [blame] | 663 | __func__, ifm->ifam_index, |
| 664 | (ifnlen ? ifname : "(nil)"), ifm->ifam_addrs, |
Andrew J. Schorr | 7ab62c5 | 2007-05-17 15:00:41 +0000 | [diff] [blame] | 665 | ifm->ifam_flags, |
Andrew J. Schorr | 5519604 | 2006-05-17 15:04:59 +0000 | [diff] [blame] | 666 | inet_ntop(AF_INET6,&addr->sin6.sin6_addr, |
| 667 | buf[0],sizeof(buf[0])), |
| 668 | ip6_masklen(mask->sin6.sin6_addr), |
| 669 | inet_ntop(AF_INET6,&brd->sin6.sin6_addr, |
Andrew J. Schorr | 7ab62c5 | 2007-05-17 15:00:41 +0000 | [diff] [blame] | 670 | buf[1],sizeof(buf[1])), |
| 671 | inet_ntop(AF_INET6,&dst.sin6.sin6_addr, |
| 672 | buf[2],sizeof(buf[2])), |
| 673 | inet_ntop(AF_INET6,&gateway.sin6.sin6_addr, |
| 674 | buf[3],sizeof(buf[3]))); |
Andrew J. Schorr | 5519604 | 2006-05-17 15:04:59 +0000 | [diff] [blame] | 675 | } |
| 676 | break; |
| 677 | #endif /* HAVE_IPV6 */ |
| 678 | default: |
| 679 | zlog_debug ("%s: ifindex %d, ifname %s, ifam_addrs 0x%x", |
| 680 | __func__, ifm->ifam_index, |
| 681 | (ifnlen ? ifname : "(nil)"), ifm->ifam_addrs); |
| 682 | break; |
| 683 | } |
| 684 | } |
Andrew J. Schorr | 7ab62c5 | 2007-05-17 15:00:41 +0000 | [diff] [blame] | 685 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 686 | /* Assert read up end point matches to end point */ |
| 687 | if (pnt != end) |
Denis Ovsienko | 85a2ebf | 2011-12-05 19:36:06 +0400 | [diff] [blame] | 688 | zlog_warn ("ifam_read() doesn't read all socket data"); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 689 | } |
| 690 | |
| 691 | /* Interface's address information get. */ |
paul | ec1a428 | 2005-11-24 15:15:17 +0000 | [diff] [blame] | 692 | int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 693 | ifam_read (struct ifa_msghdr *ifam) |
| 694 | { |
paul | 6fe70d1 | 2005-11-12 22:55:10 +0000 | [diff] [blame] | 695 | struct interface *ifp = NULL; |
paul | 0752ef0 | 2005-11-03 12:35:21 +0000 | [diff] [blame] | 696 | union sockunion addr, mask, brd; |
paul | 6fe70d1 | 2005-11-12 22:55:10 +0000 | [diff] [blame] | 697 | char ifname[INTERFACE_NAMSIZ]; |
| 698 | short ifnlen = 0; |
| 699 | char isalias = 0; |
Andrew J. Schorr | 7ab62c5 | 2007-05-17 15:00:41 +0000 | [diff] [blame] | 700 | int flags = 0; |
paul | 6fe70d1 | 2005-11-12 22:55:10 +0000 | [diff] [blame] | 701 | |
| 702 | ifname[0] = ifname[INTERFACE_NAMSIZ - 1] = '\0'; |
| 703 | |
| 704 | /* Allocate and read address information. */ |
| 705 | ifam_read_mesg (ifam, &addr, &mask, &brd, ifname, &ifnlen); |
| 706 | |
| 707 | if ((ifp = if_lookup_by_index(ifam->ifam_index)) == NULL) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 708 | { |
paul | 6fe70d1 | 2005-11-12 22:55:10 +0000 | [diff] [blame] | 709 | zlog_warn ("%s: no interface for ifname %s, index %d", |
| 710 | __func__, ifname, ifam->ifam_index); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 711 | return -1; |
| 712 | } |
paul | 6fe70d1 | 2005-11-12 22:55:10 +0000 | [diff] [blame] | 713 | |
| 714 | if (ifnlen && strncmp (ifp->name, ifname, INTERFACE_NAMSIZ)) |
| 715 | isalias = 1; |
| 716 | |
Andrew J. Schorr | 7ab62c5 | 2007-05-17 15:00:41 +0000 | [diff] [blame] | 717 | /* N.B. The info in ifa_msghdr does not tell us whether the RTA_BRD |
| 718 | field contains a broadcast address or a peer address, so we are forced to |
| 719 | rely upon the interface type. */ |
| 720 | if (if_is_pointopoint(ifp)) |
| 721 | SET_FLAG(flags, ZEBRA_IFA_PEER); |
| 722 | |
Paul Jakma | 6502208 | 2007-03-06 13:43:05 +0000 | [diff] [blame] | 723 | #if 0 |
| 724 | /* it might seem cute to grab the interface metric here, however |
| 725 | * we're processing an address update message, and so some systems |
| 726 | * (e.g. FBSD) dont bother to fill in ifam_metric. Disabled, but left |
| 727 | * in deliberately, as comment. |
| 728 | */ |
paul | d34b899 | 2006-01-17 18:03:04 +0000 | [diff] [blame] | 729 | ifp->metric = ifam->ifam_metric; |
Paul Jakma | 6502208 | 2007-03-06 13:43:05 +0000 | [diff] [blame] | 730 | #endif |
| 731 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 732 | /* Add connected address. */ |
| 733 | switch (sockunion_family (&addr)) |
| 734 | { |
| 735 | case AF_INET: |
| 736 | if (ifam->ifam_type == RTM_NEWADDR) |
Andrew J. Schorr | 7ab62c5 | 2007-05-17 15:00:41 +0000 | [diff] [blame] | 737 | connected_add_ipv4 (ifp, flags, &addr.sin.sin_addr, |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 738 | ip_masklen (mask.sin.sin_addr), |
paul | d34b899 | 2006-01-17 18:03:04 +0000 | [diff] [blame] | 739 | &brd.sin.sin_addr, |
| 740 | (isalias ? ifname : NULL)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 741 | else |
Andrew J. Schorr | 7ab62c5 | 2007-05-17 15:00:41 +0000 | [diff] [blame] | 742 | connected_delete_ipv4 (ifp, flags, &addr.sin.sin_addr, |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 743 | ip_masklen (mask.sin.sin_addr), |
paul | 0752ef0 | 2005-11-03 12:35:21 +0000 | [diff] [blame] | 744 | &brd.sin.sin_addr); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 745 | break; |
| 746 | #ifdef HAVE_IPV6 |
| 747 | case AF_INET6: |
| 748 | /* Unset interface index from link-local address when IPv6 stack |
| 749 | is KAME. */ |
| 750 | if (IN6_IS_ADDR_LINKLOCAL (&addr.sin6.sin6_addr)) |
| 751 | SET_IN6_LINKLOCAL_IFINDEX (addr.sin6.sin6_addr, 0); |
| 752 | |
| 753 | if (ifam->ifam_type == RTM_NEWADDR) |
Andrew J. Schorr | 7ab62c5 | 2007-05-17 15:00:41 +0000 | [diff] [blame] | 754 | connected_add_ipv6 (ifp, flags, &addr.sin6.sin6_addr, |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 755 | ip6_masklen (mask.sin6.sin6_addr), |
paul | d34b899 | 2006-01-17 18:03:04 +0000 | [diff] [blame] | 756 | &brd.sin6.sin6_addr, |
| 757 | (isalias ? ifname : NULL)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 758 | else |
| 759 | connected_delete_ipv6 (ifp, |
| 760 | &addr.sin6.sin6_addr, |
| 761 | ip6_masklen (mask.sin6.sin6_addr), |
paul | 0752ef0 | 2005-11-03 12:35:21 +0000 | [diff] [blame] | 762 | &brd.sin6.sin6_addr); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 763 | break; |
| 764 | #endif /* HAVE_IPV6 */ |
| 765 | default: |
| 766 | /* Unsupported family silently ignore... */ |
| 767 | break; |
| 768 | } |
paul | 5c78b3d | 2006-01-25 04:31:40 +0000 | [diff] [blame] | 769 | |
| 770 | /* Check interface flag for implicit up of the interface. */ |
| 771 | if_refresh (ifp); |
| 772 | |
| 773 | #ifdef SUNOS_5 |
| 774 | /* In addition to lacking IFANNOUNCE, on SUNOS IFF_UP is strange. |
| 775 | * See comments for SUNOS_5 in interface.c::if_flags_mangle. |
| 776 | * |
| 777 | * Here we take care of case where the real IFF_UP was previously |
| 778 | * unset (as kept in struct zebra_if.primary_state) and the mangled |
| 779 | * IFF_UP (ie IFF_UP set || listcount(connected) has now transitioned |
| 780 | * to unset due to the lost non-primary address having DELADDR'd. |
| 781 | * |
| 782 | * we must delete the interface, because in between here and next |
| 783 | * event for this interface-name the administrator could unplumb |
| 784 | * and replumb the interface. |
| 785 | */ |
| 786 | if (!if_is_up (ifp)) |
| 787 | if_delete_update (ifp); |
| 788 | #endif /* SUNOS_5 */ |
| 789 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 790 | return 0; |
| 791 | } |
David Lamparter | 6b0655a | 2014-06-04 06:53:35 +0200 | [diff] [blame] | 792 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 793 | /* Interface function for reading kernel routing table information. */ |
paul | 6621ca8 | 2005-11-23 13:02:08 +0000 | [diff] [blame] | 794 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 795 | rtm_read_mesg (struct rt_msghdr *rtm, |
| 796 | union sockunion *dest, |
| 797 | union sockunion *mask, |
paul | 6fe70d1 | 2005-11-12 22:55:10 +0000 | [diff] [blame] | 798 | union sockunion *gate, |
| 799 | char *ifname, |
| 800 | short *ifnlen) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 801 | { |
| 802 | caddr_t pnt, end; |
| 803 | |
| 804 | /* Pnt points out socket data start point. */ |
| 805 | pnt = (caddr_t)(rtm + 1); |
| 806 | end = ((caddr_t)rtm) + rtm->rtm_msglen; |
| 807 | |
| 808 | /* rt_msghdr version check. */ |
| 809 | if (rtm->rtm_version != RTM_VERSION) |
| 810 | zlog (NULL, LOG_WARNING, |
| 811 | "Routing message version different %d should be %d." |
| 812 | "This may cause problem\n", rtm->rtm_version, RTM_VERSION); |
paul | 62debbb | 2005-06-14 14:07:07 +0000 | [diff] [blame] | 813 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 814 | /* Be sure structure is cleared */ |
| 815 | memset (dest, 0, sizeof (union sockunion)); |
| 816 | memset (gate, 0, sizeof (union sockunion)); |
| 817 | memset (mask, 0, sizeof (union sockunion)); |
| 818 | |
| 819 | /* We fetch each socket variable into sockunion. */ |
paul | 62debbb | 2005-06-14 14:07:07 +0000 | [diff] [blame] | 820 | RTA_ADDR_GET (dest, RTA_DST, rtm->rtm_addrs, pnt); |
| 821 | RTA_ADDR_GET (gate, RTA_GATEWAY, rtm->rtm_addrs, pnt); |
| 822 | RTA_ATTR_GET (mask, RTA_NETMASK, rtm->rtm_addrs, pnt); |
| 823 | RTA_ADDR_GET (NULL, RTA_GENMASK, rtm->rtm_addrs, pnt); |
paul | 6fe70d1 | 2005-11-12 22:55:10 +0000 | [diff] [blame] | 824 | RTA_NAME_GET (ifname, RTA_IFP, rtm->rtm_addrs, pnt, *ifnlen); |
paul | 62debbb | 2005-06-14 14:07:07 +0000 | [diff] [blame] | 825 | RTA_ADDR_GET (NULL, RTA_IFA, rtm->rtm_addrs, pnt); |
| 826 | RTA_ADDR_GET (NULL, RTA_AUTHOR, rtm->rtm_addrs, pnt); |
| 827 | RTA_ADDR_GET (NULL, RTA_BRD, rtm->rtm_addrs, pnt); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 828 | |
| 829 | /* If there is netmask information set it's family same as |
| 830 | destination family*/ |
| 831 | if (rtm->rtm_addrs & RTA_NETMASK) |
| 832 | mask->sa.sa_family = dest->sa.sa_family; |
| 833 | |
| 834 | /* Assert read up to the end of pointer. */ |
| 835 | if (pnt != end) |
Denis Ovsienko | 85a2ebf | 2011-12-05 19:36:06 +0400 | [diff] [blame] | 836 | zlog (NULL, LOG_WARNING, "rtm_read() doesn't read all socket data."); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 837 | |
| 838 | return rtm->rtm_flags; |
| 839 | } |
| 840 | |
paul | ec1a428 | 2005-11-24 15:15:17 +0000 | [diff] [blame] | 841 | void |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 842 | rtm_read (struct rt_msghdr *rtm) |
| 843 | { |
| 844 | int flags; |
| 845 | u_char zebra_flags; |
| 846 | union sockunion dest, mask, gate; |
paul | 6fe70d1 | 2005-11-12 22:55:10 +0000 | [diff] [blame] | 847 | char ifname[INTERFACE_NAMSIZ + 1]; |
| 848 | short ifnlen = 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 849 | |
| 850 | zebra_flags = 0; |
| 851 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 852 | /* Read destination and netmask and gateway from rtm message |
| 853 | structure. */ |
paul | 6fe70d1 | 2005-11-12 22:55:10 +0000 | [diff] [blame] | 854 | flags = rtm_read_mesg (rtm, &dest, &mask, &gate, ifname, &ifnlen); |
Denis Ovsienko | 6da5980 | 2007-08-17 14:16:30 +0000 | [diff] [blame] | 855 | if (!(flags & RTF_DONE)) |
| 856 | return; |
Denis Ovsienko | dc95824 | 2007-08-13 16:03:06 +0000 | [diff] [blame] | 857 | if (IS_ZEBRA_DEBUG_KERNEL) |
| 858 | 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] | 859 | lookup (rtm_type_str, rtm->rtm_type)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 860 | |
| 861 | #ifdef RTF_CLONED /*bsdi, netbsd 1.6*/ |
| 862 | if (flags & RTF_CLONED) |
| 863 | return; |
| 864 | #endif |
| 865 | #ifdef RTF_WASCLONED /*freebsd*/ |
| 866 | if (flags & RTF_WASCLONED) |
| 867 | return; |
| 868 | #endif |
| 869 | |
| 870 | if ((rtm->rtm_type == RTM_ADD) && ! (flags & RTF_UP)) |
| 871 | return; |
| 872 | |
| 873 | /* This is connected route. */ |
| 874 | if (! (flags & RTF_GATEWAY)) |
| 875 | return; |
| 876 | |
| 877 | if (flags & RTF_PROTO1) |
| 878 | SET_FLAG (zebra_flags, ZEBRA_FLAG_SELFROUTE); |
| 879 | |
| 880 | /* This is persistent route. */ |
| 881 | if (flags & RTF_STATIC) |
| 882 | SET_FLAG (zebra_flags, ZEBRA_FLAG_STATIC); |
| 883 | |
hasso | 81dfcaa | 2003-05-25 19:21:25 +0000 | [diff] [blame] | 884 | /* This is a reject or blackhole route */ |
| 885 | if (flags & RTF_REJECT) |
| 886 | SET_FLAG (zebra_flags, ZEBRA_FLAG_REJECT); |
| 887 | if (flags & RTF_BLACKHOLE) |
| 888 | SET_FLAG (zebra_flags, ZEBRA_FLAG_BLACKHOLE); |
| 889 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 890 | if (dest.sa.sa_family == AF_INET) |
| 891 | { |
| 892 | struct prefix_ipv4 p; |
| 893 | |
| 894 | p.family = AF_INET; |
| 895 | p.prefix = dest.sin.sin_addr; |
| 896 | if (flags & RTF_HOST) |
| 897 | p.prefixlen = IPV4_MAX_PREFIXLEN; |
| 898 | else |
| 899 | p.prefixlen = ip_masklen (mask.sin.sin_addr); |
paul | ca16218 | 2005-09-12 16:58:52 +0000 | [diff] [blame] | 900 | |
Denis Ovsienko | dc95824 | 2007-08-13 16:03:06 +0000 | [diff] [blame] | 901 | /* Catch self originated messages and match them against our current RIB. |
| 902 | * At the same time, ignore unconfirmed messages, they should be tracked |
| 903 | * by rtm_write() and kernel_rtm_ipv4(). |
| 904 | */ |
Denis Ovsienko | 96934e6 | 2007-09-14 14:56:28 +0000 | [diff] [blame] | 905 | if (rtm->rtm_type != RTM_GET && rtm->rtm_pid == pid) |
Denis Ovsienko | dc95824 | 2007-08-13 16:03:06 +0000 | [diff] [blame] | 906 | { |
| 907 | char buf[INET_ADDRSTRLEN], gate_buf[INET_ADDRSTRLEN]; |
| 908 | int ret; |
Denis Ovsienko | dc95824 | 2007-08-13 16:03:06 +0000 | [diff] [blame] | 909 | if (! IS_ZEBRA_DEBUG_RIB) |
| 910 | return; |
| 911 | ret = rib_lookup_ipv4_route (&p, &gate); |
| 912 | inet_ntop (AF_INET, &p.prefix, buf, INET_ADDRSTRLEN); |
| 913 | switch (rtm->rtm_type) |
| 914 | { |
| 915 | case RTM_ADD: |
| 916 | case RTM_GET: |
| 917 | case RTM_CHANGE: |
| 918 | /* The kernel notifies us about a new route in FIB created by us. |
| 919 | Do we have a correspondent entry in our RIB? */ |
| 920 | switch (ret) |
| 921 | { |
| 922 | case ZEBRA_RIB_NOTFOUND: |
| 923 | zlog_debug ("%s: %s %s/%d: desync: RR isn't yet in RIB, while already in FIB", |
Denis Ovsienko | 2d84452 | 2007-09-14 11:31:55 +0000 | [diff] [blame] | 924 | __func__, lookup (rtm_type_str, rtm->rtm_type), buf, p.prefixlen); |
Denis Ovsienko | dc95824 | 2007-08-13 16:03:06 +0000 | [diff] [blame] | 925 | break; |
| 926 | case ZEBRA_RIB_FOUND_CONNECTED: |
| 927 | case ZEBRA_RIB_FOUND_NOGATE: |
| 928 | inet_ntop (AF_INET, &gate.sin.sin_addr, gate_buf, INET_ADDRSTRLEN); |
| 929 | zlog_debug ("%s: %s %s/%d: desync: RR is in RIB, but gate differs (ours is %s)", |
Denis Ovsienko | 2d84452 | 2007-09-14 11:31:55 +0000 | [diff] [blame] | 930 | __func__, lookup (rtm_type_str, rtm->rtm_type), buf, p.prefixlen, gate_buf); |
Denis Ovsienko | dc95824 | 2007-08-13 16:03:06 +0000 | [diff] [blame] | 931 | break; |
| 932 | case ZEBRA_RIB_FOUND_EXACT: /* RIB RR == FIB RR */ |
| 933 | zlog_debug ("%s: %s %s/%d: done Ok", |
Denis Ovsienko | 2d84452 | 2007-09-14 11:31:55 +0000 | [diff] [blame] | 934 | __func__, lookup (rtm_type_str, rtm->rtm_type), buf, p.prefixlen); |
Denis Ovsienko | dc95824 | 2007-08-13 16:03:06 +0000 | [diff] [blame] | 935 | rib_lookup_and_dump (&p); |
| 936 | return; |
| 937 | break; |
| 938 | } |
| 939 | break; |
| 940 | case RTM_DELETE: |
| 941 | /* The kernel notifies us about a route deleted by us. Do we still |
| 942 | have it in the RIB? Do we have anything instead? */ |
| 943 | switch (ret) |
| 944 | { |
| 945 | case ZEBRA_RIB_FOUND_EXACT: |
| 946 | zlog_debug ("%s: %s %s/%d: desync: RR is still in RIB, while already not in FIB", |
Denis Ovsienko | 2d84452 | 2007-09-14 11:31:55 +0000 | [diff] [blame] | 947 | __func__, lookup (rtm_type_str, rtm->rtm_type), buf, p.prefixlen); |
Denis Ovsienko | dc95824 | 2007-08-13 16:03:06 +0000 | [diff] [blame] | 948 | rib_lookup_and_dump (&p); |
| 949 | break; |
| 950 | case ZEBRA_RIB_FOUND_CONNECTED: |
| 951 | case ZEBRA_RIB_FOUND_NOGATE: |
| 952 | zlog_debug ("%s: %s %s/%d: desync: RR is still in RIB, plus gate differs", |
Denis Ovsienko | 2d84452 | 2007-09-14 11:31:55 +0000 | [diff] [blame] | 953 | __func__, lookup (rtm_type_str, rtm->rtm_type), buf, p.prefixlen); |
Denis Ovsienko | dc95824 | 2007-08-13 16:03:06 +0000 | [diff] [blame] | 954 | rib_lookup_and_dump (&p); |
| 955 | break; |
| 956 | case ZEBRA_RIB_NOTFOUND: /* RIB RR == FIB RR */ |
| 957 | zlog_debug ("%s: %s %s/%d: done Ok", |
Denis Ovsienko | 2d84452 | 2007-09-14 11:31:55 +0000 | [diff] [blame] | 958 | __func__, lookup (rtm_type_str, rtm->rtm_type), buf, p.prefixlen); |
Denis Ovsienko | dc95824 | 2007-08-13 16:03:06 +0000 | [diff] [blame] | 959 | rib_lookup_and_dump (&p); |
| 960 | return; |
| 961 | break; |
| 962 | } |
| 963 | break; |
| 964 | default: |
| 965 | zlog_debug ("%s: %s/%d: warning: loopback RTM of type %s received", |
Denis Ovsienko | 2d84452 | 2007-09-14 11:31:55 +0000 | [diff] [blame] | 966 | __func__, buf, p.prefixlen, lookup (rtm_type_str, rtm->rtm_type)); |
Denis Ovsienko | dc95824 | 2007-08-13 16:03:06 +0000 | [diff] [blame] | 967 | } |
| 968 | return; |
| 969 | } |
| 970 | |
paul | ca16218 | 2005-09-12 16:58:52 +0000 | [diff] [blame] | 971 | /* Change, delete the old prefix, we have no further information |
| 972 | * to specify the route really |
| 973 | */ |
| 974 | if (rtm->rtm_type == RTM_CHANGE) |
| 975 | rib_delete_ipv4 (ZEBRA_ROUTE_KERNEL, zebra_flags, &p, |
Denis Ovsienko | 6eac79a | 2011-12-05 13:43:18 +0400 | [diff] [blame] | 976 | NULL, 0, 0, SAFI_UNICAST); |
paul | ca16218 | 2005-09-12 16:58:52 +0000 | [diff] [blame] | 977 | |
| 978 | if (rtm->rtm_type == RTM_GET |
| 979 | || rtm->rtm_type == RTM_ADD |
| 980 | || rtm->rtm_type == RTM_CHANGE) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 981 | rib_add_ipv4 (ZEBRA_ROUTE_KERNEL, zebra_flags, |
G.Balaji | cddf391 | 2011-11-26 21:59:32 +0400 | [diff] [blame] | 982 | &p, &gate.sin.sin_addr, NULL, 0, 0, 0, 0, SAFI_UNICAST); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 983 | else |
| 984 | rib_delete_ipv4 (ZEBRA_ROUTE_KERNEL, zebra_flags, |
Denis Ovsienko | 6eac79a | 2011-12-05 13:43:18 +0400 | [diff] [blame] | 985 | &p, &gate.sin.sin_addr, 0, 0, SAFI_UNICAST); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 986 | } |
| 987 | #ifdef HAVE_IPV6 |
| 988 | if (dest.sa.sa_family == AF_INET6) |
| 989 | { |
Denis Ovsienko | 5619f56 | 2007-10-24 13:13:21 +0000 | [diff] [blame] | 990 | /* One day we might have a debug section here like one in the |
| 991 | * IPv4 case above. Just ignore own messages at the moment. |
| 992 | */ |
| 993 | if (rtm->rtm_type != RTM_GET && rtm->rtm_pid == pid) |
| 994 | return; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 995 | struct prefix_ipv6 p; |
| 996 | unsigned int ifindex = 0; |
| 997 | |
| 998 | p.family = AF_INET6; |
| 999 | p.prefix = dest.sin6.sin6_addr; |
| 1000 | if (flags & RTF_HOST) |
| 1001 | p.prefixlen = IPV6_MAX_PREFIXLEN; |
| 1002 | else |
| 1003 | p.prefixlen = ip6_masklen (mask.sin6.sin6_addr); |
| 1004 | |
| 1005 | #ifdef KAME |
| 1006 | if (IN6_IS_ADDR_LINKLOCAL (&gate.sin6.sin6_addr)) |
| 1007 | { |
| 1008 | ifindex = IN6_LINKLOCAL_IFINDEX (gate.sin6.sin6_addr); |
| 1009 | SET_IN6_LINKLOCAL_IFINDEX (gate.sin6.sin6_addr, 0); |
| 1010 | } |
| 1011 | #endif /* KAME */ |
| 1012 | |
paul | ca16218 | 2005-09-12 16:58:52 +0000 | [diff] [blame] | 1013 | /* CHANGE: delete the old prefix, we have no further information |
| 1014 | * to specify the route really |
| 1015 | */ |
| 1016 | if (rtm->rtm_type == RTM_CHANGE) |
paul | 6621ca8 | 2005-11-23 13:02:08 +0000 | [diff] [blame] | 1017 | rib_delete_ipv6 (ZEBRA_ROUTE_KERNEL, zebra_flags, &p, |
Denis Ovsienko | 6eac79a | 2011-12-05 13:43:18 +0400 | [diff] [blame] | 1018 | NULL, 0, 0, SAFI_UNICAST); |
paul | ca16218 | 2005-09-12 16:58:52 +0000 | [diff] [blame] | 1019 | |
| 1020 | if (rtm->rtm_type == RTM_GET |
| 1021 | || rtm->rtm_type == RTM_ADD |
| 1022 | || rtm->rtm_type == RTM_CHANGE) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1023 | rib_add_ipv6 (ZEBRA_ROUTE_KERNEL, zebra_flags, |
Denis Ovsienko | 6eac79a | 2011-12-05 13:43:18 +0400 | [diff] [blame] | 1024 | &p, &gate.sin6.sin6_addr, ifindex, 0, 0, 0, SAFI_UNICAST); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1025 | else |
| 1026 | rib_delete_ipv6 (ZEBRA_ROUTE_KERNEL, zebra_flags, |
Denis Ovsienko | 6eac79a | 2011-12-05 13:43:18 +0400 | [diff] [blame] | 1027 | &p, &gate.sin6.sin6_addr, ifindex, 0, SAFI_UNICAST); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1028 | } |
| 1029 | #endif /* HAVE_IPV6 */ |
| 1030 | } |
| 1031 | |
| 1032 | /* Interface function for the kernel routing table updates. Support |
paul | 6621ca8 | 2005-11-23 13:02:08 +0000 | [diff] [blame] | 1033 | * for RTM_CHANGE will be needed. |
| 1034 | * Exported only for rt_socket.c |
| 1035 | */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1036 | int |
| 1037 | rtm_write (int message, |
| 1038 | union sockunion *dest, |
| 1039 | union sockunion *mask, |
| 1040 | union sockunion *gate, |
| 1041 | unsigned int index, |
| 1042 | int zebra_flags, |
| 1043 | int metric) |
| 1044 | { |
| 1045 | int ret; |
| 1046 | caddr_t pnt; |
| 1047 | struct interface *ifp; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1048 | |
| 1049 | /* Sequencial number of routing message. */ |
| 1050 | static int msg_seq = 0; |
| 1051 | |
| 1052 | /* Struct of rt_msghdr and buffer for storing socket's data. */ |
| 1053 | struct |
| 1054 | { |
| 1055 | struct rt_msghdr rtm; |
| 1056 | char buf[512]; |
| 1057 | } msg; |
| 1058 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1059 | if (routing_sock < 0) |
| 1060 | return ZEBRA_ERR_EPERM; |
| 1061 | |
| 1062 | /* Clear and set rt_msghdr values */ |
| 1063 | memset (&msg, 0, sizeof (struct rt_msghdr)); |
| 1064 | msg.rtm.rtm_version = RTM_VERSION; |
| 1065 | msg.rtm.rtm_type = message; |
| 1066 | msg.rtm.rtm_seq = msg_seq++; |
| 1067 | msg.rtm.rtm_addrs = RTA_DST; |
| 1068 | msg.rtm.rtm_addrs |= RTA_GATEWAY; |
| 1069 | msg.rtm.rtm_flags = RTF_UP; |
| 1070 | msg.rtm.rtm_index = index; |
| 1071 | |
| 1072 | if (metric != 0) |
| 1073 | { |
| 1074 | msg.rtm.rtm_rmx.rmx_hopcount = metric; |
| 1075 | msg.rtm.rtm_inits |= RTV_HOPCOUNT; |
| 1076 | } |
| 1077 | |
| 1078 | ifp = if_lookup_by_index (index); |
| 1079 | |
| 1080 | if (gate && message == RTM_ADD) |
| 1081 | msg.rtm.rtm_flags |= RTF_GATEWAY; |
| 1082 | |
David Ward | e6f148e | 2009-12-03 21:43:11 +0300 | [diff] [blame] | 1083 | /* When RTF_CLONING is unavailable on BSD, should we set some |
| 1084 | * other flag instead? |
| 1085 | */ |
| 1086 | #ifdef RTF_CLONING |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1087 | if (! gate && message == RTM_ADD && ifp && |
| 1088 | (ifp->flags & IFF_POINTOPOINT) == 0) |
| 1089 | msg.rtm.rtm_flags |= RTF_CLONING; |
David Ward | e6f148e | 2009-12-03 21:43:11 +0300 | [diff] [blame] | 1090 | #endif /* RTF_CLONING */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1091 | |
| 1092 | /* If no protocol specific gateway is specified, use link |
| 1093 | address for gateway. */ |
| 1094 | if (! gate) |
| 1095 | { |
| 1096 | if (!ifp) |
| 1097 | { |
Denis Ovsienko | dc95824 | 2007-08-13 16:03:06 +0000 | [diff] [blame] | 1098 | char dest_buf[INET_ADDRSTRLEN] = "NULL", mask_buf[INET_ADDRSTRLEN] = "255.255.255.255"; |
| 1099 | if (dest) |
| 1100 | inet_ntop (AF_INET, &dest->sin.sin_addr, dest_buf, INET_ADDRSTRLEN); |
| 1101 | if (mask) |
| 1102 | inet_ntop (AF_INET, &mask->sin.sin_addr, mask_buf, INET_ADDRSTRLEN); |
| 1103 | zlog_warn ("%s: %s/%s: gate == NULL and no gateway found for ifindex %d", |
| 1104 | __func__, dest_buf, mask_buf, index); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1105 | return -1; |
| 1106 | } |
| 1107 | gate = (union sockunion *) & ifp->sdl; |
| 1108 | } |
| 1109 | |
| 1110 | if (mask) |
| 1111 | msg.rtm.rtm_addrs |= RTA_NETMASK; |
| 1112 | else if (message == RTM_ADD) |
| 1113 | msg.rtm.rtm_flags |= RTF_HOST; |
| 1114 | |
| 1115 | /* Tagging route with flags */ |
| 1116 | msg.rtm.rtm_flags |= (RTF_PROTO1); |
| 1117 | |
| 1118 | /* Additional flags. */ |
| 1119 | if (zebra_flags & ZEBRA_FLAG_BLACKHOLE) |
| 1120 | msg.rtm.rtm_flags |= RTF_BLACKHOLE; |
hasso | 81dfcaa | 2003-05-25 19:21:25 +0000 | [diff] [blame] | 1121 | if (zebra_flags & ZEBRA_FLAG_REJECT) |
| 1122 | msg.rtm.rtm_flags |= RTF_REJECT; |
| 1123 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1124 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1125 | #define SOCKADDRSET(X,R) \ |
| 1126 | if (msg.rtm.rtm_addrs & (R)) \ |
| 1127 | { \ |
paul | 6fe70d1 | 2005-11-12 22:55:10 +0000 | [diff] [blame] | 1128 | int len = SAROUNDUP (X); \ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1129 | memcpy (pnt, (caddr_t)(X), len); \ |
| 1130 | pnt += len; \ |
| 1131 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1132 | |
| 1133 | pnt = (caddr_t) msg.buf; |
| 1134 | |
| 1135 | /* Write each socket data into rtm message buffer */ |
| 1136 | SOCKADDRSET (dest, RTA_DST); |
| 1137 | SOCKADDRSET (gate, RTA_GATEWAY); |
| 1138 | SOCKADDRSET (mask, RTA_NETMASK); |
| 1139 | |
| 1140 | msg.rtm.rtm_msglen = pnt - (caddr_t) &msg; |
| 1141 | |
| 1142 | ret = write (routing_sock, &msg, msg.rtm.rtm_msglen); |
| 1143 | |
| 1144 | if (ret != msg.rtm.rtm_msglen) |
| 1145 | { |
| 1146 | if (errno == EEXIST) |
| 1147 | return ZEBRA_ERR_RTEXIST; |
| 1148 | if (errno == ENETUNREACH) |
| 1149 | return ZEBRA_ERR_RTUNREACH; |
Denis Ovsienko | dc95824 | 2007-08-13 16:03:06 +0000 | [diff] [blame] | 1150 | if (errno == ESRCH) |
| 1151 | return ZEBRA_ERR_RTNOEXIST; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1152 | |
Denis Ovsienko | dc95824 | 2007-08-13 16:03:06 +0000 | [diff] [blame] | 1153 | zlog_warn ("%s: write : %s (%d)", __func__, safe_strerror (errno), errno); |
| 1154 | return ZEBRA_ERR_KERNEL; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1155 | } |
Denis Ovsienko | dc95824 | 2007-08-13 16:03:06 +0000 | [diff] [blame] | 1156 | return ZEBRA_ERR_NOERROR; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1157 | } |
| 1158 | |
David Lamparter | 6b0655a | 2014-06-04 06:53:35 +0200 | [diff] [blame] | 1159 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1160 | #include "thread.h" |
| 1161 | #include "zebra/zserv.h" |
| 1162 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1163 | /* For debug purpose. */ |
ajs | b617800 | 2004-12-07 21:12:56 +0000 | [diff] [blame] | 1164 | static void |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1165 | rtmsg_debug (struct rt_msghdr *rtm) |
| 1166 | { |
Denis Ovsienko | 2d84452 | 2007-09-14 11:31:55 +0000 | [diff] [blame] | 1167 | 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] | 1168 | rtm_flag_dump (rtm->rtm_flags); |
ajs | b617800 | 2004-12-07 21:12:56 +0000 | [diff] [blame] | 1169 | zlog_debug ("Kernel: message seq %d", rtm->rtm_seq); |
paul | 6fe70d1 | 2005-11-12 22:55:10 +0000 | [diff] [blame] | 1170 | 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] | 1171 | } |
| 1172 | |
| 1173 | /* This is pretty gross, better suggestions welcome -- mhandler */ |
| 1174 | #ifndef RTAX_MAX |
| 1175 | #ifdef RTA_NUMBITS |
| 1176 | #define RTAX_MAX RTA_NUMBITS |
| 1177 | #else |
| 1178 | #define RTAX_MAX 8 |
| 1179 | #endif /* RTA_NUMBITS */ |
| 1180 | #endif /* RTAX_MAX */ |
| 1181 | |
| 1182 | /* Kernel routing table and interface updates via routing socket. */ |
paul | 6621ca8 | 2005-11-23 13:02:08 +0000 | [diff] [blame] | 1183 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1184 | kernel_read (struct thread *thread) |
| 1185 | { |
| 1186 | int sock; |
| 1187 | int nbytes; |
| 1188 | struct rt_msghdr *rtm; |
| 1189 | |
gdt | dbee01f | 2004-01-06 00:36:51 +0000 | [diff] [blame] | 1190 | /* |
| 1191 | * This must be big enough for any message the kernel might send. |
gdt | b27900b | 2004-01-08 15:44:29 +0000 | [diff] [blame] | 1192 | * Rather than determining how many sockaddrs of what size might be |
| 1193 | * in each particular message, just use RTAX_MAX of sockaddr_storage |
| 1194 | * for each. Note that the sockaddrs must be after each message |
| 1195 | * definition, or rather after whichever happens to be the largest, |
| 1196 | * since the buffer needs to be big enough for a message and the |
| 1197 | * sockaddrs together. |
gdt | dbee01f | 2004-01-06 00:36:51 +0000 | [diff] [blame] | 1198 | */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1199 | union |
| 1200 | { |
| 1201 | /* Routing information. */ |
| 1202 | struct |
| 1203 | { |
| 1204 | struct rt_msghdr rtm; |
gdt | b27900b | 2004-01-08 15:44:29 +0000 | [diff] [blame] | 1205 | struct sockaddr_storage addr[RTAX_MAX]; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1206 | } r; |
| 1207 | |
| 1208 | /* Interface information. */ |
| 1209 | struct |
| 1210 | { |
| 1211 | struct if_msghdr ifm; |
gdt | b27900b | 2004-01-08 15:44:29 +0000 | [diff] [blame] | 1212 | struct sockaddr_storage addr[RTAX_MAX]; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1213 | } im; |
| 1214 | |
| 1215 | /* Interface address information. */ |
| 1216 | struct |
| 1217 | { |
| 1218 | struct ifa_msghdr ifa; |
gdt | b27900b | 2004-01-08 15:44:29 +0000 | [diff] [blame] | 1219 | struct sockaddr_storage addr[RTAX_MAX]; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1220 | } ia; |
| 1221 | |
| 1222 | #ifdef RTM_IFANNOUNCE |
| 1223 | /* Interface arrival/departure */ |
| 1224 | struct |
| 1225 | { |
| 1226 | struct if_announcemsghdr ifan; |
gdt | b27900b | 2004-01-08 15:44:29 +0000 | [diff] [blame] | 1227 | struct sockaddr_storage addr[RTAX_MAX]; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1228 | } ian; |
| 1229 | #endif /* RTM_IFANNOUNCE */ |
| 1230 | |
| 1231 | } buf; |
| 1232 | |
| 1233 | /* Fetch routing socket. */ |
| 1234 | sock = THREAD_FD (thread); |
| 1235 | |
| 1236 | nbytes= read (sock, &buf, sizeof buf); |
| 1237 | |
| 1238 | if (nbytes <= 0) |
| 1239 | { |
| 1240 | if (nbytes < 0 && errno != EWOULDBLOCK && errno != EAGAIN) |
ajs | 6099b3b | 2004-11-20 02:06:59 +0000 | [diff] [blame] | 1241 | zlog_warn ("routing socket error: %s", safe_strerror (errno)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1242 | return 0; |
| 1243 | } |
| 1244 | |
paul | 9bcdb63 | 2003-07-08 08:09:45 +0000 | [diff] [blame] | 1245 | thread_add_read (zebrad.master, kernel_read, NULL, sock); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1246 | |
hasso | 726f9b2 | 2003-05-25 21:04:54 +0000 | [diff] [blame] | 1247 | if (IS_ZEBRA_DEBUG_KERNEL) |
| 1248 | rtmsg_debug (&buf.r.rtm); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1249 | |
| 1250 | rtm = &buf.r.rtm; |
| 1251 | |
gdt | b27900b | 2004-01-08 15:44:29 +0000 | [diff] [blame] | 1252 | /* |
| 1253 | * Ensure that we didn't drop any data, so that processing routines |
| 1254 | * can assume they have the whole message. |
| 1255 | */ |
gdt | da26e3b | 2004-01-05 17:20:59 +0000 | [diff] [blame] | 1256 | if (rtm->rtm_msglen != nbytes) |
| 1257 | { |
| 1258 | zlog_warn ("kernel_read: rtm->rtm_msglen %d, nbytes %d, type %d\n", |
| 1259 | rtm->rtm_msglen, nbytes, rtm->rtm_type); |
| 1260 | return -1; |
| 1261 | } |
| 1262 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1263 | switch (rtm->rtm_type) |
| 1264 | { |
| 1265 | case RTM_ADD: |
| 1266 | case RTM_DELETE: |
paul | ca16218 | 2005-09-12 16:58:52 +0000 | [diff] [blame] | 1267 | case RTM_CHANGE: |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1268 | rtm_read (rtm); |
| 1269 | break; |
| 1270 | case RTM_IFINFO: |
| 1271 | ifm_read (&buf.im.ifm); |
| 1272 | break; |
| 1273 | case RTM_NEWADDR: |
| 1274 | case RTM_DELADDR: |
| 1275 | ifam_read (&buf.ia.ifa); |
| 1276 | break; |
| 1277 | #ifdef RTM_IFANNOUNCE |
| 1278 | case RTM_IFANNOUNCE: |
| 1279 | ifan_read (&buf.ian.ifan); |
| 1280 | break; |
| 1281 | #endif /* RTM_IFANNOUNCE */ |
| 1282 | default: |
hasso | 726f9b2 | 2003-05-25 21:04:54 +0000 | [diff] [blame] | 1283 | if (IS_ZEBRA_DEBUG_KERNEL) |
ajs | b617800 | 2004-12-07 21:12:56 +0000 | [diff] [blame] | 1284 | zlog_debug("Unprocessed RTM_type: %d", rtm->rtm_type); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1285 | break; |
| 1286 | } |
| 1287 | return 0; |
| 1288 | } |
| 1289 | |
| 1290 | /* Make routing socket. */ |
paul | 6621ca8 | 2005-11-23 13:02:08 +0000 | [diff] [blame] | 1291 | static void |
| 1292 | routing_socket (void) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1293 | { |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 1294 | if ( zserv_privs.change (ZPRIVS_RAISE) ) |
| 1295 | zlog_err ("routing_socket: Can't raise privileges"); |
| 1296 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1297 | routing_sock = socket (AF_ROUTE, SOCK_RAW, 0); |
| 1298 | |
| 1299 | if (routing_sock < 0) |
| 1300 | { |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 1301 | if ( zserv_privs.change (ZPRIVS_LOWER) ) |
| 1302 | zlog_err ("routing_socket: Can't lower privileges"); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1303 | zlog_warn ("Can't init kernel routing socket"); |
| 1304 | return; |
| 1305 | } |
| 1306 | |
paul | 865b852 | 2005-01-05 08:30:35 +0000 | [diff] [blame] | 1307 | /* XXX: Socket should be NONBLOCK, however as we currently |
| 1308 | * discard failed writes, this will lead to inconsistencies. |
| 1309 | * For now, socket must be blocking. |
| 1310 | */ |
| 1311 | /*if (fcntl (routing_sock, F_SETFL, O_NONBLOCK) < 0) |
| 1312 | zlog_warn ("Can't set O_NONBLOCK to routing socket");*/ |
| 1313 | |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 1314 | if ( zserv_privs.change (ZPRIVS_LOWER) ) |
| 1315 | zlog_err ("routing_socket: Can't lower privileges"); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1316 | |
| 1317 | /* kernel_read needs rewrite. */ |
paul | 9bcdb63 | 2003-07-08 08:09:45 +0000 | [diff] [blame] | 1318 | thread_add_read (zebrad.master, kernel_read, NULL, routing_sock); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1319 | } |
| 1320 | |
| 1321 | /* Exported interface function. This function simply calls |
| 1322 | routing_socket (). */ |
| 1323 | void |
paul | 6621ca8 | 2005-11-23 13:02:08 +0000 | [diff] [blame] | 1324 | kernel_init (void) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1325 | { |
| 1326 | routing_socket (); |
| 1327 | } |