ip6_gre.c 59 KB

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