blob: 91f69bc883fe80cc97df0e34744caa6d40186bf8 [file] [log] [blame]
Saurabh11814122012-07-17 09:44:54 +00001/*
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>
Pravin B Shelarc5441932013-03-25 14:49:35 +000041#include <net/ip_tunnels.h>
Saurabh11814122012-07-17 09:44:54 +000042#include <net/inet_ecn.h>
43#include <net/xfrm.h>
44#include <net/net_namespace.h>
45#include <net/netns/generic.h>
46
Saurabh11814122012-07-17 09:44:54 +000047static struct rtnl_link_ops vti_link_ops __read_mostly;
48
49static int vti_net_id __read_mostly;
Saurabh11814122012-07-17 09:44:54 +000050static int vti_tunnel_init(struct net_device *dev);
Saurabh11814122012-07-17 09:44:54 +000051
Saurabh11814122012-07-17 09:44:54 +000052/* We dont digest the packet therefore let the packet pass */
53static int vti_rcv(struct sk_buff *skb)
54{
55 struct ip_tunnel *tunnel;
56 const struct iphdr *iph = ip_hdr(skb);
Amerigo Wangb9959fd2013-07-21 10:46:25 +080057 struct net *net = dev_net(skb->dev);
58 struct ip_tunnel_net *itn = net_generic(net, vti_net_id);
Saurabh11814122012-07-17 09:44:54 +000059
Amerigo Wangb9959fd2013-07-21 10:46:25 +080060 tunnel = ip_tunnel_lookup(itn, skb->dev->ifindex, TUNNEL_NO_KEY,
61 iph->saddr, iph->daddr, 0);
Saurabh11814122012-07-17 09:44:54 +000062 if (tunnel != NULL) {
63 struct pcpu_tstats *tstats;
64
Saurabh Mohanb2942002012-11-14 18:08:15 -080065 if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb))
66 return -1;
67
Saurabh11814122012-07-17 09:44:54 +000068 tstats = this_cpu_ptr(tunnel->dev->tstats);
69 u64_stats_update_begin(&tstats->syncp);
70 tstats->rx_packets++;
71 tstats->rx_bytes += skb->len;
72 u64_stats_update_end(&tstats->syncp);
73
Saurabh Mohanb2942002012-11-14 18:08:15 -080074 skb->mark = 0;
75 secpath_reset(skb);
Saurabh11814122012-07-17 09:44:54 +000076 skb->dev = tunnel->dev;
Saurabh11814122012-07-17 09:44:54 +000077 return 1;
78 }
Saurabh11814122012-07-17 09:44:54 +000079
80 return -1;
81}
82
83/* This function assumes it is being called from dev_queue_xmit()
84 * and that skb is filled properly by that function.
85 */
86
87static netdev_tx_t vti_tunnel_xmit(struct sk_buff *skb, struct net_device *dev)
88{
89 struct ip_tunnel *tunnel = netdev_priv(dev);
Saurabh11814122012-07-17 09:44:54 +000090 struct iphdr *tiph = &tunnel->parms.iph;
91 u8 tos;
92 struct rtable *rt; /* Route to the other host */
93 struct net_device *tdev; /* Device to other host */
94 struct iphdr *old_iph = ip_hdr(skb);
95 __be32 dst = tiph->daddr;
96 struct flowi4 fl4;
Amerigo Wangb9959fd2013-07-21 10:46:25 +080097 int err;
Saurabh11814122012-07-17 09:44:54 +000098
99 if (skb->protocol != htons(ETH_P_IP))
100 goto tx_error;
101
102 tos = old_iph->tos;
103
104 memset(&fl4, 0, sizeof(fl4));
105 flowi4_init_output(&fl4, tunnel->parms.link,
stephen hemminger8437e762012-10-11 12:51:28 +0000106 be32_to_cpu(tunnel->parms.i_key), RT_TOS(tos),
Saurabh11814122012-07-17 09:44:54 +0000107 RT_SCOPE_UNIVERSE,
108 IPPROTO_IPIP, 0,
109 dst, tiph->saddr, 0, 0);
110 rt = ip_route_output_key(dev_net(dev), &fl4);
111 if (IS_ERR(rt)) {
112 dev->stats.tx_carrier_errors++;
113 goto tx_error_icmp;
114 }
115 /* if there is no transform then this tunnel is not functional.
116 * Or if the xfrm is not mode tunnel.
117 */
118 if (!rt->dst.xfrm ||
119 rt->dst.xfrm->props.mode != XFRM_MODE_TUNNEL) {
120 dev->stats.tx_carrier_errors++;
121 goto tx_error_icmp;
122 }
123 tdev = rt->dst.dev;
124
125 if (tdev == dev) {
126 ip_rt_put(rt);
127 dev->stats.collisions++;
128 goto tx_error;
129 }
130
131 if (tunnel->err_count > 0) {
132 if (time_before(jiffies,
133 tunnel->err_time + IPTUNNEL_ERR_TIMEO)) {
134 tunnel->err_count--;
135 dst_link_failure(skb);
136 } else
137 tunnel->err_count = 0;
138 }
139
Saurabh Mohanbaafc772013-06-10 17:45:10 -0700140 memset(IPCB(skb), 0, sizeof(*IPCB(skb)));
Saurabh11814122012-07-17 09:44:54 +0000141 skb_dst_drop(skb);
142 skb_dst_set(skb, &rt->dst);
143 nf_reset(skb);
144 skb->dev = skb_dst(skb)->dev;
145
Amerigo Wangb9959fd2013-07-21 10:46:25 +0800146 err = dst_output(skb);
147 if (net_xmit_eval(err) == 0)
148 err = skb->len;
149 iptunnel_xmit_stats(err, &dev->stats, dev->tstats);
Saurabh11814122012-07-17 09:44:54 +0000150 return NETDEV_TX_OK;
151
152tx_error_icmp:
153 dst_link_failure(skb);
154tx_error:
155 dev->stats.tx_errors++;
156 dev_kfree_skb(skb);
157 return NETDEV_TX_OK;
158}
159
Saurabh11814122012-07-17 09:44:54 +0000160static int
161vti_tunnel_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
162{
163 int err = 0;
164 struct ip_tunnel_parm p;
Saurabh11814122012-07-17 09:44:54 +0000165
Amerigo Wangb9959fd2013-07-21 10:46:25 +0800166 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
167 return -EFAULT;
Saurabh11814122012-07-17 09:44:54 +0000168
Amerigo Wangb9959fd2013-07-21 10:46:25 +0800169 if (cmd == SIOCADDTUNNEL || cmd == SIOCCHGTUNNEL) {
Saurabh11814122012-07-17 09:44:54 +0000170 if (p.iph.version != 4 || p.iph.protocol != IPPROTO_IPIP ||
171 p.iph.ihl != 5)
Amerigo Wangb9959fd2013-07-21 10:46:25 +0800172 return -EINVAL;
Saurabh11814122012-07-17 09:44:54 +0000173 }
174
Amerigo Wangb9959fd2013-07-21 10:46:25 +0800175 err = ip_tunnel_ioctl(dev, &p, cmd);
176 if (err)
177 return err;
Saurabh11814122012-07-17 09:44:54 +0000178
Amerigo Wangb9959fd2013-07-21 10:46:25 +0800179 if (cmd != SIOCDELTUNNEL) {
180 p.i_flags |= GRE_KEY | VTI_ISVTI;
181 p.o_flags |= GRE_KEY;
182 }
183
184 if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p)))
185 return -EFAULT;
Saurabh11814122012-07-17 09:44:54 +0000186 return 0;
187}
188
189static const struct net_device_ops vti_netdev_ops = {
190 .ndo_init = vti_tunnel_init,
Amerigo Wangb9959fd2013-07-21 10:46:25 +0800191 .ndo_uninit = ip_tunnel_uninit,
Saurabh11814122012-07-17 09:44:54 +0000192 .ndo_start_xmit = vti_tunnel_xmit,
193 .ndo_do_ioctl = vti_tunnel_ioctl,
Amerigo Wangb9959fd2013-07-21 10:46:25 +0800194 .ndo_change_mtu = ip_tunnel_change_mtu,
Pravin B Shelarf61dd382013-03-25 14:50:00 +0000195 .ndo_get_stats64 = ip_tunnel_get_stats64,
Saurabh11814122012-07-17 09:44:54 +0000196};
197
Saurabh11814122012-07-17 09:44:54 +0000198static void vti_tunnel_setup(struct net_device *dev)
199{
200 dev->netdev_ops = &vti_netdev_ops;
Amerigo Wangb9959fd2013-07-21 10:46:25 +0800201 ip_tunnel_setup(dev, vti_net_id);
202}
203
204static int vti_tunnel_init(struct net_device *dev)
205{
206 struct ip_tunnel *tunnel = netdev_priv(dev);
207 struct iphdr *iph = &tunnel->parms.iph;
208
209 memcpy(dev->dev_addr, &iph->saddr, 4);
210 memcpy(dev->broadcast, &iph->daddr, 4);
Saurabh11814122012-07-17 09:44:54 +0000211
212 dev->type = ARPHRD_TUNNEL;
213 dev->hard_header_len = LL_MAX_HEADER + sizeof(struct iphdr);
214 dev->mtu = ETH_DATA_LEN;
215 dev->flags = IFF_NOARP;
216 dev->iflink = 0;
217 dev->addr_len = 4;
218 dev->features |= NETIF_F_NETNS_LOCAL;
219 dev->features |= NETIF_F_LLTX;
220 dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
Amerigo Wangb9959fd2013-07-21 10:46:25 +0800221
222 return ip_tunnel_init(dev);
Saurabh11814122012-07-17 09:44:54 +0000223}
224
Amerigo Wangb9959fd2013-07-21 10:46:25 +0800225static void __net_init vti_fb_tunnel_init(struct net_device *dev)
Saurabh11814122012-07-17 09:44:54 +0000226{
227 struct ip_tunnel *tunnel = netdev_priv(dev);
228 struct iphdr *iph = &tunnel->parms.iph;
Saurabh11814122012-07-17 09:44:54 +0000229
Saurabh11814122012-07-17 09:44:54 +0000230 iph->version = 4;
231 iph->protocol = IPPROTO_IPIP;
232 iph->ihl = 5;
Saurabh11814122012-07-17 09:44:54 +0000233}
234
Fan Duaba82692013-08-28 15:09:40 +0800235static struct xfrm_tunnel_notifier vti_handler __read_mostly = {
Saurabh11814122012-07-17 09:44:54 +0000236 .handler = vti_rcv,
Saurabh11814122012-07-17 09:44:54 +0000237 .priority = 1,
238};
239
Saurabh11814122012-07-17 09:44:54 +0000240static int __net_init vti_init_net(struct net *net)
241{
242 int err;
Amerigo Wangb9959fd2013-07-21 10:46:25 +0800243 struct ip_tunnel_net *itn;
Saurabh11814122012-07-17 09:44:54 +0000244
Amerigo Wangb9959fd2013-07-21 10:46:25 +0800245 err = ip_tunnel_init_net(net, vti_net_id, &vti_link_ops, "ip_vti0");
Saurabh11814122012-07-17 09:44:54 +0000246 if (err)
Amerigo Wangb9959fd2013-07-21 10:46:25 +0800247 return err;
248 itn = net_generic(net, vti_net_id);
249 vti_fb_tunnel_init(itn->fb_tunnel_dev);
Saurabh11814122012-07-17 09:44:54 +0000250 return 0;
Saurabh11814122012-07-17 09:44:54 +0000251}
252
253static void __net_exit vti_exit_net(struct net *net)
254{
Amerigo Wangb9959fd2013-07-21 10:46:25 +0800255 struct ip_tunnel_net *itn = net_generic(net, vti_net_id);
Nicolas Dichtel6c742e72013-08-13 17:51:11 +0200256 ip_tunnel_delete_net(itn, &vti_link_ops);
Saurabh11814122012-07-17 09:44:54 +0000257}
258
259static struct pernet_operations vti_net_ops = {
260 .init = vti_init_net,
261 .exit = vti_exit_net,
262 .id = &vti_net_id,
Amerigo Wangb9959fd2013-07-21 10:46:25 +0800263 .size = sizeof(struct ip_tunnel_net),
Saurabh11814122012-07-17 09:44:54 +0000264};
265
266static int vti_tunnel_validate(struct nlattr *tb[], struct nlattr *data[])
267{
268 return 0;
269}
270
271static void vti_netlink_parms(struct nlattr *data[],
272 struct ip_tunnel_parm *parms)
273{
274 memset(parms, 0, sizeof(*parms));
275
276 parms->iph.protocol = IPPROTO_IPIP;
277
278 if (!data)
279 return;
280
281 if (data[IFLA_VTI_LINK])
282 parms->link = nla_get_u32(data[IFLA_VTI_LINK]);
283
284 if (data[IFLA_VTI_IKEY])
285 parms->i_key = nla_get_be32(data[IFLA_VTI_IKEY]);
286
287 if (data[IFLA_VTI_OKEY])
288 parms->o_key = nla_get_be32(data[IFLA_VTI_OKEY]);
289
290 if (data[IFLA_VTI_LOCAL])
291 parms->iph.saddr = nla_get_be32(data[IFLA_VTI_LOCAL]);
292
293 if (data[IFLA_VTI_REMOTE])
294 parms->iph.daddr = nla_get_be32(data[IFLA_VTI_REMOTE]);
295
296}
297
298static int vti_newlink(struct net *src_net, struct net_device *dev,
299 struct nlattr *tb[], struct nlattr *data[])
300{
Amerigo Wangb9959fd2013-07-21 10:46:25 +0800301 struct ip_tunnel_parm parms;
Saurabh11814122012-07-17 09:44:54 +0000302
Amerigo Wangb9959fd2013-07-21 10:46:25 +0800303 vti_netlink_parms(data, &parms);
304 return ip_tunnel_newlink(dev, tb, &parms);
Saurabh11814122012-07-17 09:44:54 +0000305}
306
307static int vti_changelink(struct net_device *dev, struct nlattr *tb[],
308 struct nlattr *data[])
309{
Saurabh11814122012-07-17 09:44:54 +0000310 struct ip_tunnel_parm p;
Saurabh11814122012-07-17 09:44:54 +0000311
Saurabh11814122012-07-17 09:44:54 +0000312 vti_netlink_parms(data, &p);
Amerigo Wangb9959fd2013-07-21 10:46:25 +0800313 return ip_tunnel_changelink(dev, tb, &p);
Saurabh11814122012-07-17 09:44:54 +0000314}
315
316static size_t vti_get_size(const struct net_device *dev)
317{
318 return
319 /* IFLA_VTI_LINK */
320 nla_total_size(4) +
321 /* IFLA_VTI_IKEY */
322 nla_total_size(4) +
323 /* IFLA_VTI_OKEY */
324 nla_total_size(4) +
325 /* IFLA_VTI_LOCAL */
326 nla_total_size(4) +
327 /* IFLA_VTI_REMOTE */
328 nla_total_size(4) +
329 0;
330}
331
332static int vti_fill_info(struct sk_buff *skb, const struct net_device *dev)
333{
334 struct ip_tunnel *t = netdev_priv(dev);
335 struct ip_tunnel_parm *p = &t->parms;
336
337 nla_put_u32(skb, IFLA_VTI_LINK, p->link);
338 nla_put_be32(skb, IFLA_VTI_IKEY, p->i_key);
339 nla_put_be32(skb, IFLA_VTI_OKEY, p->o_key);
340 nla_put_be32(skb, IFLA_VTI_LOCAL, p->iph.saddr);
341 nla_put_be32(skb, IFLA_VTI_REMOTE, p->iph.daddr);
342
343 return 0;
344}
345
346static const struct nla_policy vti_policy[IFLA_VTI_MAX + 1] = {
347 [IFLA_VTI_LINK] = { .type = NLA_U32 },
348 [IFLA_VTI_IKEY] = { .type = NLA_U32 },
349 [IFLA_VTI_OKEY] = { .type = NLA_U32 },
350 [IFLA_VTI_LOCAL] = { .len = FIELD_SIZEOF(struct iphdr, saddr) },
351 [IFLA_VTI_REMOTE] = { .len = FIELD_SIZEOF(struct iphdr, daddr) },
352};
353
354static struct rtnl_link_ops vti_link_ops __read_mostly = {
355 .kind = "vti",
356 .maxtype = IFLA_VTI_MAX,
357 .policy = vti_policy,
358 .priv_size = sizeof(struct ip_tunnel),
359 .setup = vti_tunnel_setup,
360 .validate = vti_tunnel_validate,
361 .newlink = vti_newlink,
362 .changelink = vti_changelink,
363 .get_size = vti_get_size,
364 .fill_info = vti_fill_info,
365};
366
367static int __init vti_init(void)
368{
369 int err;
370
371 pr_info("IPv4 over IPSec tunneling driver\n");
372
373 err = register_pernet_device(&vti_net_ops);
374 if (err < 0)
375 return err;
376 err = xfrm4_mode_tunnel_input_register(&vti_handler);
377 if (err < 0) {
378 unregister_pernet_device(&vti_net_ops);
Amerigo Wangb9959fd2013-07-21 10:46:25 +0800379 pr_info("vti init: can't register tunnel\n");
Saurabh11814122012-07-17 09:44:54 +0000380 }
381
382 err = rtnl_link_register(&vti_link_ops);
383 if (err < 0)
384 goto rtnl_link_failed;
385
386 return err;
387
388rtnl_link_failed:
389 xfrm4_mode_tunnel_input_deregister(&vti_handler);
390 unregister_pernet_device(&vti_net_ops);
391 return err;
392}
393
394static void __exit vti_fini(void)
395{
396 rtnl_link_unregister(&vti_link_ops);
397 if (xfrm4_mode_tunnel_input_deregister(&vti_handler))
398 pr_info("vti close: can't deregister tunnel\n");
399
400 unregister_pernet_device(&vti_net_ops);
401}
402
403module_init(vti_init);
404module_exit(vti_fini);
405MODULE_LICENSE("GPL");
406MODULE_ALIAS_RTNL_LINK("vti");
407MODULE_ALIAS_NETDEV("ip_vti0");