ip6_gre.c 58 KB

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