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