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