sit.c 45 KB

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