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