ptp_classify.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * PTP 1588 support
  3. *
  4. * This file implements a BPF that recognizes PTP event messages.
  5. *
  6. * Copyright (C) 2010 OMICRON electronics GmbH
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21. */
  22. #ifndef _PTP_CLASSIFY_H_
  23. #define _PTP_CLASSIFY_H_
  24. #include <linux/if_ether.h>
  25. #include <linux/if_vlan.h>
  26. #include <linux/filter.h>
  27. #ifdef __KERNEL__
  28. #include <linux/in.h>
  29. #else
  30. #include <netinet/in.h>
  31. #endif
  32. #define PTP_CLASS_NONE 0x00 /* not a PTP event message */
  33. #define PTP_CLASS_V1 0x01 /* protocol version 1 */
  34. #define PTP_CLASS_V2 0x02 /* protocol version 2 */
  35. #define PTP_CLASS_VMASK 0x0f /* max protocol version is 15 */
  36. #define PTP_CLASS_IPV4 0x10 /* event in an IPV4 UDP packet */
  37. #define PTP_CLASS_IPV6 0x20 /* event in an IPV6 UDP packet */
  38. #define PTP_CLASS_L2 0x30 /* event in a L2 packet */
  39. #define PTP_CLASS_VLAN 0x40 /* event in a VLAN tagged L2 packet */
  40. #define PTP_CLASS_PMASK 0xf0 /* mask for the packet type field */
  41. #define PTP_CLASS_V1_IPV4 (PTP_CLASS_V1 | PTP_CLASS_IPV4)
  42. #define PTP_CLASS_V1_IPV6 (PTP_CLASS_V1 | PTP_CLASS_IPV6) /*probably DNE*/
  43. #define PTP_CLASS_V2_IPV4 (PTP_CLASS_V2 | PTP_CLASS_IPV4)
  44. #define PTP_CLASS_V2_IPV6 (PTP_CLASS_V2 | PTP_CLASS_IPV6)
  45. #define PTP_CLASS_V2_L2 (PTP_CLASS_V2 | PTP_CLASS_L2)
  46. #define PTP_CLASS_V2_VLAN (PTP_CLASS_V2 | PTP_CLASS_VLAN)
  47. #define PTP_EV_PORT 319
  48. #define OFF_ETYPE 12
  49. #define OFF_IHL 14
  50. #define OFF_FRAG 20
  51. #define OFF_PROTO4 23
  52. #define OFF_NEXT 6
  53. #define OFF_UDP_DST 2
  54. #define IP6_HLEN 40
  55. #define UDP_HLEN 8
  56. #define RELOFF_DST4 (ETH_HLEN + OFF_UDP_DST)
  57. #define OFF_DST6 (ETH_HLEN + IP6_HLEN + OFF_UDP_DST)
  58. #define OFF_PTP6 (ETH_HLEN + IP6_HLEN + UDP_HLEN)
  59. #define OP_AND (BPF_ALU | BPF_AND | BPF_K)
  60. #define OP_JEQ (BPF_JMP | BPF_JEQ | BPF_K)
  61. #define OP_JSET (BPF_JMP | BPF_JSET | BPF_K)
  62. #define OP_LDB (BPF_LD | BPF_B | BPF_ABS)
  63. #define OP_LDH (BPF_LD | BPF_H | BPF_ABS)
  64. #define OP_LDHI (BPF_LD | BPF_H | BPF_IND)
  65. #define OP_LDX (BPF_LDX | BPF_B | BPF_MSH)
  66. #define OP_OR (BPF_ALU | BPF_OR | BPF_K)
  67. #define OP_RETA (BPF_RET | BPF_A)
  68. #define OP_RETK (BPF_RET | BPF_K)
  69. static inline int ptp_filter_init(struct sock_filter *f, int len)
  70. {
  71. if (OP_LDH == f[0].code)
  72. return sk_chk_filter(f, len);
  73. else
  74. return 0;
  75. }
  76. #define PTP_FILTER \
  77. {OP_LDH, 0, 0, OFF_ETYPE }, /* */ \
  78. {OP_JEQ, 0, 12, ETH_P_IP }, /* f goto L20 */ \
  79. {OP_LDB, 0, 0, OFF_PROTO4 }, /* */ \
  80. {OP_JEQ, 0, 9, IPPROTO_UDP }, /* f goto L10 */ \
  81. {OP_LDH, 0, 0, OFF_FRAG }, /* */ \
  82. {OP_JSET, 7, 0, 0x1fff }, /* t goto L11 */ \
  83. {OP_LDX, 0, 0, OFF_IHL }, /* */ \
  84. {OP_LDHI, 0, 0, RELOFF_DST4 }, /* */ \
  85. {OP_JEQ, 0, 4, PTP_EV_PORT }, /* f goto L12 */ \
  86. {OP_LDHI, 0, 0, ETH_HLEN + UDP_HLEN }, /* */ \
  87. {OP_AND, 0, 0, PTP_CLASS_VMASK }, /* */ \
  88. {OP_OR, 0, 0, PTP_CLASS_IPV4 }, /* */ \
  89. {OP_RETA, 0, 0, 0 }, /* */ \
  90. /*L1x*/ {OP_RETK, 0, 0, PTP_CLASS_NONE }, /* */ \
  91. /*L20*/ {OP_JEQ, 0, 9, ETH_P_IPV6 }, /* f goto L40 */ \
  92. {OP_LDB, 0, 0, ETH_HLEN + OFF_NEXT }, /* */ \
  93. {OP_JEQ, 0, 6, IPPROTO_UDP }, /* f goto L30 */ \
  94. {OP_LDH, 0, 0, OFF_DST6 }, /* */ \
  95. {OP_JEQ, 0, 4, PTP_EV_PORT }, /* f goto L31 */ \
  96. {OP_LDH, 0, 0, OFF_PTP6 }, /* */ \
  97. {OP_AND, 0, 0, PTP_CLASS_VMASK }, /* */ \
  98. {OP_OR, 0, 0, PTP_CLASS_IPV6 }, /* */ \
  99. {OP_RETA, 0, 0, 0 }, /* */ \
  100. /*L3x*/ {OP_RETK, 0, 0, PTP_CLASS_NONE }, /* */ \
  101. /*L40*/ {OP_JEQ, 0, 6, ETH_P_8021Q }, /* f goto L50 */ \
  102. {OP_LDH, 0, 0, OFF_ETYPE + 4 }, /* */ \
  103. {OP_JEQ, 0, 9, ETH_P_1588 }, /* f goto L60 */ \
  104. {OP_LDH, 0, 0, ETH_HLEN + VLAN_HLEN }, /* */ \
  105. {OP_AND, 0, 0, PTP_CLASS_VMASK }, /* */ \
  106. {OP_OR, 0, 0, PTP_CLASS_VLAN }, /* */ \
  107. {OP_RETA, 0, 0, 0 }, /* */ \
  108. /*L50*/ {OP_JEQ, 0, 4, ETH_P_1588 }, /* f goto L61 */ \
  109. {OP_LDH, 0, 0, ETH_HLEN }, /* */ \
  110. {OP_AND, 0, 0, PTP_CLASS_VMASK }, /* */ \
  111. {OP_OR, 0, 0, PTP_CLASS_L2 }, /* */ \
  112. {OP_RETA, 0, 0, 0 }, /* */ \
  113. /*L6x*/ {OP_RETK, 0, 0, PTP_CLASS_NONE },
  114. #endif