summaryrefslogtreecommitdiffstats
path: root/src/plugins/igmp/igmp_report.c
blob: 7c08f3421990062fb6befb9e7760f8810b6e0757 (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
/*
 *------------------------------------------------------------------
 * Copyright (c) 2018 Cisco and/or its affiliates.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *------------------------------------------------------------------
 */

#include <igmp/igmp_report.h>
#include <igmp/igmp_pkt.h>

static ip46_address_t *
igmp_group_mk_source_list (const igmp_membership_group_v3_t * r)
{
  ip46_address_t *srcs = NULL;
  const ip4_address_t *s;
  u16 ii, n;

  /*
   * we validated this packet when we accepted it in the DP, so
   * this number is safe to use
   */
  n = clib_net_to_host_u16 (r->n_src_addresses);

  if (0 == n)
    {
      /* a (*,G) join has no source address specified */
      vec_validate (srcs, 0);
      srcs[0].ip4.as_u32 = 0;
    }
  else
    {
      vec_validate (srcs, n - 1);
      s = r->src_addresses;

      for (ii = 0; ii < n; ii++)
	{
	  srcs[ii].ip4 = *s;
	  s++;
	}
    }

  return (srcs);
}

static void
igmp_handle_group_update (igmp_config_t * config,
			  const igmp_membership_group_v3_t * igmp_group)
{
  ip46_address_t *src, *srcs;
  igmp_group_t *group;
  ip46_address_t key = {
    .ip4 = igmp_group->group_address,
  };

  srcs = igmp_group_mk_source_list (igmp_group);
  group = igmp_group_lookup (config, &key);

  IGMP_DBG (" ..group-update: %U (%U, %U)",
	    format_vnet_sw_if_index_name,
	    vnet_get_main (), config->sw_if_index,
	    format_igmp_key, &key, format_igmp_src_addr_list, srcs);

  if (NULL == group)
    {
      group = igmp_group_alloc (config, &key, IGMP_FILTER_MODE_INCLUDE);
    }

  /* create or update all sources */
  vec_foreach (src, srcs)
  {
    igmp_group_src_update (group, src, IGMP_MODE_ROUTER);
  }

  vec_free (srcs);
}

static void
igmp_handle_group_block (igmp_config_t * config,
			 const igmp_membership_group_v3_t * igmp_group)
{
  ip46_address_t *s, *srcs;
  igmp_pkt_build_query_t bq;
  igmp_group_t *group;
  ip46_address_t key = {
    .ip4 = igmp_group->group_address,
  };

  srcs = igmp_group_mk_source_list (igmp_group);
  group = igmp_group_lookup (config, &key);

  IGMP_DBG (" ..group-block: %U (%U, %U)",
	    format_vnet_sw_if_index_name,
	    vnet_get_main (), config->sw_if_index,
	    format_igmp_key, &key, format_igmp_src_addr_list, srcs);

  if (group)
    {
      igmp_src_t *src;
      /*
       * send a group+source specific query
       */
      igmp_pkt_build_query_init (&bq, config->sw_if_index);
      igmp_pkt_query_v3_add_group (&bq, group, srcs);
      igmp_pkt_query_v3_send (&bq);

      /*
       * for each source left/blocked drop the source expire timer to the leave
       * latency timer
       */
      vec_foreach (s, srcs)
      {
	src = igmp_src_lookup (group, s);
	if (NULL != src)
	  igmp_src_blocked (src);
      }
    }
  /*
   * a block/leave from a group for which we have no state
   */

  vec_free (srcs);
}

static void
igmp_handle_group (igmp_config_t * config,
		   const igmp_membership_group_v3_t * igmp_group)
{
  IGMP_DBG ("rx-group-report: %U",
	    format_vnet_sw_if_index_name,
	    vnet_get_main (), config->sw_if_index);

  switch (igmp_group->type)
    {
    case IGMP_MEMBERSHIP_GROUP_mode_is_include:
    case IGMP_MEMBERSHIP_GROUP_change_to_include:
    case IGMP_MEMBERSHIP_GROUP_allow_new_sources:
      igmp_handle_group_update (config, igmp_group);
      break;
    case IGMP_MEMBERSHIP_GROUP_block_old_sources:
      igmp_handle_group_block (config, igmp_group);
      break;
    case IGMP_MEMBERSHIP_GROUP_mode_is_exclude:
    case IGMP_MEMBERSHIP_GROUP_change_to_exclude:
      break;
      /*
       * all other types ignored
       */
    }
}

void
igmp_handle_report (const igmp_report_args_t * args)
{
  const igmp_membership_group_v3_t *igmp_group;
  igmp_config_t *config;
  u16 n_groups, ii;

  config = igmp_config_lookup (args->sw_if_index);

  if (!config)
    /*
     * no IGMP config on the interface. quit
     */
    return;

  if (IGMP_MODE_HOST == config->mode)
    {
      /*
       * Hosts need not listen to the reports of other hosts.
       * we're done here
       */
      return;
    }

  /*
   * we validated this packet when we accepted it in the DP, so
   * this number is safe to use
   */
  n_groups = clib_net_to_host_u16 (args->report[0].n_groups);
  igmp_group = args->report[0].groups;

  for (ii = 0; ii < n_groups; ii++)
    {
      igmp_handle_group (config, igmp_group);

      igmp_group = group_cptr (igmp_group,
			       igmp_membership_group_v3_length (igmp_group));
    }

  igmp_proxy_device_merge_config (config, 0);
}

/*
 * fd.io coding-style-patch-verification: ON
 *
 * Local Variables:
 * eval: (c-set-style "gnu")
 * End:
 */