]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - net/ipv4/gre.c
ip_gre: increase inner ip header ID during segmentation
[karo-tx-linux.git] / net / ipv4 / gre.c
1 /*
2  *      GRE over IPv4 demultiplexer driver
3  *
4  *      Authors: Dmitry Kozlov (xeb@mail.ru)
5  *
6  *      This program is free software; you can redistribute it and/or
7  *      modify it under the terms of the GNU General Public License
8  *      as published by the Free Software Foundation; either version
9  *      2 of the License, or (at your option) any later version.
10  *
11  */
12
13 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14
15 #include <linux/module.h>
16 #include <linux/kernel.h>
17 #include <linux/kmod.h>
18 #include <linux/skbuff.h>
19 #include <linux/in.h>
20 #include <linux/ip.h>
21 #include <linux/netdevice.h>
22 #include <linux/if_tunnel.h>
23 #include <linux/spinlock.h>
24 #include <net/protocol.h>
25 #include <net/gre.h>
26
27
28 static const struct gre_protocol __rcu *gre_proto[GREPROTO_MAX] __read_mostly;
29 static DEFINE_SPINLOCK(gre_proto_lock);
30 struct gre_base_hdr {
31         __be16 flags;
32         __be16 protocol;
33 };
34 #define GRE_HEADER_SECTION 4
35
36 int gre_add_protocol(const struct gre_protocol *proto, u8 version)
37 {
38         if (version >= GREPROTO_MAX)
39                 goto err_out;
40
41         spin_lock(&gre_proto_lock);
42         if (gre_proto[version])
43                 goto err_out_unlock;
44
45         RCU_INIT_POINTER(gre_proto[version], proto);
46         spin_unlock(&gre_proto_lock);
47         return 0;
48
49 err_out_unlock:
50         spin_unlock(&gre_proto_lock);
51 err_out:
52         return -1;
53 }
54 EXPORT_SYMBOL_GPL(gre_add_protocol);
55
56 int gre_del_protocol(const struct gre_protocol *proto, u8 version)
57 {
58         if (version >= GREPROTO_MAX)
59                 goto err_out;
60
61         spin_lock(&gre_proto_lock);
62         if (rcu_dereference_protected(gre_proto[version],
63                         lockdep_is_held(&gre_proto_lock)) != proto)
64                 goto err_out_unlock;
65         RCU_INIT_POINTER(gre_proto[version], NULL);
66         spin_unlock(&gre_proto_lock);
67         synchronize_rcu();
68         return 0;
69
70 err_out_unlock:
71         spin_unlock(&gre_proto_lock);
72 err_out:
73         return -1;
74 }
75 EXPORT_SYMBOL_GPL(gre_del_protocol);
76
77 static int gre_rcv(struct sk_buff *skb)
78 {
79         const struct gre_protocol *proto;
80         u8 ver;
81         int ret;
82
83         if (!pskb_may_pull(skb, 12))
84                 goto drop;
85
86         ver = skb->data[1]&0x7f;
87         if (ver >= GREPROTO_MAX)
88                 goto drop;
89
90         rcu_read_lock();
91         proto = rcu_dereference(gre_proto[ver]);
92         if (!proto || !proto->handler)
93                 goto drop_unlock;
94         ret = proto->handler(skb);
95         rcu_read_unlock();
96         return ret;
97
98 drop_unlock:
99         rcu_read_unlock();
100 drop:
101         kfree_skb(skb);
102         return NET_RX_DROP;
103 }
104
105 static void gre_err(struct sk_buff *skb, u32 info)
106 {
107         const struct gre_protocol *proto;
108         const struct iphdr *iph = (const struct iphdr *)skb->data;
109         u8 ver = skb->data[(iph->ihl<<2) + 1]&0x7f;
110
111         if (ver >= GREPROTO_MAX)
112                 return;
113
114         rcu_read_lock();
115         proto = rcu_dereference(gre_proto[ver]);
116         if (proto && proto->err_handler)
117                 proto->err_handler(skb, info);
118         rcu_read_unlock();
119 }
120
121 static struct sk_buff *gre_gso_segment(struct sk_buff *skb,
122                                        netdev_features_t features)
123 {
124         struct sk_buff *segs = ERR_PTR(-EINVAL);
125         netdev_features_t enc_features;
126         int ghl = GRE_HEADER_SECTION;
127         struct gre_base_hdr *greh;
128         struct iphdr *iph;
129         int mac_len = skb->mac_len;
130         int tnl_hlen, id;
131         bool csum;
132
133         if (unlikely(skb_shinfo(skb)->gso_type &
134                                 ~(SKB_GSO_TCPV4 |
135                                   SKB_GSO_TCPV6 |
136                                   SKB_GSO_UDP |
137                                   SKB_GSO_DODGY |
138                                   SKB_GSO_TCP_ECN |
139                                   SKB_GSO_GRE)))
140                 goto out;
141
142         if (unlikely(!pskb_may_pull(skb, sizeof(*greh))))
143                 goto out;
144
145         greh = (struct gre_base_hdr *)skb_transport_header(skb);
146
147         if (greh->flags & GRE_KEY)
148                 ghl += GRE_HEADER_SECTION;
149         if (greh->flags & GRE_SEQ)
150                 ghl += GRE_HEADER_SECTION;
151         if (greh->flags & GRE_CSUM) {
152                 ghl += GRE_HEADER_SECTION;
153                 csum = true;
154         } else
155                 csum = false;
156
157         /* setup inner skb. */
158         if (greh->protocol == htons(ETH_P_TEB)) {
159                 struct ethhdr *eth = eth_hdr(skb);
160                 skb->protocol = eth->h_proto;
161         } else {
162                 skb->protocol = greh->protocol;
163         }
164
165         skb->encapsulation = 0;
166
167         if (unlikely(!pskb_may_pull(skb, ghl)))
168                 goto out;
169         __skb_pull(skb, ghl);
170         skb_reset_mac_header(skb);
171         skb_set_network_header(skb, skb_inner_network_offset(skb));
172         skb->mac_len = skb_inner_network_offset(skb);
173
174         iph = ip_hdr(skb);
175         id = ntohs(iph->id);
176         /* segment inner packet. */
177         enc_features = skb->dev->hw_enc_features & netif_skb_features(skb);
178         segs = skb_mac_gso_segment(skb, enc_features);
179         if (!segs || IS_ERR(segs))
180                 goto out;
181
182         skb = segs;
183         tnl_hlen = skb_tnl_header_len(skb);
184         do {
185                 iph = (struct iphdr *)skb->data;
186                 iph->id = htons(id++);
187                 __skb_push(skb, ghl);
188                 if (csum) {
189                         __be32 *pcsum;
190
191                         if (skb_has_shared_frag(skb)) {
192                                 int err;
193
194                                 err = __skb_linearize(skb);
195                                 if (err) {
196                                         kfree_skb(segs);
197                                         segs = ERR_PTR(err);
198                                         goto out;
199                                 }
200                         }
201
202                         greh = (struct gre_base_hdr *)(skb->data);
203                         pcsum = (__be32 *)(greh + 1);
204                         *pcsum = 0;
205                         *(__sum16 *)pcsum = csum_fold(skb_checksum(skb, 0, skb->len, 0));
206                 }
207                 __skb_push(skb, tnl_hlen - ghl);
208
209                 skb_reset_mac_header(skb);
210                 skb_set_network_header(skb, mac_len);
211                 skb->mac_len = mac_len;
212         } while ((skb = skb->next));
213 out:
214         return segs;
215 }
216
217 static int gre_gso_send_check(struct sk_buff *skb)
218 {
219         if (!skb->encapsulation)
220                 return -EINVAL;
221         return 0;
222 }
223
224 static const struct net_protocol net_gre_protocol = {
225         .handler     = gre_rcv,
226         .err_handler = gre_err,
227         .netns_ok    = 1,
228 };
229
230 static const struct net_offload gre_offload = {
231         .callbacks = {
232                 .gso_send_check =       gre_gso_send_check,
233                 .gso_segment    =       gre_gso_segment,
234         },
235 };
236
237 static int __init gre_init(void)
238 {
239         pr_info("GRE over IPv4 demultiplexor driver\n");
240
241         if (inet_add_protocol(&net_gre_protocol, IPPROTO_GRE) < 0) {
242                 pr_err("can't add protocol\n");
243                 return -EAGAIN;
244         }
245
246         if (inet_add_offload(&gre_offload, IPPROTO_GRE)) {
247                 pr_err("can't add protocol offload\n");
248                 inet_del_protocol(&net_gre_protocol, IPPROTO_GRE);
249                 return -EAGAIN;
250         }
251
252         return 0;
253 }
254
255 static void __exit gre_exit(void)
256 {
257         inet_del_offload(&gre_offload, IPPROTO_GRE);
258         inet_del_protocol(&net_gre_protocol, IPPROTO_GRE);
259 }
260
261 module_init(gre_init);
262 module_exit(gre_exit);
263
264 MODULE_DESCRIPTION("GRE over IPv4 demultiplexer driver");
265 MODULE_AUTHOR("D. Kozlov (xeb@mail.ru)");
266 MODULE_LICENSE("GPL");
267