vti4: Use the on xfrm_lookup returned dst_entry directly
[linux-2.6-block.git] / net / ipv4 / ip_vti.c
CommitLineData
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>
c5441932 41#include <net/ip_tunnels.h>
1181412c
S
42#include <net/inet_ecn.h>
43#include <net/xfrm.h>
44#include <net/net_namespace.h>
45#include <net/netns/generic.h>
46
1181412c
S
47static struct rtnl_link_ops vti_link_ops __read_mostly;
48
49static int vti_net_id __read_mostly;
1181412c 50static int vti_tunnel_init(struct net_device *dev);
1181412c 51
df3893c1
SK
52static int vti_input(struct sk_buff *skb, int nexthdr, __be32 spi,
53 int encap_type)
1181412c
S
54{
55 struct ip_tunnel *tunnel;
56 const struct iphdr *iph = ip_hdr(skb);
b9959fd3
AW
57 struct net *net = dev_net(skb->dev);
58 struct ip_tunnel_net *itn = net_generic(net, vti_net_id);
1181412c 59
b9959fd3
AW
60 tunnel = ip_tunnel_lookup(itn, skb->dev->ifindex, TUNNEL_NO_KEY,
61 iph->saddr, iph->daddr, 0);
1181412c 62 if (tunnel != NULL) {
df3893c1
SK
63 if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb))
64 goto drop;
65
66 XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip4 = tunnel;
67 skb->mark = be32_to_cpu(tunnel->parms.i_key);
68
69 return xfrm_input(skb, nexthdr, spi, encap_type);
70 }
71
72 return -EINVAL;
73drop:
74 kfree_skb(skb);
75 return 0;
76}
77
78static int vti_rcv(struct sk_buff *skb)
79{
80 XFRM_SPI_SKB_CB(skb)->family = AF_INET;
81 XFRM_SPI_SKB_CB(skb)->daddroff = offsetof(struct iphdr, daddr);
82
83 return vti_input(skb, ip_hdr(skb)->protocol, 0, 0);
84}
85
86static int vti_rcv_cb(struct sk_buff *skb, int err)
87{
88 unsigned short family;
89 struct net_device *dev;
90 struct pcpu_sw_netstats *tstats;
91 struct xfrm_state *x;
92 struct ip_tunnel *tunnel = XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip4;
93
94 if (!tunnel)
1181412c 95 return 1;
df3893c1
SK
96
97 dev = tunnel->dev;
98
99 if (err) {
100 dev->stats.rx_errors++;
101 dev->stats.rx_dropped++;
102
103 return 0;
1181412c 104 }
1181412c 105
df3893c1
SK
106 x = xfrm_input_state(skb);
107 family = x->inner_mode->afinfo->family;
108
109 if (!xfrm_policy_check(NULL, XFRM_POLICY_IN, skb, family))
110 return -EPERM;
111
112 skb_scrub_packet(skb, !net_eq(tunnel->net, dev_net(skb->dev)));
113 skb->dev = dev;
114
115 tstats = this_cpu_ptr(dev->tstats);
116
117 u64_stats_update_begin(&tstats->syncp);
118 tstats->rx_packets++;
119 tstats->rx_bytes += skb->len;
120 u64_stats_update_end(&tstats->syncp);
121
122 return 0;
1181412c
S
123}
124
125/* This function assumes it is being called from dev_queue_xmit()
126 * and that skb is filled properly by that function.
127 */
1181412c
S
128static netdev_tx_t vti_tunnel_xmit(struct sk_buff *skb, struct net_device *dev)
129{
130 struct ip_tunnel *tunnel = netdev_priv(dev);
a34cd4f3 131 struct dst_entry *dst = skb_dst(skb);
1181412c 132 struct net_device *tdev; /* Device to other host */
df3893c1 133 struct flowi fl;
b9959fd3 134 int err;
1181412c
S
135
136 if (skb->protocol != htons(ETH_P_IP))
137 goto tx_error;
138
df3893c1
SK
139 memset(&fl, 0, sizeof(fl));
140 skb->mark = be32_to_cpu(tunnel->parms.o_key);
141 xfrm_decode_session(skb, &fl, AF_INET);
142
a34cd4f3 143 if (!dst) {
df3893c1
SK
144 dev->stats.tx_carrier_errors++;
145 goto tx_error_icmp;
146 }
1181412c 147
a34cd4f3
SK
148 dst_hold(dst);
149 dst = xfrm_lookup(tunnel->net, dst, &fl, NULL, 0);
150 if (IS_ERR(dst)) {
1181412c
S
151 dev->stats.tx_carrier_errors++;
152 goto tx_error_icmp;
153 }
df3893c1 154
1181412c
S
155 /* if there is no transform then this tunnel is not functional.
156 * Or if the xfrm is not mode tunnel.
157 */
a34cd4f3
SK
158 if (!dst->xfrm ||
159 dst->xfrm->props.mode != XFRM_MODE_TUNNEL) {
1181412c 160 dev->stats.tx_carrier_errors++;
a34cd4f3 161 dst_release(dst);
1181412c
S
162 goto tx_error_icmp;
163 }
a34cd4f3 164 tdev = dst->dev;
1181412c
S
165
166 if (tdev == dev) {
a34cd4f3 167 dst_release(dst);
1181412c
S
168 dev->stats.collisions++;
169 goto tx_error;
170 }
171
172 if (tunnel->err_count > 0) {
173 if (time_before(jiffies,
174 tunnel->err_time + IPTUNNEL_ERR_TIMEO)) {
175 tunnel->err_count--;
176 dst_link_failure(skb);
177 } else
178 tunnel->err_count = 0;
179 }
180
baafc77b 181 memset(IPCB(skb), 0, sizeof(*IPCB(skb)));
df3893c1 182 skb_scrub_packet(skb, !net_eq(tunnel->net, dev_net(dev)));
a34cd4f3 183 skb_dst_set(skb, dst);
1181412c
S
184 skb->dev = skb_dst(skb)->dev;
185
b9959fd3
AW
186 err = dst_output(skb);
187 if (net_xmit_eval(err) == 0)
188 err = skb->len;
189 iptunnel_xmit_stats(err, &dev->stats, dev->tstats);
1181412c
S
190 return NETDEV_TX_OK;
191
192tx_error_icmp:
193 dst_link_failure(skb);
194tx_error:
195 dev->stats.tx_errors++;
3acfa1e7 196 kfree_skb(skb);
1181412c
S
197 return NETDEV_TX_OK;
198}
199
df3893c1
SK
200static int vti4_err(struct sk_buff *skb, u32 info)
201{
202 __be32 spi;
203 struct xfrm_state *x;
204 struct ip_tunnel *tunnel;
205 struct ip_esp_hdr *esph;
206 struct ip_auth_hdr *ah ;
207 struct ip_comp_hdr *ipch;
208 struct net *net = dev_net(skb->dev);
209 const struct iphdr *iph = (const struct iphdr *)skb->data;
210 int protocol = iph->protocol;
211 struct ip_tunnel_net *itn = net_generic(net, vti_net_id);
212
213 tunnel = ip_tunnel_lookup(itn, skb->dev->ifindex, TUNNEL_NO_KEY,
214 iph->daddr, iph->saddr, 0);
215 if (!tunnel)
216 return -1;
217
218 switch (protocol) {
219 case IPPROTO_ESP:
220 esph = (struct ip_esp_hdr *)(skb->data+(iph->ihl<<2));
221 spi = esph->spi;
222 break;
223 case IPPROTO_AH:
224 ah = (struct ip_auth_hdr *)(skb->data+(iph->ihl<<2));
225 spi = ah->spi;
226 break;
227 case IPPROTO_COMP:
228 ipch = (struct ip_comp_hdr *)(skb->data+(iph->ihl<<2));
229 spi = htonl(ntohs(ipch->cpi));
230 break;
231 default:
232 return 0;
233 }
234
235 switch (icmp_hdr(skb)->type) {
236 case ICMP_DEST_UNREACH:
237 if (icmp_hdr(skb)->code != ICMP_FRAG_NEEDED)
238 return 0;
239 case ICMP_REDIRECT:
240 break;
241 default:
242 return 0;
243 }
244
245 x = xfrm_state_lookup(net, skb->mark, (const xfrm_address_t *)&iph->daddr,
246 spi, protocol, AF_INET);
247 if (!x)
248 return 0;
249
250 if (icmp_hdr(skb)->type == ICMP_DEST_UNREACH)
251 ipv4_update_pmtu(skb, net, info, 0, 0, protocol, 0);
252 else
253 ipv4_redirect(skb, net, 0, 0, protocol, 0);
254 xfrm_state_put(x);
255
256 return 0;
257}
258
1181412c
S
259static int
260vti_tunnel_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
261{
262 int err = 0;
263 struct ip_tunnel_parm p;
1181412c 264
b9959fd3
AW
265 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
266 return -EFAULT;
1181412c 267
b9959fd3 268 if (cmd == SIOCADDTUNNEL || cmd == SIOCCHGTUNNEL) {
1181412c
S
269 if (p.iph.version != 4 || p.iph.protocol != IPPROTO_IPIP ||
270 p.iph.ihl != 5)
b9959fd3
AW
271 return -EINVAL;
272 }
1181412c 273
df3893c1 274 p.i_flags |= VTI_ISVTI;
b9959fd3
AW
275 err = ip_tunnel_ioctl(dev, &p, cmd);
276 if (err)
277 return err;
1181412c 278
b9959fd3 279 if (cmd != SIOCDELTUNNEL) {
df3893c1 280 p.i_flags |= GRE_KEY;
b9959fd3 281 p.o_flags |= GRE_KEY;
1181412c
S
282 }
283
b9959fd3
AW
284 if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p)))
285 return -EFAULT;
1181412c
S
286 return 0;
287}
288
289static const struct net_device_ops vti_netdev_ops = {
290 .ndo_init = vti_tunnel_init,
b9959fd3 291 .ndo_uninit = ip_tunnel_uninit,
1181412c
S
292 .ndo_start_xmit = vti_tunnel_xmit,
293 .ndo_do_ioctl = vti_tunnel_ioctl,
b9959fd3 294 .ndo_change_mtu = ip_tunnel_change_mtu,
f61dd388 295 .ndo_get_stats64 = ip_tunnel_get_stats64,
1181412c
S
296};
297
b9959fd3 298static void vti_tunnel_setup(struct net_device *dev)
1181412c 299{
b9959fd3
AW
300 dev->netdev_ops = &vti_netdev_ops;
301 ip_tunnel_setup(dev, vti_net_id);
1181412c
S
302}
303
b9959fd3 304static int vti_tunnel_init(struct net_device *dev)
1181412c 305{
b9959fd3
AW
306 struct ip_tunnel *tunnel = netdev_priv(dev);
307 struct iphdr *iph = &tunnel->parms.iph;
308
309 memcpy(dev->dev_addr, &iph->saddr, 4);
310 memcpy(dev->broadcast, &iph->daddr, 4);
1181412c
S
311
312 dev->type = ARPHRD_TUNNEL;
313 dev->hard_header_len = LL_MAX_HEADER + sizeof(struct iphdr);
314 dev->mtu = ETH_DATA_LEN;
315 dev->flags = IFF_NOARP;
316 dev->iflink = 0;
317 dev->addr_len = 4;
318 dev->features |= NETIF_F_NETNS_LOCAL;
319 dev->features |= NETIF_F_LLTX;
320 dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
1181412c 321
b9959fd3 322 return ip_tunnel_init(dev);
1181412c
S
323}
324
b9959fd3 325static void __net_init vti_fb_tunnel_init(struct net_device *dev)
1181412c
S
326{
327 struct ip_tunnel *tunnel = netdev_priv(dev);
328 struct iphdr *iph = &tunnel->parms.iph;
1181412c 329
1181412c
S
330 iph->version = 4;
331 iph->protocol = IPPROTO_IPIP;
332 iph->ihl = 5;
1181412c
S
333}
334
df3893c1 335static struct xfrm4_protocol vti_esp4_protocol __read_mostly = {
1181412c 336 .handler = vti_rcv,
df3893c1
SK
337 .input_handler = vti_input,
338 .cb_handler = vti_rcv_cb,
339 .err_handler = vti4_err,
340 .priority = 100,
341};
342
343static struct xfrm4_protocol vti_ah4_protocol __read_mostly = {
344 .handler = vti_rcv,
345 .input_handler = vti_input,
346 .cb_handler = vti_rcv_cb,
347 .err_handler = vti4_err,
348 .priority = 100,
349};
350
351static struct xfrm4_protocol vti_ipcomp4_protocol __read_mostly = {
352 .handler = vti_rcv,
353 .input_handler = vti_input,
354 .cb_handler = vti_rcv_cb,
355 .err_handler = vti4_err,
356 .priority = 100,
1181412c
S
357};
358
1181412c
S
359static int __net_init vti_init_net(struct net *net)
360{
361 int err;
b9959fd3 362 struct ip_tunnel_net *itn;
1181412c 363
b9959fd3 364 err = ip_tunnel_init_net(net, vti_net_id, &vti_link_ops, "ip_vti0");
1181412c 365 if (err)
b9959fd3
AW
366 return err;
367 itn = net_generic(net, vti_net_id);
368 vti_fb_tunnel_init(itn->fb_tunnel_dev);
1181412c 369 return 0;
1181412c
S
370}
371
372static void __net_exit vti_exit_net(struct net *net)
373{
b9959fd3 374 struct ip_tunnel_net *itn = net_generic(net, vti_net_id);
6c742e71 375 ip_tunnel_delete_net(itn, &vti_link_ops);
1181412c
S
376}
377
378static struct pernet_operations vti_net_ops = {
379 .init = vti_init_net,
380 .exit = vti_exit_net,
381 .id = &vti_net_id,
b9959fd3 382 .size = sizeof(struct ip_tunnel_net),
1181412c
S
383};
384
385static int vti_tunnel_validate(struct nlattr *tb[], struct nlattr *data[])
386{
387 return 0;
388}
389
390static void vti_netlink_parms(struct nlattr *data[],
391 struct ip_tunnel_parm *parms)
392{
393 memset(parms, 0, sizeof(*parms));
394
395 parms->iph.protocol = IPPROTO_IPIP;
396
397 if (!data)
398 return;
399
df3893c1
SK
400 parms->i_flags = VTI_ISVTI;
401
1181412c
S
402 if (data[IFLA_VTI_LINK])
403 parms->link = nla_get_u32(data[IFLA_VTI_LINK]);
404
405 if (data[IFLA_VTI_IKEY])
406 parms->i_key = nla_get_be32(data[IFLA_VTI_IKEY]);
407
408 if (data[IFLA_VTI_OKEY])
409 parms->o_key = nla_get_be32(data[IFLA_VTI_OKEY]);
410
411 if (data[IFLA_VTI_LOCAL])
412 parms->iph.saddr = nla_get_be32(data[IFLA_VTI_LOCAL]);
413
414 if (data[IFLA_VTI_REMOTE])
415 parms->iph.daddr = nla_get_be32(data[IFLA_VTI_REMOTE]);
416
417}
418
419static int vti_newlink(struct net *src_net, struct net_device *dev,
420 struct nlattr *tb[], struct nlattr *data[])
421{
b9959fd3 422 struct ip_tunnel_parm parms;
1181412c 423
b9959fd3
AW
424 vti_netlink_parms(data, &parms);
425 return ip_tunnel_newlink(dev, tb, &parms);
1181412c
S
426}
427
428static int vti_changelink(struct net_device *dev, struct nlattr *tb[],
429 struct nlattr *data[])
430{
1181412c 431 struct ip_tunnel_parm p;
1181412c 432
1181412c 433 vti_netlink_parms(data, &p);
b9959fd3 434 return ip_tunnel_changelink(dev, tb, &p);
1181412c
S
435}
436
437static size_t vti_get_size(const struct net_device *dev)
438{
439 return
440 /* IFLA_VTI_LINK */
441 nla_total_size(4) +
442 /* IFLA_VTI_IKEY */
443 nla_total_size(4) +
444 /* IFLA_VTI_OKEY */
445 nla_total_size(4) +
446 /* IFLA_VTI_LOCAL */
447 nla_total_size(4) +
448 /* IFLA_VTI_REMOTE */
449 nla_total_size(4) +
450 0;
451}
452
453static int vti_fill_info(struct sk_buff *skb, const struct net_device *dev)
454{
455 struct ip_tunnel *t = netdev_priv(dev);
456 struct ip_tunnel_parm *p = &t->parms;
457
458 nla_put_u32(skb, IFLA_VTI_LINK, p->link);
459 nla_put_be32(skb, IFLA_VTI_IKEY, p->i_key);
460 nla_put_be32(skb, IFLA_VTI_OKEY, p->o_key);
461 nla_put_be32(skb, IFLA_VTI_LOCAL, p->iph.saddr);
462 nla_put_be32(skb, IFLA_VTI_REMOTE, p->iph.daddr);
463
464 return 0;
465}
466
467static const struct nla_policy vti_policy[IFLA_VTI_MAX + 1] = {
468 [IFLA_VTI_LINK] = { .type = NLA_U32 },
469 [IFLA_VTI_IKEY] = { .type = NLA_U32 },
470 [IFLA_VTI_OKEY] = { .type = NLA_U32 },
471 [IFLA_VTI_LOCAL] = { .len = FIELD_SIZEOF(struct iphdr, saddr) },
472 [IFLA_VTI_REMOTE] = { .len = FIELD_SIZEOF(struct iphdr, daddr) },
473};
474
475static struct rtnl_link_ops vti_link_ops __read_mostly = {
476 .kind = "vti",
477 .maxtype = IFLA_VTI_MAX,
478 .policy = vti_policy,
479 .priv_size = sizeof(struct ip_tunnel),
480 .setup = vti_tunnel_setup,
481 .validate = vti_tunnel_validate,
482 .newlink = vti_newlink,
483 .changelink = vti_changelink,
484 .get_size = vti_get_size,
485 .fill_info = vti_fill_info,
486};
487
488static int __init vti_init(void)
489{
490 int err;
491
492 pr_info("IPv4 over IPSec tunneling driver\n");
493
494 err = register_pernet_device(&vti_net_ops);
495 if (err < 0)
496 return err;
df3893c1
SK
497 err = xfrm4_protocol_register(&vti_esp4_protocol, IPPROTO_ESP);
498 if (err < 0) {
499 unregister_pernet_device(&vti_net_ops);
500 pr_info("vti init: can't register tunnel\n");
501
502 return err;
503 }
504
505 err = xfrm4_protocol_register(&vti_ah4_protocol, IPPROTO_AH);
506 if (err < 0) {
507 xfrm4_protocol_deregister(&vti_esp4_protocol, IPPROTO_ESP);
508 unregister_pernet_device(&vti_net_ops);
509 pr_info("vti init: can't register tunnel\n");
510
511 return err;
512 }
513
514 err = xfrm4_protocol_register(&vti_ipcomp4_protocol, IPPROTO_COMP);
1181412c 515 if (err < 0) {
df3893c1
SK
516 xfrm4_protocol_deregister(&vti_ah4_protocol, IPPROTO_AH);
517 xfrm4_protocol_deregister(&vti_esp4_protocol, IPPROTO_ESP);
1181412c 518 unregister_pernet_device(&vti_net_ops);
b9959fd3 519 pr_info("vti init: can't register tunnel\n");
df3893c1
SK
520
521 return err;
1181412c
S
522 }
523
524 err = rtnl_link_register(&vti_link_ops);
525 if (err < 0)
526 goto rtnl_link_failed;
527
528 return err;
529
530rtnl_link_failed:
df3893c1
SK
531 xfrm4_protocol_deregister(&vti_ipcomp4_protocol, IPPROTO_COMP);
532 xfrm4_protocol_deregister(&vti_ah4_protocol, IPPROTO_AH);
533 xfrm4_protocol_deregister(&vti_esp4_protocol, IPPROTO_ESP);
1181412c
S
534 unregister_pernet_device(&vti_net_ops);
535 return err;
536}
537
538static void __exit vti_fini(void)
539{
540 rtnl_link_unregister(&vti_link_ops);
df3893c1
SK
541 if (xfrm4_protocol_deregister(&vti_ipcomp4_protocol, IPPROTO_COMP))
542 pr_info("vti close: can't deregister tunnel\n");
543 if (xfrm4_protocol_deregister(&vti_ah4_protocol, IPPROTO_AH))
1181412c 544 pr_info("vti close: can't deregister tunnel\n");
df3893c1
SK
545 if (xfrm4_protocol_deregister(&vti_esp4_protocol, IPPROTO_ESP))
546 pr_info("vti close: can't deregister tunnel\n");
547
1181412c
S
548
549 unregister_pernet_device(&vti_net_ops);
550}
551
552module_init(vti_init);
553module_exit(vti_fini);
554MODULE_LICENSE("GPL");
555MODULE_ALIAS_RTNL_LINK("vti");
556MODULE_ALIAS_NETDEV("ip_vti0");