summaryrefslogtreecommitdiff
path: root/pimd/pim_oil.c
blob: ebbc6e19f96e843cafcbeed25dde6f91dd522dd3 (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
/*
  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
*/

#include <zebra.h>

#include "log.h"
#include "memory.h"
#include "linklist.h"
#include "if.h"

#include "pimd.h"
#include "pim_oil.h"
#include "pim_str.h"
#include "pim_iface.h"
#include "pim_time.h"

void pim_channel_oil_free(struct channel_oil *c_oil)
{
  XFREE(MTYPE_PIM_CHANNEL_OIL, c_oil);
}

static void pim_channel_oil_delete(struct channel_oil *c_oil)
{
  /*
    notice that listnode_delete() can't be moved
    into pim_channel_oil_free() because the later is
    called by list_delete_all_node()
  */
  listnode_delete(qpim_channel_oil_list, c_oil);

  pim_channel_oil_free(c_oil);
}

static struct channel_oil *channel_oil_new(struct in_addr group_addr,
					   struct in_addr source_addr,
					   int input_vif_index)
{
  struct channel_oil *c_oil;
  struct interface *ifp_in;

  ifp_in = pim_if_find_by_vif_index(input_vif_index);
  if (!ifp_in) {
    /* warning only */
    char group_str[100]; 
    char source_str[100];
    pim_inet4_dump("<group?>", group_addr, group_str, sizeof(group_str));
    pim_inet4_dump("<source?>", source_addr, source_str, sizeof(source_str));
    zlog_warn("%s: (S,G)=(%s,%s) could not find input interface for input_vif_index=%d",
	      __PRETTY_FUNCTION__,
	      source_str, group_str, input_vif_index);
  }

  c_oil = XCALLOC(MTYPE_PIM_CHANNEL_OIL, sizeof(*c_oil));
  if (!c_oil) {
    zlog_err("PIM XCALLOC(%zu) failure", sizeof(*c_oil));
    return 0;
  }

  c_oil->oil.mfcc_mcastgrp = group_addr;
  c_oil->oil.mfcc_origin   = source_addr;
  c_oil->oil.mfcc_parent   = input_vif_index;
  c_oil->oil_ref_count     = 1;
  c_oil->installed         = 0;

  zassert(c_oil->oil_size == 0);

  return c_oil;
}

static struct channel_oil *pim_add_channel_oil(struct in_addr group_addr,
					       struct in_addr source_addr,
					       int input_vif_index)
{
  struct channel_oil *c_oil;

  c_oil = channel_oil_new(group_addr, source_addr, input_vif_index);
  if (!c_oil) {
    zlog_warn("PIM XCALLOC(%zu) failure", sizeof(*c_oil));
    return 0;
  }

  listnode_add(qpim_channel_oil_list, c_oil);

  return c_oil;
}

static struct channel_oil *pim_find_channel_oil(struct in_addr group_addr,
						struct in_addr source_addr)
{
  struct listnode    *node;
  struct channel_oil *c_oil;

  for (ALL_LIST_ELEMENTS_RO(qpim_channel_oil_list, node, c_oil)) {
    if ((group_addr.s_addr == c_oil->oil.mfcc_mcastgrp.s_addr) &&
	(source_addr.s_addr == c_oil->oil.mfcc_origin.s_addr))
      return c_oil;
  }
  
  return 0;
}

struct channel_oil *pim_channel_oil_add(struct in_addr group_addr,
					struct in_addr source_addr,
					int input_vif_index)
{
  struct channel_oil *c_oil;

  c_oil = pim_find_channel_oil(group_addr, source_addr);
  if (c_oil) {
    ++c_oil->oil_ref_count;
    return c_oil;
  }

  return pim_add_channel_oil(group_addr, source_addr, input_vif_index);
}

void pim_channel_oil_del(struct channel_oil *c_oil)
{
  --c_oil->oil_ref_count;

  if (c_oil->oil_ref_count < 1) {
    pim_channel_oil_delete(c_oil);
  }
}

int pim_channel_add_oif(struct channel_oil *channel_oil,
		   struct interface *oif,
		   uint32_t proto_mask)
{
  struct pim_interface *pim_ifp;
  int old_ttl;

  zassert(channel_oil);

  pim_ifp = oif->info;

  if (PIM_DEBUG_MROUTE) {
    char group_str[100];
    char source_str[100];
    pim_inet4_dump("<group?>", channel_oil->oil.mfcc_mcastgrp, group_str, sizeof(group_str));
    pim_inet4_dump("<source?>", channel_oil->oil.mfcc_origin, source_str, sizeof(source_str));
    zlog_debug("%s %s: (S,G)=(%s,%s): proto_mask=%u OIF=%s vif_index=%d",
	       __FILE__, __PRETTY_FUNCTION__,
	       source_str, group_str,
	       proto_mask, oif->name, pim_ifp->mroute_vif_index);
  }

#ifdef PIM_ENFORCE_LOOPFREE_MFC
  /*
    Prevent creating MFC entry with OIF=IIF.

    This is a protection against implementation mistakes.

    PIM protocol implicitely ensures loopfree multicast topology.

    IGMP must be protected against adding looped MFC entries created
    by both source and receiver attached to the same interface. See
    TODO T22.
  */
  if (pim_ifp->mroute_vif_index == channel_oil->oil.mfcc_parent) {
    if (PIM_DEBUG_MROUTE)
      {
	char group_str[100];
	char source_str[100];
	pim_inet4_dump("<group?>", channel_oil->oil.mfcc_mcastgrp, group_str, sizeof(group_str));
	pim_inet4_dump("<source?>", channel_oil->oil.mfcc_origin, source_str, sizeof(source_str));
	zlog_debug("%s %s: refusing protocol mask %u request for IIF=OIF=%s (vif_index=%d) for channel (S,G)=(%s,%s)",
		   __FILE__, __PRETTY_FUNCTION__,
		   proto_mask, oif->name, pim_ifp->mroute_vif_index,
		   source_str, group_str);
      }
    return -2;
  }
#endif

  /* Prevent single protocol from subscribing same interface to
     channel (S,G) multiple times */
  if (channel_oil->oif_flags[pim_ifp->mroute_vif_index] & proto_mask) {
    if (PIM_DEBUG_MROUTE)
      {
	char group_str[100];
	char source_str[100];
	pim_inet4_dump("<group?>", channel_oil->oil.mfcc_mcastgrp, group_str, sizeof(group_str));
	pim_inet4_dump("<source?>", channel_oil->oil.mfcc_origin, source_str, sizeof(source_str));
	zlog_debug("%s %s: existing protocol mask %u requested OIF %s (vif_index=%d, min_ttl=%d) for channel (S,G)=(%s,%s)",
		   __FILE__, __PRETTY_FUNCTION__,
		   proto_mask, oif->name, pim_ifp->mroute_vif_index,
		   channel_oil->oil.mfcc_ttls[pim_ifp->mroute_vif_index],
		   source_str, group_str);
      }
    return -3;
  }

  /* Allow other protocol to request subscription of same interface to
     channel (S,G) multiple times, by silently ignoring further
     requests */
  if (channel_oil->oif_flags[pim_ifp->mroute_vif_index] & PIM_OIF_FLAG_PROTO_ANY) {

    /* Check the OIF really exists before returning, and only log
       warning otherwise */
    if (channel_oil->oil.mfcc_ttls[pim_ifp->mroute_vif_index] < 1) {
      if (PIM_DEBUG_MROUTE)
	{
	  char group_str[100];
	  char source_str[100];
	  pim_inet4_dump("<group?>", channel_oil->oil.mfcc_mcastgrp, group_str, sizeof(group_str));
	  pim_inet4_dump("<source?>", channel_oil->oil.mfcc_origin, source_str, sizeof(source_str));
	  zlog_debug("%s %s: new protocol mask %u requested nonexistent OIF %s (vif_index=%d, min_ttl=%d) for channel (S,G)=(%s,%s)",
		     __FILE__, __PRETTY_FUNCTION__,
		     proto_mask, oif->name, pim_ifp->mroute_vif_index,
		     channel_oil->oil.mfcc_ttls[pim_ifp->mroute_vif_index],
		     source_str, group_str);
	}
    }

    return 0;
  }

  old_ttl = channel_oil->oil.mfcc_ttls[pim_ifp->mroute_vif_index];

  if (old_ttl > 0) {
    if (PIM_DEBUG_MROUTE)
      {
	char group_str[100];
	char source_str[100];
	pim_inet4_dump("<group?>", channel_oil->oil.mfcc_mcastgrp, group_str, sizeof(group_str));
	pim_inet4_dump("<source?>", channel_oil->oil.mfcc_origin, source_str, sizeof(source_str));
	zlog_debug("%s %s: interface %s (vif_index=%d) is existing output for channel (S,G)=(%s,%s)",
		   __FILE__, __PRETTY_FUNCTION__,
		   oif->name, pim_ifp->mroute_vif_index,
		   source_str, group_str);
      }
    return -4;
  }

  channel_oil->oil.mfcc_ttls[pim_ifp->mroute_vif_index] = PIM_MROUTE_MIN_TTL;

  if (pim_mroute_add(channel_oil)) {
    if (PIM_DEBUG_MROUTE)
      {
	char group_str[100];
	char source_str[100];
	pim_inet4_dump("<group?>", channel_oil->oil.mfcc_mcastgrp, group_str, sizeof(group_str));
	pim_inet4_dump("<source?>", channel_oil->oil.mfcc_origin, source_str, sizeof(source_str));
	zlog_debug("%s %s: could not add output interface %s (vif_index=%d) for channel (S,G)=(%s,%s)",
		   __FILE__, __PRETTY_FUNCTION__,
		   oif->name, pim_ifp->mroute_vif_index,
		   source_str, group_str);
      }

    channel_oil->oil.mfcc_ttls[pim_ifp->mroute_vif_index] = old_ttl;
    return -5;
  }

  channel_oil->oif_creation[pim_ifp->mroute_vif_index] = pim_time_monotonic_sec();
  ++channel_oil->oil_size;
  channel_oil->oif_flags[pim_ifp->mroute_vif_index] |= proto_mask;

  if (PIM_DEBUG_MROUTE) {
    char group_str[100];
    char source_str[100];
    pim_inet4_dump("<group?>", channel_oil->oil.mfcc_mcastgrp, group_str, sizeof(group_str));
    pim_inet4_dump("<source?>", channel_oil->oil.mfcc_origin, source_str, sizeof(source_str));
    zlog_debug("%s %s: (S,G)=(%s,%s): proto_mask=%u OIF=%s vif_index=%d: DONE",
	       __FILE__, __PRETTY_FUNCTION__,
	       source_str, group_str,
	       proto_mask, oif->name, pim_ifp->mroute_vif_index);
  }

  return 0;
}