sit.c 26 KB

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