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