]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - net/ipv4/ip_vti.c
Merge remote-tracking branch 'scsi/for-next'
[karo-tx-linux.git] / net / ipv4 / ip_vti.c
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/ip_tunnels.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 static struct rtnl_link_ops vti_link_ops __read_mostly;
48
49 static int vti_net_id __read_mostly;
50 static int vti_tunnel_init(struct net_device *dev);
51
52 /* We dont digest the packet therefore let the packet pass */
53 static int vti_rcv(struct sk_buff *skb)
54 {
55         struct ip_tunnel *tunnel;
56         const struct iphdr *iph = ip_hdr(skb);
57         struct net *net = dev_net(skb->dev);
58         struct ip_tunnel_net *itn = net_generic(net, vti_net_id);
59
60         tunnel = ip_tunnel_lookup(itn, skb->dev->ifindex, TUNNEL_NO_KEY,
61                                   iph->saddr, iph->daddr, 0);
62         if (tunnel != NULL) {
63                 struct pcpu_tstats *tstats;
64                 u32 oldmark = skb->mark;
65                 int ret;
66
67
68                 /* temporarily mark the skb with the tunnel o_key, to
69                  * only match policies with this mark.
70                  */
71                 skb->mark = be32_to_cpu(tunnel->parms.o_key);
72                 ret = xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb);
73                 skb->mark = oldmark;
74                 if (!ret)
75                         return -1;
76
77                 tstats = this_cpu_ptr(tunnel->dev->tstats);
78                 u64_stats_update_begin(&tstats->syncp);
79                 tstats->rx_packets++;
80                 tstats->rx_bytes += skb->len;
81                 u64_stats_update_end(&tstats->syncp);
82
83                 secpath_reset(skb);
84                 skb->dev = tunnel->dev;
85                 return 1;
86         }
87
88         return -1;
89 }
90
91 /* This function assumes it is being called from dev_queue_xmit()
92  * and that skb is filled properly by that function.
93  */
94
95 static netdev_tx_t vti_tunnel_xmit(struct sk_buff *skb, struct net_device *dev)
96 {
97         struct ip_tunnel *tunnel = netdev_priv(dev);
98         struct iphdr  *tiph = &tunnel->parms.iph;
99         u8     tos;
100         struct rtable *rt;              /* Route to the other host */
101         struct net_device *tdev;        /* Device to other host */
102         struct iphdr  *old_iph = ip_hdr(skb);
103         __be32 dst = tiph->daddr;
104         struct flowi4 fl4;
105         int err;
106
107         if (skb->protocol != htons(ETH_P_IP))
108                 goto tx_error;
109
110         tos = old_iph->tos;
111
112         memset(&fl4, 0, sizeof(fl4));
113         flowi4_init_output(&fl4, tunnel->parms.link,
114                            be32_to_cpu(tunnel->parms.o_key), RT_TOS(tos),
115                            RT_SCOPE_UNIVERSE,
116                            IPPROTO_IPIP, 0,
117                            dst, tiph->saddr, 0, 0);
118         rt = ip_route_output_key(dev_net(dev), &fl4);
119         if (IS_ERR(rt)) {
120                 dev->stats.tx_carrier_errors++;
121                 goto tx_error_icmp;
122         }
123         /* if there is no transform then this tunnel is not functional.
124          * Or if the xfrm is not mode tunnel.
125          */
126         if (!rt->dst.xfrm ||
127             rt->dst.xfrm->props.mode != XFRM_MODE_TUNNEL) {
128                 dev->stats.tx_carrier_errors++;
129                 goto tx_error_icmp;
130         }
131         tdev = rt->dst.dev;
132
133         if (tdev == dev) {
134                 ip_rt_put(rt);
135                 dev->stats.collisions++;
136                 goto tx_error;
137         }
138
139         if (tunnel->err_count > 0) {
140                 if (time_before(jiffies,
141                                 tunnel->err_time + IPTUNNEL_ERR_TIMEO)) {
142                         tunnel->err_count--;
143                         dst_link_failure(skb);
144                 } else
145                         tunnel->err_count = 0;
146         }
147
148         memset(IPCB(skb), 0, sizeof(*IPCB(skb)));
149         skb_dst_drop(skb);
150         skb_dst_set(skb, &rt->dst);
151         nf_reset(skb);
152         skb->dev = skb_dst(skb)->dev;
153
154         err = dst_output(skb);
155         if (net_xmit_eval(err) == 0)
156                 err = skb->len;
157         iptunnel_xmit_stats(err, &dev->stats, dev->tstats);
158         return NETDEV_TX_OK;
159
160 tx_error_icmp:
161         dst_link_failure(skb);
162 tx_error:
163         dev->stats.tx_errors++;
164         dev_kfree_skb(skb);
165         return NETDEV_TX_OK;
166 }
167
168 static int
169 vti_tunnel_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
170 {
171         int err = 0;
172         struct ip_tunnel_parm p;
173
174         if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
175                 return -EFAULT;
176
177         if (cmd == SIOCADDTUNNEL || cmd == SIOCCHGTUNNEL) {
178                 if (p.iph.version != 4 || p.iph.protocol != IPPROTO_IPIP ||
179                     p.iph.ihl != 5)
180                         return -EINVAL;
181         }
182
183         err = ip_tunnel_ioctl(dev, &p, cmd);
184         if (err)
185                 return err;
186
187         if (cmd != SIOCDELTUNNEL) {
188                 p.i_flags |= GRE_KEY | VTI_ISVTI;
189                 p.o_flags |= GRE_KEY;
190         }
191
192         if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p)))
193                 return -EFAULT;
194         return 0;
195 }
196
197 static const struct net_device_ops vti_netdev_ops = {
198         .ndo_init       = vti_tunnel_init,
199         .ndo_uninit     = ip_tunnel_uninit,
200         .ndo_start_xmit = vti_tunnel_xmit,
201         .ndo_do_ioctl   = vti_tunnel_ioctl,
202         .ndo_change_mtu = ip_tunnel_change_mtu,
203         .ndo_get_stats64 = ip_tunnel_get_stats64,
204 };
205
206 static void vti_tunnel_setup(struct net_device *dev)
207 {
208         dev->netdev_ops         = &vti_netdev_ops;
209         ip_tunnel_setup(dev, vti_net_id);
210 }
211
212 static int vti_tunnel_init(struct net_device *dev)
213 {
214         struct ip_tunnel *tunnel = netdev_priv(dev);
215         struct iphdr *iph = &tunnel->parms.iph;
216
217         memcpy(dev->dev_addr, &iph->saddr, 4);
218         memcpy(dev->broadcast, &iph->daddr, 4);
219
220         dev->type               = ARPHRD_TUNNEL;
221         dev->hard_header_len    = LL_MAX_HEADER + sizeof(struct iphdr);
222         dev->mtu                = ETH_DATA_LEN;
223         dev->flags              = IFF_NOARP;
224         dev->iflink             = 0;
225         dev->addr_len           = 4;
226         dev->features           |= NETIF_F_NETNS_LOCAL;
227         dev->features           |= NETIF_F_LLTX;
228         dev->priv_flags         &= ~IFF_XMIT_DST_RELEASE;
229
230         return ip_tunnel_init(dev);
231 }
232
233 static void __net_init vti_fb_tunnel_init(struct net_device *dev)
234 {
235         struct ip_tunnel *tunnel = netdev_priv(dev);
236         struct iphdr *iph = &tunnel->parms.iph;
237
238         iph->version            = 4;
239         iph->protocol           = IPPROTO_IPIP;
240         iph->ihl                = 5;
241 }
242
243 static struct xfrm_tunnel_notifier vti_handler __read_mostly = {
244         .handler        =       vti_rcv,
245         .priority       =       1,
246 };
247
248 static int __net_init vti_init_net(struct net *net)
249 {
250         int err;
251         struct ip_tunnel_net *itn;
252
253         err = ip_tunnel_init_net(net, vti_net_id, &vti_link_ops, "ip_vti0");
254         if (err)
255                 return err;
256         itn = net_generic(net, vti_net_id);
257         vti_fb_tunnel_init(itn->fb_tunnel_dev);
258         return 0;
259 }
260
261 static void __net_exit vti_exit_net(struct net *net)
262 {
263         struct ip_tunnel_net *itn = net_generic(net, vti_net_id);
264         ip_tunnel_delete_net(itn, &vti_link_ops);
265 }
266
267 static struct pernet_operations vti_net_ops = {
268         .init = vti_init_net,
269         .exit = vti_exit_net,
270         .id   = &vti_net_id,
271         .size = sizeof(struct ip_tunnel_net),
272 };
273
274 static int vti_tunnel_validate(struct nlattr *tb[], struct nlattr *data[])
275 {
276         return 0;
277 }
278
279 static void vti_netlink_parms(struct nlattr *data[],
280                               struct ip_tunnel_parm *parms)
281 {
282         memset(parms, 0, sizeof(*parms));
283
284         parms->iph.protocol = IPPROTO_IPIP;
285
286         if (!data)
287                 return;
288
289         if (data[IFLA_VTI_LINK])
290                 parms->link = nla_get_u32(data[IFLA_VTI_LINK]);
291
292         if (data[IFLA_VTI_IKEY])
293                 parms->i_key = nla_get_be32(data[IFLA_VTI_IKEY]);
294
295         if (data[IFLA_VTI_OKEY])
296                 parms->o_key = nla_get_be32(data[IFLA_VTI_OKEY]);
297
298         if (data[IFLA_VTI_LOCAL])
299                 parms->iph.saddr = nla_get_be32(data[IFLA_VTI_LOCAL]);
300
301         if (data[IFLA_VTI_REMOTE])
302                 parms->iph.daddr = nla_get_be32(data[IFLA_VTI_REMOTE]);
303
304 }
305
306 static int vti_newlink(struct net *src_net, struct net_device *dev,
307                        struct nlattr *tb[], struct nlattr *data[])
308 {
309         struct ip_tunnel_parm parms;
310
311         vti_netlink_parms(data, &parms);
312         return ip_tunnel_newlink(dev, tb, &parms);
313 }
314
315 static int vti_changelink(struct net_device *dev, struct nlattr *tb[],
316                           struct nlattr *data[])
317 {
318         struct ip_tunnel_parm p;
319
320         vti_netlink_parms(data, &p);
321         return ip_tunnel_changelink(dev, tb, &p);
322 }
323
324 static size_t vti_get_size(const struct net_device *dev)
325 {
326         return
327                 /* IFLA_VTI_LINK */
328                 nla_total_size(4) +
329                 /* IFLA_VTI_IKEY */
330                 nla_total_size(4) +
331                 /* IFLA_VTI_OKEY */
332                 nla_total_size(4) +
333                 /* IFLA_VTI_LOCAL */
334                 nla_total_size(4) +
335                 /* IFLA_VTI_REMOTE */
336                 nla_total_size(4) +
337                 0;
338 }
339
340 static int vti_fill_info(struct sk_buff *skb, const struct net_device *dev)
341 {
342         struct ip_tunnel *t = netdev_priv(dev);
343         struct ip_tunnel_parm *p = &t->parms;
344
345         nla_put_u32(skb, IFLA_VTI_LINK, p->link);
346         nla_put_be32(skb, IFLA_VTI_IKEY, p->i_key);
347         nla_put_be32(skb, IFLA_VTI_OKEY, p->o_key);
348         nla_put_be32(skb, IFLA_VTI_LOCAL, p->iph.saddr);
349         nla_put_be32(skb, IFLA_VTI_REMOTE, p->iph.daddr);
350
351         return 0;
352 }
353
354 static const struct nla_policy vti_policy[IFLA_VTI_MAX + 1] = {
355         [IFLA_VTI_LINK]         = { .type = NLA_U32 },
356         [IFLA_VTI_IKEY]         = { .type = NLA_U32 },
357         [IFLA_VTI_OKEY]         = { .type = NLA_U32 },
358         [IFLA_VTI_LOCAL]        = { .len = FIELD_SIZEOF(struct iphdr, saddr) },
359         [IFLA_VTI_REMOTE]       = { .len = FIELD_SIZEOF(struct iphdr, daddr) },
360 };
361
362 static struct rtnl_link_ops vti_link_ops __read_mostly = {
363         .kind           = "vti",
364         .maxtype        = IFLA_VTI_MAX,
365         .policy         = vti_policy,
366         .priv_size      = sizeof(struct ip_tunnel),
367         .setup          = vti_tunnel_setup,
368         .validate       = vti_tunnel_validate,
369         .newlink        = vti_newlink,
370         .changelink     = vti_changelink,
371         .get_size       = vti_get_size,
372         .fill_info      = vti_fill_info,
373 };
374
375 static int __init vti_init(void)
376 {
377         int err;
378
379         pr_info("IPv4 over IPSec tunneling driver\n");
380
381         err = register_pernet_device(&vti_net_ops);
382         if (err < 0)
383                 return err;
384         err = xfrm4_mode_tunnel_input_register(&vti_handler);
385         if (err < 0) {
386                 unregister_pernet_device(&vti_net_ops);
387                 pr_info("vti init: can't register tunnel\n");
388         }
389
390         err = rtnl_link_register(&vti_link_ops);
391         if (err < 0)
392                 goto rtnl_link_failed;
393
394         return err;
395
396 rtnl_link_failed:
397         xfrm4_mode_tunnel_input_deregister(&vti_handler);
398         unregister_pernet_device(&vti_net_ops);
399         return err;
400 }
401
402 static void __exit vti_fini(void)
403 {
404         rtnl_link_unregister(&vti_link_ops);
405         if (xfrm4_mode_tunnel_input_deregister(&vti_handler))
406                 pr_info("vti close: can't deregister tunnel\n");
407
408         unregister_pernet_device(&vti_net_ops);
409 }
410
411 module_init(vti_init);
412 module_exit(vti_fini);
413 MODULE_LICENSE("GPL");
414 MODULE_ALIAS_RTNL_LINK("vti");
415 MODULE_ALIAS_NETDEV("ip_vti0");