ip6_gre.c 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790
  1. /*
  2. * GRE over IPv6 protocol decoder.
  3. *
  4. * Authors: Dmitry Kozlov (xeb@mail.ru)
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the License, or (at your option) any later version.
  10. *
  11. */
  12. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  13. #include <linux/capability.h>
  14. #include <linux/module.h>
  15. #include <linux/types.h>
  16. #include <linux/kernel.h>
  17. #include <linux/slab.h>
  18. #include <linux/uaccess.h>
  19. #include <linux/skbuff.h>
  20. #include <linux/netdevice.h>
  21. #include <linux/in.h>
  22. #include <linux/tcp.h>
  23. #include <linux/udp.h>
  24. #include <linux/if_arp.h>
  25. #include <linux/mroute.h>
  26. #include <linux/init.h>
  27. #include <linux/in6.h>
  28. #include <linux/inetdevice.h>
  29. #include <linux/igmp.h>
  30. #include <linux/netfilter_ipv4.h>
  31. #include <linux/etherdevice.h>
  32. #include <linux/if_ether.h>
  33. #include <linux/hash.h>
  34. #include <linux/if_tunnel.h>
  35. #include <linux/ip6_tunnel.h>
  36. #include <net/sock.h>
  37. #include <net/ip.h>
  38. #include <net/icmp.h>
  39. #include <net/protocol.h>
  40. #include <net/addrconf.h>
  41. #include <net/arp.h>
  42. #include <net/checksum.h>
  43. #include <net/dsfield.h>
  44. #include <net/inet_ecn.h>
  45. #include <net/xfrm.h>
  46. #include <net/net_namespace.h>
  47. #include <net/netns/generic.h>
  48. #include <net/rtnetlink.h>
  49. #include <net/ipv6.h>
  50. #include <net/ip6_fib.h>
  51. #include <net/ip6_route.h>
  52. #include <net/ip6_tunnel.h>
  53. #define IPV6_TCLASS_MASK (IPV6_FLOWINFO_MASK & ~IPV6_FLOWLABEL_MASK)
  54. #define IPV6_TCLASS_SHIFT 20
  55. #define HASH_SIZE_SHIFT 5
  56. #define HASH_SIZE (1 << HASH_SIZE_SHIFT)
  57. static int ip6gre_net_id __read_mostly;
  58. struct ip6gre_net {
  59. struct ip6_tnl __rcu *tunnels[4][HASH_SIZE];
  60. struct net_device *fb_tunnel_dev;
  61. };
  62. static struct rtnl_link_ops ip6gre_link_ops __read_mostly;
  63. static int ip6gre_tunnel_init(struct net_device *dev);
  64. static void ip6gre_tunnel_setup(struct net_device *dev);
  65. static void ip6gre_tunnel_link(struct ip6gre_net *ign, struct ip6_tnl *t);
  66. static void ip6gre_tnl_link_config(struct ip6_tnl *t, int set_mtu);
  67. /* Tunnel hash table */
  68. /*
  69. 4 hash tables:
  70. 3: (remote,local)
  71. 2: (remote,*)
  72. 1: (*,local)
  73. 0: (*,*)
  74. We require exact key match i.e. if a key is present in packet
  75. it will match only tunnel with the same key; if it is not present,
  76. it will match only keyless tunnel.
  77. All keysless packets, if not matched configured keyless tunnels
  78. will match fallback tunnel.
  79. */
  80. #define HASH_KEY(key) (((__force u32)key^((__force u32)key>>4))&(HASH_SIZE - 1))
  81. static u32 HASH_ADDR(const struct in6_addr *addr)
  82. {
  83. u32 hash = ipv6_addr_hash(addr);
  84. return hash_32(hash, HASH_SIZE_SHIFT);
  85. }
  86. #define tunnels_r_l tunnels[3]
  87. #define tunnels_r tunnels[2]
  88. #define tunnels_l tunnels[1]
  89. #define tunnels_wc tunnels[0]
  90. /*
  91. * Locking : hash tables are protected by RCU and RTNL
  92. */
  93. #define for_each_ip_tunnel_rcu(start) \
  94. for (t = rcu_dereference(start); t; t = rcu_dereference(t->next))
  95. /* often modified stats are per cpu, other are shared (netdev->stats) */
  96. struct pcpu_tstats {
  97. u64 rx_packets;
  98. u64 rx_bytes;
  99. u64 tx_packets;
  100. u64 tx_bytes;
  101. struct u64_stats_sync syncp;
  102. };
  103. static struct rtnl_link_stats64 *ip6gre_get_stats64(struct net_device *dev,
  104. struct rtnl_link_stats64 *tot)
  105. {
  106. int i;
  107. for_each_possible_cpu(i) {
  108. const struct pcpu_tstats *tstats = per_cpu_ptr(dev->tstats, i);
  109. u64 rx_packets, rx_bytes, tx_packets, tx_bytes;
  110. unsigned int start;
  111. do {
  112. start = u64_stats_fetch_begin_bh(&tstats->syncp);
  113. rx_packets = tstats->rx_packets;
  114. tx_packets = tstats->tx_packets;
  115. rx_bytes = tstats->rx_bytes;
  116. tx_bytes = tstats->tx_bytes;
  117. } while (u64_stats_fetch_retry_bh(&tstats->syncp, start));
  118. tot->rx_packets += rx_packets;
  119. tot->tx_packets += tx_packets;
  120. tot->rx_bytes += rx_bytes;
  121. tot->tx_bytes += tx_bytes;
  122. }
  123. tot->multicast = dev->stats.multicast;
  124. tot->rx_crc_errors = dev->stats.rx_crc_errors;
  125. tot->rx_fifo_errors = dev->stats.rx_fifo_errors;
  126. tot->rx_length_errors = dev->stats.rx_length_errors;
  127. tot->rx_errors = dev->stats.rx_errors;
  128. tot->tx_fifo_errors = dev->stats.tx_fifo_errors;
  129. tot->tx_carrier_errors = dev->stats.tx_carrier_errors;
  130. tot->tx_dropped = dev->stats.tx_dropped;
  131. tot->tx_aborted_errors = dev->stats.tx_aborted_errors;
  132. tot->tx_errors = dev->stats.tx_errors;
  133. return tot;
  134. }
  135. /* Given src, dst and key, find appropriate for input tunnel. */
  136. static struct ip6_tnl *ip6gre_tunnel_lookup(struct net_device *dev,
  137. const struct in6_addr *remote, const struct in6_addr *local,
  138. __be32 key, __be16 gre_proto)
  139. {
  140. struct net *net = dev_net(dev);
  141. int link = dev->ifindex;
  142. unsigned int h0 = HASH_ADDR(remote);
  143. unsigned int h1 = HASH_KEY(key);
  144. struct ip6_tnl *t, *cand = NULL;
  145. struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
  146. int dev_type = (gre_proto == htons(ETH_P_TEB)) ?
  147. ARPHRD_ETHER : ARPHRD_IP6GRE;
  148. int score, cand_score = 4;
  149. for_each_ip_tunnel_rcu(ign->tunnels_r_l[h0 ^ h1]) {
  150. if (!ipv6_addr_equal(local, &t->parms.laddr) ||
  151. !ipv6_addr_equal(remote, &t->parms.raddr) ||
  152. key != t->parms.i_key ||
  153. !(t->dev->flags & IFF_UP))
  154. continue;
  155. if (t->dev->type != ARPHRD_IP6GRE &&
  156. t->dev->type != dev_type)
  157. continue;
  158. score = 0;
  159. if (t->parms.link != link)
  160. score |= 1;
  161. if (t->dev->type != dev_type)
  162. score |= 2;
  163. if (score == 0)
  164. return t;
  165. if (score < cand_score) {
  166. cand = t;
  167. cand_score = score;
  168. }
  169. }
  170. for_each_ip_tunnel_rcu(ign->tunnels_r[h0 ^ h1]) {
  171. if (!ipv6_addr_equal(remote, &t->parms.raddr) ||
  172. key != t->parms.i_key ||
  173. !(t->dev->flags & IFF_UP))
  174. continue;
  175. if (t->dev->type != ARPHRD_IP6GRE &&
  176. t->dev->type != dev_type)
  177. continue;
  178. score = 0;
  179. if (t->parms.link != link)
  180. score |= 1;
  181. if (t->dev->type != dev_type)
  182. score |= 2;
  183. if (score == 0)
  184. return t;
  185. if (score < cand_score) {
  186. cand = t;
  187. cand_score = score;
  188. }
  189. }
  190. for_each_ip_tunnel_rcu(ign->tunnels_l[h1]) {
  191. if ((!ipv6_addr_equal(local, &t->parms.laddr) &&
  192. (!ipv6_addr_equal(local, &t->parms.raddr) ||
  193. !ipv6_addr_is_multicast(local))) ||
  194. key != t->parms.i_key ||
  195. !(t->dev->flags & IFF_UP))
  196. continue;
  197. if (t->dev->type != ARPHRD_IP6GRE &&
  198. t->dev->type != dev_type)
  199. continue;
  200. score = 0;
  201. if (t->parms.link != link)
  202. score |= 1;
  203. if (t->dev->type != dev_type)
  204. score |= 2;
  205. if (score == 0)
  206. return t;
  207. if (score < cand_score) {
  208. cand = t;
  209. cand_score = score;
  210. }
  211. }
  212. for_each_ip_tunnel_rcu(ign->tunnels_wc[h1]) {
  213. if (t->parms.i_key != key ||
  214. !(t->dev->flags & IFF_UP))
  215. continue;
  216. if (t->dev->type != ARPHRD_IP6GRE &&
  217. t->dev->type != dev_type)
  218. continue;
  219. score = 0;
  220. if (t->parms.link != link)
  221. score |= 1;
  222. if (t->dev->type != dev_type)
  223. score |= 2;
  224. if (score == 0)
  225. return t;
  226. if (score < cand_score) {
  227. cand = t;
  228. cand_score = score;
  229. }
  230. }
  231. if (cand != NULL)
  232. return cand;
  233. dev = ign->fb_tunnel_dev;
  234. if (dev->flags & IFF_UP)
  235. return netdev_priv(dev);
  236. return NULL;
  237. }
  238. static struct ip6_tnl __rcu **__ip6gre_bucket(struct ip6gre_net *ign,
  239. const struct __ip6_tnl_parm *p)
  240. {
  241. const struct in6_addr *remote = &p->raddr;
  242. const struct in6_addr *local = &p->laddr;
  243. unsigned int h = HASH_KEY(p->i_key);
  244. int prio = 0;
  245. if (!ipv6_addr_any(local))
  246. prio |= 1;
  247. if (!ipv6_addr_any(remote) && !ipv6_addr_is_multicast(remote)) {
  248. prio |= 2;
  249. h ^= HASH_ADDR(remote);
  250. }
  251. return &ign->tunnels[prio][h];
  252. }
  253. static inline struct ip6_tnl __rcu **ip6gre_bucket(struct ip6gre_net *ign,
  254. const struct ip6_tnl *t)
  255. {
  256. return __ip6gre_bucket(ign, &t->parms);
  257. }
  258. static void ip6gre_tunnel_link(struct ip6gre_net *ign, struct ip6_tnl *t)
  259. {
  260. struct ip6_tnl __rcu **tp = ip6gre_bucket(ign, t);
  261. rcu_assign_pointer(t->next, rtnl_dereference(*tp));
  262. rcu_assign_pointer(*tp, t);
  263. }
  264. static void ip6gre_tunnel_unlink(struct ip6gre_net *ign, struct ip6_tnl *t)
  265. {
  266. struct ip6_tnl __rcu **tp;
  267. struct ip6_tnl *iter;
  268. for (tp = ip6gre_bucket(ign, t);
  269. (iter = rtnl_dereference(*tp)) != NULL;
  270. tp = &iter->next) {
  271. if (t == iter) {
  272. rcu_assign_pointer(*tp, t->next);
  273. break;
  274. }
  275. }
  276. }
  277. static struct ip6_tnl *ip6gre_tunnel_find(struct net *net,
  278. const struct __ip6_tnl_parm *parms,
  279. int type)
  280. {
  281. const struct in6_addr *remote = &parms->raddr;
  282. const struct in6_addr *local = &parms->laddr;
  283. __be32 key = parms->i_key;
  284. int link = parms->link;
  285. struct ip6_tnl *t;
  286. struct ip6_tnl __rcu **tp;
  287. struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
  288. for (tp = __ip6gre_bucket(ign, parms);
  289. (t = rtnl_dereference(*tp)) != NULL;
  290. tp = &t->next)
  291. if (ipv6_addr_equal(local, &t->parms.laddr) &&
  292. ipv6_addr_equal(remote, &t->parms.raddr) &&
  293. key == t->parms.i_key &&
  294. link == t->parms.link &&
  295. type == t->dev->type)
  296. break;
  297. return t;
  298. }
  299. static struct ip6_tnl *ip6gre_tunnel_locate(struct net *net,
  300. const struct __ip6_tnl_parm *parms, int create)
  301. {
  302. struct ip6_tnl *t, *nt;
  303. struct net_device *dev;
  304. char name[IFNAMSIZ];
  305. struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
  306. t = ip6gre_tunnel_find(net, parms, ARPHRD_IP6GRE);
  307. if (t || !create)
  308. return t;
  309. if (parms->name[0])
  310. strlcpy(name, parms->name, IFNAMSIZ);
  311. else
  312. strcpy(name, "ip6gre%d");
  313. dev = alloc_netdev(sizeof(*t), name, ip6gre_tunnel_setup);
  314. if (!dev)
  315. return NULL;
  316. dev_net_set(dev, net);
  317. nt = netdev_priv(dev);
  318. nt->parms = *parms;
  319. dev->rtnl_link_ops = &ip6gre_link_ops;
  320. nt->dev = dev;
  321. ip6gre_tnl_link_config(nt, 1);
  322. if (register_netdevice(dev) < 0)
  323. goto failed_free;
  324. /* Can use a lockless transmit, unless we generate output sequences */
  325. if (!(nt->parms.o_flags & GRE_SEQ))
  326. dev->features |= NETIF_F_LLTX;
  327. dev_hold(dev);
  328. ip6gre_tunnel_link(ign, nt);
  329. return nt;
  330. failed_free:
  331. free_netdev(dev);
  332. return NULL;
  333. }
  334. static void ip6gre_tunnel_uninit(struct net_device *dev)
  335. {
  336. struct net *net = dev_net(dev);
  337. struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
  338. ip6gre_tunnel_unlink(ign, netdev_priv(dev));
  339. dev_put(dev);
  340. }
  341. static void ip6gre_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
  342. u8 type, u8 code, int offset, __be32 info)
  343. {
  344. const struct ipv6hdr *ipv6h = (const struct ipv6hdr *)skb->data;
  345. __be16 *p = (__be16 *)(ipv6h + 1);
  346. int grehlen = sizeof(ipv6h) + 4;
  347. struct ip6_tnl *t;
  348. __be16 flags;
  349. flags = p[0];
  350. if (flags&(GRE_CSUM|GRE_KEY|GRE_SEQ|GRE_ROUTING|GRE_VERSION)) {
  351. if (flags&(GRE_VERSION|GRE_ROUTING))
  352. return;
  353. if (flags&GRE_KEY) {
  354. grehlen += 4;
  355. if (flags&GRE_CSUM)
  356. grehlen += 4;
  357. }
  358. }
  359. /* If only 8 bytes returned, keyed message will be dropped here */
  360. if (skb_headlen(skb) < grehlen)
  361. return;
  362. rcu_read_lock();
  363. t = ip6gre_tunnel_lookup(skb->dev, &ipv6h->daddr, &ipv6h->saddr,
  364. flags & GRE_KEY ?
  365. *(((__be32 *)p) + (grehlen / 4) - 1) : 0,
  366. p[1]);
  367. if (t == NULL)
  368. goto out;
  369. switch (type) {
  370. __u32 teli;
  371. struct ipv6_tlv_tnl_enc_lim *tel;
  372. __u32 mtu;
  373. case ICMPV6_DEST_UNREACH:
  374. net_warn_ratelimited("%s: Path to destination invalid or inactive!\n",
  375. t->parms.name);
  376. break;
  377. case ICMPV6_TIME_EXCEED:
  378. if (code == ICMPV6_EXC_HOPLIMIT) {
  379. net_warn_ratelimited("%s: Too small hop limit or routing loop in tunnel!\n",
  380. t->parms.name);
  381. }
  382. break;
  383. case ICMPV6_PARAMPROB:
  384. teli = 0;
  385. if (code == ICMPV6_HDR_FIELD)
  386. teli = ip6_tnl_parse_tlv_enc_lim(skb, skb->data);
  387. if (teli && teli == info - 2) {
  388. tel = (struct ipv6_tlv_tnl_enc_lim *) &skb->data[teli];
  389. if (tel->encap_limit == 0) {
  390. net_warn_ratelimited("%s: Too small encapsulation limit or routing loop in tunnel!\n",
  391. t->parms.name);
  392. }
  393. } else {
  394. net_warn_ratelimited("%s: Recipient unable to parse tunneled packet!\n",
  395. t->parms.name);
  396. }
  397. break;
  398. case ICMPV6_PKT_TOOBIG:
  399. mtu = info - offset;
  400. if (mtu < IPV6_MIN_MTU)
  401. mtu = IPV6_MIN_MTU;
  402. t->dev->mtu = mtu;
  403. break;
  404. }
  405. if (time_before(jiffies, t->err_time + IP6TUNNEL_ERR_TIMEO))
  406. t->err_count++;
  407. else
  408. t->err_count = 1;
  409. t->err_time = jiffies;
  410. out:
  411. rcu_read_unlock();
  412. }
  413. static inline void ip6gre_ecn_decapsulate_ipv4(const struct ip6_tnl *t,
  414. const struct ipv6hdr *ipv6h, struct sk_buff *skb)
  415. {
  416. __u8 dsfield = ipv6_get_dsfield(ipv6h) & ~INET_ECN_MASK;
  417. if (t->parms.flags & IP6_TNL_F_RCV_DSCP_COPY)
  418. ipv4_change_dsfield(ip_hdr(skb), INET_ECN_MASK, dsfield);
  419. if (INET_ECN_is_ce(dsfield))
  420. IP_ECN_set_ce(ip_hdr(skb));
  421. }
  422. static inline void ip6gre_ecn_decapsulate_ipv6(const struct ip6_tnl *t,
  423. const struct ipv6hdr *ipv6h, struct sk_buff *skb)
  424. {
  425. if (t->parms.flags & IP6_TNL_F_RCV_DSCP_COPY)
  426. ipv6_copy_dscp(ipv6_get_dsfield(ipv6h), ipv6_hdr(skb));
  427. if (INET_ECN_is_ce(ipv6_get_dsfield(ipv6h)))
  428. IP6_ECN_set_ce(ipv6_hdr(skb));
  429. }
  430. static int ip6gre_rcv(struct sk_buff *skb)
  431. {
  432. const struct ipv6hdr *ipv6h;
  433. u8 *h;
  434. __be16 flags;
  435. __sum16 csum = 0;
  436. __be32 key = 0;
  437. u32 seqno = 0;
  438. struct ip6_tnl *tunnel;
  439. int offset = 4;
  440. __be16 gre_proto;
  441. if (!pskb_may_pull(skb, sizeof(struct in6_addr)))
  442. goto drop_nolock;
  443. ipv6h = ipv6_hdr(skb);
  444. h = skb->data;
  445. flags = *(__be16 *)h;
  446. if (flags&(GRE_CSUM|GRE_KEY|GRE_ROUTING|GRE_SEQ|GRE_VERSION)) {
  447. /* - Version must be 0.
  448. - We do not support routing headers.
  449. */
  450. if (flags&(GRE_VERSION|GRE_ROUTING))
  451. goto drop_nolock;
  452. if (flags&GRE_CSUM) {
  453. switch (skb->ip_summed) {
  454. case CHECKSUM_COMPLETE:
  455. csum = csum_fold(skb->csum);
  456. if (!csum)
  457. break;
  458. /* fall through */
  459. case CHECKSUM_NONE:
  460. skb->csum = 0;
  461. csum = __skb_checksum_complete(skb);
  462. skb->ip_summed = CHECKSUM_COMPLETE;
  463. }
  464. offset += 4;
  465. }
  466. if (flags&GRE_KEY) {
  467. key = *(__be32 *)(h + offset);
  468. offset += 4;
  469. }
  470. if (flags&GRE_SEQ) {
  471. seqno = ntohl(*(__be32 *)(h + offset));
  472. offset += 4;
  473. }
  474. }
  475. gre_proto = *(__be16 *)(h + 2);
  476. rcu_read_lock();
  477. tunnel = ip6gre_tunnel_lookup(skb->dev,
  478. &ipv6h->saddr, &ipv6h->daddr, key,
  479. gre_proto);
  480. if (tunnel) {
  481. struct pcpu_tstats *tstats;
  482. if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb))
  483. goto drop;
  484. if (!ip6_tnl_rcv_ctl(tunnel, &ipv6h->daddr, &ipv6h->saddr)) {
  485. tunnel->dev->stats.rx_dropped++;
  486. goto drop;
  487. }
  488. secpath_reset(skb);
  489. skb->protocol = gre_proto;
  490. /* WCCP version 1 and 2 protocol decoding.
  491. * - Change protocol to IP
  492. * - When dealing with WCCPv2, Skip extra 4 bytes in GRE header
  493. */
  494. if (flags == 0 && gre_proto == htons(ETH_P_WCCP)) {
  495. skb->protocol = htons(ETH_P_IP);
  496. if ((*(h + offset) & 0xF0) != 0x40)
  497. offset += 4;
  498. }
  499. skb->mac_header = skb->network_header;
  500. __pskb_pull(skb, offset);
  501. skb_postpull_rcsum(skb, skb_transport_header(skb), offset);
  502. skb->pkt_type = PACKET_HOST;
  503. if (((flags&GRE_CSUM) && csum) ||
  504. (!(flags&GRE_CSUM) && tunnel->parms.i_flags&GRE_CSUM)) {
  505. tunnel->dev->stats.rx_crc_errors++;
  506. tunnel->dev->stats.rx_errors++;
  507. goto drop;
  508. }
  509. if (tunnel->parms.i_flags&GRE_SEQ) {
  510. if (!(flags&GRE_SEQ) ||
  511. (tunnel->i_seqno &&
  512. (s32)(seqno - tunnel->i_seqno) < 0)) {
  513. tunnel->dev->stats.rx_fifo_errors++;
  514. tunnel->dev->stats.rx_errors++;
  515. goto drop;
  516. }
  517. tunnel->i_seqno = seqno + 1;
  518. }
  519. /* Warning: All skb pointers will be invalidated! */
  520. if (tunnel->dev->type == ARPHRD_ETHER) {
  521. if (!pskb_may_pull(skb, ETH_HLEN)) {
  522. tunnel->dev->stats.rx_length_errors++;
  523. tunnel->dev->stats.rx_errors++;
  524. goto drop;
  525. }
  526. ipv6h = ipv6_hdr(skb);
  527. skb->protocol = eth_type_trans(skb, tunnel->dev);
  528. skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN);
  529. }
  530. tstats = this_cpu_ptr(tunnel->dev->tstats);
  531. u64_stats_update_begin(&tstats->syncp);
  532. tstats->rx_packets++;
  533. tstats->rx_bytes += skb->len;
  534. u64_stats_update_end(&tstats->syncp);
  535. __skb_tunnel_rx(skb, tunnel->dev);
  536. skb_reset_network_header(skb);
  537. if (skb->protocol == htons(ETH_P_IP))
  538. ip6gre_ecn_decapsulate_ipv4(tunnel, ipv6h, skb);
  539. else if (skb->protocol == htons(ETH_P_IPV6))
  540. ip6gre_ecn_decapsulate_ipv6(tunnel, ipv6h, skb);
  541. netif_rx(skb);
  542. rcu_read_unlock();
  543. return 0;
  544. }
  545. icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0);
  546. drop:
  547. rcu_read_unlock();
  548. drop_nolock:
  549. kfree_skb(skb);
  550. return 0;
  551. }
  552. struct ipv6_tel_txoption {
  553. struct ipv6_txoptions ops;
  554. __u8 dst_opt[8];
  555. };
  556. static void init_tel_txopt(struct ipv6_tel_txoption *opt, __u8 encap_limit)
  557. {
  558. memset(opt, 0, sizeof(struct ipv6_tel_txoption));
  559. opt->dst_opt[2] = IPV6_TLV_TNL_ENCAP_LIMIT;
  560. opt->dst_opt[3] = 1;
  561. opt->dst_opt[4] = encap_limit;
  562. opt->dst_opt[5] = IPV6_TLV_PADN;
  563. opt->dst_opt[6] = 1;
  564. opt->ops.dst0opt = (struct ipv6_opt_hdr *) opt->dst_opt;
  565. opt->ops.opt_nflen = 8;
  566. }
  567. static netdev_tx_t ip6gre_xmit2(struct sk_buff *skb,
  568. struct net_device *dev,
  569. __u8 dsfield,
  570. struct flowi6 *fl6,
  571. int encap_limit,
  572. __u32 *pmtu)
  573. {
  574. struct net *net = dev_net(dev);
  575. struct ip6_tnl *tunnel = netdev_priv(dev);
  576. struct net_device *tdev; /* Device to other host */
  577. struct ipv6hdr *ipv6h; /* Our new IP header */
  578. unsigned int max_headroom; /* The extra header space needed */
  579. int gre_hlen;
  580. struct ipv6_tel_txoption opt;
  581. int mtu;
  582. struct dst_entry *dst = NULL, *ndst = NULL;
  583. struct net_device_stats *stats = &tunnel->dev->stats;
  584. int err = -1;
  585. u8 proto;
  586. int pkt_len;
  587. struct sk_buff *new_skb;
  588. if (dev->type == ARPHRD_ETHER)
  589. IPCB(skb)->flags = 0;
  590. if (dev->header_ops && dev->type == ARPHRD_IP6GRE) {
  591. gre_hlen = 0;
  592. ipv6h = (struct ipv6hdr *)skb->data;
  593. fl6->daddr = ipv6h->daddr;
  594. } else {
  595. gre_hlen = tunnel->hlen;
  596. fl6->daddr = tunnel->parms.raddr;
  597. }
  598. if (!fl6->flowi6_mark)
  599. dst = ip6_tnl_dst_check(tunnel);
  600. if (!dst) {
  601. ndst = ip6_route_output(net, NULL, fl6);
  602. if (ndst->error)
  603. goto tx_err_link_failure;
  604. ndst = xfrm_lookup(net, ndst, flowi6_to_flowi(fl6), NULL, 0);
  605. if (IS_ERR(ndst)) {
  606. err = PTR_ERR(ndst);
  607. ndst = NULL;
  608. goto tx_err_link_failure;
  609. }
  610. dst = ndst;
  611. }
  612. tdev = dst->dev;
  613. if (tdev == dev) {
  614. stats->collisions++;
  615. net_warn_ratelimited("%s: Local routing loop detected!\n",
  616. tunnel->parms.name);
  617. goto tx_err_dst_release;
  618. }
  619. mtu = dst_mtu(dst) - sizeof(*ipv6h);
  620. if (encap_limit >= 0) {
  621. max_headroom += 8;
  622. mtu -= 8;
  623. }
  624. if (mtu < IPV6_MIN_MTU)
  625. mtu = IPV6_MIN_MTU;
  626. if (skb_dst(skb))
  627. skb_dst(skb)->ops->update_pmtu(skb_dst(skb), NULL, skb, mtu);
  628. if (skb->len > mtu) {
  629. *pmtu = mtu;
  630. err = -EMSGSIZE;
  631. goto tx_err_dst_release;
  632. }
  633. if (tunnel->err_count > 0) {
  634. if (time_before(jiffies,
  635. tunnel->err_time + IP6TUNNEL_ERR_TIMEO)) {
  636. tunnel->err_count--;
  637. dst_link_failure(skb);
  638. } else
  639. tunnel->err_count = 0;
  640. }
  641. max_headroom = LL_RESERVED_SPACE(tdev) + gre_hlen + dst->header_len;
  642. if (skb_headroom(skb) < max_headroom || skb_shared(skb) ||
  643. (skb_cloned(skb) && !skb_clone_writable(skb, 0))) {
  644. new_skb = skb_realloc_headroom(skb, max_headroom);
  645. if (max_headroom > dev->needed_headroom)
  646. dev->needed_headroom = max_headroom;
  647. if (!new_skb)
  648. goto tx_err_dst_release;
  649. if (skb->sk)
  650. skb_set_owner_w(new_skb, skb->sk);
  651. consume_skb(skb);
  652. skb = new_skb;
  653. }
  654. skb_dst_drop(skb);
  655. if (fl6->flowi6_mark) {
  656. skb_dst_set(skb, dst);
  657. ndst = NULL;
  658. } else {
  659. skb_dst_set_noref(skb, dst);
  660. }
  661. skb->transport_header = skb->network_header;
  662. proto = NEXTHDR_GRE;
  663. if (encap_limit >= 0) {
  664. init_tel_txopt(&opt, encap_limit);
  665. ipv6_push_nfrag_opts(skb, &opt.ops, &proto, NULL);
  666. }
  667. skb_push(skb, gre_hlen);
  668. skb_reset_network_header(skb);
  669. /*
  670. * Push down and install the IP header.
  671. */
  672. ipv6h = ipv6_hdr(skb);
  673. *(__be32 *)ipv6h = fl6->flowlabel | htonl(0x60000000);
  674. dsfield = INET_ECN_encapsulate(0, dsfield);
  675. ipv6_change_dsfield(ipv6h, ~INET_ECN_MASK, dsfield);
  676. ipv6h->hop_limit = tunnel->parms.hop_limit;
  677. ipv6h->nexthdr = proto;
  678. ipv6h->saddr = fl6->saddr;
  679. ipv6h->daddr = fl6->daddr;
  680. ((__be16 *)(ipv6h + 1))[0] = tunnel->parms.o_flags;
  681. ((__be16 *)(ipv6h + 1))[1] = (dev->type == ARPHRD_ETHER) ?
  682. htons(ETH_P_TEB) : skb->protocol;
  683. if (tunnel->parms.o_flags&(GRE_KEY|GRE_CSUM|GRE_SEQ)) {
  684. __be32 *ptr = (__be32 *)(((u8 *)ipv6h) + tunnel->hlen - 4);
  685. if (tunnel->parms.o_flags&GRE_SEQ) {
  686. ++tunnel->o_seqno;
  687. *ptr = htonl(tunnel->o_seqno);
  688. ptr--;
  689. }
  690. if (tunnel->parms.o_flags&GRE_KEY) {
  691. *ptr = tunnel->parms.o_key;
  692. ptr--;
  693. }
  694. if (tunnel->parms.o_flags&GRE_CSUM) {
  695. *ptr = 0;
  696. *(__sum16 *)ptr = ip_compute_csum((void *)(ipv6h+1),
  697. skb->len - sizeof(struct ipv6hdr));
  698. }
  699. }
  700. nf_reset(skb);
  701. pkt_len = skb->len;
  702. err = ip6_local_out(skb);
  703. if (net_xmit_eval(err) == 0) {
  704. struct pcpu_tstats *tstats = this_cpu_ptr(tunnel->dev->tstats);
  705. tstats->tx_bytes += pkt_len;
  706. tstats->tx_packets++;
  707. } else {
  708. stats->tx_errors++;
  709. stats->tx_aborted_errors++;
  710. }
  711. if (ndst)
  712. ip6_tnl_dst_store(tunnel, ndst);
  713. return 0;
  714. tx_err_link_failure:
  715. stats->tx_carrier_errors++;
  716. dst_link_failure(skb);
  717. tx_err_dst_release:
  718. dst_release(ndst);
  719. return err;
  720. }
  721. static inline int ip6gre_xmit_ipv4(struct sk_buff *skb, struct net_device *dev)
  722. {
  723. struct ip6_tnl *t = netdev_priv(dev);
  724. const struct iphdr *iph = ip_hdr(skb);
  725. int encap_limit = -1;
  726. struct flowi6 fl6;
  727. __u8 dsfield;
  728. __u32 mtu;
  729. int err;
  730. if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
  731. encap_limit = t->parms.encap_limit;
  732. memcpy(&fl6, &t->fl.u.ip6, sizeof(fl6));
  733. fl6.flowi6_proto = IPPROTO_IPIP;
  734. dsfield = ipv4_get_dsfield(iph);
  735. if (t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS)
  736. fl6.flowlabel |= htonl((__u32)iph->tos << IPV6_TCLASS_SHIFT)
  737. & IPV6_TCLASS_MASK;
  738. if (t->parms.flags & IP6_TNL_F_USE_ORIG_FWMARK)
  739. fl6.flowi6_mark = skb->mark;
  740. err = ip6gre_xmit2(skb, dev, dsfield, &fl6, encap_limit, &mtu);
  741. if (err != 0) {
  742. /* XXX: send ICMP error even if DF is not set. */
  743. if (err == -EMSGSIZE)
  744. icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED,
  745. htonl(mtu));
  746. return -1;
  747. }
  748. return 0;
  749. }
  750. static inline int ip6gre_xmit_ipv6(struct sk_buff *skb, struct net_device *dev)
  751. {
  752. struct ip6_tnl *t = netdev_priv(dev);
  753. struct ipv6hdr *ipv6h = ipv6_hdr(skb);
  754. int encap_limit = -1;
  755. __u16 offset;
  756. struct flowi6 fl6;
  757. __u8 dsfield;
  758. __u32 mtu;
  759. int err;
  760. if (ipv6_addr_equal(&t->parms.raddr, &ipv6h->saddr))
  761. return -1;
  762. offset = ip6_tnl_parse_tlv_enc_lim(skb, skb_network_header(skb));
  763. if (offset > 0) {
  764. struct ipv6_tlv_tnl_enc_lim *tel;
  765. tel = (struct ipv6_tlv_tnl_enc_lim *)&skb_network_header(skb)[offset];
  766. if (tel->encap_limit == 0) {
  767. icmpv6_send(skb, ICMPV6_PARAMPROB,
  768. ICMPV6_HDR_FIELD, offset + 2);
  769. return -1;
  770. }
  771. encap_limit = tel->encap_limit - 1;
  772. } else if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
  773. encap_limit = t->parms.encap_limit;
  774. memcpy(&fl6, &t->fl.u.ip6, sizeof(fl6));
  775. fl6.flowi6_proto = IPPROTO_IPV6;
  776. dsfield = ipv6_get_dsfield(ipv6h);
  777. if (t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS)
  778. fl6.flowlabel |= (*(__be32 *) ipv6h & IPV6_TCLASS_MASK);
  779. if (t->parms.flags & IP6_TNL_F_USE_ORIG_FLOWLABEL)
  780. fl6.flowlabel |= (*(__be32 *) ipv6h & IPV6_FLOWLABEL_MASK);
  781. if (t->parms.flags & IP6_TNL_F_USE_ORIG_FWMARK)
  782. fl6.flowi6_mark = skb->mark;
  783. err = ip6gre_xmit2(skb, dev, dsfield, &fl6, encap_limit, &mtu);
  784. if (err != 0) {
  785. if (err == -EMSGSIZE)
  786. icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
  787. return -1;
  788. }
  789. return 0;
  790. }
  791. /**
  792. * ip6_tnl_addr_conflict - compare packet addresses to tunnel's own
  793. * @t: the outgoing tunnel device
  794. * @hdr: IPv6 header from the incoming packet
  795. *
  796. * Description:
  797. * Avoid trivial tunneling loop by checking that tunnel exit-point
  798. * doesn't match source of incoming packet.
  799. *
  800. * Return:
  801. * 1 if conflict,
  802. * 0 else
  803. **/
  804. static inline bool ip6gre_tnl_addr_conflict(const struct ip6_tnl *t,
  805. const struct ipv6hdr *hdr)
  806. {
  807. return ipv6_addr_equal(&t->parms.raddr, &hdr->saddr);
  808. }
  809. static int ip6gre_xmit_other(struct sk_buff *skb, struct net_device *dev)
  810. {
  811. struct ip6_tnl *t = netdev_priv(dev);
  812. int encap_limit = -1;
  813. struct flowi6 fl6;
  814. __u32 mtu;
  815. int err;
  816. if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
  817. encap_limit = t->parms.encap_limit;
  818. memcpy(&fl6, &t->fl.u.ip6, sizeof(fl6));
  819. fl6.flowi6_proto = skb->protocol;
  820. err = ip6gre_xmit2(skb, dev, 0, &fl6, encap_limit, &mtu);
  821. return err;
  822. }
  823. static netdev_tx_t ip6gre_tunnel_xmit(struct sk_buff *skb,
  824. struct net_device *dev)
  825. {
  826. struct ip6_tnl *t = netdev_priv(dev);
  827. struct net_device_stats *stats = &t->dev->stats;
  828. int ret;
  829. if (!ip6_tnl_xmit_ctl(t))
  830. return -1;
  831. switch (skb->protocol) {
  832. case htons(ETH_P_IP):
  833. ret = ip6gre_xmit_ipv4(skb, dev);
  834. break;
  835. case htons(ETH_P_IPV6):
  836. ret = ip6gre_xmit_ipv6(skb, dev);
  837. break;
  838. default:
  839. ret = ip6gre_xmit_other(skb, dev);
  840. break;
  841. }
  842. if (ret < 0)
  843. goto tx_err;
  844. return NETDEV_TX_OK;
  845. tx_err:
  846. stats->tx_errors++;
  847. stats->tx_dropped++;
  848. kfree_skb(skb);
  849. return NETDEV_TX_OK;
  850. }
  851. static void ip6gre_tnl_link_config(struct ip6_tnl *t, int set_mtu)
  852. {
  853. struct net_device *dev = t->dev;
  854. struct __ip6_tnl_parm *p = &t->parms;
  855. struct flowi6 *fl6 = &t->fl.u.ip6;
  856. int addend = sizeof(struct ipv6hdr) + 4;
  857. if (dev->type != ARPHRD_ETHER) {
  858. memcpy(dev->dev_addr, &p->laddr, sizeof(struct in6_addr));
  859. memcpy(dev->broadcast, &p->raddr, sizeof(struct in6_addr));
  860. }
  861. /* Set up flowi template */
  862. fl6->saddr = p->laddr;
  863. fl6->daddr = p->raddr;
  864. fl6->flowi6_oif = p->link;
  865. fl6->flowlabel = 0;
  866. if (!(p->flags&IP6_TNL_F_USE_ORIG_TCLASS))
  867. fl6->flowlabel |= IPV6_TCLASS_MASK & p->flowinfo;
  868. if (!(p->flags&IP6_TNL_F_USE_ORIG_FLOWLABEL))
  869. fl6->flowlabel |= IPV6_FLOWLABEL_MASK & p->flowinfo;
  870. p->flags &= ~(IP6_TNL_F_CAP_XMIT|IP6_TNL_F_CAP_RCV|IP6_TNL_F_CAP_PER_PACKET);
  871. p->flags |= ip6_tnl_get_cap(t, &p->laddr, &p->raddr);
  872. if (p->flags&IP6_TNL_F_CAP_XMIT &&
  873. p->flags&IP6_TNL_F_CAP_RCV && dev->type != ARPHRD_ETHER)
  874. dev->flags |= IFF_POINTOPOINT;
  875. else
  876. dev->flags &= ~IFF_POINTOPOINT;
  877. dev->iflink = p->link;
  878. /* Precalculate GRE options length */
  879. if (t->parms.o_flags&(GRE_CSUM|GRE_KEY|GRE_SEQ)) {
  880. if (t->parms.o_flags&GRE_CSUM)
  881. addend += 4;
  882. if (t->parms.o_flags&GRE_KEY)
  883. addend += 4;
  884. if (t->parms.o_flags&GRE_SEQ)
  885. addend += 4;
  886. }
  887. if (p->flags & IP6_TNL_F_CAP_XMIT) {
  888. int strict = (ipv6_addr_type(&p->raddr) &
  889. (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL));
  890. struct rt6_info *rt = rt6_lookup(dev_net(dev),
  891. &p->raddr, &p->laddr,
  892. p->link, strict);
  893. if (rt == NULL)
  894. return;
  895. if (rt->dst.dev) {
  896. dev->hard_header_len = rt->dst.dev->hard_header_len + addend;
  897. if (set_mtu) {
  898. dev->mtu = rt->dst.dev->mtu - addend;
  899. if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
  900. dev->mtu -= 8;
  901. if (dev->mtu < IPV6_MIN_MTU)
  902. dev->mtu = IPV6_MIN_MTU;
  903. }
  904. }
  905. dst_release(&rt->dst);
  906. }
  907. t->hlen = addend;
  908. }
  909. static int ip6gre_tnl_change(struct ip6_tnl *t,
  910. const struct __ip6_tnl_parm *p, int set_mtu)
  911. {
  912. t->parms.laddr = p->laddr;
  913. t->parms.raddr = p->raddr;
  914. t->parms.flags = p->flags;
  915. t->parms.hop_limit = p->hop_limit;
  916. t->parms.encap_limit = p->encap_limit;
  917. t->parms.flowinfo = p->flowinfo;
  918. t->parms.link = p->link;
  919. t->parms.proto = p->proto;
  920. t->parms.i_key = p->i_key;
  921. t->parms.o_key = p->o_key;
  922. t->parms.i_flags = p->i_flags;
  923. t->parms.o_flags = p->o_flags;
  924. ip6_tnl_dst_reset(t);
  925. ip6gre_tnl_link_config(t, set_mtu);
  926. return 0;
  927. }
  928. static void ip6gre_tnl_parm_from_user(struct __ip6_tnl_parm *p,
  929. const struct ip6_tnl_parm2 *u)
  930. {
  931. p->laddr = u->laddr;
  932. p->raddr = u->raddr;
  933. p->flags = u->flags;
  934. p->hop_limit = u->hop_limit;
  935. p->encap_limit = u->encap_limit;
  936. p->flowinfo = u->flowinfo;
  937. p->link = u->link;
  938. p->i_key = u->i_key;
  939. p->o_key = u->o_key;
  940. p->i_flags = u->i_flags;
  941. p->o_flags = u->o_flags;
  942. memcpy(p->name, u->name, sizeof(u->name));
  943. }
  944. static void ip6gre_tnl_parm_to_user(struct ip6_tnl_parm2 *u,
  945. const struct __ip6_tnl_parm *p)
  946. {
  947. u->proto = IPPROTO_GRE;
  948. u->laddr = p->laddr;
  949. u->raddr = p->raddr;
  950. u->flags = p->flags;
  951. u->hop_limit = p->hop_limit;
  952. u->encap_limit = p->encap_limit;
  953. u->flowinfo = p->flowinfo;
  954. u->link = p->link;
  955. u->i_key = p->i_key;
  956. u->o_key = p->o_key;
  957. u->i_flags = p->i_flags;
  958. u->o_flags = p->o_flags;
  959. memcpy(u->name, p->name, sizeof(u->name));
  960. }
  961. static int ip6gre_tunnel_ioctl(struct net_device *dev,
  962. struct ifreq *ifr, int cmd)
  963. {
  964. int err = 0;
  965. struct ip6_tnl_parm2 p;
  966. struct __ip6_tnl_parm p1;
  967. struct ip6_tnl *t;
  968. struct net *net = dev_net(dev);
  969. struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
  970. switch (cmd) {
  971. case SIOCGETTUNNEL:
  972. t = NULL;
  973. if (dev == ign->fb_tunnel_dev) {
  974. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) {
  975. err = -EFAULT;
  976. break;
  977. }
  978. ip6gre_tnl_parm_from_user(&p1, &p);
  979. t = ip6gre_tunnel_locate(net, &p1, 0);
  980. }
  981. if (t == NULL)
  982. t = netdev_priv(dev);
  983. ip6gre_tnl_parm_to_user(&p, &t->parms);
  984. if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p)))
  985. err = -EFAULT;
  986. break;
  987. case SIOCADDTUNNEL:
  988. case SIOCCHGTUNNEL:
  989. err = -EPERM;
  990. if (!capable(CAP_NET_ADMIN))
  991. goto done;
  992. err = -EFAULT;
  993. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
  994. goto done;
  995. err = -EINVAL;
  996. if ((p.i_flags|p.o_flags)&(GRE_VERSION|GRE_ROUTING))
  997. goto done;
  998. if (!(p.i_flags&GRE_KEY))
  999. p.i_key = 0;
  1000. if (!(p.o_flags&GRE_KEY))
  1001. p.o_key = 0;
  1002. ip6gre_tnl_parm_from_user(&p1, &p);
  1003. t = ip6gre_tunnel_locate(net, &p1, cmd == SIOCADDTUNNEL);
  1004. if (dev != ign->fb_tunnel_dev && cmd == SIOCCHGTUNNEL) {
  1005. if (t != NULL) {
  1006. if (t->dev != dev) {
  1007. err = -EEXIST;
  1008. break;
  1009. }
  1010. } else {
  1011. t = netdev_priv(dev);
  1012. ip6gre_tunnel_unlink(ign, t);
  1013. synchronize_net();
  1014. ip6gre_tnl_change(t, &p1, 1);
  1015. ip6gre_tunnel_link(ign, t);
  1016. netdev_state_change(dev);
  1017. }
  1018. }
  1019. if (t) {
  1020. err = 0;
  1021. ip6gre_tnl_parm_to_user(&p, &t->parms);
  1022. if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p)))
  1023. err = -EFAULT;
  1024. } else
  1025. err = (cmd == SIOCADDTUNNEL ? -ENOBUFS : -ENOENT);
  1026. break;
  1027. case SIOCDELTUNNEL:
  1028. err = -EPERM;
  1029. if (!capable(CAP_NET_ADMIN))
  1030. goto done;
  1031. if (dev == ign->fb_tunnel_dev) {
  1032. err = -EFAULT;
  1033. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
  1034. goto done;
  1035. err = -ENOENT;
  1036. ip6gre_tnl_parm_from_user(&p1, &p);
  1037. t = ip6gre_tunnel_locate(net, &p1, 0);
  1038. if (t == NULL)
  1039. goto done;
  1040. err = -EPERM;
  1041. if (t == netdev_priv(ign->fb_tunnel_dev))
  1042. goto done;
  1043. dev = t->dev;
  1044. }
  1045. unregister_netdevice(dev);
  1046. err = 0;
  1047. break;
  1048. default:
  1049. err = -EINVAL;
  1050. }
  1051. done:
  1052. return err;
  1053. }
  1054. static int ip6gre_tunnel_change_mtu(struct net_device *dev, int new_mtu)
  1055. {
  1056. struct ip6_tnl *tunnel = netdev_priv(dev);
  1057. if (new_mtu < 68 ||
  1058. new_mtu > 0xFFF8 - dev->hard_header_len - tunnel->hlen)
  1059. return -EINVAL;
  1060. dev->mtu = new_mtu;
  1061. return 0;
  1062. }
  1063. static int ip6gre_header(struct sk_buff *skb, struct net_device *dev,
  1064. unsigned short type,
  1065. const void *daddr, const void *saddr, unsigned int len)
  1066. {
  1067. struct ip6_tnl *t = netdev_priv(dev);
  1068. struct ipv6hdr *ipv6h = (struct ipv6hdr *)skb_push(skb, t->hlen);
  1069. __be16 *p = (__be16 *)(ipv6h+1);
  1070. *(__be32 *)ipv6h = t->fl.u.ip6.flowlabel | htonl(0x60000000);
  1071. ipv6h->hop_limit = t->parms.hop_limit;
  1072. ipv6h->nexthdr = NEXTHDR_GRE;
  1073. ipv6h->saddr = t->parms.laddr;
  1074. ipv6h->daddr = t->parms.raddr;
  1075. p[0] = t->parms.o_flags;
  1076. p[1] = htons(type);
  1077. /*
  1078. * Set the source hardware address.
  1079. */
  1080. if (saddr)
  1081. memcpy(&ipv6h->saddr, saddr, sizeof(struct in6_addr));
  1082. if (daddr)
  1083. memcpy(&ipv6h->daddr, daddr, sizeof(struct in6_addr));
  1084. if (!ipv6_addr_any(&ipv6h->daddr))
  1085. return t->hlen;
  1086. return -t->hlen;
  1087. }
  1088. static int ip6gre_header_parse(const struct sk_buff *skb, unsigned char *haddr)
  1089. {
  1090. const struct ipv6hdr *ipv6h = (const struct ipv6hdr *)skb_mac_header(skb);
  1091. memcpy(haddr, &ipv6h->saddr, sizeof(struct in6_addr));
  1092. return sizeof(struct in6_addr);
  1093. }
  1094. static const struct header_ops ip6gre_header_ops = {
  1095. .create = ip6gre_header,
  1096. .parse = ip6gre_header_parse,
  1097. };
  1098. static const struct net_device_ops ip6gre_netdev_ops = {
  1099. .ndo_init = ip6gre_tunnel_init,
  1100. .ndo_uninit = ip6gre_tunnel_uninit,
  1101. .ndo_start_xmit = ip6gre_tunnel_xmit,
  1102. .ndo_do_ioctl = ip6gre_tunnel_ioctl,
  1103. .ndo_change_mtu = ip6gre_tunnel_change_mtu,
  1104. .ndo_get_stats64 = ip6gre_get_stats64,
  1105. };
  1106. static void ip6gre_dev_free(struct net_device *dev)
  1107. {
  1108. free_percpu(dev->tstats);
  1109. free_netdev(dev);
  1110. }
  1111. static void ip6gre_tunnel_setup(struct net_device *dev)
  1112. {
  1113. struct ip6_tnl *t;
  1114. dev->netdev_ops = &ip6gre_netdev_ops;
  1115. dev->destructor = ip6gre_dev_free;
  1116. dev->type = ARPHRD_IP6GRE;
  1117. dev->hard_header_len = LL_MAX_HEADER + sizeof(struct ipv6hdr) + 4;
  1118. dev->mtu = ETH_DATA_LEN - sizeof(struct ipv6hdr) - 4;
  1119. t = netdev_priv(dev);
  1120. if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
  1121. dev->mtu -= 8;
  1122. dev->flags |= IFF_NOARP;
  1123. dev->iflink = 0;
  1124. dev->addr_len = sizeof(struct in6_addr);
  1125. dev->features |= NETIF_F_NETNS_LOCAL;
  1126. dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
  1127. }
  1128. static int ip6gre_tunnel_init(struct net_device *dev)
  1129. {
  1130. struct ip6_tnl *tunnel;
  1131. tunnel = netdev_priv(dev);
  1132. tunnel->dev = dev;
  1133. strcpy(tunnel->parms.name, dev->name);
  1134. memcpy(dev->dev_addr, &tunnel->parms.laddr, sizeof(struct in6_addr));
  1135. memcpy(dev->broadcast, &tunnel->parms.raddr, sizeof(struct in6_addr));
  1136. if (ipv6_addr_any(&tunnel->parms.raddr))
  1137. dev->header_ops = &ip6gre_header_ops;
  1138. dev->tstats = alloc_percpu(struct pcpu_tstats);
  1139. if (!dev->tstats)
  1140. return -ENOMEM;
  1141. return 0;
  1142. }
  1143. static void ip6gre_fb_tunnel_init(struct net_device *dev)
  1144. {
  1145. struct ip6_tnl *tunnel = netdev_priv(dev);
  1146. tunnel->dev = dev;
  1147. strcpy(tunnel->parms.name, dev->name);
  1148. tunnel->hlen = sizeof(struct ipv6hdr) + 4;
  1149. dev_hold(dev);
  1150. }
  1151. static struct inet6_protocol ip6gre_protocol __read_mostly = {
  1152. .handler = ip6gre_rcv,
  1153. .err_handler = ip6gre_err,
  1154. .flags = INET6_PROTO_NOPOLICY|INET6_PROTO_FINAL,
  1155. };
  1156. static void ip6gre_destroy_tunnels(struct ip6gre_net *ign,
  1157. struct list_head *head)
  1158. {
  1159. int prio;
  1160. for (prio = 0; prio < 4; prio++) {
  1161. int h;
  1162. for (h = 0; h < HASH_SIZE; h++) {
  1163. struct ip6_tnl *t;
  1164. t = rtnl_dereference(ign->tunnels[prio][h]);
  1165. while (t != NULL) {
  1166. unregister_netdevice_queue(t->dev, head);
  1167. t = rtnl_dereference(t->next);
  1168. }
  1169. }
  1170. }
  1171. }
  1172. static int __net_init ip6gre_init_net(struct net *net)
  1173. {
  1174. struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
  1175. int err;
  1176. ign->fb_tunnel_dev = alloc_netdev(sizeof(struct ip6_tnl), "ip6gre0",
  1177. ip6gre_tunnel_setup);
  1178. if (!ign->fb_tunnel_dev) {
  1179. err = -ENOMEM;
  1180. goto err_alloc_dev;
  1181. }
  1182. dev_net_set(ign->fb_tunnel_dev, net);
  1183. ip6gre_fb_tunnel_init(ign->fb_tunnel_dev);
  1184. ign->fb_tunnel_dev->rtnl_link_ops = &ip6gre_link_ops;
  1185. err = register_netdev(ign->fb_tunnel_dev);
  1186. if (err)
  1187. goto err_reg_dev;
  1188. rcu_assign_pointer(ign->tunnels_wc[0],
  1189. netdev_priv(ign->fb_tunnel_dev));
  1190. return 0;
  1191. err_reg_dev:
  1192. ip6gre_dev_free(ign->fb_tunnel_dev);
  1193. err_alloc_dev:
  1194. return err;
  1195. }
  1196. static void __net_exit ip6gre_exit_net(struct net *net)
  1197. {
  1198. struct ip6gre_net *ign;
  1199. LIST_HEAD(list);
  1200. ign = net_generic(net, ip6gre_net_id);
  1201. rtnl_lock();
  1202. ip6gre_destroy_tunnels(ign, &list);
  1203. unregister_netdevice_many(&list);
  1204. rtnl_unlock();
  1205. }
  1206. static struct pernet_operations ip6gre_net_ops = {
  1207. .init = ip6gre_init_net,
  1208. .exit = ip6gre_exit_net,
  1209. .id = &ip6gre_net_id,
  1210. .size = sizeof(struct ip6gre_net),
  1211. };
  1212. static int ip6gre_tunnel_validate(struct nlattr *tb[], struct nlattr *data[])
  1213. {
  1214. __be16 flags;
  1215. if (!data)
  1216. return 0;
  1217. flags = 0;
  1218. if (data[IFLA_GRE_IFLAGS])
  1219. flags |= nla_get_be16(data[IFLA_GRE_IFLAGS]);
  1220. if (data[IFLA_GRE_OFLAGS])
  1221. flags |= nla_get_be16(data[IFLA_GRE_OFLAGS]);
  1222. if (flags & (GRE_VERSION|GRE_ROUTING))
  1223. return -EINVAL;
  1224. return 0;
  1225. }
  1226. static int ip6gre_tap_validate(struct nlattr *tb[], struct nlattr *data[])
  1227. {
  1228. struct in6_addr daddr;
  1229. if (tb[IFLA_ADDRESS]) {
  1230. if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
  1231. return -EINVAL;
  1232. if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
  1233. return -EADDRNOTAVAIL;
  1234. }
  1235. if (!data)
  1236. goto out;
  1237. if (data[IFLA_GRE_REMOTE]) {
  1238. nla_memcpy(&daddr, data[IFLA_GRE_REMOTE], sizeof(struct in6_addr));
  1239. if (ipv6_addr_any(&daddr))
  1240. return -EINVAL;
  1241. }
  1242. out:
  1243. return ip6gre_tunnel_validate(tb, data);
  1244. }
  1245. static void ip6gre_netlink_parms(struct nlattr *data[],
  1246. struct __ip6_tnl_parm *parms)
  1247. {
  1248. memset(parms, 0, sizeof(*parms));
  1249. if (!data)
  1250. return;
  1251. if (data[IFLA_GRE_LINK])
  1252. parms->link = nla_get_u32(data[IFLA_GRE_LINK]);
  1253. if (data[IFLA_GRE_IFLAGS])
  1254. parms->i_flags = nla_get_be16(data[IFLA_GRE_IFLAGS]);
  1255. if (data[IFLA_GRE_OFLAGS])
  1256. parms->o_flags = nla_get_be16(data[IFLA_GRE_OFLAGS]);
  1257. if (data[IFLA_GRE_IKEY])
  1258. parms->i_key = nla_get_be32(data[IFLA_GRE_IKEY]);
  1259. if (data[IFLA_GRE_OKEY])
  1260. parms->o_key = nla_get_be32(data[IFLA_GRE_OKEY]);
  1261. if (data[IFLA_GRE_LOCAL])
  1262. nla_memcpy(&parms->laddr, data[IFLA_GRE_LOCAL], sizeof(struct in6_addr));
  1263. if (data[IFLA_GRE_REMOTE])
  1264. nla_memcpy(&parms->raddr, data[IFLA_GRE_REMOTE], sizeof(struct in6_addr));
  1265. if (data[IFLA_GRE_TTL])
  1266. parms->hop_limit = nla_get_u8(data[IFLA_GRE_TTL]);
  1267. if (data[IFLA_GRE_ENCAP_LIMIT])
  1268. parms->encap_limit = nla_get_u8(data[IFLA_GRE_ENCAP_LIMIT]);
  1269. if (data[IFLA_GRE_FLOWINFO])
  1270. parms->flowinfo = nla_get_u32(data[IFLA_GRE_FLOWINFO]);
  1271. if (data[IFLA_GRE_FLAGS])
  1272. parms->flags = nla_get_u32(data[IFLA_GRE_FLAGS]);
  1273. }
  1274. static int ip6gre_tap_init(struct net_device *dev)
  1275. {
  1276. struct ip6_tnl *tunnel;
  1277. tunnel = netdev_priv(dev);
  1278. tunnel->dev = dev;
  1279. strcpy(tunnel->parms.name, dev->name);
  1280. ip6gre_tnl_link_config(tunnel, 1);
  1281. dev->tstats = alloc_percpu(struct pcpu_tstats);
  1282. if (!dev->tstats)
  1283. return -ENOMEM;
  1284. return 0;
  1285. }
  1286. static const struct net_device_ops ip6gre_tap_netdev_ops = {
  1287. .ndo_init = ip6gre_tap_init,
  1288. .ndo_uninit = ip6gre_tunnel_uninit,
  1289. .ndo_start_xmit = ip6gre_tunnel_xmit,
  1290. .ndo_set_mac_address = eth_mac_addr,
  1291. .ndo_validate_addr = eth_validate_addr,
  1292. .ndo_change_mtu = ip6gre_tunnel_change_mtu,
  1293. .ndo_get_stats64 = ip6gre_get_stats64,
  1294. };
  1295. static void ip6gre_tap_setup(struct net_device *dev)
  1296. {
  1297. ether_setup(dev);
  1298. dev->netdev_ops = &ip6gre_tap_netdev_ops;
  1299. dev->destructor = ip6gre_dev_free;
  1300. dev->iflink = 0;
  1301. dev->features |= NETIF_F_NETNS_LOCAL;
  1302. }
  1303. static int ip6gre_newlink(struct net *src_net, struct net_device *dev,
  1304. struct nlattr *tb[], struct nlattr *data[])
  1305. {
  1306. struct ip6_tnl *nt;
  1307. struct net *net = dev_net(dev);
  1308. struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
  1309. int err;
  1310. nt = netdev_priv(dev);
  1311. ip6gre_netlink_parms(data, &nt->parms);
  1312. if (ip6gre_tunnel_find(net, &nt->parms, dev->type))
  1313. return -EEXIST;
  1314. if (dev->type == ARPHRD_ETHER && !tb[IFLA_ADDRESS])
  1315. eth_hw_addr_random(dev);
  1316. nt->dev = dev;
  1317. ip6gre_tnl_link_config(nt, !tb[IFLA_MTU]);
  1318. /* Can use a lockless transmit, unless we generate output sequences */
  1319. if (!(nt->parms.o_flags & GRE_SEQ))
  1320. dev->features |= NETIF_F_LLTX;
  1321. err = register_netdevice(dev);
  1322. if (err)
  1323. goto out;
  1324. dev_hold(dev);
  1325. ip6gre_tunnel_link(ign, nt);
  1326. out:
  1327. return err;
  1328. }
  1329. static int ip6gre_changelink(struct net_device *dev, struct nlattr *tb[],
  1330. struct nlattr *data[])
  1331. {
  1332. struct ip6_tnl *t, *nt;
  1333. struct net *net = dev_net(dev);
  1334. struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
  1335. struct __ip6_tnl_parm p;
  1336. if (dev == ign->fb_tunnel_dev)
  1337. return -EINVAL;
  1338. nt = netdev_priv(dev);
  1339. ip6gre_netlink_parms(data, &p);
  1340. t = ip6gre_tunnel_locate(net, &p, 0);
  1341. if (t) {
  1342. if (t->dev != dev)
  1343. return -EEXIST;
  1344. } else {
  1345. t = nt;
  1346. ip6gre_tunnel_unlink(ign, t);
  1347. ip6gre_tnl_change(t, &p, !tb[IFLA_MTU]);
  1348. ip6gre_tunnel_link(ign, t);
  1349. netdev_state_change(dev);
  1350. }
  1351. return 0;
  1352. }
  1353. static size_t ip6gre_get_size(const struct net_device *dev)
  1354. {
  1355. return
  1356. /* IFLA_GRE_LINK */
  1357. nla_total_size(4) +
  1358. /* IFLA_GRE_IFLAGS */
  1359. nla_total_size(2) +
  1360. /* IFLA_GRE_OFLAGS */
  1361. nla_total_size(2) +
  1362. /* IFLA_GRE_IKEY */
  1363. nla_total_size(4) +
  1364. /* IFLA_GRE_OKEY */
  1365. nla_total_size(4) +
  1366. /* IFLA_GRE_LOCAL */
  1367. nla_total_size(4) +
  1368. /* IFLA_GRE_REMOTE */
  1369. nla_total_size(4) +
  1370. /* IFLA_GRE_TTL */
  1371. nla_total_size(1) +
  1372. /* IFLA_GRE_TOS */
  1373. nla_total_size(1) +
  1374. /* IFLA_GRE_ENCAP_LIMIT */
  1375. nla_total_size(1) +
  1376. /* IFLA_GRE_FLOWINFO */
  1377. nla_total_size(4) +
  1378. /* IFLA_GRE_FLAGS */
  1379. nla_total_size(4) +
  1380. 0;
  1381. }
  1382. static int ip6gre_fill_info(struct sk_buff *skb, const struct net_device *dev)
  1383. {
  1384. struct ip6_tnl *t = netdev_priv(dev);
  1385. struct __ip6_tnl_parm *p = &t->parms;
  1386. if (nla_put_u32(skb, IFLA_GRE_LINK, p->link) ||
  1387. nla_put_be16(skb, IFLA_GRE_IFLAGS, p->i_flags) ||
  1388. nla_put_be16(skb, IFLA_GRE_OFLAGS, p->o_flags) ||
  1389. nla_put_be32(skb, IFLA_GRE_IKEY, p->i_key) ||
  1390. nla_put_be32(skb, IFLA_GRE_OKEY, p->o_key) ||
  1391. nla_put(skb, IFLA_GRE_LOCAL, sizeof(struct in6_addr), &p->raddr) ||
  1392. nla_put(skb, IFLA_GRE_REMOTE, sizeof(struct in6_addr), &p->laddr) ||
  1393. nla_put_u8(skb, IFLA_GRE_TTL, p->hop_limit) ||
  1394. /*nla_put_u8(skb, IFLA_GRE_TOS, t->priority) ||*/
  1395. nla_put_u8(skb, IFLA_GRE_ENCAP_LIMIT, p->encap_limit) ||
  1396. nla_put_be32(skb, IFLA_GRE_FLOWINFO, p->flowinfo) ||
  1397. nla_put_u32(skb, IFLA_GRE_FLAGS, p->flags))
  1398. goto nla_put_failure;
  1399. return 0;
  1400. nla_put_failure:
  1401. return -EMSGSIZE;
  1402. }
  1403. static const struct nla_policy ip6gre_policy[IFLA_GRE_MAX + 1] = {
  1404. [IFLA_GRE_LINK] = { .type = NLA_U32 },
  1405. [IFLA_GRE_IFLAGS] = { .type = NLA_U16 },
  1406. [IFLA_GRE_OFLAGS] = { .type = NLA_U16 },
  1407. [IFLA_GRE_IKEY] = { .type = NLA_U32 },
  1408. [IFLA_GRE_OKEY] = { .type = NLA_U32 },
  1409. [IFLA_GRE_LOCAL] = { .len = FIELD_SIZEOF(struct ipv6hdr, saddr) },
  1410. [IFLA_GRE_REMOTE] = { .len = FIELD_SIZEOF(struct ipv6hdr, daddr) },
  1411. [IFLA_GRE_TTL] = { .type = NLA_U8 },
  1412. [IFLA_GRE_ENCAP_LIMIT] = { .type = NLA_U8 },
  1413. [IFLA_GRE_FLOWINFO] = { .type = NLA_U32 },
  1414. [IFLA_GRE_FLAGS] = { .type = NLA_U32 },
  1415. };
  1416. static struct rtnl_link_ops ip6gre_link_ops __read_mostly = {
  1417. .kind = "ip6gre",
  1418. .maxtype = IFLA_GRE_MAX,
  1419. .policy = ip6gre_policy,
  1420. .priv_size = sizeof(struct ip6_tnl),
  1421. .setup = ip6gre_tunnel_setup,
  1422. .validate = ip6gre_tunnel_validate,
  1423. .newlink = ip6gre_newlink,
  1424. .changelink = ip6gre_changelink,
  1425. .get_size = ip6gre_get_size,
  1426. .fill_info = ip6gre_fill_info,
  1427. };
  1428. static struct rtnl_link_ops ip6gre_tap_ops __read_mostly = {
  1429. .kind = "ip6gretap",
  1430. .maxtype = IFLA_GRE_MAX,
  1431. .policy = ip6gre_policy,
  1432. .priv_size = sizeof(struct ip6_tnl),
  1433. .setup = ip6gre_tap_setup,
  1434. .validate = ip6gre_tap_validate,
  1435. .newlink = ip6gre_newlink,
  1436. .changelink = ip6gre_changelink,
  1437. .get_size = ip6gre_get_size,
  1438. .fill_info = ip6gre_fill_info,
  1439. };
  1440. /*
  1441. * And now the modules code and kernel interface.
  1442. */
  1443. static int __init ip6gre_init(void)
  1444. {
  1445. int err;
  1446. pr_info("GRE over IPv6 tunneling driver\n");
  1447. err = register_pernet_device(&ip6gre_net_ops);
  1448. if (err < 0)
  1449. return err;
  1450. err = inet6_add_protocol(&ip6gre_protocol, IPPROTO_GRE);
  1451. if (err < 0) {
  1452. pr_info("%s: can't add protocol\n", __func__);
  1453. goto add_proto_failed;
  1454. }
  1455. err = rtnl_link_register(&ip6gre_link_ops);
  1456. if (err < 0)
  1457. goto rtnl_link_failed;
  1458. err = rtnl_link_register(&ip6gre_tap_ops);
  1459. if (err < 0)
  1460. goto tap_ops_failed;
  1461. out:
  1462. return err;
  1463. tap_ops_failed:
  1464. rtnl_link_unregister(&ip6gre_link_ops);
  1465. rtnl_link_failed:
  1466. inet6_del_protocol(&ip6gre_protocol, IPPROTO_GRE);
  1467. add_proto_failed:
  1468. unregister_pernet_device(&ip6gre_net_ops);
  1469. goto out;
  1470. }
  1471. static void __exit ip6gre_fini(void)
  1472. {
  1473. rtnl_link_unregister(&ip6gre_tap_ops);
  1474. rtnl_link_unregister(&ip6gre_link_ops);
  1475. inet6_del_protocol(&ip6gre_protocol, IPPROTO_GRE);
  1476. unregister_pernet_device(&ip6gre_net_ops);
  1477. }
  1478. module_init(ip6gre_init);
  1479. module_exit(ip6gre_fini);
  1480. MODULE_LICENSE("GPL");
  1481. MODULE_AUTHOR("D. Kozlov (xeb@mail.ru)");
  1482. MODULE_DESCRIPTION("GRE over IPv6 tunneling device");
  1483. MODULE_ALIAS_RTNL_LINK("ip6gre");
  1484. MODULE_ALIAS_NETDEV("ip6gre0");