sit.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337
  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_bh(&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_bh(&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. case ICMP_REDIRECT:
  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)
  473. goto out;
  474. if (type == ICMP_DEST_UNREACH && code == ICMP_FRAG_NEEDED) {
  475. ipv4_update_pmtu(skb, dev_net(skb->dev), info,
  476. t->dev->ifindex, 0, IPPROTO_IPV6, 0);
  477. err = 0;
  478. goto out;
  479. }
  480. if (type == ICMP_REDIRECT) {
  481. ipv4_redirect(skb, dev_net(skb->dev), t->dev->ifindex, 0,
  482. IPPROTO_IPV6, 0);
  483. err = 0;
  484. goto out;
  485. }
  486. if (t->parms.iph.daddr == 0)
  487. goto out;
  488. err = 0;
  489. if (t->parms.iph.ttl == 0 && type == ICMP_TIME_EXCEEDED)
  490. goto out;
  491. if (time_before(jiffies, t->err_time + IPTUNNEL_ERR_TIMEO))
  492. t->err_count++;
  493. else
  494. t->err_count = 1;
  495. t->err_time = jiffies;
  496. out:
  497. rcu_read_unlock();
  498. return err;
  499. }
  500. static inline void ipip6_ecn_decapsulate(const struct iphdr *iph, struct sk_buff *skb)
  501. {
  502. if (INET_ECN_is_ce(iph->tos))
  503. IP6_ECN_set_ce(ipv6_hdr(skb));
  504. }
  505. static int ipip6_rcv(struct sk_buff *skb)
  506. {
  507. const struct iphdr *iph;
  508. struct ip_tunnel *tunnel;
  509. if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
  510. goto out;
  511. iph = ip_hdr(skb);
  512. rcu_read_lock();
  513. tunnel = ipip6_tunnel_lookup(dev_net(skb->dev), skb->dev,
  514. iph->saddr, iph->daddr);
  515. if (tunnel != NULL) {
  516. struct pcpu_tstats *tstats;
  517. secpath_reset(skb);
  518. skb->mac_header = skb->network_header;
  519. skb_reset_network_header(skb);
  520. IPCB(skb)->flags = 0;
  521. skb->protocol = htons(ETH_P_IPV6);
  522. skb->pkt_type = PACKET_HOST;
  523. if ((tunnel->dev->priv_flags & IFF_ISATAP) &&
  524. !isatap_chksrc(skb, iph, tunnel)) {
  525. tunnel->dev->stats.rx_errors++;
  526. rcu_read_unlock();
  527. kfree_skb(skb);
  528. return 0;
  529. }
  530. tstats = this_cpu_ptr(tunnel->dev->tstats);
  531. tstats->rx_packets++;
  532. tstats->rx_bytes += skb->len;
  533. __skb_tunnel_rx(skb, tunnel->dev);
  534. ipip6_ecn_decapsulate(iph, skb);
  535. netif_rx(skb);
  536. rcu_read_unlock();
  537. return 0;
  538. }
  539. /* no tunnel matched, let upstream know, ipsec may handle it */
  540. rcu_read_unlock();
  541. return 1;
  542. out:
  543. kfree_skb(skb);
  544. return 0;
  545. }
  546. /*
  547. * Returns the embedded IPv4 address if the IPv6 address
  548. * comes from 6rd / 6to4 (RFC 3056) addr space.
  549. */
  550. static inline
  551. __be32 try_6rd(const struct in6_addr *v6dst, struct ip_tunnel *tunnel)
  552. {
  553. __be32 dst = 0;
  554. #ifdef CONFIG_IPV6_SIT_6RD
  555. if (ipv6_prefix_equal(v6dst, &tunnel->ip6rd.prefix,
  556. tunnel->ip6rd.prefixlen)) {
  557. unsigned int pbw0, pbi0;
  558. int pbi1;
  559. u32 d;
  560. pbw0 = tunnel->ip6rd.prefixlen >> 5;
  561. pbi0 = tunnel->ip6rd.prefixlen & 0x1f;
  562. d = (ntohl(v6dst->s6_addr32[pbw0]) << pbi0) >>
  563. tunnel->ip6rd.relay_prefixlen;
  564. pbi1 = pbi0 - tunnel->ip6rd.relay_prefixlen;
  565. if (pbi1 > 0)
  566. d |= ntohl(v6dst->s6_addr32[pbw0 + 1]) >>
  567. (32 - pbi1);
  568. dst = tunnel->ip6rd.relay_prefix | htonl(d);
  569. }
  570. #else
  571. if (v6dst->s6_addr16[0] == htons(0x2002)) {
  572. /* 6to4 v6 addr has 16 bits prefix, 32 v4addr, 16 SLA, ... */
  573. memcpy(&dst, &v6dst->s6_addr16[1], 4);
  574. }
  575. #endif
  576. return dst;
  577. }
  578. /*
  579. * This function assumes it is being called from dev_queue_xmit()
  580. * and that skb is filled properly by that function.
  581. */
  582. static netdev_tx_t ipip6_tunnel_xmit(struct sk_buff *skb,
  583. struct net_device *dev)
  584. {
  585. struct ip_tunnel *tunnel = netdev_priv(dev);
  586. struct pcpu_tstats *tstats;
  587. const struct iphdr *tiph = &tunnel->parms.iph;
  588. const struct ipv6hdr *iph6 = ipv6_hdr(skb);
  589. u8 tos = tunnel->parms.iph.tos;
  590. __be16 df = tiph->frag_off;
  591. struct rtable *rt; /* Route to the other host */
  592. struct net_device *tdev; /* Device to other host */
  593. struct iphdr *iph; /* Our new IP header */
  594. unsigned int max_headroom; /* The extra header space needed */
  595. __be32 dst = tiph->daddr;
  596. struct flowi4 fl4;
  597. int mtu;
  598. const struct in6_addr *addr6;
  599. int addr_type;
  600. if (skb->protocol != htons(ETH_P_IPV6))
  601. goto tx_error;
  602. if (tos == 1)
  603. tos = ipv6_get_dsfield(iph6);
  604. /* ISATAP (RFC4214) - must come before 6to4 */
  605. if (dev->priv_flags & IFF_ISATAP) {
  606. struct neighbour *neigh = NULL;
  607. bool do_tx_error = false;
  608. if (skb_dst(skb))
  609. neigh = dst_neigh_lookup(skb_dst(skb), &iph6->daddr);
  610. if (neigh == NULL) {
  611. net_dbg_ratelimited("sit: nexthop == NULL\n");
  612. goto tx_error;
  613. }
  614. addr6 = (const struct in6_addr *)&neigh->primary_key;
  615. addr_type = ipv6_addr_type(addr6);
  616. if ((addr_type & IPV6_ADDR_UNICAST) &&
  617. ipv6_addr_is_isatap(addr6))
  618. dst = addr6->s6_addr32[3];
  619. else
  620. do_tx_error = true;
  621. neigh_release(neigh);
  622. if (do_tx_error)
  623. goto tx_error;
  624. }
  625. if (!dst)
  626. dst = try_6rd(&iph6->daddr, tunnel);
  627. if (!dst) {
  628. struct neighbour *neigh = NULL;
  629. bool do_tx_error = false;
  630. if (skb_dst(skb))
  631. neigh = dst_neigh_lookup(skb_dst(skb), &iph6->daddr);
  632. if (neigh == NULL) {
  633. net_dbg_ratelimited("sit: nexthop == NULL\n");
  634. goto tx_error;
  635. }
  636. addr6 = (const struct in6_addr *)&neigh->primary_key;
  637. addr_type = ipv6_addr_type(addr6);
  638. if (addr_type == IPV6_ADDR_ANY) {
  639. addr6 = &ipv6_hdr(skb)->daddr;
  640. addr_type = ipv6_addr_type(addr6);
  641. }
  642. if ((addr_type & IPV6_ADDR_COMPATv4) != 0)
  643. dst = addr6->s6_addr32[3];
  644. else
  645. do_tx_error = true;
  646. neigh_release(neigh);
  647. if (do_tx_error)
  648. goto tx_error;
  649. }
  650. rt = ip_route_output_ports(dev_net(dev), &fl4, NULL,
  651. dst, tiph->saddr,
  652. 0, 0,
  653. IPPROTO_IPV6, RT_TOS(tos),
  654. tunnel->parms.link);
  655. if (IS_ERR(rt)) {
  656. dev->stats.tx_carrier_errors++;
  657. goto tx_error_icmp;
  658. }
  659. if (rt->rt_type != RTN_UNICAST) {
  660. ip_rt_put(rt);
  661. dev->stats.tx_carrier_errors++;
  662. goto tx_error_icmp;
  663. }
  664. tdev = rt->dst.dev;
  665. if (tdev == dev) {
  666. ip_rt_put(rt);
  667. dev->stats.collisions++;
  668. goto tx_error;
  669. }
  670. if (df) {
  671. mtu = dst_mtu(&rt->dst) - sizeof(struct iphdr);
  672. if (mtu < 68) {
  673. dev->stats.collisions++;
  674. ip_rt_put(rt);
  675. goto tx_error;
  676. }
  677. if (mtu < IPV6_MIN_MTU) {
  678. mtu = IPV6_MIN_MTU;
  679. df = 0;
  680. }
  681. if (tunnel->parms.iph.daddr && skb_dst(skb))
  682. skb_dst(skb)->ops->update_pmtu(skb_dst(skb), NULL, skb, mtu);
  683. if (skb->len > mtu) {
  684. icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
  685. ip_rt_put(rt);
  686. goto tx_error;
  687. }
  688. }
  689. if (tunnel->err_count > 0) {
  690. if (time_before(jiffies,
  691. tunnel->err_time + IPTUNNEL_ERR_TIMEO)) {
  692. tunnel->err_count--;
  693. dst_link_failure(skb);
  694. } else
  695. tunnel->err_count = 0;
  696. }
  697. /*
  698. * Okay, now see if we can stuff it in the buffer as-is.
  699. */
  700. max_headroom = LL_RESERVED_SPACE(tdev)+sizeof(struct iphdr);
  701. if (skb_headroom(skb) < max_headroom || skb_shared(skb) ||
  702. (skb_cloned(skb) && !skb_clone_writable(skb, 0))) {
  703. struct sk_buff *new_skb = skb_realloc_headroom(skb, max_headroom);
  704. if (!new_skb) {
  705. ip_rt_put(rt);
  706. dev->stats.tx_dropped++;
  707. dev_kfree_skb(skb);
  708. return NETDEV_TX_OK;
  709. }
  710. if (skb->sk)
  711. skb_set_owner_w(new_skb, skb->sk);
  712. dev_kfree_skb(skb);
  713. skb = new_skb;
  714. iph6 = ipv6_hdr(skb);
  715. }
  716. skb->transport_header = skb->network_header;
  717. skb_push(skb, sizeof(struct iphdr));
  718. skb_reset_network_header(skb);
  719. memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
  720. IPCB(skb)->flags = 0;
  721. skb_dst_drop(skb);
  722. skb_dst_set(skb, &rt->dst);
  723. /*
  724. * Push down and install the IPIP header.
  725. */
  726. iph = ip_hdr(skb);
  727. iph->version = 4;
  728. iph->ihl = sizeof(struct iphdr)>>2;
  729. iph->frag_off = df;
  730. iph->protocol = IPPROTO_IPV6;
  731. iph->tos = INET_ECN_encapsulate(tos, ipv6_get_dsfield(iph6));
  732. iph->daddr = fl4.daddr;
  733. iph->saddr = fl4.saddr;
  734. if ((iph->ttl = tiph->ttl) == 0)
  735. iph->ttl = iph6->hop_limit;
  736. nf_reset(skb);
  737. tstats = this_cpu_ptr(dev->tstats);
  738. __IPTUNNEL_XMIT(tstats, &dev->stats);
  739. return NETDEV_TX_OK;
  740. tx_error_icmp:
  741. dst_link_failure(skb);
  742. tx_error:
  743. dev->stats.tx_errors++;
  744. dev_kfree_skb(skb);
  745. return NETDEV_TX_OK;
  746. }
  747. static void ipip6_tunnel_bind_dev(struct net_device *dev)
  748. {
  749. struct net_device *tdev = NULL;
  750. struct ip_tunnel *tunnel;
  751. const struct iphdr *iph;
  752. struct flowi4 fl4;
  753. tunnel = netdev_priv(dev);
  754. iph = &tunnel->parms.iph;
  755. if (iph->daddr) {
  756. struct rtable *rt = ip_route_output_ports(dev_net(dev), &fl4, NULL,
  757. iph->daddr, iph->saddr,
  758. 0, 0,
  759. IPPROTO_IPV6,
  760. RT_TOS(iph->tos),
  761. tunnel->parms.link);
  762. if (!IS_ERR(rt)) {
  763. tdev = rt->dst.dev;
  764. ip_rt_put(rt);
  765. }
  766. dev->flags |= IFF_POINTOPOINT;
  767. }
  768. if (!tdev && tunnel->parms.link)
  769. tdev = __dev_get_by_index(dev_net(dev), tunnel->parms.link);
  770. if (tdev) {
  771. dev->hard_header_len = tdev->hard_header_len + sizeof(struct iphdr);
  772. dev->mtu = tdev->mtu - sizeof(struct iphdr);
  773. if (dev->mtu < IPV6_MIN_MTU)
  774. dev->mtu = IPV6_MIN_MTU;
  775. }
  776. dev->iflink = tunnel->parms.link;
  777. }
  778. static int
  779. ipip6_tunnel_ioctl (struct net_device *dev, struct ifreq *ifr, int cmd)
  780. {
  781. int err = 0;
  782. struct ip_tunnel_parm p;
  783. struct ip_tunnel_prl prl;
  784. struct ip_tunnel *t;
  785. struct net *net = dev_net(dev);
  786. struct sit_net *sitn = net_generic(net, sit_net_id);
  787. #ifdef CONFIG_IPV6_SIT_6RD
  788. struct ip_tunnel_6rd ip6rd;
  789. #endif
  790. switch (cmd) {
  791. case SIOCGETTUNNEL:
  792. #ifdef CONFIG_IPV6_SIT_6RD
  793. case SIOCGET6RD:
  794. #endif
  795. t = NULL;
  796. if (dev == sitn->fb_tunnel_dev) {
  797. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) {
  798. err = -EFAULT;
  799. break;
  800. }
  801. t = ipip6_tunnel_locate(net, &p, 0);
  802. }
  803. if (t == NULL)
  804. t = netdev_priv(dev);
  805. err = -EFAULT;
  806. if (cmd == SIOCGETTUNNEL) {
  807. memcpy(&p, &t->parms, sizeof(p));
  808. if (copy_to_user(ifr->ifr_ifru.ifru_data, &p,
  809. sizeof(p)))
  810. goto done;
  811. #ifdef CONFIG_IPV6_SIT_6RD
  812. } else {
  813. ip6rd.prefix = t->ip6rd.prefix;
  814. ip6rd.relay_prefix = t->ip6rd.relay_prefix;
  815. ip6rd.prefixlen = t->ip6rd.prefixlen;
  816. ip6rd.relay_prefixlen = t->ip6rd.relay_prefixlen;
  817. if (copy_to_user(ifr->ifr_ifru.ifru_data, &ip6rd,
  818. sizeof(ip6rd)))
  819. goto done;
  820. #endif
  821. }
  822. err = 0;
  823. break;
  824. case SIOCADDTUNNEL:
  825. case SIOCCHGTUNNEL:
  826. err = -EPERM;
  827. if (!capable(CAP_NET_ADMIN))
  828. goto done;
  829. err = -EFAULT;
  830. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
  831. goto done;
  832. err = -EINVAL;
  833. if (p.iph.version != 4 || p.iph.protocol != IPPROTO_IPV6 ||
  834. p.iph.ihl != 5 || (p.iph.frag_off&htons(~IP_DF)))
  835. goto done;
  836. if (p.iph.ttl)
  837. p.iph.frag_off |= htons(IP_DF);
  838. t = ipip6_tunnel_locate(net, &p, cmd == SIOCADDTUNNEL);
  839. if (dev != sitn->fb_tunnel_dev && cmd == SIOCCHGTUNNEL) {
  840. if (t != NULL) {
  841. if (t->dev != dev) {
  842. err = -EEXIST;
  843. break;
  844. }
  845. } else {
  846. if (((dev->flags&IFF_POINTOPOINT) && !p.iph.daddr) ||
  847. (!(dev->flags&IFF_POINTOPOINT) && p.iph.daddr)) {
  848. err = -EINVAL;
  849. break;
  850. }
  851. t = netdev_priv(dev);
  852. ipip6_tunnel_unlink(sitn, t);
  853. synchronize_net();
  854. t->parms.iph.saddr = p.iph.saddr;
  855. t->parms.iph.daddr = p.iph.daddr;
  856. memcpy(dev->dev_addr, &p.iph.saddr, 4);
  857. memcpy(dev->broadcast, &p.iph.daddr, 4);
  858. ipip6_tunnel_link(sitn, t);
  859. netdev_state_change(dev);
  860. }
  861. }
  862. if (t) {
  863. err = 0;
  864. if (cmd == SIOCCHGTUNNEL) {
  865. t->parms.iph.ttl = p.iph.ttl;
  866. t->parms.iph.tos = p.iph.tos;
  867. if (t->parms.link != p.link) {
  868. t->parms.link = p.link;
  869. ipip6_tunnel_bind_dev(dev);
  870. netdev_state_change(dev);
  871. }
  872. }
  873. if (copy_to_user(ifr->ifr_ifru.ifru_data, &t->parms, sizeof(p)))
  874. err = -EFAULT;
  875. } else
  876. err = (cmd == SIOCADDTUNNEL ? -ENOBUFS : -ENOENT);
  877. break;
  878. case SIOCDELTUNNEL:
  879. err = -EPERM;
  880. if (!capable(CAP_NET_ADMIN))
  881. goto done;
  882. if (dev == sitn->fb_tunnel_dev) {
  883. err = -EFAULT;
  884. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
  885. goto done;
  886. err = -ENOENT;
  887. if ((t = ipip6_tunnel_locate(net, &p, 0)) == NULL)
  888. goto done;
  889. err = -EPERM;
  890. if (t == netdev_priv(sitn->fb_tunnel_dev))
  891. goto done;
  892. dev = t->dev;
  893. }
  894. unregister_netdevice(dev);
  895. err = 0;
  896. break;
  897. case SIOCGETPRL:
  898. err = -EINVAL;
  899. if (dev == sitn->fb_tunnel_dev)
  900. goto done;
  901. err = -ENOENT;
  902. if (!(t = netdev_priv(dev)))
  903. goto done;
  904. err = ipip6_tunnel_get_prl(t, ifr->ifr_ifru.ifru_data);
  905. break;
  906. case SIOCADDPRL:
  907. case SIOCDELPRL:
  908. case SIOCCHGPRL:
  909. err = -EPERM;
  910. if (!capable(CAP_NET_ADMIN))
  911. goto done;
  912. err = -EINVAL;
  913. if (dev == sitn->fb_tunnel_dev)
  914. goto done;
  915. err = -EFAULT;
  916. if (copy_from_user(&prl, ifr->ifr_ifru.ifru_data, sizeof(prl)))
  917. goto done;
  918. err = -ENOENT;
  919. if (!(t = netdev_priv(dev)))
  920. goto done;
  921. switch (cmd) {
  922. case SIOCDELPRL:
  923. err = ipip6_tunnel_del_prl(t, &prl);
  924. break;
  925. case SIOCADDPRL:
  926. case SIOCCHGPRL:
  927. err = ipip6_tunnel_add_prl(t, &prl, cmd == SIOCCHGPRL);
  928. break;
  929. }
  930. netdev_state_change(dev);
  931. break;
  932. #ifdef CONFIG_IPV6_SIT_6RD
  933. case SIOCADD6RD:
  934. case SIOCCHG6RD:
  935. case SIOCDEL6RD:
  936. err = -EPERM;
  937. if (!capable(CAP_NET_ADMIN))
  938. goto done;
  939. err = -EFAULT;
  940. if (copy_from_user(&ip6rd, ifr->ifr_ifru.ifru_data,
  941. sizeof(ip6rd)))
  942. goto done;
  943. t = netdev_priv(dev);
  944. if (cmd != SIOCDEL6RD) {
  945. struct in6_addr prefix;
  946. __be32 relay_prefix;
  947. err = -EINVAL;
  948. if (ip6rd.relay_prefixlen > 32 ||
  949. ip6rd.prefixlen + (32 - ip6rd.relay_prefixlen) > 64)
  950. goto done;
  951. ipv6_addr_prefix(&prefix, &ip6rd.prefix,
  952. ip6rd.prefixlen);
  953. if (!ipv6_addr_equal(&prefix, &ip6rd.prefix))
  954. goto done;
  955. if (ip6rd.relay_prefixlen)
  956. relay_prefix = ip6rd.relay_prefix &
  957. htonl(0xffffffffUL <<
  958. (32 - ip6rd.relay_prefixlen));
  959. else
  960. relay_prefix = 0;
  961. if (relay_prefix != ip6rd.relay_prefix)
  962. goto done;
  963. t->ip6rd.prefix = prefix;
  964. t->ip6rd.relay_prefix = relay_prefix;
  965. t->ip6rd.prefixlen = ip6rd.prefixlen;
  966. t->ip6rd.relay_prefixlen = ip6rd.relay_prefixlen;
  967. } else
  968. ipip6_tunnel_clone_6rd(dev, sitn);
  969. err = 0;
  970. break;
  971. #endif
  972. default:
  973. err = -EINVAL;
  974. }
  975. done:
  976. return err;
  977. }
  978. static int ipip6_tunnel_change_mtu(struct net_device *dev, int new_mtu)
  979. {
  980. if (new_mtu < IPV6_MIN_MTU || new_mtu > 0xFFF8 - sizeof(struct iphdr))
  981. return -EINVAL;
  982. dev->mtu = new_mtu;
  983. return 0;
  984. }
  985. static const struct net_device_ops ipip6_netdev_ops = {
  986. .ndo_uninit = ipip6_tunnel_uninit,
  987. .ndo_start_xmit = ipip6_tunnel_xmit,
  988. .ndo_do_ioctl = ipip6_tunnel_ioctl,
  989. .ndo_change_mtu = ipip6_tunnel_change_mtu,
  990. .ndo_get_stats64= ipip6_get_stats64,
  991. };
  992. static void ipip6_dev_free(struct net_device *dev)
  993. {
  994. free_percpu(dev->tstats);
  995. free_netdev(dev);
  996. }
  997. static void ipip6_tunnel_setup(struct net_device *dev)
  998. {
  999. dev->netdev_ops = &ipip6_netdev_ops;
  1000. dev->destructor = ipip6_dev_free;
  1001. dev->type = ARPHRD_SIT;
  1002. dev->hard_header_len = LL_MAX_HEADER + sizeof(struct iphdr);
  1003. dev->mtu = ETH_DATA_LEN - sizeof(struct iphdr);
  1004. dev->flags = IFF_NOARP;
  1005. dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
  1006. dev->iflink = 0;
  1007. dev->addr_len = 4;
  1008. dev->features |= NETIF_F_NETNS_LOCAL;
  1009. dev->features |= NETIF_F_LLTX;
  1010. }
  1011. static int ipip6_tunnel_init(struct net_device *dev)
  1012. {
  1013. struct ip_tunnel *tunnel = netdev_priv(dev);
  1014. tunnel->dev = dev;
  1015. memcpy(dev->dev_addr, &tunnel->parms.iph.saddr, 4);
  1016. memcpy(dev->broadcast, &tunnel->parms.iph.daddr, 4);
  1017. ipip6_tunnel_bind_dev(dev);
  1018. dev->tstats = alloc_percpu(struct pcpu_tstats);
  1019. if (!dev->tstats)
  1020. return -ENOMEM;
  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. tunnel->dev = dev;
  1030. strcpy(tunnel->parms.name, dev->name);
  1031. iph->version = 4;
  1032. iph->protocol = IPPROTO_IPV6;
  1033. iph->ihl = 5;
  1034. iph->ttl = 64;
  1035. dev->tstats = alloc_percpu(struct pcpu_tstats);
  1036. if (!dev->tstats)
  1037. return -ENOMEM;
  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. pr_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. pr_info("%s: can't add protocol\n", __func__);
  1127. }
  1128. return err;
  1129. }
  1130. module_init(sit_init);
  1131. module_exit(sit_cleanup);
  1132. MODULE_LICENSE("GPL");
  1133. MODULE_ALIAS_NETDEV("sit0");