1 // SPDX-License-Identifier: GPL-2.0-or-later
4 * Copyright (C) 2021-2022 David Lamparter for NetDEF, Inc.
19 #define MLD_DEFAULT_VERSION 2
21 /* see comment below on subs_negative/subs_positive */
23 /* negative/pruning: S,G in EXCLUDE */
25 /* positive/joining: *,G in EXCLUDE and S,G in INCLUDE */
33 /* remaining 3 only valid for S,G when *,G in EXCLUDE */
36 GM_SG_NOPRUNE_EXPIRING
,
39 static inline bool gm_sg_state_want_join(enum gm_sg_state state
)
41 return state
!= GM_SG_NOINFO
&& state
!= GM_SG_PRUNE
;
44 /* MLD (S,G) state (on an interface)
46 * group is always != ::, src is :: for (*,G) joins. sort order in RB tree is
47 * such that sources for a particular group can be iterated by starting at the
48 * group. For INCLUDE, no (*,G) entry exists, only (S,G).
51 PREDECL_RBTREE_UNIQ(gm_packet_sg_subs
);
52 PREDECL_RBTREE_UNIQ(gm_sgs
);
56 struct gm_sgs_item itm
;
58 enum gm_sg_state state
;
59 struct channel_oil
*oil
;
62 struct timeval created
;
64 /* if a group- or group-and-source specific query is running
65 * (implies we haven't received any report yet, since it's cancelled
68 struct event
*t_sg_expire
;
70 /* last-member-left triggered queries (group/group-source specific)
72 * this timer will be running even if we aren't the elected querier,
73 * in case the election result changes midway through.
75 struct event
*t_sg_query
;
77 /* we must keep sending (QRV) queries even if we get a positive
78 * response, to make sure other routers are updated. query_sbit
79 * will be set in that case, since other routers need the *response*,
85 /* subs_positive tracks gm_packet_sg resulting in a JOIN, i.e. for
86 * (*,G) it has *EXCLUDE* items, for (S,G) it has *INCLUDE* items.
88 * subs_negative is always empty for (*,G) and tracks EXCLUDE items
89 * for (S,G). This means that an (S,G) entry is active as a PRUNE if
90 * len(src->subs_negative) == len(grp->subs_positive)
91 * && len(src->subs_positive) == 0
92 * (i.e. all receivers for the group opted to exclude this S,G and
93 * noone did an SSM join for the S,G)
97 struct gm_packet_sg_subs_head subs_negative
[1];
98 struct gm_packet_sg_subs_head subs_positive
[1];
100 struct gm_packet_sg_subs_head subs
[2];
103 /* If the elected querier is not ourselves, queries and reports might
104 * get reordered in rare circumstances, i.e. the report could arrive
105 * just a microsecond before the query kicks off the timer. This can
106 * then result in us thinking there are no more receivers since no
107 * report might be received during the query period.
109 * To avoid this, keep track of the most recent report for this (S,G)
110 * so we can do a quick check to add just a little bit of slack.
112 * EXCLUDE S,Gs are never in most_recent.
114 struct gm_packet_sg
*most_recent
;
116 int gm_sg_cmp(const struct gm_sg
*a
, const struct gm_sg
*b
);
117 DECLARE_RBTREE_UNIQ(gm_sgs
, struct gm_sg
, itm
, gm_sg_cmp
);
119 /* host tracking entry. addr will be one of:
121 * :: - used by hosts during address acquisition
122 * ::1 - may show up on some OS for joins by the router itself
123 * link-local - regular operation by MLDv2 hosts
124 * ffff:..:ffff - MLDv1 entry (cannot be tracked due to report suppression)
126 * global scope IPv6 addresses can never show up here
128 PREDECL_HASH(gm_subscribers
);
129 PREDECL_DLIST(gm_packets
);
130 struct gm_subscriber
{
132 struct gm_subscribers_item itm
;
137 struct gm_packets_head packets
[1];
139 struct timeval created
;
143 * MLD join state is kept batched by packet. Since the timers for all items
144 * in a packet are the same, this reduces the number of timers we're keeping
145 * track of. It also eases tracking for EXCLUDE state groups because the
146 * excluded sources are in the same packet. (MLD does not support splitting
147 * that if it exceeds MTU, it's always a full replace for exclude.)
149 * Since packets may be partially superseded by newer packets, the "active"
150 * field is used to track this.
153 /* gm_packet_sg is allocated as part of gm_packet_state, note the items[0]
154 * array at the end of that. gm_packet_sg is NEVER directly allocated with
157 struct gm_packet_sg
{
158 /* non-NULL as long as this gm_packet_sg is the most recent entry
159 * for (subscriber,S,G). Cleared to NULL when a newer packet by the
160 * subscriber replaces this item.
162 * (Old items are kept around so we don't need to realloc/resize
163 * gm_packet_state, which would mess up a whole lot of pointers)
167 /* gm_sg -> (subscriber, gm_packet_sg)
168 * only on RB-tree while sg != NULL, i.e. not superseded by newer.
170 struct gm_packet_sg_subs_item subs_itm
;
172 bool is_src
: 1; /* := (src != ::) */
175 /* for getting back to struct gm_packet_state, cf.
176 * gm_packet_sg2state() below
180 /* if this is a group entry in EXCLUDE state, n_exclude counts how
181 * many sources are on the exclude list here. They follow immediately
187 #define gm_packet_sg2state(sg) \
188 container_of(sg, struct gm_packet_state, items[sg->offset])
190 PREDECL_DLIST(gm_packet_expires
);
191 struct gm_packet_state
{
193 struct gm_subscriber
*subscriber
;
194 struct gm_packets_item pkt_itm
;
196 struct timeval received
;
197 struct gm_packet_expires_item exp_itm
;
199 /* n_active starts equal to n_sg; whenever active is set to false on
200 * an item it is decremented. When n_active == 0, the packet can be
203 uint16_t n_sg
, n_active
;
204 struct gm_packet_sg items
[0];
207 /* general queries are rather different from group/S,G specific queries; it's
208 * not particularly efficient or useful to try to shoehorn them into the S,G
209 * timers. Instead, we keep a history of recent queries and their implied
212 struct gm_general_pending
{
213 struct timeval query
, expiry
;
216 /* similarly, group queries also age out S,G entries for the group, but in
217 * this case we only keep one query for each group
219 * why is this not in the *,G gm_sg? There may not be one (for INCLUDE mode
220 * groups, or groups we don't know about.) Also, malicious clients could spam
221 * random group-specific queries to trigger resource exhaustion, so it makes
222 * sense to limit these.
224 PREDECL_RBTREE_UNIQ(gm_grp_pends
);
225 struct gm_grp_pending
{
226 struct gm_grp_pends_item itm
;
230 struct timeval query
;
231 struct event
*t_expire
;
234 /* guaranteed MTU for IPv6 is 1280 bytes. IPv6 header is 40 bytes, MLDv2
235 * query header is 24 bytes, RA option is 8 bytes - leaves 1208 bytes for the
236 * source list, which is 151 IPv6 addresses. But we may have some more IPv6
237 * extension headers (e.g. IPsec AH), so just cap to 128
239 #define MLD_V2Q_MTU_MAX_SOURCES 128
241 /* group-and-source-specific queries are bundled together, if some host joins
242 * multiple sources it's likely to drop all at the same time.
244 * Unlike gm_grp_pending, this is only used for aggregation since the S,G
245 * state is kept directly in the gm_sg structure.
247 PREDECL_HASH(gm_gsq_pends
);
248 struct gm_gsq_pending
{
249 struct gm_gsq_pends_item itm
;
252 struct event
*t_send
;
258 pim_addr srcs
[MLD_V2Q_MTU_MAX_SOURCES
];
262 /* The size of this history is limited by QRV, i.e. there can't be more than
265 #define GM_MAX_PENDING 8
274 uint64_t rx_drop_csum
;
275 uint64_t rx_drop_srcaddr
;
276 uint64_t rx_drop_dstaddr
;
278 uint64_t rx_drop_malformed
;
279 uint64_t rx_trunc_report
;
281 /* since the types are different, this is rx_old_* not of rx_*_old */
282 uint64_t rx_old_report
;
283 uint64_t rx_old_leave
;
284 uint64_t rx_new_report
;
286 uint64_t rx_query_new_general
;
287 uint64_t rx_query_new_group
;
288 uint64_t rx_query_new_groupsrc
;
289 uint64_t rx_query_new_sbit
;
290 uint64_t rx_query_old_general
;
291 uint64_t rx_query_old_group
;
293 uint64_t tx_query_new_general
;
294 uint64_t tx_query_new_group
;
295 uint64_t tx_query_new_groupsrc
;
296 uint64_t tx_query_old_general
;
297 uint64_t tx_query_old_group
;
299 uint64_t tx_query_fail
;
303 struct interface
*ifp
;
304 struct pim_instance
*pim
;
305 struct event
*t_query
, *t_other_querier
, *t_expire
;
312 unsigned int cur_query_intv
; /* ms */
313 unsigned int cur_query_intv_trig
; /* ms */
314 unsigned int cur_max_resp
; /* ms */
315 enum gm_version cur_version
;
316 int cur_lmqc
; /* last member query count in ds */
318 /* this value (positive, default 10ms) defines our "timing tolerance":
319 * - added to deadlines for expiring joins
320 * - used to look backwards in time for queries, in case a report was
321 * reordered before the query
323 struct timeval cfg_timing_fuzz
;
325 /* items in pending[] are sorted by expiry, pending[0] is earliest */
326 struct gm_general_pending pending
[GM_MAX_PENDING
];
328 struct gm_grp_pends_head grp_pends
[1];
329 struct gm_gsq_pends_head gsq_pends
[1];
332 pim_addr cur_ll_lowest
;
334 struct gm_sgs_head sgs
[1];
335 struct gm_subscribers_head subscribers
[1];
336 struct gm_packet_expires_head expires
[1];
338 struct timeval started
;
339 struct gm_if_stats stats
;
343 extern void gm_ifp_update(struct interface
*ifp
);
344 extern void gm_ifp_teardown(struct interface
*ifp
);
345 extern void gm_group_delete(struct gm_if
*gm_ifp
);
347 static inline void gm_ifp_update(struct interface
*ifp
)
351 static inline void gm_ifp_teardown(struct interface
*ifp
)
356 extern void gm_cli_init(void);
357 bool in6_multicast_nofwd(const pim_addr
*addr
);
359 #endif /* PIM6_MLD_H */