summaryrefslogtreecommitdiffstats
path: root/pimd/pim_mroute.c
blob: cdd2dbc6dd1204148d7f340dff3d90bb9af63f31 (plain)
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
/*
  PIM for Quagga
  Copyright (C) 2008  Everton da Silva Marques

  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
  
  $QuaggaId: $Format:%an, %ai, %h$ $
*/

#include <zebra.h>
#include "log.h"
#include "privs.h"

#include "pimd.h"
#include "pim_mroute.h"
#include "pim_str.h"
#include "pim_time.h"
#include "pim_iface.h"
#include "pim_macro.h"
#include "pim_rp.h"

/* GLOBAL VARS */
extern struct zebra_privs_t pimd_privs;

static void mroute_read_on(void);

static int pim_mroute_set(int fd, int enable)
{
  int err;
  int opt = enable ? MRT_INIT : MRT_DONE;
  socklen_t opt_len = sizeof(opt);

  err = setsockopt(fd, IPPROTO_IP, opt, &opt, opt_len);
  if (err) {
    int e = errno;
    zlog_warn("%s %s: failure: setsockopt(fd=%d,IPPROTO_IP,%s=%d): errno=%d: %s",
	      __FILE__, __PRETTY_FUNCTION__,
	      fd, enable ? "MRT_INIT" : "MRT_DONE", opt, e, safe_strerror(e));
    errno = e;
    return -1;
  }

#if 0
  zlog_info("%s %s: setsockopt(fd=%d,IPPROTO_IP,MRT_INIT,opt=%d): ok",
	    __FILE__, __PRETTY_FUNCTION__,
	    fd, opt);
#endif

  return 0;
}

static const char *igmpmsgtype2str[IGMPMSG_WHOLEPKT + 1] = {
  "<unknown_upcall?>",
  "NOCACHE",
  "WRONGVIF",
  "WHOLEPKT", };

static int
pim_mroute_msg_nocache (int fd, struct interface *ifp, const struct igmpmsg *msg,
			const char *src_str, const char *grp_str)
{
  struct mfcctl mc;
  struct pim_interface *pim_ifp = ifp->info;
  struct pim_rpf *rpg;

  rpg = RP(msg->im_dst);
  /*
   * If the incoming interface is unknown OR
   * the Interface type is SSM we don't need to
   * do anything here
   */
  if ((rpg->rpf_addr.s_addr == INADDR_NONE) ||
      (!pim_ifp) ||
      (!PIM_I_am_DR(pim_ifp)) ||
      (pim_ifp->itype == PIM_INTERFACE_SSM))
    return 0;

  if (PIM_DEBUG_PIM_TRACE) {
    zlog_debug("%s: Adding a Route for %s from %s for WHOLEPKT consumption",
	       __PRETTY_FUNCTION__, grp_str, src_str);
  }

  /*
   * This is just a hack to get the (S,G) received packet up into
   * our user space so that we can register it.
   *
   * Once we run into a few more issues than we'll fix this.
   */
  memset(&mc, 0, sizeof(struct mfcctl));
  mc.mfcc_origin   = msg->im_src;
  mc.mfcc_mcastgrp = msg->im_dst;
  mc.mfcc_parent   = ifp->ifindex;
  mc.mfcc_ttls[MAXVIFS-1] = 1;
  pim_mroute_add(&mc);
  return 0;
}

static int
pim_mroute_msg_wholepkt (int fd, struct interface *ifp, const char *buf,
			 const char *src_str, const char *grp_str)
{
  struct pim_interface *pim_ifp = ifp->info;
  struct in_addr group;
  struct pim_rpf *rpg;
  const struct ip *ip_hdr;

  ip_hdr = (const struct ip *)buf;

  group = ip_hdr->ip_dst;

  rpg = RP(group);

  if ((rpg->rpf_addr.s_addr == INADDR_NONE) ||
      (!pim_ifp) ||
      (!PIM_I_am_DR(pim_ifp)) ||
      (pim_ifp->itype == PIM_INTERFACE_SSM)) {
    return 0;
  }

  //pim_register_send(buf, rpg);
  return 0;
}

static int
pim_mroute_msg_wrongvif (int fd, struct interface *ifp, const struct igmpmsg *msg,
			 const char *src_str, const char *grp_str)
{
  struct pim_ifchannel *ch;
  struct pim_interface *pim_ifp;

  /*
    Send Assert(S,G) on iif as response to WRONGVIF kernel upcall.

    RFC 4601 4.8.2.  PIM-SSM-Only Routers

    iif is the incoming interface of the packet.
    if (iif is in inherited_olist(S,G)) {
    send Assert(S,G) on iif
    }
  */

  if (!ifp) {
    if (PIM_DEBUG_PIM_TRACE) {
      zlog_debug("%s: WRONGVIF (S,G)=(%s,%s) could not find input interface for input_vif_index=%d",
		 __PRETTY_FUNCTION__,
		 src_str, grp_str, msg->im_vif);
    }
    return -1;
  }

  pim_ifp = ifp->info;
  if (!pim_ifp) {
    if (PIM_DEBUG_PIM_TRACE) {
      zlog_debug("%s: WRONGVIF (S,G)=(%s,%s) multicast not enabled on interface %s",
		 __PRETTY_FUNCTION__,
		 src_str, grp_str, ifp->name);
    }
    return -2;
  }

  ch = pim_ifchannel_find(ifp, msg->im_src, msg->im_dst);
  if (!ch) {
    if (PIM_DEBUG_PIM_TRACE) {
      zlog_debug("%s: WRONGVIF (S,G)=(%s,%s) could not find channel on interface %s",
		 __PRETTY_FUNCTION__,
		 src_str, grp_str, ifp->name);
    }
    return -3;
  }

  /*
    RFC 4601: 4.6.1.  (S,G) Assert Message State Machine

    Transitions from NoInfo State

    An (S,G) data packet arrives on interface I, AND
    CouldAssert(S,G,I)==TRUE An (S,G) data packet arrived on an
    downstream interface that is in our (S,G) outgoing interface
    list.  We optimistically assume that we will be the assert
    winner for this (S,G), and so we transition to the "I am Assert
    Winner" state and perform Actions A1 (below), which will
    initiate the assert negotiation for (S,G).
  */

  if (ch->ifassert_state != PIM_IFASSERT_NOINFO) {
    if (PIM_DEBUG_PIM_TRACE) {
      zlog_debug("%s: WRONGVIF (S,G)=(%s,%s) channel is not on Assert NoInfo state for interface %s",
		 __PRETTY_FUNCTION__,
		 src_str, grp_str, ifp->name);
    }
    return -4;
  }

  if (!PIM_IF_FLAG_TEST_COULD_ASSERT(ch->flags)) {
    if (PIM_DEBUG_PIM_TRACE) {
      zlog_debug("%s: WRONGVIF (S,G)=(%s,%s) interface %s is not downstream for channel",
		 __PRETTY_FUNCTION__,
		 src_str, grp_str, ifp->name);
    }
    return -5;
  }

  if (assert_action_a1(ch)) {
    if (PIM_DEBUG_PIM_TRACE) {
      zlog_debug("%s: WRONGVIF (S,G)=(%s,%s) assert_action_a1 failure on interface %s",
		 __PRETTY_FUNCTION__,
		 src_str, grp_str, ifp->name);
    }
    return -6;
  }

  return 0;
}

int pim_mroute_msg(int fd, const char *buf, int buf_size)
{
  struct interface     *ifp;
  const struct ip      *ip_hdr;
  const struct igmpmsg *msg;
  char src_str[100] = "<src?>";
  char grp_str[100] = "<grp?>";

  ip_hdr = (const struct ip *) buf;

  /* kernel upcall must have protocol=0 */
  if (ip_hdr->ip_p) {
    /* this is not a kernel upcall */
    if (PIM_DEBUG_PIM_TRACE) {
      pim_inet4_dump("<src?>", ip_hdr->ip_src, src_str, sizeof(src_str));
      pim_inet4_dump("<grp?>", ip_hdr->ip_dst, grp_str, sizeof(grp_str));
      zlog_debug("%s: not a kernel upcall proto=%d src: %s dst: %s msg_size=%d",
		 __PRETTY_FUNCTION__, ip_hdr->ip_p, src_str, grp_str, buf_size);
      //zlog_hexdump(buf, buf_size);
    }
    return 0;
  }

  msg = (const struct igmpmsg *) buf;

  ifp = pim_if_find_by_vif_index(msg->im_vif);

  if (PIM_DEBUG_PIM_TRACE) {
    pim_inet4_dump("<src?>", msg->im_src, src_str, sizeof(src_str));
    pim_inet4_dump("<grp?>", msg->im_dst, grp_str, sizeof(grp_str));
    zlog_warn("%s: kernel upcall %s type=%d ip_p=%d from fd=%d for (S,G)=(%s,%s) on %s vifi=%d",
	      __PRETTY_FUNCTION__,
	      igmpmsgtype2str[msg->im_msgtype],
	      msg->im_msgtype,
	      ip_hdr->ip_p,
	      fd,
	      src_str,
	      grp_str,
	      ifp ? ifp->name : "<ifname?>",
	      msg->im_vif);
  }

  switch (msg->im_msgtype) {
  case IGMPMSG_WRONGVIF:
    return pim_mroute_msg_wrongvif(fd, ifp, msg, src_str, grp_str);
    break;
  case IGMPMSG_NOCACHE:
    return pim_mroute_msg_nocache(fd, ifp, msg, src_str, grp_str);
    break;
  case IGMPMSG_WHOLEPKT:
    zlog_hexdump(buf, buf_size);
    return pim_mroute_msg_wholepkt(fd, ifp, msg, src_str, grp_str);
    break;
  default:
    break;
  }

  return 0;
}

static int mroute_read_msg(int fd)
{
  const int msg_min_size = MAX(sizeof(struct ip), sizeof(struct igmpmsg));
  char buf[1000];
  int rd;

  if (((int) sizeof(buf)) < msg_min_size) {
    zlog_err("%s: fd=%d: buf size=%zu lower than msg_min=%d",
	     __PRETTY_FUNCTION__, fd, sizeof(buf), msg_min_size);
    return -1;
  }

  rd = read(fd, buf, sizeof(buf));
  if (rd < 0) {
    zlog_warn("%s: failure reading fd=%d: errno=%d: %s",
	      __PRETTY_FUNCTION__, fd, errno, safe_strerror(errno));
    return -2;
  }

  if (rd < msg_min_size) {
    zlog_warn("%s: short message reading fd=%d: read=%d msg_min=%d",
	      __PRETTY_FUNCTION__, fd, rd, msg_min_size);
    return -3;
  }

  return pim_mroute_msg(fd, buf, rd);
}

static int mroute_read(struct thread *t)
{
  int fd;
  int result;

  zassert(t);
  zassert(!THREAD_ARG(t));

  fd = THREAD_FD(t);
  zassert(fd == qpim_mroute_socket_fd);

  result = mroute_read_msg(fd);

  /* Keep reading */
  qpim_mroute_socket_reader = 0;
  mroute_read_on();

  return result;
}

static void mroute_read_on()
{
  zassert(!qpim_mroute_socket_reader);
  zassert(PIM_MROUTE_IS_ENABLED);

  THREAD_READ_ON(master, qpim_mroute_socket_reader,
		 mroute_read, 0, qpim_mroute_socket_fd);
}

static void mroute_read_off()
{
  THREAD_OFF(qpim_mroute_socket_reader);
}

int pim_mroute_socket_enable()
{
  int fd;
  struct in_addr pimreg = { .s_addr = 0 };

  if (PIM_MROUTE_IS_ENABLED)
    return -1;

  if ( pimd_privs.change (ZPRIVS_RAISE) )
    zlog_err ("pim_mroute_socket_enable: could not raise privs, %s",
              safe_strerror (errno) );

  fd = socket(AF_INET, SOCK_RAW, IPPROTO_IGMP);

  if ( pimd_privs.change (ZPRIVS_LOWER) )
    zlog_err ("pim_mroute_socket_enable: could not lower privs, %s",
	      safe_strerror (errno) );

  if (fd < 0) {
    zlog_warn("Could not create mroute socket: errno=%d: %s",
	      errno, safe_strerror(errno));
    return -2;
  }

  if (pim_mroute_set(fd, 1)) {
    zlog_warn("Could not enable mroute on socket fd=%d: errno=%d: %s",
	      fd, errno, safe_strerror(errno));
    close(fd);
    return -3;
  }

  qpim_mroute_socket_fd       = fd;
  pim_mroute_add_vif(MAXVIFS-1, pimreg, VIFF_REGISTER);

  qpim_mroute_socket_creation = pim_time_monotonic_sec();
  mroute_read_on();

  zassert(PIM_MROUTE_IS_ENABLED);

  return 0;
}

int pim_mroute_socket_disable()
{
  if (PIM_MROUTE_IS_DISABLED)
    return -1;

  if (pim_mroute_set(qpim_mroute_socket_fd, 0)) {
    zlog_warn("Could not disable mroute on socket fd=%d: errno=%d: %s",
	      qpim_mroute_socket_fd, errno, safe_strerror(errno));
    return -2;
  }

  if (close(qpim_mroute_socket_fd)) {
    zlog_warn("Failure closing mroute socket: fd=%d errno=%d: %s",
	      qpim_mroute_socket_fd, errno, safe_strerror(errno));
    return -3;
  }

  mroute_read_off();
  qpim_mroute_socket_fd = -1;

  zassert(PIM_MROUTE_IS_DISABLED);

  return 0;
}

/*
  For each network interface (e.g., physical or a virtual tunnel) that
  would be used for multicast forwarding, a corresponding multicast
  interface must be added to the kernel.
 */
int pim_mroute_add_vif(int vif_index, struct in_addr ifaddr, unsigned char flags)
{
  struct vifctl vc;
  int err;

  if (PIM_MROUTE_IS_DISABLED) {
    zlog_warn("%s: global multicast is disabled",
	      __PRETTY_FUNCTION__);
    return -1;
  }

  memset(&vc, 0, sizeof(vc));
  vc.vifc_vifi = vif_index;
  vc.vifc_flags = flags;
  vc.vifc_threshold = PIM_MROUTE_MIN_TTL;
  vc.vifc_rate_limit = 0;
  memcpy(&vc.vifc_lcl_addr, &ifaddr, sizeof(vc.vifc_lcl_addr));

#ifdef PIM_DVMRP_TUNNEL  
  if (vc.vifc_flags & VIFF_TUNNEL) {
    memcpy(&vc.vifc_rmt_addr, &vif_remote_addr, sizeof(vc.vifc_rmt_addr));
  }
#endif

  err = setsockopt(qpim_mroute_socket_fd, IPPROTO_IP, MRT_ADD_VIF, (void*) &vc, sizeof(vc));
  if (err) {
    char ifaddr_str[100];
    int e = errno;

    pim_inet4_dump("<ifaddr?>", ifaddr, ifaddr_str, sizeof(ifaddr_str));

    zlog_warn("%s %s: failure: setsockopt(fd=%d,IPPROTO_IP,MRT_ADD_VIF,vif_index=%d,ifaddr=%s): errno=%d: %s",
	      __FILE__, __PRETTY_FUNCTION__,
	      qpim_mroute_socket_fd, vif_index, ifaddr_str,
	      e, safe_strerror(e));
    errno = e;
    return -2;
  }

  return 0;
}

int pim_mroute_del_vif(int vif_index)
{
  struct vifctl vc;
  int err;

  if (PIM_MROUTE_IS_DISABLED) {
    zlog_warn("%s: global multicast is disabled",
	      __PRETTY_FUNCTION__);
    return -1;
  }

  memset(&vc, 0, sizeof(vc));
  vc.vifc_vifi = vif_index;

  err = setsockopt(qpim_mroute_socket_fd, IPPROTO_IP, MRT_DEL_VIF, (void*) &vc, sizeof(vc)); 
  if (err) {
    int e = errno;
    zlog_warn("%s %s: failure: setsockopt(fd=%d,IPPROTO_IP,MRT_DEL_VIF,vif_index=%d): errno=%d: %s",
	      __FILE__, __PRETTY_FUNCTION__,
	      qpim_mroute_socket_fd, vif_index,
	      e, safe_strerror(e));
    errno = e;
    return -2;
  }

  return 0;
}

int pim_mroute_add(struct mfcctl *mc)
{
  int err;

  qpim_mroute_add_last = pim_time_monotonic_sec();
  ++qpim_mroute_add_events;

  if (PIM_MROUTE_IS_DISABLED) {
    zlog_warn("%s: global multicast is disabled",
	      __PRETTY_FUNCTION__);
    return -1;
  }

  err = setsockopt(qpim_mroute_socket_fd, IPPROTO_IP, MRT_ADD_MFC,
		   mc, sizeof(*mc));
  if (err) {
    int e = errno;
    zlog_warn("%s %s: failure: setsockopt(fd=%d,IPPROTO_IP,MRT_ADD_MFC): errno=%d: %s",
	      __FILE__, __PRETTY_FUNCTION__,
	      qpim_mroute_socket_fd,
	      e, safe_strerror(e));
    errno = e;
    return -2;
  }

  return 0;
}

int pim_mroute_del(struct mfcctl *mc)
{
  int err;

  qpim_mroute_del_last = pim_time_monotonic_sec();
  ++qpim_mroute_del_events;

  if (PIM_MROUTE_IS_DISABLED) {
    zlog_warn("%s: global multicast is disabled",
	      __PRETTY_FUNCTION__);
    return -1;
  }

  err = setsockopt(qpim_mroute_socket_fd, IPPROTO_IP, MRT_DEL_MFC, mc, sizeof(*mc));
  if (err) {
    int e = errno;
    zlog_warn("%s %s: failure: setsockopt(fd=%d,IPPROTO_IP,MRT_DEL_MFC): errno=%d: %s",
	      __FILE__, __PRETTY_FUNCTION__,
	      qpim_mroute_socket_fd,
	      e, safe_strerror(e));
    errno = e;
    return -2;
  }

  return 0;
}