ip_vti.c 17 KB

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