Thomas Gleixner | 74ba920 | 2019-05-20 09:19:02 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-or-later */ |
Richard Cochran | 15f0127 | 2010-07-17 08:49:17 +0000 | [diff] [blame] | 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 |
Richard Cochran | 15f0127 | 2010-07-17 08:49:17 +0000 | [diff] [blame] | 8 | */ |
| 9 | |
| 10 | #ifndef _PTP_CLASSIFY_H_ |
| 11 | #define _PTP_CLASSIFY_H_ |
| 12 | |
Richard Cochran | d94ba80 | 2011-04-22 12:03:08 +0200 | [diff] [blame] | 13 | #include <linux/ip.h> |
Daniel Borkmann | 408eccc | 2014-04-01 16:20:23 +0200 | [diff] [blame] | 14 | #include <linux/skbuff.h> |
Richard Cochran | 15f0127 | 2010-07-17 08:49:17 +0000 | [diff] [blame] | 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 */ |
Stefan Sørensen | 5f94c94 | 2015-11-03 09:34:07 +0100 | [diff] [blame] | 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 */ |
Richard Cochran | 15f0127 | 2010-07-17 08:49:17 +0000 | [diff] [blame] | 25 | |
| 26 | #define PTP_CLASS_V1_IPV4 (PTP_CLASS_V1 | PTP_CLASS_IPV4) |
Daniel Borkmann | 408eccc | 2014-04-01 16:20:23 +0200 | [diff] [blame] | 27 | #define PTP_CLASS_V1_IPV6 (PTP_CLASS_V1 | PTP_CLASS_IPV6) /* probably DNE */ |
Richard Cochran | 15f0127 | 2010-07-17 08:49:17 +0000 | [diff] [blame] | 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) |
Stefan Sørensen | 5f94c94 | 2015-11-03 09:34:07 +0100 | [diff] [blame] | 32 | #define PTP_CLASS_L4 (PTP_CLASS_IPV4 | PTP_CLASS_IPV6) |
Richard Cochran | 15f0127 | 2010-07-17 08:49:17 +0000 | [diff] [blame] | 33 | |
Christian Eggers | 076d38b | 2020-11-20 09:41:04 +0100 | [diff] [blame] | 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 | |
Richard Cochran | 15f0127 | 2010-07-17 08:49:17 +0000 | [diff] [blame] | 39 | #define PTP_EV_PORT 319 |
Vladimir Oltean | ec15bae | 2021-11-26 19:28:43 +0200 | [diff] [blame] | 40 | #define PTP_GEN_PORT 320 |
Richard Cochran | f75159e | 2011-09-20 01:25:41 +0000 | [diff] [blame] | 41 | #define PTP_GEN_BIT 0x08 /* indicates general message, if set in message type */ |
Richard Cochran | 15f0127 | 2010-07-17 08:49:17 +0000 | [diff] [blame] | 42 | |
Richard Cochran | d94ba80 | 2011-04-22 12:03:08 +0200 | [diff] [blame] | 43 | #define OFF_PTP_SOURCE_UUID 22 /* PTPv1 only */ |
| 44 | #define OFF_PTP_SEQUENCE_ID 30 |
Richard Cochran | d94ba80 | 2011-04-22 12:03:08 +0200 | [diff] [blame] | 45 | |
Harini Katakam | 5cebb40 | 2022-05-18 22:37:56 +0530 | [diff] [blame] | 46 | /* PTP header flag fields */ |
| 47 | #define PTP_FLAG_TWOSTEP BIT(1) |
| 48 | |
Daniel Borkmann | 408eccc | 2014-04-01 16:20:23 +0200 | [diff] [blame] | 49 | /* Below defines should actually be removed at some point in time. */ |
Richard Cochran | 15f0127 | 2010-07-17 08:49:17 +0000 | [diff] [blame] | 50 | #define IP6_HLEN 40 |
| 51 | #define UDP_HLEN 8 |
Daniel Borkmann | 408eccc | 2014-04-01 16:20:23 +0200 | [diff] [blame] | 52 | #define OFF_IHL 14 |
Daniel Borkmann | 408eccc | 2014-04-01 16:20:23 +0200 | [diff] [blame] | 53 | #define IPV4_HLEN(data) (((struct iphdr *)(data + OFF_IHL))->ihl << 2) |
Richard Cochran | 15f0127 | 2010-07-17 08:49:17 +0000 | [diff] [blame] | 54 | |
Kurt Kanzenbach | bdfbb63c | 2020-08-18 12:32:43 +0200 | [diff] [blame] | 55 | struct clock_identity { |
| 56 | u8 id[8]; |
| 57 | } __packed; |
| 58 | |
| 59 | struct port_identity { |
| 60 | struct clock_identity clock_identity; |
| 61 | __be16 port_number; |
| 62 | } __packed; |
| 63 | |
| 64 | struct ptp_header { |
| 65 | u8 tsmt; /* transportSpecific | messageType */ |
| 66 | u8 ver; /* reserved | versionPTP */ |
| 67 | __be16 message_length; |
| 68 | u8 domain_number; |
| 69 | u8 reserved1; |
| 70 | u8 flag_field[2]; |
| 71 | __be64 correction; |
| 72 | __be32 reserved2; |
| 73 | struct port_identity source_port_identity; |
| 74 | __be16 sequence_id; |
| 75 | u8 control; |
| 76 | u8 log_message_interval; |
| 77 | } __packed; |
| 78 | |
Daniel Borkmann | 408eccc | 2014-04-01 16:20:23 +0200 | [diff] [blame] | 79 | #if defined(CONFIG_NET_PTP_CLASSIFY) |
| 80 | /** |
| 81 | * ptp_classify_raw - classify a PTP packet |
| 82 | * @skb: buffer |
| 83 | * |
| 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. |
| 89 | */ |
Daniel Borkmann | 164d8c6 | 2014-03-28 18:58:22 +0100 | [diff] [blame] | 90 | unsigned int ptp_classify_raw(const struct sk_buff *skb); |
| 91 | |
Kurt Kanzenbach | bdfbb63c | 2020-08-18 12:32:43 +0200 | [diff] [blame] | 92 | /** |
| 93 | * ptp_parse_header - Get pointer to the PTP v2 header |
| 94 | * @skb: packet buffer |
| 95 | * @type: type of the packet (see ptp_classify_raw()) |
| 96 | * |
| 97 | * This function takes care of the VLAN, UDP, IPv4 and IPv6 headers. The length |
| 98 | * is checked. |
| 99 | * |
| 100 | * Note, internally skb_mac_header() is used. Make sure that the @skb is |
| 101 | * initialized accordingly. |
| 102 | * |
| 103 | * Return: Pointer to the ptp v2 header or NULL if not found |
| 104 | */ |
| 105 | struct ptp_header *ptp_parse_header(struct sk_buff *skb, unsigned int type); |
| 106 | |
Kurt Kanzenbach | 036c508 | 2020-08-18 12:32:44 +0200 | [diff] [blame] | 107 | /** |
| 108 | * ptp_get_msgtype - Extract ptp message type from given header |
| 109 | * @hdr: ptp header |
| 110 | * @type: type of the packet (see ptp_classify_raw()) |
| 111 | * |
| 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). |
| 114 | * |
| 115 | * Return: The message type |
| 116 | */ |
| 117 | static inline u8 ptp_get_msgtype(const struct ptp_header *hdr, |
| 118 | unsigned int type) |
| 119 | { |
| 120 | u8 msgtype; |
| 121 | |
| 122 | if (unlikely(type & PTP_CLASS_V1)) { |
| 123 | /* msg type is located at the control field for ptp v1 */ |
| 124 | msgtype = hdr->control; |
| 125 | } else { |
| 126 | msgtype = hdr->tsmt & 0x0f; |
| 127 | } |
| 128 | |
| 129 | return msgtype; |
| 130 | } |
| 131 | |
Kurt Kanzenbach | f72de02 | 2022-03-05 12:21:25 +0100 | [diff] [blame] | 132 | /** |
| 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()) |
| 136 | * |
| 137 | * This function evaluates whether the given skb is a PTP Sync message. |
| 138 | * |
| 139 | * Return: true if sync message, false otherwise |
| 140 | */ |
| 141 | bool ptp_msg_is_sync(struct sk_buff *skb, unsigned int type); |
| 142 | |
Daniel Borkmann | 408eccc | 2014-04-01 16:20:23 +0200 | [diff] [blame] | 143 | void __init ptp_classifier_init(void); |
| 144 | #else |
| 145 | static inline void ptp_classifier_init(void) |
| 146 | { |
| 147 | } |
Andrew Lunn | 052fddf | 2018-02-14 01:07:42 +0100 | [diff] [blame] | 148 | static inline unsigned int ptp_classify_raw(struct sk_buff *skb) |
| 149 | { |
| 150 | return PTP_CLASS_NONE; |
| 151 | } |
Kurt Kanzenbach | bdfbb63c | 2020-08-18 12:32:43 +0200 | [diff] [blame] | 152 | static inline struct ptp_header *ptp_parse_header(struct sk_buff *skb, |
| 153 | unsigned int type) |
| 154 | { |
| 155 | return NULL; |
| 156 | } |
Yangbo Lu | e622129 | 2020-09-27 16:01:50 +0800 | [diff] [blame] | 157 | static inline u8 ptp_get_msgtype(const struct ptp_header *hdr, |
| 158 | unsigned int type) |
| 159 | { |
| 160 | /* The return is meaningless. The stub function would not be |
| 161 | * executed since no available header from ptp_parse_header. |
| 162 | */ |
Christian Eggers | 076d38b | 2020-11-20 09:41:04 +0100 | [diff] [blame] | 163 | return PTP_MSGTYPE_SYNC; |
Yangbo Lu | e622129 | 2020-09-27 16:01:50 +0800 | [diff] [blame] | 164 | } |
Kurt Kanzenbach | f72de02 | 2022-03-05 12:21:25 +0100 | [diff] [blame] | 165 | static inline bool ptp_msg_is_sync(struct sk_buff *skb, unsigned int type) |
| 166 | { |
| 167 | return false; |
| 168 | } |
Richard Cochran | 15f0127 | 2010-07-17 08:49:17 +0000 | [diff] [blame] | 169 | #endif |
Daniel Borkmann | 408eccc | 2014-04-01 16:20:23 +0200 | [diff] [blame] | 170 | #endif /* _PTP_CLASSIFY_H_ */ |