Skip to content

Commit bdfbb63

Browse files
shifty91davem330
authored andcommitted
ptp: Add generic ptp v2 header parsing function
Reason: A lot of the ptp drivers - which implement hardware time stamping - need specific fields such as the sequence id from the ptp v2 header. Currently all drivers implement that themselves. Introduce a generic function to retrieve a pointer to the start of the ptp v2 header. Suggested-by: Russell King <rmk+kernel@armlinux.org.uk> Signed-off-by: Kurt Kanzenbach <kurt@linutronix.de> Reviewed-by: Richard Cochran <richardcochran@gmail.com> Reviewed-by: Florian Fainelli <f.fainelli@gmail.com> Reviewed-by: Russell King <rmk+kernel@armlinux.org.uk> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent f3ae59c commit bdfbb63

2 files changed

Lines changed: 74 additions & 0 deletions

File tree

‎include/linux/ptp_classify.h‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,30 @@
4444
#define OFF_IHL 14
4545
#define IPV4_HLEN(data) (((struct iphdr *)(data + OFF_IHL))->ihl << 2)
4646

47+
struct clock_identity {
48+
u8 id[8];
49+
} __packed;
50+
51+
struct port_identity {
52+
struct clock_identity clock_identity;
53+
__be16 port_number;
54+
} __packed;
55+
56+
struct ptp_header {
57+
u8 tsmt; /* transportSpecific | messageType */
58+
u8 ver; /* reserved | versionPTP */
59+
__be16 message_length;
60+
u8 domain_number;
61+
u8 reserved1;
62+
u8 flag_field[2];
63+
__be64 correction;
64+
__be32 reserved2;
65+
struct port_identity source_port_identity;
66+
__be16 sequence_id;
67+
u8 control;
68+
u8 log_message_interval;
69+
} __packed;
70+
4771
#if defined(CONFIG_NET_PTP_CLASSIFY)
4872
/**
4973
* ptp_classify_raw - classify a PTP packet
@@ -57,6 +81,21 @@
5781
*/
5882
unsigned int ptp_classify_raw(const struct sk_buff *skb);
5983

84+
/**
85+
* ptp_parse_header - Get pointer to the PTP v2 header
86+
* @skb: packet buffer
87+
* @type: type of the packet (see ptp_classify_raw())
88+
*
89+
* This function takes care of the VLAN, UDP, IPv4 and IPv6 headers. The length
90+
* is checked.
91+
*
92+
* Note, internally skb_mac_header() is used. Make sure that the @skb is
93+
* initialized accordingly.
94+
*
95+
* Return: Pointer to the ptp v2 header or NULL if not found
96+
*/
97+
struct ptp_header *ptp_parse_header(struct sk_buff *skb, unsigned int type);
98+
6099
void __init ptp_classifier_init(void);
61100
#else
62101
static inline void ptp_classifier_init(void)
@@ -66,5 +105,10 @@ static inline unsigned int ptp_classify_raw(struct sk_buff *skb)
66105
{
67106
return PTP_CLASS_NONE;
68107
}
108+
static inline struct ptp_header *ptp_parse_header(struct sk_buff *skb,
109+
unsigned int type)
110+
{
111+
return NULL;
112+
}
69113
#endif
70114
#endif /* _PTP_CLASSIFY_H_ */

‎net/core/ptp_classifier.c‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,36 @@ unsigned int ptp_classify_raw(const struct sk_buff *skb)
107107
}
108108
EXPORT_SYMBOL_GPL(ptp_classify_raw);
109109

110+
struct ptp_header *ptp_parse_header(struct sk_buff *skb, unsigned int type)
111+
{
112+
u8 *ptr = skb_mac_header(skb);
113+
114+
if (type & PTP_CLASS_VLAN)
115+
ptr += VLAN_HLEN;
116+
117+
switch (type & PTP_CLASS_PMASK) {
118+
case PTP_CLASS_IPV4:
119+
ptr += IPV4_HLEN(ptr) + UDP_HLEN;
120+
break;
121+
case PTP_CLASS_IPV6:
122+
ptr += IP6_HLEN + UDP_HLEN;
123+
break;
124+
case PTP_CLASS_L2:
125+
break;
126+
default:
127+
return NULL;
128+
}
129+
130+
ptr += ETH_HLEN;
131+
132+
/* Ensure that the entire header is present in this packet. */
133+
if (ptr + sizeof(struct ptp_header) > skb->data + skb->len)
134+
return NULL;
135+
136+
return (struct ptp_header *)ptr;
137+
}
138+
EXPORT_SYMBOL_GPL(ptp_parse_header);
139+
110140
void __init ptp_classifier_init(void)
111141
{
112142
static struct sock_filter ptp_filter[] __initdata = {

0 commit comments

Comments
 (0)