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> |
Tom Herbert | b3baa0f | 2015-06-04 09:16:46 -0700 | [diff] [blame] | 18 | #include <linux/mpls.h> |
Jiri Pirko | 1bd758e | 2015-05-12 14:56:07 +0200 | [diff] [blame] | 19 | #include <net/flow_dissector.h> |
Alexander Duyck | 56193d1 | 2014-09-05 19:20:26 -0400 | [diff] [blame] | 20 | #include <scsi/fc/fc_fcoe.h> |
Eric Dumazet | 0744dd0 | 2011-11-28 05:22:18 +0000 | [diff] [blame] | 21 | |
Jiri Pirko | fbff949 | 2015-05-12 14:56:15 +0200 | [diff] [blame] | 22 | static bool skb_flow_dissector_uses_key(struct flow_dissector *flow_dissector, |
| 23 | enum flow_dissector_key_id key_id) |
| 24 | { |
| 25 | return flow_dissector->used_keys & (1 << key_id); |
| 26 | } |
| 27 | |
| 28 | static void skb_flow_dissector_set_key(struct flow_dissector *flow_dissector, |
| 29 | enum flow_dissector_key_id key_id) |
| 30 | { |
| 31 | flow_dissector->used_keys |= (1 << key_id); |
| 32 | } |
| 33 | |
| 34 | static void *skb_flow_dissector_target(struct flow_dissector *flow_dissector, |
| 35 | enum flow_dissector_key_id key_id, |
| 36 | void *target_container) |
| 37 | { |
| 38 | return ((char *) target_container) + flow_dissector->offset[key_id]; |
| 39 | } |
| 40 | |
| 41 | void skb_flow_dissector_init(struct flow_dissector *flow_dissector, |
| 42 | const struct flow_dissector_key *key, |
| 43 | unsigned int key_count) |
| 44 | { |
| 45 | unsigned int i; |
| 46 | |
| 47 | memset(flow_dissector, 0, sizeof(*flow_dissector)); |
| 48 | |
| 49 | for (i = 0; i < key_count; i++, key++) { |
| 50 | /* User should make sure that every key target offset is withing |
| 51 | * boundaries of unsigned short. |
| 52 | */ |
| 53 | BUG_ON(key->offset > USHRT_MAX); |
| 54 | BUG_ON(skb_flow_dissector_uses_key(flow_dissector, |
| 55 | key->key_id)); |
| 56 | |
| 57 | skb_flow_dissector_set_key(flow_dissector, key->key_id); |
| 58 | flow_dissector->offset[key->key_id] = key->offset; |
| 59 | } |
| 60 | |
Tom Herbert | 42aecaa | 2015-06-04 09:16:39 -0700 | [diff] [blame] | 61 | /* Ensure that the dissector always includes control and basic key. |
| 62 | * 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] | 63 | */ |
| 64 | BUG_ON(!skb_flow_dissector_uses_key(flow_dissector, |
Tom Herbert | 42aecaa | 2015-06-04 09:16:39 -0700 | [diff] [blame] | 65 | FLOW_DISSECTOR_KEY_CONTROL)); |
| 66 | BUG_ON(!skb_flow_dissector_uses_key(flow_dissector, |
Jiri Pirko | fbff949 | 2015-05-12 14:56:15 +0200 | [diff] [blame] | 67 | FLOW_DISSECTOR_KEY_BASIC)); |
| 68 | } |
| 69 | EXPORT_SYMBOL(skb_flow_dissector_init); |
| 70 | |
Nikolay Aleksandrov | 357afe9 | 2013-10-02 13:39:24 +0200 | [diff] [blame] | 71 | /** |
WANG Cong | 6451b3f | 2014-08-25 17:03:46 -0700 | [diff] [blame] | 72 | * __skb_flow_get_ports - extract the upper layer ports and return them |
| 73 | * @skb: sk_buff to extract the ports from |
Nikolay Aleksandrov | 357afe9 | 2013-10-02 13:39:24 +0200 | [diff] [blame] | 74 | * @thoff: transport header offset |
| 75 | * @ip_proto: protocol for which to get port offset |
WANG Cong | 6451b3f | 2014-08-25 17:03:46 -0700 | [diff] [blame] | 76 | * @data: raw buffer pointer to the packet, if NULL use skb->data |
| 77 | * @hlen: packet header length, if @data is NULL use skb_headlen(skb) |
Nikolay Aleksandrov | 357afe9 | 2013-10-02 13:39:24 +0200 | [diff] [blame] | 78 | * |
| 79 | * The function will try to retrieve the ports at offset thoff + poff where poff |
| 80 | * is the protocol port offset returned from proto_ports_offset |
| 81 | */ |
David S. Miller | 690e36e | 2014-08-23 12:13:41 -0700 | [diff] [blame] | 82 | __be32 __skb_flow_get_ports(const struct sk_buff *skb, int thoff, u8 ip_proto, |
| 83 | void *data, int hlen) |
Nikolay Aleksandrov | 357afe9 | 2013-10-02 13:39:24 +0200 | [diff] [blame] | 84 | { |
| 85 | int poff = proto_ports_offset(ip_proto); |
| 86 | |
David S. Miller | 690e36e | 2014-08-23 12:13:41 -0700 | [diff] [blame] | 87 | if (!data) { |
| 88 | data = skb->data; |
| 89 | hlen = skb_headlen(skb); |
| 90 | } |
| 91 | |
Nikolay Aleksandrov | 357afe9 | 2013-10-02 13:39:24 +0200 | [diff] [blame] | 92 | if (poff >= 0) { |
| 93 | __be32 *ports, _ports; |
| 94 | |
David S. Miller | 690e36e | 2014-08-23 12:13:41 -0700 | [diff] [blame] | 95 | ports = __skb_header_pointer(skb, thoff + poff, |
| 96 | sizeof(_ports), data, hlen, &_ports); |
Nikolay Aleksandrov | 357afe9 | 2013-10-02 13:39:24 +0200 | [diff] [blame] | 97 | if (ports) |
| 98 | return *ports; |
| 99 | } |
| 100 | |
| 101 | return 0; |
| 102 | } |
David S. Miller | 690e36e | 2014-08-23 12:13:41 -0700 | [diff] [blame] | 103 | EXPORT_SYMBOL(__skb_flow_get_ports); |
Nikolay Aleksandrov | 357afe9 | 2013-10-02 13:39:24 +0200 | [diff] [blame] | 104 | |
WANG Cong | 453a940 | 2014-08-25 17:03:47 -0700 | [diff] [blame] | 105 | /** |
| 106 | * __skb_flow_dissect - extract the flow_keys struct and return it |
| 107 | * @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] | 108 | * @flow_dissector: list of keys to dissect |
| 109 | * @target_container: target structure to put dissected values into |
WANG Cong | 453a940 | 2014-08-25 17:03:47 -0700 | [diff] [blame] | 110 | * @data: raw buffer pointer to the packet, if NULL use skb->data |
| 111 | * @proto: protocol for which to get the flow, if @data is NULL use skb->protocol |
| 112 | * @nhoff: network header offset, if @data is NULL use skb_network_offset(skb) |
| 113 | * @hlen: packet header length, if @data is NULL use skb_headlen(skb) |
| 114 | * |
Jiri Pirko | 06635a3 | 2015-05-12 14:56:16 +0200 | [diff] [blame] | 115 | * The function will try to retrieve individual keys into target specified |
| 116 | * by flow_dissector from either the skbuff or a raw buffer specified by the |
| 117 | * rest parameters. |
| 118 | * |
| 119 | * Caller must take care of zeroing target container memory. |
WANG Cong | 453a940 | 2014-08-25 17:03:47 -0700 | [diff] [blame] | 120 | */ |
Jiri Pirko | 06635a3 | 2015-05-12 14:56:16 +0200 | [diff] [blame] | 121 | bool __skb_flow_dissect(const struct sk_buff *skb, |
| 122 | struct flow_dissector *flow_dissector, |
| 123 | void *target_container, |
WANG Cong | 453a940 | 2014-08-25 17:03:47 -0700 | [diff] [blame] | 124 | void *data, __be16 proto, int nhoff, int hlen) |
Eric Dumazet | 0744dd0 | 2011-11-28 05:22:18 +0000 | [diff] [blame] | 125 | { |
Tom Herbert | 42aecaa | 2015-06-04 09:16:39 -0700 | [diff] [blame] | 126 | struct flow_dissector_key_control *key_control; |
Jiri Pirko | 06635a3 | 2015-05-12 14:56:16 +0200 | [diff] [blame] | 127 | struct flow_dissector_key_basic *key_basic; |
| 128 | struct flow_dissector_key_addrs *key_addrs; |
| 129 | struct flow_dissector_key_ports *key_ports; |
Tom Herbert | d34af82 | 2015-06-04 09:16:43 -0700 | [diff] [blame] | 130 | struct flow_dissector_key_tags *key_tags; |
Tom Herbert | 1fdd512 | 2015-06-04 09:16:45 -0700 | [diff] [blame] | 131 | struct flow_dissector_key_keyid *key_keyid; |
Geert Uytterhoeven | 8e690ff | 2015-06-25 15:10:32 +0200 | [diff] [blame] | 132 | u8 ip_proto = 0; |
Eric Dumazet | 0744dd0 | 2011-11-28 05:22:18 +0000 | [diff] [blame] | 133 | |
David S. Miller | 690e36e | 2014-08-23 12:13:41 -0700 | [diff] [blame] | 134 | if (!data) { |
| 135 | data = skb->data; |
WANG Cong | 453a940 | 2014-08-25 17:03:47 -0700 | [diff] [blame] | 136 | proto = skb->protocol; |
| 137 | nhoff = skb_network_offset(skb); |
David S. Miller | 690e36e | 2014-08-23 12:13:41 -0700 | [diff] [blame] | 138 | hlen = skb_headlen(skb); |
| 139 | } |
| 140 | |
Tom Herbert | 42aecaa | 2015-06-04 09:16:39 -0700 | [diff] [blame] | 141 | /* It is ensured by skb_flow_dissector_init() that control key will |
| 142 | * be always present. |
| 143 | */ |
| 144 | key_control = skb_flow_dissector_target(flow_dissector, |
| 145 | FLOW_DISSECTOR_KEY_CONTROL, |
| 146 | target_container); |
| 147 | |
Jiri Pirko | 06635a3 | 2015-05-12 14:56:16 +0200 | [diff] [blame] | 148 | /* It is ensured by skb_flow_dissector_init() that basic key will |
| 149 | * be always present. |
| 150 | */ |
| 151 | key_basic = skb_flow_dissector_target(flow_dissector, |
| 152 | FLOW_DISSECTOR_KEY_BASIC, |
| 153 | target_container); |
Eric Dumazet | 0744dd0 | 2011-11-28 05:22:18 +0000 | [diff] [blame] | 154 | |
Jiri Pirko | 67a900c | 2015-05-12 14:56:19 +0200 | [diff] [blame] | 155 | if (skb_flow_dissector_uses_key(flow_dissector, |
| 156 | FLOW_DISSECTOR_KEY_ETH_ADDRS)) { |
| 157 | struct ethhdr *eth = eth_hdr(skb); |
| 158 | struct flow_dissector_key_eth_addrs *key_eth_addrs; |
| 159 | |
| 160 | key_eth_addrs = skb_flow_dissector_target(flow_dissector, |
| 161 | FLOW_DISSECTOR_KEY_ETH_ADDRS, |
| 162 | target_container); |
| 163 | memcpy(key_eth_addrs, ð->h_dest, sizeof(*key_eth_addrs)); |
| 164 | } |
| 165 | |
Eric Dumazet | 0744dd0 | 2011-11-28 05:22:18 +0000 | [diff] [blame] | 166 | again: |
| 167 | switch (proto) { |
Joe Perches | 2b8837a | 2014-03-12 10:04:17 -0700 | [diff] [blame] | 168 | case htons(ETH_P_IP): { |
Eric Dumazet | 0744dd0 | 2011-11-28 05:22:18 +0000 | [diff] [blame] | 169 | const struct iphdr *iph; |
| 170 | struct iphdr _iph; |
| 171 | ip: |
David S. Miller | 690e36e | 2014-08-23 12:13:41 -0700 | [diff] [blame] | 172 | iph = __skb_header_pointer(skb, nhoff, sizeof(_iph), data, hlen, &_iph); |
Jason Wang | 6f09234 | 2013-11-01 15:01:10 +0800 | [diff] [blame] | 173 | if (!iph || iph->ihl < 5) |
Eric Dumazet | 0744dd0 | 2011-11-28 05:22:18 +0000 | [diff] [blame] | 174 | return false; |
Eric Dumazet | 3797d3e | 2013-11-07 08:37:28 -0800 | [diff] [blame] | 175 | nhoff += iph->ihl * 4; |
Eric Dumazet | 0744dd0 | 2011-11-28 05:22:18 +0000 | [diff] [blame] | 176 | |
Eric Dumazet | 3797d3e | 2013-11-07 08:37:28 -0800 | [diff] [blame] | 177 | ip_proto = iph->protocol; |
Eric Dumazet | 0744dd0 | 2011-11-28 05:22:18 +0000 | [diff] [blame] | 178 | if (ip_is_fragment(iph)) |
| 179 | ip_proto = 0; |
Eric Dumazet | 3797d3e | 2013-11-07 08:37:28 -0800 | [diff] [blame] | 180 | |
Jiri Pirko | 06635a3 | 2015-05-12 14:56:16 +0200 | [diff] [blame] | 181 | if (!skb_flow_dissector_uses_key(flow_dissector, |
| 182 | FLOW_DISSECTOR_KEY_IPV4_ADDRS)) |
Alexander Duyck | 5af7fb6 | 2014-10-10 12:09:12 -0700 | [diff] [blame] | 183 | break; |
Tom Herbert | c3f8324 | 2015-06-04 09:16:40 -0700 | [diff] [blame] | 184 | |
Jiri Pirko | 06635a3 | 2015-05-12 14:56:16 +0200 | [diff] [blame] | 185 | key_addrs = skb_flow_dissector_target(flow_dissector, |
Tom Herbert | c3f8324 | 2015-06-04 09:16:40 -0700 | [diff] [blame] | 186 | FLOW_DISSECTOR_KEY_IPV4_ADDRS, target_container); |
| 187 | memcpy(&key_addrs->v4addrs, &iph->saddr, |
| 188 | sizeof(key_addrs->v4addrs)); |
| 189 | key_control->addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS; |
Eric Dumazet | 0744dd0 | 2011-11-28 05:22:18 +0000 | [diff] [blame] | 190 | break; |
| 191 | } |
Joe Perches | 2b8837a | 2014-03-12 10:04:17 -0700 | [diff] [blame] | 192 | case htons(ETH_P_IPV6): { |
Eric Dumazet | 0744dd0 | 2011-11-28 05:22:18 +0000 | [diff] [blame] | 193 | const struct ipv6hdr *iph; |
| 194 | struct ipv6hdr _iph; |
Tom Herbert | 19469a8 | 2014-07-01 21:33:01 -0700 | [diff] [blame] | 195 | __be32 flow_label; |
| 196 | |
Eric Dumazet | 0744dd0 | 2011-11-28 05:22:18 +0000 | [diff] [blame] | 197 | ipv6: |
David S. Miller | 690e36e | 2014-08-23 12:13:41 -0700 | [diff] [blame] | 198 | iph = __skb_header_pointer(skb, nhoff, sizeof(_iph), data, hlen, &_iph); |
Eric Dumazet | 0744dd0 | 2011-11-28 05:22:18 +0000 | [diff] [blame] | 199 | if (!iph) |
| 200 | return false; |
| 201 | |
| 202 | ip_proto = iph->nexthdr; |
Eric Dumazet | 0744dd0 | 2011-11-28 05:22:18 +0000 | [diff] [blame] | 203 | nhoff += sizeof(struct ipv6hdr); |
Tom Herbert | 19469a8 | 2014-07-01 21:33:01 -0700 | [diff] [blame] | 204 | |
Jiri Pirko | b924933 | 2015-05-12 14:56:18 +0200 | [diff] [blame] | 205 | if (skb_flow_dissector_uses_key(flow_dissector, |
Jiri Pirko | b924933 | 2015-05-12 14:56:18 +0200 | [diff] [blame] | 206 | FLOW_DISSECTOR_KEY_IPV6_ADDRS)) { |
| 207 | struct flow_dissector_key_ipv6_addrs *key_ipv6_addrs; |
Alexander Duyck | 5af7fb6 | 2014-10-10 12:09:12 -0700 | [diff] [blame] | 208 | |
Jiri Pirko | b924933 | 2015-05-12 14:56:18 +0200 | [diff] [blame] | 209 | key_ipv6_addrs = skb_flow_dissector_target(flow_dissector, |
| 210 | FLOW_DISSECTOR_KEY_IPV6_ADDRS, |
| 211 | target_container); |
| 212 | |
| 213 | memcpy(key_ipv6_addrs, &iph->saddr, sizeof(*key_ipv6_addrs)); |
Tom Herbert | c3f8324 | 2015-06-04 09:16:40 -0700 | [diff] [blame] | 214 | key_control->addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS; |
Jiri Pirko | b924933 | 2015-05-12 14:56:18 +0200 | [diff] [blame] | 215 | } |
Tom Herbert | 87ee9e5 | 2015-06-04 09:16:44 -0700 | [diff] [blame] | 216 | |
Tom Herbert | 19469a8 | 2014-07-01 21:33:01 -0700 | [diff] [blame] | 217 | flow_label = ip6_flowlabel(iph); |
| 218 | if (flow_label) { |
Jiri Pirko | 12c227e | 2015-05-22 11:05:58 +0200 | [diff] [blame] | 219 | if (skb_flow_dissector_uses_key(flow_dissector, |
Tom Herbert | 87ee9e5 | 2015-06-04 09:16:44 -0700 | [diff] [blame] | 220 | FLOW_DISSECTOR_KEY_FLOW_LABEL)) { |
| 221 | key_tags = skb_flow_dissector_target(flow_dissector, |
| 222 | FLOW_DISSECTOR_KEY_FLOW_LABEL, |
| 223 | target_container); |
| 224 | key_tags->flow_label = ntohl(flow_label); |
Jiri Pirko | 12c227e | 2015-05-22 11:05:58 +0200 | [diff] [blame] | 225 | } |
Tom Herbert | 19469a8 | 2014-07-01 21:33:01 -0700 | [diff] [blame] | 226 | } |
| 227 | |
Eric Dumazet | 0744dd0 | 2011-11-28 05:22:18 +0000 | [diff] [blame] | 228 | break; |
| 229 | } |
Joe Perches | 2b8837a | 2014-03-12 10:04:17 -0700 | [diff] [blame] | 230 | case htons(ETH_P_8021AD): |
| 231 | case htons(ETH_P_8021Q): { |
Eric Dumazet | 0744dd0 | 2011-11-28 05:22:18 +0000 | [diff] [blame] | 232 | const struct vlan_hdr *vlan; |
| 233 | struct vlan_hdr _vlan; |
| 234 | |
David S. Miller | 690e36e | 2014-08-23 12:13:41 -0700 | [diff] [blame] | 235 | vlan = __skb_header_pointer(skb, nhoff, sizeof(_vlan), data, hlen, &_vlan); |
Eric Dumazet | 0744dd0 | 2011-11-28 05:22:18 +0000 | [diff] [blame] | 236 | if (!vlan) |
| 237 | return false; |
| 238 | |
Tom Herbert | d34af82 | 2015-06-04 09:16:43 -0700 | [diff] [blame] | 239 | if (skb_flow_dissector_uses_key(flow_dissector, |
| 240 | FLOW_DISSECTOR_KEY_VLANID)) { |
| 241 | key_tags = skb_flow_dissector_target(flow_dissector, |
| 242 | FLOW_DISSECTOR_KEY_VLANID, |
| 243 | target_container); |
| 244 | |
| 245 | key_tags->vlan_id = skb_vlan_tag_get_id(skb); |
| 246 | } |
| 247 | |
Eric Dumazet | 0744dd0 | 2011-11-28 05:22:18 +0000 | [diff] [blame] | 248 | proto = vlan->h_vlan_encapsulated_proto; |
| 249 | nhoff += sizeof(*vlan); |
| 250 | goto again; |
| 251 | } |
Joe Perches | 2b8837a | 2014-03-12 10:04:17 -0700 | [diff] [blame] | 252 | case htons(ETH_P_PPP_SES): { |
Eric Dumazet | 0744dd0 | 2011-11-28 05:22:18 +0000 | [diff] [blame] | 253 | struct { |
| 254 | struct pppoe_hdr hdr; |
| 255 | __be16 proto; |
| 256 | } *hdr, _hdr; |
David S. Miller | 690e36e | 2014-08-23 12:13:41 -0700 | [diff] [blame] | 257 | hdr = __skb_header_pointer(skb, nhoff, sizeof(_hdr), data, hlen, &_hdr); |
Eric Dumazet | 0744dd0 | 2011-11-28 05:22:18 +0000 | [diff] [blame] | 258 | if (!hdr) |
| 259 | return false; |
| 260 | proto = hdr->proto; |
| 261 | nhoff += PPPOE_SES_HLEN; |
| 262 | switch (proto) { |
Joe Perches | 2b8837a | 2014-03-12 10:04:17 -0700 | [diff] [blame] | 263 | case htons(PPP_IP): |
Eric Dumazet | 0744dd0 | 2011-11-28 05:22:18 +0000 | [diff] [blame] | 264 | goto ip; |
Joe Perches | 2b8837a | 2014-03-12 10:04:17 -0700 | [diff] [blame] | 265 | case htons(PPP_IPV6): |
Eric Dumazet | 0744dd0 | 2011-11-28 05:22:18 +0000 | [diff] [blame] | 266 | goto ipv6; |
| 267 | default: |
| 268 | return false; |
| 269 | } |
| 270 | } |
Erik Hugne | 08bfc9c | 2015-01-22 17:10:32 +0100 | [diff] [blame] | 271 | case htons(ETH_P_TIPC): { |
| 272 | struct { |
| 273 | __be32 pre[3]; |
| 274 | __be32 srcnode; |
| 275 | } *hdr, _hdr; |
| 276 | hdr = __skb_header_pointer(skb, nhoff, sizeof(_hdr), data, hlen, &_hdr); |
| 277 | if (!hdr) |
| 278 | return false; |
Jiri Pirko | 06635a3 | 2015-05-12 14:56:16 +0200 | [diff] [blame] | 279 | key_basic->n_proto = proto; |
Tom Herbert | 42aecaa | 2015-06-04 09:16:39 -0700 | [diff] [blame] | 280 | key_control->thoff = (u16)nhoff; |
Jiri Pirko | 06635a3 | 2015-05-12 14:56:16 +0200 | [diff] [blame] | 281 | |
| 282 | if (skb_flow_dissector_uses_key(flow_dissector, |
Tom Herbert | 9f24908 | 2015-06-04 09:16:41 -0700 | [diff] [blame] | 283 | FLOW_DISSECTOR_KEY_TIPC_ADDRS)) { |
Jiri Pirko | 06635a3 | 2015-05-12 14:56:16 +0200 | [diff] [blame] | 284 | key_addrs = skb_flow_dissector_target(flow_dissector, |
Tom Herbert | 9f24908 | 2015-06-04 09:16:41 -0700 | [diff] [blame] | 285 | FLOW_DISSECTOR_KEY_TIPC_ADDRS, |
Jiri Pirko | 06635a3 | 2015-05-12 14:56:16 +0200 | [diff] [blame] | 286 | target_container); |
Tom Herbert | 9f24908 | 2015-06-04 09:16:41 -0700 | [diff] [blame] | 287 | key_addrs->tipcaddrs.srcnode = hdr->srcnode; |
| 288 | key_control->addr_type = FLOW_DISSECTOR_KEY_TIPC_ADDRS; |
Jiri Pirko | 06635a3 | 2015-05-12 14:56:16 +0200 | [diff] [blame] | 289 | } |
Erik Hugne | 08bfc9c | 2015-01-22 17:10:32 +0100 | [diff] [blame] | 290 | return true; |
| 291 | } |
Tom Herbert | b3baa0f | 2015-06-04 09:16:46 -0700 | [diff] [blame] | 292 | |
| 293 | case htons(ETH_P_MPLS_UC): |
| 294 | case htons(ETH_P_MPLS_MC): { |
| 295 | struct mpls_label *hdr, _hdr[2]; |
| 296 | mpls: |
| 297 | hdr = __skb_header_pointer(skb, nhoff, sizeof(_hdr), data, |
| 298 | hlen, &_hdr); |
| 299 | if (!hdr) |
| 300 | return false; |
| 301 | |
Tom Herbert | 611d23c | 2015-06-12 09:01:05 -0700 | [diff] [blame] | 302 | if ((ntohl(hdr[0].entry) & MPLS_LS_LABEL_MASK) >> |
| 303 | MPLS_LS_LABEL_SHIFT == MPLS_LABEL_ENTROPY) { |
Tom Herbert | b3baa0f | 2015-06-04 09:16:46 -0700 | [diff] [blame] | 304 | if (skb_flow_dissector_uses_key(flow_dissector, |
| 305 | FLOW_DISSECTOR_KEY_MPLS_ENTROPY)) { |
| 306 | key_keyid = skb_flow_dissector_target(flow_dissector, |
| 307 | FLOW_DISSECTOR_KEY_MPLS_ENTROPY, |
| 308 | target_container); |
| 309 | key_keyid->keyid = hdr[1].entry & |
| 310 | htonl(MPLS_LS_LABEL_MASK); |
| 311 | } |
| 312 | |
| 313 | key_basic->n_proto = proto; |
| 314 | key_basic->ip_proto = ip_proto; |
| 315 | key_control->thoff = (u16)nhoff; |
| 316 | |
| 317 | return true; |
| 318 | } |
| 319 | |
| 320 | return true; |
| 321 | } |
| 322 | |
Alexander Duyck | 56193d1 | 2014-09-05 19:20:26 -0400 | [diff] [blame] | 323 | case htons(ETH_P_FCOE): |
Tom Herbert | 42aecaa | 2015-06-04 09:16:39 -0700 | [diff] [blame] | 324 | key_control->thoff = (u16)(nhoff + FCOE_HEADER_LEN); |
Alexander Duyck | 56193d1 | 2014-09-05 19:20:26 -0400 | [diff] [blame] | 325 | /* fall through */ |
Eric Dumazet | 0744dd0 | 2011-11-28 05:22:18 +0000 | [diff] [blame] | 326 | default: |
| 327 | return false; |
| 328 | } |
| 329 | |
Tom Herbert | 6a74fcf | 2015-06-12 09:01:06 -0700 | [diff] [blame] | 330 | ip_proto_again: |
Eric Dumazet | 0744dd0 | 2011-11-28 05:22:18 +0000 | [diff] [blame] | 331 | switch (ip_proto) { |
| 332 | case IPPROTO_GRE: { |
| 333 | struct gre_hdr { |
| 334 | __be16 flags; |
| 335 | __be16 proto; |
| 336 | } *hdr, _hdr; |
| 337 | |
David S. Miller | 690e36e | 2014-08-23 12:13:41 -0700 | [diff] [blame] | 338 | hdr = __skb_header_pointer(skb, nhoff, sizeof(_hdr), data, hlen, &_hdr); |
Eric Dumazet | 0744dd0 | 2011-11-28 05:22:18 +0000 | [diff] [blame] | 339 | if (!hdr) |
| 340 | return false; |
| 341 | /* |
| 342 | * Only look inside GRE if version zero and no |
| 343 | * routing |
| 344 | */ |
Tom Herbert | ce3b535 | 2015-06-04 09:16:36 -0700 | [diff] [blame] | 345 | if (hdr->flags & (GRE_VERSION | GRE_ROUTING)) |
| 346 | break; |
Michael Dalton | e1733de | 2013-03-11 06:52:28 +0000 | [diff] [blame] | 347 | |
Tom Herbert | ce3b535 | 2015-06-04 09:16:36 -0700 | [diff] [blame] | 348 | proto = hdr->proto; |
| 349 | nhoff += 4; |
| 350 | if (hdr->flags & GRE_CSUM) |
| 351 | nhoff += 4; |
Tom Herbert | 1fdd512 | 2015-06-04 09:16:45 -0700 | [diff] [blame] | 352 | if (hdr->flags & GRE_KEY) { |
| 353 | const __be32 *keyid; |
| 354 | __be32 _keyid; |
| 355 | |
| 356 | keyid = __skb_header_pointer(skb, nhoff, sizeof(_keyid), |
| 357 | data, hlen, &_keyid); |
| 358 | |
| 359 | if (!keyid) |
| 360 | return false; |
| 361 | |
| 362 | if (skb_flow_dissector_uses_key(flow_dissector, |
| 363 | FLOW_DISSECTOR_KEY_GRE_KEYID)) { |
| 364 | key_keyid = skb_flow_dissector_target(flow_dissector, |
| 365 | FLOW_DISSECTOR_KEY_GRE_KEYID, |
| 366 | target_container); |
| 367 | key_keyid->keyid = *keyid; |
| 368 | } |
Tom Herbert | ce3b535 | 2015-06-04 09:16:36 -0700 | [diff] [blame] | 369 | nhoff += 4; |
Tom Herbert | 1fdd512 | 2015-06-04 09:16:45 -0700 | [diff] [blame] | 370 | } |
Tom Herbert | ce3b535 | 2015-06-04 09:16:36 -0700 | [diff] [blame] | 371 | if (hdr->flags & GRE_SEQ) |
| 372 | nhoff += 4; |
| 373 | if (proto == htons(ETH_P_TEB)) { |
| 374 | const struct ethhdr *eth; |
| 375 | struct ethhdr _eth; |
| 376 | |
| 377 | eth = __skb_header_pointer(skb, nhoff, |
| 378 | sizeof(_eth), |
| 379 | data, hlen, &_eth); |
| 380 | if (!eth) |
| 381 | return false; |
| 382 | proto = eth->h_proto; |
| 383 | nhoff += sizeof(*eth); |
Eric Dumazet | 0744dd0 | 2011-11-28 05:22:18 +0000 | [diff] [blame] | 384 | } |
Tom Herbert | ce3b535 | 2015-06-04 09:16:36 -0700 | [diff] [blame] | 385 | goto again; |
Eric Dumazet | 0744dd0 | 2011-11-28 05:22:18 +0000 | [diff] [blame] | 386 | } |
Tom Herbert | 6a74fcf | 2015-06-12 09:01:06 -0700 | [diff] [blame] | 387 | case NEXTHDR_HOP: |
| 388 | case NEXTHDR_ROUTING: |
| 389 | case NEXTHDR_DEST: { |
| 390 | u8 _opthdr[2], *opthdr; |
| 391 | |
| 392 | if (proto != htons(ETH_P_IPV6)) |
| 393 | break; |
| 394 | |
| 395 | opthdr = __skb_header_pointer(skb, nhoff, sizeof(_opthdr), |
| 396 | data, hlen, &_opthdr); |
Eric Dumazet | 1e98a0f | 2015-06-12 19:31:32 -0700 | [diff] [blame] | 397 | if (!opthdr) |
| 398 | return false; |
Tom Herbert | 6a74fcf | 2015-06-12 09:01:06 -0700 | [diff] [blame] | 399 | |
Eric Dumazet | 1e98a0f | 2015-06-12 19:31:32 -0700 | [diff] [blame] | 400 | ip_proto = opthdr[0]; |
| 401 | nhoff += (opthdr[1] + 1) << 3; |
Tom Herbert | 6a74fcf | 2015-06-12 09:01:06 -0700 | [diff] [blame] | 402 | |
| 403 | goto ip_proto_again; |
| 404 | } |
Eric Dumazet | 0744dd0 | 2011-11-28 05:22:18 +0000 | [diff] [blame] | 405 | case IPPROTO_IPIP: |
Tom Herbert | fca4189 | 2013-07-29 11:07:36 -0700 | [diff] [blame] | 406 | proto = htons(ETH_P_IP); |
| 407 | goto ip; |
Tom Herbert | b438f94 | 2013-07-29 11:07:42 -0700 | [diff] [blame] | 408 | case IPPROTO_IPV6: |
| 409 | proto = htons(ETH_P_IPV6); |
| 410 | goto ipv6; |
Tom Herbert | b3baa0f | 2015-06-04 09:16:46 -0700 | [diff] [blame] | 411 | case IPPROTO_MPLS: |
| 412 | proto = htons(ETH_P_MPLS_UC); |
| 413 | goto mpls; |
Eric Dumazet | 0744dd0 | 2011-11-28 05:22:18 +0000 | [diff] [blame] | 414 | default: |
| 415 | break; |
| 416 | } |
| 417 | |
Jiri Pirko | 06635a3 | 2015-05-12 14:56:16 +0200 | [diff] [blame] | 418 | key_basic->n_proto = proto; |
| 419 | key_basic->ip_proto = ip_proto; |
Tom Herbert | 42aecaa | 2015-06-04 09:16:39 -0700 | [diff] [blame] | 420 | key_control->thoff = (u16)nhoff; |
Daniel Borkmann | 8ed7816 | 2013-03-19 06:39:29 +0000 | [diff] [blame] | 421 | |
Jiri Pirko | 06635a3 | 2015-05-12 14:56:16 +0200 | [diff] [blame] | 422 | if (skb_flow_dissector_uses_key(flow_dissector, |
| 423 | FLOW_DISSECTOR_KEY_PORTS)) { |
| 424 | key_ports = skb_flow_dissector_target(flow_dissector, |
| 425 | FLOW_DISSECTOR_KEY_PORTS, |
| 426 | target_container); |
| 427 | key_ports->ports = __skb_flow_get_ports(skb, nhoff, ip_proto, |
| 428 | data, hlen); |
| 429 | } |
Alexander Duyck | 5af7fb6 | 2014-10-10 12:09:12 -0700 | [diff] [blame] | 430 | |
Eric Dumazet | 0744dd0 | 2011-11-28 05:22:18 +0000 | [diff] [blame] | 431 | return true; |
| 432 | } |
David S. Miller | 690e36e | 2014-08-23 12:13:41 -0700 | [diff] [blame] | 433 | EXPORT_SYMBOL(__skb_flow_dissect); |
Cong Wang | 441d9d3 | 2013-01-21 00:39:24 +0000 | [diff] [blame] | 434 | |
| 435 | static u32 hashrnd __read_mostly; |
Hannes Frederic Sowa | 66415cf | 2013-10-23 20:06:00 +0200 | [diff] [blame] | 436 | static __always_inline void __flow_hash_secret_init(void) |
| 437 | { |
| 438 | net_get_random_once(&hashrnd, sizeof(hashrnd)); |
| 439 | } |
| 440 | |
Tom Herbert | 42aecaa | 2015-06-04 09:16:39 -0700 | [diff] [blame] | 441 | static __always_inline u32 __flow_hash_words(u32 *words, u32 length, u32 keyval) |
Hannes Frederic Sowa | 66415cf | 2013-10-23 20:06:00 +0200 | [diff] [blame] | 442 | { |
Tom Herbert | 42aecaa | 2015-06-04 09:16:39 -0700 | [diff] [blame] | 443 | return jhash2(words, length, keyval); |
| 444 | } |
| 445 | |
| 446 | static inline void *flow_keys_hash_start(struct flow_keys *flow) |
| 447 | { |
| 448 | BUILD_BUG_ON(FLOW_KEYS_HASH_OFFSET % sizeof(u32)); |
| 449 | return (void *)flow + FLOW_KEYS_HASH_OFFSET; |
| 450 | } |
| 451 | |
| 452 | static inline size_t flow_keys_hash_length(struct flow_keys *flow) |
| 453 | { |
Tom Herbert | c3f8324 | 2015-06-04 09:16:40 -0700 | [diff] [blame] | 454 | size_t diff = FLOW_KEYS_HASH_OFFSET + sizeof(flow->addrs); |
Tom Herbert | 42aecaa | 2015-06-04 09:16:39 -0700 | [diff] [blame] | 455 | BUILD_BUG_ON((sizeof(*flow) - FLOW_KEYS_HASH_OFFSET) % sizeof(u32)); |
Tom Herbert | c3f8324 | 2015-06-04 09:16:40 -0700 | [diff] [blame] | 456 | BUILD_BUG_ON(offsetof(typeof(*flow), addrs) != |
| 457 | sizeof(*flow) - sizeof(flow->addrs)); |
| 458 | |
| 459 | switch (flow->control.addr_type) { |
| 460 | case FLOW_DISSECTOR_KEY_IPV4_ADDRS: |
| 461 | diff -= sizeof(flow->addrs.v4addrs); |
| 462 | break; |
| 463 | case FLOW_DISSECTOR_KEY_IPV6_ADDRS: |
| 464 | diff -= sizeof(flow->addrs.v6addrs); |
| 465 | break; |
Tom Herbert | 9f24908 | 2015-06-04 09:16:41 -0700 | [diff] [blame] | 466 | case FLOW_DISSECTOR_KEY_TIPC_ADDRS: |
| 467 | diff -= sizeof(flow->addrs.tipcaddrs); |
| 468 | break; |
Tom Herbert | c3f8324 | 2015-06-04 09:16:40 -0700 | [diff] [blame] | 469 | } |
| 470 | return (sizeof(*flow) - diff) / sizeof(u32); |
| 471 | } |
| 472 | |
| 473 | __be32 flow_get_u32_src(const struct flow_keys *flow) |
| 474 | { |
| 475 | switch (flow->control.addr_type) { |
| 476 | case FLOW_DISSECTOR_KEY_IPV4_ADDRS: |
| 477 | return flow->addrs.v4addrs.src; |
| 478 | case FLOW_DISSECTOR_KEY_IPV6_ADDRS: |
| 479 | return (__force __be32)ipv6_addr_hash( |
| 480 | &flow->addrs.v6addrs.src); |
Tom Herbert | 9f24908 | 2015-06-04 09:16:41 -0700 | [diff] [blame] | 481 | case FLOW_DISSECTOR_KEY_TIPC_ADDRS: |
| 482 | return flow->addrs.tipcaddrs.srcnode; |
Tom Herbert | c3f8324 | 2015-06-04 09:16:40 -0700 | [diff] [blame] | 483 | default: |
| 484 | return 0; |
| 485 | } |
| 486 | } |
| 487 | EXPORT_SYMBOL(flow_get_u32_src); |
| 488 | |
| 489 | __be32 flow_get_u32_dst(const struct flow_keys *flow) |
| 490 | { |
| 491 | switch (flow->control.addr_type) { |
| 492 | case FLOW_DISSECTOR_KEY_IPV4_ADDRS: |
| 493 | return flow->addrs.v4addrs.dst; |
| 494 | case FLOW_DISSECTOR_KEY_IPV6_ADDRS: |
| 495 | return (__force __be32)ipv6_addr_hash( |
| 496 | &flow->addrs.v6addrs.dst); |
| 497 | default: |
| 498 | return 0; |
| 499 | } |
| 500 | } |
| 501 | EXPORT_SYMBOL(flow_get_u32_dst); |
| 502 | |
| 503 | static inline void __flow_hash_consistentify(struct flow_keys *keys) |
| 504 | { |
| 505 | int addr_diff, i; |
| 506 | |
| 507 | switch (keys->control.addr_type) { |
| 508 | case FLOW_DISSECTOR_KEY_IPV4_ADDRS: |
| 509 | addr_diff = (__force u32)keys->addrs.v4addrs.dst - |
| 510 | (__force u32)keys->addrs.v4addrs.src; |
| 511 | if ((addr_diff < 0) || |
| 512 | (addr_diff == 0 && |
| 513 | ((__force u16)keys->ports.dst < |
| 514 | (__force u16)keys->ports.src))) { |
| 515 | swap(keys->addrs.v4addrs.src, keys->addrs.v4addrs.dst); |
| 516 | swap(keys->ports.src, keys->ports.dst); |
| 517 | } |
| 518 | break; |
| 519 | case FLOW_DISSECTOR_KEY_IPV6_ADDRS: |
| 520 | addr_diff = memcmp(&keys->addrs.v6addrs.dst, |
| 521 | &keys->addrs.v6addrs.src, |
| 522 | sizeof(keys->addrs.v6addrs.dst)); |
| 523 | if ((addr_diff < 0) || |
| 524 | (addr_diff == 0 && |
| 525 | ((__force u16)keys->ports.dst < |
| 526 | (__force u16)keys->ports.src))) { |
| 527 | for (i = 0; i < 4; i++) |
| 528 | swap(keys->addrs.v6addrs.src.s6_addr32[i], |
| 529 | keys->addrs.v6addrs.dst.s6_addr32[i]); |
| 530 | swap(keys->ports.src, keys->ports.dst); |
| 531 | } |
| 532 | break; |
| 533 | } |
Hannes Frederic Sowa | 66415cf | 2013-10-23 20:06:00 +0200 | [diff] [blame] | 534 | } |
| 535 | |
Tom Herbert | 50fb799 | 2015-05-01 11:30:12 -0700 | [diff] [blame] | 536 | 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] | 537 | { |
| 538 | u32 hash; |
| 539 | |
Tom Herbert | c3f8324 | 2015-06-04 09:16:40 -0700 | [diff] [blame] | 540 | __flow_hash_consistentify(keys); |
Tom Herbert | 5ed20a6 | 2014-07-01 21:32:05 -0700 | [diff] [blame] | 541 | |
Tom Herbert | 42aecaa | 2015-06-04 09:16:39 -0700 | [diff] [blame] | 542 | hash = __flow_hash_words((u32 *)flow_keys_hash_start(keys), |
| 543 | flow_keys_hash_length(keys), keyval); |
Tom Herbert | 5ed20a6 | 2014-07-01 21:32:05 -0700 | [diff] [blame] | 544 | if (!hash) |
| 545 | hash = 1; |
| 546 | |
| 547 | return hash; |
| 548 | } |
| 549 | |
| 550 | u32 flow_hash_from_keys(struct flow_keys *keys) |
| 551 | { |
Tom Herbert | 50fb799 | 2015-05-01 11:30:12 -0700 | [diff] [blame] | 552 | __flow_hash_secret_init(); |
| 553 | return __flow_hash_from_keys(keys, hashrnd); |
Tom Herbert | 5ed20a6 | 2014-07-01 21:32:05 -0700 | [diff] [blame] | 554 | } |
| 555 | EXPORT_SYMBOL(flow_hash_from_keys); |
| 556 | |
Tom Herbert | 50fb799 | 2015-05-01 11:30:12 -0700 | [diff] [blame] | 557 | static inline u32 ___skb_get_hash(const struct sk_buff *skb, |
| 558 | struct flow_keys *keys, u32 keyval) |
| 559 | { |
Jiri Pirko | 06635a3 | 2015-05-12 14:56:16 +0200 | [diff] [blame] | 560 | if (!skb_flow_dissect_flow_keys(skb, keys)) |
Tom Herbert | 50fb799 | 2015-05-01 11:30:12 -0700 | [diff] [blame] | 561 | return 0; |
| 562 | |
| 563 | return __flow_hash_from_keys(keys, keyval); |
| 564 | } |
| 565 | |
Tom Herbert | 2f59e1e | 2015-05-01 11:30:17 -0700 | [diff] [blame] | 566 | struct _flow_keys_digest_data { |
| 567 | __be16 n_proto; |
| 568 | u8 ip_proto; |
| 569 | u8 padding; |
| 570 | __be32 ports; |
| 571 | __be32 src; |
| 572 | __be32 dst; |
| 573 | }; |
| 574 | |
| 575 | void make_flow_keys_digest(struct flow_keys_digest *digest, |
| 576 | const struct flow_keys *flow) |
| 577 | { |
| 578 | struct _flow_keys_digest_data *data = |
| 579 | (struct _flow_keys_digest_data *)digest; |
| 580 | |
| 581 | BUILD_BUG_ON(sizeof(*data) > sizeof(*digest)); |
| 582 | |
| 583 | memset(digest, 0, sizeof(*digest)); |
| 584 | |
Jiri Pirko | 06635a3 | 2015-05-12 14:56:16 +0200 | [diff] [blame] | 585 | data->n_proto = flow->basic.n_proto; |
| 586 | data->ip_proto = flow->basic.ip_proto; |
| 587 | data->ports = flow->ports.ports; |
Tom Herbert | c3f8324 | 2015-06-04 09:16:40 -0700 | [diff] [blame] | 588 | data->src = flow->addrs.v4addrs.src; |
| 589 | data->dst = flow->addrs.v4addrs.dst; |
Tom Herbert | 2f59e1e | 2015-05-01 11:30:17 -0700 | [diff] [blame] | 590 | } |
| 591 | EXPORT_SYMBOL(make_flow_keys_digest); |
| 592 | |
Jiri Pirko | d4fd327 | 2015-05-12 14:56:10 +0200 | [diff] [blame] | 593 | /** |
| 594 | * __skb_get_hash: calculate a flow hash |
| 595 | * @skb: sk_buff to calculate flow hash from |
| 596 | * |
| 597 | * This function calculates a flow hash based on src/dst addresses |
Tom Herbert | 61b905d | 2014-03-24 15:34:47 -0700 | [diff] [blame] | 598 | * and src/dst port numbers. Sets hash in skb to non-zero hash value |
| 599 | * 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] | 600 | * if hash is a canonical 4-tuple hash over transport ports. |
| 601 | */ |
Tom Herbert | 3958afa1b | 2013-12-15 22:12:06 -0800 | [diff] [blame] | 602 | void __skb_get_hash(struct sk_buff *skb) |
Cong Wang | 441d9d3 | 2013-01-21 00:39:24 +0000 | [diff] [blame] | 603 | { |
| 604 | struct flow_keys keys; |
Tom Herbert | 50fb799 | 2015-05-01 11:30:12 -0700 | [diff] [blame] | 605 | u32 hash; |
Cong Wang | 441d9d3 | 2013-01-21 00:39:24 +0000 | [diff] [blame] | 606 | |
Tom Herbert | 50fb799 | 2015-05-01 11:30:12 -0700 | [diff] [blame] | 607 | __flow_hash_secret_init(); |
| 608 | |
| 609 | hash = ___skb_get_hash(skb, &keys, hashrnd); |
| 610 | if (!hash) |
Cong Wang | 441d9d3 | 2013-01-21 00:39:24 +0000 | [diff] [blame] | 611 | return; |
Tom Herbert | f70ea01 | 2015-07-31 16:52:10 -0700 | [diff] [blame] | 612 | |
Tom Herbert | bcc8383 | 2015-09-01 09:24:24 -0700 | [diff] [blame] | 613 | __skb_set_sw_hash(skb, hash, |
| 614 | flow_keys_have_l4(&keys)); |
Cong Wang | 441d9d3 | 2013-01-21 00:39:24 +0000 | [diff] [blame] | 615 | } |
Tom Herbert | 3958afa1b | 2013-12-15 22:12:06 -0800 | [diff] [blame] | 616 | EXPORT_SYMBOL(__skb_get_hash); |
Cong Wang | 441d9d3 | 2013-01-21 00:39:24 +0000 | [diff] [blame] | 617 | |
Tom Herbert | 50fb799 | 2015-05-01 11:30:12 -0700 | [diff] [blame] | 618 | __u32 skb_get_hash_perturb(const struct sk_buff *skb, u32 perturb) |
| 619 | { |
| 620 | struct flow_keys keys; |
| 621 | |
| 622 | return ___skb_get_hash(skb, &keys, perturb); |
| 623 | } |
| 624 | EXPORT_SYMBOL(skb_get_hash_perturb); |
| 625 | |
Tom Herbert | f70ea01 | 2015-07-31 16:52:10 -0700 | [diff] [blame] | 626 | __u32 __skb_get_hash_flowi6(struct sk_buff *skb, struct flowi6 *fl6) |
| 627 | { |
| 628 | struct flow_keys keys; |
| 629 | |
| 630 | memset(&keys, 0, sizeof(keys)); |
| 631 | |
| 632 | memcpy(&keys.addrs.v6addrs.src, &fl6->saddr, |
| 633 | sizeof(keys.addrs.v6addrs.src)); |
| 634 | memcpy(&keys.addrs.v6addrs.dst, &fl6->daddr, |
| 635 | sizeof(keys.addrs.v6addrs.dst)); |
| 636 | keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS; |
| 637 | keys.ports.src = fl6->fl6_sport; |
| 638 | keys.ports.dst = fl6->fl6_dport; |
| 639 | keys.keyid.keyid = fl6->fl6_gre_key; |
| 640 | keys.tags.flow_label = (__force u32)fl6->flowlabel; |
| 641 | keys.basic.ip_proto = fl6->flowi6_proto; |
| 642 | |
Tom Herbert | bcc8383 | 2015-09-01 09:24:24 -0700 | [diff] [blame] | 643 | __skb_set_sw_hash(skb, flow_hash_from_keys(&keys), |
| 644 | flow_keys_have_l4(&keys)); |
Tom Herbert | f70ea01 | 2015-07-31 16:52:10 -0700 | [diff] [blame] | 645 | |
| 646 | return skb->hash; |
| 647 | } |
| 648 | EXPORT_SYMBOL(__skb_get_hash_flowi6); |
| 649 | |
| 650 | __u32 __skb_get_hash_flowi4(struct sk_buff *skb, struct flowi4 *fl4) |
| 651 | { |
| 652 | struct flow_keys keys; |
| 653 | |
| 654 | memset(&keys, 0, sizeof(keys)); |
| 655 | |
| 656 | keys.addrs.v4addrs.src = fl4->saddr; |
| 657 | keys.addrs.v4addrs.dst = fl4->daddr; |
| 658 | keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS; |
| 659 | keys.ports.src = fl4->fl4_sport; |
| 660 | keys.ports.dst = fl4->fl4_dport; |
| 661 | keys.keyid.keyid = fl4->fl4_gre_key; |
| 662 | keys.basic.ip_proto = fl4->flowi4_proto; |
| 663 | |
Tom Herbert | bcc8383 | 2015-09-01 09:24:24 -0700 | [diff] [blame] | 664 | __skb_set_sw_hash(skb, flow_hash_from_keys(&keys), |
| 665 | flow_keys_have_l4(&keys)); |
Tom Herbert | f70ea01 | 2015-07-31 16:52:10 -0700 | [diff] [blame] | 666 | |
| 667 | return skb->hash; |
| 668 | } |
| 669 | EXPORT_SYMBOL(__skb_get_hash_flowi4); |
| 670 | |
Alexander Duyck | 56193d1 | 2014-09-05 19:20:26 -0400 | [diff] [blame] | 671 | u32 __skb_get_poff(const struct sk_buff *skb, void *data, |
| 672 | const struct flow_keys *keys, int hlen) |
Daniel Borkmann | f77668d | 2013-03-19 06:39:30 +0000 | [diff] [blame] | 673 | { |
Tom Herbert | 42aecaa | 2015-06-04 09:16:39 -0700 | [diff] [blame] | 674 | u32 poff = keys->control.thoff; |
Daniel Borkmann | f77668d | 2013-03-19 06:39:30 +0000 | [diff] [blame] | 675 | |
Jiri Pirko | 06635a3 | 2015-05-12 14:56:16 +0200 | [diff] [blame] | 676 | switch (keys->basic.ip_proto) { |
Daniel Borkmann | f77668d | 2013-03-19 06:39:30 +0000 | [diff] [blame] | 677 | case IPPROTO_TCP: { |
Alexander Duyck | 5af7fb6 | 2014-10-10 12:09:12 -0700 | [diff] [blame] | 678 | /* access doff as u8 to avoid unaligned access */ |
| 679 | const u8 *doff; |
| 680 | u8 _doff; |
Daniel Borkmann | f77668d | 2013-03-19 06:39:30 +0000 | [diff] [blame] | 681 | |
Alexander Duyck | 5af7fb6 | 2014-10-10 12:09:12 -0700 | [diff] [blame] | 682 | doff = __skb_header_pointer(skb, poff + 12, sizeof(_doff), |
| 683 | data, hlen, &_doff); |
| 684 | if (!doff) |
Daniel Borkmann | f77668d | 2013-03-19 06:39:30 +0000 | [diff] [blame] | 685 | return poff; |
| 686 | |
Alexander Duyck | 5af7fb6 | 2014-10-10 12:09:12 -0700 | [diff] [blame] | 687 | poff += max_t(u32, sizeof(struct tcphdr), (*doff & 0xF0) >> 2); |
Daniel Borkmann | f77668d | 2013-03-19 06:39:30 +0000 | [diff] [blame] | 688 | break; |
| 689 | } |
| 690 | case IPPROTO_UDP: |
| 691 | case IPPROTO_UDPLITE: |
| 692 | poff += sizeof(struct udphdr); |
| 693 | break; |
| 694 | /* For the rest, we do not really care about header |
| 695 | * extensions at this point for now. |
| 696 | */ |
| 697 | case IPPROTO_ICMP: |
| 698 | poff += sizeof(struct icmphdr); |
| 699 | break; |
| 700 | case IPPROTO_ICMPV6: |
| 701 | poff += sizeof(struct icmp6hdr); |
| 702 | break; |
| 703 | case IPPROTO_IGMP: |
| 704 | poff += sizeof(struct igmphdr); |
| 705 | break; |
| 706 | case IPPROTO_DCCP: |
| 707 | poff += sizeof(struct dccp_hdr); |
| 708 | break; |
| 709 | case IPPROTO_SCTP: |
| 710 | poff += sizeof(struct sctphdr); |
| 711 | break; |
| 712 | } |
| 713 | |
| 714 | return poff; |
| 715 | } |
| 716 | |
Jiri Pirko | 0db89b8 | 2015-05-12 14:56:14 +0200 | [diff] [blame] | 717 | /** |
| 718 | * skb_get_poff - get the offset to the payload |
| 719 | * @skb: sk_buff to get the payload offset from |
| 720 | * |
| 721 | * The function will get the offset to the payload as far as it could |
| 722 | * 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] | 723 | * truncate packets without needing to push actual payload to the user |
| 724 | * space and can analyze headers only, instead. |
| 725 | */ |
| 726 | u32 skb_get_poff(const struct sk_buff *skb) |
| 727 | { |
| 728 | struct flow_keys keys; |
| 729 | |
Jiri Pirko | 06635a3 | 2015-05-12 14:56:16 +0200 | [diff] [blame] | 730 | if (!skb_flow_dissect_flow_keys(skb, &keys)) |
Alexander Duyck | 56193d1 | 2014-09-05 19:20:26 -0400 | [diff] [blame] | 731 | return 0; |
| 732 | |
| 733 | return __skb_get_poff(skb, skb->data, &keys, skb_headlen(skb)); |
| 734 | } |
Jiri Pirko | 06635a3 | 2015-05-12 14:56:16 +0200 | [diff] [blame] | 735 | |
| 736 | static const struct flow_dissector_key flow_keys_dissector_keys[] = { |
| 737 | { |
Tom Herbert | 42aecaa | 2015-06-04 09:16:39 -0700 | [diff] [blame] | 738 | .key_id = FLOW_DISSECTOR_KEY_CONTROL, |
| 739 | .offset = offsetof(struct flow_keys, control), |
| 740 | }, |
| 741 | { |
Jiri Pirko | 06635a3 | 2015-05-12 14:56:16 +0200 | [diff] [blame] | 742 | .key_id = FLOW_DISSECTOR_KEY_BASIC, |
| 743 | .offset = offsetof(struct flow_keys, basic), |
| 744 | }, |
| 745 | { |
| 746 | .key_id = FLOW_DISSECTOR_KEY_IPV4_ADDRS, |
Tom Herbert | c3f8324 | 2015-06-04 09:16:40 -0700 | [diff] [blame] | 747 | .offset = offsetof(struct flow_keys, addrs.v4addrs), |
| 748 | }, |
| 749 | { |
| 750 | .key_id = FLOW_DISSECTOR_KEY_IPV6_ADDRS, |
| 751 | .offset = offsetof(struct flow_keys, addrs.v6addrs), |
Jiri Pirko | 06635a3 | 2015-05-12 14:56:16 +0200 | [diff] [blame] | 752 | }, |
| 753 | { |
Tom Herbert | 9f24908 | 2015-06-04 09:16:41 -0700 | [diff] [blame] | 754 | .key_id = FLOW_DISSECTOR_KEY_TIPC_ADDRS, |
| 755 | .offset = offsetof(struct flow_keys, addrs.tipcaddrs), |
| 756 | }, |
| 757 | { |
Jiri Pirko | 06635a3 | 2015-05-12 14:56:16 +0200 | [diff] [blame] | 758 | .key_id = FLOW_DISSECTOR_KEY_PORTS, |
| 759 | .offset = offsetof(struct flow_keys, ports), |
| 760 | }, |
Tom Herbert | d34af82 | 2015-06-04 09:16:43 -0700 | [diff] [blame] | 761 | { |
| 762 | .key_id = FLOW_DISSECTOR_KEY_VLANID, |
| 763 | .offset = offsetof(struct flow_keys, tags), |
| 764 | }, |
Tom Herbert | 87ee9e5 | 2015-06-04 09:16:44 -0700 | [diff] [blame] | 765 | { |
| 766 | .key_id = FLOW_DISSECTOR_KEY_FLOW_LABEL, |
| 767 | .offset = offsetof(struct flow_keys, tags), |
| 768 | }, |
Tom Herbert | 1fdd512 | 2015-06-04 09:16:45 -0700 | [diff] [blame] | 769 | { |
| 770 | .key_id = FLOW_DISSECTOR_KEY_GRE_KEYID, |
| 771 | .offset = offsetof(struct flow_keys, keyid), |
| 772 | }, |
Jiri Pirko | 06635a3 | 2015-05-12 14:56:16 +0200 | [diff] [blame] | 773 | }; |
| 774 | |
| 775 | static const struct flow_dissector_key flow_keys_buf_dissector_keys[] = { |
| 776 | { |
Tom Herbert | 42aecaa | 2015-06-04 09:16:39 -0700 | [diff] [blame] | 777 | .key_id = FLOW_DISSECTOR_KEY_CONTROL, |
| 778 | .offset = offsetof(struct flow_keys, control), |
| 779 | }, |
| 780 | { |
Jiri Pirko | 06635a3 | 2015-05-12 14:56:16 +0200 | [diff] [blame] | 781 | .key_id = FLOW_DISSECTOR_KEY_BASIC, |
| 782 | .offset = offsetof(struct flow_keys, basic), |
| 783 | }, |
| 784 | }; |
| 785 | |
| 786 | struct flow_dissector flow_keys_dissector __read_mostly; |
| 787 | EXPORT_SYMBOL(flow_keys_dissector); |
| 788 | |
| 789 | struct flow_dissector flow_keys_buf_dissector __read_mostly; |
| 790 | |
| 791 | static int __init init_default_flow_dissectors(void) |
| 792 | { |
| 793 | skb_flow_dissector_init(&flow_keys_dissector, |
| 794 | flow_keys_dissector_keys, |
| 795 | ARRAY_SIZE(flow_keys_dissector_keys)); |
| 796 | skb_flow_dissector_init(&flow_keys_buf_dissector, |
| 797 | flow_keys_buf_dissector_keys, |
| 798 | ARRAY_SIZE(flow_keys_buf_dissector_keys)); |
| 799 | return 0; |
| 800 | } |
| 801 | |
| 802 | late_initcall_sync(init_default_flow_dissectors); |