sit.c 19 KB

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