summaryrefslogtreecommitdiffstats
path: root/src/querier.c
blob: cf489f91af8b4cfc9379e95016f2ebfcfc3c7228 (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
/*
 * Author: Steven Barth <steven at midlink.org>
 *
 * Copyright 2015 Deutsche Telekom AG
 *
 * 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 <errno.h>
#include <string.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <libubox/ustream.h>
#include <libubox/usock.h>
#include <libubox/list.h>

#include "querier.h"

static struct list_head ifaces = LIST_HEAD_INIT(ifaces);


// Handle querier update event from a querier-interface
static void querier_announce_iface(struct querier_user_iface *user, omgp_time_t now, const struct group *group, bool enabled)
{
	bool include = true;
	size_t cnt = 0;
	struct in6_addr sources[group->source_count];

	if (enabled) {
		struct group_source *source;
		group_for_each_active_source(source, group, now)
			sources[cnt++] = source->addr;

		include = group_is_included(group, now);
	}

	if (user->user_cb)
		user->user_cb(user, &group->addr, include, sources, cnt);
}

// Handle changes from a querier for a given group (called by a group-state as callback)
static void querier_announce_change(struct groups *groups, struct group *group, omgp_time_t now)
{
	struct querier_iface *iface = container_of(groups, struct querier_iface, groups);

	// Only recognize changes to non-link-local groups
	struct querier_user_iface *user;
	list_for_each_entry(user, &iface->users, head)
		querier_announce_iface(user, now, group, true);
}

// Send query for a group + sources (called by a group-state as callback)
static void querier_send_query(struct groups *groups, const struct in6_addr *group,
		const struct list_head *sources, bool suppress)
{
	struct querier_iface *iface = container_of(groups, struct querier_iface, groups);
	char addrbuf[INET6_ADDRSTRLEN] = "::";
	inet_ntop(AF_INET6, group, addrbuf, sizeof(addrbuf));

	L_DEBUG("%s: sending %s-specific query for %s on %d (S: %d)", __FUNCTION__,
			(!sources) ? "group" : "source", addrbuf, iface->ifindex, suppress);

	bool v4 = IN6_IS_ADDR_V4MAPPED(group);
	if (v4 && !iface->igmp_other_querier)
		igmp_send_query(iface, group, sources, suppress);
	else if (!v4 && !iface->mld_other_querier)
		mld_send_query(iface, group, sources, suppress);
}

// Expire interface timers and send queries (called by timer as callback)
static void querier_iface_timer(struct uloop_timeout *timeout)
{
	struct querier_iface *iface = container_of(timeout, struct querier_iface, timeout);
	omgp_time_t now = omgp_time();
	omgp_time_t next_event = now + 3600 * OMGP_TIME_PER_SECOND;

	if (iface->igmp_next_query <= now) {
		// If the other querier is gone, reset interface config
		if (iface->igmp_other_querier) {
			iface->groups.cfg_v4 = iface->cfg;
			iface->igmp_other_querier = false;
		}

		igmp_send_query(iface, NULL, NULL, false);
		L_DEBUG("%s: sending generic IGMP-query on %d (S: 0)", __FUNCTION__, iface->ifindex);

		if (iface->igmp_startup_tries > 0)
			--iface->igmp_startup_tries;

		iface->igmp_next_query = now + ((iface->igmp_startup_tries > 0) ?
						(iface->groups.cfg_v4.query_interval / 4) :
						iface->groups.cfg_v4.query_interval);
	}

	if (iface->igmp_next_query < next_event)
		next_event = iface->igmp_next_query;

	if (iface->mld_next_query <= now) {
		// If the other querier is gone, reset interface config
		if (iface->mld_other_querier) {
			iface->groups.cfg_v6 = iface->cfg;
			iface->mld_other_querier = false;
		}

		mld_send_query(iface, NULL, NULL, false);
		L_DEBUG("%s: sending generic MLD-query on %d (S: 0)", __FUNCTION__, iface->ifindex);

		if (iface->mld_startup_tries > 0)
			--iface->mld_startup_tries;

		iface->mld_next_query = now + ((iface->mld_startup_tries > 0) ?
						(iface->groups.cfg_v6.query_interval / 4) :
						iface->groups.cfg_v6.query_interval);
	}

	if (iface->mld_next_query < next_event)
		next_event = iface->mld_next_query;

	uloop_timeout_set(&iface->timeout, (next_event > now) ? next_event - now : 0);
}


// Calculate QQI from QQIC
int querier_qqi(uint8_t qqic)
{
	return (qqic & 0x80) ? (((qqic & 0xf) | 0x10) << (((qqic >> 4) & 0x7) + 3)) : qqic;
}

// Calculate MRD from MRC
int querier_mrd(uint16_t mrc)
{
	mrc = ntohs(mrc);
	return (mrc & 0x8000) ? (((mrc & 0xfff) | 0x1000) << (((mrc >> 12) & 0x7) + 3)) : mrc;
}

// Calculate QQIC from QQI
uint8_t querier_qqic(int qqi)
{
	if (qqi >= 128) {
		int exp = 3;

		while ((qqi >> exp) > 0x1f && exp <= 10)
			++exp;

		if (exp > 10)
			qqi = 0xff;
		else
			qqi = 0x80 | ((exp - 3) << 4) | ((qqi >> exp) & 0xf);
	}
	return qqi;
}

// Calculate MRC from MRD
uint16_t querier_mrc(int mrd)
{
	if (mrd >= 32768) {
		int exp = 3;

		while ((mrd >> exp) > 0x1fff && exp <= 10)
			++exp;

		if (exp > 10)
			mrd = 0xffff;
		else
			mrd = 0x8000 | ((exp - 3) << 12) | ((mrd >> exp) & 0xfff);
	}
	return htons(mrd);
}

// Attach an interface to a querier-instance
int querier_attach(struct querier_user_iface *user,
		struct querier *querier, int ifindex, querier_iface_cb *cb)
{
	struct querier_iface *c, *iface = NULL;
	list_for_each_entry(c, &ifaces, head) {
		if (c->ifindex == ifindex) {
			iface = c;
			break;
		}
	}

	omgp_time_t now = omgp_time();
	int res = 0;
	if (!iface) {
		if (!(iface = calloc(1, sizeof(*iface)))) {
			res = -errno;
			goto out;
		}

		list_add(&iface->head, &ifaces);
		INIT_LIST_HEAD(&iface->users);

		iface->ifindex = ifindex;
		iface->timeout.cb = querier_iface_timer;
		uloop_timeout_set(&iface->timeout, 0);

		groups_init(&iface->groups);
		iface->groups.source_limit = QUERIER_MAX_SOURCE;
		iface->groups.group_limit = QUERIER_MAX_GROUPS;
		iface->groups.cb_update = querier_announce_change;
		iface->groups.cb_query = querier_send_query;
		iface->cfg = iface->groups.cfg_v6;
		iface->igmp_startup_tries = iface->groups.cfg_v4.robustness;
		iface->mld_startup_tries = iface->groups.cfg_v6.robustness;

		if ((res = mrib_attach_querier(&iface->mrib, ifindex, igmp_handle, mld_handle)))
			goto out;
	}

out:
	if (iface) {
		list_add(&user->head, &iface->users);
		user->iface = iface;

		list_add(&user->user.head, &querier->ifaces);
		user->user_cb = cb;
		user->user.querier = querier;
		user->user.groups = &iface->groups;

		struct group *group;
		groups_for_each_group(group, &iface->groups)
			querier_announce_iface(user, now, group, true);
	}

	if (res)
		querier_detach(user);
	return res;
}

// Detach an interface from a querier-instance
void querier_detach(struct querier_user_iface *user)
{
	struct querier_iface *iface = user->iface;
	list_del(&user->user.head);
	list_del(&user->head);

	omgp_time_t now = omgp_time();
	struct group *group;
	groups_for_each_group(group, &iface->groups)
		querier_announce_iface(user, now, group, false);

	if (list_empty(&iface->users)) {
		uloop_timeout_cancel(&iface->timeout);
		groups_deinit(&iface->groups);
		mrib_detach_querier(&iface->mrib);
		list_del(&iface->head);
		free(iface);
	}
}

// Initialize querier-instance
int querier_init(struct querier *querier)
{
	memset(querier, 0, sizeof(*querier));
	INIT_LIST_HEAD(&querier->ifaces);
	return 0;
}

// Cleanup querier-instance
void querier_deinit(struct querier *querier)
{
	struct querier_user *user, *n;
	list_for_each_entry_safe(user, n, &querier->ifaces, head)
		querier_detach(container_of(user, struct querier_user_iface, user));
}