ip6_gre.c 58 KB

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