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_
14 #include <linux/skbuff.h>
16 #define PTP_CLASS_NONE 0x00 /* not a PTP event message */
17 #define PTP_CLASS_V1 0x01 /* protocol version 1 */
18 #define PTP_CLASS_V2 0x02 /* protocol version 2 */
19 #define PTP_CLASS_VMASK 0x0f /* max protocol version is 15 */
20 #define PTP_CLASS_IPV4 0x10 /* event in an IPV4 UDP packet */
21 #define PTP_CLASS_IPV6 0x20 /* event in an IPV6 UDP packet */
22 #define PTP_CLASS_L2 0x40 /* event in a L2 packet */
23 #define PTP_CLASS_PMASK 0x70 /* mask for the packet type field */
24 #define PTP_CLASS_VLAN 0x80 /* event in a VLAN tagged packet */
26 #define PTP_CLASS_V1_IPV4 (PTP_CLASS_V1 | PTP_CLASS_IPV4)
27 #define PTP_CLASS_V1_IPV6 (PTP_CLASS_V1 | PTP_CLASS_IPV6) /* probably DNE */
28 #define PTP_CLASS_V2_IPV4 (PTP_CLASS_V2 | PTP_CLASS_IPV4)
29 #define PTP_CLASS_V2_IPV6 (PTP_CLASS_V2 | PTP_CLASS_IPV6)
30 #define PTP_CLASS_V2_L2 (PTP_CLASS_V2 | PTP_CLASS_L2)
31 #define PTP_CLASS_V2_VLAN (PTP_CLASS_V2 | PTP_CLASS_VLAN)
32 #define PTP_CLASS_L4 (PTP_CLASS_IPV4 | PTP_CLASS_IPV6)
34 #define PTP_MSGTYPE_SYNC 0x0
35 #define PTP_MSGTYPE_DELAY_REQ 0x1
36 #define PTP_MSGTYPE_PDELAY_REQ 0x2
37 #define PTP_MSGTYPE_PDELAY_RESP 0x3
39 #define PTP_EV_PORT 319
40 #define PTP_GEN_PORT 320
41 #define PTP_GEN_BIT 0x08 /* indicates general message, if set in message type */
43 #define OFF_PTP_SOURCE_UUID 22 /* PTPv1 only */
44 #define OFF_PTP_SEQUENCE_ID 30
46 /* PTP header flag fields */
47 #define PTP_FLAG_TWOSTEP BIT(1)
49 /* Below defines should actually be removed at some point in time. */
53 #define IPV4_HLEN(data) (((struct iphdr *)(data + OFF_IHL))->ihl << 2)
55 struct clock_identity {
59 struct port_identity {
60 struct clock_identity clock_identity;
65 u8 tsmt; /* transportSpecific | messageType */
66 u8 ver; /* reserved | versionPTP */
67 __be16 message_length;
73 struct port_identity source_port_identity;
76 u8 log_message_interval;
79 #if defined(CONFIG_NET_PTP_CLASSIFY)
81 * ptp_classify_raw - classify a PTP packet
84 * Runs a minimal BPF dissector to classify a network packet to
85 * determine the PTP class. In case the skb does not contain any
86 * PTP protocol data, PTP_CLASS_NONE will be returned, otherwise
87 * PTP_CLASS_V1_IPV{4,6}, PTP_CLASS_V2_IPV{4,6} or
88 * PTP_CLASS_V2_{L2,VLAN}, depending on the packet content.
90 unsigned int ptp_classify_raw(const struct sk_buff *skb);
93 * ptp_parse_header - Get pointer to the PTP v2 header
95 * @type: type of the packet (see ptp_classify_raw())
97 * This function takes care of the VLAN, UDP, IPv4 and IPv6 headers. The length
100 * Note, internally skb_mac_header() is used. Make sure that the @skb is
101 * initialized accordingly.
103 * Return: Pointer to the ptp v2 header or NULL if not found
105 struct ptp_header *ptp_parse_header(struct sk_buff *skb, unsigned int type);
108 * ptp_get_msgtype - Extract ptp message type from given header
110 * @type: type of the packet (see ptp_classify_raw())
112 * This function returns the message type for a given ptp header. It takes care
113 * of the different ptp header versions (v1 or v2).
115 * Return: The message type
117 static inline u8 ptp_get_msgtype(const struct ptp_header *hdr,
122 if (unlikely(type & PTP_CLASS_V1)) {
123 /* msg type is located at the control field for ptp v1 */
124 msgtype = hdr->control;
126 msgtype = hdr->tsmt & 0x0f;
133 * ptp_msg_is_sync - Evaluates whether the given skb is a PTP Sync message
134 * @skb: packet buffer
135 * @type: type of the packet (see ptp_classify_raw())
137 * This function evaluates whether the given skb is a PTP Sync message.
139 * Return: true if sync message, false otherwise
141 bool ptp_msg_is_sync(struct sk_buff *skb, unsigned int type);
143 void __init ptp_classifier_init(void);
145 static inline void ptp_classifier_init(void)
148 static inline unsigned int ptp_classify_raw(struct sk_buff *skb)
150 return PTP_CLASS_NONE;
152 static inline struct ptp_header *ptp_parse_header(struct sk_buff *skb,
157 static inline u8 ptp_get_msgtype(const struct ptp_header *hdr,
160 /* The return is meaningless. The stub function would not be
161 * executed since no available header from ptp_parse_header.
163 return PTP_MSGTYPE_SYNC;
165 static inline bool ptp_msg_is_sync(struct sk_buff *skb, unsigned int type)
170 #endif /* _PTP_CLASSIFY_H_ */