1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
  | 
/*
 * IP MSDP packet helper
 * Copyright (C) 2016 Cumulus Networks, Inc.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; see the file COPYING; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
 * MA 02110-1301 USA
 */
#include <zebra.h>
#include <lib/log.h>
#include <lib/network.h>
#include <lib/stream.h>
#include <lib/thread.h>
#include <lib/vty.h>
#include "pimd.h"
#include "pim_str.h"
#include "pim_msdp.h"
#include "pim_msdp_packet.h"
#include "pim_msdp_socket.h"
static char *
pim_msdp_pkt_type_dump(enum pim_msdp_tlv type, char *buf, int buf_size)
{
  switch (type) {
    case PIM_MSDP_V4_SOURCE_ACTIVE:
      snprintf(buf, buf_size, "%s", "SA");
      break;
    case PIM_MSDP_V4_SOURCE_ACTIVE_REQUEST:
      snprintf(buf, buf_size, "%s", "SA_REQ");
      break;
    case PIM_MSDP_V4_SOURCE_ACTIVE_RESPONSE:
      snprintf(buf, buf_size, "%s", "SA_RESP");
      break;
    case PIM_MSDP_KEEPALIVE:
      snprintf(buf, buf_size, "%s", "KA");
      break;
    case PIM_MSDP_RESERVED:
      snprintf(buf, buf_size, "%s", "RSVD");
      break;
    case PIM_MSDP_TRACEROUTE_PROGRESS:
      snprintf(buf, buf_size, "%s", "TRACE_PROG");
      break;
    case PIM_MSDP_TRACEROUTE_REPLY:
      snprintf(buf, buf_size, "%s", "TRACE_REPLY");
      break;
    default:
      snprintf(buf, buf_size, "UNK-%d", type);
  }
  return buf;
}
static void
pim_msdp_pkt_dump(struct pim_msdp_peer *mp, int type, int len, bool rx)
{
  char key_str[PIM_MSDP_PEER_KEY_STRLEN];
  char type_str[PIM_MSDP_PKT_TYPE_STRLEN];
  pim_msdp_peer_key_dump(mp, key_str, sizeof(key_str), false);
  pim_msdp_pkt_type_dump(type, type_str, sizeof(type_str));
  zlog_debug("%s pkt %s type %s len %d",
      key_str, rx?"rx":"tx", type_str, len);
  /* XXX: dump actual data */
}
/* Check file descriptor whether connect is established. */
static void
pim_msdp_connect_check(struct pim_msdp_peer *mp)
{
  int status;
  socklen_t slen;
  int ret;
  if (mp->state != PIM_MSDP_CONNECTING) {
    /* if we are here it means we are not in a connecting or established state
     * for now treat this as a fatal error */
    /* XXX:revisit; reset TCP connection */
    pim_msdp_peer_reset_tcp_conn(mp, "invalid-state");
    return;
  }
  PIM_MSDP_PEER_READ_OFF(mp);
  PIM_MSDP_PEER_WRITE_OFF(mp);
  /* Check file descriptor. */
  slen = sizeof(status);
  ret = getsockopt(mp->fd, SOL_SOCKET, SO_ERROR, (void *)&status, &slen);
  /* If getsockopt is fail, this is fatal error. */
  if (ret < 0) {
    zlog_err("can't get sockopt for nonblocking connect");
    /* XXX:revisit; reset TCP connection */
    pim_msdp_peer_reset_tcp_conn(mp, "connect-failed");
    return;
  }
  /* When status is 0 then TCP connection is established. */
  if (PIM_DEBUG_MSDP_INTERNAL) {
    char key_str[PIM_MSDP_PEER_KEY_STRLEN];
    pim_msdp_peer_key_dump(mp, key_str, sizeof(key_str), false);
    zlog_debug("%s pim_connect_check %s", key_str, status?"fail":"success");
  }
  if (status == 0) {
    pim_msdp_peer_established(mp);
  } else {
    /* XXX:revisit; reset TCP connection */
    pim_msdp_peer_reset_tcp_conn(mp, "connect-failed");
  }
}
static void
pim_msdp_pkt_delete(struct pim_msdp_peer *mp)
{
  stream_free(stream_fifo_pop(mp->obuf));
}
static void
pim_msdp_pkt_add(struct pim_msdp_peer *mp, struct stream *s)
{
  stream_fifo_push(mp->obuf, s);
}
static void
pim_msdp_write_proceed_actions(struct pim_msdp_peer *mp)
{
  if (stream_fifo_head(mp->obuf)) {
    PIM_MSDP_PEER_WRITE_ON(mp);
  }
}
int
pim_msdp_write(struct thread *thread)
{
  struct pim_msdp_peer *mp;
  struct stream *s;
  int num;
  enum pim_msdp_tlv type;
  mp = THREAD_ARG(thread);
  mp->t_write = NULL;
  if (PIM_DEBUG_MSDP_INTERNAL) {
    char key_str[PIM_MSDP_PEER_KEY_STRLEN];
    pim_msdp_peer_key_dump(mp, key_str, sizeof(key_str), false);
    zlog_debug("%s pim_msdp_write", key_str);
  }
  if (mp->fd < 0) {
    return -1;
  }
  /* check if TCP connection is established */
  if (mp->state != PIM_MSDP_ESTABLISHED) {
    pim_msdp_connect_check(mp);
    return 0;
  }
  s = stream_fifo_head(mp->obuf);
  if (!s) {
    pim_msdp_write_proceed_actions(mp);
    return 0;
  }
  sockopt_cork (mp->fd, 1);
  /* Nonblocking write until TCP output buffer is full  */
  do
  {
    int writenum;
    /* Number of bytes to be sent */
    writenum = stream_get_endp(s) - stream_get_getp(s);
    /* Call write() system call */
    num = write(mp->fd, STREAM_PNT(s), writenum);
    if (num < 0) {
      /* write failed either retry needed or error */
      if (ERRNO_IO_RETRY(errno))
        break;
      /* XXX:revisit; reset TCP connection */
      pim_msdp_peer_reset_tcp_conn(mp, "pkt-tx-failed");
      return 0;
    }
    if (num != writenum) {
      /* Partial write */
      stream_forward_getp(s, num);
      if (PIM_DEBUG_MSDP_INTERNAL) {
        char key_str[PIM_MSDP_PEER_KEY_STRLEN];
        pim_msdp_peer_key_dump(mp, key_str, sizeof(key_str), false);
        zlog_debug("%s pim_msdp_partial_write", key_str);
      }
      break;
    }
    /* Retrieve msdp packet type. */
    stream_set_getp(s,0);
    type = stream_getc(s);
    switch (type)
    {
      case PIM_MSDP_KEEPALIVE:
        mp->ka_tx_cnt++;
        break;
      case PIM_MSDP_V4_SOURCE_ACTIVE:
        mp->sa_tx_cnt++;
        break;
      default:;
    }
    if (PIM_DEBUG_MSDP_PACKETS) {
      pim_msdp_pkt_dump(mp, type, writenum, false /*rx*/);
    }
    /* packet sent delete it. */
    pim_msdp_pkt_delete(mp);
    /* XXX - may need to pause if we have done too much work in this
     * loop */
  } while ((s = stream_fifo_head(mp->obuf)) != NULL);
  pim_msdp_write_proceed_actions(mp);
  sockopt_cork (mp->fd, 0);
  return 0;
}
static void
pim_msdp_pkt_send(struct pim_msdp_peer *mp, struct stream *s)
{
  /* Add packet to the end of list. */
  pim_msdp_pkt_add(mp, s);
  PIM_MSDP_PEER_WRITE_ON(mp);
}
void
pim_msdp_pkt_ka_tx(struct pim_msdp_peer *mp)
{
  struct stream *s;
  if (mp->state != PIM_MSDP_ESTABLISHED) {
    /* don't tx anything unless a session is established */
    return;
  }
  s = stream_new(PIM_MSDP_KA_TLV_MAX_SIZE);
  stream_putc(s, PIM_MSDP_KEEPALIVE);
  stream_putw(s, PIM_MSDP_KA_TLV_MAX_SIZE);
  pim_msdp_pkt_send(mp, s);
}
static void
pim_msdp_pkt_sa_push_to_one_peer(struct pim_msdp_peer *mp)
{
  struct stream *s;
  if (mp->state != PIM_MSDP_ESTABLISHED) {
    /* don't tx anything unless a session is established */
    return;
  }
  s = stream_dup(msdp->work_obuf);
  if (s) {
    pim_msdp_pkt_send(mp, s);
    mp->flags |= PIM_MSDP_PEERF_SA_JUST_SENT;
  }
}
/* push the stream into the obuf fifo of all the peers */
static void
pim_msdp_pkt_sa_push(struct pim_msdp_peer *mp)
{
  struct listnode *mpnode;
  if (mp) {
    pim_msdp_pkt_sa_push_to_one_peer(mp);
  } else {
    for (ALL_LIST_ELEMENTS_RO(msdp->peer_list, mpnode, mp)) {
      if (PIM_DEBUG_MSDP_INTERNAL) {
        char key_str[PIM_MSDP_PEER_KEY_STRLEN];
        pim_msdp_peer_key_dump(mp, key_str, sizeof(key_str), false);
        zlog_debug("%s pim_msdp_pkt_sa_push", key_str);
      }
      pim_msdp_pkt_sa_push_to_one_peer(mp);
    }
  }
}
static int
pim_msdp_pkt_sa_fill_hdr(int local_cnt)
{
  int curr_tlv_ecnt;
  stream_reset(msdp->work_obuf);
  curr_tlv_ecnt = local_cnt>PIM_MSDP_SA_MAX_ENTRY_CNT?PIM_MSDP_SA_MAX_ENTRY_CNT:local_cnt;
  local_cnt -= curr_tlv_ecnt;
  stream_putc(msdp->work_obuf, PIM_MSDP_V4_SOURCE_ACTIVE);
  stream_putw(msdp->work_obuf, PIM_MSDP_SA_ENTRY_CNT2SIZE(curr_tlv_ecnt));
  stream_putc(msdp->work_obuf, curr_tlv_ecnt);
  stream_put_ipv4(msdp->work_obuf, msdp->originator_id.s_addr);
  return local_cnt;
}
static void
pim_msdp_pkt_sa_fill_one(struct pim_msdp_sa *sa)
{
  stream_put3(msdp->work_obuf, 0 /* reserved */);
  stream_putc(msdp->work_obuf, 32 /* sprefix len */);
  stream_put_ipv4(msdp->work_obuf, sa->sg.grp.s_addr);
  stream_put_ipv4(msdp->work_obuf, sa->sg.src.s_addr);
}
static void
pim_msdp_pkt_sa_gen(struct pim_msdp_peer *mp)
{
  struct listnode *sanode;
  struct pim_msdp_sa *sa;
  int sa_count;
  int local_cnt = msdp->local_cnt;
  sa_count = 0;
  local_cnt = pim_msdp_pkt_sa_fill_hdr(local_cnt);
  for (ALL_LIST_ELEMENTS_RO(msdp->sa_list, sanode, sa)) {
    if (!(sa->flags & PIM_MSDP_SAF_LOCAL)) {
      /* current implementation of MSDP is for anycast i.e. full mesh. so
       * no re-forwarding of SAs that we learnt from other peers */
      continue;
    }
    /* add sa into scratch pad */
    pim_msdp_pkt_sa_fill_one(sa);
    ++sa_count;
    if (sa_count >= PIM_MSDP_SA_MAX_ENTRY_CNT) {
      pim_msdp_pkt_sa_push(mp);
      /* reset headers */
      sa_count = 0;
      local_cnt = pim_msdp_pkt_sa_fill_hdr(local_cnt);
    }
  }
  if (sa_count) {
    pim_msdp_pkt_sa_push(mp);
  }
  return;
}
static void
pim_msdp_pkt_sa_tx_done(void)
{
  struct listnode *mpnode;
  struct pim_msdp_peer *mp;
  /* if SA were sent to the peers we restart ka timer and avoid
   * unnecessary ka noise */
  for (ALL_LIST_ELEMENTS_RO(msdp->peer_list, mpnode, mp)) {
    if (mp->flags & PIM_MSDP_PEERF_SA_JUST_SENT) {
      mp->flags &= ~PIM_MSDP_PEERF_SA_JUST_SENT;
      pim_msdp_peer_pkt_txed(mp);
    }
  }
}
void
pim_msdp_pkt_sa_tx(void)
{
  pim_msdp_pkt_sa_gen(NULL /* mp */);
  pim_msdp_pkt_sa_tx_done();
}
void
pim_msdp_pkt_sa_tx_one(struct pim_msdp_sa *sa)
{
  pim_msdp_pkt_sa_fill_hdr(1 /* cnt */);
  pim_msdp_pkt_sa_fill_one(sa);
  pim_msdp_pkt_sa_push(NULL);
  pim_msdp_pkt_sa_tx_done();
}
/* when a connection is first established we push all SAs immediately */
void
pim_msdp_pkt_sa_tx_to_one_peer(struct pim_msdp_peer *mp)
{
  pim_msdp_pkt_sa_gen(mp);
  pim_msdp_pkt_sa_tx_done();
}
static void
pim_msdp_pkt_rxed_with_fatal_error(struct pim_msdp_peer *mp)
{
  /* XXX:revisit; reset TCP connection */
  pim_msdp_peer_reset_tcp_conn(mp, "invalid-pkt-rx");
}
static void
pim_msdp_pkt_ka_rx(struct pim_msdp_peer *mp, int len)
{
  mp->ka_rx_cnt++;
  if (len !=  PIM_MSDP_KA_TLV_MAX_SIZE) {
    pim_msdp_pkt_rxed_with_fatal_error(mp);
    return;
  }
  pim_msdp_peer_pkt_rxed(mp);
}
static void
pim_msdp_pkt_sa_rx_one(struct pim_msdp_peer *mp, struct in_addr rp)
{
  int prefix_len;
  struct prefix_sg sg;
  /* just throw away the three reserved bytes */
  stream_get3(mp->ibuf);
  prefix_len = stream_getc(mp->ibuf);
  memset(&sg, 0, sizeof (struct prefix_sg));
  sg.grp.s_addr = stream_get_ipv4(mp->ibuf);
  sg.src.s_addr = stream_get_ipv4(mp->ibuf);
  if (prefix_len != 32) {
    /* ignore SA update if the prefix length is not 32 */
    zlog_err("rxed sa update with invalid prefix length %d", prefix_len);
    return;
  }
  if (PIM_DEBUG_MSDP_PACKETS) {
    zlog_debug("  sg %s", pim_str_sg_dump(&sg));
  }
  pim_msdp_sa_ref(mp, &sg, rp);
}
static void
pim_msdp_pkt_sa_rx(struct pim_msdp_peer *mp, int len)
{
  int entry_cnt;
  int i;
  struct in_addr rp; /* Last RP address associated with this SA */
  mp->sa_rx_cnt++;
  if (len <  PIM_MSDP_SA_TLV_MIN_SIZE) {
    pim_msdp_pkt_rxed_with_fatal_error(mp);
    return;
  }
  entry_cnt = stream_getc(mp->ibuf);
  /* some vendors include the actual multicast data in the tlv (at the end).
   * we will ignore such data. in the future we may consider pushing it down
   * the RPT */
  if (len < PIM_MSDP_SA_ENTRY_CNT2SIZE(entry_cnt)) {
    pim_msdp_pkt_rxed_with_fatal_error(mp);
    return;
  }
  rp.s_addr = stream_get_ipv4(mp->ibuf);
  if (PIM_DEBUG_MSDP_PACKETS) {
    char rp_str[INET_ADDRSTRLEN];
    pim_inet4_dump("<rp?>", rp, rp_str, sizeof(rp_str));
    zlog_debug("  entry_cnt %d rp %s", entry_cnt, rp_str);
  }
  if (!pim_msdp_peer_rpf_check(mp, rp)) {
    /* if peer-RPF check fails don't process the packet any further */
    if (PIM_DEBUG_MSDP_PACKETS) {
      zlog_debug("  peer RPF check failed");
    }
    return;
  }
  pim_msdp_peer_pkt_rxed(mp);
  /* update SA cache */
  for (i = 0; i < entry_cnt; ++i) {
    pim_msdp_pkt_sa_rx_one(mp, rp);
  }
}
static void
pim_msdp_pkt_rx(struct pim_msdp_peer *mp)
{
  enum pim_msdp_tlv type;
  int len;
  /* re-read type and len */
  type = stream_getc_from(mp->ibuf, 0);
  len = stream_getw_from(mp->ibuf, 1);
  if (len <  PIM_MSDP_HEADER_SIZE) {
    pim_msdp_pkt_rxed_with_fatal_error(mp);
    return;
  }
  if (len > PIM_MSDP_SA_TLV_MAX_SIZE) {
    /* if tlv size if greater than max just ignore the tlv */
    return;
  }
  if (PIM_DEBUG_MSDP_PACKETS) {
    pim_msdp_pkt_dump(mp, type, len, true /*rx*/);
  }
  switch(type) {
      case PIM_MSDP_KEEPALIVE:
        pim_msdp_pkt_ka_rx(mp, len);
        break;
      case PIM_MSDP_V4_SOURCE_ACTIVE:
        mp->sa_rx_cnt++;
        pim_msdp_pkt_sa_rx(mp, len);
        break;
      default:
        mp->unk_rx_cnt++;
  }
}
/* pim msdp read utility function. */
static int
pim_msdp_read_packet(struct pim_msdp_peer *mp)
{
  int nbytes;
  int readsize;
  readsize = mp->packet_size - stream_get_endp(mp->ibuf);
  if (!readsize) {
    return 0;
  }
  /* Read packet from fd */
  nbytes = stream_read_try(mp->ibuf, mp->fd, readsize);
  if (nbytes < 0) {
    if (nbytes == -2) {
      /* transient error retry */
      return -1;
    }
    pim_msdp_pkt_rxed_with_fatal_error(mp);
    return -1;
  }
  if (!nbytes) {
    pim_msdp_peer_reset_tcp_conn(mp, "peer-down");
    return -1;
  }
  /* We read partial packet. */
  if (stream_get_endp(mp->ibuf) != mp->packet_size)
    return -1;
  return 0;
}
int
pim_msdp_read(struct thread *thread)
{
  struct pim_msdp_peer *mp;
  int rc;
  uint32_t len;
  mp = THREAD_ARG(thread);
  mp->t_read = NULL;
  if (PIM_DEBUG_MSDP_INTERNAL) {
    char key_str[PIM_MSDP_PEER_KEY_STRLEN];
    pim_msdp_peer_key_dump(mp, key_str, sizeof(key_str), false);
    zlog_debug("%s pim_msdp_read", key_str);
  }
  if (mp->fd < 0) {
    return -1;
  }
  /* check if TCP connection is established */
  if (mp->state != PIM_MSDP_ESTABLISHED) {
    pim_msdp_connect_check(mp);
    return 0;
  }
  PIM_MSDP_PEER_READ_ON(mp);
  if (!mp->packet_size) {
    mp->packet_size = PIM_MSDP_HEADER_SIZE;
  }
  if (stream_get_endp(mp->ibuf) < PIM_MSDP_HEADER_SIZE) {
    /* start by reading the TLV header */
    rc = pim_msdp_read_packet(mp);
    if (rc < 0) {
      goto pim_msdp_read_end;
    }
    /* Find TLV type and len  */
    stream_getc(mp->ibuf);
    len = stream_getw(mp->ibuf);
    if (len < PIM_MSDP_HEADER_SIZE) {
      pim_msdp_pkt_rxed_with_fatal_error(mp);
      goto pim_msdp_read_end;
    }
    /* read complete TLV */
    mp->packet_size = len;
  }
  rc = pim_msdp_read_packet(mp);
  if (rc < 0) {
    goto pim_msdp_read_end;
  }
  pim_msdp_pkt_rx(mp);
  /* reset input buffers and get ready for the next packet */
  mp->packet_size = 0;
  stream_reset(mp->ibuf);
pim_msdp_read_end:
  return 0;
}
  |