sit.c 31 KB

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