sit.c 29 KB

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