1 /* $NetBSD: kern.c,v 1.9 2003/05/16 22:59:50 dsl Exp $ */
4 * The mrouted program is covered by the license in the accompanying file
5 * named "LICENSE". Use of the mrouted program represents acceptance of
6 * the terms and conditions listed in that file.
8 * The mrouted program is COPYRIGHT 1989 by The Board of Trustees of
9 * Leland Stanford Junior University.
16 void k_set_rcvbuf(int bufsize
)
18 if (setsockopt(igmp_socket
, SOL_SOCKET
, SO_RCVBUF
,
19 (char *)&bufsize
, sizeof(bufsize
)) < 0)
20 logit(LOG_ERR
, errno
, "setsockopt SO_RCVBUF %u", bufsize
);
24 void k_hdr_include(int bool)
27 if (setsockopt(igmp_socket
, IPPROTO_IP
, IP_HDRINCL
,
28 (char *)&bool, sizeof(bool)) < 0)
29 logit(LOG_ERR
, errno
, "setsockopt IP_HDRINCL %u", bool);
39 if (setsockopt(igmp_socket
, IPPROTO_IP
, IP_MULTICAST_TTL
,
40 (char *)&ttl
, sizeof(ttl
)) < 0)
41 logit(LOG_ERR
, errno
, "setsockopt IP_MULTICAST_TTL %u", ttl
);
45 void k_set_loop(int l
)
50 if (setsockopt(igmp_socket
, IPPROTO_IP
, IP_MULTICAST_LOOP
,
51 (char *)&loop
, sizeof(loop
)) < 0)
52 logit(LOG_ERR
, errno
, "setsockopt IP_MULTICAST_LOOP %u", loop
);
56 void k_set_if(u_int32_t ifa
)
61 if (setsockopt(igmp_socket
, IPPROTO_IP
, IP_MULTICAST_IF
,
62 (char *)&adr
, sizeof(adr
)) < 0)
63 logit(LOG_ERR
, errno
, "setsockopt IP_MULTICAST_IF %s",
68 void k_join(u_int32_t grp
, u_int32_t ifa
)
72 mreq
.imr_multiaddr
.s_addr
= grp
;
73 mreq
.imr_interface
.s_addr
= ifa
;
75 if (setsockopt(igmp_socket
, IPPROTO_IP
, IP_ADD_MEMBERSHIP
,
76 (char *)&mreq
, sizeof(mreq
)) < 0)
77 logit(LOG_WARNING
, errno
, "can't join group %s on interface %s",
83 void k_leave(u_int32_t grp
, u_int32_t ifa
)
87 mreq
.imr_multiaddr
.s_addr
= grp
;
88 mreq
.imr_interface
.s_addr
= ifa
;
90 if (setsockopt(igmp_socket
, IPPROTO_IP
, IP_DROP_MEMBERSHIP
,
91 (char *)&mreq
, sizeof(mreq
)) < 0)
92 logit(LOG_WARNING
, errno
, "can't leave group %s on interface %s",
98 void k_init_dvmrp(void)
101 if (setsockopt(igmp_socket
, IPPROTO_IP
, MRT_INIT
,
102 (char *)NULL
, 0) < 0)
106 if (setsockopt(igmp_socket
, IPPROTO_IP
, MRT_INIT
,
107 (char *)&v
, sizeof(int)) < 0)
109 logit(LOG_ERR
, errno
, "can't enable Multicast routing in kernel");
113 void k_stop_dvmrp(void)
115 if (setsockopt(igmp_socket
, IPPROTO_IP
, MRT_DONE
,
116 (char *)NULL
, 0) < 0)
117 logit(LOG_WARNING
, errno
, "can't disable Multicast routing in kernel");
121 void k_add_vif(vifi_t vifi
, struct uvif
*v
)
126 vc
.vifc_flags
= v
->uv_flags
& VIFF_KERNEL_FLAGS
;
127 vc
.vifc_threshold
= v
->uv_threshold
;
128 vc
.vifc_rate_limit
= v
->uv_rate_limit
;
129 vc
.vifc_lcl_addr
.s_addr
= v
->uv_lcl_addr
;
130 vc
.vifc_rmt_addr
.s_addr
= v
->uv_rmt_addr
;
132 if (setsockopt(igmp_socket
, IPPROTO_IP
, MRT_ADD_VIF
,
133 (char *)&vc
, sizeof(vc
)) < 0)
134 logit(LOG_ERR
, errno
, "setsockopt MRT_ADD_VIF");
138 void k_del_vif(vifi_t vifi
)
140 if (setsockopt(igmp_socket
, IPPROTO_IP
, MRT_DEL_VIF
,
141 (char *)&vifi
, sizeof(vifi
)) < 0)
142 logit(LOG_ERR
, errno
, "setsockopt MRT_DEL_VIF");
147 * Adds a (source, mcastgrp) entry to the kernel
149 void k_add_rg(u_int32_t origin
, struct gtable
*g
)
155 md_log(MD_ADD
, origin
, g
->gt_mcastgrp
);
157 /* copy table values so that setsockopt can process it */
158 mc
.mfcc_origin
.s_addr
= origin
;
160 mc
.mfcc_originmask
.s_addr
= 0xffffffff;
162 mc
.mfcc_mcastgrp
.s_addr
= g
->gt_mcastgrp
;
163 mc
.mfcc_parent
= g
->gt_route
? g
->gt_route
->rt_parent
: NO_VIF
;
164 for (i
= 0; i
< numvifs
; i
++)
165 mc
.mfcc_ttls
[i
] = g
->gt_ttls
[i
];
167 /* write to kernel space */
168 if (setsockopt(igmp_socket
, IPPROTO_IP
, MRT_ADD_MFC
,
169 (char *)&mc
, sizeof(mc
)) < 0) {
171 md_log(MD_ADD_FAIL
, origin
, g
->gt_mcastgrp
);
173 logit(LOG_WARNING
, errno
, "setsockopt MRT_ADD_MFC");
179 * Deletes a (source, mcastgrp) entry from the kernel
181 int k_del_rg(u_int32_t origin
, struct gtable
*g
)
187 md_log(MD_DEL
, origin
, g
->gt_mcastgrp
);
189 /* copy table values so that setsockopt can process it */
190 mc
.mfcc_origin
.s_addr
= origin
;
192 mc
.mfcc_originmask
.s_addr
= 0xffffffff;
194 mc
.mfcc_mcastgrp
.s_addr
= g
->gt_mcastgrp
;
196 /* write to kernel space */
197 if ((retval
= setsockopt(igmp_socket
, IPPROTO_IP
, MRT_DEL_MFC
,
198 (char *)&mc
, sizeof(mc
))) < 0) {
200 md_log(MD_DEL_FAIL
, origin
, g
->gt_mcastgrp
);
202 logit(LOG_WARNING
, errno
, "setsockopt MRT_DEL_MFC");
209 * Get the kernel's idea of what version of mrouted needs to run with it.
211 int k_get_version(void)
217 socklen_t len
= sizeof(vers
);
219 if (getsockopt(igmp_socket
, IPPROTO_IP
, MRT_VERSION
,
220 (char *)&vers
, &len
) < 0)
221 logit(LOG_ERR
, errno
,
222 "getsockopt MRT_VERSION: perhaps your kernel is too old");