sit.c 46 KB

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