ip6_gre.c 58 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375
  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/init.h>
  26. #include <linux/in6.h>
  27. #include <linux/inetdevice.h>
  28. #include <linux/igmp.h>
  29. #include <linux/netfilter_ipv4.h>
  30. #include <linux/etherdevice.h>
  31. #include <linux/if_ether.h>
  32. #include <linux/hash.h>
  33. #include <linux/if_tunnel.h>
  34. #include <linux/ip6_tunnel.h>
  35. #include <net/sock.h>
  36. #include <net/ip.h>
  37. #include <net/ip_tunnels.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. #include <net/gre.h>
  54. #include <net/erspan.h>
  55. #include <net/dst_metadata.h>
  56. static bool log_ecn_error = true;
  57. module_param(log_ecn_error, bool, 0644);
  58. MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN");
  59. #define IP6_GRE_HASH_SIZE_SHIFT 5
  60. #define IP6_GRE_HASH_SIZE (1 << IP6_GRE_HASH_SIZE_SHIFT)
  61. static unsigned int ip6gre_net_id __read_mostly;
  62. struct ip6gre_net {
  63. struct ip6_tnl __rcu *tunnels[4][IP6_GRE_HASH_SIZE];
  64. struct ip6_tnl __rcu *collect_md_tun;
  65. struct ip6_tnl __rcu *collect_md_tun_erspan;
  66. struct net_device *fb_tunnel_dev;
  67. };
  68. static struct rtnl_link_ops ip6gre_link_ops __read_mostly;
  69. static struct rtnl_link_ops ip6gre_tap_ops __read_mostly;
  70. static struct rtnl_link_ops ip6erspan_tap_ops __read_mostly;
  71. static int ip6gre_tunnel_init(struct net_device *dev);
  72. static void ip6gre_tunnel_setup(struct net_device *dev);
  73. static void ip6gre_tunnel_link(struct ip6gre_net *ign, struct ip6_tnl *t);
  74. static void ip6gre_tnl_link_config(struct ip6_tnl *t, int set_mtu);
  75. static void ip6erspan_tnl_link_config(struct ip6_tnl *t, int set_mtu);
  76. /* Tunnel hash table */
  77. /*
  78. 4 hash tables:
  79. 3: (remote,local)
  80. 2: (remote,*)
  81. 1: (*,local)
  82. 0: (*,*)
  83. We require exact key match i.e. if a key is present in packet
  84. it will match only tunnel with the same key; if it is not present,
  85. it will match only keyless tunnel.
  86. All keysless packets, if not matched configured keyless tunnels
  87. will match fallback tunnel.
  88. */
  89. #define HASH_KEY(key) (((__force u32)key^((__force u32)key>>4))&(IP6_GRE_HASH_SIZE - 1))
  90. static u32 HASH_ADDR(const struct in6_addr *addr)
  91. {
  92. u32 hash = ipv6_addr_hash(addr);
  93. return hash_32(hash, IP6_GRE_HASH_SIZE_SHIFT);
  94. }
  95. #define tunnels_r_l tunnels[3]
  96. #define tunnels_r tunnels[2]
  97. #define tunnels_l tunnels[1]
  98. #define tunnels_wc tunnels[0]
  99. /* Given src, dst and key, find appropriate for input tunnel. */
  100. static struct ip6_tnl *ip6gre_tunnel_lookup(struct net_device *dev,
  101. const struct in6_addr *remote, const struct in6_addr *local,
  102. __be32 key, __be16 gre_proto)
  103. {
  104. struct net *net = dev_net(dev);
  105. int link = dev->ifindex;
  106. unsigned int h0 = HASH_ADDR(remote);
  107. unsigned int h1 = HASH_KEY(key);
  108. struct ip6_tnl *t, *cand = NULL;
  109. struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
  110. int dev_type = (gre_proto == htons(ETH_P_TEB) ||
  111. gre_proto == htons(ETH_P_ERSPAN) ||
  112. gre_proto == htons(ETH_P_ERSPAN2)) ?
  113. ARPHRD_ETHER : ARPHRD_IP6GRE;
  114. int score, cand_score = 4;
  115. for_each_ip_tunnel_rcu(t, ign->tunnels_r_l[h0 ^ h1]) {
  116. if (!ipv6_addr_equal(local, &t->parms.laddr) ||
  117. !ipv6_addr_equal(remote, &t->parms.raddr) ||
  118. key != t->parms.i_key ||
  119. !(t->dev->flags & IFF_UP))
  120. continue;
  121. if (t->dev->type != ARPHRD_IP6GRE &&
  122. t->dev->type != dev_type)
  123. continue;
  124. score = 0;
  125. if (t->parms.link != link)
  126. score |= 1;
  127. if (t->dev->type != dev_type)
  128. score |= 2;
  129. if (score == 0)
  130. return t;
  131. if (score < cand_score) {
  132. cand = t;
  133. cand_score = score;
  134. }
  135. }
  136. for_each_ip_tunnel_rcu(t, ign->tunnels_r[h0 ^ h1]) {
  137. if (!ipv6_addr_equal(remote, &t->parms.raddr) ||
  138. key != t->parms.i_key ||
  139. !(t->dev->flags & IFF_UP))
  140. continue;
  141. if (t->dev->type != ARPHRD_IP6GRE &&
  142. t->dev->type != dev_type)
  143. continue;
  144. score = 0;
  145. if (t->parms.link != link)
  146. score |= 1;
  147. if (t->dev->type != dev_type)
  148. score |= 2;
  149. if (score == 0)
  150. return t;
  151. if (score < cand_score) {
  152. cand = t;
  153. cand_score = score;
  154. }
  155. }
  156. for_each_ip_tunnel_rcu(t, ign->tunnels_l[h1]) {
  157. if ((!ipv6_addr_equal(local, &t->parms.laddr) &&
  158. (!ipv6_addr_equal(local, &t->parms.raddr) ||
  159. !ipv6_addr_is_multicast(local))) ||
  160. key != t->parms.i_key ||
  161. !(t->dev->flags & IFF_UP))
  162. continue;
  163. if (t->dev->type != ARPHRD_IP6GRE &&
  164. t->dev->type != dev_type)
  165. continue;
  166. score = 0;
  167. if (t->parms.link != link)
  168. score |= 1;
  169. if (t->dev->type != dev_type)
  170. score |= 2;
  171. if (score == 0)
  172. return t;
  173. if (score < cand_score) {
  174. cand = t;
  175. cand_score = score;
  176. }
  177. }
  178. for_each_ip_tunnel_rcu(t, ign->tunnels_wc[h1]) {
  179. if (t->parms.i_key != key ||
  180. !(t->dev->flags & IFF_UP))
  181. continue;
  182. if (t->dev->type != ARPHRD_IP6GRE &&
  183. t->dev->type != dev_type)
  184. continue;
  185. score = 0;
  186. if (t->parms.link != link)
  187. score |= 1;
  188. if (t->dev->type != dev_type)
  189. score |= 2;
  190. if (score == 0)
  191. return t;
  192. if (score < cand_score) {
  193. cand = t;
  194. cand_score = score;
  195. }
  196. }
  197. if (cand)
  198. return cand;
  199. if (gre_proto == htons(ETH_P_ERSPAN) ||
  200. gre_proto == htons(ETH_P_ERSPAN2))
  201. t = rcu_dereference(ign->collect_md_tun_erspan);
  202. else
  203. t = rcu_dereference(ign->collect_md_tun);
  204. if (t && t->dev->flags & IFF_UP)
  205. return t;
  206. dev = ign->fb_tunnel_dev;
  207. if (dev && dev->flags & IFF_UP)
  208. return netdev_priv(dev);
  209. return NULL;
  210. }
  211. static struct ip6_tnl __rcu **__ip6gre_bucket(struct ip6gre_net *ign,
  212. const struct __ip6_tnl_parm *p)
  213. {
  214. const struct in6_addr *remote = &p->raddr;
  215. const struct in6_addr *local = &p->laddr;
  216. unsigned int h = HASH_KEY(p->i_key);
  217. int prio = 0;
  218. if (!ipv6_addr_any(local))
  219. prio |= 1;
  220. if (!ipv6_addr_any(remote) && !ipv6_addr_is_multicast(remote)) {
  221. prio |= 2;
  222. h ^= HASH_ADDR(remote);
  223. }
  224. return &ign->tunnels[prio][h];
  225. }
  226. static void ip6gre_tunnel_link_md(struct ip6gre_net *ign, struct ip6_tnl *t)
  227. {
  228. if (t->parms.collect_md)
  229. rcu_assign_pointer(ign->collect_md_tun, t);
  230. }
  231. static void ip6erspan_tunnel_link_md(struct ip6gre_net *ign, struct ip6_tnl *t)
  232. {
  233. if (t->parms.collect_md)
  234. rcu_assign_pointer(ign->collect_md_tun_erspan, t);
  235. }
  236. static void ip6gre_tunnel_unlink_md(struct ip6gre_net *ign, struct ip6_tnl *t)
  237. {
  238. if (t->parms.collect_md)
  239. rcu_assign_pointer(ign->collect_md_tun, NULL);
  240. }
  241. static void ip6erspan_tunnel_unlink_md(struct ip6gre_net *ign,
  242. struct ip6_tnl *t)
  243. {
  244. if (t->parms.collect_md)
  245. rcu_assign_pointer(ign->collect_md_tun_erspan, NULL);
  246. }
  247. static inline struct ip6_tnl __rcu **ip6gre_bucket(struct ip6gre_net *ign,
  248. const struct ip6_tnl *t)
  249. {
  250. return __ip6gre_bucket(ign, &t->parms);
  251. }
  252. static void ip6gre_tunnel_link(struct ip6gre_net *ign, struct ip6_tnl *t)
  253. {
  254. struct ip6_tnl __rcu **tp = ip6gre_bucket(ign, t);
  255. rcu_assign_pointer(t->next, rtnl_dereference(*tp));
  256. rcu_assign_pointer(*tp, t);
  257. }
  258. static void ip6gre_tunnel_unlink(struct ip6gre_net *ign, struct ip6_tnl *t)
  259. {
  260. struct ip6_tnl __rcu **tp;
  261. struct ip6_tnl *iter;
  262. for (tp = ip6gre_bucket(ign, t);
  263. (iter = rtnl_dereference(*tp)) != NULL;
  264. tp = &iter->next) {
  265. if (t == iter) {
  266. rcu_assign_pointer(*tp, t->next);
  267. break;
  268. }
  269. }
  270. }
  271. static struct ip6_tnl *ip6gre_tunnel_find(struct net *net,
  272. const struct __ip6_tnl_parm *parms,
  273. int type)
  274. {
  275. const struct in6_addr *remote = &parms->raddr;
  276. const struct in6_addr *local = &parms->laddr;
  277. __be32 key = parms->i_key;
  278. int link = parms->link;
  279. struct ip6_tnl *t;
  280. struct ip6_tnl __rcu **tp;
  281. struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
  282. for (tp = __ip6gre_bucket(ign, parms);
  283. (t = rtnl_dereference(*tp)) != NULL;
  284. tp = &t->next)
  285. if (ipv6_addr_equal(local, &t->parms.laddr) &&
  286. ipv6_addr_equal(remote, &t->parms.raddr) &&
  287. key == t->parms.i_key &&
  288. link == t->parms.link &&
  289. type == t->dev->type)
  290. break;
  291. return t;
  292. }
  293. static struct ip6_tnl *ip6gre_tunnel_locate(struct net *net,
  294. const struct __ip6_tnl_parm *parms, int create)
  295. {
  296. struct ip6_tnl *t, *nt;
  297. struct net_device *dev;
  298. char name[IFNAMSIZ];
  299. struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
  300. t = ip6gre_tunnel_find(net, parms, ARPHRD_IP6GRE);
  301. if (t && create)
  302. return NULL;
  303. if (t || !create)
  304. return t;
  305. if (parms->name[0]) {
  306. if (!dev_valid_name(parms->name))
  307. return NULL;
  308. strlcpy(name, parms->name, IFNAMSIZ);
  309. } else {
  310. strcpy(name, "ip6gre%d");
  311. }
  312. dev = alloc_netdev(sizeof(*t), name, NET_NAME_UNKNOWN,
  313. 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. nt->net = dev_net(dev);
  322. if (register_netdevice(dev) < 0)
  323. goto failed_free;
  324. ip6gre_tnl_link_config(nt, 1);
  325. /* Can use a lockless transmit, unless we generate output sequences */
  326. if (!(nt->parms.o_flags & TUNNEL_SEQ))
  327. dev->features |= NETIF_F_LLTX;
  328. dev_hold(dev);
  329. ip6gre_tunnel_link(ign, nt);
  330. return nt;
  331. failed_free:
  332. free_netdev(dev);
  333. return NULL;
  334. }
  335. static void ip6erspan_tunnel_uninit(struct net_device *dev)
  336. {
  337. struct ip6_tnl *t = netdev_priv(dev);
  338. struct ip6gre_net *ign = net_generic(t->net, ip6gre_net_id);
  339. ip6erspan_tunnel_unlink_md(ign, t);
  340. ip6gre_tunnel_unlink(ign, t);
  341. dst_cache_reset(&t->dst_cache);
  342. dev_put(dev);
  343. }
  344. static void ip6gre_tunnel_uninit(struct net_device *dev)
  345. {
  346. struct ip6_tnl *t = netdev_priv(dev);
  347. struct ip6gre_net *ign = net_generic(t->net, ip6gre_net_id);
  348. ip6gre_tunnel_unlink_md(ign, t);
  349. ip6gre_tunnel_unlink(ign, t);
  350. dst_cache_reset(&t->dst_cache);
  351. dev_put(dev);
  352. }
  353. static void ip6gre_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
  354. u8 type, u8 code, int offset, __be32 info)
  355. {
  356. struct net *net = dev_net(skb->dev);
  357. const struct gre_base_hdr *greh;
  358. const struct ipv6hdr *ipv6h;
  359. int grehlen = sizeof(*greh);
  360. struct ip6_tnl *t;
  361. int key_off = 0;
  362. __be16 flags;
  363. __be32 key;
  364. if (!pskb_may_pull(skb, offset + grehlen))
  365. return;
  366. greh = (const struct gre_base_hdr *)(skb->data + offset);
  367. flags = greh->flags;
  368. if (flags & (GRE_VERSION | GRE_ROUTING))
  369. return;
  370. if (flags & GRE_CSUM)
  371. grehlen += 4;
  372. if (flags & GRE_KEY) {
  373. key_off = grehlen + offset;
  374. grehlen += 4;
  375. }
  376. if (!pskb_may_pull(skb, offset + grehlen))
  377. return;
  378. ipv6h = (const struct ipv6hdr *)skb->data;
  379. greh = (const struct gre_base_hdr *)(skb->data + offset);
  380. key = key_off ? *(__be32 *)(skb->data + key_off) : 0;
  381. t = ip6gre_tunnel_lookup(skb->dev, &ipv6h->daddr, &ipv6h->saddr,
  382. key, greh->protocol);
  383. if (!t)
  384. return;
  385. switch (type) {
  386. struct ipv6_tlv_tnl_enc_lim *tel;
  387. __u32 teli;
  388. case ICMPV6_DEST_UNREACH:
  389. net_dbg_ratelimited("%s: Path to destination invalid or inactive!\n",
  390. t->parms.name);
  391. if (code != ICMPV6_PORT_UNREACH)
  392. break;
  393. return;
  394. case ICMPV6_TIME_EXCEED:
  395. if (code == ICMPV6_EXC_HOPLIMIT) {
  396. net_dbg_ratelimited("%s: Too small hop limit or routing loop in tunnel!\n",
  397. t->parms.name);
  398. break;
  399. }
  400. return;
  401. case ICMPV6_PARAMPROB:
  402. teli = 0;
  403. if (code == ICMPV6_HDR_FIELD)
  404. teli = ip6_tnl_parse_tlv_enc_lim(skb, skb->data);
  405. if (teli && teli == be32_to_cpu(info) - 2) {
  406. tel = (struct ipv6_tlv_tnl_enc_lim *) &skb->data[teli];
  407. if (tel->encap_limit == 0) {
  408. net_dbg_ratelimited("%s: Too small encapsulation limit or routing loop in tunnel!\n",
  409. t->parms.name);
  410. }
  411. } else {
  412. net_dbg_ratelimited("%s: Recipient unable to parse tunneled packet!\n",
  413. t->parms.name);
  414. }
  415. return;
  416. case ICMPV6_PKT_TOOBIG:
  417. ip6_update_pmtu(skb, net, info, 0, 0, sock_net_uid(net, NULL));
  418. return;
  419. case NDISC_REDIRECT:
  420. ip6_redirect(skb, net, skb->dev->ifindex, 0,
  421. sock_net_uid(net, NULL));
  422. return;
  423. }
  424. if (time_before(jiffies, t->err_time + IP6TUNNEL_ERR_TIMEO))
  425. t->err_count++;
  426. else
  427. t->err_count = 1;
  428. t->err_time = jiffies;
  429. }
  430. static int ip6gre_rcv(struct sk_buff *skb, const struct tnl_ptk_info *tpi)
  431. {
  432. const struct ipv6hdr *ipv6h;
  433. struct ip6_tnl *tunnel;
  434. ipv6h = ipv6_hdr(skb);
  435. tunnel = ip6gre_tunnel_lookup(skb->dev,
  436. &ipv6h->saddr, &ipv6h->daddr, tpi->key,
  437. tpi->proto);
  438. if (tunnel) {
  439. if (tunnel->parms.collect_md) {
  440. struct metadata_dst *tun_dst;
  441. __be64 tun_id;
  442. __be16 flags;
  443. flags = tpi->flags;
  444. tun_id = key32_to_tunnel_id(tpi->key);
  445. tun_dst = ipv6_tun_rx_dst(skb, flags, tun_id, 0);
  446. if (!tun_dst)
  447. return PACKET_REJECT;
  448. ip6_tnl_rcv(tunnel, skb, tpi, tun_dst, log_ecn_error);
  449. } else {
  450. ip6_tnl_rcv(tunnel, skb, tpi, NULL, log_ecn_error);
  451. }
  452. return PACKET_RCVD;
  453. }
  454. return PACKET_REJECT;
  455. }
  456. static int ip6erspan_rcv(struct sk_buff *skb, int gre_hdr_len,
  457. struct tnl_ptk_info *tpi)
  458. {
  459. struct erspan_base_hdr *ershdr;
  460. struct erspan_metadata *pkt_md;
  461. const struct ipv6hdr *ipv6h;
  462. struct erspan_md2 *md2;
  463. struct ip6_tnl *tunnel;
  464. u8 ver;
  465. if (unlikely(!pskb_may_pull(skb, sizeof(*ershdr))))
  466. return PACKET_REJECT;
  467. ipv6h = ipv6_hdr(skb);
  468. ershdr = (struct erspan_base_hdr *)skb->data;
  469. ver = ershdr->ver;
  470. tpi->key = cpu_to_be32(get_session_id(ershdr));
  471. tunnel = ip6gre_tunnel_lookup(skb->dev,
  472. &ipv6h->saddr, &ipv6h->daddr, tpi->key,
  473. tpi->proto);
  474. if (tunnel) {
  475. int len = erspan_hdr_len(ver);
  476. if (unlikely(!pskb_may_pull(skb, len)))
  477. return PACKET_REJECT;
  478. ershdr = (struct erspan_base_hdr *)skb->data;
  479. pkt_md = (struct erspan_metadata *)(ershdr + 1);
  480. if (__iptunnel_pull_header(skb, len,
  481. htons(ETH_P_TEB),
  482. false, false) < 0)
  483. return PACKET_REJECT;
  484. if (tunnel->parms.collect_md) {
  485. struct metadata_dst *tun_dst;
  486. struct ip_tunnel_info *info;
  487. struct erspan_metadata *md;
  488. __be64 tun_id;
  489. __be16 flags;
  490. tpi->flags |= TUNNEL_KEY;
  491. flags = tpi->flags;
  492. tun_id = key32_to_tunnel_id(tpi->key);
  493. tun_dst = ipv6_tun_rx_dst(skb, flags, tun_id,
  494. sizeof(*md));
  495. if (!tun_dst)
  496. return PACKET_REJECT;
  497. info = &tun_dst->u.tun_info;
  498. md = ip_tunnel_info_opts(info);
  499. md->version = ver;
  500. md2 = &md->u.md2;
  501. memcpy(md2, pkt_md, ver == 1 ? ERSPAN_V1_MDSIZE :
  502. ERSPAN_V2_MDSIZE);
  503. info->key.tun_flags |= TUNNEL_ERSPAN_OPT;
  504. info->options_len = sizeof(*md);
  505. ip6_tnl_rcv(tunnel, skb, tpi, tun_dst, log_ecn_error);
  506. } else {
  507. ip6_tnl_rcv(tunnel, skb, tpi, NULL, log_ecn_error);
  508. }
  509. return PACKET_RCVD;
  510. }
  511. return PACKET_REJECT;
  512. }
  513. static int gre_rcv(struct sk_buff *skb)
  514. {
  515. struct tnl_ptk_info tpi;
  516. bool csum_err = false;
  517. int hdr_len;
  518. hdr_len = gre_parse_header(skb, &tpi, &csum_err, htons(ETH_P_IPV6), 0);
  519. if (hdr_len < 0)
  520. goto drop;
  521. if (iptunnel_pull_header(skb, hdr_len, tpi.proto, false))
  522. goto drop;
  523. if (unlikely(tpi.proto == htons(ETH_P_ERSPAN) ||
  524. tpi.proto == htons(ETH_P_ERSPAN2))) {
  525. if (ip6erspan_rcv(skb, hdr_len, &tpi) == PACKET_RCVD)
  526. return 0;
  527. goto out;
  528. }
  529. if (ip6gre_rcv(skb, &tpi) == PACKET_RCVD)
  530. return 0;
  531. out:
  532. icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0);
  533. drop:
  534. kfree_skb(skb);
  535. return 0;
  536. }
  537. static int gre_handle_offloads(struct sk_buff *skb, bool csum)
  538. {
  539. return iptunnel_handle_offloads(skb,
  540. csum ? SKB_GSO_GRE_CSUM : SKB_GSO_GRE);
  541. }
  542. static void prepare_ip6gre_xmit_ipv4(struct sk_buff *skb,
  543. struct net_device *dev,
  544. struct flowi6 *fl6, __u8 *dsfield,
  545. int *encap_limit)
  546. {
  547. const struct iphdr *iph = ip_hdr(skb);
  548. struct ip6_tnl *t = netdev_priv(dev);
  549. if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
  550. *encap_limit = t->parms.encap_limit;
  551. memcpy(fl6, &t->fl.u.ip6, sizeof(*fl6));
  552. if (t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS)
  553. *dsfield = ipv4_get_dsfield(iph);
  554. else
  555. *dsfield = ip6_tclass(t->parms.flowinfo);
  556. if (t->parms.flags & IP6_TNL_F_USE_ORIG_FWMARK)
  557. fl6->flowi6_mark = skb->mark;
  558. else
  559. fl6->flowi6_mark = t->parms.fwmark;
  560. fl6->flowi6_uid = sock_net_uid(dev_net(dev), NULL);
  561. }
  562. static int prepare_ip6gre_xmit_ipv6(struct sk_buff *skb,
  563. struct net_device *dev,
  564. struct flowi6 *fl6, __u8 *dsfield,
  565. int *encap_limit)
  566. {
  567. struct ipv6hdr *ipv6h = ipv6_hdr(skb);
  568. struct ip6_tnl *t = netdev_priv(dev);
  569. __u16 offset;
  570. offset = ip6_tnl_parse_tlv_enc_lim(skb, skb_network_header(skb));
  571. /* ip6_tnl_parse_tlv_enc_lim() might have reallocated skb->head */
  572. if (offset > 0) {
  573. struct ipv6_tlv_tnl_enc_lim *tel;
  574. tel = (struct ipv6_tlv_tnl_enc_lim *)&skb_network_header(skb)[offset];
  575. if (tel->encap_limit == 0) {
  576. icmpv6_send(skb, ICMPV6_PARAMPROB,
  577. ICMPV6_HDR_FIELD, offset + 2);
  578. return -1;
  579. }
  580. *encap_limit = tel->encap_limit - 1;
  581. } else if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT)) {
  582. *encap_limit = t->parms.encap_limit;
  583. }
  584. memcpy(fl6, &t->fl.u.ip6, sizeof(*fl6));
  585. if (t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS)
  586. *dsfield = ipv6_get_dsfield(ipv6h);
  587. else
  588. *dsfield = ip6_tclass(t->parms.flowinfo);
  589. if (t->parms.flags & IP6_TNL_F_USE_ORIG_FLOWLABEL)
  590. fl6->flowlabel |= ip6_flowlabel(ipv6h);
  591. if (t->parms.flags & IP6_TNL_F_USE_ORIG_FWMARK)
  592. fl6->flowi6_mark = skb->mark;
  593. else
  594. fl6->flowi6_mark = t->parms.fwmark;
  595. fl6->flowi6_uid = sock_net_uid(dev_net(dev), NULL);
  596. return 0;
  597. }
  598. static netdev_tx_t __gre6_xmit(struct sk_buff *skb,
  599. struct net_device *dev, __u8 dsfield,
  600. struct flowi6 *fl6, int encap_limit,
  601. __u32 *pmtu, __be16 proto)
  602. {
  603. struct ip6_tnl *tunnel = netdev_priv(dev);
  604. __be16 protocol;
  605. if (dev->type == ARPHRD_ETHER)
  606. IPCB(skb)->flags = 0;
  607. if (dev->header_ops && dev->type == ARPHRD_IP6GRE)
  608. fl6->daddr = ((struct ipv6hdr *)skb->data)->daddr;
  609. else
  610. fl6->daddr = tunnel->parms.raddr;
  611. if (skb_cow_head(skb, dev->needed_headroom ?: tunnel->hlen))
  612. return -ENOMEM;
  613. /* Push GRE header. */
  614. protocol = (dev->type == ARPHRD_ETHER) ? htons(ETH_P_TEB) : proto;
  615. if (tunnel->parms.collect_md) {
  616. struct ip_tunnel_info *tun_info;
  617. const struct ip_tunnel_key *key;
  618. __be16 flags;
  619. tun_info = skb_tunnel_info(skb);
  620. if (unlikely(!tun_info ||
  621. !(tun_info->mode & IP_TUNNEL_INFO_TX) ||
  622. ip_tunnel_info_af(tun_info) != AF_INET6))
  623. return -EINVAL;
  624. key = &tun_info->key;
  625. memset(fl6, 0, sizeof(*fl6));
  626. fl6->flowi6_proto = IPPROTO_GRE;
  627. fl6->daddr = key->u.ipv6.dst;
  628. fl6->flowlabel = key->label;
  629. fl6->flowi6_uid = sock_net_uid(dev_net(dev), NULL);
  630. dsfield = key->tos;
  631. flags = key->tun_flags &
  632. (TUNNEL_CSUM | TUNNEL_KEY | TUNNEL_SEQ);
  633. tunnel->tun_hlen = gre_calc_hlen(flags);
  634. gre_build_header(skb, tunnel->tun_hlen,
  635. flags, protocol,
  636. tunnel_id_to_key32(tun_info->key.tun_id),
  637. (flags & TUNNEL_SEQ) ? htonl(tunnel->o_seqno++)
  638. : 0);
  639. } else {
  640. if (tunnel->parms.o_flags & TUNNEL_SEQ)
  641. tunnel->o_seqno++;
  642. gre_build_header(skb, tunnel->tun_hlen, tunnel->parms.o_flags,
  643. protocol, tunnel->parms.o_key,
  644. htonl(tunnel->o_seqno));
  645. }
  646. return ip6_tnl_xmit(skb, dev, dsfield, fl6, encap_limit, pmtu,
  647. NEXTHDR_GRE);
  648. }
  649. static inline int ip6gre_xmit_ipv4(struct sk_buff *skb, struct net_device *dev)
  650. {
  651. struct ip6_tnl *t = netdev_priv(dev);
  652. int encap_limit = -1;
  653. struct flowi6 fl6;
  654. __u8 dsfield = 0;
  655. __u32 mtu;
  656. int err;
  657. memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
  658. if (!t->parms.collect_md)
  659. prepare_ip6gre_xmit_ipv4(skb, dev, &fl6,
  660. &dsfield, &encap_limit);
  661. err = gre_handle_offloads(skb, !!(t->parms.o_flags & TUNNEL_CSUM));
  662. if (err)
  663. return -1;
  664. err = __gre6_xmit(skb, dev, dsfield, &fl6, encap_limit, &mtu,
  665. skb->protocol);
  666. if (err != 0) {
  667. /* XXX: send ICMP error even if DF is not set. */
  668. if (err == -EMSGSIZE)
  669. icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED,
  670. htonl(mtu));
  671. return -1;
  672. }
  673. return 0;
  674. }
  675. static inline int ip6gre_xmit_ipv6(struct sk_buff *skb, struct net_device *dev)
  676. {
  677. struct ip6_tnl *t = netdev_priv(dev);
  678. struct ipv6hdr *ipv6h = ipv6_hdr(skb);
  679. int encap_limit = -1;
  680. struct flowi6 fl6;
  681. __u8 dsfield = 0;
  682. __u32 mtu;
  683. int err;
  684. if (ipv6_addr_equal(&t->parms.raddr, &ipv6h->saddr))
  685. return -1;
  686. if (!t->parms.collect_md &&
  687. prepare_ip6gre_xmit_ipv6(skb, dev, &fl6, &dsfield, &encap_limit))
  688. return -1;
  689. if (gre_handle_offloads(skb, !!(t->parms.o_flags & TUNNEL_CSUM)))
  690. return -1;
  691. err = __gre6_xmit(skb, dev, dsfield, &fl6, encap_limit,
  692. &mtu, skb->protocol);
  693. if (err != 0) {
  694. if (err == -EMSGSIZE)
  695. icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
  696. return -1;
  697. }
  698. return 0;
  699. }
  700. /**
  701. * ip6gre_tnl_addr_conflict - compare packet addresses to tunnel's own
  702. * @t: the outgoing tunnel device
  703. * @hdr: IPv6 header from the incoming packet
  704. *
  705. * Description:
  706. * Avoid trivial tunneling loop by checking that tunnel exit-point
  707. * doesn't match source of incoming packet.
  708. *
  709. * Return:
  710. * 1 if conflict,
  711. * 0 else
  712. **/
  713. static inline bool ip6gre_tnl_addr_conflict(const struct ip6_tnl *t,
  714. const struct ipv6hdr *hdr)
  715. {
  716. return ipv6_addr_equal(&t->parms.raddr, &hdr->saddr);
  717. }
  718. static int ip6gre_xmit_other(struct sk_buff *skb, struct net_device *dev)
  719. {
  720. struct ip6_tnl *t = netdev_priv(dev);
  721. int encap_limit = -1;
  722. struct flowi6 fl6;
  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. if (!t->parms.collect_md)
  728. memcpy(&fl6, &t->fl.u.ip6, sizeof(fl6));
  729. err = gre_handle_offloads(skb, !!(t->parms.o_flags & TUNNEL_CSUM));
  730. if (err)
  731. return err;
  732. err = __gre6_xmit(skb, dev, 0, &fl6, encap_limit, &mtu, skb->protocol);
  733. return err;
  734. }
  735. static netdev_tx_t ip6gre_tunnel_xmit(struct sk_buff *skb,
  736. struct net_device *dev)
  737. {
  738. struct ip6_tnl *t = netdev_priv(dev);
  739. struct net_device_stats *stats = &t->dev->stats;
  740. int ret;
  741. if (!ip6_tnl_xmit_ctl(t, &t->parms.laddr, &t->parms.raddr))
  742. goto tx_err;
  743. switch (skb->protocol) {
  744. case htons(ETH_P_IP):
  745. ret = ip6gre_xmit_ipv4(skb, dev);
  746. break;
  747. case htons(ETH_P_IPV6):
  748. ret = ip6gre_xmit_ipv6(skb, dev);
  749. break;
  750. default:
  751. ret = ip6gre_xmit_other(skb, dev);
  752. break;
  753. }
  754. if (ret < 0)
  755. goto tx_err;
  756. return NETDEV_TX_OK;
  757. tx_err:
  758. stats->tx_errors++;
  759. stats->tx_dropped++;
  760. kfree_skb(skb);
  761. return NETDEV_TX_OK;
  762. }
  763. static netdev_tx_t ip6erspan_tunnel_xmit(struct sk_buff *skb,
  764. struct net_device *dev)
  765. {
  766. struct ip6_tnl *t = netdev_priv(dev);
  767. struct dst_entry *dst = skb_dst(skb);
  768. struct net_device_stats *stats;
  769. bool truncate = false;
  770. int encap_limit = -1;
  771. __u8 dsfield = false;
  772. struct flowi6 fl6;
  773. int err = -EINVAL;
  774. __u32 mtu;
  775. int nhoff;
  776. int thoff;
  777. if (!ip6_tnl_xmit_ctl(t, &t->parms.laddr, &t->parms.raddr))
  778. goto tx_err;
  779. if (gre_handle_offloads(skb, false))
  780. goto tx_err;
  781. if (skb->len > dev->mtu + dev->hard_header_len) {
  782. pskb_trim(skb, dev->mtu + dev->hard_header_len);
  783. truncate = true;
  784. }
  785. nhoff = skb_network_header(skb) - skb_mac_header(skb);
  786. if (skb->protocol == htons(ETH_P_IP) &&
  787. (ntohs(ip_hdr(skb)->tot_len) > skb->len - nhoff))
  788. truncate = true;
  789. thoff = skb_transport_header(skb) - skb_mac_header(skb);
  790. if (skb->protocol == htons(ETH_P_IPV6) &&
  791. (ntohs(ipv6_hdr(skb)->payload_len) > skb->len - thoff))
  792. truncate = true;
  793. if (skb_cow_head(skb, dev->needed_headroom ?: t->hlen))
  794. goto tx_err;
  795. t->parms.o_flags &= ~TUNNEL_KEY;
  796. IPCB(skb)->flags = 0;
  797. /* For collect_md mode, derive fl6 from the tunnel key,
  798. * for native mode, call prepare_ip6gre_xmit_{ipv4,ipv6}.
  799. */
  800. if (t->parms.collect_md) {
  801. struct ip_tunnel_info *tun_info;
  802. const struct ip_tunnel_key *key;
  803. struct erspan_metadata *md;
  804. __be32 tun_id;
  805. tun_info = skb_tunnel_info(skb);
  806. if (unlikely(!tun_info ||
  807. !(tun_info->mode & IP_TUNNEL_INFO_TX) ||
  808. ip_tunnel_info_af(tun_info) != AF_INET6))
  809. return -EINVAL;
  810. key = &tun_info->key;
  811. memset(&fl6, 0, sizeof(fl6));
  812. fl6.flowi6_proto = IPPROTO_GRE;
  813. fl6.daddr = key->u.ipv6.dst;
  814. fl6.flowlabel = key->label;
  815. fl6.flowi6_uid = sock_net_uid(dev_net(dev), NULL);
  816. dsfield = key->tos;
  817. if (!(tun_info->key.tun_flags & TUNNEL_ERSPAN_OPT))
  818. goto tx_err;
  819. md = ip_tunnel_info_opts(tun_info);
  820. if (!md)
  821. goto tx_err;
  822. tun_id = tunnel_id_to_key32(key->tun_id);
  823. if (md->version == 1) {
  824. erspan_build_header(skb,
  825. ntohl(tun_id),
  826. ntohl(md->u.index), truncate,
  827. false);
  828. } else if (md->version == 2) {
  829. erspan_build_header_v2(skb,
  830. ntohl(tun_id),
  831. md->u.md2.dir,
  832. get_hwid(&md->u.md2),
  833. truncate, false);
  834. } else {
  835. goto tx_err;
  836. }
  837. } else {
  838. struct ipv6hdr *ipv6h = ipv6_hdr(skb);
  839. switch (skb->protocol) {
  840. case htons(ETH_P_IP):
  841. memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
  842. prepare_ip6gre_xmit_ipv4(skb, dev, &fl6,
  843. &dsfield, &encap_limit);
  844. break;
  845. case htons(ETH_P_IPV6):
  846. if (ipv6_addr_equal(&t->parms.raddr, &ipv6h->saddr))
  847. goto tx_err;
  848. if (prepare_ip6gre_xmit_ipv6(skb, dev, &fl6,
  849. &dsfield, &encap_limit))
  850. goto tx_err;
  851. break;
  852. default:
  853. memcpy(&fl6, &t->fl.u.ip6, sizeof(fl6));
  854. break;
  855. }
  856. if (t->parms.erspan_ver == 1)
  857. erspan_build_header(skb, ntohl(t->parms.o_key),
  858. t->parms.index,
  859. truncate, false);
  860. else if (t->parms.erspan_ver == 2)
  861. erspan_build_header_v2(skb, ntohl(t->parms.o_key),
  862. t->parms.dir,
  863. t->parms.hwid,
  864. truncate, false);
  865. else
  866. goto tx_err;
  867. fl6.daddr = t->parms.raddr;
  868. }
  869. /* Push GRE header. */
  870. gre_build_header(skb, 8, TUNNEL_SEQ,
  871. htons(ETH_P_ERSPAN), 0, htonl(t->o_seqno++));
  872. /* TooBig packet may have updated dst->dev's mtu */
  873. if (!t->parms.collect_md && dst && dst_mtu(dst) > dst->dev->mtu)
  874. dst->ops->update_pmtu(dst, NULL, skb, dst->dev->mtu);
  875. err = ip6_tnl_xmit(skb, dev, dsfield, &fl6, encap_limit, &mtu,
  876. NEXTHDR_GRE);
  877. if (err != 0) {
  878. /* XXX: send ICMP error even if DF is not set. */
  879. if (err == -EMSGSIZE) {
  880. if (skb->protocol == htons(ETH_P_IP))
  881. icmp_send(skb, ICMP_DEST_UNREACH,
  882. ICMP_FRAG_NEEDED, htonl(mtu));
  883. else
  884. icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
  885. }
  886. goto tx_err;
  887. }
  888. return NETDEV_TX_OK;
  889. tx_err:
  890. stats = &t->dev->stats;
  891. stats->tx_errors++;
  892. stats->tx_dropped++;
  893. kfree_skb(skb);
  894. return NETDEV_TX_OK;
  895. }
  896. static void ip6gre_tnl_link_config_common(struct ip6_tnl *t)
  897. {
  898. struct net_device *dev = t->dev;
  899. struct __ip6_tnl_parm *p = &t->parms;
  900. struct flowi6 *fl6 = &t->fl.u.ip6;
  901. if (dev->type != ARPHRD_ETHER) {
  902. memcpy(dev->dev_addr, &p->laddr, sizeof(struct in6_addr));
  903. memcpy(dev->broadcast, &p->raddr, sizeof(struct in6_addr));
  904. }
  905. /* Set up flowi template */
  906. fl6->saddr = p->laddr;
  907. fl6->daddr = p->raddr;
  908. fl6->flowi6_oif = p->link;
  909. fl6->flowlabel = 0;
  910. fl6->flowi6_proto = IPPROTO_GRE;
  911. if (!(p->flags&IP6_TNL_F_USE_ORIG_TCLASS))
  912. fl6->flowlabel |= IPV6_TCLASS_MASK & p->flowinfo;
  913. if (!(p->flags&IP6_TNL_F_USE_ORIG_FLOWLABEL))
  914. fl6->flowlabel |= IPV6_FLOWLABEL_MASK & p->flowinfo;
  915. p->flags &= ~(IP6_TNL_F_CAP_XMIT|IP6_TNL_F_CAP_RCV|IP6_TNL_F_CAP_PER_PACKET);
  916. p->flags |= ip6_tnl_get_cap(t, &p->laddr, &p->raddr);
  917. if (p->flags&IP6_TNL_F_CAP_XMIT &&
  918. p->flags&IP6_TNL_F_CAP_RCV && dev->type != ARPHRD_ETHER)
  919. dev->flags |= IFF_POINTOPOINT;
  920. else
  921. dev->flags &= ~IFF_POINTOPOINT;
  922. }
  923. static void ip6gre_tnl_link_config_route(struct ip6_tnl *t, int set_mtu,
  924. int t_hlen)
  925. {
  926. const struct __ip6_tnl_parm *p = &t->parms;
  927. struct net_device *dev = t->dev;
  928. if (p->flags & IP6_TNL_F_CAP_XMIT) {
  929. int strict = (ipv6_addr_type(&p->raddr) &
  930. (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL));
  931. struct rt6_info *rt = rt6_lookup(t->net,
  932. &p->raddr, &p->laddr,
  933. p->link, NULL, strict);
  934. if (!rt)
  935. return;
  936. if (rt->dst.dev) {
  937. dev->needed_headroom = rt->dst.dev->hard_header_len +
  938. t_hlen;
  939. if (set_mtu) {
  940. dev->mtu = rt->dst.dev->mtu - t_hlen;
  941. if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
  942. dev->mtu -= 8;
  943. if (dev->type == ARPHRD_ETHER)
  944. dev->mtu -= ETH_HLEN;
  945. if (dev->mtu < IPV6_MIN_MTU)
  946. dev->mtu = IPV6_MIN_MTU;
  947. }
  948. }
  949. ip6_rt_put(rt);
  950. }
  951. }
  952. static int ip6gre_calc_hlen(struct ip6_tnl *tunnel)
  953. {
  954. int t_hlen;
  955. tunnel->tun_hlen = gre_calc_hlen(tunnel->parms.o_flags);
  956. tunnel->hlen = tunnel->tun_hlen + tunnel->encap_hlen;
  957. t_hlen = tunnel->hlen + sizeof(struct ipv6hdr);
  958. tunnel->dev->needed_headroom = LL_MAX_HEADER + t_hlen;
  959. return t_hlen;
  960. }
  961. static void ip6gre_tnl_link_config(struct ip6_tnl *t, int set_mtu)
  962. {
  963. ip6gre_tnl_link_config_common(t);
  964. ip6gre_tnl_link_config_route(t, set_mtu, ip6gre_calc_hlen(t));
  965. }
  966. static void ip6gre_tnl_copy_tnl_parm(struct ip6_tnl *t,
  967. const struct __ip6_tnl_parm *p)
  968. {
  969. t->parms.laddr = p->laddr;
  970. t->parms.raddr = p->raddr;
  971. t->parms.flags = p->flags;
  972. t->parms.hop_limit = p->hop_limit;
  973. t->parms.encap_limit = p->encap_limit;
  974. t->parms.flowinfo = p->flowinfo;
  975. t->parms.link = p->link;
  976. t->parms.proto = p->proto;
  977. t->parms.i_key = p->i_key;
  978. t->parms.o_key = p->o_key;
  979. t->parms.i_flags = p->i_flags;
  980. t->parms.o_flags = p->o_flags;
  981. t->parms.fwmark = p->fwmark;
  982. dst_cache_reset(&t->dst_cache);
  983. }
  984. static int ip6gre_tnl_change(struct ip6_tnl *t, const struct __ip6_tnl_parm *p,
  985. int set_mtu)
  986. {
  987. ip6gre_tnl_copy_tnl_parm(t, p);
  988. ip6gre_tnl_link_config(t, set_mtu);
  989. return 0;
  990. }
  991. static void ip6gre_tnl_parm_from_user(struct __ip6_tnl_parm *p,
  992. const struct ip6_tnl_parm2 *u)
  993. {
  994. p->laddr = u->laddr;
  995. p->raddr = u->raddr;
  996. p->flags = u->flags;
  997. p->hop_limit = u->hop_limit;
  998. p->encap_limit = u->encap_limit;
  999. p->flowinfo = u->flowinfo;
  1000. p->link = u->link;
  1001. p->i_key = u->i_key;
  1002. p->o_key = u->o_key;
  1003. p->i_flags = gre_flags_to_tnl_flags(u->i_flags);
  1004. p->o_flags = gre_flags_to_tnl_flags(u->o_flags);
  1005. memcpy(p->name, u->name, sizeof(u->name));
  1006. }
  1007. static void ip6gre_tnl_parm_to_user(struct ip6_tnl_parm2 *u,
  1008. const struct __ip6_tnl_parm *p)
  1009. {
  1010. u->proto = IPPROTO_GRE;
  1011. u->laddr = p->laddr;
  1012. u->raddr = p->raddr;
  1013. u->flags = p->flags;
  1014. u->hop_limit = p->hop_limit;
  1015. u->encap_limit = p->encap_limit;
  1016. u->flowinfo = p->flowinfo;
  1017. u->link = p->link;
  1018. u->i_key = p->i_key;
  1019. u->o_key = p->o_key;
  1020. u->i_flags = gre_tnl_flags_to_gre_flags(p->i_flags);
  1021. u->o_flags = gre_tnl_flags_to_gre_flags(p->o_flags);
  1022. memcpy(u->name, p->name, sizeof(u->name));
  1023. }
  1024. static int ip6gre_tunnel_ioctl(struct net_device *dev,
  1025. struct ifreq *ifr, int cmd)
  1026. {
  1027. int err = 0;
  1028. struct ip6_tnl_parm2 p;
  1029. struct __ip6_tnl_parm p1;
  1030. struct ip6_tnl *t = netdev_priv(dev);
  1031. struct net *net = t->net;
  1032. struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
  1033. memset(&p1, 0, sizeof(p1));
  1034. switch (cmd) {
  1035. case SIOCGETTUNNEL:
  1036. if (dev == ign->fb_tunnel_dev) {
  1037. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) {
  1038. err = -EFAULT;
  1039. break;
  1040. }
  1041. ip6gre_tnl_parm_from_user(&p1, &p);
  1042. t = ip6gre_tunnel_locate(net, &p1, 0);
  1043. if (!t)
  1044. t = netdev_priv(dev);
  1045. }
  1046. memset(&p, 0, sizeof(p));
  1047. ip6gre_tnl_parm_to_user(&p, &t->parms);
  1048. if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p)))
  1049. err = -EFAULT;
  1050. break;
  1051. case SIOCADDTUNNEL:
  1052. case SIOCCHGTUNNEL:
  1053. err = -EPERM;
  1054. if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  1055. goto done;
  1056. err = -EFAULT;
  1057. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
  1058. goto done;
  1059. err = -EINVAL;
  1060. if ((p.i_flags|p.o_flags)&(GRE_VERSION|GRE_ROUTING))
  1061. goto done;
  1062. if (!(p.i_flags&GRE_KEY))
  1063. p.i_key = 0;
  1064. if (!(p.o_flags&GRE_KEY))
  1065. p.o_key = 0;
  1066. ip6gre_tnl_parm_from_user(&p1, &p);
  1067. t = ip6gre_tunnel_locate(net, &p1, cmd == SIOCADDTUNNEL);
  1068. if (dev != ign->fb_tunnel_dev && cmd == SIOCCHGTUNNEL) {
  1069. if (t) {
  1070. if (t->dev != dev) {
  1071. err = -EEXIST;
  1072. break;
  1073. }
  1074. } else {
  1075. t = netdev_priv(dev);
  1076. ip6gre_tunnel_unlink(ign, t);
  1077. synchronize_net();
  1078. ip6gre_tnl_change(t, &p1, 1);
  1079. ip6gre_tunnel_link(ign, t);
  1080. netdev_state_change(dev);
  1081. }
  1082. }
  1083. if (t) {
  1084. err = 0;
  1085. memset(&p, 0, sizeof(p));
  1086. ip6gre_tnl_parm_to_user(&p, &t->parms);
  1087. if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p)))
  1088. err = -EFAULT;
  1089. } else
  1090. err = (cmd == SIOCADDTUNNEL ? -ENOBUFS : -ENOENT);
  1091. break;
  1092. case SIOCDELTUNNEL:
  1093. err = -EPERM;
  1094. if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  1095. goto done;
  1096. if (dev == ign->fb_tunnel_dev) {
  1097. err = -EFAULT;
  1098. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
  1099. goto done;
  1100. err = -ENOENT;
  1101. ip6gre_tnl_parm_from_user(&p1, &p);
  1102. t = ip6gre_tunnel_locate(net, &p1, 0);
  1103. if (!t)
  1104. goto done;
  1105. err = -EPERM;
  1106. if (t == netdev_priv(ign->fb_tunnel_dev))
  1107. goto done;
  1108. dev = t->dev;
  1109. }
  1110. unregister_netdevice(dev);
  1111. err = 0;
  1112. break;
  1113. default:
  1114. err = -EINVAL;
  1115. }
  1116. done:
  1117. return err;
  1118. }
  1119. static int ip6gre_header(struct sk_buff *skb, struct net_device *dev,
  1120. unsigned short type, const void *daddr,
  1121. const void *saddr, unsigned int len)
  1122. {
  1123. struct ip6_tnl *t = netdev_priv(dev);
  1124. struct ipv6hdr *ipv6h;
  1125. __be16 *p;
  1126. ipv6h = skb_push(skb, t->hlen + sizeof(*ipv6h));
  1127. ip6_flow_hdr(ipv6h, 0, ip6_make_flowlabel(dev_net(dev), skb,
  1128. t->fl.u.ip6.flowlabel,
  1129. true, &t->fl.u.ip6));
  1130. ipv6h->hop_limit = t->parms.hop_limit;
  1131. ipv6h->nexthdr = NEXTHDR_GRE;
  1132. ipv6h->saddr = t->parms.laddr;
  1133. ipv6h->daddr = t->parms.raddr;
  1134. p = (__be16 *)(ipv6h + 1);
  1135. p[0] = t->parms.o_flags;
  1136. p[1] = htons(type);
  1137. /*
  1138. * Set the source hardware address.
  1139. */
  1140. if (saddr)
  1141. memcpy(&ipv6h->saddr, saddr, sizeof(struct in6_addr));
  1142. if (daddr)
  1143. memcpy(&ipv6h->daddr, daddr, sizeof(struct in6_addr));
  1144. if (!ipv6_addr_any(&ipv6h->daddr))
  1145. return t->hlen;
  1146. return -t->hlen;
  1147. }
  1148. static const struct header_ops ip6gre_header_ops = {
  1149. .create = ip6gre_header,
  1150. };
  1151. static const struct net_device_ops ip6gre_netdev_ops = {
  1152. .ndo_init = ip6gre_tunnel_init,
  1153. .ndo_uninit = ip6gre_tunnel_uninit,
  1154. .ndo_start_xmit = ip6gre_tunnel_xmit,
  1155. .ndo_do_ioctl = ip6gre_tunnel_ioctl,
  1156. .ndo_change_mtu = ip6_tnl_change_mtu,
  1157. .ndo_get_stats64 = ip_tunnel_get_stats64,
  1158. .ndo_get_iflink = ip6_tnl_get_iflink,
  1159. };
  1160. static void ip6gre_dev_free(struct net_device *dev)
  1161. {
  1162. struct ip6_tnl *t = netdev_priv(dev);
  1163. gro_cells_destroy(&t->gro_cells);
  1164. dst_cache_destroy(&t->dst_cache);
  1165. free_percpu(dev->tstats);
  1166. }
  1167. static void ip6gre_tunnel_setup(struct net_device *dev)
  1168. {
  1169. dev->netdev_ops = &ip6gre_netdev_ops;
  1170. dev->needs_free_netdev = true;
  1171. dev->priv_destructor = ip6gre_dev_free;
  1172. dev->type = ARPHRD_IP6GRE;
  1173. dev->flags |= IFF_NOARP;
  1174. dev->addr_len = sizeof(struct in6_addr);
  1175. netif_keep_dst(dev);
  1176. /* This perm addr will be used as interface identifier by IPv6 */
  1177. dev->addr_assign_type = NET_ADDR_RANDOM;
  1178. eth_random_addr(dev->perm_addr);
  1179. }
  1180. #define GRE6_FEATURES (NETIF_F_SG | \
  1181. NETIF_F_FRAGLIST | \
  1182. NETIF_F_HIGHDMA | \
  1183. NETIF_F_HW_CSUM)
  1184. static void ip6gre_tnl_init_features(struct net_device *dev)
  1185. {
  1186. struct ip6_tnl *nt = netdev_priv(dev);
  1187. dev->features |= GRE6_FEATURES;
  1188. dev->hw_features |= GRE6_FEATURES;
  1189. if (!(nt->parms.o_flags & TUNNEL_SEQ)) {
  1190. /* TCP offload with GRE SEQ is not supported, nor
  1191. * can we support 2 levels of outer headers requiring
  1192. * an update.
  1193. */
  1194. if (!(nt->parms.o_flags & TUNNEL_CSUM) ||
  1195. nt->encap.type == TUNNEL_ENCAP_NONE) {
  1196. dev->features |= NETIF_F_GSO_SOFTWARE;
  1197. dev->hw_features |= NETIF_F_GSO_SOFTWARE;
  1198. }
  1199. /* Can use a lockless transmit, unless we generate
  1200. * output sequences
  1201. */
  1202. dev->features |= NETIF_F_LLTX;
  1203. }
  1204. }
  1205. static int ip6gre_tunnel_init_common(struct net_device *dev)
  1206. {
  1207. struct ip6_tnl *tunnel;
  1208. int ret;
  1209. int t_hlen;
  1210. tunnel = netdev_priv(dev);
  1211. tunnel->dev = dev;
  1212. tunnel->net = dev_net(dev);
  1213. strcpy(tunnel->parms.name, dev->name);
  1214. dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
  1215. if (!dev->tstats)
  1216. return -ENOMEM;
  1217. ret = dst_cache_init(&tunnel->dst_cache, GFP_KERNEL);
  1218. if (ret)
  1219. goto cleanup_alloc_pcpu_stats;
  1220. ret = gro_cells_init(&tunnel->gro_cells, dev);
  1221. if (ret)
  1222. goto cleanup_dst_cache_init;
  1223. t_hlen = ip6gre_calc_hlen(tunnel);
  1224. dev->mtu = ETH_DATA_LEN - t_hlen;
  1225. if (dev->type == ARPHRD_ETHER)
  1226. dev->mtu -= ETH_HLEN;
  1227. if (!(tunnel->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
  1228. dev->mtu -= 8;
  1229. if (tunnel->parms.collect_md) {
  1230. dev->features |= NETIF_F_NETNS_LOCAL;
  1231. netif_keep_dst(dev);
  1232. }
  1233. ip6gre_tnl_init_features(dev);
  1234. return 0;
  1235. cleanup_dst_cache_init:
  1236. dst_cache_destroy(&tunnel->dst_cache);
  1237. cleanup_alloc_pcpu_stats:
  1238. free_percpu(dev->tstats);
  1239. dev->tstats = NULL;
  1240. return ret;
  1241. }
  1242. static int ip6gre_tunnel_init(struct net_device *dev)
  1243. {
  1244. struct ip6_tnl *tunnel;
  1245. int ret;
  1246. ret = ip6gre_tunnel_init_common(dev);
  1247. if (ret)
  1248. return ret;
  1249. tunnel = netdev_priv(dev);
  1250. if (tunnel->parms.collect_md)
  1251. return 0;
  1252. memcpy(dev->dev_addr, &tunnel->parms.laddr, sizeof(struct in6_addr));
  1253. memcpy(dev->broadcast, &tunnel->parms.raddr, sizeof(struct in6_addr));
  1254. if (ipv6_addr_any(&tunnel->parms.raddr))
  1255. dev->header_ops = &ip6gre_header_ops;
  1256. return 0;
  1257. }
  1258. static void ip6gre_fb_tunnel_init(struct net_device *dev)
  1259. {
  1260. struct ip6_tnl *tunnel = netdev_priv(dev);
  1261. tunnel->dev = dev;
  1262. tunnel->net = dev_net(dev);
  1263. strcpy(tunnel->parms.name, dev->name);
  1264. tunnel->hlen = sizeof(struct ipv6hdr) + 4;
  1265. dev_hold(dev);
  1266. }
  1267. static struct inet6_protocol ip6gre_protocol __read_mostly = {
  1268. .handler = gre_rcv,
  1269. .err_handler = ip6gre_err,
  1270. .flags = INET6_PROTO_NOPOLICY|INET6_PROTO_FINAL,
  1271. };
  1272. static void ip6gre_destroy_tunnels(struct net *net, struct list_head *head)
  1273. {
  1274. struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
  1275. struct net_device *dev, *aux;
  1276. int prio;
  1277. for_each_netdev_safe(net, dev, aux)
  1278. if (dev->rtnl_link_ops == &ip6gre_link_ops ||
  1279. dev->rtnl_link_ops == &ip6gre_tap_ops ||
  1280. dev->rtnl_link_ops == &ip6erspan_tap_ops)
  1281. unregister_netdevice_queue(dev, head);
  1282. for (prio = 0; prio < 4; prio++) {
  1283. int h;
  1284. for (h = 0; h < IP6_GRE_HASH_SIZE; h++) {
  1285. struct ip6_tnl *t;
  1286. t = rtnl_dereference(ign->tunnels[prio][h]);
  1287. while (t) {
  1288. /* If dev is in the same netns, it has already
  1289. * been added to the list by the previous loop.
  1290. */
  1291. if (!net_eq(dev_net(t->dev), net))
  1292. unregister_netdevice_queue(t->dev,
  1293. head);
  1294. t = rtnl_dereference(t->next);
  1295. }
  1296. }
  1297. }
  1298. }
  1299. static int __net_init ip6gre_init_net(struct net *net)
  1300. {
  1301. struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
  1302. int err;
  1303. if (!net_has_fallback_tunnels(net))
  1304. return 0;
  1305. ign->fb_tunnel_dev = alloc_netdev(sizeof(struct ip6_tnl), "ip6gre0",
  1306. NET_NAME_UNKNOWN,
  1307. ip6gre_tunnel_setup);
  1308. if (!ign->fb_tunnel_dev) {
  1309. err = -ENOMEM;
  1310. goto err_alloc_dev;
  1311. }
  1312. dev_net_set(ign->fb_tunnel_dev, net);
  1313. /* FB netdevice is special: we have one, and only one per netns.
  1314. * Allowing to move it to another netns is clearly unsafe.
  1315. */
  1316. ign->fb_tunnel_dev->features |= NETIF_F_NETNS_LOCAL;
  1317. ip6gre_fb_tunnel_init(ign->fb_tunnel_dev);
  1318. ign->fb_tunnel_dev->rtnl_link_ops = &ip6gre_link_ops;
  1319. err = register_netdev(ign->fb_tunnel_dev);
  1320. if (err)
  1321. goto err_reg_dev;
  1322. rcu_assign_pointer(ign->tunnels_wc[0],
  1323. netdev_priv(ign->fb_tunnel_dev));
  1324. return 0;
  1325. err_reg_dev:
  1326. free_netdev(ign->fb_tunnel_dev);
  1327. err_alloc_dev:
  1328. return err;
  1329. }
  1330. static void __net_exit ip6gre_exit_batch_net(struct list_head *net_list)
  1331. {
  1332. struct net *net;
  1333. LIST_HEAD(list);
  1334. rtnl_lock();
  1335. list_for_each_entry(net, net_list, exit_list)
  1336. ip6gre_destroy_tunnels(net, &list);
  1337. unregister_netdevice_many(&list);
  1338. rtnl_unlock();
  1339. }
  1340. static struct pernet_operations ip6gre_net_ops = {
  1341. .init = ip6gre_init_net,
  1342. .exit_batch = ip6gre_exit_batch_net,
  1343. .id = &ip6gre_net_id,
  1344. .size = sizeof(struct ip6gre_net),
  1345. };
  1346. static int ip6gre_tunnel_validate(struct nlattr *tb[], struct nlattr *data[],
  1347. struct netlink_ext_ack *extack)
  1348. {
  1349. __be16 flags;
  1350. if (!data)
  1351. return 0;
  1352. flags = 0;
  1353. if (data[IFLA_GRE_IFLAGS])
  1354. flags |= nla_get_be16(data[IFLA_GRE_IFLAGS]);
  1355. if (data[IFLA_GRE_OFLAGS])
  1356. flags |= nla_get_be16(data[IFLA_GRE_OFLAGS]);
  1357. if (flags & (GRE_VERSION|GRE_ROUTING))
  1358. return -EINVAL;
  1359. return 0;
  1360. }
  1361. static int ip6gre_tap_validate(struct nlattr *tb[], struct nlattr *data[],
  1362. struct netlink_ext_ack *extack)
  1363. {
  1364. struct in6_addr daddr;
  1365. if (tb[IFLA_ADDRESS]) {
  1366. if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
  1367. return -EINVAL;
  1368. if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
  1369. return -EADDRNOTAVAIL;
  1370. }
  1371. if (!data)
  1372. goto out;
  1373. if (data[IFLA_GRE_REMOTE]) {
  1374. daddr = nla_get_in6_addr(data[IFLA_GRE_REMOTE]);
  1375. if (ipv6_addr_any(&daddr))
  1376. return -EINVAL;
  1377. }
  1378. out:
  1379. return ip6gre_tunnel_validate(tb, data, extack);
  1380. }
  1381. static int ip6erspan_tap_validate(struct nlattr *tb[], struct nlattr *data[],
  1382. struct netlink_ext_ack *extack)
  1383. {
  1384. __be16 flags = 0;
  1385. int ret, ver = 0;
  1386. if (!data)
  1387. return 0;
  1388. ret = ip6gre_tap_validate(tb, data, extack);
  1389. if (ret)
  1390. return ret;
  1391. /* ERSPAN should only have GRE sequence and key flag */
  1392. if (data[IFLA_GRE_OFLAGS])
  1393. flags |= nla_get_be16(data[IFLA_GRE_OFLAGS]);
  1394. if (data[IFLA_GRE_IFLAGS])
  1395. flags |= nla_get_be16(data[IFLA_GRE_IFLAGS]);
  1396. if (!data[IFLA_GRE_COLLECT_METADATA] &&
  1397. flags != (GRE_SEQ | GRE_KEY))
  1398. return -EINVAL;
  1399. /* ERSPAN Session ID only has 10-bit. Since we reuse
  1400. * 32-bit key field as ID, check it's range.
  1401. */
  1402. if (data[IFLA_GRE_IKEY] &&
  1403. (ntohl(nla_get_be32(data[IFLA_GRE_IKEY])) & ~ID_MASK))
  1404. return -EINVAL;
  1405. if (data[IFLA_GRE_OKEY] &&
  1406. (ntohl(nla_get_be32(data[IFLA_GRE_OKEY])) & ~ID_MASK))
  1407. return -EINVAL;
  1408. if (data[IFLA_GRE_ERSPAN_VER]) {
  1409. ver = nla_get_u8(data[IFLA_GRE_ERSPAN_VER]);
  1410. if (ver != 1 && ver != 2)
  1411. return -EINVAL;
  1412. }
  1413. if (ver == 1) {
  1414. if (data[IFLA_GRE_ERSPAN_INDEX]) {
  1415. u32 index = nla_get_u32(data[IFLA_GRE_ERSPAN_INDEX]);
  1416. if (index & ~INDEX_MASK)
  1417. return -EINVAL;
  1418. }
  1419. } else if (ver == 2) {
  1420. if (data[IFLA_GRE_ERSPAN_DIR]) {
  1421. u16 dir = nla_get_u8(data[IFLA_GRE_ERSPAN_DIR]);
  1422. if (dir & ~(DIR_MASK >> DIR_OFFSET))
  1423. return -EINVAL;
  1424. }
  1425. if (data[IFLA_GRE_ERSPAN_HWID]) {
  1426. u16 hwid = nla_get_u16(data[IFLA_GRE_ERSPAN_HWID]);
  1427. if (hwid & ~(HWID_MASK >> HWID_OFFSET))
  1428. return -EINVAL;
  1429. }
  1430. }
  1431. return 0;
  1432. }
  1433. static void ip6gre_netlink_parms(struct nlattr *data[],
  1434. struct __ip6_tnl_parm *parms)
  1435. {
  1436. memset(parms, 0, sizeof(*parms));
  1437. if (!data)
  1438. return;
  1439. if (data[IFLA_GRE_LINK])
  1440. parms->link = nla_get_u32(data[IFLA_GRE_LINK]);
  1441. if (data[IFLA_GRE_IFLAGS])
  1442. parms->i_flags = gre_flags_to_tnl_flags(
  1443. nla_get_be16(data[IFLA_GRE_IFLAGS]));
  1444. if (data[IFLA_GRE_OFLAGS])
  1445. parms->o_flags = gre_flags_to_tnl_flags(
  1446. nla_get_be16(data[IFLA_GRE_OFLAGS]));
  1447. if (data[IFLA_GRE_IKEY])
  1448. parms->i_key = nla_get_be32(data[IFLA_GRE_IKEY]);
  1449. if (data[IFLA_GRE_OKEY])
  1450. parms->o_key = nla_get_be32(data[IFLA_GRE_OKEY]);
  1451. if (data[IFLA_GRE_LOCAL])
  1452. parms->laddr = nla_get_in6_addr(data[IFLA_GRE_LOCAL]);
  1453. if (data[IFLA_GRE_REMOTE])
  1454. parms->raddr = nla_get_in6_addr(data[IFLA_GRE_REMOTE]);
  1455. if (data[IFLA_GRE_TTL])
  1456. parms->hop_limit = nla_get_u8(data[IFLA_GRE_TTL]);
  1457. if (data[IFLA_GRE_ENCAP_LIMIT])
  1458. parms->encap_limit = nla_get_u8(data[IFLA_GRE_ENCAP_LIMIT]);
  1459. if (data[IFLA_GRE_FLOWINFO])
  1460. parms->flowinfo = nla_get_be32(data[IFLA_GRE_FLOWINFO]);
  1461. if (data[IFLA_GRE_FLAGS])
  1462. parms->flags = nla_get_u32(data[IFLA_GRE_FLAGS]);
  1463. if (data[IFLA_GRE_FWMARK])
  1464. parms->fwmark = nla_get_u32(data[IFLA_GRE_FWMARK]);
  1465. if (data[IFLA_GRE_COLLECT_METADATA])
  1466. parms->collect_md = true;
  1467. if (data[IFLA_GRE_ERSPAN_VER])
  1468. parms->erspan_ver = nla_get_u8(data[IFLA_GRE_ERSPAN_VER]);
  1469. if (parms->erspan_ver == 1) {
  1470. if (data[IFLA_GRE_ERSPAN_INDEX])
  1471. parms->index = nla_get_u32(data[IFLA_GRE_ERSPAN_INDEX]);
  1472. } else if (parms->erspan_ver == 2) {
  1473. if (data[IFLA_GRE_ERSPAN_DIR])
  1474. parms->dir = nla_get_u8(data[IFLA_GRE_ERSPAN_DIR]);
  1475. if (data[IFLA_GRE_ERSPAN_HWID])
  1476. parms->hwid = nla_get_u16(data[IFLA_GRE_ERSPAN_HWID]);
  1477. }
  1478. }
  1479. static int ip6gre_tap_init(struct net_device *dev)
  1480. {
  1481. int ret;
  1482. ret = ip6gre_tunnel_init_common(dev);
  1483. if (ret)
  1484. return ret;
  1485. dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
  1486. return 0;
  1487. }
  1488. static const struct net_device_ops ip6gre_tap_netdev_ops = {
  1489. .ndo_init = ip6gre_tap_init,
  1490. .ndo_uninit = ip6gre_tunnel_uninit,
  1491. .ndo_start_xmit = ip6gre_tunnel_xmit,
  1492. .ndo_set_mac_address = eth_mac_addr,
  1493. .ndo_validate_addr = eth_validate_addr,
  1494. .ndo_change_mtu = ip6_tnl_change_mtu,
  1495. .ndo_get_stats64 = ip_tunnel_get_stats64,
  1496. .ndo_get_iflink = ip6_tnl_get_iflink,
  1497. };
  1498. static int ip6erspan_calc_hlen(struct ip6_tnl *tunnel)
  1499. {
  1500. int t_hlen;
  1501. tunnel->tun_hlen = 8;
  1502. tunnel->hlen = tunnel->tun_hlen + tunnel->encap_hlen +
  1503. erspan_hdr_len(tunnel->parms.erspan_ver);
  1504. t_hlen = tunnel->hlen + sizeof(struct ipv6hdr);
  1505. tunnel->dev->needed_headroom = LL_MAX_HEADER + t_hlen;
  1506. return t_hlen;
  1507. }
  1508. static int ip6erspan_tap_init(struct net_device *dev)
  1509. {
  1510. struct ip6_tnl *tunnel;
  1511. int t_hlen;
  1512. int ret;
  1513. tunnel = netdev_priv(dev);
  1514. tunnel->dev = dev;
  1515. tunnel->net = dev_net(dev);
  1516. strcpy(tunnel->parms.name, dev->name);
  1517. dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
  1518. if (!dev->tstats)
  1519. return -ENOMEM;
  1520. ret = dst_cache_init(&tunnel->dst_cache, GFP_KERNEL);
  1521. if (ret)
  1522. goto cleanup_alloc_pcpu_stats;
  1523. ret = gro_cells_init(&tunnel->gro_cells, dev);
  1524. if (ret)
  1525. goto cleanup_dst_cache_init;
  1526. t_hlen = ip6erspan_calc_hlen(tunnel);
  1527. dev->mtu = ETH_DATA_LEN - t_hlen;
  1528. if (dev->type == ARPHRD_ETHER)
  1529. dev->mtu -= ETH_HLEN;
  1530. if (!(tunnel->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
  1531. dev->mtu -= 8;
  1532. dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
  1533. ip6erspan_tnl_link_config(tunnel, 1);
  1534. return 0;
  1535. cleanup_dst_cache_init:
  1536. dst_cache_destroy(&tunnel->dst_cache);
  1537. cleanup_alloc_pcpu_stats:
  1538. free_percpu(dev->tstats);
  1539. dev->tstats = NULL;
  1540. return ret;
  1541. }
  1542. static const struct net_device_ops ip6erspan_netdev_ops = {
  1543. .ndo_init = ip6erspan_tap_init,
  1544. .ndo_uninit = ip6erspan_tunnel_uninit,
  1545. .ndo_start_xmit = ip6erspan_tunnel_xmit,
  1546. .ndo_set_mac_address = eth_mac_addr,
  1547. .ndo_validate_addr = eth_validate_addr,
  1548. .ndo_change_mtu = ip6_tnl_change_mtu,
  1549. .ndo_get_stats64 = ip_tunnel_get_stats64,
  1550. .ndo_get_iflink = ip6_tnl_get_iflink,
  1551. };
  1552. static void ip6gre_tap_setup(struct net_device *dev)
  1553. {
  1554. ether_setup(dev);
  1555. dev->max_mtu = 0;
  1556. dev->netdev_ops = &ip6gre_tap_netdev_ops;
  1557. dev->needs_free_netdev = true;
  1558. dev->priv_destructor = ip6gre_dev_free;
  1559. dev->features |= NETIF_F_NETNS_LOCAL;
  1560. dev->priv_flags &= ~IFF_TX_SKB_SHARING;
  1561. dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
  1562. netif_keep_dst(dev);
  1563. }
  1564. bool is_ip6gretap_dev(const struct net_device *dev)
  1565. {
  1566. return dev->netdev_ops == &ip6gre_tap_netdev_ops;
  1567. }
  1568. EXPORT_SYMBOL_GPL(is_ip6gretap_dev);
  1569. static bool ip6gre_netlink_encap_parms(struct nlattr *data[],
  1570. struct ip_tunnel_encap *ipencap)
  1571. {
  1572. bool ret = false;
  1573. memset(ipencap, 0, sizeof(*ipencap));
  1574. if (!data)
  1575. return ret;
  1576. if (data[IFLA_GRE_ENCAP_TYPE]) {
  1577. ret = true;
  1578. ipencap->type = nla_get_u16(data[IFLA_GRE_ENCAP_TYPE]);
  1579. }
  1580. if (data[IFLA_GRE_ENCAP_FLAGS]) {
  1581. ret = true;
  1582. ipencap->flags = nla_get_u16(data[IFLA_GRE_ENCAP_FLAGS]);
  1583. }
  1584. if (data[IFLA_GRE_ENCAP_SPORT]) {
  1585. ret = true;
  1586. ipencap->sport = nla_get_be16(data[IFLA_GRE_ENCAP_SPORT]);
  1587. }
  1588. if (data[IFLA_GRE_ENCAP_DPORT]) {
  1589. ret = true;
  1590. ipencap->dport = nla_get_be16(data[IFLA_GRE_ENCAP_DPORT]);
  1591. }
  1592. return ret;
  1593. }
  1594. static int ip6gre_newlink_common(struct net *src_net, struct net_device *dev,
  1595. struct nlattr *tb[], struct nlattr *data[],
  1596. struct netlink_ext_ack *extack)
  1597. {
  1598. struct ip6_tnl *nt;
  1599. struct ip_tunnel_encap ipencap;
  1600. int err;
  1601. nt = netdev_priv(dev);
  1602. if (ip6gre_netlink_encap_parms(data, &ipencap)) {
  1603. int err = ip6_tnl_encap_setup(nt, &ipencap);
  1604. if (err < 0)
  1605. return err;
  1606. }
  1607. if (dev->type == ARPHRD_ETHER && !tb[IFLA_ADDRESS])
  1608. eth_hw_addr_random(dev);
  1609. nt->dev = dev;
  1610. nt->net = dev_net(dev);
  1611. err = register_netdevice(dev);
  1612. if (err)
  1613. goto out;
  1614. if (tb[IFLA_MTU])
  1615. ip6_tnl_change_mtu(dev, nla_get_u32(tb[IFLA_MTU]));
  1616. dev_hold(dev);
  1617. out:
  1618. return err;
  1619. }
  1620. static int ip6gre_newlink(struct net *src_net, struct net_device *dev,
  1621. struct nlattr *tb[], struct nlattr *data[],
  1622. struct netlink_ext_ack *extack)
  1623. {
  1624. struct ip6_tnl *nt = netdev_priv(dev);
  1625. struct net *net = dev_net(dev);
  1626. struct ip6gre_net *ign;
  1627. int err;
  1628. ip6gre_netlink_parms(data, &nt->parms);
  1629. ign = net_generic(net, ip6gre_net_id);
  1630. if (nt->parms.collect_md) {
  1631. if (rtnl_dereference(ign->collect_md_tun))
  1632. return -EEXIST;
  1633. } else {
  1634. if (ip6gre_tunnel_find(net, &nt->parms, dev->type))
  1635. return -EEXIST;
  1636. }
  1637. err = ip6gre_newlink_common(src_net, dev, tb, data, extack);
  1638. if (!err) {
  1639. ip6gre_tnl_link_config(nt, !tb[IFLA_MTU]);
  1640. ip6gre_tunnel_link_md(ign, nt);
  1641. ip6gre_tunnel_link(net_generic(net, ip6gre_net_id), nt);
  1642. }
  1643. return err;
  1644. }
  1645. static struct ip6_tnl *
  1646. ip6gre_changelink_common(struct net_device *dev, struct nlattr *tb[],
  1647. struct nlattr *data[], struct __ip6_tnl_parm *p_p,
  1648. struct netlink_ext_ack *extack)
  1649. {
  1650. struct ip6_tnl *t, *nt = netdev_priv(dev);
  1651. struct net *net = nt->net;
  1652. struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
  1653. struct ip_tunnel_encap ipencap;
  1654. if (dev == ign->fb_tunnel_dev)
  1655. return ERR_PTR(-EINVAL);
  1656. if (ip6gre_netlink_encap_parms(data, &ipencap)) {
  1657. int err = ip6_tnl_encap_setup(nt, &ipencap);
  1658. if (err < 0)
  1659. return ERR_PTR(err);
  1660. }
  1661. ip6gre_netlink_parms(data, p_p);
  1662. t = ip6gre_tunnel_locate(net, p_p, 0);
  1663. if (t) {
  1664. if (t->dev != dev)
  1665. return ERR_PTR(-EEXIST);
  1666. } else {
  1667. t = nt;
  1668. }
  1669. return t;
  1670. }
  1671. static int ip6gre_changelink(struct net_device *dev, struct nlattr *tb[],
  1672. struct nlattr *data[],
  1673. struct netlink_ext_ack *extack)
  1674. {
  1675. struct ip6gre_net *ign = net_generic(dev_net(dev), ip6gre_net_id);
  1676. struct __ip6_tnl_parm p;
  1677. struct ip6_tnl *t;
  1678. t = ip6gre_changelink_common(dev, tb, data, &p, extack);
  1679. if (IS_ERR(t))
  1680. return PTR_ERR(t);
  1681. ip6gre_tunnel_unlink_md(ign, t);
  1682. ip6gre_tunnel_unlink(ign, t);
  1683. ip6gre_tnl_change(t, &p, !tb[IFLA_MTU]);
  1684. ip6gre_tunnel_link_md(ign, t);
  1685. ip6gre_tunnel_link(ign, t);
  1686. return 0;
  1687. }
  1688. static void ip6gre_dellink(struct net_device *dev, struct list_head *head)
  1689. {
  1690. struct net *net = dev_net(dev);
  1691. struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
  1692. if (dev != ign->fb_tunnel_dev)
  1693. unregister_netdevice_queue(dev, head);
  1694. }
  1695. static size_t ip6gre_get_size(const struct net_device *dev)
  1696. {
  1697. return
  1698. /* IFLA_GRE_LINK */
  1699. nla_total_size(4) +
  1700. /* IFLA_GRE_IFLAGS */
  1701. nla_total_size(2) +
  1702. /* IFLA_GRE_OFLAGS */
  1703. nla_total_size(2) +
  1704. /* IFLA_GRE_IKEY */
  1705. nla_total_size(4) +
  1706. /* IFLA_GRE_OKEY */
  1707. nla_total_size(4) +
  1708. /* IFLA_GRE_LOCAL */
  1709. nla_total_size(sizeof(struct in6_addr)) +
  1710. /* IFLA_GRE_REMOTE */
  1711. nla_total_size(sizeof(struct in6_addr)) +
  1712. /* IFLA_GRE_TTL */
  1713. nla_total_size(1) +
  1714. /* IFLA_GRE_ENCAP_LIMIT */
  1715. nla_total_size(1) +
  1716. /* IFLA_GRE_FLOWINFO */
  1717. nla_total_size(4) +
  1718. /* IFLA_GRE_FLAGS */
  1719. nla_total_size(4) +
  1720. /* IFLA_GRE_ENCAP_TYPE */
  1721. nla_total_size(2) +
  1722. /* IFLA_GRE_ENCAP_FLAGS */
  1723. nla_total_size(2) +
  1724. /* IFLA_GRE_ENCAP_SPORT */
  1725. nla_total_size(2) +
  1726. /* IFLA_GRE_ENCAP_DPORT */
  1727. nla_total_size(2) +
  1728. /* IFLA_GRE_COLLECT_METADATA */
  1729. nla_total_size(0) +
  1730. /* IFLA_GRE_FWMARK */
  1731. nla_total_size(4) +
  1732. /* IFLA_GRE_ERSPAN_INDEX */
  1733. nla_total_size(4) +
  1734. 0;
  1735. }
  1736. static int ip6gre_fill_info(struct sk_buff *skb, const struct net_device *dev)
  1737. {
  1738. struct ip6_tnl *t = netdev_priv(dev);
  1739. struct __ip6_tnl_parm *p = &t->parms;
  1740. if (nla_put_u32(skb, IFLA_GRE_LINK, p->link) ||
  1741. nla_put_be16(skb, IFLA_GRE_IFLAGS,
  1742. gre_tnl_flags_to_gre_flags(p->i_flags)) ||
  1743. nla_put_be16(skb, IFLA_GRE_OFLAGS,
  1744. gre_tnl_flags_to_gre_flags(p->o_flags)) ||
  1745. nla_put_be32(skb, IFLA_GRE_IKEY, p->i_key) ||
  1746. nla_put_be32(skb, IFLA_GRE_OKEY, p->o_key) ||
  1747. nla_put_in6_addr(skb, IFLA_GRE_LOCAL, &p->laddr) ||
  1748. nla_put_in6_addr(skb, IFLA_GRE_REMOTE, &p->raddr) ||
  1749. nla_put_u8(skb, IFLA_GRE_TTL, p->hop_limit) ||
  1750. nla_put_u8(skb, IFLA_GRE_ENCAP_LIMIT, p->encap_limit) ||
  1751. nla_put_be32(skb, IFLA_GRE_FLOWINFO, p->flowinfo) ||
  1752. nla_put_u32(skb, IFLA_GRE_FLAGS, p->flags) ||
  1753. nla_put_u32(skb, IFLA_GRE_FWMARK, p->fwmark) ||
  1754. nla_put_u32(skb, IFLA_GRE_ERSPAN_INDEX, p->index))
  1755. goto nla_put_failure;
  1756. if (nla_put_u16(skb, IFLA_GRE_ENCAP_TYPE,
  1757. t->encap.type) ||
  1758. nla_put_be16(skb, IFLA_GRE_ENCAP_SPORT,
  1759. t->encap.sport) ||
  1760. nla_put_be16(skb, IFLA_GRE_ENCAP_DPORT,
  1761. t->encap.dport) ||
  1762. nla_put_u16(skb, IFLA_GRE_ENCAP_FLAGS,
  1763. t->encap.flags))
  1764. goto nla_put_failure;
  1765. if (p->collect_md) {
  1766. if (nla_put_flag(skb, IFLA_GRE_COLLECT_METADATA))
  1767. goto nla_put_failure;
  1768. }
  1769. if (nla_put_u8(skb, IFLA_GRE_ERSPAN_VER, p->erspan_ver))
  1770. goto nla_put_failure;
  1771. if (p->erspan_ver == 1) {
  1772. if (nla_put_u32(skb, IFLA_GRE_ERSPAN_INDEX, p->index))
  1773. goto nla_put_failure;
  1774. } else if (p->erspan_ver == 2) {
  1775. if (nla_put_u8(skb, IFLA_GRE_ERSPAN_DIR, p->dir))
  1776. goto nla_put_failure;
  1777. if (nla_put_u16(skb, IFLA_GRE_ERSPAN_HWID, p->hwid))
  1778. goto nla_put_failure;
  1779. }
  1780. return 0;
  1781. nla_put_failure:
  1782. return -EMSGSIZE;
  1783. }
  1784. static const struct nla_policy ip6gre_policy[IFLA_GRE_MAX + 1] = {
  1785. [IFLA_GRE_LINK] = { .type = NLA_U32 },
  1786. [IFLA_GRE_IFLAGS] = { .type = NLA_U16 },
  1787. [IFLA_GRE_OFLAGS] = { .type = NLA_U16 },
  1788. [IFLA_GRE_IKEY] = { .type = NLA_U32 },
  1789. [IFLA_GRE_OKEY] = { .type = NLA_U32 },
  1790. [IFLA_GRE_LOCAL] = { .len = FIELD_SIZEOF(struct ipv6hdr, saddr) },
  1791. [IFLA_GRE_REMOTE] = { .len = FIELD_SIZEOF(struct ipv6hdr, daddr) },
  1792. [IFLA_GRE_TTL] = { .type = NLA_U8 },
  1793. [IFLA_GRE_ENCAP_LIMIT] = { .type = NLA_U8 },
  1794. [IFLA_GRE_FLOWINFO] = { .type = NLA_U32 },
  1795. [IFLA_GRE_FLAGS] = { .type = NLA_U32 },
  1796. [IFLA_GRE_ENCAP_TYPE] = { .type = NLA_U16 },
  1797. [IFLA_GRE_ENCAP_FLAGS] = { .type = NLA_U16 },
  1798. [IFLA_GRE_ENCAP_SPORT] = { .type = NLA_U16 },
  1799. [IFLA_GRE_ENCAP_DPORT] = { .type = NLA_U16 },
  1800. [IFLA_GRE_COLLECT_METADATA] = { .type = NLA_FLAG },
  1801. [IFLA_GRE_FWMARK] = { .type = NLA_U32 },
  1802. [IFLA_GRE_ERSPAN_INDEX] = { .type = NLA_U32 },
  1803. [IFLA_GRE_ERSPAN_VER] = { .type = NLA_U8 },
  1804. [IFLA_GRE_ERSPAN_DIR] = { .type = NLA_U8 },
  1805. [IFLA_GRE_ERSPAN_HWID] = { .type = NLA_U16 },
  1806. };
  1807. static void ip6erspan_tap_setup(struct net_device *dev)
  1808. {
  1809. ether_setup(dev);
  1810. dev->netdev_ops = &ip6erspan_netdev_ops;
  1811. dev->needs_free_netdev = true;
  1812. dev->priv_destructor = ip6gre_dev_free;
  1813. dev->features |= NETIF_F_NETNS_LOCAL;
  1814. dev->priv_flags &= ~IFF_TX_SKB_SHARING;
  1815. dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
  1816. netif_keep_dst(dev);
  1817. }
  1818. static int ip6erspan_newlink(struct net *src_net, struct net_device *dev,
  1819. struct nlattr *tb[], struct nlattr *data[],
  1820. struct netlink_ext_ack *extack)
  1821. {
  1822. struct ip6_tnl *nt = netdev_priv(dev);
  1823. struct net *net = dev_net(dev);
  1824. struct ip6gre_net *ign;
  1825. int err;
  1826. ip6gre_netlink_parms(data, &nt->parms);
  1827. ign = net_generic(net, ip6gre_net_id);
  1828. if (nt->parms.collect_md) {
  1829. if (rtnl_dereference(ign->collect_md_tun_erspan))
  1830. return -EEXIST;
  1831. } else {
  1832. if (ip6gre_tunnel_find(net, &nt->parms, dev->type))
  1833. return -EEXIST;
  1834. }
  1835. err = ip6gre_newlink_common(src_net, dev, tb, data, extack);
  1836. if (!err) {
  1837. ip6erspan_tnl_link_config(nt, !tb[IFLA_MTU]);
  1838. ip6erspan_tunnel_link_md(ign, nt);
  1839. ip6gre_tunnel_link(net_generic(net, ip6gre_net_id), nt);
  1840. }
  1841. return err;
  1842. }
  1843. static void ip6erspan_tnl_link_config(struct ip6_tnl *t, int set_mtu)
  1844. {
  1845. ip6gre_tnl_link_config_common(t);
  1846. ip6gre_tnl_link_config_route(t, set_mtu, ip6erspan_calc_hlen(t));
  1847. }
  1848. static int ip6erspan_tnl_change(struct ip6_tnl *t,
  1849. const struct __ip6_tnl_parm *p, int set_mtu)
  1850. {
  1851. ip6gre_tnl_copy_tnl_parm(t, p);
  1852. ip6erspan_tnl_link_config(t, set_mtu);
  1853. return 0;
  1854. }
  1855. static int ip6erspan_changelink(struct net_device *dev, struct nlattr *tb[],
  1856. struct nlattr *data[],
  1857. struct netlink_ext_ack *extack)
  1858. {
  1859. struct ip6gre_net *ign = net_generic(dev_net(dev), ip6gre_net_id);
  1860. struct __ip6_tnl_parm p;
  1861. struct ip6_tnl *t;
  1862. t = ip6gre_changelink_common(dev, tb, data, &p, extack);
  1863. if (IS_ERR(t))
  1864. return PTR_ERR(t);
  1865. ip6gre_tunnel_unlink_md(ign, t);
  1866. ip6gre_tunnel_unlink(ign, t);
  1867. ip6erspan_tnl_change(t, &p, !tb[IFLA_MTU]);
  1868. ip6erspan_tunnel_link_md(ign, t);
  1869. ip6gre_tunnel_link(ign, t);
  1870. return 0;
  1871. }
  1872. static struct rtnl_link_ops ip6gre_link_ops __read_mostly = {
  1873. .kind = "ip6gre",
  1874. .maxtype = IFLA_GRE_MAX,
  1875. .policy = ip6gre_policy,
  1876. .priv_size = sizeof(struct ip6_tnl),
  1877. .setup = ip6gre_tunnel_setup,
  1878. .validate = ip6gre_tunnel_validate,
  1879. .newlink = ip6gre_newlink,
  1880. .changelink = ip6gre_changelink,
  1881. .dellink = ip6gre_dellink,
  1882. .get_size = ip6gre_get_size,
  1883. .fill_info = ip6gre_fill_info,
  1884. .get_link_net = ip6_tnl_get_link_net,
  1885. };
  1886. static struct rtnl_link_ops ip6gre_tap_ops __read_mostly = {
  1887. .kind = "ip6gretap",
  1888. .maxtype = IFLA_GRE_MAX,
  1889. .policy = ip6gre_policy,
  1890. .priv_size = sizeof(struct ip6_tnl),
  1891. .setup = ip6gre_tap_setup,
  1892. .validate = ip6gre_tap_validate,
  1893. .newlink = ip6gre_newlink,
  1894. .changelink = ip6gre_changelink,
  1895. .get_size = ip6gre_get_size,
  1896. .fill_info = ip6gre_fill_info,
  1897. .get_link_net = ip6_tnl_get_link_net,
  1898. };
  1899. static struct rtnl_link_ops ip6erspan_tap_ops __read_mostly = {
  1900. .kind = "ip6erspan",
  1901. .maxtype = IFLA_GRE_MAX,
  1902. .policy = ip6gre_policy,
  1903. .priv_size = sizeof(struct ip6_tnl),
  1904. .setup = ip6erspan_tap_setup,
  1905. .validate = ip6erspan_tap_validate,
  1906. .newlink = ip6erspan_newlink,
  1907. .changelink = ip6erspan_changelink,
  1908. .get_size = ip6gre_get_size,
  1909. .fill_info = ip6gre_fill_info,
  1910. .get_link_net = ip6_tnl_get_link_net,
  1911. };
  1912. /*
  1913. * And now the modules code and kernel interface.
  1914. */
  1915. static int __init ip6gre_init(void)
  1916. {
  1917. int err;
  1918. pr_info("GRE over IPv6 tunneling driver\n");
  1919. err = register_pernet_device(&ip6gre_net_ops);
  1920. if (err < 0)
  1921. return err;
  1922. err = inet6_add_protocol(&ip6gre_protocol, IPPROTO_GRE);
  1923. if (err < 0) {
  1924. pr_info("%s: can't add protocol\n", __func__);
  1925. goto add_proto_failed;
  1926. }
  1927. err = rtnl_link_register(&ip6gre_link_ops);
  1928. if (err < 0)
  1929. goto rtnl_link_failed;
  1930. err = rtnl_link_register(&ip6gre_tap_ops);
  1931. if (err < 0)
  1932. goto tap_ops_failed;
  1933. err = rtnl_link_register(&ip6erspan_tap_ops);
  1934. if (err < 0)
  1935. goto erspan_link_failed;
  1936. out:
  1937. return err;
  1938. erspan_link_failed:
  1939. rtnl_link_unregister(&ip6gre_tap_ops);
  1940. tap_ops_failed:
  1941. rtnl_link_unregister(&ip6gre_link_ops);
  1942. rtnl_link_failed:
  1943. inet6_del_protocol(&ip6gre_protocol, IPPROTO_GRE);
  1944. add_proto_failed:
  1945. unregister_pernet_device(&ip6gre_net_ops);
  1946. goto out;
  1947. }
  1948. static void __exit ip6gre_fini(void)
  1949. {
  1950. rtnl_link_unregister(&ip6gre_tap_ops);
  1951. rtnl_link_unregister(&ip6gre_link_ops);
  1952. rtnl_link_unregister(&ip6erspan_tap_ops);
  1953. inet6_del_protocol(&ip6gre_protocol, IPPROTO_GRE);
  1954. unregister_pernet_device(&ip6gre_net_ops);
  1955. }
  1956. module_init(ip6gre_init);
  1957. module_exit(ip6gre_fini);
  1958. MODULE_LICENSE("GPL");
  1959. MODULE_AUTHOR("D. Kozlov (xeb@mail.ru)");
  1960. MODULE_DESCRIPTION("GRE over IPv6 tunneling device");
  1961. MODULE_ALIAS_RTNL_LINK("ip6gre");
  1962. MODULE_ALIAS_RTNL_LINK("ip6gretap");
  1963. MODULE_ALIAS_RTNL_LINK("ip6erspan");
  1964. MODULE_ALIAS_NETDEV("ip6gre0");