sit.c 24 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093
  1. /*
  2. * IPv6 over IPv4 tunnel device - Simple Internet Transition (SIT)
  3. * Linux INET6 implementation
  4. *
  5. * Authors:
  6. * Pedro Roque <roque@di.fc.ul.pt>
  7. * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License
  11. * as published by the Free Software Foundation; either version
  12. * 2 of the License, or (at your option) any later version.
  13. *
  14. * Changes:
  15. * Roger Venning <r.venning@telstra.com>: 6to4 support
  16. * Nate Thompson <nate@thebog.net>: 6to4 support
  17. * Fred Templin <fred.l.templin@boeing.com>: isatap support
  18. */
  19. #include <linux/module.h>
  20. #include <linux/capability.h>
  21. #include <linux/errno.h>
  22. #include <linux/types.h>
  23. #include <linux/socket.h>
  24. #include <linux/sockios.h>
  25. #include <linux/net.h>
  26. #include <linux/in6.h>
  27. #include <linux/netdevice.h>
  28. #include <linux/if_arp.h>
  29. #include <linux/icmp.h>
  30. #include <asm/uaccess.h>
  31. #include <linux/init.h>
  32. #include <linux/netfilter_ipv4.h>
  33. #include <linux/if_ether.h>
  34. #include <net/sock.h>
  35. #include <net/snmp.h>
  36. #include <net/ipv6.h>
  37. #include <net/protocol.h>
  38. #include <net/transp_v6.h>
  39. #include <net/ip6_fib.h>
  40. #include <net/ip6_route.h>
  41. #include <net/ndisc.h>
  42. #include <net/addrconf.h>
  43. #include <net/ip.h>
  44. #include <net/udp.h>
  45. #include <net/icmp.h>
  46. #include <net/ipip.h>
  47. #include <net/inet_ecn.h>
  48. #include <net/xfrm.h>
  49. #include <net/dsfield.h>
  50. #include <net/net_namespace.h>
  51. #include <net/netns/generic.h>
  52. /*
  53. This version of net/ipv6/sit.c is cloned of net/ipv4/ip_gre.c
  54. For comments look at net/ipv4/ip_gre.c --ANK
  55. */
  56. #define HASH_SIZE 16
  57. #define HASH(addr) (((__force u32)addr^((__force u32)addr>>4))&0xF)
  58. static void ipip6_fb_tunnel_init(struct net_device *dev);
  59. static void ipip6_tunnel_init(struct net_device *dev);
  60. static void ipip6_tunnel_setup(struct net_device *dev);
  61. static int sit_net_id;
  62. struct sit_net {
  63. struct ip_tunnel *tunnels_r_l[HASH_SIZE];
  64. struct ip_tunnel *tunnels_r[HASH_SIZE];
  65. struct ip_tunnel *tunnels_l[HASH_SIZE];
  66. struct ip_tunnel *tunnels_wc[1];
  67. struct ip_tunnel **tunnels[4];
  68. struct net_device *fb_tunnel_dev;
  69. };
  70. static DEFINE_RWLOCK(ipip6_lock);
  71. static struct ip_tunnel * ipip6_tunnel_lookup(struct net *net,
  72. __be32 remote, __be32 local)
  73. {
  74. unsigned h0 = HASH(remote);
  75. unsigned h1 = HASH(local);
  76. struct ip_tunnel *t;
  77. struct sit_net *sitn = net_generic(net, sit_net_id);
  78. for (t = sitn->tunnels_r_l[h0^h1]; t; t = t->next) {
  79. if (local == t->parms.iph.saddr &&
  80. remote == t->parms.iph.daddr && (t->dev->flags&IFF_UP))
  81. return t;
  82. }
  83. for (t = sitn->tunnels_r[h0]; t; t = t->next) {
  84. if (remote == t->parms.iph.daddr && (t->dev->flags&IFF_UP))
  85. return t;
  86. }
  87. for (t = sitn->tunnels_l[h1]; t; t = t->next) {
  88. if (local == t->parms.iph.saddr && (t->dev->flags&IFF_UP))
  89. return t;
  90. }
  91. if ((t = sitn->tunnels_wc[0]) != NULL && (t->dev->flags&IFF_UP))
  92. return t;
  93. return NULL;
  94. }
  95. static struct ip_tunnel **__ipip6_bucket(struct sit_net *sitn,
  96. struct ip_tunnel_parm *parms)
  97. {
  98. __be32 remote = parms->iph.daddr;
  99. __be32 local = parms->iph.saddr;
  100. unsigned h = 0;
  101. int prio = 0;
  102. if (remote) {
  103. prio |= 2;
  104. h ^= HASH(remote);
  105. }
  106. if (local) {
  107. prio |= 1;
  108. h ^= HASH(local);
  109. }
  110. return &sitn->tunnels[prio][h];
  111. }
  112. static inline struct ip_tunnel **ipip6_bucket(struct sit_net *sitn,
  113. struct ip_tunnel *t)
  114. {
  115. return __ipip6_bucket(sitn, &t->parms);
  116. }
  117. static void ipip6_tunnel_unlink(struct sit_net *sitn, struct ip_tunnel *t)
  118. {
  119. struct ip_tunnel **tp;
  120. for (tp = ipip6_bucket(sitn, t); *tp; tp = &(*tp)->next) {
  121. if (t == *tp) {
  122. write_lock_bh(&ipip6_lock);
  123. *tp = t->next;
  124. write_unlock_bh(&ipip6_lock);
  125. break;
  126. }
  127. }
  128. }
  129. static void ipip6_tunnel_link(struct sit_net *sitn, struct ip_tunnel *t)
  130. {
  131. struct ip_tunnel **tp = ipip6_bucket(sitn, t);
  132. t->next = *tp;
  133. write_lock_bh(&ipip6_lock);
  134. *tp = t;
  135. write_unlock_bh(&ipip6_lock);
  136. }
  137. static struct ip_tunnel * ipip6_tunnel_locate(struct net *net,
  138. struct ip_tunnel_parm *parms, int create)
  139. {
  140. __be32 remote = parms->iph.daddr;
  141. __be32 local = parms->iph.saddr;
  142. struct ip_tunnel *t, **tp, *nt;
  143. struct net_device *dev;
  144. char name[IFNAMSIZ];
  145. struct sit_net *sitn = net_generic(net, sit_net_id);
  146. for (tp = __ipip6_bucket(sitn, parms); (t = *tp) != NULL; tp = &t->next) {
  147. if (local == t->parms.iph.saddr && remote == t->parms.iph.daddr)
  148. return t;
  149. }
  150. if (!create)
  151. goto failed;
  152. if (parms->name[0])
  153. strlcpy(name, parms->name, IFNAMSIZ);
  154. else
  155. sprintf(name, "sit%%d");
  156. dev = alloc_netdev(sizeof(*t), name, ipip6_tunnel_setup);
  157. if (dev == NULL)
  158. return NULL;
  159. dev_net_set(dev, net);
  160. if (strchr(name, '%')) {
  161. if (dev_alloc_name(dev, name) < 0)
  162. goto failed_free;
  163. }
  164. nt = netdev_priv(dev);
  165. nt->parms = *parms;
  166. ipip6_tunnel_init(dev);
  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,
  189. struct ip_tunnel_prl __user *a)
  190. {
  191. struct ip_tunnel_prl kprl, *kp;
  192. struct ip_tunnel_prl_entry *prl;
  193. unsigned int cmax, c = 0, ca, len;
  194. int ret = 0;
  195. if (copy_from_user(&kprl, a, sizeof(kprl)))
  196. return -EFAULT;
  197. cmax = kprl.datalen / sizeof(kprl);
  198. if (cmax > 1 && kprl.addr != htonl(INADDR_ANY))
  199. cmax = 1;
  200. /* For simple GET or for root users,
  201. * we try harder to allocate.
  202. */
  203. kp = (cmax <= 1 || capable(CAP_NET_ADMIN)) ?
  204. kcalloc(cmax, sizeof(*kp), GFP_KERNEL) :
  205. NULL;
  206. read_lock(&ipip6_lock);
  207. ca = t->prl_count < cmax ? t->prl_count : cmax;
  208. if (!kp) {
  209. /* We don't try hard to allocate much memory for
  210. * non-root users.
  211. * For root users, retry allocating enough memory for
  212. * the answer.
  213. */
  214. kp = kcalloc(ca, sizeof(*kp), GFP_ATOMIC);
  215. if (!kp) {
  216. ret = -ENOMEM;
  217. goto out;
  218. }
  219. }
  220. c = 0;
  221. for (prl = t->prl; prl; prl = prl->next) {
  222. if (c > cmax)
  223. break;
  224. if (kprl.addr != htonl(INADDR_ANY) && prl->addr != kprl.addr)
  225. continue;
  226. kp[c].addr = prl->addr;
  227. kp[c].flags = prl->flags;
  228. c++;
  229. if (kprl.addr != htonl(INADDR_ANY))
  230. break;
  231. }
  232. out:
  233. read_unlock(&ipip6_lock);
  234. len = sizeof(*kp) * c;
  235. ret = 0;
  236. if ((len && copy_to_user(a + 1, kp, len)) || put_user(len, &a->datalen))
  237. ret = -EFAULT;
  238. kfree(kp);
  239. return ret;
  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. /* 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. }
  398. static inline void ipip6_ecn_decapsulate(struct iphdr *iph, struct sk_buff *skb)
  399. {
  400. if (INET_ECN_is_ce(iph->tos))
  401. IP6_ECN_set_ce(ipv6_hdr(skb));
  402. }
  403. static int ipip6_rcv(struct sk_buff *skb)
  404. {
  405. struct iphdr *iph;
  406. struct ip_tunnel *tunnel;
  407. if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
  408. goto out;
  409. iph = ip_hdr(skb);
  410. read_lock(&ipip6_lock);
  411. if ((tunnel = ipip6_tunnel_lookup(dev_net(skb->dev),
  412. iph->saddr, iph->daddr)) != NULL) {
  413. secpath_reset(skb);
  414. skb->mac_header = skb->network_header;
  415. skb_reset_network_header(skb);
  416. IPCB(skb)->flags = 0;
  417. skb->protocol = htons(ETH_P_IPV6);
  418. skb->pkt_type = PACKET_HOST;
  419. if ((tunnel->dev->priv_flags & IFF_ISATAP) &&
  420. !isatap_chksrc(skb, iph, tunnel)) {
  421. tunnel->dev->stats.rx_errors++;
  422. read_unlock(&ipip6_lock);
  423. kfree_skb(skb);
  424. return 0;
  425. }
  426. tunnel->dev->stats.rx_packets++;
  427. tunnel->dev->stats.rx_bytes += skb->len;
  428. skb->dev = tunnel->dev;
  429. dst_release(skb->dst);
  430. skb->dst = NULL;
  431. nf_reset(skb);
  432. ipip6_ecn_decapsulate(iph, skb);
  433. netif_rx(skb);
  434. read_unlock(&ipip6_lock);
  435. return 0;
  436. }
  437. icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
  438. read_unlock(&ipip6_lock);
  439. out:
  440. kfree_skb(skb);
  441. return 0;
  442. }
  443. /* Returns the embedded IPv4 address if the IPv6 address
  444. comes from 6to4 (RFC 3056) addr space */
  445. static inline __be32 try_6to4(struct in6_addr *v6dst)
  446. {
  447. __be32 dst = 0;
  448. if (v6dst->s6_addr16[0] == htons(0x2002)) {
  449. /* 6to4 v6 addr has 16 bits prefix, 32 v4addr, 16 SLA, ... */
  450. memcpy(&dst, &v6dst->s6_addr16[1], 4);
  451. }
  452. return dst;
  453. }
  454. /*
  455. * This function assumes it is being called from dev_queue_xmit()
  456. * and that skb is filled properly by that function.
  457. */
  458. static int ipip6_tunnel_xmit(struct sk_buff *skb, struct net_device *dev)
  459. {
  460. struct ip_tunnel *tunnel = netdev_priv(dev);
  461. struct net_device_stats *stats = &tunnel->dev->stats;
  462. struct iphdr *tiph = &tunnel->parms.iph;
  463. struct ipv6hdr *iph6 = ipv6_hdr(skb);
  464. u8 tos = tunnel->parms.iph.tos;
  465. struct rtable *rt; /* Route to the other host */
  466. struct net_device *tdev; /* Device to other host */
  467. struct iphdr *iph; /* Our new IP header */
  468. unsigned int max_headroom; /* The extra header space needed */
  469. __be32 dst = tiph->daddr;
  470. int mtu;
  471. struct in6_addr *addr6;
  472. int addr_type;
  473. if (tunnel->recursion++) {
  474. stats->collisions++;
  475. goto tx_error;
  476. }
  477. if (skb->protocol != htons(ETH_P_IPV6))
  478. goto tx_error;
  479. /* ISATAP (RFC4214) - must come before 6to4 */
  480. if (dev->priv_flags & IFF_ISATAP) {
  481. struct neighbour *neigh = NULL;
  482. if (skb->dst)
  483. neigh = skb->dst->neighbour;
  484. if (neigh == NULL) {
  485. if (net_ratelimit())
  486. printk(KERN_DEBUG "sit: nexthop == NULL\n");
  487. goto tx_error;
  488. }
  489. addr6 = (struct in6_addr*)&neigh->primary_key;
  490. addr_type = ipv6_addr_type(addr6);
  491. if ((addr_type & IPV6_ADDR_UNICAST) &&
  492. ipv6_addr_is_isatap(addr6))
  493. dst = addr6->s6_addr32[3];
  494. else
  495. goto tx_error;
  496. }
  497. if (!dst)
  498. dst = try_6to4(&iph6->daddr);
  499. if (!dst) {
  500. struct neighbour *neigh = NULL;
  501. if (skb->dst)
  502. neigh = skb->dst->neighbour;
  503. if (neigh == NULL) {
  504. if (net_ratelimit())
  505. printk(KERN_DEBUG "sit: nexthop == NULL\n");
  506. goto tx_error;
  507. }
  508. addr6 = (struct in6_addr*)&neigh->primary_key;
  509. addr_type = ipv6_addr_type(addr6);
  510. if (addr_type == IPV6_ADDR_ANY) {
  511. addr6 = &ipv6_hdr(skb)->daddr;
  512. addr_type = ipv6_addr_type(addr6);
  513. }
  514. if ((addr_type & IPV6_ADDR_COMPATv4) == 0)
  515. goto tx_error_icmp;
  516. dst = addr6->s6_addr32[3];
  517. }
  518. {
  519. struct flowi fl = { .nl_u = { .ip4_u =
  520. { .daddr = dst,
  521. .saddr = tiph->saddr,
  522. .tos = RT_TOS(tos) } },
  523. .oif = tunnel->parms.link,
  524. .proto = IPPROTO_IPV6 };
  525. if (ip_route_output_key(dev_net(dev), &rt, &fl)) {
  526. stats->tx_carrier_errors++;
  527. goto tx_error_icmp;
  528. }
  529. }
  530. if (rt->rt_type != RTN_UNICAST) {
  531. ip_rt_put(rt);
  532. stats->tx_carrier_errors++;
  533. goto tx_error_icmp;
  534. }
  535. tdev = rt->u.dst.dev;
  536. if (tdev == dev) {
  537. ip_rt_put(rt);
  538. stats->collisions++;
  539. goto tx_error;
  540. }
  541. if (tiph->frag_off)
  542. mtu = dst_mtu(&rt->u.dst) - sizeof(struct iphdr);
  543. else
  544. mtu = skb->dst ? dst_mtu(skb->dst) : dev->mtu;
  545. if (mtu < 68) {
  546. stats->collisions++;
  547. ip_rt_put(rt);
  548. goto tx_error;
  549. }
  550. if (mtu < IPV6_MIN_MTU)
  551. mtu = IPV6_MIN_MTU;
  552. if (tunnel->parms.iph.daddr && skb->dst)
  553. skb->dst->ops->update_pmtu(skb->dst, mtu);
  554. if (skb->len > mtu) {
  555. icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu, dev);
  556. ip_rt_put(rt);
  557. goto tx_error;
  558. }
  559. if (tunnel->err_count > 0) {
  560. if (jiffies - tunnel->err_time < IPTUNNEL_ERR_TIMEO) {
  561. tunnel->err_count--;
  562. dst_link_failure(skb);
  563. } else
  564. tunnel->err_count = 0;
  565. }
  566. /*
  567. * Okay, now see if we can stuff it in the buffer as-is.
  568. */
  569. max_headroom = LL_RESERVED_SPACE(tdev)+sizeof(struct iphdr);
  570. if (skb_headroom(skb) < max_headroom || skb_shared(skb) ||
  571. (skb_cloned(skb) && !skb_clone_writable(skb, 0))) {
  572. struct sk_buff *new_skb = skb_realloc_headroom(skb, max_headroom);
  573. if (!new_skb) {
  574. ip_rt_put(rt);
  575. stats->tx_dropped++;
  576. dev_kfree_skb(skb);
  577. tunnel->recursion--;
  578. return 0;
  579. }
  580. if (skb->sk)
  581. skb_set_owner_w(new_skb, skb->sk);
  582. dev_kfree_skb(skb);
  583. skb = new_skb;
  584. iph6 = ipv6_hdr(skb);
  585. }
  586. skb->transport_header = skb->network_header;
  587. skb_push(skb, sizeof(struct iphdr));
  588. skb_reset_network_header(skb);
  589. memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
  590. IPCB(skb)->flags = 0;
  591. dst_release(skb->dst);
  592. skb->dst = &rt->u.dst;
  593. /*
  594. * Push down and install the IPIP header.
  595. */
  596. iph = ip_hdr(skb);
  597. iph->version = 4;
  598. iph->ihl = sizeof(struct iphdr)>>2;
  599. if (mtu > IPV6_MIN_MTU)
  600. iph->frag_off = htons(IP_DF);
  601. else
  602. iph->frag_off = 0;
  603. iph->protocol = IPPROTO_IPV6;
  604. iph->tos = INET_ECN_encapsulate(tos, ipv6_get_dsfield(iph6));
  605. iph->daddr = rt->rt_dst;
  606. iph->saddr = rt->rt_src;
  607. if ((iph->ttl = tiph->ttl) == 0)
  608. iph->ttl = iph6->hop_limit;
  609. nf_reset(skb);
  610. IPTUNNEL_XMIT();
  611. tunnel->recursion--;
  612. return 0;
  613. tx_error_icmp:
  614. dst_link_failure(skb);
  615. tx_error:
  616. stats->tx_errors++;
  617. dev_kfree_skb(skb);
  618. tunnel->recursion--;
  619. return 0;
  620. }
  621. static void ipip6_tunnel_bind_dev(struct net_device *dev)
  622. {
  623. struct net_device *tdev = NULL;
  624. struct ip_tunnel *tunnel;
  625. struct iphdr *iph;
  626. tunnel = netdev_priv(dev);
  627. iph = &tunnel->parms.iph;
  628. if (iph->daddr) {
  629. struct flowi fl = { .nl_u = { .ip4_u =
  630. { .daddr = iph->daddr,
  631. .saddr = iph->saddr,
  632. .tos = RT_TOS(iph->tos) } },
  633. .oif = tunnel->parms.link,
  634. .proto = IPPROTO_IPV6 };
  635. struct rtable *rt;
  636. if (!ip_route_output_key(dev_net(dev), &rt, &fl)) {
  637. tdev = rt->u.dst.dev;
  638. ip_rt_put(rt);
  639. }
  640. dev->flags |= IFF_POINTOPOINT;
  641. }
  642. if (!tdev && tunnel->parms.link)
  643. tdev = __dev_get_by_index(dev_net(dev), tunnel->parms.link);
  644. if (tdev) {
  645. dev->hard_header_len = tdev->hard_header_len + sizeof(struct iphdr);
  646. dev->mtu = tdev->mtu - sizeof(struct iphdr);
  647. if (dev->mtu < IPV6_MIN_MTU)
  648. dev->mtu = IPV6_MIN_MTU;
  649. }
  650. dev->iflink = tunnel->parms.link;
  651. }
  652. static int
  653. ipip6_tunnel_ioctl (struct net_device *dev, struct ifreq *ifr, int cmd)
  654. {
  655. int err = 0;
  656. struct ip_tunnel_parm p;
  657. struct ip_tunnel_prl prl;
  658. struct ip_tunnel *t;
  659. struct net *net = dev_net(dev);
  660. struct sit_net *sitn = net_generic(net, sit_net_id);
  661. switch (cmd) {
  662. case SIOCGETTUNNEL:
  663. t = NULL;
  664. if (dev == sitn->fb_tunnel_dev) {
  665. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) {
  666. err = -EFAULT;
  667. break;
  668. }
  669. t = ipip6_tunnel_locate(net, &p, 0);
  670. }
  671. if (t == NULL)
  672. t = netdev_priv(dev);
  673. memcpy(&p, &t->parms, sizeof(p));
  674. if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p)))
  675. err = -EFAULT;
  676. break;
  677. case SIOCADDTUNNEL:
  678. case SIOCCHGTUNNEL:
  679. err = -EPERM;
  680. if (!capable(CAP_NET_ADMIN))
  681. goto done;
  682. err = -EFAULT;
  683. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
  684. goto done;
  685. err = -EINVAL;
  686. if (p.iph.version != 4 || p.iph.protocol != IPPROTO_IPV6 ||
  687. p.iph.ihl != 5 || (p.iph.frag_off&htons(~IP_DF)))
  688. goto done;
  689. if (p.iph.ttl)
  690. p.iph.frag_off |= htons(IP_DF);
  691. t = ipip6_tunnel_locate(net, &p, cmd == SIOCADDTUNNEL);
  692. if (dev != sitn->fb_tunnel_dev && cmd == SIOCCHGTUNNEL) {
  693. if (t != NULL) {
  694. if (t->dev != dev) {
  695. err = -EEXIST;
  696. break;
  697. }
  698. } else {
  699. if (((dev->flags&IFF_POINTOPOINT) && !p.iph.daddr) ||
  700. (!(dev->flags&IFF_POINTOPOINT) && p.iph.daddr)) {
  701. err = -EINVAL;
  702. break;
  703. }
  704. t = netdev_priv(dev);
  705. ipip6_tunnel_unlink(sitn, t);
  706. t->parms.iph.saddr = p.iph.saddr;
  707. t->parms.iph.daddr = p.iph.daddr;
  708. memcpy(dev->dev_addr, &p.iph.saddr, 4);
  709. memcpy(dev->broadcast, &p.iph.daddr, 4);
  710. ipip6_tunnel_link(sitn, t);
  711. netdev_state_change(dev);
  712. }
  713. }
  714. if (t) {
  715. err = 0;
  716. if (cmd == SIOCCHGTUNNEL) {
  717. t->parms.iph.ttl = p.iph.ttl;
  718. t->parms.iph.tos = p.iph.tos;
  719. if (t->parms.link != p.link) {
  720. t->parms.link = p.link;
  721. ipip6_tunnel_bind_dev(dev);
  722. netdev_state_change(dev);
  723. }
  724. }
  725. if (copy_to_user(ifr->ifr_ifru.ifru_data, &t->parms, sizeof(p)))
  726. err = -EFAULT;
  727. } else
  728. err = (cmd == SIOCADDTUNNEL ? -ENOBUFS : -ENOENT);
  729. break;
  730. case SIOCDELTUNNEL:
  731. err = -EPERM;
  732. if (!capable(CAP_NET_ADMIN))
  733. goto done;
  734. if (dev == sitn->fb_tunnel_dev) {
  735. err = -EFAULT;
  736. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
  737. goto done;
  738. err = -ENOENT;
  739. if ((t = ipip6_tunnel_locate(net, &p, 0)) == NULL)
  740. goto done;
  741. err = -EPERM;
  742. if (t == netdev_priv(sitn->fb_tunnel_dev))
  743. goto done;
  744. dev = t->dev;
  745. }
  746. unregister_netdevice(dev);
  747. err = 0;
  748. break;
  749. case SIOCGETPRL:
  750. err = -EINVAL;
  751. if (dev == sitn->fb_tunnel_dev)
  752. goto done;
  753. err = -ENOENT;
  754. if (!(t = netdev_priv(dev)))
  755. goto done;
  756. err = ipip6_tunnel_get_prl(t, ifr->ifr_ifru.ifru_data);
  757. break;
  758. case SIOCADDPRL:
  759. case SIOCDELPRL:
  760. case SIOCCHGPRL:
  761. err = -EPERM;
  762. if (!capable(CAP_NET_ADMIN))
  763. goto done;
  764. err = -EINVAL;
  765. if (dev == sitn->fb_tunnel_dev)
  766. goto done;
  767. err = -EFAULT;
  768. if (copy_from_user(&prl, ifr->ifr_ifru.ifru_data, sizeof(prl)))
  769. goto done;
  770. err = -ENOENT;
  771. if (!(t = netdev_priv(dev)))
  772. goto done;
  773. switch (cmd) {
  774. case SIOCDELPRL:
  775. err = ipip6_tunnel_del_prl(t, &prl);
  776. break;
  777. case SIOCADDPRL:
  778. case SIOCCHGPRL:
  779. err = ipip6_tunnel_add_prl(t, &prl, cmd == SIOCCHGPRL);
  780. break;
  781. }
  782. netdev_state_change(dev);
  783. break;
  784. default:
  785. err = -EINVAL;
  786. }
  787. done:
  788. return err;
  789. }
  790. static int ipip6_tunnel_change_mtu(struct net_device *dev, int new_mtu)
  791. {
  792. if (new_mtu < IPV6_MIN_MTU || new_mtu > 0xFFF8 - sizeof(struct iphdr))
  793. return -EINVAL;
  794. dev->mtu = new_mtu;
  795. return 0;
  796. }
  797. static const struct net_device_ops ipip6_netdev_ops = {
  798. .ndo_uninit = ipip6_tunnel_uninit,
  799. .ndo_start_xmit = ipip6_tunnel_xmit,
  800. .ndo_do_ioctl = ipip6_tunnel_ioctl,
  801. .ndo_change_mtu = ipip6_tunnel_change_mtu,
  802. };
  803. static void ipip6_tunnel_setup(struct net_device *dev)
  804. {
  805. dev->netdev_ops = &ipip6_netdev_ops;
  806. dev->destructor = free_netdev;
  807. dev->type = ARPHRD_SIT;
  808. dev->hard_header_len = LL_MAX_HEADER + sizeof(struct iphdr);
  809. dev->mtu = ETH_DATA_LEN - sizeof(struct iphdr);
  810. dev->flags = IFF_NOARP;
  811. dev->iflink = 0;
  812. dev->addr_len = 4;
  813. dev->features |= NETIF_F_NETNS_LOCAL;
  814. }
  815. static void ipip6_tunnel_init(struct net_device *dev)
  816. {
  817. struct ip_tunnel *tunnel = netdev_priv(dev);
  818. tunnel->dev = dev;
  819. strcpy(tunnel->parms.name, dev->name);
  820. memcpy(dev->dev_addr, &tunnel->parms.iph.saddr, 4);
  821. memcpy(dev->broadcast, &tunnel->parms.iph.daddr, 4);
  822. ipip6_tunnel_bind_dev(dev);
  823. }
  824. static void ipip6_fb_tunnel_init(struct net_device *dev)
  825. {
  826. struct ip_tunnel *tunnel = netdev_priv(dev);
  827. struct iphdr *iph = &tunnel->parms.iph;
  828. struct net *net = dev_net(dev);
  829. struct sit_net *sitn = net_generic(net, sit_net_id);
  830. tunnel->dev = dev;
  831. strcpy(tunnel->parms.name, dev->name);
  832. iph->version = 4;
  833. iph->protocol = IPPROTO_IPV6;
  834. iph->ihl = 5;
  835. iph->ttl = 64;
  836. dev_hold(dev);
  837. sitn->tunnels_wc[0] = tunnel;
  838. }
  839. static struct xfrm_tunnel sit_handler = {
  840. .handler = ipip6_rcv,
  841. .err_handler = ipip6_err,
  842. .priority = 1,
  843. };
  844. static void sit_destroy_tunnels(struct sit_net *sitn)
  845. {
  846. int prio;
  847. for (prio = 1; prio < 4; prio++) {
  848. int h;
  849. for (h = 0; h < HASH_SIZE; h++) {
  850. struct ip_tunnel *t;
  851. while ((t = sitn->tunnels[prio][h]) != NULL)
  852. unregister_netdevice(t->dev);
  853. }
  854. }
  855. }
  856. static int sit_init_net(struct net *net)
  857. {
  858. int err;
  859. struct sit_net *sitn;
  860. err = -ENOMEM;
  861. sitn = kzalloc(sizeof(struct sit_net), GFP_KERNEL);
  862. if (sitn == NULL)
  863. goto err_alloc;
  864. err = net_assign_generic(net, sit_net_id, sitn);
  865. if (err < 0)
  866. goto err_assign;
  867. sitn->tunnels[0] = sitn->tunnels_wc;
  868. sitn->tunnels[1] = sitn->tunnels_l;
  869. sitn->tunnels[2] = sitn->tunnels_r;
  870. sitn->tunnels[3] = sitn->tunnels_r_l;
  871. sitn->fb_tunnel_dev = alloc_netdev(sizeof(struct ip_tunnel), "sit0",
  872. ipip6_tunnel_setup);
  873. if (!sitn->fb_tunnel_dev) {
  874. err = -ENOMEM;
  875. goto err_alloc_dev;
  876. }
  877. dev_net_set(sitn->fb_tunnel_dev, net);
  878. ipip6_fb_tunnel_init(sitn->fb_tunnel_dev);
  879. if ((err = register_netdev(sitn->fb_tunnel_dev)))
  880. goto err_reg_dev;
  881. return 0;
  882. err_reg_dev:
  883. dev_put(sitn->fb_tunnel_dev);
  884. free_netdev(sitn->fb_tunnel_dev);
  885. err_alloc_dev:
  886. /* nothing */
  887. err_assign:
  888. kfree(sitn);
  889. err_alloc:
  890. return err;
  891. }
  892. static void sit_exit_net(struct net *net)
  893. {
  894. struct sit_net *sitn;
  895. sitn = net_generic(net, sit_net_id);
  896. rtnl_lock();
  897. sit_destroy_tunnels(sitn);
  898. unregister_netdevice(sitn->fb_tunnel_dev);
  899. rtnl_unlock();
  900. kfree(sitn);
  901. }
  902. static struct pernet_operations sit_net_ops = {
  903. .init = sit_init_net,
  904. .exit = sit_exit_net,
  905. };
  906. static void __exit sit_cleanup(void)
  907. {
  908. xfrm4_tunnel_deregister(&sit_handler, AF_INET6);
  909. unregister_pernet_gen_device(sit_net_id, &sit_net_ops);
  910. }
  911. static int __init sit_init(void)
  912. {
  913. int err;
  914. printk(KERN_INFO "IPv6 over IPv4 tunneling driver\n");
  915. if (xfrm4_tunnel_register(&sit_handler, AF_INET6) < 0) {
  916. printk(KERN_INFO "sit init: Can't add protocol\n");
  917. return -EAGAIN;
  918. }
  919. err = register_pernet_gen_device(&sit_net_id, &sit_net_ops);
  920. if (err < 0)
  921. xfrm4_tunnel_deregister(&sit_handler, AF_INET6);
  922. return err;
  923. }
  924. module_init(sit_init);
  925. module_exit(sit_cleanup);
  926. MODULE_LICENSE("GPL");
  927. MODULE_ALIAS("sit0");