162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only 262306a36Sopenharmony_ci/* PTP classifier 362306a36Sopenharmony_ci */ 462306a36Sopenharmony_ci 562306a36Sopenharmony_ci/* The below program is the bpf_asm (tools/net/) representation of 662306a36Sopenharmony_ci * the opcode array in the ptp_filter structure. 762306a36Sopenharmony_ci * 862306a36Sopenharmony_ci * For convenience, this can easily be altered and reviewed with 962306a36Sopenharmony_ci * bpf_asm and bpf_dbg, e.g. `./bpf_asm -c prog` where prog is a 1062306a36Sopenharmony_ci * simple file containing the below program: 1162306a36Sopenharmony_ci * 1262306a36Sopenharmony_ci * ldh [12] ; load ethertype 1362306a36Sopenharmony_ci * 1462306a36Sopenharmony_ci * ; PTP over UDP over IPv4 over Ethernet 1562306a36Sopenharmony_ci * test_ipv4: 1662306a36Sopenharmony_ci * jneq #0x800, test_ipv6 ; ETH_P_IP ? 1762306a36Sopenharmony_ci * ldb [23] ; load proto 1862306a36Sopenharmony_ci * jneq #17, drop_ipv4 ; IPPROTO_UDP ? 1962306a36Sopenharmony_ci * ldh [20] ; load frag offset field 2062306a36Sopenharmony_ci * jset #0x1fff, drop_ipv4 ; don't allow fragments 2162306a36Sopenharmony_ci * ldxb 4*([14]&0xf) ; load IP header len 2262306a36Sopenharmony_ci * ldh [x + 16] ; load UDP dst port 2362306a36Sopenharmony_ci * jneq #319, drop_ipv4 ; is port PTP_EV_PORT ? 2462306a36Sopenharmony_ci * ldh [x + 22] ; load payload 2562306a36Sopenharmony_ci * and #0xf ; mask PTP_CLASS_VMASK 2662306a36Sopenharmony_ci * or #0x10 ; PTP_CLASS_IPV4 2762306a36Sopenharmony_ci * ret a ; return PTP class 2862306a36Sopenharmony_ci * drop_ipv4: ret #0x0 ; PTP_CLASS_NONE 2962306a36Sopenharmony_ci * 3062306a36Sopenharmony_ci * ; PTP over UDP over IPv6 over Ethernet 3162306a36Sopenharmony_ci * test_ipv6: 3262306a36Sopenharmony_ci * jneq #0x86dd, test_8021q ; ETH_P_IPV6 ? 3362306a36Sopenharmony_ci * ldb [20] ; load proto 3462306a36Sopenharmony_ci * jneq #17, drop_ipv6 ; IPPROTO_UDP ? 3562306a36Sopenharmony_ci * ldh [56] ; load UDP dst port 3662306a36Sopenharmony_ci * jneq #319, drop_ipv6 ; is port PTP_EV_PORT ? 3762306a36Sopenharmony_ci * ldh [62] ; load payload 3862306a36Sopenharmony_ci * and #0xf ; mask PTP_CLASS_VMASK 3962306a36Sopenharmony_ci * or #0x20 ; PTP_CLASS_IPV6 4062306a36Sopenharmony_ci * ret a ; return PTP class 4162306a36Sopenharmony_ci * drop_ipv6: ret #0x0 ; PTP_CLASS_NONE 4262306a36Sopenharmony_ci * 4362306a36Sopenharmony_ci * ; PTP over 802.1Q over Ethernet 4462306a36Sopenharmony_ci * test_8021q: 4562306a36Sopenharmony_ci * jneq #0x8100, test_ieee1588 ; ETH_P_8021Q ? 4662306a36Sopenharmony_ci * ldh [16] ; load inner type 4762306a36Sopenharmony_ci * jneq #0x88f7, test_8021q_ipv4 ; ETH_P_1588 ? 4862306a36Sopenharmony_ci * ldb [18] ; load payload 4962306a36Sopenharmony_ci * and #0x8 ; as we don't have ports here, test 5062306a36Sopenharmony_ci * jneq #0x0, drop_ieee1588 ; for PTP_GEN_BIT and drop these 5162306a36Sopenharmony_ci * ldh [18] ; reload payload 5262306a36Sopenharmony_ci * and #0xf ; mask PTP_CLASS_VMASK 5362306a36Sopenharmony_ci * or #0xc0 ; PTP_CLASS_VLAN|PTP_CLASS_L2 5462306a36Sopenharmony_ci * ret a ; return PTP class 5562306a36Sopenharmony_ci * 5662306a36Sopenharmony_ci * ; PTP over UDP over IPv4 over 802.1Q over Ethernet 5762306a36Sopenharmony_ci * test_8021q_ipv4: 5862306a36Sopenharmony_ci * jneq #0x800, test_8021q_ipv6 ; ETH_P_IP ? 5962306a36Sopenharmony_ci * ldb [27] ; load proto 6062306a36Sopenharmony_ci * jneq #17, drop_8021q_ipv4 ; IPPROTO_UDP ? 6162306a36Sopenharmony_ci * ldh [24] ; load frag offset field 6262306a36Sopenharmony_ci * jset #0x1fff, drop_8021q_ipv4; don't allow fragments 6362306a36Sopenharmony_ci * ldxb 4*([18]&0xf) ; load IP header len 6462306a36Sopenharmony_ci * ldh [x + 20] ; load UDP dst port 6562306a36Sopenharmony_ci * jneq #319, drop_8021q_ipv4 ; is port PTP_EV_PORT ? 6662306a36Sopenharmony_ci * ldh [x + 26] ; load payload 6762306a36Sopenharmony_ci * and #0xf ; mask PTP_CLASS_VMASK 6862306a36Sopenharmony_ci * or #0x90 ; PTP_CLASS_VLAN|PTP_CLASS_IPV4 6962306a36Sopenharmony_ci * ret a ; return PTP class 7062306a36Sopenharmony_ci * drop_8021q_ipv4: ret #0x0 ; PTP_CLASS_NONE 7162306a36Sopenharmony_ci * 7262306a36Sopenharmony_ci * ; PTP over UDP over IPv6 over 802.1Q over Ethernet 7362306a36Sopenharmony_ci * test_8021q_ipv6: 7462306a36Sopenharmony_ci * jneq #0x86dd, drop_8021q_ipv6 ; ETH_P_IPV6 ? 7562306a36Sopenharmony_ci * ldb [24] ; load proto 7662306a36Sopenharmony_ci * jneq #17, drop_8021q_ipv6 ; IPPROTO_UDP ? 7762306a36Sopenharmony_ci * ldh [60] ; load UDP dst port 7862306a36Sopenharmony_ci * jneq #319, drop_8021q_ipv6 ; is port PTP_EV_PORT ? 7962306a36Sopenharmony_ci * ldh [66] ; load payload 8062306a36Sopenharmony_ci * and #0xf ; mask PTP_CLASS_VMASK 8162306a36Sopenharmony_ci * or #0xa0 ; PTP_CLASS_VLAN|PTP_CLASS_IPV6 8262306a36Sopenharmony_ci * ret a ; return PTP class 8362306a36Sopenharmony_ci * drop_8021q_ipv6: ret #0x0 ; PTP_CLASS_NONE 8462306a36Sopenharmony_ci * 8562306a36Sopenharmony_ci * ; PTP over Ethernet 8662306a36Sopenharmony_ci * test_ieee1588: 8762306a36Sopenharmony_ci * jneq #0x88f7, drop_ieee1588 ; ETH_P_1588 ? 8862306a36Sopenharmony_ci * ldb [14] ; load payload 8962306a36Sopenharmony_ci * and #0x8 ; as we don't have ports here, test 9062306a36Sopenharmony_ci * jneq #0x0, drop_ieee1588 ; for PTP_GEN_BIT and drop these 9162306a36Sopenharmony_ci * ldh [14] ; reload payload 9262306a36Sopenharmony_ci * and #0xf ; mask PTP_CLASS_VMASK 9362306a36Sopenharmony_ci * or #0x40 ; PTP_CLASS_L2 9462306a36Sopenharmony_ci * ret a ; return PTP class 9562306a36Sopenharmony_ci * drop_ieee1588: ret #0x0 ; PTP_CLASS_NONE 9662306a36Sopenharmony_ci */ 9762306a36Sopenharmony_ci 9862306a36Sopenharmony_ci#include <linux/skbuff.h> 9962306a36Sopenharmony_ci#include <linux/filter.h> 10062306a36Sopenharmony_ci#include <linux/ptp_classify.h> 10162306a36Sopenharmony_ci 10262306a36Sopenharmony_cistatic struct bpf_prog *ptp_insns __read_mostly; 10362306a36Sopenharmony_ci 10462306a36Sopenharmony_ciunsigned int ptp_classify_raw(const struct sk_buff *skb) 10562306a36Sopenharmony_ci{ 10662306a36Sopenharmony_ci return bpf_prog_run(ptp_insns, skb); 10762306a36Sopenharmony_ci} 10862306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(ptp_classify_raw); 10962306a36Sopenharmony_ci 11062306a36Sopenharmony_cistruct ptp_header *ptp_parse_header(struct sk_buff *skb, unsigned int type) 11162306a36Sopenharmony_ci{ 11262306a36Sopenharmony_ci u8 *ptr = skb_mac_header(skb); 11362306a36Sopenharmony_ci 11462306a36Sopenharmony_ci if (type & PTP_CLASS_VLAN) 11562306a36Sopenharmony_ci ptr += VLAN_HLEN; 11662306a36Sopenharmony_ci 11762306a36Sopenharmony_ci switch (type & PTP_CLASS_PMASK) { 11862306a36Sopenharmony_ci case PTP_CLASS_IPV4: 11962306a36Sopenharmony_ci ptr += IPV4_HLEN(ptr) + UDP_HLEN; 12062306a36Sopenharmony_ci break; 12162306a36Sopenharmony_ci case PTP_CLASS_IPV6: 12262306a36Sopenharmony_ci ptr += IP6_HLEN + UDP_HLEN; 12362306a36Sopenharmony_ci break; 12462306a36Sopenharmony_ci case PTP_CLASS_L2: 12562306a36Sopenharmony_ci break; 12662306a36Sopenharmony_ci default: 12762306a36Sopenharmony_ci return NULL; 12862306a36Sopenharmony_ci } 12962306a36Sopenharmony_ci 13062306a36Sopenharmony_ci ptr += ETH_HLEN; 13162306a36Sopenharmony_ci 13262306a36Sopenharmony_ci /* Ensure that the entire header is present in this packet. */ 13362306a36Sopenharmony_ci if (ptr + sizeof(struct ptp_header) > skb->data + skb->len) 13462306a36Sopenharmony_ci return NULL; 13562306a36Sopenharmony_ci 13662306a36Sopenharmony_ci return (struct ptp_header *)ptr; 13762306a36Sopenharmony_ci} 13862306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(ptp_parse_header); 13962306a36Sopenharmony_ci 14062306a36Sopenharmony_cibool ptp_msg_is_sync(struct sk_buff *skb, unsigned int type) 14162306a36Sopenharmony_ci{ 14262306a36Sopenharmony_ci struct ptp_header *hdr; 14362306a36Sopenharmony_ci 14462306a36Sopenharmony_ci hdr = ptp_parse_header(skb, type); 14562306a36Sopenharmony_ci if (!hdr) 14662306a36Sopenharmony_ci return false; 14762306a36Sopenharmony_ci 14862306a36Sopenharmony_ci return ptp_get_msgtype(hdr, type) == PTP_MSGTYPE_SYNC; 14962306a36Sopenharmony_ci} 15062306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(ptp_msg_is_sync); 15162306a36Sopenharmony_ci 15262306a36Sopenharmony_civoid __init ptp_classifier_init(void) 15362306a36Sopenharmony_ci{ 15462306a36Sopenharmony_ci static struct sock_filter ptp_filter[] __initdata = { 15562306a36Sopenharmony_ci { 0x28, 0, 0, 0x0000000c }, 15662306a36Sopenharmony_ci { 0x15, 0, 12, 0x00000800 }, 15762306a36Sopenharmony_ci { 0x30, 0, 0, 0x00000017 }, 15862306a36Sopenharmony_ci { 0x15, 0, 9, 0x00000011 }, 15962306a36Sopenharmony_ci { 0x28, 0, 0, 0x00000014 }, 16062306a36Sopenharmony_ci { 0x45, 7, 0, 0x00001fff }, 16162306a36Sopenharmony_ci { 0xb1, 0, 0, 0x0000000e }, 16262306a36Sopenharmony_ci { 0x48, 0, 0, 0x00000010 }, 16362306a36Sopenharmony_ci { 0x15, 0, 4, 0x0000013f }, 16462306a36Sopenharmony_ci { 0x48, 0, 0, 0x00000016 }, 16562306a36Sopenharmony_ci { 0x54, 0, 0, 0x0000000f }, 16662306a36Sopenharmony_ci { 0x44, 0, 0, 0x00000010 }, 16762306a36Sopenharmony_ci { 0x16, 0, 0, 0x00000000 }, 16862306a36Sopenharmony_ci { 0x06, 0, 0, 0x00000000 }, 16962306a36Sopenharmony_ci { 0x15, 0, 9, 0x000086dd }, 17062306a36Sopenharmony_ci { 0x30, 0, 0, 0x00000014 }, 17162306a36Sopenharmony_ci { 0x15, 0, 6, 0x00000011 }, 17262306a36Sopenharmony_ci { 0x28, 0, 0, 0x00000038 }, 17362306a36Sopenharmony_ci { 0x15, 0, 4, 0x0000013f }, 17462306a36Sopenharmony_ci { 0x28, 0, 0, 0x0000003e }, 17562306a36Sopenharmony_ci { 0x54, 0, 0, 0x0000000f }, 17662306a36Sopenharmony_ci { 0x44, 0, 0, 0x00000020 }, 17762306a36Sopenharmony_ci { 0x16, 0, 0, 0x00000000 }, 17862306a36Sopenharmony_ci { 0x06, 0, 0, 0x00000000 }, 17962306a36Sopenharmony_ci { 0x15, 0, 32, 0x00008100 }, 18062306a36Sopenharmony_ci { 0x28, 0, 0, 0x00000010 }, 18162306a36Sopenharmony_ci { 0x15, 0, 7, 0x000088f7 }, 18262306a36Sopenharmony_ci { 0x30, 0, 0, 0x00000012 }, 18362306a36Sopenharmony_ci { 0x54, 0, 0, 0x00000008 }, 18462306a36Sopenharmony_ci { 0x15, 0, 35, 0x00000000 }, 18562306a36Sopenharmony_ci { 0x28, 0, 0, 0x00000012 }, 18662306a36Sopenharmony_ci { 0x54, 0, 0, 0x0000000f }, 18762306a36Sopenharmony_ci { 0x44, 0, 0, 0x000000c0 }, 18862306a36Sopenharmony_ci { 0x16, 0, 0, 0x00000000 }, 18962306a36Sopenharmony_ci { 0x15, 0, 12, 0x00000800 }, 19062306a36Sopenharmony_ci { 0x30, 0, 0, 0x0000001b }, 19162306a36Sopenharmony_ci { 0x15, 0, 9, 0x00000011 }, 19262306a36Sopenharmony_ci { 0x28, 0, 0, 0x00000018 }, 19362306a36Sopenharmony_ci { 0x45, 7, 0, 0x00001fff }, 19462306a36Sopenharmony_ci { 0xb1, 0, 0, 0x00000012 }, 19562306a36Sopenharmony_ci { 0x48, 0, 0, 0x00000014 }, 19662306a36Sopenharmony_ci { 0x15, 0, 4, 0x0000013f }, 19762306a36Sopenharmony_ci { 0x48, 0, 0, 0x0000001a }, 19862306a36Sopenharmony_ci { 0x54, 0, 0, 0x0000000f }, 19962306a36Sopenharmony_ci { 0x44, 0, 0, 0x00000090 }, 20062306a36Sopenharmony_ci { 0x16, 0, 0, 0x00000000 }, 20162306a36Sopenharmony_ci { 0x06, 0, 0, 0x00000000 }, 20262306a36Sopenharmony_ci { 0x15, 0, 8, 0x000086dd }, 20362306a36Sopenharmony_ci { 0x30, 0, 0, 0x00000018 }, 20462306a36Sopenharmony_ci { 0x15, 0, 6, 0x00000011 }, 20562306a36Sopenharmony_ci { 0x28, 0, 0, 0x0000003c }, 20662306a36Sopenharmony_ci { 0x15, 0, 4, 0x0000013f }, 20762306a36Sopenharmony_ci { 0x28, 0, 0, 0x00000042 }, 20862306a36Sopenharmony_ci { 0x54, 0, 0, 0x0000000f }, 20962306a36Sopenharmony_ci { 0x44, 0, 0, 0x000000a0 }, 21062306a36Sopenharmony_ci { 0x16, 0, 0, 0x00000000 }, 21162306a36Sopenharmony_ci { 0x06, 0, 0, 0x00000000 }, 21262306a36Sopenharmony_ci { 0x15, 0, 7, 0x000088f7 }, 21362306a36Sopenharmony_ci { 0x30, 0, 0, 0x0000000e }, 21462306a36Sopenharmony_ci { 0x54, 0, 0, 0x00000008 }, 21562306a36Sopenharmony_ci { 0x15, 0, 4, 0x00000000 }, 21662306a36Sopenharmony_ci { 0x28, 0, 0, 0x0000000e }, 21762306a36Sopenharmony_ci { 0x54, 0, 0, 0x0000000f }, 21862306a36Sopenharmony_ci { 0x44, 0, 0, 0x00000040 }, 21962306a36Sopenharmony_ci { 0x16, 0, 0, 0x00000000 }, 22062306a36Sopenharmony_ci { 0x06, 0, 0, 0x00000000 }, 22162306a36Sopenharmony_ci }; 22262306a36Sopenharmony_ci struct sock_fprog_kern ptp_prog; 22362306a36Sopenharmony_ci 22462306a36Sopenharmony_ci ptp_prog.len = ARRAY_SIZE(ptp_filter); 22562306a36Sopenharmony_ci ptp_prog.filter = ptp_filter; 22662306a36Sopenharmony_ci 22762306a36Sopenharmony_ci BUG_ON(bpf_prog_create(&ptp_insns, &ptp_prog)); 22862306a36Sopenharmony_ci} 229