sit.c 21 KB

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