sit.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950
  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 L. Templin <fltemplin@acm.org>: 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. int i;
  147. for (i=1; i<100; i++) {
  148. sprintf(name, "sit%d", i);
  149. if (__dev_get_by_name(&init_net, name) == NULL)
  150. break;
  151. }
  152. if (i==100)
  153. goto failed;
  154. }
  155. dev = alloc_netdev(sizeof(*t), name, ipip6_tunnel_setup);
  156. if (dev == NULL)
  157. return NULL;
  158. nt = netdev_priv(dev);
  159. dev->init = ipip6_tunnel_init;
  160. nt->parms = *parms;
  161. if (parms->i_flags & SIT_ISATAP)
  162. dev->priv_flags |= IFF_ISATAP;
  163. if (register_netdevice(dev) < 0) {
  164. free_netdev(dev);
  165. goto failed;
  166. }
  167. dev_hold(dev);
  168. ipip6_tunnel_link(nt);
  169. return nt;
  170. failed:
  171. return NULL;
  172. }
  173. static void ipip6_tunnel_uninit(struct net_device *dev)
  174. {
  175. if (dev == ipip6_fb_tunnel_dev) {
  176. write_lock_bh(&ipip6_lock);
  177. tunnels_wc[0] = NULL;
  178. write_unlock_bh(&ipip6_lock);
  179. dev_put(dev);
  180. } else {
  181. ipip6_tunnel_unlink(netdev_priv(dev));
  182. dev_put(dev);
  183. }
  184. }
  185. static int ipip6_err(struct sk_buff *skb, u32 info)
  186. {
  187. #ifndef I_WISH_WORLD_WERE_PERFECT
  188. /* It is not :-( All the routers (except for Linux) return only
  189. 8 bytes of packet payload. It means, that precise relaying of
  190. ICMP in the real Internet is absolutely infeasible.
  191. */
  192. struct iphdr *iph = (struct iphdr*)skb->data;
  193. const int type = icmp_hdr(skb)->type;
  194. const int code = icmp_hdr(skb)->code;
  195. struct ip_tunnel *t;
  196. int err;
  197. switch (type) {
  198. default:
  199. case ICMP_PARAMETERPROB:
  200. return 0;
  201. case ICMP_DEST_UNREACH:
  202. switch (code) {
  203. case ICMP_SR_FAILED:
  204. case ICMP_PORT_UNREACH:
  205. /* Impossible event. */
  206. return 0;
  207. case ICMP_FRAG_NEEDED:
  208. /* Soft state for pmtu is maintained by IP core. */
  209. return 0;
  210. default:
  211. /* All others are translated to HOST_UNREACH.
  212. rfc2003 contains "deep thoughts" about NET_UNREACH,
  213. I believe they are just ether pollution. --ANK
  214. */
  215. break;
  216. }
  217. break;
  218. case ICMP_TIME_EXCEEDED:
  219. if (code != ICMP_EXC_TTL)
  220. return 0;
  221. break;
  222. }
  223. err = -ENOENT;
  224. read_lock(&ipip6_lock);
  225. t = ipip6_tunnel_lookup(iph->daddr, iph->saddr);
  226. if (t == NULL || t->parms.iph.daddr == 0)
  227. goto out;
  228. err = 0;
  229. if (t->parms.iph.ttl == 0 && type == ICMP_TIME_EXCEEDED)
  230. goto out;
  231. if (jiffies - t->err_time < IPTUNNEL_ERR_TIMEO)
  232. t->err_count++;
  233. else
  234. t->err_count = 1;
  235. t->err_time = jiffies;
  236. out:
  237. read_unlock(&ipip6_lock);
  238. return err;
  239. #else
  240. struct iphdr *iph = (struct iphdr*)dp;
  241. int hlen = iph->ihl<<2;
  242. struct ipv6hdr *iph6;
  243. const int type = icmp_hdr(skb)->type;
  244. const int code = icmp_hdr(skb)->code;
  245. int rel_type = 0;
  246. int rel_code = 0;
  247. int rel_info = 0;
  248. struct sk_buff *skb2;
  249. struct rt6_info *rt6i;
  250. if (len < hlen + sizeof(struct ipv6hdr))
  251. return;
  252. iph6 = (struct ipv6hdr*)(dp + hlen);
  253. switch (type) {
  254. default:
  255. return;
  256. case ICMP_PARAMETERPROB:
  257. if (icmp_hdr(skb)->un.gateway < hlen)
  258. return;
  259. /* So... This guy found something strange INSIDE encapsulated
  260. packet. Well, he is fool, but what can we do ?
  261. */
  262. rel_type = ICMPV6_PARAMPROB;
  263. rel_info = icmp_hdr(skb)->un.gateway - hlen;
  264. break;
  265. case ICMP_DEST_UNREACH:
  266. switch (code) {
  267. case ICMP_SR_FAILED:
  268. case ICMP_PORT_UNREACH:
  269. /* Impossible event. */
  270. return;
  271. case ICMP_FRAG_NEEDED:
  272. /* Too complicated case ... */
  273. return;
  274. default:
  275. /* All others are translated to HOST_UNREACH.
  276. rfc2003 contains "deep thoughts" about NET_UNREACH,
  277. I believe, it is just ether pollution. --ANK
  278. */
  279. rel_type = ICMPV6_DEST_UNREACH;
  280. rel_code = ICMPV6_ADDR_UNREACH;
  281. break;
  282. }
  283. break;
  284. case ICMP_TIME_EXCEEDED:
  285. if (code != ICMP_EXC_TTL)
  286. return;
  287. rel_type = ICMPV6_TIME_EXCEED;
  288. rel_code = ICMPV6_EXC_HOPLIMIT;
  289. break;
  290. }
  291. /* Prepare fake skb to feed it to icmpv6_send */
  292. skb2 = skb_clone(skb, GFP_ATOMIC);
  293. if (skb2 == NULL)
  294. return 0;
  295. dst_release(skb2->dst);
  296. skb2->dst = NULL;
  297. skb_pull(skb2, skb->data - (u8*)iph6);
  298. skb_reset_network_header(skb2);
  299. /* Try to guess incoming interface */
  300. rt6i = rt6_lookup(&iph6->saddr, NULL, NULL, 0);
  301. if (rt6i && rt6i->rt6i_dev) {
  302. skb2->dev = rt6i->rt6i_dev;
  303. rt6i = rt6_lookup(&iph6->daddr, &iph6->saddr, NULL, 0);
  304. if (rt6i && rt6i->rt6i_dev && rt6i->rt6i_dev->type == ARPHRD_SIT) {
  305. struct ip_tunnel *t = netdev_priv(rt6i->rt6i_dev);
  306. if (rel_type == ICMPV6_TIME_EXCEED && t->parms.iph.ttl) {
  307. rel_type = ICMPV6_DEST_UNREACH;
  308. rel_code = ICMPV6_ADDR_UNREACH;
  309. }
  310. icmpv6_send(skb2, rel_type, rel_code, rel_info, skb2->dev);
  311. }
  312. }
  313. kfree_skb(skb2);
  314. return 0;
  315. #endif
  316. }
  317. static inline void ipip6_ecn_decapsulate(struct iphdr *iph, struct sk_buff *skb)
  318. {
  319. if (INET_ECN_is_ce(iph->tos))
  320. IP6_ECN_set_ce(ipv6_hdr(skb));
  321. }
  322. /* ISATAP (RFC4214) - check source address */
  323. static int
  324. isatap_srcok(struct sk_buff *skb, struct iphdr *iph, struct net_device *dev)
  325. {
  326. struct neighbour *neigh;
  327. struct dst_entry *dst;
  328. struct rt6_info *rt;
  329. struct flowi fl;
  330. struct in6_addr *addr6;
  331. struct in6_addr rtr;
  332. struct ipv6hdr *iph6;
  333. int ok = 0;
  334. /* from onlink default router */
  335. ipv6_addr_set(&rtr, htonl(0xFE800000), 0, 0, 0);
  336. ipv6_isatap_eui64(rtr.s6_addr + 8, iph->saddr);
  337. if ((rt = rt6_get_dflt_router(&rtr, dev))) {
  338. dst_release(&rt->u.dst);
  339. return 1;
  340. }
  341. iph6 = ipv6_hdr(skb);
  342. memset(&fl, 0, sizeof(fl));
  343. fl.proto = iph6->nexthdr;
  344. ipv6_addr_copy(&fl.fl6_dst, &iph6->saddr);
  345. fl.oif = dev->ifindex;
  346. security_skb_classify_flow(skb, &fl);
  347. dst = ip6_route_output(NULL, &fl);
  348. if (!dst->error && (dst->dev == dev) && (neigh = dst->neighbour)) {
  349. addr6 = (struct in6_addr*)&neigh->primary_key;
  350. /* from correct previous hop */
  351. if (ipv6_addr_is_isatap(addr6) &&
  352. (addr6->s6_addr32[3] == iph->saddr))
  353. ok = 1;
  354. }
  355. dst_release(dst);
  356. return ok;
  357. }
  358. static int ipip6_rcv(struct sk_buff *skb)
  359. {
  360. struct iphdr *iph;
  361. struct ip_tunnel *tunnel;
  362. if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
  363. goto out;
  364. iph = ip_hdr(skb);
  365. read_lock(&ipip6_lock);
  366. if ((tunnel = ipip6_tunnel_lookup(iph->saddr, iph->daddr)) != NULL) {
  367. secpath_reset(skb);
  368. skb->mac_header = skb->network_header;
  369. skb_reset_network_header(skb);
  370. IPCB(skb)->flags = 0;
  371. skb->protocol = htons(ETH_P_IPV6);
  372. skb->pkt_type = PACKET_HOST;
  373. if ((tunnel->dev->priv_flags & IFF_ISATAP) &&
  374. !isatap_srcok(skb, iph, tunnel->dev)) {
  375. tunnel->stat.rx_errors++;
  376. read_unlock(&ipip6_lock);
  377. kfree_skb(skb);
  378. return 0;
  379. }
  380. tunnel->stat.rx_packets++;
  381. tunnel->stat.rx_bytes += skb->len;
  382. skb->dev = tunnel->dev;
  383. dst_release(skb->dst);
  384. skb->dst = NULL;
  385. nf_reset(skb);
  386. ipip6_ecn_decapsulate(iph, skb);
  387. netif_rx(skb);
  388. read_unlock(&ipip6_lock);
  389. return 0;
  390. }
  391. icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
  392. kfree_skb(skb);
  393. read_unlock(&ipip6_lock);
  394. out:
  395. return 0;
  396. }
  397. /* Returns the embedded IPv4 address if the IPv6 address
  398. comes from 6to4 (RFC 3056) addr space */
  399. static inline __be32 try_6to4(struct in6_addr *v6dst)
  400. {
  401. __be32 dst = 0;
  402. if (v6dst->s6_addr16[0] == htons(0x2002)) {
  403. /* 6to4 v6 addr has 16 bits prefix, 32 v4addr, 16 SLA, ... */
  404. memcpy(&dst, &v6dst->s6_addr16[1], 4);
  405. }
  406. return dst;
  407. }
  408. /*
  409. * This function assumes it is being called from dev_queue_xmit()
  410. * and that skb is filled properly by that function.
  411. */
  412. static int ipip6_tunnel_xmit(struct sk_buff *skb, struct net_device *dev)
  413. {
  414. struct ip_tunnel *tunnel = netdev_priv(dev);
  415. struct net_device_stats *stats = &tunnel->stat;
  416. struct iphdr *tiph = &tunnel->parms.iph;
  417. struct ipv6hdr *iph6 = ipv6_hdr(skb);
  418. u8 tos = tunnel->parms.iph.tos;
  419. struct rtable *rt; /* Route to the other host */
  420. struct net_device *tdev; /* Device to other host */
  421. struct iphdr *iph; /* Our new IP header */
  422. unsigned int max_headroom; /* The extra header space needed */
  423. __be32 dst = tiph->daddr;
  424. int mtu;
  425. struct in6_addr *addr6;
  426. int addr_type;
  427. if (tunnel->recursion++) {
  428. tunnel->stat.collisions++;
  429. goto tx_error;
  430. }
  431. if (skb->protocol != htons(ETH_P_IPV6))
  432. goto tx_error;
  433. /* ISATAP (RFC4214) - must come before 6to4 */
  434. if (dev->priv_flags & IFF_ISATAP) {
  435. struct neighbour *neigh = NULL;
  436. if (skb->dst)
  437. neigh = skb->dst->neighbour;
  438. if (neigh == NULL) {
  439. if (net_ratelimit())
  440. printk(KERN_DEBUG "sit: nexthop == NULL\n");
  441. goto tx_error;
  442. }
  443. addr6 = (struct in6_addr*)&neigh->primary_key;
  444. addr_type = ipv6_addr_type(addr6);
  445. if ((addr_type & IPV6_ADDR_UNICAST) &&
  446. ipv6_addr_is_isatap(addr6))
  447. dst = addr6->s6_addr32[3];
  448. else
  449. goto tx_error;
  450. }
  451. if (!dst)
  452. dst = try_6to4(&iph6->daddr);
  453. if (!dst) {
  454. struct neighbour *neigh = NULL;
  455. if (skb->dst)
  456. neigh = skb->dst->neighbour;
  457. if (neigh == NULL) {
  458. if (net_ratelimit())
  459. printk(KERN_DEBUG "sit: nexthop == NULL\n");
  460. goto tx_error;
  461. }
  462. addr6 = (struct in6_addr*)&neigh->primary_key;
  463. addr_type = ipv6_addr_type(addr6);
  464. if (addr_type == IPV6_ADDR_ANY) {
  465. addr6 = &ipv6_hdr(skb)->daddr;
  466. addr_type = ipv6_addr_type(addr6);
  467. }
  468. if ((addr_type & IPV6_ADDR_COMPATv4) == 0)
  469. goto tx_error_icmp;
  470. dst = addr6->s6_addr32[3];
  471. }
  472. {
  473. struct flowi fl = { .nl_u = { .ip4_u =
  474. { .daddr = dst,
  475. .saddr = tiph->saddr,
  476. .tos = RT_TOS(tos) } },
  477. .oif = tunnel->parms.link,
  478. .proto = IPPROTO_IPV6 };
  479. if (ip_route_output_key(&init_net, &rt, &fl)) {
  480. tunnel->stat.tx_carrier_errors++;
  481. goto tx_error_icmp;
  482. }
  483. }
  484. if (rt->rt_type != RTN_UNICAST) {
  485. ip_rt_put(rt);
  486. tunnel->stat.tx_carrier_errors++;
  487. goto tx_error_icmp;
  488. }
  489. tdev = rt->u.dst.dev;
  490. if (tdev == dev) {
  491. ip_rt_put(rt);
  492. tunnel->stat.collisions++;
  493. goto tx_error;
  494. }
  495. if (tiph->frag_off)
  496. mtu = dst_mtu(&rt->u.dst) - sizeof(struct iphdr);
  497. else
  498. mtu = skb->dst ? dst_mtu(skb->dst) : dev->mtu;
  499. if (mtu < 68) {
  500. tunnel->stat.collisions++;
  501. ip_rt_put(rt);
  502. goto tx_error;
  503. }
  504. if (mtu < IPV6_MIN_MTU)
  505. mtu = IPV6_MIN_MTU;
  506. if (tunnel->parms.iph.daddr && skb->dst)
  507. skb->dst->ops->update_pmtu(skb->dst, mtu);
  508. if (skb->len > mtu) {
  509. icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu, dev);
  510. ip_rt_put(rt);
  511. goto tx_error;
  512. }
  513. if (tunnel->err_count > 0) {
  514. if (jiffies - tunnel->err_time < IPTUNNEL_ERR_TIMEO) {
  515. tunnel->err_count--;
  516. dst_link_failure(skb);
  517. } else
  518. tunnel->err_count = 0;
  519. }
  520. /*
  521. * Okay, now see if we can stuff it in the buffer as-is.
  522. */
  523. max_headroom = LL_RESERVED_SPACE(tdev)+sizeof(struct iphdr);
  524. if (skb_headroom(skb) < max_headroom || skb_shared(skb) ||
  525. (skb_cloned(skb) && !skb_clone_writable(skb, 0))) {
  526. struct sk_buff *new_skb = skb_realloc_headroom(skb, max_headroom);
  527. if (!new_skb) {
  528. ip_rt_put(rt);
  529. stats->tx_dropped++;
  530. dev_kfree_skb(skb);
  531. tunnel->recursion--;
  532. return 0;
  533. }
  534. if (skb->sk)
  535. skb_set_owner_w(new_skb, skb->sk);
  536. dev_kfree_skb(skb);
  537. skb = new_skb;
  538. iph6 = ipv6_hdr(skb);
  539. }
  540. skb->transport_header = skb->network_header;
  541. skb_push(skb, sizeof(struct iphdr));
  542. skb_reset_network_header(skb);
  543. memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
  544. IPCB(skb)->flags = 0;
  545. dst_release(skb->dst);
  546. skb->dst = &rt->u.dst;
  547. /*
  548. * Push down and install the IPIP header.
  549. */
  550. iph = ip_hdr(skb);
  551. iph->version = 4;
  552. iph->ihl = sizeof(struct iphdr)>>2;
  553. if (mtu > IPV6_MIN_MTU)
  554. iph->frag_off = htons(IP_DF);
  555. else
  556. iph->frag_off = 0;
  557. iph->protocol = IPPROTO_IPV6;
  558. iph->tos = INET_ECN_encapsulate(tos, ipv6_get_dsfield(iph6));
  559. iph->daddr = rt->rt_dst;
  560. iph->saddr = rt->rt_src;
  561. if ((iph->ttl = tiph->ttl) == 0)
  562. iph->ttl = iph6->hop_limit;
  563. nf_reset(skb);
  564. IPTUNNEL_XMIT();
  565. tunnel->recursion--;
  566. return 0;
  567. tx_error_icmp:
  568. dst_link_failure(skb);
  569. tx_error:
  570. stats->tx_errors++;
  571. dev_kfree_skb(skb);
  572. tunnel->recursion--;
  573. return 0;
  574. }
  575. static void ipip6_tunnel_bind_dev(struct net_device *dev)
  576. {
  577. struct net_device *tdev = NULL;
  578. struct ip_tunnel *tunnel;
  579. struct iphdr *iph;
  580. tunnel = netdev_priv(dev);
  581. iph = &tunnel->parms.iph;
  582. if (iph->daddr) {
  583. struct flowi fl = { .nl_u = { .ip4_u =
  584. { .daddr = iph->daddr,
  585. .saddr = iph->saddr,
  586. .tos = RT_TOS(iph->tos) } },
  587. .oif = tunnel->parms.link,
  588. .proto = IPPROTO_IPV6 };
  589. struct rtable *rt;
  590. if (!ip_route_output_key(&init_net, &rt, &fl)) {
  591. tdev = rt->u.dst.dev;
  592. ip_rt_put(rt);
  593. }
  594. dev->flags |= IFF_POINTOPOINT;
  595. }
  596. if (!tdev && tunnel->parms.link)
  597. tdev = __dev_get_by_index(&init_net, tunnel->parms.link);
  598. if (tdev) {
  599. dev->hard_header_len = tdev->hard_header_len + sizeof(struct iphdr);
  600. dev->mtu = tdev->mtu - sizeof(struct iphdr);
  601. if (dev->mtu < IPV6_MIN_MTU)
  602. dev->mtu = IPV6_MIN_MTU;
  603. }
  604. dev->iflink = tunnel->parms.link;
  605. }
  606. static int
  607. ipip6_tunnel_ioctl (struct net_device *dev, struct ifreq *ifr, int cmd)
  608. {
  609. int err = 0;
  610. struct ip_tunnel_parm p;
  611. struct ip_tunnel *t;
  612. switch (cmd) {
  613. case SIOCGETTUNNEL:
  614. t = NULL;
  615. if (dev == ipip6_fb_tunnel_dev) {
  616. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) {
  617. err = -EFAULT;
  618. break;
  619. }
  620. t = ipip6_tunnel_locate(&p, 0);
  621. }
  622. if (t == NULL)
  623. t = netdev_priv(dev);
  624. memcpy(&p, &t->parms, sizeof(p));
  625. if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p)))
  626. err = -EFAULT;
  627. break;
  628. case SIOCADDTUNNEL:
  629. case SIOCCHGTUNNEL:
  630. err = -EPERM;
  631. if (!capable(CAP_NET_ADMIN))
  632. goto done;
  633. err = -EFAULT;
  634. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
  635. goto done;
  636. err = -EINVAL;
  637. if (p.iph.version != 4 || p.iph.protocol != IPPROTO_IPV6 ||
  638. p.iph.ihl != 5 || (p.iph.frag_off&htons(~IP_DF)))
  639. goto done;
  640. if (p.iph.ttl)
  641. p.iph.frag_off |= htons(IP_DF);
  642. t = ipip6_tunnel_locate(&p, cmd == SIOCADDTUNNEL);
  643. if (dev != ipip6_fb_tunnel_dev && cmd == SIOCCHGTUNNEL) {
  644. if (t != NULL) {
  645. if (t->dev != dev) {
  646. err = -EEXIST;
  647. break;
  648. }
  649. } else {
  650. if (((dev->flags&IFF_POINTOPOINT) && !p.iph.daddr) ||
  651. (!(dev->flags&IFF_POINTOPOINT) && p.iph.daddr)) {
  652. err = -EINVAL;
  653. break;
  654. }
  655. t = netdev_priv(dev);
  656. ipip6_tunnel_unlink(t);
  657. t->parms.iph.saddr = p.iph.saddr;
  658. t->parms.iph.daddr = p.iph.daddr;
  659. memcpy(dev->dev_addr, &p.iph.saddr, 4);
  660. memcpy(dev->broadcast, &p.iph.daddr, 4);
  661. ipip6_tunnel_link(t);
  662. netdev_state_change(dev);
  663. }
  664. }
  665. if (t) {
  666. err = 0;
  667. if (cmd == SIOCCHGTUNNEL) {
  668. t->parms.iph.ttl = p.iph.ttl;
  669. t->parms.iph.tos = p.iph.tos;
  670. if (t->parms.link != p.link) {
  671. t->parms.link = p.link;
  672. ipip6_tunnel_bind_dev(dev);
  673. netdev_state_change(dev);
  674. }
  675. }
  676. if (copy_to_user(ifr->ifr_ifru.ifru_data, &t->parms, sizeof(p)))
  677. err = -EFAULT;
  678. } else
  679. err = (cmd == SIOCADDTUNNEL ? -ENOBUFS : -ENOENT);
  680. break;
  681. case SIOCDELTUNNEL:
  682. err = -EPERM;
  683. if (!capable(CAP_NET_ADMIN))
  684. goto done;
  685. if (dev == ipip6_fb_tunnel_dev) {
  686. err = -EFAULT;
  687. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
  688. goto done;
  689. err = -ENOENT;
  690. if ((t = ipip6_tunnel_locate(&p, 0)) == NULL)
  691. goto done;
  692. err = -EPERM;
  693. if (t == netdev_priv(ipip6_fb_tunnel_dev))
  694. goto done;
  695. dev = t->dev;
  696. }
  697. unregister_netdevice(dev);
  698. err = 0;
  699. break;
  700. default:
  701. err = -EINVAL;
  702. }
  703. done:
  704. return err;
  705. }
  706. static struct net_device_stats *ipip6_tunnel_get_stats(struct net_device *dev)
  707. {
  708. return &(((struct ip_tunnel*)netdev_priv(dev))->stat);
  709. }
  710. static int ipip6_tunnel_change_mtu(struct net_device *dev, int new_mtu)
  711. {
  712. if (new_mtu < IPV6_MIN_MTU || new_mtu > 0xFFF8 - sizeof(struct iphdr))
  713. return -EINVAL;
  714. dev->mtu = new_mtu;
  715. return 0;
  716. }
  717. static void ipip6_tunnel_setup(struct net_device *dev)
  718. {
  719. dev->uninit = ipip6_tunnel_uninit;
  720. dev->destructor = free_netdev;
  721. dev->hard_start_xmit = ipip6_tunnel_xmit;
  722. dev->get_stats = ipip6_tunnel_get_stats;
  723. dev->do_ioctl = ipip6_tunnel_ioctl;
  724. dev->change_mtu = ipip6_tunnel_change_mtu;
  725. dev->type = ARPHRD_SIT;
  726. dev->hard_header_len = LL_MAX_HEADER + sizeof(struct iphdr);
  727. dev->mtu = ETH_DATA_LEN - sizeof(struct iphdr);
  728. dev->flags = IFF_NOARP;
  729. dev->iflink = 0;
  730. dev->addr_len = 4;
  731. }
  732. static int ipip6_tunnel_init(struct net_device *dev)
  733. {
  734. struct ip_tunnel *tunnel;
  735. tunnel = netdev_priv(dev);
  736. tunnel->dev = dev;
  737. strcpy(tunnel->parms.name, dev->name);
  738. memcpy(dev->dev_addr, &tunnel->parms.iph.saddr, 4);
  739. memcpy(dev->broadcast, &tunnel->parms.iph.daddr, 4);
  740. ipip6_tunnel_bind_dev(dev);
  741. return 0;
  742. }
  743. static int __init ipip6_fb_tunnel_init(struct net_device *dev)
  744. {
  745. struct ip_tunnel *tunnel = netdev_priv(dev);
  746. struct iphdr *iph = &tunnel->parms.iph;
  747. tunnel->dev = dev;
  748. strcpy(tunnel->parms.name, dev->name);
  749. iph->version = 4;
  750. iph->protocol = IPPROTO_IPV6;
  751. iph->ihl = 5;
  752. iph->ttl = 64;
  753. dev_hold(dev);
  754. tunnels_wc[0] = tunnel;
  755. return 0;
  756. }
  757. static struct xfrm_tunnel sit_handler = {
  758. .handler = ipip6_rcv,
  759. .err_handler = ipip6_err,
  760. .priority = 1,
  761. };
  762. static void __exit sit_destroy_tunnels(void)
  763. {
  764. int prio;
  765. for (prio = 1; prio < 4; prio++) {
  766. int h;
  767. for (h = 0; h < HASH_SIZE; h++) {
  768. struct ip_tunnel *t;
  769. while ((t = tunnels[prio][h]) != NULL)
  770. unregister_netdevice(t->dev);
  771. }
  772. }
  773. }
  774. static void __exit sit_cleanup(void)
  775. {
  776. xfrm4_tunnel_deregister(&sit_handler, AF_INET6);
  777. rtnl_lock();
  778. sit_destroy_tunnels();
  779. unregister_netdevice(ipip6_fb_tunnel_dev);
  780. rtnl_unlock();
  781. }
  782. static int __init sit_init(void)
  783. {
  784. int err;
  785. printk(KERN_INFO "IPv6 over IPv4 tunneling driver\n");
  786. if (xfrm4_tunnel_register(&sit_handler, AF_INET6) < 0) {
  787. printk(KERN_INFO "sit init: Can't add protocol\n");
  788. return -EAGAIN;
  789. }
  790. ipip6_fb_tunnel_dev = alloc_netdev(sizeof(struct ip_tunnel), "sit0",
  791. ipip6_tunnel_setup);
  792. if (!ipip6_fb_tunnel_dev) {
  793. err = -ENOMEM;
  794. goto err1;
  795. }
  796. ipip6_fb_tunnel_dev->init = ipip6_fb_tunnel_init;
  797. if ((err = register_netdev(ipip6_fb_tunnel_dev)))
  798. goto err2;
  799. out:
  800. return err;
  801. err2:
  802. free_netdev(ipip6_fb_tunnel_dev);
  803. err1:
  804. xfrm4_tunnel_deregister(&sit_handler, AF_INET6);
  805. goto out;
  806. }
  807. module_init(sit_init);
  808. module_exit(sit_cleanup);
  809. MODULE_LICENSE("GPL");
  810. MODULE_ALIAS("sit0");