ip_vti.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724
  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/init.h>
  30. #include <linux/netfilter_ipv4.h>
  31. #include <linux/if_ether.h>
  32. #include <linux/icmpv6.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 unsigned int vti_net_id __read_mostly;
  43. static int vti_tunnel_init(struct net_device *dev);
  44. static int vti_input(struct sk_buff *skb, int nexthdr, __be32 spi,
  45. int encap_type, bool update_skb_dev)
  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) {
  54. if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb))
  55. goto drop;
  56. XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip4 = tunnel;
  57. if (update_skb_dev)
  58. skb->dev = tunnel->dev;
  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_input_proto(struct sk_buff *skb, int nexthdr, __be32 spi,
  67. int encap_type)
  68. {
  69. return vti_input(skb, nexthdr, spi, encap_type, false);
  70. }
  71. static int vti_rcv(struct sk_buff *skb, __be32 spi, bool update_skb_dev)
  72. {
  73. XFRM_SPI_SKB_CB(skb)->family = AF_INET;
  74. XFRM_SPI_SKB_CB(skb)->daddroff = offsetof(struct iphdr, daddr);
  75. return vti_input(skb, ip_hdr(skb)->protocol, spi, 0, update_skb_dev);
  76. }
  77. static int vti_rcv_proto(struct sk_buff *skb)
  78. {
  79. return vti_rcv(skb, 0, false);
  80. }
  81. static int vti_rcv_tunnel(struct sk_buff *skb)
  82. {
  83. struct ip_tunnel_net *itn = net_generic(dev_net(skb->dev), vti_net_id);
  84. const struct iphdr *iph = ip_hdr(skb);
  85. struct ip_tunnel *tunnel;
  86. tunnel = ip_tunnel_lookup(itn, skb->dev->ifindex, TUNNEL_NO_KEY,
  87. iph->saddr, iph->daddr, 0);
  88. if (tunnel) {
  89. struct tnl_ptk_info tpi = {
  90. .proto = htons(ETH_P_IP),
  91. };
  92. if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb))
  93. goto drop;
  94. if (iptunnel_pull_header(skb, 0, tpi.proto, false))
  95. goto drop;
  96. return ip_tunnel_rcv(tunnel, skb, &tpi, NULL, false);
  97. }
  98. return -EINVAL;
  99. drop:
  100. kfree_skb(skb);
  101. return 0;
  102. }
  103. static int vti_rcv_cb(struct sk_buff *skb, int err)
  104. {
  105. unsigned short family;
  106. struct net_device *dev;
  107. struct pcpu_sw_netstats *tstats;
  108. struct xfrm_state *x;
  109. struct xfrm_mode *inner_mode;
  110. struct ip_tunnel *tunnel = XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip4;
  111. u32 orig_mark = skb->mark;
  112. int ret;
  113. if (!tunnel)
  114. return 1;
  115. dev = tunnel->dev;
  116. if (err) {
  117. dev->stats.rx_errors++;
  118. dev->stats.rx_dropped++;
  119. return 0;
  120. }
  121. x = xfrm_input_state(skb);
  122. inner_mode = x->inner_mode;
  123. if (x->sel.family == AF_UNSPEC) {
  124. inner_mode = xfrm_ip2inner_mode(x, XFRM_MODE_SKB_CB(skb)->protocol);
  125. if (inner_mode == NULL) {
  126. XFRM_INC_STATS(dev_net(skb->dev),
  127. LINUX_MIB_XFRMINSTATEMODEERROR);
  128. return -EINVAL;
  129. }
  130. }
  131. family = inner_mode->afinfo->family;
  132. skb->mark = be32_to_cpu(tunnel->parms.i_key);
  133. ret = xfrm_policy_check(NULL, XFRM_POLICY_IN, skb, family);
  134. skb->mark = orig_mark;
  135. if (!ret)
  136. return -EPERM;
  137. skb_scrub_packet(skb, !net_eq(tunnel->net, dev_net(skb->dev)));
  138. skb->dev = dev;
  139. tstats = this_cpu_ptr(dev->tstats);
  140. u64_stats_update_begin(&tstats->syncp);
  141. tstats->rx_packets++;
  142. tstats->rx_bytes += skb->len;
  143. u64_stats_update_end(&tstats->syncp);
  144. return 0;
  145. }
  146. static bool vti_state_check(const struct xfrm_state *x, __be32 dst, __be32 src)
  147. {
  148. xfrm_address_t *daddr = (xfrm_address_t *)&dst;
  149. xfrm_address_t *saddr = (xfrm_address_t *)&src;
  150. /* if there is no transform then this tunnel is not functional.
  151. * Or if the xfrm is not mode tunnel.
  152. */
  153. if (!x || x->props.mode != XFRM_MODE_TUNNEL ||
  154. x->props.family != AF_INET)
  155. return false;
  156. if (!dst)
  157. return xfrm_addr_equal(saddr, &x->props.saddr, AF_INET);
  158. if (!xfrm_state_addr_check(x, daddr, saddr, AF_INET))
  159. return false;
  160. return true;
  161. }
  162. static netdev_tx_t vti_xmit(struct sk_buff *skb, struct net_device *dev,
  163. struct flowi *fl)
  164. {
  165. struct ip_tunnel *tunnel = netdev_priv(dev);
  166. struct ip_tunnel_parm *parms = &tunnel->parms;
  167. struct dst_entry *dst = skb_dst(skb);
  168. struct net_device *tdev; /* Device to other host */
  169. int pkt_len = skb->len;
  170. int err;
  171. int mtu;
  172. if (!dst) {
  173. switch (skb->protocol) {
  174. case htons(ETH_P_IP): {
  175. struct rtable *rt;
  176. fl->u.ip4.flowi4_oif = dev->ifindex;
  177. fl->u.ip4.flowi4_flags |= FLOWI_FLAG_ANYSRC;
  178. rt = __ip_route_output_key(dev_net(dev), &fl->u.ip4);
  179. if (IS_ERR(rt)) {
  180. dev->stats.tx_carrier_errors++;
  181. goto tx_error_icmp;
  182. }
  183. dst = &rt->dst;
  184. skb_dst_set(skb, dst);
  185. break;
  186. }
  187. #if IS_ENABLED(CONFIG_IPV6)
  188. case htons(ETH_P_IPV6):
  189. fl->u.ip6.flowi6_oif = dev->ifindex;
  190. fl->u.ip6.flowi6_flags |= FLOWI_FLAG_ANYSRC;
  191. dst = ip6_route_output(dev_net(dev), NULL, &fl->u.ip6);
  192. if (dst->error) {
  193. dst_release(dst);
  194. dst = NULL;
  195. dev->stats.tx_carrier_errors++;
  196. goto tx_error_icmp;
  197. }
  198. skb_dst_set(skb, dst);
  199. break;
  200. #endif
  201. default:
  202. dev->stats.tx_carrier_errors++;
  203. goto tx_error_icmp;
  204. }
  205. }
  206. dst_hold(dst);
  207. dst = xfrm_lookup(tunnel->net, dst, fl, NULL, 0);
  208. if (IS_ERR(dst)) {
  209. dev->stats.tx_carrier_errors++;
  210. goto tx_error_icmp;
  211. }
  212. if (!vti_state_check(dst->xfrm, parms->iph.daddr, parms->iph.saddr)) {
  213. dev->stats.tx_carrier_errors++;
  214. dst_release(dst);
  215. goto tx_error_icmp;
  216. }
  217. tdev = dst->dev;
  218. if (tdev == dev) {
  219. dst_release(dst);
  220. dev->stats.collisions++;
  221. goto tx_error;
  222. }
  223. mtu = dst_mtu(dst);
  224. if (skb->len > mtu) {
  225. skb_dst_update_pmtu_no_confirm(skb, mtu);
  226. if (skb->protocol == htons(ETH_P_IP)) {
  227. icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED,
  228. htonl(mtu));
  229. } else {
  230. if (mtu < IPV6_MIN_MTU)
  231. mtu = IPV6_MIN_MTU;
  232. icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
  233. }
  234. dst_release(dst);
  235. goto tx_error;
  236. }
  237. skb_scrub_packet(skb, !net_eq(tunnel->net, dev_net(dev)));
  238. skb_dst_set(skb, dst);
  239. skb->dev = skb_dst(skb)->dev;
  240. err = dst_output(tunnel->net, skb->sk, skb);
  241. if (net_xmit_eval(err) == 0)
  242. err = pkt_len;
  243. iptunnel_xmit_stats(dev, err);
  244. return NETDEV_TX_OK;
  245. tx_error_icmp:
  246. dst_link_failure(skb);
  247. tx_error:
  248. dev->stats.tx_errors++;
  249. kfree_skb(skb);
  250. return NETDEV_TX_OK;
  251. }
  252. /* This function assumes it is being called from dev_queue_xmit()
  253. * and that skb is filled properly by that function.
  254. */
  255. static netdev_tx_t vti_tunnel_xmit(struct sk_buff *skb, struct net_device *dev)
  256. {
  257. struct ip_tunnel *tunnel = netdev_priv(dev);
  258. struct flowi fl;
  259. if (!pskb_inet_may_pull(skb))
  260. goto tx_err;
  261. memset(&fl, 0, sizeof(fl));
  262. switch (skb->protocol) {
  263. case htons(ETH_P_IP):
  264. xfrm_decode_session(skb, &fl, AF_INET);
  265. memset(IPCB(skb), 0, sizeof(*IPCB(skb)));
  266. break;
  267. case htons(ETH_P_IPV6):
  268. xfrm_decode_session(skb, &fl, AF_INET6);
  269. memset(IP6CB(skb), 0, sizeof(*IP6CB(skb)));
  270. break;
  271. default:
  272. goto tx_err;
  273. }
  274. /* override mark with tunnel output key */
  275. fl.flowi_mark = be32_to_cpu(tunnel->parms.o_key);
  276. return vti_xmit(skb, dev, &fl);
  277. tx_err:
  278. dev->stats.tx_errors++;
  279. kfree_skb(skb);
  280. return NETDEV_TX_OK;
  281. }
  282. static int vti4_err(struct sk_buff *skb, u32 info)
  283. {
  284. __be32 spi;
  285. __u32 mark;
  286. struct xfrm_state *x;
  287. struct ip_tunnel *tunnel;
  288. struct ip_esp_hdr *esph;
  289. struct ip_auth_hdr *ah ;
  290. struct ip_comp_hdr *ipch;
  291. struct net *net = dev_net(skb->dev);
  292. const struct iphdr *iph = (const struct iphdr *)skb->data;
  293. int protocol = iph->protocol;
  294. struct ip_tunnel_net *itn = net_generic(net, vti_net_id);
  295. tunnel = ip_tunnel_lookup(itn, skb->dev->ifindex, TUNNEL_NO_KEY,
  296. iph->daddr, iph->saddr, 0);
  297. if (!tunnel)
  298. return -1;
  299. mark = be32_to_cpu(tunnel->parms.o_key);
  300. switch (protocol) {
  301. case IPPROTO_ESP:
  302. esph = (struct ip_esp_hdr *)(skb->data+(iph->ihl<<2));
  303. spi = esph->spi;
  304. break;
  305. case IPPROTO_AH:
  306. ah = (struct ip_auth_hdr *)(skb->data+(iph->ihl<<2));
  307. spi = ah->spi;
  308. break;
  309. case IPPROTO_COMP:
  310. ipch = (struct ip_comp_hdr *)(skb->data+(iph->ihl<<2));
  311. spi = htonl(ntohs(ipch->cpi));
  312. break;
  313. default:
  314. return 0;
  315. }
  316. switch (icmp_hdr(skb)->type) {
  317. case ICMP_DEST_UNREACH:
  318. if (icmp_hdr(skb)->code != ICMP_FRAG_NEEDED)
  319. return 0;
  320. case ICMP_REDIRECT:
  321. break;
  322. default:
  323. return 0;
  324. }
  325. x = xfrm_state_lookup(net, mark, (const xfrm_address_t *)&iph->daddr,
  326. spi, protocol, AF_INET);
  327. if (!x)
  328. return 0;
  329. if (icmp_hdr(skb)->type == ICMP_DEST_UNREACH)
  330. ipv4_update_pmtu(skb, net, info, 0, 0, protocol, 0);
  331. else
  332. ipv4_redirect(skb, net, 0, 0, protocol, 0);
  333. xfrm_state_put(x);
  334. return 0;
  335. }
  336. static int
  337. vti_tunnel_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  338. {
  339. int err = 0;
  340. struct ip_tunnel_parm p;
  341. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
  342. return -EFAULT;
  343. if (cmd == SIOCADDTUNNEL || cmd == SIOCCHGTUNNEL) {
  344. if (p.iph.version != 4 || p.iph.protocol != IPPROTO_IPIP ||
  345. p.iph.ihl != 5)
  346. return -EINVAL;
  347. }
  348. if (!(p.i_flags & GRE_KEY))
  349. p.i_key = 0;
  350. if (!(p.o_flags & GRE_KEY))
  351. p.o_key = 0;
  352. p.i_flags = VTI_ISVTI;
  353. err = ip_tunnel_ioctl(dev, &p, cmd);
  354. if (err)
  355. return err;
  356. if (cmd != SIOCDELTUNNEL) {
  357. p.i_flags |= GRE_KEY;
  358. p.o_flags |= GRE_KEY;
  359. }
  360. if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p)))
  361. return -EFAULT;
  362. return 0;
  363. }
  364. static const struct net_device_ops vti_netdev_ops = {
  365. .ndo_init = vti_tunnel_init,
  366. .ndo_uninit = ip_tunnel_uninit,
  367. .ndo_start_xmit = vti_tunnel_xmit,
  368. .ndo_do_ioctl = vti_tunnel_ioctl,
  369. .ndo_change_mtu = ip_tunnel_change_mtu,
  370. .ndo_get_stats64 = ip_tunnel_get_stats64,
  371. .ndo_get_iflink = ip_tunnel_get_iflink,
  372. };
  373. static void vti_tunnel_setup(struct net_device *dev)
  374. {
  375. dev->netdev_ops = &vti_netdev_ops;
  376. dev->type = ARPHRD_TUNNEL;
  377. ip_tunnel_setup(dev, vti_net_id);
  378. }
  379. static int vti_tunnel_init(struct net_device *dev)
  380. {
  381. struct ip_tunnel *tunnel = netdev_priv(dev);
  382. struct iphdr *iph = &tunnel->parms.iph;
  383. memcpy(dev->dev_addr, &iph->saddr, 4);
  384. memcpy(dev->broadcast, &iph->daddr, 4);
  385. dev->flags = IFF_NOARP;
  386. dev->addr_len = 4;
  387. dev->features |= NETIF_F_LLTX;
  388. netif_keep_dst(dev);
  389. return ip_tunnel_init(dev);
  390. }
  391. static void __net_init vti_fb_tunnel_init(struct net_device *dev)
  392. {
  393. struct ip_tunnel *tunnel = netdev_priv(dev);
  394. struct iphdr *iph = &tunnel->parms.iph;
  395. iph->version = 4;
  396. iph->protocol = IPPROTO_IPIP;
  397. iph->ihl = 5;
  398. }
  399. static struct xfrm4_protocol vti_esp4_protocol __read_mostly = {
  400. .handler = vti_rcv_proto,
  401. .input_handler = vti_input_proto,
  402. .cb_handler = vti_rcv_cb,
  403. .err_handler = vti4_err,
  404. .priority = 100,
  405. };
  406. static struct xfrm4_protocol vti_ah4_protocol __read_mostly = {
  407. .handler = vti_rcv_proto,
  408. .input_handler = vti_input_proto,
  409. .cb_handler = vti_rcv_cb,
  410. .err_handler = vti4_err,
  411. .priority = 100,
  412. };
  413. static struct xfrm4_protocol vti_ipcomp4_protocol __read_mostly = {
  414. .handler = vti_rcv_proto,
  415. .input_handler = vti_input_proto,
  416. .cb_handler = vti_rcv_cb,
  417. .err_handler = vti4_err,
  418. .priority = 100,
  419. };
  420. static struct xfrm_tunnel ipip_handler __read_mostly = {
  421. .handler = vti_rcv_tunnel,
  422. .err_handler = vti4_err,
  423. .priority = 0,
  424. };
  425. static int __net_init vti_init_net(struct net *net)
  426. {
  427. int err;
  428. struct ip_tunnel_net *itn;
  429. err = ip_tunnel_init_net(net, vti_net_id, &vti_link_ops, "ip_vti0");
  430. if (err)
  431. return err;
  432. itn = net_generic(net, vti_net_id);
  433. if (itn->fb_tunnel_dev)
  434. vti_fb_tunnel_init(itn->fb_tunnel_dev);
  435. return 0;
  436. }
  437. static void __net_exit vti_exit_batch_net(struct list_head *list_net)
  438. {
  439. ip_tunnel_delete_nets(list_net, vti_net_id, &vti_link_ops);
  440. }
  441. static struct pernet_operations vti_net_ops = {
  442. .init = vti_init_net,
  443. .exit_batch = vti_exit_batch_net,
  444. .id = &vti_net_id,
  445. .size = sizeof(struct ip_tunnel_net),
  446. };
  447. static int vti_tunnel_validate(struct nlattr *tb[], struct nlattr *data[],
  448. struct netlink_ext_ack *extack)
  449. {
  450. return 0;
  451. }
  452. static void vti_netlink_parms(struct nlattr *data[],
  453. struct ip_tunnel_parm *parms,
  454. __u32 *fwmark)
  455. {
  456. memset(parms, 0, sizeof(*parms));
  457. parms->iph.protocol = IPPROTO_IPIP;
  458. if (!data)
  459. return;
  460. parms->i_flags = VTI_ISVTI;
  461. if (data[IFLA_VTI_LINK])
  462. parms->link = nla_get_u32(data[IFLA_VTI_LINK]);
  463. if (data[IFLA_VTI_IKEY])
  464. parms->i_key = nla_get_be32(data[IFLA_VTI_IKEY]);
  465. if (data[IFLA_VTI_OKEY])
  466. parms->o_key = nla_get_be32(data[IFLA_VTI_OKEY]);
  467. if (data[IFLA_VTI_LOCAL])
  468. parms->iph.saddr = nla_get_in_addr(data[IFLA_VTI_LOCAL]);
  469. if (data[IFLA_VTI_REMOTE])
  470. parms->iph.daddr = nla_get_in_addr(data[IFLA_VTI_REMOTE]);
  471. if (data[IFLA_VTI_FWMARK])
  472. *fwmark = nla_get_u32(data[IFLA_VTI_FWMARK]);
  473. }
  474. static int vti_newlink(struct net *src_net, struct net_device *dev,
  475. struct nlattr *tb[], struct nlattr *data[],
  476. struct netlink_ext_ack *extack)
  477. {
  478. struct ip_tunnel_parm parms;
  479. __u32 fwmark = 0;
  480. vti_netlink_parms(data, &parms, &fwmark);
  481. return ip_tunnel_newlink(dev, tb, &parms, fwmark);
  482. }
  483. static int vti_changelink(struct net_device *dev, struct nlattr *tb[],
  484. struct nlattr *data[],
  485. struct netlink_ext_ack *extack)
  486. {
  487. struct ip_tunnel *t = netdev_priv(dev);
  488. __u32 fwmark = t->fwmark;
  489. struct ip_tunnel_parm p;
  490. vti_netlink_parms(data, &p, &fwmark);
  491. return ip_tunnel_changelink(dev, tb, &p, fwmark);
  492. }
  493. static size_t vti_get_size(const struct net_device *dev)
  494. {
  495. return
  496. /* IFLA_VTI_LINK */
  497. nla_total_size(4) +
  498. /* IFLA_VTI_IKEY */
  499. nla_total_size(4) +
  500. /* IFLA_VTI_OKEY */
  501. nla_total_size(4) +
  502. /* IFLA_VTI_LOCAL */
  503. nla_total_size(4) +
  504. /* IFLA_VTI_REMOTE */
  505. nla_total_size(4) +
  506. /* IFLA_VTI_FWMARK */
  507. nla_total_size(4) +
  508. 0;
  509. }
  510. static int vti_fill_info(struct sk_buff *skb, const struct net_device *dev)
  511. {
  512. struct ip_tunnel *t = netdev_priv(dev);
  513. struct ip_tunnel_parm *p = &t->parms;
  514. if (nla_put_u32(skb, IFLA_VTI_LINK, p->link) ||
  515. nla_put_be32(skb, IFLA_VTI_IKEY, p->i_key) ||
  516. nla_put_be32(skb, IFLA_VTI_OKEY, p->o_key) ||
  517. nla_put_in_addr(skb, IFLA_VTI_LOCAL, p->iph.saddr) ||
  518. nla_put_in_addr(skb, IFLA_VTI_REMOTE, p->iph.daddr) ||
  519. nla_put_u32(skb, IFLA_VTI_FWMARK, t->fwmark))
  520. return -EMSGSIZE;
  521. return 0;
  522. }
  523. static const struct nla_policy vti_policy[IFLA_VTI_MAX + 1] = {
  524. [IFLA_VTI_LINK] = { .type = NLA_U32 },
  525. [IFLA_VTI_IKEY] = { .type = NLA_U32 },
  526. [IFLA_VTI_OKEY] = { .type = NLA_U32 },
  527. [IFLA_VTI_LOCAL] = { .len = FIELD_SIZEOF(struct iphdr, saddr) },
  528. [IFLA_VTI_REMOTE] = { .len = FIELD_SIZEOF(struct iphdr, daddr) },
  529. [IFLA_VTI_FWMARK] = { .type = NLA_U32 },
  530. };
  531. static struct rtnl_link_ops vti_link_ops __read_mostly = {
  532. .kind = "vti",
  533. .maxtype = IFLA_VTI_MAX,
  534. .policy = vti_policy,
  535. .priv_size = sizeof(struct ip_tunnel),
  536. .setup = vti_tunnel_setup,
  537. .validate = vti_tunnel_validate,
  538. .newlink = vti_newlink,
  539. .changelink = vti_changelink,
  540. .dellink = ip_tunnel_dellink,
  541. .get_size = vti_get_size,
  542. .fill_info = vti_fill_info,
  543. .get_link_net = ip_tunnel_get_link_net,
  544. };
  545. static int __init vti_init(void)
  546. {
  547. const char *msg;
  548. int err;
  549. pr_info("IPv4 over IPsec tunneling driver\n");
  550. msg = "tunnel device";
  551. err = register_pernet_device(&vti_net_ops);
  552. if (err < 0)
  553. goto pernet_dev_failed;
  554. msg = "tunnel protocols";
  555. err = xfrm4_protocol_register(&vti_esp4_protocol, IPPROTO_ESP);
  556. if (err < 0)
  557. goto xfrm_proto_esp_failed;
  558. err = xfrm4_protocol_register(&vti_ah4_protocol, IPPROTO_AH);
  559. if (err < 0)
  560. goto xfrm_proto_ah_failed;
  561. err = xfrm4_protocol_register(&vti_ipcomp4_protocol, IPPROTO_COMP);
  562. if (err < 0)
  563. goto xfrm_proto_comp_failed;
  564. msg = "ipip tunnel";
  565. err = xfrm4_tunnel_register(&ipip_handler, AF_INET);
  566. if (err < 0)
  567. goto xfrm_tunnel_failed;
  568. msg = "netlink interface";
  569. err = rtnl_link_register(&vti_link_ops);
  570. if (err < 0)
  571. goto rtnl_link_failed;
  572. return err;
  573. rtnl_link_failed:
  574. xfrm4_tunnel_deregister(&ipip_handler, AF_INET);
  575. xfrm_tunnel_failed:
  576. xfrm4_protocol_deregister(&vti_ipcomp4_protocol, IPPROTO_COMP);
  577. xfrm_proto_comp_failed:
  578. xfrm4_protocol_deregister(&vti_ah4_protocol, IPPROTO_AH);
  579. xfrm_proto_ah_failed:
  580. xfrm4_protocol_deregister(&vti_esp4_protocol, IPPROTO_ESP);
  581. xfrm_proto_esp_failed:
  582. unregister_pernet_device(&vti_net_ops);
  583. pernet_dev_failed:
  584. pr_err("vti init: failed to register %s\n", msg);
  585. return err;
  586. }
  587. static void __exit vti_fini(void)
  588. {
  589. rtnl_link_unregister(&vti_link_ops);
  590. xfrm4_tunnel_deregister(&ipip_handler, AF_INET);
  591. xfrm4_protocol_deregister(&vti_ipcomp4_protocol, IPPROTO_COMP);
  592. xfrm4_protocol_deregister(&vti_ah4_protocol, IPPROTO_AH);
  593. xfrm4_protocol_deregister(&vti_esp4_protocol, IPPROTO_ESP);
  594. unregister_pernet_device(&vti_net_ops);
  595. }
  596. module_init(vti_init);
  597. module_exit(vti_fini);
  598. MODULE_LICENSE("GPL");
  599. MODULE_ALIAS_RTNL_LINK("vti");
  600. MODULE_ALIAS_NETDEV("ip_vti0");