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