/net/ipv6/sit.c

http://github.com/mirrors/linux · C · 1970 lines · 1576 code · 302 blank · 92 comment · 320 complexity · a2a315a1595bb9d50fe1077a65cfc092 MD5 · raw file

  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * IPv6 over IPv4 tunnel device - Simple Internet Transition (SIT)
  4. * Linux INET6 implementation
  5. *
  6. * Authors:
  7. * Pedro Roque <roque@di.fc.ul.pt>
  8. * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
  9. *
  10. * Changes:
  11. * Roger Venning <r.venning@telstra.com>: 6to4 support
  12. * Nate Thompson <nate@thebog.net>: 6to4 support
  13. * Fred Templin <fred.l.templin@boeing.com>: isatap support
  14. */
  15. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  16. #include <linux/module.h>
  17. #include <linux/capability.h>
  18. #include <linux/errno.h>
  19. #include <linux/types.h>
  20. #include <linux/socket.h>
  21. #include <linux/sockios.h>
  22. #include <linux/net.h>
  23. #include <linux/in6.h>
  24. #include <linux/netdevice.h>
  25. #include <linux/if_arp.h>
  26. #include <linux/icmp.h>
  27. #include <linux/slab.h>
  28. #include <linux/uaccess.h>
  29. #include <linux/init.h>
  30. #include <linux/netfilter_ipv4.h>
  31. #include <linux/if_ether.h>
  32. #include <net/sock.h>
  33. #include <net/snmp.h>
  34. #include <net/ipv6.h>
  35. #include <net/protocol.h>
  36. #include <net/transp_v6.h>
  37. #include <net/ip6_fib.h>
  38. #include <net/ip6_route.h>
  39. #include <net/ndisc.h>
  40. #include <net/addrconf.h>
  41. #include <net/ip.h>
  42. #include <net/udp.h>
  43. #include <net/icmp.h>
  44. #include <net/ip_tunnels.h>
  45. #include <net/inet_ecn.h>
  46. #include <net/xfrm.h>
  47. #include <net/dsfield.h>
  48. #include <net/net_namespace.h>
  49. #include <net/netns/generic.h>
  50. /*
  51. This version of net/ipv6/sit.c is cloned of net/ipv4/ip_gre.c
  52. For comments look at net/ipv4/ip_gre.c --ANK
  53. */
  54. #define IP6_SIT_HASH_SIZE 16
  55. #define HASH(addr) (((__force u32)addr^((__force u32)addr>>4))&0xF)
  56. static bool log_ecn_error = true;
  57. module_param(log_ecn_error, bool, 0644);
  58. MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN");
  59. static int ipip6_tunnel_init(struct net_device *dev);
  60. static void ipip6_tunnel_setup(struct net_device *dev);
  61. static void ipip6_dev_free(struct net_device *dev);
  62. static bool check_6rd(struct ip_tunnel *tunnel, const struct in6_addr *v6dst,
  63. __be32 *v4dst);
  64. static struct rtnl_link_ops sit_link_ops __read_mostly;
  65. static unsigned int sit_net_id __read_mostly;
  66. struct sit_net {
  67. struct ip_tunnel __rcu *tunnels_r_l[IP6_SIT_HASH_SIZE];
  68. struct ip_tunnel __rcu *tunnels_r[IP6_SIT_HASH_SIZE];
  69. struct ip_tunnel __rcu *tunnels_l[IP6_SIT_HASH_SIZE];
  70. struct ip_tunnel __rcu *tunnels_wc[1];
  71. struct ip_tunnel __rcu **tunnels[4];
  72. struct net_device *fb_tunnel_dev;
  73. };
  74. /*
  75. * Must be invoked with rcu_read_lock
  76. */
  77. static struct ip_tunnel *ipip6_tunnel_lookup(struct net *net,
  78. struct net_device *dev,
  79. __be32 remote, __be32 local,
  80. int sifindex)
  81. {
  82. unsigned int h0 = HASH(remote);
  83. unsigned int h1 = HASH(local);
  84. struct ip_tunnel *t;
  85. struct sit_net *sitn = net_generic(net, sit_net_id);
  86. int ifindex = dev ? dev->ifindex : 0;
  87. for_each_ip_tunnel_rcu(t, sitn->tunnels_r_l[h0 ^ h1]) {
  88. if (local == t->parms.iph.saddr &&
  89. remote == t->parms.iph.daddr &&
  90. (!dev || !t->parms.link || ifindex == t->parms.link ||
  91. sifindex == t->parms.link) &&
  92. (t->dev->flags & IFF_UP))
  93. return t;
  94. }
  95. for_each_ip_tunnel_rcu(t, sitn->tunnels_r[h0]) {
  96. if (remote == t->parms.iph.daddr &&
  97. (!dev || !t->parms.link || ifindex == t->parms.link ||
  98. sifindex == t->parms.link) &&
  99. (t->dev->flags & IFF_UP))
  100. return t;
  101. }
  102. for_each_ip_tunnel_rcu(t, sitn->tunnels_l[h1]) {
  103. if (local == t->parms.iph.saddr &&
  104. (!dev || !t->parms.link || ifindex == t->parms.link ||
  105. sifindex == t->parms.link) &&
  106. (t->dev->flags & IFF_UP))
  107. return t;
  108. }
  109. t = rcu_dereference(sitn->tunnels_wc[0]);
  110. if (t && (t->dev->flags & IFF_UP))
  111. return t;
  112. return NULL;
  113. }
  114. static struct ip_tunnel __rcu **__ipip6_bucket(struct sit_net *sitn,
  115. struct ip_tunnel_parm *parms)
  116. {
  117. __be32 remote = parms->iph.daddr;
  118. __be32 local = parms->iph.saddr;
  119. unsigned int h = 0;
  120. int prio = 0;
  121. if (remote) {
  122. prio |= 2;
  123. h ^= HASH(remote);
  124. }
  125. if (local) {
  126. prio |= 1;
  127. h ^= HASH(local);
  128. }
  129. return &sitn->tunnels[prio][h];
  130. }
  131. static inline struct ip_tunnel __rcu **ipip6_bucket(struct sit_net *sitn,
  132. struct ip_tunnel *t)
  133. {
  134. return __ipip6_bucket(sitn, &t->parms);
  135. }
  136. static void ipip6_tunnel_unlink(struct sit_net *sitn, struct ip_tunnel *t)
  137. {
  138. struct ip_tunnel __rcu **tp;
  139. struct ip_tunnel *iter;
  140. for (tp = ipip6_bucket(sitn, t);
  141. (iter = rtnl_dereference(*tp)) != NULL;
  142. tp = &iter->next) {
  143. if (t == iter) {
  144. rcu_assign_pointer(*tp, t->next);
  145. break;
  146. }
  147. }
  148. }
  149. static void ipip6_tunnel_link(struct sit_net *sitn, struct ip_tunnel *t)
  150. {
  151. struct ip_tunnel __rcu **tp = ipip6_bucket(sitn, t);
  152. rcu_assign_pointer(t->next, rtnl_dereference(*tp));
  153. rcu_assign_pointer(*tp, t);
  154. }
  155. static void ipip6_tunnel_clone_6rd(struct net_device *dev, struct sit_net *sitn)
  156. {
  157. #ifdef CONFIG_IPV6_SIT_6RD
  158. struct ip_tunnel *t = netdev_priv(dev);
  159. if (dev == sitn->fb_tunnel_dev || !sitn->fb_tunnel_dev) {
  160. ipv6_addr_set(&t->ip6rd.prefix, htonl(0x20020000), 0, 0, 0);
  161. t->ip6rd.relay_prefix = 0;
  162. t->ip6rd.prefixlen = 16;
  163. t->ip6rd.relay_prefixlen = 0;
  164. } else {
  165. struct ip_tunnel *t0 = netdev_priv(sitn->fb_tunnel_dev);
  166. memcpy(&t->ip6rd, &t0->ip6rd, sizeof(t->ip6rd));
  167. }
  168. #endif
  169. }
  170. static int ipip6_tunnel_create(struct net_device *dev)
  171. {
  172. struct ip_tunnel *t = netdev_priv(dev);
  173. struct net *net = dev_net(dev);
  174. struct sit_net *sitn = net_generic(net, sit_net_id);
  175. int err;
  176. memcpy(dev->dev_addr, &t->parms.iph.saddr, 4);
  177. memcpy(dev->broadcast, &t->parms.iph.daddr, 4);
  178. if ((__force u16)t->parms.i_flags & SIT_ISATAP)
  179. dev->priv_flags |= IFF_ISATAP;
  180. dev->rtnl_link_ops = &sit_link_ops;
  181. err = register_netdevice(dev);
  182. if (err < 0)
  183. goto out;
  184. ipip6_tunnel_clone_6rd(dev, sitn);
  185. dev_hold(dev);
  186. ipip6_tunnel_link(sitn, t);
  187. return 0;
  188. out:
  189. return err;
  190. }
  191. static struct ip_tunnel *ipip6_tunnel_locate(struct net *net,
  192. struct ip_tunnel_parm *parms, int create)
  193. {
  194. __be32 remote = parms->iph.daddr;
  195. __be32 local = parms->iph.saddr;
  196. struct ip_tunnel *t, *nt;
  197. struct ip_tunnel __rcu **tp;
  198. struct net_device *dev;
  199. char name[IFNAMSIZ];
  200. struct sit_net *sitn = net_generic(net, sit_net_id);
  201. for (tp = __ipip6_bucket(sitn, parms);
  202. (t = rtnl_dereference(*tp)) != NULL;
  203. tp = &t->next) {
  204. if (local == t->parms.iph.saddr &&
  205. remote == t->parms.iph.daddr &&
  206. parms->link == t->parms.link) {
  207. if (create)
  208. return NULL;
  209. else
  210. return t;
  211. }
  212. }
  213. if (!create)
  214. goto failed;
  215. if (parms->name[0]) {
  216. if (!dev_valid_name(parms->name))
  217. goto failed;
  218. strlcpy(name, parms->name, IFNAMSIZ);
  219. } else {
  220. strcpy(name, "sit%d");
  221. }
  222. dev = alloc_netdev(sizeof(*t), name, NET_NAME_UNKNOWN,
  223. ipip6_tunnel_setup);
  224. if (!dev)
  225. return NULL;
  226. dev_net_set(dev, net);
  227. nt = netdev_priv(dev);
  228. nt->parms = *parms;
  229. if (ipip6_tunnel_create(dev) < 0)
  230. goto failed_free;
  231. return nt;
  232. failed_free:
  233. free_netdev(dev);
  234. failed:
  235. return NULL;
  236. }
  237. #define for_each_prl_rcu(start) \
  238. for (prl = rcu_dereference(start); \
  239. prl; \
  240. prl = rcu_dereference(prl->next))
  241. static struct ip_tunnel_prl_entry *
  242. __ipip6_tunnel_locate_prl(struct ip_tunnel *t, __be32 addr)
  243. {
  244. struct ip_tunnel_prl_entry *prl;
  245. for_each_prl_rcu(t->prl)
  246. if (prl->addr == addr)
  247. break;
  248. return prl;
  249. }
  250. static int ipip6_tunnel_get_prl(struct ip_tunnel *t,
  251. struct ip_tunnel_prl __user *a)
  252. {
  253. struct ip_tunnel_prl kprl, *kp;
  254. struct ip_tunnel_prl_entry *prl;
  255. unsigned int cmax, c = 0, ca, len;
  256. int ret = 0;
  257. if (copy_from_user(&kprl, a, sizeof(kprl)))
  258. return -EFAULT;
  259. cmax = kprl.datalen / sizeof(kprl);
  260. if (cmax > 1 && kprl.addr != htonl(INADDR_ANY))
  261. cmax = 1;
  262. /* For simple GET or for root users,
  263. * we try harder to allocate.
  264. */
  265. kp = (cmax <= 1 || capable(CAP_NET_ADMIN)) ?
  266. kcalloc(cmax, sizeof(*kp), GFP_KERNEL | __GFP_NOWARN) :
  267. NULL;
  268. rcu_read_lock();
  269. ca = t->prl_count < cmax ? t->prl_count : cmax;
  270. if (!kp) {
  271. /* We don't try hard to allocate much memory for
  272. * non-root users.
  273. * For root users, retry allocating enough memory for
  274. * the answer.
  275. */
  276. kp = kcalloc(ca, sizeof(*kp), GFP_ATOMIC);
  277. if (!kp) {
  278. ret = -ENOMEM;
  279. goto out;
  280. }
  281. }
  282. c = 0;
  283. for_each_prl_rcu(t->prl) {
  284. if (c >= cmax)
  285. break;
  286. if (kprl.addr != htonl(INADDR_ANY) && prl->addr != kprl.addr)
  287. continue;
  288. kp[c].addr = prl->addr;
  289. kp[c].flags = prl->flags;
  290. c++;
  291. if (kprl.addr != htonl(INADDR_ANY))
  292. break;
  293. }
  294. out:
  295. rcu_read_unlock();
  296. len = sizeof(*kp) * c;
  297. ret = 0;
  298. if ((len && copy_to_user(a + 1, kp, len)) || put_user(len, &a->datalen))
  299. ret = -EFAULT;
  300. kfree(kp);
  301. return ret;
  302. }
  303. static int
  304. ipip6_tunnel_add_prl(struct ip_tunnel *t, struct ip_tunnel_prl *a, int chg)
  305. {
  306. struct ip_tunnel_prl_entry *p;
  307. int err = 0;
  308. if (a->addr == htonl(INADDR_ANY))
  309. return -EINVAL;
  310. ASSERT_RTNL();
  311. for (p = rtnl_dereference(t->prl); p; p = rtnl_dereference(p->next)) {
  312. if (p->addr == a->addr) {
  313. if (chg) {
  314. p->flags = a->flags;
  315. goto out;
  316. }
  317. err = -EEXIST;
  318. goto out;
  319. }
  320. }
  321. if (chg) {
  322. err = -ENXIO;
  323. goto out;
  324. }
  325. p = kzalloc(sizeof(struct ip_tunnel_prl_entry), GFP_KERNEL);
  326. if (!p) {
  327. err = -ENOBUFS;
  328. goto out;
  329. }
  330. p->next = t->prl;
  331. p->addr = a->addr;
  332. p->flags = a->flags;
  333. t->prl_count++;
  334. rcu_assign_pointer(t->prl, p);
  335. out:
  336. return err;
  337. }
  338. static void prl_list_destroy_rcu(struct rcu_head *head)
  339. {
  340. struct ip_tunnel_prl_entry *p, *n;
  341. p = container_of(head, struct ip_tunnel_prl_entry, rcu_head);
  342. do {
  343. n = rcu_dereference_protected(p->next, 1);
  344. kfree(p);
  345. p = n;
  346. } while (p);
  347. }
  348. static int
  349. ipip6_tunnel_del_prl(struct ip_tunnel *t, struct ip_tunnel_prl *a)
  350. {
  351. struct ip_tunnel_prl_entry *x;
  352. struct ip_tunnel_prl_entry __rcu **p;
  353. int err = 0;
  354. ASSERT_RTNL();
  355. if (a && a->addr != htonl(INADDR_ANY)) {
  356. for (p = &t->prl;
  357. (x = rtnl_dereference(*p)) != NULL;
  358. p = &x->next) {
  359. if (x->addr == a->addr) {
  360. *p = x->next;
  361. kfree_rcu(x, rcu_head);
  362. t->prl_count--;
  363. goto out;
  364. }
  365. }
  366. err = -ENXIO;
  367. } else {
  368. x = rtnl_dereference(t->prl);
  369. if (x) {
  370. t->prl_count = 0;
  371. call_rcu(&x->rcu_head, prl_list_destroy_rcu);
  372. t->prl = NULL;
  373. }
  374. }
  375. out:
  376. return err;
  377. }
  378. static int
  379. isatap_chksrc(struct sk_buff *skb, const struct iphdr *iph, struct ip_tunnel *t)
  380. {
  381. struct ip_tunnel_prl_entry *p;
  382. int ok = 1;
  383. rcu_read_lock();
  384. p = __ipip6_tunnel_locate_prl(t, iph->saddr);
  385. if (p) {
  386. if (p->flags & PRL_DEFAULT)
  387. skb->ndisc_nodetype = NDISC_NODETYPE_DEFAULT;
  388. else
  389. skb->ndisc_nodetype = NDISC_NODETYPE_NODEFAULT;
  390. } else {
  391. const struct in6_addr *addr6 = &ipv6_hdr(skb)->saddr;
  392. if (ipv6_addr_is_isatap(addr6) &&
  393. (addr6->s6_addr32[3] == iph->saddr) &&
  394. ipv6_chk_prefix(addr6, t->dev))
  395. skb->ndisc_nodetype = NDISC_NODETYPE_HOST;
  396. else
  397. ok = 0;
  398. }
  399. rcu_read_unlock();
  400. return ok;
  401. }
  402. static void ipip6_tunnel_uninit(struct net_device *dev)
  403. {
  404. struct ip_tunnel *tunnel = netdev_priv(dev);
  405. struct sit_net *sitn = net_generic(tunnel->net, sit_net_id);
  406. if (dev == sitn->fb_tunnel_dev) {
  407. RCU_INIT_POINTER(sitn->tunnels_wc[0], NULL);
  408. } else {
  409. ipip6_tunnel_unlink(sitn, tunnel);
  410. ipip6_tunnel_del_prl(tunnel, NULL);
  411. }
  412. dst_cache_reset(&tunnel->dst_cache);
  413. dev_put(dev);
  414. }
  415. static int ipip6_err(struct sk_buff *skb, u32 info)
  416. {
  417. const struct iphdr *iph = (const struct iphdr *)skb->data;
  418. const int type = icmp_hdr(skb)->type;
  419. const int code = icmp_hdr(skb)->code;
  420. unsigned int data_len = 0;
  421. struct ip_tunnel *t;
  422. int sifindex;
  423. int err;
  424. switch (type) {
  425. default:
  426. case ICMP_PARAMETERPROB:
  427. return 0;
  428. case ICMP_DEST_UNREACH:
  429. switch (code) {
  430. case ICMP_SR_FAILED:
  431. /* Impossible event. */
  432. return 0;
  433. default:
  434. /* All others are translated to HOST_UNREACH.
  435. rfc2003 contains "deep thoughts" about NET_UNREACH,
  436. I believe they are just ether pollution. --ANK
  437. */
  438. break;
  439. }
  440. break;
  441. case ICMP_TIME_EXCEEDED:
  442. if (code != ICMP_EXC_TTL)
  443. return 0;
  444. data_len = icmp_hdr(skb)->un.reserved[1] * 4; /* RFC 4884 4.1 */
  445. break;
  446. case ICMP_REDIRECT:
  447. break;
  448. }
  449. err = -ENOENT;
  450. sifindex = netif_is_l3_master(skb->dev) ? IPCB(skb)->iif : 0;
  451. t = ipip6_tunnel_lookup(dev_net(skb->dev), skb->dev,
  452. iph->daddr, iph->saddr, sifindex);
  453. if (!t)
  454. goto out;
  455. if (type == ICMP_DEST_UNREACH && code == ICMP_FRAG_NEEDED) {
  456. ipv4_update_pmtu(skb, dev_net(skb->dev), info,
  457. t->parms.link, iph->protocol);
  458. err = 0;
  459. goto out;
  460. }
  461. if (type == ICMP_REDIRECT) {
  462. ipv4_redirect(skb, dev_net(skb->dev), t->parms.link,
  463. iph->protocol);
  464. err = 0;
  465. goto out;
  466. }
  467. err = 0;
  468. if (__in6_dev_get(skb->dev) &&
  469. !ip6_err_gen_icmpv6_unreach(skb, iph->ihl * 4, type, data_len))
  470. goto out;
  471. if (t->parms.iph.daddr == 0)
  472. goto out;
  473. if (t->parms.iph.ttl == 0 && type == ICMP_TIME_EXCEEDED)
  474. goto out;
  475. if (time_before(jiffies, t->err_time + IPTUNNEL_ERR_TIMEO))
  476. t->err_count++;
  477. else
  478. t->err_count = 1;
  479. t->err_time = jiffies;
  480. out:
  481. return err;
  482. }
  483. static inline bool is_spoofed_6rd(struct ip_tunnel *tunnel, const __be32 v4addr,
  484. const struct in6_addr *v6addr)
  485. {
  486. __be32 v4embed = 0;
  487. if (check_6rd(tunnel, v6addr, &v4embed) && v4addr != v4embed)
  488. return true;
  489. return false;
  490. }
  491. /* Checks if an address matches an address on the tunnel interface.
  492. * Used to detect the NAT of proto 41 packets and let them pass spoofing test.
  493. * Long story:
  494. * This function is called after we considered the packet as spoofed
  495. * in is_spoofed_6rd.
  496. * We may have a router that is doing NAT for proto 41 packets
  497. * for an internal station. Destination a.a.a.a/PREFIX:bbbb:bbbb
  498. * will be translated to n.n.n.n/PREFIX:bbbb:bbbb. And is_spoofed_6rd
  499. * function will return true, dropping the packet.
  500. * But, we can still check if is spoofed against the IP
  501. * addresses associated with the interface.
  502. */
  503. static bool only_dnatted(const struct ip_tunnel *tunnel,
  504. const struct in6_addr *v6dst)
  505. {
  506. int prefix_len;
  507. #ifdef CONFIG_IPV6_SIT_6RD
  508. prefix_len = tunnel->ip6rd.prefixlen + 32
  509. - tunnel->ip6rd.relay_prefixlen;
  510. #else
  511. prefix_len = 48;
  512. #endif
  513. return ipv6_chk_custom_prefix(v6dst, prefix_len, tunnel->dev);
  514. }
  515. /* Returns true if a packet is spoofed */
  516. static bool packet_is_spoofed(struct sk_buff *skb,
  517. const struct iphdr *iph,
  518. struct ip_tunnel *tunnel)
  519. {
  520. const struct ipv6hdr *ipv6h;
  521. if (tunnel->dev->priv_flags & IFF_ISATAP) {
  522. if (!isatap_chksrc(skb, iph, tunnel))
  523. return true;
  524. return false;
  525. }
  526. if (tunnel->dev->flags & IFF_POINTOPOINT)
  527. return false;
  528. ipv6h = ipv6_hdr(skb);
  529. if (unlikely(is_spoofed_6rd(tunnel, iph->saddr, &ipv6h->saddr))) {
  530. net_warn_ratelimited("Src spoofed %pI4/%pI6c -> %pI4/%pI6c\n",
  531. &iph->saddr, &ipv6h->saddr,
  532. &iph->daddr, &ipv6h->daddr);
  533. return true;
  534. }
  535. if (likely(!is_spoofed_6rd(tunnel, iph->daddr, &ipv6h->daddr)))
  536. return false;
  537. if (only_dnatted(tunnel, &ipv6h->daddr))
  538. return false;
  539. net_warn_ratelimited("Dst spoofed %pI4/%pI6c -> %pI4/%pI6c\n",
  540. &iph->saddr, &ipv6h->saddr,
  541. &iph->daddr, &ipv6h->daddr);
  542. return true;
  543. }
  544. static int ipip6_rcv(struct sk_buff *skb)
  545. {
  546. const struct iphdr *iph = ip_hdr(skb);
  547. struct ip_tunnel *tunnel;
  548. int sifindex;
  549. int err;
  550. sifindex = netif_is_l3_master(skb->dev) ? IPCB(skb)->iif : 0;
  551. tunnel = ipip6_tunnel_lookup(dev_net(skb->dev), skb->dev,
  552. iph->saddr, iph->daddr, sifindex);
  553. if (tunnel) {
  554. struct pcpu_sw_netstats *tstats;
  555. if (tunnel->parms.iph.protocol != IPPROTO_IPV6 &&
  556. tunnel->parms.iph.protocol != 0)
  557. goto out;
  558. skb->mac_header = skb->network_header;
  559. skb_reset_network_header(skb);
  560. IPCB(skb)->flags = 0;
  561. skb->dev = tunnel->dev;
  562. if (packet_is_spoofed(skb, iph, tunnel)) {
  563. tunnel->dev->stats.rx_errors++;
  564. goto out;
  565. }
  566. if (iptunnel_pull_header(skb, 0, htons(ETH_P_IPV6),
  567. !net_eq(tunnel->net, dev_net(tunnel->dev))))
  568. goto out;
  569. /* skb can be uncloned in iptunnel_pull_header, so
  570. * old iph is no longer valid
  571. */
  572. iph = (const struct iphdr *)skb_mac_header(skb);
  573. err = IP_ECN_decapsulate(iph, skb);
  574. if (unlikely(err)) {
  575. if (log_ecn_error)
  576. net_info_ratelimited("non-ECT from %pI4 with TOS=%#x\n",
  577. &iph->saddr, iph->tos);
  578. if (err > 1) {
  579. ++tunnel->dev->stats.rx_frame_errors;
  580. ++tunnel->dev->stats.rx_errors;
  581. goto out;
  582. }
  583. }
  584. tstats = this_cpu_ptr(tunnel->dev->tstats);
  585. u64_stats_update_begin(&tstats->syncp);
  586. tstats->rx_packets++;
  587. tstats->rx_bytes += skb->len;
  588. u64_stats_update_end(&tstats->syncp);
  589. netif_rx(skb);
  590. return 0;
  591. }
  592. /* no tunnel matched, let upstream know, ipsec may handle it */
  593. return 1;
  594. out:
  595. kfree_skb(skb);
  596. return 0;
  597. }
  598. static const struct tnl_ptk_info ipip_tpi = {
  599. /* no tunnel info required for ipip. */
  600. .proto = htons(ETH_P_IP),
  601. };
  602. #if IS_ENABLED(CONFIG_MPLS)
  603. static const struct tnl_ptk_info mplsip_tpi = {
  604. /* no tunnel info required for mplsip. */
  605. .proto = htons(ETH_P_MPLS_UC),
  606. };
  607. #endif
  608. static int sit_tunnel_rcv(struct sk_buff *skb, u8 ipproto)
  609. {
  610. const struct iphdr *iph;
  611. struct ip_tunnel *tunnel;
  612. int sifindex;
  613. sifindex = netif_is_l3_master(skb->dev) ? IPCB(skb)->iif : 0;
  614. iph = ip_hdr(skb);
  615. tunnel = ipip6_tunnel_lookup(dev_net(skb->dev), skb->dev,
  616. iph->saddr, iph->daddr, sifindex);
  617. if (tunnel) {
  618. const struct tnl_ptk_info *tpi;
  619. if (tunnel->parms.iph.protocol != ipproto &&
  620. tunnel->parms.iph.protocol != 0)
  621. goto drop;
  622. if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb))
  623. goto drop;
  624. #if IS_ENABLED(CONFIG_MPLS)
  625. if (ipproto == IPPROTO_MPLS)
  626. tpi = &mplsip_tpi;
  627. else
  628. #endif
  629. tpi = &ipip_tpi;
  630. if (iptunnel_pull_header(skb, 0, tpi->proto, false))
  631. goto drop;
  632. return ip_tunnel_rcv(tunnel, skb, tpi, NULL, log_ecn_error);
  633. }
  634. return 1;
  635. drop:
  636. kfree_skb(skb);
  637. return 0;
  638. }
  639. static int ipip_rcv(struct sk_buff *skb)
  640. {
  641. return sit_tunnel_rcv(skb, IPPROTO_IPIP);
  642. }
  643. #if IS_ENABLED(CONFIG_MPLS)
  644. static int mplsip_rcv(struct sk_buff *skb)
  645. {
  646. return sit_tunnel_rcv(skb, IPPROTO_MPLS);
  647. }
  648. #endif
  649. /*
  650. * If the IPv6 address comes from 6rd / 6to4 (RFC 3056) addr space this function
  651. * stores the embedded IPv4 address in v4dst and returns true.
  652. */
  653. static bool check_6rd(struct ip_tunnel *tunnel, const struct in6_addr *v6dst,
  654. __be32 *v4dst)
  655. {
  656. #ifdef CONFIG_IPV6_SIT_6RD
  657. if (ipv6_prefix_equal(v6dst, &tunnel->ip6rd.prefix,
  658. tunnel->ip6rd.prefixlen)) {
  659. unsigned int pbw0, pbi0;
  660. int pbi1;
  661. u32 d;
  662. pbw0 = tunnel->ip6rd.prefixlen >> 5;
  663. pbi0 = tunnel->ip6rd.prefixlen & 0x1f;
  664. d = tunnel->ip6rd.relay_prefixlen < 32 ?
  665. (ntohl(v6dst->s6_addr32[pbw0]) << pbi0) >>
  666. tunnel->ip6rd.relay_prefixlen : 0;
  667. pbi1 = pbi0 - tunnel->ip6rd.relay_prefixlen;
  668. if (pbi1 > 0)
  669. d |= ntohl(v6dst->s6_addr32[pbw0 + 1]) >>
  670. (32 - pbi1);
  671. *v4dst = tunnel->ip6rd.relay_prefix | htonl(d);
  672. return true;
  673. }
  674. #else
  675. if (v6dst->s6_addr16[0] == htons(0x2002)) {
  676. /* 6to4 v6 addr has 16 bits prefix, 32 v4addr, 16 SLA, ... */
  677. memcpy(v4dst, &v6dst->s6_addr16[1], 4);
  678. return true;
  679. }
  680. #endif
  681. return false;
  682. }
  683. static inline __be32 try_6rd(struct ip_tunnel *tunnel,
  684. const struct in6_addr *v6dst)
  685. {
  686. __be32 dst = 0;
  687. check_6rd(tunnel, v6dst, &dst);
  688. return dst;
  689. }
  690. /*
  691. * This function assumes it is being called from dev_queue_xmit()
  692. * and that skb is filled properly by that function.
  693. */
  694. static netdev_tx_t ipip6_tunnel_xmit(struct sk_buff *skb,
  695. struct net_device *dev)
  696. {
  697. struct ip_tunnel *tunnel = netdev_priv(dev);
  698. const struct iphdr *tiph = &tunnel->parms.iph;
  699. const struct ipv6hdr *iph6 = ipv6_hdr(skb);
  700. u8 tos = tunnel->parms.iph.tos;
  701. __be16 df = tiph->frag_off;
  702. struct rtable *rt; /* Route to the other host */
  703. struct net_device *tdev; /* Device to other host */
  704. unsigned int max_headroom; /* The extra header space needed */
  705. __be32 dst = tiph->daddr;
  706. struct flowi4 fl4;
  707. int mtu;
  708. const struct in6_addr *addr6;
  709. int addr_type;
  710. u8 ttl;
  711. u8 protocol = IPPROTO_IPV6;
  712. int t_hlen = tunnel->hlen + sizeof(struct iphdr);
  713. if (tos == 1)
  714. tos = ipv6_get_dsfield(iph6);
  715. /* ISATAP (RFC4214) - must come before 6to4 */
  716. if (dev->priv_flags & IFF_ISATAP) {
  717. struct neighbour *neigh = NULL;
  718. bool do_tx_error = false;
  719. if (skb_dst(skb))
  720. neigh = dst_neigh_lookup(skb_dst(skb), &iph6->daddr);
  721. if (!neigh) {
  722. net_dbg_ratelimited("nexthop == NULL\n");
  723. goto tx_error;
  724. }
  725. addr6 = (const struct in6_addr *)&neigh->primary_key;
  726. addr_type = ipv6_addr_type(addr6);
  727. if ((addr_type & IPV6_ADDR_UNICAST) &&
  728. ipv6_addr_is_isatap(addr6))
  729. dst = addr6->s6_addr32[3];
  730. else
  731. do_tx_error = true;
  732. neigh_release(neigh);
  733. if (do_tx_error)
  734. goto tx_error;
  735. }
  736. if (!dst)
  737. dst = try_6rd(tunnel, &iph6->daddr);
  738. if (!dst) {
  739. struct neighbour *neigh = NULL;
  740. bool do_tx_error = false;
  741. if (skb_dst(skb))
  742. neigh = dst_neigh_lookup(skb_dst(skb), &iph6->daddr);
  743. if (!neigh) {
  744. net_dbg_ratelimited("nexthop == NULL\n");
  745. goto tx_error;
  746. }
  747. addr6 = (const struct in6_addr *)&neigh->primary_key;
  748. addr_type = ipv6_addr_type(addr6);
  749. if (addr_type == IPV6_ADDR_ANY) {
  750. addr6 = &ipv6_hdr(skb)->daddr;
  751. addr_type = ipv6_addr_type(addr6);
  752. }
  753. if ((addr_type & IPV6_ADDR_COMPATv4) != 0)
  754. dst = addr6->s6_addr32[3];
  755. else
  756. do_tx_error = true;
  757. neigh_release(neigh);
  758. if (do_tx_error)
  759. goto tx_error;
  760. }
  761. flowi4_init_output(&fl4, tunnel->parms.link, tunnel->fwmark,
  762. RT_TOS(tos), RT_SCOPE_UNIVERSE, IPPROTO_IPV6,
  763. 0, dst, tiph->saddr, 0, 0,
  764. sock_net_uid(tunnel->net, NULL));
  765. rt = dst_cache_get_ip4(&tunnel->dst_cache, &fl4.saddr);
  766. if (!rt) {
  767. rt = ip_route_output_flow(tunnel->net, &fl4, NULL);
  768. if (IS_ERR(rt)) {
  769. dev->stats.tx_carrier_errors++;
  770. goto tx_error_icmp;
  771. }
  772. dst_cache_set_ip4(&tunnel->dst_cache, &rt->dst, fl4.saddr);
  773. }
  774. if (rt->rt_type != RTN_UNICAST) {
  775. ip_rt_put(rt);
  776. dev->stats.tx_carrier_errors++;
  777. goto tx_error_icmp;
  778. }
  779. tdev = rt->dst.dev;
  780. if (tdev == dev) {
  781. ip_rt_put(rt);
  782. dev->stats.collisions++;
  783. goto tx_error;
  784. }
  785. if (iptunnel_handle_offloads(skb, SKB_GSO_IPXIP4)) {
  786. ip_rt_put(rt);
  787. goto tx_error;
  788. }
  789. if (df) {
  790. mtu = dst_mtu(&rt->dst) - t_hlen;
  791. if (mtu < 68) {
  792. dev->stats.collisions++;
  793. ip_rt_put(rt);
  794. goto tx_error;
  795. }
  796. if (mtu < IPV6_MIN_MTU) {
  797. mtu = IPV6_MIN_MTU;
  798. df = 0;
  799. }
  800. if (tunnel->parms.iph.daddr)
  801. skb_dst_update_pmtu_no_confirm(skb, mtu);
  802. if (skb->len > mtu && !skb_is_gso(skb)) {
  803. icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
  804. ip_rt_put(rt);
  805. goto tx_error;
  806. }
  807. }
  808. if (tunnel->err_count > 0) {
  809. if (time_before(jiffies,
  810. tunnel->err_time + IPTUNNEL_ERR_TIMEO)) {
  811. tunnel->err_count--;
  812. dst_link_failure(skb);
  813. } else
  814. tunnel->err_count = 0;
  815. }
  816. /*
  817. * Okay, now see if we can stuff it in the buffer as-is.
  818. */
  819. max_headroom = LL_RESERVED_SPACE(tdev) + t_hlen;
  820. if (skb_headroom(skb) < max_headroom || skb_shared(skb) ||
  821. (skb_cloned(skb) && !skb_clone_writable(skb, 0))) {
  822. struct sk_buff *new_skb = skb_realloc_headroom(skb, max_headroom);
  823. if (!new_skb) {
  824. ip_rt_put(rt);
  825. dev->stats.tx_dropped++;
  826. kfree_skb(skb);
  827. return NETDEV_TX_OK;
  828. }
  829. if (skb->sk)
  830. skb_set_owner_w(new_skb, skb->sk);
  831. dev_kfree_skb(skb);
  832. skb = new_skb;
  833. iph6 = ipv6_hdr(skb);
  834. }
  835. ttl = tiph->ttl;
  836. if (ttl == 0)
  837. ttl = iph6->hop_limit;
  838. tos = INET_ECN_encapsulate(tos, ipv6_get_dsfield(iph6));
  839. if (ip_tunnel_encap(skb, tunnel, &protocol, &fl4) < 0) {
  840. ip_rt_put(rt);
  841. goto tx_error;
  842. }
  843. skb_set_inner_ipproto(skb, IPPROTO_IPV6);
  844. iptunnel_xmit(NULL, rt, skb, fl4.saddr, fl4.daddr, protocol, tos, ttl,
  845. df, !net_eq(tunnel->net, dev_net(dev)));
  846. return NETDEV_TX_OK;
  847. tx_error_icmp:
  848. dst_link_failure(skb);
  849. tx_error:
  850. kfree_skb(skb);
  851. dev->stats.tx_errors++;
  852. return NETDEV_TX_OK;
  853. }
  854. static netdev_tx_t sit_tunnel_xmit__(struct sk_buff *skb,
  855. struct net_device *dev, u8 ipproto)
  856. {
  857. struct ip_tunnel *tunnel = netdev_priv(dev);
  858. const struct iphdr *tiph = &tunnel->parms.iph;
  859. if (iptunnel_handle_offloads(skb, SKB_GSO_IPXIP4))
  860. goto tx_error;
  861. skb_set_inner_ipproto(skb, ipproto);
  862. ip_tunnel_xmit(skb, dev, tiph, ipproto);
  863. return NETDEV_TX_OK;
  864. tx_error:
  865. kfree_skb(skb);
  866. dev->stats.tx_errors++;
  867. return NETDEV_TX_OK;
  868. }
  869. static netdev_tx_t sit_tunnel_xmit(struct sk_buff *skb,
  870. struct net_device *dev)
  871. {
  872. if (!pskb_inet_may_pull(skb))
  873. goto tx_err;
  874. switch (skb->protocol) {
  875. case htons(ETH_P_IP):
  876. sit_tunnel_xmit__(skb, dev, IPPROTO_IPIP);
  877. break;
  878. case htons(ETH_P_IPV6):
  879. ipip6_tunnel_xmit(skb, dev);
  880. break;
  881. #if IS_ENABLED(CONFIG_MPLS)
  882. case htons(ETH_P_MPLS_UC):
  883. sit_tunnel_xmit__(skb, dev, IPPROTO_MPLS);
  884. break;
  885. #endif
  886. default:
  887. goto tx_err;
  888. }
  889. return NETDEV_TX_OK;
  890. tx_err:
  891. dev->stats.tx_errors++;
  892. kfree_skb(skb);
  893. return NETDEV_TX_OK;
  894. }
  895. static void ipip6_tunnel_bind_dev(struct net_device *dev)
  896. {
  897. struct net_device *tdev = NULL;
  898. struct ip_tunnel *tunnel;
  899. const struct iphdr *iph;
  900. struct flowi4 fl4;
  901. tunnel = netdev_priv(dev);
  902. iph = &tunnel->parms.iph;
  903. if (iph->daddr) {
  904. struct rtable *rt = ip_route_output_ports(tunnel->net, &fl4,
  905. NULL,
  906. iph->daddr, iph->saddr,
  907. 0, 0,
  908. IPPROTO_IPV6,
  909. RT_TOS(iph->tos),
  910. tunnel->parms.link);
  911. if (!IS_ERR(rt)) {
  912. tdev = rt->dst.dev;
  913. ip_rt_put(rt);
  914. }
  915. dev->flags |= IFF_POINTOPOINT;
  916. }
  917. if (!tdev && tunnel->parms.link)
  918. tdev = __dev_get_by_index(tunnel->net, tunnel->parms.link);
  919. if (tdev && !netif_is_l3_master(tdev)) {
  920. int t_hlen = tunnel->hlen + sizeof(struct iphdr);
  921. dev->hard_header_len = tdev->hard_header_len + sizeof(struct iphdr);
  922. dev->mtu = tdev->mtu - t_hlen;
  923. if (dev->mtu < IPV6_MIN_MTU)
  924. dev->mtu = IPV6_MIN_MTU;
  925. }
  926. }
  927. static void ipip6_tunnel_update(struct ip_tunnel *t, struct ip_tunnel_parm *p,
  928. __u32 fwmark)
  929. {
  930. struct net *net = t->net;
  931. struct sit_net *sitn = net_generic(net, sit_net_id);
  932. ipip6_tunnel_unlink(sitn, t);
  933. synchronize_net();
  934. t->parms.iph.saddr = p->iph.saddr;
  935. t->parms.iph.daddr = p->iph.daddr;
  936. memcpy(t->dev->dev_addr, &p->iph.saddr, 4);
  937. memcpy(t->dev->broadcast, &p->iph.daddr, 4);
  938. ipip6_tunnel_link(sitn, t);
  939. t->parms.iph.ttl = p->iph.ttl;
  940. t->parms.iph.tos = p->iph.tos;
  941. t->parms.iph.frag_off = p->iph.frag_off;
  942. if (t->parms.link != p->link || t->fwmark != fwmark) {
  943. t->parms.link = p->link;
  944. t->fwmark = fwmark;
  945. ipip6_tunnel_bind_dev(t->dev);
  946. }
  947. dst_cache_reset(&t->dst_cache);
  948. netdev_state_change(t->dev);
  949. }
  950. #ifdef CONFIG_IPV6_SIT_6RD
  951. static int ipip6_tunnel_update_6rd(struct ip_tunnel *t,
  952. struct ip_tunnel_6rd *ip6rd)
  953. {
  954. struct in6_addr prefix;
  955. __be32 relay_prefix;
  956. if (ip6rd->relay_prefixlen > 32 ||
  957. ip6rd->prefixlen + (32 - ip6rd->relay_prefixlen) > 64)
  958. return -EINVAL;
  959. ipv6_addr_prefix(&prefix, &ip6rd->prefix, ip6rd->prefixlen);
  960. if (!ipv6_addr_equal(&prefix, &ip6rd->prefix))
  961. return -EINVAL;
  962. if (ip6rd->relay_prefixlen)
  963. relay_prefix = ip6rd->relay_prefix &
  964. htonl(0xffffffffUL <<
  965. (32 - ip6rd->relay_prefixlen));
  966. else
  967. relay_prefix = 0;
  968. if (relay_prefix != ip6rd->relay_prefix)
  969. return -EINVAL;
  970. t->ip6rd.prefix = prefix;
  971. t->ip6rd.relay_prefix = relay_prefix;
  972. t->ip6rd.prefixlen = ip6rd->prefixlen;
  973. t->ip6rd.relay_prefixlen = ip6rd->relay_prefixlen;
  974. dst_cache_reset(&t->dst_cache);
  975. netdev_state_change(t->dev);
  976. return 0;
  977. }
  978. #endif
  979. static bool ipip6_valid_ip_proto(u8 ipproto)
  980. {
  981. return ipproto == IPPROTO_IPV6 ||
  982. ipproto == IPPROTO_IPIP ||
  983. #if IS_ENABLED(CONFIG_MPLS)
  984. ipproto == IPPROTO_MPLS ||
  985. #endif
  986. ipproto == 0;
  987. }
  988. static int
  989. ipip6_tunnel_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  990. {
  991. int err = 0;
  992. struct ip_tunnel_parm p;
  993. struct ip_tunnel_prl prl;
  994. struct ip_tunnel *t = netdev_priv(dev);
  995. struct net *net = t->net;
  996. struct sit_net *sitn = net_generic(net, sit_net_id);
  997. #ifdef CONFIG_IPV6_SIT_6RD
  998. struct ip_tunnel_6rd ip6rd;
  999. #endif
  1000. switch (cmd) {
  1001. case SIOCGETTUNNEL:
  1002. #ifdef CONFIG_IPV6_SIT_6RD
  1003. case SIOCGET6RD:
  1004. #endif
  1005. if (dev == sitn->fb_tunnel_dev) {
  1006. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) {
  1007. err = -EFAULT;
  1008. break;
  1009. }
  1010. t = ipip6_tunnel_locate(net, &p, 0);
  1011. if (!t)
  1012. t = netdev_priv(dev);
  1013. }
  1014. err = -EFAULT;
  1015. if (cmd == SIOCGETTUNNEL) {
  1016. memcpy(&p, &t->parms, sizeof(p));
  1017. if (copy_to_user(ifr->ifr_ifru.ifru_data, &p,
  1018. sizeof(p)))
  1019. goto done;
  1020. #ifdef CONFIG_IPV6_SIT_6RD
  1021. } else {
  1022. ip6rd.prefix = t->ip6rd.prefix;
  1023. ip6rd.relay_prefix = t->ip6rd.relay_prefix;
  1024. ip6rd.prefixlen = t->ip6rd.prefixlen;
  1025. ip6rd.relay_prefixlen = t->ip6rd.relay_prefixlen;
  1026. if (copy_to_user(ifr->ifr_ifru.ifru_data, &ip6rd,
  1027. sizeof(ip6rd)))
  1028. goto done;
  1029. #endif
  1030. }
  1031. err = 0;
  1032. break;
  1033. case SIOCADDTUNNEL:
  1034. case SIOCCHGTUNNEL:
  1035. err = -EPERM;
  1036. if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  1037. goto done;
  1038. err = -EFAULT;
  1039. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
  1040. goto done;
  1041. err = -EINVAL;
  1042. if (!ipip6_valid_ip_proto(p.iph.protocol))
  1043. goto done;
  1044. if (p.iph.version != 4 ||
  1045. p.iph.ihl != 5 || (p.iph.frag_off&htons(~IP_DF)))
  1046. goto done;
  1047. if (p.iph.ttl)
  1048. p.iph.frag_off |= htons(IP_DF);
  1049. t = ipip6_tunnel_locate(net, &p, cmd == SIOCADDTUNNEL);
  1050. if (dev != sitn->fb_tunnel_dev && cmd == SIOCCHGTUNNEL) {
  1051. if (t) {
  1052. if (t->dev != dev) {
  1053. err = -EEXIST;
  1054. break;
  1055. }
  1056. } else {
  1057. if (((dev->flags&IFF_POINTOPOINT) && !p.iph.daddr) ||
  1058. (!(dev->flags&IFF_POINTOPOINT) && p.iph.daddr)) {
  1059. err = -EINVAL;
  1060. break;
  1061. }
  1062. t = netdev_priv(dev);
  1063. }
  1064. ipip6_tunnel_update(t, &p, t->fwmark);
  1065. }
  1066. if (t) {
  1067. err = 0;
  1068. if (copy_to_user(ifr->ifr_ifru.ifru_data, &t->parms, sizeof(p)))
  1069. err = -EFAULT;
  1070. } else
  1071. err = (cmd == SIOCADDTUNNEL ? -ENOBUFS : -ENOENT);
  1072. break;
  1073. case SIOCDELTUNNEL:
  1074. err = -EPERM;
  1075. if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  1076. goto done;
  1077. if (dev == sitn->fb_tunnel_dev) {
  1078. err = -EFAULT;
  1079. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
  1080. goto done;
  1081. err = -ENOENT;
  1082. t = ipip6_tunnel_locate(net, &p, 0);
  1083. if (!t)
  1084. goto done;
  1085. err = -EPERM;
  1086. if (t == netdev_priv(sitn->fb_tunnel_dev))
  1087. goto done;
  1088. dev = t->dev;
  1089. }
  1090. unregister_netdevice(dev);
  1091. err = 0;
  1092. break;
  1093. case SIOCGETPRL:
  1094. err = -EINVAL;
  1095. if (dev == sitn->fb_tunnel_dev)
  1096. goto done;
  1097. err = ipip6_tunnel_get_prl(t, ifr->ifr_ifru.ifru_data);
  1098. break;
  1099. case SIOCADDPRL:
  1100. case SIOCDELPRL:
  1101. case SIOCCHGPRL:
  1102. err = -EPERM;
  1103. if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  1104. goto done;
  1105. err = -EINVAL;
  1106. if (dev == sitn->fb_tunnel_dev)
  1107. goto done;
  1108. err = -EFAULT;
  1109. if (copy_from_user(&prl, ifr->ifr_ifru.ifru_data, sizeof(prl)))
  1110. goto done;
  1111. switch (cmd) {
  1112. case SIOCDELPRL:
  1113. err = ipip6_tunnel_del_prl(t, &prl);
  1114. break;
  1115. case SIOCADDPRL:
  1116. case SIOCCHGPRL:
  1117. err = ipip6_tunnel_add_prl(t, &prl, cmd == SIOCCHGPRL);
  1118. break;
  1119. }
  1120. dst_cache_reset(&t->dst_cache);
  1121. netdev_state_change(dev);
  1122. break;
  1123. #ifdef CONFIG_IPV6_SIT_6RD
  1124. case SIOCADD6RD:
  1125. case SIOCCHG6RD:
  1126. case SIOCDEL6RD:
  1127. err = -EPERM;
  1128. if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  1129. goto done;
  1130. err = -EFAULT;
  1131. if (copy_from_user(&ip6rd, ifr->ifr_ifru.ifru_data,
  1132. sizeof(ip6rd)))
  1133. goto done;
  1134. if (cmd != SIOCDEL6RD) {
  1135. err = ipip6_tunnel_update_6rd(t, &ip6rd);
  1136. if (err < 0)
  1137. goto done;
  1138. } else
  1139. ipip6_tunnel_clone_6rd(dev, sitn);
  1140. err = 0;
  1141. break;
  1142. #endif
  1143. default:
  1144. err = -EINVAL;
  1145. }
  1146. done:
  1147. return err;
  1148. }
  1149. static const struct net_device_ops ipip6_netdev_ops = {
  1150. .ndo_init = ipip6_tunnel_init,
  1151. .ndo_uninit = ipip6_tunnel_uninit,
  1152. .ndo_start_xmit = sit_tunnel_xmit,
  1153. .ndo_do_ioctl = ipip6_tunnel_ioctl,
  1154. .ndo_get_stats64 = ip_tunnel_get_stats64,
  1155. .ndo_get_iflink = ip_tunnel_get_iflink,
  1156. };
  1157. static void ipip6_dev_free(struct net_device *dev)
  1158. {
  1159. struct ip_tunnel *tunnel = netdev_priv(dev);
  1160. dst_cache_destroy(&tunnel->dst_cache);
  1161. free_percpu(dev->tstats);
  1162. }
  1163. #define SIT_FEATURES (NETIF_F_SG | \
  1164. NETIF_F_FRAGLIST | \
  1165. NETIF_F_HIGHDMA | \
  1166. NETIF_F_GSO_SOFTWARE | \
  1167. NETIF_F_HW_CSUM)
  1168. static void ipip6_tunnel_setup(struct net_device *dev)
  1169. {
  1170. struct ip_tunnel *tunnel = netdev_priv(dev);
  1171. int t_hlen = tunnel->hlen + sizeof(struct iphdr);
  1172. dev->netdev_ops = &ipip6_netdev_ops;
  1173. dev->needs_free_netdev = true;
  1174. dev->priv_destructor = ipip6_dev_free;
  1175. dev->type = ARPHRD_SIT;
  1176. dev->hard_header_len = LL_MAX_HEADER + t_hlen;
  1177. dev->mtu = ETH_DATA_LEN - t_hlen;
  1178. dev->min_mtu = IPV6_MIN_MTU;
  1179. dev->max_mtu = IP6_MAX_MTU - t_hlen;
  1180. dev->flags = IFF_NOARP;
  1181. netif_keep_dst(dev);
  1182. dev->addr_len = 4;
  1183. dev->features |= NETIF_F_LLTX;
  1184. dev->features |= SIT_FEATURES;
  1185. dev->hw_features |= SIT_FEATURES;
  1186. }
  1187. static int ipip6_tunnel_init(struct net_device *dev)
  1188. {
  1189. struct ip_tunnel *tunnel = netdev_priv(dev);
  1190. int err;
  1191. tunnel->dev = dev;
  1192. tunnel->net = dev_net(dev);
  1193. strcpy(tunnel->parms.name, dev->name);
  1194. ipip6_tunnel_bind_dev(dev);
  1195. dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
  1196. if (!dev->tstats)
  1197. return -ENOMEM;
  1198. err = dst_cache_init(&tunnel->dst_cache, GFP_KERNEL);
  1199. if (err) {
  1200. free_percpu(dev->tstats);
  1201. dev->tstats = NULL;
  1202. return err;
  1203. }
  1204. return 0;
  1205. }
  1206. static void __net_init ipip6_fb_tunnel_init(struct net_device *dev)
  1207. {
  1208. struct ip_tunnel *tunnel = netdev_priv(dev);
  1209. struct iphdr *iph = &tunnel->parms.iph;
  1210. struct net *net = dev_net(dev);
  1211. struct sit_net *sitn = net_generic(net, sit_net_id);
  1212. iph->version = 4;
  1213. iph->protocol = IPPROTO_IPV6;
  1214. iph->ihl = 5;
  1215. iph->ttl = 64;
  1216. dev_hold(dev);
  1217. rcu_assign_pointer(sitn->tunnels_wc[0], tunnel);
  1218. }
  1219. static int ipip6_validate(struct nlattr *tb[], struct nlattr *data[],
  1220. struct netlink_ext_ack *extack)
  1221. {
  1222. u8 proto;
  1223. if (!data || !data[IFLA_IPTUN_PROTO])
  1224. return 0;
  1225. proto = nla_get_u8(data[IFLA_IPTUN_PROTO]);
  1226. if (!ipip6_valid_ip_proto(proto))
  1227. return -EINVAL;
  1228. return 0;
  1229. }
  1230. static void ipip6_netlink_parms(struct nlattr *data[],
  1231. struct ip_tunnel_parm *parms,
  1232. __u32 *fwmark)
  1233. {
  1234. memset(parms, 0, sizeof(*parms));
  1235. parms->iph.version = 4;
  1236. parms->iph.protocol = IPPROTO_IPV6;
  1237. parms->iph.ihl = 5;
  1238. parms->iph.ttl = 64;
  1239. if (!data)
  1240. return;
  1241. if (data[IFLA_IPTUN_LINK])
  1242. parms->link = nla_get_u32(data[IFLA_IPTUN_LINK]);
  1243. if (data[IFLA_IPTUN_LOCAL])
  1244. parms->iph.saddr = nla_get_be32(data[IFLA_IPTUN_LOCAL]);
  1245. if (data[IFLA_IPTUN_REMOTE])
  1246. parms->iph.daddr = nla_get_be32(data[IFLA_IPTUN_REMOTE]);
  1247. if (data[IFLA_IPTUN_TTL]) {
  1248. parms->iph.ttl = nla_get_u8(data[IFLA_IPTUN_TTL]);
  1249. if (parms->iph.ttl)
  1250. parms->iph.frag_off = htons(IP_DF);
  1251. }
  1252. if (data[IFLA_IPTUN_TOS])
  1253. parms->iph.tos = nla_get_u8(data[IFLA_IPTUN_TOS]);
  1254. if (!data[IFLA_IPTUN_PMTUDISC] || nla_get_u8(data[IFLA_IPTUN_PMTUDISC]))
  1255. parms->iph.frag_off = htons(IP_DF);
  1256. if (data[IFLA_IPTUN_FLAGS])
  1257. parms->i_flags = nla_get_be16(data[IFLA_IPTUN_FLAGS]);
  1258. if (data[IFLA_IPTUN_PROTO])
  1259. parms->iph.protocol = nla_get_u8(data[IFLA_IPTUN_PROTO]);
  1260. if (data[IFLA_IPTUN_FWMARK])
  1261. *fwmark = nla_get_u32(data[IFLA_IPTUN_FWMARK]);
  1262. }
  1263. /* This function returns true when ENCAP attributes are present in the nl msg */
  1264. static bool ipip6_netlink_encap_parms(struct nlattr *data[],
  1265. struct ip_tunnel_encap *ipencap)
  1266. {
  1267. bool ret = false;
  1268. memset(ipencap, 0, sizeof(*ipencap));
  1269. if (!data)
  1270. return ret;
  1271. if (data[IFLA_IPTUN_ENCAP_TYPE]) {
  1272. ret = true;
  1273. ipencap->type = nla_get_u16(data[IFLA_IPTUN_ENCAP_TYPE]);
  1274. }
  1275. if (data[IFLA_IPTUN_ENCAP_FLAGS]) {
  1276. ret = true;
  1277. ipencap->flags = nla_get_u16(data[IFLA_IPTUN_ENCAP_FLAGS]);
  1278. }
  1279. if (data[IFLA_IPTUN_ENCAP_SPORT]) {
  1280. ret = true;
  1281. ipencap->sport = nla_get_be16(data[IFLA_IPTUN_ENCAP_SPORT]);
  1282. }
  1283. if (data[IFLA_IPTUN_ENCAP_DPORT]) {
  1284. ret = true;
  1285. ipencap->dport = nla_get_be16(data[IFLA_IPTUN_ENCAP_DPORT]);
  1286. }
  1287. return ret;
  1288. }
  1289. #ifdef CONFIG_IPV6_SIT_6RD
  1290. /* This function returns true when 6RD attributes are present in the nl msg */
  1291. static bool ipip6_netlink_6rd_parms(struct nlattr *data[],
  1292. struct ip_tunnel_6rd *ip6rd)
  1293. {
  1294. bool ret = false;
  1295. memset(ip6rd, 0, sizeof(*ip6rd));
  1296. if (!data)
  1297. return ret;
  1298. if (data[IFLA_IPTUN_6RD_PREFIX]) {
  1299. ret = true;
  1300. ip6rd->prefix = nla_get_in6_addr(data[IFLA_IPTUN_6RD_PREFIX]);
  1301. }
  1302. if (data[IFLA_IPTUN_6RD_RELAY_PREFIX]) {
  1303. ret = true;
  1304. ip6rd->relay_prefix =
  1305. nla_get_be32(data[IFLA_IPTUN_6RD_RELAY_PREFIX]);
  1306. }
  1307. if (data[IFLA_IPTUN_6RD_PREFIXLEN]) {
  1308. ret = true;
  1309. ip6rd->prefixlen = nla_get_u16(data[IFLA_IPTUN_6RD_PREFIXLEN]);
  1310. }
  1311. if (data[IFLA_IPTUN_6RD_RELAY_PREFIXLEN]) {
  1312. ret = true;
  1313. ip6rd->relay_prefixlen =
  1314. nla_get_u16(data[IFLA_IPTUN_6RD_RELAY_PREFIXLEN]);
  1315. }
  1316. return ret;
  1317. }
  1318. #endif
  1319. static int ipip6_newlink(struct net *src_net, struct net_device *dev,
  1320. struct nlattr *tb[], struct nlattr *data[],
  1321. struct netlink_ext_ack *extack)
  1322. {
  1323. struct net *net = dev_net(dev);
  1324. struct ip_tunnel *nt;
  1325. struct ip_tunnel_encap ipencap;
  1326. #ifdef CONFIG_IPV6_SIT_6RD
  1327. struct ip_tunnel_6rd ip6rd;
  1328. #endif
  1329. int err;
  1330. nt = netdev_priv(dev);
  1331. if (ipip6_netlink_encap_parms(data, &ipencap)) {
  1332. err = ip_tunnel_encap_setup(nt, &ipencap);
  1333. if (err < 0)
  1334. return err;
  1335. }
  1336. ipip6_netlink_parms(data, &nt->parms, &nt->fwmark);
  1337. if (ipip6_tunnel_locate(net, &nt->parms, 0))
  1338. return -EEXIST;
  1339. err = ipip6_tunnel_create(dev);
  1340. if (err < 0)
  1341. return err;
  1342. if (tb[IFLA_MTU]) {
  1343. u32 mtu = nla_get_u32(tb[IFLA_MTU]);
  1344. if (mtu >= IPV6_MIN_MTU &&
  1345. mtu <= IP6_MAX_MTU - dev->hard_header_len)
  1346. dev->mtu = mtu;
  1347. }
  1348. #ifdef CONFIG_IPV6_SIT_6RD
  1349. if (ipip6_netlink_6rd_parms(data, &ip6rd))
  1350. err = ipip6_tunnel_update_6rd(nt, &ip6rd);
  1351. #endif
  1352. return err;
  1353. }
  1354. static int ipip6_changelink(struct net_device *dev, struct nlattr *tb[],
  1355. struct nlattr *data[],
  1356. struct netlink_ext_ack *extack)
  1357. {
  1358. struct ip_tunnel *t = netdev_priv(dev);
  1359. struct ip_tunnel_parm p;
  1360. struct ip_tunnel_encap ipencap;
  1361. struct net *net = t->net;
  1362. struct sit_net *sitn = net_generic(net, sit_net_id);
  1363. #ifdef CONFIG_IPV6_SIT_6RD
  1364. struct ip_tunnel_6rd ip6rd;
  1365. #endif
  1366. __u32 fwmark = t->fwmark;
  1367. int err;
  1368. if (dev == sitn->fb_tunnel_dev)
  1369. return -EINVAL;
  1370. if (ipip6_netlink_encap_parms(data, &ipencap)) {
  1371. err = ip_tunnel_encap_setup(t, &ipencap);
  1372. if (err < 0)
  1373. return err;
  1374. }
  1375. ipip6_netlink_parms(data, &p, &fwmark);
  1376. if (((dev->flags & IFF_POINTOPOINT) && !p.iph.daddr) ||
  1377. (!(dev->flags & IFF_POINTOPOINT) && p.iph.daddr))
  1378. return -EINVAL;
  1379. t = ipip6_tunnel_locate(net, &p, 0);
  1380. if (t) {
  1381. if (t->dev != dev)
  1382. return -EEXIST;
  1383. } else
  1384. t = netdev_priv(dev);
  1385. ipip6_tunnel_update(t, &p, fwmark);
  1386. #ifdef CONFIG_IPV6_SIT_6RD
  1387. if (ipip6_netlink_6rd_parms(data, &ip6rd))
  1388. return ipip6_tunnel_update_6rd(t, &ip6rd);
  1389. #endif
  1390. return 0;
  1391. }
  1392. static size_t ipip6_get_size(const struct net_device *dev)
  1393. {
  1394. return
  1395. /* IFLA_IPTUN_LINK */
  1396. nla_total_size(4) +
  1397. /* IFLA_IPTUN_LOCAL */
  1398. nla_total_size(4) +
  1399. /* IFLA_IPTUN_REMOTE */
  1400. nla_total_size(4) +
  1401. /* IFLA_IPTUN_TTL */
  1402. nla_total_size(1) +
  1403. /* IFLA_IPTUN_TOS */
  1404. nla_total_size(1) +
  1405. /* IFLA_IPTUN_PMTUDISC */
  1406. nla_total_size(1) +
  1407. /* IFLA_IPTUN_FLAGS */
  1408. nla_total_size(2) +
  1409. /* IFLA_IPTUN_PROTO */
  1410. nla_total_size(1) +
  1411. #ifdef CONFIG_IPV6_SIT_6RD
  1412. /* IFLA_IPTUN_6RD_PREFIX */
  1413. nla_total_size(sizeof(struct in6_addr)) +
  1414. /* IFLA_IPTUN_6RD_RELAY_PREFIX */
  1415. nla_total_size(4) +
  1416. /* IFLA_IPTUN_6RD_PREFIXLEN */
  1417. nla_total_size(2) +
  1418. /* IFLA_IPTUN_6RD_RELAY_PREFIXLEN */
  1419. nla_total_size(2) +
  1420. #endif
  1421. /* IFLA_IPTUN_ENCAP_TYPE */
  1422. nla_total_size(2) +
  1423. /* IFLA_IPTUN_ENCAP_FLAGS */
  1424. nla_total_size(2) +
  1425. /* IFLA_IPTUN_ENCAP_SPORT */
  1426. nla_total_size(2) +
  1427. /* IFLA_IPTUN_ENCAP_DPORT */
  1428. nla_total_size(2) +
  1429. /* IFLA_IPTUN_FWMARK */
  1430. nla_total_size(4) +
  1431. 0;
  1432. }
  1433. static int ipip6_fill_info(struct sk_buff *skb, const struct net_device *dev)
  1434. {
  1435. struct ip_tunnel *tunnel = netdev_priv(dev);
  1436. struct ip_tunnel_parm *parm = &tunnel->parms;
  1437. if (nla_put_u32(skb, IFLA_IPTUN_LINK, parm->link) ||
  1438. nla_put_in_addr(skb, IFLA_IPTUN_LOCAL, parm->iph.saddr) ||
  1439. nla_put_in_addr(skb, IFLA_IPTUN_REMOTE, parm->iph.daddr) ||
  1440. nla_put_u8(skb, IFLA_IPTUN_TTL, parm->iph.ttl) ||
  1441. nla_put_u8(skb, IFLA_IPTUN_TOS, parm->iph.tos) ||
  1442. nla_put_u8(skb, IFLA_IPTUN_PMTUDISC,
  1443. !!(parm->iph.frag_off & htons(IP_DF))) ||
  1444. nla_put_u8(skb, IFLA_IPTUN_PROTO, parm->iph.protocol) ||
  1445. nla_put_be16(skb, IFLA_IPTUN_FLAGS, parm->i_flags) ||
  1446. nla_put_u32(skb, IFLA_IPTUN_FWMARK, tunnel->fwmark))
  1447. goto nla_put_failure;
  1448. #ifdef CONFIG_IPV6_SIT_6RD
  1449. if (nla_put_in6_addr(skb, IFLA_IPTUN_6RD_PREFIX,
  1450. &tunnel->ip6rd.prefix) ||
  1451. nla_put_in_addr(skb, IFLA_IPTUN_6RD_RELAY_PREFIX,
  1452. tunnel->ip6rd.relay_prefix) ||
  1453. nla_put_u16(skb, IFLA_IPTUN_6RD_PREFIXLEN,
  1454. tunnel->ip6rd.prefixlen) ||
  1455. nla_put_u16(skb, IFLA_IPTUN_6RD_RELAY_PREFIXLEN,
  1456. tunnel->ip6rd.relay_prefixlen))
  1457. goto nla_put_failure;
  1458. #endif
  1459. if (nla_put_u16(skb, IFLA_IPTUN_ENCAP_TYPE,
  1460. tunnel->encap.type) ||
  1461. nla_put_be16(skb, IFLA_IPTUN_ENCAP_SPORT,
  1462. tunnel->encap.sport) ||
  1463. nla_put_be16(skb, IFLA_IPTUN_ENCAP_DPORT,
  1464. tunnel->encap.dport) ||
  1465. nla_put_u16(skb, IFLA_IPTUN_ENCAP_FLAGS,
  1466. tunnel->encap.flags))
  1467. goto nla_put_failure;
  1468. return 0;
  1469. nla_put_failure:
  1470. return -EMSGSIZE;
  1471. }
  1472. static const struct nla_policy ipip6_policy[IFLA_IPTUN_MAX + 1] = {
  1473. [IFLA_IPTUN_LINK] = { .type = NLA_U32 },
  1474. [IFLA_IPTUN_LOCAL] = { .type = NLA_U32 },
  1475. [IFLA_IPTUN_REMOTE] = { .type = NLA_U32 },
  1476. [IFLA_IPTUN_TTL] = { .type = NLA_U8 },
  1477. [IFLA_IPTUN_TOS] = { .type = NLA_U8 },
  1478. [IFLA_IPTUN_PMTUDISC] = { .type = NLA_U8 },
  1479. [IFLA_IPTUN_FLAGS] = { .type = NLA_U16 },
  1480. [IFLA_IPTUN_PROTO] = { .type = NLA_U8 },
  1481. #ifdef CONFIG_IPV6_SIT_6RD
  1482. [IFLA_IPTUN_6RD_PREFIX] = { .len = sizeof(struct in6_addr) },
  1483. [IFLA_IPTUN_6RD_RELAY_PREFIX] = { .type = NLA_U32 },
  1484. [IFLA_IPTUN_6RD_PREFIXLEN] = { .type = NLA_U16 },
  1485. [IFLA_IPTUN_6RD_RELAY_PREFIXLEN] = { .type = NLA_U16 },
  1486. #endif
  1487. [IFLA_IPTUN_ENCAP_TYPE] = { .type = NLA_U16 },
  1488. [IFLA_IPTUN_ENCAP_FLAGS] = { .type = NLA_U16 },
  1489. [IFLA_IPTUN_ENCAP_SPORT] = { .type = NLA_U16 },
  1490. [IFLA_IPTUN_ENCAP_DPORT] = { .type = NLA_U16 },
  1491. [IFLA_IPTUN_FWMARK] = { .type = NLA_U32 },
  1492. };
  1493. static void ipip6_dellink(struct net_device *dev, struct list_head *head)
  1494. {
  1495. struct net *net = dev_net(dev);
  1496. struct sit_net *sitn = net_generic(net, sit_net_id);
  1497. if (dev != sitn->fb_tunnel_dev)
  1498. unregister_netdevice_queue(dev, head);
  1499. }
  1500. static struct rtnl_link_ops sit_link_ops __read_mostly = {
  1501. .kind = "sit",
  1502. .maxtype = IFLA_IPTUN_MAX,
  1503. .policy = ipip6_policy,
  1504. .priv_size = sizeof(struct ip_tunnel),
  1505. .setup = ipip6_tunnel_setup,
  1506. .validate = ipip6_validate,
  1507. .newlink = ipip6_newlink,
  1508. .changelink = ipip6_changelink,
  1509. .get_size = ipip6_get_size,
  1510. .fill_info = ipip6_fill_info,
  1511. .dellink = ipip6_dellink,
  1512. .get_link_net = ip_tunnel_get_link_net,
  1513. };
  1514. static struct xfrm_tunnel sit_handler __read_mostly = {
  1515. .handler = ipip6_rcv,
  1516. .err_handler = ipip6_err,
  1517. .priority = 1,
  1518. };
  1519. static struct xfrm_tunnel ipip_handler __read_mostly = {
  1520. .handler = ipip_rcv,
  1521. .err_handler = ipip6_err,
  1522. .priority = 2,
  1523. };
  1524. #if IS_ENABLED(CONFIG_MPLS)
  1525. static struct xfrm_tunnel mplsip_handler __read_mostly = {
  1526. .handler = mplsip_rcv,
  1527. .err_handler = ipip6_err,
  1528. .priority = 2,
  1529. };
  1530. #endif
  1531. static void __net_exit sit_destroy_tunnels(struct net *net,
  1532. struct list_head *head)
  1533. {
  1534. struct sit_net *sitn = net_generic(net, sit_net_id);
  1535. struct net_device *dev, *aux;
  1536. int prio;
  1537. for_each_netdev_safe(net, dev, aux)
  1538. if (dev->rtnl_link_ops == &sit_link_ops)
  1539. unregister_netdevice_queue(dev, head);
  1540. for (prio = 1; prio < 4; prio++) {
  1541. int h;
  1542. for (h = 0; h < IP6_SIT_HASH_SIZE; h++) {
  1543. struct ip_tunnel *t;
  1544. t = rtnl_dereference(sitn->tunnels[prio][h]);
  1545. while (t) {
  1546. /* If dev is in the same netns, it has already
  1547. * been added to the list by the previous loop.
  1548. */
  1549. if (!net_eq(dev_net(t->dev), net))
  1550. unregister_netdevice_queue(t->dev,
  1551. head);
  1552. t = rtnl_dereference(t->next);
  1553. }
  1554. }
  1555. }
  1556. }
  1557. static int __net_init sit_init_net(struct net *net)
  1558. {
  1559. struct sit_net *sitn = net_generic(net, sit_net_id);
  1560. struct ip_tunnel *t;
  1561. int err;
  1562. sitn->tunnels[0] = sitn->tunnels_wc;
  1563. sitn->tunnels[1] = sitn->tunnels_l;
  1564. sitn->tunnels[2] = sitn->tunnels_r;
  1565. sitn->tunnels[3] = sitn->tunnels_r_l;
  1566. if (!net_has_fallback_tunnels(net))
  1567. return 0;
  1568. sitn->fb_tunnel_dev = alloc_netdev(sizeof(struct ip_tunnel), "sit0",
  1569. NET_NAME_UNKNOWN,
  1570. ipip6_tunnel_setup);
  1571. if (!sitn->fb_tunnel_dev) {
  1572. err = -ENOMEM;
  1573. goto err_alloc_dev;
  1574. }
  1575. dev_net_set(sitn->fb_tunnel_dev, net);
  1576. sitn->fb_tunnel_dev->rtnl_link_ops = &sit_link_ops;
  1577. /* FB netdevice is special: we have one, and only one per netns.
  1578. * Allowing to move it to another netns is clearly unsafe.
  1579. */
  1580. sitn->fb_tunnel_dev->features |= NETIF_F_NETNS_LOCAL;
  1581. err = register_netdev(sitn->fb_tunnel_dev);
  1582. if (err)
  1583. goto err_reg_dev;
  1584. ipip6_tunnel_clone_6rd(sitn->fb_tunnel_dev, sitn);
  1585. ipip6_fb_tunnel_init(sitn->fb_tunnel_dev);
  1586. t = netdev_priv(sitn->fb_tunnel_dev);
  1587. strcpy(t->parms.name, sitn->fb_tunnel_dev->name);
  1588. return 0;
  1589. err_reg_dev:
  1590. ipip6_dev_free(sitn->fb_tunnel_dev);
  1591. free_netdev(sitn->fb_tunnel_dev);
  1592. err_alloc_dev:
  1593. return err;
  1594. }
  1595. static void __net_exit sit_exit_batch_net(struct list_head *net_list)
  1596. {
  1597. LIST_HEAD(list);
  1598. struct net *net;
  1599. rtnl_lock();
  1600. list_for_each_entry(net, net_list, exit_list)
  1601. sit_destroy_tunnels(net, &list);
  1602. unregister_netdevice_many(&list);
  1603. rtnl_unlock();
  1604. }
  1605. static struct pernet_operations sit_net_ops = {
  1606. .init = sit_init_net,
  1607. .exit_batch = sit_exit_batch_net,
  1608. .id = &sit_net_id,
  1609. .size = sizeof(struct sit_net),
  1610. };
  1611. static void __exit sit_cleanup(void)
  1612. {
  1613. rtnl_link_unregister(&sit_link_ops);
  1614. xfrm4_tunnel_deregister(&sit_handler, AF_INET6);
  1615. xfrm4_tunnel_deregister(&ipip_handler, AF_INET);
  1616. #if IS_ENABLED(CONFIG_MPLS)
  1617. xfrm4_tunnel_deregister(&mplsip_handler, AF_MPLS);
  1618. #endif
  1619. unregister_pernet_device(&sit_net_ops);
  1620. rcu_barrier(); /* Wait for completion of call_rcu()'s */
  1621. }
  1622. static int __init sit_init(void)
  1623. {
  1624. int err;
  1625. pr_info("IPv6, IPv4 and MPLS over IPv4 tunneling driver\n");
  1626. err = register_pernet_device(&sit_net_ops);
  1627. if (err < 0)
  1628. return err;
  1629. err = xfrm4_tunnel_register(&sit_handler, AF_INET6);
  1630. if (err < 0) {
  1631. pr_info("%s: can't register ip6ip4\n", __func__);
  1632. goto xfrm_tunnel_failed;
  1633. }
  1634. err = xfrm4_tunnel_register(&ipip_handler, AF_INET);
  1635. if (err < 0) {
  1636. pr_info("%s: can't register ip4ip4\n", __func__);
  1637. goto xfrm_tunnel4_failed;
  1638. }
  1639. #if IS_ENABLED(CONFIG_MPLS)
  1640. err = xfrm4_tunnel_register(&mplsip_handler, AF_MPLS);
  1641. if (err < 0) {
  1642. pr_info("%s: can't register mplsip\n", __func__);
  1643. goto xfrm_tunnel_mpls_failed;
  1644. }
  1645. #endif
  1646. err = rtnl_link_register(&sit_link_ops);
  1647. if (err < 0)
  1648. goto rtnl_link_failed;
  1649. out:
  1650. return err;
  1651. rtnl_link_failed:
  1652. #if IS_ENABLED(CONFIG_MPLS)
  1653. xfrm4_tunnel_deregister(&mplsip_handler, AF_MPLS);
  1654. xfrm_tunnel_mpls_failed:
  1655. #endif
  1656. xfrm4_tunnel_deregister(&ipip_handler, AF_INET);
  1657. xfrm_tunnel4_failed:
  1658. xfrm4_tunnel_deregister(&sit_handler, AF_INET6);
  1659. xfrm_tunnel_failed:
  1660. unregister_pernet_device(&sit_net_ops);
  1661. goto out;
  1662. }
  1663. module_init(sit_init);
  1664. module_exit(sit_cleanup);
  1665. MODULE_LICENSE("GPL");
  1666. MODULE_ALIAS_RTNL_LINK("sit");
  1667. MODULE_ALIAS_NETDEV("sit0");