diff options
Diffstat (limited to 'sysdep/linux')
-rw-r--r-- | sysdep/linux/netlink/netlink.c | 21 | ||||
-rw-r--r-- | sysdep/linux/sysio.h | 136 |
2 files changed, 92 insertions, 65 deletions
diff --git a/sysdep/linux/netlink/netlink.c b/sysdep/linux/netlink/netlink.c index 03d43884..f45fe159 100644 --- a/sysdep/linux/netlink/netlink.c +++ b/sysdep/linux/netlink/netlink.c @@ -390,7 +390,6 @@ nl_parse_addr(struct nlmsghdr *h) else { ip_addr netmask = ipa_mkmask(ifa.pxlen); - ip_addr xbrd; ifa.prefix = ipa_and(ifa.ip, netmask); ifa.brd = ipa_or(ifa.ip, ipa_not(netmask)); #ifndef IPV6 @@ -398,6 +397,7 @@ nl_parse_addr(struct nlmsghdr *h) ifa.opposite = ipa_opposite(ifa.ip, i->ifa_prefixlen); if ((ifi->flags & IF_BROADCAST) && a[IFA_BROADCAST]) { + ip_addr xbrd; memcpy(&xbrd, RTA_DATA(a[IFA_BROADCAST]), sizeof(xbrd)); ipa_ntoh(xbrd); if (ipa_equal(xbrd, ifa.prefix) || ipa_equal(xbrd, ifa.brd)) @@ -472,6 +472,9 @@ krt_capable(rte *e) switch (a->dest) { case RTD_ROUTER: + if (ipa_has_link_scope(a->gw) && (a->iface == NULL)) + return 0; + case RTD_DEVICE: case RTD_BLACKHOLE: case RTD_UNREACHABLE: @@ -514,6 +517,11 @@ nl_send_route(struct krt_proto *p, rte *e, int new) case RTD_ROUTER: r.r.rtm_type = RTN_UNICAST; nl_add_attr_ipa(&r.h, sizeof(r), RTA_GATEWAY, a->gw); + + /* a->iface != NULL checked in krt_capable() */ + if (ipa_has_link_scope(a->gw)) + nl_add_attr_u32(&r.h, sizeof(r), RTA_OIF, a->iface->index); + break; case RTD_DEVICE: if (!a->iface) @@ -678,6 +686,7 @@ nl_parse_route(struct nlmsghdr *h, int scan) } if (a[RTA_GATEWAY]) { + struct iface *ifa = if_find_by_index(oif); neighbor *ng; ra.dest = RTD_ROUTER; memcpy(&ra.gw, RTA_DATA(a[RTA_GATEWAY]), sizeof(ra.gw)); @@ -696,15 +705,19 @@ nl_parse_route(struct nlmsghdr *h, int scan) } else { - /* standard route */ - ng = neigh_find(&p->p, &ra.gw, 0); + ng = neigh_find2(&p->p, &ra.gw, ifa, 0); if (ng && ng->scope) - ra.iface = ng->iface; + { + if (ng->iface != ifa) + log(L_WARN "KRT: Route with unexpected iface for %I/%d", net->n.prefix, net->n.pxlen); + ra.iface = ng->iface; + } else { log(L_WARN "Kernel told us to use non-neighbor %I for %I/%d", ra.gw, net->n.prefix, net->n.pxlen); return; } + } } else diff --git a/sysdep/linux/sysio.h b/sysdep/linux/sysio.h index 2fa5f0a9..70d35ccf 100644 --- a/sysdep/linux/sysio.h +++ b/sysdep/linux/sysio.h @@ -38,27 +38,33 @@ set_inaddr(struct in_addr *ia, ip_addr a) * ways. Horrible. */ -static inline char *sysio_mcast_setup(sock *s) + +#if defined(CONFIG_LINUX_MC_MREQ) || defined(CONFIG_LINUX_MC_MREQ_BIND) +/* + * Older kernels support only struct mreq which matches interfaces by their + * addresses and thus fails on unnumbered devices. On newer 2.0 kernels + * we can use SO_BINDTODEVICE to circumvent this problem. + */ + +#define MREQ_IFA struct in_addr +#define MREQ_GRP struct ip_mreq +static inline void fill_mreq_ifa(struct in_addr *m, struct iface *ifa, UNUSED ip_addr maddr) { - int zero = 0; + set_inaddr(m, ifa->addr->ip); +} - if (ipa_nonzero(s->daddr)) - { - if ( -#ifdef IP_DEFAULT_MULTICAST_TTL - s->ttl != IP_DEFAULT_MULTICAST_TTL && -#endif - setsockopt(s->fd, SOL_IP, IP_MULTICAST_TTL, &s->ttl, sizeof(s->ttl)) < 0) - return "IP_MULTICAST_TTL"; - if ( -#ifdef IP_DEFAULT_MULTICAST_LOOP - IP_DEFAULT_MULTICAST_LOOP && +static inline void fill_mreq_grp(struct ip_mreq *m, struct iface *ifa, ip_addr maddr) +{ + bzero(m, sizeof(*m)); +#ifdef CONFIG_LINUX_MC_MREQ_BIND + m->imr_interface.s_addr = INADDR_ANY; +#else + set_inaddr(&m->imr_interface, ifa->addr->ip); #endif - setsockopt(s->fd, SOL_IP, IP_MULTICAST_LOOP, &zero, sizeof(zero)) < 0) - return "IP_MULTICAST_LOOP"; - } - return NULL; + set_inaddr(&m->imr_multiaddr, maddr); } +#endif + #ifdef CONFIG_LINUX_MC_MREQN /* @@ -76,70 +82,78 @@ struct ip_mreqn }; #endif -static inline char *sysio_mcast_join(sock *s) +#define MREQ_IFA struct ip_mreqn +#define MREQ_GRP struct ip_mreqn +#define fill_mreq_ifa fill_mreq +#define fill_mreq_grp fill_mreq + +static inline fill_mreq(struct ip_mreqn *m, struct iface *ifa, ip_addr maddr) { - struct ip_mreqn mreq; - char *err; - struct ifreq ifr; - - if (err = sysio_mcast_setup(s)) - return err; - strcpy(ifr.ifr_name, s->iface->name); - if (setsockopt(s->fd, SOL_SOCKET, SO_BINDTODEVICE, &ifr, sizeof(ifr)) < 0) - return "SO_BINDTODEVICE"; - mreq.imr_ifindex = s->iface->index; - set_inaddr(&mreq.imr_address, s->iface->addr->ip); - set_inaddr(&mreq.imr_multiaddr, s->daddr); - /* This defines where should we send _outgoing_ multicasts */ - if (ipa_nonzero(s->daddr) && setsockopt(s->fd, SOL_IP, IP_MULTICAST_IF, &mreq, sizeof(mreq)) < 0) - return "IP_MULTICAST_IF"; - /* And this one sets interface for _receiving_ multicasts from */ - if (ipa_nonzero(s->saddr) && setsockopt(s->fd, SOL_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq)) < 0) - return "IP_ADD_MEMBERSHIP"; - return NULL; + bzero(m, sizeof(*m)); + m->imr_ifindex = ifa->index; + set_inaddr(&m->imr_address, ifa->addr->ip); + set_inaddr(&m->imr_multiaddr, maddr); } #endif -#if defined(CONFIG_LINUX_MC_MREQ) || defined(CONFIG_LINUX_MC_MREQ_BIND) -/* - * Older kernels support only struct mreq which matches interfaces by their - * addresses and thus fails on unnumbered devices. On newer 2.0 kernels - * we can use SO_BINDTODEVICE to circumvent this problem. - */ - -static inline char *sysio_mcast_join(sock *s) +static inline char * +sysio_setup_multicast(sock *s) { - struct in_addr mreq; - struct ip_mreq mreq_add; - char *err; + MREQ_IFA m; + int zero = 0; - if (err = sysio_mcast_setup(s)) - return err; - set_inaddr(&mreq, s->iface->addr->ip); -#ifdef CONFIG_LINUX_MC_MREQ_BIND + if (setsockopt(s->fd, SOL_IP, IP_MULTICAST_LOOP, &zero, sizeof(zero)) < 0) + return "IP_MULTICAST_LOOP"; + + if (setsockopt(s->fd, SOL_IP, IP_MULTICAST_TTL, &s->ttl, sizeof(s->ttl)) < 0) + return "IP_MULTICAST_TTL"; + + /* This defines where should we send _outgoing_ multicasts */ + fill_mreq_ifa(&m, s->iface, IPA_NONE); + if (setsockopt(s->fd, SOL_IP, IP_MULTICAST_IF, &m, sizeof(m)) < 0) + return "IP_MULTICAST_IF"; + +#if defined(CONFIG_LINUX_MC_MREQ_BIND) || defined(CONFIG_LINUX_MC_MREQN) { struct ifreq ifr; strcpy(ifr.ifr_name, s->iface->name); if (setsockopt(s->fd, SOL_SOCKET, SO_BINDTODEVICE, &ifr, sizeof(ifr)) < 0) return "SO_BINDTODEVICE"; - mreq_add.imr_interface.s_addr = INADDR_ANY; } -#else - mreq_add.imr_interface = mreq; #endif - set_inaddr(&mreq_add.imr_multiaddr, s->daddr); - /* This defines where should we send _outgoing_ multicasts */ - if (ipa_nonzero(s->daddr) && setsockopt(s->fd, SOL_IP, IP_MULTICAST_IF, &mreq, sizeof(mreq)) < 0) - return "IP_MULTICAST_IF"; + + return NULL; +} + +static inline char * +sysio_join_group(sock *s, ip_addr maddr) +{ + MREQ_GRP m; + /* And this one sets interface for _receiving_ multicasts from */ - if (ipa_nonzero(s->saddr) && setsockopt(s->fd, SOL_IP, IP_ADD_MEMBERSHIP, &mreq_add, sizeof(mreq_add)) < 0) + fill_mreq_grp(&m, s->iface, maddr); + if (setsockopt(s->fd, SOL_IP, IP_ADD_MEMBERSHIP, &m, sizeof(m)) < 0) return "IP_ADD_MEMBERSHIP"; + + return NULL; +} + +static inline char * +sysio_leave_group(sock *s, ip_addr maddr) +{ + MREQ_GRP m; + + /* And this one sets interface for _receiving_ multicasts from */ + fill_mreq_grp(&m, s->iface, maddr); + if (setsockopt(s->fd, SOL_IP, IP_DROP_MEMBERSHIP, &m, sizeof(m)) < 0) + return "IP_DROP_MEMBERSHIP"; + return NULL; } -#endif #endif + #include <linux/socket.h> #include <linux/tcp.h> |