sit.c 28 KB

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