Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id E9208C433F5 for ; Tue, 1 Mar 2022 05:37:42 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232440AbiCAFiU (ORCPT ); Tue, 1 Mar 2022 00:38:20 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35228 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232468AbiCAFiT (ORCPT ); Tue, 1 Mar 2022 00:38:19 -0500 Received: from mx0a-00082601.pphosted.com (mx0b-00082601.pphosted.com [67.231.153.30]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 32AA5286FF for ; Mon, 28 Feb 2022 21:37:37 -0800 (PST) Received: from pps.filterd (m0001303.ppops.net [127.0.0.1]) by m0001303.ppops.net (8.16.1.2/8.16.1.2) with ESMTP id 21SMwSYV007718 for ; Mon, 28 Feb 2022 21:37:37 -0800 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=fb.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : mime-version : content-transfer-encoding : content-type; s=facebook; bh=1xEk1X++uv3kxKRCeq0wSZwgM+EHtEHdbJ1AbZJwfgc=; b=VYkyq4G+kZRaoIIN65TFMIse6dVH+LagHlDGIPEa43l/WTsKfuq1fcQ5WpjzxfjjRLwt Q7ID4U8TrlfLhTUtWkmai8TI9FRXo4ehhdOxmDtnkSz986FiNeOnCVolF46mE6UtxPKV C6yG7oxhL8qLaJ2VcvMfx5kAuwjzDxMxXDI= Received: from maileast.thefacebook.com ([163.114.130.16]) by m0001303.ppops.net (PPS) with ESMTPS id 3eh2jm470c-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128 verify=NOT) for ; Mon, 28 Feb 2022 21:37:36 -0800 Received: from twshared33860.05.ash9.facebook.com (2620:10d:c0a8:1b::d) by mail.thefacebook.com (2620:10d:c0a8:83::5) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2308.21; Mon, 28 Feb 2022 21:37:36 -0800 Received: by devbig005.ftw2.facebook.com (Postfix, from userid 6611) id 214267A89633; Mon, 28 Feb 2022 21:37:28 -0800 (PST) From: Martin KaFai Lau To: , CC: Alexei Starovoitov , Andrii Nakryiko , Daniel Borkmann , David Miller , Eric Dumazet , Jakub Kicinski , , Willem de Bruijn Subject: [PATCH v5 net-next 09/13] net: Get rcv tstamp if needed in nfnetlink_{log, queue}.c Date: Mon, 28 Feb 2022 21:37:28 -0800 Message-ID: <20220301053728.934886-1-kafai@fb.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220301053631.930498-1-kafai@fb.com> References: <20220301053631.930498-1-kafai@fb.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-FB-Internal: Safe Content-Type: text/plain X-Proofpoint-ORIG-GUID: dI9hYCD7VVxagW2hBH1ifVBuy07_iKm_ X-Proofpoint-GUID: dI9hYCD7VVxagW2hBH1ifVBuy07_iKm_ X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.205,Aquarius:18.0.816,Hydra:6.0.425,FMLib:17.11.64.514 definitions=2022-02-28_10,2022-02-26_01,2022-02-23_01 X-Proofpoint-Spam-Details: rule=fb_outbound_notspam policy=fb_outbound score=0 clxscore=1015 malwarescore=0 adultscore=0 impostorscore=0 spamscore=0 suspectscore=0 priorityscore=1501 phishscore=0 mlxscore=0 bulkscore=0 lowpriorityscore=0 mlxlogscore=999 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2201110000 definitions=main-2203010026 X-FB-Internal: deliver Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org If skb has the (rcv) timestamp available, nfnetlink_{log, queue}.c logs/outputs it to the userspace. When the locally generated skb is looping from egress to ingress over a virtual interface (e.g. veth, loopback...), skb->tstamp may have the delivery time before it is known that will be delivered locally and received by another sk. Like handling the delivery time in network tapping, use ktime_get_real() to get the (rcv) timestamp. The earlier added helper skb_tstamp_cond() is used to do this. false is passed to the second 'cond' arg such that doing ktime_get_real() or not only depends on the netstamp_needed_key static key. Signed-off-by: Martin KaFai Lau --- net/netfilter/nfnetlink_log.c | 6 ++++-- net/netfilter/nfnetlink_queue.c | 8 +++++--- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/net/netfilter/nfnetlink_log.c b/net/netfilter/nfnetlink_log.= c index ae9c0756bba5..d97eb280cb2e 100644 --- a/net/netfilter/nfnetlink_log.c +++ b/net/netfilter/nfnetlink_log.c @@ -460,6 +460,7 @@ __build_packet_message(struct nfnl_log_net *log, sk_buff_data_t old_tail =3D inst->skb->tail; struct sock *sk; const unsigned char *hwhdrp; + ktime_t tstamp; =20 nlh =3D nfnl_msg_put(inst->skb, 0, 0, nfnl_msg_type(NFNL_SUBSYS_ULOG, NFULNL_MSG_PACKET), @@ -588,9 +589,10 @@ __build_packet_message(struct nfnl_log_net *log, goto nla_put_failure; } =20 - if (hooknum <=3D NF_INET_FORWARD && skb->tstamp) { + tstamp =3D skb_tstamp_cond(skb, false); + if (hooknum <=3D NF_INET_FORWARD && tstamp) { struct nfulnl_msg_packet_timestamp ts; - struct timespec64 kts =3D ktime_to_timespec64(skb->tstamp); + struct timespec64 kts =3D ktime_to_timespec64(tstamp); ts.sec =3D cpu_to_be64(kts.tv_sec); ts.usec =3D cpu_to_be64(kts.tv_nsec / NSEC_PER_USEC); =20 diff --git a/net/netfilter/nfnetlink_queue.c b/net/netfilter/nfnetlink_qu= eue.c index 8c15978d9258..db9b5357f2ca 100644 --- a/net/netfilter/nfnetlink_queue.c +++ b/net/netfilter/nfnetlink_queue.c @@ -392,6 +392,7 @@ nfqnl_build_packet_message(struct net *net, struct nf= qnl_instance *queue, bool csum_verify; char *secdata =3D NULL; u32 seclen =3D 0; + ktime_t tstamp; =20 size =3D nlmsg_total_size(sizeof(struct nfgenmsg)) + nla_total_size(sizeof(struct nfqnl_msg_packet_hdr)) @@ -407,7 +408,8 @@ nfqnl_build_packet_message(struct net *net, struct nf= qnl_instance *queue, + nla_total_size(sizeof(u_int32_t)) /* skbinfo */ + nla_total_size(sizeof(u_int32_t)); /* cap_len */ =20 - if (entskb->tstamp) + tstamp =3D skb_tstamp_cond(entskb, false); + if (tstamp) size +=3D nla_total_size(sizeof(struct nfqnl_msg_packet_timestamp)); =20 size +=3D nfqnl_get_bridge_size(entry); @@ -582,9 +584,9 @@ nfqnl_build_packet_message(struct net *net, struct nf= qnl_instance *queue, if (nfqnl_put_bridge(entry, skb) < 0) goto nla_put_failure; =20 - if (entry->state.hook <=3D NF_INET_FORWARD && entskb->tstamp) { + if (entry->state.hook <=3D NF_INET_FORWARD && tstamp) { struct nfqnl_msg_packet_timestamp ts; - struct timespec64 kts =3D ktime_to_timespec64(entskb->tstamp); + struct timespec64 kts =3D ktime_to_timespec64(tstamp); =20 ts.sec =3D cpu_to_be64(kts.tv_sec); ts.usec =3D cpu_to_be64(kts.tv_nsec / NSEC_PER_USEC); --=20 2.30.2