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
|
/*
* $Id: send.c,v 1.34 2010/01/28 13:34:26 psavola Exp $
*
* Authors:
* Pedro Roque <roque@di.fc.ul.pt>
* Lars Fenneberg <lf@elemental.net>
*
* This software is Copyright 1996,1997 by the above mentioned author(s),
* All Rights Reserved.
*
* The license which is distributed with this software in the file COPYRIGHT
* applies to this software. If your distribution is missing this file, you
* may request it from <pekkas@netcore.fi>.
*
*/
#include <config.h>
#include <includes.h>
#include <radvd.h>
/*
* Sends an advertisement for all specified clients of this interface
* (or via broadcast, if there are no restrictions configured).
*
* If a destination address is given, the RA will be sent to the destination
* address only, but only if it was configured.
*
*/
int
send_ra_forall(int sock, struct Interface *iface, struct in6_addr *dest)
{
struct Clients *current;
/* If no list of clients was specified for this interface, we broadcast */
if (iface->ClientList == NULL)
return send_ra(sock, iface, dest);
/* If clients are configured, send the advertisement to all of them via unicast */
for (current = iface->ClientList; current; current = current->next)
{
char address_text[INET6_ADDRSTRLEN];
memset(address_text, 0, sizeof(address_text));
if (get_debuglevel() >= 5)
inet_ntop(AF_INET6, ¤t->Address, address_text, INET6_ADDRSTRLEN);
/* If a non-authorized client sent a solicitation, ignore it (logging later) */
if (dest != NULL && memcmp(dest, ¤t->Address, sizeof(struct in6_addr)) != 0)
continue;
dlog(LOG_DEBUG, 5, "Sending RA to %s", address_text);
send_ra(sock, iface, &(current->Address));
/* If we should only send the RA to a specific address, we are done */
if (dest != NULL)
return 0;
}
if (dest == NULL)
return 0;
/* If we refused a client's solicitation, log it if debugging is high enough */
char address_text[INET6_ADDRSTRLEN];
memset(address_text, 0, sizeof(address_text));
if (get_debuglevel() >= 5)
inet_ntop(AF_INET6, dest, address_text, INET6_ADDRSTRLEN);
dlog(LOG_DEBUG, 5, "Not answering request from %s, not configured", address_text);
return 0;
}
static void
send_ra_inc_len(size_t *len, int add)
{
*len += add;
if(*len >= MSG_SIZE_SEND)
{
flog(LOG_ERR, "Too many prefixes or routes. Exiting.");
exit(1);
}
}
int
send_ra(int sock, struct Interface *iface, struct in6_addr *dest)
{
uint8_t all_hosts_addr[] = {0xff,0x02,0,0,0,0,0,0,0,0,0,0,0,0,0,1};
struct sockaddr_in6 addr;
struct in6_pktinfo *pkt_info;
struct msghdr mhdr;
struct cmsghdr *cmsg;
struct iovec iov;
char __attribute__((aligned(8))) chdr[CMSG_SPACE(sizeof(struct in6_pktinfo))];
struct nd_router_advert *radvert;
struct AdvPrefix *prefix;
struct AdvRoute *route;
struct AdvRDNSS *rdnss;
unsigned char buff[MSG_SIZE_SEND];
size_t len = 0;
ssize_t err;
/* First we need to check that the interface hasn't been removed or deactivated */
if(check_device(sock, iface) < 0) {
if (iface->IgnoreIfMissing) /* a bit more quiet warning message.. */
dlog(LOG_DEBUG, 4, "interface %s does not exist, ignoring the interface", iface->Name);
else {
flog(LOG_WARNING, "interface %s does not exist, ignoring the interface", iface->Name);
}
iface->HasFailed = 1;
/* not really a 'success', but we need to schedule new timers.. */
return 0;
} else {
/* check_device was successful, act if it has failed previously */
if (iface->HasFailed == 1) {
flog(LOG_WARNING, "interface %s seems to have come back up, trying to reinitialize", iface->Name);
iface->HasFailed = 0;
/*
* return -1 so timer_handler() doesn't schedule new timers,
* reload_config() will kick off new timers anyway. This avoids
* timer list corruption.
*/
reload_config();
return -1;
}
}
/* Make sure that we've joined the all-routers multicast group */
if (check_allrouters_membership(sock, iface) < 0)
flog(LOG_WARNING, "problem checking all-routers membership on %s", iface->Name);
dlog(LOG_DEBUG, 3, "sending RA on %s", iface->Name);
if (dest == NULL)
{
struct timeval tv;
dest = (struct in6_addr *)all_hosts_addr;
gettimeofday(&tv, NULL);
iface->last_multicast_sec = tv.tv_sec;
iface->last_multicast_usec = tv.tv_usec;
}
memset((void *)&addr, 0, sizeof(addr));
addr.sin6_family = AF_INET6;
addr.sin6_port = htons(IPPROTO_ICMPV6);
memcpy(&addr.sin6_addr, dest, sizeof(struct in6_addr));
memset(buff, 0, sizeof(buff));
radvert = (struct nd_router_advert *) buff;
radvert->nd_ra_type = ND_ROUTER_ADVERT;
radvert->nd_ra_code = 0;
radvert->nd_ra_cksum = 0;
radvert->nd_ra_curhoplimit = iface->AdvCurHopLimit;
radvert->nd_ra_flags_reserved =
(iface->AdvManagedFlag)?ND_RA_FLAG_MANAGED:0;
radvert->nd_ra_flags_reserved |=
(iface->AdvOtherConfigFlag)?ND_RA_FLAG_OTHER:0;
/* Mobile IPv6 ext */
radvert->nd_ra_flags_reserved |=
(iface->AdvHomeAgentFlag)?ND_RA_FLAG_HOME_AGENT:0;
/* if forwarding is disabled, send zero router lifetime */
radvert->nd_ra_router_lifetime = !check_ip6_forwarding() ? htons(iface->AdvDefaultLifetime) : 0;
radvert->nd_ra_flags_reserved |=
(iface->AdvDefaultPreference << ND_OPT_RI_PRF_SHIFT) & ND_OPT_RI_PRF_MASK;
radvert->nd_ra_reachable = htonl(iface->AdvReachableTime);
radvert->nd_ra_retransmit = htonl(iface->AdvRetransTimer);
len = sizeof(struct nd_router_advert);
prefix = iface->AdvPrefixList;
/*
* add prefix options
*/
while(prefix)
{
if( prefix->enabled )
{
struct nd_opt_prefix_info *pinfo;
pinfo = (struct nd_opt_prefix_info *) (buff + len);
pinfo->nd_opt_pi_type = ND_OPT_PREFIX_INFORMATION;
pinfo->nd_opt_pi_len = 4;
pinfo->nd_opt_pi_prefix_len = prefix->PrefixLen;
pinfo->nd_opt_pi_flags_reserved =
(prefix->AdvOnLinkFlag)?ND_OPT_PI_FLAG_ONLINK:0;
pinfo->nd_opt_pi_flags_reserved |=
(prefix->AdvAutonomousFlag)?ND_OPT_PI_FLAG_AUTO:0;
/* Mobile IPv6 ext */
pinfo->nd_opt_pi_flags_reserved |=
(prefix->AdvRouterAddr)?ND_OPT_PI_FLAG_RADDR:0;
pinfo->nd_opt_pi_valid_time = htonl(prefix->AdvValidLifetime);
pinfo->nd_opt_pi_preferred_time = htonl(prefix->AdvPreferredLifetime);
pinfo->nd_opt_pi_reserved2 = 0;
memcpy(&pinfo->nd_opt_pi_prefix, &prefix->Prefix,
sizeof(struct in6_addr));
send_ra_inc_len(&len, sizeof(*pinfo));
}
prefix = prefix->next;
}
route = iface->AdvRouteList;
/*
* add route options
*/
while(route)
{
struct nd_opt_route_info_local *rinfo;
rinfo = (struct nd_opt_route_info_local *) (buff + len);
rinfo->nd_opt_ri_type = ND_OPT_ROUTE_INFORMATION;
/* XXX: the prefixes are allowed to be sent in smaller chunks as well */
rinfo->nd_opt_ri_len = 3;
rinfo->nd_opt_ri_prefix_len = route->PrefixLen;
rinfo->nd_opt_ri_flags_reserved =
(route->AdvRoutePreference << ND_OPT_RI_PRF_SHIFT) & ND_OPT_RI_PRF_MASK;
rinfo->nd_opt_ri_lifetime = htonl(route->AdvRouteLifetime);
memcpy(&rinfo->nd_opt_ri_prefix, &route->Prefix,
sizeof(struct in6_addr));
send_ra_inc_len(&len, sizeof(*rinfo));
route = route->next;
}
rdnss = iface->AdvRDNSSList;
/*
* add rdnss options
*/
while(rdnss)
{
struct nd_opt_rdnss_info_local *rdnssinfo;
rdnssinfo = (struct nd_opt_rdnss_info_local *) (buff + len);
rdnssinfo->nd_opt_rdnssi_type = ND_OPT_RDNSS_INFORMATION;
rdnssinfo->nd_opt_rdnssi_len = 1 + 2*rdnss->AdvRDNSSNumber;
rdnssinfo->nd_opt_rdnssi_pref_flag_reserved =
((rdnss->AdvRDNSSPreference << ND_OPT_RDNSSI_PREF_SHIFT) & ND_OPT_RDNSSI_PREF_MASK);
rdnssinfo->nd_opt_rdnssi_pref_flag_reserved |=
((rdnss->AdvRDNSSOpenFlag)?ND_OPT_RDNSSI_FLAG_S:0);
rdnssinfo->nd_opt_rdnssi_lifetime = htonl(rdnss->AdvRDNSSLifetime);
memcpy(&rdnssinfo->nd_opt_rdnssi_addr1, &rdnss->AdvRDNSSAddr1,
sizeof(struct in6_addr));
memcpy(&rdnssinfo->nd_opt_rdnssi_addr2, &rdnss->AdvRDNSSAddr2,
sizeof(struct in6_addr));
memcpy(&rdnssinfo->nd_opt_rdnssi_addr3, &rdnss->AdvRDNSSAddr3,
sizeof(struct in6_addr));
send_ra_inc_len(&len, sizeof(*rdnssinfo) - (3-rdnss->AdvRDNSSNumber)*sizeof(struct in6_addr));
rdnss = rdnss->next;
}
/*
* add MTU option
*/
if (iface->AdvLinkMTU != 0) {
struct nd_opt_mtu *mtu;
mtu = (struct nd_opt_mtu *) (buff + len);
mtu->nd_opt_mtu_type = ND_OPT_MTU;
mtu->nd_opt_mtu_len = 1;
mtu->nd_opt_mtu_reserved = 0;
mtu->nd_opt_mtu_mtu = htonl(iface->AdvLinkMTU);
send_ra_inc_len(&len, sizeof(*mtu));
}
/*
* add Source Link-layer Address option
*/
if (iface->AdvSourceLLAddress && iface->if_hwaddr_len != -1)
{
uint8_t *ucp;
unsigned int i;
ucp = (uint8_t *) (buff + len);
*ucp++ = ND_OPT_SOURCE_LINKADDR;
*ucp++ = (uint8_t) ((iface->if_hwaddr_len + 16 + 63) >> 6);
send_ra_inc_len(&len, 2 * sizeof(uint8_t));
i = (iface->if_hwaddr_len + 7) >> 3;
memcpy(buff + len, iface->if_hwaddr, i);
send_ra_inc_len(&len, i);
}
/*
* Mobile IPv6 ext: Advertisement Interval Option to support
* movement detection of mobile nodes
*/
if(iface->AdvIntervalOpt)
{
struct AdvInterval a_ival;
uint32_t ival;
if(iface->MaxRtrAdvInterval < Cautious_MaxRtrAdvInterval){
ival = ((iface->MaxRtrAdvInterval +
Cautious_MaxRtrAdvInterval_Leeway ) * 1000);
}
else {
ival = (iface->MaxRtrAdvInterval * 1000);
}
a_ival.type = ND_OPT_RTR_ADV_INTERVAL;
a_ival.length = 1;
a_ival.reserved = 0;
a_ival.adv_ival = htonl(ival);
memcpy(buff + len, &a_ival, sizeof(a_ival));
send_ra_inc_len(&len, sizeof(a_ival));
}
/*
* Mobile IPv6 ext: Home Agent Information Option to support
* Dynamic Home Agent Address Discovery
*/
if(iface->AdvHomeAgentInfo &&
(iface->AdvMobRtrSupportFlag || iface->HomeAgentPreference != 0 ||
iface->HomeAgentLifetime != iface->AdvDefaultLifetime))
{
struct HomeAgentInfo ha_info;
ha_info.type = ND_OPT_HOME_AGENT_INFO;
ha_info.length = 1;
ha_info.flags_reserved =
(iface->AdvMobRtrSupportFlag)?ND_OPT_HAI_FLAG_SUPPORT_MR:0;
ha_info.preference = htons(iface->HomeAgentPreference);
ha_info.lifetime = htons(iface->HomeAgentLifetime);
memcpy(buff + len, &ha_info, sizeof(ha_info));
send_ra_inc_len(&len, sizeof(ha_info));
}
iov.iov_len = len;
iov.iov_base = (caddr_t) buff;
memset(chdr, 0, sizeof(chdr));
cmsg = (struct cmsghdr *) chdr;
cmsg->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo));
cmsg->cmsg_level = IPPROTO_IPV6;
cmsg->cmsg_type = IPV6_PKTINFO;
pkt_info = (struct in6_pktinfo *)CMSG_DATA(cmsg);
pkt_info->ipi6_ifindex = iface->if_index;
memcpy(&pkt_info->ipi6_addr, &iface->if_addr, sizeof(struct in6_addr));
#ifdef HAVE_SIN6_SCOPE_ID
if (IN6_IS_ADDR_LINKLOCAL(&addr.sin6_addr) ||
IN6_IS_ADDR_MC_LINKLOCAL(&addr.sin6_addr))
addr.sin6_scope_id = iface->if_index;
#endif
memset(&mhdr, 0, sizeof(mhdr));
mhdr.msg_name = (caddr_t)&addr;
mhdr.msg_namelen = sizeof(struct sockaddr_in6);
mhdr.msg_iov = &iov;
mhdr.msg_iovlen = 1;
mhdr.msg_control = (void *) cmsg;
mhdr.msg_controllen = sizeof(chdr);
err = sendmsg(sock, &mhdr, 0);
if (err < 0) {
if (!iface->IgnoreIfMissing || !(errno == EINVAL || errno == ENODEV))
flog(LOG_WARNING, "sendmsg: %s", strerror(errno));
else
dlog(LOG_DEBUG, 3, "sendmsg: %s", strerror(errno));
}
return 0;
}
|