diff options
Diffstat (limited to 'net/ipv4/ip_gre.c')
-rw-r--r-- | net/ipv4/ip_gre.c | 1516 |
1 files changed, 316 insertions, 1200 deletions
diff --git a/net/ipv4/ip_gre.c b/net/ipv4/ip_gre.c index 91d66dbde9c0..ad662e906f7e 100644 --- a/net/ipv4/ip_gre.c +++ b/net/ipv4/ip_gre.c | |||
@@ -37,7 +37,7 @@ | |||
37 | #include <net/ip.h> | 37 | #include <net/ip.h> |
38 | #include <net/icmp.h> | 38 | #include <net/icmp.h> |
39 | #include <net/protocol.h> | 39 | #include <net/protocol.h> |
40 | #include <net/ipip.h> | 40 | #include <net/ip_tunnels.h> |
41 | #include <net/arp.h> | 41 | #include <net/arp.h> |
42 | #include <net/checksum.h> | 42 | #include <net/checksum.h> |
43 | #include <net/dsfield.h> | 43 | #include <net/dsfield.h> |
@@ -108,15 +108,6 @@ | |||
108 | fatal route to network, even if it were you who configured | 108 | fatal route to network, even if it were you who configured |
109 | fatal static route: you are innocent. :-) | 109 | fatal static route: you are innocent. :-) |
110 | 110 | ||
111 | |||
112 | |||
113 | 3. Really, ipv4/ipip.c, ipv4/ip_gre.c and ipv6/sit.c contain | ||
114 | practically identical code. It would be good to glue them | ||
115 | together, but it is not very evident, how to make them modular. | ||
116 | sit is integral part of IPv6, ipip and gre are naturally modular. | ||
117 | We could extract common parts (hash table, ioctl etc) | ||
118 | to a separate module (ip_tunnel.c). | ||
119 | |||
120 | Alexey Kuznetsov. | 111 | Alexey Kuznetsov. |
121 | */ | 112 | */ |
122 | 113 | ||
@@ -126,400 +117,135 @@ MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN"); | |||
126 | 117 | ||
127 | static struct rtnl_link_ops ipgre_link_ops __read_mostly; | 118 | static struct rtnl_link_ops ipgre_link_ops __read_mostly; |
128 | static int ipgre_tunnel_init(struct net_device *dev); | 119 | static int ipgre_tunnel_init(struct net_device *dev); |
129 | static void ipgre_tunnel_setup(struct net_device *dev); | ||
130 | static int ipgre_tunnel_bind_dev(struct net_device *dev); | ||
131 | |||
132 | /* Fallback tunnel: no source, no destination, no key, no options */ | ||
133 | |||
134 | #define HASH_SIZE 16 | ||
135 | 120 | ||
136 | static int ipgre_net_id __read_mostly; | 121 | static int ipgre_net_id __read_mostly; |
137 | struct ipgre_net { | 122 | static int gre_tap_net_id __read_mostly; |
138 | struct ip_tunnel __rcu *tunnels[4][HASH_SIZE]; | ||
139 | |||
140 | struct net_device *fb_tunnel_dev; | ||
141 | }; | ||
142 | |||
143 | /* Tunnel hash table */ | ||
144 | |||
145 | /* | ||
146 | 4 hash tables: | ||
147 | |||
148 | 3: (remote,local) | ||
149 | 2: (remote,*) | ||
150 | 1: (*,local) | ||
151 | 0: (*,*) | ||
152 | 123 | ||
153 | We require exact key match i.e. if a key is present in packet | 124 | static __sum16 check_checksum(struct sk_buff *skb) |
154 | it will match only tunnel with the same key; if it is not present, | 125 | { |
155 | it will match only keyless tunnel. | 126 | __sum16 csum = 0; |
156 | |||
157 | All keysless packets, if not matched configured keyless tunnels | ||
158 | will match fallback tunnel. | ||
159 | */ | ||
160 | 127 | ||
161 | #define HASH(addr) (((__force u32)addr^((__force u32)addr>>4))&0xF) | 128 | switch (skb->ip_summed) { |
129 | case CHECKSUM_COMPLETE: | ||
130 | csum = csum_fold(skb->csum); | ||
162 | 131 | ||
163 | #define tunnels_r_l tunnels[3] | 132 | if (!csum) |
164 | #define tunnels_r tunnels[2] | 133 | break; |
165 | #define tunnels_l tunnels[1] | 134 | /* Fall through. */ |
166 | #define tunnels_wc tunnels[0] | ||
167 | 135 | ||
168 | static struct rtnl_link_stats64 *ipgre_get_stats64(struct net_device *dev, | 136 | case CHECKSUM_NONE: |
169 | struct rtnl_link_stats64 *tot) | 137 | skb->csum = 0; |
170 | { | 138 | csum = __skb_checksum_complete(skb); |
171 | int i; | 139 | skb->ip_summed = CHECKSUM_COMPLETE; |
172 | 140 | break; | |
173 | for_each_possible_cpu(i) { | ||
174 | const struct pcpu_tstats *tstats = per_cpu_ptr(dev->tstats, i); | ||
175 | u64 rx_packets, rx_bytes, tx_packets, tx_bytes; | ||
176 | unsigned int start; | ||
177 | |||
178 | do { | ||
179 | start = u64_stats_fetch_begin_bh(&tstats->syncp); | ||
180 | rx_packets = tstats->rx_packets; | ||
181 | tx_packets = tstats->tx_packets; | ||
182 | rx_bytes = tstats->rx_bytes; | ||
183 | tx_bytes = tstats->tx_bytes; | ||
184 | } while (u64_stats_fetch_retry_bh(&tstats->syncp, start)); | ||
185 | |||
186 | tot->rx_packets += rx_packets; | ||
187 | tot->tx_packets += tx_packets; | ||
188 | tot->rx_bytes += rx_bytes; | ||
189 | tot->tx_bytes += tx_bytes; | ||
190 | } | 141 | } |
191 | 142 | ||
192 | tot->multicast = dev->stats.multicast; | 143 | return csum; |
193 | tot->rx_crc_errors = dev->stats.rx_crc_errors; | ||
194 | tot->rx_fifo_errors = dev->stats.rx_fifo_errors; | ||
195 | tot->rx_length_errors = dev->stats.rx_length_errors; | ||
196 | tot->rx_frame_errors = dev->stats.rx_frame_errors; | ||
197 | tot->rx_errors = dev->stats.rx_errors; | ||
198 | |||
199 | tot->tx_fifo_errors = dev->stats.tx_fifo_errors; | ||
200 | tot->tx_carrier_errors = dev->stats.tx_carrier_errors; | ||
201 | tot->tx_dropped = dev->stats.tx_dropped; | ||
202 | tot->tx_aborted_errors = dev->stats.tx_aborted_errors; | ||
203 | tot->tx_errors = dev->stats.tx_errors; | ||
204 | |||
205 | return tot; | ||
206 | } | 144 | } |
207 | 145 | ||
208 | /* Does key in tunnel parameters match packet */ | 146 | static int ip_gre_calc_hlen(__be16 o_flags) |
209 | static bool ipgre_key_match(const struct ip_tunnel_parm *p, | ||
210 | __be16 flags, __be32 key) | ||
211 | { | 147 | { |
212 | if (p->i_flags & GRE_KEY) { | 148 | int addend = 4; |
213 | if (flags & GRE_KEY) | ||
214 | return key == p->i_key; | ||
215 | else | ||
216 | return false; /* key expected, none present */ | ||
217 | } else | ||
218 | return !(flags & GRE_KEY); | ||
219 | } | ||
220 | 149 | ||
221 | /* Given src, dst and key, find appropriate for input tunnel. */ | 150 | if (o_flags&TUNNEL_CSUM) |
151 | addend += 4; | ||
152 | if (o_flags&TUNNEL_KEY) | ||
153 | addend += 4; | ||
154 | if (o_flags&TUNNEL_SEQ) | ||
155 | addend += 4; | ||
156 | return addend; | ||
157 | } | ||
222 | 158 | ||
223 | static struct ip_tunnel *ipgre_tunnel_lookup(struct net_device *dev, | 159 | static int parse_gre_header(struct sk_buff *skb, struct tnl_ptk_info *tpi, |
224 | __be32 remote, __be32 local, | 160 | bool *csum_err, int *hdr_len) |
225 | __be16 flags, __be32 key, | ||
226 | __be16 gre_proto) | ||
227 | { | 161 | { |
228 | struct net *net = dev_net(dev); | 162 | struct iphdr *iph = ip_hdr(skb); |
229 | int link = dev->ifindex; | 163 | struct gre_base_hdr *greh; |
230 | unsigned int h0 = HASH(remote); | 164 | __be32 *options; |
231 | unsigned int h1 = HASH(key); | ||
232 | struct ip_tunnel *t, *cand = NULL; | ||
233 | struct ipgre_net *ign = net_generic(net, ipgre_net_id); | ||
234 | int dev_type = (gre_proto == htons(ETH_P_TEB)) ? | ||
235 | ARPHRD_ETHER : ARPHRD_IPGRE; | ||
236 | int score, cand_score = 4; | ||
237 | |||
238 | for_each_ip_tunnel_rcu(t, ign->tunnels_r_l[h0 ^ h1]) { | ||
239 | if (local != t->parms.iph.saddr || | ||
240 | remote != t->parms.iph.daddr || | ||
241 | !(t->dev->flags & IFF_UP)) | ||
242 | continue; | ||
243 | |||
244 | if (!ipgre_key_match(&t->parms, flags, key)) | ||
245 | continue; | ||
246 | |||
247 | if (t->dev->type != ARPHRD_IPGRE && | ||
248 | t->dev->type != dev_type) | ||
249 | continue; | ||
250 | |||
251 | score = 0; | ||
252 | if (t->parms.link != link) | ||
253 | score |= 1; | ||
254 | if (t->dev->type != dev_type) | ||
255 | score |= 2; | ||
256 | if (score == 0) | ||
257 | return t; | ||
258 | |||
259 | if (score < cand_score) { | ||
260 | cand = t; | ||
261 | cand_score = score; | ||
262 | } | ||
263 | } | ||
264 | |||
265 | for_each_ip_tunnel_rcu(t, ign->tunnels_r[h0 ^ h1]) { | ||
266 | if (remote != t->parms.iph.daddr || | ||
267 | !(t->dev->flags & IFF_UP)) | ||
268 | continue; | ||
269 | |||
270 | if (!ipgre_key_match(&t->parms, flags, key)) | ||
271 | continue; | ||
272 | |||
273 | if (t->dev->type != ARPHRD_IPGRE && | ||
274 | t->dev->type != dev_type) | ||
275 | continue; | ||
276 | |||
277 | score = 0; | ||
278 | if (t->parms.link != link) | ||
279 | score |= 1; | ||
280 | if (t->dev->type != dev_type) | ||
281 | score |= 2; | ||
282 | if (score == 0) | ||
283 | return t; | ||
284 | |||
285 | if (score < cand_score) { | ||
286 | cand = t; | ||
287 | cand_score = score; | ||
288 | } | ||
289 | } | ||
290 | 165 | ||
291 | for_each_ip_tunnel_rcu(t, ign->tunnels_l[h1]) { | 166 | if (unlikely(!pskb_may_pull(skb, sizeof(struct gre_base_hdr)))) |
292 | if ((local != t->parms.iph.saddr && | 167 | return -EINVAL; |
293 | (local != t->parms.iph.daddr || | ||
294 | !ipv4_is_multicast(local))) || | ||
295 | !(t->dev->flags & IFF_UP)) | ||
296 | continue; | ||
297 | |||
298 | if (!ipgre_key_match(&t->parms, flags, key)) | ||
299 | continue; | ||
300 | |||
301 | if (t->dev->type != ARPHRD_IPGRE && | ||
302 | t->dev->type != dev_type) | ||
303 | continue; | ||
304 | |||
305 | score = 0; | ||
306 | if (t->parms.link != link) | ||
307 | score |= 1; | ||
308 | if (t->dev->type != dev_type) | ||
309 | score |= 2; | ||
310 | if (score == 0) | ||
311 | return t; | ||
312 | |||
313 | if (score < cand_score) { | ||
314 | cand = t; | ||
315 | cand_score = score; | ||
316 | } | ||
317 | } | ||
318 | 168 | ||
319 | for_each_ip_tunnel_rcu(t, ign->tunnels_wc[h1]) { | 169 | greh = (struct gre_base_hdr *)((u8 *)iph + (iph->ihl << 2)); |
320 | if (t->parms.i_key != key || | 170 | if (unlikely(greh->flags & (GRE_VERSION | GRE_ROUTING))) |
321 | !(t->dev->flags & IFF_UP)) | 171 | return -EINVAL; |
322 | continue; | ||
323 | |||
324 | if (t->dev->type != ARPHRD_IPGRE && | ||
325 | t->dev->type != dev_type) | ||
326 | continue; | ||
327 | |||
328 | score = 0; | ||
329 | if (t->parms.link != link) | ||
330 | score |= 1; | ||
331 | if (t->dev->type != dev_type) | ||
332 | score |= 2; | ||
333 | if (score == 0) | ||
334 | return t; | ||
335 | |||
336 | if (score < cand_score) { | ||
337 | cand = t; | ||
338 | cand_score = score; | ||
339 | } | ||
340 | } | ||
341 | 172 | ||
342 | if (cand != NULL) | 173 | tpi->flags = gre_flags_to_tnl_flags(greh->flags); |
343 | return cand; | 174 | *hdr_len = ip_gre_calc_hlen(tpi->flags); |
344 | 175 | ||
345 | dev = ign->fb_tunnel_dev; | 176 | if (!pskb_may_pull(skb, *hdr_len)) |
346 | if (dev->flags & IFF_UP) | 177 | return -EINVAL; |
347 | return netdev_priv(dev); | ||
348 | 178 | ||
349 | return NULL; | 179 | tpi->proto = greh->protocol; |
350 | } | ||
351 | 180 | ||
352 | static struct ip_tunnel __rcu **__ipgre_bucket(struct ipgre_net *ign, | 181 | options = (__be32 *)(greh + 1); |
353 | struct ip_tunnel_parm *parms) | 182 | if (greh->flags & GRE_CSUM) { |
354 | { | 183 | if (check_checksum(skb)) { |
355 | __be32 remote = parms->iph.daddr; | 184 | *csum_err = true; |
356 | __be32 local = parms->iph.saddr; | 185 | return -EINVAL; |
357 | __be32 key = parms->i_key; | 186 | } |
358 | unsigned int h = HASH(key); | 187 | options++; |
359 | int prio = 0; | ||
360 | |||
361 | if (local) | ||
362 | prio |= 1; | ||
363 | if (remote && !ipv4_is_multicast(remote)) { | ||
364 | prio |= 2; | ||
365 | h ^= HASH(remote); | ||
366 | } | 188 | } |
367 | 189 | ||
368 | return &ign->tunnels[prio][h]; | 190 | if (greh->flags & GRE_KEY) { |
369 | } | 191 | tpi->key = *options; |
370 | 192 | options++; | |
371 | static inline struct ip_tunnel __rcu **ipgre_bucket(struct ipgre_net *ign, | 193 | } else |
372 | struct ip_tunnel *t) | 194 | tpi->key = 0; |
373 | { | ||
374 | return __ipgre_bucket(ign, &t->parms); | ||
375 | } | ||
376 | |||
377 | static void ipgre_tunnel_link(struct ipgre_net *ign, struct ip_tunnel *t) | ||
378 | { | ||
379 | struct ip_tunnel __rcu **tp = ipgre_bucket(ign, t); | ||
380 | 195 | ||
381 | rcu_assign_pointer(t->next, rtnl_dereference(*tp)); | 196 | if (unlikely(greh->flags & GRE_SEQ)) { |
382 | rcu_assign_pointer(*tp, t); | 197 | tpi->seq = *options; |
383 | } | 198 | options++; |
199 | } else | ||
200 | tpi->seq = 0; | ||
384 | 201 | ||
385 | static void ipgre_tunnel_unlink(struct ipgre_net *ign, struct ip_tunnel *t) | 202 | /* WCCP version 1 and 2 protocol decoding. |
386 | { | 203 | * - Change protocol to IP |
387 | struct ip_tunnel __rcu **tp; | 204 | * - When dealing with WCCPv2, Skip extra 4 bytes in GRE header |
388 | struct ip_tunnel *iter; | 205 | */ |
389 | 206 | if (greh->flags == 0 && tpi->proto == htons(ETH_P_WCCP)) { | |
390 | for (tp = ipgre_bucket(ign, t); | 207 | tpi->proto = htons(ETH_P_IP); |
391 | (iter = rtnl_dereference(*tp)) != NULL; | 208 | if ((*(u8 *)options & 0xF0) != 0x40) { |
392 | tp = &iter->next) { | 209 | *hdr_len += 4; |
393 | if (t == iter) { | 210 | if (!pskb_may_pull(skb, *hdr_len)) |
394 | rcu_assign_pointer(*tp, t->next); | 211 | return -EINVAL; |
395 | break; | ||
396 | } | 212 | } |
397 | } | 213 | } |
398 | } | ||
399 | |||
400 | static struct ip_tunnel *ipgre_tunnel_find(struct net *net, | ||
401 | struct ip_tunnel_parm *parms, | ||
402 | int type) | ||
403 | { | ||
404 | __be32 remote = parms->iph.daddr; | ||
405 | __be32 local = parms->iph.saddr; | ||
406 | __be32 key = parms->i_key; | ||
407 | int link = parms->link; | ||
408 | struct ip_tunnel *t; | ||
409 | struct ip_tunnel __rcu **tp; | ||
410 | struct ipgre_net *ign = net_generic(net, ipgre_net_id); | ||
411 | |||
412 | for (tp = __ipgre_bucket(ign, parms); | ||
413 | (t = rtnl_dereference(*tp)) != NULL; | ||
414 | tp = &t->next) | ||
415 | if (local == t->parms.iph.saddr && | ||
416 | remote == t->parms.iph.daddr && | ||
417 | key == t->parms.i_key && | ||
418 | link == t->parms.link && | ||
419 | type == t->dev->type) | ||
420 | break; | ||
421 | |||
422 | return t; | ||
423 | } | ||
424 | |||
425 | static struct ip_tunnel *ipgre_tunnel_locate(struct net *net, | ||
426 | struct ip_tunnel_parm *parms, int create) | ||
427 | { | ||
428 | struct ip_tunnel *t, *nt; | ||
429 | struct net_device *dev; | ||
430 | char name[IFNAMSIZ]; | ||
431 | struct ipgre_net *ign = net_generic(net, ipgre_net_id); | ||
432 | |||
433 | t = ipgre_tunnel_find(net, parms, ARPHRD_IPGRE); | ||
434 | if (t || !create) | ||
435 | return t; | ||
436 | |||
437 | if (parms->name[0]) | ||
438 | strlcpy(name, parms->name, IFNAMSIZ); | ||
439 | else | ||
440 | strcpy(name, "gre%d"); | ||
441 | |||
442 | dev = alloc_netdev(sizeof(*t), name, ipgre_tunnel_setup); | ||
443 | if (!dev) | ||
444 | return NULL; | ||
445 | |||
446 | dev_net_set(dev, net); | ||
447 | |||
448 | nt = netdev_priv(dev); | ||
449 | nt->parms = *parms; | ||
450 | dev->rtnl_link_ops = &ipgre_link_ops; | ||
451 | 214 | ||
452 | dev->mtu = ipgre_tunnel_bind_dev(dev); | 215 | return 0; |
453 | |||
454 | if (register_netdevice(dev) < 0) | ||
455 | goto failed_free; | ||
456 | |||
457 | /* Can use a lockless transmit, unless we generate output sequences */ | ||
458 | if (!(nt->parms.o_flags & GRE_SEQ)) | ||
459 | dev->features |= NETIF_F_LLTX; | ||
460 | |||
461 | dev_hold(dev); | ||
462 | ipgre_tunnel_link(ign, nt); | ||
463 | return nt; | ||
464 | |||
465 | failed_free: | ||
466 | free_netdev(dev); | ||
467 | return NULL; | ||
468 | } | ||
469 | |||
470 | static void ipgre_tunnel_uninit(struct net_device *dev) | ||
471 | { | ||
472 | struct net *net = dev_net(dev); | ||
473 | struct ipgre_net *ign = net_generic(net, ipgre_net_id); | ||
474 | |||
475 | ipgre_tunnel_unlink(ign, netdev_priv(dev)); | ||
476 | dev_put(dev); | ||
477 | } | 216 | } |
478 | 217 | ||
479 | |||
480 | static void ipgre_err(struct sk_buff *skb, u32 info) | 218 | static void ipgre_err(struct sk_buff *skb, u32 info) |
481 | { | 219 | { |
482 | 220 | ||
483 | /* All the routers (except for Linux) return only | 221 | /* All the routers (except for Linux) return only |
484 | 8 bytes of packet payload. It means, that precise relaying of | 222 | 8 bytes of packet payload. It means, that precise relaying of |
485 | ICMP in the real Internet is absolutely infeasible. | 223 | ICMP in the real Internet is absolutely infeasible. |
486 | 224 | ||
487 | Moreover, Cisco "wise men" put GRE key to the third word | 225 | Moreover, Cisco "wise men" put GRE key to the third word |
488 | in GRE header. It makes impossible maintaining even soft state for keyed | 226 | in GRE header. It makes impossible maintaining even soft |
489 | GRE tunnels with enabled checksum. Tell them "thank you". | 227 | state for keyed GRE tunnels with enabled checksum. Tell |
490 | 228 | them "thank you". | |
491 | Well, I wonder, rfc1812 was written by Cisco employee, | ||
492 | what the hell these idiots break standards established | ||
493 | by themselves??? | ||
494 | */ | ||
495 | 229 | ||
230 | Well, I wonder, rfc1812 was written by Cisco employee, | ||
231 | what the hell these idiots break standards established | ||
232 | by themselves??? | ||
233 | */ | ||
234 | struct net *net = dev_net(skb->dev); | ||
235 | struct ip_tunnel_net *itn; | ||
496 | const struct iphdr *iph = (const struct iphdr *)skb->data; | 236 | const struct iphdr *iph = (const struct iphdr *)skb->data; |
497 | __be16 *p = (__be16 *)(skb->data+(iph->ihl<<2)); | ||
498 | int grehlen = (iph->ihl<<2) + 4; | ||
499 | const int type = icmp_hdr(skb)->type; | 237 | const int type = icmp_hdr(skb)->type; |
500 | const int code = icmp_hdr(skb)->code; | 238 | const int code = icmp_hdr(skb)->code; |
501 | struct ip_tunnel *t; | 239 | struct ip_tunnel *t; |
502 | __be16 flags; | 240 | struct tnl_ptk_info tpi; |
503 | __be32 key = 0; | 241 | int hdr_len; |
242 | bool csum_err = false; | ||
504 | 243 | ||
505 | flags = p[0]; | 244 | if (parse_gre_header(skb, &tpi, &csum_err, &hdr_len)) { |
506 | if (flags&(GRE_CSUM|GRE_KEY|GRE_SEQ|GRE_ROUTING|GRE_VERSION)) { | 245 | if (!csum_err) /* ignore csum errors. */ |
507 | if (flags&(GRE_VERSION|GRE_ROUTING)) | ||
508 | return; | 246 | return; |
509 | if (flags&GRE_KEY) { | ||
510 | grehlen += 4; | ||
511 | if (flags&GRE_CSUM) | ||
512 | grehlen += 4; | ||
513 | } | ||
514 | } | 247 | } |
515 | 248 | ||
516 | /* If only 8 bytes returned, keyed message will be dropped here */ | ||
517 | if (skb_headlen(skb) < grehlen) | ||
518 | return; | ||
519 | |||
520 | if (flags & GRE_KEY) | ||
521 | key = *(((__be32 *)p) + (grehlen / 4) - 1); | ||
522 | |||
523 | switch (type) { | 249 | switch (type) { |
524 | default: | 250 | default: |
525 | case ICMP_PARAMETERPROB: | 251 | case ICMP_PARAMETERPROB: |
@@ -548,8 +274,13 @@ static void ipgre_err(struct sk_buff *skb, u32 info) | |||
548 | break; | 274 | break; |
549 | } | 275 | } |
550 | 276 | ||
551 | t = ipgre_tunnel_lookup(skb->dev, iph->daddr, iph->saddr, | 277 | if (tpi.proto == htons(ETH_P_TEB)) |
552 | flags, key, p[1]); | 278 | itn = net_generic(net, gre_tap_net_id); |
279 | else | ||
280 | itn = net_generic(net, ipgre_net_id); | ||
281 | |||
282 | t = ip_tunnel_lookup(itn, skb->dev->ifindex, tpi.flags, | ||
283 | iph->daddr, iph->saddr, tpi.key); | ||
553 | 284 | ||
554 | if (t == NULL) | 285 | if (t == NULL) |
555 | return; | 286 | return; |
@@ -578,158 +309,33 @@ static void ipgre_err(struct sk_buff *skb, u32 info) | |||
578 | t->err_time = jiffies; | 309 | t->err_time = jiffies; |
579 | } | 310 | } |
580 | 311 | ||
581 | static inline u8 | ||
582 | ipgre_ecn_encapsulate(u8 tos, const struct iphdr *old_iph, struct sk_buff *skb) | ||
583 | { | ||
584 | u8 inner = 0; | ||
585 | if (skb->protocol == htons(ETH_P_IP)) | ||
586 | inner = old_iph->tos; | ||
587 | else if (skb->protocol == htons(ETH_P_IPV6)) | ||
588 | inner = ipv6_get_dsfield((const struct ipv6hdr *)old_iph); | ||
589 | return INET_ECN_encapsulate(tos, inner); | ||
590 | } | ||
591 | |||
592 | static int ipgre_rcv(struct sk_buff *skb) | 312 | static int ipgre_rcv(struct sk_buff *skb) |
593 | { | 313 | { |
314 | struct net *net = dev_net(skb->dev); | ||
315 | struct ip_tunnel_net *itn; | ||
594 | const struct iphdr *iph; | 316 | const struct iphdr *iph; |
595 | u8 *h; | ||
596 | __be16 flags; | ||
597 | __sum16 csum = 0; | ||
598 | __be32 key = 0; | ||
599 | u32 seqno = 0; | ||
600 | struct ip_tunnel *tunnel; | 317 | struct ip_tunnel *tunnel; |
601 | int offset = 4; | 318 | struct tnl_ptk_info tpi; |
602 | __be16 gre_proto; | 319 | int hdr_len; |
603 | int err; | 320 | bool csum_err = false; |
604 | 321 | ||
605 | if (!pskb_may_pull(skb, 16)) | 322 | if (parse_gre_header(skb, &tpi, &csum_err, &hdr_len) < 0) |
606 | goto drop; | 323 | goto drop; |
607 | 324 | ||
608 | iph = ip_hdr(skb); | 325 | if (tpi.proto == htons(ETH_P_TEB)) |
609 | h = skb->data; | 326 | itn = net_generic(net, gre_tap_net_id); |
610 | flags = *(__be16 *)h; | 327 | else |
611 | 328 | itn = net_generic(net, ipgre_net_id); | |
612 | if (flags&(GRE_CSUM|GRE_KEY|GRE_ROUTING|GRE_SEQ|GRE_VERSION)) { | ||
613 | /* - Version must be 0. | ||
614 | - We do not support routing headers. | ||
615 | */ | ||
616 | if (flags&(GRE_VERSION|GRE_ROUTING)) | ||
617 | goto drop; | ||
618 | |||
619 | if (flags&GRE_CSUM) { | ||
620 | switch (skb->ip_summed) { | ||
621 | case CHECKSUM_COMPLETE: | ||
622 | csum = csum_fold(skb->csum); | ||
623 | if (!csum) | ||
624 | break; | ||
625 | /* fall through */ | ||
626 | case CHECKSUM_NONE: | ||
627 | skb->csum = 0; | ||
628 | csum = __skb_checksum_complete(skb); | ||
629 | skb->ip_summed = CHECKSUM_COMPLETE; | ||
630 | } | ||
631 | offset += 4; | ||
632 | } | ||
633 | if (flags&GRE_KEY) { | ||
634 | key = *(__be32 *)(h + offset); | ||
635 | offset += 4; | ||
636 | } | ||
637 | if (flags&GRE_SEQ) { | ||
638 | seqno = ntohl(*(__be32 *)(h + offset)); | ||
639 | offset += 4; | ||
640 | } | ||
641 | } | ||
642 | 329 | ||
643 | gre_proto = *(__be16 *)(h + 2); | 330 | iph = ip_hdr(skb); |
331 | tunnel = ip_tunnel_lookup(itn, skb->dev->ifindex, tpi.flags, | ||
332 | iph->saddr, iph->daddr, tpi.key); | ||
644 | 333 | ||
645 | tunnel = ipgre_tunnel_lookup(skb->dev, | ||
646 | iph->saddr, iph->daddr, flags, key, | ||
647 | gre_proto); | ||
648 | if (tunnel) { | 334 | if (tunnel) { |
649 | struct pcpu_tstats *tstats; | 335 | ip_tunnel_rcv(tunnel, skb, &tpi, log_ecn_error); |
650 | |||
651 | secpath_reset(skb); | ||
652 | |||
653 | skb->protocol = gre_proto; | ||
654 | /* WCCP version 1 and 2 protocol decoding. | ||
655 | * - Change protocol to IP | ||
656 | * - When dealing with WCCPv2, Skip extra 4 bytes in GRE header | ||
657 | */ | ||
658 | if (flags == 0 && gre_proto == htons(ETH_P_WCCP)) { | ||
659 | skb->protocol = htons(ETH_P_IP); | ||
660 | if ((*(h + offset) & 0xF0) != 0x40) | ||
661 | offset += 4; | ||
662 | } | ||
663 | |||
664 | skb->mac_header = skb->network_header; | ||
665 | __pskb_pull(skb, offset); | ||
666 | skb_postpull_rcsum(skb, skb_transport_header(skb), offset); | ||
667 | skb->pkt_type = PACKET_HOST; | ||
668 | #ifdef CONFIG_NET_IPGRE_BROADCAST | ||
669 | if (ipv4_is_multicast(iph->daddr)) { | ||
670 | /* Looped back packet, drop it! */ | ||
671 | if (rt_is_output_route(skb_rtable(skb))) | ||
672 | goto drop; | ||
673 | tunnel->dev->stats.multicast++; | ||
674 | skb->pkt_type = PACKET_BROADCAST; | ||
675 | } | ||
676 | #endif | ||
677 | |||
678 | if (((flags&GRE_CSUM) && csum) || | ||
679 | (!(flags&GRE_CSUM) && tunnel->parms.i_flags&GRE_CSUM)) { | ||
680 | tunnel->dev->stats.rx_crc_errors++; | ||
681 | tunnel->dev->stats.rx_errors++; | ||
682 | goto drop; | ||
683 | } | ||
684 | if (tunnel->parms.i_flags&GRE_SEQ) { | ||
685 | if (!(flags&GRE_SEQ) || | ||
686 | (tunnel->i_seqno && (s32)(seqno - tunnel->i_seqno) < 0)) { | ||
687 | tunnel->dev->stats.rx_fifo_errors++; | ||
688 | tunnel->dev->stats.rx_errors++; | ||
689 | goto drop; | ||
690 | } | ||
691 | tunnel->i_seqno = seqno + 1; | ||
692 | } | ||
693 | |||
694 | /* Warning: All skb pointers will be invalidated! */ | ||
695 | if (tunnel->dev->type == ARPHRD_ETHER) { | ||
696 | if (!pskb_may_pull(skb, ETH_HLEN)) { | ||
697 | tunnel->dev->stats.rx_length_errors++; | ||
698 | tunnel->dev->stats.rx_errors++; | ||
699 | goto drop; | ||
700 | } | ||
701 | |||
702 | iph = ip_hdr(skb); | ||
703 | skb->protocol = eth_type_trans(skb, tunnel->dev); | ||
704 | skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN); | ||
705 | } | ||
706 | |||
707 | __skb_tunnel_rx(skb, tunnel->dev); | ||
708 | |||
709 | skb_reset_network_header(skb); | ||
710 | err = IP_ECN_decapsulate(iph, skb); | ||
711 | if (unlikely(err)) { | ||
712 | if (log_ecn_error) | ||
713 | net_info_ratelimited("non-ECT from %pI4 with TOS=%#x\n", | ||
714 | &iph->saddr, iph->tos); | ||
715 | if (err > 1) { | ||
716 | ++tunnel->dev->stats.rx_frame_errors; | ||
717 | ++tunnel->dev->stats.rx_errors; | ||
718 | goto drop; | ||
719 | } | ||
720 | } | ||
721 | |||
722 | tstats = this_cpu_ptr(tunnel->dev->tstats); | ||
723 | u64_stats_update_begin(&tstats->syncp); | ||
724 | tstats->rx_packets++; | ||
725 | tstats->rx_bytes += skb->len; | ||
726 | u64_stats_update_end(&tstats->syncp); | ||
727 | |||
728 | gro_cells_receive(&tunnel->gro_cells, skb); | ||
729 | return 0; | 336 | return 0; |
730 | } | 337 | } |
731 | icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0); | 338 | icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0); |
732 | |||
733 | drop: | 339 | drop: |
734 | kfree_skb(skb); | 340 | kfree_skb(skb); |
735 | return 0; | 341 | return 0; |
@@ -746,7 +352,7 @@ static struct sk_buff *handle_offloads(struct ip_tunnel *tunnel, struct sk_buff | |||
746 | skb_shinfo(skb)->gso_type |= SKB_GSO_GRE; | 352 | skb_shinfo(skb)->gso_type |= SKB_GSO_GRE; |
747 | return skb; | 353 | return skb; |
748 | } else if (skb->ip_summed == CHECKSUM_PARTIAL && | 354 | } else if (skb->ip_summed == CHECKSUM_PARTIAL && |
749 | tunnel->parms.o_flags&GRE_CSUM) { | 355 | tunnel->parms.o_flags&TUNNEL_CSUM) { |
750 | err = skb_checksum_help(skb); | 356 | err = skb_checksum_help(skb); |
751 | if (unlikely(err)) | 357 | if (unlikely(err)) |
752 | goto error; | 358 | goto error; |
@@ -760,494 +366,157 @@ error: | |||
760 | return ERR_PTR(err); | 366 | return ERR_PTR(err); |
761 | } | 367 | } |
762 | 368 | ||
763 | static netdev_tx_t ipgre_tunnel_xmit(struct sk_buff *skb, struct net_device *dev) | 369 | static struct sk_buff *gre_build_header(struct sk_buff *skb, |
370 | const struct tnl_ptk_info *tpi, | ||
371 | int hdr_len) | ||
764 | { | 372 | { |
765 | struct pcpu_tstats *tstats = this_cpu_ptr(dev->tstats); | 373 | struct gre_base_hdr *greh; |
766 | struct ip_tunnel *tunnel = netdev_priv(dev); | ||
767 | const struct iphdr *old_iph; | ||
768 | const struct iphdr *tiph; | ||
769 | struct flowi4 fl4; | ||
770 | u8 tos; | ||
771 | __be16 df; | ||
772 | struct rtable *rt; /* Route to the other host */ | ||
773 | struct net_device *tdev; /* Device to other host */ | ||
774 | struct iphdr *iph; /* Our new IP header */ | ||
775 | unsigned int max_headroom; /* The extra header space needed */ | ||
776 | int gre_hlen; | ||
777 | __be32 dst; | ||
778 | int mtu; | ||
779 | u8 ttl; | ||
780 | int err; | ||
781 | int pkt_len; | ||
782 | |||
783 | skb = handle_offloads(tunnel, skb); | ||
784 | if (IS_ERR(skb)) { | ||
785 | dev->stats.tx_dropped++; | ||
786 | return NETDEV_TX_OK; | ||
787 | } | ||
788 | 374 | ||
789 | if (!skb->encapsulation) { | 375 | skb_push(skb, hdr_len); |
790 | skb_reset_inner_headers(skb); | ||
791 | skb->encapsulation = 1; | ||
792 | } | ||
793 | 376 | ||
794 | old_iph = ip_hdr(skb); | 377 | greh = (struct gre_base_hdr *)skb->data; |
378 | greh->flags = tnl_flags_to_gre_flags(tpi->flags); | ||
379 | greh->protocol = tpi->proto; | ||
795 | 380 | ||
796 | if (dev->type == ARPHRD_ETHER) | 381 | if (tpi->flags&(TUNNEL_KEY|TUNNEL_CSUM|TUNNEL_SEQ)) { |
797 | IPCB(skb)->flags = 0; | 382 | __be32 *ptr = (__be32 *)(((u8 *)greh) + hdr_len - 4); |
798 | 383 | ||
799 | if (dev->header_ops && dev->type == ARPHRD_IPGRE) { | 384 | if (tpi->flags&TUNNEL_SEQ) { |
800 | gre_hlen = 0; | 385 | *ptr = tpi->seq; |
801 | tiph = (const struct iphdr *)skb->data; | 386 | ptr--; |
802 | } else { | ||
803 | gre_hlen = tunnel->hlen; | ||
804 | tiph = &tunnel->parms.iph; | ||
805 | } | ||
806 | |||
807 | if ((dst = tiph->daddr) == 0) { | ||
808 | /* NBMA tunnel */ | ||
809 | |||
810 | if (skb_dst(skb) == NULL) { | ||
811 | dev->stats.tx_fifo_errors++; | ||
812 | goto tx_error; | ||
813 | } | 387 | } |
814 | 388 | if (tpi->flags&TUNNEL_KEY) { | |
815 | if (skb->protocol == htons(ETH_P_IP)) { | 389 | *ptr = tpi->key; |
816 | rt = skb_rtable(skb); | 390 | ptr--; |
817 | dst = rt_nexthop(rt, old_iph->daddr); | ||
818 | } | 391 | } |
819 | #if IS_ENABLED(CONFIG_IPV6) | 392 | if (tpi->flags&TUNNEL_CSUM && |
820 | else if (skb->protocol == htons(ETH_P_IPV6)) { | 393 | !(skb_shinfo(skb)->gso_type & SKB_GSO_GRE)) { |
821 | const struct in6_addr *addr6; | 394 | *(__sum16 *)ptr = 0; |
822 | struct neighbour *neigh; | 395 | *(__sum16 *)ptr = csum_fold(skb_checksum(skb, 0, |
823 | bool do_tx_error_icmp; | 396 | skb->len, 0)); |
824 | int addr_type; | ||
825 | |||
826 | neigh = dst_neigh_lookup(skb_dst(skb), &ipv6_hdr(skb)->daddr); | ||
827 | if (neigh == NULL) | ||
828 | goto tx_error; | ||
829 | |||
830 | addr6 = (const struct in6_addr *)&neigh->primary_key; | ||
831 | addr_type = ipv6_addr_type(addr6); | ||
832 | |||
833 | if (addr_type == IPV6_ADDR_ANY) { | ||
834 | addr6 = &ipv6_hdr(skb)->daddr; | ||
835 | addr_type = ipv6_addr_type(addr6); | ||
836 | } | ||
837 | |||
838 | if ((addr_type & IPV6_ADDR_COMPATv4) == 0) | ||
839 | do_tx_error_icmp = true; | ||
840 | else { | ||
841 | do_tx_error_icmp = false; | ||
842 | dst = addr6->s6_addr32[3]; | ||
843 | } | ||
844 | neigh_release(neigh); | ||
845 | if (do_tx_error_icmp) | ||
846 | goto tx_error_icmp; | ||
847 | } | 397 | } |
848 | #endif | ||
849 | else | ||
850 | goto tx_error; | ||
851 | } | 398 | } |
852 | 399 | ||
853 | ttl = tiph->ttl; | 400 | return skb; |
854 | tos = tiph->tos; | 401 | } |
855 | if (tos & 0x1) { | ||
856 | tos &= ~0x1; | ||
857 | if (skb->protocol == htons(ETH_P_IP)) | ||
858 | tos = old_iph->tos; | ||
859 | else if (skb->protocol == htons(ETH_P_IPV6)) | ||
860 | tos = ipv6_get_dsfield((const struct ipv6hdr *)old_iph); | ||
861 | } | ||
862 | 402 | ||
863 | rt = ip_route_output_gre(dev_net(dev), &fl4, dst, tiph->saddr, | 403 | static void __gre_xmit(struct sk_buff *skb, struct net_device *dev, |
864 | tunnel->parms.o_key, RT_TOS(tos), | 404 | const struct iphdr *tnl_params, |
865 | tunnel->parms.link); | 405 | __be16 proto) |
866 | if (IS_ERR(rt)) { | 406 | { |
867 | dev->stats.tx_carrier_errors++; | 407 | struct ip_tunnel *tunnel = netdev_priv(dev); |
868 | goto tx_error; | 408 | struct tnl_ptk_info tpi; |
869 | } | ||
870 | tdev = rt->dst.dev; | ||
871 | 409 | ||
872 | if (tdev == dev) { | 410 | if (likely(!skb->encapsulation)) { |
873 | ip_rt_put(rt); | 411 | skb_reset_inner_headers(skb); |
874 | dev->stats.collisions++; | 412 | skb->encapsulation = 1; |
875 | goto tx_error; | ||
876 | } | 413 | } |
877 | 414 | ||
878 | df = tiph->frag_off; | 415 | tpi.flags = tunnel->parms.o_flags; |
879 | if (df) | 416 | tpi.proto = proto; |
880 | mtu = dst_mtu(&rt->dst) - dev->hard_header_len - tunnel->hlen; | 417 | tpi.key = tunnel->parms.o_key; |
881 | else | 418 | if (tunnel->parms.o_flags & TUNNEL_SEQ) |
882 | mtu = skb_dst(skb) ? dst_mtu(skb_dst(skb)) : dev->mtu; | 419 | tunnel->o_seqno++; |
883 | 420 | tpi.seq = htonl(tunnel->o_seqno); | |
884 | if (skb_dst(skb)) | ||
885 | skb_dst(skb)->ops->update_pmtu(skb_dst(skb), NULL, skb, mtu); | ||
886 | |||
887 | if (skb->protocol == htons(ETH_P_IP)) { | ||
888 | df |= (old_iph->frag_off&htons(IP_DF)); | ||
889 | 421 | ||
890 | if (!skb_is_gso(skb) && | 422 | /* Push GRE header. */ |
891 | (old_iph->frag_off&htons(IP_DF)) && | 423 | skb = gre_build_header(skb, &tpi, tunnel->hlen); |
892 | mtu < ntohs(old_iph->tot_len)) { | 424 | if (unlikely(!skb)) { |
893 | icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED, htonl(mtu)); | 425 | dev->stats.tx_dropped++; |
894 | ip_rt_put(rt); | 426 | return; |
895 | goto tx_error; | ||
896 | } | ||
897 | } | 427 | } |
898 | #if IS_ENABLED(CONFIG_IPV6) | ||
899 | else if (skb->protocol == htons(ETH_P_IPV6)) { | ||
900 | struct rt6_info *rt6 = (struct rt6_info *)skb_dst(skb); | ||
901 | |||
902 | if (rt6 && mtu < dst_mtu(skb_dst(skb)) && mtu >= IPV6_MIN_MTU) { | ||
903 | if ((tunnel->parms.iph.daddr && | ||
904 | !ipv4_is_multicast(tunnel->parms.iph.daddr)) || | ||
905 | rt6->rt6i_dst.plen == 128) { | ||
906 | rt6->rt6i_flags |= RTF_MODIFIED; | ||
907 | dst_metric_set(skb_dst(skb), RTAX_MTU, mtu); | ||
908 | } | ||
909 | } | ||
910 | 428 | ||
911 | if (!skb_is_gso(skb) && | 429 | ip_tunnel_xmit(skb, dev, tnl_params); |
912 | mtu >= IPV6_MIN_MTU && | 430 | } |
913 | mtu < skb->len - tunnel->hlen + gre_hlen) { | ||
914 | icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu); | ||
915 | ip_rt_put(rt); | ||
916 | goto tx_error; | ||
917 | } | ||
918 | } | ||
919 | #endif | ||
920 | 431 | ||
921 | if (tunnel->err_count > 0) { | 432 | static netdev_tx_t ipgre_xmit(struct sk_buff *skb, |
922 | if (time_before(jiffies, | 433 | struct net_device *dev) |
923 | tunnel->err_time + IPTUNNEL_ERR_TIMEO)) { | 434 | { |
924 | tunnel->err_count--; | 435 | struct ip_tunnel *tunnel = netdev_priv(dev); |
436 | const struct iphdr *tnl_params; | ||
925 | 437 | ||
926 | dst_link_failure(skb); | 438 | skb = handle_offloads(tunnel, skb); |
927 | } else | 439 | if (IS_ERR(skb)) |
928 | tunnel->err_count = 0; | 440 | goto out; |
929 | } | ||
930 | 441 | ||
931 | max_headroom = LL_RESERVED_SPACE(tdev) + gre_hlen + rt->dst.header_len; | 442 | if (dev->header_ops) { |
932 | 443 | /* Need space for new headers */ | |
933 | if (skb_headroom(skb) < max_headroom || skb_shared(skb)|| | 444 | if (skb_cow_head(skb, dev->needed_headroom - |
934 | (skb_cloned(skb) && !skb_clone_writable(skb, 0))) { | 445 | (tunnel->hlen + sizeof(struct iphdr)))); |
935 | struct sk_buff *new_skb = skb_realloc_headroom(skb, max_headroom); | 446 | goto free_skb; |
936 | if (max_headroom > dev->needed_headroom) | ||
937 | dev->needed_headroom = max_headroom; | ||
938 | if (!new_skb) { | ||
939 | ip_rt_put(rt); | ||
940 | dev->stats.tx_dropped++; | ||
941 | dev_kfree_skb(skb); | ||
942 | return NETDEV_TX_OK; | ||
943 | } | ||
944 | if (skb->sk) | ||
945 | skb_set_owner_w(new_skb, skb->sk); | ||
946 | dev_kfree_skb(skb); | ||
947 | skb = new_skb; | ||
948 | old_iph = ip_hdr(skb); | ||
949 | /* Warning : tiph value might point to freed memory */ | ||
950 | } | ||
951 | 447 | ||
952 | skb_push(skb, gre_hlen); | 448 | tnl_params = (const struct iphdr *)skb->data; |
953 | skb_reset_network_header(skb); | ||
954 | skb_set_transport_header(skb, sizeof(*iph)); | ||
955 | memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt)); | ||
956 | IPCB(skb)->flags &= ~(IPSKB_XFRM_TUNNEL_SIZE | IPSKB_XFRM_TRANSFORMED | | ||
957 | IPSKB_REROUTED); | ||
958 | skb_dst_drop(skb); | ||
959 | skb_dst_set(skb, &rt->dst); | ||
960 | |||
961 | /* | ||
962 | * Push down and install the IPIP header. | ||
963 | */ | ||
964 | 449 | ||
965 | iph = ip_hdr(skb); | 450 | /* Pull skb since ip_tunnel_xmit() needs skb->data pointing |
966 | iph->version = 4; | 451 | * to gre header. |
967 | iph->ihl = sizeof(struct iphdr) >> 2; | 452 | */ |
968 | iph->frag_off = df; | 453 | skb_pull(skb, tunnel->hlen + sizeof(struct iphdr)); |
969 | iph->protocol = IPPROTO_GRE; | 454 | } else { |
970 | iph->tos = ipgre_ecn_encapsulate(tos, old_iph, skb); | 455 | if (skb_cow_head(skb, dev->needed_headroom)) |
971 | iph->daddr = fl4.daddr; | 456 | goto free_skb; |
972 | iph->saddr = fl4.saddr; | ||
973 | iph->ttl = ttl; | ||
974 | |||
975 | tunnel_ip_select_ident(skb, old_iph, &rt->dst); | ||
976 | |||
977 | if (ttl == 0) { | ||
978 | if (skb->protocol == htons(ETH_P_IP)) | ||
979 | iph->ttl = old_iph->ttl; | ||
980 | #if IS_ENABLED(CONFIG_IPV6) | ||
981 | else if (skb->protocol == htons(ETH_P_IPV6)) | ||
982 | iph->ttl = ((const struct ipv6hdr *)old_iph)->hop_limit; | ||
983 | #endif | ||
984 | else | ||
985 | iph->ttl = ip4_dst_hoplimit(&rt->dst); | ||
986 | } | ||
987 | |||
988 | ((__be16 *)(iph + 1))[0] = tunnel->parms.o_flags; | ||
989 | ((__be16 *)(iph + 1))[1] = (dev->type == ARPHRD_ETHER) ? | ||
990 | htons(ETH_P_TEB) : skb->protocol; | ||
991 | |||
992 | if (tunnel->parms.o_flags&(GRE_KEY|GRE_CSUM|GRE_SEQ)) { | ||
993 | __be32 *ptr = (__be32 *)(((u8 *)iph) + tunnel->hlen - 4); | ||
994 | 457 | ||
995 | if (tunnel->parms.o_flags&GRE_SEQ) { | 458 | tnl_params = &tunnel->parms.iph; |
996 | ++tunnel->o_seqno; | ||
997 | *ptr = htonl(tunnel->o_seqno); | ||
998 | ptr--; | ||
999 | } | ||
1000 | if (tunnel->parms.o_flags&GRE_KEY) { | ||
1001 | *ptr = tunnel->parms.o_key; | ||
1002 | ptr--; | ||
1003 | } | ||
1004 | /* Skip GRE checksum if skb is getting offloaded. */ | ||
1005 | if (!(skb_shinfo(skb)->gso_type & SKB_GSO_GRE) && | ||
1006 | (tunnel->parms.o_flags&GRE_CSUM)) { | ||
1007 | int offset = skb_transport_offset(skb); | ||
1008 | |||
1009 | if (skb_has_shared_frag(skb)) { | ||
1010 | err = __skb_linearize(skb); | ||
1011 | if (err) | ||
1012 | goto tx_error; | ||
1013 | } | ||
1014 | |||
1015 | *ptr = 0; | ||
1016 | *(__sum16 *)ptr = csum_fold(skb_checksum(skb, offset, | ||
1017 | skb->len - offset, | ||
1018 | 0)); | ||
1019 | } | ||
1020 | } | 459 | } |
1021 | 460 | ||
1022 | nf_reset(skb); | 461 | __gre_xmit(skb, dev, tnl_params, skb->protocol); |
1023 | 462 | ||
1024 | pkt_len = skb->len - skb_transport_offset(skb); | ||
1025 | err = ip_local_out(skb); | ||
1026 | if (likely(net_xmit_eval(err) == 0)) { | ||
1027 | u64_stats_update_begin(&tstats->syncp); | ||
1028 | tstats->tx_bytes += pkt_len; | ||
1029 | tstats->tx_packets++; | ||
1030 | u64_stats_update_end(&tstats->syncp); | ||
1031 | } else { | ||
1032 | dev->stats.tx_errors++; | ||
1033 | dev->stats.tx_aborted_errors++; | ||
1034 | } | ||
1035 | return NETDEV_TX_OK; | 463 | return NETDEV_TX_OK; |
1036 | 464 | ||
1037 | #if IS_ENABLED(CONFIG_IPV6) | 465 | free_skb: |
1038 | tx_error_icmp: | ||
1039 | dst_link_failure(skb); | ||
1040 | #endif | ||
1041 | tx_error: | ||
1042 | dev->stats.tx_errors++; | ||
1043 | dev_kfree_skb(skb); | 466 | dev_kfree_skb(skb); |
467 | out: | ||
468 | dev->stats.tx_dropped++; | ||
1044 | return NETDEV_TX_OK; | 469 | return NETDEV_TX_OK; |
1045 | } | 470 | } |
1046 | 471 | ||
1047 | static int ipgre_tunnel_bind_dev(struct net_device *dev) | 472 | static netdev_tx_t gre_tap_xmit(struct sk_buff *skb, |
473 | struct net_device *dev) | ||
1048 | { | 474 | { |
1049 | struct net_device *tdev = NULL; | 475 | struct ip_tunnel *tunnel = netdev_priv(dev); |
1050 | struct ip_tunnel *tunnel; | ||
1051 | const struct iphdr *iph; | ||
1052 | int hlen = LL_MAX_HEADER; | ||
1053 | int mtu = ETH_DATA_LEN; | ||
1054 | int addend = sizeof(struct iphdr) + 4; | ||
1055 | |||
1056 | tunnel = netdev_priv(dev); | ||
1057 | iph = &tunnel->parms.iph; | ||
1058 | |||
1059 | /* Guess output device to choose reasonable mtu and needed_headroom */ | ||
1060 | |||
1061 | if (iph->daddr) { | ||
1062 | struct flowi4 fl4; | ||
1063 | struct rtable *rt; | ||
1064 | |||
1065 | rt = ip_route_output_gre(dev_net(dev), &fl4, | ||
1066 | iph->daddr, iph->saddr, | ||
1067 | tunnel->parms.o_key, | ||
1068 | RT_TOS(iph->tos), | ||
1069 | tunnel->parms.link); | ||
1070 | if (!IS_ERR(rt)) { | ||
1071 | tdev = rt->dst.dev; | ||
1072 | ip_rt_put(rt); | ||
1073 | } | ||
1074 | |||
1075 | if (dev->type != ARPHRD_ETHER) | ||
1076 | dev->flags |= IFF_POINTOPOINT; | ||
1077 | } | ||
1078 | 476 | ||
1079 | if (!tdev && tunnel->parms.link) | 477 | skb = handle_offloads(tunnel, skb); |
1080 | tdev = __dev_get_by_index(dev_net(dev), tunnel->parms.link); | 478 | if (IS_ERR(skb)) |
479 | goto out; | ||
1081 | 480 | ||
1082 | if (tdev) { | 481 | if (skb_cow_head(skb, dev->needed_headroom)) |
1083 | hlen = tdev->hard_header_len + tdev->needed_headroom; | 482 | goto free_skb; |
1084 | mtu = tdev->mtu; | ||
1085 | } | ||
1086 | dev->iflink = tunnel->parms.link; | ||
1087 | |||
1088 | /* Precalculate GRE options length */ | ||
1089 | if (tunnel->parms.o_flags&(GRE_CSUM|GRE_KEY|GRE_SEQ)) { | ||
1090 | if (tunnel->parms.o_flags&GRE_CSUM) | ||
1091 | addend += 4; | ||
1092 | if (tunnel->parms.o_flags&GRE_KEY) | ||
1093 | addend += 4; | ||
1094 | if (tunnel->parms.o_flags&GRE_SEQ) | ||
1095 | addend += 4; | ||
1096 | } | ||
1097 | dev->needed_headroom = addend + hlen; | ||
1098 | mtu -= dev->hard_header_len + addend; | ||
1099 | 483 | ||
1100 | if (mtu < 68) | 484 | __gre_xmit(skb, dev, &tunnel->parms.iph, htons(ETH_P_TEB)); |
1101 | mtu = 68; | ||
1102 | 485 | ||
1103 | tunnel->hlen = addend; | 486 | return NETDEV_TX_OK; |
1104 | /* TCP offload with GRE SEQ is not supported. */ | ||
1105 | if (!(tunnel->parms.o_flags & GRE_SEQ)) { | ||
1106 | dev->features |= NETIF_F_GSO_SOFTWARE; | ||
1107 | dev->hw_features |= NETIF_F_GSO_SOFTWARE; | ||
1108 | } | ||
1109 | 487 | ||
1110 | return mtu; | 488 | free_skb: |
489 | dev_kfree_skb(skb); | ||
490 | out: | ||
491 | dev->stats.tx_dropped++; | ||
492 | return NETDEV_TX_OK; | ||
1111 | } | 493 | } |
1112 | 494 | ||
1113 | static int | 495 | static int ipgre_tunnel_ioctl(struct net_device *dev, |
1114 | ipgre_tunnel_ioctl (struct net_device *dev, struct ifreq *ifr, int cmd) | 496 | struct ifreq *ifr, int cmd) |
1115 | { | 497 | { |
1116 | int err = 0; | 498 | int err = 0; |
1117 | struct ip_tunnel_parm p; | 499 | struct ip_tunnel_parm p; |
1118 | struct ip_tunnel *t; | ||
1119 | struct net *net = dev_net(dev); | ||
1120 | struct ipgre_net *ign = net_generic(net, ipgre_net_id); | ||
1121 | |||
1122 | switch (cmd) { | ||
1123 | case SIOCGETTUNNEL: | ||
1124 | t = NULL; | ||
1125 | if (dev == ign->fb_tunnel_dev) { | ||
1126 | if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) { | ||
1127 | err = -EFAULT; | ||
1128 | break; | ||
1129 | } | ||
1130 | t = ipgre_tunnel_locate(net, &p, 0); | ||
1131 | } | ||
1132 | if (t == NULL) | ||
1133 | t = netdev_priv(dev); | ||
1134 | memcpy(&p, &t->parms, sizeof(p)); | ||
1135 | if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p))) | ||
1136 | err = -EFAULT; | ||
1137 | break; | ||
1138 | |||
1139 | case SIOCADDTUNNEL: | ||
1140 | case SIOCCHGTUNNEL: | ||
1141 | err = -EPERM; | ||
1142 | if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) | ||
1143 | goto done; | ||
1144 | |||
1145 | err = -EFAULT; | ||
1146 | if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) | ||
1147 | goto done; | ||
1148 | |||
1149 | err = -EINVAL; | ||
1150 | if (p.iph.version != 4 || p.iph.protocol != IPPROTO_GRE || | ||
1151 | p.iph.ihl != 5 || (p.iph.frag_off&htons(~IP_DF)) || | ||
1152 | ((p.i_flags|p.o_flags)&(GRE_VERSION|GRE_ROUTING))) | ||
1153 | goto done; | ||
1154 | if (p.iph.ttl) | ||
1155 | p.iph.frag_off |= htons(IP_DF); | ||
1156 | |||
1157 | if (!(p.i_flags&GRE_KEY)) | ||
1158 | p.i_key = 0; | ||
1159 | if (!(p.o_flags&GRE_KEY)) | ||
1160 | p.o_key = 0; | ||
1161 | |||
1162 | t = ipgre_tunnel_locate(net, &p, cmd == SIOCADDTUNNEL); | ||
1163 | |||
1164 | if (dev != ign->fb_tunnel_dev && cmd == SIOCCHGTUNNEL) { | ||
1165 | if (t != NULL) { | ||
1166 | if (t->dev != dev) { | ||
1167 | err = -EEXIST; | ||
1168 | break; | ||
1169 | } | ||
1170 | } else { | ||
1171 | unsigned int nflags = 0; | ||
1172 | |||
1173 | t = netdev_priv(dev); | ||
1174 | |||
1175 | if (ipv4_is_multicast(p.iph.daddr)) | ||
1176 | nflags = IFF_BROADCAST; | ||
1177 | else if (p.iph.daddr) | ||
1178 | nflags = IFF_POINTOPOINT; | ||
1179 | |||
1180 | if ((dev->flags^nflags)&(IFF_POINTOPOINT|IFF_BROADCAST)) { | ||
1181 | err = -EINVAL; | ||
1182 | break; | ||
1183 | } | ||
1184 | ipgre_tunnel_unlink(ign, t); | ||
1185 | synchronize_net(); | ||
1186 | t->parms.iph.saddr = p.iph.saddr; | ||
1187 | t->parms.iph.daddr = p.iph.daddr; | ||
1188 | t->parms.i_key = p.i_key; | ||
1189 | t->parms.o_key = p.o_key; | ||
1190 | memcpy(dev->dev_addr, &p.iph.saddr, 4); | ||
1191 | memcpy(dev->broadcast, &p.iph.daddr, 4); | ||
1192 | ipgre_tunnel_link(ign, t); | ||
1193 | netdev_state_change(dev); | ||
1194 | } | ||
1195 | } | ||
1196 | |||
1197 | if (t) { | ||
1198 | err = 0; | ||
1199 | if (cmd == SIOCCHGTUNNEL) { | ||
1200 | t->parms.iph.ttl = p.iph.ttl; | ||
1201 | t->parms.iph.tos = p.iph.tos; | ||
1202 | t->parms.iph.frag_off = p.iph.frag_off; | ||
1203 | if (t->parms.link != p.link) { | ||
1204 | t->parms.link = p.link; | ||
1205 | dev->mtu = ipgre_tunnel_bind_dev(dev); | ||
1206 | netdev_state_change(dev); | ||
1207 | } | ||
1208 | } | ||
1209 | if (copy_to_user(ifr->ifr_ifru.ifru_data, &t->parms, sizeof(p))) | ||
1210 | err = -EFAULT; | ||
1211 | } else | ||
1212 | err = (cmd == SIOCADDTUNNEL ? -ENOBUFS : -ENOENT); | ||
1213 | break; | ||
1214 | |||
1215 | case SIOCDELTUNNEL: | ||
1216 | err = -EPERM; | ||
1217 | if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) | ||
1218 | goto done; | ||
1219 | |||
1220 | if (dev == ign->fb_tunnel_dev) { | ||
1221 | err = -EFAULT; | ||
1222 | if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) | ||
1223 | goto done; | ||
1224 | err = -ENOENT; | ||
1225 | if ((t = ipgre_tunnel_locate(net, &p, 0)) == NULL) | ||
1226 | goto done; | ||
1227 | err = -EPERM; | ||
1228 | if (t == netdev_priv(ign->fb_tunnel_dev)) | ||
1229 | goto done; | ||
1230 | dev = t->dev; | ||
1231 | } | ||
1232 | unregister_netdevice(dev); | ||
1233 | err = 0; | ||
1234 | break; | ||
1235 | 500 | ||
1236 | default: | 501 | if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) |
1237 | err = -EINVAL; | 502 | return -EFAULT; |
503 | if (p.iph.version != 4 || p.iph.protocol != IPPROTO_GRE || | ||
504 | p.iph.ihl != 5 || (p.iph.frag_off&htons(~IP_DF)) || | ||
505 | ((p.i_flags|p.o_flags)&(GRE_VERSION|GRE_ROUTING))) { | ||
506 | return -EINVAL; | ||
1238 | } | 507 | } |
508 | p.i_flags = gre_flags_to_tnl_flags(p.i_flags); | ||
509 | p.o_flags = gre_flags_to_tnl_flags(p.o_flags); | ||
1239 | 510 | ||
1240 | done: | 511 | err = ip_tunnel_ioctl(dev, &p, cmd); |
1241 | return err; | 512 | if (err) |
1242 | } | 513 | return err; |
1243 | 514 | ||
1244 | static int ipgre_tunnel_change_mtu(struct net_device *dev, int new_mtu) | 515 | p.i_flags = tnl_flags_to_gre_flags(p.i_flags); |
1245 | { | 516 | p.o_flags = tnl_flags_to_gre_flags(p.o_flags); |
1246 | struct ip_tunnel *tunnel = netdev_priv(dev); | 517 | |
1247 | if (new_mtu < 68 || | 518 | if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p))) |
1248 | new_mtu > 0xFFF8 - dev->hard_header_len - tunnel->hlen) | 519 | return -EFAULT; |
1249 | return -EINVAL; | ||
1250 | dev->mtu = new_mtu; | ||
1251 | return 0; | 520 | return 0; |
1252 | } | 521 | } |
1253 | 522 | ||
@@ -1277,25 +546,23 @@ static int ipgre_tunnel_change_mtu(struct net_device *dev, int new_mtu) | |||
1277 | ... | 546 | ... |
1278 | ftp fec0:6666:6666::193.233.7.65 | 547 | ftp fec0:6666:6666::193.233.7.65 |
1279 | ... | 548 | ... |
1280 | |||
1281 | */ | 549 | */ |
1282 | |||
1283 | static int ipgre_header(struct sk_buff *skb, struct net_device *dev, | 550 | static int ipgre_header(struct sk_buff *skb, struct net_device *dev, |
1284 | unsigned short type, | 551 | unsigned short type, |
1285 | const void *daddr, const void *saddr, unsigned int len) | 552 | const void *daddr, const void *saddr, unsigned int len) |
1286 | { | 553 | { |
1287 | struct ip_tunnel *t = netdev_priv(dev); | 554 | struct ip_tunnel *t = netdev_priv(dev); |
1288 | struct iphdr *iph = (struct iphdr *)skb_push(skb, t->hlen); | 555 | struct iphdr *iph; |
1289 | __be16 *p = (__be16 *)(iph+1); | 556 | struct gre_base_hdr *greh; |
1290 | 557 | ||
1291 | memcpy(iph, &t->parms.iph, sizeof(struct iphdr)); | 558 | iph = (struct iphdr *)skb_push(skb, t->hlen + sizeof(*iph)); |
1292 | p[0] = t->parms.o_flags; | 559 | greh = (struct gre_base_hdr *)(iph+1); |
1293 | p[1] = htons(type); | 560 | greh->flags = tnl_flags_to_gre_flags(t->parms.o_flags); |
561 | greh->protocol = htons(type); | ||
1294 | 562 | ||
1295 | /* | 563 | memcpy(iph, &t->parms.iph, sizeof(struct iphdr)); |
1296 | * Set the source hardware address. | ||
1297 | */ | ||
1298 | 564 | ||
565 | /* Set the source hardware address. */ | ||
1299 | if (saddr) | 566 | if (saddr) |
1300 | memcpy(&iph->saddr, saddr, 4); | 567 | memcpy(&iph->saddr, saddr, 4); |
1301 | if (daddr) | 568 | if (daddr) |
@@ -1303,7 +570,7 @@ static int ipgre_header(struct sk_buff *skb, struct net_device *dev, | |||
1303 | if (iph->daddr) | 570 | if (iph->daddr) |
1304 | return t->hlen; | 571 | return t->hlen; |
1305 | 572 | ||
1306 | return -t->hlen; | 573 | return -(t->hlen + sizeof(*iph)); |
1307 | } | 574 | } |
1308 | 575 | ||
1309 | static int ipgre_header_parse(const struct sk_buff *skb, unsigned char *haddr) | 576 | static int ipgre_header_parse(const struct sk_buff *skb, unsigned char *haddr) |
@@ -1357,31 +624,21 @@ static int ipgre_close(struct net_device *dev) | |||
1357 | } | 624 | } |
1358 | return 0; | 625 | return 0; |
1359 | } | 626 | } |
1360 | |||
1361 | #endif | 627 | #endif |
1362 | 628 | ||
1363 | static const struct net_device_ops ipgre_netdev_ops = { | 629 | static const struct net_device_ops ipgre_netdev_ops = { |
1364 | .ndo_init = ipgre_tunnel_init, | 630 | .ndo_init = ipgre_tunnel_init, |
1365 | .ndo_uninit = ipgre_tunnel_uninit, | 631 | .ndo_uninit = ip_tunnel_uninit, |
1366 | #ifdef CONFIG_NET_IPGRE_BROADCAST | 632 | #ifdef CONFIG_NET_IPGRE_BROADCAST |
1367 | .ndo_open = ipgre_open, | 633 | .ndo_open = ipgre_open, |
1368 | .ndo_stop = ipgre_close, | 634 | .ndo_stop = ipgre_close, |
1369 | #endif | 635 | #endif |
1370 | .ndo_start_xmit = ipgre_tunnel_xmit, | 636 | .ndo_start_xmit = ipgre_xmit, |
1371 | .ndo_do_ioctl = ipgre_tunnel_ioctl, | 637 | .ndo_do_ioctl = ipgre_tunnel_ioctl, |
1372 | .ndo_change_mtu = ipgre_tunnel_change_mtu, | 638 | .ndo_change_mtu = ip_tunnel_change_mtu, |
1373 | .ndo_get_stats64 = ipgre_get_stats64, | 639 | .ndo_get_stats64 = ip_tunnel_get_stats64, |
1374 | }; | 640 | }; |
1375 | 641 | ||
1376 | static void ipgre_dev_free(struct net_device *dev) | ||
1377 | { | ||
1378 | struct ip_tunnel *tunnel = netdev_priv(dev); | ||
1379 | |||
1380 | gro_cells_destroy(&tunnel->gro_cells); | ||
1381 | free_percpu(dev->tstats); | ||
1382 | free_netdev(dev); | ||
1383 | } | ||
1384 | |||
1385 | #define GRE_FEATURES (NETIF_F_SG | \ | 642 | #define GRE_FEATURES (NETIF_F_SG | \ |
1386 | NETIF_F_FRAGLIST | \ | 643 | NETIF_F_FRAGLIST | \ |
1387 | NETIF_F_HIGHDMA | \ | 644 | NETIF_F_HIGHDMA | \ |
@@ -1390,35 +647,49 @@ static void ipgre_dev_free(struct net_device *dev) | |||
1390 | static void ipgre_tunnel_setup(struct net_device *dev) | 647 | static void ipgre_tunnel_setup(struct net_device *dev) |
1391 | { | 648 | { |
1392 | dev->netdev_ops = &ipgre_netdev_ops; | 649 | dev->netdev_ops = &ipgre_netdev_ops; |
1393 | dev->destructor = ipgre_dev_free; | 650 | ip_tunnel_setup(dev, ipgre_net_id); |
651 | } | ||
1394 | 652 | ||
1395 | dev->type = ARPHRD_IPGRE; | 653 | static void __gre_tunnel_init(struct net_device *dev) |
1396 | dev->needed_headroom = LL_MAX_HEADER + sizeof(struct iphdr) + 4; | 654 | { |
655 | struct ip_tunnel *tunnel; | ||
656 | |||
657 | tunnel = netdev_priv(dev); | ||
658 | tunnel->hlen = ip_gre_calc_hlen(tunnel->parms.o_flags); | ||
659 | tunnel->parms.iph.protocol = IPPROTO_GRE; | ||
660 | |||
661 | dev->needed_headroom = LL_MAX_HEADER + sizeof(struct iphdr) + 4; | ||
1397 | dev->mtu = ETH_DATA_LEN - sizeof(struct iphdr) - 4; | 662 | dev->mtu = ETH_DATA_LEN - sizeof(struct iphdr) - 4; |
1398 | dev->flags = IFF_NOARP; | ||
1399 | dev->iflink = 0; | 663 | dev->iflink = 0; |
1400 | dev->addr_len = 4; | ||
1401 | dev->features |= NETIF_F_NETNS_LOCAL; | ||
1402 | dev->priv_flags &= ~IFF_XMIT_DST_RELEASE; | ||
1403 | 664 | ||
1404 | dev->features |= GRE_FEATURES; | 665 | dev->features |= NETIF_F_NETNS_LOCAL | GRE_FEATURES; |
1405 | dev->hw_features |= GRE_FEATURES; | 666 | dev->hw_features |= GRE_FEATURES; |
667 | |||
668 | if (!(tunnel->parms.o_flags & TUNNEL_SEQ)) { | ||
669 | /* TCP offload with GRE SEQ is not supported. */ | ||
670 | dev->features |= NETIF_F_GSO_SOFTWARE; | ||
671 | dev->hw_features |= NETIF_F_GSO_SOFTWARE; | ||
672 | /* Can use a lockless transmit, unless we generate | ||
673 | * output sequences | ||
674 | */ | ||
675 | dev->features |= NETIF_F_LLTX; | ||
676 | } | ||
1406 | } | 677 | } |
1407 | 678 | ||
1408 | static int ipgre_tunnel_init(struct net_device *dev) | 679 | static int ipgre_tunnel_init(struct net_device *dev) |
1409 | { | 680 | { |
1410 | struct ip_tunnel *tunnel; | 681 | struct ip_tunnel *tunnel = netdev_priv(dev); |
1411 | struct iphdr *iph; | 682 | struct iphdr *iph = &tunnel->parms.iph; |
1412 | int err; | ||
1413 | 683 | ||
1414 | tunnel = netdev_priv(dev); | 684 | __gre_tunnel_init(dev); |
1415 | iph = &tunnel->parms.iph; | ||
1416 | 685 | ||
1417 | tunnel->dev = dev; | 686 | memcpy(dev->dev_addr, &iph->saddr, 4); |
1418 | strcpy(tunnel->parms.name, dev->name); | 687 | memcpy(dev->broadcast, &iph->daddr, 4); |
1419 | 688 | ||
1420 | memcpy(dev->dev_addr, &tunnel->parms.iph.saddr, 4); | 689 | dev->type = ARPHRD_IPGRE; |
1421 | memcpy(dev->broadcast, &tunnel->parms.iph.daddr, 4); | 690 | dev->flags = IFF_NOARP; |
691 | dev->priv_flags &= ~IFF_XMIT_DST_RELEASE; | ||
692 | dev->addr_len = 4; | ||
1422 | 693 | ||
1423 | if (iph->daddr) { | 694 | if (iph->daddr) { |
1424 | #ifdef CONFIG_NET_IPGRE_BROADCAST | 695 | #ifdef CONFIG_NET_IPGRE_BROADCAST |
@@ -1432,106 +703,30 @@ static int ipgre_tunnel_init(struct net_device *dev) | |||
1432 | } else | 703 | } else |
1433 | dev->header_ops = &ipgre_header_ops; | 704 | dev->header_ops = &ipgre_header_ops; |
1434 | 705 | ||
1435 | dev->tstats = alloc_percpu(struct pcpu_tstats); | 706 | return ip_tunnel_init(dev); |
1436 | if (!dev->tstats) | ||
1437 | return -ENOMEM; | ||
1438 | |||
1439 | err = gro_cells_init(&tunnel->gro_cells, dev); | ||
1440 | if (err) { | ||
1441 | free_percpu(dev->tstats); | ||
1442 | return err; | ||
1443 | } | ||
1444 | |||
1445 | return 0; | ||
1446 | } | 707 | } |
1447 | 708 | ||
1448 | static void ipgre_fb_tunnel_init(struct net_device *dev) | ||
1449 | { | ||
1450 | struct ip_tunnel *tunnel = netdev_priv(dev); | ||
1451 | struct iphdr *iph = &tunnel->parms.iph; | ||
1452 | |||
1453 | tunnel->dev = dev; | ||
1454 | strcpy(tunnel->parms.name, dev->name); | ||
1455 | |||
1456 | iph->version = 4; | ||
1457 | iph->protocol = IPPROTO_GRE; | ||
1458 | iph->ihl = 5; | ||
1459 | tunnel->hlen = sizeof(struct iphdr) + 4; | ||
1460 | |||
1461 | dev_hold(dev); | ||
1462 | } | ||
1463 | |||
1464 | |||
1465 | static const struct gre_protocol ipgre_protocol = { | 709 | static const struct gre_protocol ipgre_protocol = { |
1466 | .handler = ipgre_rcv, | 710 | .handler = ipgre_rcv, |
1467 | .err_handler = ipgre_err, | 711 | .err_handler = ipgre_err, |
1468 | }; | 712 | }; |
1469 | 713 | ||
1470 | static void ipgre_destroy_tunnels(struct ipgre_net *ign, struct list_head *head) | ||
1471 | { | ||
1472 | int prio; | ||
1473 | |||
1474 | for (prio = 0; prio < 4; prio++) { | ||
1475 | int h; | ||
1476 | for (h = 0; h < HASH_SIZE; h++) { | ||
1477 | struct ip_tunnel *t; | ||
1478 | |||
1479 | t = rtnl_dereference(ign->tunnels[prio][h]); | ||
1480 | |||
1481 | while (t != NULL) { | ||
1482 | unregister_netdevice_queue(t->dev, head); | ||
1483 | t = rtnl_dereference(t->next); | ||
1484 | } | ||
1485 | } | ||
1486 | } | ||
1487 | } | ||
1488 | |||
1489 | static int __net_init ipgre_init_net(struct net *net) | 714 | static int __net_init ipgre_init_net(struct net *net) |
1490 | { | 715 | { |
1491 | struct ipgre_net *ign = net_generic(net, ipgre_net_id); | 716 | return ip_tunnel_init_net(net, ipgre_net_id, &ipgre_link_ops, NULL); |
1492 | int err; | ||
1493 | |||
1494 | ign->fb_tunnel_dev = alloc_netdev(sizeof(struct ip_tunnel), "gre0", | ||
1495 | ipgre_tunnel_setup); | ||
1496 | if (!ign->fb_tunnel_dev) { | ||
1497 | err = -ENOMEM; | ||
1498 | goto err_alloc_dev; | ||
1499 | } | ||
1500 | dev_net_set(ign->fb_tunnel_dev, net); | ||
1501 | |||
1502 | ipgre_fb_tunnel_init(ign->fb_tunnel_dev); | ||
1503 | ign->fb_tunnel_dev->rtnl_link_ops = &ipgre_link_ops; | ||
1504 | |||
1505 | if ((err = register_netdev(ign->fb_tunnel_dev))) | ||
1506 | goto err_reg_dev; | ||
1507 | |||
1508 | rcu_assign_pointer(ign->tunnels_wc[0], | ||
1509 | netdev_priv(ign->fb_tunnel_dev)); | ||
1510 | return 0; | ||
1511 | |||
1512 | err_reg_dev: | ||
1513 | ipgre_dev_free(ign->fb_tunnel_dev); | ||
1514 | err_alloc_dev: | ||
1515 | return err; | ||
1516 | } | 717 | } |
1517 | 718 | ||
1518 | static void __net_exit ipgre_exit_net(struct net *net) | 719 | static void __net_exit ipgre_exit_net(struct net *net) |
1519 | { | 720 | { |
1520 | struct ipgre_net *ign; | 721 | struct ip_tunnel_net *itn = net_generic(net, ipgre_net_id); |
1521 | LIST_HEAD(list); | 722 | ip_tunnel_delete_net(itn); |
1522 | |||
1523 | ign = net_generic(net, ipgre_net_id); | ||
1524 | rtnl_lock(); | ||
1525 | ipgre_destroy_tunnels(ign, &list); | ||
1526 | unregister_netdevice_many(&list); | ||
1527 | rtnl_unlock(); | ||
1528 | } | 723 | } |
1529 | 724 | ||
1530 | static struct pernet_operations ipgre_net_ops = { | 725 | static struct pernet_operations ipgre_net_ops = { |
1531 | .init = ipgre_init_net, | 726 | .init = ipgre_init_net, |
1532 | .exit = ipgre_exit_net, | 727 | .exit = ipgre_exit_net, |
1533 | .id = &ipgre_net_id, | 728 | .id = &ipgre_net_id, |
1534 | .size = sizeof(struct ipgre_net), | 729 | .size = sizeof(struct ip_tunnel_net), |
1535 | }; | 730 | }; |
1536 | 731 | ||
1537 | static int ipgre_tunnel_validate(struct nlattr *tb[], struct nlattr *data[]) | 732 | static int ipgre_tunnel_validate(struct nlattr *tb[], struct nlattr *data[]) |
@@ -1576,8 +771,8 @@ out: | |||
1576 | return ipgre_tunnel_validate(tb, data); | 771 | return ipgre_tunnel_validate(tb, data); |
1577 | } | 772 | } |
1578 | 773 | ||
1579 | static void ipgre_netlink_parms(struct nlattr *data[], | 774 | static void ipgre_netlink_parms(struct nlattr *data[], struct nlattr *tb[], |
1580 | struct ip_tunnel_parm *parms) | 775 | struct ip_tunnel_parm *parms) |
1581 | { | 776 | { |
1582 | memset(parms, 0, sizeof(*parms)); | 777 | memset(parms, 0, sizeof(*parms)); |
1583 | 778 | ||
@@ -1590,10 +785,10 @@ static void ipgre_netlink_parms(struct nlattr *data[], | |||
1590 | parms->link = nla_get_u32(data[IFLA_GRE_LINK]); | 785 | parms->link = nla_get_u32(data[IFLA_GRE_LINK]); |
1591 | 786 | ||
1592 | if (data[IFLA_GRE_IFLAGS]) | 787 | if (data[IFLA_GRE_IFLAGS]) |
1593 | parms->i_flags = nla_get_be16(data[IFLA_GRE_IFLAGS]); | 788 | parms->i_flags = gre_flags_to_tnl_flags(nla_get_be16(data[IFLA_GRE_IFLAGS])); |
1594 | 789 | ||
1595 | if (data[IFLA_GRE_OFLAGS]) | 790 | if (data[IFLA_GRE_OFLAGS]) |
1596 | parms->o_flags = nla_get_be16(data[IFLA_GRE_OFLAGS]); | 791 | parms->o_flags = gre_flags_to_tnl_flags(nla_get_be16(data[IFLA_GRE_OFLAGS])); |
1597 | 792 | ||
1598 | if (data[IFLA_GRE_IKEY]) | 793 | if (data[IFLA_GRE_IKEY]) |
1599 | parms->i_key = nla_get_be32(data[IFLA_GRE_IKEY]); | 794 | parms->i_key = nla_get_be32(data[IFLA_GRE_IKEY]); |
@@ -1617,148 +812,46 @@ static void ipgre_netlink_parms(struct nlattr *data[], | |||
1617 | parms->iph.frag_off = htons(IP_DF); | 812 | parms->iph.frag_off = htons(IP_DF); |
1618 | } | 813 | } |
1619 | 814 | ||
1620 | static int ipgre_tap_init(struct net_device *dev) | 815 | static int gre_tap_init(struct net_device *dev) |
1621 | { | 816 | { |
1622 | struct ip_tunnel *tunnel; | 817 | __gre_tunnel_init(dev); |
1623 | |||
1624 | tunnel = netdev_priv(dev); | ||
1625 | |||
1626 | tunnel->dev = dev; | ||
1627 | strcpy(tunnel->parms.name, dev->name); | ||
1628 | |||
1629 | ipgre_tunnel_bind_dev(dev); | ||
1630 | 818 | ||
1631 | dev->tstats = alloc_percpu(struct pcpu_tstats); | 819 | return ip_tunnel_init(dev); |
1632 | if (!dev->tstats) | ||
1633 | return -ENOMEM; | ||
1634 | |||
1635 | return 0; | ||
1636 | } | 820 | } |
1637 | 821 | ||
1638 | static const struct net_device_ops ipgre_tap_netdev_ops = { | 822 | static const struct net_device_ops gre_tap_netdev_ops = { |
1639 | .ndo_init = ipgre_tap_init, | 823 | .ndo_init = gre_tap_init, |
1640 | .ndo_uninit = ipgre_tunnel_uninit, | 824 | .ndo_uninit = ip_tunnel_uninit, |
1641 | .ndo_start_xmit = ipgre_tunnel_xmit, | 825 | .ndo_start_xmit = gre_tap_xmit, |
1642 | .ndo_set_mac_address = eth_mac_addr, | 826 | .ndo_set_mac_address = eth_mac_addr, |
1643 | .ndo_validate_addr = eth_validate_addr, | 827 | .ndo_validate_addr = eth_validate_addr, |
1644 | .ndo_change_mtu = ipgre_tunnel_change_mtu, | 828 | .ndo_change_mtu = ip_tunnel_change_mtu, |
1645 | .ndo_get_stats64 = ipgre_get_stats64, | 829 | .ndo_get_stats64 = ip_tunnel_get_stats64, |
1646 | }; | 830 | }; |
1647 | 831 | ||
1648 | static void ipgre_tap_setup(struct net_device *dev) | 832 | static void ipgre_tap_setup(struct net_device *dev) |
1649 | { | 833 | { |
1650 | |||
1651 | ether_setup(dev); | 834 | ether_setup(dev); |
1652 | 835 | dev->netdev_ops = &gre_tap_netdev_ops; | |
1653 | dev->netdev_ops = &ipgre_tap_netdev_ops; | 836 | ip_tunnel_setup(dev, gre_tap_net_id); |
1654 | dev->destructor = ipgre_dev_free; | ||
1655 | |||
1656 | dev->iflink = 0; | ||
1657 | dev->features |= NETIF_F_NETNS_LOCAL; | ||
1658 | |||
1659 | dev->features |= GRE_FEATURES; | ||
1660 | dev->hw_features |= GRE_FEATURES; | ||
1661 | } | 837 | } |
1662 | 838 | ||
1663 | static int ipgre_newlink(struct net *src_net, struct net_device *dev, struct nlattr *tb[], | 839 | static int ipgre_newlink(struct net *src_net, struct net_device *dev, |
1664 | struct nlattr *data[]) | 840 | struct nlattr *tb[], struct nlattr *data[]) |
1665 | { | 841 | { |
1666 | struct ip_tunnel *nt; | 842 | struct ip_tunnel_parm p; |
1667 | struct net *net = dev_net(dev); | ||
1668 | struct ipgre_net *ign = net_generic(net, ipgre_net_id); | ||
1669 | int mtu; | ||
1670 | int err; | ||
1671 | |||
1672 | nt = netdev_priv(dev); | ||
1673 | ipgre_netlink_parms(data, &nt->parms); | ||
1674 | |||
1675 | if (ipgre_tunnel_find(net, &nt->parms, dev->type)) | ||
1676 | return -EEXIST; | ||
1677 | |||
1678 | if (dev->type == ARPHRD_ETHER && !tb[IFLA_ADDRESS]) | ||
1679 | eth_hw_addr_random(dev); | ||
1680 | |||
1681 | mtu = ipgre_tunnel_bind_dev(dev); | ||
1682 | if (!tb[IFLA_MTU]) | ||
1683 | dev->mtu = mtu; | ||
1684 | |||
1685 | /* Can use a lockless transmit, unless we generate output sequences */ | ||
1686 | if (!(nt->parms.o_flags & GRE_SEQ)) | ||
1687 | dev->features |= NETIF_F_LLTX; | ||
1688 | |||
1689 | err = register_netdevice(dev); | ||
1690 | if (err) | ||
1691 | goto out; | ||
1692 | |||
1693 | dev_hold(dev); | ||
1694 | ipgre_tunnel_link(ign, nt); | ||
1695 | 843 | ||
1696 | out: | 844 | ipgre_netlink_parms(data, tb, &p); |
1697 | return err; | 845 | return ip_tunnel_newlink(dev, tb, &p); |
1698 | } | 846 | } |
1699 | 847 | ||
1700 | static int ipgre_changelink(struct net_device *dev, struct nlattr *tb[], | 848 | static int ipgre_changelink(struct net_device *dev, struct nlattr *tb[], |
1701 | struct nlattr *data[]) | 849 | struct nlattr *data[]) |
1702 | { | 850 | { |
1703 | struct ip_tunnel *t, *nt; | ||
1704 | struct net *net = dev_net(dev); | ||
1705 | struct ipgre_net *ign = net_generic(net, ipgre_net_id); | ||
1706 | struct ip_tunnel_parm p; | 851 | struct ip_tunnel_parm p; |
1707 | int mtu; | ||
1708 | |||
1709 | if (dev == ign->fb_tunnel_dev) | ||
1710 | return -EINVAL; | ||
1711 | |||
1712 | nt = netdev_priv(dev); | ||
1713 | ipgre_netlink_parms(data, &p); | ||
1714 | |||
1715 | t = ipgre_tunnel_locate(net, &p, 0); | ||
1716 | |||
1717 | if (t) { | ||
1718 | if (t->dev != dev) | ||
1719 | return -EEXIST; | ||
1720 | } else { | ||
1721 | t = nt; | ||
1722 | |||
1723 | if (dev->type != ARPHRD_ETHER) { | ||
1724 | unsigned int nflags = 0; | ||
1725 | |||
1726 | if (ipv4_is_multicast(p.iph.daddr)) | ||
1727 | nflags = IFF_BROADCAST; | ||
1728 | else if (p.iph.daddr) | ||
1729 | nflags = IFF_POINTOPOINT; | ||
1730 | |||
1731 | if ((dev->flags ^ nflags) & | ||
1732 | (IFF_POINTOPOINT | IFF_BROADCAST)) | ||
1733 | return -EINVAL; | ||
1734 | } | ||
1735 | 852 | ||
1736 | ipgre_tunnel_unlink(ign, t); | 853 | ipgre_netlink_parms(data, tb, &p); |
1737 | t->parms.iph.saddr = p.iph.saddr; | 854 | return ip_tunnel_changelink(dev, tb, &p); |
1738 | t->parms.iph.daddr = p.iph.daddr; | ||
1739 | t->parms.i_key = p.i_key; | ||
1740 | if (dev->type != ARPHRD_ETHER) { | ||
1741 | memcpy(dev->dev_addr, &p.iph.saddr, 4); | ||
1742 | memcpy(dev->broadcast, &p.iph.daddr, 4); | ||
1743 | } | ||
1744 | ipgre_tunnel_link(ign, t); | ||
1745 | netdev_state_change(dev); | ||
1746 | } | ||
1747 | |||
1748 | t->parms.o_key = p.o_key; | ||
1749 | t->parms.iph.ttl = p.iph.ttl; | ||
1750 | t->parms.iph.tos = p.iph.tos; | ||
1751 | t->parms.iph.frag_off = p.iph.frag_off; | ||
1752 | |||
1753 | if (t->parms.link != p.link) { | ||
1754 | t->parms.link = p.link; | ||
1755 | mtu = ipgre_tunnel_bind_dev(dev); | ||
1756 | if (!tb[IFLA_MTU]) | ||
1757 | dev->mtu = mtu; | ||
1758 | netdev_state_change(dev); | ||
1759 | } | ||
1760 | |||
1761 | return 0; | ||
1762 | } | 855 | } |
1763 | 856 | ||
1764 | static size_t ipgre_get_size(const struct net_device *dev) | 857 | static size_t ipgre_get_size(const struct net_device *dev) |
@@ -1793,8 +886,8 @@ static int ipgre_fill_info(struct sk_buff *skb, const struct net_device *dev) | |||
1793 | struct ip_tunnel_parm *p = &t->parms; | 886 | struct ip_tunnel_parm *p = &t->parms; |
1794 | 887 | ||
1795 | if (nla_put_u32(skb, IFLA_GRE_LINK, p->link) || | 888 | if (nla_put_u32(skb, IFLA_GRE_LINK, p->link) || |
1796 | nla_put_be16(skb, IFLA_GRE_IFLAGS, p->i_flags) || | 889 | nla_put_be16(skb, IFLA_GRE_IFLAGS, tnl_flags_to_gre_flags(p->i_flags)) || |
1797 | nla_put_be16(skb, IFLA_GRE_OFLAGS, p->o_flags) || | 890 | nla_put_be16(skb, IFLA_GRE_OFLAGS, tnl_flags_to_gre_flags(p->o_flags)) || |
1798 | nla_put_be32(skb, IFLA_GRE_IKEY, p->i_key) || | 891 | nla_put_be32(skb, IFLA_GRE_IKEY, p->i_key) || |
1799 | nla_put_be32(skb, IFLA_GRE_OKEY, p->o_key) || | 892 | nla_put_be32(skb, IFLA_GRE_OKEY, p->o_key) || |
1800 | nla_put_be32(skb, IFLA_GRE_LOCAL, p->iph.saddr) || | 893 | nla_put_be32(skb, IFLA_GRE_LOCAL, p->iph.saddr) || |
@@ -1832,6 +925,7 @@ static struct rtnl_link_ops ipgre_link_ops __read_mostly = { | |||
1832 | .validate = ipgre_tunnel_validate, | 925 | .validate = ipgre_tunnel_validate, |
1833 | .newlink = ipgre_newlink, | 926 | .newlink = ipgre_newlink, |
1834 | .changelink = ipgre_changelink, | 927 | .changelink = ipgre_changelink, |
928 | .dellink = ip_tunnel_dellink, | ||
1835 | .get_size = ipgre_get_size, | 929 | .get_size = ipgre_get_size, |
1836 | .fill_info = ipgre_fill_info, | 930 | .fill_info = ipgre_fill_info, |
1837 | }; | 931 | }; |
@@ -1845,13 +939,28 @@ static struct rtnl_link_ops ipgre_tap_ops __read_mostly = { | |||
1845 | .validate = ipgre_tap_validate, | 939 | .validate = ipgre_tap_validate, |
1846 | .newlink = ipgre_newlink, | 940 | .newlink = ipgre_newlink, |
1847 | .changelink = ipgre_changelink, | 941 | .changelink = ipgre_changelink, |
942 | .dellink = ip_tunnel_dellink, | ||
1848 | .get_size = ipgre_get_size, | 943 | .get_size = ipgre_get_size, |
1849 | .fill_info = ipgre_fill_info, | 944 | .fill_info = ipgre_fill_info, |
1850 | }; | 945 | }; |
1851 | 946 | ||
1852 | /* | 947 | static int __net_init ipgre_tap_init_net(struct net *net) |
1853 | * And now the modules code and kernel interface. | 948 | { |
1854 | */ | 949 | return ip_tunnel_init_net(net, gre_tap_net_id, &ipgre_tap_ops, NULL); |
950 | } | ||
951 | |||
952 | static void __net_exit ipgre_tap_exit_net(struct net *net) | ||
953 | { | ||
954 | struct ip_tunnel_net *itn = net_generic(net, gre_tap_net_id); | ||
955 | ip_tunnel_delete_net(itn); | ||
956 | } | ||
957 | |||
958 | static struct pernet_operations ipgre_tap_net_ops = { | ||
959 | .init = ipgre_tap_init_net, | ||
960 | .exit = ipgre_tap_exit_net, | ||
961 | .id = &gre_tap_net_id, | ||
962 | .size = sizeof(struct ip_tunnel_net), | ||
963 | }; | ||
1855 | 964 | ||
1856 | static int __init ipgre_init(void) | 965 | static int __init ipgre_init(void) |
1857 | { | 966 | { |
@@ -1863,6 +972,10 @@ static int __init ipgre_init(void) | |||
1863 | if (err < 0) | 972 | if (err < 0) |
1864 | return err; | 973 | return err; |
1865 | 974 | ||
975 | err = register_pernet_device(&ipgre_tap_net_ops); | ||
976 | if (err < 0) | ||
977 | goto pnet_tap_faied; | ||
978 | |||
1866 | err = gre_add_protocol(&ipgre_protocol, GREPROTO_CISCO); | 979 | err = gre_add_protocol(&ipgre_protocol, GREPROTO_CISCO); |
1867 | if (err < 0) { | 980 | if (err < 0) { |
1868 | pr_info("%s: can't add protocol\n", __func__); | 981 | pr_info("%s: can't add protocol\n", __func__); |
@@ -1877,16 +990,17 @@ static int __init ipgre_init(void) | |||
1877 | if (err < 0) | 990 | if (err < 0) |
1878 | goto tap_ops_failed; | 991 | goto tap_ops_failed; |
1879 | 992 | ||
1880 | out: | 993 | return 0; |
1881 | return err; | ||
1882 | 994 | ||
1883 | tap_ops_failed: | 995 | tap_ops_failed: |
1884 | rtnl_link_unregister(&ipgre_link_ops); | 996 | rtnl_link_unregister(&ipgre_link_ops); |
1885 | rtnl_link_failed: | 997 | rtnl_link_failed: |
1886 | gre_del_protocol(&ipgre_protocol, GREPROTO_CISCO); | 998 | gre_del_protocol(&ipgre_protocol, GREPROTO_CISCO); |
1887 | add_proto_failed: | 999 | add_proto_failed: |
1000 | unregister_pernet_device(&ipgre_tap_net_ops); | ||
1001 | pnet_tap_faied: | ||
1888 | unregister_pernet_device(&ipgre_net_ops); | 1002 | unregister_pernet_device(&ipgre_net_ops); |
1889 | goto out; | 1003 | return err; |
1890 | } | 1004 | } |
1891 | 1005 | ||
1892 | static void __exit ipgre_fini(void) | 1006 | static void __exit ipgre_fini(void) |
@@ -1895,6 +1009,7 @@ static void __exit ipgre_fini(void) | |||
1895 | rtnl_link_unregister(&ipgre_link_ops); | 1009 | rtnl_link_unregister(&ipgre_link_ops); |
1896 | if (gre_del_protocol(&ipgre_protocol, GREPROTO_CISCO) < 0) | 1010 | if (gre_del_protocol(&ipgre_protocol, GREPROTO_CISCO) < 0) |
1897 | pr_info("%s: can't remove protocol\n", __func__); | 1011 | pr_info("%s: can't remove protocol\n", __func__); |
1012 | unregister_pernet_device(&ipgre_tap_net_ops); | ||
1898 | unregister_pernet_device(&ipgre_net_ops); | 1013 | unregister_pernet_device(&ipgre_net_ops); |
1899 | } | 1014 | } |
1900 | 1015 | ||
@@ -1904,3 +1019,4 @@ MODULE_LICENSE("GPL"); | |||
1904 | MODULE_ALIAS_RTNL_LINK("gre"); | 1019 | MODULE_ALIAS_RTNL_LINK("gre"); |
1905 | MODULE_ALIAS_RTNL_LINK("gretap"); | 1020 | MODULE_ALIAS_RTNL_LINK("gretap"); |
1906 | MODULE_ALIAS_NETDEV("gre0"); | 1021 | MODULE_ALIAS_NETDEV("gre0"); |
1022 | MODULE_ALIAS_NETDEV("gretap0"); | ||