sit.c 24 KB

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