ip_vti.c 16 KB

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