fib: Source Address Selection
[vpp.git] / src / vnet / fib / fib_sas.c
1 /*
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:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
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.
14  */
15
16 /**
17  * @brief FIB Source Address selection
18  *
19  * Use the FIB for source address selection on an interface
20  */
21
22 #include <vnet/fib/fib_sas.h>
23 #include <vnet/adj/adj_glean.h>
24 #include <vnet/ip/ip6_link.h>
25
26
27 bool
28 fib_sas_get (u32 sw_if_index,
29              ip_address_family_t af,
30              const ip46_address_t *dst,
31              ip46_address_t *src)
32 {
33     switch (af)
34     {
35     case AF_IP4:
36         if (dst)
37             return (fib_sas4_get(sw_if_index, &dst->ip4, &src->ip4));
38         else
39             return (fib_sas4_get(sw_if_index, NULL, &src->ip4));
40     case AF_IP6:
41         if (dst)
42             return (fib_sas6_get(sw_if_index, &dst->ip6, &src->ip6));
43         else
44             return (fib_sas6_get(sw_if_index, NULL, &src->ip6));
45     }
46     return (false);
47 }
48
49 bool
50 fib_sas4_get (u32 sw_if_index,
51               const ip4_address_t *dst,
52               ip4_address_t *src)
53 {
54     ip46_address_t d_tmp, *d_tmpp = NULL;
55     const ip46_address_t *s_tmp;
56     vnet_sw_interface_t *swif;
57
58     if (dst)
59     {
60         d_tmpp = &d_tmp;
61         d_tmp.ip4 = *dst;
62     }
63
64     /*
65      * If the interface is unnumbered then use the IP interface
66      */
67     swif = vnet_get_sw_interface (vnet_get_main(), sw_if_index);
68
69     if (swif->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED)
70         sw_if_index = swif->unnumbered_sw_if_index;
71
72     /*
73      * get the source address from the glean adjacency
74      */
75     s_tmp = adj_glean_get_src (FIB_PROTOCOL_IP4, sw_if_index, d_tmpp);
76
77     if (NULL != s_tmp)
78     {
79         src->as_u32 = s_tmp->ip4.as_u32;
80         return (true);
81     }
82
83     return (false);
84 }
85
86 bool
87 fib_sas6_get (u32 sw_if_index,
88               const ip6_address_t *dst,
89               ip6_address_t *src)
90 {
91     ip46_address_t d_tmp, *d_tmpp = NULL;
92     const ip46_address_t *s_tmp;
93
94     if (dst)
95     {
96         d_tmpp = &d_tmp;
97         d_tmp.ip6 = *dst;
98     }
99
100     /*
101      * if the dst is v6 and link local, use the source link local
102      */
103     if (ip6_address_is_link_local_unicast (dst))
104     {
105         ip6_address_copy (src, ip6_get_link_local_address (sw_if_index));
106         return (true);
107     }
108
109     /*
110      * get the source address from the glean adjacency
111      */
112     s_tmp = adj_glean_get_src (FIB_PROTOCOL_IP6, sw_if_index, d_tmpp);
113
114     if (NULL != s_tmp)
115     {
116         ip6_address_copy(src, &s_tmp->ip6);
117         return (true);
118     }
119
120     return (false);
121 }