0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050
0051
0052
0053
0054
0055
0056
0057
0058
0059
0060
0061
0062
0063
0064
0065
0066
0067
0068
0069
0070
0071
0072
0073
0074
0075
0076
0077
0078
0079
0080
0081
0082
0083
0084
0085
0086
0087
0088
0089
0090
0091
0092
0093
0094
0095
0096
0097
0098 #include <linux/skbuff.h>
0099 #include <linux/filter.h>
0100 #include <linux/ptp_classify.h>
0101
0102 static struct bpf_prog *ptp_insns __read_mostly;
0103
0104 unsigned int ptp_classify_raw(const struct sk_buff *skb)
0105 {
0106 return bpf_prog_run(ptp_insns, skb);
0107 }
0108 EXPORT_SYMBOL_GPL(ptp_classify_raw);
0109
0110 struct ptp_header *ptp_parse_header(struct sk_buff *skb, unsigned int type)
0111 {
0112 u8 *ptr = skb_mac_header(skb);
0113
0114 if (type & PTP_CLASS_VLAN)
0115 ptr += VLAN_HLEN;
0116
0117 switch (type & PTP_CLASS_PMASK) {
0118 case PTP_CLASS_IPV4:
0119 ptr += IPV4_HLEN(ptr) + UDP_HLEN;
0120 break;
0121 case PTP_CLASS_IPV6:
0122 ptr += IP6_HLEN + UDP_HLEN;
0123 break;
0124 case PTP_CLASS_L2:
0125 break;
0126 default:
0127 return NULL;
0128 }
0129
0130 ptr += ETH_HLEN;
0131
0132
0133 if (ptr + sizeof(struct ptp_header) > skb->data + skb->len)
0134 return NULL;
0135
0136 return (struct ptp_header *)ptr;
0137 }
0138 EXPORT_SYMBOL_GPL(ptp_parse_header);
0139
0140 bool ptp_msg_is_sync(struct sk_buff *skb, unsigned int type)
0141 {
0142 struct ptp_header *hdr;
0143
0144 hdr = ptp_parse_header(skb, type);
0145 if (!hdr)
0146 return false;
0147
0148 return ptp_get_msgtype(hdr, type) == PTP_MSGTYPE_SYNC;
0149 }
0150 EXPORT_SYMBOL_GPL(ptp_msg_is_sync);
0151
0152 void __init ptp_classifier_init(void)
0153 {
0154 static struct sock_filter ptp_filter[] __initdata = {
0155 { 0x28, 0, 0, 0x0000000c },
0156 { 0x15, 0, 12, 0x00000800 },
0157 { 0x30, 0, 0, 0x00000017 },
0158 { 0x15, 0, 9, 0x00000011 },
0159 { 0x28, 0, 0, 0x00000014 },
0160 { 0x45, 7, 0, 0x00001fff },
0161 { 0xb1, 0, 0, 0x0000000e },
0162 { 0x48, 0, 0, 0x00000010 },
0163 { 0x15, 0, 4, 0x0000013f },
0164 { 0x48, 0, 0, 0x00000016 },
0165 { 0x54, 0, 0, 0x0000000f },
0166 { 0x44, 0, 0, 0x00000010 },
0167 { 0x16, 0, 0, 0x00000000 },
0168 { 0x06, 0, 0, 0x00000000 },
0169 { 0x15, 0, 9, 0x000086dd },
0170 { 0x30, 0, 0, 0x00000014 },
0171 { 0x15, 0, 6, 0x00000011 },
0172 { 0x28, 0, 0, 0x00000038 },
0173 { 0x15, 0, 4, 0x0000013f },
0174 { 0x28, 0, 0, 0x0000003e },
0175 { 0x54, 0, 0, 0x0000000f },
0176 { 0x44, 0, 0, 0x00000020 },
0177 { 0x16, 0, 0, 0x00000000 },
0178 { 0x06, 0, 0, 0x00000000 },
0179 { 0x15, 0, 32, 0x00008100 },
0180 { 0x28, 0, 0, 0x00000010 },
0181 { 0x15, 0, 7, 0x000088f7 },
0182 { 0x30, 0, 0, 0x00000012 },
0183 { 0x54, 0, 0, 0x00000008 },
0184 { 0x15, 0, 35, 0x00000000 },
0185 { 0x28, 0, 0, 0x00000012 },
0186 { 0x54, 0, 0, 0x0000000f },
0187 { 0x44, 0, 0, 0x000000c0 },
0188 { 0x16, 0, 0, 0x00000000 },
0189 { 0x15, 0, 12, 0x00000800 },
0190 { 0x30, 0, 0, 0x0000001b },
0191 { 0x15, 0, 9, 0x00000011 },
0192 { 0x28, 0, 0, 0x00000018 },
0193 { 0x45, 7, 0, 0x00001fff },
0194 { 0xb1, 0, 0, 0x00000012 },
0195 { 0x48, 0, 0, 0x00000014 },
0196 { 0x15, 0, 4, 0x0000013f },
0197 { 0x48, 0, 0, 0x0000001a },
0198 { 0x54, 0, 0, 0x0000000f },
0199 { 0x44, 0, 0, 0x00000090 },
0200 { 0x16, 0, 0, 0x00000000 },
0201 { 0x06, 0, 0, 0x00000000 },
0202 { 0x15, 0, 8, 0x000086dd },
0203 { 0x30, 0, 0, 0x00000018 },
0204 { 0x15, 0, 6, 0x00000011 },
0205 { 0x28, 0, 0, 0x0000003c },
0206 { 0x15, 0, 4, 0x0000013f },
0207 { 0x28, 0, 0, 0x00000042 },
0208 { 0x54, 0, 0, 0x0000000f },
0209 { 0x44, 0, 0, 0x000000a0 },
0210 { 0x16, 0, 0, 0x00000000 },
0211 { 0x06, 0, 0, 0x00000000 },
0212 { 0x15, 0, 7, 0x000088f7 },
0213 { 0x30, 0, 0, 0x0000000e },
0214 { 0x54, 0, 0, 0x00000008 },
0215 { 0x15, 0, 4, 0x00000000 },
0216 { 0x28, 0, 0, 0x0000000e },
0217 { 0x54, 0, 0, 0x0000000f },
0218 { 0x44, 0, 0, 0x00000040 },
0219 { 0x16, 0, 0, 0x00000000 },
0220 { 0x06, 0, 0, 0x00000000 },
0221 };
0222 struct sock_fprog_kern ptp_prog;
0223
0224 ptp_prog.len = ARRAY_SIZE(ptp_filter);
0225 ptp_prog.filter = ptp_filter;
0226
0227 BUG_ON(bpf_prog_create(&ptp_insns, &ptp_prog));
0228 }