Thomas Gleixner | 457c899 | 2019-05-19 13:08:55 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Jiri Pirko | fbff949 | 2015-05-12 14:56:15 +0200 | [diff] [blame] | 2 | #include <linux/kernel.h> |
Eric Dumazet | 0744dd0 | 2011-11-28 05:22:18 +0000 | [diff] [blame] | 3 | #include <linux/skbuff.h> |
Jesper Dangaard Brouer | c452ed7 | 2012-01-24 16:03:33 -0500 | [diff] [blame] | 4 | #include <linux/export.h> |
Eric Dumazet | 0744dd0 | 2011-11-28 05:22:18 +0000 | [diff] [blame] | 5 | #include <linux/ip.h> |
| 6 | #include <linux/ipv6.h> |
| 7 | #include <linux/if_vlan.h> |
Jakub Kicinski | b645941 | 2021-12-28 16:49:13 -0800 | [diff] [blame] | 8 | #include <linux/filter.h> |
John Crispin | 43e6652 | 2017-08-09 14:41:19 +0200 | [diff] [blame] | 9 | #include <net/dsa.h> |
Simon Horman | a38402b | 2017-10-02 10:41:16 +0200 | [diff] [blame] | 10 | #include <net/dst_metadata.h> |
Eric Dumazet | 0744dd0 | 2011-11-28 05:22:18 +0000 | [diff] [blame] | 11 | #include <net/ip.h> |
Eric Dumazet | ddbe503 | 2012-07-18 08:11:12 +0000 | [diff] [blame] | 12 | #include <net/ipv6.h> |
Gao Feng | ab10dcc | 2016-08-09 12:38:24 +0800 | [diff] [blame] | 13 | #include <net/gre.h> |
| 14 | #include <net/pptp.h> |
Jon Maloy | 8d6e79d | 2017-11-08 09:59:26 +0100 | [diff] [blame] | 15 | #include <net/tipc.h> |
Daniel Borkmann | f77668d | 2013-03-19 06:39:30 +0000 | [diff] [blame] | 16 | #include <linux/igmp.h> |
| 17 | #include <linux/icmp.h> |
| 18 | #include <linux/sctp.h> |
| 19 | #include <linux/dccp.h> |
Eric Dumazet | 0744dd0 | 2011-11-28 05:22:18 +0000 | [diff] [blame] | 20 | #include <linux/if_tunnel.h> |
| 21 | #include <linux/if_pppox.h> |
| 22 | #include <linux/ppp_defs.h> |
Jiri Pirko | 06635a3 | 2015-05-12 14:56:16 +0200 | [diff] [blame] | 23 | #include <linux/stddef.h> |
Jiri Pirko | 67a900c | 2015-05-12 14:56:19 +0200 | [diff] [blame] | 24 | #include <linux/if_ether.h> |
Kurt Kanzenbach | bf08824 | 2022-02-28 20:58:56 +0100 | [diff] [blame] | 25 | #include <linux/if_hsr.h> |
Tom Herbert | b3baa0f | 2015-06-04 09:16:46 -0700 | [diff] [blame] | 26 | #include <linux/mpls.h> |
Jiri Pirko | ac4bb5d | 2017-05-23 18:40:44 +0200 | [diff] [blame] | 27 | #include <linux/tcp.h> |
Eran Ben Elisha | 4f1cc51 | 2021-01-12 21:07:13 +0200 | [diff] [blame] | 28 | #include <linux/ptp_classify.h> |
Jiri Pirko | 1bd758e | 2015-05-12 14:56:07 +0200 | [diff] [blame] | 29 | #include <net/flow_dissector.h> |
Alexander Duyck | 56193d1 | 2014-09-05 19:20:26 -0400 | [diff] [blame] | 30 | #include <scsi/fc/fc_fcoe.h> |
Sven Eckelmann | 5b0890a | 2017-12-21 10:17:42 +0100 | [diff] [blame] | 31 | #include <uapi/linux/batadv_packet.h> |
Petar Penkov | d58e468 | 2018-09-14 07:46:18 -0700 | [diff] [blame] | 32 | #include <linux/bpf.h> |
Paul Blakey | 75a5675 | 2019-07-09 10:30:49 +0300 | [diff] [blame] | 33 | #if IS_ENABLED(CONFIG_NF_CONNTRACK) |
| 34 | #include <net/netfilter/nf_conntrack_core.h> |
| 35 | #include <net/netfilter/nf_conntrack_labels.h> |
| 36 | #endif |
Jakub Sitnicki | a3fd7ce | 2020-05-31 10:28:36 +0200 | [diff] [blame] | 37 | #include <linux/bpf-netns.h> |
Petar Penkov | d58e468 | 2018-09-14 07:46:18 -0700 | [diff] [blame] | 38 | |
David S. Miller | 20a17bf | 2015-09-01 21:19:17 -0700 | [diff] [blame] | 39 | static void dissector_set_key(struct flow_dissector *flow_dissector, |
| 40 | enum flow_dissector_key_id key_id) |
Jiri Pirko | fbff949 | 2015-05-12 14:56:15 +0200 | [diff] [blame] | 41 | { |
| 42 | flow_dissector->used_keys |= (1 << key_id); |
| 43 | } |
| 44 | |
Jiri Pirko | fbff949 | 2015-05-12 14:56:15 +0200 | [diff] [blame] | 45 | void skb_flow_dissector_init(struct flow_dissector *flow_dissector, |
| 46 | const struct flow_dissector_key *key, |
| 47 | unsigned int key_count) |
| 48 | { |
| 49 | unsigned int i; |
| 50 | |
| 51 | memset(flow_dissector, 0, sizeof(*flow_dissector)); |
| 52 | |
| 53 | for (i = 0; i < key_count; i++, key++) { |
Wang Qing | 75a5fb0 | 2020-11-06 16:11:49 +0800 | [diff] [blame] | 54 | /* User should make sure that every key target offset is within |
Jiri Pirko | fbff949 | 2015-05-12 14:56:15 +0200 | [diff] [blame] | 55 | * boundaries of unsigned short. |
| 56 | */ |
| 57 | BUG_ON(key->offset > USHRT_MAX); |
David S. Miller | 20a17bf | 2015-09-01 21:19:17 -0700 | [diff] [blame] | 58 | BUG_ON(dissector_uses_key(flow_dissector, |
| 59 | key->key_id)); |
Jiri Pirko | fbff949 | 2015-05-12 14:56:15 +0200 | [diff] [blame] | 60 | |
David S. Miller | 20a17bf | 2015-09-01 21:19:17 -0700 | [diff] [blame] | 61 | dissector_set_key(flow_dissector, key->key_id); |
Jiri Pirko | fbff949 | 2015-05-12 14:56:15 +0200 | [diff] [blame] | 62 | flow_dissector->offset[key->key_id] = key->offset; |
| 63 | } |
| 64 | |
Tom Herbert | 42aecaa | 2015-06-04 09:16:39 -0700 | [diff] [blame] | 65 | /* Ensure that the dissector always includes control and basic key. |
| 66 | * That way we are able to avoid handling lack of these in fast path. |
Jiri Pirko | fbff949 | 2015-05-12 14:56:15 +0200 | [diff] [blame] | 67 | */ |
David S. Miller | 20a17bf | 2015-09-01 21:19:17 -0700 | [diff] [blame] | 68 | BUG_ON(!dissector_uses_key(flow_dissector, |
| 69 | FLOW_DISSECTOR_KEY_CONTROL)); |
| 70 | BUG_ON(!dissector_uses_key(flow_dissector, |
| 71 | FLOW_DISSECTOR_KEY_BASIC)); |
Jiri Pirko | fbff949 | 2015-05-12 14:56:15 +0200 | [diff] [blame] | 72 | } |
| 73 | EXPORT_SYMBOL(skb_flow_dissector_init); |
| 74 | |
Jakub Sitnicki | b27f7bb | 2020-05-31 10:28:37 +0200 | [diff] [blame] | 75 | #ifdef CONFIG_BPF_SYSCALL |
Jakub Sitnicki | 3b70169 | 2020-06-25 16:13:54 +0200 | [diff] [blame] | 76 | int flow_dissector_bpf_prog_attach_check(struct net *net, |
| 77 | struct bpf_prog *prog) |
Petar Penkov | d58e468 | 2018-09-14 07:46:18 -0700 | [diff] [blame] | 78 | { |
Jakub Sitnicki | a3fd7ce | 2020-05-31 10:28:36 +0200 | [diff] [blame] | 79 | enum netns_bpf_attach_type type = NETNS_BPF_FLOW_DISSECTOR; |
Stanislav Fomichev | a11c397 | 2019-10-07 09:21:02 -0700 | [diff] [blame] | 80 | |
| 81 | if (net == &init_net) { |
| 82 | /* BPF flow dissector in the root namespace overrides |
| 83 | * any per-net-namespace one. When attaching to root, |
| 84 | * make sure we don't have any BPF program attached |
| 85 | * to the non-root namespaces. |
| 86 | */ |
| 87 | struct net *ns; |
| 88 | |
| 89 | for_each_net(ns) { |
Jakub Sitnicki | 719b78a5 | 2019-10-11 10:29:45 +0200 | [diff] [blame] | 90 | if (ns == &init_net) |
| 91 | continue; |
Jakub Sitnicki | 695c121 | 2020-06-25 16:13:55 +0200 | [diff] [blame] | 92 | if (rcu_access_pointer(ns->bpf.run_array[type])) |
Jakub Sitnicki | 171526f | 2020-05-31 10:28:35 +0200 | [diff] [blame] | 93 | return -EEXIST; |
Stanislav Fomichev | a11c397 | 2019-10-07 09:21:02 -0700 | [diff] [blame] | 94 | } |
| 95 | } else { |
| 96 | /* Make sure root flow dissector is not attached |
| 97 | * when attaching to the non-root namespace. |
| 98 | */ |
Jakub Sitnicki | 695c121 | 2020-06-25 16:13:55 +0200 | [diff] [blame] | 99 | if (rcu_access_pointer(init_net.bpf.run_array[type])) |
Jakub Sitnicki | 171526f | 2020-05-31 10:28:35 +0200 | [diff] [blame] | 100 | return -EEXIST; |
Stanislav Fomichev | a11c397 | 2019-10-07 09:21:02 -0700 | [diff] [blame] | 101 | } |
| 102 | |
Jakub Sitnicki | 171526f | 2020-05-31 10:28:35 +0200 | [diff] [blame] | 103 | return 0; |
| 104 | } |
Jakub Sitnicki | b27f7bb | 2020-05-31 10:28:37 +0200 | [diff] [blame] | 105 | #endif /* CONFIG_BPF_SYSCALL */ |
Jakub Sitnicki | 5cf6592 | 2020-05-21 10:34:35 +0200 | [diff] [blame] | 106 | |
Simon Horman | 972d387 | 2016-12-07 13:48:27 +0100 | [diff] [blame] | 107 | /** |
WANG Cong | 6451b3f | 2014-08-25 17:03:46 -0700 | [diff] [blame] | 108 | * __skb_flow_get_ports - extract the upper layer ports and return them |
| 109 | * @skb: sk_buff to extract the ports from |
Nikolay Aleksandrov | 357afe9 | 2013-10-02 13:39:24 +0200 | [diff] [blame] | 110 | * @thoff: transport header offset |
| 111 | * @ip_proto: protocol for which to get port offset |
WANG Cong | 6451b3f | 2014-08-25 17:03:46 -0700 | [diff] [blame] | 112 | * @data: raw buffer pointer to the packet, if NULL use skb->data |
| 113 | * @hlen: packet header length, if @data is NULL use skb_headlen(skb) |
Nikolay Aleksandrov | 357afe9 | 2013-10-02 13:39:24 +0200 | [diff] [blame] | 114 | * |
| 115 | * The function will try to retrieve the ports at offset thoff + poff where poff |
| 116 | * is the protocol port offset returned from proto_ports_offset |
| 117 | */ |
David S. Miller | 690e36e | 2014-08-23 12:13:41 -0700 | [diff] [blame] | 118 | __be32 __skb_flow_get_ports(const struct sk_buff *skb, int thoff, u8 ip_proto, |
Alexander Lobakin | f96533c | 2021-03-14 11:11:23 +0000 | [diff] [blame] | 119 | const void *data, int hlen) |
Nikolay Aleksandrov | 357afe9 | 2013-10-02 13:39:24 +0200 | [diff] [blame] | 120 | { |
| 121 | int poff = proto_ports_offset(ip_proto); |
| 122 | |
David S. Miller | 690e36e | 2014-08-23 12:13:41 -0700 | [diff] [blame] | 123 | if (!data) { |
| 124 | data = skb->data; |
| 125 | hlen = skb_headlen(skb); |
| 126 | } |
| 127 | |
Nikolay Aleksandrov | 357afe9 | 2013-10-02 13:39:24 +0200 | [diff] [blame] | 128 | if (poff >= 0) { |
| 129 | __be32 *ports, _ports; |
| 130 | |
David S. Miller | 690e36e | 2014-08-23 12:13:41 -0700 | [diff] [blame] | 131 | ports = __skb_header_pointer(skb, thoff + poff, |
| 132 | sizeof(_ports), data, hlen, &_ports); |
Nikolay Aleksandrov | 357afe9 | 2013-10-02 13:39:24 +0200 | [diff] [blame] | 133 | if (ports) |
| 134 | return *ports; |
| 135 | } |
| 136 | |
| 137 | return 0; |
| 138 | } |
David S. Miller | 690e36e | 2014-08-23 12:13:41 -0700 | [diff] [blame] | 139 | EXPORT_SYMBOL(__skb_flow_get_ports); |
Nikolay Aleksandrov | 357afe9 | 2013-10-02 13:39:24 +0200 | [diff] [blame] | 140 | |
Matteo Croce | 5dec597 | 2019-10-29 14:50:52 +0100 | [diff] [blame] | 141 | static bool icmp_has_id(u8 type) |
| 142 | { |
| 143 | switch (type) { |
| 144 | case ICMP_ECHO: |
| 145 | case ICMP_ECHOREPLY: |
| 146 | case ICMP_TIMESTAMP: |
| 147 | case ICMP_TIMESTAMPREPLY: |
| 148 | case ICMPV6_ECHO_REQUEST: |
| 149 | case ICMPV6_ECHO_REPLY: |
| 150 | return true; |
| 151 | } |
| 152 | |
| 153 | return false; |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * skb_flow_get_icmp_tci - extract ICMP(6) Type, Code and Identifier fields |
| 158 | * @skb: sk_buff to extract from |
| 159 | * @key_icmp: struct flow_dissector_key_icmp to fill |
| 160 | * @data: raw buffer pointer to the packet |
Li RongQing | 6b3acfc | 2020-01-09 08:59:56 +0800 | [diff] [blame] | 161 | * @thoff: offset to extract at |
Matteo Croce | 5dec597 | 2019-10-29 14:50:52 +0100 | [diff] [blame] | 162 | * @hlen: packet header length |
| 163 | */ |
| 164 | void skb_flow_get_icmp_tci(const struct sk_buff *skb, |
| 165 | struct flow_dissector_key_icmp *key_icmp, |
Alexander Lobakin | f96533c | 2021-03-14 11:11:23 +0000 | [diff] [blame] | 166 | const void *data, int thoff, int hlen) |
Matteo Croce | 5dec597 | 2019-10-29 14:50:52 +0100 | [diff] [blame] | 167 | { |
| 168 | struct icmphdr *ih, _ih; |
| 169 | |
| 170 | ih = __skb_header_pointer(skb, thoff, sizeof(_ih), data, hlen, &_ih); |
| 171 | if (!ih) |
| 172 | return; |
| 173 | |
| 174 | key_icmp->type = ih->type; |
| 175 | key_icmp->code = ih->code; |
| 176 | |
| 177 | /* As we use 0 to signal that the Id field is not present, |
| 178 | * avoid confusion with packets without such field |
| 179 | */ |
| 180 | if (icmp_has_id(ih->type)) |
Alexander Lobakin | a25f822 | 2021-03-12 20:08:57 +0000 | [diff] [blame] | 181 | key_icmp->id = ih->un.echo.id ? ntohs(ih->un.echo.id) : 1; |
Matteo Croce | 5dec597 | 2019-10-29 14:50:52 +0100 | [diff] [blame] | 182 | else |
| 183 | key_icmp->id = 0; |
| 184 | } |
| 185 | EXPORT_SYMBOL(skb_flow_get_icmp_tci); |
| 186 | |
| 187 | /* If FLOW_DISSECTOR_KEY_ICMP is set, dissect an ICMP packet |
| 188 | * using skb_flow_get_icmp_tci(). |
Matteo Croce | 3b336d6f | 2019-10-29 14:50:51 +0100 | [diff] [blame] | 189 | */ |
| 190 | static void __skb_flow_dissect_icmp(const struct sk_buff *skb, |
| 191 | struct flow_dissector *flow_dissector, |
Alexander Lobakin | f96533c | 2021-03-14 11:11:23 +0000 | [diff] [blame] | 192 | void *target_container, const void *data, |
| 193 | int thoff, int hlen) |
Matteo Croce | 3b336d6f | 2019-10-29 14:50:51 +0100 | [diff] [blame] | 194 | { |
| 195 | struct flow_dissector_key_icmp *key_icmp; |
| 196 | |
| 197 | if (!dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_ICMP)) |
| 198 | return; |
| 199 | |
| 200 | key_icmp = skb_flow_dissector_target(flow_dissector, |
| 201 | FLOW_DISSECTOR_KEY_ICMP, |
| 202 | target_container); |
Matteo Croce | 5dec597 | 2019-10-29 14:50:52 +0100 | [diff] [blame] | 203 | |
| 204 | skb_flow_get_icmp_tci(skb, key_icmp, data, thoff, hlen); |
Matteo Croce | 3b336d6f | 2019-10-29 14:50:51 +0100 | [diff] [blame] | 205 | } |
| 206 | |
Wojciech Drewek | dda2fa0 | 2022-09-08 10:16:41 -0700 | [diff] [blame] | 207 | static void __skb_flow_dissect_l2tpv3(const struct sk_buff *skb, |
| 208 | struct flow_dissector *flow_dissector, |
| 209 | void *target_container, const void *data, |
| 210 | int nhoff, int hlen) |
| 211 | { |
| 212 | struct flow_dissector_key_l2tpv3 *key_l2tpv3; |
| 213 | struct { |
| 214 | __be32 session_id; |
| 215 | } *hdr, _hdr; |
| 216 | |
| 217 | if (!dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_L2TPV3)) |
| 218 | return; |
| 219 | |
| 220 | hdr = __skb_header_pointer(skb, nhoff, sizeof(_hdr), data, hlen, &_hdr); |
| 221 | if (!hdr) |
| 222 | return; |
| 223 | |
| 224 | key_l2tpv3 = skb_flow_dissector_target(flow_dissector, |
| 225 | FLOW_DISSECTOR_KEY_L2TPV3, |
| 226 | target_container); |
| 227 | |
| 228 | key_l2tpv3->session_id = hdr->session_id; |
| 229 | } |
| 230 | |
Jiri Pirko | 82828b8 | 2019-06-19 09:41:02 +0300 | [diff] [blame] | 231 | void skb_flow_dissect_meta(const struct sk_buff *skb, |
| 232 | struct flow_dissector *flow_dissector, |
| 233 | void *target_container) |
| 234 | { |
| 235 | struct flow_dissector_key_meta *meta; |
| 236 | |
| 237 | if (!dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_META)) |
| 238 | return; |
| 239 | |
| 240 | meta = skb_flow_dissector_target(flow_dissector, |
| 241 | FLOW_DISSECTOR_KEY_META, |
| 242 | target_container); |
| 243 | meta->ingress_ifindex = skb->skb_iif; |
| 244 | } |
| 245 | EXPORT_SYMBOL(skb_flow_dissect_meta); |
| 246 | |
Simon Horman | a38402b | 2017-10-02 10:41:16 +0200 | [diff] [blame] | 247 | static void |
| 248 | skb_flow_dissect_set_enc_addr_type(enum flow_dissector_key_id type, |
| 249 | struct flow_dissector *flow_dissector, |
| 250 | void *target_container) |
| 251 | { |
| 252 | struct flow_dissector_key_control *ctrl; |
| 253 | |
| 254 | if (!dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_ENC_CONTROL)) |
| 255 | return; |
| 256 | |
| 257 | ctrl = skb_flow_dissector_target(flow_dissector, |
| 258 | FLOW_DISSECTOR_KEY_ENC_CONTROL, |
| 259 | target_container); |
| 260 | ctrl->addr_type = type; |
| 261 | } |
| 262 | |
Simon Horman | 62b3237 | 2017-12-04 11:31:48 +0100 | [diff] [blame] | 263 | void |
Paul Blakey | 75a5675 | 2019-07-09 10:30:49 +0300 | [diff] [blame] | 264 | skb_flow_dissect_ct(const struct sk_buff *skb, |
| 265 | struct flow_dissector *flow_dissector, |
wenxu | 7baf242 | 2021-01-19 16:31:50 +0800 | [diff] [blame] | 266 | void *target_container, u16 *ctinfo_map, |
Paul Blakey | 3849595 | 2021-12-14 19:24:34 +0200 | [diff] [blame] | 267 | size_t mapsize, bool post_ct, u16 zone) |
Paul Blakey | 75a5675 | 2019-07-09 10:30:49 +0300 | [diff] [blame] | 268 | { |
| 269 | #if IS_ENABLED(CONFIG_NF_CONNTRACK) |
| 270 | struct flow_dissector_key_ct *key; |
| 271 | enum ip_conntrack_info ctinfo; |
| 272 | struct nf_conn_labels *cl; |
| 273 | struct nf_conn *ct; |
| 274 | |
| 275 | if (!dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_CT)) |
| 276 | return; |
| 277 | |
| 278 | ct = nf_ct_get(skb, &ctinfo); |
wenxu | 7baf242 | 2021-01-19 16:31:50 +0800 | [diff] [blame] | 279 | if (!ct && !post_ct) |
Paul Blakey | 75a5675 | 2019-07-09 10:30:49 +0300 | [diff] [blame] | 280 | return; |
| 281 | |
| 282 | key = skb_flow_dissector_target(flow_dissector, |
| 283 | FLOW_DISSECTOR_KEY_CT, |
| 284 | target_container); |
| 285 | |
wenxu | 7baf242 | 2021-01-19 16:31:50 +0800 | [diff] [blame] | 286 | if (!ct) { |
| 287 | key->ct_state = TCA_FLOWER_KEY_CT_FLAGS_TRACKED | |
| 288 | TCA_FLOWER_KEY_CT_FLAGS_INVALID; |
Paul Blakey | 3849595 | 2021-12-14 19:24:34 +0200 | [diff] [blame] | 289 | key->ct_zone = zone; |
wenxu | 7baf242 | 2021-01-19 16:31:50 +0800 | [diff] [blame] | 290 | return; |
| 291 | } |
| 292 | |
Paul Blakey | 75a5675 | 2019-07-09 10:30:49 +0300 | [diff] [blame] | 293 | if (ctinfo < mapsize) |
| 294 | key->ct_state = ctinfo_map[ctinfo]; |
| 295 | #if IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES) |
| 296 | key->ct_zone = ct->zone.id; |
| 297 | #endif |
| 298 | #if IS_ENABLED(CONFIG_NF_CONNTRACK_MARK) |
Daniel Xu | 52d1aa8 | 2022-11-09 12:39:07 -0700 | [diff] [blame] | 299 | key->ct_mark = READ_ONCE(ct->mark); |
Paul Blakey | 75a5675 | 2019-07-09 10:30:49 +0300 | [diff] [blame] | 300 | #endif |
| 301 | |
| 302 | cl = nf_ct_labels_find(ct); |
| 303 | if (cl) |
| 304 | memcpy(key->ct_labels, cl->bits, sizeof(key->ct_labels)); |
| 305 | #endif /* CONFIG_NF_CONNTRACK */ |
| 306 | } |
| 307 | EXPORT_SYMBOL(skb_flow_dissect_ct); |
| 308 | |
| 309 | void |
Simon Horman | 62b3237 | 2017-12-04 11:31:48 +0100 | [diff] [blame] | 310 | skb_flow_dissect_tunnel_info(const struct sk_buff *skb, |
| 311 | struct flow_dissector *flow_dissector, |
| 312 | void *target_container) |
Simon Horman | a38402b | 2017-10-02 10:41:16 +0200 | [diff] [blame] | 313 | { |
| 314 | struct ip_tunnel_info *info; |
| 315 | struct ip_tunnel_key *key; |
| 316 | |
| 317 | /* A quick check to see if there might be something to do. */ |
| 318 | if (!dissector_uses_key(flow_dissector, |
| 319 | FLOW_DISSECTOR_KEY_ENC_KEYID) && |
| 320 | !dissector_uses_key(flow_dissector, |
| 321 | FLOW_DISSECTOR_KEY_ENC_IPV4_ADDRS) && |
| 322 | !dissector_uses_key(flow_dissector, |
| 323 | FLOW_DISSECTOR_KEY_ENC_IPV6_ADDRS) && |
| 324 | !dissector_uses_key(flow_dissector, |
| 325 | FLOW_DISSECTOR_KEY_ENC_CONTROL) && |
| 326 | !dissector_uses_key(flow_dissector, |
Or Gerlitz | 5544adb | 2018-07-17 19:27:17 +0300 | [diff] [blame] | 327 | FLOW_DISSECTOR_KEY_ENC_PORTS) && |
| 328 | !dissector_uses_key(flow_dissector, |
Simon Horman | 92e2c40 | 2018-08-07 17:36:00 +0200 | [diff] [blame] | 329 | FLOW_DISSECTOR_KEY_ENC_IP) && |
| 330 | !dissector_uses_key(flow_dissector, |
| 331 | FLOW_DISSECTOR_KEY_ENC_OPTS)) |
Simon Horman | a38402b | 2017-10-02 10:41:16 +0200 | [diff] [blame] | 332 | return; |
| 333 | |
| 334 | info = skb_tunnel_info(skb); |
| 335 | if (!info) |
| 336 | return; |
| 337 | |
| 338 | key = &info->key; |
| 339 | |
| 340 | switch (ip_tunnel_info_af(info)) { |
| 341 | case AF_INET: |
| 342 | skb_flow_dissect_set_enc_addr_type(FLOW_DISSECTOR_KEY_IPV4_ADDRS, |
| 343 | flow_dissector, |
| 344 | target_container); |
| 345 | if (dissector_uses_key(flow_dissector, |
| 346 | FLOW_DISSECTOR_KEY_ENC_IPV4_ADDRS)) { |
| 347 | struct flow_dissector_key_ipv4_addrs *ipv4; |
| 348 | |
| 349 | ipv4 = skb_flow_dissector_target(flow_dissector, |
| 350 | FLOW_DISSECTOR_KEY_ENC_IPV4_ADDRS, |
| 351 | target_container); |
| 352 | ipv4->src = key->u.ipv4.src; |
| 353 | ipv4->dst = key->u.ipv4.dst; |
| 354 | } |
| 355 | break; |
| 356 | case AF_INET6: |
| 357 | skb_flow_dissect_set_enc_addr_type(FLOW_DISSECTOR_KEY_IPV6_ADDRS, |
| 358 | flow_dissector, |
| 359 | target_container); |
| 360 | if (dissector_uses_key(flow_dissector, |
| 361 | FLOW_DISSECTOR_KEY_ENC_IPV6_ADDRS)) { |
| 362 | struct flow_dissector_key_ipv6_addrs *ipv6; |
| 363 | |
| 364 | ipv6 = skb_flow_dissector_target(flow_dissector, |
| 365 | FLOW_DISSECTOR_KEY_ENC_IPV6_ADDRS, |
| 366 | target_container); |
| 367 | ipv6->src = key->u.ipv6.src; |
| 368 | ipv6->dst = key->u.ipv6.dst; |
| 369 | } |
| 370 | break; |
| 371 | } |
| 372 | |
| 373 | if (dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_ENC_KEYID)) { |
| 374 | struct flow_dissector_key_keyid *keyid; |
| 375 | |
| 376 | keyid = skb_flow_dissector_target(flow_dissector, |
| 377 | FLOW_DISSECTOR_KEY_ENC_KEYID, |
| 378 | target_container); |
| 379 | keyid->keyid = tunnel_id_to_key32(key->tun_id); |
| 380 | } |
| 381 | |
| 382 | if (dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_ENC_PORTS)) { |
| 383 | struct flow_dissector_key_ports *tp; |
| 384 | |
| 385 | tp = skb_flow_dissector_target(flow_dissector, |
| 386 | FLOW_DISSECTOR_KEY_ENC_PORTS, |
| 387 | target_container); |
| 388 | tp->src = key->tp_src; |
| 389 | tp->dst = key->tp_dst; |
| 390 | } |
Or Gerlitz | 5544adb | 2018-07-17 19:27:17 +0300 | [diff] [blame] | 391 | |
| 392 | if (dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_ENC_IP)) { |
| 393 | struct flow_dissector_key_ip *ip; |
| 394 | |
| 395 | ip = skb_flow_dissector_target(flow_dissector, |
| 396 | FLOW_DISSECTOR_KEY_ENC_IP, |
| 397 | target_container); |
| 398 | ip->tos = key->tos; |
| 399 | ip->ttl = key->ttl; |
| 400 | } |
Simon Horman | 92e2c40 | 2018-08-07 17:36:00 +0200 | [diff] [blame] | 401 | |
| 402 | if (dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_ENC_OPTS)) { |
| 403 | struct flow_dissector_key_enc_opts *enc_opt; |
| 404 | |
| 405 | enc_opt = skb_flow_dissector_target(flow_dissector, |
| 406 | FLOW_DISSECTOR_KEY_ENC_OPTS, |
| 407 | target_container); |
| 408 | |
| 409 | if (info->options_len) { |
| 410 | enc_opt->len = info->options_len; |
| 411 | ip_tunnel_info_opts_get(enc_opt->data, info); |
| 412 | enc_opt->dst_opt_type = info->key.tun_flags & |
| 413 | TUNNEL_OPTIONS_PRESENT; |
| 414 | } |
| 415 | } |
Simon Horman | a38402b | 2017-10-02 10:41:16 +0200 | [diff] [blame] | 416 | } |
Simon Horman | 62b3237 | 2017-12-04 11:31:48 +0100 | [diff] [blame] | 417 | EXPORT_SYMBOL(skb_flow_dissect_tunnel_info); |
Simon Horman | a38402b | 2017-10-02 10:41:16 +0200 | [diff] [blame] | 418 | |
Ariel Levkovich | 0cb09af | 2020-07-23 01:03:00 +0300 | [diff] [blame] | 419 | void skb_flow_dissect_hash(const struct sk_buff *skb, |
| 420 | struct flow_dissector *flow_dissector, |
| 421 | void *target_container) |
| 422 | { |
| 423 | struct flow_dissector_key_hash *key; |
| 424 | |
| 425 | if (!dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_HASH)) |
| 426 | return; |
| 427 | |
| 428 | key = skb_flow_dissector_target(flow_dissector, |
| 429 | FLOW_DISSECTOR_KEY_HASH, |
| 430 | target_container); |
| 431 | |
| 432 | key->hash = skb_get_hash_raw(skb); |
| 433 | } |
| 434 | EXPORT_SYMBOL(skb_flow_dissect_hash); |
| 435 | |
Jiri Pirko | 9bf881f | 2017-03-06 16:39:51 +0100 | [diff] [blame] | 436 | static enum flow_dissect_ret |
Jiri Pirko | 4a5d6c8b | 2017-03-06 16:39:52 +0100 | [diff] [blame] | 437 | __skb_flow_dissect_mpls(const struct sk_buff *skb, |
| 438 | struct flow_dissector *flow_dissector, |
Alexander Lobakin | f96533c | 2021-03-14 11:11:23 +0000 | [diff] [blame] | 439 | void *target_container, const void *data, int nhoff, |
| 440 | int hlen, int lse_index, bool *entropy_label) |
Jiri Pirko | 4a5d6c8b | 2017-03-06 16:39:52 +0100 | [diff] [blame] | 441 | { |
Guillaume Nault | 58cff78 | 2020-05-26 14:29:00 +0200 | [diff] [blame] | 442 | struct mpls_label *hdr, _hdr; |
| 443 | u32 entry, label, bos; |
Jiri Pirko | 4a5d6c8b | 2017-03-06 16:39:52 +0100 | [diff] [blame] | 444 | |
| 445 | if (!dissector_uses_key(flow_dissector, |
Benjamin LaHaise | 029c1ec | 2017-04-22 16:52:46 -0400 | [diff] [blame] | 446 | FLOW_DISSECTOR_KEY_MPLS_ENTROPY) && |
| 447 | !dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_MPLS)) |
Jiri Pirko | 4a5d6c8b | 2017-03-06 16:39:52 +0100 | [diff] [blame] | 448 | return FLOW_DISSECT_RET_OUT_GOOD; |
| 449 | |
Guillaume Nault | 58cff78 | 2020-05-26 14:29:00 +0200 | [diff] [blame] | 450 | if (lse_index >= FLOW_DIS_MPLS_MAX) |
| 451 | return FLOW_DISSECT_RET_OUT_GOOD; |
| 452 | |
Jiri Pirko | 4a5d6c8b | 2017-03-06 16:39:52 +0100 | [diff] [blame] | 453 | hdr = __skb_header_pointer(skb, nhoff, sizeof(_hdr), data, |
| 454 | hlen, &_hdr); |
| 455 | if (!hdr) |
| 456 | return FLOW_DISSECT_RET_OUT_BAD; |
| 457 | |
Guillaume Nault | 58cff78 | 2020-05-26 14:29:00 +0200 | [diff] [blame] | 458 | entry = ntohl(hdr->entry); |
Benjamin LaHaise | 029c1ec | 2017-04-22 16:52:46 -0400 | [diff] [blame] | 459 | label = (entry & MPLS_LS_LABEL_MASK) >> MPLS_LS_LABEL_SHIFT; |
Guillaume Nault | 58cff78 | 2020-05-26 14:29:00 +0200 | [diff] [blame] | 460 | bos = (entry & MPLS_LS_S_MASK) >> MPLS_LS_S_SHIFT; |
Benjamin LaHaise | 029c1ec | 2017-04-22 16:52:46 -0400 | [diff] [blame] | 461 | |
| 462 | if (dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_MPLS)) { |
| 463 | struct flow_dissector_key_mpls *key_mpls; |
Guillaume Nault | 58cff78 | 2020-05-26 14:29:00 +0200 | [diff] [blame] | 464 | struct flow_dissector_mpls_lse *lse; |
Benjamin LaHaise | 029c1ec | 2017-04-22 16:52:46 -0400 | [diff] [blame] | 465 | |
| 466 | key_mpls = skb_flow_dissector_target(flow_dissector, |
| 467 | FLOW_DISSECTOR_KEY_MPLS, |
| 468 | target_container); |
Guillaume Nault | 58cff78 | 2020-05-26 14:29:00 +0200 | [diff] [blame] | 469 | lse = &key_mpls->ls[lse_index]; |
| 470 | |
| 471 | lse->mpls_ttl = (entry & MPLS_LS_TTL_MASK) >> MPLS_LS_TTL_SHIFT; |
| 472 | lse->mpls_bos = bos; |
| 473 | lse->mpls_tc = (entry & MPLS_LS_TC_MASK) >> MPLS_LS_TC_SHIFT; |
| 474 | lse->mpls_label = label; |
| 475 | dissector_set_mpls_lse(key_mpls, lse_index); |
Benjamin LaHaise | 029c1ec | 2017-04-22 16:52:46 -0400 | [diff] [blame] | 476 | } |
| 477 | |
Guillaume Nault | 58cff78 | 2020-05-26 14:29:00 +0200 | [diff] [blame] | 478 | if (*entropy_label && |
| 479 | dissector_uses_key(flow_dissector, |
| 480 | FLOW_DISSECTOR_KEY_MPLS_ENTROPY)) { |
| 481 | struct flow_dissector_key_keyid *key_keyid; |
| 482 | |
Jiri Pirko | 4a5d6c8b | 2017-03-06 16:39:52 +0100 | [diff] [blame] | 483 | key_keyid = skb_flow_dissector_target(flow_dissector, |
| 484 | FLOW_DISSECTOR_KEY_MPLS_ENTROPY, |
| 485 | target_container); |
Guillaume Nault | 58cff78 | 2020-05-26 14:29:00 +0200 | [diff] [blame] | 486 | key_keyid->keyid = cpu_to_be32(label); |
Jiri Pirko | 4a5d6c8b | 2017-03-06 16:39:52 +0100 | [diff] [blame] | 487 | } |
Guillaume Nault | 58cff78 | 2020-05-26 14:29:00 +0200 | [diff] [blame] | 488 | |
| 489 | *entropy_label = label == MPLS_LABEL_ENTROPY; |
| 490 | |
| 491 | return bos ? FLOW_DISSECT_RET_OUT_GOOD : FLOW_DISSECT_RET_PROTO_AGAIN; |
Jiri Pirko | 4a5d6c8b | 2017-03-06 16:39:52 +0100 | [diff] [blame] | 492 | } |
| 493 | |
| 494 | static enum flow_dissect_ret |
Jiri Pirko | 9bf881f | 2017-03-06 16:39:51 +0100 | [diff] [blame] | 495 | __skb_flow_dissect_arp(const struct sk_buff *skb, |
| 496 | struct flow_dissector *flow_dissector, |
Alexander Lobakin | f96533c | 2021-03-14 11:11:23 +0000 | [diff] [blame] | 497 | void *target_container, const void *data, |
| 498 | int nhoff, int hlen) |
Jiri Pirko | 9bf881f | 2017-03-06 16:39:51 +0100 | [diff] [blame] | 499 | { |
| 500 | struct flow_dissector_key_arp *key_arp; |
| 501 | struct { |
| 502 | unsigned char ar_sha[ETH_ALEN]; |
| 503 | unsigned char ar_sip[4]; |
| 504 | unsigned char ar_tha[ETH_ALEN]; |
| 505 | unsigned char ar_tip[4]; |
| 506 | } *arp_eth, _arp_eth; |
| 507 | const struct arphdr *arp; |
David S. Miller | 6f14f44 | 2017-04-06 07:25:07 -0700 | [diff] [blame] | 508 | struct arphdr _arp; |
Jiri Pirko | 9bf881f | 2017-03-06 16:39:51 +0100 | [diff] [blame] | 509 | |
| 510 | if (!dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_ARP)) |
| 511 | return FLOW_DISSECT_RET_OUT_GOOD; |
| 512 | |
| 513 | arp = __skb_header_pointer(skb, nhoff, sizeof(_arp), data, |
| 514 | hlen, &_arp); |
| 515 | if (!arp) |
| 516 | return FLOW_DISSECT_RET_OUT_BAD; |
| 517 | |
| 518 | if (arp->ar_hrd != htons(ARPHRD_ETHER) || |
| 519 | arp->ar_pro != htons(ETH_P_IP) || |
| 520 | arp->ar_hln != ETH_ALEN || |
| 521 | arp->ar_pln != 4 || |
| 522 | (arp->ar_op != htons(ARPOP_REPLY) && |
| 523 | arp->ar_op != htons(ARPOP_REQUEST))) |
| 524 | return FLOW_DISSECT_RET_OUT_BAD; |
| 525 | |
| 526 | arp_eth = __skb_header_pointer(skb, nhoff + sizeof(_arp), |
| 527 | sizeof(_arp_eth), data, |
| 528 | hlen, &_arp_eth); |
| 529 | if (!arp_eth) |
| 530 | return FLOW_DISSECT_RET_OUT_BAD; |
| 531 | |
| 532 | key_arp = skb_flow_dissector_target(flow_dissector, |
| 533 | FLOW_DISSECTOR_KEY_ARP, |
| 534 | target_container); |
| 535 | |
| 536 | memcpy(&key_arp->sip, arp_eth->ar_sip, sizeof(key_arp->sip)); |
| 537 | memcpy(&key_arp->tip, arp_eth->ar_tip, sizeof(key_arp->tip)); |
| 538 | |
| 539 | /* Only store the lower byte of the opcode; |
| 540 | * this covers ARPOP_REPLY and ARPOP_REQUEST. |
| 541 | */ |
| 542 | key_arp->op = ntohs(arp->ar_op) & 0xff; |
| 543 | |
| 544 | ether_addr_copy(key_arp->sha, arp_eth->ar_sha); |
| 545 | ether_addr_copy(key_arp->tha, arp_eth->ar_tha); |
| 546 | |
| 547 | return FLOW_DISSECT_RET_OUT_GOOD; |
| 548 | } |
| 549 | |
Jiri Pirko | 7c92de8 | 2017-03-06 16:39:55 +0100 | [diff] [blame] | 550 | static enum flow_dissect_ret |
| 551 | __skb_flow_dissect_gre(const struct sk_buff *skb, |
| 552 | struct flow_dissector_key_control *key_control, |
| 553 | struct flow_dissector *flow_dissector, |
Alexander Lobakin | f96533c | 2021-03-14 11:11:23 +0000 | [diff] [blame] | 554 | void *target_container, const void *data, |
Jiri Pirko | 7c92de8 | 2017-03-06 16:39:55 +0100 | [diff] [blame] | 555 | __be16 *p_proto, int *p_nhoff, int *p_hlen, |
| 556 | unsigned int flags) |
| 557 | { |
| 558 | struct flow_dissector_key_keyid *key_keyid; |
| 559 | struct gre_base_hdr *hdr, _hdr; |
| 560 | int offset = 0; |
| 561 | u16 gre_ver; |
| 562 | |
| 563 | hdr = __skb_header_pointer(skb, *p_nhoff, sizeof(_hdr), |
| 564 | data, *p_hlen, &_hdr); |
| 565 | if (!hdr) |
| 566 | return FLOW_DISSECT_RET_OUT_BAD; |
| 567 | |
| 568 | /* Only look inside GRE without routing */ |
| 569 | if (hdr->flags & GRE_ROUTING) |
| 570 | return FLOW_DISSECT_RET_OUT_GOOD; |
| 571 | |
| 572 | /* Only look inside GRE for version 0 and 1 */ |
| 573 | gre_ver = ntohs(hdr->flags & GRE_VERSION); |
| 574 | if (gre_ver > 1) |
| 575 | return FLOW_DISSECT_RET_OUT_GOOD; |
| 576 | |
| 577 | *p_proto = hdr->protocol; |
| 578 | if (gre_ver) { |
| 579 | /* Version1 must be PPTP, and check the flags */ |
| 580 | if (!(*p_proto == GRE_PROTO_PPP && (hdr->flags & GRE_KEY))) |
| 581 | return FLOW_DISSECT_RET_OUT_GOOD; |
| 582 | } |
| 583 | |
| 584 | offset += sizeof(struct gre_base_hdr); |
| 585 | |
| 586 | if (hdr->flags & GRE_CSUM) |
Pankaj Bharadiya | c593642 | 2019-12-09 10:31:43 -0800 | [diff] [blame] | 587 | offset += sizeof_field(struct gre_full_hdr, csum) + |
| 588 | sizeof_field(struct gre_full_hdr, reserved1); |
Jiri Pirko | 7c92de8 | 2017-03-06 16:39:55 +0100 | [diff] [blame] | 589 | |
| 590 | if (hdr->flags & GRE_KEY) { |
| 591 | const __be32 *keyid; |
| 592 | __be32 _keyid; |
| 593 | |
| 594 | keyid = __skb_header_pointer(skb, *p_nhoff + offset, |
| 595 | sizeof(_keyid), |
| 596 | data, *p_hlen, &_keyid); |
| 597 | if (!keyid) |
| 598 | return FLOW_DISSECT_RET_OUT_BAD; |
| 599 | |
| 600 | if (dissector_uses_key(flow_dissector, |
| 601 | FLOW_DISSECTOR_KEY_GRE_KEYID)) { |
| 602 | key_keyid = skb_flow_dissector_target(flow_dissector, |
| 603 | FLOW_DISSECTOR_KEY_GRE_KEYID, |
| 604 | target_container); |
| 605 | if (gre_ver == 0) |
| 606 | key_keyid->keyid = *keyid; |
| 607 | else |
| 608 | key_keyid->keyid = *keyid & GRE_PPTP_KEY_MASK; |
| 609 | } |
Pankaj Bharadiya | c593642 | 2019-12-09 10:31:43 -0800 | [diff] [blame] | 610 | offset += sizeof_field(struct gre_full_hdr, key); |
Jiri Pirko | 7c92de8 | 2017-03-06 16:39:55 +0100 | [diff] [blame] | 611 | } |
| 612 | |
| 613 | if (hdr->flags & GRE_SEQ) |
Pankaj Bharadiya | c593642 | 2019-12-09 10:31:43 -0800 | [diff] [blame] | 614 | offset += sizeof_field(struct pptp_gre_header, seq); |
Jiri Pirko | 7c92de8 | 2017-03-06 16:39:55 +0100 | [diff] [blame] | 615 | |
| 616 | if (gre_ver == 0) { |
| 617 | if (*p_proto == htons(ETH_P_TEB)) { |
| 618 | const struct ethhdr *eth; |
| 619 | struct ethhdr _eth; |
| 620 | |
| 621 | eth = __skb_header_pointer(skb, *p_nhoff + offset, |
| 622 | sizeof(_eth), |
| 623 | data, *p_hlen, &_eth); |
| 624 | if (!eth) |
| 625 | return FLOW_DISSECT_RET_OUT_BAD; |
| 626 | *p_proto = eth->h_proto; |
| 627 | offset += sizeof(*eth); |
| 628 | |
| 629 | /* Cap headers that we access via pointers at the |
| 630 | * end of the Ethernet header as our maximum alignment |
| 631 | * at that point is only 2 bytes. |
| 632 | */ |
| 633 | if (NET_IP_ALIGN) |
| 634 | *p_hlen = *p_nhoff + offset; |
| 635 | } |
| 636 | } else { /* version 1, must be PPTP */ |
| 637 | u8 _ppp_hdr[PPP_HDRLEN]; |
| 638 | u8 *ppp_hdr; |
| 639 | |
| 640 | if (hdr->flags & GRE_ACK) |
Pankaj Bharadiya | c593642 | 2019-12-09 10:31:43 -0800 | [diff] [blame] | 641 | offset += sizeof_field(struct pptp_gre_header, ack); |
Jiri Pirko | 7c92de8 | 2017-03-06 16:39:55 +0100 | [diff] [blame] | 642 | |
| 643 | ppp_hdr = __skb_header_pointer(skb, *p_nhoff + offset, |
| 644 | sizeof(_ppp_hdr), |
| 645 | data, *p_hlen, _ppp_hdr); |
| 646 | if (!ppp_hdr) |
| 647 | return FLOW_DISSECT_RET_OUT_BAD; |
| 648 | |
| 649 | switch (PPP_PROTOCOL(ppp_hdr)) { |
| 650 | case PPP_IP: |
| 651 | *p_proto = htons(ETH_P_IP); |
| 652 | break; |
| 653 | case PPP_IPV6: |
| 654 | *p_proto = htons(ETH_P_IPV6); |
| 655 | break; |
| 656 | default: |
| 657 | /* Could probably catch some more like MPLS */ |
| 658 | break; |
| 659 | } |
| 660 | |
| 661 | offset += PPP_HDRLEN; |
| 662 | } |
| 663 | |
| 664 | *p_nhoff += offset; |
| 665 | key_control->flags |= FLOW_DIS_ENCAPSULATION; |
| 666 | if (flags & FLOW_DISSECTOR_F_STOP_AT_ENCAP) |
| 667 | return FLOW_DISSECT_RET_OUT_GOOD; |
| 668 | |
Tom Herbert | 3a1214e | 2017-09-01 14:04:11 -0700 | [diff] [blame] | 669 | return FLOW_DISSECT_RET_PROTO_AGAIN; |
Jiri Pirko | 7c92de8 | 2017-03-06 16:39:55 +0100 | [diff] [blame] | 670 | } |
| 671 | |
Sven Eckelmann | 5b0890a | 2017-12-21 10:17:42 +0100 | [diff] [blame] | 672 | /** |
| 673 | * __skb_flow_dissect_batadv() - dissect batman-adv header |
| 674 | * @skb: sk_buff to with the batman-adv header |
| 675 | * @key_control: flow dissectors control key |
| 676 | * @data: raw buffer pointer to the packet, if NULL use skb->data |
| 677 | * @p_proto: pointer used to update the protocol to process next |
| 678 | * @p_nhoff: pointer used to update inner network header offset |
| 679 | * @hlen: packet header length |
| 680 | * @flags: any combination of FLOW_DISSECTOR_F_* |
| 681 | * |
| 682 | * ETH_P_BATMAN packets are tried to be dissected. Only |
| 683 | * &struct batadv_unicast packets are actually processed because they contain an |
| 684 | * inner ethernet header and are usually followed by actual network header. This |
| 685 | * allows the flow dissector to continue processing the packet. |
| 686 | * |
| 687 | * Return: FLOW_DISSECT_RET_PROTO_AGAIN when &struct batadv_unicast was found, |
| 688 | * FLOW_DISSECT_RET_OUT_GOOD when dissector should stop after encapsulation, |
| 689 | * otherwise FLOW_DISSECT_RET_OUT_BAD |
| 690 | */ |
| 691 | static enum flow_dissect_ret |
| 692 | __skb_flow_dissect_batadv(const struct sk_buff *skb, |
| 693 | struct flow_dissector_key_control *key_control, |
Alexander Lobakin | f96533c | 2021-03-14 11:11:23 +0000 | [diff] [blame] | 694 | const void *data, __be16 *p_proto, int *p_nhoff, |
| 695 | int hlen, unsigned int flags) |
Sven Eckelmann | 5b0890a | 2017-12-21 10:17:42 +0100 | [diff] [blame] | 696 | { |
| 697 | struct { |
| 698 | struct batadv_unicast_packet batadv_unicast; |
| 699 | struct ethhdr eth; |
| 700 | } *hdr, _hdr; |
| 701 | |
| 702 | hdr = __skb_header_pointer(skb, *p_nhoff, sizeof(_hdr), data, hlen, |
| 703 | &_hdr); |
| 704 | if (!hdr) |
| 705 | return FLOW_DISSECT_RET_OUT_BAD; |
| 706 | |
| 707 | if (hdr->batadv_unicast.version != BATADV_COMPAT_VERSION) |
| 708 | return FLOW_DISSECT_RET_OUT_BAD; |
| 709 | |
| 710 | if (hdr->batadv_unicast.packet_type != BATADV_UNICAST) |
| 711 | return FLOW_DISSECT_RET_OUT_BAD; |
| 712 | |
| 713 | *p_proto = hdr->eth.h_proto; |
| 714 | *p_nhoff += sizeof(*hdr); |
| 715 | |
| 716 | key_control->flags |= FLOW_DIS_ENCAPSULATION; |
| 717 | if (flags & FLOW_DISSECTOR_F_STOP_AT_ENCAP) |
| 718 | return FLOW_DISSECT_RET_OUT_GOOD; |
| 719 | |
| 720 | return FLOW_DISSECT_RET_PROTO_AGAIN; |
| 721 | } |
| 722 | |
Jiri Pirko | ac4bb5d | 2017-05-23 18:40:44 +0200 | [diff] [blame] | 723 | static void |
| 724 | __skb_flow_dissect_tcp(const struct sk_buff *skb, |
| 725 | struct flow_dissector *flow_dissector, |
Alexander Lobakin | f96533c | 2021-03-14 11:11:23 +0000 | [diff] [blame] | 726 | void *target_container, const void *data, |
| 727 | int thoff, int hlen) |
Jiri Pirko | ac4bb5d | 2017-05-23 18:40:44 +0200 | [diff] [blame] | 728 | { |
| 729 | struct flow_dissector_key_tcp *key_tcp; |
| 730 | struct tcphdr *th, _th; |
| 731 | |
| 732 | if (!dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_TCP)) |
| 733 | return; |
| 734 | |
| 735 | th = __skb_header_pointer(skb, thoff, sizeof(_th), data, hlen, &_th); |
| 736 | if (!th) |
| 737 | return; |
| 738 | |
| 739 | if (unlikely(__tcp_hdrlen(th) < sizeof(_th))) |
| 740 | return; |
| 741 | |
| 742 | key_tcp = skb_flow_dissector_target(flow_dissector, |
| 743 | FLOW_DISSECTOR_KEY_TCP, |
| 744 | target_container); |
| 745 | key_tcp->flags = (*(__be16 *) &tcp_flag_word(th) & htons(0x0FFF)); |
| 746 | } |
| 747 | |
Or Gerlitz | 518d8a2 | 2017-06-01 21:37:37 +0300 | [diff] [blame] | 748 | static void |
Yoshiki Komachi | 8ffb055 | 2019-12-03 19:40:12 +0900 | [diff] [blame] | 749 | __skb_flow_dissect_ports(const struct sk_buff *skb, |
| 750 | struct flow_dissector *flow_dissector, |
Alexander Lobakin | f96533c | 2021-03-14 11:11:23 +0000 | [diff] [blame] | 751 | void *target_container, const void *data, |
| 752 | int nhoff, u8 ip_proto, int hlen) |
Yoshiki Komachi | 8ffb055 | 2019-12-03 19:40:12 +0900 | [diff] [blame] | 753 | { |
| 754 | enum flow_dissector_key_id dissector_ports = FLOW_DISSECTOR_KEY_MAX; |
| 755 | struct flow_dissector_key_ports *key_ports; |
| 756 | |
| 757 | if (dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_PORTS)) |
| 758 | dissector_ports = FLOW_DISSECTOR_KEY_PORTS; |
| 759 | else if (dissector_uses_key(flow_dissector, |
| 760 | FLOW_DISSECTOR_KEY_PORTS_RANGE)) |
| 761 | dissector_ports = FLOW_DISSECTOR_KEY_PORTS_RANGE; |
| 762 | |
| 763 | if (dissector_ports == FLOW_DISSECTOR_KEY_MAX) |
| 764 | return; |
| 765 | |
| 766 | key_ports = skb_flow_dissector_target(flow_dissector, |
| 767 | dissector_ports, |
| 768 | target_container); |
| 769 | key_ports->ports = __skb_flow_get_ports(skb, nhoff, ip_proto, |
| 770 | data, hlen); |
| 771 | } |
| 772 | |
| 773 | static void |
Or Gerlitz | 518d8a2 | 2017-06-01 21:37:37 +0300 | [diff] [blame] | 774 | __skb_flow_dissect_ipv4(const struct sk_buff *skb, |
| 775 | struct flow_dissector *flow_dissector, |
Alexander Lobakin | f96533c | 2021-03-14 11:11:23 +0000 | [diff] [blame] | 776 | void *target_container, const void *data, |
| 777 | const struct iphdr *iph) |
Or Gerlitz | 518d8a2 | 2017-06-01 21:37:37 +0300 | [diff] [blame] | 778 | { |
| 779 | struct flow_dissector_key_ip *key_ip; |
| 780 | |
| 781 | if (!dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_IP)) |
| 782 | return; |
| 783 | |
| 784 | key_ip = skb_flow_dissector_target(flow_dissector, |
| 785 | FLOW_DISSECTOR_KEY_IP, |
| 786 | target_container); |
| 787 | key_ip->tos = iph->tos; |
| 788 | key_ip->ttl = iph->ttl; |
| 789 | } |
| 790 | |
| 791 | static void |
| 792 | __skb_flow_dissect_ipv6(const struct sk_buff *skb, |
| 793 | struct flow_dissector *flow_dissector, |
Alexander Lobakin | f96533c | 2021-03-14 11:11:23 +0000 | [diff] [blame] | 794 | void *target_container, const void *data, |
| 795 | const struct ipv6hdr *iph) |
Or Gerlitz | 518d8a2 | 2017-06-01 21:37:37 +0300 | [diff] [blame] | 796 | { |
| 797 | struct flow_dissector_key_ip *key_ip; |
| 798 | |
| 799 | if (!dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_IP)) |
| 800 | return; |
| 801 | |
| 802 | key_ip = skb_flow_dissector_target(flow_dissector, |
| 803 | FLOW_DISSECTOR_KEY_IP, |
| 804 | target_container); |
| 805 | key_ip->tos = ipv6_get_dsfield(iph); |
| 806 | key_ip->ttl = iph->hop_limit; |
| 807 | } |
| 808 | |
Tom Herbert | 1eed4df | 2017-09-01 14:04:12 -0700 | [diff] [blame] | 809 | /* Maximum number of protocol headers that can be parsed in |
| 810 | * __skb_flow_dissect |
| 811 | */ |
| 812 | #define MAX_FLOW_DISSECT_HDRS 15 |
| 813 | |
| 814 | static bool skb_flow_dissect_allowed(int *num_hdrs) |
| 815 | { |
| 816 | ++*num_hdrs; |
| 817 | |
| 818 | return (*num_hdrs <= MAX_FLOW_DISSECT_HDRS); |
| 819 | } |
| 820 | |
Petar Penkov | d58e468 | 2018-09-14 07:46:18 -0700 | [diff] [blame] | 821 | static void __skb_flow_bpf_to_target(const struct bpf_flow_keys *flow_keys, |
| 822 | struct flow_dissector *flow_dissector, |
| 823 | void *target_container) |
| 824 | { |
Yoshiki Komachi | 59fb9b6 | 2020-01-17 16:05:32 +0900 | [diff] [blame] | 825 | struct flow_dissector_key_ports *key_ports = NULL; |
Petar Penkov | d58e468 | 2018-09-14 07:46:18 -0700 | [diff] [blame] | 826 | struct flow_dissector_key_control *key_control; |
| 827 | struct flow_dissector_key_basic *key_basic; |
| 828 | struct flow_dissector_key_addrs *key_addrs; |
Stanislav Fomichev | 71c99e3 | 2019-07-25 15:52:30 -0700 | [diff] [blame] | 829 | struct flow_dissector_key_tags *key_tags; |
Petar Penkov | d58e468 | 2018-09-14 07:46:18 -0700 | [diff] [blame] | 830 | |
| 831 | key_control = skb_flow_dissector_target(flow_dissector, |
| 832 | FLOW_DISSECTOR_KEY_CONTROL, |
| 833 | target_container); |
| 834 | key_control->thoff = flow_keys->thoff; |
| 835 | if (flow_keys->is_frag) |
| 836 | key_control->flags |= FLOW_DIS_IS_FRAGMENT; |
| 837 | if (flow_keys->is_first_frag) |
| 838 | key_control->flags |= FLOW_DIS_FIRST_FRAG; |
| 839 | if (flow_keys->is_encap) |
| 840 | key_control->flags |= FLOW_DIS_ENCAPSULATION; |
| 841 | |
| 842 | key_basic = skb_flow_dissector_target(flow_dissector, |
| 843 | FLOW_DISSECTOR_KEY_BASIC, |
| 844 | target_container); |
| 845 | key_basic->n_proto = flow_keys->n_proto; |
| 846 | key_basic->ip_proto = flow_keys->ip_proto; |
| 847 | |
| 848 | if (flow_keys->addr_proto == ETH_P_IP && |
| 849 | dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_IPV4_ADDRS)) { |
| 850 | key_addrs = skb_flow_dissector_target(flow_dissector, |
| 851 | FLOW_DISSECTOR_KEY_IPV4_ADDRS, |
| 852 | target_container); |
| 853 | key_addrs->v4addrs.src = flow_keys->ipv4_src; |
| 854 | key_addrs->v4addrs.dst = flow_keys->ipv4_dst; |
| 855 | key_control->addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS; |
| 856 | } else if (flow_keys->addr_proto == ETH_P_IPV6 && |
| 857 | dissector_uses_key(flow_dissector, |
| 858 | FLOW_DISSECTOR_KEY_IPV6_ADDRS)) { |
| 859 | key_addrs = skb_flow_dissector_target(flow_dissector, |
| 860 | FLOW_DISSECTOR_KEY_IPV6_ADDRS, |
| 861 | target_container); |
Gustavo A. R. Silva | 1e3d976 | 2021-04-16 14:31:51 -0500 | [diff] [blame] | 862 | memcpy(&key_addrs->v6addrs.src, &flow_keys->ipv6_src, |
| 863 | sizeof(key_addrs->v6addrs.src)); |
| 864 | memcpy(&key_addrs->v6addrs.dst, &flow_keys->ipv6_dst, |
| 865 | sizeof(key_addrs->v6addrs.dst)); |
Petar Penkov | d58e468 | 2018-09-14 07:46:18 -0700 | [diff] [blame] | 866 | key_control->addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS; |
| 867 | } |
| 868 | |
Yoshiki Komachi | 59fb9b6 | 2020-01-17 16:05:32 +0900 | [diff] [blame] | 869 | if (dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_PORTS)) |
Petar Penkov | d58e468 | 2018-09-14 07:46:18 -0700 | [diff] [blame] | 870 | key_ports = skb_flow_dissector_target(flow_dissector, |
| 871 | FLOW_DISSECTOR_KEY_PORTS, |
| 872 | target_container); |
Yoshiki Komachi | 59fb9b6 | 2020-01-17 16:05:32 +0900 | [diff] [blame] | 873 | else if (dissector_uses_key(flow_dissector, |
| 874 | FLOW_DISSECTOR_KEY_PORTS_RANGE)) |
| 875 | key_ports = skb_flow_dissector_target(flow_dissector, |
| 876 | FLOW_DISSECTOR_KEY_PORTS_RANGE, |
| 877 | target_container); |
| 878 | |
| 879 | if (key_ports) { |
Petar Penkov | d58e468 | 2018-09-14 07:46:18 -0700 | [diff] [blame] | 880 | key_ports->src = flow_keys->sport; |
| 881 | key_ports->dst = flow_keys->dport; |
| 882 | } |
Stanislav Fomichev | 71c99e3 | 2019-07-25 15:52:30 -0700 | [diff] [blame] | 883 | |
| 884 | if (dissector_uses_key(flow_dissector, |
| 885 | FLOW_DISSECTOR_KEY_FLOW_LABEL)) { |
| 886 | key_tags = skb_flow_dissector_target(flow_dissector, |
| 887 | FLOW_DISSECTOR_KEY_FLOW_LABEL, |
| 888 | target_container); |
| 889 | key_tags->flow_label = ntohl(flow_keys->flow_label); |
| 890 | } |
Petar Penkov | d58e468 | 2018-09-14 07:46:18 -0700 | [diff] [blame] | 891 | } |
| 892 | |
Shmulik Ladkani | 0ba9850 | 2022-08-21 14:35:16 +0300 | [diff] [blame] | 893 | u32 bpf_flow_dissect(struct bpf_prog *prog, struct bpf_flow_dissector *ctx, |
| 894 | __be16 proto, int nhoff, int hlen, unsigned int flags) |
Stanislav Fomichev | 089b19a | 2019-04-22 08:55:44 -0700 | [diff] [blame] | 895 | { |
| 896 | struct bpf_flow_keys *flow_keys = ctx->flow_keys; |
Stanislav Fomichev | c8aa703 | 2019-01-28 08:53:53 -0800 | [diff] [blame] | 897 | u32 result; |
| 898 | |
Stanislav Fomichev | c8aa703 | 2019-01-28 08:53:53 -0800 | [diff] [blame] | 899 | /* Pass parameters to the BPF program */ |
| 900 | memset(flow_keys, 0, sizeof(*flow_keys)); |
Stanislav Fomichev | 089b19a | 2019-04-22 08:55:44 -0700 | [diff] [blame] | 901 | flow_keys->n_proto = proto; |
| 902 | flow_keys->nhoff = nhoff; |
Stanislav Fomichev | c8aa703 | 2019-01-28 08:53:53 -0800 | [diff] [blame] | 903 | flow_keys->thoff = flow_keys->nhoff; |
| 904 | |
Stanislav Fomichev | 086f956 | 2019-07-25 15:52:25 -0700 | [diff] [blame] | 905 | BUILD_BUG_ON((int)BPF_FLOW_DISSECTOR_F_PARSE_1ST_FRAG != |
| 906 | (int)FLOW_DISSECTOR_F_PARSE_1ST_FRAG); |
| 907 | BUILD_BUG_ON((int)BPF_FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL != |
| 908 | (int)FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL); |
| 909 | BUILD_BUG_ON((int)BPF_FLOW_DISSECTOR_F_STOP_AT_ENCAP != |
| 910 | (int)FLOW_DISSECTOR_F_STOP_AT_ENCAP); |
| 911 | flow_keys->flags = flags; |
| 912 | |
David Miller | 3d9f773c | 2020-02-24 15:01:43 +0100 | [diff] [blame] | 913 | result = bpf_prog_run_pin_on_cpu(prog, ctx); |
Stanislav Fomichev | c8aa703 | 2019-01-28 08:53:53 -0800 | [diff] [blame] | 914 | |
Stanislav Fomichev | 089b19a | 2019-04-22 08:55:44 -0700 | [diff] [blame] | 915 | flow_keys->nhoff = clamp_t(u16, flow_keys->nhoff, nhoff, hlen); |
Stanislav Fomichev | c8aa703 | 2019-01-28 08:53:53 -0800 | [diff] [blame] | 916 | flow_keys->thoff = clamp_t(u16, flow_keys->thoff, |
Stanislav Fomichev | 089b19a | 2019-04-22 08:55:44 -0700 | [diff] [blame] | 917 | flow_keys->nhoff, hlen); |
Stanislav Fomichev | c8aa703 | 2019-01-28 08:53:53 -0800 | [diff] [blame] | 918 | |
Shmulik Ladkani | 0ba9850 | 2022-08-21 14:35:16 +0300 | [diff] [blame] | 919 | return result; |
Stanislav Fomichev | c8aa703 | 2019-01-28 08:53:53 -0800 | [diff] [blame] | 920 | } |
| 921 | |
Linus Torvalds | f86d1fb | 2022-08-03 16:29:08 -0700 | [diff] [blame] | 922 | static bool is_pppoe_ses_hdr_valid(const struct pppoe_hdr *hdr) |
Wojciech Drewek | 46126db | 2022-07-18 14:18:10 +0200 | [diff] [blame] | 923 | { |
Linus Torvalds | f86d1fb | 2022-08-03 16:29:08 -0700 | [diff] [blame] | 924 | return hdr->ver == 1 && hdr->type == 1 && hdr->code == 0; |
Wojciech Drewek | 46126db | 2022-07-18 14:18:10 +0200 | [diff] [blame] | 925 | } |
| 926 | |
WANG Cong | 453a940 | 2014-08-25 17:03:47 -0700 | [diff] [blame] | 927 | /** |
| 928 | * __skb_flow_dissect - extract the flow_keys struct and return it |
Stanislav Fomichev | 3cbf4ff | 2019-04-22 08:55:46 -0700 | [diff] [blame] | 929 | * @net: associated network namespace, derived from @skb if NULL |
WANG Cong | 453a940 | 2014-08-25 17:03:47 -0700 | [diff] [blame] | 930 | * @skb: sk_buff to extract the flow from, can be NULL if the rest are specified |
Jiri Pirko | 06635a3 | 2015-05-12 14:56:16 +0200 | [diff] [blame] | 931 | * @flow_dissector: list of keys to dissect |
| 932 | * @target_container: target structure to put dissected values into |
WANG Cong | 453a940 | 2014-08-25 17:03:47 -0700 | [diff] [blame] | 933 | * @data: raw buffer pointer to the packet, if NULL use skb->data |
| 934 | * @proto: protocol for which to get the flow, if @data is NULL use skb->protocol |
| 935 | * @nhoff: network header offset, if @data is NULL use skb_network_offset(skb) |
| 936 | * @hlen: packet header length, if @data is NULL use skb_headlen(skb) |
Bart Van Assche | d79b3ba | 2019-03-25 09:17:21 -0700 | [diff] [blame] | 937 | * @flags: flags that control the dissection process, e.g. |
Stanislav Fomichev | 1cc2645 | 2019-05-31 14:05:06 -0700 | [diff] [blame] | 938 | * FLOW_DISSECTOR_F_STOP_AT_ENCAP. |
WANG Cong | 453a940 | 2014-08-25 17:03:47 -0700 | [diff] [blame] | 939 | * |
Jiri Pirko | 06635a3 | 2015-05-12 14:56:16 +0200 | [diff] [blame] | 940 | * The function will try to retrieve individual keys into target specified |
| 941 | * by flow_dissector from either the skbuff or a raw buffer specified by the |
| 942 | * rest parameters. |
| 943 | * |
| 944 | * Caller must take care of zeroing target container memory. |
WANG Cong | 453a940 | 2014-08-25 17:03:47 -0700 | [diff] [blame] | 945 | */ |
Stanislav Fomichev | 3cbf4ff | 2019-04-22 08:55:46 -0700 | [diff] [blame] | 946 | bool __skb_flow_dissect(const struct net *net, |
| 947 | const struct sk_buff *skb, |
Jiri Pirko | 06635a3 | 2015-05-12 14:56:16 +0200 | [diff] [blame] | 948 | struct flow_dissector *flow_dissector, |
Alexander Lobakin | f96533c | 2021-03-14 11:11:23 +0000 | [diff] [blame] | 949 | void *target_container, const void *data, |
| 950 | __be16 proto, int nhoff, int hlen, unsigned int flags) |
Eric Dumazet | 0744dd0 | 2011-11-28 05:22:18 +0000 | [diff] [blame] | 951 | { |
Tom Herbert | 42aecaa | 2015-06-04 09:16:39 -0700 | [diff] [blame] | 952 | struct flow_dissector_key_control *key_control; |
Jiri Pirko | 06635a3 | 2015-05-12 14:56:16 +0200 | [diff] [blame] | 953 | struct flow_dissector_key_basic *key_basic; |
| 954 | struct flow_dissector_key_addrs *key_addrs; |
Tom Herbert | d34af82 | 2015-06-04 09:16:43 -0700 | [diff] [blame] | 955 | struct flow_dissector_key_tags *key_tags; |
Hadar Hen Zion | f6a6692 | 2016-08-17 13:36:11 +0300 | [diff] [blame] | 956 | struct flow_dissector_key_vlan *key_vlan; |
Tom Herbert | 3a1214e | 2017-09-01 14:04:11 -0700 | [diff] [blame] | 957 | enum flow_dissect_ret fdret; |
Jianbo Liu | 24c590e | 2018-07-06 05:38:14 +0000 | [diff] [blame] | 958 | enum flow_dissector_key_id dissector_vlan = FLOW_DISSECTOR_KEY_MAX; |
Guillaume Nault | 58cff78 | 2020-05-26 14:29:00 +0200 | [diff] [blame] | 959 | bool mpls_el = false; |
| 960 | int mpls_lse = 0; |
Tom Herbert | 1eed4df | 2017-09-01 14:04:12 -0700 | [diff] [blame] | 961 | int num_hdrs = 0; |
Geert Uytterhoeven | 8e690ff | 2015-06-25 15:10:32 +0200 | [diff] [blame] | 962 | u8 ip_proto = 0; |
Eric Dumazet | 34fad54 | 2016-11-09 16:04:46 -0800 | [diff] [blame] | 963 | bool ret; |
Eric Dumazet | 0744dd0 | 2011-11-28 05:22:18 +0000 | [diff] [blame] | 964 | |
David S. Miller | 690e36e | 2014-08-23 12:13:41 -0700 | [diff] [blame] | 965 | if (!data) { |
| 966 | data = skb->data; |
Hadar Hen Zion | d5709f7 | 2016-08-17 13:36:10 +0300 | [diff] [blame] | 967 | proto = skb_vlan_tag_present(skb) ? |
| 968 | skb->vlan_proto : skb->protocol; |
WANG Cong | 453a940 | 2014-08-25 17:03:47 -0700 | [diff] [blame] | 969 | nhoff = skb_network_offset(skb); |
David S. Miller | 690e36e | 2014-08-23 12:13:41 -0700 | [diff] [blame] | 970 | hlen = skb_headlen(skb); |
John Crispin | 2d571645 | 2017-08-10 10:09:03 +0200 | [diff] [blame] | 971 | #if IS_ENABLED(CONFIG_NET_DSA) |
Alexander Lobakin | 8bef0af | 2019-12-05 13:02:35 +0300 | [diff] [blame] | 972 | if (unlikely(skb->dev && netdev_uses_dsa(skb->dev) && |
| 973 | proto == htons(ETH_P_XDSA))) { |
Felix Fietkau | 570d0a58 | 2022-11-14 13:42:11 +0100 | [diff] [blame] | 974 | struct metadata_dst *md_dst = skb_metadata_dst(skb); |
John Crispin | 43e6652 | 2017-08-09 14:41:19 +0200 | [diff] [blame] | 975 | const struct dsa_device_ops *ops; |
Alexander Lobakin | 8bef0af | 2019-12-05 13:02:35 +0300 | [diff] [blame] | 976 | int offset = 0; |
John Crispin | 43e6652 | 2017-08-09 14:41:19 +0200 | [diff] [blame] | 977 | |
| 978 | ops = skb->dev->dsa_ptr->tag_ops; |
Vladimir Oltean | ec13357 | 2021-06-14 16:58:19 +0300 | [diff] [blame] | 979 | /* Only DSA header taggers break flow dissection */ |
Felix Fietkau | 570d0a58 | 2022-11-14 13:42:11 +0100 | [diff] [blame] | 980 | if (ops->needed_headroom && |
| 981 | (!md_dst || md_dst->type != METADATA_HW_PORT_MUX)) { |
Vladimir Oltean | 54fec33 | 2020-09-26 22:32:08 +0300 | [diff] [blame] | 982 | if (ops->flow_dissect) |
| 983 | ops->flow_dissect(skb, &proto, &offset); |
| 984 | else |
| 985 | dsa_tag_generic_flow_dissect(skb, |
| 986 | &proto, |
| 987 | &offset); |
John Crispin | 43e6652 | 2017-08-09 14:41:19 +0200 | [diff] [blame] | 988 | hlen -= offset; |
| 989 | nhoff += offset; |
| 990 | } |
| 991 | } |
John Crispin | 2d571645 | 2017-08-10 10:09:03 +0200 | [diff] [blame] | 992 | #endif |
David S. Miller | 690e36e | 2014-08-23 12:13:41 -0700 | [diff] [blame] | 993 | } |
| 994 | |
Tom Herbert | 42aecaa | 2015-06-04 09:16:39 -0700 | [diff] [blame] | 995 | /* It is ensured by skb_flow_dissector_init() that control key will |
| 996 | * be always present. |
| 997 | */ |
| 998 | key_control = skb_flow_dissector_target(flow_dissector, |
| 999 | FLOW_DISSECTOR_KEY_CONTROL, |
| 1000 | target_container); |
| 1001 | |
Jiri Pirko | 06635a3 | 2015-05-12 14:56:16 +0200 | [diff] [blame] | 1002 | /* It is ensured by skb_flow_dissector_init() that basic key will |
| 1003 | * be always present. |
| 1004 | */ |
| 1005 | key_basic = skb_flow_dissector_target(flow_dissector, |
| 1006 | FLOW_DISSECTOR_KEY_BASIC, |
| 1007 | target_container); |
Eric Dumazet | 0744dd0 | 2011-11-28 05:22:18 +0000 | [diff] [blame] | 1008 | |
Willem de Bruijn | d0e13a1 | 2018-09-24 16:49:57 -0400 | [diff] [blame] | 1009 | if (skb) { |
Stanislav Fomichev | 3cbf4ff | 2019-04-22 08:55:46 -0700 | [diff] [blame] | 1010 | if (!net) { |
| 1011 | if (skb->dev) |
| 1012 | net = dev_net(skb->dev); |
| 1013 | else if (skb->sk) |
| 1014 | net = sock_net(skb->sk); |
Stanislav Fomichev | 3cbf4ff | 2019-04-22 08:55:46 -0700 | [diff] [blame] | 1015 | } |
Stanislav Fomichev | 9b52e3f | 2019-04-22 08:55:47 -0700 | [diff] [blame] | 1016 | } |
Stanislav Fomichev | c8aa703 | 2019-01-28 08:53:53 -0800 | [diff] [blame] | 1017 | |
Stanislav Fomichev | 9b52e3f | 2019-04-22 08:55:47 -0700 | [diff] [blame] | 1018 | WARN_ON_ONCE(!net); |
| 1019 | if (net) { |
Jakub Sitnicki | a3fd7ce | 2020-05-31 10:28:36 +0200 | [diff] [blame] | 1020 | enum netns_bpf_attach_type type = NETNS_BPF_FLOW_DISSECTOR; |
Jakub Sitnicki | 695c121 | 2020-06-25 16:13:55 +0200 | [diff] [blame] | 1021 | struct bpf_prog_array *run_array; |
Jakub Sitnicki | a3fd7ce | 2020-05-31 10:28:36 +0200 | [diff] [blame] | 1022 | |
Stanislav Fomichev | 9b52e3f | 2019-04-22 08:55:47 -0700 | [diff] [blame] | 1023 | rcu_read_lock(); |
Jakub Sitnicki | 695c121 | 2020-06-25 16:13:55 +0200 | [diff] [blame] | 1024 | run_array = rcu_dereference(init_net.bpf.run_array[type]); |
| 1025 | if (!run_array) |
| 1026 | run_array = rcu_dereference(net->bpf.run_array[type]); |
Stanislav Fomichev | a11c397 | 2019-10-07 09:21:02 -0700 | [diff] [blame] | 1027 | |
Jakub Sitnicki | 695c121 | 2020-06-25 16:13:55 +0200 | [diff] [blame] | 1028 | if (run_array) { |
Stanislav Fomichev | 9b52e3f | 2019-04-22 08:55:47 -0700 | [diff] [blame] | 1029 | struct bpf_flow_keys flow_keys; |
| 1030 | struct bpf_flow_dissector ctx = { |
| 1031 | .flow_keys = &flow_keys, |
| 1032 | .data = data, |
| 1033 | .data_end = data + hlen, |
| 1034 | }; |
| 1035 | __be16 n_proto = proto; |
Jakub Sitnicki | 695c121 | 2020-06-25 16:13:55 +0200 | [diff] [blame] | 1036 | struct bpf_prog *prog; |
Shmulik Ladkani | 0ba9850 | 2022-08-21 14:35:16 +0300 | [diff] [blame] | 1037 | u32 result; |
Stanislav Fomichev | 9b52e3f | 2019-04-22 08:55:47 -0700 | [diff] [blame] | 1038 | |
| 1039 | if (skb) { |
| 1040 | ctx.skb = skb; |
| 1041 | /* we can't use 'proto' in the skb case |
| 1042 | * because it might be set to skb->vlan_proto |
| 1043 | * which has been pulled from the data |
| 1044 | */ |
| 1045 | n_proto = skb->protocol; |
| 1046 | } |
| 1047 | |
Jakub Sitnicki | 695c121 | 2020-06-25 16:13:55 +0200 | [diff] [blame] | 1048 | prog = READ_ONCE(run_array->items[0].prog); |
Shmulik Ladkani | 0ba9850 | 2022-08-21 14:35:16 +0300 | [diff] [blame] | 1049 | result = bpf_flow_dissect(prog, &ctx, n_proto, nhoff, |
| 1050 | hlen, flags); |
Shmulik Ladkani | 91350fe | 2022-08-21 14:35:17 +0300 | [diff] [blame] | 1051 | if (result == BPF_FLOW_DISSECTOR_CONTINUE) |
| 1052 | goto dissect_continue; |
Stanislav Fomichev | c8aa703 | 2019-01-28 08:53:53 -0800 | [diff] [blame] | 1053 | __skb_flow_bpf_to_target(&flow_keys, flow_dissector, |
| 1054 | target_container); |
| 1055 | rcu_read_unlock(); |
Shmulik Ladkani | 0ba9850 | 2022-08-21 14:35:16 +0300 | [diff] [blame] | 1056 | return result == BPF_OK; |
Stanislav Fomichev | c8aa703 | 2019-01-28 08:53:53 -0800 | [diff] [blame] | 1057 | } |
Shmulik Ladkani | 91350fe | 2022-08-21 14:35:17 +0300 | [diff] [blame] | 1058 | dissect_continue: |
Petar Penkov | d58e468 | 2018-09-14 07:46:18 -0700 | [diff] [blame] | 1059 | rcu_read_unlock(); |
Petar Penkov | d58e468 | 2018-09-14 07:46:18 -0700 | [diff] [blame] | 1060 | } |
Petar Penkov | d58e468 | 2018-09-14 07:46:18 -0700 | [diff] [blame] | 1061 | |
David S. Miller | 20a17bf | 2015-09-01 21:19:17 -0700 | [diff] [blame] | 1062 | if (dissector_uses_key(flow_dissector, |
| 1063 | FLOW_DISSECTOR_KEY_ETH_ADDRS)) { |
Jiri Pirko | 67a900c | 2015-05-12 14:56:19 +0200 | [diff] [blame] | 1064 | struct ethhdr *eth = eth_hdr(skb); |
| 1065 | struct flow_dissector_key_eth_addrs *key_eth_addrs; |
| 1066 | |
| 1067 | key_eth_addrs = skb_flow_dissector_target(flow_dissector, |
| 1068 | FLOW_DISSECTOR_KEY_ETH_ADDRS, |
| 1069 | target_container); |
Jakub Kicinski | 1b80899 | 2022-04-06 14:15:21 -0700 | [diff] [blame] | 1070 | memcpy(key_eth_addrs, eth, sizeof(*key_eth_addrs)); |
Jiri Pirko | 67a900c | 2015-05-12 14:56:19 +0200 | [diff] [blame] | 1071 | } |
| 1072 | |
Boris Sukholitko | 34951fc | 2022-04-19 11:14:32 +0300 | [diff] [blame] | 1073 | if (dissector_uses_key(flow_dissector, |
| 1074 | FLOW_DISSECTOR_KEY_NUM_OF_VLANS)) { |
| 1075 | struct flow_dissector_key_num_of_vlans *key_num_of_vlans; |
| 1076 | |
| 1077 | key_num_of_vlans = skb_flow_dissector_target(flow_dissector, |
| 1078 | FLOW_DISSECTOR_KEY_NUM_OF_VLANS, |
| 1079 | target_container); |
| 1080 | key_num_of_vlans->num_of_vlans = 0; |
| 1081 | } |
| 1082 | |
Jiri Pirko | c5ef188 | 2017-03-06 16:39:54 +0100 | [diff] [blame] | 1083 | proto_again: |
Tom Herbert | 3a1214e | 2017-09-01 14:04:11 -0700 | [diff] [blame] | 1084 | fdret = FLOW_DISSECT_RET_CONTINUE; |
| 1085 | |
Eric Dumazet | 0744dd0 | 2011-11-28 05:22:18 +0000 | [diff] [blame] | 1086 | switch (proto) { |
Joe Perches | 2b8837a | 2014-03-12 10:04:17 -0700 | [diff] [blame] | 1087 | case htons(ETH_P_IP): { |
Eric Dumazet | 0744dd0 | 2011-11-28 05:22:18 +0000 | [diff] [blame] | 1088 | const struct iphdr *iph; |
| 1089 | struct iphdr _iph; |
Tom Herbert | 3a1214e | 2017-09-01 14:04:11 -0700 | [diff] [blame] | 1090 | |
David S. Miller | 690e36e | 2014-08-23 12:13:41 -0700 | [diff] [blame] | 1091 | iph = __skb_header_pointer(skb, nhoff, sizeof(_iph), data, hlen, &_iph); |
Tom Herbert | 3a1214e | 2017-09-01 14:04:11 -0700 | [diff] [blame] | 1092 | if (!iph || iph->ihl < 5) { |
| 1093 | fdret = FLOW_DISSECT_RET_OUT_BAD; |
| 1094 | break; |
| 1095 | } |
| 1096 | |
Eric Dumazet | 3797d3e | 2013-11-07 08:37:28 -0800 | [diff] [blame] | 1097 | nhoff += iph->ihl * 4; |
Eric Dumazet | 0744dd0 | 2011-11-28 05:22:18 +0000 | [diff] [blame] | 1098 | |
Eric Dumazet | 3797d3e | 2013-11-07 08:37:28 -0800 | [diff] [blame] | 1099 | ip_proto = iph->protocol; |
Eric Dumazet | 3797d3e | 2013-11-07 08:37:28 -0800 | [diff] [blame] | 1100 | |
Alexander Duyck | 918c023 | 2016-02-24 09:29:38 -0800 | [diff] [blame] | 1101 | if (dissector_uses_key(flow_dissector, |
| 1102 | FLOW_DISSECTOR_KEY_IPV4_ADDRS)) { |
| 1103 | key_addrs = skb_flow_dissector_target(flow_dissector, |
| 1104 | FLOW_DISSECTOR_KEY_IPV4_ADDRS, |
| 1105 | target_container); |
Tom Herbert | c3f8324 | 2015-06-04 09:16:40 -0700 | [diff] [blame] | 1106 | |
Gustavo A. R. Silva | 323e0cb | 2021-07-26 14:25:11 -0500 | [diff] [blame] | 1107 | memcpy(&key_addrs->v4addrs.src, &iph->saddr, |
| 1108 | sizeof(key_addrs->v4addrs.src)); |
| 1109 | memcpy(&key_addrs->v4addrs.dst, &iph->daddr, |
| 1110 | sizeof(key_addrs->v4addrs.dst)); |
Alexander Duyck | 918c023 | 2016-02-24 09:29:38 -0800 | [diff] [blame] | 1111 | key_control->addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS; |
| 1112 | } |
Tom Herbert | 807e165 | 2015-09-01 09:24:28 -0700 | [diff] [blame] | 1113 | |
Davide Caratti | d212683 | 2021-02-12 16:12:25 +0100 | [diff] [blame] | 1114 | __skb_flow_dissect_ipv4(skb, flow_dissector, |
| 1115 | target_container, data, iph); |
| 1116 | |
Tom Herbert | 807e165 | 2015-09-01 09:24:28 -0700 | [diff] [blame] | 1117 | if (ip_is_fragment(iph)) { |
David S. Miller | 4b36993 | 2015-09-01 16:46:08 -0700 | [diff] [blame] | 1118 | key_control->flags |= FLOW_DIS_IS_FRAGMENT; |
Tom Herbert | 807e165 | 2015-09-01 09:24:28 -0700 | [diff] [blame] | 1119 | |
| 1120 | if (iph->frag_off & htons(IP_OFFSET)) { |
Tom Herbert | 3a1214e | 2017-09-01 14:04:11 -0700 | [diff] [blame] | 1121 | fdret = FLOW_DISSECT_RET_OUT_GOOD; |
| 1122 | break; |
Tom Herbert | 807e165 | 2015-09-01 09:24:28 -0700 | [diff] [blame] | 1123 | } else { |
David S. Miller | 4b36993 | 2015-09-01 16:46:08 -0700 | [diff] [blame] | 1124 | key_control->flags |= FLOW_DIS_FIRST_FRAG; |
Tom Herbert | 3a1214e | 2017-09-01 14:04:11 -0700 | [diff] [blame] | 1125 | if (!(flags & |
| 1126 | FLOW_DISSECTOR_F_PARSE_1ST_FRAG)) { |
| 1127 | fdret = FLOW_DISSECT_RET_OUT_GOOD; |
| 1128 | break; |
| 1129 | } |
Tom Herbert | 807e165 | 2015-09-01 09:24:28 -0700 | [diff] [blame] | 1130 | } |
| 1131 | } |
| 1132 | |
Eric Dumazet | 0744dd0 | 2011-11-28 05:22:18 +0000 | [diff] [blame] | 1133 | break; |
| 1134 | } |
Joe Perches | 2b8837a | 2014-03-12 10:04:17 -0700 | [diff] [blame] | 1135 | case htons(ETH_P_IPV6): { |
Eric Dumazet | 0744dd0 | 2011-11-28 05:22:18 +0000 | [diff] [blame] | 1136 | const struct ipv6hdr *iph; |
| 1137 | struct ipv6hdr _iph; |
Tom Herbert | 19469a8 | 2014-07-01 21:33:01 -0700 | [diff] [blame] | 1138 | |
David S. Miller | 690e36e | 2014-08-23 12:13:41 -0700 | [diff] [blame] | 1139 | iph = __skb_header_pointer(skb, nhoff, sizeof(_iph), data, hlen, &_iph); |
Tom Herbert | 3a1214e | 2017-09-01 14:04:11 -0700 | [diff] [blame] | 1140 | if (!iph) { |
| 1141 | fdret = FLOW_DISSECT_RET_OUT_BAD; |
| 1142 | break; |
| 1143 | } |
Eric Dumazet | 0744dd0 | 2011-11-28 05:22:18 +0000 | [diff] [blame] | 1144 | |
| 1145 | ip_proto = iph->nexthdr; |
Eric Dumazet | 0744dd0 | 2011-11-28 05:22:18 +0000 | [diff] [blame] | 1146 | nhoff += sizeof(struct ipv6hdr); |
Tom Herbert | 19469a8 | 2014-07-01 21:33:01 -0700 | [diff] [blame] | 1147 | |
David S. Miller | 20a17bf | 2015-09-01 21:19:17 -0700 | [diff] [blame] | 1148 | if (dissector_uses_key(flow_dissector, |
| 1149 | FLOW_DISSECTOR_KEY_IPV6_ADDRS)) { |
Alexander Duyck | b3c3106 | 2016-02-24 09:29:57 -0800 | [diff] [blame] | 1150 | key_addrs = skb_flow_dissector_target(flow_dissector, |
| 1151 | FLOW_DISSECTOR_KEY_IPV6_ADDRS, |
| 1152 | target_container); |
Alexander Duyck | 5af7fb6 | 2014-10-10 12:09:12 -0700 | [diff] [blame] | 1153 | |
Gustavo A. R. Silva | 323e0cb | 2021-07-26 14:25:11 -0500 | [diff] [blame] | 1154 | memcpy(&key_addrs->v6addrs.src, &iph->saddr, |
| 1155 | sizeof(key_addrs->v6addrs.src)); |
| 1156 | memcpy(&key_addrs->v6addrs.dst, &iph->daddr, |
| 1157 | sizeof(key_addrs->v6addrs.dst)); |
Tom Herbert | c3f8324 | 2015-06-04 09:16:40 -0700 | [diff] [blame] | 1158 | key_control->addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS; |
Jiri Pirko | b924933 | 2015-05-12 14:56:18 +0200 | [diff] [blame] | 1159 | } |
Tom Herbert | 87ee9e5 | 2015-06-04 09:16:44 -0700 | [diff] [blame] | 1160 | |
Alexander Duyck | 461547f | 2016-02-09 02:49:54 -0800 | [diff] [blame] | 1161 | if ((dissector_uses_key(flow_dissector, |
| 1162 | FLOW_DISSECTOR_KEY_FLOW_LABEL) || |
| 1163 | (flags & FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL)) && |
| 1164 | ip6_flowlabel(iph)) { |
| 1165 | __be32 flow_label = ip6_flowlabel(iph); |
| 1166 | |
David S. Miller | 20a17bf | 2015-09-01 21:19:17 -0700 | [diff] [blame] | 1167 | if (dissector_uses_key(flow_dissector, |
| 1168 | FLOW_DISSECTOR_KEY_FLOW_LABEL)) { |
Tom Herbert | 87ee9e5 | 2015-06-04 09:16:44 -0700 | [diff] [blame] | 1169 | key_tags = skb_flow_dissector_target(flow_dissector, |
| 1170 | FLOW_DISSECTOR_KEY_FLOW_LABEL, |
| 1171 | target_container); |
| 1172 | key_tags->flow_label = ntohl(flow_label); |
Jiri Pirko | 12c227e | 2015-05-22 11:05:58 +0200 | [diff] [blame] | 1173 | } |
Tom Herbert | 3a1214e | 2017-09-01 14:04:11 -0700 | [diff] [blame] | 1174 | if (flags & FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL) { |
| 1175 | fdret = FLOW_DISSECT_RET_OUT_GOOD; |
| 1176 | break; |
| 1177 | } |
Tom Herbert | 19469a8 | 2014-07-01 21:33:01 -0700 | [diff] [blame] | 1178 | } |
| 1179 | |
Or Gerlitz | 518d8a2 | 2017-06-01 21:37:37 +0300 | [diff] [blame] | 1180 | __skb_flow_dissect_ipv6(skb, flow_dissector, |
| 1181 | target_container, data, iph); |
| 1182 | |
Eric Dumazet | 0744dd0 | 2011-11-28 05:22:18 +0000 | [diff] [blame] | 1183 | break; |
| 1184 | } |
Joe Perches | 2b8837a | 2014-03-12 10:04:17 -0700 | [diff] [blame] | 1185 | case htons(ETH_P_8021AD): |
| 1186 | case htons(ETH_P_8021Q): { |
Jianbo Liu | 24c590e | 2018-07-06 05:38:14 +0000 | [diff] [blame] | 1187 | const struct vlan_hdr *vlan = NULL; |
Arnd Bergmann | bc72f3d | 2016-10-24 23:40:30 +0200 | [diff] [blame] | 1188 | struct vlan_hdr _vlan; |
Jianbo Liu | 2064c3d | 2018-07-06 05:38:12 +0000 | [diff] [blame] | 1189 | __be16 saved_vlan_tpid = proto; |
Eric Dumazet | 0744dd0 | 2011-11-28 05:22:18 +0000 | [diff] [blame] | 1190 | |
Jianbo Liu | 24c590e | 2018-07-06 05:38:14 +0000 | [diff] [blame] | 1191 | if (dissector_vlan == FLOW_DISSECTOR_KEY_MAX && |
| 1192 | skb && skb_vlan_tag_present(skb)) { |
Hadar Hen Zion | d5709f7 | 2016-08-17 13:36:10 +0300 | [diff] [blame] | 1193 | proto = skb->protocol; |
Jianbo Liu | 24c590e | 2018-07-06 05:38:14 +0000 | [diff] [blame] | 1194 | } else { |
Hadar Hen Zion | d5709f7 | 2016-08-17 13:36:10 +0300 | [diff] [blame] | 1195 | vlan = __skb_header_pointer(skb, nhoff, sizeof(_vlan), |
| 1196 | data, hlen, &_vlan); |
Tom Herbert | 3a1214e | 2017-09-01 14:04:11 -0700 | [diff] [blame] | 1197 | if (!vlan) { |
| 1198 | fdret = FLOW_DISSECT_RET_OUT_BAD; |
| 1199 | break; |
| 1200 | } |
| 1201 | |
Hadar Hen Zion | d5709f7 | 2016-08-17 13:36:10 +0300 | [diff] [blame] | 1202 | proto = vlan->h_vlan_encapsulated_proto; |
| 1203 | nhoff += sizeof(*vlan); |
Hadar Hen Zion | d5709f7 | 2016-08-17 13:36:10 +0300 | [diff] [blame] | 1204 | } |
| 1205 | |
Qingqing Yang | 9f87eb4 | 2022-09-19 15:48:08 +0800 | [diff] [blame] | 1206 | if (dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_NUM_OF_VLANS) && |
| 1207 | !(key_control->flags & FLOW_DIS_ENCAPSULATION)) { |
Boris Sukholitko | 34951fc | 2022-04-19 11:14:32 +0300 | [diff] [blame] | 1208 | struct flow_dissector_key_num_of_vlans *key_nvs; |
| 1209 | |
| 1210 | key_nvs = skb_flow_dissector_target(flow_dissector, |
| 1211 | FLOW_DISSECTOR_KEY_NUM_OF_VLANS, |
| 1212 | target_container); |
| 1213 | key_nvs->num_of_vlans++; |
| 1214 | } |
| 1215 | |
Jianbo Liu | 24c590e | 2018-07-06 05:38:14 +0000 | [diff] [blame] | 1216 | if (dissector_vlan == FLOW_DISSECTOR_KEY_MAX) { |
| 1217 | dissector_vlan = FLOW_DISSECTOR_KEY_VLAN; |
| 1218 | } else if (dissector_vlan == FLOW_DISSECTOR_KEY_VLAN) { |
| 1219 | dissector_vlan = FLOW_DISSECTOR_KEY_CVLAN; |
| 1220 | } else { |
| 1221 | fdret = FLOW_DISSECT_RET_PROTO_AGAIN; |
| 1222 | break; |
| 1223 | } |
| 1224 | |
| 1225 | if (dissector_uses_key(flow_dissector, dissector_vlan)) { |
Hadar Hen Zion | f6a6692 | 2016-08-17 13:36:11 +0300 | [diff] [blame] | 1226 | key_vlan = skb_flow_dissector_target(flow_dissector, |
Jianbo Liu | 24c590e | 2018-07-06 05:38:14 +0000 | [diff] [blame] | 1227 | dissector_vlan, |
Tom Herbert | d34af82 | 2015-06-04 09:16:43 -0700 | [diff] [blame] | 1228 | target_container); |
| 1229 | |
Jianbo Liu | 24c590e | 2018-07-06 05:38:14 +0000 | [diff] [blame] | 1230 | if (!vlan) { |
Hadar Hen Zion | f6a6692 | 2016-08-17 13:36:11 +0300 | [diff] [blame] | 1231 | key_vlan->vlan_id = skb_vlan_tag_get_id(skb); |
Michał Mirosław | 9b31914 | 2018-11-07 18:07:03 +0100 | [diff] [blame] | 1232 | key_vlan->vlan_priority = skb_vlan_tag_get_prio(skb); |
Hadar Hen Zion | f6a6692 | 2016-08-17 13:36:11 +0300 | [diff] [blame] | 1233 | } else { |
| 1234 | key_vlan->vlan_id = ntohs(vlan->h_vlan_TCI) & |
Hadar Hen Zion | d5709f7 | 2016-08-17 13:36:10 +0300 | [diff] [blame] | 1235 | VLAN_VID_MASK; |
Hadar Hen Zion | f6a6692 | 2016-08-17 13:36:11 +0300 | [diff] [blame] | 1236 | key_vlan->vlan_priority = |
| 1237 | (ntohs(vlan->h_vlan_TCI) & |
| 1238 | VLAN_PRIO_MASK) >> VLAN_PRIO_SHIFT; |
| 1239 | } |
Jianbo Liu | 2064c3d | 2018-07-06 05:38:12 +0000 | [diff] [blame] | 1240 | key_vlan->vlan_tpid = saved_vlan_tpid; |
Vlad Buslov | 2105f70 | 2022-04-06 14:22:41 +0300 | [diff] [blame] | 1241 | key_vlan->vlan_eth_type = proto; |
Tom Herbert | d34af82 | 2015-06-04 09:16:43 -0700 | [diff] [blame] | 1242 | } |
| 1243 | |
Tom Herbert | 3a1214e | 2017-09-01 14:04:11 -0700 | [diff] [blame] | 1244 | fdret = FLOW_DISSECT_RET_PROTO_AGAIN; |
| 1245 | break; |
Eric Dumazet | 0744dd0 | 2011-11-28 05:22:18 +0000 | [diff] [blame] | 1246 | } |
Joe Perches | 2b8837a | 2014-03-12 10:04:17 -0700 | [diff] [blame] | 1247 | case htons(ETH_P_PPP_SES): { |
Eric Dumazet | 0744dd0 | 2011-11-28 05:22:18 +0000 | [diff] [blame] | 1248 | struct { |
| 1249 | struct pppoe_hdr hdr; |
| 1250 | __be16 proto; |
| 1251 | } *hdr, _hdr; |
Wojciech Drewek | 46126db | 2022-07-18 14:18:10 +0200 | [diff] [blame] | 1252 | u16 ppp_proto; |
| 1253 | |
David S. Miller | 690e36e | 2014-08-23 12:13:41 -0700 | [diff] [blame] | 1254 | hdr = __skb_header_pointer(skb, nhoff, sizeof(_hdr), data, hlen, &_hdr); |
Tom Herbert | 3a1214e | 2017-09-01 14:04:11 -0700 | [diff] [blame] | 1255 | if (!hdr) { |
| 1256 | fdret = FLOW_DISSECT_RET_OUT_BAD; |
| 1257 | break; |
| 1258 | } |
| 1259 | |
Linus Torvalds | f86d1fb | 2022-08-03 16:29:08 -0700 | [diff] [blame] | 1260 | if (!is_pppoe_ses_hdr_valid(&hdr->hdr)) { |
Tom Herbert | 3a1214e | 2017-09-01 14:04:11 -0700 | [diff] [blame] | 1261 | fdret = FLOW_DISSECT_RET_OUT_BAD; |
| 1262 | break; |
Eric Dumazet | 0744dd0 | 2011-11-28 05:22:18 +0000 | [diff] [blame] | 1263 | } |
Wojciech Drewek | 46126db | 2022-07-18 14:18:10 +0200 | [diff] [blame] | 1264 | |
| 1265 | /* least significant bit of the most significant octet |
| 1266 | * indicates if protocol field was compressed |
| 1267 | */ |
| 1268 | ppp_proto = ntohs(hdr->proto); |
| 1269 | if (ppp_proto & 0x0100) { |
| 1270 | ppp_proto = ppp_proto >> 8; |
| 1271 | nhoff += PPPOE_SES_HLEN - 1; |
| 1272 | } else { |
| 1273 | nhoff += PPPOE_SES_HLEN; |
| 1274 | } |
| 1275 | |
| 1276 | if (ppp_proto == PPP_IP) { |
| 1277 | proto = htons(ETH_P_IP); |
| 1278 | fdret = FLOW_DISSECT_RET_PROTO_AGAIN; |
| 1279 | } else if (ppp_proto == PPP_IPV6) { |
| 1280 | proto = htons(ETH_P_IPV6); |
| 1281 | fdret = FLOW_DISSECT_RET_PROTO_AGAIN; |
| 1282 | } else if (ppp_proto == PPP_MPLS_UC) { |
| 1283 | proto = htons(ETH_P_MPLS_UC); |
| 1284 | fdret = FLOW_DISSECT_RET_PROTO_AGAIN; |
| 1285 | } else if (ppp_proto == PPP_MPLS_MC) { |
| 1286 | proto = htons(ETH_P_MPLS_MC); |
| 1287 | fdret = FLOW_DISSECT_RET_PROTO_AGAIN; |
| 1288 | } else if (ppp_proto_is_valid(ppp_proto)) { |
| 1289 | fdret = FLOW_DISSECT_RET_OUT_GOOD; |
| 1290 | } else { |
| 1291 | fdret = FLOW_DISSECT_RET_OUT_BAD; |
| 1292 | break; |
| 1293 | } |
| 1294 | |
| 1295 | if (dissector_uses_key(flow_dissector, |
| 1296 | FLOW_DISSECTOR_KEY_PPPOE)) { |
| 1297 | struct flow_dissector_key_pppoe *key_pppoe; |
| 1298 | |
| 1299 | key_pppoe = skb_flow_dissector_target(flow_dissector, |
| 1300 | FLOW_DISSECTOR_KEY_PPPOE, |
| 1301 | target_container); |
| 1302 | key_pppoe->session_id = hdr->hdr.sid; |
| 1303 | key_pppoe->ppp_proto = htons(ppp_proto); |
| 1304 | key_pppoe->type = htons(ETH_P_PPP_SES); |
| 1305 | } |
Tom Herbert | 3a1214e | 2017-09-01 14:04:11 -0700 | [diff] [blame] | 1306 | break; |
Eric Dumazet | 0744dd0 | 2011-11-28 05:22:18 +0000 | [diff] [blame] | 1307 | } |
Erik Hugne | 08bfc9c | 2015-01-22 17:10:32 +0100 | [diff] [blame] | 1308 | case htons(ETH_P_TIPC): { |
Jon Maloy | 8d6e79d | 2017-11-08 09:59:26 +0100 | [diff] [blame] | 1309 | struct tipc_basic_hdr *hdr, _hdr; |
| 1310 | |
| 1311 | hdr = __skb_header_pointer(skb, nhoff, sizeof(_hdr), |
| 1312 | data, hlen, &_hdr); |
Tom Herbert | 3a1214e | 2017-09-01 14:04:11 -0700 | [diff] [blame] | 1313 | if (!hdr) { |
| 1314 | fdret = FLOW_DISSECT_RET_OUT_BAD; |
| 1315 | break; |
| 1316 | } |
Jiri Pirko | 06635a3 | 2015-05-12 14:56:16 +0200 | [diff] [blame] | 1317 | |
David S. Miller | 20a17bf | 2015-09-01 21:19:17 -0700 | [diff] [blame] | 1318 | if (dissector_uses_key(flow_dissector, |
Jon Maloy | 8d6e79d | 2017-11-08 09:59:26 +0100 | [diff] [blame] | 1319 | FLOW_DISSECTOR_KEY_TIPC)) { |
Jiri Pirko | 06635a3 | 2015-05-12 14:56:16 +0200 | [diff] [blame] | 1320 | key_addrs = skb_flow_dissector_target(flow_dissector, |
Jon Maloy | 8d6e79d | 2017-11-08 09:59:26 +0100 | [diff] [blame] | 1321 | FLOW_DISSECTOR_KEY_TIPC, |
Jiri Pirko | 06635a3 | 2015-05-12 14:56:16 +0200 | [diff] [blame] | 1322 | target_container); |
Jon Maloy | 8d6e79d | 2017-11-08 09:59:26 +0100 | [diff] [blame] | 1323 | key_addrs->tipckey.key = tipc_hdr_rps_key(hdr); |
| 1324 | key_control->addr_type = FLOW_DISSECTOR_KEY_TIPC; |
Jiri Pirko | 06635a3 | 2015-05-12 14:56:16 +0200 | [diff] [blame] | 1325 | } |
Tom Herbert | 3a1214e | 2017-09-01 14:04:11 -0700 | [diff] [blame] | 1326 | fdret = FLOW_DISSECT_RET_OUT_GOOD; |
| 1327 | break; |
Erik Hugne | 08bfc9c | 2015-01-22 17:10:32 +0100 | [diff] [blame] | 1328 | } |
Tom Herbert | b3baa0f | 2015-06-04 09:16:46 -0700 | [diff] [blame] | 1329 | |
| 1330 | case htons(ETH_P_MPLS_UC): |
Jiri Pirko | 4a5d6c8b | 2017-03-06 16:39:52 +0100 | [diff] [blame] | 1331 | case htons(ETH_P_MPLS_MC): |
Tom Herbert | 3a1214e | 2017-09-01 14:04:11 -0700 | [diff] [blame] | 1332 | fdret = __skb_flow_dissect_mpls(skb, flow_dissector, |
Jiri Pirko | 4a5d6c8b | 2017-03-06 16:39:52 +0100 | [diff] [blame] | 1333 | target_container, data, |
Guillaume Nault | 58cff78 | 2020-05-26 14:29:00 +0200 | [diff] [blame] | 1334 | nhoff, hlen, mpls_lse, |
| 1335 | &mpls_el); |
| 1336 | nhoff += sizeof(struct mpls_label); |
| 1337 | mpls_lse++; |
Tom Herbert | 3a1214e | 2017-09-01 14:04:11 -0700 | [diff] [blame] | 1338 | break; |
Alexander Duyck | 56193d1 | 2014-09-05 19:20:26 -0400 | [diff] [blame] | 1339 | case htons(ETH_P_FCOE): |
Tom Herbert | 3a1214e | 2017-09-01 14:04:11 -0700 | [diff] [blame] | 1340 | if ((hlen - nhoff) < FCOE_HEADER_LEN) { |
| 1341 | fdret = FLOW_DISSECT_RET_OUT_BAD; |
| 1342 | break; |
| 1343 | } |
Alexander Duyck | 224516b | 2016-02-24 09:29:51 -0800 | [diff] [blame] | 1344 | |
| 1345 | nhoff += FCOE_HEADER_LEN; |
Tom Herbert | 3a1214e | 2017-09-01 14:04:11 -0700 | [diff] [blame] | 1346 | fdret = FLOW_DISSECT_RET_OUT_GOOD; |
| 1347 | break; |
Simon Horman | 5573335 | 2017-01-11 14:05:42 +0100 | [diff] [blame] | 1348 | |
| 1349 | case htons(ETH_P_ARP): |
Jiri Pirko | 9bf881f | 2017-03-06 16:39:51 +0100 | [diff] [blame] | 1350 | case htons(ETH_P_RARP): |
Tom Herbert | 3a1214e | 2017-09-01 14:04:11 -0700 | [diff] [blame] | 1351 | fdret = __skb_flow_dissect_arp(skb, flow_dissector, |
Jiri Pirko | 9bf881f | 2017-03-06 16:39:51 +0100 | [diff] [blame] | 1352 | target_container, data, |
Tom Herbert | 3a1214e | 2017-09-01 14:04:11 -0700 | [diff] [blame] | 1353 | nhoff, hlen); |
| 1354 | break; |
| 1355 | |
Sven Eckelmann | 5b0890a | 2017-12-21 10:17:42 +0100 | [diff] [blame] | 1356 | case htons(ETH_P_BATMAN): |
| 1357 | fdret = __skb_flow_dissect_batadv(skb, key_control, data, |
| 1358 | &proto, &nhoff, hlen, flags); |
| 1359 | break; |
| 1360 | |
Eran Ben Elisha | 4f1cc51 | 2021-01-12 21:07:13 +0200 | [diff] [blame] | 1361 | case htons(ETH_P_1588): { |
| 1362 | struct ptp_header *hdr, _hdr; |
| 1363 | |
| 1364 | hdr = __skb_header_pointer(skb, nhoff, sizeof(_hdr), data, |
| 1365 | hlen, &_hdr); |
| 1366 | if (!hdr) { |
| 1367 | fdret = FLOW_DISSECT_RET_OUT_BAD; |
| 1368 | break; |
| 1369 | } |
| 1370 | |
| 1371 | nhoff += ntohs(hdr->message_length); |
| 1372 | fdret = FLOW_DISSECT_RET_OUT_GOOD; |
| 1373 | break; |
| 1374 | } |
| 1375 | |
Kurt Kanzenbach | f65e584 | 2022-03-10 08:35:05 +0100 | [diff] [blame] | 1376 | case htons(ETH_P_PRP): |
Kurt Kanzenbach | bf08824 | 2022-02-28 20:58:56 +0100 | [diff] [blame] | 1377 | case htons(ETH_P_HSR): { |
| 1378 | struct hsr_tag *hdr, _hdr; |
| 1379 | |
| 1380 | hdr = __skb_header_pointer(skb, nhoff, sizeof(_hdr), data, hlen, |
| 1381 | &_hdr); |
| 1382 | if (!hdr) { |
| 1383 | fdret = FLOW_DISSECT_RET_OUT_BAD; |
| 1384 | break; |
| 1385 | } |
| 1386 | |
| 1387 | proto = hdr->encap_proto; |
| 1388 | nhoff += HSR_HLEN; |
| 1389 | fdret = FLOW_DISSECT_RET_PROTO_AGAIN; |
| 1390 | break; |
| 1391 | } |
| 1392 | |
Tom Herbert | 3a1214e | 2017-09-01 14:04:11 -0700 | [diff] [blame] | 1393 | default: |
| 1394 | fdret = FLOW_DISSECT_RET_OUT_BAD; |
| 1395 | break; |
| 1396 | } |
| 1397 | |
| 1398 | /* Process result of proto processing */ |
| 1399 | switch (fdret) { |
| 1400 | case FLOW_DISSECT_RET_OUT_GOOD: |
| 1401 | goto out_good; |
| 1402 | case FLOW_DISSECT_RET_PROTO_AGAIN: |
Tom Herbert | 1eed4df | 2017-09-01 14:04:12 -0700 | [diff] [blame] | 1403 | if (skb_flow_dissect_allowed(&num_hdrs)) |
| 1404 | goto proto_again; |
| 1405 | goto out_good; |
Tom Herbert | 3a1214e | 2017-09-01 14:04:11 -0700 | [diff] [blame] | 1406 | case FLOW_DISSECT_RET_CONTINUE: |
| 1407 | case FLOW_DISSECT_RET_IPPROTO_AGAIN: |
| 1408 | break; |
| 1409 | case FLOW_DISSECT_RET_OUT_BAD: |
Eric Dumazet | 0744dd0 | 2011-11-28 05:22:18 +0000 | [diff] [blame] | 1410 | default: |
Tom Herbert | a6e544b | 2015-09-01 09:24:26 -0700 | [diff] [blame] | 1411 | goto out_bad; |
Eric Dumazet | 0744dd0 | 2011-11-28 05:22:18 +0000 | [diff] [blame] | 1412 | } |
| 1413 | |
Tom Herbert | 6a74fcf | 2015-06-12 09:01:06 -0700 | [diff] [blame] | 1414 | ip_proto_again: |
Tom Herbert | 3a1214e | 2017-09-01 14:04:11 -0700 | [diff] [blame] | 1415 | fdret = FLOW_DISSECT_RET_CONTINUE; |
| 1416 | |
Eric Dumazet | 0744dd0 | 2011-11-28 05:22:18 +0000 | [diff] [blame] | 1417 | switch (ip_proto) { |
Jiri Pirko | 7c92de8 | 2017-03-06 16:39:55 +0100 | [diff] [blame] | 1418 | case IPPROTO_GRE: |
Yoshiki Komachi | 6de6e46 | 2021-10-29 09:21:41 +0000 | [diff] [blame] | 1419 | if (flags & FLOW_DISSECTOR_F_STOP_BEFORE_ENCAP) { |
| 1420 | fdret = FLOW_DISSECT_RET_OUT_GOOD; |
| 1421 | break; |
| 1422 | } |
| 1423 | |
Tom Herbert | 3a1214e | 2017-09-01 14:04:11 -0700 | [diff] [blame] | 1424 | fdret = __skb_flow_dissect_gre(skb, key_control, flow_dissector, |
Jiri Pirko | 7c92de8 | 2017-03-06 16:39:55 +0100 | [diff] [blame] | 1425 | target_container, data, |
Tom Herbert | 3a1214e | 2017-09-01 14:04:11 -0700 | [diff] [blame] | 1426 | &proto, &nhoff, &hlen, flags); |
| 1427 | break; |
| 1428 | |
Tom Herbert | 6a74fcf | 2015-06-12 09:01:06 -0700 | [diff] [blame] | 1429 | case NEXTHDR_HOP: |
| 1430 | case NEXTHDR_ROUTING: |
| 1431 | case NEXTHDR_DEST: { |
| 1432 | u8 _opthdr[2], *opthdr; |
| 1433 | |
| 1434 | if (proto != htons(ETH_P_IPV6)) |
| 1435 | break; |
| 1436 | |
| 1437 | opthdr = __skb_header_pointer(skb, nhoff, sizeof(_opthdr), |
| 1438 | data, hlen, &_opthdr); |
Tom Herbert | 3a1214e | 2017-09-01 14:04:11 -0700 | [diff] [blame] | 1439 | if (!opthdr) { |
| 1440 | fdret = FLOW_DISSECT_RET_OUT_BAD; |
| 1441 | break; |
| 1442 | } |
Tom Herbert | 6a74fcf | 2015-06-12 09:01:06 -0700 | [diff] [blame] | 1443 | |
Eric Dumazet | 1e98a0f | 2015-06-12 19:31:32 -0700 | [diff] [blame] | 1444 | ip_proto = opthdr[0]; |
| 1445 | nhoff += (opthdr[1] + 1) << 3; |
Tom Herbert | 6a74fcf | 2015-06-12 09:01:06 -0700 | [diff] [blame] | 1446 | |
Tom Herbert | 3a1214e | 2017-09-01 14:04:11 -0700 | [diff] [blame] | 1447 | fdret = FLOW_DISSECT_RET_IPPROTO_AGAIN; |
| 1448 | break; |
Tom Herbert | 6a74fcf | 2015-06-12 09:01:06 -0700 | [diff] [blame] | 1449 | } |
Tom Herbert | b840f28 | 2015-09-01 09:24:29 -0700 | [diff] [blame] | 1450 | case NEXTHDR_FRAGMENT: { |
| 1451 | struct frag_hdr _fh, *fh; |
| 1452 | |
| 1453 | if (proto != htons(ETH_P_IPV6)) |
| 1454 | break; |
| 1455 | |
| 1456 | fh = __skb_header_pointer(skb, nhoff, sizeof(_fh), |
| 1457 | data, hlen, &_fh); |
| 1458 | |
Tom Herbert | 3a1214e | 2017-09-01 14:04:11 -0700 | [diff] [blame] | 1459 | if (!fh) { |
| 1460 | fdret = FLOW_DISSECT_RET_OUT_BAD; |
| 1461 | break; |
| 1462 | } |
Tom Herbert | b840f28 | 2015-09-01 09:24:29 -0700 | [diff] [blame] | 1463 | |
David S. Miller | 4b36993 | 2015-09-01 16:46:08 -0700 | [diff] [blame] | 1464 | key_control->flags |= FLOW_DIS_IS_FRAGMENT; |
Tom Herbert | b840f28 | 2015-09-01 09:24:29 -0700 | [diff] [blame] | 1465 | |
| 1466 | nhoff += sizeof(_fh); |
Alexander Duyck | 43d2ccb | 2016-02-24 09:29:44 -0800 | [diff] [blame] | 1467 | ip_proto = fh->nexthdr; |
Tom Herbert | b840f28 | 2015-09-01 09:24:29 -0700 | [diff] [blame] | 1468 | |
| 1469 | if (!(fh->frag_off & htons(IP6_OFFSET))) { |
David S. Miller | 4b36993 | 2015-09-01 16:46:08 -0700 | [diff] [blame] | 1470 | key_control->flags |= FLOW_DIS_FIRST_FRAG; |
Tom Herbert | 3a1214e | 2017-09-01 14:04:11 -0700 | [diff] [blame] | 1471 | if (flags & FLOW_DISSECTOR_F_PARSE_1ST_FRAG) { |
| 1472 | fdret = FLOW_DISSECT_RET_IPPROTO_AGAIN; |
| 1473 | break; |
| 1474 | } |
Tom Herbert | b840f28 | 2015-09-01 09:24:29 -0700 | [diff] [blame] | 1475 | } |
Tom Herbert | 3a1214e | 2017-09-01 14:04:11 -0700 | [diff] [blame] | 1476 | |
| 1477 | fdret = FLOW_DISSECT_RET_OUT_GOOD; |
| 1478 | break; |
Tom Herbert | b840f28 | 2015-09-01 09:24:29 -0700 | [diff] [blame] | 1479 | } |
Eric Dumazet | 0744dd0 | 2011-11-28 05:22:18 +0000 | [diff] [blame] | 1480 | case IPPROTO_IPIP: |
Yoshiki Komachi | 6de6e46 | 2021-10-29 09:21:41 +0000 | [diff] [blame] | 1481 | if (flags & FLOW_DISSECTOR_F_STOP_BEFORE_ENCAP) { |
| 1482 | fdret = FLOW_DISSECT_RET_OUT_GOOD; |
| 1483 | break; |
| 1484 | } |
| 1485 | |
Tom Herbert | fca4189 | 2013-07-29 11:07:36 -0700 | [diff] [blame] | 1486 | proto = htons(ETH_P_IP); |
Tom Herbert | 823b969 | 2015-09-01 09:24:32 -0700 | [diff] [blame] | 1487 | |
David S. Miller | 4b36993 | 2015-09-01 16:46:08 -0700 | [diff] [blame] | 1488 | key_control->flags |= FLOW_DIS_ENCAPSULATION; |
Tom Herbert | 3a1214e | 2017-09-01 14:04:11 -0700 | [diff] [blame] | 1489 | if (flags & FLOW_DISSECTOR_F_STOP_AT_ENCAP) { |
| 1490 | fdret = FLOW_DISSECT_RET_OUT_GOOD; |
| 1491 | break; |
| 1492 | } |
Tom Herbert | 823b969 | 2015-09-01 09:24:32 -0700 | [diff] [blame] | 1493 | |
Tom Herbert | 3a1214e | 2017-09-01 14:04:11 -0700 | [diff] [blame] | 1494 | fdret = FLOW_DISSECT_RET_PROTO_AGAIN; |
| 1495 | break; |
| 1496 | |
Tom Herbert | b438f94 | 2013-07-29 11:07:42 -0700 | [diff] [blame] | 1497 | case IPPROTO_IPV6: |
Yoshiki Komachi | 6de6e46 | 2021-10-29 09:21:41 +0000 | [diff] [blame] | 1498 | if (flags & FLOW_DISSECTOR_F_STOP_BEFORE_ENCAP) { |
| 1499 | fdret = FLOW_DISSECT_RET_OUT_GOOD; |
| 1500 | break; |
| 1501 | } |
| 1502 | |
Tom Herbert | b438f94 | 2013-07-29 11:07:42 -0700 | [diff] [blame] | 1503 | proto = htons(ETH_P_IPV6); |
Tom Herbert | 823b969 | 2015-09-01 09:24:32 -0700 | [diff] [blame] | 1504 | |
David S. Miller | 4b36993 | 2015-09-01 16:46:08 -0700 | [diff] [blame] | 1505 | key_control->flags |= FLOW_DIS_ENCAPSULATION; |
Tom Herbert | 3a1214e | 2017-09-01 14:04:11 -0700 | [diff] [blame] | 1506 | if (flags & FLOW_DISSECTOR_F_STOP_AT_ENCAP) { |
| 1507 | fdret = FLOW_DISSECT_RET_OUT_GOOD; |
| 1508 | break; |
| 1509 | } |
Tom Herbert | 823b969 | 2015-09-01 09:24:32 -0700 | [diff] [blame] | 1510 | |
Tom Herbert | 3a1214e | 2017-09-01 14:04:11 -0700 | [diff] [blame] | 1511 | fdret = FLOW_DISSECT_RET_PROTO_AGAIN; |
| 1512 | break; |
| 1513 | |
| 1514 | |
Tom Herbert | b3baa0f | 2015-06-04 09:16:46 -0700 | [diff] [blame] | 1515 | case IPPROTO_MPLS: |
| 1516 | proto = htons(ETH_P_MPLS_UC); |
Tom Herbert | 3a1214e | 2017-09-01 14:04:11 -0700 | [diff] [blame] | 1517 | fdret = FLOW_DISSECT_RET_PROTO_AGAIN; |
| 1518 | break; |
| 1519 | |
Jiri Pirko | ac4bb5d | 2017-05-23 18:40:44 +0200 | [diff] [blame] | 1520 | case IPPROTO_TCP: |
| 1521 | __skb_flow_dissect_tcp(skb, flow_dissector, target_container, |
| 1522 | data, nhoff, hlen); |
| 1523 | break; |
Tom Herbert | 3a1214e | 2017-09-01 14:04:11 -0700 | [diff] [blame] | 1524 | |
Matteo Croce | 3b336d6f | 2019-10-29 14:50:51 +0100 | [diff] [blame] | 1525 | case IPPROTO_ICMP: |
| 1526 | case IPPROTO_ICMPV6: |
| 1527 | __skb_flow_dissect_icmp(skb, flow_dissector, target_container, |
| 1528 | data, nhoff, hlen); |
| 1529 | break; |
Wojciech Drewek | dda2fa0 | 2022-09-08 10:16:41 -0700 | [diff] [blame] | 1530 | case IPPROTO_L2TP: |
| 1531 | __skb_flow_dissect_l2tpv3(skb, flow_dissector, target_container, |
| 1532 | data, nhoff, hlen); |
| 1533 | break; |
Matteo Croce | 3b336d6f | 2019-10-29 14:50:51 +0100 | [diff] [blame] | 1534 | |
Eric Dumazet | 0744dd0 | 2011-11-28 05:22:18 +0000 | [diff] [blame] | 1535 | default: |
| 1536 | break; |
| 1537 | } |
| 1538 | |
Yoshiki Komachi | 8ffb055 | 2019-12-03 19:40:12 +0900 | [diff] [blame] | 1539 | if (!(key_control->flags & FLOW_DIS_IS_FRAGMENT)) |
| 1540 | __skb_flow_dissect_ports(skb, flow_dissector, target_container, |
| 1541 | data, nhoff, ip_proto, hlen); |
Alexander Duyck | 5af7fb6 | 2014-10-10 12:09:12 -0700 | [diff] [blame] | 1542 | |
Tom Herbert | 3a1214e | 2017-09-01 14:04:11 -0700 | [diff] [blame] | 1543 | /* Process result of IP proto processing */ |
| 1544 | switch (fdret) { |
| 1545 | case FLOW_DISSECT_RET_PROTO_AGAIN: |
Tom Herbert | 1eed4df | 2017-09-01 14:04:12 -0700 | [diff] [blame] | 1546 | if (skb_flow_dissect_allowed(&num_hdrs)) |
| 1547 | goto proto_again; |
| 1548 | break; |
Tom Herbert | 3a1214e | 2017-09-01 14:04:11 -0700 | [diff] [blame] | 1549 | case FLOW_DISSECT_RET_IPPROTO_AGAIN: |
Tom Herbert | 1eed4df | 2017-09-01 14:04:12 -0700 | [diff] [blame] | 1550 | if (skb_flow_dissect_allowed(&num_hdrs)) |
| 1551 | goto ip_proto_again; |
| 1552 | break; |
Tom Herbert | 3a1214e | 2017-09-01 14:04:11 -0700 | [diff] [blame] | 1553 | case FLOW_DISSECT_RET_OUT_GOOD: |
| 1554 | case FLOW_DISSECT_RET_CONTINUE: |
| 1555 | break; |
| 1556 | case FLOW_DISSECT_RET_OUT_BAD: |
| 1557 | default: |
| 1558 | goto out_bad; |
| 1559 | } |
| 1560 | |
Tom Herbert | a6e544b | 2015-09-01 09:24:26 -0700 | [diff] [blame] | 1561 | out_good: |
| 1562 | ret = true; |
| 1563 | |
Eric Dumazet | 34fad54 | 2016-11-09 16:04:46 -0800 | [diff] [blame] | 1564 | out: |
Eric Dumazet | d0c081b | 2018-01-17 14:21:13 -0800 | [diff] [blame] | 1565 | key_control->thoff = min_t(u16, nhoff, skb ? skb->len : hlen); |
Tom Herbert | a6e544b | 2015-09-01 09:24:26 -0700 | [diff] [blame] | 1566 | key_basic->n_proto = proto; |
| 1567 | key_basic->ip_proto = ip_proto; |
Tom Herbert | a6e544b | 2015-09-01 09:24:26 -0700 | [diff] [blame] | 1568 | |
| 1569 | return ret; |
Eric Dumazet | 34fad54 | 2016-11-09 16:04:46 -0800 | [diff] [blame] | 1570 | |
| 1571 | out_bad: |
| 1572 | ret = false; |
Eric Dumazet | 34fad54 | 2016-11-09 16:04:46 -0800 | [diff] [blame] | 1573 | goto out; |
Eric Dumazet | 0744dd0 | 2011-11-28 05:22:18 +0000 | [diff] [blame] | 1574 | } |
David S. Miller | 690e36e | 2014-08-23 12:13:41 -0700 | [diff] [blame] | 1575 | EXPORT_SYMBOL(__skb_flow_dissect); |
Cong Wang | 441d9d3 | 2013-01-21 00:39:24 +0000 | [diff] [blame] | 1576 | |
Eric Dumazet | 49ecc2e9 | 2021-11-15 09:23:03 -0800 | [diff] [blame] | 1577 | static siphash_aligned_key_t hashrnd; |
Hannes Frederic Sowa | 66415cf | 2013-10-23 20:06:00 +0200 | [diff] [blame] | 1578 | static __always_inline void __flow_hash_secret_init(void) |
| 1579 | { |
| 1580 | net_get_random_once(&hashrnd, sizeof(hashrnd)); |
| 1581 | } |
| 1582 | |
Eric Dumazet | 5566744 | 2019-10-22 07:57:46 -0700 | [diff] [blame] | 1583 | static const void *flow_keys_hash_start(const struct flow_keys *flow) |
Hannes Frederic Sowa | 66415cf | 2013-10-23 20:06:00 +0200 | [diff] [blame] | 1584 | { |
Eric Dumazet | 5566744 | 2019-10-22 07:57:46 -0700 | [diff] [blame] | 1585 | BUILD_BUG_ON(FLOW_KEYS_HASH_OFFSET % SIPHASH_ALIGNMENT); |
| 1586 | return &flow->FLOW_KEYS_HASH_START_FIELD; |
Tom Herbert | 42aecaa | 2015-06-04 09:16:39 -0700 | [diff] [blame] | 1587 | } |
| 1588 | |
David S. Miller | 20a17bf | 2015-09-01 21:19:17 -0700 | [diff] [blame] | 1589 | static inline size_t flow_keys_hash_length(const struct flow_keys *flow) |
Tom Herbert | 42aecaa | 2015-06-04 09:16:39 -0700 | [diff] [blame] | 1590 | { |
Tom Herbert | c3f8324 | 2015-06-04 09:16:40 -0700 | [diff] [blame] | 1591 | size_t diff = FLOW_KEYS_HASH_OFFSET + sizeof(flow->addrs); |
David S. Miller | d31e955 | 2019-11-02 13:12:51 -0700 | [diff] [blame] | 1592 | |
Tom Herbert | 42aecaa | 2015-06-04 09:16:39 -0700 | [diff] [blame] | 1593 | BUILD_BUG_ON((sizeof(*flow) - FLOW_KEYS_HASH_OFFSET) % sizeof(u32)); |
Tom Herbert | c3f8324 | 2015-06-04 09:16:40 -0700 | [diff] [blame] | 1594 | |
| 1595 | switch (flow->control.addr_type) { |
| 1596 | case FLOW_DISSECTOR_KEY_IPV4_ADDRS: |
| 1597 | diff -= sizeof(flow->addrs.v4addrs); |
| 1598 | break; |
| 1599 | case FLOW_DISSECTOR_KEY_IPV6_ADDRS: |
| 1600 | diff -= sizeof(flow->addrs.v6addrs); |
| 1601 | break; |
Jon Maloy | 8d6e79d | 2017-11-08 09:59:26 +0100 | [diff] [blame] | 1602 | case FLOW_DISSECTOR_KEY_TIPC: |
| 1603 | diff -= sizeof(flow->addrs.tipckey); |
Tom Herbert | 9f24908 | 2015-06-04 09:16:41 -0700 | [diff] [blame] | 1604 | break; |
Tom Herbert | c3f8324 | 2015-06-04 09:16:40 -0700 | [diff] [blame] | 1605 | } |
Eric Dumazet | 5566744 | 2019-10-22 07:57:46 -0700 | [diff] [blame] | 1606 | return sizeof(*flow) - diff; |
Tom Herbert | c3f8324 | 2015-06-04 09:16:40 -0700 | [diff] [blame] | 1607 | } |
| 1608 | |
| 1609 | __be32 flow_get_u32_src(const struct flow_keys *flow) |
| 1610 | { |
| 1611 | switch (flow->control.addr_type) { |
| 1612 | case FLOW_DISSECTOR_KEY_IPV4_ADDRS: |
| 1613 | return flow->addrs.v4addrs.src; |
| 1614 | case FLOW_DISSECTOR_KEY_IPV6_ADDRS: |
| 1615 | return (__force __be32)ipv6_addr_hash( |
| 1616 | &flow->addrs.v6addrs.src); |
Jon Maloy | 8d6e79d | 2017-11-08 09:59:26 +0100 | [diff] [blame] | 1617 | case FLOW_DISSECTOR_KEY_TIPC: |
| 1618 | return flow->addrs.tipckey.key; |
Tom Herbert | c3f8324 | 2015-06-04 09:16:40 -0700 | [diff] [blame] | 1619 | default: |
| 1620 | return 0; |
| 1621 | } |
| 1622 | } |
| 1623 | EXPORT_SYMBOL(flow_get_u32_src); |
| 1624 | |
| 1625 | __be32 flow_get_u32_dst(const struct flow_keys *flow) |
| 1626 | { |
| 1627 | switch (flow->control.addr_type) { |
| 1628 | case FLOW_DISSECTOR_KEY_IPV4_ADDRS: |
| 1629 | return flow->addrs.v4addrs.dst; |
| 1630 | case FLOW_DISSECTOR_KEY_IPV6_ADDRS: |
| 1631 | return (__force __be32)ipv6_addr_hash( |
| 1632 | &flow->addrs.v6addrs.dst); |
| 1633 | default: |
| 1634 | return 0; |
| 1635 | } |
| 1636 | } |
| 1637 | EXPORT_SYMBOL(flow_get_u32_dst); |
| 1638 | |
zhang kai | 1e60ceb | 2021-07-28 18:54:18 +0800 | [diff] [blame] | 1639 | /* Sort the source and destination IP and the ports, |
Matteo Croce | 98298e6 | 2019-10-29 14:50:50 +0100 | [diff] [blame] | 1640 | * to have consistent hash within the two directions |
| 1641 | */ |
Tom Herbert | c3f8324 | 2015-06-04 09:16:40 -0700 | [diff] [blame] | 1642 | static inline void __flow_hash_consistentify(struct flow_keys *keys) |
| 1643 | { |
| 1644 | int addr_diff, i; |
| 1645 | |
| 1646 | switch (keys->control.addr_type) { |
| 1647 | case FLOW_DISSECTOR_KEY_IPV4_ADDRS: |
Ludovic Cintrat | 64ae13e | 2022-09-07 12:08:13 +0200 | [diff] [blame] | 1648 | if ((__force u32)keys->addrs.v4addrs.dst < |
| 1649 | (__force u32)keys->addrs.v4addrs.src) |
Tom Herbert | c3f8324 | 2015-06-04 09:16:40 -0700 | [diff] [blame] | 1650 | swap(keys->addrs.v4addrs.src, keys->addrs.v4addrs.dst); |
zhang kai | 1e60ceb | 2021-07-28 18:54:18 +0800 | [diff] [blame] | 1651 | |
| 1652 | if ((__force u16)keys->ports.dst < |
| 1653 | (__force u16)keys->ports.src) { |
Tom Herbert | c3f8324 | 2015-06-04 09:16:40 -0700 | [diff] [blame] | 1654 | swap(keys->ports.src, keys->ports.dst); |
| 1655 | } |
| 1656 | break; |
| 1657 | case FLOW_DISSECTOR_KEY_IPV6_ADDRS: |
| 1658 | addr_diff = memcmp(&keys->addrs.v6addrs.dst, |
| 1659 | &keys->addrs.v6addrs.src, |
| 1660 | sizeof(keys->addrs.v6addrs.dst)); |
zhang kai | 1e60ceb | 2021-07-28 18:54:18 +0800 | [diff] [blame] | 1661 | if (addr_diff < 0) { |
Tom Herbert | c3f8324 | 2015-06-04 09:16:40 -0700 | [diff] [blame] | 1662 | for (i = 0; i < 4; i++) |
| 1663 | swap(keys->addrs.v6addrs.src.s6_addr32[i], |
| 1664 | keys->addrs.v6addrs.dst.s6_addr32[i]); |
zhang kai | 1e60ceb | 2021-07-28 18:54:18 +0800 | [diff] [blame] | 1665 | } |
| 1666 | if ((__force u16)keys->ports.dst < |
| 1667 | (__force u16)keys->ports.src) { |
Tom Herbert | c3f8324 | 2015-06-04 09:16:40 -0700 | [diff] [blame] | 1668 | swap(keys->ports.src, keys->ports.dst); |
| 1669 | } |
| 1670 | break; |
| 1671 | } |
Hannes Frederic Sowa | 66415cf | 2013-10-23 20:06:00 +0200 | [diff] [blame] | 1672 | } |
| 1673 | |
Eric Dumazet | 5566744 | 2019-10-22 07:57:46 -0700 | [diff] [blame] | 1674 | static inline u32 __flow_hash_from_keys(struct flow_keys *keys, |
| 1675 | const siphash_key_t *keyval) |
Tom Herbert | 5ed20a6 | 2014-07-01 21:32:05 -0700 | [diff] [blame] | 1676 | { |
| 1677 | u32 hash; |
| 1678 | |
Tom Herbert | c3f8324 | 2015-06-04 09:16:40 -0700 | [diff] [blame] | 1679 | __flow_hash_consistentify(keys); |
Tom Herbert | 5ed20a6 | 2014-07-01 21:32:05 -0700 | [diff] [blame] | 1680 | |
Eric Dumazet | 5566744 | 2019-10-22 07:57:46 -0700 | [diff] [blame] | 1681 | hash = siphash(flow_keys_hash_start(keys), |
| 1682 | flow_keys_hash_length(keys), keyval); |
Tom Herbert | 5ed20a6 | 2014-07-01 21:32:05 -0700 | [diff] [blame] | 1683 | if (!hash) |
| 1684 | hash = 1; |
| 1685 | |
| 1686 | return hash; |
| 1687 | } |
| 1688 | |
| 1689 | u32 flow_hash_from_keys(struct flow_keys *keys) |
| 1690 | { |
Tom Herbert | 50fb799 | 2015-05-01 11:30:12 -0700 | [diff] [blame] | 1691 | __flow_hash_secret_init(); |
Eric Dumazet | 5566744 | 2019-10-22 07:57:46 -0700 | [diff] [blame] | 1692 | return __flow_hash_from_keys(keys, &hashrnd); |
Tom Herbert | 5ed20a6 | 2014-07-01 21:32:05 -0700 | [diff] [blame] | 1693 | } |
| 1694 | EXPORT_SYMBOL(flow_hash_from_keys); |
| 1695 | |
Tom Herbert | 50fb799 | 2015-05-01 11:30:12 -0700 | [diff] [blame] | 1696 | static inline u32 ___skb_get_hash(const struct sk_buff *skb, |
Eric Dumazet | 5566744 | 2019-10-22 07:57:46 -0700 | [diff] [blame] | 1697 | struct flow_keys *keys, |
| 1698 | const siphash_key_t *keyval) |
Tom Herbert | 50fb799 | 2015-05-01 11:30:12 -0700 | [diff] [blame] | 1699 | { |
Tom Herbert | 6db61d7 | 2015-09-01 09:24:33 -0700 | [diff] [blame] | 1700 | skb_flow_dissect_flow_keys(skb, keys, |
| 1701 | FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL); |
Tom Herbert | 50fb799 | 2015-05-01 11:30:12 -0700 | [diff] [blame] | 1702 | |
| 1703 | return __flow_hash_from_keys(keys, keyval); |
| 1704 | } |
| 1705 | |
Tom Herbert | 2f59e1e | 2015-05-01 11:30:17 -0700 | [diff] [blame] | 1706 | struct _flow_keys_digest_data { |
| 1707 | __be16 n_proto; |
| 1708 | u8 ip_proto; |
| 1709 | u8 padding; |
| 1710 | __be32 ports; |
| 1711 | __be32 src; |
| 1712 | __be32 dst; |
| 1713 | }; |
| 1714 | |
| 1715 | void make_flow_keys_digest(struct flow_keys_digest *digest, |
| 1716 | const struct flow_keys *flow) |
| 1717 | { |
| 1718 | struct _flow_keys_digest_data *data = |
| 1719 | (struct _flow_keys_digest_data *)digest; |
| 1720 | |
| 1721 | BUILD_BUG_ON(sizeof(*data) > sizeof(*digest)); |
| 1722 | |
| 1723 | memset(digest, 0, sizeof(*digest)); |
| 1724 | |
Jiri Pirko | 06635a3 | 2015-05-12 14:56:16 +0200 | [diff] [blame] | 1725 | data->n_proto = flow->basic.n_proto; |
| 1726 | data->ip_proto = flow->basic.ip_proto; |
| 1727 | data->ports = flow->ports.ports; |
Tom Herbert | c3f8324 | 2015-06-04 09:16:40 -0700 | [diff] [blame] | 1728 | data->src = flow->addrs.v4addrs.src; |
| 1729 | data->dst = flow->addrs.v4addrs.dst; |
Tom Herbert | 2f59e1e | 2015-05-01 11:30:17 -0700 | [diff] [blame] | 1730 | } |
| 1731 | EXPORT_SYMBOL(make_flow_keys_digest); |
| 1732 | |
David S. Miller | eb70db8 | 2016-07-01 16:07:50 -0400 | [diff] [blame] | 1733 | static struct flow_dissector flow_keys_dissector_symmetric __read_mostly; |
| 1734 | |
Florian Westphal | b917783 | 2016-10-26 18:49:46 +0200 | [diff] [blame] | 1735 | u32 __skb_get_hash_symmetric(const struct sk_buff *skb) |
David S. Miller | eb70db8 | 2016-07-01 16:07:50 -0400 | [diff] [blame] | 1736 | { |
| 1737 | struct flow_keys keys; |
| 1738 | |
| 1739 | __flow_hash_secret_init(); |
| 1740 | |
| 1741 | memset(&keys, 0, sizeof(keys)); |
Stanislav Fomichev | 3cbf4ff | 2019-04-22 08:55:46 -0700 | [diff] [blame] | 1742 | __skb_flow_dissect(NULL, skb, &flow_keys_dissector_symmetric, |
| 1743 | &keys, NULL, 0, 0, 0, |
David S. Miller | eb70db8 | 2016-07-01 16:07:50 -0400 | [diff] [blame] | 1744 | FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL); |
| 1745 | |
Eric Dumazet | 5566744 | 2019-10-22 07:57:46 -0700 | [diff] [blame] | 1746 | return __flow_hash_from_keys(&keys, &hashrnd); |
David S. Miller | eb70db8 | 2016-07-01 16:07:50 -0400 | [diff] [blame] | 1747 | } |
| 1748 | EXPORT_SYMBOL_GPL(__skb_get_hash_symmetric); |
| 1749 | |
Jiri Pirko | d4fd327 | 2015-05-12 14:56:10 +0200 | [diff] [blame] | 1750 | /** |
| 1751 | * __skb_get_hash: calculate a flow hash |
| 1752 | * @skb: sk_buff to calculate flow hash from |
| 1753 | * |
| 1754 | * This function calculates a flow hash based on src/dst addresses |
Tom Herbert | 61b905d | 2014-03-24 15:34:47 -0700 | [diff] [blame] | 1755 | * and src/dst port numbers. Sets hash in skb to non-zero hash value |
| 1756 | * on success, zero indicates no valid hash. Also, sets l4_hash in skb |
Cong Wang | 441d9d3 | 2013-01-21 00:39:24 +0000 | [diff] [blame] | 1757 | * if hash is a canonical 4-tuple hash over transport ports. |
| 1758 | */ |
Tom Herbert | 3958afa1b | 2013-12-15 22:12:06 -0800 | [diff] [blame] | 1759 | void __skb_get_hash(struct sk_buff *skb) |
Cong Wang | 441d9d3 | 2013-01-21 00:39:24 +0000 | [diff] [blame] | 1760 | { |
| 1761 | struct flow_keys keys; |
Gao Feng | 635c223 | 2016-08-31 14:15:05 +0800 | [diff] [blame] | 1762 | u32 hash; |
Cong Wang | 441d9d3 | 2013-01-21 00:39:24 +0000 | [diff] [blame] | 1763 | |
Tom Herbert | 50fb799 | 2015-05-01 11:30:12 -0700 | [diff] [blame] | 1764 | __flow_hash_secret_init(); |
| 1765 | |
Eric Dumazet | 5566744 | 2019-10-22 07:57:46 -0700 | [diff] [blame] | 1766 | hash = ___skb_get_hash(skb, &keys, &hashrnd); |
Gao Feng | 635c223 | 2016-08-31 14:15:05 +0800 | [diff] [blame] | 1767 | |
| 1768 | __skb_set_sw_hash(skb, hash, flow_keys_have_l4(&keys)); |
Cong Wang | 441d9d3 | 2013-01-21 00:39:24 +0000 | [diff] [blame] | 1769 | } |
Tom Herbert | 3958afa1b | 2013-12-15 22:12:06 -0800 | [diff] [blame] | 1770 | EXPORT_SYMBOL(__skb_get_hash); |
Cong Wang | 441d9d3 | 2013-01-21 00:39:24 +0000 | [diff] [blame] | 1771 | |
Eric Dumazet | 5566744 | 2019-10-22 07:57:46 -0700 | [diff] [blame] | 1772 | __u32 skb_get_hash_perturb(const struct sk_buff *skb, |
| 1773 | const siphash_key_t *perturb) |
Tom Herbert | 50fb799 | 2015-05-01 11:30:12 -0700 | [diff] [blame] | 1774 | { |
| 1775 | struct flow_keys keys; |
| 1776 | |
| 1777 | return ___skb_get_hash(skb, &keys, perturb); |
| 1778 | } |
| 1779 | EXPORT_SYMBOL(skb_get_hash_perturb); |
| 1780 | |
Alexander Lobakin | f96533c | 2021-03-14 11:11:23 +0000 | [diff] [blame] | 1781 | u32 __skb_get_poff(const struct sk_buff *skb, const void *data, |
Paolo Abeni | 72a338b | 2018-05-04 11:32:59 +0200 | [diff] [blame] | 1782 | const struct flow_keys_basic *keys, int hlen) |
Daniel Borkmann | f77668d | 2013-03-19 06:39:30 +0000 | [diff] [blame] | 1783 | { |
Tom Herbert | 42aecaa | 2015-06-04 09:16:39 -0700 | [diff] [blame] | 1784 | u32 poff = keys->control.thoff; |
Daniel Borkmann | f77668d | 2013-03-19 06:39:30 +0000 | [diff] [blame] | 1785 | |
Alexander Duyck | 43d2ccb | 2016-02-24 09:29:44 -0800 | [diff] [blame] | 1786 | /* skip L4 headers for fragments after the first */ |
| 1787 | if ((keys->control.flags & FLOW_DIS_IS_FRAGMENT) && |
| 1788 | !(keys->control.flags & FLOW_DIS_FIRST_FRAG)) |
| 1789 | return poff; |
| 1790 | |
Jiri Pirko | 06635a3 | 2015-05-12 14:56:16 +0200 | [diff] [blame] | 1791 | switch (keys->basic.ip_proto) { |
Daniel Borkmann | f77668d | 2013-03-19 06:39:30 +0000 | [diff] [blame] | 1792 | case IPPROTO_TCP: { |
Alexander Duyck | 5af7fb6 | 2014-10-10 12:09:12 -0700 | [diff] [blame] | 1793 | /* access doff as u8 to avoid unaligned access */ |
| 1794 | const u8 *doff; |
| 1795 | u8 _doff; |
Daniel Borkmann | f77668d | 2013-03-19 06:39:30 +0000 | [diff] [blame] | 1796 | |
Alexander Duyck | 5af7fb6 | 2014-10-10 12:09:12 -0700 | [diff] [blame] | 1797 | doff = __skb_header_pointer(skb, poff + 12, sizeof(_doff), |
| 1798 | data, hlen, &_doff); |
| 1799 | if (!doff) |
Daniel Borkmann | f77668d | 2013-03-19 06:39:30 +0000 | [diff] [blame] | 1800 | return poff; |
| 1801 | |
Alexander Duyck | 5af7fb6 | 2014-10-10 12:09:12 -0700 | [diff] [blame] | 1802 | poff += max_t(u32, sizeof(struct tcphdr), (*doff & 0xF0) >> 2); |
Daniel Borkmann | f77668d | 2013-03-19 06:39:30 +0000 | [diff] [blame] | 1803 | break; |
| 1804 | } |
| 1805 | case IPPROTO_UDP: |
| 1806 | case IPPROTO_UDPLITE: |
| 1807 | poff += sizeof(struct udphdr); |
| 1808 | break; |
| 1809 | /* For the rest, we do not really care about header |
| 1810 | * extensions at this point for now. |
| 1811 | */ |
| 1812 | case IPPROTO_ICMP: |
| 1813 | poff += sizeof(struct icmphdr); |
| 1814 | break; |
| 1815 | case IPPROTO_ICMPV6: |
| 1816 | poff += sizeof(struct icmp6hdr); |
| 1817 | break; |
| 1818 | case IPPROTO_IGMP: |
| 1819 | poff += sizeof(struct igmphdr); |
| 1820 | break; |
| 1821 | case IPPROTO_DCCP: |
| 1822 | poff += sizeof(struct dccp_hdr); |
| 1823 | break; |
| 1824 | case IPPROTO_SCTP: |
| 1825 | poff += sizeof(struct sctphdr); |
| 1826 | break; |
| 1827 | } |
| 1828 | |
| 1829 | return poff; |
| 1830 | } |
| 1831 | |
Jiri Pirko | 0db89b8 | 2015-05-12 14:56:14 +0200 | [diff] [blame] | 1832 | /** |
| 1833 | * skb_get_poff - get the offset to the payload |
| 1834 | * @skb: sk_buff to get the payload offset from |
| 1835 | * |
| 1836 | * The function will get the offset to the payload as far as it could |
| 1837 | * be dissected. The main user is currently BPF, so that we can dynamically |
Alexander Duyck | 56193d1 | 2014-09-05 19:20:26 -0400 | [diff] [blame] | 1838 | * truncate packets without needing to push actual payload to the user |
| 1839 | * space and can analyze headers only, instead. |
| 1840 | */ |
| 1841 | u32 skb_get_poff(const struct sk_buff *skb) |
| 1842 | { |
Paolo Abeni | 72a338b | 2018-05-04 11:32:59 +0200 | [diff] [blame] | 1843 | struct flow_keys_basic keys; |
Alexander Duyck | 56193d1 | 2014-09-05 19:20:26 -0400 | [diff] [blame] | 1844 | |
Stanislav Fomichev | 3cbf4ff | 2019-04-22 08:55:46 -0700 | [diff] [blame] | 1845 | if (!skb_flow_dissect_flow_keys_basic(NULL, skb, &keys, |
| 1846 | NULL, 0, 0, 0, 0)) |
Alexander Duyck | 56193d1 | 2014-09-05 19:20:26 -0400 | [diff] [blame] | 1847 | return 0; |
| 1848 | |
| 1849 | return __skb_get_poff(skb, skb->data, &keys, skb_headlen(skb)); |
| 1850 | } |
Jiri Pirko | 06635a3 | 2015-05-12 14:56:16 +0200 | [diff] [blame] | 1851 | |
David S. Miller | 20a17bf | 2015-09-01 21:19:17 -0700 | [diff] [blame] | 1852 | __u32 __get_hash_from_flowi6(const struct flowi6 *fl6, struct flow_keys *keys) |
David S. Miller | a17ace9 | 2015-09-01 17:00:24 -0700 | [diff] [blame] | 1853 | { |
| 1854 | memset(keys, 0, sizeof(*keys)); |
| 1855 | |
| 1856 | memcpy(&keys->addrs.v6addrs.src, &fl6->saddr, |
| 1857 | sizeof(keys->addrs.v6addrs.src)); |
| 1858 | memcpy(&keys->addrs.v6addrs.dst, &fl6->daddr, |
| 1859 | sizeof(keys->addrs.v6addrs.dst)); |
| 1860 | keys->control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS; |
| 1861 | keys->ports.src = fl6->fl6_sport; |
| 1862 | keys->ports.dst = fl6->fl6_dport; |
| 1863 | keys->keyid.keyid = fl6->fl6_gre_key; |
Michal Kubecek | fa1be7e | 2018-06-04 11:36:05 +0200 | [diff] [blame] | 1864 | keys->tags.flow_label = (__force u32)flowi6_get_flowlabel(fl6); |
David S. Miller | a17ace9 | 2015-09-01 17:00:24 -0700 | [diff] [blame] | 1865 | keys->basic.ip_proto = fl6->flowi6_proto; |
| 1866 | |
| 1867 | return flow_hash_from_keys(keys); |
| 1868 | } |
| 1869 | EXPORT_SYMBOL(__get_hash_from_flowi6); |
| 1870 | |
Jiri Pirko | 06635a3 | 2015-05-12 14:56:16 +0200 | [diff] [blame] | 1871 | static const struct flow_dissector_key flow_keys_dissector_keys[] = { |
| 1872 | { |
Tom Herbert | 42aecaa | 2015-06-04 09:16:39 -0700 | [diff] [blame] | 1873 | .key_id = FLOW_DISSECTOR_KEY_CONTROL, |
| 1874 | .offset = offsetof(struct flow_keys, control), |
| 1875 | }, |
| 1876 | { |
Jiri Pirko | 06635a3 | 2015-05-12 14:56:16 +0200 | [diff] [blame] | 1877 | .key_id = FLOW_DISSECTOR_KEY_BASIC, |
| 1878 | .offset = offsetof(struct flow_keys, basic), |
| 1879 | }, |
| 1880 | { |
| 1881 | .key_id = FLOW_DISSECTOR_KEY_IPV4_ADDRS, |
Tom Herbert | c3f8324 | 2015-06-04 09:16:40 -0700 | [diff] [blame] | 1882 | .offset = offsetof(struct flow_keys, addrs.v4addrs), |
| 1883 | }, |
| 1884 | { |
| 1885 | .key_id = FLOW_DISSECTOR_KEY_IPV6_ADDRS, |
| 1886 | .offset = offsetof(struct flow_keys, addrs.v6addrs), |
Jiri Pirko | 06635a3 | 2015-05-12 14:56:16 +0200 | [diff] [blame] | 1887 | }, |
| 1888 | { |
Jon Maloy | 8d6e79d | 2017-11-08 09:59:26 +0100 | [diff] [blame] | 1889 | .key_id = FLOW_DISSECTOR_KEY_TIPC, |
| 1890 | .offset = offsetof(struct flow_keys, addrs.tipckey), |
Tom Herbert | 9f24908 | 2015-06-04 09:16:41 -0700 | [diff] [blame] | 1891 | }, |
| 1892 | { |
Jiri Pirko | 06635a3 | 2015-05-12 14:56:16 +0200 | [diff] [blame] | 1893 | .key_id = FLOW_DISSECTOR_KEY_PORTS, |
| 1894 | .offset = offsetof(struct flow_keys, ports), |
| 1895 | }, |
Tom Herbert | d34af82 | 2015-06-04 09:16:43 -0700 | [diff] [blame] | 1896 | { |
Hadar Hen Zion | f6a6692 | 2016-08-17 13:36:11 +0300 | [diff] [blame] | 1897 | .key_id = FLOW_DISSECTOR_KEY_VLAN, |
| 1898 | .offset = offsetof(struct flow_keys, vlan), |
Tom Herbert | d34af82 | 2015-06-04 09:16:43 -0700 | [diff] [blame] | 1899 | }, |
Tom Herbert | 87ee9e5 | 2015-06-04 09:16:44 -0700 | [diff] [blame] | 1900 | { |
| 1901 | .key_id = FLOW_DISSECTOR_KEY_FLOW_LABEL, |
| 1902 | .offset = offsetof(struct flow_keys, tags), |
| 1903 | }, |
Tom Herbert | 1fdd512 | 2015-06-04 09:16:45 -0700 | [diff] [blame] | 1904 | { |
| 1905 | .key_id = FLOW_DISSECTOR_KEY_GRE_KEYID, |
| 1906 | .offset = offsetof(struct flow_keys, keyid), |
| 1907 | }, |
Jiri Pirko | 06635a3 | 2015-05-12 14:56:16 +0200 | [diff] [blame] | 1908 | }; |
| 1909 | |
David S. Miller | eb70db8 | 2016-07-01 16:07:50 -0400 | [diff] [blame] | 1910 | static const struct flow_dissector_key flow_keys_dissector_symmetric_keys[] = { |
| 1911 | { |
| 1912 | .key_id = FLOW_DISSECTOR_KEY_CONTROL, |
| 1913 | .offset = offsetof(struct flow_keys, control), |
| 1914 | }, |
| 1915 | { |
| 1916 | .key_id = FLOW_DISSECTOR_KEY_BASIC, |
| 1917 | .offset = offsetof(struct flow_keys, basic), |
| 1918 | }, |
| 1919 | { |
| 1920 | .key_id = FLOW_DISSECTOR_KEY_IPV4_ADDRS, |
| 1921 | .offset = offsetof(struct flow_keys, addrs.v4addrs), |
| 1922 | }, |
| 1923 | { |
| 1924 | .key_id = FLOW_DISSECTOR_KEY_IPV6_ADDRS, |
| 1925 | .offset = offsetof(struct flow_keys, addrs.v6addrs), |
| 1926 | }, |
| 1927 | { |
| 1928 | .key_id = FLOW_DISSECTOR_KEY_PORTS, |
| 1929 | .offset = offsetof(struct flow_keys, ports), |
| 1930 | }, |
| 1931 | }; |
| 1932 | |
Paolo Abeni | 72a338b | 2018-05-04 11:32:59 +0200 | [diff] [blame] | 1933 | static const struct flow_dissector_key flow_keys_basic_dissector_keys[] = { |
Jiri Pirko | 06635a3 | 2015-05-12 14:56:16 +0200 | [diff] [blame] | 1934 | { |
Tom Herbert | 42aecaa | 2015-06-04 09:16:39 -0700 | [diff] [blame] | 1935 | .key_id = FLOW_DISSECTOR_KEY_CONTROL, |
| 1936 | .offset = offsetof(struct flow_keys, control), |
| 1937 | }, |
| 1938 | { |
Jiri Pirko | 06635a3 | 2015-05-12 14:56:16 +0200 | [diff] [blame] | 1939 | .key_id = FLOW_DISSECTOR_KEY_BASIC, |
| 1940 | .offset = offsetof(struct flow_keys, basic), |
| 1941 | }, |
| 1942 | }; |
| 1943 | |
| 1944 | struct flow_dissector flow_keys_dissector __read_mostly; |
| 1945 | EXPORT_SYMBOL(flow_keys_dissector); |
| 1946 | |
Paolo Abeni | 72a338b | 2018-05-04 11:32:59 +0200 | [diff] [blame] | 1947 | struct flow_dissector flow_keys_basic_dissector __read_mostly; |
| 1948 | EXPORT_SYMBOL(flow_keys_basic_dissector); |
Jiri Pirko | 06635a3 | 2015-05-12 14:56:16 +0200 | [diff] [blame] | 1949 | |
| 1950 | static int __init init_default_flow_dissectors(void) |
| 1951 | { |
| 1952 | skb_flow_dissector_init(&flow_keys_dissector, |
| 1953 | flow_keys_dissector_keys, |
| 1954 | ARRAY_SIZE(flow_keys_dissector_keys)); |
David S. Miller | eb70db8 | 2016-07-01 16:07:50 -0400 | [diff] [blame] | 1955 | skb_flow_dissector_init(&flow_keys_dissector_symmetric, |
| 1956 | flow_keys_dissector_symmetric_keys, |
| 1957 | ARRAY_SIZE(flow_keys_dissector_symmetric_keys)); |
Paolo Abeni | 72a338b | 2018-05-04 11:32:59 +0200 | [diff] [blame] | 1958 | skb_flow_dissector_init(&flow_keys_basic_dissector, |
| 1959 | flow_keys_basic_dissector_keys, |
| 1960 | ARRAY_SIZE(flow_keys_basic_dissector_keys)); |
Jakub Sitnicki | b27f7bb | 2020-05-31 10:28:37 +0200 | [diff] [blame] | 1961 | return 0; |
Jakub Sitnicki | 5cf6592 | 2020-05-21 10:34:35 +0200 | [diff] [blame] | 1962 | } |
Eric Dumazet | c9b8af1 | 2016-11-22 11:17:30 -0800 | [diff] [blame] | 1963 | core_initcall(init_default_flow_dissectors); |