sit.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298
  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. };
  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. if (strchr(name, '%')) {
  223. if (dev_alloc_name(dev, name) < 0)
  224. goto failed_free;
  225. }
  226. nt = netdev_priv(dev);
  227. nt->parms = *parms;
  228. if (ipip6_tunnel_init(dev) < 0)
  229. goto failed_free;
  230. ipip6_tunnel_clone_6rd(dev, sitn);
  231. if (parms->i_flags & SIT_ISATAP)
  232. dev->priv_flags |= IFF_ISATAP;
  233. if (register_netdevice(dev) < 0)
  234. goto failed_free;
  235. dev_hold(dev);
  236. ipip6_tunnel_link(sitn, nt);
  237. return nt;
  238. failed_free:
  239. ipip6_dev_free(dev);
  240. failed:
  241. return NULL;
  242. }
  243. #define for_each_prl_rcu(start) \
  244. for (prl = rcu_dereference(start); \
  245. prl; \
  246. prl = rcu_dereference(prl->next))
  247. static struct ip_tunnel_prl_entry *
  248. __ipip6_tunnel_locate_prl(struct ip_tunnel *t, __be32 addr)
  249. {
  250. struct ip_tunnel_prl_entry *prl;
  251. for_each_prl_rcu(t->prl)
  252. if (prl->addr == addr)
  253. break;
  254. return prl;
  255. }
  256. static int ipip6_tunnel_get_prl(struct ip_tunnel *t,
  257. struct ip_tunnel_prl __user *a)
  258. {
  259. struct ip_tunnel_prl kprl, *kp;
  260. struct ip_tunnel_prl_entry *prl;
  261. unsigned int cmax, c = 0, ca, len;
  262. int ret = 0;
  263. if (copy_from_user(&kprl, a, sizeof(kprl)))
  264. return -EFAULT;
  265. cmax = kprl.datalen / sizeof(kprl);
  266. if (cmax > 1 && kprl.addr != htonl(INADDR_ANY))
  267. cmax = 1;
  268. /* For simple GET or for root users,
  269. * we try harder to allocate.
  270. */
  271. kp = (cmax <= 1 || capable(CAP_NET_ADMIN)) ?
  272. kcalloc(cmax, sizeof(*kp), GFP_KERNEL) :
  273. NULL;
  274. rcu_read_lock();
  275. ca = t->prl_count < cmax ? t->prl_count : cmax;
  276. if (!kp) {
  277. /* We don't try hard to allocate much memory for
  278. * non-root users.
  279. * For root users, retry allocating enough memory for
  280. * the answer.
  281. */
  282. kp = kcalloc(ca, sizeof(*kp), GFP_ATOMIC);
  283. if (!kp) {
  284. ret = -ENOMEM;
  285. goto out;
  286. }
  287. }
  288. c = 0;
  289. for_each_prl_rcu(t->prl) {
  290. if (c >= cmax)
  291. break;
  292. if (kprl.addr != htonl(INADDR_ANY) && prl->addr != kprl.addr)
  293. continue;
  294. kp[c].addr = prl->addr;
  295. kp[c].flags = prl->flags;
  296. c++;
  297. if (kprl.addr != htonl(INADDR_ANY))
  298. break;
  299. }
  300. out:
  301. rcu_read_unlock();
  302. len = sizeof(*kp) * c;
  303. ret = 0;
  304. if ((len && copy_to_user(a + 1, kp, len)) || put_user(len, &a->datalen))
  305. ret = -EFAULT;
  306. kfree(kp);
  307. return ret;
  308. }
  309. static int
  310. ipip6_tunnel_add_prl(struct ip_tunnel *t, struct ip_tunnel_prl *a, int chg)
  311. {
  312. struct ip_tunnel_prl_entry *p;
  313. int err = 0;
  314. if (a->addr == htonl(INADDR_ANY))
  315. return -EINVAL;
  316. ASSERT_RTNL();
  317. for (p = rtnl_dereference(t->prl); p; p = rtnl_dereference(p->next)) {
  318. if (p->addr == a->addr) {
  319. if (chg) {
  320. p->flags = a->flags;
  321. goto out;
  322. }
  323. err = -EEXIST;
  324. goto out;
  325. }
  326. }
  327. if (chg) {
  328. err = -ENXIO;
  329. goto out;
  330. }
  331. p = kzalloc(sizeof(struct ip_tunnel_prl_entry), GFP_KERNEL);
  332. if (!p) {
  333. err = -ENOBUFS;
  334. goto out;
  335. }
  336. p->next = t->prl;
  337. p->addr = a->addr;
  338. p->flags = a->flags;
  339. t->prl_count++;
  340. rcu_assign_pointer(t->prl, p);
  341. out:
  342. return err;
  343. }
  344. static void prl_entry_destroy_rcu(struct rcu_head *head)
  345. {
  346. kfree(container_of(head, struct ip_tunnel_prl_entry, rcu_head));
  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. call_rcu(&x->rcu_head, prl_entry_destroy_rcu);
  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, 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. 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_assign_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. struct iphdr *iph = (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. case ICMP_FRAG_NEEDED:
  446. /* Soft state for pmtu is maintained by IP core. */
  447. return 0;
  448. default:
  449. /* All others are translated to HOST_UNREACH.
  450. rfc2003 contains "deep thoughts" about NET_UNREACH,
  451. I believe they are just ether pollution. --ANK
  452. */
  453. break;
  454. }
  455. break;
  456. case ICMP_TIME_EXCEEDED:
  457. if (code != ICMP_EXC_TTL)
  458. return 0;
  459. break;
  460. }
  461. err = -ENOENT;
  462. rcu_read_lock();
  463. t = ipip6_tunnel_lookup(dev_net(skb->dev),
  464. skb->dev,
  465. iph->daddr,
  466. iph->saddr);
  467. if (t == NULL || t->parms.iph.daddr == 0)
  468. goto out;
  469. err = 0;
  470. if (t->parms.iph.ttl == 0 && type == ICMP_TIME_EXCEEDED)
  471. goto out;
  472. if (time_before(jiffies, t->err_time + IPTUNNEL_ERR_TIMEO))
  473. t->err_count++;
  474. else
  475. t->err_count = 1;
  476. t->err_time = jiffies;
  477. out:
  478. rcu_read_unlock();
  479. return err;
  480. }
  481. static inline void ipip6_ecn_decapsulate(struct iphdr *iph, struct sk_buff *skb)
  482. {
  483. if (INET_ECN_is_ce(iph->tos))
  484. IP6_ECN_set_ce(ipv6_hdr(skb));
  485. }
  486. static int ipip6_rcv(struct sk_buff *skb)
  487. {
  488. struct iphdr *iph;
  489. struct ip_tunnel *tunnel;
  490. if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
  491. goto out;
  492. iph = ip_hdr(skb);
  493. rcu_read_lock();
  494. tunnel = ipip6_tunnel_lookup(dev_net(skb->dev), skb->dev,
  495. iph->saddr, iph->daddr);
  496. if (tunnel != NULL) {
  497. struct pcpu_tstats *tstats;
  498. secpath_reset(skb);
  499. skb->mac_header = skb->network_header;
  500. skb_reset_network_header(skb);
  501. IPCB(skb)->flags = 0;
  502. skb->protocol = htons(ETH_P_IPV6);
  503. skb->pkt_type = PACKET_HOST;
  504. if ((tunnel->dev->priv_flags & IFF_ISATAP) &&
  505. !isatap_chksrc(skb, iph, tunnel)) {
  506. tunnel->dev->stats.rx_errors++;
  507. rcu_read_unlock();
  508. kfree_skb(skb);
  509. return 0;
  510. }
  511. tstats = this_cpu_ptr(tunnel->dev->tstats);
  512. tstats->rx_packets++;
  513. tstats->rx_bytes += skb->len;
  514. __skb_tunnel_rx(skb, tunnel->dev);
  515. ipip6_ecn_decapsulate(iph, skb);
  516. netif_rx(skb);
  517. rcu_read_unlock();
  518. return 0;
  519. }
  520. /* no tunnel matched, let upstream know, ipsec may handle it */
  521. rcu_read_unlock();
  522. return 1;
  523. out:
  524. kfree_skb(skb);
  525. return 0;
  526. }
  527. /*
  528. * Returns the embedded IPv4 address if the IPv6 address
  529. * comes from 6rd / 6to4 (RFC 3056) addr space.
  530. */
  531. static inline
  532. __be32 try_6rd(struct in6_addr *v6dst, struct ip_tunnel *tunnel)
  533. {
  534. __be32 dst = 0;
  535. #ifdef CONFIG_IPV6_SIT_6RD
  536. if (ipv6_prefix_equal(v6dst, &tunnel->ip6rd.prefix,
  537. tunnel->ip6rd.prefixlen)) {
  538. unsigned int pbw0, pbi0;
  539. int pbi1;
  540. u32 d;
  541. pbw0 = tunnel->ip6rd.prefixlen >> 5;
  542. pbi0 = tunnel->ip6rd.prefixlen & 0x1f;
  543. d = (ntohl(v6dst->s6_addr32[pbw0]) << pbi0) >>
  544. tunnel->ip6rd.relay_prefixlen;
  545. pbi1 = pbi0 - tunnel->ip6rd.relay_prefixlen;
  546. if (pbi1 > 0)
  547. d |= ntohl(v6dst->s6_addr32[pbw0 + 1]) >>
  548. (32 - pbi1);
  549. dst = tunnel->ip6rd.relay_prefix | htonl(d);
  550. }
  551. #else
  552. if (v6dst->s6_addr16[0] == htons(0x2002)) {
  553. /* 6to4 v6 addr has 16 bits prefix, 32 v4addr, 16 SLA, ... */
  554. memcpy(&dst, &v6dst->s6_addr16[1], 4);
  555. }
  556. #endif
  557. return dst;
  558. }
  559. /*
  560. * This function assumes it is being called from dev_queue_xmit()
  561. * and that skb is filled properly by that function.
  562. */
  563. static netdev_tx_t ipip6_tunnel_xmit(struct sk_buff *skb,
  564. struct net_device *dev)
  565. {
  566. struct ip_tunnel *tunnel = netdev_priv(dev);
  567. struct pcpu_tstats *tstats;
  568. struct iphdr *tiph = &tunnel->parms.iph;
  569. struct ipv6hdr *iph6 = ipv6_hdr(skb);
  570. u8 tos = tunnel->parms.iph.tos;
  571. __be16 df = tiph->frag_off;
  572. struct rtable *rt; /* Route to the other host */
  573. struct net_device *tdev; /* Device to other host */
  574. struct iphdr *iph; /* Our new IP header */
  575. unsigned int max_headroom; /* The extra header space needed */
  576. __be32 dst = tiph->daddr;
  577. int mtu;
  578. struct in6_addr *addr6;
  579. int addr_type;
  580. if (skb->protocol != htons(ETH_P_IPV6))
  581. goto tx_error;
  582. /* ISATAP (RFC4214) - must come before 6to4 */
  583. if (dev->priv_flags & IFF_ISATAP) {
  584. struct neighbour *neigh = NULL;
  585. if (skb_dst(skb))
  586. neigh = skb_dst(skb)->neighbour;
  587. if (neigh == NULL) {
  588. if (net_ratelimit())
  589. printk(KERN_DEBUG "sit: nexthop == NULL\n");
  590. goto tx_error;
  591. }
  592. addr6 = (struct in6_addr*)&neigh->primary_key;
  593. addr_type = ipv6_addr_type(addr6);
  594. if ((addr_type & IPV6_ADDR_UNICAST) &&
  595. ipv6_addr_is_isatap(addr6))
  596. dst = addr6->s6_addr32[3];
  597. else
  598. goto tx_error;
  599. }
  600. if (!dst)
  601. dst = try_6rd(&iph6->daddr, tunnel);
  602. if (!dst) {
  603. struct neighbour *neigh = NULL;
  604. if (skb_dst(skb))
  605. neigh = skb_dst(skb)->neighbour;
  606. if (neigh == NULL) {
  607. if (net_ratelimit())
  608. printk(KERN_DEBUG "sit: nexthop == NULL\n");
  609. goto tx_error;
  610. }
  611. addr6 = (struct in6_addr*)&neigh->primary_key;
  612. addr_type = ipv6_addr_type(addr6);
  613. if (addr_type == IPV6_ADDR_ANY) {
  614. addr6 = &ipv6_hdr(skb)->daddr;
  615. addr_type = ipv6_addr_type(addr6);
  616. }
  617. if ((addr_type & IPV6_ADDR_COMPATv4) == 0)
  618. goto tx_error_icmp;
  619. dst = addr6->s6_addr32[3];
  620. }
  621. {
  622. struct flowi fl = { .fl4_dst = dst,
  623. .fl4_src = tiph->saddr,
  624. .fl4_tos = RT_TOS(tos),
  625. .oif = tunnel->parms.link,
  626. .proto = IPPROTO_IPV6 };
  627. rt = ip_route_output_key(dev_net(dev), &fl);
  628. if (IS_ERR(rt)) {
  629. dev->stats.tx_carrier_errors++;
  630. goto tx_error_icmp;
  631. }
  632. }
  633. if (rt->rt_type != RTN_UNICAST) {
  634. ip_rt_put(rt);
  635. dev->stats.tx_carrier_errors++;
  636. goto tx_error_icmp;
  637. }
  638. tdev = rt->dst.dev;
  639. if (tdev == dev) {
  640. ip_rt_put(rt);
  641. dev->stats.collisions++;
  642. goto tx_error;
  643. }
  644. if (df) {
  645. mtu = dst_mtu(&rt->dst) - sizeof(struct iphdr);
  646. if (mtu < 68) {
  647. dev->stats.collisions++;
  648. ip_rt_put(rt);
  649. goto tx_error;
  650. }
  651. if (mtu < IPV6_MIN_MTU) {
  652. mtu = IPV6_MIN_MTU;
  653. df = 0;
  654. }
  655. if (tunnel->parms.iph.daddr && skb_dst(skb))
  656. skb_dst(skb)->ops->update_pmtu(skb_dst(skb), mtu);
  657. if (skb->len > mtu) {
  658. icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
  659. ip_rt_put(rt);
  660. goto tx_error;
  661. }
  662. }
  663. if (tunnel->err_count > 0) {
  664. if (time_before(jiffies,
  665. tunnel->err_time + IPTUNNEL_ERR_TIMEO)) {
  666. tunnel->err_count--;
  667. dst_link_failure(skb);
  668. } else
  669. tunnel->err_count = 0;
  670. }
  671. /*
  672. * Okay, now see if we can stuff it in the buffer as-is.
  673. */
  674. max_headroom = LL_RESERVED_SPACE(tdev)+sizeof(struct iphdr);
  675. if (skb_headroom(skb) < max_headroom || skb_shared(skb) ||
  676. (skb_cloned(skb) && !skb_clone_writable(skb, 0))) {
  677. struct sk_buff *new_skb = skb_realloc_headroom(skb, max_headroom);
  678. if (!new_skb) {
  679. ip_rt_put(rt);
  680. dev->stats.tx_dropped++;
  681. dev_kfree_skb(skb);
  682. return NETDEV_TX_OK;
  683. }
  684. if (skb->sk)
  685. skb_set_owner_w(new_skb, skb->sk);
  686. dev_kfree_skb(skb);
  687. skb = new_skb;
  688. iph6 = ipv6_hdr(skb);
  689. }
  690. skb->transport_header = skb->network_header;
  691. skb_push(skb, sizeof(struct iphdr));
  692. skb_reset_network_header(skb);
  693. memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
  694. IPCB(skb)->flags = 0;
  695. skb_dst_drop(skb);
  696. skb_dst_set(skb, &rt->dst);
  697. /*
  698. * Push down and install the IPIP header.
  699. */
  700. iph = ip_hdr(skb);
  701. iph->version = 4;
  702. iph->ihl = sizeof(struct iphdr)>>2;
  703. iph->frag_off = df;
  704. iph->protocol = IPPROTO_IPV6;
  705. iph->tos = INET_ECN_encapsulate(tos, ipv6_get_dsfield(iph6));
  706. iph->daddr = rt->rt_dst;
  707. iph->saddr = rt->rt_src;
  708. if ((iph->ttl = tiph->ttl) == 0)
  709. iph->ttl = iph6->hop_limit;
  710. nf_reset(skb);
  711. tstats = this_cpu_ptr(dev->tstats);
  712. __IPTUNNEL_XMIT(tstats, &dev->stats);
  713. return NETDEV_TX_OK;
  714. tx_error_icmp:
  715. dst_link_failure(skb);
  716. tx_error:
  717. dev->stats.tx_errors++;
  718. dev_kfree_skb(skb);
  719. return NETDEV_TX_OK;
  720. }
  721. static void ipip6_tunnel_bind_dev(struct net_device *dev)
  722. {
  723. struct net_device *tdev = NULL;
  724. struct ip_tunnel *tunnel;
  725. struct iphdr *iph;
  726. tunnel = netdev_priv(dev);
  727. iph = &tunnel->parms.iph;
  728. if (iph->daddr) {
  729. struct flowi fl = { .fl4_dst = iph->daddr,
  730. .fl4_src = iph->saddr,
  731. .fl4_tos = RT_TOS(iph->tos),
  732. .oif = tunnel->parms.link,
  733. .proto = IPPROTO_IPV6 };
  734. struct rtable *rt = ip_route_output_key(dev_net(dev), &fl);
  735. if (!IS_ERR(rt)) {
  736. tdev = rt->dst.dev;
  737. ip_rt_put(rt);
  738. }
  739. dev->flags |= IFF_POINTOPOINT;
  740. }
  741. if (!tdev && tunnel->parms.link)
  742. tdev = __dev_get_by_index(dev_net(dev), tunnel->parms.link);
  743. if (tdev) {
  744. dev->hard_header_len = tdev->hard_header_len + sizeof(struct iphdr);
  745. dev->mtu = tdev->mtu - sizeof(struct iphdr);
  746. if (dev->mtu < IPV6_MIN_MTU)
  747. dev->mtu = IPV6_MIN_MTU;
  748. }
  749. dev->iflink = tunnel->parms.link;
  750. }
  751. static int
  752. ipip6_tunnel_ioctl (struct net_device *dev, struct ifreq *ifr, int cmd)
  753. {
  754. int err = 0;
  755. struct ip_tunnel_parm p;
  756. struct ip_tunnel_prl prl;
  757. struct ip_tunnel *t;
  758. struct net *net = dev_net(dev);
  759. struct sit_net *sitn = net_generic(net, sit_net_id);
  760. #ifdef CONFIG_IPV6_SIT_6RD
  761. struct ip_tunnel_6rd ip6rd;
  762. #endif
  763. switch (cmd) {
  764. case SIOCGETTUNNEL:
  765. #ifdef CONFIG_IPV6_SIT_6RD
  766. case SIOCGET6RD:
  767. #endif
  768. t = NULL;
  769. if (dev == sitn->fb_tunnel_dev) {
  770. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) {
  771. err = -EFAULT;
  772. break;
  773. }
  774. t = ipip6_tunnel_locate(net, &p, 0);
  775. }
  776. if (t == NULL)
  777. t = netdev_priv(dev);
  778. err = -EFAULT;
  779. if (cmd == SIOCGETTUNNEL) {
  780. memcpy(&p, &t->parms, sizeof(p));
  781. if (copy_to_user(ifr->ifr_ifru.ifru_data, &p,
  782. sizeof(p)))
  783. goto done;
  784. #ifdef CONFIG_IPV6_SIT_6RD
  785. } else {
  786. ipv6_addr_copy(&ip6rd.prefix, &t->ip6rd.prefix);
  787. ip6rd.relay_prefix = t->ip6rd.relay_prefix;
  788. ip6rd.prefixlen = t->ip6rd.prefixlen;
  789. ip6rd.relay_prefixlen = t->ip6rd.relay_prefixlen;
  790. if (copy_to_user(ifr->ifr_ifru.ifru_data, &ip6rd,
  791. sizeof(ip6rd)))
  792. goto done;
  793. #endif
  794. }
  795. err = 0;
  796. break;
  797. case SIOCADDTUNNEL:
  798. case SIOCCHGTUNNEL:
  799. err = -EPERM;
  800. if (!capable(CAP_NET_ADMIN))
  801. goto done;
  802. err = -EFAULT;
  803. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
  804. goto done;
  805. err = -EINVAL;
  806. if (p.iph.version != 4 || p.iph.protocol != IPPROTO_IPV6 ||
  807. p.iph.ihl != 5 || (p.iph.frag_off&htons(~IP_DF)))
  808. goto done;
  809. if (p.iph.ttl)
  810. p.iph.frag_off |= htons(IP_DF);
  811. t = ipip6_tunnel_locate(net, &p, cmd == SIOCADDTUNNEL);
  812. if (dev != sitn->fb_tunnel_dev && cmd == SIOCCHGTUNNEL) {
  813. if (t != NULL) {
  814. if (t->dev != dev) {
  815. err = -EEXIST;
  816. break;
  817. }
  818. } else {
  819. if (((dev->flags&IFF_POINTOPOINT) && !p.iph.daddr) ||
  820. (!(dev->flags&IFF_POINTOPOINT) && p.iph.daddr)) {
  821. err = -EINVAL;
  822. break;
  823. }
  824. t = netdev_priv(dev);
  825. ipip6_tunnel_unlink(sitn, t);
  826. synchronize_net();
  827. t->parms.iph.saddr = p.iph.saddr;
  828. t->parms.iph.daddr = p.iph.daddr;
  829. memcpy(dev->dev_addr, &p.iph.saddr, 4);
  830. memcpy(dev->broadcast, &p.iph.daddr, 4);
  831. ipip6_tunnel_link(sitn, t);
  832. netdev_state_change(dev);
  833. }
  834. }
  835. if (t) {
  836. err = 0;
  837. if (cmd == SIOCCHGTUNNEL) {
  838. t->parms.iph.ttl = p.iph.ttl;
  839. t->parms.iph.tos = p.iph.tos;
  840. if (t->parms.link != p.link) {
  841. t->parms.link = p.link;
  842. ipip6_tunnel_bind_dev(dev);
  843. netdev_state_change(dev);
  844. }
  845. }
  846. if (copy_to_user(ifr->ifr_ifru.ifru_data, &t->parms, sizeof(p)))
  847. err = -EFAULT;
  848. } else
  849. err = (cmd == SIOCADDTUNNEL ? -ENOBUFS : -ENOENT);
  850. break;
  851. case SIOCDELTUNNEL:
  852. err = -EPERM;
  853. if (!capable(CAP_NET_ADMIN))
  854. goto done;
  855. if (dev == sitn->fb_tunnel_dev) {
  856. err = -EFAULT;
  857. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
  858. goto done;
  859. err = -ENOENT;
  860. if ((t = ipip6_tunnel_locate(net, &p, 0)) == NULL)
  861. goto done;
  862. err = -EPERM;
  863. if (t == netdev_priv(sitn->fb_tunnel_dev))
  864. goto done;
  865. dev = t->dev;
  866. }
  867. unregister_netdevice(dev);
  868. err = 0;
  869. break;
  870. case SIOCGETPRL:
  871. err = -EINVAL;
  872. if (dev == sitn->fb_tunnel_dev)
  873. goto done;
  874. err = -ENOENT;
  875. if (!(t = netdev_priv(dev)))
  876. goto done;
  877. err = ipip6_tunnel_get_prl(t, ifr->ifr_ifru.ifru_data);
  878. break;
  879. case SIOCADDPRL:
  880. case SIOCDELPRL:
  881. case SIOCCHGPRL:
  882. err = -EPERM;
  883. if (!capable(CAP_NET_ADMIN))
  884. goto done;
  885. err = -EINVAL;
  886. if (dev == sitn->fb_tunnel_dev)
  887. goto done;
  888. err = -EFAULT;
  889. if (copy_from_user(&prl, ifr->ifr_ifru.ifru_data, sizeof(prl)))
  890. goto done;
  891. err = -ENOENT;
  892. if (!(t = netdev_priv(dev)))
  893. goto done;
  894. switch (cmd) {
  895. case SIOCDELPRL:
  896. err = ipip6_tunnel_del_prl(t, &prl);
  897. break;
  898. case SIOCADDPRL:
  899. case SIOCCHGPRL:
  900. err = ipip6_tunnel_add_prl(t, &prl, cmd == SIOCCHGPRL);
  901. break;
  902. }
  903. netdev_state_change(dev);
  904. break;
  905. #ifdef CONFIG_IPV6_SIT_6RD
  906. case SIOCADD6RD:
  907. case SIOCCHG6RD:
  908. case SIOCDEL6RD:
  909. err = -EPERM;
  910. if (!capable(CAP_NET_ADMIN))
  911. goto done;
  912. err = -EFAULT;
  913. if (copy_from_user(&ip6rd, ifr->ifr_ifru.ifru_data,
  914. sizeof(ip6rd)))
  915. goto done;
  916. t = netdev_priv(dev);
  917. if (cmd != SIOCDEL6RD) {
  918. struct in6_addr prefix;
  919. __be32 relay_prefix;
  920. err = -EINVAL;
  921. if (ip6rd.relay_prefixlen > 32 ||
  922. ip6rd.prefixlen + (32 - ip6rd.relay_prefixlen) > 64)
  923. goto done;
  924. ipv6_addr_prefix(&prefix, &ip6rd.prefix,
  925. ip6rd.prefixlen);
  926. if (!ipv6_addr_equal(&prefix, &ip6rd.prefix))
  927. goto done;
  928. if (ip6rd.relay_prefixlen)
  929. relay_prefix = ip6rd.relay_prefix &
  930. htonl(0xffffffffUL <<
  931. (32 - ip6rd.relay_prefixlen));
  932. else
  933. relay_prefix = 0;
  934. if (relay_prefix != ip6rd.relay_prefix)
  935. goto done;
  936. ipv6_addr_copy(&t->ip6rd.prefix, &prefix);
  937. t->ip6rd.relay_prefix = relay_prefix;
  938. t->ip6rd.prefixlen = ip6rd.prefixlen;
  939. t->ip6rd.relay_prefixlen = ip6rd.relay_prefixlen;
  940. } else
  941. ipip6_tunnel_clone_6rd(dev, sitn);
  942. err = 0;
  943. break;
  944. #endif
  945. default:
  946. err = -EINVAL;
  947. }
  948. done:
  949. return err;
  950. }
  951. static int ipip6_tunnel_change_mtu(struct net_device *dev, int new_mtu)
  952. {
  953. if (new_mtu < IPV6_MIN_MTU || new_mtu > 0xFFF8 - sizeof(struct iphdr))
  954. return -EINVAL;
  955. dev->mtu = new_mtu;
  956. return 0;
  957. }
  958. static const struct net_device_ops ipip6_netdev_ops = {
  959. .ndo_uninit = ipip6_tunnel_uninit,
  960. .ndo_start_xmit = ipip6_tunnel_xmit,
  961. .ndo_do_ioctl = ipip6_tunnel_ioctl,
  962. .ndo_change_mtu = ipip6_tunnel_change_mtu,
  963. .ndo_get_stats = ipip6_get_stats,
  964. };
  965. static void ipip6_dev_free(struct net_device *dev)
  966. {
  967. free_percpu(dev->tstats);
  968. free_netdev(dev);
  969. }
  970. static void ipip6_tunnel_setup(struct net_device *dev)
  971. {
  972. dev->netdev_ops = &ipip6_netdev_ops;
  973. dev->destructor = ipip6_dev_free;
  974. dev->type = ARPHRD_SIT;
  975. dev->hard_header_len = LL_MAX_HEADER + sizeof(struct iphdr);
  976. dev->mtu = ETH_DATA_LEN - sizeof(struct iphdr);
  977. dev->flags = IFF_NOARP;
  978. dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
  979. dev->iflink = 0;
  980. dev->addr_len = 4;
  981. dev->features |= NETIF_F_NETNS_LOCAL;
  982. dev->features |= NETIF_F_LLTX;
  983. }
  984. static int ipip6_tunnel_init(struct net_device *dev)
  985. {
  986. struct ip_tunnel *tunnel = netdev_priv(dev);
  987. tunnel->dev = dev;
  988. strcpy(tunnel->parms.name, dev->name);
  989. memcpy(dev->dev_addr, &tunnel->parms.iph.saddr, 4);
  990. memcpy(dev->broadcast, &tunnel->parms.iph.daddr, 4);
  991. ipip6_tunnel_bind_dev(dev);
  992. dev->tstats = alloc_percpu(struct pcpu_tstats);
  993. if (!dev->tstats)
  994. return -ENOMEM;
  995. return 0;
  996. }
  997. static int __net_init ipip6_fb_tunnel_init(struct net_device *dev)
  998. {
  999. struct ip_tunnel *tunnel = netdev_priv(dev);
  1000. struct iphdr *iph = &tunnel->parms.iph;
  1001. struct net *net = dev_net(dev);
  1002. struct sit_net *sitn = net_generic(net, sit_net_id);
  1003. tunnel->dev = dev;
  1004. strcpy(tunnel->parms.name, dev->name);
  1005. iph->version = 4;
  1006. iph->protocol = IPPROTO_IPV6;
  1007. iph->ihl = 5;
  1008. iph->ttl = 64;
  1009. dev->tstats = alloc_percpu(struct pcpu_tstats);
  1010. if (!dev->tstats)
  1011. return -ENOMEM;
  1012. dev_hold(dev);
  1013. rcu_assign_pointer(sitn->tunnels_wc[0], tunnel);
  1014. return 0;
  1015. }
  1016. static struct xfrm_tunnel sit_handler __read_mostly = {
  1017. .handler = ipip6_rcv,
  1018. .err_handler = ipip6_err,
  1019. .priority = 1,
  1020. };
  1021. static void __net_exit sit_destroy_tunnels(struct sit_net *sitn, struct list_head *head)
  1022. {
  1023. int prio;
  1024. for (prio = 1; prio < 4; prio++) {
  1025. int h;
  1026. for (h = 0; h < HASH_SIZE; h++) {
  1027. struct ip_tunnel *t;
  1028. t = rtnl_dereference(sitn->tunnels[prio][h]);
  1029. while (t != NULL) {
  1030. unregister_netdevice_queue(t->dev, head);
  1031. t = rtnl_dereference(t->next);
  1032. }
  1033. }
  1034. }
  1035. }
  1036. static int __net_init sit_init_net(struct net *net)
  1037. {
  1038. struct sit_net *sitn = net_generic(net, sit_net_id);
  1039. int err;
  1040. sitn->tunnels[0] = sitn->tunnels_wc;
  1041. sitn->tunnels[1] = sitn->tunnels_l;
  1042. sitn->tunnels[2] = sitn->tunnels_r;
  1043. sitn->tunnels[3] = sitn->tunnels_r_l;
  1044. sitn->fb_tunnel_dev = alloc_netdev(sizeof(struct ip_tunnel), "sit0",
  1045. ipip6_tunnel_setup);
  1046. if (!sitn->fb_tunnel_dev) {
  1047. err = -ENOMEM;
  1048. goto err_alloc_dev;
  1049. }
  1050. dev_net_set(sitn->fb_tunnel_dev, net);
  1051. err = ipip6_fb_tunnel_init(sitn->fb_tunnel_dev);
  1052. if (err)
  1053. goto err_dev_free;
  1054. ipip6_tunnel_clone_6rd(sitn->fb_tunnel_dev, sitn);
  1055. if ((err = register_netdev(sitn->fb_tunnel_dev)))
  1056. goto err_reg_dev;
  1057. return 0;
  1058. err_reg_dev:
  1059. dev_put(sitn->fb_tunnel_dev);
  1060. err_dev_free:
  1061. ipip6_dev_free(sitn->fb_tunnel_dev);
  1062. err_alloc_dev:
  1063. return err;
  1064. }
  1065. static void __net_exit sit_exit_net(struct net *net)
  1066. {
  1067. struct sit_net *sitn = net_generic(net, sit_net_id);
  1068. LIST_HEAD(list);
  1069. rtnl_lock();
  1070. sit_destroy_tunnels(sitn, &list);
  1071. unregister_netdevice_queue(sitn->fb_tunnel_dev, &list);
  1072. unregister_netdevice_many(&list);
  1073. rtnl_unlock();
  1074. }
  1075. static struct pernet_operations sit_net_ops = {
  1076. .init = sit_init_net,
  1077. .exit = sit_exit_net,
  1078. .id = &sit_net_id,
  1079. .size = sizeof(struct sit_net),
  1080. };
  1081. static void __exit sit_cleanup(void)
  1082. {
  1083. xfrm4_tunnel_deregister(&sit_handler, AF_INET6);
  1084. unregister_pernet_device(&sit_net_ops);
  1085. rcu_barrier(); /* Wait for completion of call_rcu()'s */
  1086. }
  1087. static int __init sit_init(void)
  1088. {
  1089. int err;
  1090. printk(KERN_INFO "IPv6 over IPv4 tunneling driver\n");
  1091. err = register_pernet_device(&sit_net_ops);
  1092. if (err < 0)
  1093. return err;
  1094. err = xfrm4_tunnel_register(&sit_handler, AF_INET6);
  1095. if (err < 0) {
  1096. unregister_pernet_device(&sit_net_ops);
  1097. printk(KERN_INFO "sit init: Can't add protocol\n");
  1098. }
  1099. return err;
  1100. }
  1101. module_init(sit_init);
  1102. module_exit(sit_cleanup);
  1103. MODULE_LICENSE("GPL");
  1104. MODULE_ALIAS("sit0");