sit.c 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970
  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. dev_hold(dev);
  190. ipip6_tunnel_link(sitn, t);
  191. return 0;
  192. out:
  193. return err;
  194. }
  195. static struct ip_tunnel *ipip6_tunnel_locate(struct net *net,
  196. struct ip_tunnel_parm *parms, int create)
  197. {
  198. __be32 remote = parms->iph.daddr;
  199. __be32 local = parms->iph.saddr;
  200. struct ip_tunnel *t, *nt;
  201. struct ip_tunnel __rcu **tp;
  202. struct net_device *dev;
  203. char name[IFNAMSIZ];
  204. struct sit_net *sitn = net_generic(net, sit_net_id);
  205. for (tp = __ipip6_bucket(sitn, parms);
  206. (t = rtnl_dereference(*tp)) != NULL;
  207. tp = &t->next) {
  208. if (local == t->parms.iph.saddr &&
  209. remote == t->parms.iph.daddr &&
  210. parms->link == t->parms.link) {
  211. if (create)
  212. return NULL;
  213. else
  214. return t;
  215. }
  216. }
  217. if (!create)
  218. goto failed;
  219. if (parms->name[0]) {
  220. if (!dev_valid_name(parms->name))
  221. goto failed;
  222. strlcpy(name, parms->name, IFNAMSIZ);
  223. } else {
  224. strcpy(name, "sit%d");
  225. }
  226. dev = alloc_netdev(sizeof(*t), name, NET_NAME_UNKNOWN,
  227. ipip6_tunnel_setup);
  228. if (!dev)
  229. return NULL;
  230. dev_net_set(dev, net);
  231. nt = netdev_priv(dev);
  232. nt->parms = *parms;
  233. if (ipip6_tunnel_create(dev) < 0)
  234. goto failed_free;
  235. return nt;
  236. failed_free:
  237. free_netdev(dev);
  238. failed:
  239. return NULL;
  240. }
  241. #define for_each_prl_rcu(start) \
  242. for (prl = rcu_dereference(start); \
  243. prl; \
  244. prl = rcu_dereference(prl->next))
  245. static struct ip_tunnel_prl_entry *
  246. __ipip6_tunnel_locate_prl(struct ip_tunnel *t, __be32 addr)
  247. {
  248. struct ip_tunnel_prl_entry *prl;
  249. for_each_prl_rcu(t->prl)
  250. if (prl->addr == addr)
  251. break;
  252. return prl;
  253. }
  254. static int ipip6_tunnel_get_prl(struct ip_tunnel *t,
  255. struct ip_tunnel_prl __user *a)
  256. {
  257. struct ip_tunnel_prl kprl, *kp;
  258. struct ip_tunnel_prl_entry *prl;
  259. unsigned int cmax, c = 0, ca, len;
  260. int ret = 0;
  261. if (copy_from_user(&kprl, a, sizeof(kprl)))
  262. return -EFAULT;
  263. cmax = kprl.datalen / sizeof(kprl);
  264. if (cmax > 1 && kprl.addr != htonl(INADDR_ANY))
  265. cmax = 1;
  266. /* For simple GET or for root users,
  267. * we try harder to allocate.
  268. */
  269. kp = (cmax <= 1 || capable(CAP_NET_ADMIN)) ?
  270. kcalloc(cmax, sizeof(*kp), GFP_KERNEL | __GFP_NOWARN) :
  271. NULL;
  272. rcu_read_lock();
  273. ca = t->prl_count < cmax ? t->prl_count : cmax;
  274. if (!kp) {
  275. /* We don't try hard to allocate much memory for
  276. * non-root users.
  277. * For root users, retry allocating enough memory for
  278. * the answer.
  279. */
  280. kp = kcalloc(ca, sizeof(*kp), GFP_ATOMIC);
  281. if (!kp) {
  282. ret = -ENOMEM;
  283. goto out;
  284. }
  285. }
  286. c = 0;
  287. for_each_prl_rcu(t->prl) {
  288. if (c >= cmax)
  289. break;
  290. if (kprl.addr != htonl(INADDR_ANY) && prl->addr != kprl.addr)
  291. continue;
  292. kp[c].addr = prl->addr;
  293. kp[c].flags = prl->flags;
  294. c++;
  295. if (kprl.addr != htonl(INADDR_ANY))
  296. break;
  297. }
  298. out:
  299. rcu_read_unlock();
  300. len = sizeof(*kp) * c;
  301. ret = 0;
  302. if ((len && copy_to_user(a + 1, kp, len)) || put_user(len, &a->datalen))
  303. ret = -EFAULT;
  304. kfree(kp);
  305. return ret;
  306. }
  307. static int
  308. ipip6_tunnel_add_prl(struct ip_tunnel *t, struct ip_tunnel_prl *a, int chg)
  309. {
  310. struct ip_tunnel_prl_entry *p;
  311. int err = 0;
  312. if (a->addr == htonl(INADDR_ANY))
  313. return -EINVAL;
  314. ASSERT_RTNL();
  315. for (p = rtnl_dereference(t->prl); p; p = rtnl_dereference(p->next)) {
  316. if (p->addr == a->addr) {
  317. if (chg) {
  318. p->flags = a->flags;
  319. goto out;
  320. }
  321. err = -EEXIST;
  322. goto out;
  323. }
  324. }
  325. if (chg) {
  326. err = -ENXIO;
  327. goto out;
  328. }
  329. p = kzalloc(sizeof(struct ip_tunnel_prl_entry), GFP_KERNEL);
  330. if (!p) {
  331. err = -ENOBUFS;
  332. goto out;
  333. }
  334. p->next = t->prl;
  335. p->addr = a->addr;
  336. p->flags = a->flags;
  337. t->prl_count++;
  338. rcu_assign_pointer(t->prl, p);
  339. out:
  340. return err;
  341. }
  342. static void prl_list_destroy_rcu(struct rcu_head *head)
  343. {
  344. struct ip_tunnel_prl_entry *p, *n;
  345. p = container_of(head, struct ip_tunnel_prl_entry, rcu_head);
  346. do {
  347. n = rcu_dereference_protected(p->next, 1);
  348. kfree(p);
  349. p = n;
  350. } while (p);
  351. }
  352. static int
  353. ipip6_tunnel_del_prl(struct ip_tunnel *t, struct ip_tunnel_prl *a)
  354. {
  355. struct ip_tunnel_prl_entry *x;
  356. struct ip_tunnel_prl_entry __rcu **p;
  357. int err = 0;
  358. ASSERT_RTNL();
  359. if (a && a->addr != htonl(INADDR_ANY)) {
  360. for (p = &t->prl;
  361. (x = rtnl_dereference(*p)) != NULL;
  362. p = &x->next) {
  363. if (x->addr == a->addr) {
  364. *p = x->next;
  365. kfree_rcu(x, rcu_head);
  366. t->prl_count--;
  367. goto out;
  368. }
  369. }
  370. err = -ENXIO;
  371. } else {
  372. x = rtnl_dereference(t->prl);
  373. if (x) {
  374. t->prl_count = 0;
  375. call_rcu(&x->rcu_head, prl_list_destroy_rcu);
  376. t->prl = NULL;
  377. }
  378. }
  379. out:
  380. return err;
  381. }
  382. static int
  383. isatap_chksrc(struct sk_buff *skb, const struct iphdr *iph, struct ip_tunnel *t)
  384. {
  385. struct ip_tunnel_prl_entry *p;
  386. int ok = 1;
  387. rcu_read_lock();
  388. p = __ipip6_tunnel_locate_prl(t, iph->saddr);
  389. if (p) {
  390. if (p->flags & PRL_DEFAULT)
  391. skb->ndisc_nodetype = NDISC_NODETYPE_DEFAULT;
  392. else
  393. skb->ndisc_nodetype = NDISC_NODETYPE_NODEFAULT;
  394. } else {
  395. const struct in6_addr *addr6 = &ipv6_hdr(skb)->saddr;
  396. if (ipv6_addr_is_isatap(addr6) &&
  397. (addr6->s6_addr32[3] == iph->saddr) &&
  398. ipv6_chk_prefix(addr6, t->dev))
  399. skb->ndisc_nodetype = NDISC_NODETYPE_HOST;
  400. else
  401. ok = 0;
  402. }
  403. rcu_read_unlock();
  404. return ok;
  405. }
  406. static void ipip6_tunnel_uninit(struct net_device *dev)
  407. {
  408. struct ip_tunnel *tunnel = netdev_priv(dev);
  409. struct sit_net *sitn = net_generic(tunnel->net, sit_net_id);
  410. if (dev == sitn->fb_tunnel_dev) {
  411. RCU_INIT_POINTER(sitn->tunnels_wc[0], NULL);
  412. } else {
  413. ipip6_tunnel_unlink(sitn, tunnel);
  414. ipip6_tunnel_del_prl(tunnel, NULL);
  415. }
  416. dst_cache_reset(&tunnel->dst_cache);
  417. dev_put(dev);
  418. }
  419. static int ipip6_err(struct sk_buff *skb, u32 info)
  420. {
  421. const struct iphdr *iph = (const struct iphdr *)skb->data;
  422. const int type = icmp_hdr(skb)->type;
  423. const int code = icmp_hdr(skb)->code;
  424. unsigned int data_len = 0;
  425. struct ip_tunnel *t;
  426. int sifindex;
  427. int err;
  428. switch (type) {
  429. default:
  430. case ICMP_PARAMETERPROB:
  431. return 0;
  432. case ICMP_DEST_UNREACH:
  433. switch (code) {
  434. case ICMP_SR_FAILED:
  435. /* Impossible event. */
  436. return 0;
  437. default:
  438. /* All others are translated to HOST_UNREACH.
  439. rfc2003 contains "deep thoughts" about NET_UNREACH,
  440. I believe they are just ether pollution. --ANK
  441. */
  442. break;
  443. }
  444. break;
  445. case ICMP_TIME_EXCEEDED:
  446. if (code != ICMP_EXC_TTL)
  447. return 0;
  448. data_len = icmp_hdr(skb)->un.reserved[1] * 4; /* RFC 4884 4.1 */
  449. break;
  450. case ICMP_REDIRECT:
  451. break;
  452. }
  453. err = -ENOENT;
  454. sifindex = netif_is_l3_master(skb->dev) ? IPCB(skb)->iif : 0;
  455. t = ipip6_tunnel_lookup(dev_net(skb->dev), skb->dev,
  456. iph->daddr, iph->saddr, sifindex);
  457. if (!t)
  458. goto out;
  459. if (type == ICMP_DEST_UNREACH && code == ICMP_FRAG_NEEDED) {
  460. ipv4_update_pmtu(skb, dev_net(skb->dev), info,
  461. t->parms.link, 0, iph->protocol, 0);
  462. err = 0;
  463. goto out;
  464. }
  465. if (type == ICMP_REDIRECT) {
  466. ipv4_redirect(skb, dev_net(skb->dev), t->parms.link, 0,
  467. iph->protocol, 0);
  468. err = 0;
  469. goto out;
  470. }
  471. err = 0;
  472. if (__in6_dev_get(skb->dev) &&
  473. !ip6_err_gen_icmpv6_unreach(skb, iph->ihl * 4, type, data_len))
  474. goto out;
  475. if (t->parms.iph.daddr == 0)
  476. goto out;
  477. if (t->parms.iph.ttl == 0 && type == ICMP_TIME_EXCEEDED)
  478. goto out;
  479. if (time_before(jiffies, t->err_time + IPTUNNEL_ERR_TIMEO))
  480. t->err_count++;
  481. else
  482. t->err_count = 1;
  483. t->err_time = jiffies;
  484. out:
  485. return err;
  486. }
  487. static inline bool is_spoofed_6rd(struct ip_tunnel *tunnel, const __be32 v4addr,
  488. const struct in6_addr *v6addr)
  489. {
  490. __be32 v4embed = 0;
  491. if (check_6rd(tunnel, v6addr, &v4embed) && v4addr != v4embed)
  492. return true;
  493. return false;
  494. }
  495. /* Checks if an address matches an address on the tunnel interface.
  496. * Used to detect the NAT of proto 41 packets and let them pass spoofing test.
  497. * Long story:
  498. * This function is called after we considered the packet as spoofed
  499. * in is_spoofed_6rd.
  500. * We may have a router that is doing NAT for proto 41 packets
  501. * for an internal station. Destination a.a.a.a/PREFIX:bbbb:bbbb
  502. * will be translated to n.n.n.n/PREFIX:bbbb:bbbb. And is_spoofed_6rd
  503. * function will return true, dropping the packet.
  504. * But, we can still check if is spoofed against the IP
  505. * addresses associated with the interface.
  506. */
  507. static bool only_dnatted(const struct ip_tunnel *tunnel,
  508. const struct in6_addr *v6dst)
  509. {
  510. int prefix_len;
  511. #ifdef CONFIG_IPV6_SIT_6RD
  512. prefix_len = tunnel->ip6rd.prefixlen + 32
  513. - tunnel->ip6rd.relay_prefixlen;
  514. #else
  515. prefix_len = 48;
  516. #endif
  517. return ipv6_chk_custom_prefix(v6dst, prefix_len, tunnel->dev);
  518. }
  519. /* Returns true if a packet is spoofed */
  520. static bool packet_is_spoofed(struct sk_buff *skb,
  521. const struct iphdr *iph,
  522. struct ip_tunnel *tunnel)
  523. {
  524. const struct ipv6hdr *ipv6h;
  525. if (tunnel->dev->priv_flags & IFF_ISATAP) {
  526. if (!isatap_chksrc(skb, iph, tunnel))
  527. return true;
  528. return false;
  529. }
  530. if (tunnel->dev->flags & IFF_POINTOPOINT)
  531. return false;
  532. ipv6h = ipv6_hdr(skb);
  533. if (unlikely(is_spoofed_6rd(tunnel, iph->saddr, &ipv6h->saddr))) {
  534. net_warn_ratelimited("Src spoofed %pI4/%pI6c -> %pI4/%pI6c\n",
  535. &iph->saddr, &ipv6h->saddr,
  536. &iph->daddr, &ipv6h->daddr);
  537. return true;
  538. }
  539. if (likely(!is_spoofed_6rd(tunnel, iph->daddr, &ipv6h->daddr)))
  540. return false;
  541. if (only_dnatted(tunnel, &ipv6h->daddr))
  542. return false;
  543. net_warn_ratelimited("Dst spoofed %pI4/%pI6c -> %pI4/%pI6c\n",
  544. &iph->saddr, &ipv6h->saddr,
  545. &iph->daddr, &ipv6h->daddr);
  546. return true;
  547. }
  548. static int ipip6_rcv(struct sk_buff *skb)
  549. {
  550. const struct iphdr *iph = ip_hdr(skb);
  551. struct ip_tunnel *tunnel;
  552. int sifindex;
  553. int err;
  554. sifindex = netif_is_l3_master(skb->dev) ? IPCB(skb)->iif : 0;
  555. tunnel = ipip6_tunnel_lookup(dev_net(skb->dev), skb->dev,
  556. iph->saddr, iph->daddr, sifindex);
  557. if (tunnel) {
  558. struct pcpu_sw_netstats *tstats;
  559. if (tunnel->parms.iph.protocol != IPPROTO_IPV6 &&
  560. tunnel->parms.iph.protocol != 0)
  561. goto out;
  562. skb->mac_header = skb->network_header;
  563. skb_reset_network_header(skb);
  564. IPCB(skb)->flags = 0;
  565. skb->dev = tunnel->dev;
  566. if (packet_is_spoofed(skb, iph, tunnel)) {
  567. tunnel->dev->stats.rx_errors++;
  568. goto out;
  569. }
  570. if (iptunnel_pull_header(skb, 0, htons(ETH_P_IPV6),
  571. !net_eq(tunnel->net, dev_net(tunnel->dev))))
  572. goto out;
  573. /* skb can be uncloned in iptunnel_pull_header, so
  574. * old iph is no longer valid
  575. */
  576. iph = (const struct iphdr *)skb_mac_header(skb);
  577. err = IP_ECN_decapsulate(iph, skb);
  578. if (unlikely(err)) {
  579. if (log_ecn_error)
  580. net_info_ratelimited("non-ECT from %pI4 with TOS=%#x\n",
  581. &iph->saddr, iph->tos);
  582. if (err > 1) {
  583. ++tunnel->dev->stats.rx_frame_errors;
  584. ++tunnel->dev->stats.rx_errors;
  585. goto out;
  586. }
  587. }
  588. tstats = this_cpu_ptr(tunnel->dev->tstats);
  589. u64_stats_update_begin(&tstats->syncp);
  590. tstats->rx_packets++;
  591. tstats->rx_bytes += skb->len;
  592. u64_stats_update_end(&tstats->syncp);
  593. netif_rx(skb);
  594. return 0;
  595. }
  596. /* no tunnel matched, let upstream know, ipsec may handle it */
  597. return 1;
  598. out:
  599. kfree_skb(skb);
  600. return 0;
  601. }
  602. static const struct tnl_ptk_info ipip_tpi = {
  603. /* no tunnel info required for ipip. */
  604. .proto = htons(ETH_P_IP),
  605. };
  606. #if IS_ENABLED(CONFIG_MPLS)
  607. static const struct tnl_ptk_info mplsip_tpi = {
  608. /* no tunnel info required for mplsip. */
  609. .proto = htons(ETH_P_MPLS_UC),
  610. };
  611. #endif
  612. static int sit_tunnel_rcv(struct sk_buff *skb, u8 ipproto)
  613. {
  614. const struct iphdr *iph;
  615. struct ip_tunnel *tunnel;
  616. int sifindex;
  617. sifindex = netif_is_l3_master(skb->dev) ? IPCB(skb)->iif : 0;
  618. iph = ip_hdr(skb);
  619. tunnel = ipip6_tunnel_lookup(dev_net(skb->dev), skb->dev,
  620. iph->saddr, iph->daddr, sifindex);
  621. if (tunnel) {
  622. const struct tnl_ptk_info *tpi;
  623. if (tunnel->parms.iph.protocol != ipproto &&
  624. tunnel->parms.iph.protocol != 0)
  625. goto drop;
  626. if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb))
  627. goto drop;
  628. #if IS_ENABLED(CONFIG_MPLS)
  629. if (ipproto == IPPROTO_MPLS)
  630. tpi = &mplsip_tpi;
  631. else
  632. #endif
  633. tpi = &ipip_tpi;
  634. if (iptunnel_pull_header(skb, 0, tpi->proto, false))
  635. goto drop;
  636. return ip_tunnel_rcv(tunnel, skb, tpi, NULL, log_ecn_error);
  637. }
  638. return 1;
  639. drop:
  640. kfree_skb(skb);
  641. return 0;
  642. }
  643. static int ipip_rcv(struct sk_buff *skb)
  644. {
  645. return sit_tunnel_rcv(skb, IPPROTO_IPIP);
  646. }
  647. #if IS_ENABLED(CONFIG_MPLS)
  648. static int mplsip_rcv(struct sk_buff *skb)
  649. {
  650. return sit_tunnel_rcv(skb, IPPROTO_MPLS);
  651. }
  652. #endif
  653. /*
  654. * If the IPv6 address comes from 6rd / 6to4 (RFC 3056) addr space this function
  655. * stores the embedded IPv4 address in v4dst and returns true.
  656. */
  657. static bool check_6rd(struct ip_tunnel *tunnel, const struct in6_addr *v6dst,
  658. __be32 *v4dst)
  659. {
  660. #ifdef CONFIG_IPV6_SIT_6RD
  661. if (ipv6_prefix_equal(v6dst, &tunnel->ip6rd.prefix,
  662. tunnel->ip6rd.prefixlen)) {
  663. unsigned int pbw0, pbi0;
  664. int pbi1;
  665. u32 d;
  666. pbw0 = tunnel->ip6rd.prefixlen >> 5;
  667. pbi0 = tunnel->ip6rd.prefixlen & 0x1f;
  668. d = tunnel->ip6rd.relay_prefixlen < 32 ?
  669. (ntohl(v6dst->s6_addr32[pbw0]) << pbi0) >>
  670. tunnel->ip6rd.relay_prefixlen : 0;
  671. pbi1 = pbi0 - tunnel->ip6rd.relay_prefixlen;
  672. if (pbi1 > 0)
  673. d |= ntohl(v6dst->s6_addr32[pbw0 + 1]) >>
  674. (32 - pbi1);
  675. *v4dst = tunnel->ip6rd.relay_prefix | htonl(d);
  676. return true;
  677. }
  678. #else
  679. if (v6dst->s6_addr16[0] == htons(0x2002)) {
  680. /* 6to4 v6 addr has 16 bits prefix, 32 v4addr, 16 SLA, ... */
  681. memcpy(v4dst, &v6dst->s6_addr16[1], 4);
  682. return true;
  683. }
  684. #endif
  685. return false;
  686. }
  687. static inline __be32 try_6rd(struct ip_tunnel *tunnel,
  688. const struct in6_addr *v6dst)
  689. {
  690. __be32 dst = 0;
  691. check_6rd(tunnel, v6dst, &dst);
  692. return dst;
  693. }
  694. /*
  695. * This function assumes it is being called from dev_queue_xmit()
  696. * and that skb is filled properly by that function.
  697. */
  698. static netdev_tx_t ipip6_tunnel_xmit(struct sk_buff *skb,
  699. struct net_device *dev)
  700. {
  701. struct ip_tunnel *tunnel = netdev_priv(dev);
  702. const struct iphdr *tiph = &tunnel->parms.iph;
  703. const struct ipv6hdr *iph6 = ipv6_hdr(skb);
  704. u8 tos = tunnel->parms.iph.tos;
  705. __be16 df = tiph->frag_off;
  706. struct rtable *rt; /* Route to the other host */
  707. struct net_device *tdev; /* Device to other host */
  708. unsigned int max_headroom; /* The extra header space needed */
  709. __be32 dst = tiph->daddr;
  710. struct flowi4 fl4;
  711. int mtu;
  712. const struct in6_addr *addr6;
  713. int addr_type;
  714. u8 ttl;
  715. u8 protocol = IPPROTO_IPV6;
  716. int t_hlen = tunnel->hlen + sizeof(struct iphdr);
  717. if (tos == 1)
  718. tos = ipv6_get_dsfield(iph6);
  719. /* ISATAP (RFC4214) - must come before 6to4 */
  720. if (dev->priv_flags & IFF_ISATAP) {
  721. struct neighbour *neigh = NULL;
  722. bool do_tx_error = false;
  723. if (skb_dst(skb))
  724. neigh = dst_neigh_lookup(skb_dst(skb), &iph6->daddr);
  725. if (!neigh) {
  726. net_dbg_ratelimited("nexthop == NULL\n");
  727. goto tx_error;
  728. }
  729. addr6 = (const struct in6_addr *)&neigh->primary_key;
  730. addr_type = ipv6_addr_type(addr6);
  731. if ((addr_type & IPV6_ADDR_UNICAST) &&
  732. ipv6_addr_is_isatap(addr6))
  733. dst = addr6->s6_addr32[3];
  734. else
  735. do_tx_error = true;
  736. neigh_release(neigh);
  737. if (do_tx_error)
  738. goto tx_error;
  739. }
  740. if (!dst)
  741. dst = try_6rd(tunnel, &iph6->daddr);
  742. if (!dst) {
  743. struct neighbour *neigh = NULL;
  744. bool do_tx_error = false;
  745. if (skb_dst(skb))
  746. neigh = dst_neigh_lookup(skb_dst(skb), &iph6->daddr);
  747. if (!neigh) {
  748. net_dbg_ratelimited("nexthop == NULL\n");
  749. goto tx_error;
  750. }
  751. addr6 = (const struct in6_addr *)&neigh->primary_key;
  752. addr_type = ipv6_addr_type(addr6);
  753. if (addr_type == IPV6_ADDR_ANY) {
  754. addr6 = &ipv6_hdr(skb)->daddr;
  755. addr_type = ipv6_addr_type(addr6);
  756. }
  757. if ((addr_type & IPV6_ADDR_COMPATv4) != 0)
  758. dst = addr6->s6_addr32[3];
  759. else
  760. do_tx_error = true;
  761. neigh_release(neigh);
  762. if (do_tx_error)
  763. goto tx_error;
  764. }
  765. flowi4_init_output(&fl4, tunnel->parms.link, tunnel->fwmark,
  766. RT_TOS(tos), RT_SCOPE_UNIVERSE, IPPROTO_IPV6,
  767. 0, dst, tiph->saddr, 0, 0,
  768. sock_net_uid(tunnel->net, NULL));
  769. rt = ip_route_output_flow(tunnel->net, &fl4, NULL);
  770. if (IS_ERR(rt)) {
  771. dev->stats.tx_carrier_errors++;
  772. goto tx_error_icmp;
  773. }
  774. if (rt->rt_type != RTN_UNICAST) {
  775. ip_rt_put(rt);
  776. dev->stats.tx_carrier_errors++;
  777. goto tx_error_icmp;
  778. }
  779. tdev = rt->dst.dev;
  780. if (tdev == dev) {
  781. ip_rt_put(rt);
  782. dev->stats.collisions++;
  783. goto tx_error;
  784. }
  785. if (iptunnel_handle_offloads(skb, SKB_GSO_IPXIP4)) {
  786. ip_rt_put(rt);
  787. goto tx_error;
  788. }
  789. if (df) {
  790. mtu = dst_mtu(&rt->dst) - t_hlen;
  791. if (mtu < 68) {
  792. dev->stats.collisions++;
  793. ip_rt_put(rt);
  794. goto tx_error;
  795. }
  796. if (mtu < IPV6_MIN_MTU) {
  797. mtu = IPV6_MIN_MTU;
  798. df = 0;
  799. }
  800. if (tunnel->parms.iph.daddr)
  801. skb_dst_update_pmtu_no_confirm(skb, mtu);
  802. if (skb->len > mtu && !skb_is_gso(skb)) {
  803. icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
  804. ip_rt_put(rt);
  805. goto tx_error;
  806. }
  807. }
  808. if (tunnel->err_count > 0) {
  809. if (time_before(jiffies,
  810. tunnel->err_time + IPTUNNEL_ERR_TIMEO)) {
  811. tunnel->err_count--;
  812. dst_link_failure(skb);
  813. } else
  814. tunnel->err_count = 0;
  815. }
  816. /*
  817. * Okay, now see if we can stuff it in the buffer as-is.
  818. */
  819. max_headroom = LL_RESERVED_SPACE(tdev) + t_hlen;
  820. if (skb_headroom(skb) < max_headroom || skb_shared(skb) ||
  821. (skb_cloned(skb) && !skb_clone_writable(skb, 0))) {
  822. struct sk_buff *new_skb = skb_realloc_headroom(skb, max_headroom);
  823. if (!new_skb) {
  824. ip_rt_put(rt);
  825. dev->stats.tx_dropped++;
  826. kfree_skb(skb);
  827. return NETDEV_TX_OK;
  828. }
  829. if (skb->sk)
  830. skb_set_owner_w(new_skb, skb->sk);
  831. dev_kfree_skb(skb);
  832. skb = new_skb;
  833. iph6 = ipv6_hdr(skb);
  834. }
  835. ttl = tiph->ttl;
  836. if (ttl == 0)
  837. ttl = iph6->hop_limit;
  838. tos = INET_ECN_encapsulate(tos, ipv6_get_dsfield(iph6));
  839. if (ip_tunnel_encap(skb, tunnel, &protocol, &fl4) < 0) {
  840. ip_rt_put(rt);
  841. goto tx_error;
  842. }
  843. skb_set_inner_ipproto(skb, IPPROTO_IPV6);
  844. iptunnel_xmit(NULL, rt, skb, fl4.saddr, fl4.daddr, protocol, tos, ttl,
  845. df, !net_eq(tunnel->net, dev_net(dev)));
  846. return NETDEV_TX_OK;
  847. tx_error_icmp:
  848. dst_link_failure(skb);
  849. tx_error:
  850. kfree_skb(skb);
  851. dev->stats.tx_errors++;
  852. return NETDEV_TX_OK;
  853. }
  854. static netdev_tx_t sit_tunnel_xmit__(struct sk_buff *skb,
  855. struct net_device *dev, u8 ipproto)
  856. {
  857. struct ip_tunnel *tunnel = netdev_priv(dev);
  858. const struct iphdr *tiph = &tunnel->parms.iph;
  859. if (iptunnel_handle_offloads(skb, SKB_GSO_IPXIP4))
  860. goto tx_error;
  861. skb_set_inner_ipproto(skb, ipproto);
  862. ip_tunnel_xmit(skb, dev, tiph, ipproto);
  863. return NETDEV_TX_OK;
  864. tx_error:
  865. kfree_skb(skb);
  866. dev->stats.tx_errors++;
  867. return NETDEV_TX_OK;
  868. }
  869. static netdev_tx_t sit_tunnel_xmit(struct sk_buff *skb,
  870. struct net_device *dev)
  871. {
  872. if (!pskb_inet_may_pull(skb))
  873. goto tx_err;
  874. switch (skb->protocol) {
  875. case htons(ETH_P_IP):
  876. sit_tunnel_xmit__(skb, dev, IPPROTO_IPIP);
  877. break;
  878. case htons(ETH_P_IPV6):
  879. ipip6_tunnel_xmit(skb, dev);
  880. break;
  881. #if IS_ENABLED(CONFIG_MPLS)
  882. case htons(ETH_P_MPLS_UC):
  883. sit_tunnel_xmit__(skb, dev, IPPROTO_MPLS);
  884. break;
  885. #endif
  886. default:
  887. goto tx_err;
  888. }
  889. return NETDEV_TX_OK;
  890. tx_err:
  891. dev->stats.tx_errors++;
  892. kfree_skb(skb);
  893. return NETDEV_TX_OK;
  894. }
  895. static void ipip6_tunnel_bind_dev(struct net_device *dev)
  896. {
  897. struct net_device *tdev = NULL;
  898. struct ip_tunnel *tunnel;
  899. const struct iphdr *iph;
  900. struct flowi4 fl4;
  901. tunnel = netdev_priv(dev);
  902. iph = &tunnel->parms.iph;
  903. if (iph->daddr) {
  904. struct rtable *rt = ip_route_output_ports(tunnel->net, &fl4,
  905. NULL,
  906. iph->daddr, iph->saddr,
  907. 0, 0,
  908. IPPROTO_IPV6,
  909. RT_TOS(iph->tos),
  910. tunnel->parms.link);
  911. if (!IS_ERR(rt)) {
  912. tdev = rt->dst.dev;
  913. ip_rt_put(rt);
  914. }
  915. dev->flags |= IFF_POINTOPOINT;
  916. }
  917. if (!tdev && tunnel->parms.link)
  918. tdev = __dev_get_by_index(tunnel->net, tunnel->parms.link);
  919. if (tdev && !netif_is_l3_master(tdev)) {
  920. int t_hlen = tunnel->hlen + sizeof(struct iphdr);
  921. dev->hard_header_len = tdev->hard_header_len + sizeof(struct iphdr);
  922. dev->mtu = tdev->mtu - t_hlen;
  923. if (dev->mtu < IPV6_MIN_MTU)
  924. dev->mtu = IPV6_MIN_MTU;
  925. }
  926. }
  927. static void ipip6_tunnel_update(struct ip_tunnel *t, struct ip_tunnel_parm *p,
  928. __u32 fwmark)
  929. {
  930. struct net *net = t->net;
  931. struct sit_net *sitn = net_generic(net, sit_net_id);
  932. ipip6_tunnel_unlink(sitn, t);
  933. synchronize_net();
  934. t->parms.iph.saddr = p->iph.saddr;
  935. t->parms.iph.daddr = p->iph.daddr;
  936. memcpy(t->dev->dev_addr, &p->iph.saddr, 4);
  937. memcpy(t->dev->broadcast, &p->iph.daddr, 4);
  938. ipip6_tunnel_link(sitn, t);
  939. t->parms.iph.ttl = p->iph.ttl;
  940. t->parms.iph.tos = p->iph.tos;
  941. t->parms.iph.frag_off = p->iph.frag_off;
  942. if (t->parms.link != p->link || t->fwmark != fwmark) {
  943. t->parms.link = p->link;
  944. t->fwmark = fwmark;
  945. ipip6_tunnel_bind_dev(t->dev);
  946. }
  947. dst_cache_reset(&t->dst_cache);
  948. netdev_state_change(t->dev);
  949. }
  950. #ifdef CONFIG_IPV6_SIT_6RD
  951. static int ipip6_tunnel_update_6rd(struct ip_tunnel *t,
  952. struct ip_tunnel_6rd *ip6rd)
  953. {
  954. struct in6_addr prefix;
  955. __be32 relay_prefix;
  956. if (ip6rd->relay_prefixlen > 32 ||
  957. ip6rd->prefixlen + (32 - ip6rd->relay_prefixlen) > 64)
  958. return -EINVAL;
  959. ipv6_addr_prefix(&prefix, &ip6rd->prefix, ip6rd->prefixlen);
  960. if (!ipv6_addr_equal(&prefix, &ip6rd->prefix))
  961. return -EINVAL;
  962. if (ip6rd->relay_prefixlen)
  963. relay_prefix = ip6rd->relay_prefix &
  964. htonl(0xffffffffUL <<
  965. (32 - ip6rd->relay_prefixlen));
  966. else
  967. relay_prefix = 0;
  968. if (relay_prefix != ip6rd->relay_prefix)
  969. return -EINVAL;
  970. t->ip6rd.prefix = prefix;
  971. t->ip6rd.relay_prefix = relay_prefix;
  972. t->ip6rd.prefixlen = ip6rd->prefixlen;
  973. t->ip6rd.relay_prefixlen = ip6rd->relay_prefixlen;
  974. dst_cache_reset(&t->dst_cache);
  975. netdev_state_change(t->dev);
  976. return 0;
  977. }
  978. #endif
  979. static bool ipip6_valid_ip_proto(u8 ipproto)
  980. {
  981. return ipproto == IPPROTO_IPV6 ||
  982. ipproto == IPPROTO_IPIP ||
  983. #if IS_ENABLED(CONFIG_MPLS)
  984. ipproto == IPPROTO_MPLS ||
  985. #endif
  986. ipproto == 0;
  987. }
  988. static int
  989. ipip6_tunnel_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  990. {
  991. int err = 0;
  992. struct ip_tunnel_parm p;
  993. struct ip_tunnel_prl prl;
  994. struct ip_tunnel *t = netdev_priv(dev);
  995. struct net *net = t->net;
  996. struct sit_net *sitn = net_generic(net, sit_net_id);
  997. #ifdef CONFIG_IPV6_SIT_6RD
  998. struct ip_tunnel_6rd ip6rd;
  999. #endif
  1000. switch (cmd) {
  1001. case SIOCGETTUNNEL:
  1002. #ifdef CONFIG_IPV6_SIT_6RD
  1003. case SIOCGET6RD:
  1004. #endif
  1005. if (dev == sitn->fb_tunnel_dev) {
  1006. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) {
  1007. err = -EFAULT;
  1008. break;
  1009. }
  1010. t = ipip6_tunnel_locate(net, &p, 0);
  1011. if (!t)
  1012. t = netdev_priv(dev);
  1013. }
  1014. err = -EFAULT;
  1015. if (cmd == SIOCGETTUNNEL) {
  1016. memcpy(&p, &t->parms, sizeof(p));
  1017. if (copy_to_user(ifr->ifr_ifru.ifru_data, &p,
  1018. sizeof(p)))
  1019. goto done;
  1020. #ifdef CONFIG_IPV6_SIT_6RD
  1021. } else {
  1022. ip6rd.prefix = t->ip6rd.prefix;
  1023. ip6rd.relay_prefix = t->ip6rd.relay_prefix;
  1024. ip6rd.prefixlen = t->ip6rd.prefixlen;
  1025. ip6rd.relay_prefixlen = t->ip6rd.relay_prefixlen;
  1026. if (copy_to_user(ifr->ifr_ifru.ifru_data, &ip6rd,
  1027. sizeof(ip6rd)))
  1028. goto done;
  1029. #endif
  1030. }
  1031. err = 0;
  1032. break;
  1033. case SIOCADDTUNNEL:
  1034. case SIOCCHGTUNNEL:
  1035. err = -EPERM;
  1036. if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  1037. goto done;
  1038. err = -EFAULT;
  1039. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
  1040. goto done;
  1041. err = -EINVAL;
  1042. if (!ipip6_valid_ip_proto(p.iph.protocol))
  1043. goto done;
  1044. if (p.iph.version != 4 ||
  1045. p.iph.ihl != 5 || (p.iph.frag_off&htons(~IP_DF)))
  1046. goto done;
  1047. if (p.iph.ttl)
  1048. p.iph.frag_off |= htons(IP_DF);
  1049. t = ipip6_tunnel_locate(net, &p, cmd == SIOCADDTUNNEL);
  1050. if (dev != sitn->fb_tunnel_dev && cmd == SIOCCHGTUNNEL) {
  1051. if (t) {
  1052. if (t->dev != dev) {
  1053. err = -EEXIST;
  1054. break;
  1055. }
  1056. } else {
  1057. if (((dev->flags&IFF_POINTOPOINT) && !p.iph.daddr) ||
  1058. (!(dev->flags&IFF_POINTOPOINT) && p.iph.daddr)) {
  1059. err = -EINVAL;
  1060. break;
  1061. }
  1062. t = netdev_priv(dev);
  1063. }
  1064. ipip6_tunnel_update(t, &p, t->fwmark);
  1065. }
  1066. if (t) {
  1067. err = 0;
  1068. if (copy_to_user(ifr->ifr_ifru.ifru_data, &t->parms, sizeof(p)))
  1069. err = -EFAULT;
  1070. } else
  1071. err = (cmd == SIOCADDTUNNEL ? -ENOBUFS : -ENOENT);
  1072. break;
  1073. case SIOCDELTUNNEL:
  1074. err = -EPERM;
  1075. if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  1076. goto done;
  1077. if (dev == sitn->fb_tunnel_dev) {
  1078. err = -EFAULT;
  1079. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
  1080. goto done;
  1081. err = -ENOENT;
  1082. t = ipip6_tunnel_locate(net, &p, 0);
  1083. if (!t)
  1084. goto done;
  1085. err = -EPERM;
  1086. if (t == netdev_priv(sitn->fb_tunnel_dev))
  1087. goto done;
  1088. dev = t->dev;
  1089. }
  1090. unregister_netdevice(dev);
  1091. err = 0;
  1092. break;
  1093. case SIOCGETPRL:
  1094. err = -EINVAL;
  1095. if (dev == sitn->fb_tunnel_dev)
  1096. goto done;
  1097. err = ipip6_tunnel_get_prl(t, ifr->ifr_ifru.ifru_data);
  1098. break;
  1099. case SIOCADDPRL:
  1100. case SIOCDELPRL:
  1101. case SIOCCHGPRL:
  1102. err = -EPERM;
  1103. if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  1104. goto done;
  1105. err = -EINVAL;
  1106. if (dev == sitn->fb_tunnel_dev)
  1107. goto done;
  1108. err = -EFAULT;
  1109. if (copy_from_user(&prl, ifr->ifr_ifru.ifru_data, sizeof(prl)))
  1110. goto done;
  1111. switch (cmd) {
  1112. case SIOCDELPRL:
  1113. err = ipip6_tunnel_del_prl(t, &prl);
  1114. break;
  1115. case SIOCADDPRL:
  1116. case SIOCCHGPRL:
  1117. err = ipip6_tunnel_add_prl(t, &prl, cmd == SIOCCHGPRL);
  1118. break;
  1119. }
  1120. dst_cache_reset(&t->dst_cache);
  1121. netdev_state_change(dev);
  1122. break;
  1123. #ifdef CONFIG_IPV6_SIT_6RD
  1124. case SIOCADD6RD:
  1125. case SIOCCHG6RD:
  1126. case SIOCDEL6RD:
  1127. err = -EPERM;
  1128. if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  1129. goto done;
  1130. err = -EFAULT;
  1131. if (copy_from_user(&ip6rd, ifr->ifr_ifru.ifru_data,
  1132. sizeof(ip6rd)))
  1133. goto done;
  1134. if (cmd != SIOCDEL6RD) {
  1135. err = ipip6_tunnel_update_6rd(t, &ip6rd);
  1136. if (err < 0)
  1137. goto done;
  1138. } else
  1139. ipip6_tunnel_clone_6rd(dev, sitn);
  1140. err = 0;
  1141. break;
  1142. #endif
  1143. default:
  1144. err = -EINVAL;
  1145. }
  1146. done:
  1147. return err;
  1148. }
  1149. static const struct net_device_ops ipip6_netdev_ops = {
  1150. .ndo_init = ipip6_tunnel_init,
  1151. .ndo_uninit = ipip6_tunnel_uninit,
  1152. .ndo_start_xmit = sit_tunnel_xmit,
  1153. .ndo_do_ioctl = ipip6_tunnel_ioctl,
  1154. .ndo_get_stats64 = ip_tunnel_get_stats64,
  1155. .ndo_get_iflink = ip_tunnel_get_iflink,
  1156. };
  1157. static void ipip6_dev_free(struct net_device *dev)
  1158. {
  1159. struct ip_tunnel *tunnel = netdev_priv(dev);
  1160. dst_cache_destroy(&tunnel->dst_cache);
  1161. free_percpu(dev->tstats);
  1162. }
  1163. #define SIT_FEATURES (NETIF_F_SG | \
  1164. NETIF_F_FRAGLIST | \
  1165. NETIF_F_HIGHDMA | \
  1166. NETIF_F_GSO_SOFTWARE | \
  1167. NETIF_F_HW_CSUM)
  1168. static void ipip6_tunnel_setup(struct net_device *dev)
  1169. {
  1170. struct ip_tunnel *tunnel = netdev_priv(dev);
  1171. int t_hlen = tunnel->hlen + sizeof(struct iphdr);
  1172. dev->netdev_ops = &ipip6_netdev_ops;
  1173. dev->needs_free_netdev = true;
  1174. dev->priv_destructor = ipip6_dev_free;
  1175. dev->type = ARPHRD_SIT;
  1176. dev->hard_header_len = LL_MAX_HEADER + t_hlen;
  1177. dev->mtu = ETH_DATA_LEN - t_hlen;
  1178. dev->min_mtu = IPV6_MIN_MTU;
  1179. dev->max_mtu = IP6_MAX_MTU - t_hlen;
  1180. dev->flags = IFF_NOARP;
  1181. netif_keep_dst(dev);
  1182. dev->addr_len = 4;
  1183. dev->features |= NETIF_F_LLTX;
  1184. dev->features |= SIT_FEATURES;
  1185. dev->hw_features |= SIT_FEATURES;
  1186. }
  1187. static int ipip6_tunnel_init(struct net_device *dev)
  1188. {
  1189. struct ip_tunnel *tunnel = netdev_priv(dev);
  1190. int err;
  1191. tunnel->dev = dev;
  1192. tunnel->net = dev_net(dev);
  1193. strcpy(tunnel->parms.name, dev->name);
  1194. ipip6_tunnel_bind_dev(dev);
  1195. dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
  1196. if (!dev->tstats)
  1197. return -ENOMEM;
  1198. err = dst_cache_init(&tunnel->dst_cache, GFP_KERNEL);
  1199. if (err) {
  1200. free_percpu(dev->tstats);
  1201. dev->tstats = NULL;
  1202. return err;
  1203. }
  1204. return 0;
  1205. }
  1206. static void __net_init ipip6_fb_tunnel_init(struct net_device *dev)
  1207. {
  1208. struct ip_tunnel *tunnel = netdev_priv(dev);
  1209. struct iphdr *iph = &tunnel->parms.iph;
  1210. struct net *net = dev_net(dev);
  1211. struct sit_net *sitn = net_generic(net, sit_net_id);
  1212. iph->version = 4;
  1213. iph->protocol = IPPROTO_IPV6;
  1214. iph->ihl = 5;
  1215. iph->ttl = 64;
  1216. dev_hold(dev);
  1217. rcu_assign_pointer(sitn->tunnels_wc[0], tunnel);
  1218. }
  1219. static int ipip6_validate(struct nlattr *tb[], struct nlattr *data[],
  1220. struct netlink_ext_ack *extack)
  1221. {
  1222. u8 proto;
  1223. if (!data || !data[IFLA_IPTUN_PROTO])
  1224. return 0;
  1225. proto = nla_get_u8(data[IFLA_IPTUN_PROTO]);
  1226. if (!ipip6_valid_ip_proto(proto))
  1227. return -EINVAL;
  1228. return 0;
  1229. }
  1230. static void ipip6_netlink_parms(struct nlattr *data[],
  1231. struct ip_tunnel_parm *parms,
  1232. __u32 *fwmark)
  1233. {
  1234. memset(parms, 0, sizeof(*parms));
  1235. parms->iph.version = 4;
  1236. parms->iph.protocol = IPPROTO_IPV6;
  1237. parms->iph.ihl = 5;
  1238. parms->iph.ttl = 64;
  1239. if (!data)
  1240. return;
  1241. if (data[IFLA_IPTUN_LINK])
  1242. parms->link = nla_get_u32(data[IFLA_IPTUN_LINK]);
  1243. if (data[IFLA_IPTUN_LOCAL])
  1244. parms->iph.saddr = nla_get_be32(data[IFLA_IPTUN_LOCAL]);
  1245. if (data[IFLA_IPTUN_REMOTE])
  1246. parms->iph.daddr = nla_get_be32(data[IFLA_IPTUN_REMOTE]);
  1247. if (data[IFLA_IPTUN_TTL]) {
  1248. parms->iph.ttl = nla_get_u8(data[IFLA_IPTUN_TTL]);
  1249. if (parms->iph.ttl)
  1250. parms->iph.frag_off = htons(IP_DF);
  1251. }
  1252. if (data[IFLA_IPTUN_TOS])
  1253. parms->iph.tos = nla_get_u8(data[IFLA_IPTUN_TOS]);
  1254. if (!data[IFLA_IPTUN_PMTUDISC] || nla_get_u8(data[IFLA_IPTUN_PMTUDISC]))
  1255. parms->iph.frag_off = htons(IP_DF);
  1256. if (data[IFLA_IPTUN_FLAGS])
  1257. parms->i_flags = nla_get_be16(data[IFLA_IPTUN_FLAGS]);
  1258. if (data[IFLA_IPTUN_PROTO])
  1259. parms->iph.protocol = nla_get_u8(data[IFLA_IPTUN_PROTO]);
  1260. if (data[IFLA_IPTUN_FWMARK])
  1261. *fwmark = nla_get_u32(data[IFLA_IPTUN_FWMARK]);
  1262. }
  1263. /* This function returns true when ENCAP attributes are present in the nl msg */
  1264. static bool ipip6_netlink_encap_parms(struct nlattr *data[],
  1265. struct ip_tunnel_encap *ipencap)
  1266. {
  1267. bool ret = false;
  1268. memset(ipencap, 0, sizeof(*ipencap));
  1269. if (!data)
  1270. return ret;
  1271. if (data[IFLA_IPTUN_ENCAP_TYPE]) {
  1272. ret = true;
  1273. ipencap->type = nla_get_u16(data[IFLA_IPTUN_ENCAP_TYPE]);
  1274. }
  1275. if (data[IFLA_IPTUN_ENCAP_FLAGS]) {
  1276. ret = true;
  1277. ipencap->flags = nla_get_u16(data[IFLA_IPTUN_ENCAP_FLAGS]);
  1278. }
  1279. if (data[IFLA_IPTUN_ENCAP_SPORT]) {
  1280. ret = true;
  1281. ipencap->sport = nla_get_be16(data[IFLA_IPTUN_ENCAP_SPORT]);
  1282. }
  1283. if (data[IFLA_IPTUN_ENCAP_DPORT]) {
  1284. ret = true;
  1285. ipencap->dport = nla_get_be16(data[IFLA_IPTUN_ENCAP_DPORT]);
  1286. }
  1287. return ret;
  1288. }
  1289. #ifdef CONFIG_IPV6_SIT_6RD
  1290. /* This function returns true when 6RD attributes are present in the nl msg */
  1291. static bool ipip6_netlink_6rd_parms(struct nlattr *data[],
  1292. struct ip_tunnel_6rd *ip6rd)
  1293. {
  1294. bool ret = false;
  1295. memset(ip6rd, 0, sizeof(*ip6rd));
  1296. if (!data)
  1297. return ret;
  1298. if (data[IFLA_IPTUN_6RD_PREFIX]) {
  1299. ret = true;
  1300. ip6rd->prefix = nla_get_in6_addr(data[IFLA_IPTUN_6RD_PREFIX]);
  1301. }
  1302. if (data[IFLA_IPTUN_6RD_RELAY_PREFIX]) {
  1303. ret = true;
  1304. ip6rd->relay_prefix =
  1305. nla_get_be32(data[IFLA_IPTUN_6RD_RELAY_PREFIX]);
  1306. }
  1307. if (data[IFLA_IPTUN_6RD_PREFIXLEN]) {
  1308. ret = true;
  1309. ip6rd->prefixlen = nla_get_u16(data[IFLA_IPTUN_6RD_PREFIXLEN]);
  1310. }
  1311. if (data[IFLA_IPTUN_6RD_RELAY_PREFIXLEN]) {
  1312. ret = true;
  1313. ip6rd->relay_prefixlen =
  1314. nla_get_u16(data[IFLA_IPTUN_6RD_RELAY_PREFIXLEN]);
  1315. }
  1316. return ret;
  1317. }
  1318. #endif
  1319. static int ipip6_newlink(struct net *src_net, struct net_device *dev,
  1320. struct nlattr *tb[], struct nlattr *data[],
  1321. struct netlink_ext_ack *extack)
  1322. {
  1323. struct net *net = dev_net(dev);
  1324. struct ip_tunnel *nt;
  1325. struct ip_tunnel_encap ipencap;
  1326. #ifdef CONFIG_IPV6_SIT_6RD
  1327. struct ip_tunnel_6rd ip6rd;
  1328. #endif
  1329. int err;
  1330. nt = netdev_priv(dev);
  1331. if (ipip6_netlink_encap_parms(data, &ipencap)) {
  1332. err = ip_tunnel_encap_setup(nt, &ipencap);
  1333. if (err < 0)
  1334. return err;
  1335. }
  1336. ipip6_netlink_parms(data, &nt->parms, &nt->fwmark);
  1337. if (ipip6_tunnel_locate(net, &nt->parms, 0))
  1338. return -EEXIST;
  1339. err = ipip6_tunnel_create(dev);
  1340. if (err < 0)
  1341. return err;
  1342. if (tb[IFLA_MTU]) {
  1343. u32 mtu = nla_get_u32(tb[IFLA_MTU]);
  1344. if (mtu >= IPV6_MIN_MTU &&
  1345. mtu <= IP6_MAX_MTU - dev->hard_header_len)
  1346. dev->mtu = mtu;
  1347. }
  1348. #ifdef CONFIG_IPV6_SIT_6RD
  1349. if (ipip6_netlink_6rd_parms(data, &ip6rd))
  1350. err = ipip6_tunnel_update_6rd(nt, &ip6rd);
  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 = 1; prio < 4; prio++) {
  1541. int h;
  1542. for (h = 0; h < IP6_SIT_HASH_SIZE; 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");