1 /* 2 * Author: Steven Barth <steven at midlink.org> 3 * 4 * Copyright 2015 Deutsche Telekom AG 5 * 6 * Licensed under the Apache License, Version 2.0 (the "License"); 7 * you may not use this file except in compliance with the License. 8 * You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, software 13 * distributed under the License is distributed on an "AS IS" BASIS, 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 * See the License for the specific language governing permissions and 16 * limitations under the License. 17 * 18 */ 19 20 #include <errno.h> 21 #include <stdio.h> 22 #include <string.h> 23 #include <stdlib.h> 24 #include <endian.h> 25 #include <sys/socket.h> 26 #include <sys/ioctl.h> 27 #include <unistd.h> 28 #include <net/if.h> 29 30 #include <arpa/inet.h> 31 #include <netinet/in.h> 32 #include <netinet/ip.h> 33 #include <netinet/ip6.h> 34 35 #include <linux/mroute.h> 36 #include <linux/mroute6.h> 37 38 #include <libubox/uloop.h> 39 40 #include "omcproxy.h" 41 #include "mrib.h" 42 43 struct mrib_route { 44 struct list_head head; 45 struct in6_addr group; 46 struct in6_addr source; 47 omgp_time_t valid_until; 48 }; 49 50 struct mrib_iface { 51 int ifindex; 52 struct list_head users; 53 struct list_head routes; 54 struct list_head queriers; 55 struct uloop_timeout timer; 56 }; 57 58 /* we can't use cpu_to_be32 outside a function */ 59 #if __BYTE_ORDER == __BIG_ENDIAN 60 static uint32_t ipv4_rtr_alert = 0x94040000; 61 #else 62 static uint32_t ipv4_rtr_alert = 0x00000494; 63 #endif 64 65 static struct { 66 struct ip6_hbh hdr; 67 struct ip6_opt_router rt; 68 uint8_t pad[2]; 69 } ipv6_rtr_alert = { 70 .hdr = {0, 0}, 71 .rt = {IP6OPT_ROUTER_ALERT, 2, {0, IP6_ALERT_MLD}}, 72 .pad = {0, 0} 73 }; 74 75 static struct mrib_iface mifs[MAXMIFS] = {}; 76 static struct uloop_fd mrt_fd = { .fd = -1 }; 77 static struct uloop_fd mrt6_fd = { .fd = -1 }; 78 79 80 // Unmap IPv4 address from IPv6 81 static inline void mrib_unmap(struct in_addr *addr4, const struct in6_addr *addr6) 82 { 83 addr4->s_addr = addr6->s6_addr32[3]; 84 } 85 86 // Add / delete multicast route 87 static int mrib_set(const struct in6_addr *group, const struct in6_addr *source, 88 struct mrib_iface *iface, mrib_filter dest, bool del) 89 { 90 int status = 0; 91 size_t mifid = iface - mifs; 92 if (IN6_IS_ADDR_V4MAPPED(group)) { 93 struct mfcctl ctl = { .mfcc_parent = mifid }; 94 mrib_unmap(&ctl.mfcc_origin, source); 95 mrib_unmap(&ctl.mfcc_mcastgrp, group); 96 97 if(!del) 98 for (size_t i = 0; i < MAXMIFS; ++i) 99 if (dest & (1 << i)) 100 ctl.mfcc_ttls[i] = 1; 101 102 if (setsockopt(mrt_fd.fd, IPPROTO_IP, 103 (del) ? MRT_DEL_MFC : MRT_ADD_MFC, 104 &ctl, sizeof(ctl))) 105 status = -errno; 106 } else { 107 struct mf6cctl ctl = { 108 .mf6cc_origin = {AF_INET6, 0, 0, *source, 0}, 109 .mf6cc_mcastgrp = {AF_INET6, 0, 0, *group, 0}, 110 .mf6cc_parent = mifid, 111 }; 112 113 if(!del) 114 for (size_t i = 0; i < MAXMIFS; ++i) 115 if (dest & (1 << i)) 116 IF_SET(i, &ctl.mf6cc_ifset); 117 118 if (setsockopt(mrt6_fd.fd, IPPROTO_IPV6, 119 (del) ? MRT6_DEL_MFC : MRT6_ADD_MFC, 120 &ctl, sizeof(ctl))) 121 status = -errno; 122 } 123 124 char groupbuf[INET6_ADDRSTRLEN], sourcebuf[INET6_ADDRSTRLEN]; 125 inet_ntop(AF_INET6, group, groupbuf, sizeof(groupbuf)); 126 inet_ntop(AF_INET6, source, sourcebuf, sizeof(sourcebuf)); 127 if(del) { 128 L_DEBUG("%s: deleting MFC-entry for %s from %s%%%d: %s", 129 __FUNCTION__, groupbuf, sourcebuf, iface->ifindex, strerror(-status)); 130 } else { 131 int ifbuf_len = 0; 132 char ifbuf[256] = {0}; 133 for (size_t i = 0; i < MAXMIFS; ++i) 134 if (dest & (1 << i)) 135 ifbuf_len += snprintf(&ifbuf[ifbuf_len], sizeof(ifbuf) - ifbuf_len, " %d", mifs[i].ifindex); 136 137 138 L_DEBUG("%s: setting MFC-entry for %s from %s%%%d to%s: %s", 139 __FUNCTION__, groupbuf, sourcebuf, iface->ifindex, ifbuf, strerror(-status)); 140 } 141 142 return status; 143 } 144 145 146 // We have no way of knowing when a source disappears, so we delete multicast routes from time to time 147 static void mrib_clean(struct uloop_timeout *t) 148 { 149 struct mrib_iface *iface = container_of(t, struct mrib_iface, timer); 150 omgp_time_t now = omgp_time(); 151 uloop_timeout_cancel(t); 152 153 struct mrib_route *c, *n; 154 list_for_each_entry_safe(c, n, &iface->routes, head) { 155 if (c->valid_until <= now || (list_empty(&iface->users) && list_empty(&iface->queriers))) { 156 mrib_set(&c->group, &c->source, iface, 0, 1); 157 list_del(&c->head); 158 free(c); 159 } else { 160 uloop_timeout_set(t, c->valid_until - now); 161 break; 162 } 163 } 164 } 165 166 167 // Find MIFID by ifindex 168 static size_t mrib_find(int ifindex) 169 { 170 size_t i = 0; 171 while (i < MAXMIFS && mifs[i].ifindex != ifindex) 172 ++i; 173 return i; 174 } 175 176 // Ask all users for the outgoing interfaces of a multicast route 177 static mrib_filter mrib_route_filter(struct mrib_iface *iface, 178 const struct in6_addr *group, const struct in6_addr *source) 179 { 180 mrib_filter filter = 0; 181 struct mrib_user *user; 182 list_for_each_entry(user, &iface->users, head) 183 if (user->cb_newsource) 184 user->cb_newsource(user, group, source, &filter); 185 186 return filter; 187 } 188 189 // Notify all users of a new multicast source 190 static void mrib_notify_newsource(struct mrib_iface *iface, 191 const struct in6_addr *group, const struct in6_addr *source) 192 { 193 mrib_filter filter = mrib_route_filter(iface, group, source); 194 195 char groupbuf[INET6_ADDRSTRLEN], sourcebuf[INET6_ADDRSTRLEN]; 196 inet_ntop(AF_INET6, group, groupbuf, sizeof(groupbuf)); 197 inet_ntop(AF_INET6, source, sourcebuf, sizeof(sourcebuf)); 198 L_DEBUG("%s: detected new multicast source %s for %s on %d", 199 __FUNCTION__, sourcebuf, groupbuf, iface->ifindex); 200 201 struct mrib_route *route = malloc(sizeof(*route)); 202 if (route) { 203 route->group = *group; 204 route->source = *source; 205 route->valid_until = omgp_time() + MRIB_DEFAULT_LIFETIME * OMGP_TIME_PER_SECOND; 206 207 if (list_empty(&iface->routes)) 208 uloop_timeout_set(&iface->timer, MRIB_DEFAULT_LIFETIME * OMGP_TIME_PER_SECOND); 209 210 list_add_tail(&route->head, &iface->routes); 211 mrib_set(group, source, iface, filter, 0); 212 } 213 } 214 215 // Calculate IGMP-checksum 216 static uint16_t igmp_checksum(const uint16_t *buf, size_t len) 217 { 218 int32_t sum = 0; 219 220 while (len > 1) { 221 sum += *buf++; 222 sum = (sum + (sum >> 16)) & 0xffff; 223 len -= 2; 224 } 225 226 if (len == 1) { 227 sum += *((uint8_t*)buf); 228 sum += (sum + (sum >> 16)) & 0xffff; 229 } 230 231 return ~sum; 232 } 233 234 // Receive and handle MRT event 235 static void mrib_receive_mrt(struct uloop_fd *fd, __unused unsigned flags) 236 { 237 uint8_t buf[9216], cbuf[CMSG_SPACE(sizeof(struct in_pktinfo))]; 238 char addrbuf[INET_ADDRSTRLEN]; 239 struct sockaddr_in from; 240 241 while (true) { 242 struct iovec iov = {buf, sizeof(buf)}; 243 struct msghdr hdr = { 244 .msg_name = (void*)&from, 245 .msg_namelen = sizeof(from), 246 .msg_iov = &iov, 247 .msg_iovlen = 1, 248 .msg_control = cbuf, 249 .msg_controllen = sizeof(cbuf) 250 }; 251 252 ssize_t len = recvmsg(fd->fd, &hdr, MSG_DONTWAIT); 253 if (len < 0 && errno == EAGAIN) 254 break; 255 256 struct iphdr *iph = iov.iov_base; 257 if (len < (ssize_t)sizeof(*iph)) 258 continue; 259 260 if (iph->protocol == 0) { 261 // Pseudo IP/IGMP-packet from kernel MC-API 262 struct igmpmsg *msg = iov.iov_base; 263 struct mrib_iface *iface = NULL; 264 if (msg->im_vif < MAXMIFS) 265 iface = &mifs[msg->im_vif]; 266 267 if (!iface) { 268 L_WARN("MRT kernel-message for unknown MIF %i", msg->im_vif); 269 continue; 270 } 271 272 if (msg->im_msgtype != IGMPMSG_NOCACHE) { 273 L_WARN("Unknown MRT kernel-message %i on interface %d", 274 msg->im_msgtype, iface->ifindex); 275 continue; 276 } 277 278 struct in6_addr dst = IN6ADDR_ANY_INIT; 279 struct in6_addr src = IN6ADDR_ANY_INIT; 280 dst.s6_addr32[2] = cpu_to_be32(0xffff); 281 dst.s6_addr32[3] = msg->im_dst.s_addr; 282 src.s6_addr32[2] = cpu_to_be32(0xffff); 283 src.s6_addr32[3] = msg->im_src.s_addr; 284 285 mrib_notify_newsource(iface, &dst, &src); 286 } else { 287 // IGMP packet 288 if ((len -= iph->ihl * 4) < 0) 289 continue; 290 291 int ifindex = 0; 292 for (struct cmsghdr *ch = CMSG_FIRSTHDR(&hdr); ch != NULL; ch = CMSG_NXTHDR(&hdr, ch)) { 293 if (ch->cmsg_level == IPPROTO_IP && ch->cmsg_type == IP_PKTINFO) { 294 struct in_pktinfo *info = (struct in_pktinfo*)CMSG_DATA(ch); 295 ifindex = info->ipi_ifindex; 296 } 297 } 298 299 if (ifindex == 0) 300 continue; 301 302 inet_ntop(AF_INET, &from.sin_addr, addrbuf, sizeof(addrbuf)); 303 struct igmphdr *igmp = (struct igmphdr*)&buf[iph->ihl * 4]; 304 305 uint16_t checksum = igmp->csum; 306 igmp->csum = 0; 307 308 if (iph->ttl != 1 || len < (ssize_t)sizeof(*igmp) || 309 checksum != igmp_checksum((uint16_t*)igmp, len)) { 310 L_WARN("%s: ignoring invalid IGMP-message of type %x from %s on %d", 311 __FUNCTION__, igmp->type, addrbuf, ifindex); 312 continue; 313 } 314 315 uint32_t *opts = (uint32_t*)&iph[1]; 316 bool alert = (void*)&opts[1] <= (void*)igmp && *opts == ipv4_rtr_alert; 317 if (!alert && (igmp->type != IGMP_HOST_MEMBERSHIP_QUERY || 318 (size_t)len > sizeof(*igmp))) { 319 L_WARN("%s: ignoring invalid IGMP-message of type %x from %s on %d", 320 __FUNCTION__, igmp->type, addrbuf, ifindex); 321 continue; 322 } 323 324 ssize_t mifid = mrib_find(ifindex); 325 if (mifid < MAXMIFS) { 326 struct mrib_querier *q; 327 list_for_each_entry(q, &mifs[mifid].queriers, head) 328 if (q->cb_igmp) 329 q->cb_igmp(q, igmp, len, &from); 330 } 331 } 332 } 333 } 334 335 // Receive and handle MRT6 event 336 static void mrib_receive_mrt6(struct uloop_fd *fd, __unused unsigned flags) 337 { 338 uint8_t buf[9216], cbuf[128]; 339 char addrbuf[INET6_ADDRSTRLEN]; 340 struct sockaddr_in6 from; 341 342 while (true) { 343 struct iovec iov = {buf, sizeof(buf)}; 344 struct msghdr hdr = { 345 .msg_name = (void*)&from, 346 .msg_namelen = sizeof(from), 347 .msg_iov = &iov, 348 .msg_iovlen = 1, 349 .msg_control = cbuf, 350 .msg_controllen = sizeof(cbuf) 351 }; 352 353 ssize_t len = recvmsg(fd->fd, &hdr, MSG_DONTWAIT); 354 if (len < 0 && errno == EAGAIN) 355 break; 356 357 struct mld_hdr *mld = iov.iov_base; 358 if (len < (ssize_t)sizeof(*mld)) 359 continue; 360 361 if (mld->mld_icmp6_hdr.icmp6_type == 0) { 362 // Pseudo ICMPv6/MLD-packet from kernel MC-API 363 struct mrt6msg *msg = iov.iov_base; 364 struct mrib_iface *iface = NULL; 365 if (msg->im6_mif < MAXMIFS) 366 iface = &mifs[msg->im6_mif]; 367 368 if (!iface) { 369 L_WARN("MRT6 kernel-message for unknown MIF %i", msg->im6_mif); 370 continue; 371 } 372 373 if (msg->im6_msgtype != MRT6MSG_NOCACHE) { 374 L_WARN("Unknown MRT6 kernel-message %i on interface %d", 375 msg->im6_msgtype, iface->ifindex); 376 continue; 377 } 378 379 mrib_notify_newsource(iface, &msg->im6_dst, &msg->im6_src); 380 } else { 381 int hlim = 0, ifindex = from.sin6_scope_id; 382 bool alert = false; 383 for (struct cmsghdr *ch = CMSG_FIRSTHDR(&hdr); ch != NULL; ch = CMSG_NXTHDR(&hdr, ch)) { 384 if (ch->cmsg_level == IPPROTO_IPV6 && ch->cmsg_type == IPV6_HOPLIMIT) 385 memcpy(&hlim, CMSG_DATA(ch), sizeof(hlim)); 386 else if (ch->cmsg_level == IPPROTO_IPV6 && ch->cmsg_type == IPV6_HOPOPTS && 387 ch->cmsg_len >= CMSG_LEN(sizeof(ipv6_rtr_alert)) && 388 memmem(CMSG_DATA(ch), ch->cmsg_len - CMSG_LEN(0), 389 &ipv6_rtr_alert.rt, sizeof(ipv6_rtr_alert.rt))) 390 alert = true; // FIXME: memmem is wrong 391 } 392 inet_ntop(AF_INET6, &from.sin6_addr, addrbuf, sizeof(addrbuf)); 393 394 if (!IN6_IS_ADDR_LINKLOCAL(&from.sin6_addr) || hlim != 1 || len < 24 || !alert) { 395 L_WARN("mld: ignoring invalid MLD-message of type %d from %s on %d", 396 mld->mld_icmp6_hdr.icmp6_type, addrbuf, ifindex); 397 continue; 398 } 399 400 ssize_t mifid = mrib_find(from.sin6_scope_id); 401 if (mifid < MAXMIFS) { 402 struct mrib_querier *q; 403 list_for_each_entry(q, &mifs[mifid].queriers, head) 404 if (q->cb_mld) 405 q->cb_mld(q, mld, len, &from); 406 } 407 } 408 } 409 } 410 411 // Send an IGMP-packet 412 int mrib_send_igmp(struct mrib_querier *q, struct igmpv3_query *igmp, size_t len, 413 const struct sockaddr_in *dest) 414 { 415 uint8_t cbuf[CMSG_SPACE(sizeof(struct in_pktinfo))] = {0}; 416 struct iovec iov = {igmp, len}; 417 struct msghdr msg = { 418 .msg_name = (void*)dest, 419 .msg_namelen = sizeof(*dest), 420 .msg_iov = &iov, 421 .msg_iovlen = 1, 422 .msg_control = cbuf, 423 .msg_controllen = sizeof(cbuf) 424 }; 425 426 igmp->csum = 0; 427 igmp->csum = igmp_checksum((uint16_t*)igmp, len); 428 429 // Set control data (define destination interface) 430 struct cmsghdr *chdr = CMSG_FIRSTHDR(&msg); 431 chdr->cmsg_level = IPPROTO_IP; 432 chdr->cmsg_type = IP_PKTINFO; 433 chdr->cmsg_len = CMSG_LEN(sizeof(struct in_pktinfo)); 434 435 struct in_pktinfo *pktinfo = (struct in_pktinfo*)CMSG_DATA(chdr); 436 pktinfo->ipi_addr.s_addr = 0; 437 pktinfo->ipi_ifindex = q->iface->ifindex; 438 if (mrib_igmp_source(q, &pktinfo->ipi_spec_dst)) 439 return -errno; 440 441 ssize_t s = sendmsg(mrt_fd.fd, &msg, MSG_DONTWAIT); 442 return (s < 0) ? -errno : (s < (ssize_t)len) ? -EMSGSIZE : 0; 443 } 444 445 // Send an IGMP-packet 446 int mrib_send_mld(struct mrib_querier *q, struct mld_hdr *mld, size_t len, 447 const struct sockaddr_in6 *dest) 448 { 449 uint8_t cbuf[CMSG_SPACE(sizeof(struct in6_pktinfo))] = {0}; 450 struct iovec iov = {mld, len}; 451 struct msghdr msg = { 452 .msg_name = (void*)dest, 453 .msg_namelen = sizeof(*dest), 454 .msg_iov = &iov, 455 .msg_iovlen = 1, 456 .msg_control = cbuf, 457 .msg_controllen = sizeof(cbuf) 458 }; 459 460 // Set control data (define destination interface) 461 struct cmsghdr *chdr = CMSG_FIRSTHDR(&msg); 462 chdr->cmsg_level = IPPROTO_IPV6; 463 chdr->cmsg_type = IPV6_PKTINFO; 464 chdr->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo)); 465 466 struct in6_pktinfo *pktinfo = (struct in6_pktinfo*)CMSG_DATA(chdr); 467 pktinfo->ipi6_ifindex = q->iface->ifindex; 468 if (mrib_mld_source(q, &pktinfo->ipi6_addr)) 469 return -errno; 470 471 ssize_t s = sendmsg(mrt6_fd.fd, &msg, MSG_DONTWAIT); 472 return (s < 0) ? -errno : (s < (ssize_t)len) ? -EMSGSIZE : 0; 473 } 474 475 // Initialize MRIB 476 static int mrib_init(void) 477 { 478 int fd; 479 int val; 480 481 if ((fd = socket(AF_INET, SOCK_RAW, IPPROTO_IGMP)) < 0) 482 goto err; 483 484 val = 1; 485 if (setsockopt(fd, IPPROTO_IP, MRT_INIT, &val, sizeof(val))) 486 goto err; 487 488 if (setsockopt(fd, IPPROTO_IP, IP_PKTINFO, &val, sizeof(val))) 489 goto err; 490 491 // Configure IP header fields 492 if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_TTL, &val, sizeof(val))) 493 goto err; 494 495 val = 0xc0; 496 if (setsockopt(fd, IPPROTO_IP, IP_TOS, &val, sizeof(val))) 497 goto err; 498 499 val = 0; 500 if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_LOOP, &val, sizeof(val))) 501 goto err; 502 503 // Set router-alert option 504 if (setsockopt(fd, IPPROTO_IP, IP_OPTIONS, &ipv4_rtr_alert, sizeof(ipv4_rtr_alert))) 505 goto err; 506 507 mrt_fd.fd = fd; 508 509 510 if ((fd = socket(AF_INET6, SOCK_RAW, IPPROTO_ICMPV6)) < 0) 511 goto err; 512 513 // We need to know the source interface and hop-opts 514 val = 1; 515 if (setsockopt(fd, IPPROTO_IPV6, MRT6_INIT, &val, sizeof(val))) 516 goto err; 517 518 if (setsockopt(fd, IPPROTO_IPV6, IPV6_RECVHOPOPTS, &val, sizeof(val))) 519 goto err; 520 521 if (setsockopt(fd, IPPROTO_IPV6, IPV6_RECVHOPLIMIT, &val, sizeof(val))) 522 goto err; 523 524 // MLD has hoplimit 1 525 if (setsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &val, sizeof(val))) 526 goto err; 527 528 val = 0; 529 if (setsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, &val, sizeof(val))) 530 goto err; 531 532 // Let the kernel compute our checksums 533 val = 2; 534 if (setsockopt(fd, IPPROTO_RAW, IPV6_CHECKSUM, &val, sizeof(val))) 535 goto err; 536 537 // Set hop-by-hop router alert on outgoing 538 if (setsockopt(fd, IPPROTO_IPV6, IPV6_HOPOPTS, &ipv6_rtr_alert, sizeof(ipv6_rtr_alert))) 539 goto err; 540 541 // Set ICMP6 filter 542 struct icmp6_filter flt; 543 ICMP6_FILTER_SETBLOCKALL(&flt); 544 ICMP6_FILTER_SETPASS(ICMPV6_MGM_QUERY, &flt); 545 ICMP6_FILTER_SETPASS(ICMPV6_MGM_REPORT, &flt); 546 ICMP6_FILTER_SETPASS(ICMPV6_MGM_REDUCTION, &flt); 547 ICMP6_FILTER_SETPASS(ICMPV6_MLD2_REPORT, &flt); 548 if (setsockopt(fd, IPPROTO_ICMPV6, ICMP6_FILTER, &flt, sizeof(flt))) 549 goto err; 550 551 mrt6_fd.fd = fd; 552 553 mrt_fd.cb = mrib_receive_mrt; 554 mrt6_fd.cb = mrib_receive_mrt6; 555 556 uloop_fd_add(&mrt_fd, ULOOP_READ | ULOOP_EDGE_TRIGGER); 557 uloop_fd_add(&mrt6_fd, ULOOP_READ | ULOOP_EDGE_TRIGGER); 558 559 fd = -1; 560 errno = 0; 561 562 err: 563 if (fd >= 0) 564 close(fd); 565 return -errno; 566 } 567 568 // Create new interface entry 569 static struct mrib_iface* mrib_get_iface(int ifindex) 570 { 571 if (mrt_fd.fd < 0 && mrib_init() < 0) 572 return NULL; 573 574 size_t mifid = mrib_find(ifindex); 575 if (mifid < MAXMIFS) 576 return &mifs[mifid]; 577 578 errno = EBUSY; 579 if ((mifid = mrib_find(0)) >= MAXMIFS) 580 return NULL; 581 582 struct mrib_iface *iface = &mifs[mifid]; 583 584 struct vifctl ctl = {mifid, VIFF_USE_IFINDEX, 1, 0, { .vifc_lcl_ifindex = ifindex }, {INADDR_ANY}}; 585 if (setsockopt(mrt_fd.fd, IPPROTO_IP, MRT_ADD_VIF, &ctl, sizeof(ctl))) 586 return NULL; 587 588 struct mif6ctl ctl6 = {mifid, 0, 1, ifindex, 0}; 589 if (setsockopt(mrt6_fd.fd, IPPROTO_IPV6, MRT6_ADD_MIF, &ctl6, sizeof(ctl6))) 590 return NULL; 591 592 struct ip_mreqn mreq = {{INADDR_ALLIGMPV3RTRS_GROUP}, {INADDR_ANY}, ifindex}; 593 setsockopt(mrt_fd.fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq)); 594 595 mreq.imr_multiaddr.s_addr = cpu_to_be32(INADDR_ALLRTRS_GROUP); 596 setsockopt(mrt_fd.fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq)); 597 598 struct ipv6_mreq mreq6 = {MLD2_ALL_MCR_INIT, ifindex}; 599 setsockopt(mrt6_fd.fd, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, &mreq6, sizeof(mreq6)); 600 601 mreq6.ipv6mr_multiaddr.s6_addr[15] = 0x02; 602 setsockopt(mrt6_fd.fd, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, &mreq6, sizeof(mreq6)); 603 604 iface->timer.cb = mrib_clean; 605 iface->ifindex = ifindex; 606 INIT_LIST_HEAD(&iface->routes); 607 INIT_LIST_HEAD(&iface->users); 608 INIT_LIST_HEAD(&iface->queriers); 609 return iface; 610 } 611 612 // Remove interfaces if it has no more users 613 static void mrib_clean_iface(struct mrib_iface *iface) 614 { 615 if (list_empty(&iface->users) && list_empty(&iface->queriers)) { 616 const int ifindex = iface->ifindex; 617 iface->ifindex = 0; 618 mrib_clean(&iface->timer); 619 620 size_t mifid = iface - mifs; 621 struct vifctl ctl = {mifid, VIFF_USE_IFINDEX, 1, 0, 622 { .vifc_lcl_ifindex = ifindex }, {INADDR_ANY}}; 623 setsockopt(mrt_fd.fd, IPPROTO_IP, MRT_DEL_VIF, &ctl, sizeof(ctl)); 624 625 struct mif6ctl ctl6 = {mifid, 0, 1, ifindex, 0}; 626 setsockopt(mrt6_fd.fd, IPPROTO_IPV6, MRT6_DEL_MIF, &ctl6, sizeof(ctl6)); 627 628 struct ip_mreqn mreq = {{INADDR_ALLIGMPV3RTRS_GROUP}, {INADDR_ANY}, ifindex}; 629 setsockopt(mrt_fd.fd, IPPROTO_IP, IP_DROP_MEMBERSHIP, &mreq, sizeof(mreq)); 630 631 mreq.imr_multiaddr.s_addr = cpu_to_be32(INADDR_ALLRTRS_GROUP); 632 setsockopt(mrt_fd.fd, IPPROTO_IP, IP_DROP_MEMBERSHIP, &mreq, sizeof(mreq)); 633 634 struct ipv6_mreq mreq6 = {MLD2_ALL_MCR_INIT, ifindex}; 635 setsockopt(mrt6_fd.fd, IPPROTO_IPV6, IPV6_DROP_MEMBERSHIP, &mreq6, sizeof(mreq6)); 636 637 mreq6.ipv6mr_multiaddr.s6_addr[15] = 0x02; 638 setsockopt(mrt6_fd.fd, IPPROTO_IPV6, IPV6_DROP_MEMBERSHIP, &mreq6, sizeof(mreq6)); 639 } 640 } 641 642 // Register a new interface to mrib 643 int mrib_attach_user(struct mrib_user *user, int ifindex, mrib_cb *cb_newsource) 644 { 645 struct mrib_iface *iface = mrib_get_iface(ifindex); 646 if (!iface) 647 return -errno; 648 649 if (user->iface == iface) 650 return -EALREADY; 651 652 list_add(&user->head, &iface->users); 653 user->iface = iface; 654 user->cb_newsource = cb_newsource; 655 return 0; 656 } 657 658 // Deregister an interface from mrib 659 void mrib_detach_user(struct mrib_user *user) 660 { 661 struct mrib_iface *iface = user->iface; 662 if (!iface) 663 return; 664 665 user->iface = NULL; 666 list_del(&user->head); 667 mrib_clean_iface(iface); 668 } 669 670 // Register a querier to mrib 671 int mrib_attach_querier(struct mrib_querier *querier, int ifindex, mrib_igmp_cb *cb_igmp, mrib_mld_cb *cb_mld) 672 { 673 struct mrib_iface *iface = mrib_get_iface(ifindex); 674 if (!iface) 675 return -errno; 676 677 list_add(&querier->head, &iface->queriers); 678 querier->iface = iface; 679 querier->cb_igmp = cb_igmp; 680 querier->cb_mld = cb_mld; 681 return 0; 682 } 683 684 // Deregister a querier from mrib 685 void mrib_detach_querier(struct mrib_querier *querier) 686 { 687 struct mrib_iface *iface = querier->iface; 688 if (!iface) 689 return; 690 691 querier->iface = NULL; 692 list_del(&querier->head); 693 mrib_clean_iface(iface); 694 } 695 696 uint8_t prefix_contains(const struct in6_addr *p, uint8_t plen, const struct in6_addr *addr) 697 { 698 int blen = plen >> 3; 699 if(blen && memcmp(p, addr, blen)) 700 return 0; 701 702 int rem = plen & 0x07; 703 if(rem && ((p->s6_addr[blen] ^ addr->s6_addr[blen]) >> (8 - rem))) 704 return 0; 705 706 return 1; 707 } 708 709 // Flush state for a multicast route 710 int mrib_flush(struct mrib_user *user, const struct in6_addr *group, uint8_t group_plen, const struct in6_addr *source) 711 { 712 struct mrib_iface *iface = user->iface; 713 if (!iface) 714 return -ENOENT; 715 716 bool found = false; 717 struct mrib_route *route, *n; 718 list_for_each_entry_safe(route, n, &iface->routes, head) { 719 if (prefix_contains(group, group_plen, &route->group) && 720 (!source || IN6_ARE_ADDR_EQUAL(&route->source, source))) { 721 route->valid_until = 0; 722 list_del(&route->head); 723 list_add(&route->head, &iface->routes); 724 found = true; 725 } 726 } 727 728 if (found) 729 mrib_clean(&iface->timer); 730 731 return (found) ? 0 : -ENOENT; 732 } 733 734 // Update the outgoing interfaces of the known routes of a group 735 int mrib_refresh(struct mrib_user *user, const struct in6_addr *group, uint8_t group_plen) 736 { 737 struct mrib_iface *iface = user->iface; 738 if (!iface) 739 return -ENOENT; 740 741 bool found = false; 742 struct mrib_route *route; 743 list_for_each_entry(route, &iface->routes, head) { 744 if (!prefix_contains(group, group_plen, &route->group)) 745 continue; 746 747 mrib_set(&route->group, &route->source, iface, 748 mrib_route_filter(iface, &route->group, &route->source), 0); 749 found = true; 750 } 751 752 return (found) ? 0 : -ENOENT; 753 } 754 755 // Add an interface to the filter 756 int mrib_filter_add(mrib_filter *filter, struct mrib_user *user) 757 { 758 struct mrib_iface *iface = user->iface; 759 if (!iface) 760 return -ENOENT; 761 762 *filter |= 1 << (iface - mifs); 763 return 0; 764 } 765 766 // Get MLD source address 767 int mrib_mld_source(struct mrib_querier *q, struct in6_addr *source) 768 { 769 struct sockaddr_in6 addr = {AF_INET6, 0, 0, MLD2_ALL_MCR_INIT, q->iface->ifindex}; 770 socklen_t alen = sizeof(addr); 771 int sock = socket(AF_INET6, SOCK_RAW | SOCK_CLOEXEC, IPPROTO_ICMPV6); 772 int ret = 0; 773 774 if (sock < 0 || connect(sock, (struct sockaddr*)&addr, sizeof(addr))) 775 ret = -errno; 776 777 if (ret || getsockname(sock, (struct sockaddr*)&addr, &alen)) { 778 L_WARN("%s: failed to detect local source address on %d", __FUNCTION__, q->iface->ifindex); 779 ret = -errno; 780 } 781 782 close(sock); 783 784 if (ret == 0) 785 *source = addr.sin6_addr; 786 787 return ret; 788 } 789 790 791 // Get IGMP source address 792 int mrib_igmp_source(struct mrib_querier *q, struct in_addr *source) 793 { 794 struct sockaddr_in addr = {AF_INET, 0, {cpu_to_be32(INADDR_ALLHOSTS_GROUP)}, {0}}; 795 socklen_t alen = sizeof(addr); 796 struct ifreq ifr = {.ifr_name = ""}; 797 int sock = socket(AF_INET, SOCK_RAW | SOCK_CLOEXEC, IPPROTO_IGMP); 798 int ret = 0; 799 800 ifr.ifr_ifindex = q->iface->ifindex; 801 802 if (sock < 0 || ioctl(sock, SIOCGIFNAME, &ifr) || 803 setsockopt(sock, SOL_SOCKET, SO_BINDTODEVICE, ifr.ifr_name, strlen(ifr.ifr_name))) 804 ret = -errno; 805 806 807 if (ret || connect(sock, (struct sockaddr*)&addr, sizeof(addr))) 808 ret = -errno; 809 810 if (ret || getsockname(sock, (struct sockaddr*)&addr, &alen)) { 811 L_WARN("%s: failed to detect local source address on %d", __FUNCTION__, q->iface->ifindex); 812 ret = -errno; 813 } 814 815 close(sock); 816 817 if (ret == 0) 818 *source = addr.sin_addr; 819 820 return ret; 821 } 822
This page was automatically generated by LXR 0.3.1. • OpenWrt