FD.io VPP  v21.10.1-2-g0a485f517
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;
57 
58  if (dst)
59  {
60  d_tmpp = &d_tmp;
61  d_tmp.ip4 = *dst;
62  }
63 
65  {
68  if (ip4) {
69  src->as_u32 = ip4->as_u32;
70  return (true);
71  } else {
72  return (false);
73  }
74  }
75 
76  /*
77  * If the interface is unnumbered then use the IP interface
78  */
80 
82  sw_if_index = swif->unnumbered_sw_if_index;
83 
84  /*
85  * get the source address from the glean adjacency
86  */
88 
89  if (NULL != s_tmp)
90  {
91  src->as_u32 = s_tmp->ip4.as_u32;
92  return (true);
93  }
94 
95  return (false);
96 }
97 
98 bool
100  const ip6_address_t *dst,
101  ip6_address_t *src)
102 {
103  ip46_address_t d_tmp, *d_tmpp = NULL;
104  const ip46_address_t *s_tmp;
105 
106  if (dst)
107  {
108  d_tmpp = &d_tmp;
109  d_tmp.ip6 = *dst;
110  }
111 
112  /*
113  * if the dst is v6 and link local, use the source link local
114  */
116  {
117  const ip6_address_t *ll = ip6_get_link_local_address (sw_if_index);
118  if (NULL == ll)
119  {
120  return (false);
121  }
122  ip6_address_copy (src, ll);
123  return (true);
124  }
125 
127  {
128  ip6_address_t *ip6;
130  if (ip6) {
132  return (true);
133  } else {
134  return (false);
135  }
136  }
137 
138  /*
139  * get the source address from the glean adjacency
140  */
142 
143  if (NULL != s_tmp)
144  {
145  ip6_address_copy(src, &s_tmp->ip6);
146  return (true);
147  }
148 
149  return (false);
150 }
ip6_address_is_link_local_unicast
static uword ip6_address_is_link_local_unicast(const ip6_address_t *a)
Definition: ip6_packet.h:253
adj_glean_get_src
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:251
vnet_sw_interface_t
Definition: interface.h:869
ip4
vl_api_ip4_address_t ip4
Definition: one.api:376
AF_IP4
@ AF_IP4
Definition: ip_types.h:23
vnet_get_sw_interface
static vnet_sw_interface_t * vnet_get_sw_interface(vnet_main_t *vnm, u32 sw_if_index)
Definition: interface_funcs.h:58
ip_interface_get_first_ip
void * ip_interface_get_first_ip(u32 sw_if_index, u8 is_ip4)
Definition: ip_interface.c:174
vnet_sw_interface_is_p2p
int vnet_sw_interface_is_p2p(vnet_main_t *vnm, u32 sw_if_index)
Definition: interface.c:1313
vnet_get_main
vnet_main_t * vnet_get_main(void)
Definition: pnat_test_stubs.h:56
VNET_SW_INTERFACE_FLAG_UNNUMBERED
@ VNET_SW_INTERFACE_FLAG_UNNUMBERED
Definition: interface.h:851
src
vl_api_address_t src
Definition: gre.api:54
ip4_address_t
Definition: ip4_packet.h:50
fib_sas6_get
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:99
ip6_address_copy
static void ip6_address_copy(ip6_address_t *dst, const ip6_address_t *src)
Definition: ip6_packet.h:127
FIB_PROTOCOL_IP4
@ FIB_PROTOCOL_IP4
Definition: fib_types.h:36
fib_sas.h
fib_sas4_get
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
u32
unsigned int u32
Definition: types.h:88
af
vl_api_address_family_t af
Definition: ip.api:619
ip6
vl_api_ip6_address_t ip6
Definition: one.api:424
FIB_PROTOCOL_IP6
@ FIB_PROTOCOL_IP6
Definition: fib_types.h:37
dst
vl_api_ip4_address_t dst
Definition: pnat.api:41
AF_IP6
@ AF_IP6
Definition: ip_types.h:24
adj_glean.h
fib_sas_get
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
sw_if_index
vl_api_interface_index_t sw_if_index
Definition: wireguard.api:34
ip_address_family_t
enum ip_address_family_t_ ip_address_family_t
swif
u32 swif
Definition: interface_output.c:1096