paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1 | /* zebra client |
| 2 | Copyright (C) 1997, 98, 99 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 |
| 18 | Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
| 19 | Boston, MA 02111-1307, USA. */ |
| 20 | |
| 21 | #include <zebra.h> |
| 22 | |
| 23 | #include "command.h" |
| 24 | #include "stream.h" |
| 25 | #include "network.h" |
| 26 | #include "prefix.h" |
| 27 | #include "log.h" |
| 28 | #include "sockunion.h" |
| 29 | #include "zclient.h" |
| 30 | #include "routemap.h" |
| 31 | #include "thread.h" |
| 32 | |
| 33 | #include "bgpd/bgpd.h" |
| 34 | #include "bgpd/bgp_route.h" |
| 35 | #include "bgpd/bgp_attr.h" |
| 36 | #include "bgpd/bgp_nexthop.h" |
| 37 | #include "bgpd/bgp_zebra.h" |
| 38 | #include "bgpd/bgp_fsm.h" |
Andrew J. Schorr | a39275d | 2006-11-30 16:36:57 +0000 | [diff] [blame] | 39 | #include "bgpd/bgp_debug.h" |
Josh Bailey | 8196f13 | 2011-07-20 20:47:07 -0700 | [diff] [blame^] | 40 | #include "bgpd/bgp_mpath.h" |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 41 | |
| 42 | /* All information about zebra. */ |
Chris Caputo | 228da42 | 2009-07-18 05:44:03 +0000 | [diff] [blame] | 43 | struct zclient *zclient = NULL; |
hasso | 18a6dce | 2004-10-03 18:18:34 +0000 | [diff] [blame] | 44 | struct in_addr router_id_zebra; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 45 | |
Josh Bailey | 8196f13 | 2011-07-20 20:47:07 -0700 | [diff] [blame^] | 46 | /* Growable buffer for nexthops sent to zebra */ |
| 47 | struct stream *bgp_nexthop_buf = NULL; |
| 48 | |
hasso | 18a6dce | 2004-10-03 18:18:34 +0000 | [diff] [blame] | 49 | /* Router-id update message from zebra. */ |
paul | 94f2b39 | 2005-06-28 12:44:16 +0000 | [diff] [blame] | 50 | static int |
hasso | 18a6dce | 2004-10-03 18:18:34 +0000 | [diff] [blame] | 51 | bgp_router_id_update (int command, struct zclient *zclient, zebra_size_t length) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 52 | { |
hasso | 18a6dce | 2004-10-03 18:18:34 +0000 | [diff] [blame] | 53 | struct prefix router_id; |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 54 | struct listnode *node, *nnode; |
hasso | 18a6dce | 2004-10-03 18:18:34 +0000 | [diff] [blame] | 55 | struct bgp *bgp; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 56 | |
hasso | 18a6dce | 2004-10-03 18:18:34 +0000 | [diff] [blame] | 57 | zebra_router_id_update_read(zclient->ibuf,&router_id); |
Andrew J. Schorr | a39275d | 2006-11-30 16:36:57 +0000 | [diff] [blame] | 58 | |
| 59 | if (BGP_DEBUG(zebra, ZEBRA)) |
| 60 | { |
| 61 | char buf[128]; |
| 62 | prefix2str(&router_id, buf, sizeof(buf)); |
| 63 | zlog_debug("Zebra rcvd: router id update %s", buf); |
| 64 | } |
| 65 | |
hasso | 18a6dce | 2004-10-03 18:18:34 +0000 | [diff] [blame] | 66 | router_id_zebra = router_id.u.prefix4; |
| 67 | |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 68 | for (ALL_LIST_ELEMENTS (bm->bgp, node, nnode, bgp)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 69 | { |
hasso | 18a6dce | 2004-10-03 18:18:34 +0000 | [diff] [blame] | 70 | if (!bgp->router_id_static.s_addr) |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 71 | bgp_router_id_set (bgp, &router_id.u.prefix4); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 72 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 73 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 74 | return 0; |
| 75 | } |
| 76 | |
| 77 | /* Inteface addition message from zebra. */ |
paul | 94f2b39 | 2005-06-28 12:44:16 +0000 | [diff] [blame] | 78 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 79 | bgp_interface_add (int command, struct zclient *zclient, zebra_size_t length) |
| 80 | { |
| 81 | struct interface *ifp; |
| 82 | |
| 83 | ifp = zebra_interface_add_read (zclient->ibuf); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 84 | |
Andrew J. Schorr | a39275d | 2006-11-30 16:36:57 +0000 | [diff] [blame] | 85 | if (BGP_DEBUG(zebra, ZEBRA) && ifp) |
| 86 | zlog_debug("Zebra rcvd: interface add %s", ifp->name); |
| 87 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 88 | return 0; |
| 89 | } |
| 90 | |
paul | 94f2b39 | 2005-06-28 12:44:16 +0000 | [diff] [blame] | 91 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 92 | bgp_interface_delete (int command, struct zclient *zclient, |
| 93 | zebra_size_t length) |
| 94 | { |
| 95 | struct stream *s; |
| 96 | struct interface *ifp; |
| 97 | |
| 98 | s = zclient->ibuf; |
| 99 | ifp = zebra_interface_state_read (s); |
ajs | d2fc889 | 2005-04-02 18:38:43 +0000 | [diff] [blame] | 100 | ifp->ifindex = IFINDEX_INTERNAL; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 101 | |
Andrew J. Schorr | a39275d | 2006-11-30 16:36:57 +0000 | [diff] [blame] | 102 | if (BGP_DEBUG(zebra, ZEBRA)) |
| 103 | zlog_debug("Zebra rcvd: interface delete %s", ifp->name); |
| 104 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 105 | return 0; |
| 106 | } |
| 107 | |
paul | 94f2b39 | 2005-06-28 12:44:16 +0000 | [diff] [blame] | 108 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 109 | bgp_interface_up (int command, struct zclient *zclient, zebra_size_t length) |
| 110 | { |
| 111 | struct stream *s; |
| 112 | struct interface *ifp; |
| 113 | struct connected *c; |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 114 | struct listnode *node, *nnode; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 115 | |
| 116 | s = zclient->ibuf; |
| 117 | ifp = zebra_interface_state_read (s); |
| 118 | |
| 119 | if (! ifp) |
| 120 | return 0; |
| 121 | |
Andrew J. Schorr | a39275d | 2006-11-30 16:36:57 +0000 | [diff] [blame] | 122 | if (BGP_DEBUG(zebra, ZEBRA)) |
| 123 | zlog_debug("Zebra rcvd: interface %s up", ifp->name); |
| 124 | |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 125 | for (ALL_LIST_ELEMENTS (ifp->connected, node, nnode, c)) |
| 126 | bgp_connected_add (c); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 127 | |
| 128 | return 0; |
| 129 | } |
| 130 | |
paul | 94f2b39 | 2005-06-28 12:44:16 +0000 | [diff] [blame] | 131 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 132 | bgp_interface_down (int command, struct zclient *zclient, zebra_size_t length) |
| 133 | { |
| 134 | struct stream *s; |
| 135 | struct interface *ifp; |
| 136 | struct connected *c; |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 137 | struct listnode *node, *nnode; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 138 | |
| 139 | s = zclient->ibuf; |
| 140 | ifp = zebra_interface_state_read (s); |
| 141 | if (! ifp) |
| 142 | return 0; |
| 143 | |
Andrew J. Schorr | a39275d | 2006-11-30 16:36:57 +0000 | [diff] [blame] | 144 | if (BGP_DEBUG(zebra, ZEBRA)) |
| 145 | zlog_debug("Zebra rcvd: interface %s down", ifp->name); |
| 146 | |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 147 | for (ALL_LIST_ELEMENTS (ifp->connected, node, nnode, c)) |
| 148 | bgp_connected_delete (c); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 149 | |
| 150 | /* Fast external-failover (Currently IPv4 only) */ |
| 151 | { |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 152 | struct listnode *mnode; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 153 | struct bgp *bgp; |
| 154 | struct peer *peer; |
| 155 | struct interface *peer_if; |
| 156 | |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 157 | for (ALL_LIST_ELEMENTS_RO (bm->bgp, mnode, bgp)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 158 | { |
| 159 | if (CHECK_FLAG (bgp->flags, BGP_FLAG_NO_FAST_EXT_FAILOVER)) |
| 160 | continue; |
| 161 | |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 162 | for (ALL_LIST_ELEMENTS (bgp->peer, node, nnode, peer)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 163 | { |
| 164 | if (peer->ttl != 1) |
| 165 | continue; |
| 166 | |
| 167 | if (peer->su.sa.sa_family == AF_INET) |
| 168 | peer_if = if_lookup_by_ipv4 (&peer->su.sin.sin_addr); |
| 169 | else |
| 170 | continue; |
| 171 | |
| 172 | if (ifp == peer_if) |
| 173 | BGP_EVENT_ADD (peer, BGP_Stop); |
| 174 | } |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | return 0; |
| 179 | } |
| 180 | |
paul | 94f2b39 | 2005-06-28 12:44:16 +0000 | [diff] [blame] | 181 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 182 | bgp_interface_address_add (int command, struct zclient *zclient, |
| 183 | zebra_size_t length) |
| 184 | { |
| 185 | struct connected *ifc; |
| 186 | |
paul | 0a58935 | 2004-05-08 11:48:26 +0000 | [diff] [blame] | 187 | ifc = zebra_interface_address_read (command, zclient->ibuf); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 188 | |
| 189 | if (ifc == NULL) |
| 190 | return 0; |
| 191 | |
Andrew J. Schorr | a39275d | 2006-11-30 16:36:57 +0000 | [diff] [blame] | 192 | if (BGP_DEBUG(zebra, ZEBRA)) |
| 193 | { |
| 194 | char buf[128]; |
| 195 | prefix2str(ifc->address, buf, sizeof(buf)); |
| 196 | zlog_debug("Zebra rcvd: interface %s address add %s", |
| 197 | ifc->ifp->name, buf); |
| 198 | } |
| 199 | |
paul | 2e3b2e4 | 2002-12-13 21:03:13 +0000 | [diff] [blame] | 200 | if (if_is_operative (ifc->ifp)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 201 | bgp_connected_add (ifc); |
| 202 | |
| 203 | return 0; |
| 204 | } |
| 205 | |
paul | 94f2b39 | 2005-06-28 12:44:16 +0000 | [diff] [blame] | 206 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 207 | bgp_interface_address_delete (int command, struct zclient *zclient, |
| 208 | zebra_size_t length) |
| 209 | { |
| 210 | struct connected *ifc; |
| 211 | |
paul | 0a58935 | 2004-05-08 11:48:26 +0000 | [diff] [blame] | 212 | ifc = zebra_interface_address_read (command, zclient->ibuf); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 213 | |
| 214 | if (ifc == NULL) |
| 215 | return 0; |
| 216 | |
Andrew J. Schorr | a39275d | 2006-11-30 16:36:57 +0000 | [diff] [blame] | 217 | if (BGP_DEBUG(zebra, ZEBRA)) |
| 218 | { |
| 219 | char buf[128]; |
| 220 | prefix2str(ifc->address, buf, sizeof(buf)); |
| 221 | zlog_debug("Zebra rcvd: interface %s address delete %s", |
| 222 | ifc->ifp->name, buf); |
| 223 | } |
| 224 | |
paul | 2e3b2e4 | 2002-12-13 21:03:13 +0000 | [diff] [blame] | 225 | if (if_is_operative (ifc->ifp)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 226 | bgp_connected_delete (ifc); |
| 227 | |
| 228 | connected_free (ifc); |
| 229 | |
| 230 | return 0; |
| 231 | } |
| 232 | |
| 233 | /* Zebra route add and delete treatment. */ |
paul | 94f2b39 | 2005-06-28 12:44:16 +0000 | [diff] [blame] | 234 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 235 | zebra_read_ipv4 (int command, struct zclient *zclient, zebra_size_t length) |
| 236 | { |
| 237 | struct stream *s; |
| 238 | struct zapi_ipv4 api; |
| 239 | unsigned long ifindex; |
| 240 | struct in_addr nexthop; |
| 241 | struct prefix_ipv4 p; |
| 242 | |
| 243 | s = zclient->ibuf; |
| 244 | ifindex = 0; |
| 245 | nexthop.s_addr = 0; |
| 246 | |
| 247 | /* Type, flags, message. */ |
| 248 | api.type = stream_getc (s); |
| 249 | api.flags = stream_getc (s); |
| 250 | api.message = stream_getc (s); |
| 251 | |
| 252 | /* IPv4 prefix. */ |
| 253 | memset (&p, 0, sizeof (struct prefix_ipv4)); |
| 254 | p.family = AF_INET; |
| 255 | p.prefixlen = stream_getc (s); |
| 256 | stream_get (&p.prefix, s, PSIZE (p.prefixlen)); |
| 257 | |
| 258 | /* Nexthop, ifindex, distance, metric. */ |
| 259 | if (CHECK_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP)) |
| 260 | { |
| 261 | api.nexthop_num = stream_getc (s); |
| 262 | nexthop.s_addr = stream_get_ipv4 (s); |
| 263 | } |
| 264 | if (CHECK_FLAG (api.message, ZAPI_MESSAGE_IFINDEX)) |
| 265 | { |
| 266 | api.ifindex_num = stream_getc (s); |
| 267 | ifindex = stream_getl (s); |
| 268 | } |
| 269 | if (CHECK_FLAG (api.message, ZAPI_MESSAGE_DISTANCE)) |
| 270 | api.distance = stream_getc (s); |
| 271 | if (CHECK_FLAG (api.message, ZAPI_MESSAGE_METRIC)) |
| 272 | api.metric = stream_getl (s); |
| 273 | else |
| 274 | api.metric = 0; |
| 275 | |
| 276 | if (command == ZEBRA_IPV4_ROUTE_ADD) |
Andrew J. Schorr | a39275d | 2006-11-30 16:36:57 +0000 | [diff] [blame] | 277 | { |
| 278 | if (BGP_DEBUG(zebra, ZEBRA)) |
| 279 | { |
| 280 | char buf[2][INET_ADDRSTRLEN]; |
| 281 | zlog_debug("Zebra rcvd: IPv4 route add %s %s/%d nexthop %s metric %u", |
| 282 | zebra_route_string(api.type), |
| 283 | inet_ntop(AF_INET, &p.prefix, buf[0], sizeof(buf[0])), |
| 284 | p.prefixlen, |
| 285 | inet_ntop(AF_INET, &nexthop, buf[1], sizeof(buf[1])), |
| 286 | api.metric); |
| 287 | } |
| 288 | bgp_redistribute_add((struct prefix *)&p, &nexthop, api.metric, api.type); |
| 289 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 290 | else |
Andrew J. Schorr | a39275d | 2006-11-30 16:36:57 +0000 | [diff] [blame] | 291 | { |
| 292 | if (BGP_DEBUG(zebra, ZEBRA)) |
| 293 | { |
| 294 | char buf[2][INET_ADDRSTRLEN]; |
| 295 | zlog_debug("Zebra rcvd: IPv4 route delete %s %s/%d " |
| 296 | "nexthop %s metric %u", |
| 297 | zebra_route_string(api.type), |
| 298 | inet_ntop(AF_INET, &p.prefix, buf[0], sizeof(buf[0])), |
| 299 | p.prefixlen, |
| 300 | inet_ntop(AF_INET, &nexthop, buf[1], sizeof(buf[1])), |
| 301 | api.metric); |
| 302 | } |
| 303 | bgp_redistribute_delete((struct prefix *)&p, api.type); |
| 304 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 305 | |
| 306 | return 0; |
| 307 | } |
| 308 | |
| 309 | #ifdef HAVE_IPV6 |
| 310 | /* Zebra route add and delete treatment. */ |
paul | 94f2b39 | 2005-06-28 12:44:16 +0000 | [diff] [blame] | 311 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 312 | zebra_read_ipv6 (int command, struct zclient *zclient, zebra_size_t length) |
| 313 | { |
| 314 | struct stream *s; |
| 315 | struct zapi_ipv6 api; |
| 316 | unsigned long ifindex; |
| 317 | struct in6_addr nexthop; |
| 318 | struct prefix_ipv6 p; |
| 319 | |
| 320 | s = zclient->ibuf; |
| 321 | ifindex = 0; |
| 322 | memset (&nexthop, 0, sizeof (struct in6_addr)); |
| 323 | |
| 324 | /* Type, flags, message. */ |
| 325 | api.type = stream_getc (s); |
| 326 | api.flags = stream_getc (s); |
| 327 | api.message = stream_getc (s); |
| 328 | |
| 329 | /* IPv6 prefix. */ |
| 330 | memset (&p, 0, sizeof (struct prefix_ipv6)); |
| 331 | p.family = AF_INET6; |
| 332 | p.prefixlen = stream_getc (s); |
| 333 | stream_get (&p.prefix, s, PSIZE (p.prefixlen)); |
| 334 | |
| 335 | /* Nexthop, ifindex, distance, metric. */ |
| 336 | if (CHECK_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP)) |
| 337 | { |
| 338 | api.nexthop_num = stream_getc (s); |
| 339 | stream_get (&nexthop, s, 16); |
| 340 | } |
| 341 | if (CHECK_FLAG (api.message, ZAPI_MESSAGE_IFINDEX)) |
| 342 | { |
| 343 | api.ifindex_num = stream_getc (s); |
| 344 | ifindex = stream_getl (s); |
| 345 | } |
| 346 | if (CHECK_FLAG (api.message, ZAPI_MESSAGE_DISTANCE)) |
| 347 | api.distance = stream_getc (s); |
| 348 | else |
| 349 | api.distance = 0; |
| 350 | if (CHECK_FLAG (api.message, ZAPI_MESSAGE_METRIC)) |
| 351 | api.metric = stream_getl (s); |
| 352 | else |
| 353 | api.metric = 0; |
| 354 | |
| 355 | /* Simply ignore link-local address. */ |
| 356 | if (IN6_IS_ADDR_LINKLOCAL (&p.prefix)) |
| 357 | return 0; |
| 358 | |
| 359 | if (command == ZEBRA_IPV6_ROUTE_ADD) |
Andrew J. Schorr | a39275d | 2006-11-30 16:36:57 +0000 | [diff] [blame] | 360 | { |
| 361 | if (BGP_DEBUG(zebra, ZEBRA)) |
| 362 | { |
| 363 | char buf[INET6_ADDRSTRLEN]; |
| 364 | zlog_debug("Zebra rcvd: IPv6 route add %s %s/%d metric %u", |
| 365 | zebra_route_string(api.type), |
| 366 | inet_ntop(AF_INET6, &p.prefix, buf, sizeof(buf)), |
| 367 | p.prefixlen, api.metric); |
| 368 | } |
| 369 | bgp_redistribute_add ((struct prefix *)&p, NULL, api.metric, api.type); |
| 370 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 371 | else |
Andrew J. Schorr | a39275d | 2006-11-30 16:36:57 +0000 | [diff] [blame] | 372 | { |
| 373 | if (BGP_DEBUG(zebra, ZEBRA)) |
| 374 | { |
| 375 | char buf[INET6_ADDRSTRLEN]; |
| 376 | zlog_debug("Zebra rcvd: IPv6 route delete %s %s/%d metric %u", |
| 377 | zebra_route_string(api.type), |
| 378 | inet_ntop(AF_INET6, &p.prefix, buf, sizeof(buf)), |
| 379 | p.prefixlen, api.metric); |
| 380 | } |
| 381 | bgp_redistribute_delete ((struct prefix *) &p, api.type); |
| 382 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 383 | |
| 384 | return 0; |
| 385 | } |
| 386 | #endif /* HAVE_IPV6 */ |
| 387 | |
| 388 | struct interface * |
| 389 | if_lookup_by_ipv4 (struct in_addr *addr) |
| 390 | { |
hasso | 52dc7ee | 2004-09-23 19:18:23 +0000 | [diff] [blame] | 391 | struct listnode *ifnode; |
| 392 | struct listnode *cnode; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 393 | struct interface *ifp; |
| 394 | struct connected *connected; |
| 395 | struct prefix_ipv4 p; |
| 396 | struct prefix *cp; |
| 397 | |
| 398 | p.family = AF_INET; |
| 399 | p.prefix = *addr; |
| 400 | p.prefixlen = IPV4_MAX_BITLEN; |
| 401 | |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 402 | for (ALL_LIST_ELEMENTS_RO (iflist, ifnode, ifp)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 403 | { |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 404 | for (ALL_LIST_ELEMENTS_RO (ifp->connected, cnode, connected)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 405 | { |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 406 | cp = connected->address; |
| 407 | |
| 408 | if (cp->family == AF_INET) |
| 409 | if (prefix_match (cp, (struct prefix *)&p)) |
| 410 | return ifp; |
| 411 | } |
| 412 | } |
| 413 | return NULL; |
| 414 | } |
| 415 | |
| 416 | struct interface * |
| 417 | if_lookup_by_ipv4_exact (struct in_addr *addr) |
| 418 | { |
hasso | 52dc7ee | 2004-09-23 19:18:23 +0000 | [diff] [blame] | 419 | struct listnode *ifnode; |
| 420 | struct listnode *cnode; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 421 | struct interface *ifp; |
| 422 | struct connected *connected; |
| 423 | struct prefix *cp; |
| 424 | |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 425 | for (ALL_LIST_ELEMENTS_RO (iflist, ifnode, ifp)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 426 | { |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 427 | for (ALL_LIST_ELEMENTS_RO (ifp->connected, cnode, connected)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 428 | { |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 429 | cp = connected->address; |
| 430 | |
| 431 | if (cp->family == AF_INET) |
| 432 | if (IPV4_ADDR_SAME (&cp->u.prefix4, addr)) |
| 433 | return ifp; |
| 434 | } |
| 435 | } |
| 436 | return NULL; |
| 437 | } |
| 438 | |
| 439 | #ifdef HAVE_IPV6 |
| 440 | struct interface * |
| 441 | if_lookup_by_ipv6 (struct in6_addr *addr) |
| 442 | { |
hasso | 52dc7ee | 2004-09-23 19:18:23 +0000 | [diff] [blame] | 443 | struct listnode *ifnode; |
| 444 | struct listnode *cnode; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 445 | struct interface *ifp; |
| 446 | struct connected *connected; |
| 447 | struct prefix_ipv6 p; |
| 448 | struct prefix *cp; |
| 449 | |
| 450 | p.family = AF_INET6; |
| 451 | p.prefix = *addr; |
| 452 | p.prefixlen = IPV6_MAX_BITLEN; |
| 453 | |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 454 | for (ALL_LIST_ELEMENTS_RO (iflist, ifnode, ifp)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 455 | { |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 456 | for (ALL_LIST_ELEMENTS_RO (ifp->connected, cnode, connected)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 457 | { |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 458 | cp = connected->address; |
| 459 | |
| 460 | if (cp->family == AF_INET6) |
| 461 | if (prefix_match (cp, (struct prefix *)&p)) |
| 462 | return ifp; |
| 463 | } |
| 464 | } |
| 465 | return NULL; |
| 466 | } |
| 467 | |
| 468 | struct interface * |
| 469 | if_lookup_by_ipv6_exact (struct in6_addr *addr) |
| 470 | { |
hasso | 52dc7ee | 2004-09-23 19:18:23 +0000 | [diff] [blame] | 471 | struct listnode *ifnode; |
| 472 | struct listnode *cnode; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 473 | struct interface *ifp; |
| 474 | struct connected *connected; |
| 475 | struct prefix *cp; |
| 476 | |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 477 | for (ALL_LIST_ELEMENTS_RO (iflist, ifnode, ifp)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 478 | { |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 479 | for (ALL_LIST_ELEMENTS_RO (ifp->connected, cnode, connected)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 480 | { |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 481 | cp = connected->address; |
| 482 | |
| 483 | if (cp->family == AF_INET6) |
| 484 | if (IPV6_ADDR_SAME (&cp->u.prefix6, addr)) |
| 485 | return ifp; |
| 486 | } |
| 487 | } |
| 488 | return NULL; |
| 489 | } |
| 490 | |
paul | 94f2b39 | 2005-06-28 12:44:16 +0000 | [diff] [blame] | 491 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 492 | if_get_ipv6_global (struct interface *ifp, struct in6_addr *addr) |
| 493 | { |
hasso | 52dc7ee | 2004-09-23 19:18:23 +0000 | [diff] [blame] | 494 | struct listnode *cnode; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 495 | struct connected *connected; |
| 496 | struct prefix *cp; |
| 497 | |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 498 | for (ALL_LIST_ELEMENTS_RO (ifp->connected, cnode, connected)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 499 | { |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 500 | cp = connected->address; |
| 501 | |
| 502 | if (cp->family == AF_INET6) |
| 503 | if (! IN6_IS_ADDR_LINKLOCAL (&cp->u.prefix6)) |
| 504 | { |
| 505 | memcpy (addr, &cp->u.prefix6, IPV6_MAX_BYTELEN); |
| 506 | return 1; |
| 507 | } |
| 508 | } |
| 509 | return 0; |
| 510 | } |
| 511 | |
paul | 94f2b39 | 2005-06-28 12:44:16 +0000 | [diff] [blame] | 512 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 513 | if_get_ipv6_local (struct interface *ifp, struct in6_addr *addr) |
| 514 | { |
hasso | 52dc7ee | 2004-09-23 19:18:23 +0000 | [diff] [blame] | 515 | struct listnode *cnode; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 516 | struct connected *connected; |
| 517 | struct prefix *cp; |
| 518 | |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 519 | for (ALL_LIST_ELEMENTS_RO (ifp->connected, cnode, connected)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 520 | { |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 521 | cp = connected->address; |
| 522 | |
| 523 | if (cp->family == AF_INET6) |
| 524 | if (IN6_IS_ADDR_LINKLOCAL (&cp->u.prefix6)) |
| 525 | { |
| 526 | memcpy (addr, &cp->u.prefix6, IPV6_MAX_BYTELEN); |
| 527 | return 1; |
| 528 | } |
| 529 | } |
| 530 | return 0; |
| 531 | } |
| 532 | #endif /* HAVE_IPV6 */ |
| 533 | |
| 534 | int |
| 535 | bgp_nexthop_set (union sockunion *local, union sockunion *remote, |
| 536 | struct bgp_nexthop *nexthop, struct peer *peer) |
| 537 | { |
| 538 | int ret = 0; |
| 539 | struct interface *ifp = NULL; |
| 540 | |
| 541 | memset (nexthop, 0, sizeof (struct bgp_nexthop)); |
| 542 | |
| 543 | if (!local) |
| 544 | return -1; |
| 545 | if (!remote) |
| 546 | return -1; |
| 547 | |
| 548 | if (local->sa.sa_family == AF_INET) |
| 549 | { |
| 550 | nexthop->v4 = local->sin.sin_addr; |
| 551 | ifp = if_lookup_by_ipv4 (&local->sin.sin_addr); |
| 552 | } |
| 553 | #ifdef HAVE_IPV6 |
| 554 | if (local->sa.sa_family == AF_INET6) |
| 555 | { |
| 556 | if (IN6_IS_ADDR_LINKLOCAL (&local->sin6.sin6_addr)) |
| 557 | { |
| 558 | if (peer->ifname) |
| 559 | ifp = if_lookup_by_index (if_nametoindex (peer->ifname)); |
| 560 | } |
| 561 | else |
| 562 | ifp = if_lookup_by_ipv6 (&local->sin6.sin6_addr); |
| 563 | } |
| 564 | #endif /* HAVE_IPV6 */ |
| 565 | |
| 566 | if (!ifp) |
| 567 | return -1; |
| 568 | |
| 569 | nexthop->ifp = ifp; |
| 570 | |
| 571 | /* IPv4 connection. */ |
| 572 | if (local->sa.sa_family == AF_INET) |
| 573 | { |
| 574 | #ifdef HAVE_IPV6 |
| 575 | /* IPv6 nexthop*/ |
| 576 | ret = if_get_ipv6_global (ifp, &nexthop->v6_global); |
| 577 | |
| 578 | /* There is no global nexthop. */ |
| 579 | if (!ret) |
| 580 | if_get_ipv6_local (ifp, &nexthop->v6_global); |
| 581 | else |
| 582 | if_get_ipv6_local (ifp, &nexthop->v6_local); |
| 583 | #endif /* HAVE_IPV6 */ |
| 584 | } |
| 585 | |
| 586 | #ifdef HAVE_IPV6 |
| 587 | /* IPv6 connection. */ |
| 588 | if (local->sa.sa_family == AF_INET6) |
| 589 | { |
| 590 | struct interface *direct = NULL; |
| 591 | |
| 592 | /* IPv4 nexthop. I don't care about it. */ |
| 593 | if (peer->local_id.s_addr) |
| 594 | nexthop->v4 = peer->local_id; |
| 595 | |
| 596 | /* Global address*/ |
| 597 | if (! IN6_IS_ADDR_LINKLOCAL (&local->sin6.sin6_addr)) |
| 598 | { |
| 599 | memcpy (&nexthop->v6_global, &local->sin6.sin6_addr, |
| 600 | IPV6_MAX_BYTELEN); |
| 601 | |
| 602 | /* If directory connected set link-local address. */ |
| 603 | direct = if_lookup_by_ipv6 (&remote->sin6.sin6_addr); |
| 604 | if (direct) |
| 605 | if_get_ipv6_local (ifp, &nexthop->v6_local); |
| 606 | } |
| 607 | else |
| 608 | /* Link-local address. */ |
| 609 | { |
| 610 | ret = if_get_ipv6_global (ifp, &nexthop->v6_global); |
| 611 | |
| 612 | /* If there is no global address. Set link-local address as |
| 613 | global. I know this break RFC specification... */ |
| 614 | if (!ret) |
| 615 | memcpy (&nexthop->v6_global, &local->sin6.sin6_addr, |
| 616 | IPV6_MAX_BYTELEN); |
| 617 | else |
| 618 | memcpy (&nexthop->v6_local, &local->sin6.sin6_addr, |
| 619 | IPV6_MAX_BYTELEN); |
| 620 | } |
| 621 | } |
| 622 | |
| 623 | if (IN6_IS_ADDR_LINKLOCAL (&local->sin6.sin6_addr) || |
| 624 | if_lookup_by_ipv6 (&remote->sin6.sin6_addr)) |
| 625 | peer->shared_network = 1; |
| 626 | else |
| 627 | peer->shared_network = 0; |
| 628 | |
| 629 | /* KAME stack specific treatment. */ |
| 630 | #ifdef KAME |
| 631 | if (IN6_IS_ADDR_LINKLOCAL (&nexthop->v6_global) |
| 632 | && IN6_LINKLOCAL_IFINDEX (nexthop->v6_global)) |
| 633 | { |
| 634 | SET_IN6_LINKLOCAL_IFINDEX (nexthop->v6_global, 0); |
| 635 | } |
| 636 | if (IN6_IS_ADDR_LINKLOCAL (&nexthop->v6_local) |
| 637 | && IN6_LINKLOCAL_IFINDEX (nexthop->v6_local)) |
| 638 | { |
| 639 | SET_IN6_LINKLOCAL_IFINDEX (nexthop->v6_local, 0); |
| 640 | } |
| 641 | #endif /* KAME */ |
| 642 | #endif /* HAVE_IPV6 */ |
| 643 | return ret; |
| 644 | } |
| 645 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 646 | void |
| 647 | bgp_zebra_announce (struct prefix *p, struct bgp_info *info, struct bgp *bgp) |
| 648 | { |
| 649 | int flags; |
| 650 | u_char distance; |
| 651 | struct peer *peer; |
Josh Bailey | 8196f13 | 2011-07-20 20:47:07 -0700 | [diff] [blame^] | 652 | struct bgp_info *mpinfo; |
| 653 | size_t oldsize, newsize; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 654 | |
| 655 | if (zclient->sock < 0) |
| 656 | return; |
| 657 | |
| 658 | if (! zclient->redist[ZEBRA_ROUTE_BGP]) |
| 659 | return; |
| 660 | |
| 661 | flags = 0; |
| 662 | peer = info->peer; |
| 663 | |
| 664 | if (peer_sort (peer) == BGP_PEER_IBGP || peer_sort (peer) == BGP_PEER_CONFED) |
| 665 | { |
| 666 | SET_FLAG (flags, ZEBRA_FLAG_IBGP); |
| 667 | SET_FLAG (flags, ZEBRA_FLAG_INTERNAL); |
| 668 | } |
| 669 | |
| 670 | if ((peer_sort (peer) == BGP_PEER_EBGP && peer->ttl != 1) |
hasso | 6ffd207 | 2005-02-02 14:50:11 +0000 | [diff] [blame] | 671 | || CHECK_FLAG (peer->flags, PEER_FLAG_DISABLE_CONNECTED_CHECK)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 672 | SET_FLAG (flags, ZEBRA_FLAG_INTERNAL); |
| 673 | |
Josh Bailey | 8196f13 | 2011-07-20 20:47:07 -0700 | [diff] [blame^] | 674 | /* resize nexthop buffer size if necessary */ |
| 675 | if ((oldsize = stream_get_size (bgp_nexthop_buf)) < |
| 676 | (sizeof (struct in_addr *) * (bgp_info_mpath_count (info) + 1))) |
| 677 | { |
| 678 | newsize = (sizeof (struct in_addr *) * (bgp_info_mpath_count (info) + 1)); |
| 679 | newsize = stream_resize (bgp_nexthop_buf, newsize); |
| 680 | if (newsize == oldsize) |
| 681 | { |
| 682 | zlog_err ("can't resize nexthop buffer"); |
| 683 | return; |
| 684 | } |
| 685 | } |
| 686 | |
| 687 | stream_reset (bgp_nexthop_buf); |
| 688 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 689 | if (p->family == AF_INET) |
| 690 | { |
| 691 | struct zapi_ipv4 api; |
| 692 | struct in_addr *nexthop; |
| 693 | |
| 694 | api.flags = flags; |
| 695 | nexthop = &info->attr->nexthop; |
Josh Bailey | 8196f13 | 2011-07-20 20:47:07 -0700 | [diff] [blame^] | 696 | stream_put (bgp_nexthop_buf, &nexthop, sizeof (struct in_addr *)); |
| 697 | for (mpinfo = bgp_info_mpath_first (info); mpinfo; |
| 698 | mpinfo = bgp_info_mpath_next (mpinfo)) |
| 699 | { |
| 700 | nexthop = &mpinfo->attr->nexthop; |
| 701 | stream_put (bgp_nexthop_buf, &nexthop, sizeof (struct in_addr *)); |
| 702 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 703 | |
| 704 | api.type = ZEBRA_ROUTE_BGP; |
| 705 | api.message = 0; |
| 706 | SET_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP); |
Josh Bailey | 8196f13 | 2011-07-20 20:47:07 -0700 | [diff] [blame^] | 707 | api.nexthop_num = 1 + bgp_info_mpath_count (info); |
| 708 | api.nexthop = (struct in_addr **)STREAM_DATA (bgp_nexthop_buf); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 709 | api.ifindex_num = 0; |
| 710 | SET_FLAG (api.message, ZAPI_MESSAGE_METRIC); |
| 711 | api.metric = info->attr->med; |
| 712 | |
| 713 | distance = bgp_distance_apply (p, info, bgp); |
| 714 | |
| 715 | if (distance) |
| 716 | { |
| 717 | SET_FLAG (api.message, ZAPI_MESSAGE_DISTANCE); |
| 718 | api.distance = distance; |
| 719 | } |
Andrew J. Schorr | a39275d | 2006-11-30 16:36:57 +0000 | [diff] [blame] | 720 | |
| 721 | if (BGP_DEBUG(zebra, ZEBRA)) |
| 722 | { |
Josh Bailey | 8196f13 | 2011-07-20 20:47:07 -0700 | [diff] [blame^] | 723 | int i; |
Andrew J. Schorr | a39275d | 2006-11-30 16:36:57 +0000 | [diff] [blame] | 724 | char buf[2][INET_ADDRSTRLEN]; |
Josh Bailey | 8196f13 | 2011-07-20 20:47:07 -0700 | [diff] [blame^] | 725 | zlog_debug("Zebra send: IPv4 route add %s/%d nexthop %s metric %u" |
| 726 | " count %d", |
Andrew J. Schorr | a39275d | 2006-11-30 16:36:57 +0000 | [diff] [blame] | 727 | inet_ntop(AF_INET, &p->u.prefix4, buf[0], sizeof(buf[0])), |
| 728 | p->prefixlen, |
Josh Bailey | 8196f13 | 2011-07-20 20:47:07 -0700 | [diff] [blame^] | 729 | inet_ntop(AF_INET, api.nexthop[0], buf[1], sizeof(buf[1])), |
| 730 | api.metric, api.nexthop_num); |
| 731 | for (i = 1; i < api.nexthop_num; i++) |
| 732 | zlog_debug("Zebra send: IPv4 route add [nexthop %d] %s", |
| 733 | i, inet_ntop(AF_INET, api.nexthop[i], buf[1], |
| 734 | sizeof(buf[1]))); |
Andrew J. Schorr | a39275d | 2006-11-30 16:36:57 +0000 | [diff] [blame] | 735 | } |
| 736 | |
paul | 0a58935 | 2004-05-08 11:48:26 +0000 | [diff] [blame] | 737 | zapi_ipv4_route (ZEBRA_IPV4_ROUTE_ADD, zclient, |
| 738 | (struct prefix_ipv4 *) p, &api); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 739 | } |
| 740 | #ifdef HAVE_IPV6 |
| 741 | /* We have to think about a IPv6 link-local address curse. */ |
| 742 | if (p->family == AF_INET6) |
| 743 | { |
| 744 | unsigned int ifindex; |
| 745 | struct in6_addr *nexthop; |
| 746 | struct zapi_ipv6 api; |
| 747 | |
| 748 | ifindex = 0; |
| 749 | nexthop = NULL; |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 750 | |
| 751 | assert (info->attr->extra); |
| 752 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 753 | /* Only global address nexthop exists. */ |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 754 | if (info->attr->extra->mp_nexthop_len == 16) |
| 755 | nexthop = &info->attr->extra->mp_nexthop_global; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 756 | |
| 757 | /* If both global and link-local address present. */ |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 758 | if (info->attr->extra->mp_nexthop_len == 32) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 759 | { |
| 760 | /* Workaround for Cisco's nexthop bug. */ |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 761 | if (IN6_IS_ADDR_UNSPECIFIED (&info->attr->extra->mp_nexthop_global) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 762 | && peer->su_remote->sa.sa_family == AF_INET6) |
| 763 | nexthop = &peer->su_remote->sin6.sin6_addr; |
| 764 | else |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 765 | nexthop = &info->attr->extra->mp_nexthop_local; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 766 | |
| 767 | if (info->peer->nexthop.ifp) |
| 768 | ifindex = info->peer->nexthop.ifp->ifindex; |
| 769 | } |
| 770 | |
| 771 | if (nexthop == NULL) |
| 772 | return; |
| 773 | |
| 774 | if (IN6_IS_ADDR_LINKLOCAL (nexthop) && ! ifindex) |
| 775 | { |
| 776 | if (info->peer->ifname) |
| 777 | ifindex = if_nametoindex (info->peer->ifname); |
| 778 | else if (info->peer->nexthop.ifp) |
| 779 | ifindex = info->peer->nexthop.ifp->ifindex; |
| 780 | } |
| 781 | |
| 782 | /* Make Zebra API structure. */ |
| 783 | api.flags = flags; |
| 784 | api.type = ZEBRA_ROUTE_BGP; |
| 785 | api.message = 0; |
| 786 | SET_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP); |
| 787 | api.nexthop_num = 1; |
| 788 | api.nexthop = &nexthop; |
| 789 | SET_FLAG (api.message, ZAPI_MESSAGE_IFINDEX); |
| 790 | api.ifindex_num = 1; |
| 791 | api.ifindex = &ifindex; |
| 792 | SET_FLAG (api.message, ZAPI_MESSAGE_METRIC); |
| 793 | api.metric = info->attr->med; |
| 794 | |
Andrew J. Schorr | a39275d | 2006-11-30 16:36:57 +0000 | [diff] [blame] | 795 | if (BGP_DEBUG(zebra, ZEBRA)) |
| 796 | { |
| 797 | char buf[2][INET6_ADDRSTRLEN]; |
| 798 | zlog_debug("Zebra send: IPv6 route add %s/%d nexthop %s metric %u", |
| 799 | inet_ntop(AF_INET6, &p->u.prefix6, buf[0], sizeof(buf[0])), |
| 800 | p->prefixlen, |
| 801 | inet_ntop(AF_INET6, nexthop, buf[1], sizeof(buf[1])), |
| 802 | api.metric); |
| 803 | } |
| 804 | |
paul | 0a58935 | 2004-05-08 11:48:26 +0000 | [diff] [blame] | 805 | zapi_ipv6_route (ZEBRA_IPV6_ROUTE_ADD, zclient, |
| 806 | (struct prefix_ipv6 *) p, &api); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 807 | } |
| 808 | #endif /* HAVE_IPV6 */ |
| 809 | } |
| 810 | |
| 811 | void |
| 812 | bgp_zebra_withdraw (struct prefix *p, struct bgp_info *info) |
| 813 | { |
| 814 | int flags; |
| 815 | struct peer *peer; |
| 816 | |
| 817 | if (zclient->sock < 0) |
| 818 | return; |
| 819 | |
| 820 | if (! zclient->redist[ZEBRA_ROUTE_BGP]) |
| 821 | return; |
| 822 | |
| 823 | peer = info->peer; |
| 824 | flags = 0; |
| 825 | |
| 826 | if (peer_sort (peer) == BGP_PEER_IBGP) |
| 827 | { |
| 828 | SET_FLAG (flags, ZEBRA_FLAG_INTERNAL); |
| 829 | SET_FLAG (flags, ZEBRA_FLAG_IBGP); |
| 830 | } |
| 831 | |
| 832 | if ((peer_sort (peer) == BGP_PEER_EBGP && peer->ttl != 1) |
hasso | 6ffd207 | 2005-02-02 14:50:11 +0000 | [diff] [blame] | 833 | || CHECK_FLAG (peer->flags, PEER_FLAG_DISABLE_CONNECTED_CHECK)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 834 | SET_FLAG (flags, ZEBRA_FLAG_INTERNAL); |
| 835 | |
| 836 | if (p->family == AF_INET) |
| 837 | { |
| 838 | struct zapi_ipv4 api; |
| 839 | struct in_addr *nexthop; |
| 840 | |
| 841 | api.flags = flags; |
| 842 | nexthop = &info->attr->nexthop; |
| 843 | |
| 844 | api.type = ZEBRA_ROUTE_BGP; |
| 845 | api.message = 0; |
| 846 | SET_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP); |
| 847 | api.nexthop_num = 1; |
| 848 | api.nexthop = &nexthop; |
| 849 | api.ifindex_num = 0; |
| 850 | SET_FLAG (api.message, ZAPI_MESSAGE_METRIC); |
| 851 | api.metric = info->attr->med; |
| 852 | |
Andrew J. Schorr | a39275d | 2006-11-30 16:36:57 +0000 | [diff] [blame] | 853 | if (BGP_DEBUG(zebra, ZEBRA)) |
| 854 | { |
| 855 | char buf[2][INET_ADDRSTRLEN]; |
| 856 | zlog_debug("Zebra send: IPv4 route delete %s/%d nexthop %s metric %u", |
| 857 | inet_ntop(AF_INET, &p->u.prefix4, buf[0], sizeof(buf[0])), |
| 858 | p->prefixlen, |
| 859 | inet_ntop(AF_INET, nexthop, buf[1], sizeof(buf[1])), |
| 860 | api.metric); |
| 861 | } |
| 862 | |
paul | 0a58935 | 2004-05-08 11:48:26 +0000 | [diff] [blame] | 863 | zapi_ipv4_route (ZEBRA_IPV4_ROUTE_DELETE, zclient, |
| 864 | (struct prefix_ipv4 *) p, &api); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 865 | } |
| 866 | #ifdef HAVE_IPV6 |
| 867 | /* We have to think about a IPv6 link-local address curse. */ |
| 868 | if (p->family == AF_INET6) |
| 869 | { |
| 870 | struct zapi_ipv6 api; |
| 871 | unsigned int ifindex; |
| 872 | struct in6_addr *nexthop; |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 873 | |
| 874 | assert (info->attr->extra); |
| 875 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 876 | ifindex = 0; |
| 877 | nexthop = NULL; |
| 878 | |
| 879 | /* Only global address nexthop exists. */ |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 880 | if (info->attr->extra->mp_nexthop_len == 16) |
| 881 | nexthop = &info->attr->extra->mp_nexthop_global; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 882 | |
| 883 | /* If both global and link-local address present. */ |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 884 | if (info->attr->extra->mp_nexthop_len == 32) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 885 | { |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 886 | nexthop = &info->attr->extra->mp_nexthop_local; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 887 | if (info->peer->nexthop.ifp) |
| 888 | ifindex = info->peer->nexthop.ifp->ifindex; |
| 889 | } |
| 890 | |
| 891 | if (nexthop == NULL) |
| 892 | return; |
| 893 | |
| 894 | if (IN6_IS_ADDR_LINKLOCAL (nexthop) && ! ifindex) |
| 895 | if (info->peer->ifname) |
| 896 | ifindex = if_nametoindex (info->peer->ifname); |
| 897 | |
| 898 | api.flags = flags; |
| 899 | api.type = ZEBRA_ROUTE_BGP; |
| 900 | api.message = 0; |
| 901 | SET_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP); |
| 902 | api.nexthop_num = 1; |
| 903 | api.nexthop = &nexthop; |
| 904 | SET_FLAG (api.message, ZAPI_MESSAGE_IFINDEX); |
| 905 | api.ifindex_num = 1; |
| 906 | api.ifindex = &ifindex; |
| 907 | SET_FLAG (api.message, ZAPI_MESSAGE_METRIC); |
| 908 | api.metric = info->attr->med; |
| 909 | |
Andrew J. Schorr | a39275d | 2006-11-30 16:36:57 +0000 | [diff] [blame] | 910 | if (BGP_DEBUG(zebra, ZEBRA)) |
| 911 | { |
| 912 | char buf[2][INET6_ADDRSTRLEN]; |
| 913 | zlog_debug("Zebra send: IPv6 route delete %s/%d nexthop %s metric %u", |
| 914 | inet_ntop(AF_INET6, &p->u.prefix6, buf[0], sizeof(buf[0])), |
| 915 | p->prefixlen, |
| 916 | inet_ntop(AF_INET6, nexthop, buf[1], sizeof(buf[1])), |
| 917 | api.metric); |
| 918 | } |
| 919 | |
paul | 0a58935 | 2004-05-08 11:48:26 +0000 | [diff] [blame] | 920 | zapi_ipv6_route (ZEBRA_IPV6_ROUTE_DELETE, zclient, |
| 921 | (struct prefix_ipv6 *) p, &api); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 922 | } |
| 923 | #endif /* HAVE_IPV6 */ |
| 924 | } |
| 925 | |
| 926 | /* Other routes redistribution into BGP. */ |
| 927 | int |
| 928 | bgp_redistribute_set (struct bgp *bgp, afi_t afi, int type) |
| 929 | { |
| 930 | /* Set flag to BGP instance. */ |
| 931 | bgp->redist[afi][type] = 1; |
| 932 | |
| 933 | /* Return if already redistribute flag is set. */ |
| 934 | if (zclient->redist[type]) |
| 935 | return CMD_WARNING; |
| 936 | |
| 937 | zclient->redist[type] = 1; |
| 938 | |
| 939 | /* Return if zebra connection is not established. */ |
| 940 | if (zclient->sock < 0) |
| 941 | return CMD_WARNING; |
Andrew J. Schorr | a39275d | 2006-11-30 16:36:57 +0000 | [diff] [blame] | 942 | |
| 943 | if (BGP_DEBUG(zebra, ZEBRA)) |
| 944 | zlog_debug("Zebra send: redistribute add %s", zebra_route_string(type)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 945 | |
| 946 | /* Send distribute add message to zebra. */ |
ajs | 634f9ea | 2005-04-11 15:51:40 +0000 | [diff] [blame] | 947 | zebra_redistribute_send (ZEBRA_REDISTRIBUTE_ADD, zclient, type); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 948 | |
| 949 | return CMD_SUCCESS; |
| 950 | } |
| 951 | |
| 952 | /* Redistribute with route-map specification. */ |
| 953 | int |
paul | fd79ac9 | 2004-10-13 05:06:08 +0000 | [diff] [blame] | 954 | bgp_redistribute_rmap_set (struct bgp *bgp, afi_t afi, int type, |
| 955 | const char *name) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 956 | { |
| 957 | if (bgp->rmap[afi][type].name |
| 958 | && (strcmp (bgp->rmap[afi][type].name, name) == 0)) |
| 959 | return 0; |
| 960 | |
| 961 | if (bgp->rmap[afi][type].name) |
| 962 | free (bgp->rmap[afi][type].name); |
| 963 | bgp->rmap[afi][type].name = strdup (name); |
| 964 | bgp->rmap[afi][type].map = route_map_lookup_by_name (name); |
| 965 | |
| 966 | return 1; |
| 967 | } |
| 968 | |
| 969 | /* Redistribute with metric specification. */ |
| 970 | int |
| 971 | bgp_redistribute_metric_set (struct bgp *bgp, afi_t afi, int type, |
| 972 | u_int32_t metric) |
| 973 | { |
| 974 | if (bgp->redist_metric_flag[afi][type] |
| 975 | && bgp->redist_metric[afi][type] == metric) |
| 976 | return 0; |
| 977 | |
| 978 | bgp->redist_metric_flag[afi][type] = 1; |
| 979 | bgp->redist_metric[afi][type] = metric; |
| 980 | |
| 981 | return 1; |
| 982 | } |
| 983 | |
| 984 | /* Unset redistribution. */ |
| 985 | int |
| 986 | bgp_redistribute_unset (struct bgp *bgp, afi_t afi, int type) |
| 987 | { |
| 988 | /* Unset flag from BGP instance. */ |
| 989 | bgp->redist[afi][type] = 0; |
| 990 | |
| 991 | /* Unset route-map. */ |
| 992 | if (bgp->rmap[afi][type].name) |
| 993 | free (bgp->rmap[afi][type].name); |
| 994 | bgp->rmap[afi][type].name = NULL; |
| 995 | bgp->rmap[afi][type].map = NULL; |
| 996 | |
| 997 | /* Unset metric. */ |
| 998 | bgp->redist_metric_flag[afi][type] = 0; |
| 999 | bgp->redist_metric[afi][type] = 0; |
| 1000 | |
| 1001 | /* Return if zebra connection is disabled. */ |
| 1002 | if (! zclient->redist[type]) |
| 1003 | return CMD_WARNING; |
| 1004 | zclient->redist[type] = 0; |
| 1005 | |
| 1006 | if (bgp->redist[AFI_IP][type] == 0 |
| 1007 | && bgp->redist[AFI_IP6][type] == 0 |
| 1008 | && zclient->sock >= 0) |
Andrew J. Schorr | a39275d | 2006-11-30 16:36:57 +0000 | [diff] [blame] | 1009 | { |
| 1010 | /* Send distribute delete message to zebra. */ |
| 1011 | if (BGP_DEBUG(zebra, ZEBRA)) |
| 1012 | zlog_debug("Zebra send: redistribute delete %s", |
| 1013 | zebra_route_string(type)); |
| 1014 | zebra_redistribute_send (ZEBRA_REDISTRIBUTE_DELETE, zclient, type); |
| 1015 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1016 | |
| 1017 | /* Withdraw redistributed routes from current BGP's routing table. */ |
| 1018 | bgp_redistribute_withdraw (bgp, afi, type); |
| 1019 | |
| 1020 | return CMD_SUCCESS; |
| 1021 | } |
| 1022 | |
| 1023 | /* Unset redistribution route-map configuration. */ |
| 1024 | int |
| 1025 | bgp_redistribute_routemap_unset (struct bgp *bgp, afi_t afi, int type) |
| 1026 | { |
| 1027 | if (! bgp->rmap[afi][type].name) |
| 1028 | return 0; |
| 1029 | |
| 1030 | /* Unset route-map. */ |
| 1031 | free (bgp->rmap[afi][type].name); |
| 1032 | bgp->rmap[afi][type].name = NULL; |
| 1033 | bgp->rmap[afi][type].map = NULL; |
| 1034 | |
| 1035 | return 1; |
| 1036 | } |
| 1037 | |
| 1038 | /* Unset redistribution metric configuration. */ |
| 1039 | int |
| 1040 | bgp_redistribute_metric_unset (struct bgp *bgp, afi_t afi, int type) |
| 1041 | { |
| 1042 | if (! bgp->redist_metric_flag[afi][type]) |
| 1043 | return 0; |
| 1044 | |
| 1045 | /* Unset metric. */ |
| 1046 | bgp->redist_metric_flag[afi][type] = 0; |
| 1047 | bgp->redist_metric[afi][type] = 0; |
| 1048 | |
| 1049 | return 1; |
| 1050 | } |
| 1051 | |
| 1052 | void |
paul | 94f2b39 | 2005-06-28 12:44:16 +0000 | [diff] [blame] | 1053 | bgp_zclient_reset (void) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1054 | { |
| 1055 | zclient_reset (zclient); |
| 1056 | } |
| 1057 | |
| 1058 | void |
paul | 94f2b39 | 2005-06-28 12:44:16 +0000 | [diff] [blame] | 1059 | bgp_zebra_init (void) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1060 | { |
| 1061 | /* Set default values. */ |
| 1062 | zclient = zclient_new (); |
| 1063 | zclient_init (zclient, ZEBRA_ROUTE_BGP); |
hasso | 18a6dce | 2004-10-03 18:18:34 +0000 | [diff] [blame] | 1064 | zclient->router_id_update = bgp_router_id_update; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1065 | zclient->interface_add = bgp_interface_add; |
| 1066 | zclient->interface_delete = bgp_interface_delete; |
| 1067 | zclient->interface_address_add = bgp_interface_address_add; |
| 1068 | zclient->interface_address_delete = bgp_interface_address_delete; |
| 1069 | zclient->ipv4_route_add = zebra_read_ipv4; |
| 1070 | zclient->ipv4_route_delete = zebra_read_ipv4; |
| 1071 | zclient->interface_up = bgp_interface_up; |
| 1072 | zclient->interface_down = bgp_interface_down; |
| 1073 | #ifdef HAVE_IPV6 |
| 1074 | zclient->ipv6_route_add = zebra_read_ipv6; |
| 1075 | zclient->ipv6_route_delete = zebra_read_ipv6; |
| 1076 | #endif /* HAVE_IPV6 */ |
| 1077 | |
| 1078 | /* Interface related init. */ |
| 1079 | if_init (); |
Josh Bailey | 8196f13 | 2011-07-20 20:47:07 -0700 | [diff] [blame^] | 1080 | |
| 1081 | bgp_nexthop_buf = stream_new(BGP_NEXTHOP_BUF_SIZE); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1082 | } |