1 /*
2  * ngtcp2
3  *
4  * Copyright (c) 2019 ngtcp2 contributors
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining
7  * a copy of this software and associated documentation files (the
8  * "Software"), to deal in the Software without restriction, including
9  * without limitation the rights to use, copy, modify, merge, publish,
10  * distribute, sublicense, and/or sell copies of the Software, and to
11  * permit persons to whom the Software is furnished to do so, subject to
12  * the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be
15  * included in all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24  */
25 #include "ngtcp2_pv.h"
26 
27 #include <string.h>
28 #include <assert.h>
29 
30 #include "ngtcp2_mem.h"
31 #include "ngtcp2_log.h"
32 #include "ngtcp2_macro.h"
33 #include "ngtcp2_addr.h"
34 
ngtcp2_pv_entry_init(ngtcp2_pv_entry * pvent,const uint8_t * data,ngtcp2_tstamp expiry,uint8_t flags)35 void ngtcp2_pv_entry_init(ngtcp2_pv_entry *pvent, const uint8_t *data,
36                           ngtcp2_tstamp expiry, uint8_t flags) {
37   memcpy(pvent->data, data, sizeof(pvent->data));
38   pvent->expiry = expiry;
39   pvent->flags = flags;
40 }
41 
ngtcp2_pv_new(ngtcp2_pv ** ppv,const ngtcp2_dcid * dcid,ngtcp2_duration timeout,uint8_t flags,ngtcp2_log * log,const ngtcp2_mem * mem)42 int ngtcp2_pv_new(ngtcp2_pv **ppv, const ngtcp2_dcid *dcid,
43                   ngtcp2_duration timeout, uint8_t flags, ngtcp2_log *log,
44                   const ngtcp2_mem *mem) {
45   int rv;
46 
47   (*ppv) = ngtcp2_mem_malloc(mem, sizeof(ngtcp2_pv));
48   if (*ppv == NULL) {
49     return NGTCP2_ERR_NOMEM;
50   }
51 
52   rv = ngtcp2_ringbuf_init(&(*ppv)->ents, NGTCP2_PV_MAX_ENTRIES,
53                            sizeof(ngtcp2_pv_entry), mem);
54   if (rv != 0) {
55     ngtcp2_mem_free(mem, *ppv);
56     return 0;
57   }
58 
59   ngtcp2_dcid_copy(&(*ppv)->dcid, dcid);
60 
61   (*ppv)->mem = mem;
62   (*ppv)->log = log;
63   (*ppv)->timeout = timeout;
64   (*ppv)->fallback_pto = 0;
65   (*ppv)->started_ts = UINT64_MAX;
66   (*ppv)->probe_pkt_left = NGTCP2_PV_NUM_PROBE_PKT;
67   (*ppv)->round = 0;
68   (*ppv)->flags = flags;
69 
70   return 0;
71 }
72 
ngtcp2_pv_del(ngtcp2_pv * pv)73 void ngtcp2_pv_del(ngtcp2_pv *pv) {
74   if (pv == NULL) {
75     return;
76   }
77   ngtcp2_ringbuf_free(&pv->ents);
78   ngtcp2_mem_free(pv->mem, pv);
79 }
80 
ngtcp2_pv_add_entry(ngtcp2_pv * pv,const uint8_t * data,ngtcp2_tstamp expiry,uint8_t flags,ngtcp2_tstamp ts)81 void ngtcp2_pv_add_entry(ngtcp2_pv *pv, const uint8_t *data,
82                          ngtcp2_tstamp expiry, uint8_t flags,
83                          ngtcp2_tstamp ts) {
84   ngtcp2_pv_entry *ent;
85 
86   assert(pv->probe_pkt_left);
87 
88   if (ngtcp2_ringbuf_len(&pv->ents) == 0) {
89     pv->started_ts = ts;
90   }
91 
92   ent = ngtcp2_ringbuf_push_back(&pv->ents);
93   ngtcp2_pv_entry_init(ent, data, expiry, flags);
94 
95   pv->flags &= (uint8_t)~NGTCP2_PV_FLAG_CANCEL_TIMER;
96   --pv->probe_pkt_left;
97 }
98 
ngtcp2_pv_validate(ngtcp2_pv * pv,uint8_t * pflags,const uint8_t * data)99 int ngtcp2_pv_validate(ngtcp2_pv *pv, uint8_t *pflags, const uint8_t *data) {
100   size_t len = ngtcp2_ringbuf_len(&pv->ents);
101   size_t i;
102   ngtcp2_pv_entry *ent;
103 
104   if (len == 0) {
105     return NGTCP2_ERR_INVALID_STATE;
106   }
107 
108   for (i = 0; i < len; ++i) {
109     ent = ngtcp2_ringbuf_get(&pv->ents, i);
110     if (memcmp(ent->data, data, sizeof(ent->data)) == 0) {
111       *pflags = ent->flags;
112       ngtcp2_log_info(pv->log, NGTCP2_LOG_EVENT_PTV, "path has been validated");
113       return 0;
114     }
115   }
116 
117   return NGTCP2_ERR_INVALID_ARGUMENT;
118 }
119 
ngtcp2_pv_handle_entry_expiry(ngtcp2_pv * pv,ngtcp2_tstamp ts)120 void ngtcp2_pv_handle_entry_expiry(ngtcp2_pv *pv, ngtcp2_tstamp ts) {
121   ngtcp2_pv_entry *ent;
122 
123   if (ngtcp2_ringbuf_len(&pv->ents) == 0) {
124     return;
125   }
126 
127   ent = ngtcp2_ringbuf_get(&pv->ents, ngtcp2_ringbuf_len(&pv->ents) - 1);
128 
129   if (ent->expiry > ts) {
130     return;
131   }
132 
133   ++pv->round;
134   pv->probe_pkt_left = NGTCP2_PV_NUM_PROBE_PKT;
135 }
136 
ngtcp2_pv_should_send_probe(ngtcp2_pv * pv)137 int ngtcp2_pv_should_send_probe(ngtcp2_pv *pv) {
138   return pv->probe_pkt_left > 0;
139 }
140 
ngtcp2_pv_validation_timed_out(ngtcp2_pv * pv,ngtcp2_tstamp ts)141 int ngtcp2_pv_validation_timed_out(ngtcp2_pv *pv, ngtcp2_tstamp ts) {
142   ngtcp2_tstamp t;
143   ngtcp2_pv_entry *ent;
144 
145   if (pv->started_ts == UINT64_MAX) {
146     return 0;
147   }
148 
149   assert(ngtcp2_ringbuf_len(&pv->ents));
150 
151   ent = ngtcp2_ringbuf_get(&pv->ents, ngtcp2_ringbuf_len(&pv->ents) - 1);
152 
153   t = pv->started_ts + pv->timeout;
154   t = ngtcp2_max(t, ent->expiry);
155 
156   return t <= ts;
157 }
158 
ngtcp2_pv_next_expiry(ngtcp2_pv * pv)159 ngtcp2_tstamp ngtcp2_pv_next_expiry(ngtcp2_pv *pv) {
160   ngtcp2_pv_entry *ent;
161 
162   if ((pv->flags & NGTCP2_PV_FLAG_CANCEL_TIMER) ||
163       ngtcp2_ringbuf_len(&pv->ents) == 0) {
164     return UINT64_MAX;
165   }
166 
167   ent = ngtcp2_ringbuf_get(&pv->ents, ngtcp2_ringbuf_len(&pv->ents) - 1);
168 
169   return ent->expiry;
170 }
171 
ngtcp2_pv_cancel_expired_timer(ngtcp2_pv * pv,ngtcp2_tstamp ts)172 void ngtcp2_pv_cancel_expired_timer(ngtcp2_pv *pv, ngtcp2_tstamp ts) {
173   ngtcp2_tstamp expiry = ngtcp2_pv_next_expiry(pv);
174 
175   if (expiry > ts) {
176     return;
177   }
178 
179   pv->flags |= NGTCP2_PV_FLAG_CANCEL_TIMER;
180 }
181