]>
Commit | Line | Data |
---|---|---|
1181412c S |
1 | /* |
2 | * Linux NET3: IP/IP protocol decoder modified to support | |
3 | * virtual tunnel interface | |
4 | * | |
5 | * Authors: | |
6 | * Saurabh Mohan (saurabh.mohan@vyatta.com) 05/07/2012 | |
7 | * | |
8 | * This program is free software; you can redistribute it and/or | |
9 | * modify it under the terms of the GNU General Public License | |
10 | * as published by the Free Software Foundation; either version | |
11 | * 2 of the License, or (at your option) any later version. | |
12 | * | |
13 | */ | |
14 | ||
15 | /* | |
16 | This version of net/ipv4/ip_vti.c is cloned of net/ipv4/ipip.c | |
17 | ||
18 | For comments look at net/ipv4/ip_gre.c --ANK | |
19 | */ | |
20 | ||
21 | ||
22 | #include <linux/capability.h> | |
23 | #include <linux/module.h> | |
24 | #include <linux/types.h> | |
25 | #include <linux/kernel.h> | |
26 | #include <linux/uaccess.h> | |
27 | #include <linux/skbuff.h> | |
28 | #include <linux/netdevice.h> | |
29 | #include <linux/in.h> | |
30 | #include <linux/tcp.h> | |
31 | #include <linux/udp.h> | |
32 | #include <linux/if_arp.h> | |
33 | #include <linux/mroute.h> | |
34 | #include <linux/init.h> | |
35 | #include <linux/netfilter_ipv4.h> | |
36 | #include <linux/if_ether.h> | |
37 | ||
38 | #include <net/sock.h> | |
39 | #include <net/ip.h> | |
40 | #include <net/icmp.h> | |
41 | #include <net/ipip.h> | |
42 | #include <net/inet_ecn.h> | |
43 | #include <net/xfrm.h> | |
44 | #include <net/net_namespace.h> | |
45 | #include <net/netns/generic.h> | |
46 | ||
47 | #define HASH_SIZE 16 | |
48 | #define HASH(addr) (((__force u32)addr^((__force u32)addr>>4))&(HASH_SIZE-1)) | |
49 | ||
50 | static struct rtnl_link_ops vti_link_ops __read_mostly; | |
51 | ||
52 | static int vti_net_id __read_mostly; | |
53 | struct vti_net { | |
54 | struct ip_tunnel __rcu *tunnels_r_l[HASH_SIZE]; | |
55 | struct ip_tunnel __rcu *tunnels_r[HASH_SIZE]; | |
56 | struct ip_tunnel __rcu *tunnels_l[HASH_SIZE]; | |
57 | struct ip_tunnel __rcu *tunnels_wc[1]; | |
e7d4b18c | 58 | struct ip_tunnel __rcu **tunnels[4]; |
1181412c S |
59 | |
60 | struct net_device *fb_tunnel_dev; | |
61 | }; | |
62 | ||
63 | static int vti_fb_tunnel_init(struct net_device *dev); | |
64 | static int vti_tunnel_init(struct net_device *dev); | |
65 | static void vti_tunnel_setup(struct net_device *dev); | |
66 | static void vti_dev_free(struct net_device *dev); | |
67 | static int vti_tunnel_bind_dev(struct net_device *dev); | |
68 | ||
69 | /* Locking : hash tables are protected by RCU and RTNL */ | |
70 | ||
71 | #define for_each_ip_tunnel_rcu(start) \ | |
72 | for (t = rcu_dereference(start); t; t = rcu_dereference(t->next)) | |
73 | ||
74 | /* often modified stats are per cpu, other are shared (netdev->stats) */ | |
75 | struct pcpu_tstats { | |
76 | u64 rx_packets; | |
77 | u64 rx_bytes; | |
78 | u64 tx_packets; | |
79 | u64 tx_bytes; | |
80 | struct u64_stats_sync syncp; | |
81 | }; | |
82 | ||
83 | #define VTI_XMIT(stats1, stats2) do { \ | |
84 | int err; \ | |
85 | int pkt_len = skb->len; \ | |
86 | err = dst_output(skb); \ | |
87 | if (net_xmit_eval(err) == 0) { \ | |
88 | u64_stats_update_begin(&(stats1)->syncp); \ | |
89 | (stats1)->tx_bytes += pkt_len; \ | |
90 | (stats1)->tx_packets++; \ | |
91 | u64_stats_update_end(&(stats1)->syncp); \ | |
92 | } else { \ | |
93 | (stats2)->tx_errors++; \ | |
94 | (stats2)->tx_aborted_errors++; \ | |
95 | } \ | |
96 | } while (0) | |
97 | ||
98 | ||
99 | static struct rtnl_link_stats64 *vti_get_stats64(struct net_device *dev, | |
100 | struct rtnl_link_stats64 *tot) | |
101 | { | |
102 | int i; | |
103 | ||
104 | for_each_possible_cpu(i) { | |
105 | const struct pcpu_tstats *tstats = per_cpu_ptr(dev->tstats, i); | |
106 | u64 rx_packets, rx_bytes, tx_packets, tx_bytes; | |
107 | unsigned int start; | |
108 | ||
109 | do { | |
110 | start = u64_stats_fetch_begin_bh(&tstats->syncp); | |
111 | rx_packets = tstats->rx_packets; | |
112 | tx_packets = tstats->tx_packets; | |
113 | rx_bytes = tstats->rx_bytes; | |
114 | tx_bytes = tstats->tx_bytes; | |
115 | } while (u64_stats_fetch_retry_bh(&tstats->syncp, start)); | |
116 | ||
117 | tot->rx_packets += rx_packets; | |
118 | tot->tx_packets += tx_packets; | |
119 | tot->rx_bytes += rx_bytes; | |
120 | tot->tx_bytes += tx_bytes; | |
121 | } | |
122 | ||
123 | tot->multicast = dev->stats.multicast; | |
124 | tot->rx_crc_errors = dev->stats.rx_crc_errors; | |
125 | tot->rx_fifo_errors = dev->stats.rx_fifo_errors; | |
126 | tot->rx_length_errors = dev->stats.rx_length_errors; | |
127 | tot->rx_errors = dev->stats.rx_errors; | |
128 | tot->tx_fifo_errors = dev->stats.tx_fifo_errors; | |
129 | tot->tx_carrier_errors = dev->stats.tx_carrier_errors; | |
130 | tot->tx_dropped = dev->stats.tx_dropped; | |
131 | tot->tx_aborted_errors = dev->stats.tx_aborted_errors; | |
132 | tot->tx_errors = dev->stats.tx_errors; | |
133 | ||
134 | return tot; | |
135 | } | |
136 | ||
137 | static struct ip_tunnel *vti_tunnel_lookup(struct net *net, | |
138 | __be32 remote, __be32 local) | |
139 | { | |
140 | unsigned h0 = HASH(remote); | |
141 | unsigned h1 = HASH(local); | |
142 | struct ip_tunnel *t; | |
143 | struct vti_net *ipn = net_generic(net, vti_net_id); | |
144 | ||
145 | for_each_ip_tunnel_rcu(ipn->tunnels_r_l[h0 ^ h1]) | |
146 | if (local == t->parms.iph.saddr && | |
147 | remote == t->parms.iph.daddr && (t->dev->flags&IFF_UP)) | |
148 | return t; | |
149 | for_each_ip_tunnel_rcu(ipn->tunnels_r[h0]) | |
150 | if (remote == t->parms.iph.daddr && (t->dev->flags&IFF_UP)) | |
151 | return t; | |
152 | ||
153 | for_each_ip_tunnel_rcu(ipn->tunnels_l[h1]) | |
154 | if (local == t->parms.iph.saddr && (t->dev->flags&IFF_UP)) | |
155 | return t; | |
156 | ||
157 | for_each_ip_tunnel_rcu(ipn->tunnels_wc[0]) | |
158 | if (t && (t->dev->flags&IFF_UP)) | |
159 | return t; | |
160 | return NULL; | |
161 | } | |
162 | ||
e7d4b18c S |
163 | static struct ip_tunnel __rcu **__vti_bucket(struct vti_net *ipn, |
164 | struct ip_tunnel_parm *parms) | |
1181412c S |
165 | { |
166 | __be32 remote = parms->iph.daddr; | |
167 | __be32 local = parms->iph.saddr; | |
168 | unsigned h = 0; | |
169 | int prio = 0; | |
170 | ||
171 | if (remote) { | |
172 | prio |= 2; | |
173 | h ^= HASH(remote); | |
174 | } | |
175 | if (local) { | |
176 | prio |= 1; | |
177 | h ^= HASH(local); | |
178 | } | |
179 | return &ipn->tunnels[prio][h]; | |
180 | } | |
181 | ||
e7d4b18c S |
182 | static inline struct ip_tunnel __rcu **vti_bucket(struct vti_net *ipn, |
183 | struct ip_tunnel *t) | |
1181412c S |
184 | { |
185 | return __vti_bucket(ipn, &t->parms); | |
186 | } | |
187 | ||
188 | static void vti_tunnel_unlink(struct vti_net *ipn, struct ip_tunnel *t) | |
189 | { | |
190 | struct ip_tunnel __rcu **tp; | |
191 | struct ip_tunnel *iter; | |
192 | ||
193 | for (tp = vti_bucket(ipn, t); | |
194 | (iter = rtnl_dereference(*tp)) != NULL; | |
195 | tp = &iter->next) { | |
196 | if (t == iter) { | |
197 | rcu_assign_pointer(*tp, t->next); | |
198 | break; | |
199 | } | |
200 | } | |
201 | } | |
202 | ||
203 | static void vti_tunnel_link(struct vti_net *ipn, struct ip_tunnel *t) | |
204 | { | |
205 | struct ip_tunnel __rcu **tp = vti_bucket(ipn, t); | |
206 | ||
207 | rcu_assign_pointer(t->next, rtnl_dereference(*tp)); | |
208 | rcu_assign_pointer(*tp, t); | |
209 | } | |
210 | ||
211 | static struct ip_tunnel *vti_tunnel_locate(struct net *net, | |
212 | struct ip_tunnel_parm *parms, | |
213 | int create) | |
214 | { | |
215 | __be32 remote = parms->iph.daddr; | |
216 | __be32 local = parms->iph.saddr; | |
217 | struct ip_tunnel *t, *nt; | |
218 | struct ip_tunnel __rcu **tp; | |
219 | struct net_device *dev; | |
220 | char name[IFNAMSIZ]; | |
221 | struct vti_net *ipn = net_generic(net, vti_net_id); | |
222 | ||
223 | for (tp = __vti_bucket(ipn, parms); | |
224 | (t = rtnl_dereference(*tp)) != NULL; | |
225 | tp = &t->next) { | |
226 | if (local == t->parms.iph.saddr && remote == t->parms.iph.daddr) | |
227 | return t; | |
228 | } | |
229 | if (!create) | |
230 | return NULL; | |
231 | ||
232 | if (parms->name[0]) | |
233 | strlcpy(name, parms->name, IFNAMSIZ); | |
234 | else | |
235 | strcpy(name, "vti%d"); | |
236 | ||
237 | dev = alloc_netdev(sizeof(*t), name, vti_tunnel_setup); | |
238 | if (dev == NULL) | |
239 | return NULL; | |
240 | ||
241 | dev_net_set(dev, net); | |
242 | ||
243 | nt = netdev_priv(dev); | |
244 | nt->parms = *parms; | |
245 | dev->rtnl_link_ops = &vti_link_ops; | |
246 | ||
247 | vti_tunnel_bind_dev(dev); | |
248 | ||
249 | if (register_netdevice(dev) < 0) | |
250 | goto failed_free; | |
251 | ||
252 | dev_hold(dev); | |
253 | vti_tunnel_link(ipn, nt); | |
254 | return nt; | |
255 | ||
256 | failed_free: | |
257 | free_netdev(dev); | |
258 | return NULL; | |
259 | } | |
260 | ||
261 | static void vti_tunnel_uninit(struct net_device *dev) | |
262 | { | |
263 | struct net *net = dev_net(dev); | |
264 | struct vti_net *ipn = net_generic(net, vti_net_id); | |
265 | ||
266 | vti_tunnel_unlink(ipn, netdev_priv(dev)); | |
267 | dev_put(dev); | |
268 | } | |
269 | ||
270 | static int vti_err(struct sk_buff *skb, u32 info) | |
271 | { | |
272 | ||
273 | /* All the routers (except for Linux) return only | |
274 | * 8 bytes of packet payload. It means, that precise relaying of | |
275 | * ICMP in the real Internet is absolutely infeasible. | |
276 | */ | |
277 | struct iphdr *iph = (struct iphdr *)skb->data; | |
278 | const int type = icmp_hdr(skb)->type; | |
279 | const int code = icmp_hdr(skb)->code; | |
280 | struct ip_tunnel *t; | |
281 | int err; | |
282 | ||
283 | switch (type) { | |
284 | default: | |
285 | case ICMP_PARAMETERPROB: | |
286 | return 0; | |
287 | ||
288 | case ICMP_DEST_UNREACH: | |
289 | switch (code) { | |
290 | case ICMP_SR_FAILED: | |
291 | case ICMP_PORT_UNREACH: | |
292 | /* Impossible event. */ | |
293 | return 0; | |
294 | default: | |
295 | /* All others are translated to HOST_UNREACH. */ | |
296 | break; | |
297 | } | |
298 | break; | |
299 | case ICMP_TIME_EXCEEDED: | |
300 | if (code != ICMP_EXC_TTL) | |
301 | return 0; | |
302 | break; | |
303 | } | |
304 | ||
305 | err = -ENOENT; | |
306 | ||
1181412c S |
307 | t = vti_tunnel_lookup(dev_net(skb->dev), iph->daddr, iph->saddr); |
308 | if (t == NULL) | |
309 | goto out; | |
310 | ||
311 | if (type == ICMP_DEST_UNREACH && code == ICMP_FRAG_NEEDED) { | |
312 | ipv4_update_pmtu(skb, dev_net(skb->dev), info, | |
313 | t->parms.link, 0, IPPROTO_IPIP, 0); | |
314 | err = 0; | |
315 | goto out; | |
316 | } | |
317 | ||
318 | err = 0; | |
319 | if (t->parms.iph.ttl == 0 && type == ICMP_TIME_EXCEEDED) | |
320 | goto out; | |
321 | ||
322 | if (time_before(jiffies, t->err_time + IPTUNNEL_ERR_TIMEO)) | |
323 | t->err_count++; | |
324 | else | |
325 | t->err_count = 1; | |
326 | t->err_time = jiffies; | |
327 | out: | |
1181412c S |
328 | return err; |
329 | } | |
330 | ||
331 | /* We dont digest the packet therefore let the packet pass */ | |
332 | static int vti_rcv(struct sk_buff *skb) | |
333 | { | |
334 | struct ip_tunnel *tunnel; | |
335 | const struct iphdr *iph = ip_hdr(skb); | |
336 | ||
1181412c S |
337 | tunnel = vti_tunnel_lookup(dev_net(skb->dev), iph->saddr, iph->daddr); |
338 | if (tunnel != NULL) { | |
339 | struct pcpu_tstats *tstats; | |
340 | ||
341 | tstats = this_cpu_ptr(tunnel->dev->tstats); | |
342 | u64_stats_update_begin(&tstats->syncp); | |
343 | tstats->rx_packets++; | |
344 | tstats->rx_bytes += skb->len; | |
345 | u64_stats_update_end(&tstats->syncp); | |
346 | ||
347 | skb->dev = tunnel->dev; | |
1181412c S |
348 | return 1; |
349 | } | |
1181412c S |
350 | |
351 | return -1; | |
352 | } | |
353 | ||
354 | /* This function assumes it is being called from dev_queue_xmit() | |
355 | * and that skb is filled properly by that function. | |
356 | */ | |
357 | ||
358 | static netdev_tx_t vti_tunnel_xmit(struct sk_buff *skb, struct net_device *dev) | |
359 | { | |
360 | struct ip_tunnel *tunnel = netdev_priv(dev); | |
361 | struct pcpu_tstats *tstats; | |
362 | struct iphdr *tiph = &tunnel->parms.iph; | |
363 | u8 tos; | |
364 | struct rtable *rt; /* Route to the other host */ | |
365 | struct net_device *tdev; /* Device to other host */ | |
366 | struct iphdr *old_iph = ip_hdr(skb); | |
367 | __be32 dst = tiph->daddr; | |
368 | struct flowi4 fl4; | |
369 | ||
370 | if (skb->protocol != htons(ETH_P_IP)) | |
371 | goto tx_error; | |
372 | ||
373 | tos = old_iph->tos; | |
374 | ||
375 | memset(&fl4, 0, sizeof(fl4)); | |
376 | flowi4_init_output(&fl4, tunnel->parms.link, | |
377 | htonl(tunnel->parms.i_key), RT_TOS(tos), | |
378 | RT_SCOPE_UNIVERSE, | |
379 | IPPROTO_IPIP, 0, | |
380 | dst, tiph->saddr, 0, 0); | |
381 | rt = ip_route_output_key(dev_net(dev), &fl4); | |
382 | if (IS_ERR(rt)) { | |
383 | dev->stats.tx_carrier_errors++; | |
384 | goto tx_error_icmp; | |
385 | } | |
386 | /* if there is no transform then this tunnel is not functional. | |
387 | * Or if the xfrm is not mode tunnel. | |
388 | */ | |
389 | if (!rt->dst.xfrm || | |
390 | rt->dst.xfrm->props.mode != XFRM_MODE_TUNNEL) { | |
391 | dev->stats.tx_carrier_errors++; | |
392 | goto tx_error_icmp; | |
393 | } | |
394 | tdev = rt->dst.dev; | |
395 | ||
396 | if (tdev == dev) { | |
397 | ip_rt_put(rt); | |
398 | dev->stats.collisions++; | |
399 | goto tx_error; | |
400 | } | |
401 | ||
402 | if (tunnel->err_count > 0) { | |
403 | if (time_before(jiffies, | |
404 | tunnel->err_time + IPTUNNEL_ERR_TIMEO)) { | |
405 | tunnel->err_count--; | |
406 | dst_link_failure(skb); | |
407 | } else | |
408 | tunnel->err_count = 0; | |
409 | } | |
410 | ||
411 | IPCB(skb)->flags &= ~(IPSKB_XFRM_TUNNEL_SIZE | IPSKB_XFRM_TRANSFORMED | | |
412 | IPSKB_REROUTED); | |
413 | skb_dst_drop(skb); | |
414 | skb_dst_set(skb, &rt->dst); | |
415 | nf_reset(skb); | |
416 | skb->dev = skb_dst(skb)->dev; | |
417 | ||
418 | tstats = this_cpu_ptr(dev->tstats); | |
419 | VTI_XMIT(tstats, &dev->stats); | |
420 | return NETDEV_TX_OK; | |
421 | ||
422 | tx_error_icmp: | |
423 | dst_link_failure(skb); | |
424 | tx_error: | |
425 | dev->stats.tx_errors++; | |
426 | dev_kfree_skb(skb); | |
427 | return NETDEV_TX_OK; | |
428 | } | |
429 | ||
430 | static int vti_tunnel_bind_dev(struct net_device *dev) | |
431 | { | |
432 | struct net_device *tdev = NULL; | |
433 | struct ip_tunnel *tunnel; | |
434 | struct iphdr *iph; | |
435 | ||
436 | tunnel = netdev_priv(dev); | |
437 | iph = &tunnel->parms.iph; | |
438 | ||
439 | if (iph->daddr) { | |
440 | struct rtable *rt; | |
441 | struct flowi4 fl4; | |
442 | memset(&fl4, 0, sizeof(fl4)); | |
443 | flowi4_init_output(&fl4, tunnel->parms.link, | |
444 | htonl(tunnel->parms.i_key), | |
445 | RT_TOS(iph->tos), RT_SCOPE_UNIVERSE, | |
446 | IPPROTO_IPIP, 0, | |
447 | iph->daddr, iph->saddr, 0, 0); | |
448 | rt = ip_route_output_key(dev_net(dev), &fl4); | |
449 | if (!IS_ERR(rt)) { | |
450 | tdev = rt->dst.dev; | |
451 | ip_rt_put(rt); | |
452 | } | |
453 | dev->flags |= IFF_POINTOPOINT; | |
454 | } | |
455 | ||
456 | if (!tdev && tunnel->parms.link) | |
457 | tdev = __dev_get_by_index(dev_net(dev), tunnel->parms.link); | |
458 | ||
459 | if (tdev) { | |
460 | dev->hard_header_len = tdev->hard_header_len + | |
461 | sizeof(struct iphdr); | |
462 | dev->mtu = tdev->mtu; | |
463 | } | |
464 | dev->iflink = tunnel->parms.link; | |
465 | return dev->mtu; | |
466 | } | |
467 | ||
468 | static int | |
469 | vti_tunnel_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd) | |
470 | { | |
471 | int err = 0; | |
472 | struct ip_tunnel_parm p; | |
473 | struct ip_tunnel *t; | |
474 | struct net *net = dev_net(dev); | |
475 | struct vti_net *ipn = net_generic(net, vti_net_id); | |
476 | ||
477 | switch (cmd) { | |
478 | case SIOCGETTUNNEL: | |
479 | t = NULL; | |
480 | if (dev == ipn->fb_tunnel_dev) { | |
481 | if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, | |
482 | sizeof(p))) { | |
483 | err = -EFAULT; | |
484 | break; | |
485 | } | |
486 | t = vti_tunnel_locate(net, &p, 0); | |
487 | } | |
488 | if (t == NULL) | |
489 | t = netdev_priv(dev); | |
490 | memcpy(&p, &t->parms, sizeof(p)); | |
491 | p.i_flags |= GRE_KEY | VTI_ISVTI; | |
492 | p.o_flags |= GRE_KEY; | |
493 | if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p))) | |
494 | err = -EFAULT; | |
495 | break; | |
496 | ||
497 | case SIOCADDTUNNEL: | |
498 | case SIOCCHGTUNNEL: | |
499 | err = -EPERM; | |
500 | if (!capable(CAP_NET_ADMIN)) | |
501 | goto done; | |
502 | ||
503 | err = -EFAULT; | |
504 | if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) | |
505 | goto done; | |
506 | ||
507 | err = -EINVAL; | |
508 | if (p.iph.version != 4 || p.iph.protocol != IPPROTO_IPIP || | |
509 | p.iph.ihl != 5) | |
510 | goto done; | |
511 | ||
512 | t = vti_tunnel_locate(net, &p, cmd == SIOCADDTUNNEL); | |
513 | ||
514 | if (dev != ipn->fb_tunnel_dev && cmd == SIOCCHGTUNNEL) { | |
515 | if (t != NULL) { | |
516 | if (t->dev != dev) { | |
517 | err = -EEXIST; | |
518 | break; | |
519 | } | |
520 | } else { | |
521 | if (((dev->flags&IFF_POINTOPOINT) && | |
522 | !p.iph.daddr) || | |
523 | (!(dev->flags&IFF_POINTOPOINT) && | |
524 | p.iph.daddr)) { | |
525 | err = -EINVAL; | |
526 | break; | |
527 | } | |
528 | t = netdev_priv(dev); | |
529 | vti_tunnel_unlink(ipn, t); | |
530 | synchronize_net(); | |
531 | t->parms.iph.saddr = p.iph.saddr; | |
532 | t->parms.iph.daddr = p.iph.daddr; | |
533 | t->parms.i_key = p.i_key; | |
534 | t->parms.o_key = p.o_key; | |
535 | t->parms.iph.protocol = IPPROTO_IPIP; | |
536 | memcpy(dev->dev_addr, &p.iph.saddr, 4); | |
537 | memcpy(dev->broadcast, &p.iph.daddr, 4); | |
538 | vti_tunnel_link(ipn, t); | |
539 | netdev_state_change(dev); | |
540 | } | |
541 | } | |
542 | ||
543 | if (t) { | |
544 | err = 0; | |
545 | if (cmd == SIOCCHGTUNNEL) { | |
546 | t->parms.i_key = p.i_key; | |
547 | t->parms.o_key = p.o_key; | |
548 | if (t->parms.link != p.link) { | |
549 | t->parms.link = p.link; | |
550 | vti_tunnel_bind_dev(dev); | |
551 | netdev_state_change(dev); | |
552 | } | |
553 | } | |
554 | p.i_flags |= GRE_KEY | VTI_ISVTI; | |
555 | p.o_flags |= GRE_KEY; | |
556 | if (copy_to_user(ifr->ifr_ifru.ifru_data, &t->parms, | |
557 | sizeof(p))) | |
558 | err = -EFAULT; | |
559 | } else | |
560 | err = (cmd == SIOCADDTUNNEL ? -ENOBUFS : -ENOENT); | |
561 | break; | |
562 | ||
563 | case SIOCDELTUNNEL: | |
564 | err = -EPERM; | |
565 | if (!capable(CAP_NET_ADMIN)) | |
566 | goto done; | |
567 | ||
568 | if (dev == ipn->fb_tunnel_dev) { | |
569 | err = -EFAULT; | |
570 | if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, | |
571 | sizeof(p))) | |
572 | goto done; | |
573 | err = -ENOENT; | |
574 | ||
575 | t = vti_tunnel_locate(net, &p, 0); | |
576 | if (t == NULL) | |
577 | goto done; | |
578 | err = -EPERM; | |
579 | if (t->dev == ipn->fb_tunnel_dev) | |
580 | goto done; | |
581 | dev = t->dev; | |
582 | } | |
583 | unregister_netdevice(dev); | |
584 | err = 0; | |
585 | break; | |
586 | ||
587 | default: | |
588 | err = -EINVAL; | |
589 | } | |
590 | ||
591 | done: | |
592 | return err; | |
593 | } | |
594 | ||
595 | static int vti_tunnel_change_mtu(struct net_device *dev, int new_mtu) | |
596 | { | |
597 | if (new_mtu < 68 || new_mtu > 0xFFF8) | |
598 | return -EINVAL; | |
599 | dev->mtu = new_mtu; | |
600 | return 0; | |
601 | } | |
602 | ||
603 | static const struct net_device_ops vti_netdev_ops = { | |
604 | .ndo_init = vti_tunnel_init, | |
605 | .ndo_uninit = vti_tunnel_uninit, | |
606 | .ndo_start_xmit = vti_tunnel_xmit, | |
607 | .ndo_do_ioctl = vti_tunnel_ioctl, | |
608 | .ndo_change_mtu = vti_tunnel_change_mtu, | |
609 | .ndo_get_stats64 = vti_get_stats64, | |
610 | }; | |
611 | ||
612 | static void vti_dev_free(struct net_device *dev) | |
613 | { | |
614 | free_percpu(dev->tstats); | |
615 | free_netdev(dev); | |
616 | } | |
617 | ||
618 | static void vti_tunnel_setup(struct net_device *dev) | |
619 | { | |
620 | dev->netdev_ops = &vti_netdev_ops; | |
621 | dev->destructor = vti_dev_free; | |
622 | ||
623 | dev->type = ARPHRD_TUNNEL; | |
624 | dev->hard_header_len = LL_MAX_HEADER + sizeof(struct iphdr); | |
625 | dev->mtu = ETH_DATA_LEN; | |
626 | dev->flags = IFF_NOARP; | |
627 | dev->iflink = 0; | |
628 | dev->addr_len = 4; | |
629 | dev->features |= NETIF_F_NETNS_LOCAL; | |
630 | dev->features |= NETIF_F_LLTX; | |
631 | dev->priv_flags &= ~IFF_XMIT_DST_RELEASE; | |
632 | } | |
633 | ||
634 | static int vti_tunnel_init(struct net_device *dev) | |
635 | { | |
636 | struct ip_tunnel *tunnel = netdev_priv(dev); | |
637 | ||
638 | tunnel->dev = dev; | |
639 | strcpy(tunnel->parms.name, dev->name); | |
640 | ||
641 | memcpy(dev->dev_addr, &tunnel->parms.iph.saddr, 4); | |
642 | memcpy(dev->broadcast, &tunnel->parms.iph.daddr, 4); | |
643 | ||
644 | dev->tstats = alloc_percpu(struct pcpu_tstats); | |
645 | if (!dev->tstats) | |
646 | return -ENOMEM; | |
647 | ||
648 | return 0; | |
649 | } | |
650 | ||
651 | static int __net_init vti_fb_tunnel_init(struct net_device *dev) | |
652 | { | |
653 | struct ip_tunnel *tunnel = netdev_priv(dev); | |
654 | struct iphdr *iph = &tunnel->parms.iph; | |
655 | struct vti_net *ipn = net_generic(dev_net(dev), vti_net_id); | |
656 | ||
657 | tunnel->dev = dev; | |
658 | strcpy(tunnel->parms.name, dev->name); | |
659 | ||
660 | iph->version = 4; | |
661 | iph->protocol = IPPROTO_IPIP; | |
662 | iph->ihl = 5; | |
663 | ||
664 | dev->tstats = alloc_percpu(struct pcpu_tstats); | |
665 | if (!dev->tstats) | |
666 | return -ENOMEM; | |
667 | ||
668 | dev_hold(dev); | |
669 | rcu_assign_pointer(ipn->tunnels_wc[0], tunnel); | |
670 | return 0; | |
671 | } | |
672 | ||
673 | static struct xfrm_tunnel vti_handler __read_mostly = { | |
674 | .handler = vti_rcv, | |
675 | .err_handler = vti_err, | |
676 | .priority = 1, | |
677 | }; | |
678 | ||
679 | static void vti_destroy_tunnels(struct vti_net *ipn, struct list_head *head) | |
680 | { | |
681 | int prio; | |
682 | ||
683 | for (prio = 1; prio < 4; prio++) { | |
684 | int h; | |
685 | for (h = 0; h < HASH_SIZE; h++) { | |
686 | struct ip_tunnel *t; | |
687 | ||
688 | t = rtnl_dereference(ipn->tunnels[prio][h]); | |
689 | while (t != NULL) { | |
690 | unregister_netdevice_queue(t->dev, head); | |
691 | t = rtnl_dereference(t->next); | |
692 | } | |
693 | } | |
694 | } | |
695 | } | |
696 | ||
697 | static int __net_init vti_init_net(struct net *net) | |
698 | { | |
699 | int err; | |
700 | struct vti_net *ipn = net_generic(net, vti_net_id); | |
701 | ||
702 | ipn->tunnels[0] = ipn->tunnels_wc; | |
703 | ipn->tunnels[1] = ipn->tunnels_l; | |
704 | ipn->tunnels[2] = ipn->tunnels_r; | |
705 | ipn->tunnels[3] = ipn->tunnels_r_l; | |
706 | ||
707 | ipn->fb_tunnel_dev = alloc_netdev(sizeof(struct ip_tunnel), | |
708 | "ip_vti0", | |
709 | vti_tunnel_setup); | |
710 | if (!ipn->fb_tunnel_dev) { | |
711 | err = -ENOMEM; | |
712 | goto err_alloc_dev; | |
713 | } | |
714 | dev_net_set(ipn->fb_tunnel_dev, net); | |
715 | ||
716 | err = vti_fb_tunnel_init(ipn->fb_tunnel_dev); | |
717 | if (err) | |
718 | goto err_reg_dev; | |
719 | ipn->fb_tunnel_dev->rtnl_link_ops = &vti_link_ops; | |
720 | ||
721 | err = register_netdev(ipn->fb_tunnel_dev); | |
722 | if (err) | |
723 | goto err_reg_dev; | |
724 | return 0; | |
725 | ||
726 | err_reg_dev: | |
727 | vti_dev_free(ipn->fb_tunnel_dev); | |
728 | err_alloc_dev: | |
729 | /* nothing */ | |
730 | return err; | |
731 | } | |
732 | ||
733 | static void __net_exit vti_exit_net(struct net *net) | |
734 | { | |
735 | struct vti_net *ipn = net_generic(net, vti_net_id); | |
736 | LIST_HEAD(list); | |
737 | ||
738 | rtnl_lock(); | |
739 | vti_destroy_tunnels(ipn, &list); | |
740 | unregister_netdevice_many(&list); | |
741 | rtnl_unlock(); | |
742 | } | |
743 | ||
744 | static struct pernet_operations vti_net_ops = { | |
745 | .init = vti_init_net, | |
746 | .exit = vti_exit_net, | |
747 | .id = &vti_net_id, | |
748 | .size = sizeof(struct vti_net), | |
749 | }; | |
750 | ||
751 | static int vti_tunnel_validate(struct nlattr *tb[], struct nlattr *data[]) | |
752 | { | |
753 | return 0; | |
754 | } | |
755 | ||
756 | static void vti_netlink_parms(struct nlattr *data[], | |
757 | struct ip_tunnel_parm *parms) | |
758 | { | |
759 | memset(parms, 0, sizeof(*parms)); | |
760 | ||
761 | parms->iph.protocol = IPPROTO_IPIP; | |
762 | ||
763 | if (!data) | |
764 | return; | |
765 | ||
766 | if (data[IFLA_VTI_LINK]) | |
767 | parms->link = nla_get_u32(data[IFLA_VTI_LINK]); | |
768 | ||
769 | if (data[IFLA_VTI_IKEY]) | |
770 | parms->i_key = nla_get_be32(data[IFLA_VTI_IKEY]); | |
771 | ||
772 | if (data[IFLA_VTI_OKEY]) | |
773 | parms->o_key = nla_get_be32(data[IFLA_VTI_OKEY]); | |
774 | ||
775 | if (data[IFLA_VTI_LOCAL]) | |
776 | parms->iph.saddr = nla_get_be32(data[IFLA_VTI_LOCAL]); | |
777 | ||
778 | if (data[IFLA_VTI_REMOTE]) | |
779 | parms->iph.daddr = nla_get_be32(data[IFLA_VTI_REMOTE]); | |
780 | ||
781 | } | |
782 | ||
783 | static int vti_newlink(struct net *src_net, struct net_device *dev, | |
784 | struct nlattr *tb[], struct nlattr *data[]) | |
785 | { | |
786 | struct ip_tunnel *nt; | |
787 | struct net *net = dev_net(dev); | |
788 | struct vti_net *ipn = net_generic(net, vti_net_id); | |
789 | int mtu; | |
790 | int err; | |
791 | ||
792 | nt = netdev_priv(dev); | |
793 | vti_netlink_parms(data, &nt->parms); | |
794 | ||
795 | if (vti_tunnel_locate(net, &nt->parms, 0)) | |
796 | return -EEXIST; | |
797 | ||
798 | mtu = vti_tunnel_bind_dev(dev); | |
799 | if (!tb[IFLA_MTU]) | |
800 | dev->mtu = mtu; | |
801 | ||
802 | err = register_netdevice(dev); | |
803 | if (err) | |
804 | goto out; | |
805 | ||
806 | dev_hold(dev); | |
807 | vti_tunnel_link(ipn, nt); | |
808 | ||
809 | out: | |
810 | return err; | |
811 | } | |
812 | ||
813 | static int vti_changelink(struct net_device *dev, struct nlattr *tb[], | |
814 | struct nlattr *data[]) | |
815 | { | |
816 | struct ip_tunnel *t, *nt; | |
817 | struct net *net = dev_net(dev); | |
818 | struct vti_net *ipn = net_generic(net, vti_net_id); | |
819 | struct ip_tunnel_parm p; | |
820 | int mtu; | |
821 | ||
822 | if (dev == ipn->fb_tunnel_dev) | |
823 | return -EINVAL; | |
824 | ||
825 | nt = netdev_priv(dev); | |
826 | vti_netlink_parms(data, &p); | |
827 | ||
828 | t = vti_tunnel_locate(net, &p, 0); | |
829 | ||
830 | if (t) { | |
831 | if (t->dev != dev) | |
832 | return -EEXIST; | |
833 | } else { | |
834 | t = nt; | |
835 | ||
836 | vti_tunnel_unlink(ipn, t); | |
837 | t->parms.iph.saddr = p.iph.saddr; | |
838 | t->parms.iph.daddr = p.iph.daddr; | |
839 | t->parms.i_key = p.i_key; | |
840 | t->parms.o_key = p.o_key; | |
841 | if (dev->type != ARPHRD_ETHER) { | |
842 | memcpy(dev->dev_addr, &p.iph.saddr, 4); | |
843 | memcpy(dev->broadcast, &p.iph.daddr, 4); | |
844 | } | |
845 | vti_tunnel_link(ipn, t); | |
846 | netdev_state_change(dev); | |
847 | } | |
848 | ||
849 | if (t->parms.link != p.link) { | |
850 | t->parms.link = p.link; | |
851 | mtu = vti_tunnel_bind_dev(dev); | |
852 | if (!tb[IFLA_MTU]) | |
853 | dev->mtu = mtu; | |
854 | netdev_state_change(dev); | |
855 | } | |
856 | ||
857 | return 0; | |
858 | } | |
859 | ||
860 | static size_t vti_get_size(const struct net_device *dev) | |
861 | { | |
862 | return | |
863 | /* IFLA_VTI_LINK */ | |
864 | nla_total_size(4) + | |
865 | /* IFLA_VTI_IKEY */ | |
866 | nla_total_size(4) + | |
867 | /* IFLA_VTI_OKEY */ | |
868 | nla_total_size(4) + | |
869 | /* IFLA_VTI_LOCAL */ | |
870 | nla_total_size(4) + | |
871 | /* IFLA_VTI_REMOTE */ | |
872 | nla_total_size(4) + | |
873 | 0; | |
874 | } | |
875 | ||
876 | static int vti_fill_info(struct sk_buff *skb, const struct net_device *dev) | |
877 | { | |
878 | struct ip_tunnel *t = netdev_priv(dev); | |
879 | struct ip_tunnel_parm *p = &t->parms; | |
880 | ||
881 | nla_put_u32(skb, IFLA_VTI_LINK, p->link); | |
882 | nla_put_be32(skb, IFLA_VTI_IKEY, p->i_key); | |
883 | nla_put_be32(skb, IFLA_VTI_OKEY, p->o_key); | |
884 | nla_put_be32(skb, IFLA_VTI_LOCAL, p->iph.saddr); | |
885 | nla_put_be32(skb, IFLA_VTI_REMOTE, p->iph.daddr); | |
886 | ||
887 | return 0; | |
888 | } | |
889 | ||
890 | static const struct nla_policy vti_policy[IFLA_VTI_MAX + 1] = { | |
891 | [IFLA_VTI_LINK] = { .type = NLA_U32 }, | |
892 | [IFLA_VTI_IKEY] = { .type = NLA_U32 }, | |
893 | [IFLA_VTI_OKEY] = { .type = NLA_U32 }, | |
894 | [IFLA_VTI_LOCAL] = { .len = FIELD_SIZEOF(struct iphdr, saddr) }, | |
895 | [IFLA_VTI_REMOTE] = { .len = FIELD_SIZEOF(struct iphdr, daddr) }, | |
896 | }; | |
897 | ||
898 | static struct rtnl_link_ops vti_link_ops __read_mostly = { | |
899 | .kind = "vti", | |
900 | .maxtype = IFLA_VTI_MAX, | |
901 | .policy = vti_policy, | |
902 | .priv_size = sizeof(struct ip_tunnel), | |
903 | .setup = vti_tunnel_setup, | |
904 | .validate = vti_tunnel_validate, | |
905 | .newlink = vti_newlink, | |
906 | .changelink = vti_changelink, | |
907 | .get_size = vti_get_size, | |
908 | .fill_info = vti_fill_info, | |
909 | }; | |
910 | ||
911 | static int __init vti_init(void) | |
912 | { | |
913 | int err; | |
914 | ||
915 | pr_info("IPv4 over IPSec tunneling driver\n"); | |
916 | ||
917 | err = register_pernet_device(&vti_net_ops); | |
918 | if (err < 0) | |
919 | return err; | |
920 | err = xfrm4_mode_tunnel_input_register(&vti_handler); | |
921 | if (err < 0) { | |
922 | unregister_pernet_device(&vti_net_ops); | |
923 | pr_info(KERN_INFO "vti init: can't register tunnel\n"); | |
924 | } | |
925 | ||
926 | err = rtnl_link_register(&vti_link_ops); | |
927 | if (err < 0) | |
928 | goto rtnl_link_failed; | |
929 | ||
930 | return err; | |
931 | ||
932 | rtnl_link_failed: | |
933 | xfrm4_mode_tunnel_input_deregister(&vti_handler); | |
934 | unregister_pernet_device(&vti_net_ops); | |
935 | return err; | |
936 | } | |
937 | ||
938 | static void __exit vti_fini(void) | |
939 | { | |
940 | rtnl_link_unregister(&vti_link_ops); | |
941 | if (xfrm4_mode_tunnel_input_deregister(&vti_handler)) | |
942 | pr_info("vti close: can't deregister tunnel\n"); | |
943 | ||
944 | unregister_pernet_device(&vti_net_ops); | |
945 | } | |
946 | ||
947 | module_init(vti_init); | |
948 | module_exit(vti_fini); | |
949 | MODULE_LICENSE("GPL"); | |
950 | MODULE_ALIAS_RTNL_LINK("vti"); | |
951 | MODULE_ALIAS_NETDEV("ip_vti0"); |