sit.c 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924
  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 != NULL) && (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. err = register_netdevice(dev);
  179. if (err < 0)
  180. goto out;
  181. ipip6_tunnel_clone_6rd(dev, sitn);
  182. dev->rtnl_link_ops = &sit_link_ops;
  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 == NULL)
  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. ip_tunnel_dst_reset_all(tunnel);
  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 == NULL)
  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 != NULL) {
  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->protocol = htons(ETH_P_IPV6);
  577. if (packet_is_spoofed(skb, iph, tunnel)) {
  578. tunnel->dev->stats.rx_errors++;
  579. goto out;
  580. }
  581. __skb_tunnel_rx(skb, tunnel->dev, tunnel->net);
  582. err = IP_ECN_decapsulate(iph, skb);
  583. if (unlikely(err)) {
  584. if (log_ecn_error)
  585. net_info_ratelimited("non-ECT from %pI4 with TOS=%#x\n",
  586. &iph->saddr, iph->tos);
  587. if (err > 1) {
  588. ++tunnel->dev->stats.rx_frame_errors;
  589. ++tunnel->dev->stats.rx_errors;
  590. goto out;
  591. }
  592. }
  593. tstats = this_cpu_ptr(tunnel->dev->tstats);
  594. u64_stats_update_begin(&tstats->syncp);
  595. tstats->rx_packets++;
  596. tstats->rx_bytes += skb->len;
  597. u64_stats_update_end(&tstats->syncp);
  598. netif_rx(skb);
  599. return 0;
  600. }
  601. /* no tunnel matched, let upstream know, ipsec may handle it */
  602. return 1;
  603. out:
  604. kfree_skb(skb);
  605. return 0;
  606. }
  607. static const struct tnl_ptk_info tpi = {
  608. /* no tunnel info required for ipip. */
  609. .proto = htons(ETH_P_IP),
  610. };
  611. static int ipip_rcv(struct sk_buff *skb)
  612. {
  613. const struct iphdr *iph;
  614. struct ip_tunnel *tunnel;
  615. iph = ip_hdr(skb);
  616. tunnel = ipip6_tunnel_lookup(dev_net(skb->dev), skb->dev,
  617. iph->saddr, iph->daddr);
  618. if (tunnel != NULL) {
  619. if (tunnel->parms.iph.protocol != IPPROTO_IPIP &&
  620. tunnel->parms.iph.protocol != 0)
  621. goto drop;
  622. if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb))
  623. goto drop;
  624. if (iptunnel_pull_header(skb, 0, tpi.proto))
  625. goto drop;
  626. return ip_tunnel_rcv(tunnel, skb, &tpi, log_ecn_error);
  627. }
  628. return 1;
  629. drop:
  630. kfree_skb(skb);
  631. return 0;
  632. }
  633. /*
  634. * If the IPv6 address comes from 6rd / 6to4 (RFC 3056) addr space this function
  635. * stores the embedded IPv4 address in v4dst and returns true.
  636. */
  637. static bool check_6rd(struct ip_tunnel *tunnel, const struct in6_addr *v6dst,
  638. __be32 *v4dst)
  639. {
  640. #ifdef CONFIG_IPV6_SIT_6RD
  641. if (ipv6_prefix_equal(v6dst, &tunnel->ip6rd.prefix,
  642. tunnel->ip6rd.prefixlen)) {
  643. unsigned int pbw0, pbi0;
  644. int pbi1;
  645. u32 d;
  646. pbw0 = tunnel->ip6rd.prefixlen >> 5;
  647. pbi0 = tunnel->ip6rd.prefixlen & 0x1f;
  648. d = (ntohl(v6dst->s6_addr32[pbw0]) << pbi0) >>
  649. tunnel->ip6rd.relay_prefixlen;
  650. pbi1 = pbi0 - tunnel->ip6rd.relay_prefixlen;
  651. if (pbi1 > 0)
  652. d |= ntohl(v6dst->s6_addr32[pbw0 + 1]) >>
  653. (32 - pbi1);
  654. *v4dst = tunnel->ip6rd.relay_prefix | htonl(d);
  655. return true;
  656. }
  657. #else
  658. if (v6dst->s6_addr16[0] == htons(0x2002)) {
  659. /* 6to4 v6 addr has 16 bits prefix, 32 v4addr, 16 SLA, ... */
  660. memcpy(v4dst, &v6dst->s6_addr16[1], 4);
  661. return true;
  662. }
  663. #endif
  664. return false;
  665. }
  666. static inline __be32 try_6rd(struct ip_tunnel *tunnel,
  667. const struct in6_addr *v6dst)
  668. {
  669. __be32 dst = 0;
  670. check_6rd(tunnel, v6dst, &dst);
  671. return dst;
  672. }
  673. /*
  674. * This function assumes it is being called from dev_queue_xmit()
  675. * and that skb is filled properly by that function.
  676. */
  677. static netdev_tx_t ipip6_tunnel_xmit(struct sk_buff *skb,
  678. struct net_device *dev)
  679. {
  680. struct ip_tunnel *tunnel = netdev_priv(dev);
  681. const struct iphdr *tiph = &tunnel->parms.iph;
  682. const struct ipv6hdr *iph6 = ipv6_hdr(skb);
  683. u8 tos = tunnel->parms.iph.tos;
  684. __be16 df = tiph->frag_off;
  685. struct rtable *rt; /* Route to the other host */
  686. struct net_device *tdev; /* Device to other host */
  687. unsigned int max_headroom; /* The extra header space needed */
  688. __be32 dst = tiph->daddr;
  689. struct flowi4 fl4;
  690. int mtu;
  691. const struct in6_addr *addr6;
  692. int addr_type;
  693. u8 ttl;
  694. int err;
  695. u8 protocol = IPPROTO_IPV6;
  696. int t_hlen = tunnel->hlen + sizeof(struct iphdr);
  697. if (skb->protocol != htons(ETH_P_IPV6))
  698. goto tx_error;
  699. if (tos == 1)
  700. tos = ipv6_get_dsfield(iph6);
  701. /* ISATAP (RFC4214) - must come before 6to4 */
  702. if (dev->priv_flags & IFF_ISATAP) {
  703. struct neighbour *neigh = NULL;
  704. bool do_tx_error = false;
  705. if (skb_dst(skb))
  706. neigh = dst_neigh_lookup(skb_dst(skb), &iph6->daddr);
  707. if (neigh == NULL) {
  708. net_dbg_ratelimited("nexthop == NULL\n");
  709. goto tx_error;
  710. }
  711. addr6 = (const struct in6_addr *)&neigh->primary_key;
  712. addr_type = ipv6_addr_type(addr6);
  713. if ((addr_type & IPV6_ADDR_UNICAST) &&
  714. ipv6_addr_is_isatap(addr6))
  715. dst = addr6->s6_addr32[3];
  716. else
  717. do_tx_error = true;
  718. neigh_release(neigh);
  719. if (do_tx_error)
  720. goto tx_error;
  721. }
  722. if (!dst)
  723. dst = try_6rd(tunnel, &iph6->daddr);
  724. if (!dst) {
  725. struct neighbour *neigh = NULL;
  726. bool do_tx_error = false;
  727. if (skb_dst(skb))
  728. neigh = dst_neigh_lookup(skb_dst(skb), &iph6->daddr);
  729. if (neigh == NULL) {
  730. net_dbg_ratelimited("nexthop == NULL\n");
  731. goto tx_error;
  732. }
  733. addr6 = (const struct in6_addr *)&neigh->primary_key;
  734. addr_type = ipv6_addr_type(addr6);
  735. if (addr_type == IPV6_ADDR_ANY) {
  736. addr6 = &ipv6_hdr(skb)->daddr;
  737. addr_type = ipv6_addr_type(addr6);
  738. }
  739. if ((addr_type & IPV6_ADDR_COMPATv4) != 0)
  740. dst = addr6->s6_addr32[3];
  741. else
  742. do_tx_error = true;
  743. neigh_release(neigh);
  744. if (do_tx_error)
  745. goto tx_error;
  746. }
  747. rt = ip_route_output_ports(tunnel->net, &fl4, NULL,
  748. dst, tiph->saddr,
  749. 0, 0,
  750. IPPROTO_IPV6, RT_TOS(tos),
  751. tunnel->parms.link);
  752. if (IS_ERR(rt)) {
  753. dev->stats.tx_carrier_errors++;
  754. goto tx_error_icmp;
  755. }
  756. if (rt->rt_type != RTN_UNICAST) {
  757. ip_rt_put(rt);
  758. dev->stats.tx_carrier_errors++;
  759. goto tx_error_icmp;
  760. }
  761. tdev = rt->dst.dev;
  762. if (tdev == dev) {
  763. ip_rt_put(rt);
  764. dev->stats.collisions++;
  765. goto tx_error;
  766. }
  767. skb = iptunnel_handle_offloads(skb, false, SKB_GSO_SIT);
  768. if (IS_ERR(skb)) {
  769. ip_rt_put(rt);
  770. goto out;
  771. }
  772. if (df) {
  773. mtu = dst_mtu(&rt->dst) - t_hlen;
  774. if (mtu < 68) {
  775. dev->stats.collisions++;
  776. ip_rt_put(rt);
  777. goto tx_error;
  778. }
  779. if (mtu < IPV6_MIN_MTU) {
  780. mtu = IPV6_MIN_MTU;
  781. df = 0;
  782. }
  783. if (tunnel->parms.iph.daddr && skb_dst(skb))
  784. skb_dst(skb)->ops->update_pmtu(skb_dst(skb), NULL, skb, mtu);
  785. if (skb->len > mtu && !skb_is_gso(skb)) {
  786. icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
  787. ip_rt_put(rt);
  788. goto tx_error;
  789. }
  790. }
  791. if (tunnel->err_count > 0) {
  792. if (time_before(jiffies,
  793. tunnel->err_time + IPTUNNEL_ERR_TIMEO)) {
  794. tunnel->err_count--;
  795. dst_link_failure(skb);
  796. } else
  797. tunnel->err_count = 0;
  798. }
  799. /*
  800. * Okay, now see if we can stuff it in the buffer as-is.
  801. */
  802. max_headroom = LL_RESERVED_SPACE(tdev) + t_hlen;
  803. if (skb_headroom(skb) < max_headroom || skb_shared(skb) ||
  804. (skb_cloned(skb) && !skb_clone_writable(skb, 0))) {
  805. struct sk_buff *new_skb = skb_realloc_headroom(skb, max_headroom);
  806. if (!new_skb) {
  807. ip_rt_put(rt);
  808. dev->stats.tx_dropped++;
  809. kfree_skb(skb);
  810. return NETDEV_TX_OK;
  811. }
  812. if (skb->sk)
  813. skb_set_owner_w(new_skb, skb->sk);
  814. dev_kfree_skb(skb);
  815. skb = new_skb;
  816. iph6 = ipv6_hdr(skb);
  817. }
  818. ttl = tiph->ttl;
  819. if (ttl == 0)
  820. ttl = iph6->hop_limit;
  821. tos = INET_ECN_encapsulate(tos, ipv6_get_dsfield(iph6));
  822. if (ip_tunnel_encap(skb, tunnel, &protocol, &fl4) < 0) {
  823. ip_rt_put(rt);
  824. goto tx_error;
  825. }
  826. skb_set_inner_ipproto(skb, IPPROTO_IPV6);
  827. err = iptunnel_xmit(skb->sk, rt, skb, fl4.saddr, fl4.daddr,
  828. protocol, tos, ttl, df,
  829. !net_eq(tunnel->net, dev_net(dev)));
  830. iptunnel_xmit_stats(err, &dev->stats, dev->tstats);
  831. return NETDEV_TX_OK;
  832. tx_error_icmp:
  833. dst_link_failure(skb);
  834. tx_error:
  835. kfree_skb(skb);
  836. out:
  837. dev->stats.tx_errors++;
  838. return NETDEV_TX_OK;
  839. }
  840. static netdev_tx_t ipip_tunnel_xmit(struct sk_buff *skb, struct net_device *dev)
  841. {
  842. struct ip_tunnel *tunnel = netdev_priv(dev);
  843. const struct iphdr *tiph = &tunnel->parms.iph;
  844. skb = iptunnel_handle_offloads(skb, false, SKB_GSO_IPIP);
  845. if (IS_ERR(skb))
  846. goto out;
  847. skb_set_inner_ipproto(skb, IPPROTO_IPIP);
  848. ip_tunnel_xmit(skb, dev, tiph, IPPROTO_IPIP);
  849. return NETDEV_TX_OK;
  850. out:
  851. dev->stats.tx_errors++;
  852. return NETDEV_TX_OK;
  853. }
  854. static netdev_tx_t sit_tunnel_xmit(struct sk_buff *skb,
  855. struct net_device *dev)
  856. {
  857. switch (skb->protocol) {
  858. case htons(ETH_P_IP):
  859. ipip_tunnel_xmit(skb, dev);
  860. break;
  861. case htons(ETH_P_IPV6):
  862. ipip6_tunnel_xmit(skb, dev);
  863. break;
  864. default:
  865. goto tx_err;
  866. }
  867. return NETDEV_TX_OK;
  868. tx_err:
  869. dev->stats.tx_errors++;
  870. kfree_skb(skb);
  871. return NETDEV_TX_OK;
  872. }
  873. static void ipip6_tunnel_bind_dev(struct net_device *dev)
  874. {
  875. struct net_device *tdev = NULL;
  876. struct ip_tunnel *tunnel;
  877. const struct iphdr *iph;
  878. struct flowi4 fl4;
  879. tunnel = netdev_priv(dev);
  880. iph = &tunnel->parms.iph;
  881. if (iph->daddr) {
  882. struct rtable *rt = ip_route_output_ports(tunnel->net, &fl4,
  883. NULL,
  884. iph->daddr, iph->saddr,
  885. 0, 0,
  886. IPPROTO_IPV6,
  887. RT_TOS(iph->tos),
  888. tunnel->parms.link);
  889. if (!IS_ERR(rt)) {
  890. tdev = rt->dst.dev;
  891. ip_rt_put(rt);
  892. }
  893. dev->flags |= IFF_POINTOPOINT;
  894. }
  895. if (!tdev && tunnel->parms.link)
  896. tdev = __dev_get_by_index(tunnel->net, tunnel->parms.link);
  897. if (tdev) {
  898. int t_hlen = tunnel->hlen + sizeof(struct iphdr);
  899. dev->hard_header_len = tdev->hard_header_len + sizeof(struct iphdr);
  900. dev->mtu = tdev->mtu - t_hlen;
  901. if (dev->mtu < IPV6_MIN_MTU)
  902. dev->mtu = IPV6_MIN_MTU;
  903. }
  904. dev->iflink = tunnel->parms.link;
  905. }
  906. static void ipip6_tunnel_update(struct ip_tunnel *t, struct ip_tunnel_parm *p)
  907. {
  908. struct net *net = t->net;
  909. struct sit_net *sitn = net_generic(net, sit_net_id);
  910. ipip6_tunnel_unlink(sitn, t);
  911. synchronize_net();
  912. t->parms.iph.saddr = p->iph.saddr;
  913. t->parms.iph.daddr = p->iph.daddr;
  914. memcpy(t->dev->dev_addr, &p->iph.saddr, 4);
  915. memcpy(t->dev->broadcast, &p->iph.daddr, 4);
  916. ipip6_tunnel_link(sitn, t);
  917. t->parms.iph.ttl = p->iph.ttl;
  918. t->parms.iph.tos = p->iph.tos;
  919. if (t->parms.link != p->link) {
  920. t->parms.link = p->link;
  921. ipip6_tunnel_bind_dev(t->dev);
  922. }
  923. ip_tunnel_dst_reset_all(t);
  924. netdev_state_change(t->dev);
  925. }
  926. #ifdef CONFIG_IPV6_SIT_6RD
  927. static int ipip6_tunnel_update_6rd(struct ip_tunnel *t,
  928. struct ip_tunnel_6rd *ip6rd)
  929. {
  930. struct in6_addr prefix;
  931. __be32 relay_prefix;
  932. if (ip6rd->relay_prefixlen > 32 ||
  933. ip6rd->prefixlen + (32 - ip6rd->relay_prefixlen) > 64)
  934. return -EINVAL;
  935. ipv6_addr_prefix(&prefix, &ip6rd->prefix, ip6rd->prefixlen);
  936. if (!ipv6_addr_equal(&prefix, &ip6rd->prefix))
  937. return -EINVAL;
  938. if (ip6rd->relay_prefixlen)
  939. relay_prefix = ip6rd->relay_prefix &
  940. htonl(0xffffffffUL <<
  941. (32 - ip6rd->relay_prefixlen));
  942. else
  943. relay_prefix = 0;
  944. if (relay_prefix != ip6rd->relay_prefix)
  945. return -EINVAL;
  946. t->ip6rd.prefix = prefix;
  947. t->ip6rd.relay_prefix = relay_prefix;
  948. t->ip6rd.prefixlen = ip6rd->prefixlen;
  949. t->ip6rd.relay_prefixlen = ip6rd->relay_prefixlen;
  950. ip_tunnel_dst_reset_all(t);
  951. netdev_state_change(t->dev);
  952. return 0;
  953. }
  954. #endif
  955. static int
  956. ipip6_tunnel_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  957. {
  958. int err = 0;
  959. struct ip_tunnel_parm p;
  960. struct ip_tunnel_prl prl;
  961. struct ip_tunnel *t = netdev_priv(dev);
  962. struct net *net = t->net;
  963. struct sit_net *sitn = net_generic(net, sit_net_id);
  964. #ifdef CONFIG_IPV6_SIT_6RD
  965. struct ip_tunnel_6rd ip6rd;
  966. #endif
  967. switch (cmd) {
  968. case SIOCGETTUNNEL:
  969. #ifdef CONFIG_IPV6_SIT_6RD
  970. case SIOCGET6RD:
  971. #endif
  972. if (dev == sitn->fb_tunnel_dev) {
  973. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) {
  974. err = -EFAULT;
  975. break;
  976. }
  977. t = ipip6_tunnel_locate(net, &p, 0);
  978. if (t == NULL)
  979. t = netdev_priv(dev);
  980. }
  981. err = -EFAULT;
  982. if (cmd == SIOCGETTUNNEL) {
  983. memcpy(&p, &t->parms, sizeof(p));
  984. if (copy_to_user(ifr->ifr_ifru.ifru_data, &p,
  985. sizeof(p)))
  986. goto done;
  987. #ifdef CONFIG_IPV6_SIT_6RD
  988. } else {
  989. ip6rd.prefix = t->ip6rd.prefix;
  990. ip6rd.relay_prefix = t->ip6rd.relay_prefix;
  991. ip6rd.prefixlen = t->ip6rd.prefixlen;
  992. ip6rd.relay_prefixlen = t->ip6rd.relay_prefixlen;
  993. if (copy_to_user(ifr->ifr_ifru.ifru_data, &ip6rd,
  994. sizeof(ip6rd)))
  995. goto done;
  996. #endif
  997. }
  998. err = 0;
  999. break;
  1000. case SIOCADDTUNNEL:
  1001. case SIOCCHGTUNNEL:
  1002. err = -EPERM;
  1003. if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  1004. goto done;
  1005. err = -EFAULT;
  1006. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
  1007. goto done;
  1008. err = -EINVAL;
  1009. if (p.iph.protocol != IPPROTO_IPV6 &&
  1010. p.iph.protocol != IPPROTO_IPIP &&
  1011. p.iph.protocol != 0)
  1012. goto done;
  1013. if (p.iph.version != 4 ||
  1014. p.iph.ihl != 5 || (p.iph.frag_off&htons(~IP_DF)))
  1015. goto done;
  1016. if (p.iph.ttl)
  1017. p.iph.frag_off |= htons(IP_DF);
  1018. t = ipip6_tunnel_locate(net, &p, cmd == SIOCADDTUNNEL);
  1019. if (dev != sitn->fb_tunnel_dev && cmd == SIOCCHGTUNNEL) {
  1020. if (t != NULL) {
  1021. if (t->dev != dev) {
  1022. err = -EEXIST;
  1023. break;
  1024. }
  1025. } else {
  1026. if (((dev->flags&IFF_POINTOPOINT) && !p.iph.daddr) ||
  1027. (!(dev->flags&IFF_POINTOPOINT) && p.iph.daddr)) {
  1028. err = -EINVAL;
  1029. break;
  1030. }
  1031. t = netdev_priv(dev);
  1032. }
  1033. ipip6_tunnel_update(t, &p);
  1034. }
  1035. if (t) {
  1036. err = 0;
  1037. if (copy_to_user(ifr->ifr_ifru.ifru_data, &t->parms, sizeof(p)))
  1038. err = -EFAULT;
  1039. } else
  1040. err = (cmd == SIOCADDTUNNEL ? -ENOBUFS : -ENOENT);
  1041. break;
  1042. case SIOCDELTUNNEL:
  1043. err = -EPERM;
  1044. if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  1045. goto done;
  1046. if (dev == sitn->fb_tunnel_dev) {
  1047. err = -EFAULT;
  1048. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
  1049. goto done;
  1050. err = -ENOENT;
  1051. t = ipip6_tunnel_locate(net, &p, 0);
  1052. if (t == NULL)
  1053. goto done;
  1054. err = -EPERM;
  1055. if (t == netdev_priv(sitn->fb_tunnel_dev))
  1056. goto done;
  1057. dev = t->dev;
  1058. }
  1059. unregister_netdevice(dev);
  1060. err = 0;
  1061. break;
  1062. case SIOCGETPRL:
  1063. err = -EINVAL;
  1064. if (dev == sitn->fb_tunnel_dev)
  1065. goto done;
  1066. err = ipip6_tunnel_get_prl(t, ifr->ifr_ifru.ifru_data);
  1067. break;
  1068. case SIOCADDPRL:
  1069. case SIOCDELPRL:
  1070. case SIOCCHGPRL:
  1071. err = -EPERM;
  1072. if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  1073. goto done;
  1074. err = -EINVAL;
  1075. if (dev == sitn->fb_tunnel_dev)
  1076. goto done;
  1077. err = -EFAULT;
  1078. if (copy_from_user(&prl, ifr->ifr_ifru.ifru_data, sizeof(prl)))
  1079. goto done;
  1080. switch (cmd) {
  1081. case SIOCDELPRL:
  1082. err = ipip6_tunnel_del_prl(t, &prl);
  1083. break;
  1084. case SIOCADDPRL:
  1085. case SIOCCHGPRL:
  1086. err = ipip6_tunnel_add_prl(t, &prl, cmd == SIOCCHGPRL);
  1087. break;
  1088. }
  1089. ip_tunnel_dst_reset_all(t);
  1090. netdev_state_change(dev);
  1091. break;
  1092. #ifdef CONFIG_IPV6_SIT_6RD
  1093. case SIOCADD6RD:
  1094. case SIOCCHG6RD:
  1095. case SIOCDEL6RD:
  1096. err = -EPERM;
  1097. if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  1098. goto done;
  1099. err = -EFAULT;
  1100. if (copy_from_user(&ip6rd, ifr->ifr_ifru.ifru_data,
  1101. sizeof(ip6rd)))
  1102. goto done;
  1103. if (cmd != SIOCDEL6RD) {
  1104. err = ipip6_tunnel_update_6rd(t, &ip6rd);
  1105. if (err < 0)
  1106. goto done;
  1107. } else
  1108. ipip6_tunnel_clone_6rd(dev, sitn);
  1109. err = 0;
  1110. break;
  1111. #endif
  1112. default:
  1113. err = -EINVAL;
  1114. }
  1115. done:
  1116. return err;
  1117. }
  1118. static int ipip6_tunnel_change_mtu(struct net_device *dev, int new_mtu)
  1119. {
  1120. struct ip_tunnel *tunnel = netdev_priv(dev);
  1121. int t_hlen = tunnel->hlen + sizeof(struct iphdr);
  1122. if (new_mtu < IPV6_MIN_MTU || new_mtu > 0xFFF8 - t_hlen)
  1123. return -EINVAL;
  1124. dev->mtu = new_mtu;
  1125. return 0;
  1126. }
  1127. static const struct net_device_ops ipip6_netdev_ops = {
  1128. .ndo_init = ipip6_tunnel_init,
  1129. .ndo_uninit = ipip6_tunnel_uninit,
  1130. .ndo_start_xmit = sit_tunnel_xmit,
  1131. .ndo_do_ioctl = ipip6_tunnel_ioctl,
  1132. .ndo_change_mtu = ipip6_tunnel_change_mtu,
  1133. .ndo_get_stats64 = ip_tunnel_get_stats64,
  1134. };
  1135. static void ipip6_dev_free(struct net_device *dev)
  1136. {
  1137. struct ip_tunnel *tunnel = netdev_priv(dev);
  1138. free_percpu(tunnel->dst_cache);
  1139. free_percpu(dev->tstats);
  1140. free_netdev(dev);
  1141. }
  1142. #define SIT_FEATURES (NETIF_F_SG | \
  1143. NETIF_F_FRAGLIST | \
  1144. NETIF_F_HIGHDMA | \
  1145. NETIF_F_GSO_SOFTWARE | \
  1146. NETIF_F_HW_CSUM)
  1147. static void ipip6_tunnel_setup(struct net_device *dev)
  1148. {
  1149. struct ip_tunnel *tunnel = netdev_priv(dev);
  1150. int t_hlen = tunnel->hlen + sizeof(struct iphdr);
  1151. dev->netdev_ops = &ipip6_netdev_ops;
  1152. dev->destructor = ipip6_dev_free;
  1153. dev->type = ARPHRD_SIT;
  1154. dev->hard_header_len = LL_MAX_HEADER + t_hlen;
  1155. dev->mtu = ETH_DATA_LEN - t_hlen;
  1156. dev->flags = IFF_NOARP;
  1157. netif_keep_dst(dev);
  1158. dev->iflink = 0;
  1159. dev->addr_len = 4;
  1160. dev->features |= NETIF_F_LLTX;
  1161. dev->features |= SIT_FEATURES;
  1162. dev->hw_features |= SIT_FEATURES;
  1163. }
  1164. static int ipip6_tunnel_init(struct net_device *dev)
  1165. {
  1166. struct ip_tunnel *tunnel = netdev_priv(dev);
  1167. tunnel->dev = dev;
  1168. tunnel->net = dev_net(dev);
  1169. strcpy(tunnel->parms.name, dev->name);
  1170. ipip6_tunnel_bind_dev(dev);
  1171. dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
  1172. if (!dev->tstats)
  1173. return -ENOMEM;
  1174. tunnel->dst_cache = alloc_percpu(struct ip_tunnel_dst);
  1175. if (!tunnel->dst_cache) {
  1176. free_percpu(dev->tstats);
  1177. return -ENOMEM;
  1178. }
  1179. return 0;
  1180. }
  1181. static int __net_init ipip6_fb_tunnel_init(struct net_device *dev)
  1182. {
  1183. struct ip_tunnel *tunnel = netdev_priv(dev);
  1184. struct iphdr *iph = &tunnel->parms.iph;
  1185. struct net *net = dev_net(dev);
  1186. struct sit_net *sitn = net_generic(net, sit_net_id);
  1187. tunnel->dev = dev;
  1188. tunnel->net = dev_net(dev);
  1189. iph->version = 4;
  1190. iph->protocol = IPPROTO_IPV6;
  1191. iph->ihl = 5;
  1192. iph->ttl = 64;
  1193. dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
  1194. if (!dev->tstats)
  1195. return -ENOMEM;
  1196. tunnel->dst_cache = alloc_percpu(struct ip_tunnel_dst);
  1197. if (!tunnel->dst_cache) {
  1198. free_percpu(dev->tstats);
  1199. return -ENOMEM;
  1200. }
  1201. dev_hold(dev);
  1202. rcu_assign_pointer(sitn->tunnels_wc[0], tunnel);
  1203. return 0;
  1204. }
  1205. static int ipip6_validate(struct nlattr *tb[], struct nlattr *data[])
  1206. {
  1207. u8 proto;
  1208. if (!data || !data[IFLA_IPTUN_PROTO])
  1209. return 0;
  1210. proto = nla_get_u8(data[IFLA_IPTUN_PROTO]);
  1211. if (proto != IPPROTO_IPV6 &&
  1212. proto != IPPROTO_IPIP &&
  1213. proto != 0)
  1214. return -EINVAL;
  1215. return 0;
  1216. }
  1217. static void ipip6_netlink_parms(struct nlattr *data[],
  1218. struct ip_tunnel_parm *parms)
  1219. {
  1220. memset(parms, 0, sizeof(*parms));
  1221. parms->iph.version = 4;
  1222. parms->iph.protocol = IPPROTO_IPV6;
  1223. parms->iph.ihl = 5;
  1224. parms->iph.ttl = 64;
  1225. if (!data)
  1226. return;
  1227. if (data[IFLA_IPTUN_LINK])
  1228. parms->link = nla_get_u32(data[IFLA_IPTUN_LINK]);
  1229. if (data[IFLA_IPTUN_LOCAL])
  1230. parms->iph.saddr = nla_get_be32(data[IFLA_IPTUN_LOCAL]);
  1231. if (data[IFLA_IPTUN_REMOTE])
  1232. parms->iph.daddr = nla_get_be32(data[IFLA_IPTUN_REMOTE]);
  1233. if (data[IFLA_IPTUN_TTL]) {
  1234. parms->iph.ttl = nla_get_u8(data[IFLA_IPTUN_TTL]);
  1235. if (parms->iph.ttl)
  1236. parms->iph.frag_off = htons(IP_DF);
  1237. }
  1238. if (data[IFLA_IPTUN_TOS])
  1239. parms->iph.tos = nla_get_u8(data[IFLA_IPTUN_TOS]);
  1240. if (!data[IFLA_IPTUN_PMTUDISC] || nla_get_u8(data[IFLA_IPTUN_PMTUDISC]))
  1241. parms->iph.frag_off = htons(IP_DF);
  1242. if (data[IFLA_IPTUN_FLAGS])
  1243. parms->i_flags = nla_get_be16(data[IFLA_IPTUN_FLAGS]);
  1244. if (data[IFLA_IPTUN_PROTO])
  1245. parms->iph.protocol = nla_get_u8(data[IFLA_IPTUN_PROTO]);
  1246. }
  1247. /* This function returns true when ENCAP attributes are present in the nl msg */
  1248. static bool ipip6_netlink_encap_parms(struct nlattr *data[],
  1249. struct ip_tunnel_encap *ipencap)
  1250. {
  1251. bool ret = false;
  1252. memset(ipencap, 0, sizeof(*ipencap));
  1253. if (!data)
  1254. return ret;
  1255. if (data[IFLA_IPTUN_ENCAP_TYPE]) {
  1256. ret = true;
  1257. ipencap->type = nla_get_u16(data[IFLA_IPTUN_ENCAP_TYPE]);
  1258. }
  1259. if (data[IFLA_IPTUN_ENCAP_FLAGS]) {
  1260. ret = true;
  1261. ipencap->flags = nla_get_u16(data[IFLA_IPTUN_ENCAP_FLAGS]);
  1262. }
  1263. if (data[IFLA_IPTUN_ENCAP_SPORT]) {
  1264. ret = true;
  1265. ipencap->sport = nla_get_be16(data[IFLA_IPTUN_ENCAP_SPORT]);
  1266. }
  1267. if (data[IFLA_IPTUN_ENCAP_DPORT]) {
  1268. ret = true;
  1269. ipencap->dport = nla_get_be16(data[IFLA_IPTUN_ENCAP_DPORT]);
  1270. }
  1271. return ret;
  1272. }
  1273. #ifdef CONFIG_IPV6_SIT_6RD
  1274. /* This function returns true when 6RD attributes are present in the nl msg */
  1275. static bool ipip6_netlink_6rd_parms(struct nlattr *data[],
  1276. struct ip_tunnel_6rd *ip6rd)
  1277. {
  1278. bool ret = false;
  1279. memset(ip6rd, 0, sizeof(*ip6rd));
  1280. if (!data)
  1281. return ret;
  1282. if (data[IFLA_IPTUN_6RD_PREFIX]) {
  1283. ret = true;
  1284. nla_memcpy(&ip6rd->prefix, data[IFLA_IPTUN_6RD_PREFIX],
  1285. sizeof(struct in6_addr));
  1286. }
  1287. if (data[IFLA_IPTUN_6RD_RELAY_PREFIX]) {
  1288. ret = true;
  1289. ip6rd->relay_prefix =
  1290. nla_get_be32(data[IFLA_IPTUN_6RD_RELAY_PREFIX]);
  1291. }
  1292. if (data[IFLA_IPTUN_6RD_PREFIXLEN]) {
  1293. ret = true;
  1294. ip6rd->prefixlen = nla_get_u16(data[IFLA_IPTUN_6RD_PREFIXLEN]);
  1295. }
  1296. if (data[IFLA_IPTUN_6RD_RELAY_PREFIXLEN]) {
  1297. ret = true;
  1298. ip6rd->relay_prefixlen =
  1299. nla_get_u16(data[IFLA_IPTUN_6RD_RELAY_PREFIXLEN]);
  1300. }
  1301. return ret;
  1302. }
  1303. #endif
  1304. static int ipip6_newlink(struct net *src_net, struct net_device *dev,
  1305. struct nlattr *tb[], struct nlattr *data[])
  1306. {
  1307. struct net *net = dev_net(dev);
  1308. struct ip_tunnel *nt;
  1309. struct ip_tunnel_encap ipencap;
  1310. #ifdef CONFIG_IPV6_SIT_6RD
  1311. struct ip_tunnel_6rd ip6rd;
  1312. #endif
  1313. int err;
  1314. nt = netdev_priv(dev);
  1315. if (ipip6_netlink_encap_parms(data, &ipencap)) {
  1316. err = ip_tunnel_encap_setup(nt, &ipencap);
  1317. if (err < 0)
  1318. return err;
  1319. }
  1320. ipip6_netlink_parms(data, &nt->parms);
  1321. if (ipip6_tunnel_locate(net, &nt->parms, 0))
  1322. return -EEXIST;
  1323. err = ipip6_tunnel_create(dev);
  1324. if (err < 0)
  1325. return err;
  1326. #ifdef CONFIG_IPV6_SIT_6RD
  1327. if (ipip6_netlink_6rd_parms(data, &ip6rd))
  1328. err = ipip6_tunnel_update_6rd(nt, &ip6rd);
  1329. #endif
  1330. return err;
  1331. }
  1332. static int ipip6_changelink(struct net_device *dev, struct nlattr *tb[],
  1333. struct nlattr *data[])
  1334. {
  1335. struct ip_tunnel *t = netdev_priv(dev);
  1336. struct ip_tunnel_parm p;
  1337. struct ip_tunnel_encap ipencap;
  1338. struct net *net = t->net;
  1339. struct sit_net *sitn = net_generic(net, sit_net_id);
  1340. #ifdef CONFIG_IPV6_SIT_6RD
  1341. struct ip_tunnel_6rd ip6rd;
  1342. #endif
  1343. int err;
  1344. if (dev == sitn->fb_tunnel_dev)
  1345. return -EINVAL;
  1346. if (ipip6_netlink_encap_parms(data, &ipencap)) {
  1347. err = ip_tunnel_encap_setup(t, &ipencap);
  1348. if (err < 0)
  1349. return err;
  1350. }
  1351. ipip6_netlink_parms(data, &p);
  1352. if (((dev->flags & IFF_POINTOPOINT) && !p.iph.daddr) ||
  1353. (!(dev->flags & IFF_POINTOPOINT) && p.iph.daddr))
  1354. return -EINVAL;
  1355. t = ipip6_tunnel_locate(net, &p, 0);
  1356. if (t) {
  1357. if (t->dev != dev)
  1358. return -EEXIST;
  1359. } else
  1360. t = netdev_priv(dev);
  1361. ipip6_tunnel_update(t, &p);
  1362. #ifdef CONFIG_IPV6_SIT_6RD
  1363. if (ipip6_netlink_6rd_parms(data, &ip6rd))
  1364. return ipip6_tunnel_update_6rd(t, &ip6rd);
  1365. #endif
  1366. return 0;
  1367. }
  1368. static size_t ipip6_get_size(const struct net_device *dev)
  1369. {
  1370. return
  1371. /* IFLA_IPTUN_LINK */
  1372. nla_total_size(4) +
  1373. /* IFLA_IPTUN_LOCAL */
  1374. nla_total_size(4) +
  1375. /* IFLA_IPTUN_REMOTE */
  1376. nla_total_size(4) +
  1377. /* IFLA_IPTUN_TTL */
  1378. nla_total_size(1) +
  1379. /* IFLA_IPTUN_TOS */
  1380. nla_total_size(1) +
  1381. /* IFLA_IPTUN_PMTUDISC */
  1382. nla_total_size(1) +
  1383. /* IFLA_IPTUN_FLAGS */
  1384. nla_total_size(2) +
  1385. /* IFLA_IPTUN_PROTO */
  1386. nla_total_size(1) +
  1387. #ifdef CONFIG_IPV6_SIT_6RD
  1388. /* IFLA_IPTUN_6RD_PREFIX */
  1389. nla_total_size(sizeof(struct in6_addr)) +
  1390. /* IFLA_IPTUN_6RD_RELAY_PREFIX */
  1391. nla_total_size(4) +
  1392. /* IFLA_IPTUN_6RD_PREFIXLEN */
  1393. nla_total_size(2) +
  1394. /* IFLA_IPTUN_6RD_RELAY_PREFIXLEN */
  1395. nla_total_size(2) +
  1396. #endif
  1397. /* IFLA_IPTUN_ENCAP_TYPE */
  1398. nla_total_size(2) +
  1399. /* IFLA_IPTUN_ENCAP_FLAGS */
  1400. nla_total_size(2) +
  1401. /* IFLA_IPTUN_ENCAP_SPORT */
  1402. nla_total_size(2) +
  1403. /* IFLA_IPTUN_ENCAP_DPORT */
  1404. nla_total_size(2) +
  1405. 0;
  1406. }
  1407. static int ipip6_fill_info(struct sk_buff *skb, const struct net_device *dev)
  1408. {
  1409. struct ip_tunnel *tunnel = netdev_priv(dev);
  1410. struct ip_tunnel_parm *parm = &tunnel->parms;
  1411. if (nla_put_u32(skb, IFLA_IPTUN_LINK, parm->link) ||
  1412. nla_put_be32(skb, IFLA_IPTUN_LOCAL, parm->iph.saddr) ||
  1413. nla_put_be32(skb, IFLA_IPTUN_REMOTE, parm->iph.daddr) ||
  1414. nla_put_u8(skb, IFLA_IPTUN_TTL, parm->iph.ttl) ||
  1415. nla_put_u8(skb, IFLA_IPTUN_TOS, parm->iph.tos) ||
  1416. nla_put_u8(skb, IFLA_IPTUN_PMTUDISC,
  1417. !!(parm->iph.frag_off & htons(IP_DF))) ||
  1418. nla_put_u8(skb, IFLA_IPTUN_PROTO, parm->iph.protocol) ||
  1419. nla_put_be16(skb, IFLA_IPTUN_FLAGS, parm->i_flags))
  1420. goto nla_put_failure;
  1421. #ifdef CONFIG_IPV6_SIT_6RD
  1422. if (nla_put(skb, IFLA_IPTUN_6RD_PREFIX, sizeof(struct in6_addr),
  1423. &tunnel->ip6rd.prefix) ||
  1424. nla_put_be32(skb, IFLA_IPTUN_6RD_RELAY_PREFIX,
  1425. tunnel->ip6rd.relay_prefix) ||
  1426. nla_put_u16(skb, IFLA_IPTUN_6RD_PREFIXLEN,
  1427. tunnel->ip6rd.prefixlen) ||
  1428. nla_put_u16(skb, IFLA_IPTUN_6RD_RELAY_PREFIXLEN,
  1429. tunnel->ip6rd.relay_prefixlen))
  1430. goto nla_put_failure;
  1431. #endif
  1432. if (nla_put_u16(skb, IFLA_IPTUN_ENCAP_TYPE,
  1433. tunnel->encap.type) ||
  1434. nla_put_be16(skb, IFLA_IPTUN_ENCAP_SPORT,
  1435. tunnel->encap.sport) ||
  1436. nla_put_be16(skb, IFLA_IPTUN_ENCAP_DPORT,
  1437. tunnel->encap.dport) ||
  1438. nla_put_u16(skb, IFLA_IPTUN_ENCAP_FLAGS,
  1439. tunnel->encap.flags))
  1440. goto nla_put_failure;
  1441. return 0;
  1442. nla_put_failure:
  1443. return -EMSGSIZE;
  1444. }
  1445. static const struct nla_policy ipip6_policy[IFLA_IPTUN_MAX + 1] = {
  1446. [IFLA_IPTUN_LINK] = { .type = NLA_U32 },
  1447. [IFLA_IPTUN_LOCAL] = { .type = NLA_U32 },
  1448. [IFLA_IPTUN_REMOTE] = { .type = NLA_U32 },
  1449. [IFLA_IPTUN_TTL] = { .type = NLA_U8 },
  1450. [IFLA_IPTUN_TOS] = { .type = NLA_U8 },
  1451. [IFLA_IPTUN_PMTUDISC] = { .type = NLA_U8 },
  1452. [IFLA_IPTUN_FLAGS] = { .type = NLA_U16 },
  1453. [IFLA_IPTUN_PROTO] = { .type = NLA_U8 },
  1454. #ifdef CONFIG_IPV6_SIT_6RD
  1455. [IFLA_IPTUN_6RD_PREFIX] = { .len = sizeof(struct in6_addr) },
  1456. [IFLA_IPTUN_6RD_RELAY_PREFIX] = { .type = NLA_U32 },
  1457. [IFLA_IPTUN_6RD_PREFIXLEN] = { .type = NLA_U16 },
  1458. [IFLA_IPTUN_6RD_RELAY_PREFIXLEN] = { .type = NLA_U16 },
  1459. #endif
  1460. [IFLA_IPTUN_ENCAP_TYPE] = { .type = NLA_U16 },
  1461. [IFLA_IPTUN_ENCAP_FLAGS] = { .type = NLA_U16 },
  1462. [IFLA_IPTUN_ENCAP_SPORT] = { .type = NLA_U16 },
  1463. [IFLA_IPTUN_ENCAP_DPORT] = { .type = NLA_U16 },
  1464. };
  1465. static void ipip6_dellink(struct net_device *dev, struct list_head *head)
  1466. {
  1467. struct net *net = dev_net(dev);
  1468. struct sit_net *sitn = net_generic(net, sit_net_id);
  1469. if (dev != sitn->fb_tunnel_dev)
  1470. unregister_netdevice_queue(dev, head);
  1471. }
  1472. static struct rtnl_link_ops sit_link_ops __read_mostly = {
  1473. .kind = "sit",
  1474. .maxtype = IFLA_IPTUN_MAX,
  1475. .policy = ipip6_policy,
  1476. .priv_size = sizeof(struct ip_tunnel),
  1477. .setup = ipip6_tunnel_setup,
  1478. .validate = ipip6_validate,
  1479. .newlink = ipip6_newlink,
  1480. .changelink = ipip6_changelink,
  1481. .get_size = ipip6_get_size,
  1482. .fill_info = ipip6_fill_info,
  1483. .dellink = ipip6_dellink,
  1484. .get_link_net = ip_tunnel_get_link_net,
  1485. };
  1486. static struct xfrm_tunnel sit_handler __read_mostly = {
  1487. .handler = ipip6_rcv,
  1488. .err_handler = ipip6_err,
  1489. .priority = 1,
  1490. };
  1491. static struct xfrm_tunnel ipip_handler __read_mostly = {
  1492. .handler = ipip_rcv,
  1493. .err_handler = ipip6_err,
  1494. .priority = 2,
  1495. };
  1496. static void __net_exit sit_destroy_tunnels(struct net *net,
  1497. struct list_head *head)
  1498. {
  1499. struct sit_net *sitn = net_generic(net, sit_net_id);
  1500. struct net_device *dev, *aux;
  1501. int prio;
  1502. for_each_netdev_safe(net, dev, aux)
  1503. if (dev->rtnl_link_ops == &sit_link_ops)
  1504. unregister_netdevice_queue(dev, head);
  1505. for (prio = 1; prio < 4; prio++) {
  1506. int h;
  1507. for (h = 0; h < HASH_SIZE; h++) {
  1508. struct ip_tunnel *t;
  1509. t = rtnl_dereference(sitn->tunnels[prio][h]);
  1510. while (t != NULL) {
  1511. /* If dev is in the same netns, it has already
  1512. * been added to the list by the previous loop.
  1513. */
  1514. if (!net_eq(dev_net(t->dev), net))
  1515. unregister_netdevice_queue(t->dev,
  1516. head);
  1517. t = rtnl_dereference(t->next);
  1518. }
  1519. }
  1520. }
  1521. }
  1522. static int __net_init sit_init_net(struct net *net)
  1523. {
  1524. struct sit_net *sitn = net_generic(net, sit_net_id);
  1525. struct ip_tunnel *t;
  1526. int err;
  1527. sitn->tunnels[0] = sitn->tunnels_wc;
  1528. sitn->tunnels[1] = sitn->tunnels_l;
  1529. sitn->tunnels[2] = sitn->tunnels_r;
  1530. sitn->tunnels[3] = sitn->tunnels_r_l;
  1531. sitn->fb_tunnel_dev = alloc_netdev(sizeof(struct ip_tunnel), "sit0",
  1532. NET_NAME_UNKNOWN,
  1533. ipip6_tunnel_setup);
  1534. if (!sitn->fb_tunnel_dev) {
  1535. err = -ENOMEM;
  1536. goto err_alloc_dev;
  1537. }
  1538. dev_net_set(sitn->fb_tunnel_dev, net);
  1539. sitn->fb_tunnel_dev->rtnl_link_ops = &sit_link_ops;
  1540. /* FB netdevice is special: we have one, and only one per netns.
  1541. * Allowing to move it to another netns is clearly unsafe.
  1542. */
  1543. sitn->fb_tunnel_dev->features |= NETIF_F_NETNS_LOCAL;
  1544. err = ipip6_fb_tunnel_init(sitn->fb_tunnel_dev);
  1545. if (err)
  1546. goto err_dev_free;
  1547. ipip6_tunnel_clone_6rd(sitn->fb_tunnel_dev, sitn);
  1548. err = register_netdev(sitn->fb_tunnel_dev);
  1549. if (err)
  1550. goto err_reg_dev;
  1551. t = netdev_priv(sitn->fb_tunnel_dev);
  1552. strcpy(t->parms.name, sitn->fb_tunnel_dev->name);
  1553. return 0;
  1554. err_reg_dev:
  1555. dev_put(sitn->fb_tunnel_dev);
  1556. err_dev_free:
  1557. ipip6_dev_free(sitn->fb_tunnel_dev);
  1558. err_alloc_dev:
  1559. return err;
  1560. }
  1561. static void __net_exit sit_exit_net(struct net *net)
  1562. {
  1563. LIST_HEAD(list);
  1564. rtnl_lock();
  1565. sit_destroy_tunnels(net, &list);
  1566. unregister_netdevice_many(&list);
  1567. rtnl_unlock();
  1568. }
  1569. static struct pernet_operations sit_net_ops = {
  1570. .init = sit_init_net,
  1571. .exit = sit_exit_net,
  1572. .id = &sit_net_id,
  1573. .size = sizeof(struct sit_net),
  1574. };
  1575. static void __exit sit_cleanup(void)
  1576. {
  1577. rtnl_link_unregister(&sit_link_ops);
  1578. xfrm4_tunnel_deregister(&sit_handler, AF_INET6);
  1579. xfrm4_tunnel_deregister(&ipip_handler, AF_INET);
  1580. unregister_pernet_device(&sit_net_ops);
  1581. rcu_barrier(); /* Wait for completion of call_rcu()'s */
  1582. }
  1583. static int __init sit_init(void)
  1584. {
  1585. int err;
  1586. pr_info("IPv6 over IPv4 tunneling driver\n");
  1587. err = register_pernet_device(&sit_net_ops);
  1588. if (err < 0)
  1589. return err;
  1590. err = xfrm4_tunnel_register(&sit_handler, AF_INET6);
  1591. if (err < 0) {
  1592. pr_info("%s: can't register ip6ip4\n", __func__);
  1593. goto xfrm_tunnel_failed;
  1594. }
  1595. err = xfrm4_tunnel_register(&ipip_handler, AF_INET);
  1596. if (err < 0) {
  1597. pr_info("%s: can't register ip4ip4\n", __func__);
  1598. goto xfrm_tunnel4_failed;
  1599. }
  1600. err = rtnl_link_register(&sit_link_ops);
  1601. if (err < 0)
  1602. goto rtnl_link_failed;
  1603. out:
  1604. return err;
  1605. rtnl_link_failed:
  1606. xfrm4_tunnel_deregister(&ipip_handler, AF_INET);
  1607. xfrm_tunnel4_failed:
  1608. xfrm4_tunnel_deregister(&sit_handler, AF_INET6);
  1609. xfrm_tunnel_failed:
  1610. unregister_pernet_device(&sit_net_ops);
  1611. goto out;
  1612. }
  1613. module_init(sit_init);
  1614. module_exit(sit_cleanup);
  1615. MODULE_LICENSE("GPL");
  1616. MODULE_ALIAS_RTNL_LINK("sit");
  1617. MODULE_ALIAS_NETDEV("sit0");