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