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> |
Daniel Borkmann | f77668d | 2013-03-19 06:39:30 +0000 | [diff] [blame] | 9 | #include <linux/igmp.h> |
| 10 | #include <linux/icmp.h> |
| 11 | #include <linux/sctp.h> |
| 12 | #include <linux/dccp.h> |
Eric Dumazet | 0744dd0 | 2011-11-28 05:22:18 +0000 | [diff] [blame] | 13 | #include <linux/if_tunnel.h> |
| 14 | #include <linux/if_pppox.h> |
| 15 | #include <linux/ppp_defs.h> |
Jiri Pirko | 06635a3 | 2015-05-12 14:56:16 +0200 | [diff] [blame] | 16 | #include <linux/stddef.h> |
Jiri Pirko | 67a900c | 2015-05-12 14:56:19 +0200 | [diff] [blame^] | 17 | #include <linux/if_ether.h> |
Jiri Pirko | 1bd758e | 2015-05-12 14:56:07 +0200 | [diff] [blame] | 18 | #include <net/flow_dissector.h> |
Alexander Duyck | 56193d1 | 2014-09-05 19:20:26 -0400 | [diff] [blame] | 19 | #include <scsi/fc/fc_fcoe.h> |
Eric Dumazet | 0744dd0 | 2011-11-28 05:22:18 +0000 | [diff] [blame] | 20 | |
Jiri Pirko | fbff949 | 2015-05-12 14:56:15 +0200 | [diff] [blame] | 21 | static bool skb_flow_dissector_uses_key(struct flow_dissector *flow_dissector, |
| 22 | enum flow_dissector_key_id key_id) |
| 23 | { |
| 24 | return flow_dissector->used_keys & (1 << key_id); |
| 25 | } |
| 26 | |
| 27 | static void skb_flow_dissector_set_key(struct flow_dissector *flow_dissector, |
| 28 | enum flow_dissector_key_id key_id) |
| 29 | { |
| 30 | flow_dissector->used_keys |= (1 << key_id); |
| 31 | } |
| 32 | |
| 33 | static void *skb_flow_dissector_target(struct flow_dissector *flow_dissector, |
| 34 | enum flow_dissector_key_id key_id, |
| 35 | void *target_container) |
| 36 | { |
| 37 | return ((char *) target_container) + flow_dissector->offset[key_id]; |
| 38 | } |
| 39 | |
| 40 | void skb_flow_dissector_init(struct flow_dissector *flow_dissector, |
| 41 | const struct flow_dissector_key *key, |
| 42 | unsigned int key_count) |
| 43 | { |
| 44 | unsigned int i; |
| 45 | |
| 46 | memset(flow_dissector, 0, sizeof(*flow_dissector)); |
| 47 | |
| 48 | for (i = 0; i < key_count; i++, key++) { |
| 49 | /* User should make sure that every key target offset is withing |
| 50 | * boundaries of unsigned short. |
| 51 | */ |
| 52 | BUG_ON(key->offset > USHRT_MAX); |
| 53 | BUG_ON(skb_flow_dissector_uses_key(flow_dissector, |
| 54 | key->key_id)); |
| 55 | |
| 56 | skb_flow_dissector_set_key(flow_dissector, key->key_id); |
| 57 | flow_dissector->offset[key->key_id] = key->offset; |
| 58 | } |
| 59 | |
| 60 | /* Ensure that the dissector always includes basic key. That way |
| 61 | * we are able to avoid handling lack of it in fast path. |
| 62 | */ |
| 63 | BUG_ON(!skb_flow_dissector_uses_key(flow_dissector, |
| 64 | FLOW_DISSECTOR_KEY_BASIC)); |
| 65 | } |
| 66 | EXPORT_SYMBOL(skb_flow_dissector_init); |
| 67 | |
Nikolay Aleksandrov | 357afe9 | 2013-10-02 13:39:24 +0200 | [diff] [blame] | 68 | /** |
WANG Cong | 6451b3f | 2014-08-25 17:03:46 -0700 | [diff] [blame] | 69 | * __skb_flow_get_ports - extract the upper layer ports and return them |
| 70 | * @skb: sk_buff to extract the ports from |
Nikolay Aleksandrov | 357afe9 | 2013-10-02 13:39:24 +0200 | [diff] [blame] | 71 | * @thoff: transport header offset |
| 72 | * @ip_proto: protocol for which to get port offset |
WANG Cong | 6451b3f | 2014-08-25 17:03:46 -0700 | [diff] [blame] | 73 | * @data: raw buffer pointer to the packet, if NULL use skb->data |
| 74 | * @hlen: packet header length, if @data is NULL use skb_headlen(skb) |
Nikolay Aleksandrov | 357afe9 | 2013-10-02 13:39:24 +0200 | [diff] [blame] | 75 | * |
| 76 | * The function will try to retrieve the ports at offset thoff + poff where poff |
| 77 | * is the protocol port offset returned from proto_ports_offset |
| 78 | */ |
David S. Miller | 690e36e | 2014-08-23 12:13:41 -0700 | [diff] [blame] | 79 | __be32 __skb_flow_get_ports(const struct sk_buff *skb, int thoff, u8 ip_proto, |
| 80 | void *data, int hlen) |
Nikolay Aleksandrov | 357afe9 | 2013-10-02 13:39:24 +0200 | [diff] [blame] | 81 | { |
| 82 | int poff = proto_ports_offset(ip_proto); |
| 83 | |
David S. Miller | 690e36e | 2014-08-23 12:13:41 -0700 | [diff] [blame] | 84 | if (!data) { |
| 85 | data = skb->data; |
| 86 | hlen = skb_headlen(skb); |
| 87 | } |
| 88 | |
Nikolay Aleksandrov | 357afe9 | 2013-10-02 13:39:24 +0200 | [diff] [blame] | 89 | if (poff >= 0) { |
| 90 | __be32 *ports, _ports; |
| 91 | |
David S. Miller | 690e36e | 2014-08-23 12:13:41 -0700 | [diff] [blame] | 92 | ports = __skb_header_pointer(skb, thoff + poff, |
| 93 | sizeof(_ports), data, hlen, &_ports); |
Nikolay Aleksandrov | 357afe9 | 2013-10-02 13:39:24 +0200 | [diff] [blame] | 94 | if (ports) |
| 95 | return *ports; |
| 96 | } |
| 97 | |
| 98 | return 0; |
| 99 | } |
David S. Miller | 690e36e | 2014-08-23 12:13:41 -0700 | [diff] [blame] | 100 | EXPORT_SYMBOL(__skb_flow_get_ports); |
Nikolay Aleksandrov | 357afe9 | 2013-10-02 13:39:24 +0200 | [diff] [blame] | 101 | |
WANG Cong | 453a940 | 2014-08-25 17:03:47 -0700 | [diff] [blame] | 102 | /** |
| 103 | * __skb_flow_dissect - extract the flow_keys struct and return it |
| 104 | * @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] | 105 | * @flow_dissector: list of keys to dissect |
| 106 | * @target_container: target structure to put dissected values into |
WANG Cong | 453a940 | 2014-08-25 17:03:47 -0700 | [diff] [blame] | 107 | * @data: raw buffer pointer to the packet, if NULL use skb->data |
| 108 | * @proto: protocol for which to get the flow, if @data is NULL use skb->protocol |
| 109 | * @nhoff: network header offset, if @data is NULL use skb_network_offset(skb) |
| 110 | * @hlen: packet header length, if @data is NULL use skb_headlen(skb) |
| 111 | * |
Jiri Pirko | 06635a3 | 2015-05-12 14:56:16 +0200 | [diff] [blame] | 112 | * The function will try to retrieve individual keys into target specified |
| 113 | * by flow_dissector from either the skbuff or a raw buffer specified by the |
| 114 | * rest parameters. |
| 115 | * |
| 116 | * Caller must take care of zeroing target container memory. |
WANG Cong | 453a940 | 2014-08-25 17:03:47 -0700 | [diff] [blame] | 117 | */ |
Jiri Pirko | 06635a3 | 2015-05-12 14:56:16 +0200 | [diff] [blame] | 118 | bool __skb_flow_dissect(const struct sk_buff *skb, |
| 119 | struct flow_dissector *flow_dissector, |
| 120 | void *target_container, |
WANG Cong | 453a940 | 2014-08-25 17:03:47 -0700 | [diff] [blame] | 121 | void *data, __be16 proto, int nhoff, int hlen) |
Eric Dumazet | 0744dd0 | 2011-11-28 05:22:18 +0000 | [diff] [blame] | 122 | { |
Jiri Pirko | 06635a3 | 2015-05-12 14:56:16 +0200 | [diff] [blame] | 123 | struct flow_dissector_key_basic *key_basic; |
| 124 | struct flow_dissector_key_addrs *key_addrs; |
| 125 | struct flow_dissector_key_ports *key_ports; |
Eric Dumazet | 0744dd0 | 2011-11-28 05:22:18 +0000 | [diff] [blame] | 126 | u8 ip_proto; |
Eric Dumazet | 0744dd0 | 2011-11-28 05:22:18 +0000 | [diff] [blame] | 127 | |
David S. Miller | 690e36e | 2014-08-23 12:13:41 -0700 | [diff] [blame] | 128 | if (!data) { |
| 129 | data = skb->data; |
WANG Cong | 453a940 | 2014-08-25 17:03:47 -0700 | [diff] [blame] | 130 | proto = skb->protocol; |
| 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 | |
Jiri Pirko | 06635a3 | 2015-05-12 14:56:16 +0200 | [diff] [blame] | 135 | /* It is ensured by skb_flow_dissector_init() that basic key will |
| 136 | * be always present. |
| 137 | */ |
| 138 | key_basic = skb_flow_dissector_target(flow_dissector, |
| 139 | FLOW_DISSECTOR_KEY_BASIC, |
| 140 | target_container); |
Eric Dumazet | 0744dd0 | 2011-11-28 05:22:18 +0000 | [diff] [blame] | 141 | |
Jiri Pirko | 67a900c | 2015-05-12 14:56:19 +0200 | [diff] [blame^] | 142 | if (skb_flow_dissector_uses_key(flow_dissector, |
| 143 | FLOW_DISSECTOR_KEY_ETH_ADDRS)) { |
| 144 | struct ethhdr *eth = eth_hdr(skb); |
| 145 | struct flow_dissector_key_eth_addrs *key_eth_addrs; |
| 146 | |
| 147 | key_eth_addrs = skb_flow_dissector_target(flow_dissector, |
| 148 | FLOW_DISSECTOR_KEY_ETH_ADDRS, |
| 149 | target_container); |
| 150 | memcpy(key_eth_addrs, ð->h_dest, sizeof(*key_eth_addrs)); |
| 151 | } |
| 152 | |
Eric Dumazet | 0744dd0 | 2011-11-28 05:22:18 +0000 | [diff] [blame] | 153 | again: |
| 154 | switch (proto) { |
Joe Perches | 2b8837a | 2014-03-12 10:04:17 -0700 | [diff] [blame] | 155 | case htons(ETH_P_IP): { |
Eric Dumazet | 0744dd0 | 2011-11-28 05:22:18 +0000 | [diff] [blame] | 156 | const struct iphdr *iph; |
| 157 | struct iphdr _iph; |
| 158 | ip: |
David S. Miller | 690e36e | 2014-08-23 12:13:41 -0700 | [diff] [blame] | 159 | iph = __skb_header_pointer(skb, nhoff, sizeof(_iph), data, hlen, &_iph); |
Jason Wang | 6f09234 | 2013-11-01 15:01:10 +0800 | [diff] [blame] | 160 | if (!iph || iph->ihl < 5) |
Eric Dumazet | 0744dd0 | 2011-11-28 05:22:18 +0000 | [diff] [blame] | 161 | return false; |
Eric Dumazet | 3797d3e | 2013-11-07 08:37:28 -0800 | [diff] [blame] | 162 | nhoff += iph->ihl * 4; |
Eric Dumazet | 0744dd0 | 2011-11-28 05:22:18 +0000 | [diff] [blame] | 163 | |
Eric Dumazet | 3797d3e | 2013-11-07 08:37:28 -0800 | [diff] [blame] | 164 | ip_proto = iph->protocol; |
Eric Dumazet | 0744dd0 | 2011-11-28 05:22:18 +0000 | [diff] [blame] | 165 | if (ip_is_fragment(iph)) |
| 166 | ip_proto = 0; |
Eric Dumazet | 3797d3e | 2013-11-07 08:37:28 -0800 | [diff] [blame] | 167 | |
Jiri Pirko | 06635a3 | 2015-05-12 14:56:16 +0200 | [diff] [blame] | 168 | if (!skb_flow_dissector_uses_key(flow_dissector, |
| 169 | FLOW_DISSECTOR_KEY_IPV4_ADDRS)) |
Alexander Duyck | 5af7fb6 | 2014-10-10 12:09:12 -0700 | [diff] [blame] | 170 | break; |
Jiri Pirko | 06635a3 | 2015-05-12 14:56:16 +0200 | [diff] [blame] | 171 | key_addrs = skb_flow_dissector_target(flow_dissector, |
| 172 | FLOW_DISSECTOR_KEY_IPV4_ADDRS, |
| 173 | target_container); |
| 174 | memcpy(key_addrs, &iph->saddr, sizeof(*key_addrs)); |
Eric Dumazet | 0744dd0 | 2011-11-28 05:22:18 +0000 | [diff] [blame] | 175 | break; |
| 176 | } |
Joe Perches | 2b8837a | 2014-03-12 10:04:17 -0700 | [diff] [blame] | 177 | case htons(ETH_P_IPV6): { |
Eric Dumazet | 0744dd0 | 2011-11-28 05:22:18 +0000 | [diff] [blame] | 178 | const struct ipv6hdr *iph; |
| 179 | struct ipv6hdr _iph; |
Tom Herbert | 19469a8 | 2014-07-01 21:33:01 -0700 | [diff] [blame] | 180 | __be32 flow_label; |
| 181 | |
Eric Dumazet | 0744dd0 | 2011-11-28 05:22:18 +0000 | [diff] [blame] | 182 | ipv6: |
David S. Miller | 690e36e | 2014-08-23 12:13:41 -0700 | [diff] [blame] | 183 | iph = __skb_header_pointer(skb, nhoff, sizeof(_iph), data, hlen, &_iph); |
Eric Dumazet | 0744dd0 | 2011-11-28 05:22:18 +0000 | [diff] [blame] | 184 | if (!iph) |
| 185 | return false; |
| 186 | |
| 187 | ip_proto = iph->nexthdr; |
Eric Dumazet | 0744dd0 | 2011-11-28 05:22:18 +0000 | [diff] [blame] | 188 | nhoff += sizeof(struct ipv6hdr); |
Tom Herbert | 19469a8 | 2014-07-01 21:33:01 -0700 | [diff] [blame] | 189 | |
Jiri Pirko | b924933 | 2015-05-12 14:56:18 +0200 | [diff] [blame] | 190 | if (skb_flow_dissector_uses_key(flow_dissector, |
| 191 | FLOW_DISSECTOR_KEY_IPV6_HASH_ADDRS)) { |
| 192 | key_addrs = skb_flow_dissector_target(flow_dissector, |
| 193 | FLOW_DISSECTOR_KEY_IPV6_HASH_ADDRS, |
| 194 | target_container); |
Alexander Duyck | 56193d1 | 2014-09-05 19:20:26 -0400 | [diff] [blame] | 195 | |
Jiri Pirko | b924933 | 2015-05-12 14:56:18 +0200 | [diff] [blame] | 196 | key_addrs->src = (__force __be32)ipv6_addr_hash(&iph->saddr); |
| 197 | key_addrs->dst = (__force __be32)ipv6_addr_hash(&iph->daddr); |
| 198 | goto flow_label; |
| 199 | } |
| 200 | if (skb_flow_dissector_uses_key(flow_dissector, |
| 201 | FLOW_DISSECTOR_KEY_IPV6_ADDRS)) { |
| 202 | struct flow_dissector_key_ipv6_addrs *key_ipv6_addrs; |
Alexander Duyck | 5af7fb6 | 2014-10-10 12:09:12 -0700 | [diff] [blame] | 203 | |
Jiri Pirko | b924933 | 2015-05-12 14:56:18 +0200 | [diff] [blame] | 204 | key_ipv6_addrs = skb_flow_dissector_target(flow_dissector, |
| 205 | FLOW_DISSECTOR_KEY_IPV6_ADDRS, |
| 206 | target_container); |
| 207 | |
| 208 | memcpy(key_ipv6_addrs, &iph->saddr, sizeof(*key_ipv6_addrs)); |
| 209 | goto flow_label; |
| 210 | } |
| 211 | break; |
| 212 | flow_label: |
Tom Herbert | 19469a8 | 2014-07-01 21:33:01 -0700 | [diff] [blame] | 213 | flow_label = ip6_flowlabel(iph); |
| 214 | if (flow_label) { |
| 215 | /* Awesome, IPv6 packet has a flow label so we can |
| 216 | * use that to represent the ports without any |
| 217 | * further dissection. |
| 218 | */ |
Jiri Pirko | 06635a3 | 2015-05-12 14:56:16 +0200 | [diff] [blame] | 219 | |
| 220 | key_basic->n_proto = proto; |
| 221 | key_basic->ip_proto = ip_proto; |
| 222 | key_basic->thoff = (u16)nhoff; |
| 223 | |
| 224 | if (!skb_flow_dissector_uses_key(flow_dissector, |
| 225 | FLOW_DISSECTOR_KEY_PORTS)) |
| 226 | break; |
| 227 | key_ports = skb_flow_dissector_target(flow_dissector, |
| 228 | FLOW_DISSECTOR_KEY_PORTS, |
| 229 | target_container); |
| 230 | key_ports->ports = flow_label; |
Tom Herbert | 19469a8 | 2014-07-01 21:33:01 -0700 | [diff] [blame] | 231 | |
| 232 | return true; |
| 233 | } |
| 234 | |
Eric Dumazet | 0744dd0 | 2011-11-28 05:22:18 +0000 | [diff] [blame] | 235 | break; |
| 236 | } |
Joe Perches | 2b8837a | 2014-03-12 10:04:17 -0700 | [diff] [blame] | 237 | case htons(ETH_P_8021AD): |
| 238 | case htons(ETH_P_8021Q): { |
Eric Dumazet | 0744dd0 | 2011-11-28 05:22:18 +0000 | [diff] [blame] | 239 | const struct vlan_hdr *vlan; |
| 240 | struct vlan_hdr _vlan; |
| 241 | |
David S. Miller | 690e36e | 2014-08-23 12:13:41 -0700 | [diff] [blame] | 242 | vlan = __skb_header_pointer(skb, nhoff, sizeof(_vlan), data, hlen, &_vlan); |
Eric Dumazet | 0744dd0 | 2011-11-28 05:22:18 +0000 | [diff] [blame] | 243 | if (!vlan) |
| 244 | return false; |
| 245 | |
| 246 | proto = vlan->h_vlan_encapsulated_proto; |
| 247 | nhoff += sizeof(*vlan); |
| 248 | goto again; |
| 249 | } |
Joe Perches | 2b8837a | 2014-03-12 10:04:17 -0700 | [diff] [blame] | 250 | case htons(ETH_P_PPP_SES): { |
Eric Dumazet | 0744dd0 | 2011-11-28 05:22:18 +0000 | [diff] [blame] | 251 | struct { |
| 252 | struct pppoe_hdr hdr; |
| 253 | __be16 proto; |
| 254 | } *hdr, _hdr; |
David S. Miller | 690e36e | 2014-08-23 12:13:41 -0700 | [diff] [blame] | 255 | hdr = __skb_header_pointer(skb, nhoff, sizeof(_hdr), data, hlen, &_hdr); |
Eric Dumazet | 0744dd0 | 2011-11-28 05:22:18 +0000 | [diff] [blame] | 256 | if (!hdr) |
| 257 | return false; |
| 258 | proto = hdr->proto; |
| 259 | nhoff += PPPOE_SES_HLEN; |
| 260 | switch (proto) { |
Joe Perches | 2b8837a | 2014-03-12 10:04:17 -0700 | [diff] [blame] | 261 | case htons(PPP_IP): |
Eric Dumazet | 0744dd0 | 2011-11-28 05:22:18 +0000 | [diff] [blame] | 262 | goto ip; |
Joe Perches | 2b8837a | 2014-03-12 10:04:17 -0700 | [diff] [blame] | 263 | case htons(PPP_IPV6): |
Eric Dumazet | 0744dd0 | 2011-11-28 05:22:18 +0000 | [diff] [blame] | 264 | goto ipv6; |
| 265 | default: |
| 266 | return false; |
| 267 | } |
| 268 | } |
Erik Hugne | 08bfc9c | 2015-01-22 17:10:32 +0100 | [diff] [blame] | 269 | case htons(ETH_P_TIPC): { |
| 270 | struct { |
| 271 | __be32 pre[3]; |
| 272 | __be32 srcnode; |
| 273 | } *hdr, _hdr; |
| 274 | hdr = __skb_header_pointer(skb, nhoff, sizeof(_hdr), data, hlen, &_hdr); |
| 275 | if (!hdr) |
| 276 | return false; |
Jiri Pirko | 06635a3 | 2015-05-12 14:56:16 +0200 | [diff] [blame] | 277 | key_basic->n_proto = proto; |
| 278 | key_basic->thoff = (u16)nhoff; |
| 279 | |
| 280 | if (skb_flow_dissector_uses_key(flow_dissector, |
| 281 | FLOW_DISSECTOR_KEY_IPV6_HASH_ADDRS)) { |
| 282 | return true; |
| 283 | key_addrs = skb_flow_dissector_target(flow_dissector, |
| 284 | FLOW_DISSECTOR_KEY_IPV6_HASH_ADDRS, |
| 285 | target_container); |
| 286 | key_addrs->src = hdr->srcnode; |
| 287 | key_addrs->dst = 0; |
| 288 | } |
Erik Hugne | 08bfc9c | 2015-01-22 17:10:32 +0100 | [diff] [blame] | 289 | return true; |
| 290 | } |
Alexander Duyck | 56193d1 | 2014-09-05 19:20:26 -0400 | [diff] [blame] | 291 | case htons(ETH_P_FCOE): |
Jiri Pirko | 06635a3 | 2015-05-12 14:56:16 +0200 | [diff] [blame] | 292 | key_basic->thoff = (u16)(nhoff + FCOE_HEADER_LEN); |
Alexander Duyck | 56193d1 | 2014-09-05 19:20:26 -0400 | [diff] [blame] | 293 | /* fall through */ |
Eric Dumazet | 0744dd0 | 2011-11-28 05:22:18 +0000 | [diff] [blame] | 294 | default: |
| 295 | return false; |
| 296 | } |
| 297 | |
| 298 | switch (ip_proto) { |
| 299 | case IPPROTO_GRE: { |
| 300 | struct gre_hdr { |
| 301 | __be16 flags; |
| 302 | __be16 proto; |
| 303 | } *hdr, _hdr; |
| 304 | |
David S. Miller | 690e36e | 2014-08-23 12:13:41 -0700 | [diff] [blame] | 305 | hdr = __skb_header_pointer(skb, nhoff, sizeof(_hdr), data, hlen, &_hdr); |
Eric Dumazet | 0744dd0 | 2011-11-28 05:22:18 +0000 | [diff] [blame] | 306 | if (!hdr) |
| 307 | return false; |
| 308 | /* |
| 309 | * Only look inside GRE if version zero and no |
| 310 | * routing |
| 311 | */ |
| 312 | if (!(hdr->flags & (GRE_VERSION|GRE_ROUTING))) { |
| 313 | proto = hdr->proto; |
| 314 | nhoff += 4; |
| 315 | if (hdr->flags & GRE_CSUM) |
| 316 | nhoff += 4; |
| 317 | if (hdr->flags & GRE_KEY) |
| 318 | nhoff += 4; |
| 319 | if (hdr->flags & GRE_SEQ) |
| 320 | nhoff += 4; |
Michael Dalton | e1733de | 2013-03-11 06:52:28 +0000 | [diff] [blame] | 321 | if (proto == htons(ETH_P_TEB)) { |
| 322 | const struct ethhdr *eth; |
| 323 | struct ethhdr _eth; |
| 324 | |
David S. Miller | 690e36e | 2014-08-23 12:13:41 -0700 | [diff] [blame] | 325 | eth = __skb_header_pointer(skb, nhoff, |
| 326 | sizeof(_eth), |
| 327 | data, hlen, &_eth); |
Michael Dalton | e1733de | 2013-03-11 06:52:28 +0000 | [diff] [blame] | 328 | if (!eth) |
| 329 | return false; |
| 330 | proto = eth->h_proto; |
| 331 | nhoff += sizeof(*eth); |
| 332 | } |
Eric Dumazet | 0744dd0 | 2011-11-28 05:22:18 +0000 | [diff] [blame] | 333 | goto again; |
| 334 | } |
| 335 | break; |
| 336 | } |
| 337 | case IPPROTO_IPIP: |
Tom Herbert | fca4189 | 2013-07-29 11:07:36 -0700 | [diff] [blame] | 338 | proto = htons(ETH_P_IP); |
| 339 | goto ip; |
Tom Herbert | b438f94 | 2013-07-29 11:07:42 -0700 | [diff] [blame] | 340 | case IPPROTO_IPV6: |
| 341 | proto = htons(ETH_P_IPV6); |
| 342 | goto ipv6; |
Eric Dumazet | 0744dd0 | 2011-11-28 05:22:18 +0000 | [diff] [blame] | 343 | default: |
| 344 | break; |
| 345 | } |
| 346 | |
Jiri Pirko | 06635a3 | 2015-05-12 14:56:16 +0200 | [diff] [blame] | 347 | /* It is ensured by skb_flow_dissector_init() that basic key will |
| 348 | * be always present. |
| 349 | */ |
| 350 | key_basic = skb_flow_dissector_target(flow_dissector, |
| 351 | FLOW_DISSECTOR_KEY_BASIC, |
| 352 | target_container); |
| 353 | key_basic->n_proto = proto; |
| 354 | key_basic->ip_proto = ip_proto; |
| 355 | key_basic->thoff = (u16) nhoff; |
Daniel Borkmann | 8ed7816 | 2013-03-19 06:39:29 +0000 | [diff] [blame] | 356 | |
Jiri Pirko | 06635a3 | 2015-05-12 14:56:16 +0200 | [diff] [blame] | 357 | if (skb_flow_dissector_uses_key(flow_dissector, |
| 358 | FLOW_DISSECTOR_KEY_PORTS)) { |
| 359 | key_ports = skb_flow_dissector_target(flow_dissector, |
| 360 | FLOW_DISSECTOR_KEY_PORTS, |
| 361 | target_container); |
| 362 | key_ports->ports = __skb_flow_get_ports(skb, nhoff, ip_proto, |
| 363 | data, hlen); |
| 364 | } |
Alexander Duyck | 5af7fb6 | 2014-10-10 12:09:12 -0700 | [diff] [blame] | 365 | |
Eric Dumazet | 0744dd0 | 2011-11-28 05:22:18 +0000 | [diff] [blame] | 366 | return true; |
| 367 | } |
David S. Miller | 690e36e | 2014-08-23 12:13:41 -0700 | [diff] [blame] | 368 | EXPORT_SYMBOL(__skb_flow_dissect); |
Cong Wang | 441d9d3 | 2013-01-21 00:39:24 +0000 | [diff] [blame] | 369 | |
| 370 | static u32 hashrnd __read_mostly; |
Hannes Frederic Sowa | 66415cf | 2013-10-23 20:06:00 +0200 | [diff] [blame] | 371 | static __always_inline void __flow_hash_secret_init(void) |
| 372 | { |
| 373 | net_get_random_once(&hashrnd, sizeof(hashrnd)); |
| 374 | } |
| 375 | |
Tom Herbert | 50fb799 | 2015-05-01 11:30:12 -0700 | [diff] [blame] | 376 | static __always_inline u32 __flow_hash_3words(u32 a, u32 b, u32 c, u32 keyval) |
Hannes Frederic Sowa | 66415cf | 2013-10-23 20:06:00 +0200 | [diff] [blame] | 377 | { |
Tom Herbert | 50fb799 | 2015-05-01 11:30:12 -0700 | [diff] [blame] | 378 | return jhash_3words(a, b, c, keyval); |
Hannes Frederic Sowa | 66415cf | 2013-10-23 20:06:00 +0200 | [diff] [blame] | 379 | } |
| 380 | |
Tom Herbert | 50fb799 | 2015-05-01 11:30:12 -0700 | [diff] [blame] | 381 | 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] | 382 | { |
| 383 | u32 hash; |
| 384 | |
| 385 | /* get a consistent hash (same value on both flow directions) */ |
Jiri Pirko | 06635a3 | 2015-05-12 14:56:16 +0200 | [diff] [blame] | 386 | if (((__force u32)keys->addrs.dst < (__force u32)keys->addrs.src) || |
| 387 | (((__force u32)keys->addrs.dst == (__force u32)keys->addrs.src) && |
| 388 | ((__force u16)keys->ports.port16[1] < (__force u16)keys->ports.port16[0]))) { |
| 389 | swap(keys->addrs.dst, keys->addrs.src); |
| 390 | swap(keys->ports.port16[0], keys->ports.port16[1]); |
Tom Herbert | 5ed20a6 | 2014-07-01 21:32:05 -0700 | [diff] [blame] | 391 | } |
| 392 | |
Jiri Pirko | 06635a3 | 2015-05-12 14:56:16 +0200 | [diff] [blame] | 393 | hash = __flow_hash_3words((__force u32)keys->addrs.dst, |
| 394 | (__force u32)keys->addrs.src, |
| 395 | (__force u32)keys->ports.ports, |
Tom Herbert | 50fb799 | 2015-05-01 11:30:12 -0700 | [diff] [blame] | 396 | keyval); |
Tom Herbert | 5ed20a6 | 2014-07-01 21:32:05 -0700 | [diff] [blame] | 397 | if (!hash) |
| 398 | hash = 1; |
| 399 | |
| 400 | return hash; |
| 401 | } |
| 402 | |
| 403 | u32 flow_hash_from_keys(struct flow_keys *keys) |
| 404 | { |
Tom Herbert | 50fb799 | 2015-05-01 11:30:12 -0700 | [diff] [blame] | 405 | __flow_hash_secret_init(); |
| 406 | return __flow_hash_from_keys(keys, hashrnd); |
Tom Herbert | 5ed20a6 | 2014-07-01 21:32:05 -0700 | [diff] [blame] | 407 | } |
| 408 | EXPORT_SYMBOL(flow_hash_from_keys); |
| 409 | |
Tom Herbert | 50fb799 | 2015-05-01 11:30:12 -0700 | [diff] [blame] | 410 | static inline u32 ___skb_get_hash(const struct sk_buff *skb, |
| 411 | struct flow_keys *keys, u32 keyval) |
| 412 | { |
Jiri Pirko | 06635a3 | 2015-05-12 14:56:16 +0200 | [diff] [blame] | 413 | if (!skb_flow_dissect_flow_keys(skb, keys)) |
Tom Herbert | 50fb799 | 2015-05-01 11:30:12 -0700 | [diff] [blame] | 414 | return 0; |
| 415 | |
| 416 | return __flow_hash_from_keys(keys, keyval); |
| 417 | } |
| 418 | |
Tom Herbert | 2f59e1e | 2015-05-01 11:30:17 -0700 | [diff] [blame] | 419 | struct _flow_keys_digest_data { |
| 420 | __be16 n_proto; |
| 421 | u8 ip_proto; |
| 422 | u8 padding; |
| 423 | __be32 ports; |
| 424 | __be32 src; |
| 425 | __be32 dst; |
| 426 | }; |
| 427 | |
| 428 | void make_flow_keys_digest(struct flow_keys_digest *digest, |
| 429 | const struct flow_keys *flow) |
| 430 | { |
| 431 | struct _flow_keys_digest_data *data = |
| 432 | (struct _flow_keys_digest_data *)digest; |
| 433 | |
| 434 | BUILD_BUG_ON(sizeof(*data) > sizeof(*digest)); |
| 435 | |
| 436 | memset(digest, 0, sizeof(*digest)); |
| 437 | |
Jiri Pirko | 06635a3 | 2015-05-12 14:56:16 +0200 | [diff] [blame] | 438 | data->n_proto = flow->basic.n_proto; |
| 439 | data->ip_proto = flow->basic.ip_proto; |
| 440 | data->ports = flow->ports.ports; |
| 441 | data->src = flow->addrs.src; |
| 442 | data->dst = flow->addrs.dst; |
Tom Herbert | 2f59e1e | 2015-05-01 11:30:17 -0700 | [diff] [blame] | 443 | } |
| 444 | EXPORT_SYMBOL(make_flow_keys_digest); |
| 445 | |
Jiri Pirko | d4fd327 | 2015-05-12 14:56:10 +0200 | [diff] [blame] | 446 | /** |
| 447 | * __skb_get_hash: calculate a flow hash |
| 448 | * @skb: sk_buff to calculate flow hash from |
| 449 | * |
| 450 | * This function calculates a flow hash based on src/dst addresses |
Tom Herbert | 61b905d | 2014-03-24 15:34:47 -0700 | [diff] [blame] | 451 | * and src/dst port numbers. Sets hash in skb to non-zero hash value |
| 452 | * 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] | 453 | * if hash is a canonical 4-tuple hash over transport ports. |
| 454 | */ |
Tom Herbert | 3958afa1b | 2013-12-15 22:12:06 -0800 | [diff] [blame] | 455 | void __skb_get_hash(struct sk_buff *skb) |
Cong Wang | 441d9d3 | 2013-01-21 00:39:24 +0000 | [diff] [blame] | 456 | { |
| 457 | struct flow_keys keys; |
Tom Herbert | 50fb799 | 2015-05-01 11:30:12 -0700 | [diff] [blame] | 458 | u32 hash; |
Cong Wang | 441d9d3 | 2013-01-21 00:39:24 +0000 | [diff] [blame] | 459 | |
Tom Herbert | 50fb799 | 2015-05-01 11:30:12 -0700 | [diff] [blame] | 460 | __flow_hash_secret_init(); |
| 461 | |
| 462 | hash = ___skb_get_hash(skb, &keys, hashrnd); |
| 463 | if (!hash) |
Cong Wang | 441d9d3 | 2013-01-21 00:39:24 +0000 | [diff] [blame] | 464 | return; |
Jiri Pirko | 06635a3 | 2015-05-12 14:56:16 +0200 | [diff] [blame] | 465 | if (keys.ports.ports) |
Tom Herbert | 61b905d | 2014-03-24 15:34:47 -0700 | [diff] [blame] | 466 | skb->l4_hash = 1; |
Tom Herbert | a3b18dd | 2014-07-01 21:33:17 -0700 | [diff] [blame] | 467 | skb->sw_hash = 1; |
Tom Herbert | 50fb799 | 2015-05-01 11:30:12 -0700 | [diff] [blame] | 468 | skb->hash = hash; |
Cong Wang | 441d9d3 | 2013-01-21 00:39:24 +0000 | [diff] [blame] | 469 | } |
Tom Herbert | 3958afa1b | 2013-12-15 22:12:06 -0800 | [diff] [blame] | 470 | EXPORT_SYMBOL(__skb_get_hash); |
Cong Wang | 441d9d3 | 2013-01-21 00:39:24 +0000 | [diff] [blame] | 471 | |
Tom Herbert | 50fb799 | 2015-05-01 11:30:12 -0700 | [diff] [blame] | 472 | __u32 skb_get_hash_perturb(const struct sk_buff *skb, u32 perturb) |
| 473 | { |
| 474 | struct flow_keys keys; |
| 475 | |
| 476 | return ___skb_get_hash(skb, &keys, perturb); |
| 477 | } |
| 478 | EXPORT_SYMBOL(skb_get_hash_perturb); |
| 479 | |
Alexander Duyck | 56193d1 | 2014-09-05 19:20:26 -0400 | [diff] [blame] | 480 | u32 __skb_get_poff(const struct sk_buff *skb, void *data, |
| 481 | const struct flow_keys *keys, int hlen) |
Daniel Borkmann | f77668d | 2013-03-19 06:39:30 +0000 | [diff] [blame] | 482 | { |
Jiri Pirko | 06635a3 | 2015-05-12 14:56:16 +0200 | [diff] [blame] | 483 | u32 poff = keys->basic.thoff; |
Daniel Borkmann | f77668d | 2013-03-19 06:39:30 +0000 | [diff] [blame] | 484 | |
Jiri Pirko | 06635a3 | 2015-05-12 14:56:16 +0200 | [diff] [blame] | 485 | switch (keys->basic.ip_proto) { |
Daniel Borkmann | f77668d | 2013-03-19 06:39:30 +0000 | [diff] [blame] | 486 | case IPPROTO_TCP: { |
Alexander Duyck | 5af7fb6 | 2014-10-10 12:09:12 -0700 | [diff] [blame] | 487 | /* access doff as u8 to avoid unaligned access */ |
| 488 | const u8 *doff; |
| 489 | u8 _doff; |
Daniel Borkmann | f77668d | 2013-03-19 06:39:30 +0000 | [diff] [blame] | 490 | |
Alexander Duyck | 5af7fb6 | 2014-10-10 12:09:12 -0700 | [diff] [blame] | 491 | doff = __skb_header_pointer(skb, poff + 12, sizeof(_doff), |
| 492 | data, hlen, &_doff); |
| 493 | if (!doff) |
Daniel Borkmann | f77668d | 2013-03-19 06:39:30 +0000 | [diff] [blame] | 494 | return poff; |
| 495 | |
Alexander Duyck | 5af7fb6 | 2014-10-10 12:09:12 -0700 | [diff] [blame] | 496 | poff += max_t(u32, sizeof(struct tcphdr), (*doff & 0xF0) >> 2); |
Daniel Borkmann | f77668d | 2013-03-19 06:39:30 +0000 | [diff] [blame] | 497 | break; |
| 498 | } |
| 499 | case IPPROTO_UDP: |
| 500 | case IPPROTO_UDPLITE: |
| 501 | poff += sizeof(struct udphdr); |
| 502 | break; |
| 503 | /* For the rest, we do not really care about header |
| 504 | * extensions at this point for now. |
| 505 | */ |
| 506 | case IPPROTO_ICMP: |
| 507 | poff += sizeof(struct icmphdr); |
| 508 | break; |
| 509 | case IPPROTO_ICMPV6: |
| 510 | poff += sizeof(struct icmp6hdr); |
| 511 | break; |
| 512 | case IPPROTO_IGMP: |
| 513 | poff += sizeof(struct igmphdr); |
| 514 | break; |
| 515 | case IPPROTO_DCCP: |
| 516 | poff += sizeof(struct dccp_hdr); |
| 517 | break; |
| 518 | case IPPROTO_SCTP: |
| 519 | poff += sizeof(struct sctphdr); |
| 520 | break; |
| 521 | } |
| 522 | |
| 523 | return poff; |
| 524 | } |
| 525 | |
Jiri Pirko | 0db89b8 | 2015-05-12 14:56:14 +0200 | [diff] [blame] | 526 | /** |
| 527 | * skb_get_poff - get the offset to the payload |
| 528 | * @skb: sk_buff to get the payload offset from |
| 529 | * |
| 530 | * The function will get the offset to the payload as far as it could |
| 531 | * 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] | 532 | * truncate packets without needing to push actual payload to the user |
| 533 | * space and can analyze headers only, instead. |
| 534 | */ |
| 535 | u32 skb_get_poff(const struct sk_buff *skb) |
| 536 | { |
| 537 | struct flow_keys keys; |
| 538 | |
Jiri Pirko | 06635a3 | 2015-05-12 14:56:16 +0200 | [diff] [blame] | 539 | if (!skb_flow_dissect_flow_keys(skb, &keys)) |
Alexander Duyck | 56193d1 | 2014-09-05 19:20:26 -0400 | [diff] [blame] | 540 | return 0; |
| 541 | |
| 542 | return __skb_get_poff(skb, skb->data, &keys, skb_headlen(skb)); |
| 543 | } |
Jiri Pirko | 06635a3 | 2015-05-12 14:56:16 +0200 | [diff] [blame] | 544 | |
| 545 | static const struct flow_dissector_key flow_keys_dissector_keys[] = { |
| 546 | { |
| 547 | .key_id = FLOW_DISSECTOR_KEY_BASIC, |
| 548 | .offset = offsetof(struct flow_keys, basic), |
| 549 | }, |
| 550 | { |
| 551 | .key_id = FLOW_DISSECTOR_KEY_IPV4_ADDRS, |
| 552 | .offset = offsetof(struct flow_keys, addrs), |
| 553 | }, |
| 554 | { |
| 555 | .key_id = FLOW_DISSECTOR_KEY_IPV6_HASH_ADDRS, |
| 556 | .offset = offsetof(struct flow_keys, addrs), |
| 557 | }, |
| 558 | { |
| 559 | .key_id = FLOW_DISSECTOR_KEY_PORTS, |
| 560 | .offset = offsetof(struct flow_keys, ports), |
| 561 | }, |
| 562 | }; |
| 563 | |
| 564 | static const struct flow_dissector_key flow_keys_buf_dissector_keys[] = { |
| 565 | { |
| 566 | .key_id = FLOW_DISSECTOR_KEY_BASIC, |
| 567 | .offset = offsetof(struct flow_keys, basic), |
| 568 | }, |
| 569 | }; |
| 570 | |
| 571 | struct flow_dissector flow_keys_dissector __read_mostly; |
| 572 | EXPORT_SYMBOL(flow_keys_dissector); |
| 573 | |
| 574 | struct flow_dissector flow_keys_buf_dissector __read_mostly; |
| 575 | |
| 576 | static int __init init_default_flow_dissectors(void) |
| 577 | { |
| 578 | skb_flow_dissector_init(&flow_keys_dissector, |
| 579 | flow_keys_dissector_keys, |
| 580 | ARRAY_SIZE(flow_keys_dissector_keys)); |
| 581 | skb_flow_dissector_init(&flow_keys_buf_dissector, |
| 582 | flow_keys_buf_dissector_keys, |
| 583 | ARRAY_SIZE(flow_keys_buf_dissector_keys)); |
| 584 | return 0; |
| 585 | } |
| 586 | |
| 587 | late_initcall_sync(init_default_flow_dissectors); |