ip_vti.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742
  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. IP_TUNNEL_DECLARE_FLAGS(flags) = { };
  47. __set_bit(IP_TUNNEL_NO_KEY_BIT, flags);
  48. tunnel = ip_tunnel_lookup(itn, skb->dev->ifindex, flags,
  49. iph->saddr, iph->daddr, 0);
  50. if (tunnel) {
  51. if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb))
  52. goto drop;
  53. XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip4 = tunnel;
  54. if (update_skb_dev)
  55. skb->dev = tunnel->dev;
  56. return xfrm_input(skb, nexthdr, spi, encap_type);
  57. }
  58. return -EINVAL;
  59. drop:
  60. kfree_skb(skb);
  61. return 0;
  62. }
  63. static int vti_input_proto(struct sk_buff *skb, int nexthdr, __be32 spi,
  64. int encap_type)
  65. {
  66. return vti_input(skb, nexthdr, spi, encap_type, false);
  67. }
  68. static int vti_rcv(struct sk_buff *skb, __be32 spi, bool update_skb_dev)
  69. {
  70. XFRM_SPI_SKB_CB(skb)->family = AF_INET;
  71. XFRM_SPI_SKB_CB(skb)->daddroff = offsetof(struct iphdr, daddr);
  72. return vti_input(skb, ip_hdr(skb)->protocol, spi, 0, update_skb_dev);
  73. }
  74. static int vti_rcv_proto(struct sk_buff *skb)
  75. {
  76. return vti_rcv(skb, 0, false);
  77. }
  78. static int vti_rcv_cb(struct sk_buff *skb, int err)
  79. {
  80. unsigned short family;
  81. struct net_device *dev;
  82. struct xfrm_state *x;
  83. const struct xfrm_mode *inner_mode;
  84. struct ip_tunnel *tunnel = XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip4;
  85. u32 orig_mark = skb->mark;
  86. int ret;
  87. if (!tunnel)
  88. return 1;
  89. dev = tunnel->dev;
  90. if (err) {
  91. DEV_STATS_INC(dev, rx_errors);
  92. DEV_STATS_INC(dev, rx_dropped);
  93. return 0;
  94. }
  95. x = xfrm_input_state(skb);
  96. inner_mode = &x->inner_mode;
  97. if (x->sel.family == AF_UNSPEC) {
  98. inner_mode = xfrm_ip2inner_mode(x, XFRM_MODE_SKB_CB(skb)->protocol);
  99. if (inner_mode == NULL) {
  100. XFRM_INC_STATS(dev_net(skb->dev),
  101. LINUX_MIB_XFRMINSTATEMODEERROR);
  102. return -EINVAL;
  103. }
  104. }
  105. family = inner_mode->family;
  106. skb->mark = be32_to_cpu(tunnel->parms.i_key);
  107. ret = xfrm_policy_check(NULL, XFRM_POLICY_IN, skb, family);
  108. skb->mark = orig_mark;
  109. if (!ret)
  110. return -EPERM;
  111. skb_scrub_packet(skb, !net_eq(tunnel->net, dev_net(skb->dev)));
  112. skb->dev = dev;
  113. dev_sw_netstats_rx_add(dev, skb->len);
  114. return 0;
  115. }
  116. static bool vti_state_check(const struct xfrm_state *x, __be32 dst, __be32 src)
  117. {
  118. xfrm_address_t *daddr = (xfrm_address_t *)&dst;
  119. xfrm_address_t *saddr = (xfrm_address_t *)&src;
  120. /* if there is no transform then this tunnel is not functional.
  121. * Or if the xfrm is not mode tunnel.
  122. */
  123. if (!x || x->props.mode != XFRM_MODE_TUNNEL ||
  124. x->props.family != AF_INET)
  125. return false;
  126. if (!dst)
  127. return xfrm_addr_equal(saddr, &x->props.saddr, AF_INET);
  128. if (!xfrm_state_addr_check(x, daddr, saddr, AF_INET))
  129. return false;
  130. return true;
  131. }
  132. static netdev_tx_t vti_xmit(struct sk_buff *skb, struct net_device *dev,
  133. struct flowi *fl)
  134. {
  135. struct ip_tunnel *tunnel = netdev_priv(dev);
  136. struct ip_tunnel_parm_kern *parms = &tunnel->parms;
  137. struct dst_entry *dst = skb_dst(skb);
  138. struct net_device *tdev; /* Device to other host */
  139. int pkt_len = skb->len;
  140. int err;
  141. int mtu;
  142. if (!dst) {
  143. switch (skb->protocol) {
  144. case htons(ETH_P_IP): {
  145. struct rtable *rt;
  146. fl->u.ip4.flowi4_oif = dev->ifindex;
  147. fl->u.ip4.flowi4_flags |= FLOWI_FLAG_ANYSRC;
  148. rt = __ip_route_output_key(dev_net(dev), &fl->u.ip4);
  149. if (IS_ERR(rt)) {
  150. DEV_STATS_INC(dev, tx_carrier_errors);
  151. goto tx_error_icmp;
  152. }
  153. dst = &rt->dst;
  154. skb_dst_set(skb, dst);
  155. break;
  156. }
  157. #if IS_ENABLED(CONFIG_IPV6)
  158. case htons(ETH_P_IPV6):
  159. fl->u.ip6.flowi6_oif = dev->ifindex;
  160. fl->u.ip6.flowi6_flags |= FLOWI_FLAG_ANYSRC;
  161. dst = ip6_route_output(dev_net(dev), NULL, &fl->u.ip6);
  162. if (dst->error) {
  163. dst_release(dst);
  164. dst = NULL;
  165. DEV_STATS_INC(dev, tx_carrier_errors);
  166. goto tx_error_icmp;
  167. }
  168. skb_dst_set(skb, dst);
  169. break;
  170. #endif
  171. default:
  172. DEV_STATS_INC(dev, tx_carrier_errors);
  173. goto tx_error_icmp;
  174. }
  175. }
  176. dst_hold(dst);
  177. dst = xfrm_lookup_route(tunnel->net, dst, fl, NULL, 0);
  178. if (IS_ERR(dst)) {
  179. DEV_STATS_INC(dev, tx_carrier_errors);
  180. goto tx_error_icmp;
  181. }
  182. if (dst->flags & DST_XFRM_QUEUE)
  183. goto xmit;
  184. if (!vti_state_check(dst->xfrm, parms->iph.daddr, parms->iph.saddr)) {
  185. DEV_STATS_INC(dev, tx_carrier_errors);
  186. dst_release(dst);
  187. goto tx_error_icmp;
  188. }
  189. tdev = dst_dev(dst);
  190. if (tdev == dev) {
  191. dst_release(dst);
  192. DEV_STATS_INC(dev, collisions);
  193. goto tx_error;
  194. }
  195. mtu = dst_mtu(dst);
  196. if (skb->len > mtu) {
  197. skb_dst_update_pmtu_no_confirm(skb, mtu);
  198. if (skb->protocol == htons(ETH_P_IP)) {
  199. if (!(ip_hdr(skb)->frag_off & htons(IP_DF)))
  200. goto xmit;
  201. icmp_ndo_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED,
  202. htonl(mtu));
  203. } else {
  204. if (mtu < IPV6_MIN_MTU)
  205. mtu = IPV6_MIN_MTU;
  206. icmpv6_ndo_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
  207. }
  208. dst_release(dst);
  209. goto tx_error;
  210. }
  211. xmit:
  212. skb_scrub_packet(skb, !net_eq(tunnel->net, dev_net(dev)));
  213. skb_dst_set(skb, dst);
  214. skb->dev = skb_dst_dev(skb);
  215. err = dst_output(tunnel->net, skb->sk, skb);
  216. if (net_xmit_eval(err) == 0)
  217. err = pkt_len;
  218. iptunnel_xmit_stats(dev, err);
  219. return NETDEV_TX_OK;
  220. tx_error_icmp:
  221. dst_link_failure(skb);
  222. tx_error:
  223. DEV_STATS_INC(dev, tx_errors);
  224. kfree_skb(skb);
  225. return NETDEV_TX_OK;
  226. }
  227. /* This function assumes it is being called from dev_queue_xmit()
  228. * and that skb is filled properly by that function.
  229. */
  230. static netdev_tx_t vti_tunnel_xmit(struct sk_buff *skb, struct net_device *dev)
  231. {
  232. struct ip_tunnel *tunnel = netdev_priv(dev);
  233. struct flowi fl;
  234. if (!pskb_inet_may_pull(skb))
  235. goto tx_err;
  236. memset(&fl, 0, sizeof(fl));
  237. switch (skb->protocol) {
  238. case htons(ETH_P_IP):
  239. memset(IPCB(skb), 0, sizeof(*IPCB(skb)));
  240. xfrm_decode_session(dev_net(dev), skb, &fl, AF_INET);
  241. break;
  242. case htons(ETH_P_IPV6):
  243. memset(IP6CB(skb), 0, sizeof(*IP6CB(skb)));
  244. xfrm_decode_session(dev_net(dev), skb, &fl, AF_INET6);
  245. break;
  246. default:
  247. goto tx_err;
  248. }
  249. /* override mark with tunnel output key */
  250. fl.flowi_mark = be32_to_cpu(tunnel->parms.o_key);
  251. return vti_xmit(skb, dev, &fl);
  252. tx_err:
  253. DEV_STATS_INC(dev, tx_errors);
  254. kfree_skb(skb);
  255. return NETDEV_TX_OK;
  256. }
  257. static int vti4_err(struct sk_buff *skb, u32 info)
  258. {
  259. __be32 spi;
  260. __u32 mark;
  261. struct xfrm_state *x;
  262. struct ip_tunnel *tunnel;
  263. struct ip_esp_hdr *esph;
  264. struct ip_auth_hdr *ah ;
  265. struct ip_comp_hdr *ipch;
  266. struct net *net = dev_net(skb->dev);
  267. const struct iphdr *iph = (const struct iphdr *)skb->data;
  268. int protocol = iph->protocol;
  269. struct ip_tunnel_net *itn = net_generic(net, vti_net_id);
  270. IP_TUNNEL_DECLARE_FLAGS(flags) = { };
  271. __set_bit(IP_TUNNEL_NO_KEY_BIT, flags);
  272. tunnel = ip_tunnel_lookup(itn, skb->dev->ifindex, flags,
  273. iph->daddr, iph->saddr, 0);
  274. if (!tunnel)
  275. return -1;
  276. mark = be32_to_cpu(tunnel->parms.o_key);
  277. switch (protocol) {
  278. case IPPROTO_ESP:
  279. esph = (struct ip_esp_hdr *)(skb->data+(iph->ihl<<2));
  280. spi = esph->spi;
  281. break;
  282. case IPPROTO_AH:
  283. ah = (struct ip_auth_hdr *)(skb->data+(iph->ihl<<2));
  284. spi = ah->spi;
  285. break;
  286. case IPPROTO_COMP:
  287. ipch = (struct ip_comp_hdr *)(skb->data+(iph->ihl<<2));
  288. spi = htonl(ntohs(ipch->cpi));
  289. break;
  290. default:
  291. return 0;
  292. }
  293. switch (icmp_hdr(skb)->type) {
  294. case ICMP_DEST_UNREACH:
  295. if (icmp_hdr(skb)->code != ICMP_FRAG_NEEDED)
  296. return 0;
  297. break;
  298. case ICMP_REDIRECT:
  299. break;
  300. default:
  301. return 0;
  302. }
  303. x = xfrm_state_lookup(net, mark, (const xfrm_address_t *)&iph->daddr,
  304. spi, protocol, AF_INET);
  305. if (!x)
  306. return 0;
  307. if (icmp_hdr(skb)->type == ICMP_DEST_UNREACH)
  308. ipv4_update_pmtu(skb, net, info, 0, protocol);
  309. else
  310. ipv4_redirect(skb, net, 0, protocol);
  311. xfrm_state_put(x);
  312. return 0;
  313. }
  314. static int
  315. vti_tunnel_ctl(struct net_device *dev, struct ip_tunnel_parm_kern *p, int cmd)
  316. {
  317. IP_TUNNEL_DECLARE_FLAGS(flags) = { };
  318. int err = 0;
  319. if (cmd == SIOCADDTUNNEL || cmd == SIOCCHGTUNNEL) {
  320. if (p->iph.version != 4 || p->iph.protocol != IPPROTO_IPIP ||
  321. p->iph.ihl != 5)
  322. return -EINVAL;
  323. }
  324. if (!ip_tunnel_flags_is_be16_compat(p->i_flags) ||
  325. !ip_tunnel_flags_is_be16_compat(p->o_flags))
  326. return -EOVERFLOW;
  327. if (!(ip_tunnel_flags_to_be16(p->i_flags) & GRE_KEY))
  328. p->i_key = 0;
  329. if (!(ip_tunnel_flags_to_be16(p->o_flags) & GRE_KEY))
  330. p->o_key = 0;
  331. __set_bit(IP_TUNNEL_VTI_BIT, flags);
  332. ip_tunnel_flags_copy(p->i_flags, flags);
  333. err = ip_tunnel_ctl(dev, p, cmd);
  334. if (err)
  335. return err;
  336. if (cmd != SIOCDELTUNNEL) {
  337. ip_tunnel_flags_from_be16(flags, GRE_KEY);
  338. ip_tunnel_flags_or(p->i_flags, p->i_flags, flags);
  339. ip_tunnel_flags_or(p->o_flags, p->o_flags, flags);
  340. }
  341. return 0;
  342. }
  343. static const struct net_device_ops vti_netdev_ops = {
  344. .ndo_init = vti_tunnel_init,
  345. .ndo_uninit = ip_tunnel_uninit,
  346. .ndo_start_xmit = vti_tunnel_xmit,
  347. .ndo_siocdevprivate = ip_tunnel_siocdevprivate,
  348. .ndo_change_mtu = ip_tunnel_change_mtu,
  349. .ndo_get_stats64 = dev_get_tstats64,
  350. .ndo_get_iflink = ip_tunnel_get_iflink,
  351. .ndo_tunnel_ctl = vti_tunnel_ctl,
  352. };
  353. static void vti_tunnel_setup(struct net_device *dev)
  354. {
  355. dev->netdev_ops = &vti_netdev_ops;
  356. dev->header_ops = &ip_tunnel_header_ops;
  357. dev->type = ARPHRD_TUNNEL;
  358. ip_tunnel_setup(dev, vti_net_id);
  359. }
  360. static int vti_tunnel_init(struct net_device *dev)
  361. {
  362. struct ip_tunnel *tunnel = netdev_priv(dev);
  363. struct iphdr *iph = &tunnel->parms.iph;
  364. __dev_addr_set(dev, &iph->saddr, 4);
  365. memcpy(dev->broadcast, &iph->daddr, 4);
  366. dev->flags = IFF_NOARP;
  367. dev->addr_len = 4;
  368. dev->lltx = true;
  369. netif_keep_dst(dev);
  370. return ip_tunnel_init(dev);
  371. }
  372. static void __net_init vti_fb_tunnel_init(struct net_device *dev)
  373. {
  374. struct ip_tunnel *tunnel = netdev_priv(dev);
  375. struct iphdr *iph = &tunnel->parms.iph;
  376. iph->version = 4;
  377. iph->protocol = IPPROTO_IPIP;
  378. iph->ihl = 5;
  379. }
  380. static struct xfrm4_protocol vti_esp4_protocol __read_mostly = {
  381. .handler = vti_rcv_proto,
  382. .input_handler = vti_input_proto,
  383. .cb_handler = vti_rcv_cb,
  384. .err_handler = vti4_err,
  385. .priority = 100,
  386. };
  387. static struct xfrm4_protocol vti_ah4_protocol __read_mostly = {
  388. .handler = vti_rcv_proto,
  389. .input_handler = vti_input_proto,
  390. .cb_handler = vti_rcv_cb,
  391. .err_handler = vti4_err,
  392. .priority = 100,
  393. };
  394. static struct xfrm4_protocol vti_ipcomp4_protocol __read_mostly = {
  395. .handler = vti_rcv_proto,
  396. .input_handler = vti_input_proto,
  397. .cb_handler = vti_rcv_cb,
  398. .err_handler = vti4_err,
  399. .priority = 100,
  400. };
  401. #if IS_ENABLED(CONFIG_INET_XFRM_TUNNEL)
  402. static int vti_rcv_tunnel(struct sk_buff *skb)
  403. {
  404. XFRM_SPI_SKB_CB(skb)->family = AF_INET;
  405. XFRM_SPI_SKB_CB(skb)->daddroff = offsetof(struct iphdr, daddr);
  406. return vti_input(skb, IPPROTO_IPIP, ip_hdr(skb)->saddr, 0, false);
  407. }
  408. static struct xfrm_tunnel vti_ipip_handler __read_mostly = {
  409. .handler = vti_rcv_tunnel,
  410. .cb_handler = vti_rcv_cb,
  411. .err_handler = vti4_err,
  412. .priority = 0,
  413. };
  414. #if IS_ENABLED(CONFIG_IPV6)
  415. static struct xfrm_tunnel vti_ipip6_handler __read_mostly = {
  416. .handler = vti_rcv_tunnel,
  417. .cb_handler = vti_rcv_cb,
  418. .err_handler = vti4_err,
  419. .priority = 0,
  420. };
  421. #endif
  422. #endif
  423. static int __net_init vti_init_net(struct net *net)
  424. {
  425. int err;
  426. struct ip_tunnel_net *itn;
  427. err = ip_tunnel_init_net(net, vti_net_id, &vti_link_ops, "ip_vti0");
  428. if (err)
  429. return err;
  430. itn = net_generic(net, vti_net_id);
  431. if (itn->fb_tunnel_dev)
  432. vti_fb_tunnel_init(itn->fb_tunnel_dev);
  433. return 0;
  434. }
  435. static void __net_exit vti_exit_batch_rtnl(struct list_head *list_net,
  436. struct list_head *dev_to_kill)
  437. {
  438. ip_tunnel_delete_nets(list_net, vti_net_id, &vti_link_ops,
  439. dev_to_kill);
  440. }
  441. static struct pernet_operations vti_net_ops = {
  442. .init = vti_init_net,
  443. .exit_batch_rtnl = vti_exit_batch_rtnl,
  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_kern *parms,
  454. __u32 *fwmark)
  455. {
  456. memset(parms, 0, sizeof(*parms));
  457. parms->iph.protocol = IPPROTO_IPIP;
  458. if (!data)
  459. return;
  460. __set_bit(IP_TUNNEL_VTI_BIT, parms->i_flags);
  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_kern 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. struct ip_tunnel_parm_kern p;
  489. __u32 fwmark = t->fwmark;
  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_kern *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 = sizeof_field(struct iphdr, saddr) },
  528. [IFLA_VTI_REMOTE] = { .len = sizeof_field(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. #if IS_ENABLED(CONFIG_INET_XFRM_TUNNEL)
  565. msg = "ipip tunnel";
  566. err = xfrm4_tunnel_register(&vti_ipip_handler, AF_INET);
  567. if (err < 0)
  568. goto xfrm_tunnel_ipip_failed;
  569. #if IS_ENABLED(CONFIG_IPV6)
  570. err = xfrm4_tunnel_register(&vti_ipip6_handler, AF_INET6);
  571. if (err < 0)
  572. goto xfrm_tunnel_ipip6_failed;
  573. #endif
  574. #endif
  575. msg = "netlink interface";
  576. err = rtnl_link_register(&vti_link_ops);
  577. if (err < 0)
  578. goto rtnl_link_failed;
  579. return err;
  580. rtnl_link_failed:
  581. #if IS_ENABLED(CONFIG_INET_XFRM_TUNNEL)
  582. #if IS_ENABLED(CONFIG_IPV6)
  583. xfrm4_tunnel_deregister(&vti_ipip6_handler, AF_INET6);
  584. xfrm_tunnel_ipip6_failed:
  585. #endif
  586. xfrm4_tunnel_deregister(&vti_ipip_handler, AF_INET);
  587. xfrm_tunnel_ipip_failed:
  588. #endif
  589. xfrm4_protocol_deregister(&vti_ipcomp4_protocol, IPPROTO_COMP);
  590. xfrm_proto_comp_failed:
  591. xfrm4_protocol_deregister(&vti_ah4_protocol, IPPROTO_AH);
  592. xfrm_proto_ah_failed:
  593. xfrm4_protocol_deregister(&vti_esp4_protocol, IPPROTO_ESP);
  594. xfrm_proto_esp_failed:
  595. unregister_pernet_device(&vti_net_ops);
  596. pernet_dev_failed:
  597. pr_err("vti init: failed to register %s\n", msg);
  598. return err;
  599. }
  600. static void __exit vti_fini(void)
  601. {
  602. rtnl_link_unregister(&vti_link_ops);
  603. #if IS_ENABLED(CONFIG_INET_XFRM_TUNNEL)
  604. #if IS_ENABLED(CONFIG_IPV6)
  605. xfrm4_tunnel_deregister(&vti_ipip6_handler, AF_INET6);
  606. #endif
  607. xfrm4_tunnel_deregister(&vti_ipip_handler, AF_INET);
  608. #endif
  609. xfrm4_protocol_deregister(&vti_ipcomp4_protocol, IPPROTO_COMP);
  610. xfrm4_protocol_deregister(&vti_ah4_protocol, IPPROTO_AH);
  611. xfrm4_protocol_deregister(&vti_esp4_protocol, IPPROTO_ESP);
  612. unregister_pernet_device(&vti_net_ops);
  613. }
  614. module_init(vti_init);
  615. module_exit(vti_fini);
  616. MODULE_DESCRIPTION("Virtual (secure) IP tunneling library");
  617. MODULE_LICENSE("GPL");
  618. MODULE_ALIAS_RTNL_LINK("vti");
  619. MODULE_ALIAS_NETDEV("ip_vti0");