sit.c 47 KB

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