ip_vti.c 14 KB

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