2012-04-20 22:30:01 +08:00
|
|
|
/**
|
|
|
|
* @file udp6.c
|
|
|
|
* @note Copyright (C) 2012 Richard Cochran <richardcochran@gmail.com>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <net/if.h>
|
|
|
|
#include <netdb.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <stdio.h>
|
2012-11-26 01:01:12 +08:00
|
|
|
#include <stdlib.h>
|
2012-04-20 22:30:01 +08:00
|
|
|
#include <string.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2014-04-22 22:00:59 +08:00
|
|
|
#include "address.h"
|
2015-08-16 00:53:43 +08:00
|
|
|
#include "config.h"
|
2012-11-26 01:01:12 +08:00
|
|
|
#include "contain.h"
|
2012-04-20 22:30:01 +08:00
|
|
|
#include "print.h"
|
|
|
|
#include "sk.h"
|
2013-01-09 08:21:28 +08:00
|
|
|
#include "ether.h"
|
2012-04-20 22:30:01 +08:00
|
|
|
#include "transport_private.h"
|
|
|
|
#include "udp6.h"
|
|
|
|
|
|
|
|
#define EVENT_PORT 319
|
|
|
|
#define GENERAL_PORT 320
|
|
|
|
#define PTP_PRIMARY_MCAST_IP6ADDR "FF0E:0:0:0:0:0:0:181"
|
|
|
|
#define PTP_PDELAY_MCAST_IP6ADDR "FF02:0:0:0:0:0:0:6B"
|
|
|
|
|
2012-11-26 01:01:12 +08:00
|
|
|
struct udp6 {
|
|
|
|
struct transport t;
|
|
|
|
int index;
|
2014-04-22 22:00:59 +08:00
|
|
|
struct address ip;
|
|
|
|
struct address mac;
|
2012-11-26 01:01:12 +08:00
|
|
|
};
|
|
|
|
|
2012-11-26 02:24:27 +08:00
|
|
|
static int is_link_local(struct in6_addr *addr)
|
|
|
|
{
|
|
|
|
return addr->s6_addr[1] == 0x02 ? 1 : 0;
|
|
|
|
}
|
|
|
|
|
2012-04-20 22:30:01 +08:00
|
|
|
static int mc_bind(int fd, int index)
|
|
|
|
{
|
|
|
|
int err;
|
|
|
|
struct ipv6_mreq req;
|
|
|
|
memset(&req, 0, sizeof(req));
|
|
|
|
req.ipv6mr_interface = index;
|
|
|
|
err = setsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_IF, &req, sizeof(req));
|
|
|
|
if (err) {
|
|
|
|
pr_err("setsockopt IPV6_MULTICAST_IF failed: %m");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int mc_join(int fd, int index, const struct sockaddr *grp, socklen_t grplen)
|
|
|
|
{
|
|
|
|
int err, off = 0;
|
|
|
|
struct ipv6_mreq req;
|
|
|
|
struct sockaddr_in6 *sa = (struct sockaddr_in6 *) grp;
|
|
|
|
|
|
|
|
memset(&req, 0, sizeof(req));
|
|
|
|
memcpy(&req.ipv6mr_multiaddr, &sa->sin6_addr, sizeof(struct in6_addr));
|
|
|
|
req.ipv6mr_interface = index;
|
|
|
|
err = setsockopt(fd, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, &req, sizeof(req));
|
|
|
|
if (err) {
|
|
|
|
pr_err("setsockopt IPV6_ADD_MEMBERSHIP failed: %m");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
err = setsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, &off, sizeof(off));
|
|
|
|
if (err) {
|
|
|
|
pr_err("setsockopt IPV6_MULTICAST_LOOP failed: %m");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int udp6_close(struct transport *t, struct fdarray *fda)
|
|
|
|
{
|
|
|
|
close(fda->fd[0]);
|
|
|
|
close(fda->fd[1]);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-02-08 00:52:03 +08:00
|
|
|
static int open_socket_ipv6(const char *name, struct in6_addr mc_addr[2], short port,
|
2015-08-31 20:02:46 +08:00
|
|
|
int *interface_index, int hop_limit)
|
2012-04-20 22:30:01 +08:00
|
|
|
{
|
|
|
|
struct sockaddr_in6 addr;
|
|
|
|
int fd, index, on = 1;
|
|
|
|
|
2012-11-26 00:45:37 +08:00
|
|
|
memset(&addr, 0, sizeof(addr));
|
2012-04-20 22:30:01 +08:00
|
|
|
addr.sin6_family = AF_INET6;
|
|
|
|
addr.sin6_addr = in6addr_any;
|
|
|
|
addr.sin6_port = htons(port);
|
|
|
|
|
|
|
|
fd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_UDP);
|
|
|
|
if (fd < 0) {
|
|
|
|
pr_err("socket failed: %m");
|
|
|
|
goto no_socket;
|
|
|
|
}
|
|
|
|
index = sk_interface_index(fd, name);
|
|
|
|
if (index < 0)
|
|
|
|
goto no_option;
|
|
|
|
|
2012-11-26 01:01:12 +08:00
|
|
|
*interface_index = index;
|
|
|
|
|
2012-04-20 22:30:01 +08:00
|
|
|
if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on))) {
|
|
|
|
pr_err("setsockopt SO_REUSEADDR failed: %m");
|
|
|
|
goto no_option;
|
|
|
|
}
|
|
|
|
if (bind(fd, (struct sockaddr *) &addr, sizeof(addr))) {
|
|
|
|
pr_err("bind failed: %m");
|
|
|
|
goto no_option;
|
|
|
|
}
|
|
|
|
if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, name, strlen(name))) {
|
|
|
|
pr_err("setsockopt SO_BINDTODEVICE failed: %m");
|
|
|
|
goto no_option;
|
|
|
|
}
|
2015-08-31 20:02:46 +08:00
|
|
|
if (setsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &hop_limit,
|
|
|
|
sizeof(hop_limit))) {
|
|
|
|
pr_err("setsockopt IPV6_MULTICAST_HOPS failed: %m");
|
|
|
|
goto no_option;
|
|
|
|
}
|
2012-04-20 22:30:01 +08:00
|
|
|
addr.sin6_addr = mc_addr[0];
|
|
|
|
if (mc_join(fd, index, (struct sockaddr *) &addr, sizeof(addr))) {
|
|
|
|
pr_err("mcast_join failed");
|
|
|
|
goto no_option;
|
|
|
|
}
|
|
|
|
addr.sin6_addr = mc_addr[1];
|
|
|
|
if (mc_join(fd, index, (struct sockaddr *) &addr, sizeof(addr))) {
|
|
|
|
pr_err("mcast_join failed");
|
|
|
|
goto no_option;
|
|
|
|
}
|
|
|
|
if (mc_bind(fd, index)) {
|
|
|
|
goto no_option;
|
|
|
|
}
|
|
|
|
return fd;
|
|
|
|
no_option:
|
|
|
|
close(fd);
|
|
|
|
no_socket:
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
enum { MC_PRIMARY, MC_PDELAY };
|
|
|
|
|
|
|
|
static struct in6_addr mc6_addr[2];
|
|
|
|
|
2014-02-08 00:52:03 +08:00
|
|
|
static int udp6_open(struct transport *t, const char *name, struct fdarray *fda,
|
2012-04-20 22:30:01 +08:00
|
|
|
enum timestamp_type ts_type)
|
|
|
|
{
|
2012-11-26 01:01:12 +08:00
|
|
|
struct udp6 *udp6 = container_of(t, struct udp6, t);
|
2015-08-31 20:02:46 +08:00
|
|
|
int efd, gfd, hop_limit;
|
2012-04-20 22:30:01 +08:00
|
|
|
|
2015-08-31 20:02:46 +08:00
|
|
|
hop_limit = config_get_int(t->cfg, name, "udp_ttl");
|
2014-04-22 22:00:59 +08:00
|
|
|
udp6->mac.len = 0;
|
|
|
|
sk_interface_macaddr(name, &udp6->mac);
|
2013-01-09 08:21:28 +08:00
|
|
|
|
2014-04-22 22:00:59 +08:00
|
|
|
udp6->ip.len = 0;
|
|
|
|
sk_interface_addr(name, AF_INET6, &udp6->ip);
|
2013-01-09 08:21:28 +08:00
|
|
|
|
2012-04-20 22:30:01 +08:00
|
|
|
if (1 != inet_pton(AF_INET6, PTP_PRIMARY_MCAST_IP6ADDR, &mc6_addr[MC_PRIMARY]))
|
|
|
|
return -1;
|
|
|
|
|
2015-08-16 00:53:43 +08:00
|
|
|
mc6_addr[MC_PRIMARY].s6_addr[1] = config_get_int(t->cfg, name, "udp6_scope");
|
2012-11-26 03:30:14 +08:00
|
|
|
|
2012-04-20 22:30:01 +08:00
|
|
|
if (1 != inet_pton(AF_INET6, PTP_PDELAY_MCAST_IP6ADDR, &mc6_addr[MC_PDELAY]))
|
|
|
|
return -1;
|
|
|
|
|
2015-08-31 20:02:46 +08:00
|
|
|
efd = open_socket_ipv6(name, mc6_addr, EVENT_PORT, &udp6->index, hop_limit);
|
2012-04-20 22:30:01 +08:00
|
|
|
if (efd < 0)
|
|
|
|
goto no_event;
|
|
|
|
|
2015-08-31 20:02:46 +08:00
|
|
|
gfd = open_socket_ipv6(name, mc6_addr, GENERAL_PORT, &udp6->index, hop_limit);
|
2012-04-20 22:30:01 +08:00
|
|
|
if (gfd < 0)
|
|
|
|
goto no_general;
|
|
|
|
|
2012-10-25 21:19:31 +08:00
|
|
|
if (sk_timestamping_init(efd, name, ts_type, TRANS_UDP_IPV6))
|
2012-04-20 22:30:01 +08:00
|
|
|
goto no_timestamping;
|
|
|
|
|
2013-08-29 16:54:55 +08:00
|
|
|
if (sk_general_init(gfd))
|
|
|
|
goto no_timestamping;
|
|
|
|
|
2012-04-20 22:30:01 +08:00
|
|
|
fda->fd[FD_EVENT] = efd;
|
|
|
|
fda->fd[FD_GENERAL] = gfd;
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
no_timestamping:
|
|
|
|
close(gfd);
|
|
|
|
no_general:
|
|
|
|
close(efd);
|
|
|
|
no_event:
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int udp6_recv(struct transport *t, int fd, void *buf, int buflen,
|
2014-04-22 22:00:59 +08:00
|
|
|
struct address *addr, struct hw_timestamp *hwts)
|
2012-04-20 22:30:01 +08:00
|
|
|
{
|
2014-04-22 22:00:59 +08:00
|
|
|
return sk_receive(fd, buf, buflen, addr, hwts, 0);
|
2012-04-20 22:30:01 +08:00
|
|
|
}
|
|
|
|
|
2014-04-22 22:00:59 +08:00
|
|
|
static int udp6_send(struct transport *t, struct fdarray *fda, int event,
|
|
|
|
int peer, void *buf, int len, struct address *addr,
|
|
|
|
struct hw_timestamp *hwts)
|
2012-04-20 22:30:01 +08:00
|
|
|
{
|
2012-11-26 02:24:27 +08:00
|
|
|
struct udp6 *udp6 = container_of(t, struct udp6, t);
|
2012-04-20 22:30:01 +08:00
|
|
|
ssize_t cnt;
|
|
|
|
int fd = event ? fda->fd[FD_EVENT] : fda->fd[FD_GENERAL];
|
2014-04-22 22:00:59 +08:00
|
|
|
struct address addr_buf;
|
2012-04-20 22:30:01 +08:00
|
|
|
unsigned char junk[1600];
|
|
|
|
|
2014-04-22 22:00:59 +08:00
|
|
|
if (!addr) {
|
|
|
|
memset(&addr_buf, 0, sizeof(addr_buf));
|
|
|
|
addr_buf.sin6.sin6_family = AF_INET6;
|
|
|
|
addr_buf.sin6.sin6_addr = peer ? mc6_addr[MC_PDELAY] :
|
|
|
|
mc6_addr[MC_PRIMARY];
|
|
|
|
addr_buf.sin6.sin6_port = htons(event ? EVENT_PORT :
|
|
|
|
GENERAL_PORT);
|
2012-04-20 22:30:01 +08:00
|
|
|
|
2014-04-22 22:00:59 +08:00
|
|
|
if (is_link_local(&addr_buf.sin6.sin6_addr))
|
|
|
|
addr_buf.sin6.sin6_scope_id = udp6->index;
|
|
|
|
|
|
|
|
addr_buf.len = sizeof(addr_buf.sin6);
|
|
|
|
addr = &addr_buf;
|
2012-11-26 02:24:27 +08:00
|
|
|
}
|
|
|
|
|
2012-04-20 22:30:01 +08:00
|
|
|
len += 2; /* Extend the payload by two, for UDP checksum corrections. */
|
|
|
|
|
2014-04-22 22:00:59 +08:00
|
|
|
cnt = sendto(fd, buf, len, 0, &addr->sa, sizeof(addr->sin6));
|
2012-04-20 22:30:01 +08:00
|
|
|
if (cnt < 1) {
|
|
|
|
pr_err("sendto failed: %m");
|
|
|
|
return cnt;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Get the time stamp right away.
|
|
|
|
*/
|
2014-04-22 22:00:59 +08:00
|
|
|
return event == TRANS_EVENT ? sk_receive(fd, junk, len, NULL, hwts, MSG_ERRQUEUE) : cnt;
|
2012-04-20 22:30:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void udp6_release(struct transport *t)
|
|
|
|
{
|
2012-11-26 01:01:12 +08:00
|
|
|
struct udp6 *udp6 = container_of(t, struct udp6, t);
|
|
|
|
free(udp6);
|
2012-04-20 22:30:01 +08:00
|
|
|
}
|
|
|
|
|
2013-01-09 08:21:28 +08:00
|
|
|
static int udp6_physical_addr(struct transport *t, uint8_t *addr)
|
|
|
|
{
|
|
|
|
struct udp6 *udp6 = container_of(t, struct udp6, t);
|
2014-04-22 22:00:59 +08:00
|
|
|
int len = 0;
|
|
|
|
|
|
|
|
if (udp6->mac.len) {
|
|
|
|
len = MAC_LEN;
|
|
|
|
memcpy(addr, udp6->mac.sa.sa_data, len);
|
|
|
|
}
|
|
|
|
return len;
|
2013-01-09 08:21:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int udp6_protocol_addr(struct transport *t, uint8_t *addr)
|
|
|
|
{
|
|
|
|
struct udp6 *udp6 = container_of(t, struct udp6, t);
|
2014-04-22 22:00:59 +08:00
|
|
|
int len = 0;
|
|
|
|
|
|
|
|
if (udp6->ip.len) {
|
|
|
|
len = sizeof(udp6->ip.sin6.sin6_addr.s6_addr);
|
|
|
|
memcpy(addr, &udp6->ip.sin6.sin6_addr.s6_addr, len);
|
|
|
|
}
|
|
|
|
return len;
|
2013-01-09 08:21:28 +08:00
|
|
|
}
|
|
|
|
|
2012-04-20 22:30:01 +08:00
|
|
|
struct transport *udp6_transport_create(void)
|
|
|
|
{
|
2012-11-26 01:01:12 +08:00
|
|
|
struct udp6 *udp6;
|
|
|
|
udp6 = calloc(1, sizeof(*udp6));
|
|
|
|
if (!udp6)
|
|
|
|
return NULL;
|
|
|
|
udp6->t.close = udp6_close;
|
|
|
|
udp6->t.open = udp6_open;
|
|
|
|
udp6->t.recv = udp6_recv;
|
|
|
|
udp6->t.send = udp6_send;
|
|
|
|
udp6->t.release = udp6_release;
|
2013-01-09 08:21:28 +08:00
|
|
|
udp6->t.physical_addr = udp6_physical_addr;
|
|
|
|
udp6->t.protocol_addr = udp6_protocol_addr;
|
2012-11-26 01:01:12 +08:00
|
|
|
return &udp6->t;
|
2012-04-20 22:30:01 +08:00
|
|
|
}
|