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 struct sk_buff *gre_gso_segment(struct sk_buff *skb, 19c50cd357SDaniel Borkmann netdev_features_t features) 20c50cd357SDaniel Borkmann { 21c50cd357SDaniel Borkmann struct sk_buff *segs = ERR_PTR(-EINVAL); 22c50cd357SDaniel Borkmann netdev_features_t enc_features; 23b884b1a4SNeal Cardwell int ghl; 24c50cd357SDaniel Borkmann struct gre_base_hdr *greh; 257a7ffbabSWei-Chun Chao u16 mac_offset = skb->mac_header; 26c50cd357SDaniel Borkmann int mac_len = skb->mac_len; 27c50cd357SDaniel Borkmann __be16 protocol = skb->protocol; 28c50cd357SDaniel Borkmann int tnl_hlen; 29c50cd357SDaniel Borkmann bool csum; 30c50cd357SDaniel Borkmann 31c50cd357SDaniel Borkmann if (unlikely(skb_shinfo(skb)->gso_type & 32c50cd357SDaniel Borkmann ~(SKB_GSO_TCPV4 | 33c50cd357SDaniel Borkmann SKB_GSO_TCPV6 | 34c50cd357SDaniel Borkmann SKB_GSO_UDP | 35c50cd357SDaniel Borkmann SKB_GSO_DODGY | 36c50cd357SDaniel Borkmann SKB_GSO_TCP_ECN | 37cb32f511SEric Dumazet SKB_GSO_GRE | 384749c09cSTom Herbert SKB_GSO_GRE_CSUM | 39cb32f511SEric Dumazet SKB_GSO_IPIP))) 40c50cd357SDaniel Borkmann goto out; 41c50cd357SDaniel Borkmann 4253e50398STom Herbert if (!skb->encapsulation) 4353e50398STom Herbert goto out; 4453e50398STom Herbert 45c50cd357SDaniel Borkmann if (unlikely(!pskb_may_pull(skb, sizeof(*greh)))) 46c50cd357SDaniel Borkmann goto out; 47c50cd357SDaniel Borkmann 48c50cd357SDaniel Borkmann greh = (struct gre_base_hdr *)skb_transport_header(skb); 49c50cd357SDaniel Borkmann 5014051f04STom Herbert ghl = skb_inner_mac_header(skb) - skb_transport_header(skb); 51b884b1a4SNeal Cardwell if (unlikely(ghl < sizeof(*greh))) 52b884b1a4SNeal Cardwell goto out; 53b884b1a4SNeal Cardwell 54b884b1a4SNeal Cardwell csum = !!(greh->flags & GRE_CSUM); 554749c09cSTom Herbert if (csum) 564749c09cSTom Herbert skb->encap_hdr_csum = 1; 57c50cd357SDaniel Borkmann 58c50cd357SDaniel Borkmann /* setup inner skb. */ 59c50cd357SDaniel Borkmann skb->protocol = greh->protocol; 60c50cd357SDaniel Borkmann skb->encapsulation = 0; 61c50cd357SDaniel Borkmann 62b4e3cef7SLi RongQing if (unlikely(!pskb_may_pull(skb, ghl))) 63b4e3cef7SLi RongQing goto out; 64b4e3cef7SLi RongQing 65c50cd357SDaniel Borkmann __skb_pull(skb, ghl); 66c50cd357SDaniel Borkmann skb_reset_mac_header(skb); 67c50cd357SDaniel Borkmann skb_set_network_header(skb, skb_inner_network_offset(skb)); 68c50cd357SDaniel Borkmann skb->mac_len = skb_inner_network_offset(skb); 69c50cd357SDaniel Borkmann 70c50cd357SDaniel Borkmann /* segment inner packet. */ 711e16aa3dSFlorian Westphal enc_features = skb->dev->hw_enc_features & features; 72c50cd357SDaniel Borkmann segs = skb_mac_gso_segment(skb, enc_features); 735a8dbf03SHimangi Saraogi if (IS_ERR_OR_NULL(segs)) { 747a7ffbabSWei-Chun Chao skb_gso_error_unwind(skb, protocol, ghl, mac_offset, mac_len); 75c50cd357SDaniel Borkmann goto out; 767a7ffbabSWei-Chun Chao } 77c50cd357SDaniel Borkmann 78c50cd357SDaniel Borkmann skb = segs; 79c50cd357SDaniel Borkmann tnl_hlen = skb_tnl_header_len(skb); 80c50cd357SDaniel Borkmann do { 81c50cd357SDaniel Borkmann __skb_push(skb, ghl); 82c50cd357SDaniel Borkmann if (csum) { 83c50cd357SDaniel Borkmann __be32 *pcsum; 84c50cd357SDaniel Borkmann 85c50cd357SDaniel Borkmann if (skb_has_shared_frag(skb)) { 86c50cd357SDaniel Borkmann int err; 87c50cd357SDaniel Borkmann 88c50cd357SDaniel Borkmann err = __skb_linearize(skb); 89c50cd357SDaniel Borkmann if (err) { 900c1072aeSDavid S. Miller kfree_skb_list(segs); 91c50cd357SDaniel Borkmann segs = ERR_PTR(err); 92c50cd357SDaniel Borkmann goto out; 93c50cd357SDaniel Borkmann } 94c50cd357SDaniel Borkmann } 95c50cd357SDaniel Borkmann 964749c09cSTom Herbert skb_reset_transport_header(skb); 974749c09cSTom Herbert 984749c09cSTom Herbert greh = (struct gre_base_hdr *) 994749c09cSTom Herbert skb_transport_header(skb); 100c50cd357SDaniel Borkmann pcsum = (__be32 *)(greh + 1); 101c50cd357SDaniel Borkmann *pcsum = 0; 1024749c09cSTom Herbert *(__sum16 *)pcsum = gso_make_checksum(skb, 0); 103c50cd357SDaniel Borkmann } 104c50cd357SDaniel Borkmann __skb_push(skb, tnl_hlen - ghl); 105c50cd357SDaniel Borkmann 106cdbaa0bbSAlexander Duyck skb_reset_inner_headers(skb); 107cdbaa0bbSAlexander Duyck skb->encapsulation = 1; 108cdbaa0bbSAlexander Duyck 109c50cd357SDaniel Borkmann skb_reset_mac_header(skb); 110c50cd357SDaniel Borkmann skb_set_network_header(skb, mac_len); 111c50cd357SDaniel Borkmann skb->mac_len = mac_len; 112c50cd357SDaniel Borkmann skb->protocol = protocol; 113c50cd357SDaniel Borkmann } while ((skb = skb->next)); 114c50cd357SDaniel Borkmann out: 115c50cd357SDaniel Borkmann return segs; 116c50cd357SDaniel Borkmann } 117c50cd357SDaniel Borkmann 118bf5a755fSJerry Chu static struct sk_buff **gre_gro_receive(struct sk_buff **head, 119bf5a755fSJerry Chu struct sk_buff *skb) 120bf5a755fSJerry Chu { 121bf5a755fSJerry Chu struct sk_buff **pp = NULL; 122bf5a755fSJerry Chu struct sk_buff *p; 123bf5a755fSJerry Chu const struct gre_base_hdr *greh; 124bf5a755fSJerry Chu unsigned int hlen, grehlen; 125bf5a755fSJerry Chu unsigned int off; 126bf5a755fSJerry Chu int flush = 1; 127bf5a755fSJerry Chu struct packet_offload *ptype; 128bf5a755fSJerry Chu __be16 type; 129bf5a755fSJerry Chu 130bf5a755fSJerry Chu off = skb_gro_offset(skb); 131bf5a755fSJerry Chu hlen = off + sizeof(*greh); 132bf5a755fSJerry Chu greh = skb_gro_header_fast(skb, off); 133bf5a755fSJerry Chu if (skb_gro_header_hard(skb, hlen)) { 134bf5a755fSJerry Chu greh = skb_gro_header_slow(skb, hlen, off); 135bf5a755fSJerry Chu if (unlikely(!greh)) 136bf5a755fSJerry Chu goto out; 137bf5a755fSJerry Chu } 138bf5a755fSJerry Chu 139bf5a755fSJerry Chu /* Only support version 0 and K (key), C (csum) flags. Note that 140bf5a755fSJerry Chu * although the support for the S (seq#) flag can be added easily 141bf5a755fSJerry Chu * for GRO, this is problematic for GSO hence can not be enabled 142bf5a755fSJerry Chu * here because a GRO pkt may end up in the forwarding path, thus 143bf5a755fSJerry Chu * requiring GSO support to break it up correctly. 144bf5a755fSJerry Chu */ 145bf5a755fSJerry Chu if ((greh->flags & ~(GRE_KEY|GRE_CSUM)) != 0) 146bf5a755fSJerry Chu goto out; 147bf5a755fSJerry Chu 148bf5a755fSJerry Chu type = greh->protocol; 149bf5a755fSJerry Chu 150bf5a755fSJerry Chu rcu_read_lock(); 151bf5a755fSJerry Chu ptype = gro_find_receive_by_type(type); 152bf5a755fSJerry Chu if (ptype == NULL) 153bf5a755fSJerry Chu goto out_unlock; 154bf5a755fSJerry Chu 155bf5a755fSJerry Chu grehlen = GRE_HEADER_SECTION; 156bf5a755fSJerry Chu 157bf5a755fSJerry Chu if (greh->flags & GRE_KEY) 158bf5a755fSJerry Chu grehlen += GRE_HEADER_SECTION; 159bf5a755fSJerry Chu 160bf5a755fSJerry Chu if (greh->flags & GRE_CSUM) 161bf5a755fSJerry Chu grehlen += GRE_HEADER_SECTION; 162bf5a755fSJerry Chu 163bf5a755fSJerry Chu hlen = off + grehlen; 164bf5a755fSJerry Chu if (skb_gro_header_hard(skb, hlen)) { 165bf5a755fSJerry Chu greh = skb_gro_header_slow(skb, hlen, off); 166bf5a755fSJerry Chu if (unlikely(!greh)) 167bf5a755fSJerry Chu goto out_unlock; 168bf5a755fSJerry Chu } 169bf5a755fSJerry Chu 170758f75d1STom Herbert /* Don't bother verifying checksum if we're going to flush anyway. */ 171884d338cSTom Herbert if ((greh->flags & GRE_CSUM) && !NAPI_GRO_CB(skb)->flush) { 172884d338cSTom Herbert if (skb_gro_checksum_simple_validate(skb)) 173bf5a755fSJerry Chu goto out_unlock; 174758f75d1STom Herbert 175884d338cSTom Herbert skb_gro_checksum_try_convert(skb, IPPROTO_GRE, 0, 176884d338cSTom Herbert null_compute_pseudo); 177884d338cSTom Herbert } 178884d338cSTom Herbert 179bf5a755fSJerry Chu flush = 0; 180bf5a755fSJerry Chu 181bf5a755fSJerry Chu for (p = *head; p; p = p->next) { 182bf5a755fSJerry Chu const struct gre_base_hdr *greh2; 183bf5a755fSJerry Chu 184bf5a755fSJerry Chu if (!NAPI_GRO_CB(p)->same_flow) 185bf5a755fSJerry Chu continue; 186bf5a755fSJerry Chu 187bf5a755fSJerry Chu /* The following checks are needed to ensure only pkts 188bf5a755fSJerry Chu * from the same tunnel are considered for aggregation. 189bf5a755fSJerry Chu * The criteria for "the same tunnel" includes: 190bf5a755fSJerry Chu * 1) same version (we only support version 0 here) 191bf5a755fSJerry Chu * 2) same protocol (we only support ETH_P_IP for now) 192bf5a755fSJerry Chu * 3) same set of flags 193bf5a755fSJerry Chu * 4) same key if the key field is present. 194bf5a755fSJerry Chu */ 195bf5a755fSJerry Chu greh2 = (struct gre_base_hdr *)(p->data + off); 196bf5a755fSJerry Chu 197bf5a755fSJerry Chu if (greh2->flags != greh->flags || 198bf5a755fSJerry Chu greh2->protocol != greh->protocol) { 199bf5a755fSJerry Chu NAPI_GRO_CB(p)->same_flow = 0; 200bf5a755fSJerry Chu continue; 201bf5a755fSJerry Chu } 202bf5a755fSJerry Chu if (greh->flags & GRE_KEY) { 203bf5a755fSJerry Chu /* compare keys */ 204bf5a755fSJerry Chu if (*(__be32 *)(greh2+1) != *(__be32 *)(greh+1)) { 205bf5a755fSJerry Chu NAPI_GRO_CB(p)->same_flow = 0; 206bf5a755fSJerry Chu continue; 207bf5a755fSJerry Chu } 208bf5a755fSJerry Chu } 209bf5a755fSJerry Chu } 210bf5a755fSJerry Chu 211bf5a755fSJerry Chu skb_gro_pull(skb, grehlen); 212bf5a755fSJerry Chu 213bf5a755fSJerry Chu /* Adjusted NAPI_GRO_CB(skb)->csum after skb_gro_pull()*/ 214bf5a755fSJerry Chu skb_gro_postpull_rcsum(skb, greh, grehlen); 215bf5a755fSJerry Chu 216bf5a755fSJerry Chu pp = ptype->callbacks.gro_receive(head, skb); 217bf5a755fSJerry Chu 218bf5a755fSJerry Chu out_unlock: 219bf5a755fSJerry Chu rcu_read_unlock(); 220bf5a755fSJerry Chu out: 221bf5a755fSJerry Chu NAPI_GRO_CB(skb)->flush |= flush; 222bf5a755fSJerry Chu 223bf5a755fSJerry Chu return pp; 224bf5a755fSJerry Chu } 225bf5a755fSJerry Chu 226d10dbad2SWei Yongjun static int gre_gro_complete(struct sk_buff *skb, int nhoff) 227bf5a755fSJerry Chu { 228bf5a755fSJerry Chu struct gre_base_hdr *greh = (struct gre_base_hdr *)(skb->data + nhoff); 229bf5a755fSJerry Chu struct packet_offload *ptype; 230bf5a755fSJerry Chu unsigned int grehlen = sizeof(*greh); 231bf5a755fSJerry Chu int err = -ENOENT; 232bf5a755fSJerry Chu __be16 type; 233bf5a755fSJerry Chu 234c3caf119SJerry Chu skb->encapsulation = 1; 235c3caf119SJerry Chu skb_shinfo(skb)->gso_type = SKB_GSO_GRE; 236c3caf119SJerry Chu 237bf5a755fSJerry Chu type = greh->protocol; 238bf5a755fSJerry Chu if (greh->flags & GRE_KEY) 239bf5a755fSJerry Chu grehlen += GRE_HEADER_SECTION; 240bf5a755fSJerry Chu 241bf5a755fSJerry Chu if (greh->flags & GRE_CSUM) 242bf5a755fSJerry Chu grehlen += GRE_HEADER_SECTION; 243bf5a755fSJerry Chu 244bf5a755fSJerry Chu rcu_read_lock(); 245bf5a755fSJerry Chu ptype = gro_find_complete_by_type(type); 246bf5a755fSJerry Chu if (ptype != NULL) 247bf5a755fSJerry Chu err = ptype->callbacks.gro_complete(skb, nhoff + grehlen); 248bf5a755fSJerry Chu 249bf5a755fSJerry Chu rcu_read_unlock(); 2506fb2a756STom Herbert 2516fb2a756STom Herbert skb_set_inner_mac_header(skb, nhoff + grehlen); 2526fb2a756STom Herbert 253bf5a755fSJerry Chu return err; 254bf5a755fSJerry Chu } 255bf5a755fSJerry Chu 256c50cd357SDaniel Borkmann static const struct net_offload gre_offload = { 257c50cd357SDaniel Borkmann .callbacks = { 258c50cd357SDaniel Borkmann .gso_segment = gre_gso_segment, 259bf5a755fSJerry Chu .gro_receive = gre_gro_receive, 260bf5a755fSJerry Chu .gro_complete = gre_gro_complete, 261c50cd357SDaniel Borkmann }, 262c50cd357SDaniel Borkmann }; 263c50cd357SDaniel Borkmann 264438e38faSEric Dumazet static int __init gre_offload_init(void) 265c50cd357SDaniel Borkmann { 266c50cd357SDaniel Borkmann return inet_add_offload(&gre_offload, IPPROTO_GRE); 267c50cd357SDaniel Borkmann } 268cf172283SPaul Gortmaker device_initcall(gre_offload_init); 269