1*4882a593Smuzhiyun /* SPDX-License-Identifier: GPL-2.0-or-later */
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * PTP 1588 support
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * This file implements a BPF that recognizes PTP event messages.
6*4882a593Smuzhiyun *
7*4882a593Smuzhiyun * Copyright (C) 2010 OMICRON electronics GmbH
8*4882a593Smuzhiyun */
9*4882a593Smuzhiyun
10*4882a593Smuzhiyun #ifndef _PTP_CLASSIFY_H_
11*4882a593Smuzhiyun #define _PTP_CLASSIFY_H_
12*4882a593Smuzhiyun
13*4882a593Smuzhiyun #include <linux/ip.h>
14*4882a593Smuzhiyun #include <linux/skbuff.h>
15*4882a593Smuzhiyun
16*4882a593Smuzhiyun #define PTP_CLASS_NONE 0x00 /* not a PTP event message */
17*4882a593Smuzhiyun #define PTP_CLASS_V1 0x01 /* protocol version 1 */
18*4882a593Smuzhiyun #define PTP_CLASS_V2 0x02 /* protocol version 2 */
19*4882a593Smuzhiyun #define PTP_CLASS_VMASK 0x0f /* max protocol version is 15 */
20*4882a593Smuzhiyun #define PTP_CLASS_IPV4 0x10 /* event in an IPV4 UDP packet */
21*4882a593Smuzhiyun #define PTP_CLASS_IPV6 0x20 /* event in an IPV6 UDP packet */
22*4882a593Smuzhiyun #define PTP_CLASS_L2 0x40 /* event in a L2 packet */
23*4882a593Smuzhiyun #define PTP_CLASS_PMASK 0x70 /* mask for the packet type field */
24*4882a593Smuzhiyun #define PTP_CLASS_VLAN 0x80 /* event in a VLAN tagged packet */
25*4882a593Smuzhiyun
26*4882a593Smuzhiyun #define PTP_CLASS_V1_IPV4 (PTP_CLASS_V1 | PTP_CLASS_IPV4)
27*4882a593Smuzhiyun #define PTP_CLASS_V1_IPV6 (PTP_CLASS_V1 | PTP_CLASS_IPV6) /* probably DNE */
28*4882a593Smuzhiyun #define PTP_CLASS_V2_IPV4 (PTP_CLASS_V2 | PTP_CLASS_IPV4)
29*4882a593Smuzhiyun #define PTP_CLASS_V2_IPV6 (PTP_CLASS_V2 | PTP_CLASS_IPV6)
30*4882a593Smuzhiyun #define PTP_CLASS_V2_L2 (PTP_CLASS_V2 | PTP_CLASS_L2)
31*4882a593Smuzhiyun #define PTP_CLASS_V2_VLAN (PTP_CLASS_V2 | PTP_CLASS_VLAN)
32*4882a593Smuzhiyun #define PTP_CLASS_L4 (PTP_CLASS_IPV4 | PTP_CLASS_IPV6)
33*4882a593Smuzhiyun
34*4882a593Smuzhiyun #define PTP_EV_PORT 319
35*4882a593Smuzhiyun #define PTP_GEN_BIT 0x08 /* indicates general message, if set in message type */
36*4882a593Smuzhiyun
37*4882a593Smuzhiyun #define OFF_PTP_SOURCE_UUID 22 /* PTPv1 only */
38*4882a593Smuzhiyun #define OFF_PTP_SEQUENCE_ID 30
39*4882a593Smuzhiyun
40*4882a593Smuzhiyun /* Below defines should actually be removed at some point in time. */
41*4882a593Smuzhiyun #define IP6_HLEN 40
42*4882a593Smuzhiyun #define UDP_HLEN 8
43*4882a593Smuzhiyun #define OFF_IHL 14
44*4882a593Smuzhiyun #define IPV4_HLEN(data) (((struct iphdr *)(data + OFF_IHL))->ihl << 2)
45*4882a593Smuzhiyun
46*4882a593Smuzhiyun struct clock_identity {
47*4882a593Smuzhiyun u8 id[8];
48*4882a593Smuzhiyun } __packed;
49*4882a593Smuzhiyun
50*4882a593Smuzhiyun struct port_identity {
51*4882a593Smuzhiyun struct clock_identity clock_identity;
52*4882a593Smuzhiyun __be16 port_number;
53*4882a593Smuzhiyun } __packed;
54*4882a593Smuzhiyun
55*4882a593Smuzhiyun struct ptp_header {
56*4882a593Smuzhiyun u8 tsmt; /* transportSpecific | messageType */
57*4882a593Smuzhiyun u8 ver; /* reserved | versionPTP */
58*4882a593Smuzhiyun __be16 message_length;
59*4882a593Smuzhiyun u8 domain_number;
60*4882a593Smuzhiyun u8 reserved1;
61*4882a593Smuzhiyun u8 flag_field[2];
62*4882a593Smuzhiyun __be64 correction;
63*4882a593Smuzhiyun __be32 reserved2;
64*4882a593Smuzhiyun struct port_identity source_port_identity;
65*4882a593Smuzhiyun __be16 sequence_id;
66*4882a593Smuzhiyun u8 control;
67*4882a593Smuzhiyun u8 log_message_interval;
68*4882a593Smuzhiyun } __packed;
69*4882a593Smuzhiyun
70*4882a593Smuzhiyun #if defined(CONFIG_NET_PTP_CLASSIFY)
71*4882a593Smuzhiyun /**
72*4882a593Smuzhiyun * ptp_classify_raw - classify a PTP packet
73*4882a593Smuzhiyun * @skb: buffer
74*4882a593Smuzhiyun *
75*4882a593Smuzhiyun * Runs a minimal BPF dissector to classify a network packet to
76*4882a593Smuzhiyun * determine the PTP class. In case the skb does not contain any
77*4882a593Smuzhiyun * PTP protocol data, PTP_CLASS_NONE will be returned, otherwise
78*4882a593Smuzhiyun * PTP_CLASS_V1_IPV{4,6}, PTP_CLASS_V2_IPV{4,6} or
79*4882a593Smuzhiyun * PTP_CLASS_V2_{L2,VLAN}, depending on the packet content.
80*4882a593Smuzhiyun */
81*4882a593Smuzhiyun unsigned int ptp_classify_raw(const struct sk_buff *skb);
82*4882a593Smuzhiyun
83*4882a593Smuzhiyun /**
84*4882a593Smuzhiyun * ptp_parse_header - Get pointer to the PTP v2 header
85*4882a593Smuzhiyun * @skb: packet buffer
86*4882a593Smuzhiyun * @type: type of the packet (see ptp_classify_raw())
87*4882a593Smuzhiyun *
88*4882a593Smuzhiyun * This function takes care of the VLAN, UDP, IPv4 and IPv6 headers. The length
89*4882a593Smuzhiyun * is checked.
90*4882a593Smuzhiyun *
91*4882a593Smuzhiyun * Note, internally skb_mac_header() is used. Make sure that the @skb is
92*4882a593Smuzhiyun * initialized accordingly.
93*4882a593Smuzhiyun *
94*4882a593Smuzhiyun * Return: Pointer to the ptp v2 header or NULL if not found
95*4882a593Smuzhiyun */
96*4882a593Smuzhiyun struct ptp_header *ptp_parse_header(struct sk_buff *skb, unsigned int type);
97*4882a593Smuzhiyun
98*4882a593Smuzhiyun /**
99*4882a593Smuzhiyun * ptp_get_msgtype - Extract ptp message type from given header
100*4882a593Smuzhiyun * @hdr: ptp header
101*4882a593Smuzhiyun * @type: type of the packet (see ptp_classify_raw())
102*4882a593Smuzhiyun *
103*4882a593Smuzhiyun * This function returns the message type for a given ptp header. It takes care
104*4882a593Smuzhiyun * of the different ptp header versions (v1 or v2).
105*4882a593Smuzhiyun *
106*4882a593Smuzhiyun * Return: The message type
107*4882a593Smuzhiyun */
ptp_get_msgtype(const struct ptp_header * hdr,unsigned int type)108*4882a593Smuzhiyun static inline u8 ptp_get_msgtype(const struct ptp_header *hdr,
109*4882a593Smuzhiyun unsigned int type)
110*4882a593Smuzhiyun {
111*4882a593Smuzhiyun u8 msgtype;
112*4882a593Smuzhiyun
113*4882a593Smuzhiyun if (unlikely(type & PTP_CLASS_V1)) {
114*4882a593Smuzhiyun /* msg type is located at the control field for ptp v1 */
115*4882a593Smuzhiyun msgtype = hdr->control;
116*4882a593Smuzhiyun } else {
117*4882a593Smuzhiyun msgtype = hdr->tsmt & 0x0f;
118*4882a593Smuzhiyun }
119*4882a593Smuzhiyun
120*4882a593Smuzhiyun return msgtype;
121*4882a593Smuzhiyun }
122*4882a593Smuzhiyun
123*4882a593Smuzhiyun void __init ptp_classifier_init(void);
124*4882a593Smuzhiyun #else
ptp_classifier_init(void)125*4882a593Smuzhiyun static inline void ptp_classifier_init(void)
126*4882a593Smuzhiyun {
127*4882a593Smuzhiyun }
ptp_classify_raw(struct sk_buff * skb)128*4882a593Smuzhiyun static inline unsigned int ptp_classify_raw(struct sk_buff *skb)
129*4882a593Smuzhiyun {
130*4882a593Smuzhiyun return PTP_CLASS_NONE;
131*4882a593Smuzhiyun }
ptp_parse_header(struct sk_buff * skb,unsigned int type)132*4882a593Smuzhiyun static inline struct ptp_header *ptp_parse_header(struct sk_buff *skb,
133*4882a593Smuzhiyun unsigned int type)
134*4882a593Smuzhiyun {
135*4882a593Smuzhiyun return NULL;
136*4882a593Smuzhiyun }
ptp_get_msgtype(const struct ptp_header * hdr,unsigned int type)137*4882a593Smuzhiyun static inline u8 ptp_get_msgtype(const struct ptp_header *hdr,
138*4882a593Smuzhiyun unsigned int type)
139*4882a593Smuzhiyun {
140*4882a593Smuzhiyun /* The return is meaningless. The stub function would not be
141*4882a593Smuzhiyun * executed since no available header from ptp_parse_header.
142*4882a593Smuzhiyun */
143*4882a593Smuzhiyun return 0;
144*4882a593Smuzhiyun }
145*4882a593Smuzhiyun #endif
146*4882a593Smuzhiyun #endif /* _PTP_CLASSIFY_H_ */
147