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
|
/*
* Copyright (c) 1998 by the University of Southern California.
* All rights reserved.
*
* Permission to use, copy, modify, and distribute this software and
* its documentation in source and binary forms for lawful
* purposes and without fee is hereby granted, provided
* that the above copyright notice appear in all copies and that both
* the copyright notice and this permission notice appear in supporting
* documentation, and that any documentation, advertising materials,
* and other materials related to such distribution and use acknowledge
* that the software was developed by the University of Southern
* California and/or Information Sciences Institute.
* The name of the University of Southern California may not
* be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THE UNIVERSITY OF SOUTHERN CALIFORNIA DOES NOT MAKE ANY REPRESENTATIONS
* ABOUT THE SUITABILITY OF THIS SOFTWARE FOR ANY PURPOSE. THIS SOFTWARE IS
* PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, TITLE, AND
* NON-INFRINGEMENT.
*
* IN NO EVENT SHALL USC, OR ANY OTHER CONTRIBUTOR BE LIABLE FOR ANY
* SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES, WHETHER IN CONTRACT,
* TORT, OR OTHER FORM OF ACTION, ARISING OUT OF OR IN CONNECTION WITH,
* THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
* Other copyrights might apply to parts of this software and are so
* noted when applicable.
*/
/*
* 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/time.h>
#include <sys/param.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <errno.h>
#include <net/if.h>
#include <net/route.h>
#include <netinet/in.h>
#include <netinet6/ip6_mroute.h>
#if defined(__FreeBSD__) && __FreeBSD__ >= 3
#include <net/if_var.h>
#endif
#include <netinet6/in6_var.h>
#include <syslog.h>
#include "pimd.h"
#include "inet6.h"
#include "vif.h"
#include "mrt.h"
#include "debug.h"
#include "kern.h"
/*
* Open/init the multicast routing in the kernel and sets the MRT_ASSERT
* flag in the kernel.
*
*/
void
k_init_pim(int socket)
{
int v = 1;
if (setsockopt(socket, IPPROTO_IPV6, MRT6_INIT, (char *) &v, sizeof(int)) < 0)
log(LOG_ERR, errno, "cannot enable multicast routing in kernel");
if (setsockopt(socket, IPPROTO_IPV6, MRT6_PIM, (char *) &v, sizeof(int)) < 0)
log(LOG_ERR, errno, "Pim kernel initialization");
}
/*
* Stops the multicast routing in the kernel and resets the MRT_ASSERT flag
* in the kernel.
*/
void
k_stop_pim(socket)
int socket;
{
int v = 0;
if (setsockopt(socket, IPPROTO_IPV6, MRT6_PIM,
(char *) &v, sizeof(int)) < 0)
log(LOG_ERR, errno, "Cannot reset PIM flag in kernel");
if (setsockopt(socket, IPPROTO_IPV6, MRT6_DONE, (char *) NULL, 0) < 0)
log(LOG_ERR, errno, "cannot disable multicast routing in kernel");
}
/*
* Set the socket receiving buffer. `bufsize` is the preferred size,
* `minsize` is the smallest acceptable size.
*/
void
k_set_rcvbuf(int socket, int bufsize, int minsize)
{
int delta = bufsize / 2;
int iter = 0;
/*
* Set the socket buffer. If we can't set it as large as we
* want, search around to try to find the highest acceptable
* value. The highest acceptable value being smaller than
* minsize is a fatal error.
*/
if (setsockopt(socket, SOL_SOCKET, SO_RCVBUF, (char *) &bufsize, sizeof(bufsize)) < 0)
{
bufsize -= delta;
while (1)
{
iter++;
if (delta > 1)
delta /= 2;
if (setsockopt(socket, SOL_SOCKET, SO_RCVBUF, (char *) &bufsize, sizeof(bufsize)) < 0)
bufsize -= delta;
else
{
if (delta < 1024)
break;
bufsize += delta;
}
}
if (bufsize < minsize)
log(LOG_ERR, 0, "OS-allowed buffer size %u < app min %u",
bufsize, minsize);
/*NOTREACHED*/
}
IF_DEBUG(DEBUG_KERN)
log(LOG_DEBUG,0,"Buffer reception size for socket %d : %d in %d iterations",socket, bufsize, iter);
}
/*
* Set the default Hop Limit for the multicast packets outgoing from this
* socket.
*/
void
k_set_hlim(int socket, int h)
{
int hlim = h;
if (setsockopt(socket, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, (char *) &hlim, sizeof(hlim)) < 0)
log(LOG_ERR,errno,"k_set_hlim");
}
/*
* Set/reset the IPV6_MULTICAST_LOOP. Set/reset is specified by "flag".
*/
void
k_set_loop(int socket, int flag)
{
u_int loop;
loop = flag;
if (setsockopt(socket, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, (char *) &loop, sizeof(loop)) < 0)
log(LOG_ERR,errno,"k_set_loop");
}
/*
* Set the IPV6_MULTICAST_IF option on local interface which has the
* specified index.
*/
void
k_set_if(int socket, u_int ifindex)
{
if (setsockopt(socket, IPPROTO_IPV6, IPV6_MULTICAST_IF,
(char *) &ifindex, sizeof(ifindex)) < 0)
log(LOG_ERR, errno, "setsockopt IPV6_MULTICAST_IF for %s",
ifindex2str(ifindex));
}
/*
* Join a multicast grp group on local interface ifa.
*/
void
k_join(int socket, struct in6_addr * grp, u_int ifindex)
{
struct ipv6_mreq mreq;
mreq.ipv6mr_multiaddr = *grp;
mreq.ipv6mr_interface = ifindex;
if (setsockopt(socket, IPPROTO_IPV6, IPV6_JOIN_GROUP,
(char *) &mreq, sizeof(mreq)) < 0)
syslog(LOG_WARNING, "Cannot join group %s on interface %s",
inet6_fmt(grp), ifindex2str(ifindex));
}
/*
* Leave a multicats grp group on local interface ifa.
*/
void
k_leave(int socket, struct in6_addr * grp, u_int ifindex)
{
struct ipv6_mreq mreq;
mreq.ipv6mr_multiaddr = *grp;
mreq.ipv6mr_interface = ifindex;
if (setsockopt(socket, IPPROTO_IPV6, IPV6_LEAVE_GROUP,
(char *) &mreq, sizeof(mreq)) < 0)
syslog(LOG_WARNING, "Cannot leave group %s on interface %s",
inet6_fmt(grp), ifindex2str(ifindex));
}
/*
* Add a virtual interface in the kernel.
*/
void
k_add_vif(int socket, vifi_t vifi, struct uvif * v)
{
struct mif6ctl mc;
mc.mif6c_mifi = vifi;
mc.mif6c_flags = v->uv_flags;
mc.mif6c_pifi = v->uv_ifindex;
if ((v->uv_flags & MIFF_REGISTER))
IF_DEBUG(DEBUG_PIM_REGISTER)
log(LOG_DEBUG,0,"register vifi : %d , register pifi : %d ", vifi, v->uv_ifindex);
if (setsockopt(socket, IPPROTO_IPV6, MRT6_ADD_MIF,
(char *) &mc, sizeof(mc)) < 0)
log(LOG_ERR, errno, "setsockopt MRT6_ADD_MIF on mif %d", vifi);
}
/*
* Delete a virtual interface in the kernel.
*/
void
k_del_vif(int socket, vifi_t vifi)
{
if (setsockopt(socket, IPPROTO_IPV6, MRT6_DEL_MIF,
(char *) &vifi, sizeof(vifi)) < 0)
log(LOG_ERR, errno, "setsockopt MRT6_DEL_MIF on mif %d", vifi);
}
/*
* Delete all MFC entries for particular routing entry from the kernel.
*/
int
k_del_mfc(int socket, struct sockaddr_in6 * source, struct sockaddr_in6 * group)
{
struct mf6cctl mc;
mc.mf6cc_origin = *source;
mc.mf6cc_mcastgrp = *group;
pim6dstat.kern_del_cache++;
if (setsockopt(socket, IPPROTO_IPV6, MRT6_DEL_MFC, (char *) &mc, sizeof(mc)) < 0)
{
pim6dstat.kern_del_cache_fail++;
log(LOG_WARNING, errno, "setsockopt MRT6_DEL_MFC");
return FALSE;
}
syslog(LOG_DEBUG, "Deleted MFC entry : src %s ,grp %s", inet6_fmt(&source->sin6_addr),
inet6_fmt(&group->sin6_addr));
return TRUE;
}
/*
* Install/modify a MFC entry in the kernel
*/
int
k_chg_mfc(socket, source, group, iif, oifs, rp_addr)
int socket;
struct sockaddr_in6 *source;
struct sockaddr_in6 *group;
vifi_t iif;
if_set *oifs;
struct sockaddr_in6 *rp_addr;
{
struct mf6cctl mc;
vifi_t vifi;
struct uvif *v;
mc.mf6cc_origin = *source;
mc.mf6cc_mcastgrp = *group;
mc.mf6cc_parent = iif;
IF_ZERO(&mc.mf6cc_ifset);
for (vifi = 0, v = uvifs; vifi < numvifs; vifi++, v++)
{
if (IF_ISSET(vifi, oifs))
IF_SET(vifi, &mc.mf6cc_ifset);
else
IF_CLR(vifi, &mc.mf6cc_ifset);
}
#ifdef PIM_REG_KERNEL_ENCAP
mc.mf6cc_rp_addr.s_addr = rp_addr;
#endif
pim6dstat.kern_add_cache++;
if (setsockopt(socket, IPPROTO_IPV6, MRT6_ADD_MFC, (char *) &mc,
sizeof(mc)) < 0)
{
pim6dstat.kern_add_cache_fail++;
log(LOG_WARNING, errno,
"setsockopt MRT_ADD_MFC for source %s and group %s",
inet6_fmt(&source->sin6_addr), inet6_fmt(&group->sin6_addr));
return (FALSE);
}
return (TRUE);
}
/*
* Get packet counters for particular interface
*/
/*
* XXX: TODO: currently not used, but keep just in case we need it later.
*/
int
k_get_vif_count(vifi, retval)
vifi_t vifi;
struct vif_count *retval;
{
struct sioc_mif_req6 mreq;
mreq.mifi = vifi;
if (ioctl(udp_socket, SIOCGETMIFCNT_IN6, (char *) &mreq) < 0)
{
log(LOG_WARNING, errno, "SIOCGETMIFCNT_IN6 on vif %d", vifi);
retval->icount = retval->ocount = retval->ibytes =
retval->obytes = 0xffffffff;
return (1);
}
retval->icount = mreq.icount;
retval->ocount = mreq.ocount;
retval->ibytes = mreq.ibytes;
retval->obytes = mreq.obytes;
return (0);
}
/*
* Gets the number of packets, bytes, and number of packets arrived on wrong
* if in the kernel for particular (S,G) entry.
*/
int
k_get_sg_cnt(socket, source, group, retval)
int socket; /* udp_socket */
struct sockaddr_in6 *source;
struct sockaddr_in6 *group;
struct sg_count *retval;
{
struct sioc_sg_req6 sgreq;
sgreq.src = *source;
sgreq.grp = *group;
if (ioctl(socket, SIOCGETSGCNT_IN6, (char *) &sgreq) < 0)
{
pim6dstat.kern_sgcnt_fail++;
log(LOG_WARNING, errno, "SIOCGETSGCNT_IN6 on (%s %s)",
inet6_fmt(&source->sin6_addr), inet6_fmt(&group->sin6_addr));
retval->pktcnt = retval->bytecnt = retval->wrong_if = ~0; /* XXX */
return (1);
}
retval->pktcnt = sgreq.pktcnt;
retval->bytecnt = sgreq.bytecnt;
retval->wrong_if = sgreq.wrong_if;
return (0);
}
|