2011-11-06 15:16:19 +08:00
|
|
|
/**
|
|
|
|
* @file udp.c
|
|
|
|
* @note Copyright (C) 2011 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 <netinet/in.h>
|
|
|
|
#include <stdio.h>
|
2013-01-09 08:21:27 +08:00
|
|
|
#include <stdlib.h>
|
2011-11-06 15:16:19 +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-13 00:45:42 +08:00
|
|
|
#include "config.h"
|
2013-01-09 08:21:27 +08:00
|
|
|
#include "contain.h"
|
2011-12-18 15:21:47 +08:00
|
|
|
#include "print.h"
|
2012-03-17 21:53:10 +08:00
|
|
|
#include "sk.h"
|
2013-01-09 08:21:27 +08:00
|
|
|
#include "ether.h"
|
2012-03-13 15:28:37 +08:00
|
|
|
#include "transport_private.h"
|
2011-11-06 15:16:19 +08:00
|
|
|
#include "udp.h"
|
|
|
|
|
|
|
|
#define EVENT_PORT 319
|
|
|
|
#define GENERAL_PORT 320
|
2012-04-05 23:18:49 +08:00
|
|
|
#define PTP_PRIMARY_MCAST_IPADDR "224.0.1.129"
|
|
|
|
#define PTP_PDELAY_MCAST_IPADDR "224.0.0.107"
|
2011-11-06 15:16:19 +08:00
|
|
|
|
2013-01-09 08:21:27 +08:00
|
|
|
struct udp {
|
|
|
|
struct transport t;
|
2014-04-22 22:00:59 +08:00
|
|
|
struct address ip;
|
|
|
|
struct address mac;
|
2013-01-09 08:21:27 +08:00
|
|
|
};
|
|
|
|
|
2011-11-21 01:31:33 +08:00
|
|
|
static int mcast_bind(int fd, int index)
|
|
|
|
{
|
|
|
|
int err;
|
|
|
|
struct ip_mreqn req;
|
|
|
|
memset(&req, 0, sizeof(req));
|
|
|
|
req.imr_ifindex = index;
|
|
|
|
err = setsockopt(fd, IPPROTO_IP, IP_MULTICAST_IF, &req, sizeof(req));
|
|
|
|
if (err) {
|
2011-12-18 15:21:47 +08:00
|
|
|
pr_err("setsockopt IP_MULTICAST_IF failed: %m");
|
2011-11-21 01:31:33 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-06-25 21:06:33 +08:00
|
|
|
static int mcast_join(int fd, int index, const struct sockaddr_in *sa)
|
2011-11-06 15:16:19 +08:00
|
|
|
{
|
2012-01-02 02:44:49 +08:00
|
|
|
int err, off = 0;
|
2011-12-30 17:19:21 +08:00
|
|
|
struct ip_mreqn req;
|
|
|
|
|
|
|
|
memset(&req, 0, sizeof(req));
|
|
|
|
memcpy(&req.imr_multiaddr, &sa->sin_addr, sizeof(struct in_addr));
|
|
|
|
req.imr_ifindex = index;
|
|
|
|
err = setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &req, sizeof(req));
|
2011-11-06 15:16:19 +08:00
|
|
|
if (err) {
|
2011-12-30 17:19:21 +08:00
|
|
|
pr_err("setsockopt IP_ADD_MEMBERSHIP failed: %m");
|
2012-01-02 02:44:49 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
err = setsockopt(fd, IPPROTO_IP, IP_MULTICAST_LOOP, &off, sizeof(off));
|
|
|
|
if (err) {
|
|
|
|
pr_err("setsockopt IP_MULTICAST_LOOP failed: %m");
|
2011-11-06 15:16:19 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-03-13 15:28:37 +08:00
|
|
|
static int udp_close(struct transport *t, struct fdarray *fda)
|
2011-11-06 15:16:19 +08:00
|
|
|
{
|
|
|
|
close(fda->fd[0]);
|
|
|
|
close(fda->fd[1]);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-08-13 00:45:42 +08:00
|
|
|
static int open_socket(const char *name, struct in_addr mc_addr[2], short port,
|
|
|
|
int ttl)
|
2011-11-06 15:16:19 +08:00
|
|
|
{
|
|
|
|
struct sockaddr_in addr;
|
|
|
|
int fd, index, on = 1;
|
|
|
|
|
2012-11-26 00:45:37 +08:00
|
|
|
memset(&addr, 0, sizeof(addr));
|
2011-11-06 15:16:19 +08:00
|
|
|
addr.sin_family = AF_INET;
|
|
|
|
addr.sin_addr.s_addr = htonl(INADDR_ANY);
|
|
|
|
addr.sin_port = htons(port);
|
|
|
|
|
|
|
|
fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
|
|
|
|
if (fd < 0) {
|
2011-12-18 15:21:47 +08:00
|
|
|
pr_err("socket failed: %m");
|
2011-11-06 15:16:19 +08:00
|
|
|
goto no_socket;
|
|
|
|
}
|
2012-03-17 21:53:10 +08:00
|
|
|
index = sk_interface_index(fd, name);
|
2011-11-06 15:16:19 +08:00
|
|
|
if (index < 0)
|
|
|
|
goto no_option;
|
|
|
|
|
|
|
|
if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on))) {
|
2011-12-18 15:21:47 +08:00
|
|
|
pr_err("setsockopt SO_REUSEADDR failed: %m");
|
2011-11-06 15:16:19 +08:00
|
|
|
goto no_option;
|
|
|
|
}
|
|
|
|
if (bind(fd, (struct sockaddr *) &addr, sizeof(addr))) {
|
2011-12-18 15:21:47 +08:00
|
|
|
pr_err("bind failed: %m");
|
2011-11-06 15:16:19 +08:00
|
|
|
goto no_option;
|
|
|
|
}
|
2012-01-07 14:56:57 +08:00
|
|
|
if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, name, strlen(name))) {
|
|
|
|
pr_err("setsockopt SO_BINDTODEVICE failed: %m");
|
|
|
|
goto no_option;
|
|
|
|
}
|
2015-08-13 00:45:42 +08:00
|
|
|
if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_TTL, &ttl, sizeof(ttl))) {
|
|
|
|
pr_err("setsockopt IP_MULTICAST_TTL failed: %m");
|
|
|
|
goto no_option;
|
|
|
|
}
|
2012-04-05 23:18:49 +08:00
|
|
|
addr.sin_addr = mc_addr[0];
|
2018-06-25 21:06:33 +08:00
|
|
|
if (mcast_join(fd, index, &addr)) {
|
2012-04-05 23:18:49 +08:00
|
|
|
pr_err("mcast_join failed");
|
|
|
|
goto no_option;
|
|
|
|
}
|
|
|
|
addr.sin_addr = mc_addr[1];
|
2018-06-25 21:06:33 +08:00
|
|
|
if (mcast_join(fd, index, &addr)) {
|
2011-12-18 15:21:47 +08:00
|
|
|
pr_err("mcast_join failed");
|
2011-11-06 15:16:19 +08:00
|
|
|
goto no_option;
|
|
|
|
}
|
2011-11-21 01:31:33 +08:00
|
|
|
if (mcast_bind(fd, index)) {
|
|
|
|
goto no_option;
|
|
|
|
}
|
2011-11-06 15:16:19 +08:00
|
|
|
return fd;
|
|
|
|
no_option:
|
|
|
|
close(fd);
|
|
|
|
no_socket:
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2012-04-05 23:18:49 +08:00
|
|
|
enum { MC_PRIMARY, MC_PDELAY };
|
|
|
|
|
|
|
|
static struct in_addr mcast_addr[2];
|
2011-11-06 15:16:19 +08:00
|
|
|
|
2017-10-09 22:31:47 +08:00
|
|
|
static int udp_open(struct transport *t, struct interface *iface,
|
|
|
|
struct fdarray *fda, enum timestamp_type ts_type)
|
2011-11-06 15:16:19 +08:00
|
|
|
{
|
2013-01-09 08:21:27 +08:00
|
|
|
struct udp *udp = container_of(t, struct udp, t);
|
2016-07-12 19:52:48 +08:00
|
|
|
uint8_t event_dscp, general_dscp;
|
2015-08-13 00:45:42 +08:00
|
|
|
int efd, gfd, ttl;
|
2017-10-09 22:31:47 +08:00
|
|
|
char *name = iface->name;
|
2011-11-06 15:16:19 +08:00
|
|
|
|
2015-08-13 00:45:42 +08:00
|
|
|
ttl = config_get_int(t->cfg, name, "udp_ttl");
|
2014-04-22 22:00:59 +08:00
|
|
|
udp->mac.len = 0;
|
|
|
|
sk_interface_macaddr(name, &udp->mac);
|
2013-01-09 08:21:27 +08:00
|
|
|
|
2014-04-22 22:00:59 +08:00
|
|
|
udp->ip.len = 0;
|
|
|
|
sk_interface_addr(name, AF_INET, &udp->ip);
|
2013-01-09 08:21:27 +08:00
|
|
|
|
2012-04-05 23:18:49 +08:00
|
|
|
if (!inet_aton(PTP_PRIMARY_MCAST_IPADDR, &mcast_addr[MC_PRIMARY]))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (!inet_aton(PTP_PDELAY_MCAST_IPADDR, &mcast_addr[MC_PDELAY]))
|
2011-11-06 15:16:19 +08:00
|
|
|
return -1;
|
|
|
|
|
2015-08-13 00:45:42 +08:00
|
|
|
efd = open_socket(name, mcast_addr, EVENT_PORT, ttl);
|
2011-11-06 15:16:19 +08:00
|
|
|
if (efd < 0)
|
|
|
|
goto no_event;
|
|
|
|
|
2015-08-13 00:45:42 +08:00
|
|
|
gfd = open_socket(name, mcast_addr, GENERAL_PORT, ttl);
|
2011-11-06 15:16:19 +08:00
|
|
|
if (gfd < 0)
|
|
|
|
goto no_general;
|
|
|
|
|
2017-10-09 22:31:47 +08:00
|
|
|
if (sk_timestamping_init(efd, iface->ts_label, ts_type, TRANS_UDP_IPV4))
|
2011-11-06 15:16:19 +08:00
|
|
|
goto no_timestamping;
|
|
|
|
|
2013-08-29 16:54:55 +08:00
|
|
|
if (sk_general_init(gfd))
|
|
|
|
goto no_timestamping;
|
|
|
|
|
2016-07-12 19:52:48 +08:00
|
|
|
event_dscp = config_get_int(t->cfg, NULL, "dscp_event");
|
|
|
|
general_dscp = config_get_int(t->cfg, NULL, "dscp_general");
|
|
|
|
|
2018-10-24 21:07:06 +08:00
|
|
|
if (event_dscp && sk_set_priority(efd, AF_INET, event_dscp)) {
|
2016-07-12 19:52:48 +08:00
|
|
|
pr_warning("Failed to set event DSCP priority.");
|
|
|
|
}
|
2018-10-24 21:07:06 +08:00
|
|
|
if (general_dscp && sk_set_priority(gfd, AF_INET, general_dscp)) {
|
2016-07-12 19:52:48 +08:00
|
|
|
pr_warning("Failed to set general DSCP priority.");
|
|
|
|
}
|
|
|
|
|
2011-11-06 15:16:19 +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;
|
|
|
|
}
|
|
|
|
|
2012-03-13 15:28:37 +08:00
|
|
|
static int udp_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)
|
2011-11-06 15:16:19 +08:00
|
|
|
{
|
2014-04-22 22:00:59 +08:00
|
|
|
return sk_receive(fd, buf, buflen, addr, hwts, 0);
|
2011-11-06 15:16:19 +08:00
|
|
|
}
|
|
|
|
|
2018-03-17 14:22:35 +08:00
|
|
|
static int udp_send(struct transport *t, struct fdarray *fda,
|
|
|
|
enum transport_event event, int peer, void *buf, int len,
|
|
|
|
struct address *addr, struct hw_timestamp *hwts)
|
2011-11-06 15:16:19 +08:00
|
|
|
{
|
2014-04-22 22:00:59 +08:00
|
|
|
struct address addr_buf;
|
2011-11-06 15:16:19 +08:00
|
|
|
unsigned char junk[1600];
|
2018-03-18 00:30:04 +08:00
|
|
|
ssize_t cnt;
|
|
|
|
int fd = -1;
|
|
|
|
|
|
|
|
switch (event) {
|
|
|
|
case TRANS_GENERAL:
|
|
|
|
fd = fda->fd[FD_GENERAL];
|
|
|
|
break;
|
|
|
|
case TRANS_EVENT:
|
|
|
|
case TRANS_ONESTEP:
|
|
|
|
case TRANS_P2P1STEP:
|
|
|
|
case TRANS_DEFER_EVENT:
|
|
|
|
fd = fda->fd[FD_EVENT];
|
|
|
|
break;
|
|
|
|
}
|
2011-11-06 15:16:19 +08:00
|
|
|
|
2014-04-22 22:00:59 +08:00
|
|
|
if (!addr) {
|
|
|
|
memset(&addr_buf, 0, sizeof(addr_buf));
|
|
|
|
addr_buf.sin.sin_family = AF_INET;
|
|
|
|
addr_buf.sin.sin_addr = peer ? mcast_addr[MC_PDELAY] :
|
|
|
|
mcast_addr[MC_PRIMARY];
|
|
|
|
addr_buf.len = sizeof(addr_buf.sin);
|
|
|
|
addr = &addr_buf;
|
|
|
|
}
|
2011-11-06 15:16:19 +08:00
|
|
|
|
2015-08-29 16:31:14 +08:00
|
|
|
addr->sin.sin_port = htons(event ? EVENT_PORT : GENERAL_PORT);
|
|
|
|
|
2012-11-30 17:11:36 +08:00
|
|
|
/*
|
|
|
|
* Extend the payload by two, for UDP checksum correction.
|
|
|
|
* This is not really part of the standard, but it is the way
|
|
|
|
* that the phyter works.
|
|
|
|
*/
|
|
|
|
if (event == TRANS_ONESTEP)
|
|
|
|
len += 2;
|
|
|
|
|
2014-04-22 22:00:59 +08:00
|
|
|
cnt = sendto(fd, buf, len, 0, &addr->sa, sizeof(addr->sin));
|
2011-12-13 12:28:54 +08:00
|
|
|
if (cnt < 1) {
|
2011-12-18 15:21:47 +08:00
|
|
|
pr_err("sendto failed: %m");
|
2011-11-06 15:16:19 +08:00
|
|
|
return cnt;
|
2011-12-13 12:28:54 +08:00
|
|
|
}
|
2011-11-06 15:16:19 +08:00
|
|
|
/*
|
|
|
|
* 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;
|
2011-11-06 15:16:19 +08:00
|
|
|
}
|
|
|
|
|
2012-03-17 21:54:09 +08:00
|
|
|
static void udp_release(struct transport *t)
|
|
|
|
{
|
2013-01-09 08:21:27 +08:00
|
|
|
struct udp *udp = container_of(t, struct udp, t);
|
|
|
|
free(udp);
|
2012-03-17 21:54:09 +08:00
|
|
|
}
|
|
|
|
|
2014-02-08 00:26:31 +08:00
|
|
|
static int udp_physical_addr(struct transport *t, uint8_t *addr)
|
2013-01-09 08:21:27 +08:00
|
|
|
{
|
|
|
|
struct udp *udp = container_of(t, struct udp, t);
|
2014-04-22 22:00:59 +08:00
|
|
|
int len = 0;
|
|
|
|
|
|
|
|
if (udp->mac.len) {
|
|
|
|
len = MAC_LEN;
|
2015-08-29 16:28:51 +08:00
|
|
|
memcpy(addr, udp->mac.sll.sll_addr, len);
|
2014-04-22 22:00:59 +08:00
|
|
|
}
|
|
|
|
return len;
|
2013-01-09 08:21:27 +08:00
|
|
|
}
|
|
|
|
|
2014-02-08 00:26:31 +08:00
|
|
|
static int udp_protocol_addr(struct transport *t, uint8_t *addr)
|
2013-01-09 08:21:27 +08:00
|
|
|
{
|
|
|
|
struct udp *udp = container_of(t, struct udp, t);
|
2014-04-22 22:00:59 +08:00
|
|
|
int len = 0;
|
|
|
|
|
|
|
|
if (udp->ip.len) {
|
|
|
|
len = sizeof(udp->ip.sin.sin_addr.s_addr);
|
|
|
|
memcpy(addr, &udp->ip.sin.sin_addr.s_addr, len);
|
|
|
|
}
|
|
|
|
return len;
|
2013-01-09 08:21:27 +08:00
|
|
|
}
|
2012-03-13 15:28:37 +08:00
|
|
|
|
|
|
|
struct transport *udp_transport_create(void)
|
|
|
|
{
|
2013-01-09 08:21:27 +08:00
|
|
|
struct udp *udp = calloc(1, sizeof(*udp));
|
|
|
|
if (!udp)
|
|
|
|
return NULL;
|
|
|
|
udp->t.close = udp_close;
|
|
|
|
udp->t.open = udp_open;
|
|
|
|
udp->t.recv = udp_recv;
|
|
|
|
udp->t.send = udp_send;
|
|
|
|
udp->t.release = udp_release;
|
|
|
|
udp->t.physical_addr = udp_physical_addr;
|
|
|
|
udp->t.protocol_addr = udp_protocol_addr;
|
|
|
|
return &udp->t;
|
2012-03-13 15:28:37 +08:00
|
|
|
}
|