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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
|
/*
* Copyright (C) 1999 LSIIT Laboratory.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the project nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/*
* Copyright (C) 1998 WIDE Project.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the project nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/*
* Questions concerning this software should be directed to
* Mickael Hoerdt (hoerdt@clarinet.u-strasbg.fr) LSIIT Strasbourg.
*
*/
/*
* This program has been derived from pim6dd.
* The pim6dd program is covered by the license in the accompanying file
* named "LICENSE.pim6dd".
*/
/*
* This program has been derived from pimd.
* The pimd program is covered by the license in the accompanying file
* named "LICENSE.pimd".
*
* $FreeBSD$
*/
#include <sys/param.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/uio.h>
#include <errno.h>
#include <netinet/in.h>
#include <string.h>
#include <netinet6/ip6_mroute.h>
#include <netinet6/pim6.h>
#include <netinet/ip6.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <syslog.h>
#include <signal.h>
#include "mld6.h"
#include "defs.h"
#include "kern.h"
#include "pim6.h"
#include "pimd.h"
#include "pim6_proto.h"
#include "inet6.h"
#include "debug.h"
struct sockaddr_in6 allpim6routers_group;
int pim6_socket;
char *pim6_recv_buf;
char *pim6_send_buf;
static struct sockaddr_in6 from;
static struct iovec sndiovpim[2];
static struct iovec rcviovpim[2];
static struct msghdr sndmhpim,
rcvmhpim;
static u_char *sndcmsgbufpim = NULL;
static int sndcmsglen;
static u_char *rcvcmsgbufpim = NULL;
static int rcvcmsglen;
/*
* Local function definitions.
*/
static void pim6_read __P((int f, fd_set *rfd));
static void accept_pim6 __P((int recvlen));
static int pim6_cksum __P((u_short *, struct in6_addr *,
struct in6_addr *, int));
void
init_pim6()
{
struct cmsghdr *cmsgp;
int on;
if ( (pim6_recv_buf = malloc( RECV_BUF_SIZE)) == NULL ||
(pim6_send_buf = malloc (RECV_BUF_SIZE)) == NULL)
log(LOG_ERR,errno,"pim6 buffer allocation");
IF_DEBUG(DEBUG_KERN)
log(LOG_DEBUG,0,"%d octets allocated for the emit/recept buffer pim6",RECV_BUF_SIZE);
if( (pim6_socket = socket(AF_INET6,SOCK_RAW,IPPROTO_PIM)) < 0 )
log(LOG_ERR,errno,"pim6_socket");
k_set_rcvbuf(pim6_socket,SO_RECV_BUF_SIZE_MAX,SO_RECV_BUF_SIZE_MIN);
k_set_hlim(pim6_socket,MINHLIM);
k_set_loop(pim6_socket,FALSE);
memset(&allpim6routers_group, 0, sizeof(allpim6routers_group));
allpim6routers_group.sin6_len = sizeof(allpim6routers_group);
allpim6routers_group.sin6_family = AF_INET6;
if (inet_pton(AF_INET6, "ff02::d",
(void *)&allpim6routers_group.sin6_addr) != 1 )
log(LOG_ERR, 0, "inet_pton failed for ff02::d");
memset(&sockaddr6_d, 0, sizeof(sockaddr6_d));
sockaddr6_d.sin6_len = sizeof(sockaddr6_d);
sockaddr6_d.sin6_family = AF_INET6;
if (inet_pton(AF_INET6, "ff00::",
(void *)&sockaddr6_d.sin6_addr) != 1)
log(LOG_ERR, 0, "inet_pton failed for ff00::");
/* specify to tell receiving interface */
on = 1;
#ifdef IPV6_RECVPKTINFO
if (setsockopt(pim6_socket, IPPROTO_IPV6, IPV6_RECVPKTINFO, &on,
sizeof(on)) < 0)
log(LOG_ERR, errno, "setsockopt(IPV6_RECVPKTINFO)");
#else
if (setsockopt(pim6_socket, IPPROTO_IPV6, IPV6_PKTINFO, &on,
sizeof(on)) < 0)
log(LOG_ERR, errno, "setsockopt(IPV6_PKTINFO)");
#endif
/* initialize msghdr for receiving packets */
rcviovpim[0].iov_base = (caddr_t) pim6_recv_buf;
rcviovpim[0].iov_len = RECV_BUF_SIZE;
rcvmhpim.msg_name = (caddr_t ) &from;
rcvmhpim.msg_namelen = sizeof (from);
rcvmhpim.msg_iov = rcviovpim;
rcvmhpim.msg_iovlen = 1;
rcvcmsglen = CMSG_SPACE(sizeof(struct in6_pktinfo));
if (rcvcmsgbufpim == NULL &&
(rcvcmsgbufpim = malloc(rcvcmsglen)) == NULL)
log(LOG_ERR, 0, "malloc failed");
rcvmhpim.msg_control = (caddr_t ) rcvcmsgbufpim;
rcvmhpim.msg_controllen = rcvcmsglen;
sndmhpim.msg_namelen=sizeof(struct sockaddr_in6);
sndmhpim.msg_iov=sndiovpim;
sndmhpim.msg_iovlen=1;
sndcmsglen = CMSG_SPACE(sizeof(struct in6_pktinfo));
if (sndcmsgbufpim == NULL &&
(sndcmsgbufpim = malloc(sndcmsglen)) == NULL)
log(LOG_ERR, 0, "malloc failed");
sndmhpim.msg_control = (caddr_t)sndcmsgbufpim;
sndmhpim.msg_controllen = sndcmsglen;
cmsgp=(struct cmsghdr *)sndcmsgbufpim;
cmsgp->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo));
cmsgp->cmsg_level = IPPROTO_IPV6;
cmsgp->cmsg_type = IPV6_PKTINFO;
if ( register_input_handler(pim6_socket, pim6_read) <0)
log(LOG_ERR,0,"Registering pim6 socket");
/* Initialize the building Join/Prune messages working area */
build_jp_message_pool = (build_jp_message_t *)NULL;
build_jp_message_pool_counter = 0;
}
/* Read a PIM message */
static void
pim6_read(f, rfd)
int f;
fd_set *rfd;
{
register int pim6_recvlen;
#ifdef SYSV
sigset_t block, oblock;
#else
register int omask;
#endif
pim6_recvlen = recvmsg(pim6_socket,&rcvmhpim,0);
if (pim6_recvlen < 0) {
if (errno != EINTR)
log(LOG_ERR, errno, "PIM6 recvmsg");
return;
}
#ifdef SYSV
(void)sigemptyset(&block);
(void)sigaddset(&block, SIGALRM);
if (sigprocmask(SIG_BLOCK, &block, &oblock) < 0)
log(LOG_ERR, errno, "sigprocmask");
#else
/* Use of omask taken from main() */
omask = sigblock(sigmask(SIGALRM));
#endif /* SYSV */
accept_pim6(pim6_recvlen);
#ifdef SYSV
(void)sigprocmask(SIG_SETMASK, &oblock, (sigset_t *)NULL);
#else
(void)sigsetmask(omask);
#endif /* SYSV */
}
static void
accept_pim6(pimlen)
int pimlen;
{
register struct pim *pim;
struct sockaddr_in6 dst;
struct in6_pktinfo *pi=NULL;
struct sockaddr_in6 *src = (struct sockaddr_in6 *)rcvmhpim.msg_name;
struct cmsghdr *cm;
int ifindex=0;
/* sanity check */
if (pimlen < sizeof(pim)) {
log(LOG_WARNING, 0,
"data field too short (%u bytes) for PIM header, from %s",
pimlen, inet6_fmt(&src->sin6_addr));
return;
}
pim = (struct pim *)rcvmhpim.msg_iov[0].iov_base;
/* extract vital information via Advanced API */
for(cm = (struct cmsghdr *)CMSG_FIRSTHDR(&rcvmhpim);
cm;
cm =(struct cmsghdr *)CMSG_NXTHDR(&rcvmhpim , cm ))
{
if( cm->cmsg_level == IPPROTO_IPV6 &&
cm->cmsg_type == IPV6_PKTINFO &&
cm->cmsg_len == CMSG_LEN(sizeof(struct in6_pktinfo)))
{
pi=(struct in6_pktinfo *)(CMSG_DATA(cm));
dst.sin6_addr=pi->ipi6_addr;
ifindex = pi->ipi6_ifindex;
if (IN6_IS_ADDR_LINKLOCAL(&dst.sin6_addr))
dst.sin6_scope_id = ifindex;
else
dst.sin6_scope_id = 0;
}
}
if(pi==NULL)
log(LOG_ERR,0,"pim6_socket : unable to get destination packet");
if(ifindex==0)
log(LOG_ERR,0,"pim6_socket : unable to get ifindex");
#define NOSUCHDEF
#ifdef NOSUCHDEF /* TODO: delete. Too noisy */
IF_DEBUG(DEBUG_PIM_DETAIL) {
IF_DEBUG(DEBUG_PIM) {
log(LOG_DEBUG, 0, "Receiving %s from %s",
packet_kind(IPPROTO_PIM, pim->pim_type, 0),
inet6_fmt(&src->sin6_addr));
}
}
#endif /* NOSUCHDEF */
/* Check of PIM version is already done in the kernel */
/*
* TODO: check the dest. is ALL_PIM_ROUTERS (if multicast address)
* is it necessary?
*/
/* Checksum verification is done in the kernel. */
switch (pim->pim_type) {
case PIM_HELLO:
receive_pim6_hello(src, (char *)(pim), pimlen);
break;
case PIM_REGISTER:
receive_pim6_register(src, &dst, (char *)(pim), pimlen);
break;
case PIM_REGISTER_STOP:
receive_pim6_register_stop(src, &dst, (char *)(pim), pimlen);
break;
case PIM_JOIN_PRUNE:
receive_pim6_join_prune(src, &dst, (char *)(pim), pimlen);
break;
case PIM_BOOTSTRAP:
receive_pim6_bootstrap(src, &dst, (char *)(pim), pimlen);
break;
case PIM_ASSERT:
receive_pim6_assert(src, &dst, (char *)(pim), pimlen);
break;
case PIM_GRAFT:
pim6dstat.in_pim6_graft++;
log(LOG_INFO, 0, "ignore %s from %s",
packet_kind(IPPROTO_PIM, pim->pim_type, 0),
inet6_fmt(&src->sin6_addr));
break;
case PIM_GRAFT_ACK:
pim6dstat.in_pim6_graft_ack++;
log(LOG_INFO, 0, "ignore %s from %s",
packet_kind(IPPROTO_PIM, pim->pim_type, 0),
inet6_fmt(&src->sin6_addr));
break;
case PIM_CAND_RP_ADV:
receive_pim6_cand_rp_adv(src, &dst, (char *)(pim), pimlen);
break;
default:
log(LOG_INFO, 0,
"ignore unknown PIM message code %u from %s",
pim->pim_type,
inet6_fmt(&src->sin6_addr));
break;
}
}
void
send_pim6(char *buf, struct sockaddr_in6 *src,
struct sockaddr_in6 *dst, int type, int datalen)
{
struct pim *pim;
int setloop=0;
int ifindex=0;
int sendlen=sizeof(struct pim)+datalen;
struct cmsghdr *cmsgp;
struct in6_pktinfo *sndpktinfo;
sndiovpim[0].iov_base=(caddr_t)buf;
sndiovpim[0].iov_len=datalen+sizeof(struct pim);
cmsgp=(struct cmsghdr *)sndcmsgbufpim;
sndpktinfo=(struct in6_pktinfo *)CMSG_DATA(cmsgp);
sndmhpim.msg_name=(caddr_t)dst;
pim = (struct pim *)buf;
pim->pim_type = type;
pim->pim_ver = PIM_PROTOCOL_VERSION;
pim->pim_rsv = 0;
pim->pim_cksum = 0;
if(pim->pim_type == PIM_REGISTER)
{
sendlen = sizeof(struct pim)+sizeof(pim_register_t);
}
pim->pim_cksum = pim6_cksum((u_int16 *)pim,
&src->sin6_addr, &dst->sin6_addr,
sendlen);
if (IN6_IS_ADDR_MULTICAST(&dst->sin6_addr))
{
if (!IN6_IS_ADDR_LINKLOCAL(&src->sin6_addr))
{
log(LOG_WARNING, 0,
"trying to send pim multicast packet "
"with non linklocal src(%s), ignoring",
inet6_fmt(&src->sin6_addr));
return;
}
sndmhpim.msg_control=NULL;
sndmhpim.msg_controllen=0;
ifindex=src->sin6_scope_id;
k_set_if(pim6_socket , ifindex);
if( IN6_ARE_ADDR_EQUAL(&dst->sin6_addr,
&allnodes_group.sin6_addr) ||
IN6_ARE_ADDR_EQUAL(&dst->sin6_addr,
&allrouters_group.sin6_addr) ||
IN6_ARE_ADDR_EQUAL(&dst->sin6_addr,
&allpim6routers_group.sin6_addr))
{
setloop=1;
k_set_loop(pim6_socket, TRUE);
}
}
else
{
sndmhpim.msg_control = (caddr_t)sndcmsgbufpim;
sndmhpim.msg_controllen = sndcmsglen;
sndpktinfo->ipi6_ifindex=src->sin6_scope_id;
memcpy(&sndpktinfo->ipi6_addr, &src->sin6_addr,
sizeof(sndpktinfo->ipi6_addr));
}
if (sendmsg(pim6_socket, &sndmhpim, 0) < 0) {
if (errno == ENETDOWN)
check_vif_state();
else {
log(LOG_WARNING, errno, "sendmsg from %s to %s",
inet6_fmt(&src->sin6_addr),
inet6_fmt(&dst->sin6_addr));
}
}
if(setloop)
k_set_loop(pim6_socket, FALSE);
return;
}
/* ============================== */
/*
* Checksum routine for Internet Protocol family headers (Portable Version).
*
* This routine is very heavily used in the network
* code and should be modified for each CPU to be as fast as possible.
*/
#define ADDCARRY(x) (x > 65535 ? x -= 65535 : x)
#define REDUCE {l_util.l = sum; sum = l_util.s[0] + l_util.s[1]; ADDCARRY(sum);}
static union {
u_short phs[4];
struct {
u_long ph_len;
u_char ph_zero[3];
u_char ph_nxt;
} ph;
} uph;
/*
* Our algorithm is simple, using a 32 bit accumulator (sum), we add
* sequential 16 bit words to it, and at the end, fold back all the
* carry bits from the top 16 bits into the lower 16 bits.
*/
int pim6_cksum(u_short *addr, struct in6_addr *src ,struct in6_addr *dst , int len )
{
register int nleft = len;
register u_short *w;
register int sum = 0;
u_short answer = 0;
/*
* First create IP6 pseudo header and calculate a summary.
*/
w = (u_short *)src;
uph.ph.ph_len = htonl(len);
uph.ph.ph_nxt = IPPROTO_PIM;
/* IPv6 source address */
sum += w[0];
/* XXX: necessary? */
if (!(IN6_IS_ADDR_LINKLOCAL(src) || IN6_IS_ADDR_MC_LINKLOCAL(src)))
sum += w[1];
sum += w[2]; sum += w[3]; sum += w[4]; sum += w[5];
sum += w[6]; sum += w[7];
/* IPv6 destination address */
w = (u_short *)dst;
sum += w[0];
/* XXX: necessary? */
if (!(IN6_IS_ADDR_LINKLOCAL(dst) || IN6_IS_ADDR_MC_LINKLOCAL(dst)))
sum += w[1];
sum += w[2]; sum += w[3]; sum += w[4]; sum += w[5];
sum += w[6]; sum += w[7];
/* Payload length and upper layer identifier */
sum += uph.phs[0]; sum += uph.phs[1];
sum += uph.phs[2]; sum += uph.phs[3];
/*
* Secondly calculate a summary of the first mbuf excluding offset.
*/
w = addr;
while (nleft > 1) {
sum += *w++;
nleft -= 2;
}
/* mop up an odd byte, if necessary */
if (nleft == 1) {
*(u_char *)(&answer) = *(u_char *)w ;
sum += answer;
}
/* add back carry outs from top 16 bits to low 16 bits */
sum = (sum >> 16) + (sum & 0xffff); /* add hi 16 to low 16 */
sum += (sum >> 16); /* add carry */
answer = ~sum; /* truncate to 16 bits */
return(answer);
}
|