sit.c 28 KB

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