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" |
| 39 | |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 40 | extern struct zebra_privs_t zserv_privs; |
paul | 9bcdb63 | 2003-07-08 08:09:45 +0000 | [diff] [blame] | 41 | extern struct zebra_t zebrad; |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 42 | |
gdt | 4bfbea8 | 2004-01-06 01:13:05 +0000 | [diff] [blame] | 43 | /* |
| 44 | * Given a sockaddr length, round it up to include pad bytes following |
| 45 | * it. Assumes the kernel pads to sizeof(long). |
| 46 | * |
| 47 | * XXX: why is ROUNDUP(0) sizeof(long)? 0 is an illegal sockaddr |
| 48 | * length anyway (< sizeof (struct sockaddr)), so this shouldn't |
| 49 | * matter. |
| 50 | */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 51 | #define ROUNDUP(a) \ |
| 52 | ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long)) |
| 53 | |
gdt | 4bfbea8 | 2004-01-06 01:13:05 +0000 | [diff] [blame] | 54 | /* |
| 55 | * Given a pointer (sockaddr or void *), return the number of bytes |
| 56 | * taken up by the sockaddr and any padding needed for alignment. |
| 57 | */ |
paul | 30be802 | 2003-10-22 02:51:38 +0000 | [diff] [blame] | 58 | #if defined(HAVE_SA_LEN) |
gdt | 4bfbea8 | 2004-01-06 01:13:05 +0000 | [diff] [blame] | 59 | #define SAROUNDUP(X) ROUNDUP(((struct sockaddr *)(X))->sa_len) |
paul | 30be802 | 2003-10-22 02:51:38 +0000 | [diff] [blame] | 60 | #elif defined(HAVE_IPV6) |
gdt | 4bfbea8 | 2004-01-06 01:13:05 +0000 | [diff] [blame] | 61 | /* |
| 62 | * One would hope all fixed-size structure definitions are aligned, |
| 63 | * but round them up nonetheless. |
| 64 | */ |
| 65 | #define SAROUNDUP(X) \ |
paul | 3e95a07 | 2003-09-24 00:05:45 +0000 | [diff] [blame] | 66 | (((struct sockaddr *)(X))->sa_family == AF_INET ? \ |
| 67 | ROUNDUP(sizeof(struct sockaddr_in)):\ |
| 68 | (((struct sockaddr *)(X))->sa_family == AF_INET6 ? \ |
| 69 | ROUNDUP(sizeof(struct sockaddr_in6)) : \ |
| 70 | (((struct sockaddr *)(X))->sa_family == AF_LINK ? \ |
paul | c50ae8b | 2004-05-11 11:31:07 +0000 | [diff] [blame] | 71 | ROUNDUP(sizeof(struct sockaddr_dl)) : sizeof(struct sockaddr)))) |
paul | 30be802 | 2003-10-22 02:51:38 +0000 | [diff] [blame] | 72 | #else /* HAVE_IPV6 */ |
gdt | 4bfbea8 | 2004-01-06 01:13:05 +0000 | [diff] [blame] | 73 | #define SAROUNDUP(X) \ |
paul | 30be802 | 2003-10-22 02:51:38 +0000 | [diff] [blame] | 74 | (((struct sockaddr *)(X))->sa_family == AF_INET ? \ |
| 75 | ROUNDUP(sizeof(struct sockaddr_in)):\ |
| 76 | (((struct sockaddr *)(X))->sa_family == AF_LINK ? \ |
| 77 | ROUNDUP(sizeof(struct sockaddr_dl)) : sizeof(struct sockaddr))) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 78 | #endif /* HAVE_SA_LEN */ |
| 79 | |
paul | 62debbb | 2005-06-14 14:07:07 +0000 | [diff] [blame] | 80 | #define RTA_ADDR_GET(DEST, RTA, RTMADDRS, PNT) \ |
| 81 | if ((RTMADDRS) & (RTA)) \ |
| 82 | { \ |
| 83 | int len = SAROUNDUP ((PNT)); \ |
paul | ea6f82b | 2005-06-28 17:20:26 +0000 | [diff] [blame] | 84 | if ( ((DEST) != NULL) && \ |
paul | 62debbb | 2005-06-14 14:07:07 +0000 | [diff] [blame] | 85 | af_check (((struct sockaddr *)(PNT))->sa_family)) \ |
| 86 | memcpy ((caddr_t)(DEST), (PNT), len); \ |
| 87 | (PNT) += len; \ |
| 88 | } |
| 89 | #define RTA_ATTR_GET(DEST, RTA, RTMADDRS, PNT) \ |
| 90 | if ((RTMADDRS) & (RTA)) \ |
| 91 | { \ |
| 92 | int len = SAROUNDUP ((PNT)); \ |
paul | ea6f82b | 2005-06-28 17:20:26 +0000 | [diff] [blame] | 93 | if ( ((DEST) != NULL) ) \ |
paul | 62debbb | 2005-06-14 14:07:07 +0000 | [diff] [blame] | 94 | memcpy ((caddr_t)(DEST), (PNT), len); \ |
| 95 | (PNT) += len; \ |
| 96 | } |
| 97 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 98 | /* Routing socket message types. */ |
| 99 | struct message rtm_type_str[] = |
| 100 | { |
| 101 | {RTM_ADD, "RTM_ADD"}, |
| 102 | {RTM_DELETE, "RTM_DELETE"}, |
| 103 | {RTM_CHANGE, "RTM_CHANGE"}, |
| 104 | {RTM_GET, "RTM_GET"}, |
| 105 | {RTM_LOSING, "RTM_LOSING"}, |
| 106 | {RTM_REDIRECT, "RTM_REDIRECT"}, |
| 107 | {RTM_MISS, "RTM_MISS"}, |
| 108 | {RTM_LOCK, "RTM_LOCK"}, |
| 109 | {RTM_OLDADD, "RTM_OLDADD"}, |
| 110 | {RTM_OLDDEL, "RTM_OLDDEL"}, |
| 111 | {RTM_RESOLVE, "RTM_RESOLVE"}, |
| 112 | {RTM_NEWADDR, "RTM_NEWADDR"}, |
| 113 | {RTM_DELADDR, "RTM_DELADDR"}, |
| 114 | {RTM_IFINFO, "RTM_IFINFO"}, |
| 115 | #ifdef RTM_OIFINFO |
| 116 | {RTM_OIFINFO, "RTM_OIFINFO"}, |
| 117 | #endif /* RTM_OIFINFO */ |
| 118 | #ifdef RTM_NEWMADDR |
| 119 | {RTM_NEWMADDR, "RTM_NEWMADDR"}, |
| 120 | #endif /* RTM_NEWMADDR */ |
| 121 | #ifdef RTM_DELMADDR |
| 122 | {RTM_DELMADDR, "RTM_DELMADDR"}, |
| 123 | #endif /* RTM_DELMADDR */ |
| 124 | #ifdef RTM_IFANNOUNCE |
| 125 | {RTM_IFANNOUNCE, "RTM_IFANNOUNCE"}, |
| 126 | #endif /* RTM_IFANNOUNCE */ |
| 127 | {0, NULL} |
| 128 | }; |
| 129 | |
| 130 | struct message rtm_flag_str[] = |
| 131 | { |
| 132 | {RTF_UP, "UP"}, |
| 133 | {RTF_GATEWAY, "GATEWAY"}, |
| 134 | {RTF_HOST, "HOST"}, |
| 135 | {RTF_REJECT, "REJECT"}, |
| 136 | {RTF_DYNAMIC, "DYNAMIC"}, |
| 137 | {RTF_MODIFIED, "MODIFIED"}, |
| 138 | {RTF_DONE, "DONE"}, |
| 139 | #ifdef RTF_MASK |
| 140 | {RTF_MASK, "MASK"}, |
| 141 | #endif /* RTF_MASK */ |
| 142 | {RTF_CLONING, "CLONING"}, |
| 143 | {RTF_XRESOLVE, "XRESOLVE"}, |
| 144 | {RTF_LLINFO, "LLINFO"}, |
| 145 | {RTF_STATIC, "STATIC"}, |
| 146 | {RTF_BLACKHOLE, "BLACKHOLE"}, |
| 147 | {RTF_PROTO1, "PROTO1"}, |
| 148 | {RTF_PROTO2, "PROTO2"}, |
| 149 | #ifdef RTF_PRCLONING |
| 150 | {RTF_PRCLONING, "PRCLONING"}, |
| 151 | #endif /* RTF_PRCLONING */ |
| 152 | #ifdef RTF_WASCLONED |
| 153 | {RTF_WASCLONED, "WASCLONED"}, |
| 154 | #endif /* RTF_WASCLONED */ |
| 155 | #ifdef RTF_PROTO3 |
| 156 | {RTF_PROTO3, "PROTO3"}, |
| 157 | #endif /* RTF_PROTO3 */ |
| 158 | #ifdef RTF_PINNED |
| 159 | {RTF_PINNED, "PINNED"}, |
| 160 | #endif /* RTF_PINNED */ |
| 161 | #ifdef RTF_LOCAL |
| 162 | {RTF_LOCAL, "LOCAL"}, |
| 163 | #endif /* RTF_LOCAL */ |
| 164 | #ifdef RTF_BROADCAST |
| 165 | {RTF_BROADCAST, "BROADCAST"}, |
| 166 | #endif /* RTF_BROADCAST */ |
| 167 | #ifdef RTF_MULTICAST |
| 168 | {RTF_MULTICAST, "MULTICAST"}, |
| 169 | #endif /* RTF_MULTICAST */ |
| 170 | {0, NULL} |
| 171 | }; |
| 172 | |
| 173 | /* Kernel routing update socket. */ |
| 174 | int routing_sock = -1; |
| 175 | |
| 176 | /* Yes I'm checking ugly routing socket behavior. */ |
| 177 | /* #define DEBUG */ |
| 178 | |
| 179 | /* Supported address family check. */ |
paul | 62debbb | 2005-06-14 14:07:07 +0000 | [diff] [blame] | 180 | static int inline |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 181 | af_check (int family) |
| 182 | { |
| 183 | if (family == AF_INET) |
| 184 | return 1; |
| 185 | #ifdef HAVE_IPV6 |
| 186 | if (family == AF_INET6) |
| 187 | return 1; |
| 188 | #endif /* HAVE_IPV6 */ |
| 189 | return 0; |
| 190 | } |
| 191 | |
| 192 | /* Dump routing table flag for debug purpose. */ |
ajs | b617800 | 2004-12-07 21:12:56 +0000 | [diff] [blame] | 193 | static void |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 194 | rtm_flag_dump (int flag) |
| 195 | { |
| 196 | struct message *mes; |
| 197 | static char buf[BUFSIZ]; |
| 198 | |
gdt | cced60d | 2004-07-13 16:45:54 +0000 | [diff] [blame] | 199 | buf[0] = '\0'; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 200 | for (mes = rtm_flag_str; mes->key != 0; mes++) |
| 201 | { |
| 202 | if (mes->key & flag) |
| 203 | { |
| 204 | strlcat (buf, mes->str, BUFSIZ); |
| 205 | strlcat (buf, " ", BUFSIZ); |
| 206 | } |
| 207 | } |
ajs | b617800 | 2004-12-07 21:12:56 +0000 | [diff] [blame] | 208 | zlog_debug ("Kernel: %s", buf); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | #ifdef RTM_IFANNOUNCE |
| 212 | /* Interface adding function */ |
| 213 | int |
| 214 | ifan_read (struct if_announcemsghdr *ifan) |
| 215 | { |
| 216 | struct interface *ifp; |
| 217 | |
| 218 | ifp = if_lookup_by_index (ifan->ifan_index); |
| 219 | if (ifp == NULL && ifan->ifan_what == IFAN_ARRIVAL) |
| 220 | { |
| 221 | /* Create Interface */ |
ajs | 08dbfb6 | 2005-04-03 03:40:52 +0000 | [diff] [blame] | 222 | ifp = if_get_by_name_len(ifan->ifan_name, |
| 223 | strnlen(ifan->ifan_name, |
| 224 | sizeof(ifan->ifan_name))); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 225 | ifp->ifindex = ifan->ifan_index; |
| 226 | |
| 227 | if_add_update (ifp); |
| 228 | } |
| 229 | else if (ifp != NULL && ifan->ifan_what == IFAN_DEPARTURE) |
paul | 6eb8827 | 2005-07-29 14:36:00 +0000 | [diff] [blame] | 230 | if_delete_update (ifp); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 231 | |
| 232 | if_get_flags (ifp); |
| 233 | if_get_mtu (ifp); |
| 234 | if_get_metric (ifp); |
| 235 | |
| 236 | if (IS_ZEBRA_DEBUG_KERNEL) |
ajs | b617800 | 2004-12-07 21:12:56 +0000 | [diff] [blame] | 237 | zlog_debug ("interface %s index %d", ifp->name, ifp->ifindex); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 238 | |
| 239 | return 0; |
| 240 | } |
| 241 | #endif /* RTM_IFANNOUNCE */ |
| 242 | |
gdt | da26e3b | 2004-01-05 17:20:59 +0000 | [diff] [blame] | 243 | /* |
| 244 | * Handle struct if_msghdr obtained from reading routing socket or |
| 245 | * sysctl (from interface_list). There may or may not be sockaddrs |
| 246 | * present after the header. |
| 247 | */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 248 | int |
| 249 | ifm_read (struct if_msghdr *ifm) |
| 250 | { |
paul | 3e95a07 | 2003-09-24 00:05:45 +0000 | [diff] [blame] | 251 | struct interface *ifp = NULL; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 252 | struct sockaddr_dl *sdl = NULL; |
gdt | 4bfbea8 | 2004-01-06 01:13:05 +0000 | [diff] [blame] | 253 | void *cp; |
| 254 | unsigned int i; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 255 | |
gdt | da26e3b | 2004-01-05 17:20:59 +0000 | [diff] [blame] | 256 | /* paranoia: sanity check structure */ |
| 257 | if (ifm->ifm_msglen < sizeof(struct if_msghdr)) |
| 258 | { |
| 259 | zlog_err ("ifm_read: ifm->ifm_msglen %d too short\n", |
| 260 | ifm->ifm_msglen); |
| 261 | return -1; |
| 262 | } |
| 263 | |
| 264 | /* |
gdt | 4bfbea8 | 2004-01-06 01:13:05 +0000 | [diff] [blame] | 265 | * Check for a sockaddr_dl following the message. First, point to |
| 266 | * where a socakddr might be if one follows the message. |
gdt | da26e3b | 2004-01-05 17:20:59 +0000 | [diff] [blame] | 267 | */ |
gdt | 4bfbea8 | 2004-01-06 01:13:05 +0000 | [diff] [blame] | 268 | cp = (void *)(ifm + 1); |
| 269 | |
paul | 3e95a07 | 2003-09-24 00:05:45 +0000 | [diff] [blame] | 270 | #ifdef SUNOS_5 |
paul | 3e95a07 | 2003-09-24 00:05:45 +0000 | [diff] [blame] | 271 | /* |
gdt | 4bfbea8 | 2004-01-06 01:13:05 +0000 | [diff] [blame] | 272 | * XXX This behavior should be narrowed to only the kernel versions |
| 273 | * for which the structures returned do not match the headers. |
| 274 | * |
paul | 3e95a07 | 2003-09-24 00:05:45 +0000 | [diff] [blame] | 275 | * if_msghdr_t on 64 bit kernels in Solaris 9 and earlier versions |
gdt | 4bfbea8 | 2004-01-06 01:13:05 +0000 | [diff] [blame] | 276 | * is 12 bytes larger than the 32 bit version. |
paul | 3e95a07 | 2003-09-24 00:05:45 +0000 | [diff] [blame] | 277 | */ |
gdt | 4bfbea8 | 2004-01-06 01:13:05 +0000 | [diff] [blame] | 278 | if (((struct sockaddr *) cp)->sa_family == AF_UNSPEC) |
paul | 3e95a07 | 2003-09-24 00:05:45 +0000 | [diff] [blame] | 279 | cp = cp + 12; |
paul | 3e95a07 | 2003-09-24 00:05:45 +0000 | [diff] [blame] | 280 | #endif |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 281 | |
gdt | 4bfbea8 | 2004-01-06 01:13:05 +0000 | [diff] [blame] | 282 | /* |
| 283 | * Check for each sockaddr in turn, advancing over it. After this |
| 284 | * loop, sdl should point to a sockaddr_dl iff one was present. |
| 285 | */ |
| 286 | for (i = 1; i != 0; i <<= 1) |
| 287 | { |
| 288 | if (i & ifm->ifm_addrs) |
| 289 | { |
| 290 | if (i == RTA_IFP) |
| 291 | { |
| 292 | sdl = (struct sockaddr_dl *)cp; |
| 293 | break; |
| 294 | } |
gdt | 6a250b0 | 2004-12-09 14:48:12 +0000 | [diff] [blame] | 295 | /* XXX warning: pointer of type `void *' used in arithmetic */ |
gdt | 4bfbea8 | 2004-01-06 01:13:05 +0000 | [diff] [blame] | 296 | cp += SAROUNDUP(cp); |
| 297 | } |
| 298 | } |
gdt | da26e3b | 2004-01-05 17:20:59 +0000 | [diff] [blame] | 299 | |
gdt | 4bfbea8 | 2004-01-06 01:13:05 +0000 | [diff] [blame] | 300 | /* Ensure that sdl, if present, is actually a sockaddr_dl. */ |
| 301 | if (sdl != NULL && sdl->sdl_family != AF_LINK) |
| 302 | { |
| 303 | zlog_err ("ifm_read: sockaddr_dl bad AF %d\n", |
| 304 | sdl->sdl_family); |
| 305 | return -1; |
| 306 | } |
gdt | da26e3b | 2004-01-05 17:20:59 +0000 | [diff] [blame] | 307 | |
| 308 | /* |
gdt | 4bfbea8 | 2004-01-06 01:13:05 +0000 | [diff] [blame] | 309 | * Look up on ifindex first, because ifindices are the primary |
| 310 | * handle for interfaces across the user/kernel boundary. (Some |
| 311 | * messages, such as up/down status changes on NetBSD, do not |
| 312 | * include a sockaddr_dl). |
gdt | da26e3b | 2004-01-05 17:20:59 +0000 | [diff] [blame] | 313 | */ |
gdt | 4bfbea8 | 2004-01-06 01:13:05 +0000 | [diff] [blame] | 314 | ifp = if_lookup_by_index (ifm->ifm_index); |
gdt | da26e3b | 2004-01-05 17:20:59 +0000 | [diff] [blame] | 315 | |
gdt | da26e3b | 2004-01-05 17:20:59 +0000 | [diff] [blame] | 316 | /* |
| 317 | * If lookup by index was unsuccessful and we have a name, try |
| 318 | * looking up by name. Interfaces specified in the configuration |
| 319 | * file for which the ifindex has not been determined will have |
ajs | d2fc889 | 2005-04-02 18:38:43 +0000 | [diff] [blame] | 320 | * ifindex == IFINDEX_INTERNAL, and such interfaces are found by this search, |
| 321 | * and then their ifindex values can be filled in. |
gdt | da26e3b | 2004-01-05 17:20:59 +0000 | [diff] [blame] | 322 | */ |
gdt | cb42c03 | 2004-01-05 17:55:46 +0000 | [diff] [blame] | 323 | if (ifp == NULL && sdl != NULL) |
paul | 3e95a07 | 2003-09-24 00:05:45 +0000 | [diff] [blame] | 324 | { |
gdt | da26e3b | 2004-01-05 17:20:59 +0000 | [diff] [blame] | 325 | /* |
| 326 | * paranoia: sanity check name length. nlen does not include |
| 327 | * trailing zero, but IFNAMSIZ max length does. |
ajs | a349198 | 2005-04-02 22:50:38 +0000 | [diff] [blame] | 328 | * |
| 329 | * XXX Is this test correct? Should it be '>=' or '>'? And is it even |
| 330 | * necessary now that we are using if_lookup_by_name_len? |
gdt | da26e3b | 2004-01-05 17:20:59 +0000 | [diff] [blame] | 331 | */ |
| 332 | if (sdl->sdl_nlen >= IFNAMSIZ) |
| 333 | { |
| 334 | zlog_err ("ifm_read: illegal sdl_nlen %d\n", sdl->sdl_nlen); |
| 335 | return -1; |
| 336 | } |
| 337 | |
ajs | a349198 | 2005-04-02 22:50:38 +0000 | [diff] [blame] | 338 | ifp = if_lookup_by_name_len (sdl->sdl_data, sdl->sdl_nlen); |
paul | 3e95a07 | 2003-09-24 00:05:45 +0000 | [diff] [blame] | 339 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 340 | |
gdt | da26e3b | 2004-01-05 17:20:59 +0000 | [diff] [blame] | 341 | /* |
ajs | d2fc889 | 2005-04-02 18:38:43 +0000 | [diff] [blame] | 342 | * If ifp does not exist or has an invalid index (IFINDEX_INTERNAL), create or |
gdt | da26e3b | 2004-01-05 17:20:59 +0000 | [diff] [blame] | 343 | * fill in an interface. |
| 344 | */ |
ajs | d2fc889 | 2005-04-02 18:38:43 +0000 | [diff] [blame] | 345 | if ((ifp == NULL) || (ifp->ifindex == IFINDEX_INTERNAL)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 346 | { |
gdt | 4bfbea8 | 2004-01-06 01:13:05 +0000 | [diff] [blame] | 347 | /* |
| 348 | * To create or fill in an interface, a sockaddr_dl (via |
| 349 | * RTA_IFP) is required. |
| 350 | */ |
| 351 | if (sdl == NULL) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 352 | { |
gdt | 4bfbea8 | 2004-01-06 01:13:05 +0000 | [diff] [blame] | 353 | zlog_warn ("Interface index %d (new) missing RTA_IFP sockaddr_dl\n", |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 354 | ifm->ifm_index); |
| 355 | return -1; |
| 356 | } |
| 357 | |
paul | 3e95a07 | 2003-09-24 00:05:45 +0000 | [diff] [blame] | 358 | if (ifp == NULL) |
gdt | 4bfbea8 | 2004-01-06 01:13:05 +0000 | [diff] [blame] | 359 | /* Interface that zebra was not previously aware of, so create. */ |
paul | 3e95a07 | 2003-09-24 00:05:45 +0000 | [diff] [blame] | 360 | ifp = if_create (sdl->sdl_data, sdl->sdl_nlen); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 361 | |
gdt | 4bfbea8 | 2004-01-06 01:13:05 +0000 | [diff] [blame] | 362 | /* |
| 363 | * Fill in newly created interface structure, or larval |
ajs | d2fc889 | 2005-04-02 18:38:43 +0000 | [diff] [blame] | 364 | * structure with ifindex IFINDEX_INTERNAL. |
gdt | 4bfbea8 | 2004-01-06 01:13:05 +0000 | [diff] [blame] | 365 | */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 366 | ifp->ifindex = ifm->ifm_index; |
| 367 | ifp->flags = ifm->ifm_flags; |
| 368 | #if defined(__bsdi__) |
| 369 | if_kvm_get_mtu (ifp); |
| 370 | #else |
| 371 | if_get_mtu (ifp); |
| 372 | #endif /* __bsdi__ */ |
| 373 | if_get_metric (ifp); |
| 374 | |
gdt | 4bfbea8 | 2004-01-06 01:13:05 +0000 | [diff] [blame] | 375 | /* |
| 376 | * XXX sockaddr_dl contents can be larger than the structure |
| 377 | * definition, so the user of the stored structure must be |
| 378 | * careful not to read off the end. |
| 379 | */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 380 | memcpy (&ifp->sdl, sdl, sizeof (struct sockaddr_dl)); |
| 381 | |
| 382 | if_add_update (ifp); |
| 383 | } |
| 384 | else |
gdt | da26e3b | 2004-01-05 17:20:59 +0000 | [diff] [blame] | 385 | /* |
| 386 | * Interface structure exists. Adjust stored flags from |
| 387 | * notification. If interface has up->down or down->up |
| 388 | * transition, call state change routines (to adjust routes, |
| 389 | * notify routing daemons, etc.). (Other flag changes are stored |
| 390 | * but apparently do not trigger action.) |
| 391 | */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 392 | { |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 393 | if (if_is_up (ifp)) |
| 394 | { |
| 395 | ifp->flags = ifm->ifm_flags; |
| 396 | if (! if_is_up (ifp)) |
paul | 6eb8827 | 2005-07-29 14:36:00 +0000 | [diff] [blame] | 397 | { |
| 398 | if_down (ifp); |
| 399 | #ifndef RTM_IFANNOUNCE |
| 400 | /* No RTM_IFANNOUNCE on this platform, so we can never |
| 401 | * distinguish between down and delete. We must presume |
| 402 | * it has been deleted. |
| 403 | * Eg, Solaris will not notify us of unplumb. |
| 404 | * |
| 405 | * XXX: Fixme - this should be runtime detected |
| 406 | * So that a binary compiled on a system with IFANNOUNCE |
| 407 | * will still behave correctly if run on a platform without |
| 408 | */ |
| 409 | if_delete_update (ifp); |
| 410 | #endif /* RTM_IFANNOUNCE */ |
| 411 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 412 | } |
| 413 | else |
| 414 | { |
| 415 | ifp->flags = ifm->ifm_flags; |
| 416 | if (if_is_up (ifp)) |
| 417 | if_up (ifp); |
| 418 | } |
| 419 | } |
| 420 | |
| 421 | #ifdef HAVE_NET_RT_IFLIST |
| 422 | ifp->stats = ifm->ifm_data; |
| 423 | #endif /* HAVE_NET_RT_IFLIST */ |
| 424 | |
| 425 | if (IS_ZEBRA_DEBUG_KERNEL) |
ajs | b617800 | 2004-12-07 21:12:56 +0000 | [diff] [blame] | 426 | zlog_debug ("interface %s index %d", ifp->name, ifp->ifindex); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 427 | |
| 428 | return 0; |
| 429 | } |
| 430 | |
| 431 | /* Address read from struct ifa_msghdr. */ |
| 432 | void |
| 433 | ifam_read_mesg (struct ifa_msghdr *ifm, |
| 434 | union sockunion *addr, |
| 435 | union sockunion *mask, |
| 436 | union sockunion *dest) |
| 437 | { |
| 438 | caddr_t pnt, end; |
| 439 | |
| 440 | pnt = (caddr_t)(ifm + 1); |
| 441 | end = ((caddr_t)ifm) + ifm->ifam_msglen; |
| 442 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 443 | /* Be sure structure is cleared */ |
| 444 | memset (mask, 0, sizeof (union sockunion)); |
| 445 | memset (addr, 0, sizeof (union sockunion)); |
| 446 | memset (dest, 0, sizeof (union sockunion)); |
| 447 | |
| 448 | /* We fetch each socket variable into sockunion. */ |
paul | 62debbb | 2005-06-14 14:07:07 +0000 | [diff] [blame] | 449 | RTA_ADDR_GET (NULL, RTA_DST, ifm->ifam_addrs, pnt); |
| 450 | RTA_ADDR_GET (NULL, RTA_GATEWAY, ifm->ifam_addrs, pnt); |
| 451 | RTA_ATTR_GET (mask, RTA_NETMASK, ifm->ifam_addrs, pnt); |
| 452 | RTA_ADDR_GET (NULL, RTA_GENMASK, ifm->ifam_addrs, pnt); |
| 453 | RTA_ADDR_GET (NULL, RTA_IFP, ifm->ifam_addrs, pnt); |
| 454 | RTA_ADDR_GET (addr, RTA_IFA, ifm->ifam_addrs, pnt); |
| 455 | RTA_ADDR_GET (NULL, RTA_AUTHOR, ifm->ifam_addrs, pnt); |
| 456 | RTA_ADDR_GET (dest, RTA_BRD, ifm->ifam_addrs, pnt); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 457 | |
| 458 | /* Assert read up end point matches to end point */ |
| 459 | if (pnt != end) |
| 460 | zlog_warn ("ifam_read() does't read all socket data"); |
| 461 | } |
| 462 | |
| 463 | /* Interface's address information get. */ |
| 464 | int |
| 465 | ifam_read (struct ifa_msghdr *ifam) |
| 466 | { |
| 467 | struct interface *ifp; |
| 468 | union sockunion addr, mask, gate; |
| 469 | |
| 470 | /* Check does this interface exist or not. */ |
| 471 | ifp = if_lookup_by_index (ifam->ifam_index); |
| 472 | if (ifp == NULL) |
| 473 | { |
| 474 | zlog_warn ("no interface for index %d", ifam->ifam_index); |
| 475 | return -1; |
| 476 | } |
| 477 | |
| 478 | /* Allocate and read address information. */ |
| 479 | ifam_read_mesg (ifam, &addr, &mask, &gate); |
| 480 | |
| 481 | /* Check interface flag for implicit up of the interface. */ |
| 482 | if_refresh (ifp); |
| 483 | |
| 484 | /* Add connected address. */ |
| 485 | switch (sockunion_family (&addr)) |
| 486 | { |
| 487 | case AF_INET: |
| 488 | if (ifam->ifam_type == RTM_NEWADDR) |
| 489 | connected_add_ipv4 (ifp, 0, &addr.sin.sin_addr, |
| 490 | ip_masklen (mask.sin.sin_addr), |
| 491 | &gate.sin.sin_addr, NULL); |
| 492 | else |
| 493 | connected_delete_ipv4 (ifp, 0, &addr.sin.sin_addr, |
| 494 | ip_masklen (mask.sin.sin_addr), |
| 495 | &gate.sin.sin_addr, NULL); |
| 496 | break; |
| 497 | #ifdef HAVE_IPV6 |
| 498 | case AF_INET6: |
| 499 | /* Unset interface index from link-local address when IPv6 stack |
| 500 | is KAME. */ |
| 501 | if (IN6_IS_ADDR_LINKLOCAL (&addr.sin6.sin6_addr)) |
| 502 | SET_IN6_LINKLOCAL_IFINDEX (addr.sin6.sin6_addr, 0); |
| 503 | |
| 504 | if (ifam->ifam_type == RTM_NEWADDR) |
| 505 | connected_add_ipv6 (ifp, |
| 506 | &addr.sin6.sin6_addr, |
| 507 | ip6_masklen (mask.sin6.sin6_addr), |
| 508 | &gate.sin6.sin6_addr); |
| 509 | else |
| 510 | connected_delete_ipv6 (ifp, |
| 511 | &addr.sin6.sin6_addr, |
| 512 | ip6_masklen (mask.sin6.sin6_addr), |
| 513 | &gate.sin6.sin6_addr); |
| 514 | break; |
| 515 | #endif /* HAVE_IPV6 */ |
| 516 | default: |
| 517 | /* Unsupported family silently ignore... */ |
| 518 | break; |
| 519 | } |
| 520 | return 0; |
| 521 | } |
| 522 | |
| 523 | /* Interface function for reading kernel routing table information. */ |
| 524 | int |
| 525 | rtm_read_mesg (struct rt_msghdr *rtm, |
| 526 | union sockunion *dest, |
| 527 | union sockunion *mask, |
| 528 | union sockunion *gate) |
| 529 | { |
| 530 | caddr_t pnt, end; |
| 531 | |
| 532 | /* Pnt points out socket data start point. */ |
| 533 | pnt = (caddr_t)(rtm + 1); |
| 534 | end = ((caddr_t)rtm) + rtm->rtm_msglen; |
| 535 | |
| 536 | /* rt_msghdr version check. */ |
| 537 | if (rtm->rtm_version != RTM_VERSION) |
| 538 | zlog (NULL, LOG_WARNING, |
| 539 | "Routing message version different %d should be %d." |
| 540 | "This may cause problem\n", rtm->rtm_version, RTM_VERSION); |
paul | 62debbb | 2005-06-14 14:07:07 +0000 | [diff] [blame] | 541 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 542 | /* Be sure structure is cleared */ |
| 543 | memset (dest, 0, sizeof (union sockunion)); |
| 544 | memset (gate, 0, sizeof (union sockunion)); |
| 545 | memset (mask, 0, sizeof (union sockunion)); |
| 546 | |
| 547 | /* We fetch each socket variable into sockunion. */ |
paul | 62debbb | 2005-06-14 14:07:07 +0000 | [diff] [blame] | 548 | RTA_ADDR_GET (dest, RTA_DST, rtm->rtm_addrs, pnt); |
| 549 | RTA_ADDR_GET (gate, RTA_GATEWAY, rtm->rtm_addrs, pnt); |
| 550 | RTA_ATTR_GET (mask, RTA_NETMASK, rtm->rtm_addrs, pnt); |
| 551 | RTA_ADDR_GET (NULL, RTA_GENMASK, rtm->rtm_addrs, pnt); |
| 552 | RTA_ADDR_GET (NULL, RTA_IFP, rtm->rtm_addrs, pnt); |
| 553 | RTA_ADDR_GET (NULL, RTA_IFA, rtm->rtm_addrs, pnt); |
| 554 | RTA_ADDR_GET (NULL, RTA_AUTHOR, rtm->rtm_addrs, pnt); |
| 555 | RTA_ADDR_GET (NULL, RTA_BRD, rtm->rtm_addrs, pnt); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 556 | |
| 557 | /* If there is netmask information set it's family same as |
| 558 | destination family*/ |
| 559 | if (rtm->rtm_addrs & RTA_NETMASK) |
| 560 | mask->sa.sa_family = dest->sa.sa_family; |
| 561 | |
| 562 | /* Assert read up to the end of pointer. */ |
| 563 | if (pnt != end) |
| 564 | zlog (NULL, LOG_WARNING, "rtm_read() does't read all socket data."); |
| 565 | |
| 566 | return rtm->rtm_flags; |
| 567 | } |
| 568 | |
| 569 | void |
| 570 | rtm_read (struct rt_msghdr *rtm) |
| 571 | { |
| 572 | int flags; |
| 573 | u_char zebra_flags; |
| 574 | union sockunion dest, mask, gate; |
| 575 | |
| 576 | zebra_flags = 0; |
| 577 | |
| 578 | /* Discard self send message. */ |
| 579 | if (rtm->rtm_type != RTM_GET |
| 580 | && (rtm->rtm_pid == pid || rtm->rtm_pid == old_pid)) |
| 581 | return; |
| 582 | |
| 583 | /* Read destination and netmask and gateway from rtm message |
| 584 | structure. */ |
| 585 | flags = rtm_read_mesg (rtm, &dest, &mask, &gate); |
| 586 | |
| 587 | #ifdef RTF_CLONED /*bsdi, netbsd 1.6*/ |
| 588 | if (flags & RTF_CLONED) |
| 589 | return; |
| 590 | #endif |
| 591 | #ifdef RTF_WASCLONED /*freebsd*/ |
| 592 | if (flags & RTF_WASCLONED) |
| 593 | return; |
| 594 | #endif |
| 595 | |
| 596 | if ((rtm->rtm_type == RTM_ADD) && ! (flags & RTF_UP)) |
| 597 | return; |
| 598 | |
| 599 | /* This is connected route. */ |
| 600 | if (! (flags & RTF_GATEWAY)) |
| 601 | return; |
| 602 | |
| 603 | if (flags & RTF_PROTO1) |
| 604 | SET_FLAG (zebra_flags, ZEBRA_FLAG_SELFROUTE); |
| 605 | |
| 606 | /* This is persistent route. */ |
| 607 | if (flags & RTF_STATIC) |
| 608 | SET_FLAG (zebra_flags, ZEBRA_FLAG_STATIC); |
| 609 | |
hasso | 81dfcaa | 2003-05-25 19:21:25 +0000 | [diff] [blame] | 610 | /* This is a reject or blackhole route */ |
| 611 | if (flags & RTF_REJECT) |
| 612 | SET_FLAG (zebra_flags, ZEBRA_FLAG_REJECT); |
| 613 | if (flags & RTF_BLACKHOLE) |
| 614 | SET_FLAG (zebra_flags, ZEBRA_FLAG_BLACKHOLE); |
| 615 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 616 | if (dest.sa.sa_family == AF_INET) |
| 617 | { |
| 618 | struct prefix_ipv4 p; |
| 619 | |
| 620 | p.family = AF_INET; |
| 621 | p.prefix = dest.sin.sin_addr; |
| 622 | if (flags & RTF_HOST) |
| 623 | p.prefixlen = IPV4_MAX_PREFIXLEN; |
| 624 | else |
| 625 | p.prefixlen = ip_masklen (mask.sin.sin_addr); |
| 626 | |
| 627 | if (rtm->rtm_type == RTM_GET || rtm->rtm_type == RTM_ADD) |
| 628 | rib_add_ipv4 (ZEBRA_ROUTE_KERNEL, zebra_flags, |
| 629 | &p, &gate.sin.sin_addr, 0, 0, 0, 0); |
| 630 | else |
| 631 | rib_delete_ipv4 (ZEBRA_ROUTE_KERNEL, zebra_flags, |
| 632 | &p, &gate.sin.sin_addr, 0, 0); |
| 633 | } |
| 634 | #ifdef HAVE_IPV6 |
| 635 | if (dest.sa.sa_family == AF_INET6) |
| 636 | { |
| 637 | struct prefix_ipv6 p; |
| 638 | unsigned int ifindex = 0; |
| 639 | |
| 640 | p.family = AF_INET6; |
| 641 | p.prefix = dest.sin6.sin6_addr; |
| 642 | if (flags & RTF_HOST) |
| 643 | p.prefixlen = IPV6_MAX_PREFIXLEN; |
| 644 | else |
| 645 | p.prefixlen = ip6_masklen (mask.sin6.sin6_addr); |
| 646 | |
| 647 | #ifdef KAME |
| 648 | if (IN6_IS_ADDR_LINKLOCAL (&gate.sin6.sin6_addr)) |
| 649 | { |
| 650 | ifindex = IN6_LINKLOCAL_IFINDEX (gate.sin6.sin6_addr); |
| 651 | SET_IN6_LINKLOCAL_IFINDEX (gate.sin6.sin6_addr, 0); |
| 652 | } |
| 653 | #endif /* KAME */ |
| 654 | |
| 655 | if (rtm->rtm_type == RTM_GET || rtm->rtm_type == RTM_ADD) |
| 656 | rib_add_ipv6 (ZEBRA_ROUTE_KERNEL, zebra_flags, |
hasso | be61c4e | 2005-08-27 06:05:47 +0000 | [diff] [blame^] | 657 | &p, &gate.sin6.sin6_addr, ifindex, 0, 0, 0); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 658 | else |
| 659 | rib_delete_ipv6 (ZEBRA_ROUTE_KERNEL, zebra_flags, |
| 660 | &p, &gate.sin6.sin6_addr, ifindex, 0); |
| 661 | } |
| 662 | #endif /* HAVE_IPV6 */ |
| 663 | } |
| 664 | |
| 665 | /* Interface function for the kernel routing table updates. Support |
| 666 | for RTM_CHANGE will be needed. */ |
| 667 | int |
| 668 | rtm_write (int message, |
| 669 | union sockunion *dest, |
| 670 | union sockunion *mask, |
| 671 | union sockunion *gate, |
| 672 | unsigned int index, |
| 673 | int zebra_flags, |
| 674 | int metric) |
| 675 | { |
| 676 | int ret; |
| 677 | caddr_t pnt; |
| 678 | struct interface *ifp; |
| 679 | struct sockaddr_in tmp_gate; |
| 680 | #ifdef HAVE_IPV6 |
| 681 | struct sockaddr_in6 tmp_gate6; |
| 682 | #endif /* HAVE_IPV6 */ |
| 683 | |
| 684 | /* Sequencial number of routing message. */ |
| 685 | static int msg_seq = 0; |
| 686 | |
| 687 | /* Struct of rt_msghdr and buffer for storing socket's data. */ |
| 688 | struct |
| 689 | { |
| 690 | struct rt_msghdr rtm; |
| 691 | char buf[512]; |
| 692 | } msg; |
| 693 | |
| 694 | memset (&tmp_gate, 0, sizeof (struct sockaddr_in)); |
| 695 | tmp_gate.sin_family = AF_INET; |
| 696 | #ifdef HAVE_SIN_LEN |
| 697 | tmp_gate.sin_len = sizeof (struct sockaddr_in); |
| 698 | #endif /* HAVE_SIN_LEN */ |
| 699 | |
| 700 | #ifdef HAVE_IPV6 |
| 701 | memset (&tmp_gate6, 0, sizeof (struct sockaddr_in6)); |
| 702 | tmp_gate6.sin6_family = AF_INET6; |
| 703 | #ifdef SIN6_LEN |
| 704 | tmp_gate6.sin6_len = sizeof (struct sockaddr_in6); |
| 705 | #endif /* SIN6_LEN */ |
| 706 | #endif /* HAVE_IPV6 */ |
| 707 | |
| 708 | if (routing_sock < 0) |
| 709 | return ZEBRA_ERR_EPERM; |
| 710 | |
| 711 | /* Clear and set rt_msghdr values */ |
| 712 | memset (&msg, 0, sizeof (struct rt_msghdr)); |
| 713 | msg.rtm.rtm_version = RTM_VERSION; |
| 714 | msg.rtm.rtm_type = message; |
| 715 | msg.rtm.rtm_seq = msg_seq++; |
| 716 | msg.rtm.rtm_addrs = RTA_DST; |
| 717 | msg.rtm.rtm_addrs |= RTA_GATEWAY; |
| 718 | msg.rtm.rtm_flags = RTF_UP; |
| 719 | msg.rtm.rtm_index = index; |
| 720 | |
| 721 | if (metric != 0) |
| 722 | { |
| 723 | msg.rtm.rtm_rmx.rmx_hopcount = metric; |
| 724 | msg.rtm.rtm_inits |= RTV_HOPCOUNT; |
| 725 | } |
| 726 | |
| 727 | ifp = if_lookup_by_index (index); |
| 728 | |
| 729 | if (gate && message == RTM_ADD) |
| 730 | msg.rtm.rtm_flags |= RTF_GATEWAY; |
| 731 | |
| 732 | if (! gate && message == RTM_ADD && ifp && |
| 733 | (ifp->flags & IFF_POINTOPOINT) == 0) |
| 734 | msg.rtm.rtm_flags |= RTF_CLONING; |
| 735 | |
| 736 | /* If no protocol specific gateway is specified, use link |
| 737 | address for gateway. */ |
| 738 | if (! gate) |
| 739 | { |
| 740 | if (!ifp) |
| 741 | { |
| 742 | zlog_warn ("no gateway found for interface index %d", index); |
| 743 | return -1; |
| 744 | } |
| 745 | gate = (union sockunion *) & ifp->sdl; |
| 746 | } |
| 747 | |
| 748 | if (mask) |
| 749 | msg.rtm.rtm_addrs |= RTA_NETMASK; |
| 750 | else if (message == RTM_ADD) |
| 751 | msg.rtm.rtm_flags |= RTF_HOST; |
| 752 | |
| 753 | /* Tagging route with flags */ |
| 754 | msg.rtm.rtm_flags |= (RTF_PROTO1); |
| 755 | |
| 756 | /* Additional flags. */ |
| 757 | if (zebra_flags & ZEBRA_FLAG_BLACKHOLE) |
| 758 | msg.rtm.rtm_flags |= RTF_BLACKHOLE; |
hasso | 81dfcaa | 2003-05-25 19:21:25 +0000 | [diff] [blame] | 759 | if (zebra_flags & ZEBRA_FLAG_REJECT) |
| 760 | msg.rtm.rtm_flags |= RTF_REJECT; |
| 761 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 762 | |
| 763 | #ifdef HAVE_SIN_LEN |
| 764 | #define SOCKADDRSET(X,R) \ |
| 765 | if (msg.rtm.rtm_addrs & (R)) \ |
| 766 | { \ |
| 767 | int len = ROUNDUP ((X)->sa.sa_len); \ |
| 768 | memcpy (pnt, (caddr_t)(X), len); \ |
| 769 | pnt += len; \ |
| 770 | } |
| 771 | #else |
| 772 | #define SOCKADDRSET(X,R) \ |
| 773 | if (msg.rtm.rtm_addrs & (R)) \ |
| 774 | { \ |
| 775 | int len = ROUNDUP (sizeof((X)->sa)); \ |
| 776 | memcpy (pnt, (caddr_t)(X), len); \ |
| 777 | pnt += len; \ |
| 778 | } |
| 779 | #endif /* HAVE_SIN_LEN */ |
| 780 | |
| 781 | pnt = (caddr_t) msg.buf; |
| 782 | |
| 783 | /* Write each socket data into rtm message buffer */ |
| 784 | SOCKADDRSET (dest, RTA_DST); |
| 785 | SOCKADDRSET (gate, RTA_GATEWAY); |
| 786 | SOCKADDRSET (mask, RTA_NETMASK); |
| 787 | |
| 788 | msg.rtm.rtm_msglen = pnt - (caddr_t) &msg; |
| 789 | |
| 790 | ret = write (routing_sock, &msg, msg.rtm.rtm_msglen); |
| 791 | |
| 792 | if (ret != msg.rtm.rtm_msglen) |
| 793 | { |
| 794 | if (errno == EEXIST) |
| 795 | return ZEBRA_ERR_RTEXIST; |
| 796 | if (errno == ENETUNREACH) |
| 797 | return ZEBRA_ERR_RTUNREACH; |
| 798 | |
ajs | 6099b3b | 2004-11-20 02:06:59 +0000 | [diff] [blame] | 799 | zlog_warn ("write : %s (%d)", safe_strerror (errno), errno); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 800 | return -1; |
| 801 | } |
| 802 | return 0; |
| 803 | } |
| 804 | |
| 805 | |
| 806 | #include "thread.h" |
| 807 | #include "zebra/zserv.h" |
| 808 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 809 | /* For debug purpose. */ |
ajs | b617800 | 2004-12-07 21:12:56 +0000 | [diff] [blame] | 810 | static void |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 811 | rtmsg_debug (struct rt_msghdr *rtm) |
| 812 | { |
gdt | 6a250b0 | 2004-12-09 14:48:12 +0000 | [diff] [blame] | 813 | const char *type = "Unknown"; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 814 | struct message *mes; |
| 815 | |
| 816 | for (mes = rtm_type_str; mes->str; mes++) |
| 817 | if (mes->key == rtm->rtm_type) |
| 818 | { |
| 819 | type = mes->str; |
| 820 | break; |
| 821 | } |
| 822 | |
ajs | b617800 | 2004-12-07 21:12:56 +0000 | [diff] [blame] | 823 | zlog_debug ("Kernel: Len: %d Type: %s", rtm->rtm_msglen, type); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 824 | rtm_flag_dump (rtm->rtm_flags); |
ajs | b617800 | 2004-12-07 21:12:56 +0000 | [diff] [blame] | 825 | zlog_debug ("Kernel: message seq %d", rtm->rtm_seq); |
| 826 | zlog_debug ("Kernel: pid %d", rtm->rtm_pid); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 827 | } |
| 828 | |
| 829 | /* This is pretty gross, better suggestions welcome -- mhandler */ |
| 830 | #ifndef RTAX_MAX |
| 831 | #ifdef RTA_NUMBITS |
| 832 | #define RTAX_MAX RTA_NUMBITS |
| 833 | #else |
| 834 | #define RTAX_MAX 8 |
| 835 | #endif /* RTA_NUMBITS */ |
| 836 | #endif /* RTAX_MAX */ |
| 837 | |
| 838 | /* Kernel routing table and interface updates via routing socket. */ |
| 839 | int |
| 840 | kernel_read (struct thread *thread) |
| 841 | { |
| 842 | int sock; |
| 843 | int nbytes; |
| 844 | struct rt_msghdr *rtm; |
| 845 | |
gdt | dbee01f | 2004-01-06 00:36:51 +0000 | [diff] [blame] | 846 | /* |
| 847 | * This must be big enough for any message the kernel might send. |
gdt | b27900b | 2004-01-08 15:44:29 +0000 | [diff] [blame] | 848 | * Rather than determining how many sockaddrs of what size might be |
| 849 | * in each particular message, just use RTAX_MAX of sockaddr_storage |
| 850 | * for each. Note that the sockaddrs must be after each message |
| 851 | * definition, or rather after whichever happens to be the largest, |
| 852 | * since the buffer needs to be big enough for a message and the |
| 853 | * sockaddrs together. |
gdt | dbee01f | 2004-01-06 00:36:51 +0000 | [diff] [blame] | 854 | */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 855 | union |
| 856 | { |
| 857 | /* Routing information. */ |
| 858 | struct |
| 859 | { |
| 860 | struct rt_msghdr rtm; |
gdt | b27900b | 2004-01-08 15:44:29 +0000 | [diff] [blame] | 861 | struct sockaddr_storage addr[RTAX_MAX]; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 862 | } r; |
| 863 | |
| 864 | /* Interface information. */ |
| 865 | struct |
| 866 | { |
| 867 | struct if_msghdr ifm; |
gdt | b27900b | 2004-01-08 15:44:29 +0000 | [diff] [blame] | 868 | struct sockaddr_storage addr[RTAX_MAX]; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 869 | } im; |
| 870 | |
| 871 | /* Interface address information. */ |
| 872 | struct |
| 873 | { |
| 874 | struct ifa_msghdr ifa; |
gdt | b27900b | 2004-01-08 15:44:29 +0000 | [diff] [blame] | 875 | struct sockaddr_storage addr[RTAX_MAX]; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 876 | } ia; |
| 877 | |
| 878 | #ifdef RTM_IFANNOUNCE |
| 879 | /* Interface arrival/departure */ |
| 880 | struct |
| 881 | { |
| 882 | struct if_announcemsghdr ifan; |
gdt | b27900b | 2004-01-08 15:44:29 +0000 | [diff] [blame] | 883 | struct sockaddr_storage addr[RTAX_MAX]; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 884 | } ian; |
| 885 | #endif /* RTM_IFANNOUNCE */ |
| 886 | |
| 887 | } buf; |
| 888 | |
| 889 | /* Fetch routing socket. */ |
| 890 | sock = THREAD_FD (thread); |
| 891 | |
| 892 | nbytes= read (sock, &buf, sizeof buf); |
| 893 | |
| 894 | if (nbytes <= 0) |
| 895 | { |
| 896 | if (nbytes < 0 && errno != EWOULDBLOCK && errno != EAGAIN) |
ajs | 6099b3b | 2004-11-20 02:06:59 +0000 | [diff] [blame] | 897 | zlog_warn ("routing socket error: %s", safe_strerror (errno)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 898 | return 0; |
| 899 | } |
| 900 | |
paul | 9bcdb63 | 2003-07-08 08:09:45 +0000 | [diff] [blame] | 901 | thread_add_read (zebrad.master, kernel_read, NULL, sock); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 902 | |
hasso | 726f9b2 | 2003-05-25 21:04:54 +0000 | [diff] [blame] | 903 | if (IS_ZEBRA_DEBUG_KERNEL) |
| 904 | rtmsg_debug (&buf.r.rtm); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 905 | |
| 906 | rtm = &buf.r.rtm; |
| 907 | |
gdt | b27900b | 2004-01-08 15:44:29 +0000 | [diff] [blame] | 908 | /* |
| 909 | * Ensure that we didn't drop any data, so that processing routines |
| 910 | * can assume they have the whole message. |
| 911 | */ |
gdt | da26e3b | 2004-01-05 17:20:59 +0000 | [diff] [blame] | 912 | if (rtm->rtm_msglen != nbytes) |
| 913 | { |
| 914 | zlog_warn ("kernel_read: rtm->rtm_msglen %d, nbytes %d, type %d\n", |
| 915 | rtm->rtm_msglen, nbytes, rtm->rtm_type); |
| 916 | return -1; |
| 917 | } |
| 918 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 919 | switch (rtm->rtm_type) |
| 920 | { |
| 921 | case RTM_ADD: |
| 922 | case RTM_DELETE: |
| 923 | rtm_read (rtm); |
| 924 | break; |
| 925 | case RTM_IFINFO: |
| 926 | ifm_read (&buf.im.ifm); |
| 927 | break; |
| 928 | case RTM_NEWADDR: |
| 929 | case RTM_DELADDR: |
| 930 | ifam_read (&buf.ia.ifa); |
| 931 | break; |
| 932 | #ifdef RTM_IFANNOUNCE |
| 933 | case RTM_IFANNOUNCE: |
| 934 | ifan_read (&buf.ian.ifan); |
| 935 | break; |
| 936 | #endif /* RTM_IFANNOUNCE */ |
| 937 | default: |
hasso | 726f9b2 | 2003-05-25 21:04:54 +0000 | [diff] [blame] | 938 | if (IS_ZEBRA_DEBUG_KERNEL) |
ajs | b617800 | 2004-12-07 21:12:56 +0000 | [diff] [blame] | 939 | zlog_debug("Unprocessed RTM_type: %d", rtm->rtm_type); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 940 | break; |
| 941 | } |
| 942 | return 0; |
| 943 | } |
| 944 | |
| 945 | /* Make routing socket. */ |
| 946 | void |
| 947 | routing_socket () |
| 948 | { |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 949 | if ( zserv_privs.change (ZPRIVS_RAISE) ) |
| 950 | zlog_err ("routing_socket: Can't raise privileges"); |
| 951 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 952 | routing_sock = socket (AF_ROUTE, SOCK_RAW, 0); |
| 953 | |
| 954 | if (routing_sock < 0) |
| 955 | { |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 956 | if ( zserv_privs.change (ZPRIVS_LOWER) ) |
| 957 | zlog_err ("routing_socket: Can't lower privileges"); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 958 | zlog_warn ("Can't init kernel routing socket"); |
| 959 | return; |
| 960 | } |
| 961 | |
paul | 865b852 | 2005-01-05 08:30:35 +0000 | [diff] [blame] | 962 | /* XXX: Socket should be NONBLOCK, however as we currently |
| 963 | * discard failed writes, this will lead to inconsistencies. |
| 964 | * For now, socket must be blocking. |
| 965 | */ |
| 966 | /*if (fcntl (routing_sock, F_SETFL, O_NONBLOCK) < 0) |
| 967 | zlog_warn ("Can't set O_NONBLOCK to routing socket");*/ |
| 968 | |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 969 | if ( zserv_privs.change (ZPRIVS_LOWER) ) |
| 970 | zlog_err ("routing_socket: Can't lower privileges"); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 971 | |
| 972 | /* kernel_read needs rewrite. */ |
paul | 9bcdb63 | 2003-07-08 08:09:45 +0000 | [diff] [blame] | 973 | thread_add_read (zebrad.master, kernel_read, NULL, routing_sock); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 974 | } |
| 975 | |
| 976 | /* Exported interface function. This function simply calls |
| 977 | routing_socket (). */ |
| 978 | void |
| 979 | kernel_init () |
| 980 | { |
| 981 | routing_socket (); |
| 982 | } |