sit.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243
  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. #include <linux/module.h>
  20. #include <linux/capability.h>
  21. #include <linux/errno.h>
  22. #include <linux/types.h>
  23. #include <linux/socket.h>
  24. #include <linux/sockios.h>
  25. #include <linux/net.h>
  26. #include <linux/in6.h>
  27. #include <linux/netdevice.h>
  28. #include <linux/if_arp.h>
  29. #include <linux/icmp.h>
  30. #include <asm/uaccess.h>
  31. #include <linux/init.h>
  32. #include <linux/netfilter_ipv4.h>
  33. #include <linux/if_ether.h>
  34. #include <net/sock.h>
  35. #include <net/snmp.h>
  36. #include <net/ipv6.h>
  37. #include <net/protocol.h>
  38. #include <net/transp_v6.h>
  39. #include <net/ip6_fib.h>
  40. #include <net/ip6_route.h>
  41. #include <net/ndisc.h>
  42. #include <net/addrconf.h>
  43. #include <net/ip.h>
  44. #include <net/udp.h>
  45. #include <net/icmp.h>
  46. #include <net/ipip.h>
  47. #include <net/inet_ecn.h>
  48. #include <net/xfrm.h>
  49. #include <net/dsfield.h>
  50. #include <net/net_namespace.h>
  51. #include <net/netns/generic.h>
  52. /*
  53. This version of net/ipv6/sit.c is cloned of net/ipv4/ip_gre.c
  54. For comments look at net/ipv4/ip_gre.c --ANK
  55. */
  56. #define HASH_SIZE 16
  57. #define HASH(addr) (((__force u32)addr^((__force u32)addr>>4))&0xF)
  58. static void ipip6_tunnel_init(struct net_device *dev);
  59. static void ipip6_tunnel_setup(struct net_device *dev);
  60. static int sit_net_id __read_mostly;
  61. struct sit_net {
  62. struct ip_tunnel *tunnels_r_l[HASH_SIZE];
  63. struct ip_tunnel *tunnels_r[HASH_SIZE];
  64. struct ip_tunnel *tunnels_l[HASH_SIZE];
  65. struct ip_tunnel *tunnels_wc[1];
  66. struct ip_tunnel **tunnels[4];
  67. struct net_device *fb_tunnel_dev;
  68. };
  69. /*
  70. * Locking : hash tables are protected by RCU and a spinlock
  71. */
  72. static DEFINE_SPINLOCK(ipip6_lock);
  73. #define for_each_ip_tunnel_rcu(start) \
  74. for (t = rcu_dereference(start); t; t = rcu_dereference(t->next))
  75. /*
  76. * Must be invoked with rcu_read_lock
  77. */
  78. static struct ip_tunnel * ipip6_tunnel_lookup(struct net *net,
  79. struct net_device *dev, __be32 remote, __be32 local)
  80. {
  81. unsigned h0 = HASH(remote);
  82. unsigned h1 = HASH(local);
  83. struct ip_tunnel *t;
  84. struct sit_net *sitn = net_generic(net, sit_net_id);
  85. for_each_ip_tunnel_rcu(sitn->tunnels_r_l[h0 ^ h1]) {
  86. if (local == t->parms.iph.saddr &&
  87. remote == t->parms.iph.daddr &&
  88. (!dev || !t->parms.link || dev->iflink == t->parms.link) &&
  89. (t->dev->flags & IFF_UP))
  90. return t;
  91. }
  92. for_each_ip_tunnel_rcu(sitn->tunnels_r[h0]) {
  93. if (remote == t->parms.iph.daddr &&
  94. (!dev || !t->parms.link || dev->iflink == t->parms.link) &&
  95. (t->dev->flags & IFF_UP))
  96. return t;
  97. }
  98. for_each_ip_tunnel_rcu(sitn->tunnels_l[h1]) {
  99. if (local == t->parms.iph.saddr &&
  100. (!dev || !t->parms.link || dev->iflink == t->parms.link) &&
  101. (t->dev->flags & IFF_UP))
  102. return t;
  103. }
  104. t = rcu_dereference(sitn->tunnels_wc[0]);
  105. if ((t != NULL) && (t->dev->flags & IFF_UP))
  106. return t;
  107. return NULL;
  108. }
  109. static struct ip_tunnel **__ipip6_bucket(struct sit_net *sitn,
  110. struct ip_tunnel_parm *parms)
  111. {
  112. __be32 remote = parms->iph.daddr;
  113. __be32 local = parms->iph.saddr;
  114. unsigned h = 0;
  115. int prio = 0;
  116. if (remote) {
  117. prio |= 2;
  118. h ^= HASH(remote);
  119. }
  120. if (local) {
  121. prio |= 1;
  122. h ^= HASH(local);
  123. }
  124. return &sitn->tunnels[prio][h];
  125. }
  126. static inline struct ip_tunnel **ipip6_bucket(struct sit_net *sitn,
  127. struct ip_tunnel *t)
  128. {
  129. return __ipip6_bucket(sitn, &t->parms);
  130. }
  131. static void ipip6_tunnel_unlink(struct sit_net *sitn, struct ip_tunnel *t)
  132. {
  133. struct ip_tunnel **tp;
  134. for (tp = ipip6_bucket(sitn, t); *tp; tp = &(*tp)->next) {
  135. if (t == *tp) {
  136. spin_lock_bh(&ipip6_lock);
  137. *tp = t->next;
  138. spin_unlock_bh(&ipip6_lock);
  139. break;
  140. }
  141. }
  142. }
  143. static void ipip6_tunnel_link(struct sit_net *sitn, struct ip_tunnel *t)
  144. {
  145. struct ip_tunnel **tp = ipip6_bucket(sitn, t);
  146. spin_lock_bh(&ipip6_lock);
  147. t->next = *tp;
  148. rcu_assign_pointer(*tp, t);
  149. spin_unlock_bh(&ipip6_lock);
  150. }
  151. static void ipip6_tunnel_clone_6rd(struct net_device *dev, struct sit_net *sitn)
  152. {
  153. #ifdef CONFIG_IPV6_SIT_6RD
  154. struct ip_tunnel *t = netdev_priv(dev);
  155. if (t->dev == sitn->fb_tunnel_dev) {
  156. ipv6_addr_set(&t->ip6rd.prefix, htonl(0x20020000), 0, 0, 0);
  157. t->ip6rd.relay_prefix = 0;
  158. t->ip6rd.prefixlen = 16;
  159. t->ip6rd.relay_prefixlen = 0;
  160. } else {
  161. struct ip_tunnel *t0 = netdev_priv(sitn->fb_tunnel_dev);
  162. memcpy(&t->ip6rd, &t0->ip6rd, sizeof(t->ip6rd));
  163. }
  164. #endif
  165. }
  166. static struct ip_tunnel * ipip6_tunnel_locate(struct net *net,
  167. struct ip_tunnel_parm *parms, int create)
  168. {
  169. __be32 remote = parms->iph.daddr;
  170. __be32 local = parms->iph.saddr;
  171. struct ip_tunnel *t, **tp, *nt;
  172. struct net_device *dev;
  173. char name[IFNAMSIZ];
  174. struct sit_net *sitn = net_generic(net, sit_net_id);
  175. for (tp = __ipip6_bucket(sitn, parms); (t = *tp) != NULL; tp = &t->next) {
  176. if (local == t->parms.iph.saddr &&
  177. remote == t->parms.iph.daddr &&
  178. parms->link == t->parms.link) {
  179. if (create)
  180. return NULL;
  181. else
  182. return t;
  183. }
  184. }
  185. if (!create)
  186. goto failed;
  187. if (parms->name[0])
  188. strlcpy(name, parms->name, IFNAMSIZ);
  189. else
  190. sprintf(name, "sit%%d");
  191. dev = alloc_netdev(sizeof(*t), name, ipip6_tunnel_setup);
  192. if (dev == NULL)
  193. return NULL;
  194. dev_net_set(dev, net);
  195. if (strchr(name, '%')) {
  196. if (dev_alloc_name(dev, name) < 0)
  197. goto failed_free;
  198. }
  199. nt = netdev_priv(dev);
  200. nt->parms = *parms;
  201. ipip6_tunnel_init(dev);
  202. ipip6_tunnel_clone_6rd(dev, sitn);
  203. if (parms->i_flags & SIT_ISATAP)
  204. dev->priv_flags |= IFF_ISATAP;
  205. if (register_netdevice(dev) < 0)
  206. goto failed_free;
  207. dev_hold(dev);
  208. ipip6_tunnel_link(sitn, nt);
  209. return nt;
  210. failed_free:
  211. free_netdev(dev);
  212. failed:
  213. return NULL;
  214. }
  215. static DEFINE_SPINLOCK(ipip6_prl_lock);
  216. #define for_each_prl_rcu(start) \
  217. for (prl = rcu_dereference(start); \
  218. prl; \
  219. prl = rcu_dereference(prl->next))
  220. static struct ip_tunnel_prl_entry *
  221. __ipip6_tunnel_locate_prl(struct ip_tunnel *t, __be32 addr)
  222. {
  223. struct ip_tunnel_prl_entry *prl;
  224. for_each_prl_rcu(t->prl)
  225. if (prl->addr == addr)
  226. break;
  227. return prl;
  228. }
  229. static int ipip6_tunnel_get_prl(struct ip_tunnel *t,
  230. struct ip_tunnel_prl __user *a)
  231. {
  232. struct ip_tunnel_prl kprl, *kp;
  233. struct ip_tunnel_prl_entry *prl;
  234. unsigned int cmax, c = 0, ca, len;
  235. int ret = 0;
  236. if (copy_from_user(&kprl, a, sizeof(kprl)))
  237. return -EFAULT;
  238. cmax = kprl.datalen / sizeof(kprl);
  239. if (cmax > 1 && kprl.addr != htonl(INADDR_ANY))
  240. cmax = 1;
  241. /* For simple GET or for root users,
  242. * we try harder to allocate.
  243. */
  244. kp = (cmax <= 1 || capable(CAP_NET_ADMIN)) ?
  245. kcalloc(cmax, sizeof(*kp), GFP_KERNEL) :
  246. NULL;
  247. rcu_read_lock();
  248. ca = t->prl_count < cmax ? t->prl_count : cmax;
  249. if (!kp) {
  250. /* We don't try hard to allocate much memory for
  251. * non-root users.
  252. * For root users, retry allocating enough memory for
  253. * the answer.
  254. */
  255. kp = kcalloc(ca, sizeof(*kp), GFP_ATOMIC);
  256. if (!kp) {
  257. ret = -ENOMEM;
  258. goto out;
  259. }
  260. }
  261. c = 0;
  262. for_each_prl_rcu(t->prl) {
  263. if (c >= cmax)
  264. break;
  265. if (kprl.addr != htonl(INADDR_ANY) && prl->addr != kprl.addr)
  266. continue;
  267. kp[c].addr = prl->addr;
  268. kp[c].flags = prl->flags;
  269. c++;
  270. if (kprl.addr != htonl(INADDR_ANY))
  271. break;
  272. }
  273. out:
  274. rcu_read_unlock();
  275. len = sizeof(*kp) * c;
  276. ret = 0;
  277. if ((len && copy_to_user(a + 1, kp, len)) || put_user(len, &a->datalen))
  278. ret = -EFAULT;
  279. kfree(kp);
  280. return ret;
  281. }
  282. static int
  283. ipip6_tunnel_add_prl(struct ip_tunnel *t, struct ip_tunnel_prl *a, int chg)
  284. {
  285. struct ip_tunnel_prl_entry *p;
  286. int err = 0;
  287. if (a->addr == htonl(INADDR_ANY))
  288. return -EINVAL;
  289. spin_lock(&ipip6_prl_lock);
  290. for (p = t->prl; p; p = p->next) {
  291. if (p->addr == a->addr) {
  292. if (chg) {
  293. p->flags = a->flags;
  294. goto out;
  295. }
  296. err = -EEXIST;
  297. goto out;
  298. }
  299. }
  300. if (chg) {
  301. err = -ENXIO;
  302. goto out;
  303. }
  304. p = kzalloc(sizeof(struct ip_tunnel_prl_entry), GFP_KERNEL);
  305. if (!p) {
  306. err = -ENOBUFS;
  307. goto out;
  308. }
  309. p->next = t->prl;
  310. p->addr = a->addr;
  311. p->flags = a->flags;
  312. t->prl_count++;
  313. rcu_assign_pointer(t->prl, p);
  314. out:
  315. spin_unlock(&ipip6_prl_lock);
  316. return err;
  317. }
  318. static void prl_entry_destroy_rcu(struct rcu_head *head)
  319. {
  320. kfree(container_of(head, struct ip_tunnel_prl_entry, rcu_head));
  321. }
  322. static void prl_list_destroy_rcu(struct rcu_head *head)
  323. {
  324. struct ip_tunnel_prl_entry *p, *n;
  325. p = container_of(head, struct ip_tunnel_prl_entry, rcu_head);
  326. do {
  327. n = p->next;
  328. kfree(p);
  329. p = n;
  330. } while (p);
  331. }
  332. static int
  333. ipip6_tunnel_del_prl(struct ip_tunnel *t, struct ip_tunnel_prl *a)
  334. {
  335. struct ip_tunnel_prl_entry *x, **p;
  336. int err = 0;
  337. spin_lock(&ipip6_prl_lock);
  338. if (a && a->addr != htonl(INADDR_ANY)) {
  339. for (p = &t->prl; *p; p = &(*p)->next) {
  340. if ((*p)->addr == a->addr) {
  341. x = *p;
  342. *p = x->next;
  343. call_rcu(&x->rcu_head, prl_entry_destroy_rcu);
  344. t->prl_count--;
  345. goto out;
  346. }
  347. }
  348. err = -ENXIO;
  349. } else {
  350. if (t->prl) {
  351. t->prl_count = 0;
  352. x = t->prl;
  353. call_rcu(&x->rcu_head, prl_list_destroy_rcu);
  354. t->prl = NULL;
  355. }
  356. }
  357. out:
  358. spin_unlock(&ipip6_prl_lock);
  359. return err;
  360. }
  361. static int
  362. isatap_chksrc(struct sk_buff *skb, struct iphdr *iph, struct ip_tunnel *t)
  363. {
  364. struct ip_tunnel_prl_entry *p;
  365. int ok = 1;
  366. rcu_read_lock();
  367. p = __ipip6_tunnel_locate_prl(t, iph->saddr);
  368. if (p) {
  369. if (p->flags & PRL_DEFAULT)
  370. skb->ndisc_nodetype = NDISC_NODETYPE_DEFAULT;
  371. else
  372. skb->ndisc_nodetype = NDISC_NODETYPE_NODEFAULT;
  373. } else {
  374. struct in6_addr *addr6 = &ipv6_hdr(skb)->saddr;
  375. if (ipv6_addr_is_isatap(addr6) &&
  376. (addr6->s6_addr32[3] == iph->saddr) &&
  377. ipv6_chk_prefix(addr6, t->dev))
  378. skb->ndisc_nodetype = NDISC_NODETYPE_HOST;
  379. else
  380. ok = 0;
  381. }
  382. rcu_read_unlock();
  383. return ok;
  384. }
  385. static void ipip6_tunnel_uninit(struct net_device *dev)
  386. {
  387. struct net *net = dev_net(dev);
  388. struct sit_net *sitn = net_generic(net, sit_net_id);
  389. if (dev == sitn->fb_tunnel_dev) {
  390. spin_lock_bh(&ipip6_lock);
  391. sitn->tunnels_wc[0] = NULL;
  392. spin_unlock_bh(&ipip6_lock);
  393. dev_put(dev);
  394. } else {
  395. ipip6_tunnel_unlink(sitn, netdev_priv(dev));
  396. ipip6_tunnel_del_prl(netdev_priv(dev), NULL);
  397. dev_put(dev);
  398. }
  399. }
  400. static int ipip6_err(struct sk_buff *skb, u32 info)
  401. {
  402. /* All the routers (except for Linux) return only
  403. 8 bytes of packet payload. It means, that precise relaying of
  404. ICMP in the real Internet is absolutely infeasible.
  405. */
  406. struct iphdr *iph = (struct iphdr*)skb->data;
  407. const int type = icmp_hdr(skb)->type;
  408. const int code = icmp_hdr(skb)->code;
  409. struct ip_tunnel *t;
  410. int err;
  411. switch (type) {
  412. default:
  413. case ICMP_PARAMETERPROB:
  414. return 0;
  415. case ICMP_DEST_UNREACH:
  416. switch (code) {
  417. case ICMP_SR_FAILED:
  418. case ICMP_PORT_UNREACH:
  419. /* Impossible event. */
  420. return 0;
  421. case ICMP_FRAG_NEEDED:
  422. /* Soft state for pmtu is maintained by IP core. */
  423. return 0;
  424. default:
  425. /* All others are translated to HOST_UNREACH.
  426. rfc2003 contains "deep thoughts" about NET_UNREACH,
  427. I believe they are just ether pollution. --ANK
  428. */
  429. break;
  430. }
  431. break;
  432. case ICMP_TIME_EXCEEDED:
  433. if (code != ICMP_EXC_TTL)
  434. return 0;
  435. break;
  436. }
  437. err = -ENOENT;
  438. rcu_read_lock();
  439. t = ipip6_tunnel_lookup(dev_net(skb->dev),
  440. skb->dev,
  441. iph->daddr,
  442. iph->saddr);
  443. if (t == NULL || t->parms.iph.daddr == 0)
  444. goto out;
  445. err = 0;
  446. if (t->parms.iph.ttl == 0 && type == ICMP_TIME_EXCEEDED)
  447. goto out;
  448. if (time_before(jiffies, t->err_time + IPTUNNEL_ERR_TIMEO))
  449. t->err_count++;
  450. else
  451. t->err_count = 1;
  452. t->err_time = jiffies;
  453. out:
  454. rcu_read_unlock();
  455. return err;
  456. }
  457. static inline void ipip6_ecn_decapsulate(struct iphdr *iph, struct sk_buff *skb)
  458. {
  459. if (INET_ECN_is_ce(iph->tos))
  460. IP6_ECN_set_ce(ipv6_hdr(skb));
  461. }
  462. static int ipip6_rcv(struct sk_buff *skb)
  463. {
  464. struct iphdr *iph;
  465. struct ip_tunnel *tunnel;
  466. if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
  467. goto out;
  468. iph = ip_hdr(skb);
  469. rcu_read_lock();
  470. tunnel = ipip6_tunnel_lookup(dev_net(skb->dev), skb->dev,
  471. iph->saddr, iph->daddr);
  472. if (tunnel != NULL) {
  473. secpath_reset(skb);
  474. skb->mac_header = skb->network_header;
  475. skb_reset_network_header(skb);
  476. IPCB(skb)->flags = 0;
  477. skb->protocol = htons(ETH_P_IPV6);
  478. skb->pkt_type = PACKET_HOST;
  479. if ((tunnel->dev->priv_flags & IFF_ISATAP) &&
  480. !isatap_chksrc(skb, iph, tunnel)) {
  481. tunnel->dev->stats.rx_errors++;
  482. rcu_read_unlock();
  483. kfree_skb(skb);
  484. return 0;
  485. }
  486. tunnel->dev->stats.rx_packets++;
  487. tunnel->dev->stats.rx_bytes += skb->len;
  488. skb->dev = tunnel->dev;
  489. skb_dst_drop(skb);
  490. nf_reset(skb);
  491. ipip6_ecn_decapsulate(iph, skb);
  492. netif_rx(skb);
  493. rcu_read_unlock();
  494. return 0;
  495. }
  496. icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
  497. rcu_read_unlock();
  498. out:
  499. kfree_skb(skb);
  500. return 0;
  501. }
  502. /*
  503. * Returns the embedded IPv4 address if the IPv6 address
  504. * comes from 6rd / 6to4 (RFC 3056) addr space.
  505. */
  506. static inline
  507. __be32 try_6rd(struct in6_addr *v6dst, struct ip_tunnel *tunnel)
  508. {
  509. __be32 dst = 0;
  510. #ifdef CONFIG_IPV6_SIT_6RD
  511. if (ipv6_prefix_equal(v6dst, &tunnel->ip6rd.prefix,
  512. tunnel->ip6rd.prefixlen)) {
  513. unsigned pbw0, pbi0;
  514. int pbi1;
  515. u32 d;
  516. pbw0 = tunnel->ip6rd.prefixlen >> 5;
  517. pbi0 = tunnel->ip6rd.prefixlen & 0x1f;
  518. d = (ntohl(v6dst->s6_addr32[pbw0]) << pbi0) >>
  519. tunnel->ip6rd.relay_prefixlen;
  520. pbi1 = pbi0 - tunnel->ip6rd.relay_prefixlen;
  521. if (pbi1 > 0)
  522. d |= ntohl(v6dst->s6_addr32[pbw0 + 1]) >>
  523. (32 - pbi1);
  524. dst = tunnel->ip6rd.relay_prefix | htonl(d);
  525. }
  526. #else
  527. if (v6dst->s6_addr16[0] == htons(0x2002)) {
  528. /* 6to4 v6 addr has 16 bits prefix, 32 v4addr, 16 SLA, ... */
  529. memcpy(&dst, &v6dst->s6_addr16[1], 4);
  530. }
  531. #endif
  532. return dst;
  533. }
  534. /*
  535. * This function assumes it is being called from dev_queue_xmit()
  536. * and that skb is filled properly by that function.
  537. */
  538. static netdev_tx_t ipip6_tunnel_xmit(struct sk_buff *skb,
  539. struct net_device *dev)
  540. {
  541. struct ip_tunnel *tunnel = netdev_priv(dev);
  542. struct net_device_stats *stats = &dev->stats;
  543. struct netdev_queue *txq = netdev_get_tx_queue(dev, 0);
  544. struct iphdr *tiph = &tunnel->parms.iph;
  545. struct ipv6hdr *iph6 = ipv6_hdr(skb);
  546. u8 tos = tunnel->parms.iph.tos;
  547. __be16 df = tiph->frag_off;
  548. struct rtable *rt; /* Route to the other host */
  549. struct net_device *tdev; /* Device to other host */
  550. struct iphdr *iph; /* Our new IP header */
  551. unsigned int max_headroom; /* The extra header space needed */
  552. __be32 dst = tiph->daddr;
  553. int mtu;
  554. struct in6_addr *addr6;
  555. int addr_type;
  556. if (skb->protocol != htons(ETH_P_IPV6))
  557. goto tx_error;
  558. /* ISATAP (RFC4214) - must come before 6to4 */
  559. if (dev->priv_flags & IFF_ISATAP) {
  560. struct neighbour *neigh = NULL;
  561. if (skb_dst(skb))
  562. neigh = skb_dst(skb)->neighbour;
  563. if (neigh == NULL) {
  564. if (net_ratelimit())
  565. printk(KERN_DEBUG "sit: nexthop == NULL\n");
  566. goto tx_error;
  567. }
  568. addr6 = (struct in6_addr*)&neigh->primary_key;
  569. addr_type = ipv6_addr_type(addr6);
  570. if ((addr_type & IPV6_ADDR_UNICAST) &&
  571. ipv6_addr_is_isatap(addr6))
  572. dst = addr6->s6_addr32[3];
  573. else
  574. goto tx_error;
  575. }
  576. if (!dst)
  577. dst = try_6rd(&iph6->daddr, tunnel);
  578. if (!dst) {
  579. struct neighbour *neigh = NULL;
  580. if (skb_dst(skb))
  581. neigh = skb_dst(skb)->neighbour;
  582. if (neigh == NULL) {
  583. if (net_ratelimit())
  584. printk(KERN_DEBUG "sit: nexthop == NULL\n");
  585. goto tx_error;
  586. }
  587. addr6 = (struct in6_addr*)&neigh->primary_key;
  588. addr_type = ipv6_addr_type(addr6);
  589. if (addr_type == IPV6_ADDR_ANY) {
  590. addr6 = &ipv6_hdr(skb)->daddr;
  591. addr_type = ipv6_addr_type(addr6);
  592. }
  593. if ((addr_type & IPV6_ADDR_COMPATv4) == 0)
  594. goto tx_error_icmp;
  595. dst = addr6->s6_addr32[3];
  596. }
  597. {
  598. struct flowi fl = { .nl_u = { .ip4_u =
  599. { .daddr = dst,
  600. .saddr = tiph->saddr,
  601. .tos = RT_TOS(tos) } },
  602. .oif = tunnel->parms.link,
  603. .proto = IPPROTO_IPV6 };
  604. if (ip_route_output_key(dev_net(dev), &rt, &fl)) {
  605. stats->tx_carrier_errors++;
  606. goto tx_error_icmp;
  607. }
  608. }
  609. if (rt->rt_type != RTN_UNICAST) {
  610. ip_rt_put(rt);
  611. stats->tx_carrier_errors++;
  612. goto tx_error_icmp;
  613. }
  614. tdev = rt->u.dst.dev;
  615. if (tdev == dev) {
  616. ip_rt_put(rt);
  617. stats->collisions++;
  618. goto tx_error;
  619. }
  620. if (df) {
  621. mtu = dst_mtu(&rt->u.dst) - sizeof(struct iphdr);
  622. if (mtu < 68) {
  623. stats->collisions++;
  624. ip_rt_put(rt);
  625. goto tx_error;
  626. }
  627. if (mtu < IPV6_MIN_MTU) {
  628. mtu = IPV6_MIN_MTU;
  629. df = 0;
  630. }
  631. if (tunnel->parms.iph.daddr && skb_dst(skb))
  632. skb_dst(skb)->ops->update_pmtu(skb_dst(skb), mtu);
  633. if (skb->len > mtu) {
  634. icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
  635. ip_rt_put(rt);
  636. goto tx_error;
  637. }
  638. }
  639. if (tunnel->err_count > 0) {
  640. if (time_before(jiffies,
  641. tunnel->err_time + IPTUNNEL_ERR_TIMEO)) {
  642. tunnel->err_count--;
  643. dst_link_failure(skb);
  644. } else
  645. tunnel->err_count = 0;
  646. }
  647. /*
  648. * Okay, now see if we can stuff it in the buffer as-is.
  649. */
  650. max_headroom = LL_RESERVED_SPACE(tdev)+sizeof(struct iphdr);
  651. if (skb_headroom(skb) < max_headroom || skb_shared(skb) ||
  652. (skb_cloned(skb) && !skb_clone_writable(skb, 0))) {
  653. struct sk_buff *new_skb = skb_realloc_headroom(skb, max_headroom);
  654. if (!new_skb) {
  655. ip_rt_put(rt);
  656. txq->tx_dropped++;
  657. dev_kfree_skb(skb);
  658. return NETDEV_TX_OK;
  659. }
  660. if (skb->sk)
  661. skb_set_owner_w(new_skb, skb->sk);
  662. dev_kfree_skb(skb);
  663. skb = new_skb;
  664. iph6 = ipv6_hdr(skb);
  665. }
  666. skb->transport_header = skb->network_header;
  667. skb_push(skb, sizeof(struct iphdr));
  668. skb_reset_network_header(skb);
  669. memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
  670. IPCB(skb)->flags = 0;
  671. skb_dst_drop(skb);
  672. skb_dst_set(skb, &rt->u.dst);
  673. /*
  674. * Push down and install the IPIP header.
  675. */
  676. iph = ip_hdr(skb);
  677. iph->version = 4;
  678. iph->ihl = sizeof(struct iphdr)>>2;
  679. iph->frag_off = df;
  680. iph->protocol = IPPROTO_IPV6;
  681. iph->tos = INET_ECN_encapsulate(tos, ipv6_get_dsfield(iph6));
  682. iph->daddr = rt->rt_dst;
  683. iph->saddr = rt->rt_src;
  684. if ((iph->ttl = tiph->ttl) == 0)
  685. iph->ttl = iph6->hop_limit;
  686. nf_reset(skb);
  687. IPTUNNEL_XMIT();
  688. return NETDEV_TX_OK;
  689. tx_error_icmp:
  690. dst_link_failure(skb);
  691. tx_error:
  692. stats->tx_errors++;
  693. dev_kfree_skb(skb);
  694. return NETDEV_TX_OK;
  695. }
  696. static void ipip6_tunnel_bind_dev(struct net_device *dev)
  697. {
  698. struct net_device *tdev = NULL;
  699. struct ip_tunnel *tunnel;
  700. struct iphdr *iph;
  701. tunnel = netdev_priv(dev);
  702. iph = &tunnel->parms.iph;
  703. if (iph->daddr) {
  704. struct flowi fl = { .nl_u = { .ip4_u =
  705. { .daddr = iph->daddr,
  706. .saddr = iph->saddr,
  707. .tos = RT_TOS(iph->tos) } },
  708. .oif = tunnel->parms.link,
  709. .proto = IPPROTO_IPV6 };
  710. struct rtable *rt;
  711. if (!ip_route_output_key(dev_net(dev), &rt, &fl)) {
  712. tdev = rt->u.dst.dev;
  713. ip_rt_put(rt);
  714. }
  715. dev->flags |= IFF_POINTOPOINT;
  716. }
  717. if (!tdev && tunnel->parms.link)
  718. tdev = __dev_get_by_index(dev_net(dev), tunnel->parms.link);
  719. if (tdev) {
  720. dev->hard_header_len = tdev->hard_header_len + sizeof(struct iphdr);
  721. dev->mtu = tdev->mtu - sizeof(struct iphdr);
  722. if (dev->mtu < IPV6_MIN_MTU)
  723. dev->mtu = IPV6_MIN_MTU;
  724. }
  725. dev->iflink = tunnel->parms.link;
  726. }
  727. static int
  728. ipip6_tunnel_ioctl (struct net_device *dev, struct ifreq *ifr, int cmd)
  729. {
  730. int err = 0;
  731. struct ip_tunnel_parm p;
  732. struct ip_tunnel_prl prl;
  733. struct ip_tunnel *t;
  734. struct net *net = dev_net(dev);
  735. struct sit_net *sitn = net_generic(net, sit_net_id);
  736. #ifdef CONFIG_IPV6_SIT_6RD
  737. struct ip_tunnel_6rd ip6rd;
  738. #endif
  739. switch (cmd) {
  740. case SIOCGETTUNNEL:
  741. #ifdef CONFIG_IPV6_SIT_6RD
  742. case SIOCGET6RD:
  743. #endif
  744. t = NULL;
  745. if (dev == sitn->fb_tunnel_dev) {
  746. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) {
  747. err = -EFAULT;
  748. break;
  749. }
  750. t = ipip6_tunnel_locate(net, &p, 0);
  751. }
  752. if (t == NULL)
  753. t = netdev_priv(dev);
  754. err = -EFAULT;
  755. if (cmd == SIOCGETTUNNEL) {
  756. memcpy(&p, &t->parms, sizeof(p));
  757. if (copy_to_user(ifr->ifr_ifru.ifru_data, &p,
  758. sizeof(p)))
  759. goto done;
  760. #ifdef CONFIG_IPV6_SIT_6RD
  761. } else {
  762. ipv6_addr_copy(&ip6rd.prefix, &t->ip6rd.prefix);
  763. ip6rd.relay_prefix = t->ip6rd.relay_prefix;
  764. ip6rd.prefixlen = t->ip6rd.prefixlen;
  765. ip6rd.relay_prefixlen = t->ip6rd.relay_prefixlen;
  766. if (copy_to_user(ifr->ifr_ifru.ifru_data, &ip6rd,
  767. sizeof(ip6rd)))
  768. goto done;
  769. #endif
  770. }
  771. err = 0;
  772. break;
  773. case SIOCADDTUNNEL:
  774. case SIOCCHGTUNNEL:
  775. err = -EPERM;
  776. if (!capable(CAP_NET_ADMIN))
  777. goto done;
  778. err = -EFAULT;
  779. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
  780. goto done;
  781. err = -EINVAL;
  782. if (p.iph.version != 4 || p.iph.protocol != IPPROTO_IPV6 ||
  783. p.iph.ihl != 5 || (p.iph.frag_off&htons(~IP_DF)))
  784. goto done;
  785. if (p.iph.ttl)
  786. p.iph.frag_off |= htons(IP_DF);
  787. t = ipip6_tunnel_locate(net, &p, cmd == SIOCADDTUNNEL);
  788. if (dev != sitn->fb_tunnel_dev && cmd == SIOCCHGTUNNEL) {
  789. if (t != NULL) {
  790. if (t->dev != dev) {
  791. err = -EEXIST;
  792. break;
  793. }
  794. } else {
  795. if (((dev->flags&IFF_POINTOPOINT) && !p.iph.daddr) ||
  796. (!(dev->flags&IFF_POINTOPOINT) && p.iph.daddr)) {
  797. err = -EINVAL;
  798. break;
  799. }
  800. t = netdev_priv(dev);
  801. ipip6_tunnel_unlink(sitn, t);
  802. t->parms.iph.saddr = p.iph.saddr;
  803. t->parms.iph.daddr = p.iph.daddr;
  804. memcpy(dev->dev_addr, &p.iph.saddr, 4);
  805. memcpy(dev->broadcast, &p.iph.daddr, 4);
  806. ipip6_tunnel_link(sitn, t);
  807. netdev_state_change(dev);
  808. }
  809. }
  810. if (t) {
  811. err = 0;
  812. if (cmd == SIOCCHGTUNNEL) {
  813. t->parms.iph.ttl = p.iph.ttl;
  814. t->parms.iph.tos = p.iph.tos;
  815. if (t->parms.link != p.link) {
  816. t->parms.link = p.link;
  817. ipip6_tunnel_bind_dev(dev);
  818. netdev_state_change(dev);
  819. }
  820. }
  821. if (copy_to_user(ifr->ifr_ifru.ifru_data, &t->parms, sizeof(p)))
  822. err = -EFAULT;
  823. } else
  824. err = (cmd == SIOCADDTUNNEL ? -ENOBUFS : -ENOENT);
  825. break;
  826. case SIOCDELTUNNEL:
  827. err = -EPERM;
  828. if (!capable(CAP_NET_ADMIN))
  829. goto done;
  830. if (dev == sitn->fb_tunnel_dev) {
  831. err = -EFAULT;
  832. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
  833. goto done;
  834. err = -ENOENT;
  835. if ((t = ipip6_tunnel_locate(net, &p, 0)) == NULL)
  836. goto done;
  837. err = -EPERM;
  838. if (t == netdev_priv(sitn->fb_tunnel_dev))
  839. goto done;
  840. dev = t->dev;
  841. }
  842. unregister_netdevice(dev);
  843. err = 0;
  844. break;
  845. case SIOCGETPRL:
  846. err = -EINVAL;
  847. if (dev == sitn->fb_tunnel_dev)
  848. goto done;
  849. err = -ENOENT;
  850. if (!(t = netdev_priv(dev)))
  851. goto done;
  852. err = ipip6_tunnel_get_prl(t, ifr->ifr_ifru.ifru_data);
  853. break;
  854. case SIOCADDPRL:
  855. case SIOCDELPRL:
  856. case SIOCCHGPRL:
  857. err = -EPERM;
  858. if (!capable(CAP_NET_ADMIN))
  859. goto done;
  860. err = -EINVAL;
  861. if (dev == sitn->fb_tunnel_dev)
  862. goto done;
  863. err = -EFAULT;
  864. if (copy_from_user(&prl, ifr->ifr_ifru.ifru_data, sizeof(prl)))
  865. goto done;
  866. err = -ENOENT;
  867. if (!(t = netdev_priv(dev)))
  868. goto done;
  869. switch (cmd) {
  870. case SIOCDELPRL:
  871. err = ipip6_tunnel_del_prl(t, &prl);
  872. break;
  873. case SIOCADDPRL:
  874. case SIOCCHGPRL:
  875. err = ipip6_tunnel_add_prl(t, &prl, cmd == SIOCCHGPRL);
  876. break;
  877. }
  878. netdev_state_change(dev);
  879. break;
  880. #ifdef CONFIG_IPV6_SIT_6RD
  881. case SIOCADD6RD:
  882. case SIOCCHG6RD:
  883. case SIOCDEL6RD:
  884. err = -EPERM;
  885. if (!capable(CAP_NET_ADMIN))
  886. goto done;
  887. err = -EFAULT;
  888. if (copy_from_user(&ip6rd, ifr->ifr_ifru.ifru_data,
  889. sizeof(ip6rd)))
  890. goto done;
  891. t = netdev_priv(dev);
  892. if (cmd != SIOCDEL6RD) {
  893. struct in6_addr prefix;
  894. __be32 relay_prefix;
  895. err = -EINVAL;
  896. if (ip6rd.relay_prefixlen > 32 ||
  897. ip6rd.prefixlen + (32 - ip6rd.relay_prefixlen) > 64)
  898. goto done;
  899. ipv6_addr_prefix(&prefix, &ip6rd.prefix,
  900. ip6rd.prefixlen);
  901. if (!ipv6_addr_equal(&prefix, &ip6rd.prefix))
  902. goto done;
  903. if (ip6rd.relay_prefixlen)
  904. relay_prefix = ip6rd.relay_prefix &
  905. htonl(0xffffffffUL <<
  906. (32 - ip6rd.relay_prefixlen));
  907. else
  908. relay_prefix = 0;
  909. if (relay_prefix != ip6rd.relay_prefix)
  910. goto done;
  911. ipv6_addr_copy(&t->ip6rd.prefix, &prefix);
  912. t->ip6rd.relay_prefix = relay_prefix;
  913. t->ip6rd.prefixlen = ip6rd.prefixlen;
  914. t->ip6rd.relay_prefixlen = ip6rd.relay_prefixlen;
  915. } else
  916. ipip6_tunnel_clone_6rd(dev, sitn);
  917. err = 0;
  918. break;
  919. #endif
  920. default:
  921. err = -EINVAL;
  922. }
  923. done:
  924. return err;
  925. }
  926. static int ipip6_tunnel_change_mtu(struct net_device *dev, int new_mtu)
  927. {
  928. if (new_mtu < IPV6_MIN_MTU || new_mtu > 0xFFF8 - sizeof(struct iphdr))
  929. return -EINVAL;
  930. dev->mtu = new_mtu;
  931. return 0;
  932. }
  933. static const struct net_device_ops ipip6_netdev_ops = {
  934. .ndo_uninit = ipip6_tunnel_uninit,
  935. .ndo_start_xmit = ipip6_tunnel_xmit,
  936. .ndo_do_ioctl = ipip6_tunnel_ioctl,
  937. .ndo_change_mtu = ipip6_tunnel_change_mtu,
  938. };
  939. static void ipip6_tunnel_setup(struct net_device *dev)
  940. {
  941. dev->netdev_ops = &ipip6_netdev_ops;
  942. dev->destructor = free_netdev;
  943. dev->type = ARPHRD_SIT;
  944. dev->hard_header_len = LL_MAX_HEADER + sizeof(struct iphdr);
  945. dev->mtu = ETH_DATA_LEN - sizeof(struct iphdr);
  946. dev->flags = IFF_NOARP;
  947. dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
  948. dev->iflink = 0;
  949. dev->addr_len = 4;
  950. dev->features |= NETIF_F_NETNS_LOCAL;
  951. }
  952. static void ipip6_tunnel_init(struct net_device *dev)
  953. {
  954. struct ip_tunnel *tunnel = netdev_priv(dev);
  955. tunnel->dev = dev;
  956. strcpy(tunnel->parms.name, dev->name);
  957. memcpy(dev->dev_addr, &tunnel->parms.iph.saddr, 4);
  958. memcpy(dev->broadcast, &tunnel->parms.iph.daddr, 4);
  959. ipip6_tunnel_bind_dev(dev);
  960. }
  961. static void __net_init ipip6_fb_tunnel_init(struct net_device *dev)
  962. {
  963. struct ip_tunnel *tunnel = netdev_priv(dev);
  964. struct iphdr *iph = &tunnel->parms.iph;
  965. struct net *net = dev_net(dev);
  966. struct sit_net *sitn = net_generic(net, sit_net_id);
  967. tunnel->dev = dev;
  968. strcpy(tunnel->parms.name, dev->name);
  969. iph->version = 4;
  970. iph->protocol = IPPROTO_IPV6;
  971. iph->ihl = 5;
  972. iph->ttl = 64;
  973. dev_hold(dev);
  974. sitn->tunnels_wc[0] = tunnel;
  975. }
  976. static struct xfrm_tunnel sit_handler = {
  977. .handler = ipip6_rcv,
  978. .err_handler = ipip6_err,
  979. .priority = 1,
  980. };
  981. static void __net_exit sit_destroy_tunnels(struct sit_net *sitn, struct list_head *head)
  982. {
  983. int prio;
  984. for (prio = 1; prio < 4; prio++) {
  985. int h;
  986. for (h = 0; h < HASH_SIZE; h++) {
  987. struct ip_tunnel *t = sitn->tunnels[prio][h];
  988. while (t != NULL) {
  989. unregister_netdevice_queue(t->dev, head);
  990. t = t->next;
  991. }
  992. }
  993. }
  994. }
  995. static int __net_init sit_init_net(struct net *net)
  996. {
  997. struct sit_net *sitn = net_generic(net, sit_net_id);
  998. int err;
  999. sitn->tunnels[0] = sitn->tunnels_wc;
  1000. sitn->tunnels[1] = sitn->tunnels_l;
  1001. sitn->tunnels[2] = sitn->tunnels_r;
  1002. sitn->tunnels[3] = sitn->tunnels_r_l;
  1003. sitn->fb_tunnel_dev = alloc_netdev(sizeof(struct ip_tunnel), "sit0",
  1004. ipip6_tunnel_setup);
  1005. if (!sitn->fb_tunnel_dev) {
  1006. err = -ENOMEM;
  1007. goto err_alloc_dev;
  1008. }
  1009. dev_net_set(sitn->fb_tunnel_dev, net);
  1010. ipip6_fb_tunnel_init(sitn->fb_tunnel_dev);
  1011. ipip6_tunnel_clone_6rd(sitn->fb_tunnel_dev, sitn);
  1012. if ((err = register_netdev(sitn->fb_tunnel_dev)))
  1013. goto err_reg_dev;
  1014. return 0;
  1015. err_reg_dev:
  1016. dev_put(sitn->fb_tunnel_dev);
  1017. free_netdev(sitn->fb_tunnel_dev);
  1018. err_alloc_dev:
  1019. return err;
  1020. }
  1021. static void __net_exit sit_exit_net(struct net *net)
  1022. {
  1023. struct sit_net *sitn = net_generic(net, sit_net_id);
  1024. LIST_HEAD(list);
  1025. rtnl_lock();
  1026. sit_destroy_tunnels(sitn, &list);
  1027. unregister_netdevice_queue(sitn->fb_tunnel_dev, &list);
  1028. unregister_netdevice_many(&list);
  1029. rtnl_unlock();
  1030. }
  1031. static struct pernet_operations sit_net_ops = {
  1032. .init = sit_init_net,
  1033. .exit = sit_exit_net,
  1034. .id = &sit_net_id,
  1035. .size = sizeof(struct sit_net),
  1036. };
  1037. static void __exit sit_cleanup(void)
  1038. {
  1039. xfrm4_tunnel_deregister(&sit_handler, AF_INET6);
  1040. unregister_pernet_device(&sit_net_ops);
  1041. rcu_barrier(); /* Wait for completion of call_rcu()'s */
  1042. }
  1043. static int __init sit_init(void)
  1044. {
  1045. int err;
  1046. printk(KERN_INFO "IPv6 over IPv4 tunneling driver\n");
  1047. err = register_pernet_device(&sit_net_ops);
  1048. if (err < 0)
  1049. return err;
  1050. err = xfrm4_tunnel_register(&sit_handler, AF_INET6);
  1051. if (err < 0) {
  1052. unregister_pernet_device(&sit_net_ops);
  1053. printk(KERN_INFO "sit init: Can't add protocol\n");
  1054. }
  1055. return err;
  1056. }
  1057. module_init(sit_init);
  1058. module_exit(sit_cleanup);
  1059. MODULE_LICENSE("GPL");
  1060. MODULE_ALIAS("sit0");