ip6_gre.c 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784
  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 *)(skb->data + offset);
  346. int grehlen = offset + 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 (!pskb_may_pull(skb, grehlen))
  361. return;
  362. ipv6h = (const struct ipv6hdr *)skb->data;
  363. p = (__be16 *)(skb->data + offset);
  364. t = ip6gre_tunnel_lookup(skb->dev, &ipv6h->daddr, &ipv6h->saddr,
  365. flags & GRE_KEY ?
  366. *(((__be32 *)p) + (grehlen / 4) - 1) : 0,
  367. p[1]);
  368. if (t == NULL)
  369. return;
  370. switch (type) {
  371. __u32 teli;
  372. struct ipv6_tlv_tnl_enc_lim *tel;
  373. __u32 mtu;
  374. case ICMPV6_DEST_UNREACH:
  375. net_warn_ratelimited("%s: Path to destination invalid or inactive!\n",
  376. t->parms.name);
  377. break;
  378. case ICMPV6_TIME_EXCEED:
  379. if (code == ICMPV6_EXC_HOPLIMIT) {
  380. net_warn_ratelimited("%s: Too small hop limit or routing loop in tunnel!\n",
  381. t->parms.name);
  382. }
  383. break;
  384. case ICMPV6_PARAMPROB:
  385. teli = 0;
  386. if (code == ICMPV6_HDR_FIELD)
  387. teli = ip6_tnl_parse_tlv_enc_lim(skb, skb->data);
  388. if (teli && teli == info - 2) {
  389. tel = (struct ipv6_tlv_tnl_enc_lim *) &skb->data[teli];
  390. if (tel->encap_limit == 0) {
  391. net_warn_ratelimited("%s: Too small encapsulation limit or routing loop in tunnel!\n",
  392. t->parms.name);
  393. }
  394. } else {
  395. net_warn_ratelimited("%s: Recipient unable to parse tunneled packet!\n",
  396. t->parms.name);
  397. }
  398. break;
  399. case ICMPV6_PKT_TOOBIG:
  400. mtu = info - offset;
  401. if (mtu < IPV6_MIN_MTU)
  402. mtu = IPV6_MIN_MTU;
  403. t->dev->mtu = mtu;
  404. break;
  405. }
  406. if (time_before(jiffies, t->err_time + IP6TUNNEL_ERR_TIMEO))
  407. t->err_count++;
  408. else
  409. t->err_count = 1;
  410. t->err_time = jiffies;
  411. }
  412. static inline void ip6gre_ecn_decapsulate_ipv4(const struct ip6_tnl *t,
  413. const struct ipv6hdr *ipv6h, struct sk_buff *skb)
  414. {
  415. __u8 dsfield = ipv6_get_dsfield(ipv6h) & ~INET_ECN_MASK;
  416. if (t->parms.flags & IP6_TNL_F_RCV_DSCP_COPY)
  417. ipv4_change_dsfield(ip_hdr(skb), INET_ECN_MASK, dsfield);
  418. if (INET_ECN_is_ce(dsfield))
  419. IP_ECN_set_ce(ip_hdr(skb));
  420. }
  421. static inline void ip6gre_ecn_decapsulate_ipv6(const struct ip6_tnl *t,
  422. const struct ipv6hdr *ipv6h, struct sk_buff *skb)
  423. {
  424. if (t->parms.flags & IP6_TNL_F_RCV_DSCP_COPY)
  425. ipv6_copy_dscp(ipv6_get_dsfield(ipv6h), ipv6_hdr(skb));
  426. if (INET_ECN_is_ce(ipv6_get_dsfield(ipv6h)))
  427. IP6_ECN_set_ce(ipv6_hdr(skb));
  428. }
  429. static int ip6gre_rcv(struct sk_buff *skb)
  430. {
  431. const struct ipv6hdr *ipv6h;
  432. u8 *h;
  433. __be16 flags;
  434. __sum16 csum = 0;
  435. __be32 key = 0;
  436. u32 seqno = 0;
  437. struct ip6_tnl *tunnel;
  438. int offset = 4;
  439. __be16 gre_proto;
  440. if (!pskb_may_pull(skb, sizeof(struct in6_addr)))
  441. goto drop;
  442. ipv6h = ipv6_hdr(skb);
  443. h = skb->data;
  444. flags = *(__be16 *)h;
  445. if (flags&(GRE_CSUM|GRE_KEY|GRE_ROUTING|GRE_SEQ|GRE_VERSION)) {
  446. /* - Version must be 0.
  447. - We do not support routing headers.
  448. */
  449. if (flags&(GRE_VERSION|GRE_ROUTING))
  450. goto drop;
  451. if (flags&GRE_CSUM) {
  452. switch (skb->ip_summed) {
  453. case CHECKSUM_COMPLETE:
  454. csum = csum_fold(skb->csum);
  455. if (!csum)
  456. break;
  457. /* fall through */
  458. case CHECKSUM_NONE:
  459. skb->csum = 0;
  460. csum = __skb_checksum_complete(skb);
  461. skb->ip_summed = CHECKSUM_COMPLETE;
  462. }
  463. offset += 4;
  464. }
  465. if (flags&GRE_KEY) {
  466. key = *(__be32 *)(h + offset);
  467. offset += 4;
  468. }
  469. if (flags&GRE_SEQ) {
  470. seqno = ntohl(*(__be32 *)(h + offset));
  471. offset += 4;
  472. }
  473. }
  474. gre_proto = *(__be16 *)(h + 2);
  475. tunnel = ip6gre_tunnel_lookup(skb->dev,
  476. &ipv6h->saddr, &ipv6h->daddr, key,
  477. gre_proto);
  478. if (tunnel) {
  479. struct pcpu_tstats *tstats;
  480. if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb))
  481. goto drop;
  482. if (!ip6_tnl_rcv_ctl(tunnel, &ipv6h->daddr, &ipv6h->saddr)) {
  483. tunnel->dev->stats.rx_dropped++;
  484. goto drop;
  485. }
  486. secpath_reset(skb);
  487. skb->protocol = gre_proto;
  488. /* WCCP version 1 and 2 protocol decoding.
  489. * - Change protocol to IP
  490. * - When dealing with WCCPv2, Skip extra 4 bytes in GRE header
  491. */
  492. if (flags == 0 && gre_proto == htons(ETH_P_WCCP)) {
  493. skb->protocol = htons(ETH_P_IP);
  494. if ((*(h + offset) & 0xF0) != 0x40)
  495. offset += 4;
  496. }
  497. skb->mac_header = skb->network_header;
  498. __pskb_pull(skb, offset);
  499. skb_postpull_rcsum(skb, skb_transport_header(skb), offset);
  500. skb->pkt_type = PACKET_HOST;
  501. if (((flags&GRE_CSUM) && csum) ||
  502. (!(flags&GRE_CSUM) && tunnel->parms.i_flags&GRE_CSUM)) {
  503. tunnel->dev->stats.rx_crc_errors++;
  504. tunnel->dev->stats.rx_errors++;
  505. goto drop;
  506. }
  507. if (tunnel->parms.i_flags&GRE_SEQ) {
  508. if (!(flags&GRE_SEQ) ||
  509. (tunnel->i_seqno &&
  510. (s32)(seqno - tunnel->i_seqno) < 0)) {
  511. tunnel->dev->stats.rx_fifo_errors++;
  512. tunnel->dev->stats.rx_errors++;
  513. goto drop;
  514. }
  515. tunnel->i_seqno = seqno + 1;
  516. }
  517. /* Warning: All skb pointers will be invalidated! */
  518. if (tunnel->dev->type == ARPHRD_ETHER) {
  519. if (!pskb_may_pull(skb, ETH_HLEN)) {
  520. tunnel->dev->stats.rx_length_errors++;
  521. tunnel->dev->stats.rx_errors++;
  522. goto drop;
  523. }
  524. ipv6h = ipv6_hdr(skb);
  525. skb->protocol = eth_type_trans(skb, tunnel->dev);
  526. skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN);
  527. }
  528. tstats = this_cpu_ptr(tunnel->dev->tstats);
  529. u64_stats_update_begin(&tstats->syncp);
  530. tstats->rx_packets++;
  531. tstats->rx_bytes += skb->len;
  532. u64_stats_update_end(&tstats->syncp);
  533. __skb_tunnel_rx(skb, tunnel->dev);
  534. skb_reset_network_header(skb);
  535. if (skb->protocol == htons(ETH_P_IP))
  536. ip6gre_ecn_decapsulate_ipv4(tunnel, ipv6h, skb);
  537. else if (skb->protocol == htons(ETH_P_IPV6))
  538. ip6gre_ecn_decapsulate_ipv6(tunnel, ipv6h, skb);
  539. netif_rx(skb);
  540. return 0;
  541. }
  542. icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0);
  543. drop:
  544. kfree_skb(skb);
  545. return 0;
  546. }
  547. struct ipv6_tel_txoption {
  548. struct ipv6_txoptions ops;
  549. __u8 dst_opt[8];
  550. };
  551. static void init_tel_txopt(struct ipv6_tel_txoption *opt, __u8 encap_limit)
  552. {
  553. memset(opt, 0, sizeof(struct ipv6_tel_txoption));
  554. opt->dst_opt[2] = IPV6_TLV_TNL_ENCAP_LIMIT;
  555. opt->dst_opt[3] = 1;
  556. opt->dst_opt[4] = encap_limit;
  557. opt->dst_opt[5] = IPV6_TLV_PADN;
  558. opt->dst_opt[6] = 1;
  559. opt->ops.dst0opt = (struct ipv6_opt_hdr *) opt->dst_opt;
  560. opt->ops.opt_nflen = 8;
  561. }
  562. static netdev_tx_t ip6gre_xmit2(struct sk_buff *skb,
  563. struct net_device *dev,
  564. __u8 dsfield,
  565. struct flowi6 *fl6,
  566. int encap_limit,
  567. __u32 *pmtu)
  568. {
  569. struct net *net = dev_net(dev);
  570. struct ip6_tnl *tunnel = netdev_priv(dev);
  571. struct net_device *tdev; /* Device to other host */
  572. struct ipv6hdr *ipv6h; /* Our new IP header */
  573. unsigned int max_headroom; /* The extra header space needed */
  574. int gre_hlen;
  575. struct ipv6_tel_txoption opt;
  576. int mtu;
  577. struct dst_entry *dst = NULL, *ndst = NULL;
  578. struct net_device_stats *stats = &tunnel->dev->stats;
  579. int err = -1;
  580. u8 proto;
  581. int pkt_len;
  582. struct sk_buff *new_skb;
  583. if (dev->type == ARPHRD_ETHER)
  584. IPCB(skb)->flags = 0;
  585. if (dev->header_ops && dev->type == ARPHRD_IP6GRE) {
  586. gre_hlen = 0;
  587. ipv6h = (struct ipv6hdr *)skb->data;
  588. fl6->daddr = ipv6h->daddr;
  589. } else {
  590. gre_hlen = tunnel->hlen;
  591. fl6->daddr = tunnel->parms.raddr;
  592. }
  593. if (!fl6->flowi6_mark)
  594. dst = ip6_tnl_dst_check(tunnel);
  595. if (!dst) {
  596. ndst = ip6_route_output(net, NULL, fl6);
  597. if (ndst->error)
  598. goto tx_err_link_failure;
  599. ndst = xfrm_lookup(net, ndst, flowi6_to_flowi(fl6), NULL, 0);
  600. if (IS_ERR(ndst)) {
  601. err = PTR_ERR(ndst);
  602. ndst = NULL;
  603. goto tx_err_link_failure;
  604. }
  605. dst = ndst;
  606. }
  607. tdev = dst->dev;
  608. if (tdev == dev) {
  609. stats->collisions++;
  610. net_warn_ratelimited("%s: Local routing loop detected!\n",
  611. tunnel->parms.name);
  612. goto tx_err_dst_release;
  613. }
  614. mtu = dst_mtu(dst) - sizeof(*ipv6h);
  615. if (encap_limit >= 0) {
  616. max_headroom += 8;
  617. mtu -= 8;
  618. }
  619. if (mtu < IPV6_MIN_MTU)
  620. mtu = IPV6_MIN_MTU;
  621. if (skb_dst(skb))
  622. skb_dst(skb)->ops->update_pmtu(skb_dst(skb), NULL, skb, mtu);
  623. if (skb->len > mtu) {
  624. *pmtu = mtu;
  625. err = -EMSGSIZE;
  626. goto tx_err_dst_release;
  627. }
  628. if (tunnel->err_count > 0) {
  629. if (time_before(jiffies,
  630. tunnel->err_time + IP6TUNNEL_ERR_TIMEO)) {
  631. tunnel->err_count--;
  632. dst_link_failure(skb);
  633. } else
  634. tunnel->err_count = 0;
  635. }
  636. max_headroom = LL_RESERVED_SPACE(tdev) + gre_hlen + dst->header_len;
  637. if (skb_headroom(skb) < max_headroom || skb_shared(skb) ||
  638. (skb_cloned(skb) && !skb_clone_writable(skb, 0))) {
  639. new_skb = skb_realloc_headroom(skb, max_headroom);
  640. if (max_headroom > dev->needed_headroom)
  641. dev->needed_headroom = max_headroom;
  642. if (!new_skb)
  643. goto tx_err_dst_release;
  644. if (skb->sk)
  645. skb_set_owner_w(new_skb, skb->sk);
  646. consume_skb(skb);
  647. skb = new_skb;
  648. }
  649. skb_dst_drop(skb);
  650. if (fl6->flowi6_mark) {
  651. skb_dst_set(skb, dst);
  652. ndst = NULL;
  653. } else {
  654. skb_dst_set_noref(skb, dst);
  655. }
  656. skb->transport_header = skb->network_header;
  657. proto = NEXTHDR_GRE;
  658. if (encap_limit >= 0) {
  659. init_tel_txopt(&opt, encap_limit);
  660. ipv6_push_nfrag_opts(skb, &opt.ops, &proto, NULL);
  661. }
  662. skb_push(skb, gre_hlen);
  663. skb_reset_network_header(skb);
  664. /*
  665. * Push down and install the IP header.
  666. */
  667. ipv6h = ipv6_hdr(skb);
  668. *(__be32 *)ipv6h = fl6->flowlabel | htonl(0x60000000);
  669. dsfield = INET_ECN_encapsulate(0, dsfield);
  670. ipv6_change_dsfield(ipv6h, ~INET_ECN_MASK, dsfield);
  671. ipv6h->hop_limit = tunnel->parms.hop_limit;
  672. ipv6h->nexthdr = proto;
  673. ipv6h->saddr = fl6->saddr;
  674. ipv6h->daddr = fl6->daddr;
  675. ((__be16 *)(ipv6h + 1))[0] = tunnel->parms.o_flags;
  676. ((__be16 *)(ipv6h + 1))[1] = (dev->type == ARPHRD_ETHER) ?
  677. htons(ETH_P_TEB) : skb->protocol;
  678. if (tunnel->parms.o_flags&(GRE_KEY|GRE_CSUM|GRE_SEQ)) {
  679. __be32 *ptr = (__be32 *)(((u8 *)ipv6h) + tunnel->hlen - 4);
  680. if (tunnel->parms.o_flags&GRE_SEQ) {
  681. ++tunnel->o_seqno;
  682. *ptr = htonl(tunnel->o_seqno);
  683. ptr--;
  684. }
  685. if (tunnel->parms.o_flags&GRE_KEY) {
  686. *ptr = tunnel->parms.o_key;
  687. ptr--;
  688. }
  689. if (tunnel->parms.o_flags&GRE_CSUM) {
  690. *ptr = 0;
  691. *(__sum16 *)ptr = ip_compute_csum((void *)(ipv6h+1),
  692. skb->len - sizeof(struct ipv6hdr));
  693. }
  694. }
  695. nf_reset(skb);
  696. pkt_len = skb->len;
  697. err = ip6_local_out(skb);
  698. if (net_xmit_eval(err) == 0) {
  699. struct pcpu_tstats *tstats = this_cpu_ptr(tunnel->dev->tstats);
  700. tstats->tx_bytes += pkt_len;
  701. tstats->tx_packets++;
  702. } else {
  703. stats->tx_errors++;
  704. stats->tx_aborted_errors++;
  705. }
  706. if (ndst)
  707. ip6_tnl_dst_store(tunnel, ndst);
  708. return 0;
  709. tx_err_link_failure:
  710. stats->tx_carrier_errors++;
  711. dst_link_failure(skb);
  712. tx_err_dst_release:
  713. dst_release(ndst);
  714. return err;
  715. }
  716. static inline int ip6gre_xmit_ipv4(struct sk_buff *skb, struct net_device *dev)
  717. {
  718. struct ip6_tnl *t = netdev_priv(dev);
  719. const struct iphdr *iph = ip_hdr(skb);
  720. int encap_limit = -1;
  721. struct flowi6 fl6;
  722. __u8 dsfield;
  723. __u32 mtu;
  724. int err;
  725. if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
  726. encap_limit = t->parms.encap_limit;
  727. memcpy(&fl6, &t->fl.u.ip6, sizeof(fl6));
  728. fl6.flowi6_proto = IPPROTO_IPIP;
  729. dsfield = ipv4_get_dsfield(iph);
  730. if (t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS)
  731. fl6.flowlabel |= htonl((__u32)iph->tos << IPV6_TCLASS_SHIFT)
  732. & IPV6_TCLASS_MASK;
  733. if (t->parms.flags & IP6_TNL_F_USE_ORIG_FWMARK)
  734. fl6.flowi6_mark = skb->mark;
  735. err = ip6gre_xmit2(skb, dev, dsfield, &fl6, encap_limit, &mtu);
  736. if (err != 0) {
  737. /* XXX: send ICMP error even if DF is not set. */
  738. if (err == -EMSGSIZE)
  739. icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED,
  740. htonl(mtu));
  741. return -1;
  742. }
  743. return 0;
  744. }
  745. static inline int ip6gre_xmit_ipv6(struct sk_buff *skb, struct net_device *dev)
  746. {
  747. struct ip6_tnl *t = netdev_priv(dev);
  748. struct ipv6hdr *ipv6h = ipv6_hdr(skb);
  749. int encap_limit = -1;
  750. __u16 offset;
  751. struct flowi6 fl6;
  752. __u8 dsfield;
  753. __u32 mtu;
  754. int err;
  755. if (ipv6_addr_equal(&t->parms.raddr, &ipv6h->saddr))
  756. return -1;
  757. offset = ip6_tnl_parse_tlv_enc_lim(skb, skb_network_header(skb));
  758. if (offset > 0) {
  759. struct ipv6_tlv_tnl_enc_lim *tel;
  760. tel = (struct ipv6_tlv_tnl_enc_lim *)&skb_network_header(skb)[offset];
  761. if (tel->encap_limit == 0) {
  762. icmpv6_send(skb, ICMPV6_PARAMPROB,
  763. ICMPV6_HDR_FIELD, offset + 2);
  764. return -1;
  765. }
  766. encap_limit = tel->encap_limit - 1;
  767. } else if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
  768. encap_limit = t->parms.encap_limit;
  769. memcpy(&fl6, &t->fl.u.ip6, sizeof(fl6));
  770. fl6.flowi6_proto = IPPROTO_IPV6;
  771. dsfield = ipv6_get_dsfield(ipv6h);
  772. if (t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS)
  773. fl6.flowlabel |= (*(__be32 *) ipv6h & IPV6_TCLASS_MASK);
  774. if (t->parms.flags & IP6_TNL_F_USE_ORIG_FLOWLABEL)
  775. fl6.flowlabel |= (*(__be32 *) ipv6h & IPV6_FLOWLABEL_MASK);
  776. if (t->parms.flags & IP6_TNL_F_USE_ORIG_FWMARK)
  777. fl6.flowi6_mark = skb->mark;
  778. err = ip6gre_xmit2(skb, dev, dsfield, &fl6, encap_limit, &mtu);
  779. if (err != 0) {
  780. if (err == -EMSGSIZE)
  781. icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
  782. return -1;
  783. }
  784. return 0;
  785. }
  786. /**
  787. * ip6_tnl_addr_conflict - compare packet addresses to tunnel's own
  788. * @t: the outgoing tunnel device
  789. * @hdr: IPv6 header from the incoming packet
  790. *
  791. * Description:
  792. * Avoid trivial tunneling loop by checking that tunnel exit-point
  793. * doesn't match source of incoming packet.
  794. *
  795. * Return:
  796. * 1 if conflict,
  797. * 0 else
  798. **/
  799. static inline bool ip6gre_tnl_addr_conflict(const struct ip6_tnl *t,
  800. const struct ipv6hdr *hdr)
  801. {
  802. return ipv6_addr_equal(&t->parms.raddr, &hdr->saddr);
  803. }
  804. static int ip6gre_xmit_other(struct sk_buff *skb, struct net_device *dev)
  805. {
  806. struct ip6_tnl *t = netdev_priv(dev);
  807. int encap_limit = -1;
  808. struct flowi6 fl6;
  809. __u32 mtu;
  810. int err;
  811. if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
  812. encap_limit = t->parms.encap_limit;
  813. memcpy(&fl6, &t->fl.u.ip6, sizeof(fl6));
  814. fl6.flowi6_proto = skb->protocol;
  815. err = ip6gre_xmit2(skb, dev, 0, &fl6, encap_limit, &mtu);
  816. return err;
  817. }
  818. static netdev_tx_t ip6gre_tunnel_xmit(struct sk_buff *skb,
  819. struct net_device *dev)
  820. {
  821. struct ip6_tnl *t = netdev_priv(dev);
  822. struct net_device_stats *stats = &t->dev->stats;
  823. int ret;
  824. if (!ip6_tnl_xmit_ctl(t))
  825. return -1;
  826. switch (skb->protocol) {
  827. case htons(ETH_P_IP):
  828. ret = ip6gre_xmit_ipv4(skb, dev);
  829. break;
  830. case htons(ETH_P_IPV6):
  831. ret = ip6gre_xmit_ipv6(skb, dev);
  832. break;
  833. default:
  834. ret = ip6gre_xmit_other(skb, dev);
  835. break;
  836. }
  837. if (ret < 0)
  838. goto tx_err;
  839. return NETDEV_TX_OK;
  840. tx_err:
  841. stats->tx_errors++;
  842. stats->tx_dropped++;
  843. kfree_skb(skb);
  844. return NETDEV_TX_OK;
  845. }
  846. static void ip6gre_tnl_link_config(struct ip6_tnl *t, int set_mtu)
  847. {
  848. struct net_device *dev = t->dev;
  849. struct __ip6_tnl_parm *p = &t->parms;
  850. struct flowi6 *fl6 = &t->fl.u.ip6;
  851. int addend = sizeof(struct ipv6hdr) + 4;
  852. if (dev->type != ARPHRD_ETHER) {
  853. memcpy(dev->dev_addr, &p->laddr, sizeof(struct in6_addr));
  854. memcpy(dev->broadcast, &p->raddr, sizeof(struct in6_addr));
  855. }
  856. /* Set up flowi template */
  857. fl6->saddr = p->laddr;
  858. fl6->daddr = p->raddr;
  859. fl6->flowi6_oif = p->link;
  860. fl6->flowlabel = 0;
  861. if (!(p->flags&IP6_TNL_F_USE_ORIG_TCLASS))
  862. fl6->flowlabel |= IPV6_TCLASS_MASK & p->flowinfo;
  863. if (!(p->flags&IP6_TNL_F_USE_ORIG_FLOWLABEL))
  864. fl6->flowlabel |= IPV6_FLOWLABEL_MASK & p->flowinfo;
  865. p->flags &= ~(IP6_TNL_F_CAP_XMIT|IP6_TNL_F_CAP_RCV|IP6_TNL_F_CAP_PER_PACKET);
  866. p->flags |= ip6_tnl_get_cap(t, &p->laddr, &p->raddr);
  867. if (p->flags&IP6_TNL_F_CAP_XMIT &&
  868. p->flags&IP6_TNL_F_CAP_RCV && dev->type != ARPHRD_ETHER)
  869. dev->flags |= IFF_POINTOPOINT;
  870. else
  871. dev->flags &= ~IFF_POINTOPOINT;
  872. dev->iflink = p->link;
  873. /* Precalculate GRE options length */
  874. if (t->parms.o_flags&(GRE_CSUM|GRE_KEY|GRE_SEQ)) {
  875. if (t->parms.o_flags&GRE_CSUM)
  876. addend += 4;
  877. if (t->parms.o_flags&GRE_KEY)
  878. addend += 4;
  879. if (t->parms.o_flags&GRE_SEQ)
  880. addend += 4;
  881. }
  882. if (p->flags & IP6_TNL_F_CAP_XMIT) {
  883. int strict = (ipv6_addr_type(&p->raddr) &
  884. (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL));
  885. struct rt6_info *rt = rt6_lookup(dev_net(dev),
  886. &p->raddr, &p->laddr,
  887. p->link, strict);
  888. if (rt == NULL)
  889. return;
  890. if (rt->dst.dev) {
  891. dev->hard_header_len = rt->dst.dev->hard_header_len + addend;
  892. if (set_mtu) {
  893. dev->mtu = rt->dst.dev->mtu - addend;
  894. if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
  895. dev->mtu -= 8;
  896. if (dev->mtu < IPV6_MIN_MTU)
  897. dev->mtu = IPV6_MIN_MTU;
  898. }
  899. }
  900. dst_release(&rt->dst);
  901. }
  902. t->hlen = addend;
  903. }
  904. static int ip6gre_tnl_change(struct ip6_tnl *t,
  905. const struct __ip6_tnl_parm *p, int set_mtu)
  906. {
  907. t->parms.laddr = p->laddr;
  908. t->parms.raddr = p->raddr;
  909. t->parms.flags = p->flags;
  910. t->parms.hop_limit = p->hop_limit;
  911. t->parms.encap_limit = p->encap_limit;
  912. t->parms.flowinfo = p->flowinfo;
  913. t->parms.link = p->link;
  914. t->parms.proto = p->proto;
  915. t->parms.i_key = p->i_key;
  916. t->parms.o_key = p->o_key;
  917. t->parms.i_flags = p->i_flags;
  918. t->parms.o_flags = p->o_flags;
  919. ip6_tnl_dst_reset(t);
  920. ip6gre_tnl_link_config(t, set_mtu);
  921. return 0;
  922. }
  923. static void ip6gre_tnl_parm_from_user(struct __ip6_tnl_parm *p,
  924. const struct ip6_tnl_parm2 *u)
  925. {
  926. p->laddr = u->laddr;
  927. p->raddr = u->raddr;
  928. p->flags = u->flags;
  929. p->hop_limit = u->hop_limit;
  930. p->encap_limit = u->encap_limit;
  931. p->flowinfo = u->flowinfo;
  932. p->link = u->link;
  933. p->i_key = u->i_key;
  934. p->o_key = u->o_key;
  935. p->i_flags = u->i_flags;
  936. p->o_flags = u->o_flags;
  937. memcpy(p->name, u->name, sizeof(u->name));
  938. }
  939. static void ip6gre_tnl_parm_to_user(struct ip6_tnl_parm2 *u,
  940. const struct __ip6_tnl_parm *p)
  941. {
  942. u->proto = IPPROTO_GRE;
  943. u->laddr = p->laddr;
  944. u->raddr = p->raddr;
  945. u->flags = p->flags;
  946. u->hop_limit = p->hop_limit;
  947. u->encap_limit = p->encap_limit;
  948. u->flowinfo = p->flowinfo;
  949. u->link = p->link;
  950. u->i_key = p->i_key;
  951. u->o_key = p->o_key;
  952. u->i_flags = p->i_flags;
  953. u->o_flags = p->o_flags;
  954. memcpy(u->name, p->name, sizeof(u->name));
  955. }
  956. static int ip6gre_tunnel_ioctl(struct net_device *dev,
  957. struct ifreq *ifr, int cmd)
  958. {
  959. int err = 0;
  960. struct ip6_tnl_parm2 p;
  961. struct __ip6_tnl_parm p1;
  962. struct ip6_tnl *t;
  963. struct net *net = dev_net(dev);
  964. struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
  965. switch (cmd) {
  966. case SIOCGETTUNNEL:
  967. t = NULL;
  968. if (dev == ign->fb_tunnel_dev) {
  969. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) {
  970. err = -EFAULT;
  971. break;
  972. }
  973. ip6gre_tnl_parm_from_user(&p1, &p);
  974. t = ip6gre_tunnel_locate(net, &p1, 0);
  975. }
  976. if (t == NULL)
  977. t = netdev_priv(dev);
  978. ip6gre_tnl_parm_to_user(&p, &t->parms);
  979. if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p)))
  980. err = -EFAULT;
  981. break;
  982. case SIOCADDTUNNEL:
  983. case SIOCCHGTUNNEL:
  984. err = -EPERM;
  985. if (!capable(CAP_NET_ADMIN))
  986. goto done;
  987. err = -EFAULT;
  988. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
  989. goto done;
  990. err = -EINVAL;
  991. if ((p.i_flags|p.o_flags)&(GRE_VERSION|GRE_ROUTING))
  992. goto done;
  993. if (!(p.i_flags&GRE_KEY))
  994. p.i_key = 0;
  995. if (!(p.o_flags&GRE_KEY))
  996. p.o_key = 0;
  997. ip6gre_tnl_parm_from_user(&p1, &p);
  998. t = ip6gre_tunnel_locate(net, &p1, cmd == SIOCADDTUNNEL);
  999. if (dev != ign->fb_tunnel_dev && cmd == SIOCCHGTUNNEL) {
  1000. if (t != NULL) {
  1001. if (t->dev != dev) {
  1002. err = -EEXIST;
  1003. break;
  1004. }
  1005. } else {
  1006. t = netdev_priv(dev);
  1007. ip6gre_tunnel_unlink(ign, t);
  1008. synchronize_net();
  1009. ip6gre_tnl_change(t, &p1, 1);
  1010. ip6gre_tunnel_link(ign, t);
  1011. netdev_state_change(dev);
  1012. }
  1013. }
  1014. if (t) {
  1015. err = 0;
  1016. ip6gre_tnl_parm_to_user(&p, &t->parms);
  1017. if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p)))
  1018. err = -EFAULT;
  1019. } else
  1020. err = (cmd == SIOCADDTUNNEL ? -ENOBUFS : -ENOENT);
  1021. break;
  1022. case SIOCDELTUNNEL:
  1023. err = -EPERM;
  1024. if (!capable(CAP_NET_ADMIN))
  1025. goto done;
  1026. if (dev == ign->fb_tunnel_dev) {
  1027. err = -EFAULT;
  1028. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
  1029. goto done;
  1030. err = -ENOENT;
  1031. ip6gre_tnl_parm_from_user(&p1, &p);
  1032. t = ip6gre_tunnel_locate(net, &p1, 0);
  1033. if (t == NULL)
  1034. goto done;
  1035. err = -EPERM;
  1036. if (t == netdev_priv(ign->fb_tunnel_dev))
  1037. goto done;
  1038. dev = t->dev;
  1039. }
  1040. unregister_netdevice(dev);
  1041. err = 0;
  1042. break;
  1043. default:
  1044. err = -EINVAL;
  1045. }
  1046. done:
  1047. return err;
  1048. }
  1049. static int ip6gre_tunnel_change_mtu(struct net_device *dev, int new_mtu)
  1050. {
  1051. struct ip6_tnl *tunnel = netdev_priv(dev);
  1052. if (new_mtu < 68 ||
  1053. new_mtu > 0xFFF8 - dev->hard_header_len - tunnel->hlen)
  1054. return -EINVAL;
  1055. dev->mtu = new_mtu;
  1056. return 0;
  1057. }
  1058. static int ip6gre_header(struct sk_buff *skb, struct net_device *dev,
  1059. unsigned short type,
  1060. const void *daddr, const void *saddr, unsigned int len)
  1061. {
  1062. struct ip6_tnl *t = netdev_priv(dev);
  1063. struct ipv6hdr *ipv6h = (struct ipv6hdr *)skb_push(skb, t->hlen);
  1064. __be16 *p = (__be16 *)(ipv6h+1);
  1065. *(__be32 *)ipv6h = t->fl.u.ip6.flowlabel | htonl(0x60000000);
  1066. ipv6h->hop_limit = t->parms.hop_limit;
  1067. ipv6h->nexthdr = NEXTHDR_GRE;
  1068. ipv6h->saddr = t->parms.laddr;
  1069. ipv6h->daddr = t->parms.raddr;
  1070. p[0] = t->parms.o_flags;
  1071. p[1] = htons(type);
  1072. /*
  1073. * Set the source hardware address.
  1074. */
  1075. if (saddr)
  1076. memcpy(&ipv6h->saddr, saddr, sizeof(struct in6_addr));
  1077. if (daddr)
  1078. memcpy(&ipv6h->daddr, daddr, sizeof(struct in6_addr));
  1079. if (!ipv6_addr_any(&ipv6h->daddr))
  1080. return t->hlen;
  1081. return -t->hlen;
  1082. }
  1083. static int ip6gre_header_parse(const struct sk_buff *skb, unsigned char *haddr)
  1084. {
  1085. const struct ipv6hdr *ipv6h = (const struct ipv6hdr *)skb_mac_header(skb);
  1086. memcpy(haddr, &ipv6h->saddr, sizeof(struct in6_addr));
  1087. return sizeof(struct in6_addr);
  1088. }
  1089. static const struct header_ops ip6gre_header_ops = {
  1090. .create = ip6gre_header,
  1091. .parse = ip6gre_header_parse,
  1092. };
  1093. static const struct net_device_ops ip6gre_netdev_ops = {
  1094. .ndo_init = ip6gre_tunnel_init,
  1095. .ndo_uninit = ip6gre_tunnel_uninit,
  1096. .ndo_start_xmit = ip6gre_tunnel_xmit,
  1097. .ndo_do_ioctl = ip6gre_tunnel_ioctl,
  1098. .ndo_change_mtu = ip6gre_tunnel_change_mtu,
  1099. .ndo_get_stats64 = ip6gre_get_stats64,
  1100. };
  1101. static void ip6gre_dev_free(struct net_device *dev)
  1102. {
  1103. free_percpu(dev->tstats);
  1104. free_netdev(dev);
  1105. }
  1106. static void ip6gre_tunnel_setup(struct net_device *dev)
  1107. {
  1108. struct ip6_tnl *t;
  1109. dev->netdev_ops = &ip6gre_netdev_ops;
  1110. dev->destructor = ip6gre_dev_free;
  1111. dev->type = ARPHRD_IP6GRE;
  1112. dev->hard_header_len = LL_MAX_HEADER + sizeof(struct ipv6hdr) + 4;
  1113. dev->mtu = ETH_DATA_LEN - sizeof(struct ipv6hdr) - 4;
  1114. t = netdev_priv(dev);
  1115. if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
  1116. dev->mtu -= 8;
  1117. dev->flags |= IFF_NOARP;
  1118. dev->iflink = 0;
  1119. dev->addr_len = sizeof(struct in6_addr);
  1120. dev->features |= NETIF_F_NETNS_LOCAL;
  1121. dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
  1122. }
  1123. static int ip6gre_tunnel_init(struct net_device *dev)
  1124. {
  1125. struct ip6_tnl *tunnel;
  1126. tunnel = netdev_priv(dev);
  1127. tunnel->dev = dev;
  1128. strcpy(tunnel->parms.name, dev->name);
  1129. memcpy(dev->dev_addr, &tunnel->parms.laddr, sizeof(struct in6_addr));
  1130. memcpy(dev->broadcast, &tunnel->parms.raddr, sizeof(struct in6_addr));
  1131. if (ipv6_addr_any(&tunnel->parms.raddr))
  1132. dev->header_ops = &ip6gre_header_ops;
  1133. dev->tstats = alloc_percpu(struct pcpu_tstats);
  1134. if (!dev->tstats)
  1135. return -ENOMEM;
  1136. return 0;
  1137. }
  1138. static void ip6gre_fb_tunnel_init(struct net_device *dev)
  1139. {
  1140. struct ip6_tnl *tunnel = netdev_priv(dev);
  1141. tunnel->dev = dev;
  1142. strcpy(tunnel->parms.name, dev->name);
  1143. tunnel->hlen = sizeof(struct ipv6hdr) + 4;
  1144. dev_hold(dev);
  1145. }
  1146. static struct inet6_protocol ip6gre_protocol __read_mostly = {
  1147. .handler = ip6gre_rcv,
  1148. .err_handler = ip6gre_err,
  1149. .flags = INET6_PROTO_NOPOLICY|INET6_PROTO_FINAL,
  1150. };
  1151. static void ip6gre_destroy_tunnels(struct ip6gre_net *ign,
  1152. struct list_head *head)
  1153. {
  1154. int prio;
  1155. for (prio = 0; prio < 4; prio++) {
  1156. int h;
  1157. for (h = 0; h < HASH_SIZE; h++) {
  1158. struct ip6_tnl *t;
  1159. t = rtnl_dereference(ign->tunnels[prio][h]);
  1160. while (t != NULL) {
  1161. unregister_netdevice_queue(t->dev, head);
  1162. t = rtnl_dereference(t->next);
  1163. }
  1164. }
  1165. }
  1166. }
  1167. static int __net_init ip6gre_init_net(struct net *net)
  1168. {
  1169. struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
  1170. int err;
  1171. ign->fb_tunnel_dev = alloc_netdev(sizeof(struct ip6_tnl), "ip6gre0",
  1172. ip6gre_tunnel_setup);
  1173. if (!ign->fb_tunnel_dev) {
  1174. err = -ENOMEM;
  1175. goto err_alloc_dev;
  1176. }
  1177. dev_net_set(ign->fb_tunnel_dev, net);
  1178. ip6gre_fb_tunnel_init(ign->fb_tunnel_dev);
  1179. ign->fb_tunnel_dev->rtnl_link_ops = &ip6gre_link_ops;
  1180. err = register_netdev(ign->fb_tunnel_dev);
  1181. if (err)
  1182. goto err_reg_dev;
  1183. rcu_assign_pointer(ign->tunnels_wc[0],
  1184. netdev_priv(ign->fb_tunnel_dev));
  1185. return 0;
  1186. err_reg_dev:
  1187. ip6gre_dev_free(ign->fb_tunnel_dev);
  1188. err_alloc_dev:
  1189. return err;
  1190. }
  1191. static void __net_exit ip6gre_exit_net(struct net *net)
  1192. {
  1193. struct ip6gre_net *ign;
  1194. LIST_HEAD(list);
  1195. ign = net_generic(net, ip6gre_net_id);
  1196. rtnl_lock();
  1197. ip6gre_destroy_tunnels(ign, &list);
  1198. unregister_netdevice_many(&list);
  1199. rtnl_unlock();
  1200. }
  1201. static struct pernet_operations ip6gre_net_ops = {
  1202. .init = ip6gre_init_net,
  1203. .exit = ip6gre_exit_net,
  1204. .id = &ip6gre_net_id,
  1205. .size = sizeof(struct ip6gre_net),
  1206. };
  1207. static int ip6gre_tunnel_validate(struct nlattr *tb[], struct nlattr *data[])
  1208. {
  1209. __be16 flags;
  1210. if (!data)
  1211. return 0;
  1212. flags = 0;
  1213. if (data[IFLA_GRE_IFLAGS])
  1214. flags |= nla_get_be16(data[IFLA_GRE_IFLAGS]);
  1215. if (data[IFLA_GRE_OFLAGS])
  1216. flags |= nla_get_be16(data[IFLA_GRE_OFLAGS]);
  1217. if (flags & (GRE_VERSION|GRE_ROUTING))
  1218. return -EINVAL;
  1219. return 0;
  1220. }
  1221. static int ip6gre_tap_validate(struct nlattr *tb[], struct nlattr *data[])
  1222. {
  1223. struct in6_addr daddr;
  1224. if (tb[IFLA_ADDRESS]) {
  1225. if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
  1226. return -EINVAL;
  1227. if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
  1228. return -EADDRNOTAVAIL;
  1229. }
  1230. if (!data)
  1231. goto out;
  1232. if (data[IFLA_GRE_REMOTE]) {
  1233. nla_memcpy(&daddr, data[IFLA_GRE_REMOTE], sizeof(struct in6_addr));
  1234. if (ipv6_addr_any(&daddr))
  1235. return -EINVAL;
  1236. }
  1237. out:
  1238. return ip6gre_tunnel_validate(tb, data);
  1239. }
  1240. static void ip6gre_netlink_parms(struct nlattr *data[],
  1241. struct __ip6_tnl_parm *parms)
  1242. {
  1243. memset(parms, 0, sizeof(*parms));
  1244. if (!data)
  1245. return;
  1246. if (data[IFLA_GRE_LINK])
  1247. parms->link = nla_get_u32(data[IFLA_GRE_LINK]);
  1248. if (data[IFLA_GRE_IFLAGS])
  1249. parms->i_flags = nla_get_be16(data[IFLA_GRE_IFLAGS]);
  1250. if (data[IFLA_GRE_OFLAGS])
  1251. parms->o_flags = nla_get_be16(data[IFLA_GRE_OFLAGS]);
  1252. if (data[IFLA_GRE_IKEY])
  1253. parms->i_key = nla_get_be32(data[IFLA_GRE_IKEY]);
  1254. if (data[IFLA_GRE_OKEY])
  1255. parms->o_key = nla_get_be32(data[IFLA_GRE_OKEY]);
  1256. if (data[IFLA_GRE_LOCAL])
  1257. nla_memcpy(&parms->laddr, data[IFLA_GRE_LOCAL], sizeof(struct in6_addr));
  1258. if (data[IFLA_GRE_REMOTE])
  1259. nla_memcpy(&parms->raddr, data[IFLA_GRE_REMOTE], sizeof(struct in6_addr));
  1260. if (data[IFLA_GRE_TTL])
  1261. parms->hop_limit = nla_get_u8(data[IFLA_GRE_TTL]);
  1262. if (data[IFLA_GRE_ENCAP_LIMIT])
  1263. parms->encap_limit = nla_get_u8(data[IFLA_GRE_ENCAP_LIMIT]);
  1264. if (data[IFLA_GRE_FLOWINFO])
  1265. parms->flowinfo = nla_get_u32(data[IFLA_GRE_FLOWINFO]);
  1266. if (data[IFLA_GRE_FLAGS])
  1267. parms->flags = nla_get_u32(data[IFLA_GRE_FLAGS]);
  1268. }
  1269. static int ip6gre_tap_init(struct net_device *dev)
  1270. {
  1271. struct ip6_tnl *tunnel;
  1272. tunnel = netdev_priv(dev);
  1273. tunnel->dev = dev;
  1274. strcpy(tunnel->parms.name, dev->name);
  1275. ip6gre_tnl_link_config(tunnel, 1);
  1276. dev->tstats = alloc_percpu(struct pcpu_tstats);
  1277. if (!dev->tstats)
  1278. return -ENOMEM;
  1279. return 0;
  1280. }
  1281. static const struct net_device_ops ip6gre_tap_netdev_ops = {
  1282. .ndo_init = ip6gre_tap_init,
  1283. .ndo_uninit = ip6gre_tunnel_uninit,
  1284. .ndo_start_xmit = ip6gre_tunnel_xmit,
  1285. .ndo_set_mac_address = eth_mac_addr,
  1286. .ndo_validate_addr = eth_validate_addr,
  1287. .ndo_change_mtu = ip6gre_tunnel_change_mtu,
  1288. .ndo_get_stats64 = ip6gre_get_stats64,
  1289. };
  1290. static void ip6gre_tap_setup(struct net_device *dev)
  1291. {
  1292. ether_setup(dev);
  1293. dev->netdev_ops = &ip6gre_tap_netdev_ops;
  1294. dev->destructor = ip6gre_dev_free;
  1295. dev->iflink = 0;
  1296. dev->features |= NETIF_F_NETNS_LOCAL;
  1297. }
  1298. static int ip6gre_newlink(struct net *src_net, struct net_device *dev,
  1299. struct nlattr *tb[], struct nlattr *data[])
  1300. {
  1301. struct ip6_tnl *nt;
  1302. struct net *net = dev_net(dev);
  1303. struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
  1304. int err;
  1305. nt = netdev_priv(dev);
  1306. ip6gre_netlink_parms(data, &nt->parms);
  1307. if (ip6gre_tunnel_find(net, &nt->parms, dev->type))
  1308. return -EEXIST;
  1309. if (dev->type == ARPHRD_ETHER && !tb[IFLA_ADDRESS])
  1310. eth_hw_addr_random(dev);
  1311. nt->dev = dev;
  1312. ip6gre_tnl_link_config(nt, !tb[IFLA_MTU]);
  1313. /* Can use a lockless transmit, unless we generate output sequences */
  1314. if (!(nt->parms.o_flags & GRE_SEQ))
  1315. dev->features |= NETIF_F_LLTX;
  1316. err = register_netdevice(dev);
  1317. if (err)
  1318. goto out;
  1319. dev_hold(dev);
  1320. ip6gre_tunnel_link(ign, nt);
  1321. out:
  1322. return err;
  1323. }
  1324. static int ip6gre_changelink(struct net_device *dev, struct nlattr *tb[],
  1325. struct nlattr *data[])
  1326. {
  1327. struct ip6_tnl *t, *nt;
  1328. struct net *net = dev_net(dev);
  1329. struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
  1330. struct __ip6_tnl_parm p;
  1331. if (dev == ign->fb_tunnel_dev)
  1332. return -EINVAL;
  1333. nt = netdev_priv(dev);
  1334. ip6gre_netlink_parms(data, &p);
  1335. t = ip6gre_tunnel_locate(net, &p, 0);
  1336. if (t) {
  1337. if (t->dev != dev)
  1338. return -EEXIST;
  1339. } else {
  1340. t = nt;
  1341. ip6gre_tunnel_unlink(ign, t);
  1342. ip6gre_tnl_change(t, &p, !tb[IFLA_MTU]);
  1343. ip6gre_tunnel_link(ign, t);
  1344. netdev_state_change(dev);
  1345. }
  1346. return 0;
  1347. }
  1348. static size_t ip6gre_get_size(const struct net_device *dev)
  1349. {
  1350. return
  1351. /* IFLA_GRE_LINK */
  1352. nla_total_size(4) +
  1353. /* IFLA_GRE_IFLAGS */
  1354. nla_total_size(2) +
  1355. /* IFLA_GRE_OFLAGS */
  1356. nla_total_size(2) +
  1357. /* IFLA_GRE_IKEY */
  1358. nla_total_size(4) +
  1359. /* IFLA_GRE_OKEY */
  1360. nla_total_size(4) +
  1361. /* IFLA_GRE_LOCAL */
  1362. nla_total_size(4) +
  1363. /* IFLA_GRE_REMOTE */
  1364. nla_total_size(4) +
  1365. /* IFLA_GRE_TTL */
  1366. nla_total_size(1) +
  1367. /* IFLA_GRE_TOS */
  1368. nla_total_size(1) +
  1369. /* IFLA_GRE_ENCAP_LIMIT */
  1370. nla_total_size(1) +
  1371. /* IFLA_GRE_FLOWINFO */
  1372. nla_total_size(4) +
  1373. /* IFLA_GRE_FLAGS */
  1374. nla_total_size(4) +
  1375. 0;
  1376. }
  1377. static int ip6gre_fill_info(struct sk_buff *skb, const struct net_device *dev)
  1378. {
  1379. struct ip6_tnl *t = netdev_priv(dev);
  1380. struct __ip6_tnl_parm *p = &t->parms;
  1381. if (nla_put_u32(skb, IFLA_GRE_LINK, p->link) ||
  1382. nla_put_be16(skb, IFLA_GRE_IFLAGS, p->i_flags) ||
  1383. nla_put_be16(skb, IFLA_GRE_OFLAGS, p->o_flags) ||
  1384. nla_put_be32(skb, IFLA_GRE_IKEY, p->i_key) ||
  1385. nla_put_be32(skb, IFLA_GRE_OKEY, p->o_key) ||
  1386. nla_put(skb, IFLA_GRE_LOCAL, sizeof(struct in6_addr), &p->raddr) ||
  1387. nla_put(skb, IFLA_GRE_REMOTE, sizeof(struct in6_addr), &p->laddr) ||
  1388. nla_put_u8(skb, IFLA_GRE_TTL, p->hop_limit) ||
  1389. /*nla_put_u8(skb, IFLA_GRE_TOS, t->priority) ||*/
  1390. nla_put_u8(skb, IFLA_GRE_ENCAP_LIMIT, p->encap_limit) ||
  1391. nla_put_be32(skb, IFLA_GRE_FLOWINFO, p->flowinfo) ||
  1392. nla_put_u32(skb, IFLA_GRE_FLAGS, p->flags))
  1393. goto nla_put_failure;
  1394. return 0;
  1395. nla_put_failure:
  1396. return -EMSGSIZE;
  1397. }
  1398. static const struct nla_policy ip6gre_policy[IFLA_GRE_MAX + 1] = {
  1399. [IFLA_GRE_LINK] = { .type = NLA_U32 },
  1400. [IFLA_GRE_IFLAGS] = { .type = NLA_U16 },
  1401. [IFLA_GRE_OFLAGS] = { .type = NLA_U16 },
  1402. [IFLA_GRE_IKEY] = { .type = NLA_U32 },
  1403. [IFLA_GRE_OKEY] = { .type = NLA_U32 },
  1404. [IFLA_GRE_LOCAL] = { .len = FIELD_SIZEOF(struct ipv6hdr, saddr) },
  1405. [IFLA_GRE_REMOTE] = { .len = FIELD_SIZEOF(struct ipv6hdr, daddr) },
  1406. [IFLA_GRE_TTL] = { .type = NLA_U8 },
  1407. [IFLA_GRE_ENCAP_LIMIT] = { .type = NLA_U8 },
  1408. [IFLA_GRE_FLOWINFO] = { .type = NLA_U32 },
  1409. [IFLA_GRE_FLAGS] = { .type = NLA_U32 },
  1410. };
  1411. static struct rtnl_link_ops ip6gre_link_ops __read_mostly = {
  1412. .kind = "ip6gre",
  1413. .maxtype = IFLA_GRE_MAX,
  1414. .policy = ip6gre_policy,
  1415. .priv_size = sizeof(struct ip6_tnl),
  1416. .setup = ip6gre_tunnel_setup,
  1417. .validate = ip6gre_tunnel_validate,
  1418. .newlink = ip6gre_newlink,
  1419. .changelink = ip6gre_changelink,
  1420. .get_size = ip6gre_get_size,
  1421. .fill_info = ip6gre_fill_info,
  1422. };
  1423. static struct rtnl_link_ops ip6gre_tap_ops __read_mostly = {
  1424. .kind = "ip6gretap",
  1425. .maxtype = IFLA_GRE_MAX,
  1426. .policy = ip6gre_policy,
  1427. .priv_size = sizeof(struct ip6_tnl),
  1428. .setup = ip6gre_tap_setup,
  1429. .validate = ip6gre_tap_validate,
  1430. .newlink = ip6gre_newlink,
  1431. .changelink = ip6gre_changelink,
  1432. .get_size = ip6gre_get_size,
  1433. .fill_info = ip6gre_fill_info,
  1434. };
  1435. /*
  1436. * And now the modules code and kernel interface.
  1437. */
  1438. static int __init ip6gre_init(void)
  1439. {
  1440. int err;
  1441. pr_info("GRE over IPv6 tunneling driver\n");
  1442. err = register_pernet_device(&ip6gre_net_ops);
  1443. if (err < 0)
  1444. return err;
  1445. err = inet6_add_protocol(&ip6gre_protocol, IPPROTO_GRE);
  1446. if (err < 0) {
  1447. pr_info("%s: can't add protocol\n", __func__);
  1448. goto add_proto_failed;
  1449. }
  1450. err = rtnl_link_register(&ip6gre_link_ops);
  1451. if (err < 0)
  1452. goto rtnl_link_failed;
  1453. err = rtnl_link_register(&ip6gre_tap_ops);
  1454. if (err < 0)
  1455. goto tap_ops_failed;
  1456. out:
  1457. return err;
  1458. tap_ops_failed:
  1459. rtnl_link_unregister(&ip6gre_link_ops);
  1460. rtnl_link_failed:
  1461. inet6_del_protocol(&ip6gre_protocol, IPPROTO_GRE);
  1462. add_proto_failed:
  1463. unregister_pernet_device(&ip6gre_net_ops);
  1464. goto out;
  1465. }
  1466. static void __exit ip6gre_fini(void)
  1467. {
  1468. rtnl_link_unregister(&ip6gre_tap_ops);
  1469. rtnl_link_unregister(&ip6gre_link_ops);
  1470. inet6_del_protocol(&ip6gre_protocol, IPPROTO_GRE);
  1471. unregister_pernet_device(&ip6gre_net_ops);
  1472. }
  1473. module_init(ip6gre_init);
  1474. module_exit(ip6gre_fini);
  1475. MODULE_LICENSE("GPL");
  1476. MODULE_AUTHOR("D. Kozlov (xeb@mail.ru)");
  1477. MODULE_DESCRIPTION("GRE over IPv6 tunneling device");
  1478. MODULE_ALIAS_RTNL_LINK("ip6gre");
  1479. MODULE_ALIAS_NETDEV("ip6gre0");