ip_vti.c 17 KB

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