sit.c 36 KB

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