Commit | Line | Data |
---|---|---|
c12b395a | 1 | /* |
2 | * GRE over IPv6 protocol decoder. | |
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/capability.h> | |
16 | #include <linux/module.h> | |
17 | #include <linux/types.h> | |
18 | #include <linux/kernel.h> | |
19 | #include <linux/slab.h> | |
20 | #include <linux/uaccess.h> | |
21 | #include <linux/skbuff.h> | |
22 | #include <linux/netdevice.h> | |
23 | #include <linux/in.h> | |
24 | #include <linux/tcp.h> | |
25 | #include <linux/udp.h> | |
26 | #include <linux/if_arp.h> | |
27 | #include <linux/mroute.h> | |
28 | #include <linux/init.h> | |
29 | #include <linux/in6.h> | |
30 | #include <linux/inetdevice.h> | |
31 | #include <linux/igmp.h> | |
32 | #include <linux/netfilter_ipv4.h> | |
33 | #include <linux/etherdevice.h> | |
34 | #include <linux/if_ether.h> | |
35 | #include <linux/hash.h> | |
36 | #include <linux/if_tunnel.h> | |
37 | #include <linux/ip6_tunnel.h> | |
38 | ||
39 | #include <net/sock.h> | |
40 | #include <net/ip.h> | |
c5441932 | 41 | #include <net/ip_tunnels.h> |
c12b395a | 42 | #include <net/icmp.h> |
43 | #include <net/protocol.h> | |
44 | #include <net/addrconf.h> | |
45 | #include <net/arp.h> | |
46 | #include <net/checksum.h> | |
47 | #include <net/dsfield.h> | |
48 | #include <net/inet_ecn.h> | |
49 | #include <net/xfrm.h> | |
50 | #include <net/net_namespace.h> | |
51 | #include <net/netns/generic.h> | |
52 | #include <net/rtnetlink.h> | |
53 | ||
54 | #include <net/ipv6.h> | |
55 | #include <net/ip6_fib.h> | |
56 | #include <net/ip6_route.h> | |
57 | #include <net/ip6_tunnel.h> | |
58 | ||
59 | ||
eccc1bb8 | 60 | static bool log_ecn_error = true; |
61 | module_param(log_ecn_error, bool, 0644); | |
62 | MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN"); | |
63 | ||
c12b395a | 64 | #define IPV6_TCLASS_MASK (IPV6_FLOWINFO_MASK & ~IPV6_FLOWLABEL_MASK) |
65 | #define IPV6_TCLASS_SHIFT 20 | |
66 | ||
67 | #define HASH_SIZE_SHIFT 5 | |
68 | #define HASH_SIZE (1 << HASH_SIZE_SHIFT) | |
69 | ||
70 | static int ip6gre_net_id __read_mostly; | |
71 | struct ip6gre_net { | |
72 | struct ip6_tnl __rcu *tunnels[4][HASH_SIZE]; | |
73 | ||
74 | struct net_device *fb_tunnel_dev; | |
75 | }; | |
76 | ||
77 | static struct rtnl_link_ops ip6gre_link_ops __read_mostly; | |
78 | static int ip6gre_tunnel_init(struct net_device *dev); | |
79 | static void ip6gre_tunnel_setup(struct net_device *dev); | |
80 | static void ip6gre_tunnel_link(struct ip6gre_net *ign, struct ip6_tnl *t); | |
81 | static void ip6gre_tnl_link_config(struct ip6_tnl *t, int set_mtu); | |
82 | ||
83 | /* Tunnel hash table */ | |
84 | ||
85 | /* | |
86 | 4 hash tables: | |
87 | ||
88 | 3: (remote,local) | |
89 | 2: (remote,*) | |
90 | 1: (*,local) | |
91 | 0: (*,*) | |
92 | ||
93 | We require exact key match i.e. if a key is present in packet | |
94 | it will match only tunnel with the same key; if it is not present, | |
95 | it will match only keyless tunnel. | |
96 | ||
97 | All keysless packets, if not matched configured keyless tunnels | |
98 | will match fallback tunnel. | |
99 | */ | |
100 | ||
101 | #define HASH_KEY(key) (((__force u32)key^((__force u32)key>>4))&(HASH_SIZE - 1)) | |
102 | static u32 HASH_ADDR(const struct in6_addr *addr) | |
103 | { | |
104 | u32 hash = ipv6_addr_hash(addr); | |
105 | ||
106 | return hash_32(hash, HASH_SIZE_SHIFT); | |
107 | } | |
108 | ||
109 | #define tunnels_r_l tunnels[3] | |
110 | #define tunnels_r tunnels[2] | |
111 | #define tunnels_l tunnels[1] | |
112 | #define tunnels_wc tunnels[0] | |
c12b395a | 113 | |
c12b395a | 114 | static struct rtnl_link_stats64 *ip6gre_get_stats64(struct net_device *dev, |
115 | struct rtnl_link_stats64 *tot) | |
116 | { | |
117 | int i; | |
118 | ||
119 | for_each_possible_cpu(i) { | |
120 | const struct pcpu_tstats *tstats = per_cpu_ptr(dev->tstats, i); | |
121 | u64 rx_packets, rx_bytes, tx_packets, tx_bytes; | |
122 | unsigned int start; | |
123 | ||
124 | do { | |
125 | start = u64_stats_fetch_begin_bh(&tstats->syncp); | |
126 | rx_packets = tstats->rx_packets; | |
127 | tx_packets = tstats->tx_packets; | |
128 | rx_bytes = tstats->rx_bytes; | |
129 | tx_bytes = tstats->tx_bytes; | |
130 | } while (u64_stats_fetch_retry_bh(&tstats->syncp, start)); | |
131 | ||
132 | tot->rx_packets += rx_packets; | |
133 | tot->tx_packets += tx_packets; | |
134 | tot->rx_bytes += rx_bytes; | |
135 | tot->tx_bytes += tx_bytes; | |
136 | } | |
137 | ||
138 | tot->multicast = dev->stats.multicast; | |
139 | tot->rx_crc_errors = dev->stats.rx_crc_errors; | |
140 | tot->rx_fifo_errors = dev->stats.rx_fifo_errors; | |
141 | tot->rx_length_errors = dev->stats.rx_length_errors; | |
eccc1bb8 | 142 | tot->rx_frame_errors = dev->stats.rx_frame_errors; |
c12b395a | 143 | tot->rx_errors = dev->stats.rx_errors; |
eccc1bb8 | 144 | |
c12b395a | 145 | tot->tx_fifo_errors = dev->stats.tx_fifo_errors; |
146 | tot->tx_carrier_errors = dev->stats.tx_carrier_errors; | |
147 | tot->tx_dropped = dev->stats.tx_dropped; | |
148 | tot->tx_aborted_errors = dev->stats.tx_aborted_errors; | |
149 | tot->tx_errors = dev->stats.tx_errors; | |
150 | ||
151 | return tot; | |
152 | } | |
153 | ||
154 | /* Given src, dst and key, find appropriate for input tunnel. */ | |
155 | ||
156 | static struct ip6_tnl *ip6gre_tunnel_lookup(struct net_device *dev, | |
157 | const struct in6_addr *remote, const struct in6_addr *local, | |
158 | __be32 key, __be16 gre_proto) | |
159 | { | |
160 | struct net *net = dev_net(dev); | |
161 | int link = dev->ifindex; | |
162 | unsigned int h0 = HASH_ADDR(remote); | |
163 | unsigned int h1 = HASH_KEY(key); | |
164 | struct ip6_tnl *t, *cand = NULL; | |
165 | struct ip6gre_net *ign = net_generic(net, ip6gre_net_id); | |
166 | int dev_type = (gre_proto == htons(ETH_P_TEB)) ? | |
167 | ARPHRD_ETHER : ARPHRD_IP6GRE; | |
168 | int score, cand_score = 4; | |
169 | ||
e086cadc | 170 | for_each_ip_tunnel_rcu(t, ign->tunnels_r_l[h0 ^ h1]) { |
c12b395a | 171 | if (!ipv6_addr_equal(local, &t->parms.laddr) || |
172 | !ipv6_addr_equal(remote, &t->parms.raddr) || | |
173 | key != t->parms.i_key || | |
174 | !(t->dev->flags & IFF_UP)) | |
175 | continue; | |
176 | ||
177 | if (t->dev->type != ARPHRD_IP6GRE && | |
178 | t->dev->type != dev_type) | |
179 | continue; | |
180 | ||
181 | score = 0; | |
182 | if (t->parms.link != link) | |
183 | score |= 1; | |
184 | if (t->dev->type != dev_type) | |
185 | score |= 2; | |
186 | if (score == 0) | |
187 | return t; | |
188 | ||
189 | if (score < cand_score) { | |
190 | cand = t; | |
191 | cand_score = score; | |
192 | } | |
193 | } | |
194 | ||
e086cadc | 195 | for_each_ip_tunnel_rcu(t, ign->tunnels_r[h0 ^ h1]) { |
c12b395a | 196 | if (!ipv6_addr_equal(remote, &t->parms.raddr) || |
197 | key != t->parms.i_key || | |
198 | !(t->dev->flags & IFF_UP)) | |
199 | continue; | |
200 | ||
201 | if (t->dev->type != ARPHRD_IP6GRE && | |
202 | t->dev->type != dev_type) | |
203 | continue; | |
204 | ||
205 | score = 0; | |
206 | if (t->parms.link != link) | |
207 | score |= 1; | |
208 | if (t->dev->type != dev_type) | |
209 | score |= 2; | |
210 | if (score == 0) | |
211 | return t; | |
212 | ||
213 | if (score < cand_score) { | |
214 | cand = t; | |
215 | cand_score = score; | |
216 | } | |
217 | } | |
218 | ||
e086cadc | 219 | for_each_ip_tunnel_rcu(t, ign->tunnels_l[h1]) { |
c12b395a | 220 | if ((!ipv6_addr_equal(local, &t->parms.laddr) && |
221 | (!ipv6_addr_equal(local, &t->parms.raddr) || | |
222 | !ipv6_addr_is_multicast(local))) || | |
223 | key != t->parms.i_key || | |
224 | !(t->dev->flags & IFF_UP)) | |
225 | continue; | |
226 | ||
227 | if (t->dev->type != ARPHRD_IP6GRE && | |
228 | t->dev->type != dev_type) | |
229 | continue; | |
230 | ||
231 | score = 0; | |
232 | if (t->parms.link != link) | |
233 | score |= 1; | |
234 | if (t->dev->type != dev_type) | |
235 | score |= 2; | |
236 | if (score == 0) | |
237 | return t; | |
238 | ||
239 | if (score < cand_score) { | |
240 | cand = t; | |
241 | cand_score = score; | |
242 | } | |
243 | } | |
244 | ||
e086cadc | 245 | for_each_ip_tunnel_rcu(t, ign->tunnels_wc[h1]) { |
c12b395a | 246 | if (t->parms.i_key != key || |
247 | !(t->dev->flags & IFF_UP)) | |
248 | continue; | |
249 | ||
250 | if (t->dev->type != ARPHRD_IP6GRE && | |
251 | t->dev->type != dev_type) | |
252 | continue; | |
253 | ||
254 | score = 0; | |
255 | if (t->parms.link != link) | |
256 | score |= 1; | |
257 | if (t->dev->type != dev_type) | |
258 | score |= 2; | |
259 | if (score == 0) | |
260 | return t; | |
261 | ||
262 | if (score < cand_score) { | |
263 | cand = t; | |
264 | cand_score = score; | |
265 | } | |
266 | } | |
267 | ||
268 | if (cand != NULL) | |
269 | return cand; | |
270 | ||
271 | dev = ign->fb_tunnel_dev; | |
272 | if (dev->flags & IFF_UP) | |
273 | return netdev_priv(dev); | |
274 | ||
275 | return NULL; | |
276 | } | |
277 | ||
278 | static struct ip6_tnl __rcu **__ip6gre_bucket(struct ip6gre_net *ign, | |
279 | const struct __ip6_tnl_parm *p) | |
280 | { | |
281 | const struct in6_addr *remote = &p->raddr; | |
282 | const struct in6_addr *local = &p->laddr; | |
283 | unsigned int h = HASH_KEY(p->i_key); | |
284 | int prio = 0; | |
285 | ||
286 | if (!ipv6_addr_any(local)) | |
287 | prio |= 1; | |
288 | if (!ipv6_addr_any(remote) && !ipv6_addr_is_multicast(remote)) { | |
289 | prio |= 2; | |
290 | h ^= HASH_ADDR(remote); | |
291 | } | |
292 | ||
293 | return &ign->tunnels[prio][h]; | |
294 | } | |
295 | ||
296 | static inline struct ip6_tnl __rcu **ip6gre_bucket(struct ip6gre_net *ign, | |
297 | const struct ip6_tnl *t) | |
298 | { | |
299 | return __ip6gre_bucket(ign, &t->parms); | |
300 | } | |
301 | ||
302 | static void ip6gre_tunnel_link(struct ip6gre_net *ign, struct ip6_tnl *t) | |
303 | { | |
304 | struct ip6_tnl __rcu **tp = ip6gre_bucket(ign, t); | |
305 | ||
306 | rcu_assign_pointer(t->next, rtnl_dereference(*tp)); | |
307 | rcu_assign_pointer(*tp, t); | |
308 | } | |
309 | ||
310 | static void ip6gre_tunnel_unlink(struct ip6gre_net *ign, struct ip6_tnl *t) | |
311 | { | |
312 | struct ip6_tnl __rcu **tp; | |
313 | struct ip6_tnl *iter; | |
314 | ||
315 | for (tp = ip6gre_bucket(ign, t); | |
316 | (iter = rtnl_dereference(*tp)) != NULL; | |
317 | tp = &iter->next) { | |
318 | if (t == iter) { | |
319 | rcu_assign_pointer(*tp, t->next); | |
320 | break; | |
321 | } | |
322 | } | |
323 | } | |
324 | ||
325 | static struct ip6_tnl *ip6gre_tunnel_find(struct net *net, | |
326 | const struct __ip6_tnl_parm *parms, | |
327 | int type) | |
328 | { | |
329 | const struct in6_addr *remote = &parms->raddr; | |
330 | const struct in6_addr *local = &parms->laddr; | |
331 | __be32 key = parms->i_key; | |
332 | int link = parms->link; | |
333 | struct ip6_tnl *t; | |
334 | struct ip6_tnl __rcu **tp; | |
335 | struct ip6gre_net *ign = net_generic(net, ip6gre_net_id); | |
336 | ||
337 | for (tp = __ip6gre_bucket(ign, parms); | |
338 | (t = rtnl_dereference(*tp)) != NULL; | |
339 | tp = &t->next) | |
340 | if (ipv6_addr_equal(local, &t->parms.laddr) && | |
341 | ipv6_addr_equal(remote, &t->parms.raddr) && | |
342 | key == t->parms.i_key && | |
343 | link == t->parms.link && | |
344 | type == t->dev->type) | |
345 | break; | |
346 | ||
347 | return t; | |
348 | } | |
349 | ||
350 | static struct ip6_tnl *ip6gre_tunnel_locate(struct net *net, | |
351 | const struct __ip6_tnl_parm *parms, int create) | |
352 | { | |
353 | struct ip6_tnl *t, *nt; | |
354 | struct net_device *dev; | |
355 | char name[IFNAMSIZ]; | |
356 | struct ip6gre_net *ign = net_generic(net, ip6gre_net_id); | |
357 | ||
358 | t = ip6gre_tunnel_find(net, parms, ARPHRD_IP6GRE); | |
359 | if (t || !create) | |
360 | return t; | |
361 | ||
362 | if (parms->name[0]) | |
363 | strlcpy(name, parms->name, IFNAMSIZ); | |
364 | else | |
365 | strcpy(name, "ip6gre%d"); | |
366 | ||
367 | dev = alloc_netdev(sizeof(*t), name, ip6gre_tunnel_setup); | |
368 | if (!dev) | |
369 | return NULL; | |
370 | ||
371 | dev_net_set(dev, net); | |
372 | ||
373 | nt = netdev_priv(dev); | |
374 | nt->parms = *parms; | |
375 | dev->rtnl_link_ops = &ip6gre_link_ops; | |
376 | ||
377 | nt->dev = dev; | |
378 | ip6gre_tnl_link_config(nt, 1); | |
379 | ||
380 | if (register_netdevice(dev) < 0) | |
381 | goto failed_free; | |
382 | ||
383 | /* Can use a lockless transmit, unless we generate output sequences */ | |
384 | if (!(nt->parms.o_flags & GRE_SEQ)) | |
385 | dev->features |= NETIF_F_LLTX; | |
386 | ||
387 | dev_hold(dev); | |
388 | ip6gre_tunnel_link(ign, nt); | |
389 | return nt; | |
390 | ||
391 | failed_free: | |
392 | free_netdev(dev); | |
393 | return NULL; | |
394 | } | |
395 | ||
396 | static void ip6gre_tunnel_uninit(struct net_device *dev) | |
397 | { | |
398 | struct net *net = dev_net(dev); | |
399 | struct ip6gre_net *ign = net_generic(net, ip6gre_net_id); | |
400 | ||
401 | ip6gre_tunnel_unlink(ign, netdev_priv(dev)); | |
402 | dev_put(dev); | |
403 | } | |
404 | ||
405 | ||
406 | static void ip6gre_err(struct sk_buff *skb, struct inet6_skb_parm *opt, | |
407 | u8 type, u8 code, int offset, __be32 info) | |
408 | { | |
409 | const struct ipv6hdr *ipv6h = (const struct ipv6hdr *)skb->data; | |
b87fb39e ED |
410 | __be16 *p = (__be16 *)(skb->data + offset); |
411 | int grehlen = offset + 4; | |
c12b395a | 412 | struct ip6_tnl *t; |
413 | __be16 flags; | |
414 | ||
415 | flags = p[0]; | |
416 | if (flags&(GRE_CSUM|GRE_KEY|GRE_SEQ|GRE_ROUTING|GRE_VERSION)) { | |
417 | if (flags&(GRE_VERSION|GRE_ROUTING)) | |
418 | return; | |
419 | if (flags&GRE_KEY) { | |
420 | grehlen += 4; | |
421 | if (flags&GRE_CSUM) | |
422 | grehlen += 4; | |
423 | } | |
424 | } | |
425 | ||
426 | /* If only 8 bytes returned, keyed message will be dropped here */ | |
b87fb39e | 427 | if (!pskb_may_pull(skb, grehlen)) |
c12b395a | 428 | return; |
b87fb39e ED |
429 | ipv6h = (const struct ipv6hdr *)skb->data; |
430 | p = (__be16 *)(skb->data + offset); | |
c12b395a | 431 | |
c12b395a | 432 | t = ip6gre_tunnel_lookup(skb->dev, &ipv6h->daddr, &ipv6h->saddr, |
433 | flags & GRE_KEY ? | |
434 | *(((__be32 *)p) + (grehlen / 4) - 1) : 0, | |
435 | p[1]); | |
436 | if (t == NULL) | |
0c5794a6 | 437 | return; |
c12b395a | 438 | |
439 | switch (type) { | |
440 | __u32 teli; | |
441 | struct ipv6_tlv_tnl_enc_lim *tel; | |
442 | __u32 mtu; | |
443 | case ICMPV6_DEST_UNREACH: | |
444 | net_warn_ratelimited("%s: Path to destination invalid or inactive!\n", | |
445 | t->parms.name); | |
446 | break; | |
447 | case ICMPV6_TIME_EXCEED: | |
448 | if (code == ICMPV6_EXC_HOPLIMIT) { | |
449 | net_warn_ratelimited("%s: Too small hop limit or routing loop in tunnel!\n", | |
450 | t->parms.name); | |
451 | } | |
452 | break; | |
453 | case ICMPV6_PARAMPROB: | |
454 | teli = 0; | |
455 | if (code == ICMPV6_HDR_FIELD) | |
456 | teli = ip6_tnl_parse_tlv_enc_lim(skb, skb->data); | |
457 | ||
458 | if (teli && teli == info - 2) { | |
459 | tel = (struct ipv6_tlv_tnl_enc_lim *) &skb->data[teli]; | |
460 | if (tel->encap_limit == 0) { | |
461 | net_warn_ratelimited("%s: Too small encapsulation limit or routing loop in tunnel!\n", | |
462 | t->parms.name); | |
463 | } | |
464 | } else { | |
465 | net_warn_ratelimited("%s: Recipient unable to parse tunneled packet!\n", | |
466 | t->parms.name); | |
467 | } | |
468 | break; | |
469 | case ICMPV6_PKT_TOOBIG: | |
470 | mtu = info - offset; | |
471 | if (mtu < IPV6_MIN_MTU) | |
472 | mtu = IPV6_MIN_MTU; | |
473 | t->dev->mtu = mtu; | |
474 | break; | |
475 | } | |
476 | ||
477 | if (time_before(jiffies, t->err_time + IP6TUNNEL_ERR_TIMEO)) | |
478 | t->err_count++; | |
479 | else | |
480 | t->err_count = 1; | |
481 | t->err_time = jiffies; | |
c12b395a | 482 | } |
483 | ||
c12b395a | 484 | static int ip6gre_rcv(struct sk_buff *skb) |
485 | { | |
486 | const struct ipv6hdr *ipv6h; | |
487 | u8 *h; | |
488 | __be16 flags; | |
489 | __sum16 csum = 0; | |
490 | __be32 key = 0; | |
491 | u32 seqno = 0; | |
492 | struct ip6_tnl *tunnel; | |
493 | int offset = 4; | |
494 | __be16 gre_proto; | |
eccc1bb8 | 495 | int err; |
c12b395a | 496 | |
497 | if (!pskb_may_pull(skb, sizeof(struct in6_addr))) | |
0c5794a6 | 498 | goto drop; |
c12b395a | 499 | |
500 | ipv6h = ipv6_hdr(skb); | |
501 | h = skb->data; | |
502 | flags = *(__be16 *)h; | |
503 | ||
504 | if (flags&(GRE_CSUM|GRE_KEY|GRE_ROUTING|GRE_SEQ|GRE_VERSION)) { | |
505 | /* - Version must be 0. | |
506 | - We do not support routing headers. | |
507 | */ | |
508 | if (flags&(GRE_VERSION|GRE_ROUTING)) | |
0c5794a6 | 509 | goto drop; |
c12b395a | 510 | |
511 | if (flags&GRE_CSUM) { | |
512 | switch (skb->ip_summed) { | |
513 | case CHECKSUM_COMPLETE: | |
514 | csum = csum_fold(skb->csum); | |
515 | if (!csum) | |
516 | break; | |
517 | /* fall through */ | |
518 | case CHECKSUM_NONE: | |
519 | skb->csum = 0; | |
520 | csum = __skb_checksum_complete(skb); | |
521 | skb->ip_summed = CHECKSUM_COMPLETE; | |
522 | } | |
523 | offset += 4; | |
524 | } | |
525 | if (flags&GRE_KEY) { | |
526 | key = *(__be32 *)(h + offset); | |
527 | offset += 4; | |
528 | } | |
529 | if (flags&GRE_SEQ) { | |
530 | seqno = ntohl(*(__be32 *)(h + offset)); | |
531 | offset += 4; | |
532 | } | |
533 | } | |
534 | ||
535 | gre_proto = *(__be16 *)(h + 2); | |
536 | ||
c12b395a | 537 | tunnel = ip6gre_tunnel_lookup(skb->dev, |
538 | &ipv6h->saddr, &ipv6h->daddr, key, | |
539 | gre_proto); | |
540 | if (tunnel) { | |
541 | struct pcpu_tstats *tstats; | |
542 | ||
543 | if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb)) | |
544 | goto drop; | |
545 | ||
546 | if (!ip6_tnl_rcv_ctl(tunnel, &ipv6h->daddr, &ipv6h->saddr)) { | |
547 | tunnel->dev->stats.rx_dropped++; | |
548 | goto drop; | |
549 | } | |
550 | ||
551 | secpath_reset(skb); | |
552 | ||
553 | skb->protocol = gre_proto; | |
554 | /* WCCP version 1 and 2 protocol decoding. | |
555 | * - Change protocol to IP | |
556 | * - When dealing with WCCPv2, Skip extra 4 bytes in GRE header | |
557 | */ | |
558 | if (flags == 0 && gre_proto == htons(ETH_P_WCCP)) { | |
559 | skb->protocol = htons(ETH_P_IP); | |
560 | if ((*(h + offset) & 0xF0) != 0x40) | |
561 | offset += 4; | |
562 | } | |
563 | ||
564 | skb->mac_header = skb->network_header; | |
565 | __pskb_pull(skb, offset); | |
566 | skb_postpull_rcsum(skb, skb_transport_header(skb), offset); | |
567 | skb->pkt_type = PACKET_HOST; | |
568 | ||
569 | if (((flags&GRE_CSUM) && csum) || | |
570 | (!(flags&GRE_CSUM) && tunnel->parms.i_flags&GRE_CSUM)) { | |
571 | tunnel->dev->stats.rx_crc_errors++; | |
572 | tunnel->dev->stats.rx_errors++; | |
573 | goto drop; | |
574 | } | |
575 | if (tunnel->parms.i_flags&GRE_SEQ) { | |
576 | if (!(flags&GRE_SEQ) || | |
577 | (tunnel->i_seqno && | |
578 | (s32)(seqno - tunnel->i_seqno) < 0)) { | |
579 | tunnel->dev->stats.rx_fifo_errors++; | |
580 | tunnel->dev->stats.rx_errors++; | |
581 | goto drop; | |
582 | } | |
583 | tunnel->i_seqno = seqno + 1; | |
584 | } | |
585 | ||
586 | /* Warning: All skb pointers will be invalidated! */ | |
587 | if (tunnel->dev->type == ARPHRD_ETHER) { | |
588 | if (!pskb_may_pull(skb, ETH_HLEN)) { | |
589 | tunnel->dev->stats.rx_length_errors++; | |
590 | tunnel->dev->stats.rx_errors++; | |
591 | goto drop; | |
592 | } | |
593 | ||
594 | ipv6h = ipv6_hdr(skb); | |
595 | skb->protocol = eth_type_trans(skb, tunnel->dev); | |
596 | skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN); | |
597 | } | |
598 | ||
eccc1bb8 | 599 | __skb_tunnel_rx(skb, tunnel->dev); |
600 | ||
601 | skb_reset_network_header(skb); | |
602 | ||
603 | err = IP6_ECN_decapsulate(ipv6h, skb); | |
604 | if (unlikely(err)) { | |
605 | if (log_ecn_error) | |
606 | net_info_ratelimited("non-ECT from %pI6 with dsfield=%#x\n", | |
607 | &ipv6h->saddr, | |
608 | ipv6_get_dsfield(ipv6h)); | |
609 | if (err > 1) { | |
610 | ++tunnel->dev->stats.rx_frame_errors; | |
611 | ++tunnel->dev->stats.rx_errors; | |
612 | goto drop; | |
613 | } | |
614 | } | |
615 | ||
c12b395a | 616 | tstats = this_cpu_ptr(tunnel->dev->tstats); |
617 | u64_stats_update_begin(&tstats->syncp); | |
618 | tstats->rx_packets++; | |
619 | tstats->rx_bytes += skb->len; | |
620 | u64_stats_update_end(&tstats->syncp); | |
621 | ||
c12b395a | 622 | netif_rx(skb); |
623 | ||
c12b395a | 624 | return 0; |
625 | } | |
626 | icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0); | |
627 | ||
628 | drop: | |
c12b395a | 629 | kfree_skb(skb); |
630 | return 0; | |
631 | } | |
632 | ||
633 | struct ipv6_tel_txoption { | |
634 | struct ipv6_txoptions ops; | |
635 | __u8 dst_opt[8]; | |
636 | }; | |
637 | ||
638 | static void init_tel_txopt(struct ipv6_tel_txoption *opt, __u8 encap_limit) | |
639 | { | |
640 | memset(opt, 0, sizeof(struct ipv6_tel_txoption)); | |
641 | ||
642 | opt->dst_opt[2] = IPV6_TLV_TNL_ENCAP_LIMIT; | |
643 | opt->dst_opt[3] = 1; | |
644 | opt->dst_opt[4] = encap_limit; | |
645 | opt->dst_opt[5] = IPV6_TLV_PADN; | |
646 | opt->dst_opt[6] = 1; | |
647 | ||
648 | opt->ops.dst0opt = (struct ipv6_opt_hdr *) opt->dst_opt; | |
649 | opt->ops.opt_nflen = 8; | |
650 | } | |
651 | ||
652 | static netdev_tx_t ip6gre_xmit2(struct sk_buff *skb, | |
653 | struct net_device *dev, | |
654 | __u8 dsfield, | |
655 | struct flowi6 *fl6, | |
656 | int encap_limit, | |
657 | __u32 *pmtu) | |
658 | { | |
659 | struct net *net = dev_net(dev); | |
660 | struct ip6_tnl *tunnel = netdev_priv(dev); | |
661 | struct net_device *tdev; /* Device to other host */ | |
662 | struct ipv6hdr *ipv6h; /* Our new IP header */ | |
663 | unsigned int max_headroom; /* The extra header space needed */ | |
664 | int gre_hlen; | |
665 | struct ipv6_tel_txoption opt; | |
666 | int mtu; | |
667 | struct dst_entry *dst = NULL, *ndst = NULL; | |
668 | struct net_device_stats *stats = &tunnel->dev->stats; | |
669 | int err = -1; | |
670 | u8 proto; | |
c12b395a | 671 | struct sk_buff *new_skb; |
672 | ||
673 | if (dev->type == ARPHRD_ETHER) | |
674 | IPCB(skb)->flags = 0; | |
675 | ||
676 | if (dev->header_ops && dev->type == ARPHRD_IP6GRE) { | |
677 | gre_hlen = 0; | |
678 | ipv6h = (struct ipv6hdr *)skb->data; | |
679 | fl6->daddr = ipv6h->daddr; | |
680 | } else { | |
681 | gre_hlen = tunnel->hlen; | |
682 | fl6->daddr = tunnel->parms.raddr; | |
683 | } | |
684 | ||
685 | if (!fl6->flowi6_mark) | |
686 | dst = ip6_tnl_dst_check(tunnel); | |
687 | ||
688 | if (!dst) { | |
689 | ndst = ip6_route_output(net, NULL, fl6); | |
690 | ||
691 | if (ndst->error) | |
692 | goto tx_err_link_failure; | |
693 | ndst = xfrm_lookup(net, ndst, flowi6_to_flowi(fl6), NULL, 0); | |
694 | if (IS_ERR(ndst)) { | |
695 | err = PTR_ERR(ndst); | |
696 | ndst = NULL; | |
697 | goto tx_err_link_failure; | |
698 | } | |
699 | dst = ndst; | |
700 | } | |
701 | ||
702 | tdev = dst->dev; | |
703 | ||
704 | if (tdev == dev) { | |
705 | stats->collisions++; | |
706 | net_warn_ratelimited("%s: Local routing loop detected!\n", | |
707 | tunnel->parms.name); | |
708 | goto tx_err_dst_release; | |
709 | } | |
710 | ||
711 | mtu = dst_mtu(dst) - sizeof(*ipv6h); | |
712 | if (encap_limit >= 0) { | |
713 | max_headroom += 8; | |
714 | mtu -= 8; | |
715 | } | |
716 | if (mtu < IPV6_MIN_MTU) | |
717 | mtu = IPV6_MIN_MTU; | |
718 | if (skb_dst(skb)) | |
719 | skb_dst(skb)->ops->update_pmtu(skb_dst(skb), NULL, skb, mtu); | |
720 | if (skb->len > mtu) { | |
721 | *pmtu = mtu; | |
722 | err = -EMSGSIZE; | |
723 | goto tx_err_dst_release; | |
724 | } | |
725 | ||
726 | if (tunnel->err_count > 0) { | |
727 | if (time_before(jiffies, | |
728 | tunnel->err_time + IP6TUNNEL_ERR_TIMEO)) { | |
729 | tunnel->err_count--; | |
730 | ||
731 | dst_link_failure(skb); | |
732 | } else | |
733 | tunnel->err_count = 0; | |
734 | } | |
735 | ||
736 | max_headroom = LL_RESERVED_SPACE(tdev) + gre_hlen + dst->header_len; | |
737 | ||
738 | if (skb_headroom(skb) < max_headroom || skb_shared(skb) || | |
739 | (skb_cloned(skb) && !skb_clone_writable(skb, 0))) { | |
740 | new_skb = skb_realloc_headroom(skb, max_headroom); | |
741 | if (max_headroom > dev->needed_headroom) | |
742 | dev->needed_headroom = max_headroom; | |
743 | if (!new_skb) | |
744 | goto tx_err_dst_release; | |
745 | ||
746 | if (skb->sk) | |
747 | skb_set_owner_w(new_skb, skb->sk); | |
748 | consume_skb(skb); | |
749 | skb = new_skb; | |
750 | } | |
751 | ||
752 | skb_dst_drop(skb); | |
753 | ||
754 | if (fl6->flowi6_mark) { | |
755 | skb_dst_set(skb, dst); | |
756 | ndst = NULL; | |
757 | } else { | |
758 | skb_dst_set_noref(skb, dst); | |
759 | } | |
760 | ||
c12b395a | 761 | proto = NEXTHDR_GRE; |
762 | if (encap_limit >= 0) { | |
763 | init_tel_txopt(&opt, encap_limit); | |
764 | ipv6_push_nfrag_opts(skb, &opt.ops, &proto, NULL); | |
765 | } | |
766 | ||
767 | skb_push(skb, gre_hlen); | |
768 | skb_reset_network_header(skb); | |
ae782bb1 | 769 | skb_set_transport_header(skb, sizeof(*ipv6h)); |
c12b395a | 770 | |
771 | /* | |
772 | * Push down and install the IP header. | |
773 | */ | |
774 | ipv6h = ipv6_hdr(skb); | |
3e4e4c1f | 775 | ip6_flow_hdr(ipv6h, INET_ECN_encapsulate(0, dsfield), fl6->flowlabel); |
c12b395a | 776 | ipv6h->hop_limit = tunnel->parms.hop_limit; |
777 | ipv6h->nexthdr = proto; | |
778 | ipv6h->saddr = fl6->saddr; | |
779 | ipv6h->daddr = fl6->daddr; | |
780 | ||
781 | ((__be16 *)(ipv6h + 1))[0] = tunnel->parms.o_flags; | |
782 | ((__be16 *)(ipv6h + 1))[1] = (dev->type == ARPHRD_ETHER) ? | |
783 | htons(ETH_P_TEB) : skb->protocol; | |
784 | ||
785 | if (tunnel->parms.o_flags&(GRE_KEY|GRE_CSUM|GRE_SEQ)) { | |
786 | __be32 *ptr = (__be32 *)(((u8 *)ipv6h) + tunnel->hlen - 4); | |
787 | ||
788 | if (tunnel->parms.o_flags&GRE_SEQ) { | |
789 | ++tunnel->o_seqno; | |
790 | *ptr = htonl(tunnel->o_seqno); | |
791 | ptr--; | |
792 | } | |
793 | if (tunnel->parms.o_flags&GRE_KEY) { | |
794 | *ptr = tunnel->parms.o_key; | |
795 | ptr--; | |
796 | } | |
797 | if (tunnel->parms.o_flags&GRE_CSUM) { | |
798 | *ptr = 0; | |
799 | *(__sum16 *)ptr = ip_compute_csum((void *)(ipv6h+1), | |
800 | skb->len - sizeof(struct ipv6hdr)); | |
801 | } | |
802 | } | |
803 | ||
e8f72ea4 | 804 | ip6tunnel_xmit(skb, dev); |
c12b395a | 805 | if (ndst) |
806 | ip6_tnl_dst_store(tunnel, ndst); | |
c12b395a | 807 | return 0; |
808 | tx_err_link_failure: | |
809 | stats->tx_carrier_errors++; | |
810 | dst_link_failure(skb); | |
811 | tx_err_dst_release: | |
812 | dst_release(ndst); | |
813 | return err; | |
814 | } | |
815 | ||
816 | static inline int ip6gre_xmit_ipv4(struct sk_buff *skb, struct net_device *dev) | |
817 | { | |
818 | struct ip6_tnl *t = netdev_priv(dev); | |
819 | const struct iphdr *iph = ip_hdr(skb); | |
820 | int encap_limit = -1; | |
821 | struct flowi6 fl6; | |
822 | __u8 dsfield; | |
823 | __u32 mtu; | |
824 | int err; | |
825 | ||
826 | if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT)) | |
827 | encap_limit = t->parms.encap_limit; | |
828 | ||
829 | memcpy(&fl6, &t->fl.u.ip6, sizeof(fl6)); | |
830 | fl6.flowi6_proto = IPPROTO_IPIP; | |
831 | ||
832 | dsfield = ipv4_get_dsfield(iph); | |
833 | ||
834 | if (t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS) | |
835 | fl6.flowlabel |= htonl((__u32)iph->tos << IPV6_TCLASS_SHIFT) | |
836 | & IPV6_TCLASS_MASK; | |
837 | if (t->parms.flags & IP6_TNL_F_USE_ORIG_FWMARK) | |
838 | fl6.flowi6_mark = skb->mark; | |
839 | ||
840 | err = ip6gre_xmit2(skb, dev, dsfield, &fl6, encap_limit, &mtu); | |
841 | if (err != 0) { | |
842 | /* XXX: send ICMP error even if DF is not set. */ | |
843 | if (err == -EMSGSIZE) | |
844 | icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED, | |
845 | htonl(mtu)); | |
846 | return -1; | |
847 | } | |
848 | ||
849 | return 0; | |
850 | } | |
851 | ||
852 | static inline int ip6gre_xmit_ipv6(struct sk_buff *skb, struct net_device *dev) | |
853 | { | |
854 | struct ip6_tnl *t = netdev_priv(dev); | |
855 | struct ipv6hdr *ipv6h = ipv6_hdr(skb); | |
856 | int encap_limit = -1; | |
857 | __u16 offset; | |
858 | struct flowi6 fl6; | |
859 | __u8 dsfield; | |
860 | __u32 mtu; | |
861 | int err; | |
862 | ||
863 | if (ipv6_addr_equal(&t->parms.raddr, &ipv6h->saddr)) | |
864 | return -1; | |
865 | ||
866 | offset = ip6_tnl_parse_tlv_enc_lim(skb, skb_network_header(skb)); | |
867 | if (offset > 0) { | |
868 | struct ipv6_tlv_tnl_enc_lim *tel; | |
869 | tel = (struct ipv6_tlv_tnl_enc_lim *)&skb_network_header(skb)[offset]; | |
870 | if (tel->encap_limit == 0) { | |
871 | icmpv6_send(skb, ICMPV6_PARAMPROB, | |
872 | ICMPV6_HDR_FIELD, offset + 2); | |
873 | return -1; | |
874 | } | |
875 | encap_limit = tel->encap_limit - 1; | |
876 | } else if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT)) | |
877 | encap_limit = t->parms.encap_limit; | |
878 | ||
879 | memcpy(&fl6, &t->fl.u.ip6, sizeof(fl6)); | |
880 | fl6.flowi6_proto = IPPROTO_IPV6; | |
881 | ||
882 | dsfield = ipv6_get_dsfield(ipv6h); | |
883 | if (t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS) | |
884 | fl6.flowlabel |= (*(__be32 *) ipv6h & IPV6_TCLASS_MASK); | |
885 | if (t->parms.flags & IP6_TNL_F_USE_ORIG_FLOWLABEL) | |
886 | fl6.flowlabel |= (*(__be32 *) ipv6h & IPV6_FLOWLABEL_MASK); | |
887 | if (t->parms.flags & IP6_TNL_F_USE_ORIG_FWMARK) | |
888 | fl6.flowi6_mark = skb->mark; | |
889 | ||
890 | err = ip6gre_xmit2(skb, dev, dsfield, &fl6, encap_limit, &mtu); | |
891 | if (err != 0) { | |
892 | if (err == -EMSGSIZE) | |
893 | icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu); | |
894 | return -1; | |
895 | } | |
896 | ||
897 | return 0; | |
898 | } | |
899 | ||
900 | /** | |
901 | * ip6_tnl_addr_conflict - compare packet addresses to tunnel's own | |
902 | * @t: the outgoing tunnel device | |
903 | * @hdr: IPv6 header from the incoming packet | |
904 | * | |
905 | * Description: | |
906 | * Avoid trivial tunneling loop by checking that tunnel exit-point | |
907 | * doesn't match source of incoming packet. | |
908 | * | |
909 | * Return: | |
910 | * 1 if conflict, | |
911 | * 0 else | |
912 | **/ | |
913 | ||
914 | static inline bool ip6gre_tnl_addr_conflict(const struct ip6_tnl *t, | |
915 | const struct ipv6hdr *hdr) | |
916 | { | |
917 | return ipv6_addr_equal(&t->parms.raddr, &hdr->saddr); | |
918 | } | |
919 | ||
920 | static int ip6gre_xmit_other(struct sk_buff *skb, struct net_device *dev) | |
921 | { | |
922 | struct ip6_tnl *t = netdev_priv(dev); | |
923 | int encap_limit = -1; | |
924 | struct flowi6 fl6; | |
925 | __u32 mtu; | |
926 | int err; | |
927 | ||
928 | if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT)) | |
929 | encap_limit = t->parms.encap_limit; | |
930 | ||
931 | memcpy(&fl6, &t->fl.u.ip6, sizeof(fl6)); | |
932 | fl6.flowi6_proto = skb->protocol; | |
933 | ||
934 | err = ip6gre_xmit2(skb, dev, 0, &fl6, encap_limit, &mtu); | |
935 | ||
936 | return err; | |
937 | } | |
938 | ||
939 | static netdev_tx_t ip6gre_tunnel_xmit(struct sk_buff *skb, | |
940 | struct net_device *dev) | |
941 | { | |
942 | struct ip6_tnl *t = netdev_priv(dev); | |
943 | struct net_device_stats *stats = &t->dev->stats; | |
944 | int ret; | |
945 | ||
946 | if (!ip6_tnl_xmit_ctl(t)) | |
41ab3e31 | 947 | goto tx_err; |
c12b395a | 948 | |
949 | switch (skb->protocol) { | |
950 | case htons(ETH_P_IP): | |
951 | ret = ip6gre_xmit_ipv4(skb, dev); | |
952 | break; | |
953 | case htons(ETH_P_IPV6): | |
954 | ret = ip6gre_xmit_ipv6(skb, dev); | |
955 | break; | |
956 | default: | |
957 | ret = ip6gre_xmit_other(skb, dev); | |
958 | break; | |
959 | } | |
960 | ||
961 | if (ret < 0) | |
962 | goto tx_err; | |
963 | ||
964 | return NETDEV_TX_OK; | |
965 | ||
966 | tx_err: | |
967 | stats->tx_errors++; | |
968 | stats->tx_dropped++; | |
969 | kfree_skb(skb); | |
970 | return NETDEV_TX_OK; | |
971 | } | |
972 | ||
973 | static void ip6gre_tnl_link_config(struct ip6_tnl *t, int set_mtu) | |
974 | { | |
975 | struct net_device *dev = t->dev; | |
976 | struct __ip6_tnl_parm *p = &t->parms; | |
977 | struct flowi6 *fl6 = &t->fl.u.ip6; | |
978 | int addend = sizeof(struct ipv6hdr) + 4; | |
979 | ||
980 | if (dev->type != ARPHRD_ETHER) { | |
981 | memcpy(dev->dev_addr, &p->laddr, sizeof(struct in6_addr)); | |
982 | memcpy(dev->broadcast, &p->raddr, sizeof(struct in6_addr)); | |
983 | } | |
984 | ||
985 | /* Set up flowi template */ | |
986 | fl6->saddr = p->laddr; | |
987 | fl6->daddr = p->raddr; | |
988 | fl6->flowi6_oif = p->link; | |
989 | fl6->flowlabel = 0; | |
990 | ||
991 | if (!(p->flags&IP6_TNL_F_USE_ORIG_TCLASS)) | |
992 | fl6->flowlabel |= IPV6_TCLASS_MASK & p->flowinfo; | |
993 | if (!(p->flags&IP6_TNL_F_USE_ORIG_FLOWLABEL)) | |
994 | fl6->flowlabel |= IPV6_FLOWLABEL_MASK & p->flowinfo; | |
995 | ||
996 | p->flags &= ~(IP6_TNL_F_CAP_XMIT|IP6_TNL_F_CAP_RCV|IP6_TNL_F_CAP_PER_PACKET); | |
997 | p->flags |= ip6_tnl_get_cap(t, &p->laddr, &p->raddr); | |
998 | ||
999 | if (p->flags&IP6_TNL_F_CAP_XMIT && | |
1000 | p->flags&IP6_TNL_F_CAP_RCV && dev->type != ARPHRD_ETHER) | |
1001 | dev->flags |= IFF_POINTOPOINT; | |
1002 | else | |
1003 | dev->flags &= ~IFF_POINTOPOINT; | |
1004 | ||
1005 | dev->iflink = p->link; | |
1006 | ||
1007 | /* Precalculate GRE options length */ | |
1008 | if (t->parms.o_flags&(GRE_CSUM|GRE_KEY|GRE_SEQ)) { | |
1009 | if (t->parms.o_flags&GRE_CSUM) | |
1010 | addend += 4; | |
1011 | if (t->parms.o_flags&GRE_KEY) | |
1012 | addend += 4; | |
1013 | if (t->parms.o_flags&GRE_SEQ) | |
1014 | addend += 4; | |
1015 | } | |
1016 | ||
1017 | if (p->flags & IP6_TNL_F_CAP_XMIT) { | |
1018 | int strict = (ipv6_addr_type(&p->raddr) & | |
1019 | (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL)); | |
1020 | ||
1021 | struct rt6_info *rt = rt6_lookup(dev_net(dev), | |
1022 | &p->raddr, &p->laddr, | |
1023 | p->link, strict); | |
1024 | ||
1025 | if (rt == NULL) | |
1026 | return; | |
1027 | ||
1028 | if (rt->dst.dev) { | |
1029 | dev->hard_header_len = rt->dst.dev->hard_header_len + addend; | |
1030 | ||
1031 | if (set_mtu) { | |
1032 | dev->mtu = rt->dst.dev->mtu - addend; | |
1033 | if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT)) | |
1034 | dev->mtu -= 8; | |
1035 | ||
1036 | if (dev->mtu < IPV6_MIN_MTU) | |
1037 | dev->mtu = IPV6_MIN_MTU; | |
1038 | } | |
1039 | } | |
94e187c0 | 1040 | ip6_rt_put(rt); |
c12b395a | 1041 | } |
1042 | ||
1043 | t->hlen = addend; | |
1044 | } | |
1045 | ||
1046 | static int ip6gre_tnl_change(struct ip6_tnl *t, | |
1047 | const struct __ip6_tnl_parm *p, int set_mtu) | |
1048 | { | |
1049 | t->parms.laddr = p->laddr; | |
1050 | t->parms.raddr = p->raddr; | |
1051 | t->parms.flags = p->flags; | |
1052 | t->parms.hop_limit = p->hop_limit; | |
1053 | t->parms.encap_limit = p->encap_limit; | |
1054 | t->parms.flowinfo = p->flowinfo; | |
1055 | t->parms.link = p->link; | |
1056 | t->parms.proto = p->proto; | |
1057 | t->parms.i_key = p->i_key; | |
1058 | t->parms.o_key = p->o_key; | |
1059 | t->parms.i_flags = p->i_flags; | |
1060 | t->parms.o_flags = p->o_flags; | |
1061 | ip6_tnl_dst_reset(t); | |
1062 | ip6gre_tnl_link_config(t, set_mtu); | |
1063 | return 0; | |
1064 | } | |
1065 | ||
1066 | static void ip6gre_tnl_parm_from_user(struct __ip6_tnl_parm *p, | |
1067 | const struct ip6_tnl_parm2 *u) | |
1068 | { | |
1069 | p->laddr = u->laddr; | |
1070 | p->raddr = u->raddr; | |
1071 | p->flags = u->flags; | |
1072 | p->hop_limit = u->hop_limit; | |
1073 | p->encap_limit = u->encap_limit; | |
1074 | p->flowinfo = u->flowinfo; | |
1075 | p->link = u->link; | |
1076 | p->i_key = u->i_key; | |
1077 | p->o_key = u->o_key; | |
1078 | p->i_flags = u->i_flags; | |
1079 | p->o_flags = u->o_flags; | |
1080 | memcpy(p->name, u->name, sizeof(u->name)); | |
1081 | } | |
1082 | ||
1083 | static void ip6gre_tnl_parm_to_user(struct ip6_tnl_parm2 *u, | |
1084 | const struct __ip6_tnl_parm *p) | |
1085 | { | |
1086 | u->proto = IPPROTO_GRE; | |
1087 | u->laddr = p->laddr; | |
1088 | u->raddr = p->raddr; | |
1089 | u->flags = p->flags; | |
1090 | u->hop_limit = p->hop_limit; | |
1091 | u->encap_limit = p->encap_limit; | |
1092 | u->flowinfo = p->flowinfo; | |
1093 | u->link = p->link; | |
1094 | u->i_key = p->i_key; | |
1095 | u->o_key = p->o_key; | |
1096 | u->i_flags = p->i_flags; | |
1097 | u->o_flags = p->o_flags; | |
1098 | memcpy(u->name, p->name, sizeof(u->name)); | |
1099 | } | |
1100 | ||
1101 | static int ip6gre_tunnel_ioctl(struct net_device *dev, | |
1102 | struct ifreq *ifr, int cmd) | |
1103 | { | |
1104 | int err = 0; | |
1105 | struct ip6_tnl_parm2 p; | |
1106 | struct __ip6_tnl_parm p1; | |
1107 | struct ip6_tnl *t; | |
1108 | struct net *net = dev_net(dev); | |
1109 | struct ip6gre_net *ign = net_generic(net, ip6gre_net_id); | |
1110 | ||
1111 | switch (cmd) { | |
1112 | case SIOCGETTUNNEL: | |
1113 | t = NULL; | |
1114 | if (dev == ign->fb_tunnel_dev) { | |
1115 | if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) { | |
1116 | err = -EFAULT; | |
1117 | break; | |
1118 | } | |
1119 | ip6gre_tnl_parm_from_user(&p1, &p); | |
1120 | t = ip6gre_tunnel_locate(net, &p1, 0); | |
1121 | } | |
1122 | if (t == NULL) | |
1123 | t = netdev_priv(dev); | |
1124 | ip6gre_tnl_parm_to_user(&p, &t->parms); | |
1125 | if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p))) | |
1126 | err = -EFAULT; | |
1127 | break; | |
1128 | ||
1129 | case SIOCADDTUNNEL: | |
1130 | case SIOCCHGTUNNEL: | |
1131 | err = -EPERM; | |
af31f412 | 1132 | if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) |
c12b395a | 1133 | goto done; |
1134 | ||
1135 | err = -EFAULT; | |
1136 | if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) | |
1137 | goto done; | |
1138 | ||
1139 | err = -EINVAL; | |
1140 | if ((p.i_flags|p.o_flags)&(GRE_VERSION|GRE_ROUTING)) | |
1141 | goto done; | |
1142 | ||
1143 | if (!(p.i_flags&GRE_KEY)) | |
1144 | p.i_key = 0; | |
1145 | if (!(p.o_flags&GRE_KEY)) | |
1146 | p.o_key = 0; | |
1147 | ||
1148 | ip6gre_tnl_parm_from_user(&p1, &p); | |
1149 | t = ip6gre_tunnel_locate(net, &p1, cmd == SIOCADDTUNNEL); | |
1150 | ||
1151 | if (dev != ign->fb_tunnel_dev && cmd == SIOCCHGTUNNEL) { | |
1152 | if (t != NULL) { | |
1153 | if (t->dev != dev) { | |
1154 | err = -EEXIST; | |
1155 | break; | |
1156 | } | |
1157 | } else { | |
1158 | t = netdev_priv(dev); | |
1159 | ||
1160 | ip6gre_tunnel_unlink(ign, t); | |
1161 | synchronize_net(); | |
1162 | ip6gre_tnl_change(t, &p1, 1); | |
1163 | ip6gre_tunnel_link(ign, t); | |
1164 | netdev_state_change(dev); | |
1165 | } | |
1166 | } | |
1167 | ||
1168 | if (t) { | |
1169 | err = 0; | |
1170 | ||
1171 | ip6gre_tnl_parm_to_user(&p, &t->parms); | |
1172 | if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p))) | |
1173 | err = -EFAULT; | |
1174 | } else | |
1175 | err = (cmd == SIOCADDTUNNEL ? -ENOBUFS : -ENOENT); | |
1176 | break; | |
1177 | ||
1178 | case SIOCDELTUNNEL: | |
1179 | err = -EPERM; | |
af31f412 | 1180 | if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) |
c12b395a | 1181 | goto done; |
1182 | ||
1183 | if (dev == ign->fb_tunnel_dev) { | |
1184 | err = -EFAULT; | |
1185 | if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) | |
1186 | goto done; | |
1187 | err = -ENOENT; | |
1188 | ip6gre_tnl_parm_from_user(&p1, &p); | |
1189 | t = ip6gre_tunnel_locate(net, &p1, 0); | |
1190 | if (t == NULL) | |
1191 | goto done; | |
1192 | err = -EPERM; | |
1193 | if (t == netdev_priv(ign->fb_tunnel_dev)) | |
1194 | goto done; | |
1195 | dev = t->dev; | |
1196 | } | |
1197 | unregister_netdevice(dev); | |
1198 | err = 0; | |
1199 | break; | |
1200 | ||
1201 | default: | |
1202 | err = -EINVAL; | |
1203 | } | |
1204 | ||
1205 | done: | |
1206 | return err; | |
1207 | } | |
1208 | ||
1209 | static int ip6gre_tunnel_change_mtu(struct net_device *dev, int new_mtu) | |
1210 | { | |
1211 | struct ip6_tnl *tunnel = netdev_priv(dev); | |
1212 | if (new_mtu < 68 || | |
1213 | new_mtu > 0xFFF8 - dev->hard_header_len - tunnel->hlen) | |
1214 | return -EINVAL; | |
1215 | dev->mtu = new_mtu; | |
1216 | return 0; | |
1217 | } | |
1218 | ||
1219 | static int ip6gre_header(struct sk_buff *skb, struct net_device *dev, | |
1220 | unsigned short type, | |
1221 | const void *daddr, const void *saddr, unsigned int len) | |
1222 | { | |
1223 | struct ip6_tnl *t = netdev_priv(dev); | |
1224 | struct ipv6hdr *ipv6h = (struct ipv6hdr *)skb_push(skb, t->hlen); | |
1225 | __be16 *p = (__be16 *)(ipv6h+1); | |
1226 | ||
3e4e4c1f | 1227 | ip6_flow_hdr(ipv6h, 0, t->fl.u.ip6.flowlabel); |
c12b395a | 1228 | ipv6h->hop_limit = t->parms.hop_limit; |
1229 | ipv6h->nexthdr = NEXTHDR_GRE; | |
1230 | ipv6h->saddr = t->parms.laddr; | |
1231 | ipv6h->daddr = t->parms.raddr; | |
1232 | ||
1233 | p[0] = t->parms.o_flags; | |
1234 | p[1] = htons(type); | |
1235 | ||
1236 | /* | |
1237 | * Set the source hardware address. | |
1238 | */ | |
1239 | ||
1240 | if (saddr) | |
1241 | memcpy(&ipv6h->saddr, saddr, sizeof(struct in6_addr)); | |
1242 | if (daddr) | |
1243 | memcpy(&ipv6h->daddr, daddr, sizeof(struct in6_addr)); | |
1244 | if (!ipv6_addr_any(&ipv6h->daddr)) | |
1245 | return t->hlen; | |
1246 | ||
1247 | return -t->hlen; | |
1248 | } | |
1249 | ||
c12b395a | 1250 | static const struct header_ops ip6gre_header_ops = { |
1251 | .create = ip6gre_header, | |
c12b395a | 1252 | }; |
1253 | ||
1254 | static const struct net_device_ops ip6gre_netdev_ops = { | |
1255 | .ndo_init = ip6gre_tunnel_init, | |
1256 | .ndo_uninit = ip6gre_tunnel_uninit, | |
1257 | .ndo_start_xmit = ip6gre_tunnel_xmit, | |
1258 | .ndo_do_ioctl = ip6gre_tunnel_ioctl, | |
1259 | .ndo_change_mtu = ip6gre_tunnel_change_mtu, | |
1260 | .ndo_get_stats64 = ip6gre_get_stats64, | |
1261 | }; | |
1262 | ||
1263 | static void ip6gre_dev_free(struct net_device *dev) | |
1264 | { | |
1265 | free_percpu(dev->tstats); | |
1266 | free_netdev(dev); | |
1267 | } | |
1268 | ||
1269 | static void ip6gre_tunnel_setup(struct net_device *dev) | |
1270 | { | |
1271 | struct ip6_tnl *t; | |
1272 | ||
1273 | dev->netdev_ops = &ip6gre_netdev_ops; | |
1274 | dev->destructor = ip6gre_dev_free; | |
1275 | ||
1276 | dev->type = ARPHRD_IP6GRE; | |
1277 | dev->hard_header_len = LL_MAX_HEADER + sizeof(struct ipv6hdr) + 4; | |
1278 | dev->mtu = ETH_DATA_LEN - sizeof(struct ipv6hdr) - 4; | |
1279 | t = netdev_priv(dev); | |
1280 | if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT)) | |
1281 | dev->mtu -= 8; | |
1282 | dev->flags |= IFF_NOARP; | |
1283 | dev->iflink = 0; | |
1284 | dev->addr_len = sizeof(struct in6_addr); | |
1285 | dev->features |= NETIF_F_NETNS_LOCAL; | |
1286 | dev->priv_flags &= ~IFF_XMIT_DST_RELEASE; | |
1287 | } | |
1288 | ||
1289 | static int ip6gre_tunnel_init(struct net_device *dev) | |
1290 | { | |
1291 | struct ip6_tnl *tunnel; | |
1292 | ||
1293 | tunnel = netdev_priv(dev); | |
1294 | ||
1295 | tunnel->dev = dev; | |
1296 | strcpy(tunnel->parms.name, dev->name); | |
1297 | ||
1298 | memcpy(dev->dev_addr, &tunnel->parms.laddr, sizeof(struct in6_addr)); | |
1299 | memcpy(dev->broadcast, &tunnel->parms.raddr, sizeof(struct in6_addr)); | |
1300 | ||
1301 | if (ipv6_addr_any(&tunnel->parms.raddr)) | |
1302 | dev->header_ops = &ip6gre_header_ops; | |
1303 | ||
1304 | dev->tstats = alloc_percpu(struct pcpu_tstats); | |
1305 | if (!dev->tstats) | |
1306 | return -ENOMEM; | |
1307 | ||
1308 | return 0; | |
1309 | } | |
1310 | ||
1311 | static void ip6gre_fb_tunnel_init(struct net_device *dev) | |
1312 | { | |
1313 | struct ip6_tnl *tunnel = netdev_priv(dev); | |
1314 | ||
1315 | tunnel->dev = dev; | |
1316 | strcpy(tunnel->parms.name, dev->name); | |
1317 | ||
1318 | tunnel->hlen = sizeof(struct ipv6hdr) + 4; | |
1319 | ||
1320 | dev_hold(dev); | |
1321 | } | |
1322 | ||
1323 | ||
1324 | static struct inet6_protocol ip6gre_protocol __read_mostly = { | |
1325 | .handler = ip6gre_rcv, | |
1326 | .err_handler = ip6gre_err, | |
1327 | .flags = INET6_PROTO_NOPOLICY|INET6_PROTO_FINAL, | |
1328 | }; | |
1329 | ||
1330 | static void ip6gre_destroy_tunnels(struct ip6gre_net *ign, | |
1331 | struct list_head *head) | |
1332 | { | |
1333 | int prio; | |
1334 | ||
1335 | for (prio = 0; prio < 4; prio++) { | |
1336 | int h; | |
1337 | for (h = 0; h < HASH_SIZE; h++) { | |
1338 | struct ip6_tnl *t; | |
1339 | ||
1340 | t = rtnl_dereference(ign->tunnels[prio][h]); | |
1341 | ||
1342 | while (t != NULL) { | |
1343 | unregister_netdevice_queue(t->dev, head); | |
1344 | t = rtnl_dereference(t->next); | |
1345 | } | |
1346 | } | |
1347 | } | |
1348 | } | |
1349 | ||
1350 | static int __net_init ip6gre_init_net(struct net *net) | |
1351 | { | |
1352 | struct ip6gre_net *ign = net_generic(net, ip6gre_net_id); | |
1353 | int err; | |
1354 | ||
1355 | ign->fb_tunnel_dev = alloc_netdev(sizeof(struct ip6_tnl), "ip6gre0", | |
1356 | ip6gre_tunnel_setup); | |
1357 | if (!ign->fb_tunnel_dev) { | |
1358 | err = -ENOMEM; | |
1359 | goto err_alloc_dev; | |
1360 | } | |
1361 | dev_net_set(ign->fb_tunnel_dev, net); | |
1362 | ||
1363 | ip6gre_fb_tunnel_init(ign->fb_tunnel_dev); | |
1364 | ign->fb_tunnel_dev->rtnl_link_ops = &ip6gre_link_ops; | |
1365 | ||
1366 | err = register_netdev(ign->fb_tunnel_dev); | |
1367 | if (err) | |
1368 | goto err_reg_dev; | |
1369 | ||
1370 | rcu_assign_pointer(ign->tunnels_wc[0], | |
1371 | netdev_priv(ign->fb_tunnel_dev)); | |
1372 | return 0; | |
1373 | ||
1374 | err_reg_dev: | |
1375 | ip6gre_dev_free(ign->fb_tunnel_dev); | |
1376 | err_alloc_dev: | |
1377 | return err; | |
1378 | } | |
1379 | ||
1380 | static void __net_exit ip6gre_exit_net(struct net *net) | |
1381 | { | |
1382 | struct ip6gre_net *ign; | |
1383 | LIST_HEAD(list); | |
1384 | ||
1385 | ign = net_generic(net, ip6gre_net_id); | |
1386 | rtnl_lock(); | |
1387 | ip6gre_destroy_tunnels(ign, &list); | |
1388 | unregister_netdevice_many(&list); | |
1389 | rtnl_unlock(); | |
1390 | } | |
1391 | ||
1392 | static struct pernet_operations ip6gre_net_ops = { | |
1393 | .init = ip6gre_init_net, | |
1394 | .exit = ip6gre_exit_net, | |
1395 | .id = &ip6gre_net_id, | |
1396 | .size = sizeof(struct ip6gre_net), | |
1397 | }; | |
1398 | ||
1399 | static int ip6gre_tunnel_validate(struct nlattr *tb[], struct nlattr *data[]) | |
1400 | { | |
1401 | __be16 flags; | |
1402 | ||
1403 | if (!data) | |
1404 | return 0; | |
1405 | ||
1406 | flags = 0; | |
1407 | if (data[IFLA_GRE_IFLAGS]) | |
1408 | flags |= nla_get_be16(data[IFLA_GRE_IFLAGS]); | |
1409 | if (data[IFLA_GRE_OFLAGS]) | |
1410 | flags |= nla_get_be16(data[IFLA_GRE_OFLAGS]); | |
1411 | if (flags & (GRE_VERSION|GRE_ROUTING)) | |
1412 | return -EINVAL; | |
1413 | ||
1414 | return 0; | |
1415 | } | |
1416 | ||
1417 | static int ip6gre_tap_validate(struct nlattr *tb[], struct nlattr *data[]) | |
1418 | { | |
1419 | struct in6_addr daddr; | |
1420 | ||
1421 | if (tb[IFLA_ADDRESS]) { | |
1422 | if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) | |
1423 | return -EINVAL; | |
1424 | if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) | |
1425 | return -EADDRNOTAVAIL; | |
1426 | } | |
1427 | ||
1428 | if (!data) | |
1429 | goto out; | |
1430 | ||
1431 | if (data[IFLA_GRE_REMOTE]) { | |
1432 | nla_memcpy(&daddr, data[IFLA_GRE_REMOTE], sizeof(struct in6_addr)); | |
1433 | if (ipv6_addr_any(&daddr)) | |
1434 | return -EINVAL; | |
1435 | } | |
1436 | ||
1437 | out: | |
1438 | return ip6gre_tunnel_validate(tb, data); | |
1439 | } | |
1440 | ||
1441 | ||
1442 | static void ip6gre_netlink_parms(struct nlattr *data[], | |
1443 | struct __ip6_tnl_parm *parms) | |
1444 | { | |
1445 | memset(parms, 0, sizeof(*parms)); | |
1446 | ||
1447 | if (!data) | |
1448 | return; | |
1449 | ||
1450 | if (data[IFLA_GRE_LINK]) | |
1451 | parms->link = nla_get_u32(data[IFLA_GRE_LINK]); | |
1452 | ||
1453 | if (data[IFLA_GRE_IFLAGS]) | |
1454 | parms->i_flags = nla_get_be16(data[IFLA_GRE_IFLAGS]); | |
1455 | ||
1456 | if (data[IFLA_GRE_OFLAGS]) | |
1457 | parms->o_flags = nla_get_be16(data[IFLA_GRE_OFLAGS]); | |
1458 | ||
1459 | if (data[IFLA_GRE_IKEY]) | |
1460 | parms->i_key = nla_get_be32(data[IFLA_GRE_IKEY]); | |
1461 | ||
1462 | if (data[IFLA_GRE_OKEY]) | |
1463 | parms->o_key = nla_get_be32(data[IFLA_GRE_OKEY]); | |
1464 | ||
1465 | if (data[IFLA_GRE_LOCAL]) | |
1466 | nla_memcpy(&parms->laddr, data[IFLA_GRE_LOCAL], sizeof(struct in6_addr)); | |
1467 | ||
1468 | if (data[IFLA_GRE_REMOTE]) | |
1469 | nla_memcpy(&parms->raddr, data[IFLA_GRE_REMOTE], sizeof(struct in6_addr)); | |
1470 | ||
1471 | if (data[IFLA_GRE_TTL]) | |
1472 | parms->hop_limit = nla_get_u8(data[IFLA_GRE_TTL]); | |
1473 | ||
1474 | if (data[IFLA_GRE_ENCAP_LIMIT]) | |
1475 | parms->encap_limit = nla_get_u8(data[IFLA_GRE_ENCAP_LIMIT]); | |
1476 | ||
1477 | if (data[IFLA_GRE_FLOWINFO]) | |
1478 | parms->flowinfo = nla_get_u32(data[IFLA_GRE_FLOWINFO]); | |
1479 | ||
1480 | if (data[IFLA_GRE_FLAGS]) | |
1481 | parms->flags = nla_get_u32(data[IFLA_GRE_FLAGS]); | |
1482 | } | |
1483 | ||
1484 | static int ip6gre_tap_init(struct net_device *dev) | |
1485 | { | |
1486 | struct ip6_tnl *tunnel; | |
1487 | ||
1488 | tunnel = netdev_priv(dev); | |
1489 | ||
1490 | tunnel->dev = dev; | |
1491 | strcpy(tunnel->parms.name, dev->name); | |
1492 | ||
1493 | ip6gre_tnl_link_config(tunnel, 1); | |
1494 | ||
1495 | dev->tstats = alloc_percpu(struct pcpu_tstats); | |
1496 | if (!dev->tstats) | |
1497 | return -ENOMEM; | |
1498 | ||
1499 | return 0; | |
1500 | } | |
1501 | ||
1502 | static const struct net_device_ops ip6gre_tap_netdev_ops = { | |
1503 | .ndo_init = ip6gre_tap_init, | |
1504 | .ndo_uninit = ip6gre_tunnel_uninit, | |
1505 | .ndo_start_xmit = ip6gre_tunnel_xmit, | |
1506 | .ndo_set_mac_address = eth_mac_addr, | |
1507 | .ndo_validate_addr = eth_validate_addr, | |
1508 | .ndo_change_mtu = ip6gre_tunnel_change_mtu, | |
1509 | .ndo_get_stats64 = ip6gre_get_stats64, | |
1510 | }; | |
1511 | ||
1512 | static void ip6gre_tap_setup(struct net_device *dev) | |
1513 | { | |
1514 | ||
1515 | ether_setup(dev); | |
1516 | ||
1517 | dev->netdev_ops = &ip6gre_tap_netdev_ops; | |
1518 | dev->destructor = ip6gre_dev_free; | |
1519 | ||
1520 | dev->iflink = 0; | |
1521 | dev->features |= NETIF_F_NETNS_LOCAL; | |
1522 | } | |
1523 | ||
1524 | static int ip6gre_newlink(struct net *src_net, struct net_device *dev, | |
1525 | struct nlattr *tb[], struct nlattr *data[]) | |
1526 | { | |
1527 | struct ip6_tnl *nt; | |
1528 | struct net *net = dev_net(dev); | |
1529 | struct ip6gre_net *ign = net_generic(net, ip6gre_net_id); | |
1530 | int err; | |
1531 | ||
1532 | nt = netdev_priv(dev); | |
1533 | ip6gre_netlink_parms(data, &nt->parms); | |
1534 | ||
1535 | if (ip6gre_tunnel_find(net, &nt->parms, dev->type)) | |
1536 | return -EEXIST; | |
1537 | ||
1538 | if (dev->type == ARPHRD_ETHER && !tb[IFLA_ADDRESS]) | |
1539 | eth_hw_addr_random(dev); | |
1540 | ||
1541 | nt->dev = dev; | |
1542 | ip6gre_tnl_link_config(nt, !tb[IFLA_MTU]); | |
1543 | ||
1544 | /* Can use a lockless transmit, unless we generate output sequences */ | |
1545 | if (!(nt->parms.o_flags & GRE_SEQ)) | |
1546 | dev->features |= NETIF_F_LLTX; | |
1547 | ||
1548 | err = register_netdevice(dev); | |
1549 | if (err) | |
1550 | goto out; | |
1551 | ||
1552 | dev_hold(dev); | |
1553 | ip6gre_tunnel_link(ign, nt); | |
1554 | ||
1555 | out: | |
1556 | return err; | |
1557 | } | |
1558 | ||
1559 | static int ip6gre_changelink(struct net_device *dev, struct nlattr *tb[], | |
1560 | struct nlattr *data[]) | |
1561 | { | |
1562 | struct ip6_tnl *t, *nt; | |
1563 | struct net *net = dev_net(dev); | |
1564 | struct ip6gre_net *ign = net_generic(net, ip6gre_net_id); | |
1565 | struct __ip6_tnl_parm p; | |
1566 | ||
1567 | if (dev == ign->fb_tunnel_dev) | |
1568 | return -EINVAL; | |
1569 | ||
1570 | nt = netdev_priv(dev); | |
1571 | ip6gre_netlink_parms(data, &p); | |
1572 | ||
1573 | t = ip6gre_tunnel_locate(net, &p, 0); | |
1574 | ||
1575 | if (t) { | |
1576 | if (t->dev != dev) | |
1577 | return -EEXIST; | |
1578 | } else { | |
1579 | t = nt; | |
1580 | ||
1581 | ip6gre_tunnel_unlink(ign, t); | |
1582 | ip6gre_tnl_change(t, &p, !tb[IFLA_MTU]); | |
1583 | ip6gre_tunnel_link(ign, t); | |
1584 | netdev_state_change(dev); | |
1585 | } | |
1586 | ||
1587 | return 0; | |
1588 | } | |
1589 | ||
1590 | static size_t ip6gre_get_size(const struct net_device *dev) | |
1591 | { | |
1592 | return | |
1593 | /* IFLA_GRE_LINK */ | |
1594 | nla_total_size(4) + | |
1595 | /* IFLA_GRE_IFLAGS */ | |
1596 | nla_total_size(2) + | |
1597 | /* IFLA_GRE_OFLAGS */ | |
1598 | nla_total_size(2) + | |
1599 | /* IFLA_GRE_IKEY */ | |
1600 | nla_total_size(4) + | |
1601 | /* IFLA_GRE_OKEY */ | |
1602 | nla_total_size(4) + | |
1603 | /* IFLA_GRE_LOCAL */ | |
a3754133 | 1604 | nla_total_size(sizeof(struct in6_addr)) + |
c12b395a | 1605 | /* IFLA_GRE_REMOTE */ |
a3754133 | 1606 | nla_total_size(sizeof(struct in6_addr)) + |
c12b395a | 1607 | /* IFLA_GRE_TTL */ |
1608 | nla_total_size(1) + | |
1609 | /* IFLA_GRE_TOS */ | |
1610 | nla_total_size(1) + | |
1611 | /* IFLA_GRE_ENCAP_LIMIT */ | |
1612 | nla_total_size(1) + | |
1613 | /* IFLA_GRE_FLOWINFO */ | |
1614 | nla_total_size(4) + | |
1615 | /* IFLA_GRE_FLAGS */ | |
1616 | nla_total_size(4) + | |
1617 | 0; | |
1618 | } | |
1619 | ||
1620 | static int ip6gre_fill_info(struct sk_buff *skb, const struct net_device *dev) | |
1621 | { | |
1622 | struct ip6_tnl *t = netdev_priv(dev); | |
1623 | struct __ip6_tnl_parm *p = &t->parms; | |
1624 | ||
1625 | if (nla_put_u32(skb, IFLA_GRE_LINK, p->link) || | |
1626 | nla_put_be16(skb, IFLA_GRE_IFLAGS, p->i_flags) || | |
1627 | nla_put_be16(skb, IFLA_GRE_OFLAGS, p->o_flags) || | |
1628 | nla_put_be32(skb, IFLA_GRE_IKEY, p->i_key) || | |
1629 | nla_put_be32(skb, IFLA_GRE_OKEY, p->o_key) || | |
a3754133 ND |
1630 | nla_put(skb, IFLA_GRE_LOCAL, sizeof(struct in6_addr), &p->laddr) || |
1631 | nla_put(skb, IFLA_GRE_REMOTE, sizeof(struct in6_addr), &p->raddr) || | |
c12b395a | 1632 | nla_put_u8(skb, IFLA_GRE_TTL, p->hop_limit) || |
1633 | /*nla_put_u8(skb, IFLA_GRE_TOS, t->priority) ||*/ | |
1634 | nla_put_u8(skb, IFLA_GRE_ENCAP_LIMIT, p->encap_limit) || | |
1635 | nla_put_be32(skb, IFLA_GRE_FLOWINFO, p->flowinfo) || | |
1636 | nla_put_u32(skb, IFLA_GRE_FLAGS, p->flags)) | |
1637 | goto nla_put_failure; | |
1638 | return 0; | |
1639 | ||
1640 | nla_put_failure: | |
1641 | return -EMSGSIZE; | |
1642 | } | |
1643 | ||
1644 | static const struct nla_policy ip6gre_policy[IFLA_GRE_MAX + 1] = { | |
1645 | [IFLA_GRE_LINK] = { .type = NLA_U32 }, | |
1646 | [IFLA_GRE_IFLAGS] = { .type = NLA_U16 }, | |
1647 | [IFLA_GRE_OFLAGS] = { .type = NLA_U16 }, | |
1648 | [IFLA_GRE_IKEY] = { .type = NLA_U32 }, | |
1649 | [IFLA_GRE_OKEY] = { .type = NLA_U32 }, | |
1650 | [IFLA_GRE_LOCAL] = { .len = FIELD_SIZEOF(struct ipv6hdr, saddr) }, | |
1651 | [IFLA_GRE_REMOTE] = { .len = FIELD_SIZEOF(struct ipv6hdr, daddr) }, | |
1652 | [IFLA_GRE_TTL] = { .type = NLA_U8 }, | |
1653 | [IFLA_GRE_ENCAP_LIMIT] = { .type = NLA_U8 }, | |
1654 | [IFLA_GRE_FLOWINFO] = { .type = NLA_U32 }, | |
1655 | [IFLA_GRE_FLAGS] = { .type = NLA_U32 }, | |
1656 | }; | |
1657 | ||
1658 | static struct rtnl_link_ops ip6gre_link_ops __read_mostly = { | |
1659 | .kind = "ip6gre", | |
1660 | .maxtype = IFLA_GRE_MAX, | |
1661 | .policy = ip6gre_policy, | |
1662 | .priv_size = sizeof(struct ip6_tnl), | |
1663 | .setup = ip6gre_tunnel_setup, | |
1664 | .validate = ip6gre_tunnel_validate, | |
1665 | .newlink = ip6gre_newlink, | |
1666 | .changelink = ip6gre_changelink, | |
1667 | .get_size = ip6gre_get_size, | |
1668 | .fill_info = ip6gre_fill_info, | |
1669 | }; | |
1670 | ||
1671 | static struct rtnl_link_ops ip6gre_tap_ops __read_mostly = { | |
1672 | .kind = "ip6gretap", | |
1673 | .maxtype = IFLA_GRE_MAX, | |
1674 | .policy = ip6gre_policy, | |
1675 | .priv_size = sizeof(struct ip6_tnl), | |
1676 | .setup = ip6gre_tap_setup, | |
1677 | .validate = ip6gre_tap_validate, | |
1678 | .newlink = ip6gre_newlink, | |
1679 | .changelink = ip6gre_changelink, | |
1680 | .get_size = ip6gre_get_size, | |
1681 | .fill_info = ip6gre_fill_info, | |
1682 | }; | |
1683 | ||
1684 | /* | |
1685 | * And now the modules code and kernel interface. | |
1686 | */ | |
1687 | ||
1688 | static int __init ip6gre_init(void) | |
1689 | { | |
1690 | int err; | |
1691 | ||
1692 | pr_info("GRE over IPv6 tunneling driver\n"); | |
1693 | ||
1694 | err = register_pernet_device(&ip6gre_net_ops); | |
1695 | if (err < 0) | |
1696 | return err; | |
1697 | ||
1698 | err = inet6_add_protocol(&ip6gre_protocol, IPPROTO_GRE); | |
1699 | if (err < 0) { | |
1700 | pr_info("%s: can't add protocol\n", __func__); | |
1701 | goto add_proto_failed; | |
1702 | } | |
1703 | ||
1704 | err = rtnl_link_register(&ip6gre_link_ops); | |
1705 | if (err < 0) | |
1706 | goto rtnl_link_failed; | |
1707 | ||
1708 | err = rtnl_link_register(&ip6gre_tap_ops); | |
1709 | if (err < 0) | |
1710 | goto tap_ops_failed; | |
1711 | ||
1712 | out: | |
1713 | return err; | |
1714 | ||
1715 | tap_ops_failed: | |
1716 | rtnl_link_unregister(&ip6gre_link_ops); | |
1717 | rtnl_link_failed: | |
1718 | inet6_del_protocol(&ip6gre_protocol, IPPROTO_GRE); | |
1719 | add_proto_failed: | |
1720 | unregister_pernet_device(&ip6gre_net_ops); | |
1721 | goto out; | |
1722 | } | |
1723 | ||
1724 | static void __exit ip6gre_fini(void) | |
1725 | { | |
1726 | rtnl_link_unregister(&ip6gre_tap_ops); | |
1727 | rtnl_link_unregister(&ip6gre_link_ops); | |
1728 | inet6_del_protocol(&ip6gre_protocol, IPPROTO_GRE); | |
1729 | unregister_pernet_device(&ip6gre_net_ops); | |
1730 | } | |
1731 | ||
1732 | module_init(ip6gre_init); | |
1733 | module_exit(ip6gre_fini); | |
1734 | MODULE_LICENSE("GPL"); | |
1735 | MODULE_AUTHOR("D. Kozlov (xeb@mail.ru)"); | |
1736 | MODULE_DESCRIPTION("GRE over IPv6 tunneling device"); | |
1737 | MODULE_ALIAS_RTNL_LINK("ip6gre"); | |
1738 | MODULE_ALIAS_NETDEV("ip6gre0"); |