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
|
/*
* IGMPv2 - internet group management protocol (and MLDv1)
*/
#include "u.h"
#include "../port/lib.h"
#include "mem.h"
#include "dat.h"
#include "fns.h"
#include "../port/error.h"
#include "ip.h"
#include "ipv6.h"
enum
{
IGMP_IPHDRSIZE = 20, /* size of ip header */
IGMP_HDRSIZE = 8, /* size of IGMP header */
IP_IGMPPROTO = 2,
IGMPquery = 1,
IGMPreport = 2,
IGMPv2report = 6,
IGMPv2leave = 7,
IP_MLDPROTO = HBH, /* hop-by-hop header */
MLDquery = 130,
MLDreport = 131,
MLDdone = 132,
MSPTICK = 100,
MAXTIMEOUT = 10000/MSPTICK, /* at most 10 secs for a response */
};
typedef struct IGMPpkt IGMPpkt;
struct IGMPpkt
{
/* ip header */
uchar vihl; /* Version and header length */
uchar tos; /* Type of service */
uchar len[2]; /* packet length (including headers) */
uchar id[2]; /* Identification */
uchar frag[2]; /* Fragment information */
uchar Unused;
uchar proto; /* Protocol */
uchar cksum[2]; /* checksum of ip portion */
uchar src[4]; /* Ip source */
uchar dst[4]; /* Ip destination */
/* igmp header */
uchar vertype; /* version and type */
uchar resptime;
uchar igmpcksum[2]; /* checksum of igmp portion */
uchar group[4]; /* multicast group */
uchar payload[];
};
#define IGMPPKTSZ offsetof(IGMPpkt, payload[0])
typedef struct MLDpkt MLDpkt;
struct MLDpkt {
IPV6HDR;
uchar type;
uchar code;
uchar cksum[2];
uchar delay[2];
uchar res[2];
uchar group[IPaddrlen];
uchar payload[];
};
#define MLDPKTSZ offsetof(MLDpkt, payload[0])
static uchar mldhbhopt[] = {
ICMPv6, /* NextHeader */
0x00, /* HeaderLength */
0x05, /* Option: Router Alert */
0x02, /* Length */
0x00, 0x00, /* MLD */
0x01, /* Option: PadN */
0x00, /* Length */
};
typedef struct Report Report;
struct Report
{
Report *next;
Proto *proto;
Ipifc *ifc;
int ifcid;
int timeout; /* in MSPTICK's */
Ipmulti *multi;
};
typedef struct Priv Priv;
struct Priv
{
QLock;
Rendez r;
Report *reports;
};
static void
igmpsendreport(Fs *f, uchar *src, uchar *dst, uchar *group, int done)
{
IGMPpkt *p;
Block *bp;
bp = allocb(IGMPPKTSZ);
bp->wp += IGMPPKTSZ;
p = (IGMPpkt*)bp->rp;
memset(p, 0, IGMPPKTSZ);
p->vihl = IP_VER4;
memmove(p->src, src+IPv4off, IPv4addrlen);
memmove(p->dst, dst+IPv4off, IPv4addrlen);
p->vertype = (1<<4) | (done? IGMPv2leave: IGMPv2report);
p->resptime = 0;
p->proto = IP_IGMPPROTO;
memmove(p->group, group+IPv4off, IPv4addrlen);
hnputs(p->igmpcksum, ptclcsum(bp, IGMP_IPHDRSIZE, IGMP_HDRSIZE));
ipoput4(f, bp, nil, 1, DFLTTOS, nil); /* TTL of 1 */
}
static void
mldsendreport(Fs *f, uchar *src, uchar *dst, uchar *group, int done)
{
MLDpkt *p;
Block *bp;
if(!islinklocal(src))
return;
bp = allocb(sizeof(mldhbhopt)+MLDPKTSZ);
bp->wp += sizeof(mldhbhopt)+MLDPKTSZ;
bp->rp += sizeof(mldhbhopt);
p = (MLDpkt*)bp->rp;
memset(p, 0, MLDPKTSZ);
ipmove(p->src, src);
ipmove(p->dst, dst);
p->type = done? MLDdone: MLDreport;
p->code = 0;
ipmove(p->group, group);
/* generate checksum */
hnputl(p->vcf, 0);
hnputs(p->ploadlen, MLDPKTSZ-IP6HDR);
p->proto = 0;
p->ttl = ICMPv6; /* ttl gets set later */
hnputs(p->cksum, 0);
hnputs(p->cksum, ptclcsum(bp, 0, MLDPKTSZ));
/* add hop-by-hop option header */
bp->rp -= sizeof(mldhbhopt);
memmove(bp->rp, p, IP6HDR);
memmove(bp->rp + IP6HDR, mldhbhopt, sizeof(mldhbhopt));
p = (MLDpkt*)bp->rp;
p->proto = IP_MLDPROTO;
hnputs(p->ploadlen, BLEN(bp) - IP6HDR);
ipoput6(f, bp, nil, 1, DFLTTOS, nil); /* TTL of 1 */
}
static void
sendreport(Proto *pr, uchar *ia, uchar *group, int done)
{
switch(pr->ipproto){
case IP_IGMPPROTO:
igmpsendreport(pr->f, ia, group, group, done);
break;
case IP_MLDPROTO:
mldsendreport(pr->f, ia, group, group, done);
break;
}
}
static int
isreport(void *a)
{
return ((Priv*)a)->reports != 0;
}
static void
igmpproc(void *a)
{
Proto *pr, *igmp = a;
Priv *priv = igmp->priv;
Report *rp, **lrp;
Ipmulti *mp, **lmp;
for(;;){
sleep(&priv->r, isreport, priv);
for(;;){
qlock(priv);
if(priv->reports == nil)
break;
/* look for a single report */
mp = nil;
pr = nil;
lrp = &priv->reports;
for(rp = *lrp; rp != nil; rp = *lrp){
lmp = &rp->multi;
for(mp = *lmp; mp != nil; mp = *lmp){
if(rp->timeout <= 1 || nrand(rp->timeout) == 0){
*lmp = mp->next;
break;
}
lmp = &mp->next;
}
pr = rp->proto;
if(rp->multi != nil){
rp->timeout--;
lrp = &rp->next;
} else {
*lrp = rp->next;
free(rp);
}
if(mp != nil)
break;
}
qunlock(priv);
if(mp != nil){
/* do a single report and try again */
if(pr != nil && !waserror()){
sendreport(pr, mp->ia, mp->ma, 0);
poperror();
}
free(mp);
continue;
}
tsleep(&up->sleep, return0, 0, MSPTICK);
}
qunlock(priv);
}
}
/*
* find report list for this protocol and interface
*/
static Report*
findreport(Report *rp, Proto *pr, Ipifc *ifc)
{
for(; rp != nil; rp = rp->next)
if(rp->proto == pr && rp->ifc == ifc && rp->ifcid == ifc->ifcid)
return rp;
return nil;
}
static void
queuereport(Proto *pr, Ipifc *ifc, uchar *group, int timeout)
{
Priv *priv = pr->priv;
Report *rp;
qlock(priv);
if(findreport(priv->reports, pr, ifc) != nil){
/*
* we are already reporting on this interface,
* wait for the report to time-out.
*/
qunlock(priv);
return;
}
/*
* start reporting groups that we're a member of.
*/
rp = smalloc(sizeof(Report));
rp->proto = pr;
rp->ifc = ifc;
rp->ifcid = ifc->ifcid;
rp->timeout = (timeout < 1 || timeout > MAXTIMEOUT) ? MAXTIMEOUT : timeout;
rp->multi = ipifcgetmulti(pr->f, ifc, group);
rp->next = priv->reports;
priv->reports = rp;
wakeup(&priv->r);
qunlock(priv);
}
static void
purgereport(Proto *pr, Ipifc *ifc, uchar *group)
{
Priv *priv = pr->priv;
Report *rp;
qlock(priv);
if((rp = findreport(priv->reports, pr, ifc)) != nil){
Ipmulti *mp, **lmp;
lmp = &rp->multi;
for(mp = *lmp; mp; mp = *lmp){
if(ipcmp(mp->ma, group) == 0){
*lmp = mp->next;
free(mp);
break;
}
lmp = &mp->next;
}
}
qunlock(priv);
}
static void
mldiput(Proto *mld, Ipifc *ifc, Block *bp)
{
MLDpkt *p;
uchar *opt, *payload;
p = (MLDpkt*)(bp->rp);
/* check we have the hop-by-hop header */
if((p->vcf[0] & 0xF0) != IP_VER6 || p->proto != IP_MLDPROTO)
goto error;
if(p->ttl != 1 || !isv6mcast(p->dst) || !islinklocal(p->src))
goto error;
/* strip the hop-by-hop option header */
if(BLEN(bp) < IP6HDR+sizeof(mldhbhopt))
goto error;
opt = bp->rp + IP6HDR;
if(opt[0] != ICMPv6)
goto error;
payload = opt + ((int)opt[1] + 1)*8;
if(payload >= bp->wp)
goto error;
if(memcmp(opt+2, mldhbhopt+2, 4) != 0)
goto error;
memmove(payload-IP6HDR, bp->rp, IP6HDR);
bp->rp = payload - IP6HDR;
if(BLEN(bp) < MLDPKTSZ)
goto error;
p = (MLDpkt*)bp->rp;
/* verify ICMPv6 checksum */
hnputl(p->vcf, 0); /* borrow IP header as pseudoheader */
p->ttl = ICMPv6;
p->proto = 0;
hnputs(p->ploadlen, MLDPKTSZ-IP6HDR);
if(ptclcsum(bp, 0, MLDPKTSZ))
goto error;
if(ipcmp(p->group, IPnoaddr) != 0 && ipismulticast(p->group) != V6)
goto error;
switch(p->type){
case MLDquery:
queuereport(mld, ifc, p->group, nhgets(p->delay)/MSPTICK);
break;
case MLDreport:
purgereport(mld, ifc, p->group);
break;
}
error:
freeblist(bp);
}
static void
igmpiput(Proto *igmp, Ipifc *ifc, Block *bp)
{
uchar group[IPaddrlen];
IGMPpkt *p;
p = (IGMPpkt*)bp->rp;
if((p->vihl & 0xF0) != IP_VER4)
goto error;
if(BLEN(bp) < IGMP_IPHDRSIZE+IGMP_HDRSIZE)
goto error;
if((p->vertype>>4) != 1)
goto error;
if(ptclcsum(bp, IGMP_IPHDRSIZE, IGMP_HDRSIZE))
goto error;
v4tov6(group, p->group);
if(ipcmp(group, v4prefix) != 0 && ipismulticast(group) != V4)
goto error;
switch(p->vertype & 0xF){
case IGMPquery:
queuereport(igmp, ifc, group, p->resptime);
break;
case IGMPreport:
case IGMPv2report:
purgereport(igmp, ifc, group);
break;
}
error:
freeblist(bp);
}
static void
multicastreport(Fs *f, Ipifc *ifc, uchar *ma, uchar *ia, int done)
{
Proto *pr = Fsrcvpcolx(f, isv4(ma)? IP_IGMPPROTO: IP_MLDPROTO);
purgereport(pr, ifc, ma);
sendreport(pr, ia, ma, done);
}
void
igmpinit(Fs *f)
{
Proto *igmp, *mld;
igmp = smalloc(sizeof(Proto));
igmp->priv = smalloc(sizeof(Priv));
igmp->name = "igmp";
igmp->connect = nil;
igmp->announce = nil;
igmp->ctl = nil;
igmp->state = nil;
igmp->close = nil;
igmp->rcv = igmpiput;
igmp->stats = nil;
igmp->ipproto = IP_IGMPPROTO;
igmp->nc = 0;
igmp->ptclsize = 0;
Fsproto(f, igmp);
mld = smalloc(sizeof(Proto));
mld->priv = igmp->priv;
mld->name = "mld";
mld->connect = nil;
mld->announce = nil;
mld->ctl = nil;
mld->state = nil;
mld->close = nil;
mld->rcv = mldiput;
mld->stats = nil;
mld->ipproto = IP_MLDPROTO;
mld->nc = 0;
mld->ptclsize = 0;
Fsproto(f, mld);
multicastreportfn = multicastreport;
kproc("igmpproc", igmpproc, igmp);
}
|