ip_vti.c 9.8 KB

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