sit.c 28 KB

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