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