Skip to content

Commit da92b19

Browse files
richardcochrandavem330
authored andcommitted
net: hold sock reference while processing tx timestamps
The pair of functions, * skb_clone_tx_timestamp() * skb_complete_tx_timestamp() were designed to allow timestamping in PHY devices. The first function, called during the MAC driver's hard_xmit method, identifies PTP protocol packets, clones them, and gives them to the PHY device driver. The PHY driver may hold onto the packet and deliver it at a later time using the second function, which adds the packet to the socket's error queue. As pointed out by Johannes, nothing prevents the socket from disappearing while the cloned packet is sitting in the PHY driver awaiting a timestamp. This patch fixes the issue by taking a reference on the socket for each such packet. In addition, the comments regarding the usage of these function are expanded to highlight the rule that PHY drivers must use skb_complete_tx_timestamp() to release the packet, in order to release the socket reference, too. These functions first appeared in v2.6.36. Reported-by: Johannes Berg <johannes@sipsolutions.net> Signed-off-by: Richard Cochran <richard.cochran@omicron.at> Cc: <stable@vger.kernel.org> Signed-off-by: Eric Dumazet <eric.dumazet@gmail.com> Reviewed-by: Johannes Berg <johannes@sipsolutions.net> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent f7ff198 commit da92b19

3 files changed

Lines changed: 17 additions & 4 deletions

File tree

‎include/linux/phy.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,7 @@ struct phy_driver {
420420

421421
/*
422422
* Requests a Tx timestamp for 'skb'. The phy driver promises
423-
* to deliver it to the socket's error queue as soon as a
423+
* to deliver it using skb_complete_tx_timestamp() as soon as a
424424
* timestamp becomes available. One of the PTP_CLASS_ values
425425
* is passed in 'type'.
426426
*/

‎include/linux/skbuff.h‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2020,8 +2020,13 @@ static inline bool skb_defer_rx_timestamp(struct sk_buff *skb)
20202020
/**
20212021
* skb_complete_tx_timestamp() - deliver cloned skb with tx timestamps
20222022
*
2023+
* PHY drivers may accept clones of transmitted packets for
2024+
* timestamping via their phy_driver.txtstamp method. These drivers
2025+
* must call this function to return the skb back to the stack, with
2026+
* or without a timestamp.
2027+
*
20232028
* @skb: clone of the the original outgoing packet
2024-
* @hwtstamps: hardware time stamps
2029+
* @hwtstamps: hardware time stamps, may be NULL if not available
20252030
*
20262031
*/
20272032
void skb_complete_tx_timestamp(struct sk_buff *skb,

‎net/core/timestamping.c‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,13 @@ void skb_clone_tx_timestamp(struct sk_buff *skb)
5757
case PTP_CLASS_V2_VLAN:
5858
phydev = skb->dev->phydev;
5959
if (likely(phydev->drv->txtstamp)) {
60+
if (!atomic_inc_not_zero(&sk->sk_refcnt))
61+
return;
6062
clone = skb_clone(skb, GFP_ATOMIC);
61-
if (!clone)
63+
if (!clone) {
64+
sock_put(sk);
6265
return;
66+
}
6367
clone->sk = sk;
6468
phydev->drv->txtstamp(phydev, clone, type);
6569
}
@@ -77,8 +81,11 @@ void skb_complete_tx_timestamp(struct sk_buff *skb,
7781
struct sock_exterr_skb *serr;
7882
int err;
7983

80-
if (!hwtstamps)
84+
if (!hwtstamps) {
85+
sock_put(sk);
86+
kfree_skb(skb);
8187
return;
88+
}
8289

8390
*skb_hwtstamps(skb) = *hwtstamps;
8491
serr = SKB_EXT_ERR(skb);
@@ -87,6 +94,7 @@ void skb_complete_tx_timestamp(struct sk_buff *skb,
8794
serr->ee.ee_origin = SO_EE_ORIGIN_TIMESTAMPING;
8895
skb->sk = NULL;
8996
err = sock_queue_err_skb(sk, skb);
97+
sock_put(sk);
9098
if (err)
9199
kfree_skb(skb);
92100
}

0 commit comments

Comments
 (0)