xfrm_interface.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * XFRM virtual interface
  4. *
  5. * Copyright (C) 2018 secunet Security Networks AG
  6. *
  7. * Author:
  8. * Steffen Klassert <steffen.klassert@secunet.com>
  9. */
  10. #include <linux/module.h>
  11. #include <linux/capability.h>
  12. #include <linux/errno.h>
  13. #include <linux/types.h>
  14. #include <linux/sockios.h>
  15. #include <linux/icmp.h>
  16. #include <linux/if.h>
  17. #include <linux/in.h>
  18. #include <linux/ip.h>
  19. #include <linux/net.h>
  20. #include <linux/in6.h>
  21. #include <linux/netdevice.h>
  22. #include <linux/if_link.h>
  23. #include <linux/if_arp.h>
  24. #include <linux/icmpv6.h>
  25. #include <linux/init.h>
  26. #include <linux/route.h>
  27. #include <linux/rtnetlink.h>
  28. #include <linux/netfilter_ipv6.h>
  29. #include <linux/slab.h>
  30. #include <linux/hash.h>
  31. #include <linux/uaccess.h>
  32. #include <linux/atomic.h>
  33. #include <net/icmp.h>
  34. #include <net/ip.h>
  35. #include <net/ipv6.h>
  36. #include <net/ip6_route.h>
  37. #include <net/addrconf.h>
  38. #include <net/xfrm.h>
  39. #include <net/net_namespace.h>
  40. #include <net/netns/generic.h>
  41. #include <linux/etherdevice.h>
  42. static int xfrmi_dev_init(struct net_device *dev);
  43. static void xfrmi_dev_setup(struct net_device *dev);
  44. static struct rtnl_link_ops xfrmi_link_ops __read_mostly;
  45. static unsigned int xfrmi_net_id __read_mostly;
  46. struct xfrmi_net {
  47. /* lists for storing interfaces in use */
  48. struct xfrm_if __rcu *xfrmi[1];
  49. };
  50. #define for_each_xfrmi_rcu(start, xi) \
  51. for (xi = rcu_dereference(start); xi; xi = rcu_dereference(xi->next))
  52. static struct xfrm_if *xfrmi_lookup(struct net *net, struct xfrm_state *x)
  53. {
  54. struct xfrmi_net *xfrmn = net_generic(net, xfrmi_net_id);
  55. struct xfrm_if *xi;
  56. for_each_xfrmi_rcu(xfrmn->xfrmi[0], xi) {
  57. if (x->if_id == xi->p.if_id &&
  58. (xi->dev->flags & IFF_UP))
  59. return xi;
  60. }
  61. return NULL;
  62. }
  63. static struct xfrm_if *xfrmi_decode_session(struct sk_buff *skb,
  64. unsigned short family)
  65. {
  66. struct xfrmi_net *xfrmn;
  67. struct xfrm_if *xi;
  68. int ifindex = 0;
  69. if (!secpath_exists(skb) || !skb->dev)
  70. return NULL;
  71. switch (family) {
  72. case AF_INET6:
  73. ifindex = inet6_sdif(skb);
  74. break;
  75. case AF_INET:
  76. ifindex = inet_sdif(skb);
  77. break;
  78. }
  79. if (!ifindex)
  80. ifindex = skb->dev->ifindex;
  81. xfrmn = net_generic(xs_net(xfrm_input_state(skb)), xfrmi_net_id);
  82. for_each_xfrmi_rcu(xfrmn->xfrmi[0], xi) {
  83. if (ifindex == xi->dev->ifindex &&
  84. (xi->dev->flags & IFF_UP))
  85. return xi;
  86. }
  87. return NULL;
  88. }
  89. static void xfrmi_link(struct xfrmi_net *xfrmn, struct xfrm_if *xi)
  90. {
  91. struct xfrm_if __rcu **xip = &xfrmn->xfrmi[0];
  92. rcu_assign_pointer(xi->next , rtnl_dereference(*xip));
  93. rcu_assign_pointer(*xip, xi);
  94. }
  95. static void xfrmi_unlink(struct xfrmi_net *xfrmn, struct xfrm_if *xi)
  96. {
  97. struct xfrm_if __rcu **xip;
  98. struct xfrm_if *iter;
  99. for (xip = &xfrmn->xfrmi[0];
  100. (iter = rtnl_dereference(*xip)) != NULL;
  101. xip = &iter->next) {
  102. if (xi == iter) {
  103. rcu_assign_pointer(*xip, xi->next);
  104. break;
  105. }
  106. }
  107. }
  108. static void xfrmi_dev_free(struct net_device *dev)
  109. {
  110. struct xfrm_if *xi = netdev_priv(dev);
  111. gro_cells_destroy(&xi->gro_cells);
  112. free_percpu(dev->tstats);
  113. }
  114. static int xfrmi_create(struct net_device *dev)
  115. {
  116. struct xfrm_if *xi = netdev_priv(dev);
  117. struct net *net = dev_net(dev);
  118. struct xfrmi_net *xfrmn = net_generic(net, xfrmi_net_id);
  119. int err;
  120. dev->rtnl_link_ops = &xfrmi_link_ops;
  121. err = register_netdevice(dev);
  122. if (err < 0)
  123. goto out;
  124. dev_hold(dev);
  125. xfrmi_link(xfrmn, xi);
  126. return 0;
  127. out:
  128. return err;
  129. }
  130. static struct xfrm_if *xfrmi_locate(struct net *net, struct xfrm_if_parms *p)
  131. {
  132. struct xfrm_if __rcu **xip;
  133. struct xfrm_if *xi;
  134. struct xfrmi_net *xfrmn = net_generic(net, xfrmi_net_id);
  135. for (xip = &xfrmn->xfrmi[0];
  136. (xi = rtnl_dereference(*xip)) != NULL;
  137. xip = &xi->next)
  138. if (xi->p.if_id == p->if_id)
  139. return xi;
  140. return NULL;
  141. }
  142. static void xfrmi_dev_uninit(struct net_device *dev)
  143. {
  144. struct xfrm_if *xi = netdev_priv(dev);
  145. struct xfrmi_net *xfrmn = net_generic(xi->net, xfrmi_net_id);
  146. xfrmi_unlink(xfrmn, xi);
  147. dev_put(dev);
  148. }
  149. static void xfrmi_scrub_packet(struct sk_buff *skb, bool xnet)
  150. {
  151. skb->tstamp = 0;
  152. skb->pkt_type = PACKET_HOST;
  153. skb->skb_iif = 0;
  154. skb->ignore_df = 0;
  155. skb_dst_drop(skb);
  156. nf_reset(skb);
  157. nf_reset_trace(skb);
  158. if (!xnet)
  159. return;
  160. ipvs_reset(skb);
  161. secpath_reset(skb);
  162. skb_orphan(skb);
  163. skb->mark = 0;
  164. }
  165. static int xfrmi_rcv_cb(struct sk_buff *skb, int err)
  166. {
  167. struct pcpu_sw_netstats *tstats;
  168. struct xfrm_mode *inner_mode;
  169. struct net_device *dev;
  170. struct xfrm_state *x;
  171. struct xfrm_if *xi;
  172. bool xnet;
  173. if (err && !skb->sp)
  174. return 0;
  175. x = xfrm_input_state(skb);
  176. xi = xfrmi_lookup(xs_net(x), x);
  177. if (!xi)
  178. return 1;
  179. dev = xi->dev;
  180. skb->dev = dev;
  181. if (err) {
  182. dev->stats.rx_errors++;
  183. dev->stats.rx_dropped++;
  184. return 0;
  185. }
  186. xnet = !net_eq(xi->net, dev_net(skb->dev));
  187. if (xnet) {
  188. inner_mode = x->inner_mode;
  189. if (x->sel.family == AF_UNSPEC) {
  190. inner_mode = xfrm_ip2inner_mode(x, XFRM_MODE_SKB_CB(skb)->protocol);
  191. if (inner_mode == NULL) {
  192. XFRM_INC_STATS(dev_net(skb->dev),
  193. LINUX_MIB_XFRMINSTATEMODEERROR);
  194. return -EINVAL;
  195. }
  196. }
  197. if (!xfrm_policy_check(NULL, XFRM_POLICY_IN, skb,
  198. inner_mode->afinfo->family))
  199. return -EPERM;
  200. }
  201. xfrmi_scrub_packet(skb, xnet);
  202. tstats = this_cpu_ptr(dev->tstats);
  203. u64_stats_update_begin(&tstats->syncp);
  204. tstats->rx_packets++;
  205. tstats->rx_bytes += skb->len;
  206. u64_stats_update_end(&tstats->syncp);
  207. return 0;
  208. }
  209. static int
  210. xfrmi_xmit2(struct sk_buff *skb, struct net_device *dev, struct flowi *fl)
  211. {
  212. struct xfrm_if *xi = netdev_priv(dev);
  213. struct net_device_stats *stats = &xi->dev->stats;
  214. struct dst_entry *dst = skb_dst(skb);
  215. unsigned int length = skb->len;
  216. struct net_device *tdev;
  217. struct xfrm_state *x;
  218. int err = -1;
  219. int mtu;
  220. dst_hold(dst);
  221. dst = xfrm_lookup_with_ifid(xi->net, dst, fl, NULL, 0, xi->p.if_id);
  222. if (IS_ERR(dst)) {
  223. err = PTR_ERR(dst);
  224. dst = NULL;
  225. goto tx_err_link_failure;
  226. }
  227. x = dst->xfrm;
  228. if (!x)
  229. goto tx_err_link_failure;
  230. if (x->if_id != xi->p.if_id)
  231. goto tx_err_link_failure;
  232. tdev = dst->dev;
  233. if (tdev == dev) {
  234. stats->collisions++;
  235. net_warn_ratelimited("%s: Local routing loop detected!\n",
  236. dev->name);
  237. goto tx_err_dst_release;
  238. }
  239. mtu = dst_mtu(dst);
  240. if (!skb->ignore_df && skb->len > mtu) {
  241. skb_dst_update_pmtu_no_confirm(skb, mtu);
  242. if (skb->protocol == htons(ETH_P_IPV6)) {
  243. if (mtu < IPV6_MIN_MTU)
  244. mtu = IPV6_MIN_MTU;
  245. icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
  246. } else {
  247. icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED,
  248. htonl(mtu));
  249. }
  250. dst_release(dst);
  251. return -EMSGSIZE;
  252. }
  253. xfrmi_scrub_packet(skb, !net_eq(xi->net, dev_net(dev)));
  254. skb_dst_set(skb, dst);
  255. skb->dev = tdev;
  256. err = dst_output(xi->net, skb->sk, skb);
  257. if (net_xmit_eval(err) == 0) {
  258. struct pcpu_sw_netstats *tstats = this_cpu_ptr(dev->tstats);
  259. u64_stats_update_begin(&tstats->syncp);
  260. tstats->tx_bytes += length;
  261. tstats->tx_packets++;
  262. u64_stats_update_end(&tstats->syncp);
  263. } else {
  264. stats->tx_errors++;
  265. stats->tx_aborted_errors++;
  266. }
  267. return 0;
  268. tx_err_link_failure:
  269. stats->tx_carrier_errors++;
  270. dst_link_failure(skb);
  271. tx_err_dst_release:
  272. dst_release(dst);
  273. return err;
  274. }
  275. static netdev_tx_t xfrmi_xmit(struct sk_buff *skb, struct net_device *dev)
  276. {
  277. struct xfrm_if *xi = netdev_priv(dev);
  278. struct net_device_stats *stats = &xi->dev->stats;
  279. struct dst_entry *dst = skb_dst(skb);
  280. struct flowi fl;
  281. int ret;
  282. memset(&fl, 0, sizeof(fl));
  283. switch (skb->protocol) {
  284. case htons(ETH_P_IPV6):
  285. xfrm_decode_session(skb, &fl, AF_INET6);
  286. memset(IP6CB(skb), 0, sizeof(*IP6CB(skb)));
  287. if (!dst) {
  288. fl.u.ip6.flowi6_oif = dev->ifindex;
  289. fl.u.ip6.flowi6_flags |= FLOWI_FLAG_ANYSRC;
  290. dst = ip6_route_output(dev_net(dev), NULL, &fl.u.ip6);
  291. if (dst->error) {
  292. dst_release(dst);
  293. stats->tx_carrier_errors++;
  294. goto tx_err;
  295. }
  296. skb_dst_set(skb, dst);
  297. }
  298. break;
  299. case htons(ETH_P_IP):
  300. xfrm_decode_session(skb, &fl, AF_INET);
  301. memset(IPCB(skb), 0, sizeof(*IPCB(skb)));
  302. if (!dst) {
  303. struct rtable *rt;
  304. fl.u.ip4.flowi4_oif = dev->ifindex;
  305. fl.u.ip4.flowi4_flags |= FLOWI_FLAG_ANYSRC;
  306. rt = __ip_route_output_key(dev_net(dev), &fl.u.ip4);
  307. if (IS_ERR(rt)) {
  308. stats->tx_carrier_errors++;
  309. goto tx_err;
  310. }
  311. skb_dst_set(skb, &rt->dst);
  312. }
  313. break;
  314. default:
  315. goto tx_err;
  316. }
  317. fl.flowi_oif = xi->p.link;
  318. ret = xfrmi_xmit2(skb, dev, &fl);
  319. if (ret < 0)
  320. goto tx_err;
  321. return NETDEV_TX_OK;
  322. tx_err:
  323. stats->tx_errors++;
  324. stats->tx_dropped++;
  325. kfree_skb(skb);
  326. return NETDEV_TX_OK;
  327. }
  328. static int xfrmi4_err(struct sk_buff *skb, u32 info)
  329. {
  330. const struct iphdr *iph = (const struct iphdr *)skb->data;
  331. struct net *net = dev_net(skb->dev);
  332. int protocol = iph->protocol;
  333. struct ip_comp_hdr *ipch;
  334. struct ip_esp_hdr *esph;
  335. struct ip_auth_hdr *ah ;
  336. struct xfrm_state *x;
  337. struct xfrm_if *xi;
  338. __be32 spi;
  339. switch (protocol) {
  340. case IPPROTO_ESP:
  341. esph = (struct ip_esp_hdr *)(skb->data+(iph->ihl<<2));
  342. spi = esph->spi;
  343. break;
  344. case IPPROTO_AH:
  345. ah = (struct ip_auth_hdr *)(skb->data+(iph->ihl<<2));
  346. spi = ah->spi;
  347. break;
  348. case IPPROTO_COMP:
  349. ipch = (struct ip_comp_hdr *)(skb->data+(iph->ihl<<2));
  350. spi = htonl(ntohs(ipch->cpi));
  351. break;
  352. default:
  353. return 0;
  354. }
  355. switch (icmp_hdr(skb)->type) {
  356. case ICMP_DEST_UNREACH:
  357. if (icmp_hdr(skb)->code != ICMP_FRAG_NEEDED)
  358. return 0;
  359. case ICMP_REDIRECT:
  360. break;
  361. default:
  362. return 0;
  363. }
  364. x = xfrm_state_lookup(net, skb->mark, (const xfrm_address_t *)&iph->daddr,
  365. spi, protocol, AF_INET);
  366. if (!x)
  367. return 0;
  368. xi = xfrmi_lookup(net, x);
  369. if (!xi) {
  370. xfrm_state_put(x);
  371. return -1;
  372. }
  373. if (icmp_hdr(skb)->type == ICMP_DEST_UNREACH)
  374. ipv4_update_pmtu(skb, net, info, 0, 0, protocol, 0);
  375. else
  376. ipv4_redirect(skb, net, 0, 0, protocol, 0);
  377. xfrm_state_put(x);
  378. return 0;
  379. }
  380. static int xfrmi6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
  381. u8 type, u8 code, int offset, __be32 info)
  382. {
  383. const struct ipv6hdr *iph = (const struct ipv6hdr *)skb->data;
  384. struct net *net = dev_net(skb->dev);
  385. int protocol = iph->nexthdr;
  386. struct ip_comp_hdr *ipch;
  387. struct ip_esp_hdr *esph;
  388. struct ip_auth_hdr *ah;
  389. struct xfrm_state *x;
  390. struct xfrm_if *xi;
  391. __be32 spi;
  392. switch (protocol) {
  393. case IPPROTO_ESP:
  394. esph = (struct ip_esp_hdr *)(skb->data + offset);
  395. spi = esph->spi;
  396. break;
  397. case IPPROTO_AH:
  398. ah = (struct ip_auth_hdr *)(skb->data + offset);
  399. spi = ah->spi;
  400. break;
  401. case IPPROTO_COMP:
  402. ipch = (struct ip_comp_hdr *)(skb->data + offset);
  403. spi = htonl(ntohs(ipch->cpi));
  404. break;
  405. default:
  406. return 0;
  407. }
  408. if (type != ICMPV6_PKT_TOOBIG &&
  409. type != NDISC_REDIRECT)
  410. return 0;
  411. x = xfrm_state_lookup(net, skb->mark, (const xfrm_address_t *)&iph->daddr,
  412. spi, protocol, AF_INET6);
  413. if (!x)
  414. return 0;
  415. xi = xfrmi_lookup(net, x);
  416. if (!xi) {
  417. xfrm_state_put(x);
  418. return -1;
  419. }
  420. if (type == NDISC_REDIRECT)
  421. ip6_redirect(skb, net, skb->dev->ifindex, 0,
  422. sock_net_uid(net, NULL));
  423. else
  424. ip6_update_pmtu(skb, net, info, 0, 0, sock_net_uid(net, NULL));
  425. xfrm_state_put(x);
  426. return 0;
  427. }
  428. static int xfrmi_change(struct xfrm_if *xi, const struct xfrm_if_parms *p)
  429. {
  430. if (xi->p.link != p->link)
  431. return -EINVAL;
  432. xi->p.if_id = p->if_id;
  433. return 0;
  434. }
  435. static int xfrmi_update(struct xfrm_if *xi, struct xfrm_if_parms *p)
  436. {
  437. struct net *net = xi->net;
  438. struct xfrmi_net *xfrmn = net_generic(net, xfrmi_net_id);
  439. int err;
  440. xfrmi_unlink(xfrmn, xi);
  441. synchronize_net();
  442. err = xfrmi_change(xi, p);
  443. xfrmi_link(xfrmn, xi);
  444. netdev_state_change(xi->dev);
  445. return err;
  446. }
  447. static void xfrmi_get_stats64(struct net_device *dev,
  448. struct rtnl_link_stats64 *s)
  449. {
  450. int cpu;
  451. if (!dev->tstats)
  452. return;
  453. for_each_possible_cpu(cpu) {
  454. struct pcpu_sw_netstats *stats;
  455. struct pcpu_sw_netstats tmp;
  456. int start;
  457. stats = per_cpu_ptr(dev->tstats, cpu);
  458. do {
  459. start = u64_stats_fetch_begin_irq(&stats->syncp);
  460. tmp.rx_packets = stats->rx_packets;
  461. tmp.rx_bytes = stats->rx_bytes;
  462. tmp.tx_packets = stats->tx_packets;
  463. tmp.tx_bytes = stats->tx_bytes;
  464. } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
  465. s->rx_packets += tmp.rx_packets;
  466. s->rx_bytes += tmp.rx_bytes;
  467. s->tx_packets += tmp.tx_packets;
  468. s->tx_bytes += tmp.tx_bytes;
  469. }
  470. s->rx_dropped = dev->stats.rx_dropped;
  471. s->tx_dropped = dev->stats.tx_dropped;
  472. }
  473. static int xfrmi_get_iflink(const struct net_device *dev)
  474. {
  475. struct xfrm_if *xi = netdev_priv(dev);
  476. return xi->p.link;
  477. }
  478. static const struct net_device_ops xfrmi_netdev_ops = {
  479. .ndo_init = xfrmi_dev_init,
  480. .ndo_uninit = xfrmi_dev_uninit,
  481. .ndo_start_xmit = xfrmi_xmit,
  482. .ndo_get_stats64 = xfrmi_get_stats64,
  483. .ndo_get_iflink = xfrmi_get_iflink,
  484. };
  485. static void xfrmi_dev_setup(struct net_device *dev)
  486. {
  487. dev->netdev_ops = &xfrmi_netdev_ops;
  488. dev->type = ARPHRD_NONE;
  489. dev->mtu = ETH_DATA_LEN;
  490. dev->min_mtu = ETH_MIN_MTU;
  491. dev->max_mtu = IP_MAX_MTU;
  492. dev->flags = IFF_NOARP;
  493. dev->needs_free_netdev = true;
  494. dev->priv_destructor = xfrmi_dev_free;
  495. netif_keep_dst(dev);
  496. eth_broadcast_addr(dev->broadcast);
  497. }
  498. static int xfrmi_dev_init(struct net_device *dev)
  499. {
  500. struct xfrm_if *xi = netdev_priv(dev);
  501. struct net_device *phydev = __dev_get_by_index(xi->net, xi->p.link);
  502. int err;
  503. dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
  504. if (!dev->tstats)
  505. return -ENOMEM;
  506. err = gro_cells_init(&xi->gro_cells, dev);
  507. if (err) {
  508. free_percpu(dev->tstats);
  509. return err;
  510. }
  511. dev->features |= NETIF_F_LLTX;
  512. if (phydev) {
  513. dev->needed_headroom = phydev->needed_headroom;
  514. dev->needed_tailroom = phydev->needed_tailroom;
  515. if (is_zero_ether_addr(dev->dev_addr))
  516. eth_hw_addr_inherit(dev, phydev);
  517. if (is_zero_ether_addr(dev->broadcast))
  518. memcpy(dev->broadcast, phydev->broadcast,
  519. dev->addr_len);
  520. } else {
  521. eth_hw_addr_random(dev);
  522. eth_broadcast_addr(dev->broadcast);
  523. }
  524. return 0;
  525. }
  526. static int xfrmi_validate(struct nlattr *tb[], struct nlattr *data[],
  527. struct netlink_ext_ack *extack)
  528. {
  529. return 0;
  530. }
  531. static void xfrmi_netlink_parms(struct nlattr *data[],
  532. struct xfrm_if_parms *parms)
  533. {
  534. memset(parms, 0, sizeof(*parms));
  535. if (!data)
  536. return;
  537. if (data[IFLA_XFRM_LINK])
  538. parms->link = nla_get_u32(data[IFLA_XFRM_LINK]);
  539. if (data[IFLA_XFRM_IF_ID])
  540. parms->if_id = nla_get_u32(data[IFLA_XFRM_IF_ID]);
  541. }
  542. static int xfrmi_newlink(struct net *src_net, struct net_device *dev,
  543. struct nlattr *tb[], struct nlattr *data[],
  544. struct netlink_ext_ack *extack)
  545. {
  546. struct net *net = dev_net(dev);
  547. struct xfrm_if_parms p;
  548. struct xfrm_if *xi;
  549. int err;
  550. xfrmi_netlink_parms(data, &p);
  551. xi = xfrmi_locate(net, &p);
  552. if (xi)
  553. return -EEXIST;
  554. xi = netdev_priv(dev);
  555. xi->p = p;
  556. xi->net = net;
  557. xi->dev = dev;
  558. err = xfrmi_create(dev);
  559. return err;
  560. }
  561. static void xfrmi_dellink(struct net_device *dev, struct list_head *head)
  562. {
  563. unregister_netdevice_queue(dev, head);
  564. }
  565. static int xfrmi_changelink(struct net_device *dev, struct nlattr *tb[],
  566. struct nlattr *data[],
  567. struct netlink_ext_ack *extack)
  568. {
  569. struct xfrm_if *xi = netdev_priv(dev);
  570. struct net *net = xi->net;
  571. struct xfrm_if_parms p;
  572. xfrmi_netlink_parms(data, &p);
  573. xi = xfrmi_locate(net, &p);
  574. if (!xi) {
  575. xi = netdev_priv(dev);
  576. } else {
  577. if (xi->dev != dev)
  578. return -EEXIST;
  579. }
  580. return xfrmi_update(xi, &p);
  581. }
  582. static size_t xfrmi_get_size(const struct net_device *dev)
  583. {
  584. return
  585. /* IFLA_XFRM_LINK */
  586. nla_total_size(4) +
  587. /* IFLA_XFRM_IF_ID */
  588. nla_total_size(4) +
  589. 0;
  590. }
  591. static int xfrmi_fill_info(struct sk_buff *skb, const struct net_device *dev)
  592. {
  593. struct xfrm_if *xi = netdev_priv(dev);
  594. struct xfrm_if_parms *parm = &xi->p;
  595. if (nla_put_u32(skb, IFLA_XFRM_LINK, parm->link) ||
  596. nla_put_u32(skb, IFLA_XFRM_IF_ID, parm->if_id))
  597. goto nla_put_failure;
  598. return 0;
  599. nla_put_failure:
  600. return -EMSGSIZE;
  601. }
  602. struct net *xfrmi_get_link_net(const struct net_device *dev)
  603. {
  604. struct xfrm_if *xi = netdev_priv(dev);
  605. return xi->net;
  606. }
  607. static const struct nla_policy xfrmi_policy[IFLA_XFRM_MAX + 1] = {
  608. [IFLA_XFRM_LINK] = { .type = NLA_U32 },
  609. [IFLA_XFRM_IF_ID] = { .type = NLA_U32 },
  610. };
  611. static struct rtnl_link_ops xfrmi_link_ops __read_mostly = {
  612. .kind = "xfrm",
  613. .maxtype = IFLA_XFRM_MAX,
  614. .policy = xfrmi_policy,
  615. .priv_size = sizeof(struct xfrm_if),
  616. .setup = xfrmi_dev_setup,
  617. .validate = xfrmi_validate,
  618. .newlink = xfrmi_newlink,
  619. .dellink = xfrmi_dellink,
  620. .changelink = xfrmi_changelink,
  621. .get_size = xfrmi_get_size,
  622. .fill_info = xfrmi_fill_info,
  623. .get_link_net = xfrmi_get_link_net,
  624. };
  625. static void __net_exit xfrmi_destroy_interfaces(struct xfrmi_net *xfrmn)
  626. {
  627. struct xfrm_if *xi;
  628. LIST_HEAD(list);
  629. xi = rtnl_dereference(xfrmn->xfrmi[0]);
  630. if (!xi)
  631. return;
  632. unregister_netdevice_queue(xi->dev, &list);
  633. unregister_netdevice_many(&list);
  634. }
  635. static int __net_init xfrmi_init_net(struct net *net)
  636. {
  637. return 0;
  638. }
  639. static void __net_exit xfrmi_exit_net(struct net *net)
  640. {
  641. struct xfrmi_net *xfrmn = net_generic(net, xfrmi_net_id);
  642. rtnl_lock();
  643. xfrmi_destroy_interfaces(xfrmn);
  644. rtnl_unlock();
  645. }
  646. static struct pernet_operations xfrmi_net_ops = {
  647. .init = xfrmi_init_net,
  648. .exit = xfrmi_exit_net,
  649. .id = &xfrmi_net_id,
  650. .size = sizeof(struct xfrmi_net),
  651. };
  652. static struct xfrm6_protocol xfrmi_esp6_protocol __read_mostly = {
  653. .handler = xfrm6_rcv,
  654. .cb_handler = xfrmi_rcv_cb,
  655. .err_handler = xfrmi6_err,
  656. .priority = 10,
  657. };
  658. static struct xfrm6_protocol xfrmi_ah6_protocol __read_mostly = {
  659. .handler = xfrm6_rcv,
  660. .cb_handler = xfrmi_rcv_cb,
  661. .err_handler = xfrmi6_err,
  662. .priority = 10,
  663. };
  664. static struct xfrm6_protocol xfrmi_ipcomp6_protocol __read_mostly = {
  665. .handler = xfrm6_rcv,
  666. .cb_handler = xfrmi_rcv_cb,
  667. .err_handler = xfrmi6_err,
  668. .priority = 10,
  669. };
  670. static struct xfrm4_protocol xfrmi_esp4_protocol __read_mostly = {
  671. .handler = xfrm4_rcv,
  672. .input_handler = xfrm_input,
  673. .cb_handler = xfrmi_rcv_cb,
  674. .err_handler = xfrmi4_err,
  675. .priority = 10,
  676. };
  677. static struct xfrm4_protocol xfrmi_ah4_protocol __read_mostly = {
  678. .handler = xfrm4_rcv,
  679. .input_handler = xfrm_input,
  680. .cb_handler = xfrmi_rcv_cb,
  681. .err_handler = xfrmi4_err,
  682. .priority = 10,
  683. };
  684. static struct xfrm4_protocol xfrmi_ipcomp4_protocol __read_mostly = {
  685. .handler = xfrm4_rcv,
  686. .input_handler = xfrm_input,
  687. .cb_handler = xfrmi_rcv_cb,
  688. .err_handler = xfrmi4_err,
  689. .priority = 10,
  690. };
  691. static int __init xfrmi4_init(void)
  692. {
  693. int err;
  694. err = xfrm4_protocol_register(&xfrmi_esp4_protocol, IPPROTO_ESP);
  695. if (err < 0)
  696. goto xfrm_proto_esp_failed;
  697. err = xfrm4_protocol_register(&xfrmi_ah4_protocol, IPPROTO_AH);
  698. if (err < 0)
  699. goto xfrm_proto_ah_failed;
  700. err = xfrm4_protocol_register(&xfrmi_ipcomp4_protocol, IPPROTO_COMP);
  701. if (err < 0)
  702. goto xfrm_proto_comp_failed;
  703. return 0;
  704. xfrm_proto_comp_failed:
  705. xfrm4_protocol_deregister(&xfrmi_ah4_protocol, IPPROTO_AH);
  706. xfrm_proto_ah_failed:
  707. xfrm4_protocol_deregister(&xfrmi_esp4_protocol, IPPROTO_ESP);
  708. xfrm_proto_esp_failed:
  709. return err;
  710. }
  711. static void xfrmi4_fini(void)
  712. {
  713. xfrm4_protocol_deregister(&xfrmi_ipcomp4_protocol, IPPROTO_COMP);
  714. xfrm4_protocol_deregister(&xfrmi_ah4_protocol, IPPROTO_AH);
  715. xfrm4_protocol_deregister(&xfrmi_esp4_protocol, IPPROTO_ESP);
  716. }
  717. static int __init xfrmi6_init(void)
  718. {
  719. int err;
  720. err = xfrm6_protocol_register(&xfrmi_esp6_protocol, IPPROTO_ESP);
  721. if (err < 0)
  722. goto xfrm_proto_esp_failed;
  723. err = xfrm6_protocol_register(&xfrmi_ah6_protocol, IPPROTO_AH);
  724. if (err < 0)
  725. goto xfrm_proto_ah_failed;
  726. err = xfrm6_protocol_register(&xfrmi_ipcomp6_protocol, IPPROTO_COMP);
  727. if (err < 0)
  728. goto xfrm_proto_comp_failed;
  729. return 0;
  730. xfrm_proto_comp_failed:
  731. xfrm6_protocol_deregister(&xfrmi_ah6_protocol, IPPROTO_AH);
  732. xfrm_proto_ah_failed:
  733. xfrm6_protocol_deregister(&xfrmi_esp6_protocol, IPPROTO_ESP);
  734. xfrm_proto_esp_failed:
  735. return err;
  736. }
  737. static void xfrmi6_fini(void)
  738. {
  739. xfrm6_protocol_deregister(&xfrmi_ipcomp6_protocol, IPPROTO_COMP);
  740. xfrm6_protocol_deregister(&xfrmi_ah6_protocol, IPPROTO_AH);
  741. xfrm6_protocol_deregister(&xfrmi_esp6_protocol, IPPROTO_ESP);
  742. }
  743. static const struct xfrm_if_cb xfrm_if_cb = {
  744. .decode_session = xfrmi_decode_session,
  745. };
  746. static int __init xfrmi_init(void)
  747. {
  748. const char *msg;
  749. int err;
  750. pr_info("IPsec XFRM device driver\n");
  751. msg = "tunnel device";
  752. err = register_pernet_device(&xfrmi_net_ops);
  753. if (err < 0)
  754. goto pernet_dev_failed;
  755. msg = "xfrm4 protocols";
  756. err = xfrmi4_init();
  757. if (err < 0)
  758. goto xfrmi4_failed;
  759. msg = "xfrm6 protocols";
  760. err = xfrmi6_init();
  761. if (err < 0)
  762. goto xfrmi6_failed;
  763. msg = "netlink interface";
  764. err = rtnl_link_register(&xfrmi_link_ops);
  765. if (err < 0)
  766. goto rtnl_link_failed;
  767. xfrm_if_register_cb(&xfrm_if_cb);
  768. return err;
  769. rtnl_link_failed:
  770. xfrmi6_fini();
  771. xfrmi6_failed:
  772. xfrmi4_fini();
  773. xfrmi4_failed:
  774. unregister_pernet_device(&xfrmi_net_ops);
  775. pernet_dev_failed:
  776. pr_err("xfrmi init: failed to register %s\n", msg);
  777. return err;
  778. }
  779. static void __exit xfrmi_fini(void)
  780. {
  781. xfrm_if_unregister_cb();
  782. rtnl_link_unregister(&xfrmi_link_ops);
  783. xfrmi4_fini();
  784. xfrmi6_fini();
  785. unregister_pernet_device(&xfrmi_net_ops);
  786. }
  787. module_init(xfrmi_init);
  788. module_exit(xfrmi_fini);
  789. MODULE_LICENSE("GPL");
  790. MODULE_ALIAS_RTNL_LINK("xfrm");
  791. MODULE_ALIAS_NETDEV("xfrm0");
  792. MODULE_AUTHOR("Steffen Klassert");
  793. MODULE_DESCRIPTION("XFRM virtual interface");