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 | |
| 34 | #define PTP_EV_PORT 319 |
Richard Cochran | f75159e | 2011-09-20 01:25:41 +0000 | [diff] [blame] | 35 | #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] | 36 | |
Richard Cochran | d94ba80 | 2011-04-22 12:03:08 +0200 | [diff] [blame] | 37 | #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 Borkmann | 408eccc | 2014-04-01 16:20:23 +0200 | [diff] [blame] | 41 | /* Below defines should actually be removed at some point in time. */ |
Richard Cochran | 15f0127 | 2010-07-17 08:49:17 +0000 | [diff] [blame] | 42 | #define IP6_HLEN 40 |
| 43 | #define UDP_HLEN 8 |
Daniel Borkmann | 408eccc | 2014-04-01 16:20:23 +0200 | [diff] [blame] | 44 | #define OFF_IHL 14 |
Daniel Borkmann | 408eccc | 2014-04-01 16:20:23 +0200 | [diff] [blame] | 45 | #define IPV4_HLEN(data) (((struct iphdr *)(data + OFF_IHL))->ihl << 2) |
Richard Cochran | 15f0127 | 2010-07-17 08:49:17 +0000 | [diff] [blame] | 46 | |
Daniel Borkmann | 408eccc | 2014-04-01 16:20:23 +0200 | [diff] [blame] | 47 | #if defined(CONFIG_NET_PTP_CLASSIFY) |
| 48 | /** |
| 49 | * ptp_classify_raw - classify a PTP packet |
| 50 | * @skb: buffer |
| 51 | * |
| 52 | * Runs a minimal BPF dissector to classify a network packet to |
| 53 | * determine the PTP class. In case the skb does not contain any |
| 54 | * PTP protocol data, PTP_CLASS_NONE will be returned, otherwise |
| 55 | * PTP_CLASS_V1_IPV{4,6}, PTP_CLASS_V2_IPV{4,6} or |
| 56 | * PTP_CLASS_V2_{L2,VLAN}, depending on the packet content. |
| 57 | */ |
Daniel Borkmann | 164d8c6 | 2014-03-28 18:58:22 +0100 | [diff] [blame] | 58 | unsigned int ptp_classify_raw(const struct sk_buff *skb); |
| 59 | |
Daniel Borkmann | 408eccc | 2014-04-01 16:20:23 +0200 | [diff] [blame] | 60 | void __init ptp_classifier_init(void); |
| 61 | #else |
| 62 | static inline void ptp_classifier_init(void) |
| 63 | { |
| 64 | } |
Andrew Lunn | 052fddf | 2018-02-14 01:07:42 +0100 | [diff] [blame] | 65 | static inline unsigned int ptp_classify_raw(struct sk_buff *skb) |
| 66 | { |
| 67 | return PTP_CLASS_NONE; |
| 68 | } |
Richard Cochran | 15f0127 | 2010-07-17 08:49:17 +0000 | [diff] [blame] | 69 | #endif |
Daniel Borkmann | 408eccc | 2014-04-01 16:20:23 +0200 | [diff] [blame] | 70 | #endif /* _PTP_CLASSIFY_H_ */ |