sit.c 26 KB

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