sit.c 31 KB

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