FD.io VPP  v21.10.1-2-g0a485f517
Vector Packet Processing
ip6_inlines.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016 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  * ip/ip6.h: ip6 main include file
17  *
18  * Copyright (c) 2008 Eliot Dresselhaus
19  *
20  * Permission is hereby granted, free of charge, to any person obtaining
21  * a copy of this software and associated documentation files (the
22  * "Software"), to deal in the Software without restriction, including
23  * without limitation the rights to use, copy, modify, merge, publish,
24  * distribute, sublicense, and/or sell copies of the Software, and to
25  * permit persons to whom the Software is furnished to do so, subject to
26  * the following conditions:
27  *
28  * The above copyright notice and this permission notice shall be
29  * included in all copies or substantial portions of the Software.
30  *
31  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
32  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
33  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
34  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
35  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
36  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
37  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
38  */
39 
40 #ifndef included_ip_ip6_inlines_h
41 #define included_ip_ip6_inlines_h
42 
43 #include <vnet/ip/ip6_packet.h>
45 
46 /* Compute flow hash. We'll use it to select which Sponge to use for this
47  flow. And other things. */
50  flow_hash_config_t flow_hash_config)
51 {
52  tcp_header_t *tcp;
53  u64 a, b, c;
54  u64 t1, t2;
55  uword is_tcp_udp = 0;
56  u8 protocol = ip->protocol;
57 
58  if (PREDICT_TRUE
59  ((ip->protocol == IP_PROTOCOL_TCP)
60  || (ip->protocol == IP_PROTOCOL_UDP)))
61  {
62  is_tcp_udp = 1;
63  tcp = (void *) (ip + 1);
64  }
65  else if (ip->protocol == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS)
66  {
68  if ((hbh->protocol == IP_PROTOCOL_TCP) ||
69  (hbh->protocol == IP_PROTOCOL_UDP))
70  {
71  is_tcp_udp = 1;
72  tcp = (tcp_header_t *) ((u8 *) hbh + ((hbh->length + 1) << 3));
73  }
74  protocol = hbh->protocol;
75  }
76 
77  t1 = (ip->src_address.as_u64[0] ^ ip->src_address.as_u64[1]);
78  t1 = (flow_hash_config & IP_FLOW_HASH_SRC_ADDR) ? t1 : 0;
79 
80  t2 = (ip->dst_address.as_u64[0] ^ ip->dst_address.as_u64[1]);
81  t2 = (flow_hash_config & IP_FLOW_HASH_DST_ADDR) ? t2 : 0;
82 
83  a = (flow_hash_config & IP_FLOW_HASH_REVERSE_SRC_DST) ? t2 : t1;
84  b = (flow_hash_config & IP_FLOW_HASH_REVERSE_SRC_DST) ? t1 : t2;
85 
86  t1 = is_tcp_udp ? tcp->src : 0;
87  t2 = is_tcp_udp ? tcp->dst : 0;
88 
89  t1 = (flow_hash_config & IP_FLOW_HASH_SRC_PORT) ? t1 : 0;
90  t2 = (flow_hash_config & IP_FLOW_HASH_DST_PORT) ? t2 : 0;
91 
92  if (flow_hash_config & IP_FLOW_HASH_SYMMETRIC)
93  {
94  if (b < a)
95  {
96  c = a;
97  a = b;
98  b = c;
99  }
100  if (t2 < t1)
101  {
102  t2 += t1;
103  t1 = t2 - t1;
104  t2 = t2 - t1;
105  }
106  }
107 
108  b ^= (flow_hash_config & IP_FLOW_HASH_PROTO) ? protocol : 0;
109  c = ((flow_hash_config & IP_FLOW_HASH_REVERSE_SRC_DST) ? ((t1 << 16) | t2) :
110  ((t2 << 16) | t1));
111  t1 = ((u64) ip_flow_hash_router_id << 32);
112  t1 |=
113  ((flow_hash_config & IP_FLOW_HASH_FL) ? ip6_flow_label_network_order (ip) :
114  0);
115  c ^= t1;
116 
117  hash_mix64 (a, b, c);
118  return (u32) c;
119 }
120 
121 /* ip6_locate_header
122  *
123  * This function is to search for the header specified by the protocol number
124  * in find_hdr_type.
125  * This is used to locate a specific IPv6 extension header
126  * or to find transport layer header.
127  * 1. If the find_hdr_type < 0 then it finds and returns the protocol number and
128  * offset stored in *offset of the transport or ESP header in the chain if
129  * found.
130  * 2. If a header with find_hdr_type > 0 protocol number is found then the
131  * offset is stored in *offset and protocol number of the header is
132  * returned.
133  * 3. If find_hdr_type is not found or packet is malformed or
134  * it is a non-first fragment -1 is returned.
135  */
136 always_inline int
138  ip6_header_t * ip0, int find_hdr_type, u32 * offset)
139 {
140  u8 next_proto = ip0->protocol;
141  u8 *next_header;
142  u8 done = 0;
143  u32 cur_offset;
144  u8 *temp_nxthdr = 0;
145  u32 exthdr_len = 0;
146 
147  next_header = ip6_next_header (ip0);
148  cur_offset = sizeof (ip6_header_t);
149  while (1)
150  {
151  done = (next_proto == find_hdr_type);
152  if (PREDICT_FALSE
153  (next_header >=
154  (u8 *) vlib_buffer_get_current (p0) + p0->current_length))
155  {
156  //A malicious packet could set an extension header with a too big size
157  return (-1);
158  }
159  if (done)
160  break;
161  if ((!ip6_ext_hdr (next_proto)) || next_proto == IP_PROTOCOL_IP6_NONXT)
162  {
163  if (find_hdr_type < 0)
164  break;
165  return -1;
166  }
167  if (next_proto == IP_PROTOCOL_IPV6_FRAGMENTATION)
168  {
169  ip6_frag_hdr_t *frag_hdr = (ip6_frag_hdr_t *) next_header;
170  u16 frag_off = ip6_frag_hdr_offset (frag_hdr);
171  /* Non first fragment return -1 */
172  if (frag_off)
173  return (-1);
174  exthdr_len = sizeof (ip6_frag_hdr_t);
175  temp_nxthdr = next_header + exthdr_len;
176  }
177  else if (next_proto == IP_PROTOCOL_IPSEC_AH)
178  {
179  exthdr_len =
180  ip6_ext_authhdr_len (((ip6_ext_header_t *) next_header));
181  temp_nxthdr = next_header + exthdr_len;
182  }
183  else
184  {
185  exthdr_len =
186  ip6_ext_header_len (((ip6_ext_header_t *) next_header));
187  temp_nxthdr = next_header + exthdr_len;
188  }
189  next_proto = ((ip6_ext_header_t *) next_header)->next_hdr;
190  next_header = temp_nxthdr;
191  cur_offset += exthdr_len;
192  }
193 
194  *offset = cur_offset;
195  return (next_proto);
196 }
197 
198 
199 /**
200  * Push IPv6 header to buffer
201  *
202  * @param vm - vlib_main
203  * @param b - buffer to write the header to
204  * @param src - source IP
205  * @param dst - destination IP
206  * @param prot - payload proto
207  * @param flow_label - flow label
208  *
209  * @return - pointer to start of IP header
210  */
211 always_inline void *
213  ip6_address_t * src, ip6_address_t * dst,
214  int proto, u32 flow_label)
215 {
216  ip6_header_t *ip6h;
217  u16 payload_length;
218 
219  /* make some room */
220  ip6h = vlib_buffer_push_uninit (b, sizeof (ip6_header_t));
221  ASSERT (flow_label < 1 << 20);
223  clib_host_to_net_u32 ((0x6 << 28) | flow_label);
224 
225  /* calculate ip6 payload length */
226  payload_length = vlib_buffer_length_in_chain (vm, b);
227  payload_length -= sizeof (*ip6h);
228 
229  ip6h->payload_length = clib_host_to_net_u16 (payload_length);
230 
231  ip6h->hop_limit = 0xff;
232  ip6h->protocol = proto;
233  clib_memcpy_fast (ip6h->src_address.as_u8, src->as_u8,
234  sizeof (ip6h->src_address));
235  clib_memcpy_fast (ip6h->dst_address.as_u8, dst->as_u8,
236  sizeof (ip6h->src_address));
237  vnet_buffer (b)->l3_hdr_offset = (u8 *) ip6h - b->data;
238  b->flags |= VNET_BUFFER_F_IS_IP6 | VNET_BUFFER_F_L3_HDR_OFFSET_VALID;
239 
240  return ip6h;
241 }
242 
243 /**
244  * Push IPv6 header to buffer
245  *
246  * @param vm - vlib_main
247  * @param b - buffer to write the header to
248  * @param src - source IP
249  * @param dst - destination IP
250  * @param prot - payload proto
251  *
252  * @return - pointer to start of IP header
253  */
254 always_inline void *
256  ip6_address_t * src, ip6_address_t * dst, int proto)
257 {
259  0 /* flow label */ );
260 
261 }
262 
263 #endif /* included_ip_ip6_h */
264 
265 /*
266  * fd.io coding-style-patch-verification: ON
267  *
268  * Local Variables:
269  * eval: (c-set-style "gnu")
270  * End:
271  */
ip6_flow_label_network_order
static_always_inline u32 ip6_flow_label_network_order(const ip6_header_t *ip6)
Definition: ip6_packet.h:377
ip6_header_t::protocol
u8 protocol
Definition: ip6_packet.h:304
tcp_header_t
struct _tcp_header tcp_header_t
vlib_buffer_push_ip6
static void * vlib_buffer_push_ip6(vlib_main_t *vm, vlib_buffer_t *b, ip6_address_t *src, ip6_address_t *dst, int proto)
Push IPv6 header to buffer.
Definition: ip6_inlines.h:255
ip6_ext_header_len
#define ip6_ext_header_len(p)
Definition: ip6_packet.h:545
ip6_header_t::hop_limit
u8 hop_limit
Definition: ip6_packet.h:307
u16
unsigned short u16
Definition: types.h:57
ip6_hop_by_hop_packet.h
vm
vlib_main_t * vm
X-connect all packets from the HOST to the PHY.
Definition: nat44_ei.c:3047
ip_flow_hash_router_id
u32 ip_flow_hash_router_id
Definition: ip.c:19
ip6_next_header
static void * ip6_next_header(ip6_header_t *i)
Definition: ip6_packet.h:407
ip6_hop_by_hop_header_t
Definition: ip6_hop_by_hop_packet.h:18
vlib_buffer_length_in_chain
static uword vlib_buffer_length_in_chain(vlib_main_t *vm, vlib_buffer_t *b)
Get length in bytes of the buffer chain.
Definition: buffer_funcs.h:433
clib_memcpy_fast
static_always_inline void * clib_memcpy_fast(void *restrict dst, const void *restrict src, size_t n)
Definition: string.h:92
ip6_packet.h
ip6_compute_flow_hash
static u32 ip6_compute_flow_hash(const ip6_header_t *ip, flow_hash_config_t flow_hash_config)
Definition: ip6_inlines.h:49
vnet_buffer
#define vnet_buffer(b)
Definition: buffer.h:441
PREDICT_FALSE
#define PREDICT_FALSE(x)
Definition: clib.h:124
flow_hash_config_t
enum flow_hash_config_t_ flow_hash_config_t
A flow hash configuration is a mask of the flow hash options.
c
svmdb_client_t * c
Definition: vpp_get_metrics.c:48
uword
u64 uword
Definition: types.h:112
ip6_header_t::dst_address
ip6_address_t dst_address
Definition: ip6_packet.h:310
ip6_frag_hdr_offset
#define ip6_frag_hdr_offset(hdr)
Definition: ip6_packet.h:700
ip6_locate_header
static int ip6_locate_header(vlib_buffer_t *p0, ip6_header_t *ip0, int find_hdr_type, u32 *offset)
Definition: ip6_inlines.h:137
src
vl_api_address_t src
Definition: gre.api:54
ip6_ext_authhdr_len
#define ip6_ext_authhdr_len(p)
Definition: ip6_packet.h:546
vlib_buffer_t::current_length
u16 current_length
Nbytes between current data and the end of this buffer.
Definition: buffer.h:122
vlib_buffer_push_uninit
static void * vlib_buffer_push_uninit(vlib_buffer_t *b, u8 size)
Prepend uninitialized data to buffer.
Definition: buffer.h:363
always_inline
#define always_inline
Definition: rdma_mlx5dv.h:23
clib_bihash_value
template key/value backing page structure
Definition: bihash_doc.h:44
u64
unsigned long u64
Definition: types.h:89
ASSERT
#define ASSERT(truth)
Definition: error_bootstrap.h:69
ip6_ext_hdr
static u8 ip6_ext_hdr(u8 nexthdr)
Definition: ip6_packet.h:524
u32
unsigned int u32
Definition: types.h:88
protocol
vl_api_ip_proto_t protocol
Definition: lb_types.api:72
dst
vl_api_ip4_address_t dst
Definition: pnat.api:41
ip6_hop_by_hop_header_t::length
u8 length
Definition: ip6_hop_by_hop_packet.h:26
ip6_header_t
Definition: ip6_packet.h:294
next_proto
vl_api_udp_decap_next_proto_t next_proto
Definition: udp.api:65
ip6_header_t::src_address
ip6_address_t src_address
Definition: ip6_packet.h:310
vlib_main_t
Definition: main.h:102
b
vlib_buffer_t ** b
Definition: nat44_ei_out2in.c:717
u8
unsigned char u8
Definition: types.h:56
a
a
Definition: bitmap.h:525
vlib_buffer_get_current
static void * vlib_buffer_get_current(vlib_buffer_t *b)
Get pointer to current data to process.
Definition: buffer.h:257
ip
vl_api_address_t ip
Definition: l2.api:558
vlib_buffer_t::data
u8 data[]
Packet data.
Definition: buffer.h:204
ip6_hop_by_hop_header_t::protocol
u8 protocol
Definition: ip6_hop_by_hop_packet.h:21
ip6_header_t::payload_length
u16 payload_length
Definition: ip6_packet.h:301
proto
vl_api_ip_proto_t proto
Definition: acl_types.api:51
hash_mix64
#define hash_mix64(a0, b0, c0)
Definition: hash.h:530
PREDICT_TRUE
#define PREDICT_TRUE(x)
Definition: clib.h:125
ip6_header_t::ip_version_traffic_class_and_flow_label
u32 ip_version_traffic_class_and_flow_label
Definition: ip6_packet.h:297
vlib_buffer_push_ip6_custom
static void * vlib_buffer_push_ip6_custom(vlib_main_t *vm, vlib_buffer_t *b, ip6_address_t *src, ip6_address_t *dst, int proto, u32 flow_label)
Push IPv6 header to buffer.
Definition: ip6_inlines.h:212
vlib_buffer_t::flags
u32 flags
buffer flags: VLIB_BUFFER_FREE_LIST_INDEX_MASK: bits used to store free list index,...
Definition: buffer.h:133
vlib_buffer_t
VLIB buffer representation.
Definition: buffer.h:111