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; |
| 41 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 42 | /* Socket length roundup function. */ |
| 43 | #define ROUNDUP(a) \ |
| 44 | ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long)) |
| 45 | |
| 46 | /* And this macro is wrapper for handling sa_len. */ |
| 47 | #ifdef HAVE_SA_LEN |
| 48 | #define WRAPUP(X) ROUNDUP(((struct sockaddr *)(X))->sa_len) |
| 49 | #else |
| 50 | #define WRAPUP(X) ROUNDUP(sizeof (struct sockaddr)) |
| 51 | #endif /* HAVE_SA_LEN */ |
| 52 | |
| 53 | /* Routing socket message types. */ |
| 54 | struct message rtm_type_str[] = |
| 55 | { |
| 56 | {RTM_ADD, "RTM_ADD"}, |
| 57 | {RTM_DELETE, "RTM_DELETE"}, |
| 58 | {RTM_CHANGE, "RTM_CHANGE"}, |
| 59 | {RTM_GET, "RTM_GET"}, |
| 60 | {RTM_LOSING, "RTM_LOSING"}, |
| 61 | {RTM_REDIRECT, "RTM_REDIRECT"}, |
| 62 | {RTM_MISS, "RTM_MISS"}, |
| 63 | {RTM_LOCK, "RTM_LOCK"}, |
| 64 | {RTM_OLDADD, "RTM_OLDADD"}, |
| 65 | {RTM_OLDDEL, "RTM_OLDDEL"}, |
| 66 | {RTM_RESOLVE, "RTM_RESOLVE"}, |
| 67 | {RTM_NEWADDR, "RTM_NEWADDR"}, |
| 68 | {RTM_DELADDR, "RTM_DELADDR"}, |
| 69 | {RTM_IFINFO, "RTM_IFINFO"}, |
| 70 | #ifdef RTM_OIFINFO |
| 71 | {RTM_OIFINFO, "RTM_OIFINFO"}, |
| 72 | #endif /* RTM_OIFINFO */ |
| 73 | #ifdef RTM_NEWMADDR |
| 74 | {RTM_NEWMADDR, "RTM_NEWMADDR"}, |
| 75 | #endif /* RTM_NEWMADDR */ |
| 76 | #ifdef RTM_DELMADDR |
| 77 | {RTM_DELMADDR, "RTM_DELMADDR"}, |
| 78 | #endif /* RTM_DELMADDR */ |
| 79 | #ifdef RTM_IFANNOUNCE |
| 80 | {RTM_IFANNOUNCE, "RTM_IFANNOUNCE"}, |
| 81 | #endif /* RTM_IFANNOUNCE */ |
| 82 | {0, NULL} |
| 83 | }; |
| 84 | |
| 85 | struct message rtm_flag_str[] = |
| 86 | { |
| 87 | {RTF_UP, "UP"}, |
| 88 | {RTF_GATEWAY, "GATEWAY"}, |
| 89 | {RTF_HOST, "HOST"}, |
| 90 | {RTF_REJECT, "REJECT"}, |
| 91 | {RTF_DYNAMIC, "DYNAMIC"}, |
| 92 | {RTF_MODIFIED, "MODIFIED"}, |
| 93 | {RTF_DONE, "DONE"}, |
| 94 | #ifdef RTF_MASK |
| 95 | {RTF_MASK, "MASK"}, |
| 96 | #endif /* RTF_MASK */ |
| 97 | {RTF_CLONING, "CLONING"}, |
| 98 | {RTF_XRESOLVE, "XRESOLVE"}, |
| 99 | {RTF_LLINFO, "LLINFO"}, |
| 100 | {RTF_STATIC, "STATIC"}, |
| 101 | {RTF_BLACKHOLE, "BLACKHOLE"}, |
| 102 | {RTF_PROTO1, "PROTO1"}, |
| 103 | {RTF_PROTO2, "PROTO2"}, |
| 104 | #ifdef RTF_PRCLONING |
| 105 | {RTF_PRCLONING, "PRCLONING"}, |
| 106 | #endif /* RTF_PRCLONING */ |
| 107 | #ifdef RTF_WASCLONED |
| 108 | {RTF_WASCLONED, "WASCLONED"}, |
| 109 | #endif /* RTF_WASCLONED */ |
| 110 | #ifdef RTF_PROTO3 |
| 111 | {RTF_PROTO3, "PROTO3"}, |
| 112 | #endif /* RTF_PROTO3 */ |
| 113 | #ifdef RTF_PINNED |
| 114 | {RTF_PINNED, "PINNED"}, |
| 115 | #endif /* RTF_PINNED */ |
| 116 | #ifdef RTF_LOCAL |
| 117 | {RTF_LOCAL, "LOCAL"}, |
| 118 | #endif /* RTF_LOCAL */ |
| 119 | #ifdef RTF_BROADCAST |
| 120 | {RTF_BROADCAST, "BROADCAST"}, |
| 121 | #endif /* RTF_BROADCAST */ |
| 122 | #ifdef RTF_MULTICAST |
| 123 | {RTF_MULTICAST, "MULTICAST"}, |
| 124 | #endif /* RTF_MULTICAST */ |
| 125 | {0, NULL} |
| 126 | }; |
| 127 | |
| 128 | /* Kernel routing update socket. */ |
| 129 | int routing_sock = -1; |
| 130 | |
| 131 | /* Yes I'm checking ugly routing socket behavior. */ |
| 132 | /* #define DEBUG */ |
| 133 | |
| 134 | /* Supported address family check. */ |
| 135 | static int |
| 136 | af_check (int family) |
| 137 | { |
| 138 | if (family == AF_INET) |
| 139 | return 1; |
| 140 | #ifdef HAVE_IPV6 |
| 141 | if (family == AF_INET6) |
| 142 | return 1; |
| 143 | #endif /* HAVE_IPV6 */ |
| 144 | return 0; |
| 145 | } |
| 146 | |
| 147 | /* Dump routing table flag for debug purpose. */ |
| 148 | void |
| 149 | rtm_flag_dump (int flag) |
| 150 | { |
| 151 | struct message *mes; |
| 152 | static char buf[BUFSIZ]; |
| 153 | |
hasso | 81dfcaa | 2003-05-25 19:21:25 +0000 | [diff] [blame] | 154 | buf[0] = '0'; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 155 | for (mes = rtm_flag_str; mes->key != 0; mes++) |
| 156 | { |
| 157 | if (mes->key & flag) |
| 158 | { |
| 159 | strlcat (buf, mes->str, BUFSIZ); |
| 160 | strlcat (buf, " ", BUFSIZ); |
| 161 | } |
| 162 | } |
| 163 | zlog_info ("Kernel: %s", buf); |
| 164 | } |
| 165 | |
| 166 | #ifdef RTM_IFANNOUNCE |
| 167 | /* Interface adding function */ |
| 168 | int |
| 169 | ifan_read (struct if_announcemsghdr *ifan) |
| 170 | { |
| 171 | struct interface *ifp; |
| 172 | |
| 173 | ifp = if_lookup_by_index (ifan->ifan_index); |
| 174 | if (ifp == NULL && ifan->ifan_what == IFAN_ARRIVAL) |
| 175 | { |
| 176 | /* Create Interface */ |
| 177 | ifp = if_get_by_name (ifan->ifan_name); |
| 178 | ifp->ifindex = ifan->ifan_index; |
| 179 | |
| 180 | if_add_update (ifp); |
| 181 | } |
| 182 | else if (ifp != NULL && ifan->ifan_what == IFAN_DEPARTURE) |
| 183 | { |
| 184 | if_delete_update (ifp); |
| 185 | if_delete (ifp); |
| 186 | } |
| 187 | |
| 188 | if_get_flags (ifp); |
| 189 | if_get_mtu (ifp); |
| 190 | if_get_metric (ifp); |
| 191 | |
| 192 | if (IS_ZEBRA_DEBUG_KERNEL) |
| 193 | zlog_info ("interface %s index %d", ifp->name, ifp->ifindex); |
| 194 | |
| 195 | return 0; |
| 196 | } |
| 197 | #endif /* RTM_IFANNOUNCE */ |
| 198 | |
| 199 | /* Interface adding function called from interface_list. */ |
| 200 | int |
| 201 | ifm_read (struct if_msghdr *ifm) |
| 202 | { |
| 203 | struct interface *ifp; |
| 204 | struct sockaddr_dl *sdl = NULL; |
| 205 | |
| 206 | sdl = (struct sockaddr_dl *)(ifm + 1); |
| 207 | |
| 208 | /* Use sdl index. */ |
| 209 | ifp = if_lookup_by_index (ifm->ifm_index); |
| 210 | |
| 211 | if (ifp == NULL) |
| 212 | { |
| 213 | /* Check interface's address.*/ |
| 214 | if (! (ifm->ifm_addrs & RTA_IFP)) |
| 215 | { |
| 216 | zlog_warn ("There must be RTA_IFP address for ifindex %d\n", |
| 217 | ifm->ifm_index); |
| 218 | return -1; |
| 219 | } |
| 220 | |
paul | 592c814 | 2003-06-06 23:24:55 +0000 | [diff] [blame^] | 221 | ifp = if_create (); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 222 | |
paul | 592c814 | 2003-06-06 23:24:55 +0000 | [diff] [blame^] | 223 | strncpy (ifp->name, sdl->sdl_data, sdl->sdl_nlen); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 224 | ifp->ifindex = ifm->ifm_index; |
| 225 | ifp->flags = ifm->ifm_flags; |
| 226 | #if defined(__bsdi__) |
| 227 | if_kvm_get_mtu (ifp); |
| 228 | #else |
| 229 | if_get_mtu (ifp); |
| 230 | #endif /* __bsdi__ */ |
| 231 | if_get_metric (ifp); |
| 232 | |
| 233 | /* Fetch hardware address. */ |
| 234 | if (sdl->sdl_family != AF_LINK) |
| 235 | { |
| 236 | zlog_warn ("sockaddr_dl->sdl_family is not AF_LINK"); |
| 237 | return -1; |
| 238 | } |
| 239 | memcpy (&ifp->sdl, sdl, sizeof (struct sockaddr_dl)); |
| 240 | |
| 241 | if_add_update (ifp); |
| 242 | } |
| 243 | else |
| 244 | { |
| 245 | /* There is a case of promisc, allmulti flag modification. */ |
| 246 | if (if_is_up (ifp)) |
| 247 | { |
| 248 | ifp->flags = ifm->ifm_flags; |
| 249 | if (! if_is_up (ifp)) |
| 250 | if_down (ifp); |
| 251 | } |
| 252 | else |
| 253 | { |
| 254 | ifp->flags = ifm->ifm_flags; |
| 255 | if (if_is_up (ifp)) |
| 256 | if_up (ifp); |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | #ifdef HAVE_NET_RT_IFLIST |
| 261 | ifp->stats = ifm->ifm_data; |
| 262 | #endif /* HAVE_NET_RT_IFLIST */ |
| 263 | |
| 264 | if (IS_ZEBRA_DEBUG_KERNEL) |
| 265 | zlog_info ("interface %s index %d", ifp->name, ifp->ifindex); |
| 266 | |
| 267 | return 0; |
| 268 | } |
| 269 | |
| 270 | /* Address read from struct ifa_msghdr. */ |
| 271 | void |
| 272 | ifam_read_mesg (struct ifa_msghdr *ifm, |
| 273 | union sockunion *addr, |
| 274 | union sockunion *mask, |
| 275 | union sockunion *dest) |
| 276 | { |
| 277 | caddr_t pnt, end; |
| 278 | |
| 279 | pnt = (caddr_t)(ifm + 1); |
| 280 | end = ((caddr_t)ifm) + ifm->ifam_msglen; |
| 281 | |
| 282 | #define IFAMADDRGET(X,R) \ |
| 283 | if (ifm->ifam_addrs & (R)) \ |
| 284 | { \ |
| 285 | int len = WRAPUP(pnt); \ |
| 286 | if (((X) != NULL) && af_check (((struct sockaddr *)pnt)->sa_family)) \ |
| 287 | memcpy ((caddr_t)(X), pnt, len); \ |
| 288 | pnt += len; \ |
| 289 | } |
| 290 | #define IFAMMASKGET(X,R) \ |
| 291 | if (ifm->ifam_addrs & (R)) \ |
| 292 | { \ |
| 293 | int len = WRAPUP(pnt); \ |
| 294 | if ((X) != NULL) \ |
| 295 | memcpy ((caddr_t)(X), pnt, len); \ |
| 296 | pnt += len; \ |
| 297 | } |
| 298 | |
| 299 | /* Be sure structure is cleared */ |
| 300 | memset (mask, 0, sizeof (union sockunion)); |
| 301 | memset (addr, 0, sizeof (union sockunion)); |
| 302 | memset (dest, 0, sizeof (union sockunion)); |
| 303 | |
| 304 | /* We fetch each socket variable into sockunion. */ |
| 305 | IFAMADDRGET (NULL, RTA_DST); |
| 306 | IFAMADDRGET (NULL, RTA_GATEWAY); |
| 307 | IFAMMASKGET (mask, RTA_NETMASK); |
| 308 | IFAMADDRGET (NULL, RTA_GENMASK); |
| 309 | IFAMADDRGET (NULL, RTA_IFP); |
| 310 | IFAMADDRGET (addr, RTA_IFA); |
| 311 | IFAMADDRGET (NULL, RTA_AUTHOR); |
| 312 | IFAMADDRGET (dest, RTA_BRD); |
| 313 | |
| 314 | /* Assert read up end point matches to end point */ |
| 315 | if (pnt != end) |
| 316 | zlog_warn ("ifam_read() does't read all socket data"); |
| 317 | } |
| 318 | |
| 319 | /* Interface's address information get. */ |
| 320 | int |
| 321 | ifam_read (struct ifa_msghdr *ifam) |
| 322 | { |
| 323 | struct interface *ifp; |
| 324 | union sockunion addr, mask, gate; |
| 325 | |
| 326 | /* Check does this interface exist or not. */ |
| 327 | ifp = if_lookup_by_index (ifam->ifam_index); |
| 328 | if (ifp == NULL) |
| 329 | { |
| 330 | zlog_warn ("no interface for index %d", ifam->ifam_index); |
| 331 | return -1; |
| 332 | } |
| 333 | |
| 334 | /* Allocate and read address information. */ |
| 335 | ifam_read_mesg (ifam, &addr, &mask, &gate); |
| 336 | |
| 337 | /* Check interface flag for implicit up of the interface. */ |
| 338 | if_refresh (ifp); |
| 339 | |
| 340 | /* Add connected address. */ |
| 341 | switch (sockunion_family (&addr)) |
| 342 | { |
| 343 | case AF_INET: |
| 344 | if (ifam->ifam_type == RTM_NEWADDR) |
| 345 | connected_add_ipv4 (ifp, 0, &addr.sin.sin_addr, |
| 346 | ip_masklen (mask.sin.sin_addr), |
| 347 | &gate.sin.sin_addr, NULL); |
| 348 | else |
| 349 | connected_delete_ipv4 (ifp, 0, &addr.sin.sin_addr, |
| 350 | ip_masklen (mask.sin.sin_addr), |
| 351 | &gate.sin.sin_addr, NULL); |
| 352 | break; |
| 353 | #ifdef HAVE_IPV6 |
| 354 | case AF_INET6: |
| 355 | /* Unset interface index from link-local address when IPv6 stack |
| 356 | is KAME. */ |
| 357 | if (IN6_IS_ADDR_LINKLOCAL (&addr.sin6.sin6_addr)) |
| 358 | SET_IN6_LINKLOCAL_IFINDEX (addr.sin6.sin6_addr, 0); |
| 359 | |
| 360 | if (ifam->ifam_type == RTM_NEWADDR) |
| 361 | connected_add_ipv6 (ifp, |
| 362 | &addr.sin6.sin6_addr, |
| 363 | ip6_masklen (mask.sin6.sin6_addr), |
| 364 | &gate.sin6.sin6_addr); |
| 365 | else |
| 366 | connected_delete_ipv6 (ifp, |
| 367 | &addr.sin6.sin6_addr, |
| 368 | ip6_masklen (mask.sin6.sin6_addr), |
| 369 | &gate.sin6.sin6_addr); |
| 370 | break; |
| 371 | #endif /* HAVE_IPV6 */ |
| 372 | default: |
| 373 | /* Unsupported family silently ignore... */ |
| 374 | break; |
| 375 | } |
| 376 | return 0; |
| 377 | } |
| 378 | |
| 379 | /* Interface function for reading kernel routing table information. */ |
| 380 | int |
| 381 | rtm_read_mesg (struct rt_msghdr *rtm, |
| 382 | union sockunion *dest, |
| 383 | union sockunion *mask, |
| 384 | union sockunion *gate) |
| 385 | { |
| 386 | caddr_t pnt, end; |
| 387 | |
| 388 | /* Pnt points out socket data start point. */ |
| 389 | pnt = (caddr_t)(rtm + 1); |
| 390 | end = ((caddr_t)rtm) + rtm->rtm_msglen; |
| 391 | |
| 392 | /* rt_msghdr version check. */ |
| 393 | if (rtm->rtm_version != RTM_VERSION) |
| 394 | zlog (NULL, LOG_WARNING, |
| 395 | "Routing message version different %d should be %d." |
| 396 | "This may cause problem\n", rtm->rtm_version, RTM_VERSION); |
| 397 | |
| 398 | #define RTMADDRGET(X,R) \ |
| 399 | if (rtm->rtm_addrs & (R)) \ |
| 400 | { \ |
| 401 | int len = WRAPUP (pnt); \ |
| 402 | if (((X) != NULL) && af_check (((struct sockaddr *)pnt)->sa_family)) \ |
| 403 | memcpy ((caddr_t)(X), pnt, len); \ |
| 404 | pnt += len; \ |
| 405 | } |
| 406 | #define RTMMASKGET(X,R) \ |
| 407 | if (rtm->rtm_addrs & (R)) \ |
| 408 | { \ |
| 409 | int len = WRAPUP (pnt); \ |
| 410 | if ((X) != NULL) \ |
| 411 | memcpy ((caddr_t)(X), pnt, len); \ |
| 412 | pnt += len; \ |
| 413 | } |
| 414 | |
| 415 | /* Be sure structure is cleared */ |
| 416 | memset (dest, 0, sizeof (union sockunion)); |
| 417 | memset (gate, 0, sizeof (union sockunion)); |
| 418 | memset (mask, 0, sizeof (union sockunion)); |
| 419 | |
| 420 | /* We fetch each socket variable into sockunion. */ |
| 421 | RTMADDRGET (dest, RTA_DST); |
| 422 | RTMADDRGET (gate, RTA_GATEWAY); |
| 423 | RTMMASKGET (mask, RTA_NETMASK); |
| 424 | RTMADDRGET (NULL, RTA_GENMASK); |
| 425 | RTMADDRGET (NULL, RTA_IFP); |
| 426 | RTMADDRGET (NULL, RTA_IFA); |
| 427 | RTMADDRGET (NULL, RTA_AUTHOR); |
| 428 | RTMADDRGET (NULL, RTA_BRD); |
| 429 | |
| 430 | /* If there is netmask information set it's family same as |
| 431 | destination family*/ |
| 432 | if (rtm->rtm_addrs & RTA_NETMASK) |
| 433 | mask->sa.sa_family = dest->sa.sa_family; |
| 434 | |
| 435 | /* Assert read up to the end of pointer. */ |
| 436 | if (pnt != end) |
| 437 | zlog (NULL, LOG_WARNING, "rtm_read() does't read all socket data."); |
| 438 | |
| 439 | return rtm->rtm_flags; |
| 440 | } |
| 441 | |
| 442 | void |
| 443 | rtm_read (struct rt_msghdr *rtm) |
| 444 | { |
| 445 | int flags; |
| 446 | u_char zebra_flags; |
| 447 | union sockunion dest, mask, gate; |
| 448 | |
| 449 | zebra_flags = 0; |
| 450 | |
| 451 | /* Discard self send message. */ |
| 452 | if (rtm->rtm_type != RTM_GET |
| 453 | && (rtm->rtm_pid == pid || rtm->rtm_pid == old_pid)) |
| 454 | return; |
| 455 | |
| 456 | /* Read destination and netmask and gateway from rtm message |
| 457 | structure. */ |
| 458 | flags = rtm_read_mesg (rtm, &dest, &mask, &gate); |
| 459 | |
| 460 | #ifdef RTF_CLONED /*bsdi, netbsd 1.6*/ |
| 461 | if (flags & RTF_CLONED) |
| 462 | return; |
| 463 | #endif |
| 464 | #ifdef RTF_WASCLONED /*freebsd*/ |
| 465 | if (flags & RTF_WASCLONED) |
| 466 | return; |
| 467 | #endif |
| 468 | |
| 469 | if ((rtm->rtm_type == RTM_ADD) && ! (flags & RTF_UP)) |
| 470 | return; |
| 471 | |
| 472 | /* This is connected route. */ |
| 473 | if (! (flags & RTF_GATEWAY)) |
| 474 | return; |
| 475 | |
| 476 | if (flags & RTF_PROTO1) |
| 477 | SET_FLAG (zebra_flags, ZEBRA_FLAG_SELFROUTE); |
| 478 | |
| 479 | /* This is persistent route. */ |
| 480 | if (flags & RTF_STATIC) |
| 481 | SET_FLAG (zebra_flags, ZEBRA_FLAG_STATIC); |
| 482 | |
hasso | 81dfcaa | 2003-05-25 19:21:25 +0000 | [diff] [blame] | 483 | /* This is a reject or blackhole route */ |
| 484 | if (flags & RTF_REJECT) |
| 485 | SET_FLAG (zebra_flags, ZEBRA_FLAG_REJECT); |
| 486 | if (flags & RTF_BLACKHOLE) |
| 487 | SET_FLAG (zebra_flags, ZEBRA_FLAG_BLACKHOLE); |
| 488 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 489 | if (dest.sa.sa_family == AF_INET) |
| 490 | { |
| 491 | struct prefix_ipv4 p; |
| 492 | |
| 493 | p.family = AF_INET; |
| 494 | p.prefix = dest.sin.sin_addr; |
| 495 | if (flags & RTF_HOST) |
| 496 | p.prefixlen = IPV4_MAX_PREFIXLEN; |
| 497 | else |
| 498 | p.prefixlen = ip_masklen (mask.sin.sin_addr); |
| 499 | |
| 500 | if (rtm->rtm_type == RTM_GET || rtm->rtm_type == RTM_ADD) |
| 501 | rib_add_ipv4 (ZEBRA_ROUTE_KERNEL, zebra_flags, |
| 502 | &p, &gate.sin.sin_addr, 0, 0, 0, 0); |
| 503 | else |
| 504 | rib_delete_ipv4 (ZEBRA_ROUTE_KERNEL, zebra_flags, |
| 505 | &p, &gate.sin.sin_addr, 0, 0); |
| 506 | } |
| 507 | #ifdef HAVE_IPV6 |
| 508 | if (dest.sa.sa_family == AF_INET6) |
| 509 | { |
| 510 | struct prefix_ipv6 p; |
| 511 | unsigned int ifindex = 0; |
| 512 | |
| 513 | p.family = AF_INET6; |
| 514 | p.prefix = dest.sin6.sin6_addr; |
| 515 | if (flags & RTF_HOST) |
| 516 | p.prefixlen = IPV6_MAX_PREFIXLEN; |
| 517 | else |
| 518 | p.prefixlen = ip6_masklen (mask.sin6.sin6_addr); |
| 519 | |
| 520 | #ifdef KAME |
| 521 | if (IN6_IS_ADDR_LINKLOCAL (&gate.sin6.sin6_addr)) |
| 522 | { |
| 523 | ifindex = IN6_LINKLOCAL_IFINDEX (gate.sin6.sin6_addr); |
| 524 | SET_IN6_LINKLOCAL_IFINDEX (gate.sin6.sin6_addr, 0); |
| 525 | } |
| 526 | #endif /* KAME */ |
| 527 | |
| 528 | if (rtm->rtm_type == RTM_GET || rtm->rtm_type == RTM_ADD) |
| 529 | rib_add_ipv6 (ZEBRA_ROUTE_KERNEL, zebra_flags, |
| 530 | &p, &gate.sin6.sin6_addr, ifindex, 0); |
| 531 | else |
| 532 | rib_delete_ipv6 (ZEBRA_ROUTE_KERNEL, zebra_flags, |
| 533 | &p, &gate.sin6.sin6_addr, ifindex, 0); |
| 534 | } |
| 535 | #endif /* HAVE_IPV6 */ |
| 536 | } |
| 537 | |
| 538 | /* Interface function for the kernel routing table updates. Support |
| 539 | for RTM_CHANGE will be needed. */ |
| 540 | int |
| 541 | rtm_write (int message, |
| 542 | union sockunion *dest, |
| 543 | union sockunion *mask, |
| 544 | union sockunion *gate, |
| 545 | unsigned int index, |
| 546 | int zebra_flags, |
| 547 | int metric) |
| 548 | { |
| 549 | int ret; |
| 550 | caddr_t pnt; |
| 551 | struct interface *ifp; |
| 552 | struct sockaddr_in tmp_gate; |
| 553 | #ifdef HAVE_IPV6 |
| 554 | struct sockaddr_in6 tmp_gate6; |
| 555 | #endif /* HAVE_IPV6 */ |
| 556 | |
| 557 | /* Sequencial number of routing message. */ |
| 558 | static int msg_seq = 0; |
| 559 | |
| 560 | /* Struct of rt_msghdr and buffer for storing socket's data. */ |
| 561 | struct |
| 562 | { |
| 563 | struct rt_msghdr rtm; |
| 564 | char buf[512]; |
| 565 | } msg; |
| 566 | |
| 567 | memset (&tmp_gate, 0, sizeof (struct sockaddr_in)); |
| 568 | tmp_gate.sin_family = AF_INET; |
| 569 | #ifdef HAVE_SIN_LEN |
| 570 | tmp_gate.sin_len = sizeof (struct sockaddr_in); |
| 571 | #endif /* HAVE_SIN_LEN */ |
| 572 | |
| 573 | #ifdef HAVE_IPV6 |
| 574 | memset (&tmp_gate6, 0, sizeof (struct sockaddr_in6)); |
| 575 | tmp_gate6.sin6_family = AF_INET6; |
| 576 | #ifdef SIN6_LEN |
| 577 | tmp_gate6.sin6_len = sizeof (struct sockaddr_in6); |
| 578 | #endif /* SIN6_LEN */ |
| 579 | #endif /* HAVE_IPV6 */ |
| 580 | |
| 581 | if (routing_sock < 0) |
| 582 | return ZEBRA_ERR_EPERM; |
| 583 | |
| 584 | /* Clear and set rt_msghdr values */ |
| 585 | memset (&msg, 0, sizeof (struct rt_msghdr)); |
| 586 | msg.rtm.rtm_version = RTM_VERSION; |
| 587 | msg.rtm.rtm_type = message; |
| 588 | msg.rtm.rtm_seq = msg_seq++; |
| 589 | msg.rtm.rtm_addrs = RTA_DST; |
| 590 | msg.rtm.rtm_addrs |= RTA_GATEWAY; |
| 591 | msg.rtm.rtm_flags = RTF_UP; |
| 592 | msg.rtm.rtm_index = index; |
| 593 | |
| 594 | if (metric != 0) |
| 595 | { |
| 596 | msg.rtm.rtm_rmx.rmx_hopcount = metric; |
| 597 | msg.rtm.rtm_inits |= RTV_HOPCOUNT; |
| 598 | } |
| 599 | |
| 600 | ifp = if_lookup_by_index (index); |
| 601 | |
| 602 | if (gate && message == RTM_ADD) |
| 603 | msg.rtm.rtm_flags |= RTF_GATEWAY; |
| 604 | |
| 605 | if (! gate && message == RTM_ADD && ifp && |
| 606 | (ifp->flags & IFF_POINTOPOINT) == 0) |
| 607 | msg.rtm.rtm_flags |= RTF_CLONING; |
| 608 | |
| 609 | /* If no protocol specific gateway is specified, use link |
| 610 | address for gateway. */ |
| 611 | if (! gate) |
| 612 | { |
| 613 | if (!ifp) |
| 614 | { |
| 615 | zlog_warn ("no gateway found for interface index %d", index); |
| 616 | return -1; |
| 617 | } |
| 618 | gate = (union sockunion *) & ifp->sdl; |
| 619 | } |
| 620 | |
| 621 | if (mask) |
| 622 | msg.rtm.rtm_addrs |= RTA_NETMASK; |
| 623 | else if (message == RTM_ADD) |
| 624 | msg.rtm.rtm_flags |= RTF_HOST; |
| 625 | |
| 626 | /* Tagging route with flags */ |
| 627 | msg.rtm.rtm_flags |= (RTF_PROTO1); |
| 628 | |
| 629 | /* Additional flags. */ |
| 630 | if (zebra_flags & ZEBRA_FLAG_BLACKHOLE) |
| 631 | msg.rtm.rtm_flags |= RTF_BLACKHOLE; |
hasso | 81dfcaa | 2003-05-25 19:21:25 +0000 | [diff] [blame] | 632 | if (zebra_flags & ZEBRA_FLAG_REJECT) |
| 633 | msg.rtm.rtm_flags |= RTF_REJECT; |
| 634 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 635 | |
| 636 | #ifdef HAVE_SIN_LEN |
| 637 | #define SOCKADDRSET(X,R) \ |
| 638 | if (msg.rtm.rtm_addrs & (R)) \ |
| 639 | { \ |
| 640 | int len = ROUNDUP ((X)->sa.sa_len); \ |
| 641 | memcpy (pnt, (caddr_t)(X), len); \ |
| 642 | pnt += len; \ |
| 643 | } |
| 644 | #else |
| 645 | #define SOCKADDRSET(X,R) \ |
| 646 | if (msg.rtm.rtm_addrs & (R)) \ |
| 647 | { \ |
| 648 | int len = ROUNDUP (sizeof((X)->sa)); \ |
| 649 | memcpy (pnt, (caddr_t)(X), len); \ |
| 650 | pnt += len; \ |
| 651 | } |
| 652 | #endif /* HAVE_SIN_LEN */ |
| 653 | |
| 654 | pnt = (caddr_t) msg.buf; |
| 655 | |
| 656 | /* Write each socket data into rtm message buffer */ |
| 657 | SOCKADDRSET (dest, RTA_DST); |
| 658 | SOCKADDRSET (gate, RTA_GATEWAY); |
| 659 | SOCKADDRSET (mask, RTA_NETMASK); |
| 660 | |
| 661 | msg.rtm.rtm_msglen = pnt - (caddr_t) &msg; |
| 662 | |
| 663 | ret = write (routing_sock, &msg, msg.rtm.rtm_msglen); |
| 664 | |
| 665 | if (ret != msg.rtm.rtm_msglen) |
| 666 | { |
| 667 | if (errno == EEXIST) |
| 668 | return ZEBRA_ERR_RTEXIST; |
| 669 | if (errno == ENETUNREACH) |
| 670 | return ZEBRA_ERR_RTUNREACH; |
| 671 | |
| 672 | zlog_warn ("write : %s (%d)", strerror (errno), errno); |
| 673 | return -1; |
| 674 | } |
| 675 | return 0; |
| 676 | } |
| 677 | |
| 678 | |
| 679 | #include "thread.h" |
| 680 | #include "zebra/zserv.h" |
| 681 | |
| 682 | extern struct thread_master *master; |
| 683 | |
| 684 | /* For debug purpose. */ |
| 685 | void |
| 686 | rtmsg_debug (struct rt_msghdr *rtm) |
| 687 | { |
| 688 | char *type = "Unknown"; |
| 689 | struct message *mes; |
| 690 | |
| 691 | for (mes = rtm_type_str; mes->str; mes++) |
| 692 | if (mes->key == rtm->rtm_type) |
| 693 | { |
| 694 | type = mes->str; |
| 695 | break; |
| 696 | } |
| 697 | |
| 698 | zlog_info ("Kernel: Len: %d Type: %s", rtm->rtm_msglen, type); |
| 699 | rtm_flag_dump (rtm->rtm_flags); |
| 700 | zlog_info ("Kernel: message seq %d", rtm->rtm_seq); |
| 701 | zlog_info ("Kernel: pid %d", rtm->rtm_pid); |
| 702 | } |
| 703 | |
| 704 | /* This is pretty gross, better suggestions welcome -- mhandler */ |
| 705 | #ifndef RTAX_MAX |
| 706 | #ifdef RTA_NUMBITS |
| 707 | #define RTAX_MAX RTA_NUMBITS |
| 708 | #else |
| 709 | #define RTAX_MAX 8 |
| 710 | #endif /* RTA_NUMBITS */ |
| 711 | #endif /* RTAX_MAX */ |
| 712 | |
| 713 | /* Kernel routing table and interface updates via routing socket. */ |
| 714 | int |
| 715 | kernel_read (struct thread *thread) |
| 716 | { |
| 717 | int sock; |
| 718 | int nbytes; |
| 719 | struct rt_msghdr *rtm; |
| 720 | |
| 721 | union |
| 722 | { |
| 723 | /* Routing information. */ |
| 724 | struct |
| 725 | { |
| 726 | struct rt_msghdr rtm; |
| 727 | struct sockaddr addr[RTAX_MAX]; |
| 728 | } r; |
| 729 | |
| 730 | /* Interface information. */ |
| 731 | struct |
| 732 | { |
| 733 | struct if_msghdr ifm; |
| 734 | struct sockaddr addr[RTAX_MAX]; |
| 735 | } im; |
| 736 | |
| 737 | /* Interface address information. */ |
| 738 | struct |
| 739 | { |
| 740 | struct ifa_msghdr ifa; |
| 741 | struct sockaddr addr[RTAX_MAX]; |
| 742 | } ia; |
| 743 | |
| 744 | #ifdef RTM_IFANNOUNCE |
| 745 | /* Interface arrival/departure */ |
| 746 | struct |
| 747 | { |
| 748 | struct if_announcemsghdr ifan; |
| 749 | struct sockaddr addr[RTAX_MAX]; |
| 750 | } ian; |
| 751 | #endif /* RTM_IFANNOUNCE */ |
| 752 | |
| 753 | } buf; |
| 754 | |
| 755 | /* Fetch routing socket. */ |
| 756 | sock = THREAD_FD (thread); |
| 757 | |
| 758 | nbytes= read (sock, &buf, sizeof buf); |
| 759 | |
| 760 | if (nbytes <= 0) |
| 761 | { |
| 762 | if (nbytes < 0 && errno != EWOULDBLOCK && errno != EAGAIN) |
| 763 | zlog_warn ("routing socket error: %s", strerror (errno)); |
| 764 | return 0; |
| 765 | } |
| 766 | |
| 767 | thread_add_read (master, kernel_read, NULL, sock); |
| 768 | |
hasso | 726f9b2 | 2003-05-25 21:04:54 +0000 | [diff] [blame] | 769 | if (IS_ZEBRA_DEBUG_KERNEL) |
| 770 | rtmsg_debug (&buf.r.rtm); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 771 | |
| 772 | rtm = &buf.r.rtm; |
| 773 | |
| 774 | switch (rtm->rtm_type) |
| 775 | { |
| 776 | case RTM_ADD: |
| 777 | case RTM_DELETE: |
| 778 | rtm_read (rtm); |
| 779 | break; |
| 780 | case RTM_IFINFO: |
| 781 | ifm_read (&buf.im.ifm); |
| 782 | break; |
| 783 | case RTM_NEWADDR: |
| 784 | case RTM_DELADDR: |
| 785 | ifam_read (&buf.ia.ifa); |
| 786 | break; |
| 787 | #ifdef RTM_IFANNOUNCE |
| 788 | case RTM_IFANNOUNCE: |
| 789 | ifan_read (&buf.ian.ifan); |
| 790 | break; |
| 791 | #endif /* RTM_IFANNOUNCE */ |
| 792 | default: |
hasso | 726f9b2 | 2003-05-25 21:04:54 +0000 | [diff] [blame] | 793 | if (IS_ZEBRA_DEBUG_KERNEL) |
| 794 | zlog_info("Unprocessed RTM_type: %d", rtm->rtm_type); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 795 | break; |
| 796 | } |
| 797 | return 0; |
| 798 | } |
| 799 | |
| 800 | /* Make routing socket. */ |
| 801 | void |
| 802 | routing_socket () |
| 803 | { |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 804 | if ( zserv_privs.change (ZPRIVS_RAISE) ) |
| 805 | zlog_err ("routing_socket: Can't raise privileges"); |
| 806 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 807 | routing_sock = socket (AF_ROUTE, SOCK_RAW, 0); |
| 808 | |
| 809 | if (routing_sock < 0) |
| 810 | { |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 811 | if ( zserv_privs.change (ZPRIVS_LOWER) ) |
| 812 | zlog_err ("routing_socket: Can't lower privileges"); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 813 | zlog_warn ("Can't init kernel routing socket"); |
| 814 | return; |
| 815 | } |
| 816 | |
| 817 | if (fcntl (routing_sock, F_SETFL, O_NONBLOCK) < 0) |
| 818 | zlog_warn ("Can't set O_NONBLOCK to routing socket"); |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 819 | if ( zserv_privs.change (ZPRIVS_LOWER) ) |
| 820 | zlog_err ("routing_socket: Can't lower privileges"); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 821 | |
| 822 | /* kernel_read needs rewrite. */ |
| 823 | thread_add_read (master, kernel_read, NULL, routing_sock); |
| 824 | } |
| 825 | |
| 826 | /* Exported interface function. This function simply calls |
| 827 | routing_socket (). */ |
| 828 | void |
| 829 | kernel_init () |
| 830 | { |
| 831 | routing_socket (); |
| 832 | } |