sit.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859
  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. */
  20. #include <linux/module.h>
  21. #include <linux/capability.h>
  22. #include <linux/errno.h>
  23. #include <linux/types.h>
  24. #include <linux/socket.h>
  25. #include <linux/sockios.h>
  26. #include <linux/net.h>
  27. #include <linux/in6.h>
  28. #include <linux/netdevice.h>
  29. #include <linux/if_arp.h>
  30. #include <linux/icmp.h>
  31. #include <asm/uaccess.h>
  32. #include <linux/init.h>
  33. #include <linux/netfilter_ipv4.h>
  34. #include <linux/if_ether.h>
  35. #include <net/sock.h>
  36. #include <net/snmp.h>
  37. #include <net/ipv6.h>
  38. #include <net/protocol.h>
  39. #include <net/transp_v6.h>
  40. #include <net/ip6_fib.h>
  41. #include <net/ip6_route.h>
  42. #include <net/ndisc.h>
  43. #include <net/addrconf.h>
  44. #include <net/ip.h>
  45. #include <net/udp.h>
  46. #include <net/icmp.h>
  47. #include <net/ipip.h>
  48. #include <net/inet_ecn.h>
  49. #include <net/xfrm.h>
  50. #include <net/dsfield.h>
  51. /*
  52. This version of net/ipv6/sit.c is cloned of net/ipv4/ip_gre.c
  53. For comments look at net/ipv4/ip_gre.c --ANK
  54. */
  55. #define HASH_SIZE 16
  56. #define HASH(addr) (((__force u32)addr^((__force u32)addr>>4))&0xF)
  57. static int ipip6_fb_tunnel_init(struct net_device *dev);
  58. static int ipip6_tunnel_init(struct net_device *dev);
  59. static void ipip6_tunnel_setup(struct net_device *dev);
  60. static struct net_device *ipip6_fb_tunnel_dev;
  61. static struct ip_tunnel *tunnels_r_l[HASH_SIZE];
  62. static struct ip_tunnel *tunnels_r[HASH_SIZE];
  63. static struct ip_tunnel *tunnels_l[HASH_SIZE];
  64. static struct ip_tunnel *tunnels_wc[1];
  65. static struct ip_tunnel **tunnels[4] = { tunnels_wc, tunnels_l, tunnels_r, tunnels_r_l };
  66. static DEFINE_RWLOCK(ipip6_lock);
  67. static struct ip_tunnel * ipip6_tunnel_lookup(__be32 remote, __be32 local)
  68. {
  69. unsigned h0 = HASH(remote);
  70. unsigned h1 = HASH(local);
  71. struct ip_tunnel *t;
  72. for (t = tunnels_r_l[h0^h1]; t; t = t->next) {
  73. if (local == t->parms.iph.saddr &&
  74. remote == t->parms.iph.daddr && (t->dev->flags&IFF_UP))
  75. return t;
  76. }
  77. for (t = tunnels_r[h0]; t; t = t->next) {
  78. if (remote == t->parms.iph.daddr && (t->dev->flags&IFF_UP))
  79. return t;
  80. }
  81. for (t = tunnels_l[h1]; t; t = t->next) {
  82. if (local == t->parms.iph.saddr && (t->dev->flags&IFF_UP))
  83. return t;
  84. }
  85. if ((t = tunnels_wc[0]) != NULL && (t->dev->flags&IFF_UP))
  86. return t;
  87. return NULL;
  88. }
  89. static struct ip_tunnel **__ipip6_bucket(struct ip_tunnel_parm *parms)
  90. {
  91. __be32 remote = parms->iph.daddr;
  92. __be32 local = parms->iph.saddr;
  93. unsigned h = 0;
  94. int prio = 0;
  95. if (remote) {
  96. prio |= 2;
  97. h ^= HASH(remote);
  98. }
  99. if (local) {
  100. prio |= 1;
  101. h ^= HASH(local);
  102. }
  103. return &tunnels[prio][h];
  104. }
  105. static inline struct ip_tunnel **ipip6_bucket(struct ip_tunnel *t)
  106. {
  107. return __ipip6_bucket(&t->parms);
  108. }
  109. static void ipip6_tunnel_unlink(struct ip_tunnel *t)
  110. {
  111. struct ip_tunnel **tp;
  112. for (tp = ipip6_bucket(t); *tp; tp = &(*tp)->next) {
  113. if (t == *tp) {
  114. write_lock_bh(&ipip6_lock);
  115. *tp = t->next;
  116. write_unlock_bh(&ipip6_lock);
  117. break;
  118. }
  119. }
  120. }
  121. static void ipip6_tunnel_link(struct ip_tunnel *t)
  122. {
  123. struct ip_tunnel **tp = ipip6_bucket(t);
  124. t->next = *tp;
  125. write_lock_bh(&ipip6_lock);
  126. *tp = t;
  127. write_unlock_bh(&ipip6_lock);
  128. }
  129. static struct ip_tunnel * ipip6_tunnel_locate(struct ip_tunnel_parm *parms, int create)
  130. {
  131. __be32 remote = parms->iph.daddr;
  132. __be32 local = parms->iph.saddr;
  133. struct ip_tunnel *t, **tp, *nt;
  134. struct net_device *dev;
  135. char name[IFNAMSIZ];
  136. for (tp = __ipip6_bucket(parms); (t = *tp) != NULL; tp = &t->next) {
  137. if (local == t->parms.iph.saddr && remote == t->parms.iph.daddr)
  138. return t;
  139. }
  140. if (!create)
  141. goto failed;
  142. if (parms->name[0])
  143. strlcpy(name, parms->name, IFNAMSIZ);
  144. else {
  145. int i;
  146. for (i=1; i<100; i++) {
  147. sprintf(name, "sit%d", i);
  148. if (__dev_get_by_name(&init_net, name) == NULL)
  149. break;
  150. }
  151. if (i==100)
  152. goto failed;
  153. }
  154. dev = alloc_netdev(sizeof(*t), name, ipip6_tunnel_setup);
  155. if (dev == NULL)
  156. return NULL;
  157. nt = netdev_priv(dev);
  158. dev->init = ipip6_tunnel_init;
  159. nt->parms = *parms;
  160. if (register_netdevice(dev) < 0) {
  161. free_netdev(dev);
  162. goto failed;
  163. }
  164. dev_hold(dev);
  165. ipip6_tunnel_link(nt);
  166. return nt;
  167. failed:
  168. return NULL;
  169. }
  170. static void ipip6_tunnel_uninit(struct net_device *dev)
  171. {
  172. if (dev == ipip6_fb_tunnel_dev) {
  173. write_lock_bh(&ipip6_lock);
  174. tunnels_wc[0] = NULL;
  175. write_unlock_bh(&ipip6_lock);
  176. dev_put(dev);
  177. } else {
  178. ipip6_tunnel_unlink(netdev_priv(dev));
  179. dev_put(dev);
  180. }
  181. }
  182. static int ipip6_err(struct sk_buff *skb, u32 info)
  183. {
  184. #ifndef I_WISH_WORLD_WERE_PERFECT
  185. /* It is not :-( All the routers (except for Linux) return only
  186. 8 bytes of packet payload. It means, that precise relaying of
  187. ICMP in the real Internet is absolutely infeasible.
  188. */
  189. struct iphdr *iph = (struct iphdr*)skb->data;
  190. const int type = icmp_hdr(skb)->type;
  191. const int code = icmp_hdr(skb)->code;
  192. struct ip_tunnel *t;
  193. int err;
  194. switch (type) {
  195. default:
  196. case ICMP_PARAMETERPROB:
  197. return 0;
  198. case ICMP_DEST_UNREACH:
  199. switch (code) {
  200. case ICMP_SR_FAILED:
  201. case ICMP_PORT_UNREACH:
  202. /* Impossible event. */
  203. return 0;
  204. case ICMP_FRAG_NEEDED:
  205. /* Soft state for pmtu is maintained by IP core. */
  206. return 0;
  207. default:
  208. /* All others are translated to HOST_UNREACH.
  209. rfc2003 contains "deep thoughts" about NET_UNREACH,
  210. I believe they are just ether pollution. --ANK
  211. */
  212. break;
  213. }
  214. break;
  215. case ICMP_TIME_EXCEEDED:
  216. if (code != ICMP_EXC_TTL)
  217. return 0;
  218. break;
  219. }
  220. err = -ENOENT;
  221. read_lock(&ipip6_lock);
  222. t = ipip6_tunnel_lookup(iph->daddr, iph->saddr);
  223. if (t == NULL || t->parms.iph.daddr == 0)
  224. goto out;
  225. err = 0;
  226. if (t->parms.iph.ttl == 0 && type == ICMP_TIME_EXCEEDED)
  227. goto out;
  228. if (jiffies - t->err_time < IPTUNNEL_ERR_TIMEO)
  229. t->err_count++;
  230. else
  231. t->err_count = 1;
  232. t->err_time = jiffies;
  233. out:
  234. read_unlock(&ipip6_lock);
  235. return err;
  236. #else
  237. struct iphdr *iph = (struct iphdr*)dp;
  238. int hlen = iph->ihl<<2;
  239. struct ipv6hdr *iph6;
  240. const int type = icmp_hdr(skb)->type;
  241. const int code = icmp_hdr(skb)->code;
  242. int rel_type = 0;
  243. int rel_code = 0;
  244. int rel_info = 0;
  245. struct sk_buff *skb2;
  246. struct rt6_info *rt6i;
  247. if (len < hlen + sizeof(struct ipv6hdr))
  248. return;
  249. iph6 = (struct ipv6hdr*)(dp + hlen);
  250. switch (type) {
  251. default:
  252. return;
  253. case ICMP_PARAMETERPROB:
  254. if (icmp_hdr(skb)->un.gateway < hlen)
  255. return;
  256. /* So... This guy found something strange INSIDE encapsulated
  257. packet. Well, he is fool, but what can we do ?
  258. */
  259. rel_type = ICMPV6_PARAMPROB;
  260. rel_info = icmp_hdr(skb)->un.gateway - hlen;
  261. break;
  262. case ICMP_DEST_UNREACH:
  263. switch (code) {
  264. case ICMP_SR_FAILED:
  265. case ICMP_PORT_UNREACH:
  266. /* Impossible event. */
  267. return;
  268. case ICMP_FRAG_NEEDED:
  269. /* Too complicated case ... */
  270. return;
  271. default:
  272. /* All others are translated to HOST_UNREACH.
  273. rfc2003 contains "deep thoughts" about NET_UNREACH,
  274. I believe, it is just ether pollution. --ANK
  275. */
  276. rel_type = ICMPV6_DEST_UNREACH;
  277. rel_code = ICMPV6_ADDR_UNREACH;
  278. break;
  279. }
  280. break;
  281. case ICMP_TIME_EXCEEDED:
  282. if (code != ICMP_EXC_TTL)
  283. return;
  284. rel_type = ICMPV6_TIME_EXCEED;
  285. rel_code = ICMPV6_EXC_HOPLIMIT;
  286. break;
  287. }
  288. /* Prepare fake skb to feed it to icmpv6_send */
  289. skb2 = skb_clone(skb, GFP_ATOMIC);
  290. if (skb2 == NULL)
  291. return 0;
  292. dst_release(skb2->dst);
  293. skb2->dst = NULL;
  294. skb_pull(skb2, skb->data - (u8*)iph6);
  295. skb_reset_network_header(skb2);
  296. /* Try to guess incoming interface */
  297. rt6i = rt6_lookup(&iph6->saddr, NULL, NULL, 0);
  298. if (rt6i && rt6i->rt6i_dev) {
  299. skb2->dev = rt6i->rt6i_dev;
  300. rt6i = rt6_lookup(&iph6->daddr, &iph6->saddr, NULL, 0);
  301. if (rt6i && rt6i->rt6i_dev && rt6i->rt6i_dev->type == ARPHRD_SIT) {
  302. struct ip_tunnel *t = netdev_priv(rt6i->rt6i_dev);
  303. if (rel_type == ICMPV6_TIME_EXCEED && t->parms.iph.ttl) {
  304. rel_type = ICMPV6_DEST_UNREACH;
  305. rel_code = ICMPV6_ADDR_UNREACH;
  306. }
  307. icmpv6_send(skb2, rel_type, rel_code, rel_info, skb2->dev);
  308. }
  309. }
  310. kfree_skb(skb2);
  311. return 0;
  312. #endif
  313. }
  314. static inline void ipip6_ecn_decapsulate(struct iphdr *iph, struct sk_buff *skb)
  315. {
  316. if (INET_ECN_is_ce(iph->tos))
  317. IP6_ECN_set_ce(ipv6_hdr(skb));
  318. }
  319. static int ipip6_rcv(struct sk_buff *skb)
  320. {
  321. struct iphdr *iph;
  322. struct ip_tunnel *tunnel;
  323. if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
  324. goto out;
  325. iph = ip_hdr(skb);
  326. read_lock(&ipip6_lock);
  327. if ((tunnel = ipip6_tunnel_lookup(iph->saddr, iph->daddr)) != NULL) {
  328. secpath_reset(skb);
  329. skb->mac_header = skb->network_header;
  330. skb_reset_network_header(skb);
  331. IPCB(skb)->flags = 0;
  332. skb->protocol = htons(ETH_P_IPV6);
  333. skb->pkt_type = PACKET_HOST;
  334. tunnel->stat.rx_packets++;
  335. tunnel->stat.rx_bytes += skb->len;
  336. skb->dev = tunnel->dev;
  337. dst_release(skb->dst);
  338. skb->dst = NULL;
  339. nf_reset(skb);
  340. ipip6_ecn_decapsulate(iph, skb);
  341. netif_rx(skb);
  342. read_unlock(&ipip6_lock);
  343. return 0;
  344. }
  345. icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
  346. kfree_skb(skb);
  347. read_unlock(&ipip6_lock);
  348. out:
  349. return 0;
  350. }
  351. /* Returns the embedded IPv4 address if the IPv6 address
  352. comes from 6to4 (RFC 3056) addr space */
  353. static inline __be32 try_6to4(struct in6_addr *v6dst)
  354. {
  355. __be32 dst = 0;
  356. if (v6dst->s6_addr16[0] == htons(0x2002)) {
  357. /* 6to4 v6 addr has 16 bits prefix, 32 v4addr, 16 SLA, ... */
  358. memcpy(&dst, &v6dst->s6_addr16[1], 4);
  359. }
  360. return dst;
  361. }
  362. /*
  363. * This function assumes it is being called from dev_queue_xmit()
  364. * and that skb is filled properly by that function.
  365. */
  366. static int ipip6_tunnel_xmit(struct sk_buff *skb, struct net_device *dev)
  367. {
  368. struct ip_tunnel *tunnel = netdev_priv(dev);
  369. struct net_device_stats *stats = &tunnel->stat;
  370. struct iphdr *tiph = &tunnel->parms.iph;
  371. struct ipv6hdr *iph6 = ipv6_hdr(skb);
  372. u8 tos = tunnel->parms.iph.tos;
  373. struct rtable *rt; /* Route to the other host */
  374. struct net_device *tdev; /* Device to other host */
  375. struct iphdr *iph; /* Our new IP header */
  376. int max_headroom; /* The extra header space needed */
  377. __be32 dst = tiph->daddr;
  378. int mtu;
  379. struct in6_addr *addr6;
  380. int addr_type;
  381. if (tunnel->recursion++) {
  382. tunnel->stat.collisions++;
  383. goto tx_error;
  384. }
  385. if (skb->protocol != htons(ETH_P_IPV6))
  386. goto tx_error;
  387. if (!dst)
  388. dst = try_6to4(&iph6->daddr);
  389. if (!dst) {
  390. struct neighbour *neigh = NULL;
  391. if (skb->dst)
  392. neigh = skb->dst->neighbour;
  393. if (neigh == NULL) {
  394. if (net_ratelimit())
  395. printk(KERN_DEBUG "sit: nexthop == NULL\n");
  396. goto tx_error;
  397. }
  398. addr6 = (struct in6_addr*)&neigh->primary_key;
  399. addr_type = ipv6_addr_type(addr6);
  400. if (addr_type == IPV6_ADDR_ANY) {
  401. addr6 = &ipv6_hdr(skb)->daddr;
  402. addr_type = ipv6_addr_type(addr6);
  403. }
  404. if ((addr_type & IPV6_ADDR_COMPATv4) == 0)
  405. goto tx_error_icmp;
  406. dst = addr6->s6_addr32[3];
  407. }
  408. {
  409. struct flowi fl = { .nl_u = { .ip4_u =
  410. { .daddr = dst,
  411. .saddr = tiph->saddr,
  412. .tos = RT_TOS(tos) } },
  413. .oif = tunnel->parms.link,
  414. .proto = IPPROTO_IPV6 };
  415. if (ip_route_output_key(&rt, &fl)) {
  416. tunnel->stat.tx_carrier_errors++;
  417. goto tx_error_icmp;
  418. }
  419. }
  420. if (rt->rt_type != RTN_UNICAST) {
  421. ip_rt_put(rt);
  422. tunnel->stat.tx_carrier_errors++;
  423. goto tx_error_icmp;
  424. }
  425. tdev = rt->u.dst.dev;
  426. if (tdev == dev) {
  427. ip_rt_put(rt);
  428. tunnel->stat.collisions++;
  429. goto tx_error;
  430. }
  431. if (tiph->frag_off)
  432. mtu = dst_mtu(&rt->u.dst) - sizeof(struct iphdr);
  433. else
  434. mtu = skb->dst ? dst_mtu(skb->dst) : dev->mtu;
  435. if (mtu < 68) {
  436. tunnel->stat.collisions++;
  437. ip_rt_put(rt);
  438. goto tx_error;
  439. }
  440. if (mtu < IPV6_MIN_MTU)
  441. mtu = IPV6_MIN_MTU;
  442. if (tunnel->parms.iph.daddr && skb->dst)
  443. skb->dst->ops->update_pmtu(skb->dst, mtu);
  444. if (skb->len > mtu) {
  445. icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu, dev);
  446. ip_rt_put(rt);
  447. goto tx_error;
  448. }
  449. if (tunnel->err_count > 0) {
  450. if (jiffies - tunnel->err_time < IPTUNNEL_ERR_TIMEO) {
  451. tunnel->err_count--;
  452. dst_link_failure(skb);
  453. } else
  454. tunnel->err_count = 0;
  455. }
  456. /*
  457. * Okay, now see if we can stuff it in the buffer as-is.
  458. */
  459. max_headroom = LL_RESERVED_SPACE(tdev)+sizeof(struct iphdr);
  460. if (skb_headroom(skb) < max_headroom || skb_shared(skb) ||
  461. (skb_cloned(skb) && !skb_clone_writable(skb, 0))) {
  462. struct sk_buff *new_skb = skb_realloc_headroom(skb, max_headroom);
  463. if (!new_skb) {
  464. ip_rt_put(rt);
  465. stats->tx_dropped++;
  466. dev_kfree_skb(skb);
  467. tunnel->recursion--;
  468. return 0;
  469. }
  470. if (skb->sk)
  471. skb_set_owner_w(new_skb, skb->sk);
  472. dev_kfree_skb(skb);
  473. skb = new_skb;
  474. iph6 = ipv6_hdr(skb);
  475. }
  476. skb->transport_header = skb->network_header;
  477. skb_push(skb, sizeof(struct iphdr));
  478. skb_reset_network_header(skb);
  479. memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
  480. IPCB(skb)->flags = 0;
  481. dst_release(skb->dst);
  482. skb->dst = &rt->u.dst;
  483. /*
  484. * Push down and install the IPIP header.
  485. */
  486. iph = ip_hdr(skb);
  487. iph->version = 4;
  488. iph->ihl = sizeof(struct iphdr)>>2;
  489. if (mtu > IPV6_MIN_MTU)
  490. iph->frag_off = htons(IP_DF);
  491. else
  492. iph->frag_off = 0;
  493. iph->protocol = IPPROTO_IPV6;
  494. iph->tos = INET_ECN_encapsulate(tos, ipv6_get_dsfield(iph6));
  495. iph->daddr = rt->rt_dst;
  496. iph->saddr = rt->rt_src;
  497. if ((iph->ttl = tiph->ttl) == 0)
  498. iph->ttl = iph6->hop_limit;
  499. nf_reset(skb);
  500. IPTUNNEL_XMIT();
  501. tunnel->recursion--;
  502. return 0;
  503. tx_error_icmp:
  504. dst_link_failure(skb);
  505. tx_error:
  506. stats->tx_errors++;
  507. dev_kfree_skb(skb);
  508. tunnel->recursion--;
  509. return 0;
  510. }
  511. static int
  512. ipip6_tunnel_ioctl (struct net_device *dev, struct ifreq *ifr, int cmd)
  513. {
  514. int err = 0;
  515. struct ip_tunnel_parm p;
  516. struct ip_tunnel *t;
  517. switch (cmd) {
  518. case SIOCGETTUNNEL:
  519. t = NULL;
  520. if (dev == ipip6_fb_tunnel_dev) {
  521. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) {
  522. err = -EFAULT;
  523. break;
  524. }
  525. t = ipip6_tunnel_locate(&p, 0);
  526. }
  527. if (t == NULL)
  528. t = netdev_priv(dev);
  529. memcpy(&p, &t->parms, sizeof(p));
  530. if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p)))
  531. err = -EFAULT;
  532. break;
  533. case SIOCADDTUNNEL:
  534. case SIOCCHGTUNNEL:
  535. err = -EPERM;
  536. if (!capable(CAP_NET_ADMIN))
  537. goto done;
  538. err = -EFAULT;
  539. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
  540. goto done;
  541. err = -EINVAL;
  542. if (p.iph.version != 4 || p.iph.protocol != IPPROTO_IPV6 ||
  543. p.iph.ihl != 5 || (p.iph.frag_off&htons(~IP_DF)))
  544. goto done;
  545. if (p.iph.ttl)
  546. p.iph.frag_off |= htons(IP_DF);
  547. t = ipip6_tunnel_locate(&p, cmd == SIOCADDTUNNEL);
  548. if (dev != ipip6_fb_tunnel_dev && cmd == SIOCCHGTUNNEL) {
  549. if (t != NULL) {
  550. if (t->dev != dev) {
  551. err = -EEXIST;
  552. break;
  553. }
  554. } else {
  555. if (((dev->flags&IFF_POINTOPOINT) && !p.iph.daddr) ||
  556. (!(dev->flags&IFF_POINTOPOINT) && p.iph.daddr)) {
  557. err = -EINVAL;
  558. break;
  559. }
  560. t = netdev_priv(dev);
  561. ipip6_tunnel_unlink(t);
  562. t->parms.iph.saddr = p.iph.saddr;
  563. t->parms.iph.daddr = p.iph.daddr;
  564. memcpy(dev->dev_addr, &p.iph.saddr, 4);
  565. memcpy(dev->broadcast, &p.iph.daddr, 4);
  566. ipip6_tunnel_link(t);
  567. netdev_state_change(dev);
  568. }
  569. }
  570. if (t) {
  571. err = 0;
  572. if (cmd == SIOCCHGTUNNEL) {
  573. t->parms.iph.ttl = p.iph.ttl;
  574. t->parms.iph.tos = p.iph.tos;
  575. }
  576. if (copy_to_user(ifr->ifr_ifru.ifru_data, &t->parms, sizeof(p)))
  577. err = -EFAULT;
  578. } else
  579. err = (cmd == SIOCADDTUNNEL ? -ENOBUFS : -ENOENT);
  580. break;
  581. case SIOCDELTUNNEL:
  582. err = -EPERM;
  583. if (!capable(CAP_NET_ADMIN))
  584. goto done;
  585. if (dev == ipip6_fb_tunnel_dev) {
  586. err = -EFAULT;
  587. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
  588. goto done;
  589. err = -ENOENT;
  590. if ((t = ipip6_tunnel_locate(&p, 0)) == NULL)
  591. goto done;
  592. err = -EPERM;
  593. if (t == netdev_priv(ipip6_fb_tunnel_dev))
  594. goto done;
  595. dev = t->dev;
  596. }
  597. unregister_netdevice(dev);
  598. err = 0;
  599. break;
  600. default:
  601. err = -EINVAL;
  602. }
  603. done:
  604. return err;
  605. }
  606. static struct net_device_stats *ipip6_tunnel_get_stats(struct net_device *dev)
  607. {
  608. return &(((struct ip_tunnel*)netdev_priv(dev))->stat);
  609. }
  610. static int ipip6_tunnel_change_mtu(struct net_device *dev, int new_mtu)
  611. {
  612. if (new_mtu < IPV6_MIN_MTU || new_mtu > 0xFFF8 - sizeof(struct iphdr))
  613. return -EINVAL;
  614. dev->mtu = new_mtu;
  615. return 0;
  616. }
  617. static void ipip6_tunnel_setup(struct net_device *dev)
  618. {
  619. dev->uninit = ipip6_tunnel_uninit;
  620. dev->destructor = free_netdev;
  621. dev->hard_start_xmit = ipip6_tunnel_xmit;
  622. dev->get_stats = ipip6_tunnel_get_stats;
  623. dev->do_ioctl = ipip6_tunnel_ioctl;
  624. dev->change_mtu = ipip6_tunnel_change_mtu;
  625. dev->type = ARPHRD_SIT;
  626. dev->hard_header_len = LL_MAX_HEADER + sizeof(struct iphdr);
  627. dev->mtu = ETH_DATA_LEN - sizeof(struct iphdr);
  628. dev->flags = IFF_NOARP;
  629. dev->iflink = 0;
  630. dev->addr_len = 4;
  631. }
  632. static int ipip6_tunnel_init(struct net_device *dev)
  633. {
  634. struct net_device *tdev = NULL;
  635. struct ip_tunnel *tunnel;
  636. struct iphdr *iph;
  637. tunnel = netdev_priv(dev);
  638. iph = &tunnel->parms.iph;
  639. tunnel->dev = dev;
  640. strcpy(tunnel->parms.name, dev->name);
  641. memcpy(dev->dev_addr, &tunnel->parms.iph.saddr, 4);
  642. memcpy(dev->broadcast, &tunnel->parms.iph.daddr, 4);
  643. if (iph->daddr) {
  644. struct flowi fl = { .nl_u = { .ip4_u =
  645. { .daddr = iph->daddr,
  646. .saddr = iph->saddr,
  647. .tos = RT_TOS(iph->tos) } },
  648. .oif = tunnel->parms.link,
  649. .proto = IPPROTO_IPV6 };
  650. struct rtable *rt;
  651. if (!ip_route_output_key(&rt, &fl)) {
  652. tdev = rt->u.dst.dev;
  653. ip_rt_put(rt);
  654. }
  655. dev->flags |= IFF_POINTOPOINT;
  656. }
  657. if (!tdev && tunnel->parms.link)
  658. tdev = __dev_get_by_index(&init_net, tunnel->parms.link);
  659. if (tdev) {
  660. dev->hard_header_len = tdev->hard_header_len + sizeof(struct iphdr);
  661. dev->mtu = tdev->mtu - sizeof(struct iphdr);
  662. if (dev->mtu < IPV6_MIN_MTU)
  663. dev->mtu = IPV6_MIN_MTU;
  664. }
  665. dev->iflink = tunnel->parms.link;
  666. return 0;
  667. }
  668. static int __init ipip6_fb_tunnel_init(struct net_device *dev)
  669. {
  670. struct ip_tunnel *tunnel = netdev_priv(dev);
  671. struct iphdr *iph = &tunnel->parms.iph;
  672. tunnel->dev = dev;
  673. strcpy(tunnel->parms.name, dev->name);
  674. iph->version = 4;
  675. iph->protocol = IPPROTO_IPV6;
  676. iph->ihl = 5;
  677. iph->ttl = 64;
  678. dev_hold(dev);
  679. tunnels_wc[0] = tunnel;
  680. return 0;
  681. }
  682. static struct xfrm_tunnel sit_handler = {
  683. .handler = ipip6_rcv,
  684. .err_handler = ipip6_err,
  685. .priority = 1,
  686. };
  687. static void __exit sit_destroy_tunnels(void)
  688. {
  689. int prio;
  690. for (prio = 1; prio < 4; prio++) {
  691. int h;
  692. for (h = 0; h < HASH_SIZE; h++) {
  693. struct ip_tunnel *t;
  694. while ((t = tunnels[prio][h]) != NULL)
  695. unregister_netdevice(t->dev);
  696. }
  697. }
  698. }
  699. static void __exit sit_cleanup(void)
  700. {
  701. xfrm4_tunnel_deregister(&sit_handler, AF_INET6);
  702. rtnl_lock();
  703. sit_destroy_tunnels();
  704. unregister_netdevice(ipip6_fb_tunnel_dev);
  705. rtnl_unlock();
  706. }
  707. static int __init sit_init(void)
  708. {
  709. int err;
  710. printk(KERN_INFO "IPv6 over IPv4 tunneling driver\n");
  711. if (xfrm4_tunnel_register(&sit_handler, AF_INET6) < 0) {
  712. printk(KERN_INFO "sit init: Can't add protocol\n");
  713. return -EAGAIN;
  714. }
  715. ipip6_fb_tunnel_dev = alloc_netdev(sizeof(struct ip_tunnel), "sit0",
  716. ipip6_tunnel_setup);
  717. if (!ipip6_fb_tunnel_dev) {
  718. err = -ENOMEM;
  719. goto err1;
  720. }
  721. ipip6_fb_tunnel_dev->init = ipip6_fb_tunnel_init;
  722. if ((err = register_netdev(ipip6_fb_tunnel_dev)))
  723. goto err2;
  724. out:
  725. return err;
  726. err2:
  727. free_netdev(ipip6_fb_tunnel_dev);
  728. err1:
  729. xfrm4_tunnel_deregister(&sit_handler, AF_INET6);
  730. goto out;
  731. }
  732. module_init(sit_init);
  733. module_exit(sit_cleanup);
  734. MODULE_LICENSE("GPL");
  735. MODULE_ALIAS("sit0");