blob: 0a9cc0eb0801e4a05724a663a3966e5777603622 [file] [log] [blame]
Thomas Gleixner74ba9202019-05-20 09:19:02 +02001/* SPDX-License-Identifier: GPL-2.0-or-later */
Richard Cochran15f01272010-07-17 08:49:17 +00002/*
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 Cochran15f01272010-07-17 08:49:17 +00008 */
9
10#ifndef _PTP_CLASSIFY_H_
11#define _PTP_CLASSIFY_H_
12
Richard Cochrand94ba802011-04-22 12:03:08 +020013#include <linux/ip.h>
Daniel Borkmann408eccc2014-04-01 16:20:23 +020014#include <linux/skbuff.h>
Richard Cochran15f01272010-07-17 08:49:17 +000015
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ørensen5f94c942015-11-03 09:34:07 +010022#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 Cochran15f01272010-07-17 08:49:17 +000025
26#define PTP_CLASS_V1_IPV4 (PTP_CLASS_V1 | PTP_CLASS_IPV4)
Daniel Borkmann408eccc2014-04-01 16:20:23 +020027#define PTP_CLASS_V1_IPV6 (PTP_CLASS_V1 | PTP_CLASS_IPV6) /* probably DNE */
Richard Cochran15f01272010-07-17 08:49:17 +000028#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ørensen5f94c942015-11-03 09:34:07 +010032#define PTP_CLASS_L4 (PTP_CLASS_IPV4 | PTP_CLASS_IPV6)
Richard Cochran15f01272010-07-17 08:49:17 +000033
34#define PTP_EV_PORT 319
Richard Cochranf75159e2011-09-20 01:25:41 +000035#define PTP_GEN_BIT 0x08 /* indicates general message, if set in message type */
Richard Cochran15f01272010-07-17 08:49:17 +000036
Richard Cochrand94ba802011-04-22 12:03:08 +020037#define OFF_PTP_SOURCE_UUID 22 /* PTPv1 only */
38#define OFF_PTP_SEQUENCE_ID 30
39#define OFF_PTP_CONTROL 32 /* PTPv1 only */
40
Daniel Borkmann408eccc2014-04-01 16:20:23 +020041/* Below defines should actually be removed at some point in time. */
Richard Cochran15f01272010-07-17 08:49:17 +000042#define IP6_HLEN 40
43#define UDP_HLEN 8
Daniel Borkmann408eccc2014-04-01 16:20:23 +020044#define OFF_IHL 14
Daniel Borkmann408eccc2014-04-01 16:20:23 +020045#define IPV4_HLEN(data) (((struct iphdr *)(data + OFF_IHL))->ihl << 2)
Richard Cochran15f01272010-07-17 08:49:17 +000046
Kurt Kanzenbachbdfbb63c2020-08-18 12:32:43 +020047struct clock_identity {
48 u8 id[8];
49} __packed;
50
51struct port_identity {
52 struct clock_identity clock_identity;
53 __be16 port_number;
54} __packed;
55
56struct 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
Daniel Borkmann408eccc2014-04-01 16:20:23 +020071#if defined(CONFIG_NET_PTP_CLASSIFY)
72/**
73 * ptp_classify_raw - classify a PTP packet
74 * @skb: buffer
75 *
76 * Runs a minimal BPF dissector to classify a network packet to
77 * determine the PTP class. In case the skb does not contain any
78 * PTP protocol data, PTP_CLASS_NONE will be returned, otherwise
79 * PTP_CLASS_V1_IPV{4,6}, PTP_CLASS_V2_IPV{4,6} or
80 * PTP_CLASS_V2_{L2,VLAN}, depending on the packet content.
81 */
Daniel Borkmann164d8c62014-03-28 18:58:22 +010082unsigned int ptp_classify_raw(const struct sk_buff *skb);
83
Kurt Kanzenbachbdfbb63c2020-08-18 12:32:43 +020084/**
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 */
97struct ptp_header *ptp_parse_header(struct sk_buff *skb, unsigned int type);
98
Daniel Borkmann408eccc2014-04-01 16:20:23 +020099void __init ptp_classifier_init(void);
100#else
101static inline void ptp_classifier_init(void)
102{
103}
Andrew Lunn052fddf2018-02-14 01:07:42 +0100104static inline unsigned int ptp_classify_raw(struct sk_buff *skb)
105{
106 return PTP_CLASS_NONE;
107}
Kurt Kanzenbachbdfbb63c2020-08-18 12:32:43 +0200108static inline struct ptp_header *ptp_parse_header(struct sk_buff *skb,
109 unsigned int type)
110{
111 return NULL;
112}
Richard Cochran15f01272010-07-17 08:49:17 +0000113#endif
Daniel Borkmann408eccc2014-04-01 16:20:23 +0200114#endif /* _PTP_CLASSIFY_H_ */