sit.c 44 KB

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