sit.c 46 KB

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