ptp_classifier.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* PTP classifier
  3. */
  4. /* The below program is the bpf_asm (tools/net/) representation of
  5. * the opcode array in the ptp_filter structure.
  6. *
  7. * For convenience, this can easily be altered and reviewed with
  8. * bpf_asm and bpf_dbg, e.g. `./bpf_asm -c prog` where prog is a
  9. * simple file containing the below program:
  10. *
  11. * ldh [12] ; load ethertype
  12. *
  13. * ; PTP over UDP over IPv4 over Ethernet
  14. * test_ipv4:
  15. * jneq #0x800, test_ipv6 ; ETH_P_IP ?
  16. * ldb [23] ; load proto
  17. * jneq #17, drop_ipv4 ; IPPROTO_UDP ?
  18. * ldh [20] ; load frag offset field
  19. * jset #0x1fff, drop_ipv4 ; don't allow fragments
  20. * ldxb 4*([14]&0xf) ; load IP header len
  21. * ldh [x + 16] ; load UDP dst port
  22. * jneq #319, drop_ipv4 ; is port PTP_EV_PORT ?
  23. * ldh [x + 22] ; load payload
  24. * and #0xf ; mask PTP_CLASS_VMASK
  25. * or #0x10 ; PTP_CLASS_IPV4
  26. * ret a ; return PTP class
  27. * drop_ipv4: ret #0x0 ; PTP_CLASS_NONE
  28. *
  29. * ; PTP over UDP over IPv6 over Ethernet
  30. * test_ipv6:
  31. * jneq #0x86dd, test_8021q ; ETH_P_IPV6 ?
  32. * ldb [20] ; load proto
  33. * jneq #17, drop_ipv6 ; IPPROTO_UDP ?
  34. * ldh [56] ; load UDP dst port
  35. * jneq #319, drop_ipv6 ; is port PTP_EV_PORT ?
  36. * ldh [62] ; load payload
  37. * and #0xf ; mask PTP_CLASS_VMASK
  38. * or #0x20 ; PTP_CLASS_IPV6
  39. * ret a ; return PTP class
  40. * drop_ipv6: ret #0x0 ; PTP_CLASS_NONE
  41. *
  42. * ; PTP over 802.1Q over Ethernet
  43. * test_8021q:
  44. * jneq #0x8100, test_ieee1588 ; ETH_P_8021Q ?
  45. * ldh [16] ; load inner type
  46. * jneq #0x88f7, test_8021q_ipv4 ; ETH_P_1588 ?
  47. * ldb [18] ; load payload
  48. * and #0x8 ; as we don't have ports here, test
  49. * jneq #0x0, drop_ieee1588 ; for PTP_GEN_BIT and drop these
  50. * ldh [18] ; reload payload
  51. * and #0xf ; mask PTP_CLASS_VMASK
  52. * or #0xc0 ; PTP_CLASS_VLAN|PTP_CLASS_L2
  53. * ret a ; return PTP class
  54. *
  55. * ; PTP over UDP over IPv4 over 802.1Q over Ethernet
  56. * test_8021q_ipv4:
  57. * jneq #0x800, test_8021q_ipv6 ; ETH_P_IP ?
  58. * ldb [27] ; load proto
  59. * jneq #17, drop_8021q_ipv4 ; IPPROTO_UDP ?
  60. * ldh [24] ; load frag offset field
  61. * jset #0x1fff, drop_8021q_ipv4; don't allow fragments
  62. * ldxb 4*([18]&0xf) ; load IP header len
  63. * ldh [x + 20] ; load UDP dst port
  64. * jneq #319, drop_8021q_ipv4 ; is port PTP_EV_PORT ?
  65. * ldh [x + 26] ; load payload
  66. * and #0xf ; mask PTP_CLASS_VMASK
  67. * or #0x90 ; PTP_CLASS_VLAN|PTP_CLASS_IPV4
  68. * ret a ; return PTP class
  69. * drop_8021q_ipv4: ret #0x0 ; PTP_CLASS_NONE
  70. *
  71. * ; PTP over UDP over IPv6 over 802.1Q over Ethernet
  72. * test_8021q_ipv6:
  73. * jneq #0x86dd, drop_8021q_ipv6 ; ETH_P_IPV6 ?
  74. * ldb [24] ; load proto
  75. * jneq #17, drop_8021q_ipv6 ; IPPROTO_UDP ?
  76. * ldh [60] ; load UDP dst port
  77. * jneq #319, drop_8021q_ipv6 ; is port PTP_EV_PORT ?
  78. * ldh [66] ; load payload
  79. * and #0xf ; mask PTP_CLASS_VMASK
  80. * or #0xa0 ; PTP_CLASS_VLAN|PTP_CLASS_IPV6
  81. * ret a ; return PTP class
  82. * drop_8021q_ipv6: ret #0x0 ; PTP_CLASS_NONE
  83. *
  84. * ; PTP over Ethernet
  85. * test_ieee1588:
  86. * jneq #0x88f7, drop_ieee1588 ; ETH_P_1588 ?
  87. * ldb [14] ; load payload
  88. * and #0x8 ; as we don't have ports here, test
  89. * jneq #0x0, drop_ieee1588 ; for PTP_GEN_BIT and drop these
  90. * ldh [14] ; reload payload
  91. * and #0xf ; mask PTP_CLASS_VMASK
  92. * or #0x40 ; PTP_CLASS_L2
  93. * ret a ; return PTP class
  94. * drop_ieee1588: ret #0x0 ; PTP_CLASS_NONE
  95. */
  96. #include <linux/skbuff.h>
  97. #include <linux/filter.h>
  98. #include <linux/ptp_classify.h>
  99. static struct bpf_prog *ptp_insns __read_mostly;
  100. unsigned int ptp_classify_raw(const struct sk_buff *skb)
  101. {
  102. return bpf_prog_run(ptp_insns, skb);
  103. }
  104. EXPORT_SYMBOL_GPL(ptp_classify_raw);
  105. struct ptp_header *ptp_parse_header(struct sk_buff *skb, unsigned int type)
  106. {
  107. u8 *ptr = skb_mac_header(skb);
  108. if (type & PTP_CLASS_VLAN)
  109. ptr += VLAN_HLEN;
  110. switch (type & PTP_CLASS_PMASK) {
  111. case PTP_CLASS_IPV4:
  112. ptr += IPV4_HLEN(ptr) + UDP_HLEN;
  113. break;
  114. case PTP_CLASS_IPV6:
  115. ptr += IP6_HLEN + UDP_HLEN;
  116. break;
  117. case PTP_CLASS_L2:
  118. break;
  119. default:
  120. return NULL;
  121. }
  122. ptr += ETH_HLEN;
  123. /* Ensure that the entire header is present in this packet. */
  124. if (ptr + sizeof(struct ptp_header) > skb->data + skb->len)
  125. return NULL;
  126. return (struct ptp_header *)ptr;
  127. }
  128. EXPORT_SYMBOL_GPL(ptp_parse_header);
  129. bool ptp_msg_is_sync(struct sk_buff *skb, unsigned int type)
  130. {
  131. struct ptp_header *hdr;
  132. hdr = ptp_parse_header(skb, type);
  133. if (!hdr)
  134. return false;
  135. return ptp_get_msgtype(hdr, type) == PTP_MSGTYPE_SYNC;
  136. }
  137. EXPORT_SYMBOL_GPL(ptp_msg_is_sync);
  138. void __init ptp_classifier_init(void)
  139. {
  140. static struct sock_filter ptp_filter[] __initdata = {
  141. { 0x28, 0, 0, 0x0000000c },
  142. { 0x15, 0, 12, 0x00000800 },
  143. { 0x30, 0, 0, 0x00000017 },
  144. { 0x15, 0, 9, 0x00000011 },
  145. { 0x28, 0, 0, 0x00000014 },
  146. { 0x45, 7, 0, 0x00001fff },
  147. { 0xb1, 0, 0, 0x0000000e },
  148. { 0x48, 0, 0, 0x00000010 },
  149. { 0x15, 0, 4, 0x0000013f },
  150. { 0x48, 0, 0, 0x00000016 },
  151. { 0x54, 0, 0, 0x0000000f },
  152. { 0x44, 0, 0, 0x00000010 },
  153. { 0x16, 0, 0, 0x00000000 },
  154. { 0x06, 0, 0, 0x00000000 },
  155. { 0x15, 0, 9, 0x000086dd },
  156. { 0x30, 0, 0, 0x00000014 },
  157. { 0x15, 0, 6, 0x00000011 },
  158. { 0x28, 0, 0, 0x00000038 },
  159. { 0x15, 0, 4, 0x0000013f },
  160. { 0x28, 0, 0, 0x0000003e },
  161. { 0x54, 0, 0, 0x0000000f },
  162. { 0x44, 0, 0, 0x00000020 },
  163. { 0x16, 0, 0, 0x00000000 },
  164. { 0x06, 0, 0, 0x00000000 },
  165. { 0x15, 0, 32, 0x00008100 },
  166. { 0x28, 0, 0, 0x00000010 },
  167. { 0x15, 0, 7, 0x000088f7 },
  168. { 0x30, 0, 0, 0x00000012 },
  169. { 0x54, 0, 0, 0x00000008 },
  170. { 0x15, 0, 35, 0x00000000 },
  171. { 0x28, 0, 0, 0x00000012 },
  172. { 0x54, 0, 0, 0x0000000f },
  173. { 0x44, 0, 0, 0x000000c0 },
  174. { 0x16, 0, 0, 0x00000000 },
  175. { 0x15, 0, 12, 0x00000800 },
  176. { 0x30, 0, 0, 0x0000001b },
  177. { 0x15, 0, 9, 0x00000011 },
  178. { 0x28, 0, 0, 0x00000018 },
  179. { 0x45, 7, 0, 0x00001fff },
  180. { 0xb1, 0, 0, 0x00000012 },
  181. { 0x48, 0, 0, 0x00000014 },
  182. { 0x15, 0, 4, 0x0000013f },
  183. { 0x48, 0, 0, 0x0000001a },
  184. { 0x54, 0, 0, 0x0000000f },
  185. { 0x44, 0, 0, 0x00000090 },
  186. { 0x16, 0, 0, 0x00000000 },
  187. { 0x06, 0, 0, 0x00000000 },
  188. { 0x15, 0, 8, 0x000086dd },
  189. { 0x30, 0, 0, 0x00000018 },
  190. { 0x15, 0, 6, 0x00000011 },
  191. { 0x28, 0, 0, 0x0000003c },
  192. { 0x15, 0, 4, 0x0000013f },
  193. { 0x28, 0, 0, 0x00000042 },
  194. { 0x54, 0, 0, 0x0000000f },
  195. { 0x44, 0, 0, 0x000000a0 },
  196. { 0x16, 0, 0, 0x00000000 },
  197. { 0x06, 0, 0, 0x00000000 },
  198. { 0x15, 0, 7, 0x000088f7 },
  199. { 0x30, 0, 0, 0x0000000e },
  200. { 0x54, 0, 0, 0x00000008 },
  201. { 0x15, 0, 4, 0x00000000 },
  202. { 0x28, 0, 0, 0x0000000e },
  203. { 0x54, 0, 0, 0x0000000f },
  204. { 0x44, 0, 0, 0x00000040 },
  205. { 0x16, 0, 0, 0x00000000 },
  206. { 0x06, 0, 0, 0x00000000 },
  207. };
  208. struct sock_fprog_kern ptp_prog;
  209. ptp_prog.len = ARRAY_SIZE(ptp_filter);
  210. ptp_prog.filter = ptp_filter;
  211. BUG_ON(bpf_prog_create(&ptp_insns, &ptp_prog));
  212. }