SCTP: fix connection memory corruption
[vpp.git] / src / vnet / sctp / sctp.c
1 /*
2  * Copyright (c) 2017 SUSE LLC.
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 #include <vnet/sctp/sctp.h>
16 #include <vnet/sctp/sctp_debug.h>
17
18 sctp_main_t sctp_main;
19
20 static u32
21 sctp_connection_bind (u32 session_index, transport_endpoint_t * tep)
22 {
23   sctp_main_t *tm = &sctp_main;
24   sctp_connection_t *listener;
25   void *iface_ip;
26
27   pool_get (tm->listener_pool, listener);
28   memset (listener, 0, sizeof (*listener));
29
30   listener->sub_conn[MAIN_SCTP_SUB_CONN_IDX].subconn_idx =
31     MAIN_SCTP_SUB_CONN_IDX;
32   listener->sub_conn[MAIN_SCTP_SUB_CONN_IDX].c_c_index =
33     listener - tm->listener_pool;
34   listener->sub_conn[MAIN_SCTP_SUB_CONN_IDX].connection.lcl_port = tep->port;
35
36   /* If we are provided a sw_if_index, bind using one of its IPs */
37   if (ip_is_zero (&tep->ip, 1) && tep->sw_if_index != ENDPOINT_INVALID_INDEX)
38     {
39       if ((iface_ip = ip_interface_get_first_ip (tep->sw_if_index,
40                                                  tep->is_ip4)))
41         ip_set (&tep->ip, iface_ip, tep->is_ip4);
42     }
43   ip_copy (&listener->sub_conn[MAIN_SCTP_SUB_CONN_IDX].connection.lcl_ip,
44            &tep->ip, tep->is_ip4);
45
46   listener->sub_conn[MAIN_SCTP_SUB_CONN_IDX].PMTU =
47     vnet_sw_interface_get_mtu (vnet_get_main (), tep->sw_if_index, VLIB_TX);
48   listener->sub_conn[MAIN_SCTP_SUB_CONN_IDX].connection.is_ip4 = tep->is_ip4;
49   listener->sub_conn[MAIN_SCTP_SUB_CONN_IDX].connection.proto =
50     TRANSPORT_PROTO_SCTP;
51   listener->sub_conn[MAIN_SCTP_SUB_CONN_IDX].c_s_index = session_index;
52   listener->sub_conn[MAIN_SCTP_SUB_CONN_IDX].connection.fib_index =
53     tep->fib_index;
54   listener->state = SCTP_STATE_CLOSED;
55
56   sctp_connection_timers_init (listener);
57
58   return listener->sub_conn[MAIN_SCTP_SUB_CONN_IDX].c_c_index;
59 }
60
61 u32
62 sctp_session_bind (u32 session_index, transport_endpoint_t * tep)
63 {
64   return sctp_connection_bind (session_index, tep);
65 }
66
67 static void
68 sctp_connection_unbind (u32 listener_index)
69 {
70   sctp_main_t *tm = vnet_get_sctp_main ();
71   sctp_connection_t *sctp_conn;
72
73   sctp_conn = pool_elt_at_index (tm->listener_pool, listener_index);
74
75   /* Poison the entry */
76   if (CLIB_DEBUG > 0)
77     memset (sctp_conn, 0xFA, sizeof (*sctp_conn));
78
79   pool_put_index (tm->listener_pool, listener_index);
80 }
81
82 u32
83 sctp_session_unbind (u32 listener_index)
84 {
85   sctp_connection_unbind (listener_index);
86   return 0;
87 }
88
89 void
90 sctp_punt_unknown (vlib_main_t * vm, u8 is_ip4, u8 is_add)
91 {
92   sctp_main_t *tm = &sctp_main;
93   if (is_ip4)
94     tm->punt_unknown4 = is_add;
95   else
96     tm->punt_unknown6 = is_add;
97 }
98
99 static int
100 sctp_alloc_custom_local_endpoint (sctp_main_t * tm, ip46_address_t * lcl_addr,
101                                   u16 * lcl_port, u8 is_ip4)
102 {
103   int index, port;
104   if (is_ip4)
105     {
106       index = tm->last_v4_address_rotor++;
107       if (tm->last_v4_address_rotor >= vec_len (tm->ip4_src_addresses))
108         tm->last_v4_address_rotor = 0;
109       lcl_addr->ip4.as_u32 = tm->ip4_src_addresses[index].as_u32;
110     }
111   else
112     {
113       index = tm->last_v6_address_rotor++;
114       if (tm->last_v6_address_rotor >= vec_len (tm->ip6_src_addresses))
115         tm->last_v6_address_rotor = 0;
116       clib_memcpy (&lcl_addr->ip6, &tm->ip6_src_addresses[index],
117                    sizeof (ip6_address_t));
118     }
119   port = transport_alloc_local_port (TRANSPORT_PROTO_SCTP, lcl_addr);
120   if (port < 1)
121     {
122       clib_warning ("Failed to allocate src port");
123       return -1;
124     }
125   *lcl_port = port;
126   return 0;
127 }
128
129 /**
130  * Initialize all connection timers as invalid
131  */
132 void
133 sctp_connection_timers_init (sctp_connection_t * sctp_conn)
134 {
135   int i, j;
136
137   /* Set all to invalid */
138   for (i = 0; i < MAX_SCTP_CONNECTIONS; i++)
139     {
140       sctp_conn->sub_conn[i].RTO = SCTP_RTO_INIT;
141
142       for (j = 0; j < SCTP_N_TIMERS; j++)
143         {
144           sctp_conn->sub_conn[i].timers[j] = SCTP_TIMER_HANDLE_INVALID;
145         }
146     }
147 }
148
149 /**
150  * Stop all connection timers
151  */
152 void
153 sctp_connection_timers_reset (sctp_connection_t * sctp_conn)
154 {
155   int i, j;
156   for (i = 0; i < MAX_SCTP_CONNECTIONS; i++)
157     {
158       for (j = 0; j < SCTP_N_TIMERS; j++)
159         sctp_timer_reset (sctp_conn, i, j);
160     }
161 }
162
163 const char *sctp_fsm_states[] = {
164 #define _(sym, str) str,
165   foreach_sctp_fsm_state
166 #undef _
167 };
168
169 u8 *
170 format_sctp_state (u8 * s, va_list * args)
171 {
172   u32 state = va_arg (*args, u32);
173
174   if (state < SCTP_N_STATES)
175     s = format (s, "%s", sctp_fsm_states[state]);
176   else
177     s = format (s, "UNKNOWN (%d (0x%x))", state, state);
178   return s;
179 }
180
181 u8 *
182 format_sctp_connection_id (u8 * s, va_list * args)
183 {
184   sctp_connection_t *sctp_conn = va_arg (*args, sctp_connection_t *);
185   if (!sctp_conn)
186     return s;
187
188   u8 i;
189   for (i = 0; i < MAX_SCTP_CONNECTIONS; i++)
190     {
191       if (sctp_conn->sub_conn[i].connection.is_ip4)
192         {
193           s = format (s, "%U[#%d][%s] %U:%d->%U:%d",
194                       s,
195                       sctp_conn->sub_conn[i].connection.thread_index,
196                       "T",
197                       format_ip4_address,
198                       &sctp_conn->sub_conn[i].connection.lcl_ip.ip4,
199                       clib_net_to_host_u16 (sctp_conn->sub_conn[i].
200                                             connection.lcl_port),
201                       format_ip4_address,
202                       &sctp_conn->sub_conn[i].connection.rmt_ip.ip4,
203                       clib_net_to_host_u16 (sctp_conn->sub_conn[i].
204                                             connection.rmt_port));
205         }
206       else
207         {
208           s = format (s, "%U[#%d][%s] %U:%d->%U:%d",
209                       s,
210                       sctp_conn->sub_conn[i].connection.thread_index,
211                       "T",
212                       format_ip6_address,
213                       &sctp_conn->sub_conn[i].connection.lcl_ip.ip6,
214                       clib_net_to_host_u16 (sctp_conn->sub_conn[i].
215                                             connection.lcl_port),
216                       format_ip6_address,
217                       &sctp_conn->sub_conn[i].connection.rmt_ip.ip6,
218                       clib_net_to_host_u16 (sctp_conn->sub_conn[i].
219                                             connection.rmt_port));
220         }
221     }
222   return s;
223 }
224
225 u8 *
226 format_sctp_connection (u8 * s, va_list * args)
227 {
228   sctp_connection_t *sctp_conn = va_arg (*args, sctp_connection_t *);
229   u32 verbose = va_arg (*args, u32);
230
231   if (!sctp_conn)
232     return s;
233   s = format (s, "%-50U", format_sctp_connection_id, sctp_conn);
234   if (verbose)
235     {
236       s = format (s, "%-15U", format_sctp_state, sctp_conn->state);
237     }
238
239   return s;
240 }
241
242 /**
243  * Initialize connection send variables.
244  */
245 void
246 sctp_init_snd_vars (sctp_connection_t * sctp_conn)
247 {
248   u32 time_now;
249   /*
250    * We use the time to randomize iss and for setting up the initial
251    * timestamp. Make sure it's updated otherwise syn and ack in the
252    * handshake may make it look as if time has flown in the opposite
253    * direction for us.
254    */
255
256   sctp_set_time_now (vlib_get_thread_index ());
257   time_now = sctp_time_now ();
258
259   sctp_conn->local_initial_tsn = random_u32 (&time_now);
260   sctp_conn->last_unacked_tsn = sctp_conn->local_initial_tsn;
261   sctp_conn->next_tsn = sctp_conn->local_initial_tsn + 1;
262
263   sctp_conn->remote_initial_tsn = 0x0;
264   sctp_conn->last_rcvd_tsn = sctp_conn->remote_initial_tsn;
265 }
266
267 always_inline sctp_connection_t *
268 sctp_sub_connection_add (u8 thread_index)
269 {
270   sctp_main_t *tm = vnet_get_sctp_main ();
271   sctp_connection_t *sctp_conn = tm->connections[thread_index];
272
273   sctp_conn->sub_conn[sctp_conn->next_avail_sub_conn].connection.c_index =
274     sctp_conn->sub_conn[MAIN_SCTP_SUB_CONN_IDX].connection.c_index;
275   sctp_conn->sub_conn[sctp_conn->next_avail_sub_conn].
276     connection.thread_index = thread_index;
277   sctp_conn->sub_conn[sctp_conn->next_avail_sub_conn].subconn_idx =
278     sctp_conn->next_avail_sub_conn;
279
280   sctp_conn->next_avail_sub_conn += 1;
281
282   return sctp_conn;
283 }
284
285 void
286 sctp_sub_connection_add_ip4 (u8 thread_index,
287                              sctp_ipv4_addr_param_t * ipv4_addr)
288 {
289   sctp_connection_t *sctp_conn = sctp_sub_connection_add (thread_index);
290
291   clib_memcpy (&sctp_conn->
292                sub_conn[sctp_conn->next_avail_sub_conn].connection.lcl_ip.ip4,
293                &ipv4_addr->address, sizeof (ipv4_addr->address));
294 }
295
296 void
297 sctp_sub_connection_add_ip6 (u8 thread_index,
298                              sctp_ipv6_addr_param_t * ipv6_addr)
299 {
300   sctp_connection_t *sctp_conn = sctp_sub_connection_add (thread_index);
301
302   clib_memcpy (&sctp_conn->
303                sub_conn[sctp_conn->next_avail_sub_conn].connection.lcl_ip.ip6,
304                &ipv6_addr->address, sizeof (ipv6_addr->address));
305 }
306
307 sctp_connection_t *
308 sctp_connection_new (u8 thread_index)
309 {
310   sctp_main_t *sctp_main = vnet_get_sctp_main ();
311   sctp_connection_t *sctp_conn;
312
313   pool_get (sctp_main->connections[thread_index], sctp_conn);
314   memset (sctp_conn, 0, sizeof (*sctp_conn));
315   sctp_conn->sub_conn[MAIN_SCTP_SUB_CONN_IDX].subconn_idx =
316     MAIN_SCTP_SUB_CONN_IDX;
317   sctp_conn->sub_conn[MAIN_SCTP_SUB_CONN_IDX].c_c_index =
318     sctp_conn - sctp_main->connections[thread_index];
319   sctp_conn->sub_conn[MAIN_SCTP_SUB_CONN_IDX].c_thread_index = thread_index;
320   sctp_conn->local_tag = 0;
321   sctp_conn->next_avail_sub_conn = 1;
322
323   return sctp_conn;
324 }
325
326 sctp_connection_t *
327 sctp_half_open_connection_new (u8 thread_index)
328 {
329   sctp_main_t *tm = vnet_get_sctp_main ();
330   sctp_connection_t *sctp_conn = 0;
331   ASSERT (vlib_get_thread_index () == 0);
332   pool_get (tm->half_open_connections, sctp_conn);
333   memset (sctp_conn, 0, sizeof (*sctp_conn));
334   sctp_conn->sub_conn[MAIN_SCTP_SUB_CONN_IDX].c_c_index =
335     sctp_conn - tm->half_open_connections;
336   sctp_conn->sub_conn[MAIN_SCTP_SUB_CONN_IDX].subconn_idx =
337     MAIN_SCTP_SUB_CONN_IDX;
338   return sctp_conn;
339 }
340
341 static inline int
342 sctp_connection_open (transport_endpoint_t * rmt)
343 {
344   sctp_main_t *tm = vnet_get_sctp_main ();
345   sctp_connection_t *sctp_conn;
346   ip46_address_t lcl_addr;
347   u16 lcl_port;
348   uword thread_id;
349   int rv;
350
351   u8 idx = MAIN_SCTP_SUB_CONN_IDX;
352
353   /*
354    * Allocate local endpoint
355    */
356   if ((rmt->is_ip4 && vec_len (tm->ip4_src_addresses))
357       || (!rmt->is_ip4 && vec_len (tm->ip6_src_addresses)))
358     rv = sctp_alloc_custom_local_endpoint (tm, &lcl_addr, &lcl_port,
359                                            rmt->is_ip4);
360   else
361     rv = transport_alloc_local_endpoint (TRANSPORT_PROTO_SCTP,
362                                          rmt, &lcl_addr, &lcl_port);
363
364   if (rv)
365     return -1;
366
367   /*
368    * Create connection and send INIT CHUNK
369    */
370   thread_id = vlib_get_thread_index ();
371   ASSERT (thread_id == 0);
372
373   clib_spinlock_lock_if_init (&tm->half_open_lock);
374   sctp_conn = sctp_half_open_connection_new (thread_id);
375   sctp_conn->sub_conn[idx].PMTU =
376     vnet_sw_interface_get_mtu (vnet_get_main (), rmt->sw_if_index, VLIB_TX);
377
378   transport_connection_t *trans_conn = &sctp_conn->sub_conn[idx].connection;
379   ip_copy (&trans_conn->rmt_ip, &rmt->ip, rmt->is_ip4);
380   ip_copy (&trans_conn->lcl_ip, &lcl_addr, rmt->is_ip4);
381   sctp_conn->sub_conn[idx].subconn_idx = idx;
382   trans_conn->rmt_port = rmt->port;
383   trans_conn->lcl_port = clib_host_to_net_u16 (lcl_port);
384   trans_conn->is_ip4 = rmt->is_ip4;
385   trans_conn->proto = TRANSPORT_PROTO_SCTP;
386   trans_conn->fib_index = rmt->fib_index;
387
388   sctp_connection_timers_init (sctp_conn);
389   /* The other connection vars will be initialized after INIT_ACK chunk received */
390   sctp_init_snd_vars (sctp_conn);
391
392   sctp_send_init (sctp_conn);
393
394   clib_spinlock_unlock_if_init (&tm->half_open_lock);
395
396   return sctp_conn->sub_conn[idx].connection.c_index;
397 }
398
399 /**
400  * Cleans up connection state.
401  *
402  * No notifications.
403  */
404 void
405 sctp_connection_cleanup (sctp_connection_t * sctp_conn)
406 {
407   sctp_main_t *tm = &sctp_main;
408   u8 i;
409
410   /* Cleanup local endpoint if this was an active connect */
411   for (i = 0; i < MAX_SCTP_CONNECTIONS; i++)
412     transport_endpoint_cleanup (TRANSPORT_PROTO_SCTP,
413                                 &sctp_conn->sub_conn[i].connection.lcl_ip,
414                                 sctp_conn->sub_conn[i].connection.lcl_port);
415
416   /* Check if connection is not yet fully established */
417   if (sctp_conn->state == SCTP_STATE_COOKIE_WAIT)
418     {
419
420     }
421   else
422     {
423       int thread_index =
424         sctp_conn->sub_conn[MAIN_SCTP_SUB_CONN_IDX].connection.thread_index;
425
426       /* Make sure all timers are cleared */
427       sctp_connection_timers_reset (sctp_conn);
428
429       /* Poison the entry */
430       if (CLIB_DEBUG > 0)
431         memset (sctp_conn, 0xFA, sizeof (*sctp_conn));
432       pool_put (tm->connections[thread_index], sctp_conn);
433     }
434 }
435
436 int
437 sctp_session_open (transport_endpoint_t * tep)
438 {
439   return sctp_connection_open (tep);
440 }
441
442 u16
443 sctp_check_outstanding_data_chunks (sctp_connection_t * sctp_conn)
444 {
445   u8 i;
446   for (i = 0; i < MAX_SCTP_CONNECTIONS; i++)
447     {
448       if (sctp_conn->sub_conn[i].state == SCTP_SUBCONN_STATE_DOWN)
449         continue;
450
451       if (sctp_conn->sub_conn[i].is_retransmitting == 1 ||
452           sctp_conn->sub_conn[i].enqueue_state != SCTP_ERROR_ENQUEUED)
453         {
454           SCTP_DBG_OUTPUT
455             ("Connection %u has still DATA to be enqueued inboud / outboud",
456              sctp_conn->sub_conn[i].connection.c_index);
457           return 1;
458         }
459
460     }
461   return 0;                     /* Indicates no more data to be read/sent */
462 }
463
464 void
465 sctp_connection_close (sctp_connection_t * sctp_conn)
466 {
467   SCTP_DBG ("Closing connection %u...",
468             sctp_conn->sub_conn[MAIN_SCTP_SUB_CONN_IDX].connection.c_index);
469
470   sctp_conn->state = SCTP_STATE_SHUTDOWN_PENDING;
471
472   sctp_send_shutdown (sctp_conn);
473 }
474
475 void
476 sctp_session_close (u32 conn_index, u32 thread_index)
477 {
478   ASSERT (thread_index == 0);
479
480   sctp_connection_t *sctp_conn =
481     sctp_connection_get (conn_index, thread_index);
482   if (sctp_conn != NULL)
483     sctp_connection_close (sctp_conn);
484 }
485
486 void
487 sctp_session_cleanup (u32 conn_index, u32 thread_index)
488 {
489   sctp_connection_t *sctp_conn =
490     sctp_connection_get (conn_index, thread_index);
491
492   if (sctp_conn != NULL)
493     {
494       sctp_connection_timers_reset (sctp_conn);
495       /* Wait for the session tx events to clear */
496       sctp_conn->state = SCTP_STATE_CLOSED;
497     }
498 }
499
500 /**
501  * Compute maximum segment size for session layer.
502  */
503 u16
504 sctp_session_send_mss (transport_connection_t * trans_conn)
505 {
506   sctp_connection_t *sctp_conn =
507     sctp_get_connection_from_transport (trans_conn);
508
509   if (sctp_conn == NULL)
510     {
511       SCTP_DBG ("sctp_conn == NULL");
512       return 0;
513     }
514
515   update_cwnd (sctp_conn);
516   update_smallest_pmtu_idx (sctp_conn);
517
518   return sctp_conn->sub_conn[sctp_conn->smallest_PMTU_idx].cwnd;
519 }
520
521 u16
522 sctp_snd_space (sctp_connection_t * sctp_conn)
523 {
524   /* Finally, let's subtract the DATA chunk headers overhead */
525   return sctp_conn->sub_conn[sctp_conn->smallest_PMTU_idx].cwnd -
526     sizeof (sctp_payload_data_chunk_t) - sizeof (sctp_full_hdr_t);
527 }
528
529 /**
530  * Compute TX window session is allowed to fill.
531  */
532 u32
533 sctp_session_send_space (transport_connection_t * trans_conn)
534 {
535   sctp_connection_t *sctp_conn =
536     sctp_get_connection_from_transport (trans_conn);
537
538   return sctp_snd_space (sctp_conn);
539 }
540
541 transport_connection_t *
542 sctp_session_get_transport (u32 conn_index, u32 thread_index)
543 {
544   sctp_connection_t *sctp_conn =
545     sctp_connection_get (conn_index, thread_index);
546
547   if (PREDICT_TRUE (sctp_conn != NULL))
548     return &sctp_conn->sub_conn[MAIN_SCTP_SUB_CONN_IDX].connection;
549
550   return NULL;
551 }
552
553 transport_connection_t *
554 sctp_session_get_listener (u32 listener_index)
555 {
556   sctp_main_t *tm = vnet_get_sctp_main ();
557   sctp_connection_t *sctp_conn;
558   sctp_conn = pool_elt_at_index (tm->listener_pool, listener_index);
559   return &sctp_conn->sub_conn[MAIN_SCTP_SUB_CONN_IDX].connection;
560 }
561
562 u8 *
563 format_sctp_session (u8 * s, va_list * args)
564 {
565   u32 tci = va_arg (*args, u32);
566   u32 thread_index = va_arg (*args, u32);
567   u32 verbose = va_arg (*args, u32);
568   sctp_connection_t *tc;
569
570   tc = sctp_connection_get (tci, thread_index);
571   if (tc)
572     s = format (s, "%U", format_sctp_connection, tc, verbose);
573   else
574     s = format (s, "empty\n");
575   return s;
576 }
577
578 u8 *
579 format_sctp_listener_session (u8 * s, va_list * args)
580 {
581   u32 tci = va_arg (*args, u32);
582   sctp_connection_t *tc = sctp_listener_get (tci);
583   return format (s, "%U", format_sctp_connection_id, tc);
584 }
585
586 void
587 sctp_expired_timers_cb (u32 conn_index, u32 timer_id)
588 {
589   sctp_connection_t *sctp_conn;
590
591   sctp_conn = sctp_connection_get (conn_index, vlib_get_thread_index ());
592   /* note: the connection may have already disappeared */
593   if (PREDICT_FALSE (sctp_conn == 0))
594     return;
595
596   SCTP_DBG ("%s expired", sctp_timer_to_string (timer_id));
597
598   switch (timer_id)
599     {
600     case SCTP_TIMER_T1_INIT:
601     case SCTP_TIMER_T1_COOKIE:
602     case SCTP_TIMER_T2_SHUTDOWN:
603     case SCTP_TIMER_T3_RXTX:
604       sctp_timer_reset (sctp_conn, conn_index, timer_id);
605       break;
606     case SCTP_TIMER_T4_HEARTBEAT:
607       sctp_timer_reset (sctp_conn, conn_index, timer_id);
608       goto heartbeat;
609     }
610
611   if (sctp_conn->sub_conn[conn_index].unacknowledged_hb >
612       SCTP_PATH_MAX_RETRANS)
613     {
614       // The remote-peer is considered to be unreachable hence shutting down
615       u8 i, total_subs_down = 1;
616       for (i = 0; i < MAX_SCTP_CONNECTIONS; i++)
617         {
618           if (sctp_conn->sub_conn[i].state == SCTP_SUBCONN_STATE_DOWN)
619             continue;
620
621           u32 now = sctp_time_now ();
622           if (now > (sctp_conn->sub_conn[i].last_seen + SCTP_HB_INTERVAL))
623             {
624               total_subs_down += 1;
625               sctp_conn->sub_conn[i].state = SCTP_SUBCONN_STATE_DOWN;
626             }
627         }
628
629       if (total_subs_down == MAX_SCTP_CONNECTIONS)
630         {
631           /* Start cleanup. App wasn't notified yet so use delete notify as
632            * opposed to delete to cleanup session layer state. */
633           stream_session_delete_notify (&sctp_conn->sub_conn
634                                         [MAIN_SCTP_SUB_CONN_IDX].connection);
635
636           sctp_connection_timers_reset (sctp_conn);
637
638           sctp_connection_cleanup (sctp_conn);
639         }
640     }
641   return;
642
643 heartbeat:
644   sctp_send_heartbeat (sctp_conn);
645 }
646
647 static void
648 sctp_expired_timers_dispatch (u32 * expired_timers)
649 {
650   int i;
651   u32 connection_index, timer_id;
652
653   for (i = 0; i < vec_len (expired_timers); i++)
654     {
655       /* Get session index and timer id */
656       connection_index = expired_timers[i] & 0x0FFFFFFF;
657       timer_id = expired_timers[i] >> 28;
658
659       SCTP_DBG ("Expired timer ID: %u", timer_id);
660
661       /* Handle expiration */
662       sctp_expired_timers_cb (connection_index, timer_id);
663     }
664 }
665
666 void
667 sctp_initialize_timer_wheels (sctp_main_t * tm)
668 {
669   tw_timer_wheel_16t_2w_512sl_t *tw;
670   /* *INDENT-OFF* */
671   foreach_vlib_main (({
672     tw = &tm->timer_wheels[ii];
673     tw_timer_wheel_init_16t_2w_512sl (tw, sctp_expired_timers_dispatch,
674                                       100e-3 /* timer period 100ms */ , ~0);
675     tw->last_run_time = vlib_time_now (this_vlib_main);
676   }));
677   /* *INDENT-ON* */
678 }
679
680 clib_error_t *
681 sctp_main_enable (vlib_main_t * vm)
682 {
683   sctp_main_t *tm = vnet_get_sctp_main ();
684   vlib_thread_main_t *vtm = vlib_get_thread_main ();
685   clib_error_t *error = 0;
686   u32 num_threads;
687   int thread;
688   sctp_connection_t *sctp_conn __attribute__ ((unused));
689   u32 preallocated_connections_per_thread;
690
691   if ((error = vlib_call_init_function (vm, ip_main_init)))
692     return error;
693   if ((error = vlib_call_init_function (vm, ip4_lookup_init)))
694     return error;
695   if ((error = vlib_call_init_function (vm, ip6_lookup_init)))
696     return error;
697
698   /*
699    * Registrations
700    */
701
702   ip4_register_protocol (IP_PROTOCOL_SCTP, sctp4_input_node.index);
703   ip6_register_protocol (IP_PROTOCOL_SCTP, sctp6_input_node.index);
704
705   /*
706    * Initialize data structures
707    */
708
709   num_threads = 1 /* main thread */  + vtm->n_threads;
710   vec_validate (tm->connections, num_threads - 1);
711
712   /*
713    * Preallocate connections. Assume that thread 0 won't
714    * use preallocated threads when running multi-core
715    */
716   if (num_threads == 1)
717     {
718       thread = 0;
719       preallocated_connections_per_thread = tm->preallocated_connections;
720     }
721   else
722     {
723       thread = 1;
724       preallocated_connections_per_thread =
725         tm->preallocated_connections / (num_threads - 1);
726     }
727   for (; thread < num_threads; thread++)
728     {
729       if (preallocated_connections_per_thread)
730         pool_init_fixed (tm->connections[thread],
731                          preallocated_connections_per_thread);
732     }
733
734   /* Initialize per worker thread tx buffers (used for control messages) */
735   vec_validate (tm->tx_buffers, num_threads - 1);
736
737   /* Initialize timer wheels */
738   vec_validate (tm->timer_wheels, num_threads - 1);
739   sctp_initialize_timer_wheels (tm);
740
741   /* Initialize clocks per tick for SCTP timestamp. Used to compute
742    * monotonically increasing timestamps. */
743   tm->tstamp_ticks_per_clock = vm->clib_time.seconds_per_clock
744     / SCTP_TSTAMP_RESOLUTION;
745
746   if (num_threads > 1)
747     {
748       clib_spinlock_init (&tm->half_open_lock);
749     }
750
751   vec_validate (tm->tx_frames[0], num_threads - 1);
752   vec_validate (tm->tx_frames[1], num_threads - 1);
753   vec_validate (tm->ip_lookup_tx_frames[0], num_threads - 1);
754   vec_validate (tm->ip_lookup_tx_frames[1], num_threads - 1);
755
756   tm->bytes_per_buffer = vlib_buffer_free_list_buffer_size
757     (vm, VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX);
758
759   vec_validate (tm->time_now, num_threads - 1);
760   return error;
761 }
762
763 clib_error_t *
764 sctp_enable_disable (vlib_main_t * vm, u8 is_en)
765 {
766   if (is_en)
767     {
768       if (sctp_main.is_enabled)
769         return 0;
770
771       return sctp_main_enable (vm);
772     }
773   else
774     {
775       sctp_main.is_enabled = 0;
776     }
777
778   return 0;
779 }
780
781 transport_connection_t *
782 sctp_half_open_session_get_transport (u32 conn_index)
783 {
784   sctp_connection_t *sctp_conn = sctp_half_open_connection_get (conn_index);
785   return &sctp_conn->sub_conn[MAIN_SCTP_SUB_CONN_IDX].connection;
786 }
787
788 u8 *
789 format_sctp_half_open (u8 * s, va_list * args)
790 {
791   u32 tci = va_arg (*args, u32);
792   sctp_connection_t *sctp_conn = sctp_half_open_connection_get (tci);
793   return format (s, "%U", format_sctp_connection_id, sctp_conn);
794 }
795
796 void
797 sctp_update_time (f64 now, u8 thread_index)
798 {
799   sctp_set_time_now (thread_index);
800   tw_timer_expire_timers_16t_2w_512sl (&sctp_main.timer_wheels[thread_index],
801                                        now);
802   sctp_flush_frames_to_output (thread_index);
803 }
804
805 /* *INDENT OFF* */
806 const static transport_proto_vft_t sctp_proto = {
807   .enable = sctp_enable_disable,
808   .bind = sctp_session_bind,
809   .unbind = sctp_session_unbind,
810   .open = sctp_session_open,
811   .close = sctp_session_close,
812   .cleanup = sctp_session_cleanup,
813   .push_header = sctp_push_header,
814   .send_mss = sctp_session_send_mss,
815   .send_space = sctp_session_send_space,
816   .update_time = sctp_update_time,
817   .get_connection = sctp_session_get_transport,
818   .get_listener = sctp_session_get_listener,
819   .get_half_open = sctp_half_open_session_get_transport,
820   .format_connection = format_sctp_session,
821   .format_listener = format_sctp_listener_session,
822   .format_half_open = format_sctp_half_open,
823 };
824
825 /* *INDENT ON* */
826
827 clib_error_t *
828 sctp_init (vlib_main_t * vm)
829 {
830   sctp_main_t *tm = vnet_get_sctp_main ();
831   ip_main_t *im = &ip_main;
832   ip_protocol_info_t *pi;
833   /* Session layer, and by implication SCTP, are disabled by default */
834   tm->is_enabled = 0;
835
836   /* Register with IP for header parsing */
837   pi = ip_get_protocol_info (im, IP_PROTOCOL_SCTP);
838   if (pi == 0)
839     return clib_error_return (0, "SCTP protocol info AWOL");
840   pi->format_header = format_sctp_header;
841   pi->unformat_pg_edit = unformat_pg_sctp_header;
842
843   /* Register as transport with session layer */
844   transport_register_protocol (TRANSPORT_PROTO_SCTP, &sctp_proto,
845                                FIB_PROTOCOL_IP4, sctp4_output_node.index);
846   transport_register_protocol (TRANSPORT_PROTO_SCTP, &sctp_proto,
847                                FIB_PROTOCOL_IP6, sctp6_output_node.index);
848
849   return 0;
850 }
851
852 VLIB_INIT_FUNCTION (sctp_init);
853
854 /*
855  * fd.io coding-style-patch-verification: ON
856  *
857  * Local Variables:
858  * eval: (c-set-style "gnu")
859  * End:
860  */