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