1 /* SPDX-License-Identifier: GPL-2.0-or-later */
5 * This file implements a BPF that recognizes PTP event messages.
7 * Copyright (C) 2010 OMICRON electronics GmbH
10 #ifndef _PTP_CLASSIFY_H_
11 #define _PTP_CLASSIFY_H_
13 #include <asm/unaligned.h>
15 #include <linux/ktime.h>
16 #include <linux/skbuff.h>
17 #include <linux/udp.h>
18 #include <net/checksum.h>
20 #define PTP_CLASS_NONE 0x00 /* not a PTP event message */
21 #define PTP_CLASS_V1 0x01 /* protocol version 1 */
22 #define PTP_CLASS_V2 0x02 /* protocol version 2 */
23 #define PTP_CLASS_VMASK 0x0f /* max protocol version is 15 */
24 #define PTP_CLASS_IPV4 0x10 /* event in an IPV4 UDP packet */
25 #define PTP_CLASS_IPV6 0x20 /* event in an IPV6 UDP packet */
26 #define PTP_CLASS_L2 0x40 /* event in a L2 packet */
27 #define PTP_CLASS_PMASK 0x70 /* mask for the packet type field */
28 #define PTP_CLASS_VLAN 0x80 /* event in a VLAN tagged packet */
30 #define PTP_CLASS_V1_IPV4 (PTP_CLASS_V1 | PTP_CLASS_IPV4)
31 #define PTP_CLASS_V1_IPV6 (PTP_CLASS_V1 | PTP_CLASS_IPV6) /* probably DNE */
32 #define PTP_CLASS_V2_IPV4 (PTP_CLASS_V2 | PTP_CLASS_IPV4)
33 #define PTP_CLASS_V2_IPV6 (PTP_CLASS_V2 | PTP_CLASS_IPV6)
34 #define PTP_CLASS_V2_L2 (PTP_CLASS_V2 | PTP_CLASS_L2)
35 #define PTP_CLASS_V2_VLAN (PTP_CLASS_V2 | PTP_CLASS_VLAN)
36 #define PTP_CLASS_L4 (PTP_CLASS_IPV4 | PTP_CLASS_IPV6)
38 #define PTP_MSGTYPE_SYNC 0x0
39 #define PTP_MSGTYPE_DELAY_REQ 0x1
40 #define PTP_MSGTYPE_PDELAY_REQ 0x2
41 #define PTP_MSGTYPE_PDELAY_RESP 0x3
43 #define PTP_EV_PORT 319
44 #define PTP_GEN_PORT 320
45 #define PTP_GEN_BIT 0x08 /* indicates general message, if set in message type */
47 #define OFF_PTP_SOURCE_UUID 22 /* PTPv1 only */
48 #define OFF_PTP_SEQUENCE_ID 30
50 /* PTP header flag fields */
51 #define PTP_FLAG_TWOSTEP BIT(1)
53 /* Below defines should actually be removed at some point in time. */
57 #define IPV4_HLEN(data) (((struct iphdr *)(data + OFF_IHL))->ihl << 2)
59 struct clock_identity {
63 struct port_identity {
64 struct clock_identity clock_identity;
69 u8 tsmt; /* transportSpecific | messageType */
70 u8 ver; /* reserved | versionPTP */
71 __be16 message_length;
77 struct port_identity source_port_identity;
80 u8 log_message_interval;
83 #if defined(CONFIG_NET_PTP_CLASSIFY)
85 * ptp_classify_raw - classify a PTP packet
88 * Runs a minimal BPF dissector to classify a network packet to
89 * determine the PTP class. In case the skb does not contain any
90 * PTP protocol data, PTP_CLASS_NONE will be returned, otherwise
91 * PTP_CLASS_V1_IPV{4,6}, PTP_CLASS_V2_IPV{4,6} or
92 * PTP_CLASS_V2_{L2,VLAN}, depending on the packet content.
94 unsigned int ptp_classify_raw(const struct sk_buff *skb);
97 * ptp_parse_header - Get pointer to the PTP v2 header
99 * @type: type of the packet (see ptp_classify_raw())
101 * This function takes care of the VLAN, UDP, IPv4 and IPv6 headers. The length
104 * Note, internally skb_mac_header() is used. Make sure that the @skb is
105 * initialized accordingly.
107 * Return: Pointer to the ptp v2 header or NULL if not found
109 struct ptp_header *ptp_parse_header(struct sk_buff *skb, unsigned int type);
112 * ptp_get_msgtype - Extract ptp message type from given header
114 * @type: type of the packet (see ptp_classify_raw())
116 * This function returns the message type for a given ptp header. It takes care
117 * of the different ptp header versions (v1 or v2).
119 * Return: The message type
121 static inline u8 ptp_get_msgtype(const struct ptp_header *hdr,
126 if (unlikely(type & PTP_CLASS_V1)) {
127 /* msg type is located at the control field for ptp v1 */
128 msgtype = hdr->control;
130 msgtype = hdr->tsmt & 0x0f;
137 * ptp_check_diff8 - Computes new checksum (when altering a 64-bit field)
138 * @old: old field value
139 * @new: new field value
140 * @oldsum: previous checksum
142 * This function can be used to calculate a new checksum when only a single
143 * field is changed. Similar as ip_vs_check_diff*() in ip_vs.h.
145 * Return: Updated checksum
147 static inline __wsum ptp_check_diff8(__be64 old, __be64 new, __wsum oldsum)
149 __be64 diff[2] = { ~old, new };
151 return csum_partial(diff, sizeof(diff), oldsum);
155 * ptp_header_update_correction - Update PTP header's correction field
156 * @skb: packet buffer
157 * @type: type of the packet (see ptp_classify_raw())
159 * @correction: new correction value
161 * This updates the correction field of a PTP header and updates the UDP
162 * checksum (if UDP is used as transport). It is needed for hardware capable of
163 * one-step P2P that does not already modify the correction field of Pdelay_Req
164 * event messages on ingress.
167 void ptp_header_update_correction(struct sk_buff *skb, unsigned int type,
168 struct ptp_header *hdr, s64 correction)
170 __be64 correction_old;
173 /* previous correction value is required for checksum update. */
174 memcpy(&correction_old, &hdr->correction, sizeof(correction_old));
176 /* write new correction value */
177 put_unaligned_be64((u64)correction, &hdr->correction);
179 switch (type & PTP_CLASS_PMASK) {
182 /* locate udp header */
183 uhdr = (struct udphdr *)((char *)hdr - sizeof(struct udphdr));
189 /* update checksum */
190 uhdr->check = csum_fold(ptp_check_diff8(correction_old,
192 ~csum_unfold(uhdr->check)));
194 uhdr->check = CSUM_MANGLED_0;
196 skb->ip_summed = CHECKSUM_NONE;
200 * ptp_msg_is_sync - Evaluates whether the given skb is a PTP Sync message
201 * @skb: packet buffer
202 * @type: type of the packet (see ptp_classify_raw())
204 * This function evaluates whether the given skb is a PTP Sync message.
206 * Return: true if sync message, false otherwise
208 bool ptp_msg_is_sync(struct sk_buff *skb, unsigned int type);
210 void __init ptp_classifier_init(void);
212 static inline void ptp_classifier_init(void)
215 static inline unsigned int ptp_classify_raw(struct sk_buff *skb)
217 return PTP_CLASS_NONE;
219 static inline struct ptp_header *ptp_parse_header(struct sk_buff *skb,
224 static inline u8 ptp_get_msgtype(const struct ptp_header *hdr,
227 /* The return is meaningless. The stub function would not be
228 * executed since no available header from ptp_parse_header.
230 return PTP_MSGTYPE_SYNC;
232 static inline bool ptp_msg_is_sync(struct sk_buff *skb, unsigned int type)
238 void ptp_header_update_correction(struct sk_buff *skb, unsigned int type,
239 struct ptp_header *hdr, s64 correction)
243 #endif /* _PTP_CLASSIFY_H_ */