ptp_classifier.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /* PTP classifier
  2. *
  3. * This program is free software; you can redistribute it and/or
  4. * modify it under the terms of version 2 of the GNU General Public
  5. * License as published by the Free Software Foundation.
  6. *
  7. * This program is distributed in the hope that it will be useful, but
  8. * WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. * General Public License for more details.
  11. */
  12. /* The below program is the bpf_asm (tools/net/) representation of
  13. * the opcode array in the ptp_filter structure.
  14. *
  15. * For convenience, this can easily be altered and reviewed with
  16. * bpf_asm and bpf_dbg, e.g. `./bpf_asm -c prog` where prog is a
  17. * simple file containing the below program:
  18. *
  19. * ldh [12] ; load ethertype
  20. *
  21. * ; PTP over UDP over IPv4 over Ethernet
  22. * test_ipv4:
  23. * jneq #0x800, test_ipv6 ; ETH_P_IP ?
  24. * ldb [23] ; load proto
  25. * jneq #17, drop_ipv4 ; IPPROTO_UDP ?
  26. * ldh [20] ; load frag offset field
  27. * jset #0x1fff, drop_ipv4 ; don't allow fragments
  28. * ldxb 4*([14]&0xf) ; load IP header len
  29. * ldh [x + 16] ; load UDP dst port
  30. * jneq #319, drop_ipv4 ; is port PTP_EV_PORT ?
  31. * ldh [x + 22] ; load payload
  32. * and #0xf ; mask PTP_CLASS_VMASK
  33. * or #0x10 ; PTP_CLASS_IPV4
  34. * ret a ; return PTP class
  35. * drop_ipv4: ret #0x0 ; PTP_CLASS_NONE
  36. *
  37. * ; PTP over UDP over IPv6 over Ethernet
  38. * test_ipv6:
  39. * jneq #0x86dd, test_8021q ; ETH_P_IPV6 ?
  40. * ldb [20] ; load proto
  41. * jneq #17, drop_ipv6 ; IPPROTO_UDP ?
  42. * ldh [56] ; load UDP dst port
  43. * jneq #319, drop_ipv6 ; is port PTP_EV_PORT ?
  44. * ldh [62] ; load payload
  45. * and #0xf ; mask PTP_CLASS_VMASK
  46. * or #0x20 ; PTP_CLASS_IPV6
  47. * ret a ; return PTP class
  48. * drop_ipv6: ret #0x0 ; PTP_CLASS_NONE
  49. *
  50. * ; PTP over 802.1Q over Ethernet
  51. * test_8021q:
  52. * jneq #0x8100, test_ieee1588 ; ETH_P_8021Q ?
  53. * ldh [16] ; load inner type
  54. * jneq #0x88f7, test_8021q_ipv4 ; ETH_P_1588 ?
  55. * ldb [18] ; load payload
  56. * and #0x8 ; as we don't have ports here, test
  57. * jneq #0x0, drop_ieee1588 ; for PTP_GEN_BIT and drop these
  58. * ldh [18] ; reload payload
  59. * and #0xf ; mask PTP_CLASS_VMASK
  60. * or #0xc0 ; PTP_CLASS_VLAN|PTP_CLASS_L2
  61. * ret a ; return PTP class
  62. *
  63. * ; PTP over UDP over IPv4 over 802.1Q over Ethernet
  64. * test_8021q_ipv4:
  65. * jneq #0x800, test_8021q_ipv6 ; ETH_P_IP ?
  66. * ldb [27] ; load proto
  67. * jneq #17, drop_8021q_ipv4 ; IPPROTO_UDP ?
  68. * ldh [24] ; load frag offset field
  69. * jset #0x1fff, drop_8021q_ipv4; don't allow fragments
  70. * ldxb 4*([18]&0xf) ; load IP header len
  71. * ldh [x + 20] ; load UDP dst port
  72. * jneq #319, drop_8021q_ipv4 ; is port PTP_EV_PORT ?
  73. * ldh [x + 26] ; load payload
  74. * and #0xf ; mask PTP_CLASS_VMASK
  75. * or #0x90 ; PTP_CLASS_VLAN|PTP_CLASS_IPV4
  76. * ret a ; return PTP class
  77. * drop_8021q_ipv4: ret #0x0 ; PTP_CLASS_NONE
  78. *
  79. * ; PTP over UDP over IPv6 over 802.1Q over Ethernet
  80. * test_8021q_ipv6:
  81. * jneq #0x86dd, drop_8021q_ipv6 ; ETH_P_IPV6 ?
  82. * ldb [24] ; load proto
  83. * jneq #17, drop_8021q_ipv6 ; IPPROTO_UDP ?
  84. * ldh [60] ; load UDP dst port
  85. * jneq #319, drop_8021q_ipv6 ; is port PTP_EV_PORT ?
  86. * ldh [66] ; load payload
  87. * and #0xf ; mask PTP_CLASS_VMASK
  88. * or #0xa0 ; PTP_CLASS_VLAN|PTP_CLASS_IPV6
  89. * ret a ; return PTP class
  90. * drop_8021q_ipv6: ret #0x0 ; PTP_CLASS_NONE
  91. *
  92. * ; PTP over Ethernet
  93. * test_ieee1588:
  94. * jneq #0x88f7, drop_ieee1588 ; ETH_P_1588 ?
  95. * ldb [14] ; load payload
  96. * and #0x8 ; as we don't have ports here, test
  97. * jneq #0x0, drop_ieee1588 ; for PTP_GEN_BIT and drop these
  98. * ldh [14] ; reload payload
  99. * and #0xf ; mask PTP_CLASS_VMASK
  100. * or #0x40 ; PTP_CLASS_L2
  101. * ret a ; return PTP class
  102. * drop_ieee1588: ret #0x0 ; PTP_CLASS_NONE
  103. */
  104. #include <linux/skbuff.h>
  105. #include <linux/filter.h>
  106. #include <linux/ptp_classify.h>
  107. static struct bpf_prog *ptp_insns __read_mostly;
  108. unsigned int ptp_classify_raw(const struct sk_buff *skb)
  109. {
  110. return BPF_PROG_RUN(ptp_insns, skb);
  111. }
  112. EXPORT_SYMBOL_GPL(ptp_classify_raw);
  113. void __init ptp_classifier_init(void)
  114. {
  115. static struct sock_filter ptp_filter[] __initdata = {
  116. { 0x28, 0, 0, 0x0000000c },
  117. { 0x15, 0, 12, 0x00000800 },
  118. { 0x30, 0, 0, 0x00000017 },
  119. { 0x15, 0, 9, 0x00000011 },
  120. { 0x28, 0, 0, 0x00000014 },
  121. { 0x45, 7, 0, 0x00001fff },
  122. { 0xb1, 0, 0, 0x0000000e },
  123. { 0x48, 0, 0, 0x00000010 },
  124. { 0x15, 0, 4, 0x0000013f },
  125. { 0x48, 0, 0, 0x00000016 },
  126. { 0x54, 0, 0, 0x0000000f },
  127. { 0x44, 0, 0, 0x00000010 },
  128. { 0x16, 0, 0, 0x00000000 },
  129. { 0x06, 0, 0, 0x00000000 },
  130. { 0x15, 0, 9, 0x000086dd },
  131. { 0x30, 0, 0, 0x00000014 },
  132. { 0x15, 0, 6, 0x00000011 },
  133. { 0x28, 0, 0, 0x00000038 },
  134. { 0x15, 0, 4, 0x0000013f },
  135. { 0x28, 0, 0, 0x0000003e },
  136. { 0x54, 0, 0, 0x0000000f },
  137. { 0x44, 0, 0, 0x00000020 },
  138. { 0x16, 0, 0, 0x00000000 },
  139. { 0x06, 0, 0, 0x00000000 },
  140. { 0x15, 0, 32, 0x00008100 },
  141. { 0x28, 0, 0, 0x00000010 },
  142. { 0x15, 0, 7, 0x000088f7 },
  143. { 0x30, 0, 0, 0x00000012 },
  144. { 0x54, 0, 0, 0x00000008 },
  145. { 0x15, 0, 35, 0x00000000 },
  146. { 0x28, 0, 0, 0x00000012 },
  147. { 0x54, 0, 0, 0x0000000f },
  148. { 0x44, 0, 0, 0x000000c0 },
  149. { 0x16, 0, 0, 0x00000000 },
  150. { 0x15, 0, 12, 0x00000800 },
  151. { 0x30, 0, 0, 0x0000001b },
  152. { 0x15, 0, 9, 0x00000011 },
  153. { 0x28, 0, 0, 0x00000018 },
  154. { 0x45, 7, 0, 0x00001fff },
  155. { 0xb1, 0, 0, 0x00000012 },
  156. { 0x48, 0, 0, 0x00000014 },
  157. { 0x15, 0, 4, 0x0000013f },
  158. { 0x48, 0, 0, 0x0000001a },
  159. { 0x54, 0, 0, 0x0000000f },
  160. { 0x44, 0, 0, 0x00000090 },
  161. { 0x16, 0, 0, 0x00000000 },
  162. { 0x06, 0, 0, 0x00000000 },
  163. { 0x15, 0, 8, 0x000086dd },
  164. { 0x30, 0, 0, 0x00000018 },
  165. { 0x15, 0, 6, 0x00000011 },
  166. { 0x28, 0, 0, 0x0000003c },
  167. { 0x15, 0, 4, 0x0000013f },
  168. { 0x28, 0, 0, 0x00000042 },
  169. { 0x54, 0, 0, 0x0000000f },
  170. { 0x44, 0, 0, 0x000000a0 },
  171. { 0x16, 0, 0, 0x00000000 },
  172. { 0x06, 0, 0, 0x00000000 },
  173. { 0x15, 0, 7, 0x000088f7 },
  174. { 0x30, 0, 0, 0x0000000e },
  175. { 0x54, 0, 0, 0x00000008 },
  176. { 0x15, 0, 4, 0x00000000 },
  177. { 0x28, 0, 0, 0x0000000e },
  178. { 0x54, 0, 0, 0x0000000f },
  179. { 0x44, 0, 0, 0x00000040 },
  180. { 0x16, 0, 0, 0x00000000 },
  181. { 0x06, 0, 0, 0x00000000 },
  182. };
  183. struct sock_fprog_kern ptp_prog = {
  184. .len = ARRAY_SIZE(ptp_filter), .filter = ptp_filter,
  185. };
  186. BUG_ON(bpf_prog_create(&ptp_insns, &ptp_prog));
  187. }