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 <asm/unaligned.h> |
14 | #include <linux/ip.h> |
15 | #include <linux/ktime.h> |
16 | #include <linux/skbuff.h> |
17 | #include <linux/udp.h> |
18 | #include <net/checksum.h> |
19 | |
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 */ |
29 | |
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) |
37 | |
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 |
42 | |
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 */ |
46 | |
47 | #define OFF_PTP_SOURCE_UUID 22 /* PTPv1 only */ |
48 | #define OFF_PTP_SEQUENCE_ID 30 |
49 | |
50 | /* PTP header flag fields */ |
51 | #define PTP_FLAG_TWOSTEP BIT(1) |
52 | |
53 | /* Below defines should actually be removed at some point in time. */ |
54 | #define IP6_HLEN 40 |
55 | #define UDP_HLEN 8 |
56 | #define OFF_IHL 14 |
57 | #define IPV4_HLEN(data) (((struct iphdr *)(data + OFF_IHL))->ihl << 2) |
58 | |
59 | struct clock_identity { |
60 | u8 id[8]; |
61 | } __packed; |
62 | |
63 | struct port_identity { |
64 | struct clock_identity clock_identity; |
65 | __be16 port_number; |
66 | } __packed; |
67 | |
68 | struct { |
69 | u8 ; /* transportSpecific | messageType */ |
70 | u8 ; /* reserved | versionPTP */ |
71 | __be16 ; |
72 | u8 domain_number; |
73 | u8 ; |
74 | u8 [2]; |
75 | __be64 ; |
76 | __be32 ; |
77 | struct port_identity ; |
78 | __be16 ; |
79 | u8 ; |
80 | u8 ; |
81 | } __packed; |
82 | |
83 | #if defined(CONFIG_NET_PTP_CLASSIFY) |
84 | /** |
85 | * ptp_classify_raw - classify a PTP packet |
86 | * @skb: buffer |
87 | * |
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. |
93 | */ |
94 | unsigned int ptp_classify_raw(const struct sk_buff *skb); |
95 | |
96 | /** |
97 | * ptp_parse_header - Get pointer to the PTP v2 header |
98 | * @skb: packet buffer |
99 | * @type: type of the packet (see ptp_classify_raw()) |
100 | * |
101 | * This function takes care of the VLAN, UDP, IPv4 and IPv6 headers. The length |
102 | * is checked. |
103 | * |
104 | * Note, internally skb_mac_header() is used. Make sure that the @skb is |
105 | * initialized accordingly. |
106 | * |
107 | * Return: Pointer to the ptp v2 header or NULL if not found |
108 | */ |
109 | struct ptp_header *(struct sk_buff *skb, unsigned int type); |
110 | |
111 | /** |
112 | * ptp_get_msgtype - Extract ptp message type from given header |
113 | * @hdr: ptp header |
114 | * @type: type of the packet (see ptp_classify_raw()) |
115 | * |
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). |
118 | * |
119 | * Return: The message type |
120 | */ |
121 | static inline u8 ptp_get_msgtype(const struct ptp_header *hdr, |
122 | unsigned int type) |
123 | { |
124 | u8 msgtype; |
125 | |
126 | if (unlikely(type & PTP_CLASS_V1)) { |
127 | /* msg type is located at the control field for ptp v1 */ |
128 | msgtype = hdr->control; |
129 | } else { |
130 | msgtype = hdr->tsmt & 0x0f; |
131 | } |
132 | |
133 | return msgtype; |
134 | } |
135 | |
136 | /** |
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 |
141 | * |
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. |
144 | * |
145 | * Return: Updated checksum |
146 | */ |
147 | static inline __wsum ptp_check_diff8(__be64 old, __be64 new, __wsum oldsum) |
148 | { |
149 | __be64 diff[2] = { ~old, new }; |
150 | |
151 | return csum_partial(buff: diff, len: sizeof(diff), sum: oldsum); |
152 | } |
153 | |
154 | /** |
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()) |
158 | * @hdr: ptp header |
159 | * @correction: new correction value |
160 | * |
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. |
165 | */ |
166 | static inline |
167 | void (struct sk_buff *skb, unsigned int type, |
168 | struct ptp_header *hdr, s64 correction) |
169 | { |
170 | __be64 correction_old; |
171 | struct udphdr *uhdr; |
172 | |
173 | /* previous correction value is required for checksum update. */ |
174 | memcpy(&correction_old, &hdr->correction, sizeof(correction_old)); |
175 | |
176 | /* write new correction value */ |
177 | put_unaligned_be64(val: (u64)correction, p: &hdr->correction); |
178 | |
179 | switch (type & PTP_CLASS_PMASK) { |
180 | case PTP_CLASS_IPV4: |
181 | case PTP_CLASS_IPV6: |
182 | /* locate udp header */ |
183 | uhdr = (struct udphdr *)((char *)hdr - sizeof(struct udphdr)); |
184 | break; |
185 | default: |
186 | return; |
187 | } |
188 | |
189 | /* update checksum */ |
190 | uhdr->check = csum_fold(sum: ptp_check_diff8(old: correction_old, |
191 | new: hdr->correction, |
192 | oldsum: ~csum_unfold(n: uhdr->check))); |
193 | if (!uhdr->check) |
194 | uhdr->check = CSUM_MANGLED_0; |
195 | |
196 | skb->ip_summed = CHECKSUM_NONE; |
197 | } |
198 | |
199 | /** |
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()) |
203 | * |
204 | * This function evaluates whether the given skb is a PTP Sync message. |
205 | * |
206 | * Return: true if sync message, false otherwise |
207 | */ |
208 | bool ptp_msg_is_sync(struct sk_buff *skb, unsigned int type); |
209 | |
210 | void __init ptp_classifier_init(void); |
211 | #else |
212 | static inline void ptp_classifier_init(void) |
213 | { |
214 | } |
215 | static inline unsigned int ptp_classify_raw(struct sk_buff *skb) |
216 | { |
217 | return PTP_CLASS_NONE; |
218 | } |
219 | static inline struct ptp_header *ptp_parse_header(struct sk_buff *skb, |
220 | unsigned int type) |
221 | { |
222 | return NULL; |
223 | } |
224 | static inline u8 ptp_get_msgtype(const struct ptp_header *hdr, |
225 | unsigned int type) |
226 | { |
227 | /* The return is meaningless. The stub function would not be |
228 | * executed since no available header from ptp_parse_header. |
229 | */ |
230 | return PTP_MSGTYPE_SYNC; |
231 | } |
232 | static inline bool ptp_msg_is_sync(struct sk_buff *skb, unsigned int type) |
233 | { |
234 | return false; |
235 | } |
236 | |
237 | static inline |
238 | void ptp_header_update_correction(struct sk_buff *skb, unsigned int type, |
239 | struct ptp_header *hdr, s64 correction) |
240 | { |
241 | } |
242 | #endif |
243 | #endif /* _PTP_CLASSIFY_H_ */ |
244 | |