sit.c 24 KB

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