ip_vti.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726
  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 ([email protected]) 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 xmit;
  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. if (!(ip_hdr(skb)->frag_off & htons(IP_DF)))
  198. goto xmit;
  199. icmp_ndo_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED,
  200. htonl(mtu));
  201. } else {
  202. if (mtu < IPV6_MIN_MTU)
  203. mtu = IPV6_MIN_MTU;
  204. icmpv6_ndo_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
  205. }
  206. dst_release(dst);
  207. goto tx_error;
  208. }
  209. xmit:
  210. skb_scrub_packet(skb, !net_eq(tunnel->net, dev_net(dev)));
  211. skb_dst_set(skb, dst);
  212. skb->dev = skb_dst(skb)->dev;
  213. err = dst_output(tunnel->net, skb->sk, skb);
  214. if (net_xmit_eval(err) == 0)
  215. err = pkt_len;
  216. iptunnel_xmit_stats(dev, err);
  217. return NETDEV_TX_OK;
  218. tx_error_icmp:
  219. dst_link_failure(skb);
  220. tx_error:
  221. dev->stats.tx_errors++;
  222. kfree_skb(skb);
  223. return NETDEV_TX_OK;
  224. }
  225. /* This function assumes it is being called from dev_queue_xmit()
  226. * and that skb is filled properly by that function.
  227. */
  228. static netdev_tx_t vti_tunnel_xmit(struct sk_buff *skb, struct net_device *dev)
  229. {
  230. struct ip_tunnel *tunnel = netdev_priv(dev);
  231. struct flowi fl;
  232. if (!pskb_inet_may_pull(skb))
  233. goto tx_err;
  234. memset(&fl, 0, sizeof(fl));
  235. switch (skb->protocol) {
  236. case htons(ETH_P_IP):
  237. memset(IPCB(skb), 0, sizeof(*IPCB(skb)));
  238. xfrm_decode_session(skb, &fl, AF_INET);
  239. break;
  240. case htons(ETH_P_IPV6):
  241. memset(IP6CB(skb), 0, sizeof(*IP6CB(skb)));
  242. xfrm_decode_session(skb, &fl, AF_INET6);
  243. break;
  244. default:
  245. goto tx_err;
  246. }
  247. /* override mark with tunnel output key */
  248. fl.flowi_mark = be32_to_cpu(tunnel->parms.o_key);
  249. return vti_xmit(skb, dev, &fl);
  250. tx_err:
  251. dev->stats.tx_errors++;
  252. kfree_skb(skb);
  253. return NETDEV_TX_OK;
  254. }
  255. static int vti4_err(struct sk_buff *skb, u32 info)
  256. {
  257. __be32 spi;
  258. __u32 mark;
  259. struct xfrm_state *x;
  260. struct ip_tunnel *tunnel;
  261. struct ip_esp_hdr *esph;
  262. struct ip_auth_hdr *ah ;
  263. struct ip_comp_hdr *ipch;
  264. struct net *net = dev_net(skb->dev);
  265. const struct iphdr *iph = (const struct iphdr *)skb->data;
  266. int protocol = iph->protocol;
  267. struct ip_tunnel_net *itn = net_generic(net, vti_net_id);
  268. tunnel = ip_tunnel_lookup(itn, skb->dev->ifindex, TUNNEL_NO_KEY,
  269. iph->daddr, iph->saddr, 0);
  270. if (!tunnel)
  271. return -1;
  272. mark = be32_to_cpu(tunnel->parms.o_key);
  273. switch (protocol) {
  274. case IPPROTO_ESP:
  275. esph = (struct ip_esp_hdr *)(skb->data+(iph->ihl<<2));
  276. spi = esph->spi;
  277. break;
  278. case IPPROTO_AH:
  279. ah = (struct ip_auth_hdr *)(skb->data+(iph->ihl<<2));
  280. spi = ah->spi;
  281. break;
  282. case IPPROTO_COMP:
  283. ipch = (struct ip_comp_hdr *)(skb->data+(iph->ihl<<2));
  284. spi = htonl(ntohs(ipch->cpi));
  285. break;
  286. default:
  287. return 0;
  288. }
  289. switch (icmp_hdr(skb)->type) {
  290. case ICMP_DEST_UNREACH:
  291. if (icmp_hdr(skb)->code != ICMP_FRAG_NEEDED)
  292. return 0;
  293. break;
  294. case ICMP_REDIRECT:
  295. break;
  296. default:
  297. return 0;
  298. }
  299. x = xfrm_state_lookup(net, mark, (const xfrm_address_t *)&iph->daddr,
  300. spi, protocol, AF_INET);
  301. if (!x)
  302. return 0;
  303. if (icmp_hdr(skb)->type == ICMP_DEST_UNREACH)
  304. ipv4_update_pmtu(skb, net, info, 0, protocol);
  305. else
  306. ipv4_redirect(skb, net, 0, protocol);
  307. xfrm_state_put(x);
  308. return 0;
  309. }
  310. static int
  311. vti_tunnel_ctl(struct net_device *dev, struct ip_tunnel_parm *p, int cmd)
  312. {
  313. int err = 0;
  314. if (cmd == SIOCADDTUNNEL || cmd == SIOCCHGTUNNEL) {
  315. if (p->iph.version != 4 || p->iph.protocol != IPPROTO_IPIP ||
  316. p->iph.ihl != 5)
  317. return -EINVAL;
  318. }
  319. if (!(p->i_flags & GRE_KEY))
  320. p->i_key = 0;
  321. if (!(p->o_flags & GRE_KEY))
  322. p->o_key = 0;
  323. p->i_flags = VTI_ISVTI;
  324. err = ip_tunnel_ctl(dev, p, cmd);
  325. if (err)
  326. return err;
  327. if (cmd != SIOCDELTUNNEL) {
  328. p->i_flags |= GRE_KEY;
  329. p->o_flags |= GRE_KEY;
  330. }
  331. return 0;
  332. }
  333. static const struct net_device_ops vti_netdev_ops = {
  334. .ndo_init = vti_tunnel_init,
  335. .ndo_uninit = ip_tunnel_uninit,
  336. .ndo_start_xmit = vti_tunnel_xmit,
  337. .ndo_siocdevprivate = ip_tunnel_siocdevprivate,
  338. .ndo_change_mtu = ip_tunnel_change_mtu,
  339. .ndo_get_stats64 = dev_get_tstats64,
  340. .ndo_get_iflink = ip_tunnel_get_iflink,
  341. .ndo_tunnel_ctl = vti_tunnel_ctl,
  342. };
  343. static void vti_tunnel_setup(struct net_device *dev)
  344. {
  345. dev->netdev_ops = &vti_netdev_ops;
  346. dev->header_ops = &ip_tunnel_header_ops;
  347. dev->type = ARPHRD_TUNNEL;
  348. ip_tunnel_setup(dev, vti_net_id);
  349. }
  350. static int vti_tunnel_init(struct net_device *dev)
  351. {
  352. struct ip_tunnel *tunnel = netdev_priv(dev);
  353. struct iphdr *iph = &tunnel->parms.iph;
  354. __dev_addr_set(dev, &iph->saddr, 4);
  355. memcpy(dev->broadcast, &iph->daddr, 4);
  356. dev->flags = IFF_NOARP;
  357. dev->addr_len = 4;
  358. dev->features |= NETIF_F_LLTX;
  359. netif_keep_dst(dev);
  360. return ip_tunnel_init(dev);
  361. }
  362. static void __net_init vti_fb_tunnel_init(struct net_device *dev)
  363. {
  364. struct ip_tunnel *tunnel = netdev_priv(dev);
  365. struct iphdr *iph = &tunnel->parms.iph;
  366. iph->version = 4;
  367. iph->protocol = IPPROTO_IPIP;
  368. iph->ihl = 5;
  369. }
  370. static struct xfrm4_protocol vti_esp4_protocol __read_mostly = {
  371. .handler = vti_rcv_proto,
  372. .input_handler = vti_input_proto,
  373. .cb_handler = vti_rcv_cb,
  374. .err_handler = vti4_err,
  375. .priority = 100,
  376. };
  377. static struct xfrm4_protocol vti_ah4_protocol __read_mostly = {
  378. .handler = vti_rcv_proto,
  379. .input_handler = vti_input_proto,
  380. .cb_handler = vti_rcv_cb,
  381. .err_handler = vti4_err,
  382. .priority = 100,
  383. };
  384. static struct xfrm4_protocol vti_ipcomp4_protocol __read_mostly = {
  385. .handler = vti_rcv_proto,
  386. .input_handler = vti_input_proto,
  387. .cb_handler = vti_rcv_cb,
  388. .err_handler = vti4_err,
  389. .priority = 100,
  390. };
  391. #if IS_ENABLED(CONFIG_INET_XFRM_TUNNEL)
  392. static int vti_rcv_tunnel(struct sk_buff *skb)
  393. {
  394. XFRM_SPI_SKB_CB(skb)->family = AF_INET;
  395. XFRM_SPI_SKB_CB(skb)->daddroff = offsetof(struct iphdr, daddr);
  396. return vti_input(skb, IPPROTO_IPIP, ip_hdr(skb)->saddr, 0, false);
  397. }
  398. static struct xfrm_tunnel vti_ipip_handler __read_mostly = {
  399. .handler = vti_rcv_tunnel,
  400. .cb_handler = vti_rcv_cb,
  401. .err_handler = vti4_err,
  402. .priority = 0,
  403. };
  404. #if IS_ENABLED(CONFIG_IPV6)
  405. static struct xfrm_tunnel vti_ipip6_handler __read_mostly = {
  406. .handler = vti_rcv_tunnel,
  407. .cb_handler = vti_rcv_cb,
  408. .err_handler = vti4_err,
  409. .priority = 0,
  410. };
  411. #endif
  412. #endif
  413. static int __net_init vti_init_net(struct net *net)
  414. {
  415. int err;
  416. struct ip_tunnel_net *itn;
  417. err = ip_tunnel_init_net(net, vti_net_id, &vti_link_ops, "ip_vti0");
  418. if (err)
  419. return err;
  420. itn = net_generic(net, vti_net_id);
  421. if (itn->fb_tunnel_dev)
  422. vti_fb_tunnel_init(itn->fb_tunnel_dev);
  423. return 0;
  424. }
  425. static void __net_exit vti_exit_batch_net(struct list_head *list_net)
  426. {
  427. ip_tunnel_delete_nets(list_net, vti_net_id, &vti_link_ops);
  428. }
  429. static struct pernet_operations vti_net_ops = {
  430. .init = vti_init_net,
  431. .exit_batch = vti_exit_batch_net,
  432. .id = &vti_net_id,
  433. .size = sizeof(struct ip_tunnel_net),
  434. };
  435. static int vti_tunnel_validate(struct nlattr *tb[], struct nlattr *data[],
  436. struct netlink_ext_ack *extack)
  437. {
  438. return 0;
  439. }
  440. static void vti_netlink_parms(struct nlattr *data[],
  441. struct ip_tunnel_parm *parms,
  442. __u32 *fwmark)
  443. {
  444. memset(parms, 0, sizeof(*parms));
  445. parms->iph.protocol = IPPROTO_IPIP;
  446. if (!data)
  447. return;
  448. parms->i_flags = VTI_ISVTI;
  449. if (data[IFLA_VTI_LINK])
  450. parms->link = nla_get_u32(data[IFLA_VTI_LINK]);
  451. if (data[IFLA_VTI_IKEY])
  452. parms->i_key = nla_get_be32(data[IFLA_VTI_IKEY]);
  453. if (data[IFLA_VTI_OKEY])
  454. parms->o_key = nla_get_be32(data[IFLA_VTI_OKEY]);
  455. if (data[IFLA_VTI_LOCAL])
  456. parms->iph.saddr = nla_get_in_addr(data[IFLA_VTI_LOCAL]);
  457. if (data[IFLA_VTI_REMOTE])
  458. parms->iph.daddr = nla_get_in_addr(data[IFLA_VTI_REMOTE]);
  459. if (data[IFLA_VTI_FWMARK])
  460. *fwmark = nla_get_u32(data[IFLA_VTI_FWMARK]);
  461. }
  462. static int vti_newlink(struct net *src_net, struct net_device *dev,
  463. struct nlattr *tb[], struct nlattr *data[],
  464. struct netlink_ext_ack *extack)
  465. {
  466. struct ip_tunnel_parm parms;
  467. __u32 fwmark = 0;
  468. vti_netlink_parms(data, &parms, &fwmark);
  469. return ip_tunnel_newlink(dev, tb, &parms, fwmark);
  470. }
  471. static int vti_changelink(struct net_device *dev, struct nlattr *tb[],
  472. struct nlattr *data[],
  473. struct netlink_ext_ack *extack)
  474. {
  475. struct ip_tunnel *t = netdev_priv(dev);
  476. __u32 fwmark = t->fwmark;
  477. struct ip_tunnel_parm p;
  478. vti_netlink_parms(data, &p, &fwmark);
  479. return ip_tunnel_changelink(dev, tb, &p, fwmark);
  480. }
  481. static size_t vti_get_size(const struct net_device *dev)
  482. {
  483. return
  484. /* IFLA_VTI_LINK */
  485. nla_total_size(4) +
  486. /* IFLA_VTI_IKEY */
  487. nla_total_size(4) +
  488. /* IFLA_VTI_OKEY */
  489. nla_total_size(4) +
  490. /* IFLA_VTI_LOCAL */
  491. nla_total_size(4) +
  492. /* IFLA_VTI_REMOTE */
  493. nla_total_size(4) +
  494. /* IFLA_VTI_FWMARK */
  495. nla_total_size(4) +
  496. 0;
  497. }
  498. static int vti_fill_info(struct sk_buff *skb, const struct net_device *dev)
  499. {
  500. struct ip_tunnel *t = netdev_priv(dev);
  501. struct ip_tunnel_parm *p = &t->parms;
  502. if (nla_put_u32(skb, IFLA_VTI_LINK, p->link) ||
  503. nla_put_be32(skb, IFLA_VTI_IKEY, p->i_key) ||
  504. nla_put_be32(skb, IFLA_VTI_OKEY, p->o_key) ||
  505. nla_put_in_addr(skb, IFLA_VTI_LOCAL, p->iph.saddr) ||
  506. nla_put_in_addr(skb, IFLA_VTI_REMOTE, p->iph.daddr) ||
  507. nla_put_u32(skb, IFLA_VTI_FWMARK, t->fwmark))
  508. return -EMSGSIZE;
  509. return 0;
  510. }
  511. static const struct nla_policy vti_policy[IFLA_VTI_MAX + 1] = {
  512. [IFLA_VTI_LINK] = { .type = NLA_U32 },
  513. [IFLA_VTI_IKEY] = { .type = NLA_U32 },
  514. [IFLA_VTI_OKEY] = { .type = NLA_U32 },
  515. [IFLA_VTI_LOCAL] = { .len = sizeof_field(struct iphdr, saddr) },
  516. [IFLA_VTI_REMOTE] = { .len = sizeof_field(struct iphdr, daddr) },
  517. [IFLA_VTI_FWMARK] = { .type = NLA_U32 },
  518. };
  519. static struct rtnl_link_ops vti_link_ops __read_mostly = {
  520. .kind = "vti",
  521. .maxtype = IFLA_VTI_MAX,
  522. .policy = vti_policy,
  523. .priv_size = sizeof(struct ip_tunnel),
  524. .setup = vti_tunnel_setup,
  525. .validate = vti_tunnel_validate,
  526. .newlink = vti_newlink,
  527. .changelink = vti_changelink,
  528. .dellink = ip_tunnel_dellink,
  529. .get_size = vti_get_size,
  530. .fill_info = vti_fill_info,
  531. .get_link_net = ip_tunnel_get_link_net,
  532. };
  533. static int __init vti_init(void)
  534. {
  535. const char *msg;
  536. int err;
  537. pr_info("IPv4 over IPsec tunneling driver\n");
  538. msg = "tunnel device";
  539. err = register_pernet_device(&vti_net_ops);
  540. if (err < 0)
  541. goto pernet_dev_failed;
  542. msg = "tunnel protocols";
  543. err = xfrm4_protocol_register(&vti_esp4_protocol, IPPROTO_ESP);
  544. if (err < 0)
  545. goto xfrm_proto_esp_failed;
  546. err = xfrm4_protocol_register(&vti_ah4_protocol, IPPROTO_AH);
  547. if (err < 0)
  548. goto xfrm_proto_ah_failed;
  549. err = xfrm4_protocol_register(&vti_ipcomp4_protocol, IPPROTO_COMP);
  550. if (err < 0)
  551. goto xfrm_proto_comp_failed;
  552. #if IS_ENABLED(CONFIG_INET_XFRM_TUNNEL)
  553. msg = "ipip tunnel";
  554. err = xfrm4_tunnel_register(&vti_ipip_handler, AF_INET);
  555. if (err < 0)
  556. goto xfrm_tunnel_ipip_failed;
  557. #if IS_ENABLED(CONFIG_IPV6)
  558. err = xfrm4_tunnel_register(&vti_ipip6_handler, AF_INET6);
  559. if (err < 0)
  560. goto xfrm_tunnel_ipip6_failed;
  561. #endif
  562. #endif
  563. msg = "netlink interface";
  564. err = rtnl_link_register(&vti_link_ops);
  565. if (err < 0)
  566. goto rtnl_link_failed;
  567. return err;
  568. rtnl_link_failed:
  569. #if IS_ENABLED(CONFIG_INET_XFRM_TUNNEL)
  570. #if IS_ENABLED(CONFIG_IPV6)
  571. xfrm4_tunnel_deregister(&vti_ipip6_handler, AF_INET6);
  572. xfrm_tunnel_ipip6_failed:
  573. #endif
  574. xfrm4_tunnel_deregister(&vti_ipip_handler, AF_INET);
  575. xfrm_tunnel_ipip_failed:
  576. #endif
  577. xfrm4_protocol_deregister(&vti_ipcomp4_protocol, IPPROTO_COMP);
  578. xfrm_proto_comp_failed:
  579. xfrm4_protocol_deregister(&vti_ah4_protocol, IPPROTO_AH);
  580. xfrm_proto_ah_failed:
  581. xfrm4_protocol_deregister(&vti_esp4_protocol, IPPROTO_ESP);
  582. xfrm_proto_esp_failed:
  583. unregister_pernet_device(&vti_net_ops);
  584. pernet_dev_failed:
  585. pr_err("vti init: failed to register %s\n", msg);
  586. return err;
  587. }
  588. static void __exit vti_fini(void)
  589. {
  590. rtnl_link_unregister(&vti_link_ops);
  591. #if IS_ENABLED(CONFIG_INET_XFRM_TUNNEL)
  592. #if IS_ENABLED(CONFIG_IPV6)
  593. xfrm4_tunnel_deregister(&vti_ipip6_handler, AF_INET6);
  594. #endif
  595. xfrm4_tunnel_deregister(&vti_ipip_handler, AF_INET);
  596. #endif
  597. xfrm4_protocol_deregister(&vti_ipcomp4_protocol, IPPROTO_COMP);
  598. xfrm4_protocol_deregister(&vti_ah4_protocol, IPPROTO_AH);
  599. xfrm4_protocol_deregister(&vti_esp4_protocol, IPPROTO_ESP);
  600. unregister_pernet_device(&vti_net_ops);
  601. }
  602. module_init(vti_init);
  603. module_exit(vti_fini);
  604. MODULE_LICENSE("GPL");
  605. MODULE_ALIAS_RTNL_LINK("vti");
  606. MODULE_ALIAS_NETDEV("ip_vti0");