sit.c 26 KB

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