sit.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627
  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/ipip.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. static struct rtnl_link_stats64 *ipip6_get_stats64(struct net_device *dev,
  79. struct rtnl_link_stats64 *tot)
  80. {
  81. int i;
  82. for_each_possible_cpu(i) {
  83. const struct pcpu_tstats *tstats = per_cpu_ptr(dev->tstats, i);
  84. u64 rx_packets, rx_bytes, tx_packets, tx_bytes;
  85. unsigned int start;
  86. do {
  87. start = u64_stats_fetch_begin_bh(&tstats->syncp);
  88. rx_packets = tstats->rx_packets;
  89. tx_packets = tstats->tx_packets;
  90. rx_bytes = tstats->rx_bytes;
  91. tx_bytes = tstats->tx_bytes;
  92. } while (u64_stats_fetch_retry_bh(&tstats->syncp, start));
  93. tot->rx_packets += rx_packets;
  94. tot->tx_packets += tx_packets;
  95. tot->rx_bytes += rx_bytes;
  96. tot->tx_bytes += tx_bytes;
  97. }
  98. tot->rx_errors = dev->stats.rx_errors;
  99. tot->rx_frame_errors = dev->stats.rx_frame_errors;
  100. tot->tx_fifo_errors = dev->stats.tx_fifo_errors;
  101. tot->tx_carrier_errors = dev->stats.tx_carrier_errors;
  102. tot->tx_dropped = dev->stats.tx_dropped;
  103. tot->tx_aborted_errors = dev->stats.tx_aborted_errors;
  104. tot->tx_errors = dev->stats.tx_errors;
  105. return tot;
  106. }
  107. /*
  108. * Must be invoked with rcu_read_lock
  109. */
  110. static struct ip_tunnel *ipip6_tunnel_lookup(struct net *net,
  111. struct net_device *dev, __be32 remote, __be32 local)
  112. {
  113. unsigned int h0 = HASH(remote);
  114. unsigned int h1 = HASH(local);
  115. struct ip_tunnel *t;
  116. struct sit_net *sitn = net_generic(net, sit_net_id);
  117. for_each_ip_tunnel_rcu(t, sitn->tunnels_r_l[h0 ^ h1]) {
  118. if (local == t->parms.iph.saddr &&
  119. remote == t->parms.iph.daddr &&
  120. (!dev || !t->parms.link || dev->iflink == t->parms.link) &&
  121. (t->dev->flags & IFF_UP))
  122. return t;
  123. }
  124. for_each_ip_tunnel_rcu(t, sitn->tunnels_r[h0]) {
  125. if (remote == t->parms.iph.daddr &&
  126. (!dev || !t->parms.link || dev->iflink == t->parms.link) &&
  127. (t->dev->flags & IFF_UP))
  128. return t;
  129. }
  130. for_each_ip_tunnel_rcu(t, sitn->tunnels_l[h1]) {
  131. if (local == t->parms.iph.saddr &&
  132. (!dev || !t->parms.link || dev->iflink == t->parms.link) &&
  133. (t->dev->flags & IFF_UP))
  134. return t;
  135. }
  136. t = rcu_dereference(sitn->tunnels_wc[0]);
  137. if ((t != NULL) && (t->dev->flags & IFF_UP))
  138. return t;
  139. return NULL;
  140. }
  141. static struct ip_tunnel __rcu **__ipip6_bucket(struct sit_net *sitn,
  142. struct ip_tunnel_parm *parms)
  143. {
  144. __be32 remote = parms->iph.daddr;
  145. __be32 local = parms->iph.saddr;
  146. unsigned int h = 0;
  147. int prio = 0;
  148. if (remote) {
  149. prio |= 2;
  150. h ^= HASH(remote);
  151. }
  152. if (local) {
  153. prio |= 1;
  154. h ^= HASH(local);
  155. }
  156. return &sitn->tunnels[prio][h];
  157. }
  158. static inline struct ip_tunnel __rcu **ipip6_bucket(struct sit_net *sitn,
  159. struct ip_tunnel *t)
  160. {
  161. return __ipip6_bucket(sitn, &t->parms);
  162. }
  163. static void ipip6_tunnel_unlink(struct sit_net *sitn, struct ip_tunnel *t)
  164. {
  165. struct ip_tunnel __rcu **tp;
  166. struct ip_tunnel *iter;
  167. for (tp = ipip6_bucket(sitn, t);
  168. (iter = rtnl_dereference(*tp)) != NULL;
  169. tp = &iter->next) {
  170. if (t == iter) {
  171. rcu_assign_pointer(*tp, t->next);
  172. break;
  173. }
  174. }
  175. }
  176. static void ipip6_tunnel_link(struct sit_net *sitn, struct ip_tunnel *t)
  177. {
  178. struct ip_tunnel __rcu **tp = ipip6_bucket(sitn, t);
  179. rcu_assign_pointer(t->next, rtnl_dereference(*tp));
  180. rcu_assign_pointer(*tp, t);
  181. }
  182. static void ipip6_tunnel_clone_6rd(struct net_device *dev, struct sit_net *sitn)
  183. {
  184. #ifdef CONFIG_IPV6_SIT_6RD
  185. struct ip_tunnel *t = netdev_priv(dev);
  186. if (t->dev == sitn->fb_tunnel_dev) {
  187. ipv6_addr_set(&t->ip6rd.prefix, htonl(0x20020000), 0, 0, 0);
  188. t->ip6rd.relay_prefix = 0;
  189. t->ip6rd.prefixlen = 16;
  190. t->ip6rd.relay_prefixlen = 0;
  191. } else {
  192. struct ip_tunnel *t0 = netdev_priv(sitn->fb_tunnel_dev);
  193. memcpy(&t->ip6rd, &t0->ip6rd, sizeof(t->ip6rd));
  194. }
  195. #endif
  196. }
  197. static int ipip6_tunnel_create(struct net_device *dev)
  198. {
  199. struct ip_tunnel *t = netdev_priv(dev);
  200. struct net *net = dev_net(dev);
  201. struct sit_net *sitn = net_generic(net, sit_net_id);
  202. int err;
  203. err = ipip6_tunnel_init(dev);
  204. if (err < 0)
  205. goto out;
  206. ipip6_tunnel_clone_6rd(dev, sitn);
  207. if ((__force u16)t->parms.i_flags & SIT_ISATAP)
  208. dev->priv_flags |= IFF_ISATAP;
  209. err = register_netdevice(dev);
  210. if (err < 0)
  211. goto out;
  212. strcpy(t->parms.name, dev->name);
  213. dev->rtnl_link_ops = &sit_link_ops;
  214. dev_hold(dev);
  215. ipip6_tunnel_link(sitn, t);
  216. return 0;
  217. out:
  218. return err;
  219. }
  220. static struct ip_tunnel *ipip6_tunnel_locate(struct net *net,
  221. struct ip_tunnel_parm *parms, int create)
  222. {
  223. __be32 remote = parms->iph.daddr;
  224. __be32 local = parms->iph.saddr;
  225. struct ip_tunnel *t, *nt;
  226. struct ip_tunnel __rcu **tp;
  227. struct net_device *dev;
  228. char name[IFNAMSIZ];
  229. struct sit_net *sitn = net_generic(net, sit_net_id);
  230. for (tp = __ipip6_bucket(sitn, parms);
  231. (t = rtnl_dereference(*tp)) != NULL;
  232. tp = &t->next) {
  233. if (local == t->parms.iph.saddr &&
  234. remote == t->parms.iph.daddr &&
  235. parms->link == t->parms.link) {
  236. if (create)
  237. return NULL;
  238. else
  239. return t;
  240. }
  241. }
  242. if (!create)
  243. goto failed;
  244. if (parms->name[0])
  245. strlcpy(name, parms->name, IFNAMSIZ);
  246. else
  247. strcpy(name, "sit%d");
  248. dev = alloc_netdev(sizeof(*t), name, ipip6_tunnel_setup);
  249. if (dev == NULL)
  250. return NULL;
  251. dev_net_set(dev, net);
  252. nt = netdev_priv(dev);
  253. nt->parms = *parms;
  254. if (ipip6_tunnel_create(dev) < 0)
  255. goto failed_free;
  256. return nt;
  257. failed_free:
  258. ipip6_dev_free(dev);
  259. failed:
  260. return NULL;
  261. }
  262. #define for_each_prl_rcu(start) \
  263. for (prl = rcu_dereference(start); \
  264. prl; \
  265. prl = rcu_dereference(prl->next))
  266. static struct ip_tunnel_prl_entry *
  267. __ipip6_tunnel_locate_prl(struct ip_tunnel *t, __be32 addr)
  268. {
  269. struct ip_tunnel_prl_entry *prl;
  270. for_each_prl_rcu(t->prl)
  271. if (prl->addr == addr)
  272. break;
  273. return prl;
  274. }
  275. static int ipip6_tunnel_get_prl(struct ip_tunnel *t,
  276. struct ip_tunnel_prl __user *a)
  277. {
  278. struct ip_tunnel_prl kprl, *kp;
  279. struct ip_tunnel_prl_entry *prl;
  280. unsigned int cmax, c = 0, ca, len;
  281. int ret = 0;
  282. if (copy_from_user(&kprl, a, sizeof(kprl)))
  283. return -EFAULT;
  284. cmax = kprl.datalen / sizeof(kprl);
  285. if (cmax > 1 && kprl.addr != htonl(INADDR_ANY))
  286. cmax = 1;
  287. /* For simple GET or for root users,
  288. * we try harder to allocate.
  289. */
  290. kp = (cmax <= 1 || capable(CAP_NET_ADMIN)) ?
  291. kcalloc(cmax, sizeof(*kp), GFP_KERNEL) :
  292. NULL;
  293. rcu_read_lock();
  294. ca = t->prl_count < cmax ? t->prl_count : cmax;
  295. if (!kp) {
  296. /* We don't try hard to allocate much memory for
  297. * non-root users.
  298. * For root users, retry allocating enough memory for
  299. * the answer.
  300. */
  301. kp = kcalloc(ca, sizeof(*kp), GFP_ATOMIC);
  302. if (!kp) {
  303. ret = -ENOMEM;
  304. goto out;
  305. }
  306. }
  307. c = 0;
  308. for_each_prl_rcu(t->prl) {
  309. if (c >= cmax)
  310. break;
  311. if (kprl.addr != htonl(INADDR_ANY) && prl->addr != kprl.addr)
  312. continue;
  313. kp[c].addr = prl->addr;
  314. kp[c].flags = prl->flags;
  315. c++;
  316. if (kprl.addr != htonl(INADDR_ANY))
  317. break;
  318. }
  319. out:
  320. rcu_read_unlock();
  321. len = sizeof(*kp) * c;
  322. ret = 0;
  323. if ((len && copy_to_user(a + 1, kp, len)) || put_user(len, &a->datalen))
  324. ret = -EFAULT;
  325. kfree(kp);
  326. return ret;
  327. }
  328. static int
  329. ipip6_tunnel_add_prl(struct ip_tunnel *t, struct ip_tunnel_prl *a, int chg)
  330. {
  331. struct ip_tunnel_prl_entry *p;
  332. int err = 0;
  333. if (a->addr == htonl(INADDR_ANY))
  334. return -EINVAL;
  335. ASSERT_RTNL();
  336. for (p = rtnl_dereference(t->prl); p; p = rtnl_dereference(p->next)) {
  337. if (p->addr == a->addr) {
  338. if (chg) {
  339. p->flags = a->flags;
  340. goto out;
  341. }
  342. err = -EEXIST;
  343. goto out;
  344. }
  345. }
  346. if (chg) {
  347. err = -ENXIO;
  348. goto out;
  349. }
  350. p = kzalloc(sizeof(struct ip_tunnel_prl_entry), GFP_KERNEL);
  351. if (!p) {
  352. err = -ENOBUFS;
  353. goto out;
  354. }
  355. p->next = t->prl;
  356. p->addr = a->addr;
  357. p->flags = a->flags;
  358. t->prl_count++;
  359. rcu_assign_pointer(t->prl, p);
  360. out:
  361. return err;
  362. }
  363. static void prl_list_destroy_rcu(struct rcu_head *head)
  364. {
  365. struct ip_tunnel_prl_entry *p, *n;
  366. p = container_of(head, struct ip_tunnel_prl_entry, rcu_head);
  367. do {
  368. n = rcu_dereference_protected(p->next, 1);
  369. kfree(p);
  370. p = n;
  371. } while (p);
  372. }
  373. static int
  374. ipip6_tunnel_del_prl(struct ip_tunnel *t, struct ip_tunnel_prl *a)
  375. {
  376. struct ip_tunnel_prl_entry *x;
  377. struct ip_tunnel_prl_entry __rcu **p;
  378. int err = 0;
  379. ASSERT_RTNL();
  380. if (a && a->addr != htonl(INADDR_ANY)) {
  381. for (p = &t->prl;
  382. (x = rtnl_dereference(*p)) != NULL;
  383. p = &x->next) {
  384. if (x->addr == a->addr) {
  385. *p = x->next;
  386. kfree_rcu(x, rcu_head);
  387. t->prl_count--;
  388. goto out;
  389. }
  390. }
  391. err = -ENXIO;
  392. } else {
  393. x = rtnl_dereference(t->prl);
  394. if (x) {
  395. t->prl_count = 0;
  396. call_rcu(&x->rcu_head, prl_list_destroy_rcu);
  397. t->prl = NULL;
  398. }
  399. }
  400. out:
  401. return err;
  402. }
  403. static int
  404. isatap_chksrc(struct sk_buff *skb, const struct iphdr *iph, struct ip_tunnel *t)
  405. {
  406. struct ip_tunnel_prl_entry *p;
  407. int ok = 1;
  408. rcu_read_lock();
  409. p = __ipip6_tunnel_locate_prl(t, iph->saddr);
  410. if (p) {
  411. if (p->flags & PRL_DEFAULT)
  412. skb->ndisc_nodetype = NDISC_NODETYPE_DEFAULT;
  413. else
  414. skb->ndisc_nodetype = NDISC_NODETYPE_NODEFAULT;
  415. } else {
  416. const struct in6_addr *addr6 = &ipv6_hdr(skb)->saddr;
  417. if (ipv6_addr_is_isatap(addr6) &&
  418. (addr6->s6_addr32[3] == iph->saddr) &&
  419. ipv6_chk_prefix(addr6, t->dev))
  420. skb->ndisc_nodetype = NDISC_NODETYPE_HOST;
  421. else
  422. ok = 0;
  423. }
  424. rcu_read_unlock();
  425. return ok;
  426. }
  427. static void ipip6_tunnel_uninit(struct net_device *dev)
  428. {
  429. struct net *net = dev_net(dev);
  430. struct sit_net *sitn = net_generic(net, sit_net_id);
  431. if (dev == sitn->fb_tunnel_dev) {
  432. RCU_INIT_POINTER(sitn->tunnels_wc[0], NULL);
  433. } else {
  434. ipip6_tunnel_unlink(sitn, netdev_priv(dev));
  435. ipip6_tunnel_del_prl(netdev_priv(dev), NULL);
  436. }
  437. dev_put(dev);
  438. }
  439. static int ipip6_err(struct sk_buff *skb, u32 info)
  440. {
  441. /* All the routers (except for Linux) return only
  442. 8 bytes of packet payload. It means, that precise relaying of
  443. ICMP in the real Internet is absolutely infeasible.
  444. */
  445. const struct iphdr *iph = (const struct iphdr *)skb->data;
  446. const int type = icmp_hdr(skb)->type;
  447. const int code = icmp_hdr(skb)->code;
  448. struct ip_tunnel *t;
  449. int err;
  450. switch (type) {
  451. default:
  452. case ICMP_PARAMETERPROB:
  453. return 0;
  454. case ICMP_DEST_UNREACH:
  455. switch (code) {
  456. case ICMP_SR_FAILED:
  457. case ICMP_PORT_UNREACH:
  458. /* Impossible event. */
  459. return 0;
  460. default:
  461. /* All others are translated to HOST_UNREACH.
  462. rfc2003 contains "deep thoughts" about NET_UNREACH,
  463. I believe they are just ether pollution. --ANK
  464. */
  465. break;
  466. }
  467. break;
  468. case ICMP_TIME_EXCEEDED:
  469. if (code != ICMP_EXC_TTL)
  470. return 0;
  471. break;
  472. case ICMP_REDIRECT:
  473. break;
  474. }
  475. err = -ENOENT;
  476. t = ipip6_tunnel_lookup(dev_net(skb->dev),
  477. skb->dev,
  478. iph->daddr,
  479. iph->saddr);
  480. if (t == NULL)
  481. goto out;
  482. if (type == ICMP_DEST_UNREACH && code == ICMP_FRAG_NEEDED) {
  483. ipv4_update_pmtu(skb, dev_net(skb->dev), info,
  484. t->dev->ifindex, 0, IPPROTO_IPV6, 0);
  485. err = 0;
  486. goto out;
  487. }
  488. if (type == ICMP_REDIRECT) {
  489. ipv4_redirect(skb, dev_net(skb->dev), t->dev->ifindex, 0,
  490. IPPROTO_IPV6, 0);
  491. err = 0;
  492. goto out;
  493. }
  494. if (t->parms.iph.daddr == 0)
  495. goto out;
  496. err = 0;
  497. if (t->parms.iph.ttl == 0 && type == ICMP_TIME_EXCEEDED)
  498. goto out;
  499. if (time_before(jiffies, t->err_time + IPTUNNEL_ERR_TIMEO))
  500. t->err_count++;
  501. else
  502. t->err_count = 1;
  503. t->err_time = jiffies;
  504. out:
  505. return err;
  506. }
  507. static inline bool is_spoofed_6rd(struct ip_tunnel *tunnel, const __be32 v4addr,
  508. const struct in6_addr *v6addr)
  509. {
  510. __be32 v4embed = 0;
  511. if (check_6rd(tunnel, v6addr, &v4embed) && v4addr != v4embed)
  512. return true;
  513. return false;
  514. }
  515. static int ipip6_rcv(struct sk_buff *skb)
  516. {
  517. const struct iphdr *iph = ip_hdr(skb);
  518. struct ip_tunnel *tunnel;
  519. int err;
  520. tunnel = ipip6_tunnel_lookup(dev_net(skb->dev), skb->dev,
  521. iph->saddr, iph->daddr);
  522. if (tunnel != NULL) {
  523. struct pcpu_tstats *tstats;
  524. secpath_reset(skb);
  525. skb->mac_header = skb->network_header;
  526. skb_reset_network_header(skb);
  527. IPCB(skb)->flags = 0;
  528. skb->protocol = htons(ETH_P_IPV6);
  529. skb->pkt_type = PACKET_HOST;
  530. if (tunnel->dev->priv_flags & IFF_ISATAP) {
  531. if (!isatap_chksrc(skb, iph, tunnel)) {
  532. tunnel->dev->stats.rx_errors++;
  533. goto out;
  534. }
  535. } else {
  536. if (is_spoofed_6rd(tunnel, iph->saddr,
  537. &ipv6_hdr(skb)->saddr) ||
  538. is_spoofed_6rd(tunnel, iph->daddr,
  539. &ipv6_hdr(skb)->daddr)) {
  540. tunnel->dev->stats.rx_errors++;
  541. goto out;
  542. }
  543. }
  544. __skb_tunnel_rx(skb, tunnel->dev);
  545. err = IP_ECN_decapsulate(iph, skb);
  546. if (unlikely(err)) {
  547. if (log_ecn_error)
  548. net_info_ratelimited("non-ECT from %pI4 with TOS=%#x\n",
  549. &iph->saddr, iph->tos);
  550. if (err > 1) {
  551. ++tunnel->dev->stats.rx_frame_errors;
  552. ++tunnel->dev->stats.rx_errors;
  553. goto out;
  554. }
  555. }
  556. tstats = this_cpu_ptr(tunnel->dev->tstats);
  557. tstats->rx_packets++;
  558. tstats->rx_bytes += skb->len;
  559. netif_rx(skb);
  560. return 0;
  561. }
  562. /* no tunnel matched, let upstream know, ipsec may handle it */
  563. return 1;
  564. out:
  565. kfree_skb(skb);
  566. return 0;
  567. }
  568. /*
  569. * If the IPv6 address comes from 6rd / 6to4 (RFC 3056) addr space this function
  570. * stores the embedded IPv4 address in v4dst and returns true.
  571. */
  572. static bool check_6rd(struct ip_tunnel *tunnel, const struct in6_addr *v6dst,
  573. __be32 *v4dst)
  574. {
  575. #ifdef CONFIG_IPV6_SIT_6RD
  576. if (ipv6_prefix_equal(v6dst, &tunnel->ip6rd.prefix,
  577. tunnel->ip6rd.prefixlen)) {
  578. unsigned int pbw0, pbi0;
  579. int pbi1;
  580. u32 d;
  581. pbw0 = tunnel->ip6rd.prefixlen >> 5;
  582. pbi0 = tunnel->ip6rd.prefixlen & 0x1f;
  583. d = (ntohl(v6dst->s6_addr32[pbw0]) << pbi0) >>
  584. tunnel->ip6rd.relay_prefixlen;
  585. pbi1 = pbi0 - tunnel->ip6rd.relay_prefixlen;
  586. if (pbi1 > 0)
  587. d |= ntohl(v6dst->s6_addr32[pbw0 + 1]) >>
  588. (32 - pbi1);
  589. *v4dst = tunnel->ip6rd.relay_prefix | htonl(d);
  590. return true;
  591. }
  592. #else
  593. if (v6dst->s6_addr16[0] == htons(0x2002)) {
  594. /* 6to4 v6 addr has 16 bits prefix, 32 v4addr, 16 SLA, ... */
  595. memcpy(v4dst, &v6dst->s6_addr16[1], 4);
  596. return true;
  597. }
  598. #endif
  599. return false;
  600. }
  601. static inline __be32 try_6rd(struct ip_tunnel *tunnel,
  602. const struct in6_addr *v6dst)
  603. {
  604. __be32 dst = 0;
  605. check_6rd(tunnel, v6dst, &dst);
  606. return dst;
  607. }
  608. /*
  609. * This function assumes it is being called from dev_queue_xmit()
  610. * and that skb is filled properly by that function.
  611. */
  612. static netdev_tx_t ipip6_tunnel_xmit(struct sk_buff *skb,
  613. struct net_device *dev)
  614. {
  615. struct ip_tunnel *tunnel = netdev_priv(dev);
  616. const struct iphdr *tiph = &tunnel->parms.iph;
  617. const struct ipv6hdr *iph6 = ipv6_hdr(skb);
  618. u8 tos = tunnel->parms.iph.tos;
  619. __be16 df = tiph->frag_off;
  620. struct rtable *rt; /* Route to the other host */
  621. struct net_device *tdev; /* Device to other host */
  622. struct iphdr *iph; /* Our new IP header */
  623. unsigned int max_headroom; /* The extra header space needed */
  624. __be32 dst = tiph->daddr;
  625. struct flowi4 fl4;
  626. int mtu;
  627. const struct in6_addr *addr6;
  628. int addr_type;
  629. if (skb->protocol != htons(ETH_P_IPV6))
  630. goto tx_error;
  631. if (tos == 1)
  632. tos = ipv6_get_dsfield(iph6);
  633. /* ISATAP (RFC4214) - must come before 6to4 */
  634. if (dev->priv_flags & IFF_ISATAP) {
  635. struct neighbour *neigh = NULL;
  636. bool do_tx_error = false;
  637. if (skb_dst(skb))
  638. neigh = dst_neigh_lookup(skb_dst(skb), &iph6->daddr);
  639. if (neigh == NULL) {
  640. net_dbg_ratelimited("sit: nexthop == NULL\n");
  641. goto tx_error;
  642. }
  643. addr6 = (const struct in6_addr *)&neigh->primary_key;
  644. addr_type = ipv6_addr_type(addr6);
  645. if ((addr_type & IPV6_ADDR_UNICAST) &&
  646. ipv6_addr_is_isatap(addr6))
  647. dst = addr6->s6_addr32[3];
  648. else
  649. do_tx_error = true;
  650. neigh_release(neigh);
  651. if (do_tx_error)
  652. goto tx_error;
  653. }
  654. if (!dst)
  655. dst = try_6rd(tunnel, &iph6->daddr);
  656. if (!dst) {
  657. struct neighbour *neigh = NULL;
  658. bool do_tx_error = false;
  659. if (skb_dst(skb))
  660. neigh = dst_neigh_lookup(skb_dst(skb), &iph6->daddr);
  661. if (neigh == NULL) {
  662. net_dbg_ratelimited("sit: nexthop == NULL\n");
  663. goto tx_error;
  664. }
  665. addr6 = (const struct in6_addr *)&neigh->primary_key;
  666. addr_type = ipv6_addr_type(addr6);
  667. if (addr_type == IPV6_ADDR_ANY) {
  668. addr6 = &ipv6_hdr(skb)->daddr;
  669. addr_type = ipv6_addr_type(addr6);
  670. }
  671. if ((addr_type & IPV6_ADDR_COMPATv4) != 0)
  672. dst = addr6->s6_addr32[3];
  673. else
  674. do_tx_error = true;
  675. neigh_release(neigh);
  676. if (do_tx_error)
  677. goto tx_error;
  678. }
  679. rt = ip_route_output_ports(dev_net(dev), &fl4, NULL,
  680. dst, tiph->saddr,
  681. 0, 0,
  682. IPPROTO_IPV6, RT_TOS(tos),
  683. tunnel->parms.link);
  684. if (IS_ERR(rt)) {
  685. dev->stats.tx_carrier_errors++;
  686. goto tx_error_icmp;
  687. }
  688. if (rt->rt_type != RTN_UNICAST) {
  689. ip_rt_put(rt);
  690. dev->stats.tx_carrier_errors++;
  691. goto tx_error_icmp;
  692. }
  693. tdev = rt->dst.dev;
  694. if (tdev == dev) {
  695. ip_rt_put(rt);
  696. dev->stats.collisions++;
  697. goto tx_error;
  698. }
  699. if (df) {
  700. mtu = dst_mtu(&rt->dst) - sizeof(struct iphdr);
  701. if (mtu < 68) {
  702. dev->stats.collisions++;
  703. ip_rt_put(rt);
  704. goto tx_error;
  705. }
  706. if (mtu < IPV6_MIN_MTU) {
  707. mtu = IPV6_MIN_MTU;
  708. df = 0;
  709. }
  710. if (tunnel->parms.iph.daddr && skb_dst(skb))
  711. skb_dst(skb)->ops->update_pmtu(skb_dst(skb), NULL, skb, mtu);
  712. if (skb->len > mtu) {
  713. icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
  714. ip_rt_put(rt);
  715. goto tx_error;
  716. }
  717. }
  718. if (tunnel->err_count > 0) {
  719. if (time_before(jiffies,
  720. tunnel->err_time + IPTUNNEL_ERR_TIMEO)) {
  721. tunnel->err_count--;
  722. dst_link_failure(skb);
  723. } else
  724. tunnel->err_count = 0;
  725. }
  726. /*
  727. * Okay, now see if we can stuff it in the buffer as-is.
  728. */
  729. max_headroom = LL_RESERVED_SPACE(tdev)+sizeof(struct iphdr);
  730. if (skb_headroom(skb) < max_headroom || skb_shared(skb) ||
  731. (skb_cloned(skb) && !skb_clone_writable(skb, 0))) {
  732. struct sk_buff *new_skb = skb_realloc_headroom(skb, max_headroom);
  733. if (!new_skb) {
  734. ip_rt_put(rt);
  735. dev->stats.tx_dropped++;
  736. dev_kfree_skb(skb);
  737. return NETDEV_TX_OK;
  738. }
  739. if (skb->sk)
  740. skb_set_owner_w(new_skb, skb->sk);
  741. dev_kfree_skb(skb);
  742. skb = new_skb;
  743. iph6 = ipv6_hdr(skb);
  744. }
  745. skb->transport_header = skb->network_header;
  746. skb_push(skb, sizeof(struct iphdr));
  747. skb_reset_network_header(skb);
  748. memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
  749. IPCB(skb)->flags = 0;
  750. skb_dst_drop(skb);
  751. skb_dst_set(skb, &rt->dst);
  752. /*
  753. * Push down and install the IPIP header.
  754. */
  755. iph = ip_hdr(skb);
  756. iph->version = 4;
  757. iph->ihl = sizeof(struct iphdr)>>2;
  758. iph->frag_off = df;
  759. iph->protocol = IPPROTO_IPV6;
  760. iph->tos = INET_ECN_encapsulate(tos, ipv6_get_dsfield(iph6));
  761. iph->daddr = fl4.daddr;
  762. iph->saddr = fl4.saddr;
  763. if ((iph->ttl = tiph->ttl) == 0)
  764. iph->ttl = iph6->hop_limit;
  765. skb->ip_summed = CHECKSUM_NONE;
  766. ip_select_ident(iph, skb_dst(skb), NULL);
  767. iptunnel_xmit(skb, dev);
  768. return NETDEV_TX_OK;
  769. tx_error_icmp:
  770. dst_link_failure(skb);
  771. tx_error:
  772. dev->stats.tx_errors++;
  773. dev_kfree_skb(skb);
  774. return NETDEV_TX_OK;
  775. }
  776. static void ipip6_tunnel_bind_dev(struct net_device *dev)
  777. {
  778. struct net_device *tdev = NULL;
  779. struct ip_tunnel *tunnel;
  780. const struct iphdr *iph;
  781. struct flowi4 fl4;
  782. tunnel = netdev_priv(dev);
  783. iph = &tunnel->parms.iph;
  784. if (iph->daddr) {
  785. struct rtable *rt = ip_route_output_ports(dev_net(dev), &fl4, NULL,
  786. iph->daddr, iph->saddr,
  787. 0, 0,
  788. IPPROTO_IPV6,
  789. RT_TOS(iph->tos),
  790. tunnel->parms.link);
  791. if (!IS_ERR(rt)) {
  792. tdev = rt->dst.dev;
  793. ip_rt_put(rt);
  794. }
  795. dev->flags |= IFF_POINTOPOINT;
  796. }
  797. if (!tdev && tunnel->parms.link)
  798. tdev = __dev_get_by_index(dev_net(dev), tunnel->parms.link);
  799. if (tdev) {
  800. dev->hard_header_len = tdev->hard_header_len + sizeof(struct iphdr);
  801. dev->mtu = tdev->mtu - sizeof(struct iphdr);
  802. if (dev->mtu < IPV6_MIN_MTU)
  803. dev->mtu = IPV6_MIN_MTU;
  804. }
  805. dev->iflink = tunnel->parms.link;
  806. }
  807. static void ipip6_tunnel_update(struct ip_tunnel *t, struct ip_tunnel_parm *p)
  808. {
  809. struct net *net = dev_net(t->dev);
  810. struct sit_net *sitn = net_generic(net, sit_net_id);
  811. ipip6_tunnel_unlink(sitn, t);
  812. synchronize_net();
  813. t->parms.iph.saddr = p->iph.saddr;
  814. t->parms.iph.daddr = p->iph.daddr;
  815. memcpy(t->dev->dev_addr, &p->iph.saddr, 4);
  816. memcpy(t->dev->broadcast, &p->iph.daddr, 4);
  817. ipip6_tunnel_link(sitn, t);
  818. t->parms.iph.ttl = p->iph.ttl;
  819. t->parms.iph.tos = p->iph.tos;
  820. if (t->parms.link != p->link) {
  821. t->parms.link = p->link;
  822. ipip6_tunnel_bind_dev(t->dev);
  823. }
  824. netdev_state_change(t->dev);
  825. }
  826. #ifdef CONFIG_IPV6_SIT_6RD
  827. static int ipip6_tunnel_update_6rd(struct ip_tunnel *t,
  828. struct ip_tunnel_6rd *ip6rd)
  829. {
  830. struct in6_addr prefix;
  831. __be32 relay_prefix;
  832. if (ip6rd->relay_prefixlen > 32 ||
  833. ip6rd->prefixlen + (32 - ip6rd->relay_prefixlen) > 64)
  834. return -EINVAL;
  835. ipv6_addr_prefix(&prefix, &ip6rd->prefix, ip6rd->prefixlen);
  836. if (!ipv6_addr_equal(&prefix, &ip6rd->prefix))
  837. return -EINVAL;
  838. if (ip6rd->relay_prefixlen)
  839. relay_prefix = ip6rd->relay_prefix &
  840. htonl(0xffffffffUL <<
  841. (32 - ip6rd->relay_prefixlen));
  842. else
  843. relay_prefix = 0;
  844. if (relay_prefix != ip6rd->relay_prefix)
  845. return -EINVAL;
  846. t->ip6rd.prefix = prefix;
  847. t->ip6rd.relay_prefix = relay_prefix;
  848. t->ip6rd.prefixlen = ip6rd->prefixlen;
  849. t->ip6rd.relay_prefixlen = ip6rd->relay_prefixlen;
  850. netdev_state_change(t->dev);
  851. return 0;
  852. }
  853. #endif
  854. static int
  855. ipip6_tunnel_ioctl (struct net_device *dev, struct ifreq *ifr, int cmd)
  856. {
  857. int err = 0;
  858. struct ip_tunnel_parm p;
  859. struct ip_tunnel_prl prl;
  860. struct ip_tunnel *t;
  861. struct net *net = dev_net(dev);
  862. struct sit_net *sitn = net_generic(net, sit_net_id);
  863. #ifdef CONFIG_IPV6_SIT_6RD
  864. struct ip_tunnel_6rd ip6rd;
  865. #endif
  866. switch (cmd) {
  867. case SIOCGETTUNNEL:
  868. #ifdef CONFIG_IPV6_SIT_6RD
  869. case SIOCGET6RD:
  870. #endif
  871. t = NULL;
  872. if (dev == sitn->fb_tunnel_dev) {
  873. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) {
  874. err = -EFAULT;
  875. break;
  876. }
  877. t = ipip6_tunnel_locate(net, &p, 0);
  878. }
  879. if (t == NULL)
  880. t = netdev_priv(dev);
  881. err = -EFAULT;
  882. if (cmd == SIOCGETTUNNEL) {
  883. memcpy(&p, &t->parms, sizeof(p));
  884. if (copy_to_user(ifr->ifr_ifru.ifru_data, &p,
  885. sizeof(p)))
  886. goto done;
  887. #ifdef CONFIG_IPV6_SIT_6RD
  888. } else {
  889. ip6rd.prefix = t->ip6rd.prefix;
  890. ip6rd.relay_prefix = t->ip6rd.relay_prefix;
  891. ip6rd.prefixlen = t->ip6rd.prefixlen;
  892. ip6rd.relay_prefixlen = t->ip6rd.relay_prefixlen;
  893. if (copy_to_user(ifr->ifr_ifru.ifru_data, &ip6rd,
  894. sizeof(ip6rd)))
  895. goto done;
  896. #endif
  897. }
  898. err = 0;
  899. break;
  900. case SIOCADDTUNNEL:
  901. case SIOCCHGTUNNEL:
  902. err = -EPERM;
  903. if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  904. goto done;
  905. err = -EFAULT;
  906. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
  907. goto done;
  908. err = -EINVAL;
  909. if (p.iph.version != 4 || p.iph.protocol != IPPROTO_IPV6 ||
  910. p.iph.ihl != 5 || (p.iph.frag_off&htons(~IP_DF)))
  911. goto done;
  912. if (p.iph.ttl)
  913. p.iph.frag_off |= htons(IP_DF);
  914. t = ipip6_tunnel_locate(net, &p, cmd == SIOCADDTUNNEL);
  915. if (dev != sitn->fb_tunnel_dev && cmd == SIOCCHGTUNNEL) {
  916. if (t != NULL) {
  917. if (t->dev != dev) {
  918. err = -EEXIST;
  919. break;
  920. }
  921. } else {
  922. if (((dev->flags&IFF_POINTOPOINT) && !p.iph.daddr) ||
  923. (!(dev->flags&IFF_POINTOPOINT) && p.iph.daddr)) {
  924. err = -EINVAL;
  925. break;
  926. }
  927. t = netdev_priv(dev);
  928. }
  929. ipip6_tunnel_update(t, &p);
  930. }
  931. if (t) {
  932. err = 0;
  933. if (copy_to_user(ifr->ifr_ifru.ifru_data, &t->parms, sizeof(p)))
  934. err = -EFAULT;
  935. } else
  936. err = (cmd == SIOCADDTUNNEL ? -ENOBUFS : -ENOENT);
  937. break;
  938. case SIOCDELTUNNEL:
  939. err = -EPERM;
  940. if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  941. goto done;
  942. if (dev == sitn->fb_tunnel_dev) {
  943. err = -EFAULT;
  944. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
  945. goto done;
  946. err = -ENOENT;
  947. if ((t = ipip6_tunnel_locate(net, &p, 0)) == NULL)
  948. goto done;
  949. err = -EPERM;
  950. if (t == netdev_priv(sitn->fb_tunnel_dev))
  951. goto done;
  952. dev = t->dev;
  953. }
  954. unregister_netdevice(dev);
  955. err = 0;
  956. break;
  957. case SIOCGETPRL:
  958. err = -EINVAL;
  959. if (dev == sitn->fb_tunnel_dev)
  960. goto done;
  961. err = -ENOENT;
  962. if (!(t = netdev_priv(dev)))
  963. goto done;
  964. err = ipip6_tunnel_get_prl(t, ifr->ifr_ifru.ifru_data);
  965. break;
  966. case SIOCADDPRL:
  967. case SIOCDELPRL:
  968. case SIOCCHGPRL:
  969. err = -EPERM;
  970. if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  971. goto done;
  972. err = -EINVAL;
  973. if (dev == sitn->fb_tunnel_dev)
  974. goto done;
  975. err = -EFAULT;
  976. if (copy_from_user(&prl, ifr->ifr_ifru.ifru_data, sizeof(prl)))
  977. goto done;
  978. err = -ENOENT;
  979. if (!(t = netdev_priv(dev)))
  980. goto done;
  981. switch (cmd) {
  982. case SIOCDELPRL:
  983. err = ipip6_tunnel_del_prl(t, &prl);
  984. break;
  985. case SIOCADDPRL:
  986. case SIOCCHGPRL:
  987. err = ipip6_tunnel_add_prl(t, &prl, cmd == SIOCCHGPRL);
  988. break;
  989. }
  990. netdev_state_change(dev);
  991. break;
  992. #ifdef CONFIG_IPV6_SIT_6RD
  993. case SIOCADD6RD:
  994. case SIOCCHG6RD:
  995. case SIOCDEL6RD:
  996. err = -EPERM;
  997. if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  998. goto done;
  999. err = -EFAULT;
  1000. if (copy_from_user(&ip6rd, ifr->ifr_ifru.ifru_data,
  1001. sizeof(ip6rd)))
  1002. goto done;
  1003. t = netdev_priv(dev);
  1004. if (cmd != SIOCDEL6RD) {
  1005. err = ipip6_tunnel_update_6rd(t, &ip6rd);
  1006. if (err < 0)
  1007. goto done;
  1008. } else
  1009. ipip6_tunnel_clone_6rd(dev, sitn);
  1010. err = 0;
  1011. break;
  1012. #endif
  1013. default:
  1014. err = -EINVAL;
  1015. }
  1016. done:
  1017. return err;
  1018. }
  1019. static int ipip6_tunnel_change_mtu(struct net_device *dev, int new_mtu)
  1020. {
  1021. if (new_mtu < IPV6_MIN_MTU || new_mtu > 0xFFF8 - sizeof(struct iphdr))
  1022. return -EINVAL;
  1023. dev->mtu = new_mtu;
  1024. return 0;
  1025. }
  1026. static const struct net_device_ops ipip6_netdev_ops = {
  1027. .ndo_uninit = ipip6_tunnel_uninit,
  1028. .ndo_start_xmit = ipip6_tunnel_xmit,
  1029. .ndo_do_ioctl = ipip6_tunnel_ioctl,
  1030. .ndo_change_mtu = ipip6_tunnel_change_mtu,
  1031. .ndo_get_stats64= ipip6_get_stats64,
  1032. };
  1033. static void ipip6_dev_free(struct net_device *dev)
  1034. {
  1035. free_percpu(dev->tstats);
  1036. free_netdev(dev);
  1037. }
  1038. static void ipip6_tunnel_setup(struct net_device *dev)
  1039. {
  1040. dev->netdev_ops = &ipip6_netdev_ops;
  1041. dev->destructor = ipip6_dev_free;
  1042. dev->type = ARPHRD_SIT;
  1043. dev->hard_header_len = LL_MAX_HEADER + sizeof(struct iphdr);
  1044. dev->mtu = ETH_DATA_LEN - sizeof(struct iphdr);
  1045. dev->flags = IFF_NOARP;
  1046. dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
  1047. dev->iflink = 0;
  1048. dev->addr_len = 4;
  1049. dev->features |= NETIF_F_NETNS_LOCAL;
  1050. dev->features |= NETIF_F_LLTX;
  1051. }
  1052. static int ipip6_tunnel_init(struct net_device *dev)
  1053. {
  1054. struct ip_tunnel *tunnel = netdev_priv(dev);
  1055. tunnel->dev = dev;
  1056. memcpy(dev->dev_addr, &tunnel->parms.iph.saddr, 4);
  1057. memcpy(dev->broadcast, &tunnel->parms.iph.daddr, 4);
  1058. ipip6_tunnel_bind_dev(dev);
  1059. dev->tstats = alloc_percpu(struct pcpu_tstats);
  1060. if (!dev->tstats)
  1061. return -ENOMEM;
  1062. return 0;
  1063. }
  1064. static int __net_init ipip6_fb_tunnel_init(struct net_device *dev)
  1065. {
  1066. struct ip_tunnel *tunnel = netdev_priv(dev);
  1067. struct iphdr *iph = &tunnel->parms.iph;
  1068. struct net *net = dev_net(dev);
  1069. struct sit_net *sitn = net_generic(net, sit_net_id);
  1070. tunnel->dev = dev;
  1071. strcpy(tunnel->parms.name, dev->name);
  1072. iph->version = 4;
  1073. iph->protocol = IPPROTO_IPV6;
  1074. iph->ihl = 5;
  1075. iph->ttl = 64;
  1076. dev->tstats = alloc_percpu(struct pcpu_tstats);
  1077. if (!dev->tstats)
  1078. return -ENOMEM;
  1079. dev_hold(dev);
  1080. rcu_assign_pointer(sitn->tunnels_wc[0], tunnel);
  1081. return 0;
  1082. }
  1083. static void ipip6_netlink_parms(struct nlattr *data[],
  1084. struct ip_tunnel_parm *parms)
  1085. {
  1086. memset(parms, 0, sizeof(*parms));
  1087. parms->iph.version = 4;
  1088. parms->iph.protocol = IPPROTO_IPV6;
  1089. parms->iph.ihl = 5;
  1090. parms->iph.ttl = 64;
  1091. if (!data)
  1092. return;
  1093. if (data[IFLA_IPTUN_LINK])
  1094. parms->link = nla_get_u32(data[IFLA_IPTUN_LINK]);
  1095. if (data[IFLA_IPTUN_LOCAL])
  1096. parms->iph.saddr = nla_get_be32(data[IFLA_IPTUN_LOCAL]);
  1097. if (data[IFLA_IPTUN_REMOTE])
  1098. parms->iph.daddr = nla_get_be32(data[IFLA_IPTUN_REMOTE]);
  1099. if (data[IFLA_IPTUN_TTL]) {
  1100. parms->iph.ttl = nla_get_u8(data[IFLA_IPTUN_TTL]);
  1101. if (parms->iph.ttl)
  1102. parms->iph.frag_off = htons(IP_DF);
  1103. }
  1104. if (data[IFLA_IPTUN_TOS])
  1105. parms->iph.tos = nla_get_u8(data[IFLA_IPTUN_TOS]);
  1106. if (!data[IFLA_IPTUN_PMTUDISC] || nla_get_u8(data[IFLA_IPTUN_PMTUDISC]))
  1107. parms->iph.frag_off = htons(IP_DF);
  1108. if (data[IFLA_IPTUN_FLAGS])
  1109. parms->i_flags = nla_get_be16(data[IFLA_IPTUN_FLAGS]);
  1110. }
  1111. #ifdef CONFIG_IPV6_SIT_6RD
  1112. /* This function returns true when 6RD attributes are present in the nl msg */
  1113. static bool ipip6_netlink_6rd_parms(struct nlattr *data[],
  1114. struct ip_tunnel_6rd *ip6rd)
  1115. {
  1116. bool ret = false;
  1117. memset(ip6rd, 0, sizeof(*ip6rd));
  1118. if (!data)
  1119. return ret;
  1120. if (data[IFLA_IPTUN_6RD_PREFIX]) {
  1121. ret = true;
  1122. nla_memcpy(&ip6rd->prefix, data[IFLA_IPTUN_6RD_PREFIX],
  1123. sizeof(struct in6_addr));
  1124. }
  1125. if (data[IFLA_IPTUN_6RD_RELAY_PREFIX]) {
  1126. ret = true;
  1127. ip6rd->relay_prefix =
  1128. nla_get_be32(data[IFLA_IPTUN_6RD_RELAY_PREFIX]);
  1129. }
  1130. if (data[IFLA_IPTUN_6RD_PREFIXLEN]) {
  1131. ret = true;
  1132. ip6rd->prefixlen = nla_get_u16(data[IFLA_IPTUN_6RD_PREFIXLEN]);
  1133. }
  1134. if (data[IFLA_IPTUN_6RD_RELAY_PREFIXLEN]) {
  1135. ret = true;
  1136. ip6rd->relay_prefixlen =
  1137. nla_get_u16(data[IFLA_IPTUN_6RD_RELAY_PREFIXLEN]);
  1138. }
  1139. return ret;
  1140. }
  1141. #endif
  1142. static int ipip6_newlink(struct net *src_net, struct net_device *dev,
  1143. struct nlattr *tb[], struct nlattr *data[])
  1144. {
  1145. struct net *net = dev_net(dev);
  1146. struct ip_tunnel *nt;
  1147. #ifdef CONFIG_IPV6_SIT_6RD
  1148. struct ip_tunnel_6rd ip6rd;
  1149. #endif
  1150. int err;
  1151. nt = netdev_priv(dev);
  1152. ipip6_netlink_parms(data, &nt->parms);
  1153. if (ipip6_tunnel_locate(net, &nt->parms, 0))
  1154. return -EEXIST;
  1155. err = ipip6_tunnel_create(dev);
  1156. if (err < 0)
  1157. return err;
  1158. #ifdef CONFIG_IPV6_SIT_6RD
  1159. if (ipip6_netlink_6rd_parms(data, &ip6rd))
  1160. err = ipip6_tunnel_update_6rd(nt, &ip6rd);
  1161. #endif
  1162. return err;
  1163. }
  1164. static int ipip6_changelink(struct net_device *dev, struct nlattr *tb[],
  1165. struct nlattr *data[])
  1166. {
  1167. struct ip_tunnel *t;
  1168. struct ip_tunnel_parm p;
  1169. struct net *net = dev_net(dev);
  1170. struct sit_net *sitn = net_generic(net, sit_net_id);
  1171. #ifdef CONFIG_IPV6_SIT_6RD
  1172. struct ip_tunnel_6rd ip6rd;
  1173. #endif
  1174. if (dev == sitn->fb_tunnel_dev)
  1175. return -EINVAL;
  1176. ipip6_netlink_parms(data, &p);
  1177. if (((dev->flags & IFF_POINTOPOINT) && !p.iph.daddr) ||
  1178. (!(dev->flags & IFF_POINTOPOINT) && p.iph.daddr))
  1179. return -EINVAL;
  1180. t = ipip6_tunnel_locate(net, &p, 0);
  1181. if (t) {
  1182. if (t->dev != dev)
  1183. return -EEXIST;
  1184. } else
  1185. t = netdev_priv(dev);
  1186. ipip6_tunnel_update(t, &p);
  1187. #ifdef CONFIG_IPV6_SIT_6RD
  1188. if (ipip6_netlink_6rd_parms(data, &ip6rd))
  1189. return ipip6_tunnel_update_6rd(t, &ip6rd);
  1190. #endif
  1191. return 0;
  1192. }
  1193. static size_t ipip6_get_size(const struct net_device *dev)
  1194. {
  1195. return
  1196. /* IFLA_IPTUN_LINK */
  1197. nla_total_size(4) +
  1198. /* IFLA_IPTUN_LOCAL */
  1199. nla_total_size(4) +
  1200. /* IFLA_IPTUN_REMOTE */
  1201. nla_total_size(4) +
  1202. /* IFLA_IPTUN_TTL */
  1203. nla_total_size(1) +
  1204. /* IFLA_IPTUN_TOS */
  1205. nla_total_size(1) +
  1206. /* IFLA_IPTUN_PMTUDISC */
  1207. nla_total_size(1) +
  1208. /* IFLA_IPTUN_FLAGS */
  1209. nla_total_size(2) +
  1210. #ifdef CONFIG_IPV6_SIT_6RD
  1211. /* IFLA_IPTUN_6RD_PREFIX */
  1212. nla_total_size(sizeof(struct in6_addr)) +
  1213. /* IFLA_IPTUN_6RD_RELAY_PREFIX */
  1214. nla_total_size(4) +
  1215. /* IFLA_IPTUN_6RD_PREFIXLEN */
  1216. nla_total_size(2) +
  1217. /* IFLA_IPTUN_6RD_RELAY_PREFIXLEN */
  1218. nla_total_size(2) +
  1219. #endif
  1220. 0;
  1221. }
  1222. static int ipip6_fill_info(struct sk_buff *skb, const struct net_device *dev)
  1223. {
  1224. struct ip_tunnel *tunnel = netdev_priv(dev);
  1225. struct ip_tunnel_parm *parm = &tunnel->parms;
  1226. if (nla_put_u32(skb, IFLA_IPTUN_LINK, parm->link) ||
  1227. nla_put_be32(skb, IFLA_IPTUN_LOCAL, parm->iph.saddr) ||
  1228. nla_put_be32(skb, IFLA_IPTUN_REMOTE, parm->iph.daddr) ||
  1229. nla_put_u8(skb, IFLA_IPTUN_TTL, parm->iph.ttl) ||
  1230. nla_put_u8(skb, IFLA_IPTUN_TOS, parm->iph.tos) ||
  1231. nla_put_u8(skb, IFLA_IPTUN_PMTUDISC,
  1232. !!(parm->iph.frag_off & htons(IP_DF))) ||
  1233. nla_put_be16(skb, IFLA_IPTUN_FLAGS, parm->i_flags))
  1234. goto nla_put_failure;
  1235. #ifdef CONFIG_IPV6_SIT_6RD
  1236. if (nla_put(skb, IFLA_IPTUN_6RD_PREFIX, sizeof(struct in6_addr),
  1237. &tunnel->ip6rd.prefix) ||
  1238. nla_put_be32(skb, IFLA_IPTUN_6RD_RELAY_PREFIX,
  1239. tunnel->ip6rd.relay_prefix) ||
  1240. nla_put_u16(skb, IFLA_IPTUN_6RD_PREFIXLEN,
  1241. tunnel->ip6rd.prefixlen) ||
  1242. nla_put_u16(skb, IFLA_IPTUN_6RD_RELAY_PREFIXLEN,
  1243. tunnel->ip6rd.relay_prefixlen))
  1244. goto nla_put_failure;
  1245. #endif
  1246. return 0;
  1247. nla_put_failure:
  1248. return -EMSGSIZE;
  1249. }
  1250. static const struct nla_policy ipip6_policy[IFLA_IPTUN_MAX + 1] = {
  1251. [IFLA_IPTUN_LINK] = { .type = NLA_U32 },
  1252. [IFLA_IPTUN_LOCAL] = { .type = NLA_U32 },
  1253. [IFLA_IPTUN_REMOTE] = { .type = NLA_U32 },
  1254. [IFLA_IPTUN_TTL] = { .type = NLA_U8 },
  1255. [IFLA_IPTUN_TOS] = { .type = NLA_U8 },
  1256. [IFLA_IPTUN_PMTUDISC] = { .type = NLA_U8 },
  1257. [IFLA_IPTUN_FLAGS] = { .type = NLA_U16 },
  1258. #ifdef CONFIG_IPV6_SIT_6RD
  1259. [IFLA_IPTUN_6RD_PREFIX] = { .len = sizeof(struct in6_addr) },
  1260. [IFLA_IPTUN_6RD_RELAY_PREFIX] = { .type = NLA_U32 },
  1261. [IFLA_IPTUN_6RD_PREFIXLEN] = { .type = NLA_U16 },
  1262. [IFLA_IPTUN_6RD_RELAY_PREFIXLEN] = { .type = NLA_U16 },
  1263. #endif
  1264. };
  1265. static struct rtnl_link_ops sit_link_ops __read_mostly = {
  1266. .kind = "sit",
  1267. .maxtype = IFLA_IPTUN_MAX,
  1268. .policy = ipip6_policy,
  1269. .priv_size = sizeof(struct ip_tunnel),
  1270. .setup = ipip6_tunnel_setup,
  1271. .newlink = ipip6_newlink,
  1272. .changelink = ipip6_changelink,
  1273. .get_size = ipip6_get_size,
  1274. .fill_info = ipip6_fill_info,
  1275. };
  1276. static struct xfrm_tunnel sit_handler __read_mostly = {
  1277. .handler = ipip6_rcv,
  1278. .err_handler = ipip6_err,
  1279. .priority = 1,
  1280. };
  1281. static void __net_exit sit_destroy_tunnels(struct sit_net *sitn, struct list_head *head)
  1282. {
  1283. int prio;
  1284. for (prio = 1; prio < 4; prio++) {
  1285. int h;
  1286. for (h = 0; h < HASH_SIZE; h++) {
  1287. struct ip_tunnel *t;
  1288. t = rtnl_dereference(sitn->tunnels[prio][h]);
  1289. while (t != NULL) {
  1290. unregister_netdevice_queue(t->dev, head);
  1291. t = rtnl_dereference(t->next);
  1292. }
  1293. }
  1294. }
  1295. }
  1296. static int __net_init sit_init_net(struct net *net)
  1297. {
  1298. struct sit_net *sitn = net_generic(net, sit_net_id);
  1299. struct ip_tunnel *t;
  1300. int err;
  1301. sitn->tunnels[0] = sitn->tunnels_wc;
  1302. sitn->tunnels[1] = sitn->tunnels_l;
  1303. sitn->tunnels[2] = sitn->tunnels_r;
  1304. sitn->tunnels[3] = sitn->tunnels_r_l;
  1305. sitn->fb_tunnel_dev = alloc_netdev(sizeof(struct ip_tunnel), "sit0",
  1306. ipip6_tunnel_setup);
  1307. if (!sitn->fb_tunnel_dev) {
  1308. err = -ENOMEM;
  1309. goto err_alloc_dev;
  1310. }
  1311. dev_net_set(sitn->fb_tunnel_dev, net);
  1312. err = ipip6_fb_tunnel_init(sitn->fb_tunnel_dev);
  1313. if (err)
  1314. goto err_dev_free;
  1315. ipip6_tunnel_clone_6rd(sitn->fb_tunnel_dev, sitn);
  1316. if ((err = register_netdev(sitn->fb_tunnel_dev)))
  1317. goto err_reg_dev;
  1318. t = netdev_priv(sitn->fb_tunnel_dev);
  1319. strcpy(t->parms.name, sitn->fb_tunnel_dev->name);
  1320. return 0;
  1321. err_reg_dev:
  1322. dev_put(sitn->fb_tunnel_dev);
  1323. err_dev_free:
  1324. ipip6_dev_free(sitn->fb_tunnel_dev);
  1325. err_alloc_dev:
  1326. return err;
  1327. }
  1328. static void __net_exit sit_exit_net(struct net *net)
  1329. {
  1330. struct sit_net *sitn = net_generic(net, sit_net_id);
  1331. LIST_HEAD(list);
  1332. rtnl_lock();
  1333. sit_destroy_tunnels(sitn, &list);
  1334. unregister_netdevice_queue(sitn->fb_tunnel_dev, &list);
  1335. unregister_netdevice_many(&list);
  1336. rtnl_unlock();
  1337. }
  1338. static struct pernet_operations sit_net_ops = {
  1339. .init = sit_init_net,
  1340. .exit = sit_exit_net,
  1341. .id = &sit_net_id,
  1342. .size = sizeof(struct sit_net),
  1343. };
  1344. static void __exit sit_cleanup(void)
  1345. {
  1346. rtnl_link_unregister(&sit_link_ops);
  1347. xfrm4_tunnel_deregister(&sit_handler, AF_INET6);
  1348. unregister_pernet_device(&sit_net_ops);
  1349. rcu_barrier(); /* Wait for completion of call_rcu()'s */
  1350. }
  1351. static int __init sit_init(void)
  1352. {
  1353. int err;
  1354. pr_info("IPv6 over IPv4 tunneling driver\n");
  1355. err = register_pernet_device(&sit_net_ops);
  1356. if (err < 0)
  1357. return err;
  1358. err = xfrm4_tunnel_register(&sit_handler, AF_INET6);
  1359. if (err < 0) {
  1360. pr_info("%s: can't add protocol\n", __func__);
  1361. goto xfrm_tunnel_failed;
  1362. }
  1363. err = rtnl_link_register(&sit_link_ops);
  1364. if (err < 0)
  1365. goto rtnl_link_failed;
  1366. out:
  1367. return err;
  1368. rtnl_link_failed:
  1369. xfrm4_tunnel_deregister(&sit_handler, AF_INET6);
  1370. xfrm_tunnel_failed:
  1371. unregister_pernet_device(&sit_net_ops);
  1372. goto out;
  1373. }
  1374. module_init(sit_init);
  1375. module_exit(sit_cleanup);
  1376. MODULE_LICENSE("GPL");
  1377. MODULE_ALIAS_NETDEV("sit0");