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" |
Donald Sharp | 0490729 | 2016-01-07 10:03:01 -0500 | [diff] [blame] | 32 | #include "filter.h" |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 33 | |
| 34 | #include "bgpd/bgpd.h" |
| 35 | #include "bgpd/bgp_route.h" |
| 36 | #include "bgpd/bgp_attr.h" |
| 37 | #include "bgpd/bgp_nexthop.h" |
| 38 | #include "bgpd/bgp_zebra.h" |
| 39 | #include "bgpd/bgp_fsm.h" |
Andrew J. Schorr | a39275d | 2006-11-30 16:36:57 +0000 | [diff] [blame] | 40 | #include "bgpd/bgp_debug.h" |
Josh Bailey | 8196f13 | 2011-07-20 20:47:07 -0700 | [diff] [blame] | 41 | #include "bgpd/bgp_mpath.h" |
David Lamparter | 6b0655a | 2014-06-04 06:53:35 +0200 | [diff] [blame] | 42 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 43 | /* All information about zebra. */ |
Chris Caputo | 228da42 | 2009-07-18 05:44:03 +0000 | [diff] [blame] | 44 | struct zclient *zclient = NULL; |
hasso | 18a6dce | 2004-10-03 18:18:34 +0000 | [diff] [blame] | 45 | struct in_addr router_id_zebra; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 46 | |
Josh Bailey | 8196f13 | 2011-07-20 20:47:07 -0700 | [diff] [blame] | 47 | /* Growable buffer for nexthops sent to zebra */ |
| 48 | struct stream *bgp_nexthop_buf = NULL; |
Ayan Banerjee | b8d1f71 | 2015-11-09 20:14:54 -0500 | [diff] [blame] | 49 | struct stream *bgp_ifindices_buf = NULL; |
Josh Bailey | 8196f13 | 2011-07-20 20:47:07 -0700 | [diff] [blame] | 50 | |
hasso | 18a6dce | 2004-10-03 18:18:34 +0000 | [diff] [blame] | 51 | /* Router-id update message from zebra. */ |
paul | 94f2b39 | 2005-06-28 12:44:16 +0000 | [diff] [blame] | 52 | static int |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 53 | bgp_router_id_update (int command, struct zclient *zclient, zebra_size_t length, |
| 54 | vrf_id_t vrf_id) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 55 | { |
hasso | 18a6dce | 2004-10-03 18:18:34 +0000 | [diff] [blame] | 56 | struct prefix router_id; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 57 | |
hasso | 18a6dce | 2004-10-03 18:18:34 +0000 | [diff] [blame] | 58 | zebra_router_id_update_read(zclient->ibuf,&router_id); |
Andrew J. Schorr | a39275d | 2006-11-30 16:36:57 +0000 | [diff] [blame] | 59 | |
| 60 | if (BGP_DEBUG(zebra, ZEBRA)) |
| 61 | { |
| 62 | char buf[128]; |
| 63 | prefix2str(&router_id, buf, sizeof(buf)); |
| 64 | zlog_debug("Zebra rcvd: router id update %s", buf); |
| 65 | } |
| 66 | |
hasso | 18a6dce | 2004-10-03 18:18:34 +0000 | [diff] [blame] | 67 | router_id_zebra = router_id.u.prefix4; |
| 68 | |
David Lamparter | 584083d | 2016-05-24 18:58:08 +0200 | [diff] [blame] | 69 | bgp_router_id_zebra_bump (); |
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 |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 75 | bgp_interface_add (int command, struct zclient *zclient, zebra_size_t length, |
| 76 | vrf_id_t vrf_id) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 77 | { |
| 78 | struct interface *ifp; |
| 79 | |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 80 | ifp = zebra_interface_add_read (zclient->ibuf, vrf_id); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 81 | |
Andrew J. Schorr | a39275d | 2006-11-30 16:36:57 +0000 | [diff] [blame] | 82 | if (BGP_DEBUG(zebra, ZEBRA) && ifp) |
| 83 | zlog_debug("Zebra rcvd: interface add %s", ifp->name); |
| 84 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 85 | return 0; |
| 86 | } |
| 87 | |
paul | 94f2b39 | 2005-06-28 12:44:16 +0000 | [diff] [blame] | 88 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 89 | bgp_interface_delete (int command, struct zclient *zclient, |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 90 | zebra_size_t length, vrf_id_t vrf_id) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 91 | { |
| 92 | struct stream *s; |
| 93 | struct interface *ifp; |
| 94 | |
| 95 | s = zclient->ibuf; |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 96 | ifp = zebra_interface_state_read (s, vrf_id); |
ajs | d2fc889 | 2005-04-02 18:38:43 +0000 | [diff] [blame] | 97 | ifp->ifindex = IFINDEX_INTERNAL; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 98 | |
Andrew J. Schorr | a39275d | 2006-11-30 16:36:57 +0000 | [diff] [blame] | 99 | if (BGP_DEBUG(zebra, ZEBRA)) |
| 100 | zlog_debug("Zebra rcvd: interface delete %s", ifp->name); |
| 101 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 102 | return 0; |
| 103 | } |
| 104 | |
paul | 94f2b39 | 2005-06-28 12:44:16 +0000 | [diff] [blame] | 105 | static int |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 106 | bgp_interface_up (int command, struct zclient *zclient, zebra_size_t length, |
| 107 | vrf_id_t vrf_id) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 108 | { |
| 109 | struct stream *s; |
| 110 | struct interface *ifp; |
| 111 | struct connected *c; |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 112 | struct listnode *node, *nnode; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 113 | |
| 114 | s = zclient->ibuf; |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 115 | ifp = zebra_interface_state_read (s, vrf_id); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 116 | |
| 117 | if (! ifp) |
| 118 | return 0; |
| 119 | |
Andrew J. Schorr | a39275d | 2006-11-30 16:36:57 +0000 | [diff] [blame] | 120 | if (BGP_DEBUG(zebra, ZEBRA)) |
| 121 | zlog_debug("Zebra rcvd: interface %s up", ifp->name); |
| 122 | |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 123 | for (ALL_LIST_ELEMENTS (ifp->connected, node, nnode, c)) |
| 124 | bgp_connected_add (c); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 125 | |
| 126 | return 0; |
| 127 | } |
| 128 | |
paul | 94f2b39 | 2005-06-28 12:44:16 +0000 | [diff] [blame] | 129 | static int |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 130 | bgp_interface_down (int command, struct zclient *zclient, zebra_size_t length, |
| 131 | vrf_id_t vrf_id) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 132 | { |
| 133 | struct stream *s; |
| 134 | struct interface *ifp; |
| 135 | struct connected *c; |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 136 | struct listnode *node, *nnode; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 137 | |
| 138 | s = zclient->ibuf; |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 139 | ifp = zebra_interface_state_read (s, vrf_id); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 140 | if (! ifp) |
| 141 | return 0; |
| 142 | |
Andrew J. Schorr | a39275d | 2006-11-30 16:36:57 +0000 | [diff] [blame] | 143 | if (BGP_DEBUG(zebra, ZEBRA)) |
| 144 | zlog_debug("Zebra rcvd: interface %s down", ifp->name); |
| 145 | |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 146 | for (ALL_LIST_ELEMENTS (ifp->connected, node, nnode, c)) |
| 147 | bgp_connected_delete (c); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 148 | |
Pradosh Mohapatra | 8da8689 | 2013-09-11 03:33:55 +0000 | [diff] [blame] | 149 | /* Fast external-failover */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 150 | { |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 151 | struct listnode *mnode; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 152 | struct bgp *bgp; |
| 153 | struct peer *peer; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 154 | |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 155 | for (ALL_LIST_ELEMENTS_RO (bm->bgp, mnode, bgp)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 156 | { |
| 157 | if (CHECK_FLAG (bgp->flags, BGP_FLAG_NO_FAST_EXT_FAILOVER)) |
| 158 | continue; |
| 159 | |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 160 | for (ALL_LIST_ELEMENTS (bgp->peer, node, nnode, peer)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 161 | { |
Pradosh Mohapatra | 8da8689 | 2013-09-11 03:33:55 +0000 | [diff] [blame] | 162 | if ((peer->ttl != 1) && (peer->gtsm_hops != 1)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 163 | continue; |
| 164 | |
Pradosh Mohapatra | 8da8689 | 2013-09-11 03:33:55 +0000 | [diff] [blame] | 165 | if (ifp == peer->nexthop.ifp) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 166 | BGP_EVENT_ADD (peer, BGP_Stop); |
| 167 | } |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | return 0; |
| 172 | } |
| 173 | |
paul | 94f2b39 | 2005-06-28 12:44:16 +0000 | [diff] [blame] | 174 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 175 | bgp_interface_address_add (int command, struct zclient *zclient, |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 176 | zebra_size_t length, vrf_id_t vrf_id) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 177 | { |
| 178 | struct connected *ifc; |
| 179 | |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 180 | ifc = zebra_interface_address_read (command, zclient->ibuf, vrf_id); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 181 | |
| 182 | if (ifc == NULL) |
| 183 | return 0; |
| 184 | |
Andrew J. Schorr | a39275d | 2006-11-30 16:36:57 +0000 | [diff] [blame] | 185 | if (BGP_DEBUG(zebra, ZEBRA)) |
| 186 | { |
| 187 | char buf[128]; |
| 188 | prefix2str(ifc->address, buf, sizeof(buf)); |
| 189 | zlog_debug("Zebra rcvd: interface %s address add %s", |
| 190 | ifc->ifp->name, buf); |
| 191 | } |
| 192 | |
paul | 2e3b2e4 | 2002-12-13 21:03:13 +0000 | [diff] [blame] | 193 | if (if_is_operative (ifc->ifp)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 194 | bgp_connected_add (ifc); |
| 195 | |
| 196 | return 0; |
| 197 | } |
| 198 | |
paul | 94f2b39 | 2005-06-28 12:44:16 +0000 | [diff] [blame] | 199 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 200 | bgp_interface_address_delete (int command, struct zclient *zclient, |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 201 | zebra_size_t length, vrf_id_t vrf_id) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 202 | { |
| 203 | struct connected *ifc; |
| 204 | |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 205 | ifc = zebra_interface_address_read (command, zclient->ibuf, vrf_id); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 206 | |
| 207 | if (ifc == NULL) |
| 208 | return 0; |
| 209 | |
Andrew J. Schorr | a39275d | 2006-11-30 16:36:57 +0000 | [diff] [blame] | 210 | if (BGP_DEBUG(zebra, ZEBRA)) |
| 211 | { |
| 212 | char buf[128]; |
| 213 | prefix2str(ifc->address, buf, sizeof(buf)); |
| 214 | zlog_debug("Zebra rcvd: interface %s address delete %s", |
| 215 | ifc->ifp->name, buf); |
| 216 | } |
| 217 | |
paul | 2e3b2e4 | 2002-12-13 21:03:13 +0000 | [diff] [blame] | 218 | if (if_is_operative (ifc->ifp)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 219 | bgp_connected_delete (ifc); |
| 220 | |
| 221 | connected_free (ifc); |
| 222 | |
| 223 | return 0; |
| 224 | } |
| 225 | |
| 226 | /* Zebra route add and delete treatment. */ |
paul | 94f2b39 | 2005-06-28 12:44:16 +0000 | [diff] [blame] | 227 | static int |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 228 | zebra_read_ipv4 (int command, struct zclient *zclient, zebra_size_t length, |
| 229 | vrf_id_t vrf_id) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 230 | { |
| 231 | struct stream *s; |
| 232 | struct zapi_ipv4 api; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 233 | struct in_addr nexthop; |
| 234 | struct prefix_ipv4 p; |
Donald Sharp | 5e57b5f | 2016-03-11 16:28:34 -0500 | [diff] [blame] | 235 | unsigned char plength = 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 236 | |
| 237 | s = zclient->ibuf; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 238 | nexthop.s_addr = 0; |
| 239 | |
| 240 | /* Type, flags, message. */ |
| 241 | api.type = stream_getc (s); |
| 242 | api.flags = stream_getc (s); |
| 243 | api.message = stream_getc (s); |
| 244 | |
| 245 | /* IPv4 prefix. */ |
| 246 | memset (&p, 0, sizeof (struct prefix_ipv4)); |
| 247 | p.family = AF_INET; |
Donald Sharp | 5e57b5f | 2016-03-11 16:28:34 -0500 | [diff] [blame] | 248 | plength = stream_getc (s); |
| 249 | p.prefixlen = MIN(IPV4_MAX_PREFIXLEN, plength); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 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 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 304 | /* Zebra route add and delete treatment. */ |
paul | 94f2b39 | 2005-06-28 12:44:16 +0000 | [diff] [blame] | 305 | static int |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 306 | zebra_read_ipv6 (int command, struct zclient *zclient, zebra_size_t length, |
| 307 | vrf_id_t vrf_id) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 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; |
Donald Sharp | 5e57b5f | 2016-03-11 16:28:34 -0500 | [diff] [blame] | 313 | unsigned char plength = 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 314 | |
| 315 | s = zclient->ibuf; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 316 | memset (&nexthop, 0, sizeof (struct in6_addr)); |
| 317 | |
| 318 | /* Type, flags, message. */ |
| 319 | api.type = stream_getc (s); |
| 320 | api.flags = stream_getc (s); |
| 321 | api.message = stream_getc (s); |
| 322 | |
| 323 | /* IPv6 prefix. */ |
| 324 | memset (&p, 0, sizeof (struct prefix_ipv6)); |
| 325 | p.family = AF_INET6; |
Donald Sharp | 5e57b5f | 2016-03-11 16:28:34 -0500 | [diff] [blame] | 326 | plength = stream_getc (s); |
| 327 | p.prefixlen = MIN(IPV6_MAX_PREFIXLEN, plength); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 328 | stream_get (&p.prefix, s, PSIZE (p.prefixlen)); |
| 329 | |
| 330 | /* Nexthop, ifindex, distance, metric. */ |
| 331 | if (CHECK_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP)) |
| 332 | { |
| 333 | api.nexthop_num = stream_getc (s); |
| 334 | stream_get (&nexthop, s, 16); |
| 335 | } |
| 336 | if (CHECK_FLAG (api.message, ZAPI_MESSAGE_IFINDEX)) |
| 337 | { |
| 338 | api.ifindex_num = stream_getc (s); |
Stephen Hemminger | 9206f9e | 2011-12-18 19:43:40 +0400 | [diff] [blame] | 339 | stream_getl (s); /* ifindex, unused */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 340 | } |
| 341 | if (CHECK_FLAG (api.message, ZAPI_MESSAGE_DISTANCE)) |
| 342 | api.distance = stream_getc (s); |
| 343 | else |
| 344 | api.distance = 0; |
| 345 | if (CHECK_FLAG (api.message, ZAPI_MESSAGE_METRIC)) |
| 346 | api.metric = stream_getl (s); |
| 347 | else |
| 348 | api.metric = 0; |
| 349 | |
| 350 | /* Simply ignore link-local address. */ |
| 351 | if (IN6_IS_ADDR_LINKLOCAL (&p.prefix)) |
| 352 | return 0; |
| 353 | |
| 354 | if (command == ZEBRA_IPV6_ROUTE_ADD) |
Andrew J. Schorr | a39275d | 2006-11-30 16:36:57 +0000 | [diff] [blame] | 355 | { |
| 356 | if (BGP_DEBUG(zebra, ZEBRA)) |
| 357 | { |
Stephen Hemminger | f04a80a | 2011-12-06 14:51:10 +0400 | [diff] [blame] | 358 | char buf[2][INET6_ADDRSTRLEN]; |
| 359 | 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] | 360 | zebra_route_string(api.type), |
Stephen Hemminger | f04a80a | 2011-12-06 14:51:10 +0400 | [diff] [blame] | 361 | inet_ntop(AF_INET6, &p.prefix, buf[0], sizeof(buf[0])), |
| 362 | p.prefixlen, |
| 363 | inet_ntop(AF_INET, &nexthop, buf[1], sizeof(buf[1])), |
| 364 | api.metric); |
Andrew J. Schorr | a39275d | 2006-11-30 16:36:57 +0000 | [diff] [blame] | 365 | } |
Stephen Hemminger | f04a80a | 2011-12-06 14:51:10 +0400 | [diff] [blame] | 366 | bgp_redistribute_add ((struct prefix *)&p, NULL, &nexthop, |
| 367 | api.metric, api.type); |
Andrew J. Schorr | a39275d | 2006-11-30 16:36:57 +0000 | [diff] [blame] | 368 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 369 | else |
Andrew J. Schorr | a39275d | 2006-11-30 16:36:57 +0000 | [diff] [blame] | 370 | { |
| 371 | if (BGP_DEBUG(zebra, ZEBRA)) |
| 372 | { |
Stephen Hemminger | f04a80a | 2011-12-06 14:51:10 +0400 | [diff] [blame] | 373 | char buf[2][INET6_ADDRSTRLEN]; |
| 374 | zlog_debug("Zebra rcvd: IPv6 route delete %s %s/%d " |
| 375 | "nexthop %s metric %u", |
Andrew J. Schorr | a39275d | 2006-11-30 16:36:57 +0000 | [diff] [blame] | 376 | zebra_route_string(api.type), |
Stephen Hemminger | f04a80a | 2011-12-06 14:51:10 +0400 | [diff] [blame] | 377 | inet_ntop(AF_INET6, &p.prefix, buf[0], sizeof(buf[0])), |
| 378 | p.prefixlen, |
| 379 | inet_ntop(AF_INET6, &nexthop, buf[1], sizeof(buf[1])), |
| 380 | api.metric); |
Andrew J. Schorr | a39275d | 2006-11-30 16:36:57 +0000 | [diff] [blame] | 381 | } |
| 382 | bgp_redistribute_delete ((struct prefix *) &p, api.type); |
| 383 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 384 | |
| 385 | return 0; |
| 386 | } |
David Lamparter | 6b0655a | 2014-06-04 06:53:35 +0200 | [diff] [blame] | 387 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 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 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 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 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 531 | |
Pradosh Mohapatra | 6ee06fa | 2014-01-12 18:30:13 +0000 | [diff] [blame] | 532 | static int |
| 533 | if_get_ipv4_address (struct interface *ifp, struct in_addr *addr) |
| 534 | { |
| 535 | struct listnode *cnode; |
| 536 | struct connected *connected; |
| 537 | struct prefix *cp; |
| 538 | |
| 539 | for (ALL_LIST_ELEMENTS_RO (ifp->connected, cnode, connected)) |
| 540 | { |
| 541 | cp = connected->address; |
| 542 | if ((cp->family == AF_INET) && !ipv4_martian(&(cp->u.prefix4))) |
| 543 | { |
| 544 | *addr = cp->u.prefix4; |
| 545 | return 1; |
| 546 | } |
| 547 | } |
| 548 | return 0; |
| 549 | } |
| 550 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 551 | int |
| 552 | bgp_nexthop_set (union sockunion *local, union sockunion *remote, |
| 553 | struct bgp_nexthop *nexthop, struct peer *peer) |
| 554 | { |
| 555 | int ret = 0; |
| 556 | struct interface *ifp = NULL; |
| 557 | |
| 558 | memset (nexthop, 0, sizeof (struct bgp_nexthop)); |
| 559 | |
| 560 | if (!local) |
| 561 | return -1; |
| 562 | if (!remote) |
| 563 | return -1; |
| 564 | |
| 565 | if (local->sa.sa_family == AF_INET) |
| 566 | { |
| 567 | nexthop->v4 = local->sin.sin_addr; |
| 568 | ifp = if_lookup_by_ipv4 (&local->sin.sin_addr); |
| 569 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 570 | if (local->sa.sa_family == AF_INET6) |
| 571 | { |
| 572 | if (IN6_IS_ADDR_LINKLOCAL (&local->sin6.sin6_addr)) |
| 573 | { |
| 574 | if (peer->ifname) |
Feng Lu | 395828e | 2015-05-22 11:39:55 +0200 | [diff] [blame] | 575 | ifp = if_lookup_by_name (peer->ifname); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 576 | } |
| 577 | else |
| 578 | ifp = if_lookup_by_ipv6 (&local->sin6.sin6_addr); |
| 579 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 580 | |
| 581 | if (!ifp) |
| 582 | return -1; |
| 583 | |
| 584 | nexthop->ifp = ifp; |
| 585 | |
| 586 | /* IPv4 connection. */ |
| 587 | if (local->sa.sa_family == AF_INET) |
| 588 | { |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 589 | /* IPv6 nexthop*/ |
| 590 | ret = if_get_ipv6_global (ifp, &nexthop->v6_global); |
| 591 | |
| 592 | /* There is no global nexthop. */ |
| 593 | if (!ret) |
| 594 | if_get_ipv6_local (ifp, &nexthop->v6_global); |
| 595 | else |
| 596 | if_get_ipv6_local (ifp, &nexthop->v6_local); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 597 | } |
| 598 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 599 | /* IPv6 connection. */ |
| 600 | if (local->sa.sa_family == AF_INET6) |
| 601 | { |
| 602 | struct interface *direct = NULL; |
| 603 | |
Pradosh Mohapatra | 6ee06fa | 2014-01-12 18:30:13 +0000 | [diff] [blame] | 604 | /* IPv4 nexthop. */ |
| 605 | ret = if_get_ipv4_address(ifp, &nexthop->v4); |
| 606 | if (!ret && peer->local_id.s_addr) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 607 | nexthop->v4 = peer->local_id; |
| 608 | |
| 609 | /* Global address*/ |
| 610 | if (! IN6_IS_ADDR_LINKLOCAL (&local->sin6.sin6_addr)) |
| 611 | { |
| 612 | memcpy (&nexthop->v6_global, &local->sin6.sin6_addr, |
| 613 | IPV6_MAX_BYTELEN); |
| 614 | |
| 615 | /* If directory connected set link-local address. */ |
| 616 | direct = if_lookup_by_ipv6 (&remote->sin6.sin6_addr); |
| 617 | if (direct) |
| 618 | if_get_ipv6_local (ifp, &nexthop->v6_local); |
| 619 | } |
| 620 | else |
| 621 | /* Link-local address. */ |
| 622 | { |
| 623 | ret = if_get_ipv6_global (ifp, &nexthop->v6_global); |
| 624 | |
| 625 | /* If there is no global address. Set link-local address as |
| 626 | global. I know this break RFC specification... */ |
| 627 | if (!ret) |
| 628 | memcpy (&nexthop->v6_global, &local->sin6.sin6_addr, |
| 629 | IPV6_MAX_BYTELEN); |
| 630 | else |
| 631 | memcpy (&nexthop->v6_local, &local->sin6.sin6_addr, |
| 632 | IPV6_MAX_BYTELEN); |
| 633 | } |
| 634 | } |
| 635 | |
| 636 | if (IN6_IS_ADDR_LINKLOCAL (&local->sin6.sin6_addr) || |
| 637 | if_lookup_by_ipv6 (&remote->sin6.sin6_addr)) |
| 638 | peer->shared_network = 1; |
| 639 | else |
| 640 | peer->shared_network = 0; |
| 641 | |
| 642 | /* KAME stack specific treatment. */ |
| 643 | #ifdef KAME |
| 644 | if (IN6_IS_ADDR_LINKLOCAL (&nexthop->v6_global) |
| 645 | && IN6_LINKLOCAL_IFINDEX (nexthop->v6_global)) |
| 646 | { |
| 647 | SET_IN6_LINKLOCAL_IFINDEX (nexthop->v6_global, 0); |
| 648 | } |
| 649 | if (IN6_IS_ADDR_LINKLOCAL (&nexthop->v6_local) |
| 650 | && IN6_LINKLOCAL_IFINDEX (nexthop->v6_local)) |
| 651 | { |
| 652 | SET_IN6_LINKLOCAL_IFINDEX (nexthop->v6_local, 0); |
| 653 | } |
| 654 | #endif /* KAME */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 655 | return ret; |
| 656 | } |
| 657 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 658 | void |
G.Balaji | 5a616c0 | 2011-11-26 21:58:42 +0400 | [diff] [blame] | 659 | 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] | 660 | { |
| 661 | int flags; |
| 662 | u_char distance; |
| 663 | struct peer *peer; |
Josh Bailey | 8196f13 | 2011-07-20 20:47:07 -0700 | [diff] [blame] | 664 | struct bgp_info *mpinfo; |
| 665 | size_t oldsize, newsize; |
Ayan Banerjee | b8d1f71 | 2015-11-09 20:14:54 -0500 | [diff] [blame] | 666 | u_int32_t nhcount; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 667 | |
| 668 | if (zclient->sock < 0) |
| 669 | return; |
| 670 | |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 671 | if (! vrf_bitmap_check (zclient->redist[ZEBRA_ROUTE_BGP], VRF_DEFAULT)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 672 | return; |
| 673 | |
| 674 | flags = 0; |
| 675 | peer = info->peer; |
| 676 | |
Jorge Boncompte [DTI2] | 6d85b15 | 2012-05-07 16:52:54 +0000 | [diff] [blame] | 677 | if (peer->sort == BGP_PEER_IBGP || peer->sort == BGP_PEER_CONFED) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 678 | { |
| 679 | SET_FLAG (flags, ZEBRA_FLAG_IBGP); |
| 680 | SET_FLAG (flags, ZEBRA_FLAG_INTERNAL); |
| 681 | } |
| 682 | |
Jorge Boncompte [DTI2] | 6d85b15 | 2012-05-07 16:52:54 +0000 | [diff] [blame] | 683 | if ((peer->sort == BGP_PEER_EBGP && peer->ttl != 1) |
hasso | 6ffd207 | 2005-02-02 14:50:11 +0000 | [diff] [blame] | 684 | || CHECK_FLAG (peer->flags, PEER_FLAG_DISABLE_CONNECTED_CHECK)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 685 | SET_FLAG (flags, ZEBRA_FLAG_INTERNAL); |
| 686 | |
Ayan Banerjee | b8d1f71 | 2015-11-09 20:14:54 -0500 | [diff] [blame] | 687 | nhcount = 1 + bgp_info_mpath_count (info); |
Josh Bailey | 8196f13 | 2011-07-20 20:47:07 -0700 | [diff] [blame] | 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 | |
Ayan Banerjee | b8d1f71 | 2015-11-09 20:14:54 -0500 | [diff] [blame] | 694 | /* resize nexthop buffer size if necessary */ |
| 695 | if ((oldsize = stream_get_size (bgp_nexthop_buf)) < |
| 696 | (sizeof (struct in_addr *) * nhcount)) |
| 697 | { |
| 698 | newsize = (sizeof (struct in_addr *) * nhcount); |
| 699 | newsize = stream_resize (bgp_nexthop_buf, newsize); |
| 700 | if (newsize == oldsize) |
| 701 | { |
| 702 | zlog_err ("can't resize nexthop buffer"); |
| 703 | return; |
| 704 | } |
| 705 | } |
| 706 | stream_reset (bgp_nexthop_buf); |
| 707 | |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 708 | api.vrf_id = VRF_DEFAULT; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 709 | api.flags = flags; |
| 710 | nexthop = &info->attr->nexthop; |
Josh Bailey | 8196f13 | 2011-07-20 20:47:07 -0700 | [diff] [blame] | 711 | stream_put (bgp_nexthop_buf, &nexthop, sizeof (struct in_addr *)); |
| 712 | for (mpinfo = bgp_info_mpath_first (info); mpinfo; |
| 713 | mpinfo = bgp_info_mpath_next (mpinfo)) |
| 714 | { |
| 715 | nexthop = &mpinfo->attr->nexthop; |
| 716 | stream_put (bgp_nexthop_buf, &nexthop, sizeof (struct in_addr *)); |
| 717 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 718 | |
| 719 | api.type = ZEBRA_ROUTE_BGP; |
| 720 | api.message = 0; |
G.Balaji | 5a616c0 | 2011-11-26 21:58:42 +0400 | [diff] [blame] | 721 | api.safi = safi; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 722 | SET_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP); |
Ayan Banerjee | b8d1f71 | 2015-11-09 20:14:54 -0500 | [diff] [blame] | 723 | api.nexthop_num = nhcount; |
Josh Bailey | 8196f13 | 2011-07-20 20:47:07 -0700 | [diff] [blame] | 724 | api.nexthop = (struct in_addr **)STREAM_DATA (bgp_nexthop_buf); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 725 | api.ifindex_num = 0; |
| 726 | SET_FLAG (api.message, ZAPI_MESSAGE_METRIC); |
| 727 | api.metric = info->attr->med; |
| 728 | |
| 729 | distance = bgp_distance_apply (p, info, bgp); |
| 730 | |
| 731 | if (distance) |
| 732 | { |
| 733 | SET_FLAG (api.message, ZAPI_MESSAGE_DISTANCE); |
| 734 | api.distance = distance; |
| 735 | } |
Andrew J. Schorr | a39275d | 2006-11-30 16:36:57 +0000 | [diff] [blame] | 736 | |
| 737 | if (BGP_DEBUG(zebra, ZEBRA)) |
| 738 | { |
Josh Bailey | 8196f13 | 2011-07-20 20:47:07 -0700 | [diff] [blame] | 739 | int i; |
Andrew J. Schorr | a39275d | 2006-11-30 16:36:57 +0000 | [diff] [blame] | 740 | char buf[2][INET_ADDRSTRLEN]; |
Josh Bailey | 8196f13 | 2011-07-20 20:47:07 -0700 | [diff] [blame] | 741 | zlog_debug("Zebra send: IPv4 route add %s/%d nexthop %s metric %u" |
| 742 | " count %d", |
Andrew J. Schorr | a39275d | 2006-11-30 16:36:57 +0000 | [diff] [blame] | 743 | inet_ntop(AF_INET, &p->u.prefix4, buf[0], sizeof(buf[0])), |
| 744 | p->prefixlen, |
Josh Bailey | 8196f13 | 2011-07-20 20:47:07 -0700 | [diff] [blame] | 745 | inet_ntop(AF_INET, api.nexthop[0], buf[1], sizeof(buf[1])), |
| 746 | api.metric, api.nexthop_num); |
| 747 | for (i = 1; i < api.nexthop_num; i++) |
| 748 | zlog_debug("Zebra send: IPv4 route add [nexthop %d] %s", |
| 749 | i, inet_ntop(AF_INET, api.nexthop[i], buf[1], |
| 750 | sizeof(buf[1]))); |
Andrew J. Schorr | a39275d | 2006-11-30 16:36:57 +0000 | [diff] [blame] | 751 | } |
| 752 | |
paul | 0a58935 | 2004-05-08 11:48:26 +0000 | [diff] [blame] | 753 | zapi_ipv4_route (ZEBRA_IPV4_ROUTE_ADD, zclient, |
| 754 | (struct prefix_ipv4 *) p, &api); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 755 | } |
Lou Berger | 205e674 | 2016-01-12 13:42:11 -0500 | [diff] [blame] | 756 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 757 | /* We have to think about a IPv6 link-local address curse. */ |
| 758 | if (p->family == AF_INET6) |
| 759 | { |
Paul Jakma | 9099f9b | 2016-01-18 10:12:10 +0000 | [diff] [blame] | 760 | ifindex_t ifindex; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 761 | struct in6_addr *nexthop; |
| 762 | struct zapi_ipv6 api; |
Ayan Banerjee | b8d1f71 | 2015-11-09 20:14:54 -0500 | [diff] [blame] | 763 | int valid_nh_count = 0; |
| 764 | |
| 765 | /* resize nexthop buffer size if necessary */ |
| 766 | if ((oldsize = stream_get_size (bgp_nexthop_buf)) < |
| 767 | (sizeof (struct in6_addr *) * nhcount)) |
| 768 | { |
| 769 | newsize = (sizeof (struct in6_addr *) * nhcount); |
| 770 | newsize = stream_resize (bgp_nexthop_buf, newsize); |
| 771 | if (newsize == oldsize) |
| 772 | { |
| 773 | zlog_err ("can't resize nexthop buffer"); |
| 774 | return; |
| 775 | } |
| 776 | } |
| 777 | stream_reset (bgp_nexthop_buf); |
| 778 | |
| 779 | /* resize ifindices buffer size if necessary */ |
| 780 | if ((oldsize = stream_get_size (bgp_ifindices_buf)) < |
| 781 | (sizeof (unsigned int) * nhcount)) |
| 782 | { |
| 783 | newsize = (sizeof (unsigned int) * nhcount); |
| 784 | newsize = stream_resize (bgp_ifindices_buf, newsize); |
| 785 | if (newsize == oldsize) |
| 786 | { |
| 787 | zlog_err ("can't resize nexthop buffer"); |
| 788 | return; |
| 789 | } |
| 790 | } |
| 791 | stream_reset (bgp_ifindices_buf); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 792 | |
| 793 | ifindex = 0; |
| 794 | nexthop = NULL; |
Ayan Banerjee | b8d1f71 | 2015-11-09 20:14:54 -0500 | [diff] [blame] | 795 | |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 796 | assert (info->attr->extra); |
| 797 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 798 | /* Only global address nexthop exists. */ |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 799 | if (info->attr->extra->mp_nexthop_len == 16) |
| 800 | nexthop = &info->attr->extra->mp_nexthop_global; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 801 | |
| 802 | /* If both global and link-local address present. */ |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 803 | if (info->attr->extra->mp_nexthop_len == 32) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 804 | { |
| 805 | /* Workaround for Cisco's nexthop bug. */ |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 806 | if (IN6_IS_ADDR_UNSPECIFIED (&info->attr->extra->mp_nexthop_global) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 807 | && peer->su_remote->sa.sa_family == AF_INET6) |
| 808 | nexthop = &peer->su_remote->sin6.sin6_addr; |
| 809 | else |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 810 | nexthop = &info->attr->extra->mp_nexthop_local; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 811 | |
| 812 | if (info->peer->nexthop.ifp) |
| 813 | ifindex = info->peer->nexthop.ifp->ifindex; |
| 814 | } |
| 815 | |
| 816 | if (nexthop == NULL) |
| 817 | return; |
| 818 | |
Dinesh Dutt | 4feb0d0 | 2015-11-09 20:14:55 -0500 | [diff] [blame^] | 819 | if (!ifindex) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 820 | { |
| 821 | if (info->peer->ifname) |
Feng Lu | 395828e | 2015-05-22 11:39:55 +0200 | [diff] [blame] | 822 | ifindex = ifname2ifindex (info->peer->ifname); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 823 | else if (info->peer->nexthop.ifp) |
| 824 | ifindex = info->peer->nexthop.ifp->ifindex; |
| 825 | } |
Ayan Banerjee | b8d1f71 | 2015-11-09 20:14:54 -0500 | [diff] [blame] | 826 | stream_put (bgp_nexthop_buf, &nexthop, sizeof (struct in6_addr *)); |
| 827 | stream_put (bgp_ifindices_buf, &ifindex, sizeof (unsigned int)); |
| 828 | valid_nh_count++; |
| 829 | |
| 830 | for (mpinfo = bgp_info_mpath_first (info); mpinfo; |
| 831 | mpinfo = bgp_info_mpath_next (mpinfo)) |
| 832 | { |
Dinesh Dutt | 4feb0d0 | 2015-11-09 20:14:55 -0500 | [diff] [blame^] | 833 | ifindex = 0; |
| 834 | |
Ayan Banerjee | b8d1f71 | 2015-11-09 20:14:54 -0500 | [diff] [blame] | 835 | /* Only global address nexthop exists. */ |
| 836 | if (mpinfo->attr->extra->mp_nexthop_len == 16) |
Ayan Banerjee | b8d1f71 | 2015-11-09 20:14:54 -0500 | [diff] [blame] | 837 | nexthop = &mpinfo->attr->extra->mp_nexthop_global; |
Dinesh Dutt | 4feb0d0 | 2015-11-09 20:14:55 -0500 | [diff] [blame^] | 838 | |
Ayan Banerjee | b8d1f71 | 2015-11-09 20:14:54 -0500 | [diff] [blame] | 839 | /* If both global and link-local address present. */ |
| 840 | if (mpinfo->attr->extra->mp_nexthop_len == 32) |
| 841 | { |
| 842 | /* Workaround for Cisco's nexthop bug. */ |
| 843 | if (IN6_IS_ADDR_UNSPECIFIED (&mpinfo->attr->extra->mp_nexthop_global) |
| 844 | && mpinfo->peer->su_remote->sa.sa_family == AF_INET6) |
| 845 | { |
| 846 | nexthop = &mpinfo->peer->su_remote->sin6.sin6_addr; |
| 847 | } |
| 848 | else |
| 849 | { |
| 850 | nexthop = &mpinfo->attr->extra->mp_nexthop_local; |
| 851 | } |
| 852 | |
| 853 | if (mpinfo->peer->nexthop.ifp) |
| 854 | { |
| 855 | ifindex = mpinfo->peer->nexthop.ifp->ifindex; |
| 856 | } |
| 857 | } |
Dinesh Dutt | 4feb0d0 | 2015-11-09 20:14:55 -0500 | [diff] [blame^] | 858 | |
Ayan Banerjee | b8d1f71 | 2015-11-09 20:14:54 -0500 | [diff] [blame] | 859 | if (nexthop == NULL) |
| 860 | { |
| 861 | continue; |
| 862 | } |
| 863 | |
Dinesh Dutt | 4feb0d0 | 2015-11-09 20:14:55 -0500 | [diff] [blame^] | 864 | if (!ifindex) |
Ayan Banerjee | b8d1f71 | 2015-11-09 20:14:54 -0500 | [diff] [blame] | 865 | { |
| 866 | if (mpinfo->peer->ifname) |
| 867 | { |
| 868 | ifindex = if_nametoindex (mpinfo->peer->ifname); |
| 869 | } |
| 870 | else if (mpinfo->peer->nexthop.ifp) |
| 871 | { |
| 872 | ifindex = mpinfo->peer->nexthop.ifp->ifindex; |
| 873 | } |
| 874 | } |
Dinesh Dutt | 4feb0d0 | 2015-11-09 20:14:55 -0500 | [diff] [blame^] | 875 | |
Ayan Banerjee | b8d1f71 | 2015-11-09 20:14:54 -0500 | [diff] [blame] | 876 | if (ifindex == 0) |
| 877 | { |
| 878 | continue; |
| 879 | } |
| 880 | |
| 881 | stream_put (bgp_nexthop_buf, &nexthop, sizeof (struct in6_addr *)); |
| 882 | stream_put (bgp_ifindices_buf, &ifindex, sizeof (unsigned int)); |
| 883 | valid_nh_count++; |
| 884 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 885 | |
| 886 | /* Make Zebra API structure. */ |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 887 | api.vrf_id = VRF_DEFAULT; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 888 | api.flags = flags; |
| 889 | api.type = ZEBRA_ROUTE_BGP; |
| 890 | api.message = 0; |
G.Balaji | c7ec179 | 2011-11-26 22:04:05 +0400 | [diff] [blame] | 891 | api.safi = safi; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 892 | SET_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP); |
Ayan Banerjee | b8d1f71 | 2015-11-09 20:14:54 -0500 | [diff] [blame] | 893 | api.nexthop_num = valid_nh_count; |
| 894 | api.nexthop = (struct in6_addr **)STREAM_DATA (bgp_nexthop_buf); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 895 | SET_FLAG (api.message, ZAPI_MESSAGE_IFINDEX); |
Ayan Banerjee | b8d1f71 | 2015-11-09 20:14:54 -0500 | [diff] [blame] | 896 | api.ifindex_num = valid_nh_count; |
| 897 | api.ifindex = (ifindex_t *)STREAM_DATA (bgp_ifindices_buf); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 898 | SET_FLAG (api.message, ZAPI_MESSAGE_METRIC); |
| 899 | api.metric = info->attr->med; |
| 900 | |
Roman Hoog Antink | 6184c39 | 2014-03-17 14:01:42 +0100 | [diff] [blame] | 901 | distance = ipv6_bgp_distance_apply (p, info, bgp); |
| 902 | |
| 903 | if (distance) |
| 904 | { |
| 905 | SET_FLAG (api.message, ZAPI_MESSAGE_DISTANCE); |
| 906 | api.distance = distance; |
| 907 | } |
| 908 | |
Andrew J. Schorr | a39275d | 2006-11-30 16:36:57 +0000 | [diff] [blame] | 909 | if (BGP_DEBUG(zebra, ZEBRA)) |
| 910 | { |
| 911 | char buf[2][INET6_ADDRSTRLEN]; |
| 912 | zlog_debug("Zebra send: IPv6 route add %s/%d nexthop %s metric %u", |
| 913 | inet_ntop(AF_INET6, &p->u.prefix6, buf[0], sizeof(buf[0])), |
| 914 | p->prefixlen, |
| 915 | inet_ntop(AF_INET6, nexthop, buf[1], sizeof(buf[1])), |
| 916 | api.metric); |
| 917 | } |
| 918 | |
paul | 0a58935 | 2004-05-08 11:48:26 +0000 | [diff] [blame] | 919 | zapi_ipv6_route (ZEBRA_IPV6_ROUTE_ADD, zclient, |
| 920 | (struct prefix_ipv6 *) p, &api); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 921 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 922 | } |
| 923 | |
| 924 | void |
G.Balaji | 5a616c0 | 2011-11-26 21:58:42 +0400 | [diff] [blame] | 925 | bgp_zebra_withdraw (struct prefix *p, struct bgp_info *info, safi_t safi) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 926 | { |
| 927 | int flags; |
| 928 | struct peer *peer; |
| 929 | |
| 930 | if (zclient->sock < 0) |
| 931 | return; |
| 932 | |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 933 | if (! vrf_bitmap_check (zclient->redist[ZEBRA_ROUTE_BGP], VRF_DEFAULT)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 934 | return; |
| 935 | |
| 936 | peer = info->peer; |
| 937 | flags = 0; |
| 938 | |
Jorge Boncompte [DTI2] | 6d85b15 | 2012-05-07 16:52:54 +0000 | [diff] [blame] | 939 | if (peer->sort == BGP_PEER_IBGP) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 940 | { |
| 941 | SET_FLAG (flags, ZEBRA_FLAG_INTERNAL); |
| 942 | SET_FLAG (flags, ZEBRA_FLAG_IBGP); |
| 943 | } |
| 944 | |
Jorge Boncompte [DTI2] | 6d85b15 | 2012-05-07 16:52:54 +0000 | [diff] [blame] | 945 | if ((peer->sort == BGP_PEER_EBGP && peer->ttl != 1) |
hasso | 6ffd207 | 2005-02-02 14:50:11 +0000 | [diff] [blame] | 946 | || CHECK_FLAG (peer->flags, PEER_FLAG_DISABLE_CONNECTED_CHECK)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 947 | SET_FLAG (flags, ZEBRA_FLAG_INTERNAL); |
| 948 | |
| 949 | if (p->family == AF_INET) |
| 950 | { |
| 951 | struct zapi_ipv4 api; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 952 | |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 953 | api.vrf_id = VRF_DEFAULT; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 954 | api.flags = flags; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 955 | |
| 956 | api.type = ZEBRA_ROUTE_BGP; |
| 957 | api.message = 0; |
G.Balaji | 5a616c0 | 2011-11-26 21:58:42 +0400 | [diff] [blame] | 958 | api.safi = safi; |
Paul Jakma | 64e0ac2 | 2015-11-18 16:00:54 +0000 | [diff] [blame] | 959 | api.nexthop_num = 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 960 | api.ifindex_num = 0; |
| 961 | SET_FLAG (api.message, ZAPI_MESSAGE_METRIC); |
| 962 | api.metric = info->attr->med; |
| 963 | |
Andrew J. Schorr | a39275d | 2006-11-30 16:36:57 +0000 | [diff] [blame] | 964 | if (BGP_DEBUG(zebra, ZEBRA)) |
| 965 | { |
| 966 | char buf[2][INET_ADDRSTRLEN]; |
Paul Jakma | 64e0ac2 | 2015-11-18 16:00:54 +0000 | [diff] [blame] | 967 | zlog_debug("Zebra send: IPv4 route delete %s/%d metric %u", |
Andrew J. Schorr | a39275d | 2006-11-30 16:36:57 +0000 | [diff] [blame] | 968 | inet_ntop(AF_INET, &p->u.prefix4, buf[0], sizeof(buf[0])), |
| 969 | p->prefixlen, |
Andrew J. Schorr | a39275d | 2006-11-30 16:36:57 +0000 | [diff] [blame] | 970 | api.metric); |
| 971 | } |
| 972 | |
paul | 0a58935 | 2004-05-08 11:48:26 +0000 | [diff] [blame] | 973 | zapi_ipv4_route (ZEBRA_IPV4_ROUTE_DELETE, zclient, |
| 974 | (struct prefix_ipv4 *) p, &api); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 975 | } |
Lou Berger | 205e674 | 2016-01-12 13:42:11 -0500 | [diff] [blame] | 976 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 977 | /* We have to think about a IPv6 link-local address curse. */ |
| 978 | if (p->family == AF_INET6) |
| 979 | { |
| 980 | struct zapi_ipv6 api; |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 981 | |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 982 | api.vrf_id = VRF_DEFAULT; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 983 | api.flags = flags; |
| 984 | api.type = ZEBRA_ROUTE_BGP; |
| 985 | api.message = 0; |
G.Balaji | c7ec179 | 2011-11-26 22:04:05 +0400 | [diff] [blame] | 986 | api.safi = safi; |
Paul Jakma | 64e0ac2 | 2015-11-18 16:00:54 +0000 | [diff] [blame] | 987 | api.nexthop_num = 0; |
| 988 | api.ifindex_num = 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 989 | SET_FLAG (api.message, ZAPI_MESSAGE_METRIC); |
| 990 | api.metric = info->attr->med; |
| 991 | |
Andrew J. Schorr | a39275d | 2006-11-30 16:36:57 +0000 | [diff] [blame] | 992 | if (BGP_DEBUG(zebra, ZEBRA)) |
| 993 | { |
| 994 | char buf[2][INET6_ADDRSTRLEN]; |
Paul Jakma | 64e0ac2 | 2015-11-18 16:00:54 +0000 | [diff] [blame] | 995 | zlog_debug("Zebra send: IPv6 route delete %s/%d metric %u", |
Andrew J. Schorr | a39275d | 2006-11-30 16:36:57 +0000 | [diff] [blame] | 996 | inet_ntop(AF_INET6, &p->u.prefix6, buf[0], sizeof(buf[0])), |
| 997 | p->prefixlen, |
Andrew J. Schorr | a39275d | 2006-11-30 16:36:57 +0000 | [diff] [blame] | 998 | api.metric); |
| 999 | } |
| 1000 | |
paul | 0a58935 | 2004-05-08 11:48:26 +0000 | [diff] [blame] | 1001 | zapi_ipv6_route (ZEBRA_IPV6_ROUTE_DELETE, zclient, |
| 1002 | (struct prefix_ipv6 *) p, &api); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1003 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1004 | } |
David Lamparter | 6b0655a | 2014-06-04 06:53:35 +0200 | [diff] [blame] | 1005 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1006 | /* Other routes redistribution into BGP. */ |
| 1007 | int |
| 1008 | bgp_redistribute_set (struct bgp *bgp, afi_t afi, int type) |
| 1009 | { |
| 1010 | /* Set flag to BGP instance. */ |
| 1011 | bgp->redist[afi][type] = 1; |
| 1012 | |
| 1013 | /* Return if already redistribute flag is set. */ |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 1014 | if (vrf_bitmap_check (zclient->redist[type], VRF_DEFAULT)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1015 | return CMD_WARNING; |
| 1016 | |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 1017 | vrf_bitmap_set (zclient->redist[type], VRF_DEFAULT); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1018 | |
| 1019 | /* Return if zebra connection is not established. */ |
| 1020 | if (zclient->sock < 0) |
| 1021 | return CMD_WARNING; |
Andrew J. Schorr | a39275d | 2006-11-30 16:36:57 +0000 | [diff] [blame] | 1022 | |
| 1023 | if (BGP_DEBUG(zebra, ZEBRA)) |
| 1024 | zlog_debug("Zebra send: redistribute add %s", zebra_route_string(type)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1025 | |
| 1026 | /* Send distribute add message to zebra. */ |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 1027 | zebra_redistribute_send (ZEBRA_REDISTRIBUTE_ADD, zclient, type, VRF_DEFAULT); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1028 | |
| 1029 | return CMD_SUCCESS; |
| 1030 | } |
| 1031 | |
| 1032 | /* Redistribute with route-map specification. */ |
| 1033 | int |
paul | fd79ac9 | 2004-10-13 05:06:08 +0000 | [diff] [blame] | 1034 | bgp_redistribute_rmap_set (struct bgp *bgp, afi_t afi, int type, |
| 1035 | const char *name) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1036 | { |
| 1037 | if (bgp->rmap[afi][type].name |
| 1038 | && (strcmp (bgp->rmap[afi][type].name, name) == 0)) |
| 1039 | return 0; |
| 1040 | |
| 1041 | if (bgp->rmap[afi][type].name) |
| 1042 | free (bgp->rmap[afi][type].name); |
| 1043 | bgp->rmap[afi][type].name = strdup (name); |
| 1044 | bgp->rmap[afi][type].map = route_map_lookup_by_name (name); |
| 1045 | |
| 1046 | return 1; |
| 1047 | } |
| 1048 | |
| 1049 | /* Redistribute with metric specification. */ |
| 1050 | int |
| 1051 | bgp_redistribute_metric_set (struct bgp *bgp, afi_t afi, int type, |
| 1052 | u_int32_t metric) |
| 1053 | { |
| 1054 | if (bgp->redist_metric_flag[afi][type] |
| 1055 | && bgp->redist_metric[afi][type] == metric) |
| 1056 | return 0; |
| 1057 | |
| 1058 | bgp->redist_metric_flag[afi][type] = 1; |
| 1059 | bgp->redist_metric[afi][type] = metric; |
| 1060 | |
| 1061 | return 1; |
| 1062 | } |
| 1063 | |
| 1064 | /* Unset redistribution. */ |
| 1065 | int |
| 1066 | bgp_redistribute_unset (struct bgp *bgp, afi_t afi, int type) |
| 1067 | { |
| 1068 | /* Unset flag from BGP instance. */ |
| 1069 | bgp->redist[afi][type] = 0; |
| 1070 | |
| 1071 | /* Unset route-map. */ |
| 1072 | if (bgp->rmap[afi][type].name) |
| 1073 | free (bgp->rmap[afi][type].name); |
| 1074 | bgp->rmap[afi][type].name = NULL; |
| 1075 | bgp->rmap[afi][type].map = NULL; |
| 1076 | |
| 1077 | /* Unset metric. */ |
| 1078 | bgp->redist_metric_flag[afi][type] = 0; |
| 1079 | bgp->redist_metric[afi][type] = 0; |
| 1080 | |
| 1081 | /* Return if zebra connection is disabled. */ |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 1082 | if (! vrf_bitmap_check (zclient->redist[type], VRF_DEFAULT)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1083 | return CMD_WARNING; |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 1084 | vrf_bitmap_unset (zclient->redist[type], VRF_DEFAULT); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1085 | |
| 1086 | if (bgp->redist[AFI_IP][type] == 0 |
| 1087 | && bgp->redist[AFI_IP6][type] == 0 |
| 1088 | && zclient->sock >= 0) |
Andrew J. Schorr | a39275d | 2006-11-30 16:36:57 +0000 | [diff] [blame] | 1089 | { |
| 1090 | /* Send distribute delete message to zebra. */ |
| 1091 | if (BGP_DEBUG(zebra, ZEBRA)) |
| 1092 | zlog_debug("Zebra send: redistribute delete %s", |
| 1093 | zebra_route_string(type)); |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 1094 | zebra_redistribute_send (ZEBRA_REDISTRIBUTE_DELETE, zclient, type, |
| 1095 | VRF_DEFAULT); |
Andrew J. Schorr | a39275d | 2006-11-30 16:36:57 +0000 | [diff] [blame] | 1096 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1097 | |
| 1098 | /* Withdraw redistributed routes from current BGP's routing table. */ |
| 1099 | bgp_redistribute_withdraw (bgp, afi, type); |
| 1100 | |
| 1101 | return CMD_SUCCESS; |
| 1102 | } |
| 1103 | |
| 1104 | /* Unset redistribution route-map configuration. */ |
| 1105 | int |
| 1106 | bgp_redistribute_routemap_unset (struct bgp *bgp, afi_t afi, int type) |
| 1107 | { |
| 1108 | if (! bgp->rmap[afi][type].name) |
| 1109 | return 0; |
| 1110 | |
| 1111 | /* Unset route-map. */ |
| 1112 | free (bgp->rmap[afi][type].name); |
| 1113 | bgp->rmap[afi][type].name = NULL; |
| 1114 | bgp->rmap[afi][type].map = NULL; |
| 1115 | |
| 1116 | return 1; |
| 1117 | } |
| 1118 | |
| 1119 | /* Unset redistribution metric configuration. */ |
| 1120 | int |
| 1121 | bgp_redistribute_metric_unset (struct bgp *bgp, afi_t afi, int type) |
| 1122 | { |
| 1123 | if (! bgp->redist_metric_flag[afi][type]) |
| 1124 | return 0; |
| 1125 | |
| 1126 | /* Unset metric. */ |
| 1127 | bgp->redist_metric_flag[afi][type] = 0; |
| 1128 | bgp->redist_metric[afi][type] = 0; |
| 1129 | |
| 1130 | return 1; |
| 1131 | } |
David Lamparter | 6b0655a | 2014-06-04 06:53:35 +0200 | [diff] [blame] | 1132 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1133 | void |
paul | 94f2b39 | 2005-06-28 12:44:16 +0000 | [diff] [blame] | 1134 | bgp_zclient_reset (void) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1135 | { |
| 1136 | zclient_reset (zclient); |
| 1137 | } |
| 1138 | |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 1139 | static void |
| 1140 | bgp_zebra_connected (struct zclient *zclient) |
| 1141 | { |
| 1142 | zclient_send_requests (zclient, VRF_DEFAULT); |
| 1143 | } |
| 1144 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1145 | void |
Donald Sharp | 7125293 | 2015-09-24 09:25:19 -0400 | [diff] [blame] | 1146 | bgp_zebra_init (struct thread_master *master) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1147 | { |
| 1148 | /* Set default values. */ |
Donald Sharp | 7125293 | 2015-09-24 09:25:19 -0400 | [diff] [blame] | 1149 | zclient = zclient_new (master); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1150 | zclient_init (zclient, ZEBRA_ROUTE_BGP); |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 1151 | zclient->zebra_connected = bgp_zebra_connected; |
hasso | 18a6dce | 2004-10-03 18:18:34 +0000 | [diff] [blame] | 1152 | zclient->router_id_update = bgp_router_id_update; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1153 | zclient->interface_add = bgp_interface_add; |
| 1154 | zclient->interface_delete = bgp_interface_delete; |
| 1155 | zclient->interface_address_add = bgp_interface_address_add; |
| 1156 | zclient->interface_address_delete = bgp_interface_address_delete; |
| 1157 | zclient->ipv4_route_add = zebra_read_ipv4; |
| 1158 | zclient->ipv4_route_delete = zebra_read_ipv4; |
| 1159 | zclient->interface_up = bgp_interface_up; |
| 1160 | zclient->interface_down = bgp_interface_down; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1161 | zclient->ipv6_route_add = zebra_read_ipv6; |
| 1162 | zclient->ipv6_route_delete = zebra_read_ipv6; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1163 | |
Josh Bailey | 8196f13 | 2011-07-20 20:47:07 -0700 | [diff] [blame] | 1164 | bgp_nexthop_buf = stream_new(BGP_NEXTHOP_BUF_SIZE); |
Ayan Banerjee | b8d1f71 | 2015-11-09 20:14:54 -0500 | [diff] [blame] | 1165 | bgp_ifindices_buf = stream_new(BGP_IFINDICES_BUF_SIZE); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1166 | } |
Lou Berger | 82dd707 | 2016-01-12 13:41:57 -0500 | [diff] [blame] | 1167 | |
| 1168 | void |
| 1169 | bgp_zebra_destroy(void) |
| 1170 | { |
| 1171 | if (zclient == NULL) |
| 1172 | return; |
| 1173 | zclient_stop(zclient); |
| 1174 | zclient_free(zclient); |
| 1175 | zclient = NULL; |
| 1176 | } |