sit.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180
  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(dev_net(skb->dev), &iph6->saddr, NULL, NULL, 0);
  459. if (rt6i && rt6i->rt6i_dev) {
  460. skb2->dev = rt6i->rt6i_dev;
  461. rt6i = rt6_lookup(dev_net(skb->dev),
  462. &iph6->daddr, &iph6->saddr, NULL, 0);
  463. if (rt6i && rt6i->rt6i_dev && rt6i->rt6i_dev->type == ARPHRD_SIT) {
  464. struct ip_tunnel *t = netdev_priv(rt6i->rt6i_dev);
  465. if (rel_type == ICMPV6_TIME_EXCEED && t->parms.iph.ttl) {
  466. rel_type = ICMPV6_DEST_UNREACH;
  467. rel_code = ICMPV6_ADDR_UNREACH;
  468. }
  469. icmpv6_send(skb2, rel_type, rel_code, rel_info, skb2->dev);
  470. }
  471. }
  472. kfree_skb(skb2);
  473. return 0;
  474. #endif
  475. }
  476. static inline void ipip6_ecn_decapsulate(struct iphdr *iph, struct sk_buff *skb)
  477. {
  478. if (INET_ECN_is_ce(iph->tos))
  479. IP6_ECN_set_ce(ipv6_hdr(skb));
  480. }
  481. static int ipip6_rcv(struct sk_buff *skb)
  482. {
  483. struct iphdr *iph;
  484. struct ip_tunnel *tunnel;
  485. if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
  486. goto out;
  487. iph = ip_hdr(skb);
  488. read_lock(&ipip6_lock);
  489. if ((tunnel = ipip6_tunnel_lookup(dev_net(skb->dev),
  490. iph->saddr, iph->daddr)) != NULL) {
  491. secpath_reset(skb);
  492. skb->mac_header = skb->network_header;
  493. skb_reset_network_header(skb);
  494. IPCB(skb)->flags = 0;
  495. skb->protocol = htons(ETH_P_IPV6);
  496. skb->pkt_type = PACKET_HOST;
  497. if ((tunnel->dev->priv_flags & IFF_ISATAP) &&
  498. !isatap_chksrc(skb, iph, tunnel)) {
  499. tunnel->stat.rx_errors++;
  500. read_unlock(&ipip6_lock);
  501. kfree_skb(skb);
  502. return 0;
  503. }
  504. tunnel->stat.rx_packets++;
  505. tunnel->stat.rx_bytes += skb->len;
  506. skb->dev = tunnel->dev;
  507. dst_release(skb->dst);
  508. skb->dst = NULL;
  509. nf_reset(skb);
  510. ipip6_ecn_decapsulate(iph, skb);
  511. netif_rx(skb);
  512. read_unlock(&ipip6_lock);
  513. return 0;
  514. }
  515. icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
  516. kfree_skb(skb);
  517. read_unlock(&ipip6_lock);
  518. out:
  519. return 0;
  520. }
  521. /* Returns the embedded IPv4 address if the IPv6 address
  522. comes from 6to4 (RFC 3056) addr space */
  523. static inline __be32 try_6to4(struct in6_addr *v6dst)
  524. {
  525. __be32 dst = 0;
  526. if (v6dst->s6_addr16[0] == htons(0x2002)) {
  527. /* 6to4 v6 addr has 16 bits prefix, 32 v4addr, 16 SLA, ... */
  528. memcpy(&dst, &v6dst->s6_addr16[1], 4);
  529. }
  530. return dst;
  531. }
  532. /*
  533. * This function assumes it is being called from dev_queue_xmit()
  534. * and that skb is filled properly by that function.
  535. */
  536. static int ipip6_tunnel_xmit(struct sk_buff *skb, struct net_device *dev)
  537. {
  538. struct ip_tunnel *tunnel = netdev_priv(dev);
  539. struct net_device_stats *stats = &tunnel->stat;
  540. struct iphdr *tiph = &tunnel->parms.iph;
  541. struct ipv6hdr *iph6 = ipv6_hdr(skb);
  542. u8 tos = tunnel->parms.iph.tos;
  543. struct rtable *rt; /* Route to the other host */
  544. struct net_device *tdev; /* Device to other host */
  545. struct iphdr *iph; /* Our new IP header */
  546. unsigned int max_headroom; /* The extra header space needed */
  547. __be32 dst = tiph->daddr;
  548. int mtu;
  549. struct in6_addr *addr6;
  550. int addr_type;
  551. if (tunnel->recursion++) {
  552. tunnel->stat.collisions++;
  553. goto tx_error;
  554. }
  555. if (skb->protocol != htons(ETH_P_IPV6))
  556. goto tx_error;
  557. /* ISATAP (RFC4214) - must come before 6to4 */
  558. if (dev->priv_flags & IFF_ISATAP) {
  559. struct neighbour *neigh = NULL;
  560. if (skb->dst)
  561. neigh = skb->dst->neighbour;
  562. if (neigh == NULL) {
  563. if (net_ratelimit())
  564. printk(KERN_DEBUG "sit: nexthop == NULL\n");
  565. goto tx_error;
  566. }
  567. addr6 = (struct in6_addr*)&neigh->primary_key;
  568. addr_type = ipv6_addr_type(addr6);
  569. if ((addr_type & IPV6_ADDR_UNICAST) &&
  570. ipv6_addr_is_isatap(addr6))
  571. dst = addr6->s6_addr32[3];
  572. else
  573. goto tx_error;
  574. }
  575. if (!dst)
  576. dst = try_6to4(&iph6->daddr);
  577. if (!dst) {
  578. struct neighbour *neigh = NULL;
  579. if (skb->dst)
  580. neigh = skb->dst->neighbour;
  581. if (neigh == NULL) {
  582. if (net_ratelimit())
  583. printk(KERN_DEBUG "sit: nexthop == NULL\n");
  584. goto tx_error;
  585. }
  586. addr6 = (struct in6_addr*)&neigh->primary_key;
  587. addr_type = ipv6_addr_type(addr6);
  588. if (addr_type == IPV6_ADDR_ANY) {
  589. addr6 = &ipv6_hdr(skb)->daddr;
  590. addr_type = ipv6_addr_type(addr6);
  591. }
  592. if ((addr_type & IPV6_ADDR_COMPATv4) == 0)
  593. goto tx_error_icmp;
  594. dst = addr6->s6_addr32[3];
  595. }
  596. {
  597. struct flowi fl = { .nl_u = { .ip4_u =
  598. { .daddr = dst,
  599. .saddr = tiph->saddr,
  600. .tos = RT_TOS(tos) } },
  601. .oif = tunnel->parms.link,
  602. .proto = IPPROTO_IPV6 };
  603. if (ip_route_output_key(dev_net(dev), &rt, &fl)) {
  604. tunnel->stat.tx_carrier_errors++;
  605. goto tx_error_icmp;
  606. }
  607. }
  608. if (rt->rt_type != RTN_UNICAST) {
  609. ip_rt_put(rt);
  610. tunnel->stat.tx_carrier_errors++;
  611. goto tx_error_icmp;
  612. }
  613. tdev = rt->u.dst.dev;
  614. if (tdev == dev) {
  615. ip_rt_put(rt);
  616. tunnel->stat.collisions++;
  617. goto tx_error;
  618. }
  619. if (tiph->frag_off)
  620. mtu = dst_mtu(&rt->u.dst) - sizeof(struct iphdr);
  621. else
  622. mtu = skb->dst ? dst_mtu(skb->dst) : dev->mtu;
  623. if (mtu < 68) {
  624. tunnel->stat.collisions++;
  625. ip_rt_put(rt);
  626. goto tx_error;
  627. }
  628. if (mtu < IPV6_MIN_MTU)
  629. mtu = IPV6_MIN_MTU;
  630. if (tunnel->parms.iph.daddr && skb->dst)
  631. skb->dst->ops->update_pmtu(skb->dst, mtu);
  632. if (skb->len > mtu) {
  633. icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu, dev);
  634. ip_rt_put(rt);
  635. goto tx_error;
  636. }
  637. if (tunnel->err_count > 0) {
  638. if (jiffies - tunnel->err_time < IPTUNNEL_ERR_TIMEO) {
  639. tunnel->err_count--;
  640. dst_link_failure(skb);
  641. } else
  642. tunnel->err_count = 0;
  643. }
  644. /*
  645. * Okay, now see if we can stuff it in the buffer as-is.
  646. */
  647. max_headroom = LL_RESERVED_SPACE(tdev)+sizeof(struct iphdr);
  648. if (skb_headroom(skb) < max_headroom || skb_shared(skb) ||
  649. (skb_cloned(skb) && !skb_clone_writable(skb, 0))) {
  650. struct sk_buff *new_skb = skb_realloc_headroom(skb, max_headroom);
  651. if (!new_skb) {
  652. ip_rt_put(rt);
  653. stats->tx_dropped++;
  654. dev_kfree_skb(skb);
  655. tunnel->recursion--;
  656. return 0;
  657. }
  658. if (skb->sk)
  659. skb_set_owner_w(new_skb, skb->sk);
  660. dev_kfree_skb(skb);
  661. skb = new_skb;
  662. iph6 = ipv6_hdr(skb);
  663. }
  664. skb->transport_header = skb->network_header;
  665. skb_push(skb, sizeof(struct iphdr));
  666. skb_reset_network_header(skb);
  667. memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
  668. IPCB(skb)->flags = 0;
  669. dst_release(skb->dst);
  670. skb->dst = &rt->u.dst;
  671. /*
  672. * Push down and install the IPIP header.
  673. */
  674. iph = ip_hdr(skb);
  675. iph->version = 4;
  676. iph->ihl = sizeof(struct iphdr)>>2;
  677. if (mtu > IPV6_MIN_MTU)
  678. iph->frag_off = htons(IP_DF);
  679. else
  680. iph->frag_off = 0;
  681. iph->protocol = IPPROTO_IPV6;
  682. iph->tos = INET_ECN_encapsulate(tos, ipv6_get_dsfield(iph6));
  683. iph->daddr = rt->rt_dst;
  684. iph->saddr = rt->rt_src;
  685. if ((iph->ttl = tiph->ttl) == 0)
  686. iph->ttl = iph6->hop_limit;
  687. nf_reset(skb);
  688. IPTUNNEL_XMIT();
  689. tunnel->recursion--;
  690. return 0;
  691. tx_error_icmp:
  692. dst_link_failure(skb);
  693. tx_error:
  694. stats->tx_errors++;
  695. dev_kfree_skb(skb);
  696. tunnel->recursion--;
  697. return 0;
  698. }
  699. static void ipip6_tunnel_bind_dev(struct net_device *dev)
  700. {
  701. struct net_device *tdev = NULL;
  702. struct ip_tunnel *tunnel;
  703. struct iphdr *iph;
  704. tunnel = netdev_priv(dev);
  705. iph = &tunnel->parms.iph;
  706. if (iph->daddr) {
  707. struct flowi fl = { .nl_u = { .ip4_u =
  708. { .daddr = iph->daddr,
  709. .saddr = iph->saddr,
  710. .tos = RT_TOS(iph->tos) } },
  711. .oif = tunnel->parms.link,
  712. .proto = IPPROTO_IPV6 };
  713. struct rtable *rt;
  714. if (!ip_route_output_key(dev_net(dev), &rt, &fl)) {
  715. tdev = rt->u.dst.dev;
  716. ip_rt_put(rt);
  717. }
  718. dev->flags |= IFF_POINTOPOINT;
  719. }
  720. if (!tdev && tunnel->parms.link)
  721. tdev = __dev_get_by_index(dev_net(dev), tunnel->parms.link);
  722. if (tdev) {
  723. dev->hard_header_len = tdev->hard_header_len + sizeof(struct iphdr);
  724. dev->mtu = tdev->mtu - sizeof(struct iphdr);
  725. if (dev->mtu < IPV6_MIN_MTU)
  726. dev->mtu = IPV6_MIN_MTU;
  727. }
  728. dev->iflink = tunnel->parms.link;
  729. }
  730. static int
  731. ipip6_tunnel_ioctl (struct net_device *dev, struct ifreq *ifr, int cmd)
  732. {
  733. int err = 0;
  734. struct ip_tunnel_parm p;
  735. struct ip_tunnel_prl prl;
  736. struct ip_tunnel *t;
  737. struct net *net = dev_net(dev);
  738. struct sit_net *sitn = net_generic(net, sit_net_id);
  739. switch (cmd) {
  740. case SIOCGETTUNNEL:
  741. t = NULL;
  742. if (dev == sitn->fb_tunnel_dev) {
  743. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) {
  744. err = -EFAULT;
  745. break;
  746. }
  747. t = ipip6_tunnel_locate(net, &p, 0);
  748. }
  749. if (t == NULL)
  750. t = netdev_priv(dev);
  751. memcpy(&p, &t->parms, sizeof(p));
  752. if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p)))
  753. err = -EFAULT;
  754. break;
  755. case SIOCADDTUNNEL:
  756. case SIOCCHGTUNNEL:
  757. err = -EPERM;
  758. if (!capable(CAP_NET_ADMIN))
  759. goto done;
  760. err = -EFAULT;
  761. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
  762. goto done;
  763. err = -EINVAL;
  764. if (p.iph.version != 4 || p.iph.protocol != IPPROTO_IPV6 ||
  765. p.iph.ihl != 5 || (p.iph.frag_off&htons(~IP_DF)))
  766. goto done;
  767. if (p.iph.ttl)
  768. p.iph.frag_off |= htons(IP_DF);
  769. t = ipip6_tunnel_locate(net, &p, cmd == SIOCADDTUNNEL);
  770. if (dev != sitn->fb_tunnel_dev && cmd == SIOCCHGTUNNEL) {
  771. if (t != NULL) {
  772. if (t->dev != dev) {
  773. err = -EEXIST;
  774. break;
  775. }
  776. } else {
  777. if (((dev->flags&IFF_POINTOPOINT) && !p.iph.daddr) ||
  778. (!(dev->flags&IFF_POINTOPOINT) && p.iph.daddr)) {
  779. err = -EINVAL;
  780. break;
  781. }
  782. t = netdev_priv(dev);
  783. ipip6_tunnel_unlink(sitn, t);
  784. t->parms.iph.saddr = p.iph.saddr;
  785. t->parms.iph.daddr = p.iph.daddr;
  786. memcpy(dev->dev_addr, &p.iph.saddr, 4);
  787. memcpy(dev->broadcast, &p.iph.daddr, 4);
  788. ipip6_tunnel_link(sitn, t);
  789. netdev_state_change(dev);
  790. }
  791. }
  792. if (t) {
  793. err = 0;
  794. if (cmd == SIOCCHGTUNNEL) {
  795. t->parms.iph.ttl = p.iph.ttl;
  796. t->parms.iph.tos = p.iph.tos;
  797. if (t->parms.link != p.link) {
  798. t->parms.link = p.link;
  799. ipip6_tunnel_bind_dev(dev);
  800. netdev_state_change(dev);
  801. }
  802. }
  803. if (copy_to_user(ifr->ifr_ifru.ifru_data, &t->parms, sizeof(p)))
  804. err = -EFAULT;
  805. } else
  806. err = (cmd == SIOCADDTUNNEL ? -ENOBUFS : -ENOENT);
  807. break;
  808. case SIOCDELTUNNEL:
  809. err = -EPERM;
  810. if (!capable(CAP_NET_ADMIN))
  811. goto done;
  812. if (dev == sitn->fb_tunnel_dev) {
  813. err = -EFAULT;
  814. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
  815. goto done;
  816. err = -ENOENT;
  817. if ((t = ipip6_tunnel_locate(net, &p, 0)) == NULL)
  818. goto done;
  819. err = -EPERM;
  820. if (t == netdev_priv(sitn->fb_tunnel_dev))
  821. goto done;
  822. dev = t->dev;
  823. }
  824. unregister_netdevice(dev);
  825. err = 0;
  826. break;
  827. case SIOCGETPRL:
  828. case SIOCADDPRL:
  829. case SIOCDELPRL:
  830. case SIOCCHGPRL:
  831. err = -EPERM;
  832. if (cmd != SIOCGETPRL && !capable(CAP_NET_ADMIN))
  833. goto done;
  834. err = -EINVAL;
  835. if (dev == sitn->fb_tunnel_dev)
  836. goto done;
  837. err = -EFAULT;
  838. if (copy_from_user(&prl, ifr->ifr_ifru.ifru_data, sizeof(prl)))
  839. goto done;
  840. err = -ENOENT;
  841. if (!(t = netdev_priv(dev)))
  842. goto done;
  843. switch (cmd) {
  844. case SIOCGETPRL:
  845. err = ipip6_tunnel_get_prl(t, &prl);
  846. if (!err && copy_to_user(ifr->ifr_ifru.ifru_data,
  847. &prl, sizeof(prl)))
  848. err = -EFAULT;
  849. break;
  850. case SIOCDELPRL:
  851. err = ipip6_tunnel_del_prl(t, &prl);
  852. break;
  853. case SIOCADDPRL:
  854. case SIOCCHGPRL:
  855. err = ipip6_tunnel_add_prl(t, &prl, cmd == SIOCCHGPRL);
  856. break;
  857. }
  858. if (cmd != SIOCGETPRL)
  859. netdev_state_change(dev);
  860. break;
  861. default:
  862. err = -EINVAL;
  863. }
  864. done:
  865. return err;
  866. }
  867. static struct net_device_stats *ipip6_tunnel_get_stats(struct net_device *dev)
  868. {
  869. return &(((struct ip_tunnel*)netdev_priv(dev))->stat);
  870. }
  871. static int ipip6_tunnel_change_mtu(struct net_device *dev, int new_mtu)
  872. {
  873. if (new_mtu < IPV6_MIN_MTU || new_mtu > 0xFFF8 - sizeof(struct iphdr))
  874. return -EINVAL;
  875. dev->mtu = new_mtu;
  876. return 0;
  877. }
  878. static void ipip6_tunnel_setup(struct net_device *dev)
  879. {
  880. dev->uninit = ipip6_tunnel_uninit;
  881. dev->destructor = free_netdev;
  882. dev->hard_start_xmit = ipip6_tunnel_xmit;
  883. dev->get_stats = ipip6_tunnel_get_stats;
  884. dev->do_ioctl = ipip6_tunnel_ioctl;
  885. dev->change_mtu = ipip6_tunnel_change_mtu;
  886. dev->type = ARPHRD_SIT;
  887. dev->hard_header_len = LL_MAX_HEADER + sizeof(struct iphdr);
  888. dev->mtu = ETH_DATA_LEN - sizeof(struct iphdr);
  889. dev->flags = IFF_NOARP;
  890. dev->iflink = 0;
  891. dev->addr_len = 4;
  892. }
  893. static int ipip6_tunnel_init(struct net_device *dev)
  894. {
  895. struct ip_tunnel *tunnel;
  896. tunnel = netdev_priv(dev);
  897. tunnel->dev = dev;
  898. strcpy(tunnel->parms.name, dev->name);
  899. memcpy(dev->dev_addr, &tunnel->parms.iph.saddr, 4);
  900. memcpy(dev->broadcast, &tunnel->parms.iph.daddr, 4);
  901. ipip6_tunnel_bind_dev(dev);
  902. return 0;
  903. }
  904. static int ipip6_fb_tunnel_init(struct net_device *dev)
  905. {
  906. struct ip_tunnel *tunnel = netdev_priv(dev);
  907. struct iphdr *iph = &tunnel->parms.iph;
  908. struct net *net = dev_net(dev);
  909. struct sit_net *sitn = net_generic(net, sit_net_id);
  910. tunnel->dev = dev;
  911. strcpy(tunnel->parms.name, dev->name);
  912. iph->version = 4;
  913. iph->protocol = IPPROTO_IPV6;
  914. iph->ihl = 5;
  915. iph->ttl = 64;
  916. dev_hold(dev);
  917. sitn->tunnels_wc[0] = tunnel;
  918. return 0;
  919. }
  920. static struct xfrm_tunnel sit_handler = {
  921. .handler = ipip6_rcv,
  922. .err_handler = ipip6_err,
  923. .priority = 1,
  924. };
  925. static void sit_destroy_tunnels(struct sit_net *sitn)
  926. {
  927. int prio;
  928. for (prio = 1; prio < 4; prio++) {
  929. int h;
  930. for (h = 0; h < HASH_SIZE; h++) {
  931. struct ip_tunnel *t;
  932. while ((t = sitn->tunnels[prio][h]) != NULL)
  933. unregister_netdevice(t->dev);
  934. }
  935. }
  936. }
  937. static int sit_init_net(struct net *net)
  938. {
  939. int err;
  940. struct sit_net *sitn;
  941. err = -ENOMEM;
  942. sitn = kzalloc(sizeof(struct sit_net), GFP_KERNEL);
  943. if (sitn == NULL)
  944. goto err_alloc;
  945. err = net_assign_generic(net, sit_net_id, sitn);
  946. if (err < 0)
  947. goto err_assign;
  948. sitn->tunnels[0] = sitn->tunnels_wc;
  949. sitn->tunnels[1] = sitn->tunnels_l;
  950. sitn->tunnels[2] = sitn->tunnels_r;
  951. sitn->tunnels[3] = sitn->tunnels_r_l;
  952. sitn->fb_tunnel_dev = alloc_netdev(sizeof(struct ip_tunnel), "sit0",
  953. ipip6_tunnel_setup);
  954. if (!sitn->fb_tunnel_dev) {
  955. err = -ENOMEM;
  956. goto err_alloc_dev;
  957. }
  958. sitn->fb_tunnel_dev->init = ipip6_fb_tunnel_init;
  959. dev_net_set(sitn->fb_tunnel_dev, net);
  960. if ((err = register_netdev(sitn->fb_tunnel_dev)))
  961. goto err_reg_dev;
  962. return 0;
  963. err_reg_dev:
  964. free_netdev(sitn->fb_tunnel_dev);
  965. err_alloc_dev:
  966. /* nothing */
  967. err_assign:
  968. kfree(sitn);
  969. err_alloc:
  970. return err;
  971. }
  972. static void sit_exit_net(struct net *net)
  973. {
  974. struct sit_net *sitn;
  975. sitn = net_generic(net, sit_net_id);
  976. rtnl_lock();
  977. sit_destroy_tunnels(sitn);
  978. unregister_netdevice(sitn->fb_tunnel_dev);
  979. rtnl_unlock();
  980. kfree(sitn);
  981. }
  982. static struct pernet_operations sit_net_ops = {
  983. .init = sit_init_net,
  984. .exit = sit_exit_net,
  985. };
  986. static void __exit sit_cleanup(void)
  987. {
  988. xfrm4_tunnel_deregister(&sit_handler, AF_INET6);
  989. unregister_pernet_gen_device(sit_net_id, &sit_net_ops);
  990. }
  991. static int __init sit_init(void)
  992. {
  993. int err;
  994. printk(KERN_INFO "IPv6 over IPv4 tunneling driver\n");
  995. if (xfrm4_tunnel_register(&sit_handler, AF_INET6) < 0) {
  996. printk(KERN_INFO "sit init: Can't add protocol\n");
  997. return -EAGAIN;
  998. }
  999. err = register_pernet_gen_device(&sit_net_id, &sit_net_ops);
  1000. if (err < 0)
  1001. xfrm4_tunnel_deregister(&sit_handler, AF_INET6);
  1002. return err;
  1003. }
  1004. module_init(sit_init);
  1005. module_exit(sit_cleanup);
  1006. MODULE_LICENSE("GPL");
  1007. MODULE_ALIAS("sit0");