sit.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339
  1. /*
  2. * IPv6 over IPv4 tunnel device - Simple Internet Transition (SIT)
  3. * Linux INET6 implementation
  4. *
  5. * Authors:
  6. * Pedro Roque <roque@di.fc.ul.pt>
  7. * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License
  11. * as published by the Free Software Foundation; either version
  12. * 2 of the License, or (at your option) any later version.
  13. *
  14. * Changes:
  15. * Roger Venning <r.venning@telstra.com>: 6to4 support
  16. * Nate Thompson <nate@thebog.net>: 6to4 support
  17. * Fred Templin <fred.l.templin@boeing.com>: isatap support
  18. */
  19. #include <linux/module.h>
  20. #include <linux/capability.h>
  21. #include <linux/errno.h>
  22. #include <linux/types.h>
  23. #include <linux/socket.h>
  24. #include <linux/sockios.h>
  25. #include <linux/net.h>
  26. #include <linux/in6.h>
  27. #include <linux/netdevice.h>
  28. #include <linux/if_arp.h>
  29. #include <linux/icmp.h>
  30. #include <linux/slab.h>
  31. #include <asm/uaccess.h>
  32. #include <linux/init.h>
  33. #include <linux/netfilter_ipv4.h>
  34. #include <linux/if_ether.h>
  35. #include <net/sock.h>
  36. #include <net/snmp.h>
  37. #include <net/ipv6.h>
  38. #include <net/protocol.h>
  39. #include <net/transp_v6.h>
  40. #include <net/ip6_fib.h>
  41. #include <net/ip6_route.h>
  42. #include <net/ndisc.h>
  43. #include <net/addrconf.h>
  44. #include <net/ip.h>
  45. #include <net/udp.h>
  46. #include <net/icmp.h>
  47. #include <net/ipip.h>
  48. #include <net/inet_ecn.h>
  49. #include <net/xfrm.h>
  50. #include <net/dsfield.h>
  51. #include <net/net_namespace.h>
  52. #include <net/netns/generic.h>
  53. /*
  54. This version of net/ipv6/sit.c is cloned of net/ipv4/ip_gre.c
  55. For comments look at net/ipv4/ip_gre.c --ANK
  56. */
  57. #define HASH_SIZE 16
  58. #define HASH(addr) (((__force u32)addr^((__force u32)addr>>4))&0xF)
  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 int sit_net_id __read_mostly;
  63. struct sit_net {
  64. struct ip_tunnel __rcu *tunnels_r_l[HASH_SIZE];
  65. struct ip_tunnel __rcu *tunnels_r[HASH_SIZE];
  66. struct ip_tunnel __rcu *tunnels_l[HASH_SIZE];
  67. struct ip_tunnel __rcu *tunnels_wc[1];
  68. struct ip_tunnel __rcu **tunnels[4];
  69. struct net_device *fb_tunnel_dev;
  70. };
  71. /*
  72. * Locking : hash tables are protected by RCU and RTNL
  73. */
  74. #define for_each_ip_tunnel_rcu(start) \
  75. for (t = rcu_dereference(start); t; t = rcu_dereference(t->next))
  76. /* often modified stats are per cpu, other are shared (netdev->stats) */
  77. struct pcpu_tstats {
  78. u64 rx_packets;
  79. u64 rx_bytes;
  80. u64 tx_packets;
  81. u64 tx_bytes;
  82. struct u64_stats_sync syncp;
  83. };
  84. static struct rtnl_link_stats64 *ipip6_get_stats64(struct net_device *dev,
  85. struct rtnl_link_stats64 *tot)
  86. {
  87. int i;
  88. for_each_possible_cpu(i) {
  89. const struct pcpu_tstats *tstats = per_cpu_ptr(dev->tstats, i);
  90. u64 rx_packets, rx_bytes, tx_packets, tx_bytes;
  91. unsigned int start;
  92. do {
  93. start = u64_stats_fetch_begin_irq(&tstats->syncp);
  94. rx_packets = tstats->rx_packets;
  95. tx_packets = tstats->tx_packets;
  96. rx_bytes = tstats->rx_bytes;
  97. tx_bytes = tstats->tx_bytes;
  98. } while (u64_stats_fetch_retry_irq(&tstats->syncp, start));
  99. tot->rx_packets += rx_packets;
  100. tot->tx_packets += tx_packets;
  101. tot->rx_bytes += rx_bytes;
  102. tot->tx_bytes += tx_bytes;
  103. }
  104. tot->rx_errors = dev->stats.rx_errors;
  105. tot->tx_fifo_errors = dev->stats.tx_fifo_errors;
  106. tot->tx_carrier_errors = dev->stats.tx_carrier_errors;
  107. tot->tx_dropped = dev->stats.tx_dropped;
  108. tot->tx_aborted_errors = dev->stats.tx_aborted_errors;
  109. tot->tx_errors = dev->stats.tx_errors;
  110. return tot;
  111. }
  112. /*
  113. * Must be invoked with rcu_read_lock
  114. */
  115. static struct ip_tunnel * ipip6_tunnel_lookup(struct net *net,
  116. struct net_device *dev, __be32 remote, __be32 local)
  117. {
  118. unsigned int h0 = HASH(remote);
  119. unsigned int h1 = HASH(local);
  120. struct ip_tunnel *t;
  121. struct sit_net *sitn = net_generic(net, sit_net_id);
  122. for_each_ip_tunnel_rcu(sitn->tunnels_r_l[h0 ^ h1]) {
  123. if (local == t->parms.iph.saddr &&
  124. remote == t->parms.iph.daddr &&
  125. (!dev || !t->parms.link || dev->iflink == t->parms.link) &&
  126. (t->dev->flags & IFF_UP))
  127. return t;
  128. }
  129. for_each_ip_tunnel_rcu(sitn->tunnels_r[h0]) {
  130. if (remote == t->parms.iph.daddr &&
  131. (!dev || !t->parms.link || dev->iflink == t->parms.link) &&
  132. (t->dev->flags & IFF_UP))
  133. return t;
  134. }
  135. for_each_ip_tunnel_rcu(sitn->tunnels_l[h1]) {
  136. if (local == t->parms.iph.saddr &&
  137. (!dev || !t->parms.link || dev->iflink == t->parms.link) &&
  138. (t->dev->flags & IFF_UP))
  139. return t;
  140. }
  141. t = rcu_dereference(sitn->tunnels_wc[0]);
  142. if ((t != NULL) && (t->dev->flags & IFF_UP))
  143. return t;
  144. return NULL;
  145. }
  146. static struct ip_tunnel __rcu **__ipip6_bucket(struct sit_net *sitn,
  147. struct ip_tunnel_parm *parms)
  148. {
  149. __be32 remote = parms->iph.daddr;
  150. __be32 local = parms->iph.saddr;
  151. unsigned int h = 0;
  152. int prio = 0;
  153. if (remote) {
  154. prio |= 2;
  155. h ^= HASH(remote);
  156. }
  157. if (local) {
  158. prio |= 1;
  159. h ^= HASH(local);
  160. }
  161. return &sitn->tunnels[prio][h];
  162. }
  163. static inline struct ip_tunnel __rcu **ipip6_bucket(struct sit_net *sitn,
  164. struct ip_tunnel *t)
  165. {
  166. return __ipip6_bucket(sitn, &t->parms);
  167. }
  168. static void ipip6_tunnel_unlink(struct sit_net *sitn, struct ip_tunnel *t)
  169. {
  170. struct ip_tunnel __rcu **tp;
  171. struct ip_tunnel *iter;
  172. for (tp = ipip6_bucket(sitn, t);
  173. (iter = rtnl_dereference(*tp)) != NULL;
  174. tp = &iter->next) {
  175. if (t == iter) {
  176. rcu_assign_pointer(*tp, t->next);
  177. break;
  178. }
  179. }
  180. }
  181. static void ipip6_tunnel_link(struct sit_net *sitn, struct ip_tunnel *t)
  182. {
  183. struct ip_tunnel __rcu **tp = ipip6_bucket(sitn, t);
  184. rcu_assign_pointer(t->next, rtnl_dereference(*tp));
  185. rcu_assign_pointer(*tp, t);
  186. }
  187. static void ipip6_tunnel_clone_6rd(struct net_device *dev, struct sit_net *sitn)
  188. {
  189. #ifdef CONFIG_IPV6_SIT_6RD
  190. struct ip_tunnel *t = netdev_priv(dev);
  191. if (t->dev == sitn->fb_tunnel_dev) {
  192. ipv6_addr_set(&t->ip6rd.prefix, htonl(0x20020000), 0, 0, 0);
  193. t->ip6rd.relay_prefix = 0;
  194. t->ip6rd.prefixlen = 16;
  195. t->ip6rd.relay_prefixlen = 0;
  196. } else {
  197. struct ip_tunnel *t0 = netdev_priv(sitn->fb_tunnel_dev);
  198. memcpy(&t->ip6rd, &t0->ip6rd, sizeof(t->ip6rd));
  199. }
  200. #endif
  201. }
  202. static struct ip_tunnel *ipip6_tunnel_locate(struct net *net,
  203. struct ip_tunnel_parm *parms, int create)
  204. {
  205. __be32 remote = parms->iph.daddr;
  206. __be32 local = parms->iph.saddr;
  207. struct ip_tunnel *t, *nt;
  208. struct ip_tunnel __rcu **tp;
  209. struct net_device *dev;
  210. char name[IFNAMSIZ];
  211. struct sit_net *sitn = net_generic(net, sit_net_id);
  212. for (tp = __ipip6_bucket(sitn, parms);
  213. (t = rtnl_dereference(*tp)) != NULL;
  214. tp = &t->next) {
  215. if (local == t->parms.iph.saddr &&
  216. remote == t->parms.iph.daddr &&
  217. parms->link == t->parms.link) {
  218. if (create)
  219. return NULL;
  220. else
  221. return t;
  222. }
  223. }
  224. if (!create)
  225. goto failed;
  226. if (parms->name[0])
  227. strlcpy(name, parms->name, IFNAMSIZ);
  228. else
  229. strcpy(name, "sit%d");
  230. dev = alloc_netdev(sizeof(*t), name, ipip6_tunnel_setup);
  231. if (dev == NULL)
  232. return NULL;
  233. dev_net_set(dev, net);
  234. nt = netdev_priv(dev);
  235. nt->parms = *parms;
  236. if (ipip6_tunnel_init(dev) < 0)
  237. goto failed_free;
  238. ipip6_tunnel_clone_6rd(dev, sitn);
  239. if (parms->i_flags & SIT_ISATAP)
  240. dev->priv_flags |= IFF_ISATAP;
  241. if (register_netdevice(dev) < 0)
  242. goto failed_free;
  243. strcpy(nt->parms.name, dev->name);
  244. dev_hold(dev);
  245. ipip6_tunnel_link(sitn, nt);
  246. return nt;
  247. failed_free:
  248. ipip6_dev_free(dev);
  249. failed:
  250. return NULL;
  251. }
  252. #define for_each_prl_rcu(start) \
  253. for (prl = rcu_dereference(start); \
  254. prl; \
  255. prl = rcu_dereference(prl->next))
  256. static struct ip_tunnel_prl_entry *
  257. __ipip6_tunnel_locate_prl(struct ip_tunnel *t, __be32 addr)
  258. {
  259. struct ip_tunnel_prl_entry *prl;
  260. for_each_prl_rcu(t->prl)
  261. if (prl->addr == addr)
  262. break;
  263. return prl;
  264. }
  265. static int ipip6_tunnel_get_prl(struct ip_tunnel *t,
  266. struct ip_tunnel_prl __user *a)
  267. {
  268. struct ip_tunnel_prl kprl, *kp;
  269. struct ip_tunnel_prl_entry *prl;
  270. unsigned int cmax, c = 0, ca, len;
  271. int ret = 0;
  272. if (copy_from_user(&kprl, a, sizeof(kprl)))
  273. return -EFAULT;
  274. cmax = kprl.datalen / sizeof(kprl);
  275. if (cmax > 1 && kprl.addr != htonl(INADDR_ANY))
  276. cmax = 1;
  277. /* For simple GET or for root users,
  278. * we try harder to allocate.
  279. */
  280. kp = (cmax <= 1 || capable(CAP_NET_ADMIN)) ?
  281. kcalloc(cmax, sizeof(*kp), GFP_KERNEL) :
  282. NULL;
  283. rcu_read_lock();
  284. ca = t->prl_count < cmax ? t->prl_count : cmax;
  285. if (!kp) {
  286. /* We don't try hard to allocate much memory for
  287. * non-root users.
  288. * For root users, retry allocating enough memory for
  289. * the answer.
  290. */
  291. kp = kcalloc(ca, sizeof(*kp), GFP_ATOMIC);
  292. if (!kp) {
  293. ret = -ENOMEM;
  294. goto out;
  295. }
  296. }
  297. c = 0;
  298. for_each_prl_rcu(t->prl) {
  299. if (c >= cmax)
  300. break;
  301. if (kprl.addr != htonl(INADDR_ANY) && prl->addr != kprl.addr)
  302. continue;
  303. kp[c].addr = prl->addr;
  304. kp[c].flags = prl->flags;
  305. c++;
  306. if (kprl.addr != htonl(INADDR_ANY))
  307. break;
  308. }
  309. out:
  310. rcu_read_unlock();
  311. len = sizeof(*kp) * c;
  312. ret = 0;
  313. if ((len && copy_to_user(a + 1, kp, len)) || put_user(len, &a->datalen))
  314. ret = -EFAULT;
  315. kfree(kp);
  316. return ret;
  317. }
  318. static int
  319. ipip6_tunnel_add_prl(struct ip_tunnel *t, struct ip_tunnel_prl *a, int chg)
  320. {
  321. struct ip_tunnel_prl_entry *p;
  322. int err = 0;
  323. if (a->addr == htonl(INADDR_ANY))
  324. return -EINVAL;
  325. ASSERT_RTNL();
  326. for (p = rtnl_dereference(t->prl); p; p = rtnl_dereference(p->next)) {
  327. if (p->addr == a->addr) {
  328. if (chg) {
  329. p->flags = a->flags;
  330. goto out;
  331. }
  332. err = -EEXIST;
  333. goto out;
  334. }
  335. }
  336. if (chg) {
  337. err = -ENXIO;
  338. goto out;
  339. }
  340. p = kzalloc(sizeof(struct ip_tunnel_prl_entry), GFP_KERNEL);
  341. if (!p) {
  342. err = -ENOBUFS;
  343. goto out;
  344. }
  345. p->next = t->prl;
  346. p->addr = a->addr;
  347. p->flags = a->flags;
  348. t->prl_count++;
  349. rcu_assign_pointer(t->prl, p);
  350. out:
  351. return err;
  352. }
  353. static void prl_list_destroy_rcu(struct rcu_head *head)
  354. {
  355. struct ip_tunnel_prl_entry *p, *n;
  356. p = container_of(head, struct ip_tunnel_prl_entry, rcu_head);
  357. do {
  358. n = rcu_dereference_protected(p->next, 1);
  359. kfree(p);
  360. p = n;
  361. } while (p);
  362. }
  363. static int
  364. ipip6_tunnel_del_prl(struct ip_tunnel *t, struct ip_tunnel_prl *a)
  365. {
  366. struct ip_tunnel_prl_entry *x;
  367. struct ip_tunnel_prl_entry __rcu **p;
  368. int err = 0;
  369. ASSERT_RTNL();
  370. if (a && a->addr != htonl(INADDR_ANY)) {
  371. for (p = &t->prl;
  372. (x = rtnl_dereference(*p)) != NULL;
  373. p = &x->next) {
  374. if (x->addr == a->addr) {
  375. *p = x->next;
  376. kfree_rcu(x, rcu_head);
  377. t->prl_count--;
  378. goto out;
  379. }
  380. }
  381. err = -ENXIO;
  382. } else {
  383. x = rtnl_dereference(t->prl);
  384. if (x) {
  385. t->prl_count = 0;
  386. call_rcu(&x->rcu_head, prl_list_destroy_rcu);
  387. t->prl = NULL;
  388. }
  389. }
  390. out:
  391. return err;
  392. }
  393. static int
  394. isatap_chksrc(struct sk_buff *skb, const struct iphdr *iph, struct ip_tunnel *t)
  395. {
  396. struct ip_tunnel_prl_entry *p;
  397. int ok = 1;
  398. rcu_read_lock();
  399. p = __ipip6_tunnel_locate_prl(t, iph->saddr);
  400. if (p) {
  401. if (p->flags & PRL_DEFAULT)
  402. skb->ndisc_nodetype = NDISC_NODETYPE_DEFAULT;
  403. else
  404. skb->ndisc_nodetype = NDISC_NODETYPE_NODEFAULT;
  405. } else {
  406. const struct in6_addr *addr6 = &ipv6_hdr(skb)->saddr;
  407. if (ipv6_addr_is_isatap(addr6) &&
  408. (addr6->s6_addr32[3] == iph->saddr) &&
  409. ipv6_chk_prefix(addr6, t->dev))
  410. skb->ndisc_nodetype = NDISC_NODETYPE_HOST;
  411. else
  412. ok = 0;
  413. }
  414. rcu_read_unlock();
  415. return ok;
  416. }
  417. static void ipip6_tunnel_uninit(struct net_device *dev)
  418. {
  419. struct net *net = dev_net(dev);
  420. struct sit_net *sitn = net_generic(net, sit_net_id);
  421. if (dev == sitn->fb_tunnel_dev) {
  422. RCU_INIT_POINTER(sitn->tunnels_wc[0], NULL);
  423. } else {
  424. ipip6_tunnel_unlink(sitn, netdev_priv(dev));
  425. ipip6_tunnel_del_prl(netdev_priv(dev), NULL);
  426. }
  427. dev_put(dev);
  428. }
  429. static int ipip6_err(struct sk_buff *skb, u32 info)
  430. {
  431. /* All the routers (except for Linux) return only
  432. 8 bytes of packet payload. It means, that precise relaying of
  433. ICMP in the real Internet is absolutely infeasible.
  434. */
  435. const struct iphdr *iph = (const struct iphdr *)skb->data;
  436. const int type = icmp_hdr(skb)->type;
  437. const int code = icmp_hdr(skb)->code;
  438. struct ip_tunnel *t;
  439. int err;
  440. switch (type) {
  441. default:
  442. case ICMP_PARAMETERPROB:
  443. return 0;
  444. case ICMP_DEST_UNREACH:
  445. switch (code) {
  446. case ICMP_SR_FAILED:
  447. case ICMP_PORT_UNREACH:
  448. /* Impossible event. */
  449. return 0;
  450. case ICMP_FRAG_NEEDED:
  451. /* Soft state for pmtu is maintained by IP core. */
  452. return 0;
  453. default:
  454. /* All others are translated to HOST_UNREACH.
  455. rfc2003 contains "deep thoughts" about NET_UNREACH,
  456. I believe they are just ether pollution. --ANK
  457. */
  458. break;
  459. }
  460. break;
  461. case ICMP_TIME_EXCEEDED:
  462. if (code != ICMP_EXC_TTL)
  463. return 0;
  464. break;
  465. }
  466. err = -ENOENT;
  467. rcu_read_lock();
  468. t = ipip6_tunnel_lookup(dev_net(skb->dev),
  469. skb->dev,
  470. iph->daddr,
  471. iph->saddr);
  472. if (t == NULL || t->parms.iph.daddr == 0)
  473. goto out;
  474. err = 0;
  475. if (t->parms.iph.ttl == 0 && type == ICMP_TIME_EXCEEDED)
  476. goto out;
  477. if (time_before(jiffies, t->err_time + IPTUNNEL_ERR_TIMEO))
  478. t->err_count++;
  479. else
  480. t->err_count = 1;
  481. t->err_time = jiffies;
  482. out:
  483. rcu_read_unlock();
  484. return err;
  485. }
  486. static inline void ipip6_ecn_decapsulate(const struct iphdr *iph, struct sk_buff *skb)
  487. {
  488. if (INET_ECN_is_ce(iph->tos))
  489. IP6_ECN_set_ce(ipv6_hdr(skb));
  490. }
  491. static int ipip6_rcv(struct sk_buff *skb)
  492. {
  493. const struct iphdr *iph;
  494. struct ip_tunnel *tunnel;
  495. if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
  496. goto out;
  497. iph = ip_hdr(skb);
  498. rcu_read_lock();
  499. tunnel = ipip6_tunnel_lookup(dev_net(skb->dev), skb->dev,
  500. iph->saddr, iph->daddr);
  501. if (tunnel != NULL) {
  502. struct pcpu_tstats *tstats;
  503. secpath_reset(skb);
  504. skb->mac_header = skb->network_header;
  505. skb_reset_network_header(skb);
  506. IPCB(skb)->flags = 0;
  507. skb->protocol = htons(ETH_P_IPV6);
  508. skb->pkt_type = PACKET_HOST;
  509. if ((tunnel->dev->priv_flags & IFF_ISATAP) &&
  510. !isatap_chksrc(skb, iph, tunnel)) {
  511. tunnel->dev->stats.rx_errors++;
  512. rcu_read_unlock();
  513. kfree_skb(skb);
  514. return 0;
  515. }
  516. tstats = this_cpu_ptr(tunnel->dev->tstats);
  517. u64_stats_update_begin(&tstats->syncp);
  518. tstats->rx_packets++;
  519. tstats->rx_bytes += skb->len;
  520. u64_stats_update_end(&tstats->syncp);
  521. __skb_tunnel_rx(skb, tunnel->dev);
  522. ipip6_ecn_decapsulate(iph, skb);
  523. netif_rx(skb);
  524. rcu_read_unlock();
  525. return 0;
  526. }
  527. /* no tunnel matched, let upstream know, ipsec may handle it */
  528. rcu_read_unlock();
  529. return 1;
  530. out:
  531. kfree_skb(skb);
  532. return 0;
  533. }
  534. /*
  535. * Returns the embedded IPv4 address if the IPv6 address
  536. * comes from 6rd / 6to4 (RFC 3056) addr space.
  537. */
  538. static inline
  539. __be32 try_6rd(const struct in6_addr *v6dst, struct ip_tunnel *tunnel)
  540. {
  541. __be32 dst = 0;
  542. #ifdef CONFIG_IPV6_SIT_6RD
  543. if (ipv6_prefix_equal(v6dst, &tunnel->ip6rd.prefix,
  544. tunnel->ip6rd.prefixlen)) {
  545. unsigned int pbw0, pbi0;
  546. int pbi1;
  547. u32 d;
  548. pbw0 = tunnel->ip6rd.prefixlen >> 5;
  549. pbi0 = tunnel->ip6rd.prefixlen & 0x1f;
  550. d = (ntohl(v6dst->s6_addr32[pbw0]) << pbi0) >>
  551. tunnel->ip6rd.relay_prefixlen;
  552. pbi1 = pbi0 - tunnel->ip6rd.relay_prefixlen;
  553. if (pbi1 > 0)
  554. d |= ntohl(v6dst->s6_addr32[pbw0 + 1]) >>
  555. (32 - pbi1);
  556. dst = tunnel->ip6rd.relay_prefix | htonl(d);
  557. }
  558. #else
  559. if (v6dst->s6_addr16[0] == htons(0x2002)) {
  560. /* 6to4 v6 addr has 16 bits prefix, 32 v4addr, 16 SLA, ... */
  561. memcpy(&dst, &v6dst->s6_addr16[1], 4);
  562. }
  563. #endif
  564. return dst;
  565. }
  566. /*
  567. * This function assumes it is being called from dev_queue_xmit()
  568. * and that skb is filled properly by that function.
  569. */
  570. static netdev_tx_t ipip6_tunnel_xmit(struct sk_buff *skb,
  571. struct net_device *dev)
  572. {
  573. struct ip_tunnel *tunnel = netdev_priv(dev);
  574. struct pcpu_tstats *tstats;
  575. const struct iphdr *tiph = &tunnel->parms.iph;
  576. const struct ipv6hdr *iph6 = ipv6_hdr(skb);
  577. u8 tos = tunnel->parms.iph.tos;
  578. __be16 df = tiph->frag_off;
  579. struct rtable *rt; /* Route to the other host */
  580. struct net_device *tdev; /* Device to other host */
  581. struct iphdr *iph; /* Our new IP header */
  582. unsigned int max_headroom; /* The extra header space needed */
  583. __be32 dst = tiph->daddr;
  584. struct flowi4 fl4;
  585. int mtu;
  586. const struct in6_addr *addr6;
  587. int addr_type;
  588. if (skb->protocol != htons(ETH_P_IPV6))
  589. goto tx_error;
  590. if (tos == 1)
  591. tos = ipv6_get_dsfield(iph6);
  592. /* ISATAP (RFC4214) - must come before 6to4 */
  593. if (dev->priv_flags & IFF_ISATAP) {
  594. struct neighbour *neigh = NULL;
  595. bool do_tx_error = false;
  596. if (skb_dst(skb))
  597. neigh = dst_neigh_lookup(skb_dst(skb), &iph6->daddr);
  598. if (neigh == NULL) {
  599. net_dbg_ratelimited("sit: nexthop == NULL\n");
  600. goto tx_error;
  601. }
  602. addr6 = (const struct in6_addr*)&neigh->primary_key;
  603. addr_type = ipv6_addr_type(addr6);
  604. if ((addr_type & IPV6_ADDR_UNICAST) &&
  605. ipv6_addr_is_isatap(addr6))
  606. dst = addr6->s6_addr32[3];
  607. else
  608. do_tx_error = true;
  609. neigh_release(neigh);
  610. if (do_tx_error)
  611. goto tx_error;
  612. }
  613. if (!dst)
  614. dst = try_6rd(&iph6->daddr, tunnel);
  615. if (!dst) {
  616. struct neighbour *neigh = NULL;
  617. bool do_tx_error = false;
  618. if (skb_dst(skb))
  619. neigh = dst_neigh_lookup(skb_dst(skb), &iph6->daddr);
  620. if (neigh == NULL) {
  621. net_dbg_ratelimited("sit: nexthop == NULL\n");
  622. goto tx_error;
  623. }
  624. addr6 = (const struct in6_addr*)&neigh->primary_key;
  625. addr_type = ipv6_addr_type(addr6);
  626. if (addr_type == IPV6_ADDR_ANY) {
  627. addr6 = &ipv6_hdr(skb)->daddr;
  628. addr_type = ipv6_addr_type(addr6);
  629. }
  630. if ((addr_type & IPV6_ADDR_COMPATv4) != 0)
  631. dst = addr6->s6_addr32[3];
  632. else
  633. do_tx_error = true;
  634. neigh_release(neigh);
  635. if (do_tx_error)
  636. goto tx_error;
  637. }
  638. rt = ip_route_output_ports(dev_net(dev), &fl4, NULL,
  639. dst, tiph->saddr,
  640. 0, 0,
  641. IPPROTO_IPV6, RT_TOS(tos),
  642. tunnel->parms.link);
  643. if (IS_ERR(rt)) {
  644. dev->stats.tx_carrier_errors++;
  645. goto tx_error_icmp;
  646. }
  647. if (rt->rt_type != RTN_UNICAST) {
  648. ip_rt_put(rt);
  649. dev->stats.tx_carrier_errors++;
  650. goto tx_error_icmp;
  651. }
  652. tdev = rt->dst.dev;
  653. if (tdev == dev) {
  654. ip_rt_put(rt);
  655. dev->stats.collisions++;
  656. goto tx_error;
  657. }
  658. if (df) {
  659. mtu = dst_mtu(&rt->dst) - sizeof(struct iphdr);
  660. if (mtu < 68) {
  661. dev->stats.collisions++;
  662. ip_rt_put(rt);
  663. goto tx_error;
  664. }
  665. if (mtu < IPV6_MIN_MTU) {
  666. mtu = IPV6_MIN_MTU;
  667. df = 0;
  668. }
  669. if (tunnel->parms.iph.daddr && skb_dst(skb))
  670. skb_dst(skb)->ops->update_pmtu(skb_dst(skb), mtu);
  671. if (skb->len > mtu) {
  672. icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
  673. ip_rt_put(rt);
  674. goto tx_error;
  675. }
  676. }
  677. if (tunnel->err_count > 0) {
  678. if (time_before(jiffies,
  679. tunnel->err_time + IPTUNNEL_ERR_TIMEO)) {
  680. tunnel->err_count--;
  681. dst_link_failure(skb);
  682. } else
  683. tunnel->err_count = 0;
  684. }
  685. /*
  686. * Okay, now see if we can stuff it in the buffer as-is.
  687. */
  688. max_headroom = LL_RESERVED_SPACE(tdev)+sizeof(struct iphdr);
  689. if (skb_headroom(skb) < max_headroom || skb_shared(skb) ||
  690. (skb_cloned(skb) && !skb_clone_writable(skb, 0))) {
  691. struct sk_buff *new_skb = skb_realloc_headroom(skb, max_headroom);
  692. if (!new_skb) {
  693. ip_rt_put(rt);
  694. dev->stats.tx_dropped++;
  695. dev_kfree_skb(skb);
  696. return NETDEV_TX_OK;
  697. }
  698. if (skb->sk)
  699. skb_set_owner_w(new_skb, skb->sk);
  700. dev_kfree_skb(skb);
  701. skb = new_skb;
  702. iph6 = ipv6_hdr(skb);
  703. }
  704. skb->transport_header = skb->network_header;
  705. skb_push(skb, sizeof(struct iphdr));
  706. skb_reset_network_header(skb);
  707. memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
  708. IPCB(skb)->flags = 0;
  709. skb_dst_drop(skb);
  710. skb_dst_set(skb, &rt->dst);
  711. /*
  712. * Push down and install the IPIP header.
  713. */
  714. iph = ip_hdr(skb);
  715. iph->version = 4;
  716. iph->ihl = sizeof(struct iphdr)>>2;
  717. iph->frag_off = df;
  718. iph->protocol = IPPROTO_IPV6;
  719. iph->tos = INET_ECN_encapsulate(tos, ipv6_get_dsfield(iph6));
  720. iph->daddr = fl4.daddr;
  721. iph->saddr = fl4.saddr;
  722. if ((iph->ttl = tiph->ttl) == 0)
  723. iph->ttl = iph6->hop_limit;
  724. nf_reset(skb);
  725. tstats = this_cpu_ptr(dev->tstats);
  726. __IPTUNNEL_XMIT(tstats, &dev->stats);
  727. return NETDEV_TX_OK;
  728. tx_error_icmp:
  729. dst_link_failure(skb);
  730. tx_error:
  731. dev->stats.tx_errors++;
  732. dev_kfree_skb(skb);
  733. return NETDEV_TX_OK;
  734. }
  735. static void ipip6_tunnel_bind_dev(struct net_device *dev)
  736. {
  737. struct net_device *tdev = NULL;
  738. struct ip_tunnel *tunnel;
  739. const struct iphdr *iph;
  740. struct flowi4 fl4;
  741. tunnel = netdev_priv(dev);
  742. iph = &tunnel->parms.iph;
  743. if (iph->daddr) {
  744. struct rtable *rt = ip_route_output_ports(dev_net(dev), &fl4, NULL,
  745. iph->daddr, iph->saddr,
  746. 0, 0,
  747. IPPROTO_IPV6,
  748. RT_TOS(iph->tos),
  749. tunnel->parms.link);
  750. if (!IS_ERR(rt)) {
  751. tdev = rt->dst.dev;
  752. ip_rt_put(rt);
  753. }
  754. dev->flags |= IFF_POINTOPOINT;
  755. }
  756. if (!tdev && tunnel->parms.link)
  757. tdev = __dev_get_by_index(dev_net(dev), tunnel->parms.link);
  758. if (tdev) {
  759. dev->hard_header_len = tdev->hard_header_len + sizeof(struct iphdr);
  760. dev->mtu = tdev->mtu - sizeof(struct iphdr);
  761. if (dev->mtu < IPV6_MIN_MTU)
  762. dev->mtu = IPV6_MIN_MTU;
  763. }
  764. dev->iflink = tunnel->parms.link;
  765. }
  766. static int
  767. ipip6_tunnel_ioctl (struct net_device *dev, struct ifreq *ifr, int cmd)
  768. {
  769. int err = 0;
  770. struct ip_tunnel_parm p;
  771. struct ip_tunnel_prl prl;
  772. struct ip_tunnel *t;
  773. struct net *net = dev_net(dev);
  774. struct sit_net *sitn = net_generic(net, sit_net_id);
  775. #ifdef CONFIG_IPV6_SIT_6RD
  776. struct ip_tunnel_6rd ip6rd;
  777. #endif
  778. switch (cmd) {
  779. case SIOCGETTUNNEL:
  780. #ifdef CONFIG_IPV6_SIT_6RD
  781. case SIOCGET6RD:
  782. #endif
  783. t = NULL;
  784. if (dev == sitn->fb_tunnel_dev) {
  785. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) {
  786. err = -EFAULT;
  787. break;
  788. }
  789. t = ipip6_tunnel_locate(net, &p, 0);
  790. }
  791. if (t == NULL)
  792. t = netdev_priv(dev);
  793. err = -EFAULT;
  794. if (cmd == SIOCGETTUNNEL) {
  795. memcpy(&p, &t->parms, sizeof(p));
  796. if (copy_to_user(ifr->ifr_ifru.ifru_data, &p,
  797. sizeof(p)))
  798. goto done;
  799. #ifdef CONFIG_IPV6_SIT_6RD
  800. } else {
  801. ip6rd.prefix = t->ip6rd.prefix;
  802. ip6rd.relay_prefix = t->ip6rd.relay_prefix;
  803. ip6rd.prefixlen = t->ip6rd.prefixlen;
  804. ip6rd.relay_prefixlen = t->ip6rd.relay_prefixlen;
  805. if (copy_to_user(ifr->ifr_ifru.ifru_data, &ip6rd,
  806. sizeof(ip6rd)))
  807. goto done;
  808. #endif
  809. }
  810. err = 0;
  811. break;
  812. case SIOCADDTUNNEL:
  813. case SIOCCHGTUNNEL:
  814. err = -EPERM;
  815. if (!capable(CAP_NET_ADMIN))
  816. goto done;
  817. err = -EFAULT;
  818. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
  819. goto done;
  820. err = -EINVAL;
  821. if (p.iph.version != 4 || p.iph.protocol != IPPROTO_IPV6 ||
  822. p.iph.ihl != 5 || (p.iph.frag_off&htons(~IP_DF)))
  823. goto done;
  824. if (p.iph.ttl)
  825. p.iph.frag_off |= htons(IP_DF);
  826. t = ipip6_tunnel_locate(net, &p, cmd == SIOCADDTUNNEL);
  827. if (dev != sitn->fb_tunnel_dev && cmd == SIOCCHGTUNNEL) {
  828. if (t != NULL) {
  829. if (t->dev != dev) {
  830. err = -EEXIST;
  831. break;
  832. }
  833. } else {
  834. if (((dev->flags&IFF_POINTOPOINT) && !p.iph.daddr) ||
  835. (!(dev->flags&IFF_POINTOPOINT) && p.iph.daddr)) {
  836. err = -EINVAL;
  837. break;
  838. }
  839. t = netdev_priv(dev);
  840. ipip6_tunnel_unlink(sitn, t);
  841. synchronize_net();
  842. t->parms.iph.saddr = p.iph.saddr;
  843. t->parms.iph.daddr = p.iph.daddr;
  844. memcpy(dev->dev_addr, &p.iph.saddr, 4);
  845. memcpy(dev->broadcast, &p.iph.daddr, 4);
  846. ipip6_tunnel_link(sitn, t);
  847. netdev_state_change(dev);
  848. }
  849. }
  850. if (t) {
  851. err = 0;
  852. if (cmd == SIOCCHGTUNNEL) {
  853. t->parms.iph.ttl = p.iph.ttl;
  854. t->parms.iph.tos = p.iph.tos;
  855. if (t->parms.link != p.link) {
  856. t->parms.link = p.link;
  857. ipip6_tunnel_bind_dev(dev);
  858. netdev_state_change(dev);
  859. }
  860. }
  861. if (copy_to_user(ifr->ifr_ifru.ifru_data, &t->parms, sizeof(p)))
  862. err = -EFAULT;
  863. } else
  864. err = (cmd == SIOCADDTUNNEL ? -ENOBUFS : -ENOENT);
  865. break;
  866. case SIOCDELTUNNEL:
  867. err = -EPERM;
  868. if (!capable(CAP_NET_ADMIN))
  869. goto done;
  870. if (dev == sitn->fb_tunnel_dev) {
  871. err = -EFAULT;
  872. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
  873. goto done;
  874. err = -ENOENT;
  875. if ((t = ipip6_tunnel_locate(net, &p, 0)) == NULL)
  876. goto done;
  877. err = -EPERM;
  878. if (t == netdev_priv(sitn->fb_tunnel_dev))
  879. goto done;
  880. dev = t->dev;
  881. }
  882. unregister_netdevice(dev);
  883. err = 0;
  884. break;
  885. case SIOCGETPRL:
  886. err = -EINVAL;
  887. if (dev == sitn->fb_tunnel_dev)
  888. goto done;
  889. err = -ENOENT;
  890. if (!(t = netdev_priv(dev)))
  891. goto done;
  892. err = ipip6_tunnel_get_prl(t, ifr->ifr_ifru.ifru_data);
  893. break;
  894. case SIOCADDPRL:
  895. case SIOCDELPRL:
  896. case SIOCCHGPRL:
  897. err = -EPERM;
  898. if (!capable(CAP_NET_ADMIN))
  899. goto done;
  900. err = -EINVAL;
  901. if (dev == sitn->fb_tunnel_dev)
  902. goto done;
  903. err = -EFAULT;
  904. if (copy_from_user(&prl, ifr->ifr_ifru.ifru_data, sizeof(prl)))
  905. goto done;
  906. err = -ENOENT;
  907. if (!(t = netdev_priv(dev)))
  908. goto done;
  909. switch (cmd) {
  910. case SIOCDELPRL:
  911. err = ipip6_tunnel_del_prl(t, &prl);
  912. break;
  913. case SIOCADDPRL:
  914. case SIOCCHGPRL:
  915. err = ipip6_tunnel_add_prl(t, &prl, cmd == SIOCCHGPRL);
  916. break;
  917. }
  918. netdev_state_change(dev);
  919. break;
  920. #ifdef CONFIG_IPV6_SIT_6RD
  921. case SIOCADD6RD:
  922. case SIOCCHG6RD:
  923. case SIOCDEL6RD:
  924. err = -EPERM;
  925. if (!capable(CAP_NET_ADMIN))
  926. goto done;
  927. err = -EFAULT;
  928. if (copy_from_user(&ip6rd, ifr->ifr_ifru.ifru_data,
  929. sizeof(ip6rd)))
  930. goto done;
  931. t = netdev_priv(dev);
  932. if (cmd != SIOCDEL6RD) {
  933. struct in6_addr prefix;
  934. __be32 relay_prefix;
  935. err = -EINVAL;
  936. if (ip6rd.relay_prefixlen > 32 ||
  937. ip6rd.prefixlen + (32 - ip6rd.relay_prefixlen) > 64)
  938. goto done;
  939. ipv6_addr_prefix(&prefix, &ip6rd.prefix,
  940. ip6rd.prefixlen);
  941. if (!ipv6_addr_equal(&prefix, &ip6rd.prefix))
  942. goto done;
  943. if (ip6rd.relay_prefixlen)
  944. relay_prefix = ip6rd.relay_prefix &
  945. htonl(0xffffffffUL <<
  946. (32 - ip6rd.relay_prefixlen));
  947. else
  948. relay_prefix = 0;
  949. if (relay_prefix != ip6rd.relay_prefix)
  950. goto done;
  951. t->ip6rd.prefix = prefix;
  952. t->ip6rd.relay_prefix = relay_prefix;
  953. t->ip6rd.prefixlen = ip6rd.prefixlen;
  954. t->ip6rd.relay_prefixlen = ip6rd.relay_prefixlen;
  955. } else
  956. ipip6_tunnel_clone_6rd(dev, sitn);
  957. err = 0;
  958. break;
  959. #endif
  960. default:
  961. err = -EINVAL;
  962. }
  963. done:
  964. return err;
  965. }
  966. static int ipip6_tunnel_change_mtu(struct net_device *dev, int new_mtu)
  967. {
  968. if (new_mtu < IPV6_MIN_MTU || new_mtu > 0xFFF8 - sizeof(struct iphdr))
  969. return -EINVAL;
  970. dev->mtu = new_mtu;
  971. return 0;
  972. }
  973. static const struct net_device_ops ipip6_netdev_ops = {
  974. .ndo_uninit = ipip6_tunnel_uninit,
  975. .ndo_start_xmit = ipip6_tunnel_xmit,
  976. .ndo_do_ioctl = ipip6_tunnel_ioctl,
  977. .ndo_change_mtu = ipip6_tunnel_change_mtu,
  978. .ndo_get_stats64= ipip6_get_stats64,
  979. };
  980. static void ipip6_dev_free(struct net_device *dev)
  981. {
  982. free_percpu(dev->tstats);
  983. free_netdev(dev);
  984. }
  985. static void ipip6_tunnel_setup(struct net_device *dev)
  986. {
  987. dev->netdev_ops = &ipip6_netdev_ops;
  988. dev->destructor = ipip6_dev_free;
  989. dev->type = ARPHRD_SIT;
  990. dev->hard_header_len = LL_MAX_HEADER + sizeof(struct iphdr);
  991. dev->mtu = ETH_DATA_LEN - sizeof(struct iphdr);
  992. dev->flags = IFF_NOARP;
  993. dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
  994. dev->iflink = 0;
  995. dev->addr_len = 4;
  996. dev->features |= NETIF_F_NETNS_LOCAL;
  997. dev->features |= NETIF_F_LLTX;
  998. }
  999. static int ipip6_tunnel_init(struct net_device *dev)
  1000. {
  1001. struct ip_tunnel *tunnel = netdev_priv(dev);
  1002. int i;
  1003. tunnel->dev = dev;
  1004. memcpy(dev->dev_addr, &tunnel->parms.iph.saddr, 4);
  1005. memcpy(dev->broadcast, &tunnel->parms.iph.daddr, 4);
  1006. ipip6_tunnel_bind_dev(dev);
  1007. dev->tstats = alloc_percpu(struct pcpu_tstats);
  1008. if (!dev->tstats)
  1009. return -ENOMEM;
  1010. for_each_possible_cpu(i) {
  1011. struct pcpu_tstats *ipip6_tunnel_stats;
  1012. ipip6_tunnel_stats = per_cpu_ptr(dev->tstats, i);
  1013. u64_stats_init(&ipip6_tunnel_stats->syncp);
  1014. }
  1015. return 0;
  1016. }
  1017. static int __net_init ipip6_fb_tunnel_init(struct net_device *dev)
  1018. {
  1019. struct ip_tunnel *tunnel = netdev_priv(dev);
  1020. struct iphdr *iph = &tunnel->parms.iph;
  1021. struct net *net = dev_net(dev);
  1022. struct sit_net *sitn = net_generic(net, sit_net_id);
  1023. int i;
  1024. tunnel->dev = dev;
  1025. strcpy(tunnel->parms.name, dev->name);
  1026. iph->version = 4;
  1027. iph->protocol = IPPROTO_IPV6;
  1028. iph->ihl = 5;
  1029. iph->ttl = 64;
  1030. dev->tstats = alloc_percpu(struct pcpu_tstats);
  1031. if (!dev->tstats)
  1032. return -ENOMEM;
  1033. for_each_possible_cpu(i) {
  1034. struct pcpu_tstats *ipip6_fb_stats;
  1035. ipip6_fb_stats = per_cpu_ptr(dev->tstats, i);
  1036. u64_stats_init(&ipip6_fb_stats->syncp);
  1037. }
  1038. dev_hold(dev);
  1039. rcu_assign_pointer(sitn->tunnels_wc[0], tunnel);
  1040. return 0;
  1041. }
  1042. static struct xfrm_tunnel sit_handler __read_mostly = {
  1043. .handler = ipip6_rcv,
  1044. .err_handler = ipip6_err,
  1045. .priority = 1,
  1046. };
  1047. static void __net_exit sit_destroy_tunnels(struct sit_net *sitn, struct list_head *head)
  1048. {
  1049. int prio;
  1050. for (prio = 1; prio < 4; prio++) {
  1051. int h;
  1052. for (h = 0; h < HASH_SIZE; h++) {
  1053. struct ip_tunnel *t;
  1054. t = rtnl_dereference(sitn->tunnels[prio][h]);
  1055. while (t != NULL) {
  1056. unregister_netdevice_queue(t->dev, head);
  1057. t = rtnl_dereference(t->next);
  1058. }
  1059. }
  1060. }
  1061. }
  1062. static int __net_init sit_init_net(struct net *net)
  1063. {
  1064. struct sit_net *sitn = net_generic(net, sit_net_id);
  1065. struct ip_tunnel *t;
  1066. int err;
  1067. sitn->tunnels[0] = sitn->tunnels_wc;
  1068. sitn->tunnels[1] = sitn->tunnels_l;
  1069. sitn->tunnels[2] = sitn->tunnels_r;
  1070. sitn->tunnels[3] = sitn->tunnels_r_l;
  1071. sitn->fb_tunnel_dev = alloc_netdev(sizeof(struct ip_tunnel), "sit0",
  1072. ipip6_tunnel_setup);
  1073. if (!sitn->fb_tunnel_dev) {
  1074. err = -ENOMEM;
  1075. goto err_alloc_dev;
  1076. }
  1077. dev_net_set(sitn->fb_tunnel_dev, net);
  1078. err = ipip6_fb_tunnel_init(sitn->fb_tunnel_dev);
  1079. if (err)
  1080. goto err_dev_free;
  1081. ipip6_tunnel_clone_6rd(sitn->fb_tunnel_dev, sitn);
  1082. if ((err = register_netdev(sitn->fb_tunnel_dev)))
  1083. goto err_reg_dev;
  1084. t = netdev_priv(sitn->fb_tunnel_dev);
  1085. strcpy(t->parms.name, sitn->fb_tunnel_dev->name);
  1086. return 0;
  1087. err_reg_dev:
  1088. dev_put(sitn->fb_tunnel_dev);
  1089. err_dev_free:
  1090. ipip6_dev_free(sitn->fb_tunnel_dev);
  1091. err_alloc_dev:
  1092. return err;
  1093. }
  1094. static void __net_exit sit_exit_net(struct net *net)
  1095. {
  1096. struct sit_net *sitn = net_generic(net, sit_net_id);
  1097. LIST_HEAD(list);
  1098. rtnl_lock();
  1099. sit_destroy_tunnels(sitn, &list);
  1100. unregister_netdevice_queue(sitn->fb_tunnel_dev, &list);
  1101. unregister_netdevice_many(&list);
  1102. rtnl_unlock();
  1103. }
  1104. static struct pernet_operations sit_net_ops = {
  1105. .init = sit_init_net,
  1106. .exit = sit_exit_net,
  1107. .id = &sit_net_id,
  1108. .size = sizeof(struct sit_net),
  1109. };
  1110. static void __exit sit_cleanup(void)
  1111. {
  1112. xfrm4_tunnel_deregister(&sit_handler, AF_INET6);
  1113. unregister_pernet_device(&sit_net_ops);
  1114. rcu_barrier(); /* Wait for completion of call_rcu()'s */
  1115. }
  1116. static int __init sit_init(void)
  1117. {
  1118. int err;
  1119. printk(KERN_INFO "IPv6 over IPv4 tunneling driver\n");
  1120. err = register_pernet_device(&sit_net_ops);
  1121. if (err < 0)
  1122. return err;
  1123. err = xfrm4_tunnel_register(&sit_handler, AF_INET6);
  1124. if (err < 0) {
  1125. unregister_pernet_device(&sit_net_ops);
  1126. printk(KERN_INFO "sit init: Can't add protocol\n");
  1127. }
  1128. return err;
  1129. }
  1130. module_init(sit_init);
  1131. module_exit(sit_cleanup);
  1132. MODULE_LICENSE("GPL");
  1133. MODULE_ALIAS_RTNL_LINK("sit");
  1134. MODULE_ALIAS_NETDEV("sit0");