sit.c 24 KB

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