diff options
Diffstat (limited to 'sysdep')
-rw-r--r-- | sysdep/bsd/sysio.h | 51 | ||||
-rw-r--r-- | sysdep/linux/sysio.h | 136 | ||||
-rw-r--r-- | sysdep/unix/io.c | 190 |
3 files changed, 234 insertions, 143 deletions
diff --git a/sysdep/bsd/sysio.h b/sysdep/bsd/sysio.h index b0ec456e..80f8f942 100644 --- a/sysdep/bsd/sysio.h +++ b/sysdep/bsd/sysio.h @@ -27,48 +27,55 @@ set_inaddr(struct in_addr * ia, ip_addr a) } static inline char * -sysio_mcast_setup(sock * s) +sysio_setup_multicast(sock *s) { - u8 zero = 0; - u8 one = 1; + struct in_addr m; + u8 zero = 0; + u8 ttl = s->ttl; - if (ipa_nonzero(s->daddr)) { + if (setsockopt(s->fd, IPPROTO_IP, IP_MULTICAST_LOOP, &zero, sizeof(zero)) < 0) + return "IP_MULTICAST_LOOP"; - if (setsockopt(s->fd, IPPROTO_IP, IP_MULTICAST_LOOP, &zero, sizeof(zero)) < 0) - return "IP_MULTICAST_LOOP"; + if (setsockopt(s->fd, IPPROTO_IP, IP_MULTICAST_TTL, &ttl, sizeof(ttl)) < 0) + return "IP_MULTICAST_TTL"; + + /* This defines where should we send _outgoing_ multicasts */ + set_inaddr(&m, s->iface->addr->ip); + if (setsockopt(s->fd, IPPROTO_IP, IP_MULTICAST_IF, &m, sizeof(m)) < 0) + return "IP_MULTICAST_IF"; - if (setsockopt(s->fd, IPPROTO_IP, IP_MULTICAST_TTL, &one, sizeof(one)) < 0) - return "IP_MULTICAST_TTL"; - } return NULL; } static inline char * -sysio_mcast_join(sock * s) +sysio_join_group(sock *s, ip_addr maddr) { - struct in_addr m; struct ip_mreq mreq; - char *err; - - set_inaddr(&m, s->iface->addr->ip ); bzero(&mreq, sizeof(mreq)); set_inaddr(&mreq.imr_interface, s->iface->addr->ip); - set_inaddr(&mreq.imr_multiaddr, s->daddr); + set_inaddr(&mreq.imr_multiaddr, maddr); /* And this one sets interface for _receiving_ multicasts from */ - if (ipa_nonzero(s->daddr) && - setsockopt(s->fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq)) < 0) + if (setsockopt(s->fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq)) < 0) return "IP_ADD_MEMBERSHIP"; + return NULL; +} - /* This defines where should we send _outgoing_ multicasts */ - if (ipa_nonzero(s->daddr) && setsockopt(s->fd, IPPROTO_IP, IP_MULTICAST_IF, &m, sizeof(m)) < 0) - return "IP_MULTICAST_IF"; +static inline char * +sysio_leave_group(sock *s, ip_addr maddr) +{ + struct ip_mreq mreq; - if (err = sysio_mcast_setup(s)) - return err; + bzero(&mreq, sizeof(mreq)); + set_inaddr(&mreq.imr_interface, s->iface->addr->ip); + set_inaddr(&mreq.imr_multiaddr, maddr); + + /* And this one sets interface for _receiving_ multicasts from */ + if (setsockopt(s->fd, IPPROTO_IP, IP_DROP_MEMBERSHIP, &mreq, sizeof(mreq)) < 0) + return "IP_DROP_MEMBERSHIP"; return NULL; } 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> diff --git a/sysdep/unix/io.c b/sysdep/unix/io.c index d9547161..5cc80dcd 100644 --- a/sysdep/unix/io.c +++ b/sysdep/unix/io.c @@ -690,8 +690,7 @@ sk_set_ttl_int(sock *s) { int one = 1; #ifdef IPV6 - if (s->type != SK_UDP_MC && s->type != SK_IP_MC && - setsockopt(s->fd, SOL_IPV6, IPV6_UNICAST_HOPS, &s->ttl, sizeof(s->ttl)) < 0) + if (setsockopt(s->fd, SOL_IPV6, IPV6_UNICAST_HOPS, &s->ttl, sizeof(s->ttl)) < 0) return "IPV6_UNICAST_HOPS"; #else if (setsockopt(s->fd, SOL_IP, IP_TTL, &s->ttl, sizeof(s->ttl)) < 0) @@ -788,6 +787,134 @@ sk_set_md5_auth(sock *s, ip_addr a, char *passwd) return sk_set_md5_auth_int(s, &sa, passwd); } +int +sk_set_broadcast(sock *s, int enable) +{ + if (setsockopt(s->fd, SOL_SOCKET, SO_BROADCAST, &enable, sizeof(enable)) < 0) + log(L_ERR "sk_set_broadcast: SO_BROADCAST: %m"); +} + + +#ifdef IPV6 + +int +sk_setup_multicast(sock *s) +{ + char *err; + int zero = 0; + int index; + + ASSERT(s->iface && s->iface->addr); + + index = s->iface->index; + if (setsockopt(s->fd, SOL_IPV6, IPV6_MULTICAST_HOPS, &s->ttl, sizeof(s->ttl)) < 0) + ERR("IPV6_MULTICAST_HOPS"); + if (setsockopt(s->fd, SOL_IPV6, IPV6_MULTICAST_LOOP, &zero, sizeof(zero)) < 0) + ERR("IPV6_MULTICAST_LOOP"); + if (setsockopt(s->fd, SOL_IPV6, IPV6_MULTICAST_IF, &index, sizeof(index)) < 0) + ERR("IPV6_MULTICAST_IF"); + + return 0; + +bad: + log(L_ERR "sk_setup_multicast: %s: %m", err); + return -1; +} + +int +sk_join_group(sock *s, ip_addr maddr) +{ + struct ipv6_mreq mreq; + + set_inaddr(&mreq.ipv6mr_multiaddr, maddr); + +#ifdef CONFIG_IPV6_GLIBC_20 + mreq.ipv6mr_ifindex = s->iface->index; +#else + mreq.ipv6mr_interface = s->iface->index; +#endif + + /* RFC 2553 says IPV6_JOIN_GROUP */ + if (setsockopt(s->fd, SOL_IPV6, IPV6_ADD_MEMBERSHIP, &mreq, sizeof(mreq)) < 0) + { + log(L_ERR "sk_join_group: IPV6_ADD_MEMBERSHIP: %m"); + return -1; + } + + return 0; +} + +int +sk_leave_group(sock *s, ip_addr maddr) +{ + struct ipv6_mreq mreq; + + set_inaddr(&mreq.ipv6mr_multiaddr, maddr); + +#ifdef CONFIG_IPV6_GLIBC_20 + mreq.ipv6mr_ifindex = s->iface->index; +#else + mreq.ipv6mr_interface = s->iface->index; +#endif + + /* RFC 2553 says IPV6_LEAVE_GROUP */ + if (setsockopt(s->fd, SOL_IPV6, IPV6_DROP_MEMBERSHIP, &mreq, sizeof(mreq)) < 0) + { + log(L_ERR "sk_leave_group: IPV6_DROP_MEMBERSHIP: %m"); + return -1; + } + + return 0; +} + +#else /* IPV4 */ + +int +sk_setup_multicast(sock *s) +{ + char *err; + + ASSERT(s->iface && s->iface->addr); + + if (err = sysio_setup_multicast(s)) + { + log(L_ERR "sk_setup_multicast: %s: %m", err); + return -1; + } + + return 0; +} + +int +sk_join_group(sock *s, ip_addr maddr) +{ + char *err; + + if (err = sysio_join_group(s, maddr)) + { + log(L_ERR "sk_join_group: %s: %m", err); + return -1; + } + + return 0; +} + +int +sk_leave_group(sock *s, ip_addr maddr) +{ + char *err; + + if (err = sysio_leave_group(s, maddr)) + { + log(L_ERR "sk_leave_group: %s: %m", err); + return -1; + } + + return 0; +} + +#endif + static void sk_tcp_connected(sock *s) @@ -861,11 +988,9 @@ sk_open(sock *s) fd = socket(BIRD_PF, SOCK_STREAM, IPPROTO_TCP); break; case SK_UDP: - case SK_UDP_MC: fd = socket(BIRD_PF, SOCK_DGRAM, IPPROTO_UDP); break; case SK_IP: - case SK_IP_MC: fd = socket(BIRD_PF, SOCK_RAW, s->dport); break; case SK_MAGIC: @@ -881,59 +1006,11 @@ sk_open(sock *s) if (err = sk_setup(s)) goto bad; - switch (type) - { - case SK_UDP: - case SK_IP: -#ifndef IPV6 - if (s->iface) /* It's a broadcast socket */ - if (setsockopt(fd, SOL_SOCKET, SO_BROADCAST, &one, sizeof(one)) < 0) - ERR("SO_BROADCAST"); -#endif - break; - case SK_UDP_MC: - case SK_IP_MC: - { -#ifdef IPV6 - /* Fortunately, IPv6 socket interface is recent enough and therefore standardized */ - ASSERT(s->iface && s->iface->addr); - if (ipa_nonzero(s->daddr)) - { - int t = s->iface->index; - int zero = 0; - if (setsockopt(fd, SOL_IPV6, IPV6_MULTICAST_HOPS, &s->ttl, sizeof(s->ttl)) < 0) - ERR("IPV6_MULTICAST_HOPS"); - if (setsockopt(fd, SOL_IPV6, IPV6_MULTICAST_LOOP, &zero, sizeof(zero)) < 0) - ERR("IPV6_MULTICAST_LOOP"); - if (setsockopt(fd, SOL_IPV6, IPV6_MULTICAST_IF, &t, sizeof(t)) < 0) - ERR("IPV6_MULTICAST_IF"); - } - if (has_src) - { - struct ipv6_mreq mreq; - set_inaddr(&mreq.ipv6mr_multiaddr, s->daddr); -#ifdef CONFIG_IPV6_GLIBC_20 - mreq.ipv6mr_ifindex = s->iface->index; -#else - mreq.ipv6mr_interface = s->iface->index; -#endif /* CONFIG_IPV6_GLIBC_20 */ - if (setsockopt(fd, SOL_IPV6, IPV6_ADD_MEMBERSHIP, &mreq, sizeof(mreq)) < 0) - ERR("IPV6_ADD_MEMBERSHIP"); - } -#else /* IPv4 */ - /* With IPv4 there are zillions of different socket interface variants. Ugh. */ - ASSERT(s->iface && s->iface->addr); - if (err = sysio_mcast_join(s)) - goto bad; -#endif /* IPV6 */ - break; - } - } if (has_src) { int port; - if (type == SK_IP || type == SK_IP_MC) + if (type == SK_IP) port = 0; else { @@ -943,12 +1020,7 @@ sk_open(sock *s) } fill_in_sockaddr(&sa, s->saddr, port); fill_in_sockifa(&sa, s->iface); -#ifdef CONFIG_SKIP_MC_BIND - if ((type != SK_UDP_MC) && (type != SK_IP_MC) && - bind(fd, (struct sockaddr *) &sa, sizeof(sa)) < 0) -#else if (bind(fd, (struct sockaddr *) &sa, sizeof(sa)) < 0) -#endif ERR("bind"); } fill_in_sockaddr(&sa, s->daddr, s->dport); @@ -1069,9 +1141,7 @@ sk_maybe_write(sock *s) s->ttx = s->tpos = s->tbuf; return 1; case SK_UDP: - case SK_UDP_MC: case SK_IP: - case SK_IP_MC: { sockaddr sa; |