Skip to content

Commit ed85b56

Browse files
richardcochrandavem330
authored andcommitted
packet: support for TX time stamps on RAW sockets
Enable the SO_TIMESTAMPING socket infrastructure for raw packet sockets. We introduce PACKET_TX_TIMESTAMP for the control message cmsg_type. Similar support for UDP and CAN sockets was added in commit 51f31ca Signed-off-by: Richard Cochran <richard.cochran@omicron.at> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 7d53b80 commit ed85b56

2 files changed

Lines changed: 61 additions & 1 deletion

File tree

‎include/linux/if_packet.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ struct sockaddr_ll {
4747
#define PACKET_TX_RING 13
4848
#define PACKET_LOSS 14
4949
#define PACKET_VNET_HDR 15
50+
#define PACKET_TX_TIMESTAMP 16
5051

5152
struct tpacket_stats {
5253
unsigned int tp_packets;

‎net/packet/af_packet.c‎

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@
8282
#include <linux/mutex.h>
8383
#include <linux/if_vlan.h>
8484
#include <linux/virtio_net.h>
85+
#include <linux/errqueue.h>
8586

8687
#ifdef CONFIG_INET
8788
#include <net/inet_common.h>
@@ -315,6 +316,8 @@ static inline struct packet_sock *pkt_sk(struct sock *sk)
315316

316317
static void packet_sock_destruct(struct sock *sk)
317318
{
319+
skb_queue_purge(&sk->sk_error_queue);
320+
318321
WARN_ON(atomic_read(&sk->sk_rmem_alloc));
319322
WARN_ON(atomic_read(&sk->sk_wmem_alloc));
320323

@@ -483,6 +486,9 @@ static int packet_sendmsg_spkt(struct kiocb *iocb, struct socket *sock,
483486
skb->dev = dev;
484487
skb->priority = sk->sk_priority;
485488
skb->mark = sk->sk_mark;
489+
err = sock_tx_timestamp(msg, sk, skb_tx(skb));
490+
if (err < 0)
491+
goto out_unlock;
486492

487493
dev_queue_xmit(skb);
488494
rcu_read_unlock();
@@ -1188,6 +1194,9 @@ static int packet_snd(struct socket *sock,
11881194
err = skb_copy_datagram_from_iovec(skb, offset, msg->msg_iov, 0, len);
11891195
if (err)
11901196
goto out_free;
1197+
err = sock_tx_timestamp(msg, sk, skb_tx(skb));
1198+
if (err < 0)
1199+
goto out_free;
11911200

11921201
skb->protocol = proto;
11931202
skb->dev = dev;
@@ -1487,6 +1496,51 @@ static int packet_create(struct net *net, struct socket *sock, int protocol,
14871496
return err;
14881497
}
14891498

1499+
static int packet_recv_error(struct sock *sk, struct msghdr *msg, int len)
1500+
{
1501+
struct sock_exterr_skb *serr;
1502+
struct sk_buff *skb, *skb2;
1503+
int copied, err;
1504+
1505+
err = -EAGAIN;
1506+
skb = skb_dequeue(&sk->sk_error_queue);
1507+
if (skb == NULL)
1508+
goto out;
1509+
1510+
copied = skb->len;
1511+
if (copied > len) {
1512+
msg->msg_flags |= MSG_TRUNC;
1513+
copied = len;
1514+
}
1515+
err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
1516+
if (err)
1517+
goto out_free_skb;
1518+
1519+
sock_recv_timestamp(msg, sk, skb);
1520+
1521+
serr = SKB_EXT_ERR(skb);
1522+
put_cmsg(msg, SOL_PACKET, PACKET_TX_TIMESTAMP,
1523+
sizeof(serr->ee), &serr->ee);
1524+
1525+
msg->msg_flags |= MSG_ERRQUEUE;
1526+
err = copied;
1527+
1528+
/* Reset and regenerate socket error */
1529+
spin_lock_bh(&sk->sk_error_queue.lock);
1530+
sk->sk_err = 0;
1531+
if ((skb2 = skb_peek(&sk->sk_error_queue)) != NULL) {
1532+
sk->sk_err = SKB_EXT_ERR(skb2)->ee.ee_errno;
1533+
spin_unlock_bh(&sk->sk_error_queue.lock);
1534+
sk->sk_error_report(sk);
1535+
} else
1536+
spin_unlock_bh(&sk->sk_error_queue.lock);
1537+
1538+
out_free_skb:
1539+
kfree_skb(skb);
1540+
out:
1541+
return err;
1542+
}
1543+
14901544
/*
14911545
* Pull a packet from our receive queue and hand it to the user.
14921546
* If necessary we block.
@@ -1502,7 +1556,7 @@ static int packet_recvmsg(struct kiocb *iocb, struct socket *sock,
15021556
int vnet_hdr_len = 0;
15031557

15041558
err = -EINVAL;
1505-
if (flags & ~(MSG_PEEK|MSG_DONTWAIT|MSG_TRUNC|MSG_CMSG_COMPAT))
1559+
if (flags & ~(MSG_PEEK|MSG_DONTWAIT|MSG_TRUNC|MSG_CMSG_COMPAT|MSG_ERRQUEUE))
15061560
goto out;
15071561

15081562
#if 0
@@ -1511,6 +1565,11 @@ static int packet_recvmsg(struct kiocb *iocb, struct socket *sock,
15111565
return -ENODEV;
15121566
#endif
15131567

1568+
if (flags & MSG_ERRQUEUE) {
1569+
err = packet_recv_error(sk, msg, len);
1570+
goto out;
1571+
}
1572+
15141573
/*
15151574
* Call the generic datagram receiver. This handles all sorts
15161575
* of horrible races and re-entrancy so we can forget about it

0 commit comments

Comments
 (0)