FD.io VPP  v21.01.1
Vector Packet Processing
fib_sas.c
Go to the documentation of this file.
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
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
51  const ip4_address_t *dst,
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 
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
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  */
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 }
vnet_main_t * vnet_get_main(void)
Definition: misc.c:46
vl_api_address_t src
Definition: gre.api:54
static vnet_sw_interface_t * vnet_get_sw_interface(vnet_main_t *vnm, u32 sw_if_index)
const ip46_address_t * adj_glean_get_src(fib_protocol_t proto, u32 sw_if_index, const ip46_address_t *nh)
Return the source address from the glean.
Definition: adj_glean.c:257
bool fib_sas6_get(u32 sw_if_index, const ip6_address_t *dst, ip6_address_t *src)
Get an IPv6 Source address to use in a packet being sent out an interface.
Definition: fib_sas.c:87
unsigned int u32
Definition: types.h:88
bool fib_sas_get(u32 sw_if_index, ip_address_family_t af, const ip46_address_t *dst, ip46_address_t *src)
FIB Source Address selection.
Definition: fib_sas.c:28
vnet_sw_interface_flags_t flags
Definition: interface.h:739
vl_api_address_t dst
Definition: gre.api:55
static uword ip6_address_is_link_local_unicast(const ip6_address_t *a)
Definition: ip6_packet.h:253
enum ip_address_family_t_ ip_address_family_t
bool fib_sas4_get(u32 sw_if_index, const ip4_address_t *dst, ip4_address_t *src)
Get an IPv4 Source address to use in a packet being sent out an interface.
Definition: fib_sas.c:50
static void ip6_address_copy(ip6_address_t *dst, const ip6_address_t *src)
Definition: ip6_packet.h:127
vl_api_interface_index_t sw_if_index
Definition: wireguard.api:34