1c50cd357SDaniel Borkmann /* 2c50cd357SDaniel Borkmann * IPV4 GSO/GRO offload support 3c50cd357SDaniel Borkmann * Linux INET implementation 4c50cd357SDaniel Borkmann * 5c50cd357SDaniel Borkmann * This program is free software; you can redistribute it and/or 6c50cd357SDaniel Borkmann * modify it under the terms of the GNU General Public License 7c50cd357SDaniel Borkmann * as published by the Free Software Foundation; either version 8c50cd357SDaniel Borkmann * 2 of the License, or (at your option) any later version. 9c50cd357SDaniel Borkmann * 10c50cd357SDaniel Borkmann * GRE GSO support 11c50cd357SDaniel Borkmann */ 12c50cd357SDaniel Borkmann 13c50cd357SDaniel Borkmann #include <linux/skbuff.h> 14cf172283SPaul Gortmaker #include <linux/init.h> 15c50cd357SDaniel Borkmann #include <net/protocol.h> 16c50cd357SDaniel Borkmann #include <net/gre.h> 17c50cd357SDaniel Borkmann 18c50cd357SDaniel Borkmann static int gre_gso_send_check(struct sk_buff *skb) 19c50cd357SDaniel Borkmann { 20c50cd357SDaniel Borkmann if (!skb->encapsulation) 21c50cd357SDaniel Borkmann return -EINVAL; 22c50cd357SDaniel Borkmann return 0; 23c50cd357SDaniel Borkmann } 24c50cd357SDaniel Borkmann 25c50cd357SDaniel Borkmann static struct sk_buff *gre_gso_segment(struct sk_buff *skb, 26c50cd357SDaniel Borkmann netdev_features_t features) 27c50cd357SDaniel Borkmann { 28c50cd357SDaniel Borkmann struct sk_buff *segs = ERR_PTR(-EINVAL); 29c50cd357SDaniel Borkmann netdev_features_t enc_features; 30b884b1a4SNeal Cardwell int ghl; 31c50cd357SDaniel Borkmann struct gre_base_hdr *greh; 327a7ffbabSWei-Chun Chao u16 mac_offset = skb->mac_header; 33c50cd357SDaniel Borkmann int mac_len = skb->mac_len; 34c50cd357SDaniel Borkmann __be16 protocol = skb->protocol; 35c50cd357SDaniel Borkmann int tnl_hlen; 36c50cd357SDaniel Borkmann bool csum; 37c50cd357SDaniel Borkmann 38c50cd357SDaniel Borkmann if (unlikely(skb_shinfo(skb)->gso_type & 39c50cd357SDaniel Borkmann ~(SKB_GSO_TCPV4 | 40c50cd357SDaniel Borkmann SKB_GSO_TCPV6 | 41c50cd357SDaniel Borkmann SKB_GSO_UDP | 42c50cd357SDaniel Borkmann SKB_GSO_DODGY | 43c50cd357SDaniel Borkmann SKB_GSO_TCP_ECN | 44cb32f511SEric Dumazet SKB_GSO_GRE | 454749c09cSTom Herbert SKB_GSO_GRE_CSUM | 46cb32f511SEric Dumazet SKB_GSO_IPIP))) 47c50cd357SDaniel Borkmann goto out; 48c50cd357SDaniel Borkmann 49c50cd357SDaniel Borkmann if (unlikely(!pskb_may_pull(skb, sizeof(*greh)))) 50c50cd357SDaniel Borkmann goto out; 51c50cd357SDaniel Borkmann 52c50cd357SDaniel Borkmann greh = (struct gre_base_hdr *)skb_transport_header(skb); 53c50cd357SDaniel Borkmann 54b884b1a4SNeal Cardwell ghl = skb_inner_network_header(skb) - skb_transport_header(skb); 55b884b1a4SNeal Cardwell if (unlikely(ghl < sizeof(*greh))) 56b884b1a4SNeal Cardwell goto out; 57b884b1a4SNeal Cardwell 58b884b1a4SNeal Cardwell csum = !!(greh->flags & GRE_CSUM); 594749c09cSTom Herbert if (csum) 604749c09cSTom Herbert skb->encap_hdr_csum = 1; 61c50cd357SDaniel Borkmann 627a7ffbabSWei-Chun Chao if (unlikely(!pskb_may_pull(skb, ghl))) 637a7ffbabSWei-Chun Chao goto out; 647a7ffbabSWei-Chun Chao 65c50cd357SDaniel Borkmann /* setup inner skb. */ 66c50cd357SDaniel Borkmann skb->protocol = greh->protocol; 67c50cd357SDaniel Borkmann skb->encapsulation = 0; 68c50cd357SDaniel Borkmann 69c50cd357SDaniel Borkmann __skb_pull(skb, ghl); 70c50cd357SDaniel Borkmann skb_reset_mac_header(skb); 71c50cd357SDaniel Borkmann skb_set_network_header(skb, skb_inner_network_offset(skb)); 72c50cd357SDaniel Borkmann skb->mac_len = skb_inner_network_offset(skb); 73c50cd357SDaniel Borkmann 74c50cd357SDaniel Borkmann /* segment inner packet. */ 75c50cd357SDaniel Borkmann enc_features = skb->dev->hw_enc_features & netif_skb_features(skb); 76c50cd357SDaniel Borkmann segs = skb_mac_gso_segment(skb, enc_features); 777a7ffbabSWei-Chun Chao if (!segs || IS_ERR(segs)) { 787a7ffbabSWei-Chun Chao skb_gso_error_unwind(skb, protocol, ghl, mac_offset, mac_len); 79c50cd357SDaniel Borkmann goto out; 807a7ffbabSWei-Chun Chao } 81c50cd357SDaniel Borkmann 82c50cd357SDaniel Borkmann skb = segs; 83c50cd357SDaniel Borkmann tnl_hlen = skb_tnl_header_len(skb); 84c50cd357SDaniel Borkmann do { 85c50cd357SDaniel Borkmann __skb_push(skb, ghl); 86c50cd357SDaniel Borkmann if (csum) { 87c50cd357SDaniel Borkmann __be32 *pcsum; 88c50cd357SDaniel Borkmann 89c50cd357SDaniel Borkmann if (skb_has_shared_frag(skb)) { 90c50cd357SDaniel Borkmann int err; 91c50cd357SDaniel Borkmann 92c50cd357SDaniel Borkmann err = __skb_linearize(skb); 93c50cd357SDaniel Borkmann if (err) { 940c1072aeSDavid S. Miller kfree_skb_list(segs); 95c50cd357SDaniel Borkmann segs = ERR_PTR(err); 96c50cd357SDaniel Borkmann goto out; 97c50cd357SDaniel Borkmann } 98c50cd357SDaniel Borkmann } 99c50cd357SDaniel Borkmann 1004749c09cSTom Herbert skb_reset_transport_header(skb); 1014749c09cSTom Herbert 1024749c09cSTom Herbert greh = (struct gre_base_hdr *) 1034749c09cSTom Herbert skb_transport_header(skb); 104c50cd357SDaniel Borkmann pcsum = (__be32 *)(greh + 1); 105c50cd357SDaniel Borkmann *pcsum = 0; 1064749c09cSTom Herbert *(__sum16 *)pcsum = gso_make_checksum(skb, 0); 107c50cd357SDaniel Borkmann } 108c50cd357SDaniel Borkmann __skb_push(skb, tnl_hlen - ghl); 109c50cd357SDaniel Borkmann 110cdbaa0bbSAlexander Duyck skb_reset_inner_headers(skb); 111cdbaa0bbSAlexander Duyck skb->encapsulation = 1; 112cdbaa0bbSAlexander Duyck 113c50cd357SDaniel Borkmann skb_reset_mac_header(skb); 114c50cd357SDaniel Borkmann skb_set_network_header(skb, mac_len); 115c50cd357SDaniel Borkmann skb->mac_len = mac_len; 116c50cd357SDaniel Borkmann skb->protocol = protocol; 117c50cd357SDaniel Borkmann } while ((skb = skb->next)); 118c50cd357SDaniel Borkmann out: 119c50cd357SDaniel Borkmann return segs; 120c50cd357SDaniel Borkmann } 121c50cd357SDaniel Borkmann 122bf5a755fSJerry Chu /* Compute the whole skb csum in s/w and store it, then verify GRO csum 123bf5a755fSJerry Chu * starting from gro_offset. 124bf5a755fSJerry Chu */ 125bf5a755fSJerry Chu static __sum16 gro_skb_checksum(struct sk_buff *skb) 126bf5a755fSJerry Chu { 127bf5a755fSJerry Chu __sum16 sum; 128bf5a755fSJerry Chu 129bf5a755fSJerry Chu skb->csum = skb_checksum(skb, 0, skb->len, 0); 130bf5a755fSJerry Chu NAPI_GRO_CB(skb)->csum = csum_sub(skb->csum, 131bf5a755fSJerry Chu csum_partial(skb->data, skb_gro_offset(skb), 0)); 132bf5a755fSJerry Chu sum = csum_fold(NAPI_GRO_CB(skb)->csum); 133bf5a755fSJerry Chu if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE)) { 134bf5a755fSJerry Chu if (unlikely(!sum)) 135bf5a755fSJerry Chu netdev_rx_csum_fault(skb->dev); 136bf5a755fSJerry Chu } else 137bf5a755fSJerry Chu skb->ip_summed = CHECKSUM_COMPLETE; 138bf5a755fSJerry Chu 139bf5a755fSJerry Chu return sum; 140bf5a755fSJerry Chu } 141bf5a755fSJerry Chu 142bf5a755fSJerry Chu static struct sk_buff **gre_gro_receive(struct sk_buff **head, 143bf5a755fSJerry Chu struct sk_buff *skb) 144bf5a755fSJerry Chu { 145bf5a755fSJerry Chu struct sk_buff **pp = NULL; 146bf5a755fSJerry Chu struct sk_buff *p; 147bf5a755fSJerry Chu const struct gre_base_hdr *greh; 148bf5a755fSJerry Chu unsigned int hlen, grehlen; 149bf5a755fSJerry Chu unsigned int off; 150bf5a755fSJerry Chu int flush = 1; 151bf5a755fSJerry Chu struct packet_offload *ptype; 152bf5a755fSJerry Chu __be16 type; 153bf5a755fSJerry Chu 154bf5a755fSJerry Chu off = skb_gro_offset(skb); 155bf5a755fSJerry Chu hlen = off + sizeof(*greh); 156bf5a755fSJerry Chu greh = skb_gro_header_fast(skb, off); 157bf5a755fSJerry Chu if (skb_gro_header_hard(skb, hlen)) { 158bf5a755fSJerry Chu greh = skb_gro_header_slow(skb, hlen, off); 159bf5a755fSJerry Chu if (unlikely(!greh)) 160bf5a755fSJerry Chu goto out; 161bf5a755fSJerry Chu } 162bf5a755fSJerry Chu 163bf5a755fSJerry Chu /* Only support version 0 and K (key), C (csum) flags. Note that 164bf5a755fSJerry Chu * although the support for the S (seq#) flag can be added easily 165bf5a755fSJerry Chu * for GRO, this is problematic for GSO hence can not be enabled 166bf5a755fSJerry Chu * here because a GRO pkt may end up in the forwarding path, thus 167bf5a755fSJerry Chu * requiring GSO support to break it up correctly. 168bf5a755fSJerry Chu */ 169bf5a755fSJerry Chu if ((greh->flags & ~(GRE_KEY|GRE_CSUM)) != 0) 170bf5a755fSJerry Chu goto out; 171bf5a755fSJerry Chu 172bf5a755fSJerry Chu type = greh->protocol; 173bf5a755fSJerry Chu 174bf5a755fSJerry Chu rcu_read_lock(); 175bf5a755fSJerry Chu ptype = gro_find_receive_by_type(type); 176bf5a755fSJerry Chu if (ptype == NULL) 177bf5a755fSJerry Chu goto out_unlock; 178bf5a755fSJerry Chu 179bf5a755fSJerry Chu grehlen = GRE_HEADER_SECTION; 180bf5a755fSJerry Chu 181bf5a755fSJerry Chu if (greh->flags & GRE_KEY) 182bf5a755fSJerry Chu grehlen += GRE_HEADER_SECTION; 183bf5a755fSJerry Chu 184bf5a755fSJerry Chu if (greh->flags & GRE_CSUM) 185bf5a755fSJerry Chu grehlen += GRE_HEADER_SECTION; 186bf5a755fSJerry Chu 187bf5a755fSJerry Chu hlen = off + grehlen; 188bf5a755fSJerry Chu if (skb_gro_header_hard(skb, hlen)) { 189bf5a755fSJerry Chu greh = skb_gro_header_slow(skb, hlen, off); 190bf5a755fSJerry Chu if (unlikely(!greh)) 191bf5a755fSJerry Chu goto out_unlock; 192bf5a755fSJerry Chu } 193bf5a755fSJerry Chu if (greh->flags & GRE_CSUM) { /* Need to verify GRE csum first */ 194bf5a755fSJerry Chu __sum16 csum = 0; 195bf5a755fSJerry Chu 196bf5a755fSJerry Chu if (skb->ip_summed == CHECKSUM_COMPLETE) 197bf5a755fSJerry Chu csum = csum_fold(NAPI_GRO_CB(skb)->csum); 198bf5a755fSJerry Chu /* Don't trust csum error calculated/reported by h/w */ 199bf5a755fSJerry Chu if (skb->ip_summed == CHECKSUM_NONE || csum != 0) 200bf5a755fSJerry Chu csum = gro_skb_checksum(skb); 201bf5a755fSJerry Chu 202bf5a755fSJerry Chu /* GRE CSUM is the 1's complement of the 1's complement sum 203bf5a755fSJerry Chu * of the GRE hdr plus payload so it should add up to 0xffff 204bf5a755fSJerry Chu * (and 0 after csum_fold()) just like the IPv4 hdr csum. 205bf5a755fSJerry Chu */ 206bf5a755fSJerry Chu if (csum) 207bf5a755fSJerry Chu goto out_unlock; 208bf5a755fSJerry Chu } 209bf5a755fSJerry Chu flush = 0; 210bf5a755fSJerry Chu 211bf5a755fSJerry Chu for (p = *head; p; p = p->next) { 212bf5a755fSJerry Chu const struct gre_base_hdr *greh2; 213bf5a755fSJerry Chu 214bf5a755fSJerry Chu if (!NAPI_GRO_CB(p)->same_flow) 215bf5a755fSJerry Chu continue; 216bf5a755fSJerry Chu 217bf5a755fSJerry Chu /* The following checks are needed to ensure only pkts 218bf5a755fSJerry Chu * from the same tunnel are considered for aggregation. 219bf5a755fSJerry Chu * The criteria for "the same tunnel" includes: 220bf5a755fSJerry Chu * 1) same version (we only support version 0 here) 221bf5a755fSJerry Chu * 2) same protocol (we only support ETH_P_IP for now) 222bf5a755fSJerry Chu * 3) same set of flags 223bf5a755fSJerry Chu * 4) same key if the key field is present. 224bf5a755fSJerry Chu */ 225bf5a755fSJerry Chu greh2 = (struct gre_base_hdr *)(p->data + off); 226bf5a755fSJerry Chu 227bf5a755fSJerry Chu if (greh2->flags != greh->flags || 228bf5a755fSJerry Chu greh2->protocol != greh->protocol) { 229bf5a755fSJerry Chu NAPI_GRO_CB(p)->same_flow = 0; 230bf5a755fSJerry Chu continue; 231bf5a755fSJerry Chu } 232bf5a755fSJerry Chu if (greh->flags & GRE_KEY) { 233bf5a755fSJerry Chu /* compare keys */ 234bf5a755fSJerry Chu if (*(__be32 *)(greh2+1) != *(__be32 *)(greh+1)) { 235bf5a755fSJerry Chu NAPI_GRO_CB(p)->same_flow = 0; 236bf5a755fSJerry Chu continue; 237bf5a755fSJerry Chu } 238bf5a755fSJerry Chu } 239bf5a755fSJerry Chu } 240bf5a755fSJerry Chu 241bf5a755fSJerry Chu skb_gro_pull(skb, grehlen); 242bf5a755fSJerry Chu 243bf5a755fSJerry Chu /* Adjusted NAPI_GRO_CB(skb)->csum after skb_gro_pull()*/ 244bf5a755fSJerry Chu skb_gro_postpull_rcsum(skb, greh, grehlen); 245bf5a755fSJerry Chu 246bf5a755fSJerry Chu pp = ptype->callbacks.gro_receive(head, skb); 247bf5a755fSJerry Chu 248bf5a755fSJerry Chu out_unlock: 249bf5a755fSJerry Chu rcu_read_unlock(); 250bf5a755fSJerry Chu out: 251bf5a755fSJerry Chu NAPI_GRO_CB(skb)->flush |= flush; 252bf5a755fSJerry Chu 253bf5a755fSJerry Chu return pp; 254bf5a755fSJerry Chu } 255bf5a755fSJerry Chu 256d10dbad2SWei Yongjun static int gre_gro_complete(struct sk_buff *skb, int nhoff) 257bf5a755fSJerry Chu { 258bf5a755fSJerry Chu struct gre_base_hdr *greh = (struct gre_base_hdr *)(skb->data + nhoff); 259bf5a755fSJerry Chu struct packet_offload *ptype; 260bf5a755fSJerry Chu unsigned int grehlen = sizeof(*greh); 261bf5a755fSJerry Chu int err = -ENOENT; 262bf5a755fSJerry Chu __be16 type; 263bf5a755fSJerry Chu 264bf5a755fSJerry Chu type = greh->protocol; 265bf5a755fSJerry Chu if (greh->flags & GRE_KEY) 266bf5a755fSJerry Chu grehlen += GRE_HEADER_SECTION; 267bf5a755fSJerry Chu 268bf5a755fSJerry Chu if (greh->flags & GRE_CSUM) 269bf5a755fSJerry Chu grehlen += GRE_HEADER_SECTION; 270bf5a755fSJerry Chu 271bf5a755fSJerry Chu rcu_read_lock(); 272bf5a755fSJerry Chu ptype = gro_find_complete_by_type(type); 273bf5a755fSJerry Chu if (ptype != NULL) 274bf5a755fSJerry Chu err = ptype->callbacks.gro_complete(skb, nhoff + grehlen); 275bf5a755fSJerry Chu 276bf5a755fSJerry Chu rcu_read_unlock(); 277bf5a755fSJerry Chu return err; 278bf5a755fSJerry Chu } 279bf5a755fSJerry Chu 280c50cd357SDaniel Borkmann static const struct net_offload gre_offload = { 281c50cd357SDaniel Borkmann .callbacks = { 282c50cd357SDaniel Borkmann .gso_send_check = gre_gso_send_check, 283c50cd357SDaniel Borkmann .gso_segment = gre_gso_segment, 284bf5a755fSJerry Chu .gro_receive = gre_gro_receive, 285bf5a755fSJerry Chu .gro_complete = gre_gro_complete, 286c50cd357SDaniel Borkmann }, 287c50cd357SDaniel Borkmann }; 288c50cd357SDaniel Borkmann 289438e38faSEric Dumazet static int __init gre_offload_init(void) 290c50cd357SDaniel Borkmann { 291c50cd357SDaniel Borkmann return inet_add_offload(&gre_offload, IPPROTO_GRE); 292c50cd357SDaniel Borkmann } 293cf172283SPaul Gortmaker device_initcall(gre_offload_init); 294