2 * Copyright (c) 2020 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
17 * @brief FIB Source Address selection
19 * Use the FIB for source address selection on an interface
22 #include <vnet/fib/fib_sas.h>
23 #include <vnet/adj/adj_glean.h>
24 #include <vnet/ip/ip6_link.h>
28 fib_sas_get (u32 sw_if_index,
29 ip_address_family_t af,
30 const ip46_address_t *dst,
37 return (fib_sas4_get(sw_if_index, &dst->ip4, &src->ip4));
39 return (fib_sas4_get(sw_if_index, NULL, &src->ip4));
42 return (fib_sas6_get(sw_if_index, &dst->ip6, &src->ip6));
44 return (fib_sas6_get(sw_if_index, NULL, &src->ip6));
50 fib_sas4_get (u32 sw_if_index,
51 const ip4_address_t *dst,
54 ip46_address_t d_tmp, *d_tmpp = NULL;
55 const ip46_address_t *s_tmp;
56 vnet_sw_interface_t *swif;
64 if (vnet_sw_interface_is_p2p(vnet_get_main(), sw_if_index))
67 ip4 = ip_interface_get_first_ip (sw_if_index, 1);
69 src->as_u32 = ip4->as_u32;
77 * If the interface is unnumbered then use the IP interface
79 swif = vnet_get_sw_interface (vnet_get_main(), sw_if_index);
81 if (swif->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED)
82 sw_if_index = swif->unnumbered_sw_if_index;
85 * get the source address from the glean adjacency
87 s_tmp = adj_glean_get_src (FIB_PROTOCOL_IP4, sw_if_index, d_tmpp);
91 src->as_u32 = s_tmp->ip4.as_u32;
99 fib_sas6_get (u32 sw_if_index,
100 const ip6_address_t *dst,
103 ip46_address_t d_tmp, *d_tmpp = NULL;
104 const ip46_address_t *s_tmp;
113 * if the dst is v6 and link local, use the source link local
115 if (dst && ip6_address_is_link_local_unicast (dst))
117 const ip6_address_t *ll = ip6_get_link_local_address (sw_if_index);
122 ip6_address_copy (src, ll);
126 if (vnet_sw_interface_is_p2p(vnet_get_main(), sw_if_index))
129 ip6 = ip_interface_get_first_ip (sw_if_index, 0);
131 ip6_address_copy(src, ip6);
139 * get the source address from the glean adjacency
141 s_tmp = adj_glean_get_src (FIB_PROTOCOL_IP6, sw_if_index, d_tmpp);
145 ip6_address_copy(src, &s_tmp->ip6);