sit.c 46 KB

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