Jiri Pirko | fbff949 | 2015-05-12 14:56:15 +0200 | [diff] [blame] | 1 | #include <linux/kernel.h> |
Eric Dumazet | 0744dd0 | 2011-11-28 05:22:18 +0000 | [diff] [blame] | 2 | #include <linux/skbuff.h> |
Jesper Dangaard Brouer | c452ed7 | 2012-01-24 16:03:33 -0500 | [diff] [blame] | 3 | #include <linux/export.h> |
Eric Dumazet | 0744dd0 | 2011-11-28 05:22:18 +0000 | [diff] [blame] | 4 | #include <linux/ip.h> |
| 5 | #include <linux/ipv6.h> |
| 6 | #include <linux/if_vlan.h> |
| 7 | #include <net/ip.h> |
Eric Dumazet | ddbe503 | 2012-07-18 08:11:12 +0000 | [diff] [blame] | 8 | #include <net/ipv6.h> |
Gao Feng | ab10dcc | 2016-08-09 12:38:24 +0800 | [diff] [blame] | 9 | #include <net/gre.h> |
| 10 | #include <net/pptp.h> |
Daniel Borkmann | f77668d | 2013-03-19 06:39:30 +0000 | [diff] [blame] | 11 | #include <linux/igmp.h> |
| 12 | #include <linux/icmp.h> |
| 13 | #include <linux/sctp.h> |
| 14 | #include <linux/dccp.h> |
Eric Dumazet | 0744dd0 | 2011-11-28 05:22:18 +0000 | [diff] [blame] | 15 | #include <linux/if_tunnel.h> |
| 16 | #include <linux/if_pppox.h> |
| 17 | #include <linux/ppp_defs.h> |
Jiri Pirko | 06635a3 | 2015-05-12 14:56:16 +0200 | [diff] [blame] | 18 | #include <linux/stddef.h> |
Jiri Pirko | 67a900c | 2015-05-12 14:56:19 +0200 | [diff] [blame] | 19 | #include <linux/if_ether.h> |
Tom Herbert | b3baa0f | 2015-06-04 09:16:46 -0700 | [diff] [blame] | 20 | #include <linux/mpls.h> |
Jiri Pirko | 1bd758e | 2015-05-12 14:56:07 +0200 | [diff] [blame] | 21 | #include <net/flow_dissector.h> |
Alexander Duyck | 56193d1 | 2014-09-05 19:20:26 -0400 | [diff] [blame] | 22 | #include <scsi/fc/fc_fcoe.h> |
Eric Dumazet | 0744dd0 | 2011-11-28 05:22:18 +0000 | [diff] [blame] | 23 | |
David S. Miller | 20a17bf | 2015-09-01 21:19:17 -0700 | [diff] [blame] | 24 | static void dissector_set_key(struct flow_dissector *flow_dissector, |
| 25 | enum flow_dissector_key_id key_id) |
Jiri Pirko | fbff949 | 2015-05-12 14:56:15 +0200 | [diff] [blame] | 26 | { |
| 27 | flow_dissector->used_keys |= (1 << key_id); |
| 28 | } |
| 29 | |
Jiri Pirko | fbff949 | 2015-05-12 14:56:15 +0200 | [diff] [blame] | 30 | void skb_flow_dissector_init(struct flow_dissector *flow_dissector, |
| 31 | const struct flow_dissector_key *key, |
| 32 | unsigned int key_count) |
| 33 | { |
| 34 | unsigned int i; |
| 35 | |
| 36 | memset(flow_dissector, 0, sizeof(*flow_dissector)); |
| 37 | |
| 38 | for (i = 0; i < key_count; i++, key++) { |
| 39 | /* User should make sure that every key target offset is withing |
| 40 | * boundaries of unsigned short. |
| 41 | */ |
| 42 | BUG_ON(key->offset > USHRT_MAX); |
David S. Miller | 20a17bf | 2015-09-01 21:19:17 -0700 | [diff] [blame] | 43 | BUG_ON(dissector_uses_key(flow_dissector, |
| 44 | key->key_id)); |
Jiri Pirko | fbff949 | 2015-05-12 14:56:15 +0200 | [diff] [blame] | 45 | |
David S. Miller | 20a17bf | 2015-09-01 21:19:17 -0700 | [diff] [blame] | 46 | dissector_set_key(flow_dissector, key->key_id); |
Jiri Pirko | fbff949 | 2015-05-12 14:56:15 +0200 | [diff] [blame] | 47 | flow_dissector->offset[key->key_id] = key->offset; |
| 48 | } |
| 49 | |
Tom Herbert | 42aecaa | 2015-06-04 09:16:39 -0700 | [diff] [blame] | 50 | /* Ensure that the dissector always includes control and basic key. |
| 51 | * 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] | 52 | */ |
David S. Miller | 20a17bf | 2015-09-01 21:19:17 -0700 | [diff] [blame] | 53 | BUG_ON(!dissector_uses_key(flow_dissector, |
| 54 | FLOW_DISSECTOR_KEY_CONTROL)); |
| 55 | BUG_ON(!dissector_uses_key(flow_dissector, |
| 56 | FLOW_DISSECTOR_KEY_BASIC)); |
Jiri Pirko | fbff949 | 2015-05-12 14:56:15 +0200 | [diff] [blame] | 57 | } |
| 58 | EXPORT_SYMBOL(skb_flow_dissector_init); |
| 59 | |
Nikolay Aleksandrov | 357afe9 | 2013-10-02 13:39:24 +0200 | [diff] [blame] | 60 | /** |
WANG Cong | 6451b3f | 2014-08-25 17:03:46 -0700 | [diff] [blame] | 61 | * __skb_flow_get_ports - extract the upper layer ports and return them |
| 62 | * @skb: sk_buff to extract the ports from |
Nikolay Aleksandrov | 357afe9 | 2013-10-02 13:39:24 +0200 | [diff] [blame] | 63 | * @thoff: transport header offset |
| 64 | * @ip_proto: protocol for which to get port offset |
WANG Cong | 6451b3f | 2014-08-25 17:03:46 -0700 | [diff] [blame] | 65 | * @data: raw buffer pointer to the packet, if NULL use skb->data |
| 66 | * @hlen: packet header length, if @data is NULL use skb_headlen(skb) |
Nikolay Aleksandrov | 357afe9 | 2013-10-02 13:39:24 +0200 | [diff] [blame] | 67 | * |
| 68 | * The function will try to retrieve the ports at offset thoff + poff where poff |
| 69 | * is the protocol port offset returned from proto_ports_offset |
| 70 | */ |
David S. Miller | 690e36e | 2014-08-23 12:13:41 -0700 | [diff] [blame] | 71 | __be32 __skb_flow_get_ports(const struct sk_buff *skb, int thoff, u8 ip_proto, |
| 72 | void *data, int hlen) |
Nikolay Aleksandrov | 357afe9 | 2013-10-02 13:39:24 +0200 | [diff] [blame] | 73 | { |
| 74 | int poff = proto_ports_offset(ip_proto); |
| 75 | |
David S. Miller | 690e36e | 2014-08-23 12:13:41 -0700 | [diff] [blame] | 76 | if (!data) { |
| 77 | data = skb->data; |
| 78 | hlen = skb_headlen(skb); |
| 79 | } |
| 80 | |
Nikolay Aleksandrov | 357afe9 | 2013-10-02 13:39:24 +0200 | [diff] [blame] | 81 | if (poff >= 0) { |
| 82 | __be32 *ports, _ports; |
| 83 | |
David S. Miller | 690e36e | 2014-08-23 12:13:41 -0700 | [diff] [blame] | 84 | ports = __skb_header_pointer(skb, thoff + poff, |
| 85 | sizeof(_ports), data, hlen, &_ports); |
Nikolay Aleksandrov | 357afe9 | 2013-10-02 13:39:24 +0200 | [diff] [blame] | 86 | if (ports) |
| 87 | return *ports; |
| 88 | } |
| 89 | |
| 90 | return 0; |
| 91 | } |
David S. Miller | 690e36e | 2014-08-23 12:13:41 -0700 | [diff] [blame] | 92 | EXPORT_SYMBOL(__skb_flow_get_ports); |
Nikolay Aleksandrov | 357afe9 | 2013-10-02 13:39:24 +0200 | [diff] [blame] | 93 | |
WANG Cong | 453a940 | 2014-08-25 17:03:47 -0700 | [diff] [blame] | 94 | /** |
| 95 | * __skb_flow_dissect - extract the flow_keys struct and return it |
| 96 | * @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] | 97 | * @flow_dissector: list of keys to dissect |
| 98 | * @target_container: target structure to put dissected values into |
WANG Cong | 453a940 | 2014-08-25 17:03:47 -0700 | [diff] [blame] | 99 | * @data: raw buffer pointer to the packet, if NULL use skb->data |
| 100 | * @proto: protocol for which to get the flow, if @data is NULL use skb->protocol |
| 101 | * @nhoff: network header offset, if @data is NULL use skb_network_offset(skb) |
| 102 | * @hlen: packet header length, if @data is NULL use skb_headlen(skb) |
| 103 | * |
Jiri Pirko | 06635a3 | 2015-05-12 14:56:16 +0200 | [diff] [blame] | 104 | * The function will try to retrieve individual keys into target specified |
| 105 | * by flow_dissector from either the skbuff or a raw buffer specified by the |
| 106 | * rest parameters. |
| 107 | * |
| 108 | * Caller must take care of zeroing target container memory. |
WANG Cong | 453a940 | 2014-08-25 17:03:47 -0700 | [diff] [blame] | 109 | */ |
Jiri Pirko | 06635a3 | 2015-05-12 14:56:16 +0200 | [diff] [blame] | 110 | bool __skb_flow_dissect(const struct sk_buff *skb, |
| 111 | struct flow_dissector *flow_dissector, |
| 112 | void *target_container, |
Tom Herbert | cd79a23 | 2015-09-01 09:24:27 -0700 | [diff] [blame] | 113 | void *data, __be16 proto, int nhoff, int hlen, |
| 114 | unsigned int flags) |
Eric Dumazet | 0744dd0 | 2011-11-28 05:22:18 +0000 | [diff] [blame] | 115 | { |
Tom Herbert | 42aecaa | 2015-06-04 09:16:39 -0700 | [diff] [blame] | 116 | struct flow_dissector_key_control *key_control; |
Jiri Pirko | 06635a3 | 2015-05-12 14:56:16 +0200 | [diff] [blame] | 117 | struct flow_dissector_key_basic *key_basic; |
| 118 | struct flow_dissector_key_addrs *key_addrs; |
| 119 | struct flow_dissector_key_ports *key_ports; |
Tom Herbert | d34af82 | 2015-06-04 09:16:43 -0700 | [diff] [blame] | 120 | struct flow_dissector_key_tags *key_tags; |
Hadar Hen Zion | f6a6692 | 2016-08-17 13:36:11 +0300 | [diff] [blame] | 121 | struct flow_dissector_key_vlan *key_vlan; |
Tom Herbert | 1fdd512 | 2015-06-04 09:16:45 -0700 | [diff] [blame] | 122 | struct flow_dissector_key_keyid *key_keyid; |
Hadar Hen Zion | d5709f7 | 2016-08-17 13:36:10 +0300 | [diff] [blame] | 123 | bool skip_vlan = false; |
Geert Uytterhoeven | 8e690ff | 2015-06-25 15:10:32 +0200 | [diff] [blame] | 124 | u8 ip_proto = 0; |
Tom Herbert | a6e544b | 2015-09-01 09:24:26 -0700 | [diff] [blame] | 125 | bool ret = false; |
Eric Dumazet | 0744dd0 | 2011-11-28 05:22:18 +0000 | [diff] [blame] | 126 | |
David S. Miller | 690e36e | 2014-08-23 12:13:41 -0700 | [diff] [blame] | 127 | if (!data) { |
| 128 | data = skb->data; |
Hadar Hen Zion | d5709f7 | 2016-08-17 13:36:10 +0300 | [diff] [blame] | 129 | proto = skb_vlan_tag_present(skb) ? |
| 130 | skb->vlan_proto : skb->protocol; |
WANG Cong | 453a940 | 2014-08-25 17:03:47 -0700 | [diff] [blame] | 131 | nhoff = skb_network_offset(skb); |
David S. Miller | 690e36e | 2014-08-23 12:13:41 -0700 | [diff] [blame] | 132 | hlen = skb_headlen(skb); |
| 133 | } |
| 134 | |
Tom Herbert | 42aecaa | 2015-06-04 09:16:39 -0700 | [diff] [blame] | 135 | /* It is ensured by skb_flow_dissector_init() that control key will |
| 136 | * be always present. |
| 137 | */ |
| 138 | key_control = skb_flow_dissector_target(flow_dissector, |
| 139 | FLOW_DISSECTOR_KEY_CONTROL, |
| 140 | target_container); |
| 141 | |
Jiri Pirko | 06635a3 | 2015-05-12 14:56:16 +0200 | [diff] [blame] | 142 | /* It is ensured by skb_flow_dissector_init() that basic key will |
| 143 | * be always present. |
| 144 | */ |
| 145 | key_basic = skb_flow_dissector_target(flow_dissector, |
| 146 | FLOW_DISSECTOR_KEY_BASIC, |
| 147 | target_container); |
Eric Dumazet | 0744dd0 | 2011-11-28 05:22:18 +0000 | [diff] [blame] | 148 | |
David S. Miller | 20a17bf | 2015-09-01 21:19:17 -0700 | [diff] [blame] | 149 | if (dissector_uses_key(flow_dissector, |
| 150 | FLOW_DISSECTOR_KEY_ETH_ADDRS)) { |
Jiri Pirko | 67a900c | 2015-05-12 14:56:19 +0200 | [diff] [blame] | 151 | struct ethhdr *eth = eth_hdr(skb); |
| 152 | struct flow_dissector_key_eth_addrs *key_eth_addrs; |
| 153 | |
| 154 | key_eth_addrs = skb_flow_dissector_target(flow_dissector, |
| 155 | FLOW_DISSECTOR_KEY_ETH_ADDRS, |
| 156 | target_container); |
| 157 | memcpy(key_eth_addrs, ð->h_dest, sizeof(*key_eth_addrs)); |
| 158 | } |
| 159 | |
Eric Dumazet | 0744dd0 | 2011-11-28 05:22:18 +0000 | [diff] [blame] | 160 | again: |
| 161 | switch (proto) { |
Joe Perches | 2b8837a | 2014-03-12 10:04:17 -0700 | [diff] [blame] | 162 | case htons(ETH_P_IP): { |
Eric Dumazet | 0744dd0 | 2011-11-28 05:22:18 +0000 | [diff] [blame] | 163 | const struct iphdr *iph; |
| 164 | struct iphdr _iph; |
| 165 | ip: |
David S. Miller | 690e36e | 2014-08-23 12:13:41 -0700 | [diff] [blame] | 166 | iph = __skb_header_pointer(skb, nhoff, sizeof(_iph), data, hlen, &_iph); |
Jason Wang | 6f09234 | 2013-11-01 15:01:10 +0800 | [diff] [blame] | 167 | if (!iph || iph->ihl < 5) |
Tom Herbert | a6e544b | 2015-09-01 09:24:26 -0700 | [diff] [blame] | 168 | goto out_bad; |
Eric Dumazet | 3797d3e | 2013-11-07 08:37:28 -0800 | [diff] [blame] | 169 | nhoff += iph->ihl * 4; |
Eric Dumazet | 0744dd0 | 2011-11-28 05:22:18 +0000 | [diff] [blame] | 170 | |
Eric Dumazet | 3797d3e | 2013-11-07 08:37:28 -0800 | [diff] [blame] | 171 | ip_proto = iph->protocol; |
Eric Dumazet | 3797d3e | 2013-11-07 08:37:28 -0800 | [diff] [blame] | 172 | |
Alexander Duyck | 918c023 | 2016-02-24 09:29:38 -0800 | [diff] [blame] | 173 | if (dissector_uses_key(flow_dissector, |
| 174 | FLOW_DISSECTOR_KEY_IPV4_ADDRS)) { |
| 175 | key_addrs = skb_flow_dissector_target(flow_dissector, |
| 176 | FLOW_DISSECTOR_KEY_IPV4_ADDRS, |
| 177 | target_container); |
Tom Herbert | c3f8324 | 2015-06-04 09:16:40 -0700 | [diff] [blame] | 178 | |
Alexander Duyck | 918c023 | 2016-02-24 09:29:38 -0800 | [diff] [blame] | 179 | memcpy(&key_addrs->v4addrs, &iph->saddr, |
| 180 | sizeof(key_addrs->v4addrs)); |
| 181 | key_control->addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS; |
| 182 | } |
Tom Herbert | 807e165 | 2015-09-01 09:24:28 -0700 | [diff] [blame] | 183 | |
| 184 | if (ip_is_fragment(iph)) { |
David S. Miller | 4b36993 | 2015-09-01 16:46:08 -0700 | [diff] [blame] | 185 | key_control->flags |= FLOW_DIS_IS_FRAGMENT; |
Tom Herbert | 807e165 | 2015-09-01 09:24:28 -0700 | [diff] [blame] | 186 | |
| 187 | if (iph->frag_off & htons(IP_OFFSET)) { |
| 188 | goto out_good; |
| 189 | } else { |
David S. Miller | 4b36993 | 2015-09-01 16:46:08 -0700 | [diff] [blame] | 190 | key_control->flags |= FLOW_DIS_FIRST_FRAG; |
Tom Herbert | 807e165 | 2015-09-01 09:24:28 -0700 | [diff] [blame] | 191 | if (!(flags & FLOW_DISSECTOR_F_PARSE_1ST_FRAG)) |
| 192 | goto out_good; |
| 193 | } |
| 194 | } |
| 195 | |
Tom Herbert | 8306b68 | 2015-09-01 09:24:30 -0700 | [diff] [blame] | 196 | if (flags & FLOW_DISSECTOR_F_STOP_AT_L3) |
| 197 | goto out_good; |
| 198 | |
Eric Dumazet | 0744dd0 | 2011-11-28 05:22:18 +0000 | [diff] [blame] | 199 | break; |
| 200 | } |
Joe Perches | 2b8837a | 2014-03-12 10:04:17 -0700 | [diff] [blame] | 201 | case htons(ETH_P_IPV6): { |
Eric Dumazet | 0744dd0 | 2011-11-28 05:22:18 +0000 | [diff] [blame] | 202 | const struct ipv6hdr *iph; |
| 203 | struct ipv6hdr _iph; |
Tom Herbert | 19469a8 | 2014-07-01 21:33:01 -0700 | [diff] [blame] | 204 | |
Eric Dumazet | 0744dd0 | 2011-11-28 05:22:18 +0000 | [diff] [blame] | 205 | ipv6: |
David S. Miller | 690e36e | 2014-08-23 12:13:41 -0700 | [diff] [blame] | 206 | iph = __skb_header_pointer(skb, nhoff, sizeof(_iph), data, hlen, &_iph); |
Eric Dumazet | 0744dd0 | 2011-11-28 05:22:18 +0000 | [diff] [blame] | 207 | if (!iph) |
Tom Herbert | a6e544b | 2015-09-01 09:24:26 -0700 | [diff] [blame] | 208 | goto out_bad; |
Eric Dumazet | 0744dd0 | 2011-11-28 05:22:18 +0000 | [diff] [blame] | 209 | |
| 210 | ip_proto = iph->nexthdr; |
Eric Dumazet | 0744dd0 | 2011-11-28 05:22:18 +0000 | [diff] [blame] | 211 | nhoff += sizeof(struct ipv6hdr); |
Tom Herbert | 19469a8 | 2014-07-01 21:33:01 -0700 | [diff] [blame] | 212 | |
David S. Miller | 20a17bf | 2015-09-01 21:19:17 -0700 | [diff] [blame] | 213 | if (dissector_uses_key(flow_dissector, |
| 214 | FLOW_DISSECTOR_KEY_IPV6_ADDRS)) { |
Alexander Duyck | b3c3106 | 2016-02-24 09:29:57 -0800 | [diff] [blame] | 215 | key_addrs = skb_flow_dissector_target(flow_dissector, |
| 216 | FLOW_DISSECTOR_KEY_IPV6_ADDRS, |
| 217 | target_container); |
Alexander Duyck | 5af7fb6 | 2014-10-10 12:09:12 -0700 | [diff] [blame] | 218 | |
Alexander Duyck | b3c3106 | 2016-02-24 09:29:57 -0800 | [diff] [blame] | 219 | memcpy(&key_addrs->v6addrs, &iph->saddr, |
| 220 | sizeof(key_addrs->v6addrs)); |
Tom Herbert | c3f8324 | 2015-06-04 09:16:40 -0700 | [diff] [blame] | 221 | key_control->addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS; |
Jiri Pirko | b924933 | 2015-05-12 14:56:18 +0200 | [diff] [blame] | 222 | } |
Tom Herbert | 87ee9e5 | 2015-06-04 09:16:44 -0700 | [diff] [blame] | 223 | |
Alexander Duyck | 461547f | 2016-02-09 02:49:54 -0800 | [diff] [blame] | 224 | if ((dissector_uses_key(flow_dissector, |
| 225 | FLOW_DISSECTOR_KEY_FLOW_LABEL) || |
| 226 | (flags & FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL)) && |
| 227 | ip6_flowlabel(iph)) { |
| 228 | __be32 flow_label = ip6_flowlabel(iph); |
| 229 | |
David S. Miller | 20a17bf | 2015-09-01 21:19:17 -0700 | [diff] [blame] | 230 | if (dissector_uses_key(flow_dissector, |
| 231 | FLOW_DISSECTOR_KEY_FLOW_LABEL)) { |
Tom Herbert | 87ee9e5 | 2015-06-04 09:16:44 -0700 | [diff] [blame] | 232 | key_tags = skb_flow_dissector_target(flow_dissector, |
| 233 | FLOW_DISSECTOR_KEY_FLOW_LABEL, |
| 234 | target_container); |
| 235 | key_tags->flow_label = ntohl(flow_label); |
Jiri Pirko | 12c227e | 2015-05-22 11:05:58 +0200 | [diff] [blame] | 236 | } |
Tom Herbert | 872b1ab | 2015-09-01 09:24:31 -0700 | [diff] [blame] | 237 | if (flags & FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL) |
| 238 | goto out_good; |
Tom Herbert | 19469a8 | 2014-07-01 21:33:01 -0700 | [diff] [blame] | 239 | } |
| 240 | |
Tom Herbert | 8306b68 | 2015-09-01 09:24:30 -0700 | [diff] [blame] | 241 | if (flags & FLOW_DISSECTOR_F_STOP_AT_L3) |
| 242 | goto out_good; |
| 243 | |
Eric Dumazet | 0744dd0 | 2011-11-28 05:22:18 +0000 | [diff] [blame] | 244 | break; |
| 245 | } |
Joe Perches | 2b8837a | 2014-03-12 10:04:17 -0700 | [diff] [blame] | 246 | case htons(ETH_P_8021AD): |
| 247 | case htons(ETH_P_8021Q): { |
Eric Dumazet | 0744dd0 | 2011-11-28 05:22:18 +0000 | [diff] [blame] | 248 | const struct vlan_hdr *vlan; |
Eric Dumazet | 0744dd0 | 2011-11-28 05:22:18 +0000 | [diff] [blame] | 249 | |
Eric Garver | 3805a93 | 2016-10-17 16:30:12 -0400 | [diff] [blame^] | 250 | if (skb && skb_vlan_tag_present(skb)) |
Hadar Hen Zion | d5709f7 | 2016-08-17 13:36:10 +0300 | [diff] [blame] | 251 | proto = skb->protocol; |
Eric Dumazet | 0744dd0 | 2011-11-28 05:22:18 +0000 | [diff] [blame] | 252 | |
Eric Garver | 3805a93 | 2016-10-17 16:30:12 -0400 | [diff] [blame^] | 253 | if (eth_type_vlan(proto)) { |
Hadar Hen Zion | d5709f7 | 2016-08-17 13:36:10 +0300 | [diff] [blame] | 254 | struct vlan_hdr _vlan; |
| 255 | |
| 256 | vlan = __skb_header_pointer(skb, nhoff, sizeof(_vlan), |
| 257 | data, hlen, &_vlan); |
| 258 | if (!vlan) |
| 259 | goto out_bad; |
| 260 | proto = vlan->h_vlan_encapsulated_proto; |
| 261 | nhoff += sizeof(*vlan); |
| 262 | if (skip_vlan) |
| 263 | goto again; |
| 264 | } |
| 265 | |
| 266 | skip_vlan = true; |
David S. Miller | 20a17bf | 2015-09-01 21:19:17 -0700 | [diff] [blame] | 267 | if (dissector_uses_key(flow_dissector, |
Hadar Hen Zion | f6a6692 | 2016-08-17 13:36:11 +0300 | [diff] [blame] | 268 | FLOW_DISSECTOR_KEY_VLAN)) { |
| 269 | key_vlan = skb_flow_dissector_target(flow_dissector, |
| 270 | FLOW_DISSECTOR_KEY_VLAN, |
Tom Herbert | d34af82 | 2015-06-04 09:16:43 -0700 | [diff] [blame] | 271 | target_container); |
| 272 | |
Hadar Hen Zion | f6a6692 | 2016-08-17 13:36:11 +0300 | [diff] [blame] | 273 | if (skb_vlan_tag_present(skb)) { |
| 274 | key_vlan->vlan_id = skb_vlan_tag_get_id(skb); |
| 275 | key_vlan->vlan_priority = |
| 276 | (skb_vlan_tag_get_prio(skb) >> VLAN_PRIO_SHIFT); |
| 277 | } else { |
| 278 | key_vlan->vlan_id = ntohs(vlan->h_vlan_TCI) & |
Hadar Hen Zion | d5709f7 | 2016-08-17 13:36:10 +0300 | [diff] [blame] | 279 | VLAN_VID_MASK; |
Hadar Hen Zion | f6a6692 | 2016-08-17 13:36:11 +0300 | [diff] [blame] | 280 | key_vlan->vlan_priority = |
| 281 | (ntohs(vlan->h_vlan_TCI) & |
| 282 | VLAN_PRIO_MASK) >> VLAN_PRIO_SHIFT; |
| 283 | } |
Tom Herbert | d34af82 | 2015-06-04 09:16:43 -0700 | [diff] [blame] | 284 | } |
| 285 | |
Eric Dumazet | 0744dd0 | 2011-11-28 05:22:18 +0000 | [diff] [blame] | 286 | goto again; |
| 287 | } |
Joe Perches | 2b8837a | 2014-03-12 10:04:17 -0700 | [diff] [blame] | 288 | case htons(ETH_P_PPP_SES): { |
Eric Dumazet | 0744dd0 | 2011-11-28 05:22:18 +0000 | [diff] [blame] | 289 | struct { |
| 290 | struct pppoe_hdr hdr; |
| 291 | __be16 proto; |
| 292 | } *hdr, _hdr; |
David S. Miller | 690e36e | 2014-08-23 12:13:41 -0700 | [diff] [blame] | 293 | hdr = __skb_header_pointer(skb, nhoff, sizeof(_hdr), data, hlen, &_hdr); |
Eric Dumazet | 0744dd0 | 2011-11-28 05:22:18 +0000 | [diff] [blame] | 294 | if (!hdr) |
Tom Herbert | a6e544b | 2015-09-01 09:24:26 -0700 | [diff] [blame] | 295 | goto out_bad; |
Eric Dumazet | 0744dd0 | 2011-11-28 05:22:18 +0000 | [diff] [blame] | 296 | proto = hdr->proto; |
| 297 | nhoff += PPPOE_SES_HLEN; |
| 298 | switch (proto) { |
Joe Perches | 2b8837a | 2014-03-12 10:04:17 -0700 | [diff] [blame] | 299 | case htons(PPP_IP): |
Eric Dumazet | 0744dd0 | 2011-11-28 05:22:18 +0000 | [diff] [blame] | 300 | goto ip; |
Joe Perches | 2b8837a | 2014-03-12 10:04:17 -0700 | [diff] [blame] | 301 | case htons(PPP_IPV6): |
Eric Dumazet | 0744dd0 | 2011-11-28 05:22:18 +0000 | [diff] [blame] | 302 | goto ipv6; |
| 303 | default: |
Tom Herbert | a6e544b | 2015-09-01 09:24:26 -0700 | [diff] [blame] | 304 | goto out_bad; |
Eric Dumazet | 0744dd0 | 2011-11-28 05:22:18 +0000 | [diff] [blame] | 305 | } |
| 306 | } |
Erik Hugne | 08bfc9c | 2015-01-22 17:10:32 +0100 | [diff] [blame] | 307 | case htons(ETH_P_TIPC): { |
| 308 | struct { |
| 309 | __be32 pre[3]; |
| 310 | __be32 srcnode; |
| 311 | } *hdr, _hdr; |
| 312 | hdr = __skb_header_pointer(skb, nhoff, sizeof(_hdr), data, hlen, &_hdr); |
| 313 | if (!hdr) |
Tom Herbert | a6e544b | 2015-09-01 09:24:26 -0700 | [diff] [blame] | 314 | goto out_bad; |
Jiri Pirko | 06635a3 | 2015-05-12 14:56:16 +0200 | [diff] [blame] | 315 | |
David S. Miller | 20a17bf | 2015-09-01 21:19:17 -0700 | [diff] [blame] | 316 | if (dissector_uses_key(flow_dissector, |
| 317 | FLOW_DISSECTOR_KEY_TIPC_ADDRS)) { |
Jiri Pirko | 06635a3 | 2015-05-12 14:56:16 +0200 | [diff] [blame] | 318 | key_addrs = skb_flow_dissector_target(flow_dissector, |
Tom Herbert | 9f24908 | 2015-06-04 09:16:41 -0700 | [diff] [blame] | 319 | FLOW_DISSECTOR_KEY_TIPC_ADDRS, |
Jiri Pirko | 06635a3 | 2015-05-12 14:56:16 +0200 | [diff] [blame] | 320 | target_container); |
Tom Herbert | 9f24908 | 2015-06-04 09:16:41 -0700 | [diff] [blame] | 321 | key_addrs->tipcaddrs.srcnode = hdr->srcnode; |
| 322 | key_control->addr_type = FLOW_DISSECTOR_KEY_TIPC_ADDRS; |
Jiri Pirko | 06635a3 | 2015-05-12 14:56:16 +0200 | [diff] [blame] | 323 | } |
Tom Herbert | a6e544b | 2015-09-01 09:24:26 -0700 | [diff] [blame] | 324 | goto out_good; |
Erik Hugne | 08bfc9c | 2015-01-22 17:10:32 +0100 | [diff] [blame] | 325 | } |
Tom Herbert | b3baa0f | 2015-06-04 09:16:46 -0700 | [diff] [blame] | 326 | |
| 327 | case htons(ETH_P_MPLS_UC): |
| 328 | case htons(ETH_P_MPLS_MC): { |
| 329 | struct mpls_label *hdr, _hdr[2]; |
| 330 | mpls: |
| 331 | hdr = __skb_header_pointer(skb, nhoff, sizeof(_hdr), data, |
| 332 | hlen, &_hdr); |
| 333 | if (!hdr) |
Tom Herbert | a6e544b | 2015-09-01 09:24:26 -0700 | [diff] [blame] | 334 | goto out_bad; |
Tom Herbert | b3baa0f | 2015-06-04 09:16:46 -0700 | [diff] [blame] | 335 | |
Tom Herbert | 611d23c | 2015-06-12 09:01:05 -0700 | [diff] [blame] | 336 | if ((ntohl(hdr[0].entry) & MPLS_LS_LABEL_MASK) >> |
| 337 | MPLS_LS_LABEL_SHIFT == MPLS_LABEL_ENTROPY) { |
David S. Miller | 20a17bf | 2015-09-01 21:19:17 -0700 | [diff] [blame] | 338 | if (dissector_uses_key(flow_dissector, |
| 339 | FLOW_DISSECTOR_KEY_MPLS_ENTROPY)) { |
Tom Herbert | b3baa0f | 2015-06-04 09:16:46 -0700 | [diff] [blame] | 340 | key_keyid = skb_flow_dissector_target(flow_dissector, |
| 341 | FLOW_DISSECTOR_KEY_MPLS_ENTROPY, |
| 342 | target_container); |
| 343 | key_keyid->keyid = hdr[1].entry & |
| 344 | htonl(MPLS_LS_LABEL_MASK); |
| 345 | } |
| 346 | |
Tom Herbert | a6e544b | 2015-09-01 09:24:26 -0700 | [diff] [blame] | 347 | goto out_good; |
Tom Herbert | b3baa0f | 2015-06-04 09:16:46 -0700 | [diff] [blame] | 348 | } |
| 349 | |
Tom Herbert | a6e544b | 2015-09-01 09:24:26 -0700 | [diff] [blame] | 350 | goto out_good; |
Tom Herbert | b3baa0f | 2015-06-04 09:16:46 -0700 | [diff] [blame] | 351 | } |
| 352 | |
Alexander Duyck | 56193d1 | 2014-09-05 19:20:26 -0400 | [diff] [blame] | 353 | case htons(ETH_P_FCOE): |
Alexander Duyck | 224516b | 2016-02-24 09:29:51 -0800 | [diff] [blame] | 354 | if ((hlen - nhoff) < FCOE_HEADER_LEN) |
| 355 | goto out_bad; |
| 356 | |
| 357 | nhoff += FCOE_HEADER_LEN; |
| 358 | goto out_good; |
Eric Dumazet | 0744dd0 | 2011-11-28 05:22:18 +0000 | [diff] [blame] | 359 | default: |
Tom Herbert | a6e544b | 2015-09-01 09:24:26 -0700 | [diff] [blame] | 360 | goto out_bad; |
Eric Dumazet | 0744dd0 | 2011-11-28 05:22:18 +0000 | [diff] [blame] | 361 | } |
| 362 | |
Tom Herbert | 6a74fcf | 2015-06-12 09:01:06 -0700 | [diff] [blame] | 363 | ip_proto_again: |
Eric Dumazet | 0744dd0 | 2011-11-28 05:22:18 +0000 | [diff] [blame] | 364 | switch (ip_proto) { |
| 365 | case IPPROTO_GRE: { |
Gao Feng | ab10dcc | 2016-08-09 12:38:24 +0800 | [diff] [blame] | 366 | struct gre_base_hdr *hdr, _hdr; |
| 367 | u16 gre_ver; |
| 368 | int offset = 0; |
Eric Dumazet | 0744dd0 | 2011-11-28 05:22:18 +0000 | [diff] [blame] | 369 | |
David S. Miller | 690e36e | 2014-08-23 12:13:41 -0700 | [diff] [blame] | 370 | hdr = __skb_header_pointer(skb, nhoff, sizeof(_hdr), data, hlen, &_hdr); |
Eric Dumazet | 0744dd0 | 2011-11-28 05:22:18 +0000 | [diff] [blame] | 371 | if (!hdr) |
Tom Herbert | a6e544b | 2015-09-01 09:24:26 -0700 | [diff] [blame] | 372 | goto out_bad; |
Gao Feng | ab10dcc | 2016-08-09 12:38:24 +0800 | [diff] [blame] | 373 | |
| 374 | /* Only look inside GRE without routing */ |
| 375 | if (hdr->flags & GRE_ROUTING) |
Tom Herbert | ce3b535 | 2015-06-04 09:16:36 -0700 | [diff] [blame] | 376 | break; |
Michael Dalton | e1733de | 2013-03-11 06:52:28 +0000 | [diff] [blame] | 377 | |
Gao Feng | ab10dcc | 2016-08-09 12:38:24 +0800 | [diff] [blame] | 378 | /* Only look inside GRE for version 0 and 1 */ |
| 379 | gre_ver = ntohs(hdr->flags & GRE_VERSION); |
| 380 | if (gre_ver > 1) |
| 381 | break; |
| 382 | |
| 383 | proto = hdr->protocol; |
| 384 | if (gre_ver) { |
| 385 | /* Version1 must be PPTP, and check the flags */ |
| 386 | if (!(proto == GRE_PROTO_PPP && (hdr->flags & GRE_KEY))) |
| 387 | break; |
| 388 | } |
| 389 | |
| 390 | offset += sizeof(struct gre_base_hdr); |
| 391 | |
Tom Herbert | ce3b535 | 2015-06-04 09:16:36 -0700 | [diff] [blame] | 392 | if (hdr->flags & GRE_CSUM) |
Gao Feng | ab10dcc | 2016-08-09 12:38:24 +0800 | [diff] [blame] | 393 | offset += sizeof(((struct gre_full_hdr *)0)->csum) + |
| 394 | sizeof(((struct gre_full_hdr *)0)->reserved1); |
| 395 | |
Tom Herbert | 1fdd512 | 2015-06-04 09:16:45 -0700 | [diff] [blame] | 396 | if (hdr->flags & GRE_KEY) { |
| 397 | const __be32 *keyid; |
| 398 | __be32 _keyid; |
| 399 | |
Gao Feng | ab10dcc | 2016-08-09 12:38:24 +0800 | [diff] [blame] | 400 | keyid = __skb_header_pointer(skb, nhoff + offset, sizeof(_keyid), |
Tom Herbert | 1fdd512 | 2015-06-04 09:16:45 -0700 | [diff] [blame] | 401 | data, hlen, &_keyid); |
Tom Herbert | 1fdd512 | 2015-06-04 09:16:45 -0700 | [diff] [blame] | 402 | if (!keyid) |
Tom Herbert | a6e544b | 2015-09-01 09:24:26 -0700 | [diff] [blame] | 403 | goto out_bad; |
Tom Herbert | 1fdd512 | 2015-06-04 09:16:45 -0700 | [diff] [blame] | 404 | |
David S. Miller | 20a17bf | 2015-09-01 21:19:17 -0700 | [diff] [blame] | 405 | if (dissector_uses_key(flow_dissector, |
| 406 | FLOW_DISSECTOR_KEY_GRE_KEYID)) { |
Tom Herbert | 1fdd512 | 2015-06-04 09:16:45 -0700 | [diff] [blame] | 407 | key_keyid = skb_flow_dissector_target(flow_dissector, |
| 408 | FLOW_DISSECTOR_KEY_GRE_KEYID, |
| 409 | target_container); |
Gao Feng | ab10dcc | 2016-08-09 12:38:24 +0800 | [diff] [blame] | 410 | if (gre_ver == 0) |
| 411 | key_keyid->keyid = *keyid; |
| 412 | else |
| 413 | key_keyid->keyid = *keyid & GRE_PPTP_KEY_MASK; |
Tom Herbert | 1fdd512 | 2015-06-04 09:16:45 -0700 | [diff] [blame] | 414 | } |
Gao Feng | ab10dcc | 2016-08-09 12:38:24 +0800 | [diff] [blame] | 415 | offset += sizeof(((struct gre_full_hdr *)0)->key); |
Tom Herbert | 1fdd512 | 2015-06-04 09:16:45 -0700 | [diff] [blame] | 416 | } |
Gao Feng | ab10dcc | 2016-08-09 12:38:24 +0800 | [diff] [blame] | 417 | |
Tom Herbert | ce3b535 | 2015-06-04 09:16:36 -0700 | [diff] [blame] | 418 | if (hdr->flags & GRE_SEQ) |
Gao Feng | ab10dcc | 2016-08-09 12:38:24 +0800 | [diff] [blame] | 419 | offset += sizeof(((struct pptp_gre_header *)0)->seq); |
Tom Herbert | ce3b535 | 2015-06-04 09:16:36 -0700 | [diff] [blame] | 420 | |
Gao Feng | ab10dcc | 2016-08-09 12:38:24 +0800 | [diff] [blame] | 421 | if (gre_ver == 0) { |
| 422 | if (proto == htons(ETH_P_TEB)) { |
| 423 | const struct ethhdr *eth; |
| 424 | struct ethhdr _eth; |
| 425 | |
| 426 | eth = __skb_header_pointer(skb, nhoff + offset, |
| 427 | sizeof(_eth), |
| 428 | data, hlen, &_eth); |
| 429 | if (!eth) |
| 430 | goto out_bad; |
| 431 | proto = eth->h_proto; |
| 432 | offset += sizeof(*eth); |
| 433 | |
| 434 | /* Cap headers that we access via pointers at the |
| 435 | * end of the Ethernet header as our maximum alignment |
| 436 | * at that point is only 2 bytes. |
| 437 | */ |
| 438 | if (NET_IP_ALIGN) |
| 439 | hlen = (nhoff + offset); |
| 440 | } |
| 441 | } else { /* version 1, must be PPTP */ |
| 442 | u8 _ppp_hdr[PPP_HDRLEN]; |
| 443 | u8 *ppp_hdr; |
| 444 | |
| 445 | if (hdr->flags & GRE_ACK) |
| 446 | offset += sizeof(((struct pptp_gre_header *)0)->ack); |
| 447 | |
| 448 | ppp_hdr = skb_header_pointer(skb, nhoff + offset, |
| 449 | sizeof(_ppp_hdr), _ppp_hdr); |
| 450 | if (!ppp_hdr) |
Tom Herbert | a6e544b | 2015-09-01 09:24:26 -0700 | [diff] [blame] | 451 | goto out_bad; |
Alexander Duyck | 7856520 | 2016-02-09 06:14:43 -0800 | [diff] [blame] | 452 | |
Gao Feng | ab10dcc | 2016-08-09 12:38:24 +0800 | [diff] [blame] | 453 | switch (PPP_PROTOCOL(ppp_hdr)) { |
| 454 | case PPP_IP: |
| 455 | proto = htons(ETH_P_IP); |
| 456 | break; |
| 457 | case PPP_IPV6: |
| 458 | proto = htons(ETH_P_IPV6); |
| 459 | break; |
| 460 | default: |
| 461 | /* Could probably catch some more like MPLS */ |
| 462 | break; |
| 463 | } |
| 464 | |
| 465 | offset += PPP_HDRLEN; |
Eric Dumazet | 0744dd0 | 2011-11-28 05:22:18 +0000 | [diff] [blame] | 466 | } |
Tom Herbert | 823b969 | 2015-09-01 09:24:32 -0700 | [diff] [blame] | 467 | |
Gao Feng | ab10dcc | 2016-08-09 12:38:24 +0800 | [diff] [blame] | 468 | nhoff += offset; |
David S. Miller | 4b36993 | 2015-09-01 16:46:08 -0700 | [diff] [blame] | 469 | key_control->flags |= FLOW_DIS_ENCAPSULATION; |
Tom Herbert | 823b969 | 2015-09-01 09:24:32 -0700 | [diff] [blame] | 470 | if (flags & FLOW_DISSECTOR_F_STOP_AT_ENCAP) |
| 471 | goto out_good; |
| 472 | |
Tom Herbert | ce3b535 | 2015-06-04 09:16:36 -0700 | [diff] [blame] | 473 | goto again; |
Eric Dumazet | 0744dd0 | 2011-11-28 05:22:18 +0000 | [diff] [blame] | 474 | } |
Tom Herbert | 6a74fcf | 2015-06-12 09:01:06 -0700 | [diff] [blame] | 475 | case NEXTHDR_HOP: |
| 476 | case NEXTHDR_ROUTING: |
| 477 | case NEXTHDR_DEST: { |
| 478 | u8 _opthdr[2], *opthdr; |
| 479 | |
| 480 | if (proto != htons(ETH_P_IPV6)) |
| 481 | break; |
| 482 | |
| 483 | opthdr = __skb_header_pointer(skb, nhoff, sizeof(_opthdr), |
| 484 | data, hlen, &_opthdr); |
Eric Dumazet | 1e98a0f | 2015-06-12 19:31:32 -0700 | [diff] [blame] | 485 | if (!opthdr) |
Tom Herbert | a6e544b | 2015-09-01 09:24:26 -0700 | [diff] [blame] | 486 | goto out_bad; |
Tom Herbert | 6a74fcf | 2015-06-12 09:01:06 -0700 | [diff] [blame] | 487 | |
Eric Dumazet | 1e98a0f | 2015-06-12 19:31:32 -0700 | [diff] [blame] | 488 | ip_proto = opthdr[0]; |
| 489 | nhoff += (opthdr[1] + 1) << 3; |
Tom Herbert | 6a74fcf | 2015-06-12 09:01:06 -0700 | [diff] [blame] | 490 | |
| 491 | goto ip_proto_again; |
| 492 | } |
Tom Herbert | b840f28 | 2015-09-01 09:24:29 -0700 | [diff] [blame] | 493 | case NEXTHDR_FRAGMENT: { |
| 494 | struct frag_hdr _fh, *fh; |
| 495 | |
| 496 | if (proto != htons(ETH_P_IPV6)) |
| 497 | break; |
| 498 | |
| 499 | fh = __skb_header_pointer(skb, nhoff, sizeof(_fh), |
| 500 | data, hlen, &_fh); |
| 501 | |
| 502 | if (!fh) |
| 503 | goto out_bad; |
| 504 | |
David S. Miller | 4b36993 | 2015-09-01 16:46:08 -0700 | [diff] [blame] | 505 | key_control->flags |= FLOW_DIS_IS_FRAGMENT; |
Tom Herbert | b840f28 | 2015-09-01 09:24:29 -0700 | [diff] [blame] | 506 | |
| 507 | nhoff += sizeof(_fh); |
Alexander Duyck | 43d2ccb | 2016-02-24 09:29:44 -0800 | [diff] [blame] | 508 | ip_proto = fh->nexthdr; |
Tom Herbert | b840f28 | 2015-09-01 09:24:29 -0700 | [diff] [blame] | 509 | |
| 510 | if (!(fh->frag_off & htons(IP6_OFFSET))) { |
David S. Miller | 4b36993 | 2015-09-01 16:46:08 -0700 | [diff] [blame] | 511 | key_control->flags |= FLOW_DIS_FIRST_FRAG; |
Alexander Duyck | 43d2ccb | 2016-02-24 09:29:44 -0800 | [diff] [blame] | 512 | if (flags & FLOW_DISSECTOR_F_PARSE_1ST_FRAG) |
Tom Herbert | b840f28 | 2015-09-01 09:24:29 -0700 | [diff] [blame] | 513 | goto ip_proto_again; |
Tom Herbert | b840f28 | 2015-09-01 09:24:29 -0700 | [diff] [blame] | 514 | } |
| 515 | goto out_good; |
| 516 | } |
Eric Dumazet | 0744dd0 | 2011-11-28 05:22:18 +0000 | [diff] [blame] | 517 | case IPPROTO_IPIP: |
Tom Herbert | fca4189 | 2013-07-29 11:07:36 -0700 | [diff] [blame] | 518 | proto = htons(ETH_P_IP); |
Tom Herbert | 823b969 | 2015-09-01 09:24:32 -0700 | [diff] [blame] | 519 | |
David S. Miller | 4b36993 | 2015-09-01 16:46:08 -0700 | [diff] [blame] | 520 | key_control->flags |= FLOW_DIS_ENCAPSULATION; |
Tom Herbert | 823b969 | 2015-09-01 09:24:32 -0700 | [diff] [blame] | 521 | if (flags & FLOW_DISSECTOR_F_STOP_AT_ENCAP) |
| 522 | goto out_good; |
| 523 | |
Tom Herbert | fca4189 | 2013-07-29 11:07:36 -0700 | [diff] [blame] | 524 | goto ip; |
Tom Herbert | b438f94 | 2013-07-29 11:07:42 -0700 | [diff] [blame] | 525 | case IPPROTO_IPV6: |
| 526 | proto = htons(ETH_P_IPV6); |
Tom Herbert | 823b969 | 2015-09-01 09:24:32 -0700 | [diff] [blame] | 527 | |
David S. Miller | 4b36993 | 2015-09-01 16:46:08 -0700 | [diff] [blame] | 528 | key_control->flags |= FLOW_DIS_ENCAPSULATION; |
Tom Herbert | 823b969 | 2015-09-01 09:24:32 -0700 | [diff] [blame] | 529 | if (flags & FLOW_DISSECTOR_F_STOP_AT_ENCAP) |
| 530 | goto out_good; |
| 531 | |
Tom Herbert | b438f94 | 2013-07-29 11:07:42 -0700 | [diff] [blame] | 532 | goto ipv6; |
Tom Herbert | b3baa0f | 2015-06-04 09:16:46 -0700 | [diff] [blame] | 533 | case IPPROTO_MPLS: |
| 534 | proto = htons(ETH_P_MPLS_UC); |
| 535 | goto mpls; |
Eric Dumazet | 0744dd0 | 2011-11-28 05:22:18 +0000 | [diff] [blame] | 536 | default: |
| 537 | break; |
| 538 | } |
| 539 | |
David S. Miller | 20a17bf | 2015-09-01 21:19:17 -0700 | [diff] [blame] | 540 | if (dissector_uses_key(flow_dissector, |
| 541 | FLOW_DISSECTOR_KEY_PORTS)) { |
Jiri Pirko | 06635a3 | 2015-05-12 14:56:16 +0200 | [diff] [blame] | 542 | key_ports = skb_flow_dissector_target(flow_dissector, |
| 543 | FLOW_DISSECTOR_KEY_PORTS, |
| 544 | target_container); |
| 545 | key_ports->ports = __skb_flow_get_ports(skb, nhoff, ip_proto, |
| 546 | data, hlen); |
| 547 | } |
Alexander Duyck | 5af7fb6 | 2014-10-10 12:09:12 -0700 | [diff] [blame] | 548 | |
Tom Herbert | a6e544b | 2015-09-01 09:24:26 -0700 | [diff] [blame] | 549 | out_good: |
| 550 | ret = true; |
| 551 | |
| 552 | out_bad: |
| 553 | key_basic->n_proto = proto; |
| 554 | key_basic->ip_proto = ip_proto; |
| 555 | key_control->thoff = (u16)nhoff; |
| 556 | |
| 557 | return ret; |
Eric Dumazet | 0744dd0 | 2011-11-28 05:22:18 +0000 | [diff] [blame] | 558 | } |
David S. Miller | 690e36e | 2014-08-23 12:13:41 -0700 | [diff] [blame] | 559 | EXPORT_SYMBOL(__skb_flow_dissect); |
Cong Wang | 441d9d3 | 2013-01-21 00:39:24 +0000 | [diff] [blame] | 560 | |
| 561 | static u32 hashrnd __read_mostly; |
Hannes Frederic Sowa | 66415cf | 2013-10-23 20:06:00 +0200 | [diff] [blame] | 562 | static __always_inline void __flow_hash_secret_init(void) |
| 563 | { |
| 564 | net_get_random_once(&hashrnd, sizeof(hashrnd)); |
| 565 | } |
| 566 | |
David S. Miller | 20a17bf | 2015-09-01 21:19:17 -0700 | [diff] [blame] | 567 | static __always_inline u32 __flow_hash_words(const u32 *words, u32 length, |
| 568 | u32 keyval) |
Hannes Frederic Sowa | 66415cf | 2013-10-23 20:06:00 +0200 | [diff] [blame] | 569 | { |
Tom Herbert | 42aecaa | 2015-06-04 09:16:39 -0700 | [diff] [blame] | 570 | return jhash2(words, length, keyval); |
| 571 | } |
| 572 | |
David S. Miller | 20a17bf | 2015-09-01 21:19:17 -0700 | [diff] [blame] | 573 | static inline const u32 *flow_keys_hash_start(const struct flow_keys *flow) |
Tom Herbert | 42aecaa | 2015-06-04 09:16:39 -0700 | [diff] [blame] | 574 | { |
David S. Miller | 20a17bf | 2015-09-01 21:19:17 -0700 | [diff] [blame] | 575 | const void *p = flow; |
| 576 | |
Tom Herbert | 42aecaa | 2015-06-04 09:16:39 -0700 | [diff] [blame] | 577 | BUILD_BUG_ON(FLOW_KEYS_HASH_OFFSET % sizeof(u32)); |
David S. Miller | 20a17bf | 2015-09-01 21:19:17 -0700 | [diff] [blame] | 578 | return (const u32 *)(p + FLOW_KEYS_HASH_OFFSET); |
Tom Herbert | 42aecaa | 2015-06-04 09:16:39 -0700 | [diff] [blame] | 579 | } |
| 580 | |
David S. Miller | 20a17bf | 2015-09-01 21:19:17 -0700 | [diff] [blame] | 581 | 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] | 582 | { |
Tom Herbert | c3f8324 | 2015-06-04 09:16:40 -0700 | [diff] [blame] | 583 | size_t diff = FLOW_KEYS_HASH_OFFSET + sizeof(flow->addrs); |
Tom Herbert | 42aecaa | 2015-06-04 09:16:39 -0700 | [diff] [blame] | 584 | BUILD_BUG_ON((sizeof(*flow) - FLOW_KEYS_HASH_OFFSET) % sizeof(u32)); |
Tom Herbert | c3f8324 | 2015-06-04 09:16:40 -0700 | [diff] [blame] | 585 | BUILD_BUG_ON(offsetof(typeof(*flow), addrs) != |
| 586 | sizeof(*flow) - sizeof(flow->addrs)); |
| 587 | |
| 588 | switch (flow->control.addr_type) { |
| 589 | case FLOW_DISSECTOR_KEY_IPV4_ADDRS: |
| 590 | diff -= sizeof(flow->addrs.v4addrs); |
| 591 | break; |
| 592 | case FLOW_DISSECTOR_KEY_IPV6_ADDRS: |
| 593 | diff -= sizeof(flow->addrs.v6addrs); |
| 594 | break; |
Tom Herbert | 9f24908 | 2015-06-04 09:16:41 -0700 | [diff] [blame] | 595 | case FLOW_DISSECTOR_KEY_TIPC_ADDRS: |
| 596 | diff -= sizeof(flow->addrs.tipcaddrs); |
| 597 | break; |
Tom Herbert | c3f8324 | 2015-06-04 09:16:40 -0700 | [diff] [blame] | 598 | } |
| 599 | return (sizeof(*flow) - diff) / sizeof(u32); |
| 600 | } |
| 601 | |
| 602 | __be32 flow_get_u32_src(const struct flow_keys *flow) |
| 603 | { |
| 604 | switch (flow->control.addr_type) { |
| 605 | case FLOW_DISSECTOR_KEY_IPV4_ADDRS: |
| 606 | return flow->addrs.v4addrs.src; |
| 607 | case FLOW_DISSECTOR_KEY_IPV6_ADDRS: |
| 608 | return (__force __be32)ipv6_addr_hash( |
| 609 | &flow->addrs.v6addrs.src); |
Tom Herbert | 9f24908 | 2015-06-04 09:16:41 -0700 | [diff] [blame] | 610 | case FLOW_DISSECTOR_KEY_TIPC_ADDRS: |
| 611 | return flow->addrs.tipcaddrs.srcnode; |
Tom Herbert | c3f8324 | 2015-06-04 09:16:40 -0700 | [diff] [blame] | 612 | default: |
| 613 | return 0; |
| 614 | } |
| 615 | } |
| 616 | EXPORT_SYMBOL(flow_get_u32_src); |
| 617 | |
| 618 | __be32 flow_get_u32_dst(const struct flow_keys *flow) |
| 619 | { |
| 620 | switch (flow->control.addr_type) { |
| 621 | case FLOW_DISSECTOR_KEY_IPV4_ADDRS: |
| 622 | return flow->addrs.v4addrs.dst; |
| 623 | case FLOW_DISSECTOR_KEY_IPV6_ADDRS: |
| 624 | return (__force __be32)ipv6_addr_hash( |
| 625 | &flow->addrs.v6addrs.dst); |
| 626 | default: |
| 627 | return 0; |
| 628 | } |
| 629 | } |
| 630 | EXPORT_SYMBOL(flow_get_u32_dst); |
| 631 | |
| 632 | static inline void __flow_hash_consistentify(struct flow_keys *keys) |
| 633 | { |
| 634 | int addr_diff, i; |
| 635 | |
| 636 | switch (keys->control.addr_type) { |
| 637 | case FLOW_DISSECTOR_KEY_IPV4_ADDRS: |
| 638 | addr_diff = (__force u32)keys->addrs.v4addrs.dst - |
| 639 | (__force u32)keys->addrs.v4addrs.src; |
| 640 | if ((addr_diff < 0) || |
| 641 | (addr_diff == 0 && |
| 642 | ((__force u16)keys->ports.dst < |
| 643 | (__force u16)keys->ports.src))) { |
| 644 | swap(keys->addrs.v4addrs.src, keys->addrs.v4addrs.dst); |
| 645 | swap(keys->ports.src, keys->ports.dst); |
| 646 | } |
| 647 | break; |
| 648 | case FLOW_DISSECTOR_KEY_IPV6_ADDRS: |
| 649 | addr_diff = memcmp(&keys->addrs.v6addrs.dst, |
| 650 | &keys->addrs.v6addrs.src, |
| 651 | sizeof(keys->addrs.v6addrs.dst)); |
| 652 | if ((addr_diff < 0) || |
| 653 | (addr_diff == 0 && |
| 654 | ((__force u16)keys->ports.dst < |
| 655 | (__force u16)keys->ports.src))) { |
| 656 | for (i = 0; i < 4; i++) |
| 657 | swap(keys->addrs.v6addrs.src.s6_addr32[i], |
| 658 | keys->addrs.v6addrs.dst.s6_addr32[i]); |
| 659 | swap(keys->ports.src, keys->ports.dst); |
| 660 | } |
| 661 | break; |
| 662 | } |
Hannes Frederic Sowa | 66415cf | 2013-10-23 20:06:00 +0200 | [diff] [blame] | 663 | } |
| 664 | |
Tom Herbert | 50fb799 | 2015-05-01 11:30:12 -0700 | [diff] [blame] | 665 | static inline u32 __flow_hash_from_keys(struct flow_keys *keys, u32 keyval) |
Tom Herbert | 5ed20a6 | 2014-07-01 21:32:05 -0700 | [diff] [blame] | 666 | { |
| 667 | u32 hash; |
| 668 | |
Tom Herbert | c3f8324 | 2015-06-04 09:16:40 -0700 | [diff] [blame] | 669 | __flow_hash_consistentify(keys); |
Tom Herbert | 5ed20a6 | 2014-07-01 21:32:05 -0700 | [diff] [blame] | 670 | |
David S. Miller | 20a17bf | 2015-09-01 21:19:17 -0700 | [diff] [blame] | 671 | hash = __flow_hash_words(flow_keys_hash_start(keys), |
Tom Herbert | 42aecaa | 2015-06-04 09:16:39 -0700 | [diff] [blame] | 672 | flow_keys_hash_length(keys), keyval); |
Tom Herbert | 5ed20a6 | 2014-07-01 21:32:05 -0700 | [diff] [blame] | 673 | if (!hash) |
| 674 | hash = 1; |
| 675 | |
| 676 | return hash; |
| 677 | } |
| 678 | |
| 679 | u32 flow_hash_from_keys(struct flow_keys *keys) |
| 680 | { |
Tom Herbert | 50fb799 | 2015-05-01 11:30:12 -0700 | [diff] [blame] | 681 | __flow_hash_secret_init(); |
| 682 | return __flow_hash_from_keys(keys, hashrnd); |
Tom Herbert | 5ed20a6 | 2014-07-01 21:32:05 -0700 | [diff] [blame] | 683 | } |
| 684 | EXPORT_SYMBOL(flow_hash_from_keys); |
| 685 | |
Tom Herbert | 50fb799 | 2015-05-01 11:30:12 -0700 | [diff] [blame] | 686 | static inline u32 ___skb_get_hash(const struct sk_buff *skb, |
| 687 | struct flow_keys *keys, u32 keyval) |
| 688 | { |
Tom Herbert | 6db61d7 | 2015-09-01 09:24:33 -0700 | [diff] [blame] | 689 | skb_flow_dissect_flow_keys(skb, keys, |
| 690 | FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL); |
Tom Herbert | 50fb799 | 2015-05-01 11:30:12 -0700 | [diff] [blame] | 691 | |
| 692 | return __flow_hash_from_keys(keys, keyval); |
| 693 | } |
| 694 | |
Tom Herbert | 2f59e1e | 2015-05-01 11:30:17 -0700 | [diff] [blame] | 695 | struct _flow_keys_digest_data { |
| 696 | __be16 n_proto; |
| 697 | u8 ip_proto; |
| 698 | u8 padding; |
| 699 | __be32 ports; |
| 700 | __be32 src; |
| 701 | __be32 dst; |
| 702 | }; |
| 703 | |
| 704 | void make_flow_keys_digest(struct flow_keys_digest *digest, |
| 705 | const struct flow_keys *flow) |
| 706 | { |
| 707 | struct _flow_keys_digest_data *data = |
| 708 | (struct _flow_keys_digest_data *)digest; |
| 709 | |
| 710 | BUILD_BUG_ON(sizeof(*data) > sizeof(*digest)); |
| 711 | |
| 712 | memset(digest, 0, sizeof(*digest)); |
| 713 | |
Jiri Pirko | 06635a3 | 2015-05-12 14:56:16 +0200 | [diff] [blame] | 714 | data->n_proto = flow->basic.n_proto; |
| 715 | data->ip_proto = flow->basic.ip_proto; |
| 716 | data->ports = flow->ports.ports; |
Tom Herbert | c3f8324 | 2015-06-04 09:16:40 -0700 | [diff] [blame] | 717 | data->src = flow->addrs.v4addrs.src; |
| 718 | data->dst = flow->addrs.v4addrs.dst; |
Tom Herbert | 2f59e1e | 2015-05-01 11:30:17 -0700 | [diff] [blame] | 719 | } |
| 720 | EXPORT_SYMBOL(make_flow_keys_digest); |
| 721 | |
David S. Miller | eb70db8 | 2016-07-01 16:07:50 -0400 | [diff] [blame] | 722 | static struct flow_dissector flow_keys_dissector_symmetric __read_mostly; |
| 723 | |
| 724 | u32 __skb_get_hash_symmetric(struct sk_buff *skb) |
| 725 | { |
| 726 | struct flow_keys keys; |
| 727 | |
| 728 | __flow_hash_secret_init(); |
| 729 | |
| 730 | memset(&keys, 0, sizeof(keys)); |
| 731 | __skb_flow_dissect(skb, &flow_keys_dissector_symmetric, &keys, |
| 732 | NULL, 0, 0, 0, |
| 733 | FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL); |
| 734 | |
| 735 | return __flow_hash_from_keys(&keys, hashrnd); |
| 736 | } |
| 737 | EXPORT_SYMBOL_GPL(__skb_get_hash_symmetric); |
| 738 | |
Jiri Pirko | d4fd327 | 2015-05-12 14:56:10 +0200 | [diff] [blame] | 739 | /** |
| 740 | * __skb_get_hash: calculate a flow hash |
| 741 | * @skb: sk_buff to calculate flow hash from |
| 742 | * |
| 743 | * This function calculates a flow hash based on src/dst addresses |
Tom Herbert | 61b905d | 2014-03-24 15:34:47 -0700 | [diff] [blame] | 744 | * and src/dst port numbers. Sets hash in skb to non-zero hash value |
| 745 | * 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] | 746 | * if hash is a canonical 4-tuple hash over transport ports. |
| 747 | */ |
Tom Herbert | 3958afa1b | 2013-12-15 22:12:06 -0800 | [diff] [blame] | 748 | void __skb_get_hash(struct sk_buff *skb) |
Cong Wang | 441d9d3 | 2013-01-21 00:39:24 +0000 | [diff] [blame] | 749 | { |
| 750 | struct flow_keys keys; |
Gao Feng | 635c223 | 2016-08-31 14:15:05 +0800 | [diff] [blame] | 751 | u32 hash; |
Cong Wang | 441d9d3 | 2013-01-21 00:39:24 +0000 | [diff] [blame] | 752 | |
Tom Herbert | 50fb799 | 2015-05-01 11:30:12 -0700 | [diff] [blame] | 753 | __flow_hash_secret_init(); |
| 754 | |
Gao Feng | 635c223 | 2016-08-31 14:15:05 +0800 | [diff] [blame] | 755 | hash = ___skb_get_hash(skb, &keys, hashrnd); |
| 756 | |
| 757 | __skb_set_sw_hash(skb, hash, flow_keys_have_l4(&keys)); |
Cong Wang | 441d9d3 | 2013-01-21 00:39:24 +0000 | [diff] [blame] | 758 | } |
Tom Herbert | 3958afa1b | 2013-12-15 22:12:06 -0800 | [diff] [blame] | 759 | EXPORT_SYMBOL(__skb_get_hash); |
Cong Wang | 441d9d3 | 2013-01-21 00:39:24 +0000 | [diff] [blame] | 760 | |
Tom Herbert | 50fb799 | 2015-05-01 11:30:12 -0700 | [diff] [blame] | 761 | __u32 skb_get_hash_perturb(const struct sk_buff *skb, u32 perturb) |
| 762 | { |
| 763 | struct flow_keys keys; |
| 764 | |
| 765 | return ___skb_get_hash(skb, &keys, perturb); |
| 766 | } |
| 767 | EXPORT_SYMBOL(skb_get_hash_perturb); |
| 768 | |
David S. Miller | 20a17bf | 2015-09-01 21:19:17 -0700 | [diff] [blame] | 769 | __u32 __skb_get_hash_flowi6(struct sk_buff *skb, const struct flowi6 *fl6) |
Tom Herbert | f70ea01 | 2015-07-31 16:52:10 -0700 | [diff] [blame] | 770 | { |
| 771 | struct flow_keys keys; |
| 772 | |
| 773 | memset(&keys, 0, sizeof(keys)); |
| 774 | |
| 775 | memcpy(&keys.addrs.v6addrs.src, &fl6->saddr, |
| 776 | sizeof(keys.addrs.v6addrs.src)); |
| 777 | memcpy(&keys.addrs.v6addrs.dst, &fl6->daddr, |
| 778 | sizeof(keys.addrs.v6addrs.dst)); |
| 779 | keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS; |
| 780 | keys.ports.src = fl6->fl6_sport; |
| 781 | keys.ports.dst = fl6->fl6_dport; |
| 782 | keys.keyid.keyid = fl6->fl6_gre_key; |
| 783 | keys.tags.flow_label = (__force u32)fl6->flowlabel; |
| 784 | keys.basic.ip_proto = fl6->flowi6_proto; |
| 785 | |
Tom Herbert | bcc8383 | 2015-09-01 09:24:24 -0700 | [diff] [blame] | 786 | __skb_set_sw_hash(skb, flow_hash_from_keys(&keys), |
| 787 | flow_keys_have_l4(&keys)); |
Tom Herbert | f70ea01 | 2015-07-31 16:52:10 -0700 | [diff] [blame] | 788 | |
| 789 | return skb->hash; |
| 790 | } |
| 791 | EXPORT_SYMBOL(__skb_get_hash_flowi6); |
| 792 | |
David S. Miller | 20a17bf | 2015-09-01 21:19:17 -0700 | [diff] [blame] | 793 | __u32 __skb_get_hash_flowi4(struct sk_buff *skb, const struct flowi4 *fl4) |
Tom Herbert | f70ea01 | 2015-07-31 16:52:10 -0700 | [diff] [blame] | 794 | { |
| 795 | struct flow_keys keys; |
| 796 | |
| 797 | memset(&keys, 0, sizeof(keys)); |
| 798 | |
| 799 | keys.addrs.v4addrs.src = fl4->saddr; |
| 800 | keys.addrs.v4addrs.dst = fl4->daddr; |
| 801 | keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS; |
| 802 | keys.ports.src = fl4->fl4_sport; |
| 803 | keys.ports.dst = fl4->fl4_dport; |
| 804 | keys.keyid.keyid = fl4->fl4_gre_key; |
| 805 | keys.basic.ip_proto = fl4->flowi4_proto; |
| 806 | |
Tom Herbert | bcc8383 | 2015-09-01 09:24:24 -0700 | [diff] [blame] | 807 | __skb_set_sw_hash(skb, flow_hash_from_keys(&keys), |
| 808 | flow_keys_have_l4(&keys)); |
Tom Herbert | f70ea01 | 2015-07-31 16:52:10 -0700 | [diff] [blame] | 809 | |
| 810 | return skb->hash; |
| 811 | } |
| 812 | EXPORT_SYMBOL(__skb_get_hash_flowi4); |
| 813 | |
Alexander Duyck | 56193d1 | 2014-09-05 19:20:26 -0400 | [diff] [blame] | 814 | u32 __skb_get_poff(const struct sk_buff *skb, void *data, |
| 815 | const struct flow_keys *keys, int hlen) |
Daniel Borkmann | f77668d | 2013-03-19 06:39:30 +0000 | [diff] [blame] | 816 | { |
Tom Herbert | 42aecaa | 2015-06-04 09:16:39 -0700 | [diff] [blame] | 817 | u32 poff = keys->control.thoff; |
Daniel Borkmann | f77668d | 2013-03-19 06:39:30 +0000 | [diff] [blame] | 818 | |
Alexander Duyck | 43d2ccb | 2016-02-24 09:29:44 -0800 | [diff] [blame] | 819 | /* skip L4 headers for fragments after the first */ |
| 820 | if ((keys->control.flags & FLOW_DIS_IS_FRAGMENT) && |
| 821 | !(keys->control.flags & FLOW_DIS_FIRST_FRAG)) |
| 822 | return poff; |
| 823 | |
Jiri Pirko | 06635a3 | 2015-05-12 14:56:16 +0200 | [diff] [blame] | 824 | switch (keys->basic.ip_proto) { |
Daniel Borkmann | f77668d | 2013-03-19 06:39:30 +0000 | [diff] [blame] | 825 | case IPPROTO_TCP: { |
Alexander Duyck | 5af7fb6 | 2014-10-10 12:09:12 -0700 | [diff] [blame] | 826 | /* access doff as u8 to avoid unaligned access */ |
| 827 | const u8 *doff; |
| 828 | u8 _doff; |
Daniel Borkmann | f77668d | 2013-03-19 06:39:30 +0000 | [diff] [blame] | 829 | |
Alexander Duyck | 5af7fb6 | 2014-10-10 12:09:12 -0700 | [diff] [blame] | 830 | doff = __skb_header_pointer(skb, poff + 12, sizeof(_doff), |
| 831 | data, hlen, &_doff); |
| 832 | if (!doff) |
Daniel Borkmann | f77668d | 2013-03-19 06:39:30 +0000 | [diff] [blame] | 833 | return poff; |
| 834 | |
Alexander Duyck | 5af7fb6 | 2014-10-10 12:09:12 -0700 | [diff] [blame] | 835 | poff += max_t(u32, sizeof(struct tcphdr), (*doff & 0xF0) >> 2); |
Daniel Borkmann | f77668d | 2013-03-19 06:39:30 +0000 | [diff] [blame] | 836 | break; |
| 837 | } |
| 838 | case IPPROTO_UDP: |
| 839 | case IPPROTO_UDPLITE: |
| 840 | poff += sizeof(struct udphdr); |
| 841 | break; |
| 842 | /* For the rest, we do not really care about header |
| 843 | * extensions at this point for now. |
| 844 | */ |
| 845 | case IPPROTO_ICMP: |
| 846 | poff += sizeof(struct icmphdr); |
| 847 | break; |
| 848 | case IPPROTO_ICMPV6: |
| 849 | poff += sizeof(struct icmp6hdr); |
| 850 | break; |
| 851 | case IPPROTO_IGMP: |
| 852 | poff += sizeof(struct igmphdr); |
| 853 | break; |
| 854 | case IPPROTO_DCCP: |
| 855 | poff += sizeof(struct dccp_hdr); |
| 856 | break; |
| 857 | case IPPROTO_SCTP: |
| 858 | poff += sizeof(struct sctphdr); |
| 859 | break; |
| 860 | } |
| 861 | |
| 862 | return poff; |
| 863 | } |
| 864 | |
Jiri Pirko | 0db89b8 | 2015-05-12 14:56:14 +0200 | [diff] [blame] | 865 | /** |
| 866 | * skb_get_poff - get the offset to the payload |
| 867 | * @skb: sk_buff to get the payload offset from |
| 868 | * |
| 869 | * The function will get the offset to the payload as far as it could |
| 870 | * 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] | 871 | * truncate packets without needing to push actual payload to the user |
| 872 | * space and can analyze headers only, instead. |
| 873 | */ |
| 874 | u32 skb_get_poff(const struct sk_buff *skb) |
| 875 | { |
| 876 | struct flow_keys keys; |
| 877 | |
Tom Herbert | cd79a23 | 2015-09-01 09:24:27 -0700 | [diff] [blame] | 878 | if (!skb_flow_dissect_flow_keys(skb, &keys, 0)) |
Alexander Duyck | 56193d1 | 2014-09-05 19:20:26 -0400 | [diff] [blame] | 879 | return 0; |
| 880 | |
| 881 | return __skb_get_poff(skb, skb->data, &keys, skb_headlen(skb)); |
| 882 | } |
Jiri Pirko | 06635a3 | 2015-05-12 14:56:16 +0200 | [diff] [blame] | 883 | |
David S. Miller | 20a17bf | 2015-09-01 21:19:17 -0700 | [diff] [blame] | 884 | __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] | 885 | { |
| 886 | memset(keys, 0, sizeof(*keys)); |
| 887 | |
| 888 | memcpy(&keys->addrs.v6addrs.src, &fl6->saddr, |
| 889 | sizeof(keys->addrs.v6addrs.src)); |
| 890 | memcpy(&keys->addrs.v6addrs.dst, &fl6->daddr, |
| 891 | sizeof(keys->addrs.v6addrs.dst)); |
| 892 | keys->control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS; |
| 893 | keys->ports.src = fl6->fl6_sport; |
| 894 | keys->ports.dst = fl6->fl6_dport; |
| 895 | keys->keyid.keyid = fl6->fl6_gre_key; |
| 896 | keys->tags.flow_label = (__force u32)fl6->flowlabel; |
| 897 | keys->basic.ip_proto = fl6->flowi6_proto; |
| 898 | |
| 899 | return flow_hash_from_keys(keys); |
| 900 | } |
| 901 | EXPORT_SYMBOL(__get_hash_from_flowi6); |
| 902 | |
David S. Miller | 20a17bf | 2015-09-01 21:19:17 -0700 | [diff] [blame] | 903 | __u32 __get_hash_from_flowi4(const struct flowi4 *fl4, struct flow_keys *keys) |
David S. Miller | a17ace9 | 2015-09-01 17:00:24 -0700 | [diff] [blame] | 904 | { |
| 905 | memset(keys, 0, sizeof(*keys)); |
| 906 | |
| 907 | keys->addrs.v4addrs.src = fl4->saddr; |
| 908 | keys->addrs.v4addrs.dst = fl4->daddr; |
| 909 | keys->control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS; |
| 910 | keys->ports.src = fl4->fl4_sport; |
| 911 | keys->ports.dst = fl4->fl4_dport; |
| 912 | keys->keyid.keyid = fl4->fl4_gre_key; |
| 913 | keys->basic.ip_proto = fl4->flowi4_proto; |
| 914 | |
| 915 | return flow_hash_from_keys(keys); |
| 916 | } |
| 917 | EXPORT_SYMBOL(__get_hash_from_flowi4); |
| 918 | |
Jiri Pirko | 06635a3 | 2015-05-12 14:56:16 +0200 | [diff] [blame] | 919 | static const struct flow_dissector_key flow_keys_dissector_keys[] = { |
| 920 | { |
Tom Herbert | 42aecaa | 2015-06-04 09:16:39 -0700 | [diff] [blame] | 921 | .key_id = FLOW_DISSECTOR_KEY_CONTROL, |
| 922 | .offset = offsetof(struct flow_keys, control), |
| 923 | }, |
| 924 | { |
Jiri Pirko | 06635a3 | 2015-05-12 14:56:16 +0200 | [diff] [blame] | 925 | .key_id = FLOW_DISSECTOR_KEY_BASIC, |
| 926 | .offset = offsetof(struct flow_keys, basic), |
| 927 | }, |
| 928 | { |
| 929 | .key_id = FLOW_DISSECTOR_KEY_IPV4_ADDRS, |
Tom Herbert | c3f8324 | 2015-06-04 09:16:40 -0700 | [diff] [blame] | 930 | .offset = offsetof(struct flow_keys, addrs.v4addrs), |
| 931 | }, |
| 932 | { |
| 933 | .key_id = FLOW_DISSECTOR_KEY_IPV6_ADDRS, |
| 934 | .offset = offsetof(struct flow_keys, addrs.v6addrs), |
Jiri Pirko | 06635a3 | 2015-05-12 14:56:16 +0200 | [diff] [blame] | 935 | }, |
| 936 | { |
Tom Herbert | 9f24908 | 2015-06-04 09:16:41 -0700 | [diff] [blame] | 937 | .key_id = FLOW_DISSECTOR_KEY_TIPC_ADDRS, |
| 938 | .offset = offsetof(struct flow_keys, addrs.tipcaddrs), |
| 939 | }, |
| 940 | { |
Jiri Pirko | 06635a3 | 2015-05-12 14:56:16 +0200 | [diff] [blame] | 941 | .key_id = FLOW_DISSECTOR_KEY_PORTS, |
| 942 | .offset = offsetof(struct flow_keys, ports), |
| 943 | }, |
Tom Herbert | d34af82 | 2015-06-04 09:16:43 -0700 | [diff] [blame] | 944 | { |
Hadar Hen Zion | f6a6692 | 2016-08-17 13:36:11 +0300 | [diff] [blame] | 945 | .key_id = FLOW_DISSECTOR_KEY_VLAN, |
| 946 | .offset = offsetof(struct flow_keys, vlan), |
Tom Herbert | d34af82 | 2015-06-04 09:16:43 -0700 | [diff] [blame] | 947 | }, |
Tom Herbert | 87ee9e5 | 2015-06-04 09:16:44 -0700 | [diff] [blame] | 948 | { |
| 949 | .key_id = FLOW_DISSECTOR_KEY_FLOW_LABEL, |
| 950 | .offset = offsetof(struct flow_keys, tags), |
| 951 | }, |
Tom Herbert | 1fdd512 | 2015-06-04 09:16:45 -0700 | [diff] [blame] | 952 | { |
| 953 | .key_id = FLOW_DISSECTOR_KEY_GRE_KEYID, |
| 954 | .offset = offsetof(struct flow_keys, keyid), |
| 955 | }, |
Jiri Pirko | 06635a3 | 2015-05-12 14:56:16 +0200 | [diff] [blame] | 956 | }; |
| 957 | |
David S. Miller | eb70db8 | 2016-07-01 16:07:50 -0400 | [diff] [blame] | 958 | static const struct flow_dissector_key flow_keys_dissector_symmetric_keys[] = { |
| 959 | { |
| 960 | .key_id = FLOW_DISSECTOR_KEY_CONTROL, |
| 961 | .offset = offsetof(struct flow_keys, control), |
| 962 | }, |
| 963 | { |
| 964 | .key_id = FLOW_DISSECTOR_KEY_BASIC, |
| 965 | .offset = offsetof(struct flow_keys, basic), |
| 966 | }, |
| 967 | { |
| 968 | .key_id = FLOW_DISSECTOR_KEY_IPV4_ADDRS, |
| 969 | .offset = offsetof(struct flow_keys, addrs.v4addrs), |
| 970 | }, |
| 971 | { |
| 972 | .key_id = FLOW_DISSECTOR_KEY_IPV6_ADDRS, |
| 973 | .offset = offsetof(struct flow_keys, addrs.v6addrs), |
| 974 | }, |
| 975 | { |
| 976 | .key_id = FLOW_DISSECTOR_KEY_PORTS, |
| 977 | .offset = offsetof(struct flow_keys, ports), |
| 978 | }, |
| 979 | }; |
| 980 | |
Jiri Pirko | 06635a3 | 2015-05-12 14:56:16 +0200 | [diff] [blame] | 981 | static const struct flow_dissector_key flow_keys_buf_dissector_keys[] = { |
| 982 | { |
Tom Herbert | 42aecaa | 2015-06-04 09:16:39 -0700 | [diff] [blame] | 983 | .key_id = FLOW_DISSECTOR_KEY_CONTROL, |
| 984 | .offset = offsetof(struct flow_keys, control), |
| 985 | }, |
| 986 | { |
Jiri Pirko | 06635a3 | 2015-05-12 14:56:16 +0200 | [diff] [blame] | 987 | .key_id = FLOW_DISSECTOR_KEY_BASIC, |
| 988 | .offset = offsetof(struct flow_keys, basic), |
| 989 | }, |
| 990 | }; |
| 991 | |
| 992 | struct flow_dissector flow_keys_dissector __read_mostly; |
| 993 | EXPORT_SYMBOL(flow_keys_dissector); |
| 994 | |
| 995 | struct flow_dissector flow_keys_buf_dissector __read_mostly; |
| 996 | |
| 997 | static int __init init_default_flow_dissectors(void) |
| 998 | { |
| 999 | skb_flow_dissector_init(&flow_keys_dissector, |
| 1000 | flow_keys_dissector_keys, |
| 1001 | ARRAY_SIZE(flow_keys_dissector_keys)); |
David S. Miller | eb70db8 | 2016-07-01 16:07:50 -0400 | [diff] [blame] | 1002 | skb_flow_dissector_init(&flow_keys_dissector_symmetric, |
| 1003 | flow_keys_dissector_symmetric_keys, |
| 1004 | ARRAY_SIZE(flow_keys_dissector_symmetric_keys)); |
Jiri Pirko | 06635a3 | 2015-05-12 14:56:16 +0200 | [diff] [blame] | 1005 | skb_flow_dissector_init(&flow_keys_buf_dissector, |
| 1006 | flow_keys_buf_dissector_keys, |
| 1007 | ARRAY_SIZE(flow_keys_buf_dissector_keys)); |
| 1008 | return 0; |
| 1009 | } |
| 1010 | |
| 1011 | late_initcall_sync(init_default_flow_dissectors); |