sit.c 30 KB

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