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:
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.
16 #include <vnet/lisp-cp/lisp_msg_serdes.h>
17 #include <vnet/lisp-cp/packets.h>
18 #include <vppinfra/time.h>
20 void *lisp_msg_put_gid (vlib_buffer_t * b, gid_address_t * gid);
23 lisp_msg_put_locators (vlib_buffer_t * b, locator_t * locators)
27 vec_foreach (loc, locators)
29 u8 *p = vlib_buffer_put_uninit (b, sizeof (locator_hdr_t));
30 memset (p, 0, sizeof (locator_hdr_t));
31 LOC_PRIORITY (p) = loc->priority;
32 LOC_MPRIORITY (p) = loc->mpriority;
33 LOC_WEIGHT (p) = loc->weight;
34 LOC_MWEIGHT (p) = loc->mweight;
35 LOC_LOCAL (p) = loc->local;
36 LOC_PROBED (p) = loc->probed ? 1 : 0;
37 lisp_msg_put_gid (b, &loc->address);
42 lisp_msg_put_mapping_record (vlib_buffer_t * b, mapping_t * record)
44 mapping_record_hdr_t *p =
45 vlib_buffer_put_uninit (b, sizeof (mapping_record_hdr_t));
46 gid_address_t *eid = &record->eid;
48 memset (p, 0, sizeof (*p));
49 MAP_REC_EID_PLEN (p) = gid_address_len (eid);
50 MAP_REC_TTL (p) = clib_host_to_net_u32 (MAP_REGISTER_DEFAULT_TTL);
51 MAP_REC_AUTH (p) = record->authoritative ? 1 : 0;
52 MAP_REC_LOC_COUNT (p) = vec_len (record->locators);
54 lisp_msg_put_gid (b, eid);
55 lisp_msg_put_locators (b, record->locators);
59 lisp_msg_put_mreg_records (vlib_buffer_t * b, mapping_t * records)
62 for (i = 0; i < vec_len (records); i++)
63 lisp_msg_put_mapping_record (b, &records[i]);
67 lisp_msg_put_gid (vlib_buffer_t * b, gid_address_t * gid)
72 /* insert only src-eid-afi field set to 0 */
73 p = vlib_buffer_put_uninit (b, sizeof (u16));
78 p = vlib_buffer_put_uninit (b, gid_address_size_to_put (gid));
79 gid_address_put (p, gid);
85 lisp_msg_put_itr_rlocs (lisp_cp_main_t * lcm, vlib_buffer_t * b,
86 gid_address_t * rlocs, u8 * locs_put)
91 bp = vlib_buffer_get_current (b);
92 for (i = 0; i < vec_len (rlocs); i++)
94 lisp_msg_put_gid (b, &rlocs[i]);
98 *locs_put = count - 1;
103 lisp_msg_put_eid_rec (vlib_buffer_t * b, gid_address_t * eid)
105 eid_record_hdr_t *h = vlib_buffer_put_uninit (b, sizeof (*h));
107 memset (h, 0, sizeof (*h));
108 EID_REC_MLEN (h) = gid_address_len (eid);
109 lisp_msg_put_gid (b, eid);
114 nonce_build (u32 seed)
121 /* Put nanosecond clock in lower 32-bits and put an XOR of the nanosecond
122 * clock with the seond clock in the upper 32-bits. */
123 syscall (SYS_clock_gettime, CLOCK_REALTIME, &ts);
124 nonce_lower = ts.tv_nsec;
125 nonce_upper = ts.tv_sec ^ clib_host_to_net_u32 (nonce_lower);
127 /* OR in a caller provided seed to the low-order 32-bits. */
130 /* Return 64-bit nonce. */
132 nonce = (nonce << 32) | nonce_lower;
137 lisp_msg_put_map_reply (vlib_buffer_t * b, mapping_t * records, u64 nonce,
140 map_reply_hdr_t *h = vlib_buffer_put_uninit (b, sizeof (h[0]));
142 memset (h, 0, sizeof (h[0]));
143 MREP_TYPE (h) = LISP_MAP_REPLY;
144 MREP_NONCE (h) = nonce;
145 MREP_REC_COUNT (h) = 1;
146 MREP_RLOC_PROBE (h) = probe_bit;
148 lisp_msg_put_mreg_records (b, records);
153 lisp_msg_put_map_register (vlib_buffer_t * b, mapping_t * records,
154 u8 want_map_notify, u16 auth_data_len, u64 * nonce,
159 /* Basic header init */
160 map_register_hdr_t *h = vlib_buffer_put_uninit (b, sizeof (h[0]));
162 memset (h, 0, sizeof (h[0]));
163 MREG_TYPE (h) = LISP_MAP_REGISTER;
164 MREG_NONCE (h) = nonce_build (0);
165 MREG_WANT_MAP_NOTIFY (h) = want_map_notify ? 1 : 0;
166 MREG_REC_COUNT (h) = vec_len (records);
168 auth_data = vlib_buffer_put_uninit (b, auth_data_len);
169 memset (auth_data, 0, auth_data_len);
171 /* Put map register records */
172 lisp_msg_put_mreg_records (b, records);
174 nonce[0] = MREG_NONCE (h);
175 msg_len[0] = vlib_buffer_get_tail (b) - (u8 *) h;
180 lisp_msg_put_mreq (lisp_cp_main_t * lcm, vlib_buffer_t * b,
181 gid_address_t * seid, gid_address_t * deid,
182 gid_address_t * rlocs, u8 is_smr_invoked,
183 u8 rloc_probe_set, u64 * nonce)
187 /* Basic header init */
188 map_request_hdr_t *h = vlib_buffer_put_uninit (b, sizeof (h[0]));
190 memset (h, 0, sizeof (h[0]));
191 MREQ_TYPE (h) = LISP_MAP_REQUEST;
192 MREQ_NONCE (h) = nonce_build (0);
193 MREQ_SMR_INVOKED (h) = is_smr_invoked ? 1 : 0;
194 MREQ_RLOC_PROBE (h) = rloc_probe_set ? 1 : 0;
196 /* We're adding one eid record */
197 increment_record_count (h);
199 /* Fill source eid */
200 lisp_msg_put_gid (b, seid);
203 lisp_msg_put_itr_rlocs (lcm, b, rlocs, &loc_count);
204 MREQ_ITR_RLOC_COUNT (h) = loc_count;
207 lisp_msg_put_eid_rec (b, deid);
209 nonce[0] = MREQ_NONCE (h);
214 lisp_msg_push_ecm (vlib_main_t * vm, vlib_buffer_t * b, int lp, int rp,
215 gid_address_t * la, gid_address_t * ra)
218 ip_address_t _src_ip, *src_ip = &_src_ip, _dst_ip, *dst_ip = &_dst_ip;
219 if (gid_address_type (la) != GID_ADDR_IP_PREFIX)
222 memset (src_ip, 0, sizeof (src_ip[0]));
223 memset (dst_ip, 0, sizeof (dst_ip[0]));
227 src_ip = &gid_address_ip (la);
228 dst_ip = &gid_address_ip (ra);
231 /* Push inner ip and udp */
232 pkt_push_udp_and_ip (vm, b, lp, rp, src_ip, dst_ip);
234 /* Push lisp ecm hdr */
235 h = pkt_push_ecm_hdr (b);
241 msg_type_to_hdr_len (lisp_msg_type_e type)
245 case LISP_MAP_REQUEST:
246 return (sizeof (map_request_hdr_t));
248 return (sizeof (map_reply_hdr_t));
255 lisp_msg_pull_hdr (vlib_buffer_t * b, lisp_msg_type_e type)
257 return vlib_buffer_pull (b, msg_type_to_hdr_len (type));
261 lisp_msg_parse_addr (vlib_buffer_t * b, gid_address_t * eid)
264 memset (eid, 0, sizeof (*eid));
265 len = gid_address_parse (vlib_buffer_get_current (b), eid);
267 vlib_buffer_pull (b, len);
272 lisp_msg_parse_eid_rec (vlib_buffer_t * b, gid_address_t * eid)
274 eid_record_hdr_t *h = vlib_buffer_get_current (b);
276 memset (eid, 0, sizeof (*eid));
277 len = gid_address_parse (EID_REC_ADDR (h), eid);
281 gid_address_ippref_len (eid) = EID_REC_MLEN (h);
282 vlib_buffer_pull (b, len + sizeof (eid_record_hdr_t));
284 return len + sizeof (eid_record_hdr_t);
288 lisp_msg_parse_itr_rlocs (vlib_buffer_t * b, gid_address_t ** rlocs,
292 u32 i, len = 0, tlen = 0;
294 //MREQ_ITR_RLOC_COUNT(mreq_hdr) + 1
295 for (i = 0; i < rloc_count; i++)
297 len = lisp_msg_parse_addr (b, &tloc);
300 vec_add1 (*rlocs, tloc);
307 lisp_msg_parse_loc (vlib_buffer_t * b, locator_t * loc)
311 len = locator_parse (vlib_buffer_get_current (b), loc);
315 vlib_buffer_pull (b, len);
321 lisp_msg_parse_mapping_record (vlib_buffer_t * b, gid_address_t * eid,
322 locator_t ** locs, locator_t * probed_)
324 void *h = 0, *loc_hdr = 0;
325 locator_t loc, *probed = 0;
326 int i = 0, len = 0, llen = 0;
328 h = vlib_buffer_get_current (b);
329 vlib_buffer_pull (b, sizeof (mapping_record_hdr_t));
331 memset (eid, 0, sizeof (*eid));
332 len = gid_address_parse (vlib_buffer_get_current (b), eid);
336 vlib_buffer_pull (b, len);
337 if (GID_ADDR_IP_PREFIX == gid_address_type (eid))
338 gid_address_ippref_len (eid) = MAP_REC_EID_PLEN (h);
340 for (i = 0; i < MAP_REC_LOC_COUNT (h); i++)
342 loc_hdr = vlib_buffer_get_current (b);
344 llen = lisp_msg_parse_loc (b, &loc);
347 vec_add1 (*locs, loc);
350 if (LOC_PROBED (loc_hdr))
354 ("Multiple locators probed! Probing only the first!");
360 if (probed_ != 0 && probed)
363 return len + sizeof (map_reply_hdr_t);
367 * fd.io coding-style-patch-verification: ON
370 * eval: (c-set-style "gnu")