sit.c 27 KB

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