ptp_classify.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. #ifndef _PTP_CLASSIFY_H_
  10. #define _PTP_CLASSIFY_H_
  11. #include <linux/ip.h>
  12. #include <linux/skbuff.h>
  13. #define PTP_CLASS_NONE 0x00 /* not a PTP event message */
  14. #define PTP_CLASS_V1 0x01 /* protocol version 1 */
  15. #define PTP_CLASS_V2 0x02 /* protocol version 2 */
  16. #define PTP_CLASS_VMASK 0x0f /* max protocol version is 15 */
  17. #define PTP_CLASS_IPV4 0x10 /* event in an IPV4 UDP packet */
  18. #define PTP_CLASS_IPV6 0x20 /* event in an IPV6 UDP packet */
  19. #define PTP_CLASS_L2 0x40 /* event in a L2 packet */
  20. #define PTP_CLASS_PMASK 0x70 /* mask for the packet type field */
  21. #define PTP_CLASS_VLAN 0x80 /* event in a VLAN tagged packet */
  22. #define PTP_CLASS_V1_IPV4 (PTP_CLASS_V1 | PTP_CLASS_IPV4)
  23. #define PTP_CLASS_V1_IPV6 (PTP_CLASS_V1 | PTP_CLASS_IPV6) /* probably DNE */
  24. #define PTP_CLASS_V2_IPV4 (PTP_CLASS_V2 | PTP_CLASS_IPV4)
  25. #define PTP_CLASS_V2_IPV6 (PTP_CLASS_V2 | PTP_CLASS_IPV6)
  26. #define PTP_CLASS_V2_L2 (PTP_CLASS_V2 | PTP_CLASS_L2)
  27. #define PTP_CLASS_V2_VLAN (PTP_CLASS_V2 | PTP_CLASS_VLAN)
  28. #define PTP_CLASS_L4 (PTP_CLASS_IPV4 | PTP_CLASS_IPV6)
  29. #define PTP_MSGTYPE_SYNC 0x0
  30. #define PTP_MSGTYPE_DELAY_REQ 0x1
  31. #define PTP_MSGTYPE_PDELAY_REQ 0x2
  32. #define PTP_MSGTYPE_PDELAY_RESP 0x3
  33. #define PTP_EV_PORT 319
  34. #define PTP_GEN_PORT 320
  35. #define PTP_GEN_BIT 0x08 /* indicates general message, if set in message type */
  36. #define OFF_PTP_SOURCE_UUID 22 /* PTPv1 only */
  37. #define OFF_PTP_SEQUENCE_ID 30
  38. /* PTP header flag fields */
  39. #define PTP_FLAG_TWOSTEP BIT(1)
  40. /* Below defines should actually be removed at some point in time. */
  41. #define IP6_HLEN 40
  42. #define UDP_HLEN 8
  43. #define OFF_IHL 14
  44. #define IPV4_HLEN(data) (((struct iphdr *)(data + OFF_IHL))->ihl << 2)
  45. struct clock_identity {
  46. u8 id[8];
  47. } __packed;
  48. struct port_identity {
  49. struct clock_identity clock_identity;
  50. __be16 port_number;
  51. } __packed;
  52. struct ptp_header {
  53. u8 tsmt; /* transportSpecific | messageType */
  54. u8 ver; /* reserved | versionPTP */
  55. __be16 message_length;
  56. u8 domain_number;
  57. u8 reserved1;
  58. u8 flag_field[2];
  59. __be64 correction;
  60. __be32 reserved2;
  61. struct port_identity source_port_identity;
  62. __be16 sequence_id;
  63. u8 control;
  64. u8 log_message_interval;
  65. } __packed;
  66. #if defined(CONFIG_NET_PTP_CLASSIFY)
  67. /**
  68. * ptp_classify_raw - classify a PTP packet
  69. * @skb: buffer
  70. *
  71. * Runs a minimal BPF dissector to classify a network packet to
  72. * determine the PTP class. In case the skb does not contain any
  73. * PTP protocol data, PTP_CLASS_NONE will be returned, otherwise
  74. * PTP_CLASS_V1_IPV{4,6}, PTP_CLASS_V2_IPV{4,6} or
  75. * PTP_CLASS_V2_{L2,VLAN}, depending on the packet content.
  76. */
  77. unsigned int ptp_classify_raw(const struct sk_buff *skb);
  78. /**
  79. * ptp_parse_header - Get pointer to the PTP v2 header
  80. * @skb: packet buffer
  81. * @type: type of the packet (see ptp_classify_raw())
  82. *
  83. * This function takes care of the VLAN, UDP, IPv4 and IPv6 headers. The length
  84. * is checked.
  85. *
  86. * Note, internally skb_mac_header() is used. Make sure that the @skb is
  87. * initialized accordingly.
  88. *
  89. * Return: Pointer to the ptp v2 header or NULL if not found
  90. */
  91. struct ptp_header *ptp_parse_header(struct sk_buff *skb, unsigned int type);
  92. /**
  93. * ptp_get_msgtype - Extract ptp message type from given header
  94. * @hdr: ptp header
  95. * @type: type of the packet (see ptp_classify_raw())
  96. *
  97. * This function returns the message type for a given ptp header. It takes care
  98. * of the different ptp header versions (v1 or v2).
  99. *
  100. * Return: The message type
  101. */
  102. static inline u8 ptp_get_msgtype(const struct ptp_header *hdr,
  103. unsigned int type)
  104. {
  105. u8 msgtype;
  106. if (unlikely(type & PTP_CLASS_V1)) {
  107. /* msg type is located at the control field for ptp v1 */
  108. msgtype = hdr->control;
  109. } else {
  110. msgtype = hdr->tsmt & 0x0f;
  111. }
  112. return msgtype;
  113. }
  114. /**
  115. * ptp_msg_is_sync - Evaluates whether the given skb is a PTP Sync message
  116. * @skb: packet buffer
  117. * @type: type of the packet (see ptp_classify_raw())
  118. *
  119. * This function evaluates whether the given skb is a PTP Sync message.
  120. *
  121. * Return: true if sync message, false otherwise
  122. */
  123. bool ptp_msg_is_sync(struct sk_buff *skb, unsigned int type);
  124. void __init ptp_classifier_init(void);
  125. #else
  126. static inline void ptp_classifier_init(void)
  127. {
  128. }
  129. static inline unsigned int ptp_classify_raw(struct sk_buff *skb)
  130. {
  131. return PTP_CLASS_NONE;
  132. }
  133. static inline struct ptp_header *ptp_parse_header(struct sk_buff *skb,
  134. unsigned int type)
  135. {
  136. return NULL;
  137. }
  138. static inline u8 ptp_get_msgtype(const struct ptp_header *hdr,
  139. unsigned int type)
  140. {
  141. /* The return is meaningless. The stub function would not be
  142. * executed since no available header from ptp_parse_header.
  143. */
  144. return PTP_MSGTYPE_SYNC;
  145. }
  146. static inline bool ptp_msg_is_sync(struct sk_buff *skb, unsigned int type)
  147. {
  148. return false;
  149. }
  150. #endif
  151. #endif /* _PTP_CLASSIFY_H_ */