sit.c 45 KB

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