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