sit.c 44 KB

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