sit.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183
  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. dev_net_set(dev, net);
  162. if (strchr(name, '%')) {
  163. if (dev_alloc_name(dev, name) < 0)
  164. goto failed_free;
  165. }
  166. nt = netdev_priv(dev);
  167. dev->init = ipip6_tunnel_init;
  168. nt->parms = *parms;
  169. if (parms->i_flags & SIT_ISATAP)
  170. dev->priv_flags |= IFF_ISATAP;
  171. if (register_netdevice(dev) < 0)
  172. goto failed_free;
  173. dev_hold(dev);
  174. ipip6_tunnel_link(sitn, nt);
  175. return nt;
  176. failed_free:
  177. free_netdev(dev);
  178. failed:
  179. return NULL;
  180. }
  181. static struct ip_tunnel_prl_entry *
  182. __ipip6_tunnel_locate_prl(struct ip_tunnel *t, __be32 addr)
  183. {
  184. struct ip_tunnel_prl_entry *p = (struct ip_tunnel_prl_entry *)NULL;
  185. for (p = t->prl; p; p = p->next)
  186. if (p->addr == addr)
  187. break;
  188. return p;
  189. }
  190. static int ipip6_tunnel_get_prl(struct ip_tunnel *t, struct ip_tunnel_prl *a)
  191. {
  192. struct ip_tunnel_prl *kp;
  193. struct ip_tunnel_prl_entry *prl;
  194. unsigned int cmax, c = 0, ca, len;
  195. int ret = 0;
  196. cmax = a->datalen / sizeof(*a);
  197. if (cmax > 1 && a->addr != htonl(INADDR_ANY))
  198. cmax = 1;
  199. /* For simple GET or for root users,
  200. * we try harder to allocate.
  201. */
  202. kp = (cmax <= 1 || capable(CAP_NET_ADMIN)) ?
  203. kcalloc(cmax, sizeof(*kp), GFP_KERNEL) :
  204. NULL;
  205. read_lock(&ipip6_lock);
  206. ca = t->prl_count < cmax ? t->prl_count : cmax;
  207. if (!kp) {
  208. /* We don't try hard to allocate much memory for
  209. * non-root users.
  210. * For root users, retry allocating enough memory for
  211. * the answer.
  212. */
  213. kp = kcalloc(ca, sizeof(*kp), GFP_ATOMIC);
  214. if (!kp) {
  215. ret = -ENOMEM;
  216. goto out;
  217. }
  218. }
  219. c = 0;
  220. for (prl = t->prl; prl; prl = prl->next) {
  221. if (c > cmax)
  222. break;
  223. if (a->addr != htonl(INADDR_ANY) && prl->addr != a->addr)
  224. continue;
  225. kp[c].addr = prl->addr;
  226. kp[c].flags = prl->flags;
  227. c++;
  228. if (a->addr != htonl(INADDR_ANY))
  229. break;
  230. }
  231. out:
  232. read_unlock(&ipip6_lock);
  233. len = sizeof(*kp) * c;
  234. ret = len ? copy_to_user(a->data, kp, len) : 0;
  235. kfree(kp);
  236. if (ret)
  237. return -EFAULT;
  238. a->datalen = len;
  239. return 0;
  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. #ifndef I_WISH_WORLD_WERE_PERFECT
  347. /* It is not :-( All the routers (except for Linux) return only
  348. 8 bytes of packet payload. It means, that precise relaying of
  349. ICMP in the real Internet is absolutely infeasible.
  350. */
  351. struct iphdr *iph = (struct iphdr*)skb->data;
  352. const int type = icmp_hdr(skb)->type;
  353. const int code = icmp_hdr(skb)->code;
  354. struct ip_tunnel *t;
  355. int err;
  356. switch (type) {
  357. default:
  358. case ICMP_PARAMETERPROB:
  359. return 0;
  360. case ICMP_DEST_UNREACH:
  361. switch (code) {
  362. case ICMP_SR_FAILED:
  363. case ICMP_PORT_UNREACH:
  364. /* Impossible event. */
  365. return 0;
  366. case ICMP_FRAG_NEEDED:
  367. /* Soft state for pmtu is maintained by IP core. */
  368. return 0;
  369. default:
  370. /* All others are translated to HOST_UNREACH.
  371. rfc2003 contains "deep thoughts" about NET_UNREACH,
  372. I believe they are just ether pollution. --ANK
  373. */
  374. break;
  375. }
  376. break;
  377. case ICMP_TIME_EXCEEDED:
  378. if (code != ICMP_EXC_TTL)
  379. return 0;
  380. break;
  381. }
  382. err = -ENOENT;
  383. read_lock(&ipip6_lock);
  384. t = ipip6_tunnel_lookup(dev_net(skb->dev), iph->daddr, iph->saddr);
  385. if (t == NULL || t->parms.iph.daddr == 0)
  386. goto out;
  387. err = 0;
  388. if (t->parms.iph.ttl == 0 && type == ICMP_TIME_EXCEEDED)
  389. goto out;
  390. if (jiffies - t->err_time < IPTUNNEL_ERR_TIMEO)
  391. t->err_count++;
  392. else
  393. t->err_count = 1;
  394. t->err_time = jiffies;
  395. out:
  396. read_unlock(&ipip6_lock);
  397. return err;
  398. #else
  399. struct iphdr *iph = (struct iphdr*)dp;
  400. int hlen = iph->ihl<<2;
  401. struct ipv6hdr *iph6;
  402. const int type = icmp_hdr(skb)->type;
  403. const int code = icmp_hdr(skb)->code;
  404. int rel_type = 0;
  405. int rel_code = 0;
  406. int rel_info = 0;
  407. struct sk_buff *skb2;
  408. struct rt6_info *rt6i;
  409. if (len < hlen + sizeof(struct ipv6hdr))
  410. return;
  411. iph6 = (struct ipv6hdr*)(dp + hlen);
  412. switch (type) {
  413. default:
  414. return;
  415. case ICMP_PARAMETERPROB:
  416. if (icmp_hdr(skb)->un.gateway < hlen)
  417. return;
  418. /* So... This guy found something strange INSIDE encapsulated
  419. packet. Well, he is fool, but what can we do ?
  420. */
  421. rel_type = ICMPV6_PARAMPROB;
  422. rel_info = icmp_hdr(skb)->un.gateway - hlen;
  423. break;
  424. case ICMP_DEST_UNREACH:
  425. switch (code) {
  426. case ICMP_SR_FAILED:
  427. case ICMP_PORT_UNREACH:
  428. /* Impossible event. */
  429. return;
  430. case ICMP_FRAG_NEEDED:
  431. /* Too complicated case ... */
  432. return;
  433. default:
  434. /* All others are translated to HOST_UNREACH.
  435. rfc2003 contains "deep thoughts" about NET_UNREACH,
  436. I believe, it is just ether pollution. --ANK
  437. */
  438. rel_type = ICMPV6_DEST_UNREACH;
  439. rel_code = ICMPV6_ADDR_UNREACH;
  440. break;
  441. }
  442. break;
  443. case ICMP_TIME_EXCEEDED:
  444. if (code != ICMP_EXC_TTL)
  445. return;
  446. rel_type = ICMPV6_TIME_EXCEED;
  447. rel_code = ICMPV6_EXC_HOPLIMIT;
  448. break;
  449. }
  450. /* Prepare fake skb to feed it to icmpv6_send */
  451. skb2 = skb_clone(skb, GFP_ATOMIC);
  452. if (skb2 == NULL)
  453. return 0;
  454. dst_release(skb2->dst);
  455. skb2->dst = NULL;
  456. skb_pull(skb2, skb->data - (u8*)iph6);
  457. skb_reset_network_header(skb2);
  458. /* Try to guess incoming interface */
  459. rt6i = rt6_lookup(dev_net(skb->dev), &iph6->saddr, NULL, NULL, 0);
  460. if (rt6i && rt6i->rt6i_dev) {
  461. skb2->dev = rt6i->rt6i_dev;
  462. rt6i = rt6_lookup(dev_net(skb->dev),
  463. &iph6->daddr, &iph6->saddr, NULL, 0);
  464. if (rt6i && rt6i->rt6i_dev && rt6i->rt6i_dev->type == ARPHRD_SIT) {
  465. struct ip_tunnel *t = netdev_priv(rt6i->rt6i_dev);
  466. if (rel_type == ICMPV6_TIME_EXCEED && t->parms.iph.ttl) {
  467. rel_type = ICMPV6_DEST_UNREACH;
  468. rel_code = ICMPV6_ADDR_UNREACH;
  469. }
  470. icmpv6_send(skb2, rel_type, rel_code, rel_info, skb2->dev);
  471. }
  472. }
  473. kfree_skb(skb2);
  474. return 0;
  475. #endif
  476. }
  477. static inline void ipip6_ecn_decapsulate(struct iphdr *iph, struct sk_buff *skb)
  478. {
  479. if (INET_ECN_is_ce(iph->tos))
  480. IP6_ECN_set_ce(ipv6_hdr(skb));
  481. }
  482. static int ipip6_rcv(struct sk_buff *skb)
  483. {
  484. struct iphdr *iph;
  485. struct ip_tunnel *tunnel;
  486. if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
  487. goto out;
  488. iph = ip_hdr(skb);
  489. read_lock(&ipip6_lock);
  490. if ((tunnel = ipip6_tunnel_lookup(dev_net(skb->dev),
  491. iph->saddr, iph->daddr)) != NULL) {
  492. secpath_reset(skb);
  493. skb->mac_header = skb->network_header;
  494. skb_reset_network_header(skb);
  495. IPCB(skb)->flags = 0;
  496. skb->protocol = htons(ETH_P_IPV6);
  497. skb->pkt_type = PACKET_HOST;
  498. if ((tunnel->dev->priv_flags & IFF_ISATAP) &&
  499. !isatap_chksrc(skb, iph, tunnel)) {
  500. tunnel->stat.rx_errors++;
  501. read_unlock(&ipip6_lock);
  502. kfree_skb(skb);
  503. return 0;
  504. }
  505. tunnel->stat.rx_packets++;
  506. tunnel->stat.rx_bytes += skb->len;
  507. skb->dev = tunnel->dev;
  508. dst_release(skb->dst);
  509. skb->dst = NULL;
  510. nf_reset(skb);
  511. ipip6_ecn_decapsulate(iph, skb);
  512. netif_rx(skb);
  513. read_unlock(&ipip6_lock);
  514. return 0;
  515. }
  516. icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
  517. kfree_skb(skb);
  518. read_unlock(&ipip6_lock);
  519. out:
  520. return 0;
  521. }
  522. /* Returns the embedded IPv4 address if the IPv6 address
  523. comes from 6to4 (RFC 3056) addr space */
  524. static inline __be32 try_6to4(struct in6_addr *v6dst)
  525. {
  526. __be32 dst = 0;
  527. if (v6dst->s6_addr16[0] == htons(0x2002)) {
  528. /* 6to4 v6 addr has 16 bits prefix, 32 v4addr, 16 SLA, ... */
  529. memcpy(&dst, &v6dst->s6_addr16[1], 4);
  530. }
  531. return dst;
  532. }
  533. /*
  534. * This function assumes it is being called from dev_queue_xmit()
  535. * and that skb is filled properly by that function.
  536. */
  537. static int ipip6_tunnel_xmit(struct sk_buff *skb, struct net_device *dev)
  538. {
  539. struct ip_tunnel *tunnel = netdev_priv(dev);
  540. struct net_device_stats *stats = &tunnel->stat;
  541. struct iphdr *tiph = &tunnel->parms.iph;
  542. struct ipv6hdr *iph6 = ipv6_hdr(skb);
  543. u8 tos = tunnel->parms.iph.tos;
  544. struct rtable *rt; /* Route to the other host */
  545. struct net_device *tdev; /* Device to other host */
  546. struct iphdr *iph; /* Our new IP header */
  547. unsigned int max_headroom; /* The extra header space needed */
  548. __be32 dst = tiph->daddr;
  549. int mtu;
  550. struct in6_addr *addr6;
  551. int addr_type;
  552. if (tunnel->recursion++) {
  553. tunnel->stat.collisions++;
  554. goto tx_error;
  555. }
  556. if (skb->protocol != htons(ETH_P_IPV6))
  557. goto tx_error;
  558. /* ISATAP (RFC4214) - must come before 6to4 */
  559. if (dev->priv_flags & IFF_ISATAP) {
  560. struct neighbour *neigh = NULL;
  561. if (skb->dst)
  562. neigh = skb->dst->neighbour;
  563. if (neigh == NULL) {
  564. if (net_ratelimit())
  565. printk(KERN_DEBUG "sit: nexthop == NULL\n");
  566. goto tx_error;
  567. }
  568. addr6 = (struct in6_addr*)&neigh->primary_key;
  569. addr_type = ipv6_addr_type(addr6);
  570. if ((addr_type & IPV6_ADDR_UNICAST) &&
  571. ipv6_addr_is_isatap(addr6))
  572. dst = addr6->s6_addr32[3];
  573. else
  574. goto tx_error;
  575. }
  576. if (!dst)
  577. dst = try_6to4(&iph6->daddr);
  578. if (!dst) {
  579. struct neighbour *neigh = NULL;
  580. if (skb->dst)
  581. neigh = skb->dst->neighbour;
  582. if (neigh == NULL) {
  583. if (net_ratelimit())
  584. printk(KERN_DEBUG "sit: nexthop == NULL\n");
  585. goto tx_error;
  586. }
  587. addr6 = (struct in6_addr*)&neigh->primary_key;
  588. addr_type = ipv6_addr_type(addr6);
  589. if (addr_type == IPV6_ADDR_ANY) {
  590. addr6 = &ipv6_hdr(skb)->daddr;
  591. addr_type = ipv6_addr_type(addr6);
  592. }
  593. if ((addr_type & IPV6_ADDR_COMPATv4) == 0)
  594. goto tx_error_icmp;
  595. dst = addr6->s6_addr32[3];
  596. }
  597. {
  598. struct flowi fl = { .nl_u = { .ip4_u =
  599. { .daddr = dst,
  600. .saddr = tiph->saddr,
  601. .tos = RT_TOS(tos) } },
  602. .oif = tunnel->parms.link,
  603. .proto = IPPROTO_IPV6 };
  604. if (ip_route_output_key(dev_net(dev), &rt, &fl)) {
  605. tunnel->stat.tx_carrier_errors++;
  606. goto tx_error_icmp;
  607. }
  608. }
  609. if (rt->rt_type != RTN_UNICAST) {
  610. ip_rt_put(rt);
  611. tunnel->stat.tx_carrier_errors++;
  612. goto tx_error_icmp;
  613. }
  614. tdev = rt->u.dst.dev;
  615. if (tdev == dev) {
  616. ip_rt_put(rt);
  617. tunnel->stat.collisions++;
  618. goto tx_error;
  619. }
  620. if (tiph->frag_off)
  621. mtu = dst_mtu(&rt->u.dst) - sizeof(struct iphdr);
  622. else
  623. mtu = skb->dst ? dst_mtu(skb->dst) : dev->mtu;
  624. if (mtu < 68) {
  625. tunnel->stat.collisions++;
  626. ip_rt_put(rt);
  627. goto tx_error;
  628. }
  629. if (mtu < IPV6_MIN_MTU)
  630. mtu = IPV6_MIN_MTU;
  631. if (tunnel->parms.iph.daddr && skb->dst)
  632. skb->dst->ops->update_pmtu(skb->dst, mtu);
  633. if (skb->len > mtu) {
  634. icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu, dev);
  635. ip_rt_put(rt);
  636. goto tx_error;
  637. }
  638. if (tunnel->err_count > 0) {
  639. if (jiffies - tunnel->err_time < IPTUNNEL_ERR_TIMEO) {
  640. tunnel->err_count--;
  641. dst_link_failure(skb);
  642. } else
  643. tunnel->err_count = 0;
  644. }
  645. /*
  646. * Okay, now see if we can stuff it in the buffer as-is.
  647. */
  648. max_headroom = LL_RESERVED_SPACE(tdev)+sizeof(struct iphdr);
  649. if (skb_headroom(skb) < max_headroom || skb_shared(skb) ||
  650. (skb_cloned(skb) && !skb_clone_writable(skb, 0))) {
  651. struct sk_buff *new_skb = skb_realloc_headroom(skb, max_headroom);
  652. if (!new_skb) {
  653. ip_rt_put(rt);
  654. stats->tx_dropped++;
  655. dev_kfree_skb(skb);
  656. tunnel->recursion--;
  657. return 0;
  658. }
  659. if (skb->sk)
  660. skb_set_owner_w(new_skb, skb->sk);
  661. dev_kfree_skb(skb);
  662. skb = new_skb;
  663. iph6 = ipv6_hdr(skb);
  664. }
  665. skb->transport_header = skb->network_header;
  666. skb_push(skb, sizeof(struct iphdr));
  667. skb_reset_network_header(skb);
  668. memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
  669. IPCB(skb)->flags = 0;
  670. dst_release(skb->dst);
  671. skb->dst = &rt->u.dst;
  672. /*
  673. * Push down and install the IPIP header.
  674. */
  675. iph = ip_hdr(skb);
  676. iph->version = 4;
  677. iph->ihl = sizeof(struct iphdr)>>2;
  678. if (mtu > IPV6_MIN_MTU)
  679. iph->frag_off = htons(IP_DF);
  680. else
  681. iph->frag_off = 0;
  682. iph->protocol = IPPROTO_IPV6;
  683. iph->tos = INET_ECN_encapsulate(tos, ipv6_get_dsfield(iph6));
  684. iph->daddr = rt->rt_dst;
  685. iph->saddr = rt->rt_src;
  686. if ((iph->ttl = tiph->ttl) == 0)
  687. iph->ttl = iph6->hop_limit;
  688. nf_reset(skb);
  689. IPTUNNEL_XMIT();
  690. tunnel->recursion--;
  691. return 0;
  692. tx_error_icmp:
  693. dst_link_failure(skb);
  694. tx_error:
  695. stats->tx_errors++;
  696. dev_kfree_skb(skb);
  697. tunnel->recursion--;
  698. return 0;
  699. }
  700. static void ipip6_tunnel_bind_dev(struct net_device *dev)
  701. {
  702. struct net_device *tdev = NULL;
  703. struct ip_tunnel *tunnel;
  704. struct iphdr *iph;
  705. tunnel = netdev_priv(dev);
  706. iph = &tunnel->parms.iph;
  707. if (iph->daddr) {
  708. struct flowi fl = { .nl_u = { .ip4_u =
  709. { .daddr = iph->daddr,
  710. .saddr = iph->saddr,
  711. .tos = RT_TOS(iph->tos) } },
  712. .oif = tunnel->parms.link,
  713. .proto = IPPROTO_IPV6 };
  714. struct rtable *rt;
  715. if (!ip_route_output_key(dev_net(dev), &rt, &fl)) {
  716. tdev = rt->u.dst.dev;
  717. ip_rt_put(rt);
  718. }
  719. dev->flags |= IFF_POINTOPOINT;
  720. }
  721. if (!tdev && tunnel->parms.link)
  722. tdev = __dev_get_by_index(dev_net(dev), tunnel->parms.link);
  723. if (tdev) {
  724. dev->hard_header_len = tdev->hard_header_len + sizeof(struct iphdr);
  725. dev->mtu = tdev->mtu - sizeof(struct iphdr);
  726. if (dev->mtu < IPV6_MIN_MTU)
  727. dev->mtu = IPV6_MIN_MTU;
  728. }
  729. dev->iflink = tunnel->parms.link;
  730. }
  731. static int
  732. ipip6_tunnel_ioctl (struct net_device *dev, struct ifreq *ifr, int cmd)
  733. {
  734. int err = 0;
  735. struct ip_tunnel_parm p;
  736. struct ip_tunnel_prl prl;
  737. struct ip_tunnel *t;
  738. struct net *net = dev_net(dev);
  739. struct sit_net *sitn = net_generic(net, sit_net_id);
  740. switch (cmd) {
  741. case SIOCGETTUNNEL:
  742. t = NULL;
  743. if (dev == sitn->fb_tunnel_dev) {
  744. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) {
  745. err = -EFAULT;
  746. break;
  747. }
  748. t = ipip6_tunnel_locate(net, &p, 0);
  749. }
  750. if (t == NULL)
  751. t = netdev_priv(dev);
  752. memcpy(&p, &t->parms, sizeof(p));
  753. if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p)))
  754. err = -EFAULT;
  755. break;
  756. case SIOCADDTUNNEL:
  757. case SIOCCHGTUNNEL:
  758. err = -EPERM;
  759. if (!capable(CAP_NET_ADMIN))
  760. goto done;
  761. err = -EFAULT;
  762. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
  763. goto done;
  764. err = -EINVAL;
  765. if (p.iph.version != 4 || p.iph.protocol != IPPROTO_IPV6 ||
  766. p.iph.ihl != 5 || (p.iph.frag_off&htons(~IP_DF)))
  767. goto done;
  768. if (p.iph.ttl)
  769. p.iph.frag_off |= htons(IP_DF);
  770. t = ipip6_tunnel_locate(net, &p, cmd == SIOCADDTUNNEL);
  771. if (dev != sitn->fb_tunnel_dev && cmd == SIOCCHGTUNNEL) {
  772. if (t != NULL) {
  773. if (t->dev != dev) {
  774. err = -EEXIST;
  775. break;
  776. }
  777. } else {
  778. if (((dev->flags&IFF_POINTOPOINT) && !p.iph.daddr) ||
  779. (!(dev->flags&IFF_POINTOPOINT) && p.iph.daddr)) {
  780. err = -EINVAL;
  781. break;
  782. }
  783. t = netdev_priv(dev);
  784. ipip6_tunnel_unlink(sitn, t);
  785. t->parms.iph.saddr = p.iph.saddr;
  786. t->parms.iph.daddr = p.iph.daddr;
  787. memcpy(dev->dev_addr, &p.iph.saddr, 4);
  788. memcpy(dev->broadcast, &p.iph.daddr, 4);
  789. ipip6_tunnel_link(sitn, t);
  790. netdev_state_change(dev);
  791. }
  792. }
  793. if (t) {
  794. err = 0;
  795. if (cmd == SIOCCHGTUNNEL) {
  796. t->parms.iph.ttl = p.iph.ttl;
  797. t->parms.iph.tos = p.iph.tos;
  798. if (t->parms.link != p.link) {
  799. t->parms.link = p.link;
  800. ipip6_tunnel_bind_dev(dev);
  801. netdev_state_change(dev);
  802. }
  803. }
  804. if (copy_to_user(ifr->ifr_ifru.ifru_data, &t->parms, sizeof(p)))
  805. err = -EFAULT;
  806. } else
  807. err = (cmd == SIOCADDTUNNEL ? -ENOBUFS : -ENOENT);
  808. break;
  809. case SIOCDELTUNNEL:
  810. err = -EPERM;
  811. if (!capable(CAP_NET_ADMIN))
  812. goto done;
  813. if (dev == sitn->fb_tunnel_dev) {
  814. err = -EFAULT;
  815. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
  816. goto done;
  817. err = -ENOENT;
  818. if ((t = ipip6_tunnel_locate(net, &p, 0)) == NULL)
  819. goto done;
  820. err = -EPERM;
  821. if (t == netdev_priv(sitn->fb_tunnel_dev))
  822. goto done;
  823. dev = t->dev;
  824. }
  825. unregister_netdevice(dev);
  826. err = 0;
  827. break;
  828. case SIOCGETPRL:
  829. case SIOCADDPRL:
  830. case SIOCDELPRL:
  831. case SIOCCHGPRL:
  832. err = -EPERM;
  833. if (cmd != SIOCGETPRL && !capable(CAP_NET_ADMIN))
  834. goto done;
  835. err = -EINVAL;
  836. if (dev == sitn->fb_tunnel_dev)
  837. goto done;
  838. err = -EFAULT;
  839. if (copy_from_user(&prl, ifr->ifr_ifru.ifru_data, sizeof(prl)))
  840. goto done;
  841. err = -ENOENT;
  842. if (!(t = netdev_priv(dev)))
  843. goto done;
  844. switch (cmd) {
  845. case SIOCGETPRL:
  846. err = ipip6_tunnel_get_prl(t, &prl);
  847. if (!err && copy_to_user(ifr->ifr_ifru.ifru_data,
  848. &prl, sizeof(prl)))
  849. err = -EFAULT;
  850. break;
  851. case SIOCDELPRL:
  852. err = ipip6_tunnel_del_prl(t, &prl);
  853. break;
  854. case SIOCADDPRL:
  855. case SIOCCHGPRL:
  856. err = ipip6_tunnel_add_prl(t, &prl, cmd == SIOCCHGPRL);
  857. break;
  858. }
  859. if (cmd != SIOCGETPRL)
  860. netdev_state_change(dev);
  861. break;
  862. default:
  863. err = -EINVAL;
  864. }
  865. done:
  866. return err;
  867. }
  868. static struct net_device_stats *ipip6_tunnel_get_stats(struct net_device *dev)
  869. {
  870. return &(((struct ip_tunnel*)netdev_priv(dev))->stat);
  871. }
  872. static int ipip6_tunnel_change_mtu(struct net_device *dev, int new_mtu)
  873. {
  874. if (new_mtu < IPV6_MIN_MTU || new_mtu > 0xFFF8 - sizeof(struct iphdr))
  875. return -EINVAL;
  876. dev->mtu = new_mtu;
  877. return 0;
  878. }
  879. static void ipip6_tunnel_setup(struct net_device *dev)
  880. {
  881. dev->uninit = ipip6_tunnel_uninit;
  882. dev->destructor = free_netdev;
  883. dev->hard_start_xmit = ipip6_tunnel_xmit;
  884. dev->get_stats = ipip6_tunnel_get_stats;
  885. dev->do_ioctl = ipip6_tunnel_ioctl;
  886. dev->change_mtu = ipip6_tunnel_change_mtu;
  887. dev->type = ARPHRD_SIT;
  888. dev->hard_header_len = LL_MAX_HEADER + sizeof(struct iphdr);
  889. dev->mtu = ETH_DATA_LEN - sizeof(struct iphdr);
  890. dev->flags = IFF_NOARP;
  891. dev->iflink = 0;
  892. dev->addr_len = 4;
  893. dev->features |= NETIF_F_NETNS_LOCAL;
  894. }
  895. static int ipip6_tunnel_init(struct net_device *dev)
  896. {
  897. struct ip_tunnel *tunnel;
  898. tunnel = netdev_priv(dev);
  899. tunnel->dev = dev;
  900. strcpy(tunnel->parms.name, dev->name);
  901. memcpy(dev->dev_addr, &tunnel->parms.iph.saddr, 4);
  902. memcpy(dev->broadcast, &tunnel->parms.iph.daddr, 4);
  903. ipip6_tunnel_bind_dev(dev);
  904. return 0;
  905. }
  906. static int ipip6_fb_tunnel_init(struct net_device *dev)
  907. {
  908. struct ip_tunnel *tunnel = netdev_priv(dev);
  909. struct iphdr *iph = &tunnel->parms.iph;
  910. struct net *net = dev_net(dev);
  911. struct sit_net *sitn = net_generic(net, sit_net_id);
  912. tunnel->dev = dev;
  913. strcpy(tunnel->parms.name, dev->name);
  914. iph->version = 4;
  915. iph->protocol = IPPROTO_IPV6;
  916. iph->ihl = 5;
  917. iph->ttl = 64;
  918. dev_hold(dev);
  919. sitn->tunnels_wc[0] = tunnel;
  920. return 0;
  921. }
  922. static struct xfrm_tunnel sit_handler = {
  923. .handler = ipip6_rcv,
  924. .err_handler = ipip6_err,
  925. .priority = 1,
  926. };
  927. static void sit_destroy_tunnels(struct sit_net *sitn)
  928. {
  929. int prio;
  930. for (prio = 1; prio < 4; prio++) {
  931. int h;
  932. for (h = 0; h < HASH_SIZE; h++) {
  933. struct ip_tunnel *t;
  934. while ((t = sitn->tunnels[prio][h]) != NULL)
  935. unregister_netdevice(t->dev);
  936. }
  937. }
  938. }
  939. static int sit_init_net(struct net *net)
  940. {
  941. int err;
  942. struct sit_net *sitn;
  943. err = -ENOMEM;
  944. sitn = kzalloc(sizeof(struct sit_net), GFP_KERNEL);
  945. if (sitn == NULL)
  946. goto err_alloc;
  947. err = net_assign_generic(net, sit_net_id, sitn);
  948. if (err < 0)
  949. goto err_assign;
  950. sitn->tunnels[0] = sitn->tunnels_wc;
  951. sitn->tunnels[1] = sitn->tunnels_l;
  952. sitn->tunnels[2] = sitn->tunnels_r;
  953. sitn->tunnels[3] = sitn->tunnels_r_l;
  954. sitn->fb_tunnel_dev = alloc_netdev(sizeof(struct ip_tunnel), "sit0",
  955. ipip6_tunnel_setup);
  956. if (!sitn->fb_tunnel_dev) {
  957. err = -ENOMEM;
  958. goto err_alloc_dev;
  959. }
  960. sitn->fb_tunnel_dev->init = ipip6_fb_tunnel_init;
  961. dev_net_set(sitn->fb_tunnel_dev, net);
  962. if ((err = register_netdev(sitn->fb_tunnel_dev)))
  963. goto err_reg_dev;
  964. return 0;
  965. err_reg_dev:
  966. free_netdev(sitn->fb_tunnel_dev);
  967. err_alloc_dev:
  968. /* nothing */
  969. err_assign:
  970. kfree(sitn);
  971. err_alloc:
  972. return err;
  973. }
  974. static void sit_exit_net(struct net *net)
  975. {
  976. struct sit_net *sitn;
  977. sitn = net_generic(net, sit_net_id);
  978. rtnl_lock();
  979. sit_destroy_tunnels(sitn);
  980. unregister_netdevice(sitn->fb_tunnel_dev);
  981. rtnl_unlock();
  982. kfree(sitn);
  983. }
  984. static struct pernet_operations sit_net_ops = {
  985. .init = sit_init_net,
  986. .exit = sit_exit_net,
  987. };
  988. static void __exit sit_cleanup(void)
  989. {
  990. xfrm4_tunnel_deregister(&sit_handler, AF_INET6);
  991. unregister_pernet_gen_device(sit_net_id, &sit_net_ops);
  992. }
  993. static int __init sit_init(void)
  994. {
  995. int err;
  996. printk(KERN_INFO "IPv6 over IPv4 tunneling driver\n");
  997. if (xfrm4_tunnel_register(&sit_handler, AF_INET6) < 0) {
  998. printk(KERN_INFO "sit init: Can't add protocol\n");
  999. return -EAGAIN;
  1000. }
  1001. err = register_pernet_gen_device(&sit_net_id, &sit_net_ops);
  1002. if (err < 0)
  1003. xfrm4_tunnel_deregister(&sit_handler, AF_INET6);
  1004. return err;
  1005. }
  1006. module_init(sit_init);
  1007. module_exit(sit_cleanup);
  1008. MODULE_LICENSE("GPL");
  1009. MODULE_ALIAS("sit0");