ip6_gre.c 58 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401
  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, struct tnl_ptk_info *tpi,
  457. int gre_hdr_len)
  458. {
  459. struct erspan_base_hdr *ershdr;
  460. const struct ipv6hdr *ipv6h;
  461. struct erspan_md2 *md2;
  462. struct ip6_tnl *tunnel;
  463. u8 ver;
  464. ipv6h = ipv6_hdr(skb);
  465. ershdr = (struct erspan_base_hdr *)skb->data;
  466. ver = ershdr->ver;
  467. tunnel = ip6gre_tunnel_lookup(skb->dev,
  468. &ipv6h->saddr, &ipv6h->daddr, tpi->key,
  469. tpi->proto);
  470. if (tunnel) {
  471. int len = erspan_hdr_len(ver);
  472. if (unlikely(!pskb_may_pull(skb, len)))
  473. return PACKET_REJECT;
  474. if (__iptunnel_pull_header(skb, len,
  475. htons(ETH_P_TEB),
  476. false, false) < 0)
  477. return PACKET_REJECT;
  478. if (tunnel->parms.collect_md) {
  479. struct erspan_metadata *pkt_md, *md;
  480. struct metadata_dst *tun_dst;
  481. struct ip_tunnel_info *info;
  482. unsigned char *gh;
  483. __be64 tun_id;
  484. __be16 flags;
  485. tpi->flags |= TUNNEL_KEY;
  486. flags = tpi->flags;
  487. tun_id = key32_to_tunnel_id(tpi->key);
  488. tun_dst = ipv6_tun_rx_dst(skb, flags, tun_id,
  489. sizeof(*md));
  490. if (!tun_dst)
  491. return PACKET_REJECT;
  492. /* skb can be uncloned in __iptunnel_pull_header, so
  493. * old pkt_md is no longer valid and we need to reset
  494. * it
  495. */
  496. gh = skb_network_header(skb) +
  497. skb_network_header_len(skb);
  498. pkt_md = (struct erspan_metadata *)(gh + gre_hdr_len +
  499. sizeof(*ershdr));
  500. info = &tun_dst->u.tun_info;
  501. md = ip_tunnel_info_opts(info);
  502. md->version = ver;
  503. md2 = &md->u.md2;
  504. memcpy(md2, pkt_md, ver == 1 ? ERSPAN_V1_MDSIZE :
  505. ERSPAN_V2_MDSIZE);
  506. info->key.tun_flags |= TUNNEL_ERSPAN_OPT;
  507. info->options_len = sizeof(*md);
  508. ip6_tnl_rcv(tunnel, skb, tpi, tun_dst, log_ecn_error);
  509. } else {
  510. ip6_tnl_rcv(tunnel, skb, tpi, NULL, log_ecn_error);
  511. }
  512. return PACKET_RCVD;
  513. }
  514. return PACKET_REJECT;
  515. }
  516. static int gre_rcv(struct sk_buff *skb)
  517. {
  518. struct tnl_ptk_info tpi;
  519. bool csum_err = false;
  520. int hdr_len;
  521. hdr_len = gre_parse_header(skb, &tpi, &csum_err, htons(ETH_P_IPV6), 0);
  522. if (hdr_len < 0)
  523. goto drop;
  524. if (iptunnel_pull_header(skb, hdr_len, tpi.proto, false))
  525. goto drop;
  526. if (unlikely(tpi.proto == htons(ETH_P_ERSPAN) ||
  527. tpi.proto == htons(ETH_P_ERSPAN2))) {
  528. if (ip6erspan_rcv(skb, &tpi, hdr_len) == PACKET_RCVD)
  529. return 0;
  530. goto out;
  531. }
  532. if (ip6gre_rcv(skb, &tpi) == PACKET_RCVD)
  533. return 0;
  534. out:
  535. icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0);
  536. drop:
  537. kfree_skb(skb);
  538. return 0;
  539. }
  540. static int gre_handle_offloads(struct sk_buff *skb, bool csum)
  541. {
  542. return iptunnel_handle_offloads(skb,
  543. csum ? SKB_GSO_GRE_CSUM : SKB_GSO_GRE);
  544. }
  545. static void prepare_ip6gre_xmit_ipv4(struct sk_buff *skb,
  546. struct net_device *dev,
  547. struct flowi6 *fl6, __u8 *dsfield,
  548. int *encap_limit)
  549. {
  550. const struct iphdr *iph = ip_hdr(skb);
  551. struct ip6_tnl *t = netdev_priv(dev);
  552. if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
  553. *encap_limit = t->parms.encap_limit;
  554. memcpy(fl6, &t->fl.u.ip6, sizeof(*fl6));
  555. if (t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS)
  556. *dsfield = ipv4_get_dsfield(iph);
  557. else
  558. *dsfield = ip6_tclass(t->parms.flowinfo);
  559. if (t->parms.flags & IP6_TNL_F_USE_ORIG_FWMARK)
  560. fl6->flowi6_mark = skb->mark;
  561. else
  562. fl6->flowi6_mark = t->parms.fwmark;
  563. fl6->flowi6_uid = sock_net_uid(dev_net(dev), NULL);
  564. }
  565. static int prepare_ip6gre_xmit_ipv6(struct sk_buff *skb,
  566. struct net_device *dev,
  567. struct flowi6 *fl6, __u8 *dsfield,
  568. int *encap_limit)
  569. {
  570. struct ipv6hdr *ipv6h;
  571. struct ip6_tnl *t = netdev_priv(dev);
  572. __u16 offset;
  573. offset = ip6_tnl_parse_tlv_enc_lim(skb, skb_network_header(skb));
  574. /* ip6_tnl_parse_tlv_enc_lim() might have reallocated skb->head */
  575. ipv6h = ipv6_hdr(skb);
  576. if (offset > 0) {
  577. struct ipv6_tlv_tnl_enc_lim *tel;
  578. tel = (struct ipv6_tlv_tnl_enc_lim *)&skb_network_header(skb)[offset];
  579. if (tel->encap_limit == 0) {
  580. icmpv6_send(skb, ICMPV6_PARAMPROB,
  581. ICMPV6_HDR_FIELD, offset + 2);
  582. return -1;
  583. }
  584. *encap_limit = tel->encap_limit - 1;
  585. } else if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT)) {
  586. *encap_limit = t->parms.encap_limit;
  587. }
  588. memcpy(fl6, &t->fl.u.ip6, sizeof(*fl6));
  589. if (t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS)
  590. *dsfield = ipv6_get_dsfield(ipv6h);
  591. else
  592. *dsfield = ip6_tclass(t->parms.flowinfo);
  593. if (t->parms.flags & IP6_TNL_F_USE_ORIG_FLOWLABEL)
  594. fl6->flowlabel |= ip6_flowlabel(ipv6h);
  595. if (t->parms.flags & IP6_TNL_F_USE_ORIG_FWMARK)
  596. fl6->flowi6_mark = skb->mark;
  597. else
  598. fl6->flowi6_mark = t->parms.fwmark;
  599. fl6->flowi6_uid = sock_net_uid(dev_net(dev), NULL);
  600. return 0;
  601. }
  602. static netdev_tx_t __gre6_xmit(struct sk_buff *skb,
  603. struct net_device *dev, __u8 dsfield,
  604. struct flowi6 *fl6, int encap_limit,
  605. __u32 *pmtu, __be16 proto)
  606. {
  607. struct ip6_tnl *tunnel = netdev_priv(dev);
  608. __be16 protocol;
  609. if (dev->type == ARPHRD_ETHER)
  610. IPCB(skb)->flags = 0;
  611. if (dev->header_ops && dev->type == ARPHRD_IP6GRE)
  612. fl6->daddr = ((struct ipv6hdr *)skb->data)->daddr;
  613. else
  614. fl6->daddr = tunnel->parms.raddr;
  615. if (skb_cow_head(skb, dev->needed_headroom ?: tunnel->hlen))
  616. return -ENOMEM;
  617. /* Push GRE header. */
  618. protocol = (dev->type == ARPHRD_ETHER) ? htons(ETH_P_TEB) : proto;
  619. if (tunnel->parms.collect_md) {
  620. struct ip_tunnel_info *tun_info;
  621. const struct ip_tunnel_key *key;
  622. __be16 flags;
  623. tun_info = skb_tunnel_info(skb);
  624. if (unlikely(!tun_info ||
  625. !(tun_info->mode & IP_TUNNEL_INFO_TX) ||
  626. ip_tunnel_info_af(tun_info) != AF_INET6))
  627. return -EINVAL;
  628. key = &tun_info->key;
  629. memset(fl6, 0, sizeof(*fl6));
  630. fl6->flowi6_proto = IPPROTO_GRE;
  631. fl6->daddr = key->u.ipv6.dst;
  632. fl6->flowlabel = key->label;
  633. fl6->flowi6_uid = sock_net_uid(dev_net(dev), NULL);
  634. dsfield = key->tos;
  635. flags = key->tun_flags &
  636. (TUNNEL_CSUM | TUNNEL_KEY | TUNNEL_SEQ);
  637. tunnel->tun_hlen = gre_calc_hlen(flags);
  638. gre_build_header(skb, tunnel->tun_hlen,
  639. flags, protocol,
  640. tunnel_id_to_key32(tun_info->key.tun_id),
  641. (flags & TUNNEL_SEQ) ? htonl(tunnel->o_seqno++)
  642. : 0);
  643. } else {
  644. if (tunnel->parms.o_flags & TUNNEL_SEQ)
  645. tunnel->o_seqno++;
  646. gre_build_header(skb, tunnel->tun_hlen, tunnel->parms.o_flags,
  647. protocol, tunnel->parms.o_key,
  648. htonl(tunnel->o_seqno));
  649. }
  650. return ip6_tnl_xmit(skb, dev, dsfield, fl6, encap_limit, pmtu,
  651. NEXTHDR_GRE);
  652. }
  653. static inline int ip6gre_xmit_ipv4(struct sk_buff *skb, struct net_device *dev)
  654. {
  655. struct ip6_tnl *t = netdev_priv(dev);
  656. int encap_limit = -1;
  657. struct flowi6 fl6;
  658. __u8 dsfield = 0;
  659. __u32 mtu;
  660. int err;
  661. memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
  662. if (!t->parms.collect_md)
  663. prepare_ip6gre_xmit_ipv4(skb, dev, &fl6,
  664. &dsfield, &encap_limit);
  665. err = gre_handle_offloads(skb, !!(t->parms.o_flags & TUNNEL_CSUM));
  666. if (err)
  667. return -1;
  668. err = __gre6_xmit(skb, dev, dsfield, &fl6, encap_limit, &mtu,
  669. skb->protocol);
  670. if (err != 0) {
  671. /* XXX: send ICMP error even if DF is not set. */
  672. if (err == -EMSGSIZE)
  673. icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED,
  674. htonl(mtu));
  675. return -1;
  676. }
  677. return 0;
  678. }
  679. static inline int ip6gre_xmit_ipv6(struct sk_buff *skb, struct net_device *dev)
  680. {
  681. struct ip6_tnl *t = netdev_priv(dev);
  682. struct ipv6hdr *ipv6h = ipv6_hdr(skb);
  683. int encap_limit = -1;
  684. struct flowi6 fl6;
  685. __u8 dsfield = 0;
  686. __u32 mtu;
  687. int err;
  688. if (ipv6_addr_equal(&t->parms.raddr, &ipv6h->saddr))
  689. return -1;
  690. if (!t->parms.collect_md &&
  691. prepare_ip6gre_xmit_ipv6(skb, dev, &fl6, &dsfield, &encap_limit))
  692. return -1;
  693. if (gre_handle_offloads(skb, !!(t->parms.o_flags & TUNNEL_CSUM)))
  694. return -1;
  695. err = __gre6_xmit(skb, dev, dsfield, &fl6, encap_limit,
  696. &mtu, skb->protocol);
  697. if (err != 0) {
  698. if (err == -EMSGSIZE)
  699. icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
  700. return -1;
  701. }
  702. return 0;
  703. }
  704. /**
  705. * ip6gre_tnl_addr_conflict - compare packet addresses to tunnel's own
  706. * @t: the outgoing tunnel device
  707. * @hdr: IPv6 header from the incoming packet
  708. *
  709. * Description:
  710. * Avoid trivial tunneling loop by checking that tunnel exit-point
  711. * doesn't match source of incoming packet.
  712. *
  713. * Return:
  714. * 1 if conflict,
  715. * 0 else
  716. **/
  717. static inline bool ip6gre_tnl_addr_conflict(const struct ip6_tnl *t,
  718. const struct ipv6hdr *hdr)
  719. {
  720. return ipv6_addr_equal(&t->parms.raddr, &hdr->saddr);
  721. }
  722. static int ip6gre_xmit_other(struct sk_buff *skb, struct net_device *dev)
  723. {
  724. struct ip6_tnl *t = netdev_priv(dev);
  725. int encap_limit = -1;
  726. struct flowi6 fl6;
  727. __u32 mtu;
  728. int err;
  729. if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
  730. encap_limit = t->parms.encap_limit;
  731. if (!t->parms.collect_md)
  732. memcpy(&fl6, &t->fl.u.ip6, sizeof(fl6));
  733. err = gre_handle_offloads(skb, !!(t->parms.o_flags & TUNNEL_CSUM));
  734. if (err)
  735. return err;
  736. err = __gre6_xmit(skb, dev, 0, &fl6, encap_limit, &mtu, skb->protocol);
  737. return err;
  738. }
  739. static netdev_tx_t ip6gre_tunnel_xmit(struct sk_buff *skb,
  740. struct net_device *dev)
  741. {
  742. struct ip6_tnl *t = netdev_priv(dev);
  743. struct net_device_stats *stats = &t->dev->stats;
  744. int ret;
  745. if (!pskb_inet_may_pull(skb))
  746. goto tx_err;
  747. if (!ip6_tnl_xmit_ctl(t, &t->parms.laddr, &t->parms.raddr))
  748. goto tx_err;
  749. switch (skb->protocol) {
  750. case htons(ETH_P_IP):
  751. ret = ip6gre_xmit_ipv4(skb, dev);
  752. break;
  753. case htons(ETH_P_IPV6):
  754. ret = ip6gre_xmit_ipv6(skb, dev);
  755. break;
  756. default:
  757. ret = ip6gre_xmit_other(skb, dev);
  758. break;
  759. }
  760. if (ret < 0)
  761. goto tx_err;
  762. return NETDEV_TX_OK;
  763. tx_err:
  764. stats->tx_errors++;
  765. stats->tx_dropped++;
  766. kfree_skb(skb);
  767. return NETDEV_TX_OK;
  768. }
  769. static netdev_tx_t ip6erspan_tunnel_xmit(struct sk_buff *skb,
  770. struct net_device *dev)
  771. {
  772. struct ip6_tnl *t = netdev_priv(dev);
  773. struct dst_entry *dst = skb_dst(skb);
  774. struct net_device_stats *stats;
  775. bool truncate = false;
  776. int encap_limit = -1;
  777. __u8 dsfield = false;
  778. struct flowi6 fl6;
  779. int err = -EINVAL;
  780. __be16 proto;
  781. __u32 mtu;
  782. int nhoff;
  783. int thoff;
  784. if (!pskb_inet_may_pull(skb))
  785. goto tx_err;
  786. if (!ip6_tnl_xmit_ctl(t, &t->parms.laddr, &t->parms.raddr))
  787. goto tx_err;
  788. if (gre_handle_offloads(skb, false))
  789. goto tx_err;
  790. if (skb->len > dev->mtu + dev->hard_header_len) {
  791. pskb_trim(skb, dev->mtu + dev->hard_header_len);
  792. truncate = true;
  793. }
  794. nhoff = skb_network_header(skb) - skb_mac_header(skb);
  795. if (skb->protocol == htons(ETH_P_IP) &&
  796. (ntohs(ip_hdr(skb)->tot_len) > skb->len - nhoff))
  797. truncate = true;
  798. thoff = skb_transport_header(skb) - skb_mac_header(skb);
  799. if (skb->protocol == htons(ETH_P_IPV6) &&
  800. (ntohs(ipv6_hdr(skb)->payload_len) > skb->len - thoff))
  801. truncate = true;
  802. if (skb_cow_head(skb, dev->needed_headroom ?: t->hlen))
  803. goto tx_err;
  804. t->parms.o_flags &= ~TUNNEL_KEY;
  805. IPCB(skb)->flags = 0;
  806. /* For collect_md mode, derive fl6 from the tunnel key,
  807. * for native mode, call prepare_ip6gre_xmit_{ipv4,ipv6}.
  808. */
  809. if (t->parms.collect_md) {
  810. struct ip_tunnel_info *tun_info;
  811. const struct ip_tunnel_key *key;
  812. struct erspan_metadata *md;
  813. __be32 tun_id;
  814. tun_info = skb_tunnel_info(skb);
  815. if (unlikely(!tun_info ||
  816. !(tun_info->mode & IP_TUNNEL_INFO_TX) ||
  817. ip_tunnel_info_af(tun_info) != AF_INET6))
  818. goto tx_err;
  819. key = &tun_info->key;
  820. memset(&fl6, 0, sizeof(fl6));
  821. fl6.flowi6_proto = IPPROTO_GRE;
  822. fl6.daddr = key->u.ipv6.dst;
  823. fl6.flowlabel = key->label;
  824. fl6.flowi6_uid = sock_net_uid(dev_net(dev), NULL);
  825. dsfield = key->tos;
  826. if (!(tun_info->key.tun_flags & TUNNEL_ERSPAN_OPT))
  827. goto tx_err;
  828. if (tun_info->options_len < sizeof(*md))
  829. goto tx_err;
  830. md = ip_tunnel_info_opts(tun_info);
  831. tun_id = tunnel_id_to_key32(key->tun_id);
  832. if (md->version == 1) {
  833. erspan_build_header(skb,
  834. ntohl(tun_id),
  835. ntohl(md->u.index), truncate,
  836. false);
  837. } else if (md->version == 2) {
  838. erspan_build_header_v2(skb,
  839. ntohl(tun_id),
  840. md->u.md2.dir,
  841. get_hwid(&md->u.md2),
  842. truncate, false);
  843. } else {
  844. goto tx_err;
  845. }
  846. } else {
  847. switch (skb->protocol) {
  848. case htons(ETH_P_IP):
  849. memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
  850. prepare_ip6gre_xmit_ipv4(skb, dev, &fl6,
  851. &dsfield, &encap_limit);
  852. break;
  853. case htons(ETH_P_IPV6):
  854. if (ipv6_addr_equal(&t->parms.raddr, &ipv6_hdr(skb)->saddr))
  855. goto tx_err;
  856. if (prepare_ip6gre_xmit_ipv6(skb, dev, &fl6,
  857. &dsfield, &encap_limit))
  858. goto tx_err;
  859. break;
  860. default:
  861. memcpy(&fl6, &t->fl.u.ip6, sizeof(fl6));
  862. break;
  863. }
  864. if (t->parms.erspan_ver == 1)
  865. erspan_build_header(skb, ntohl(t->parms.o_key),
  866. t->parms.index,
  867. truncate, false);
  868. else if (t->parms.erspan_ver == 2)
  869. erspan_build_header_v2(skb, ntohl(t->parms.o_key),
  870. t->parms.dir,
  871. t->parms.hwid,
  872. truncate, false);
  873. else
  874. goto tx_err;
  875. fl6.daddr = t->parms.raddr;
  876. }
  877. /* Push GRE header. */
  878. proto = (t->parms.erspan_ver == 1) ? htons(ETH_P_ERSPAN)
  879. : htons(ETH_P_ERSPAN2);
  880. gre_build_header(skb, 8, TUNNEL_SEQ, proto, 0, htonl(t->o_seqno++));
  881. /* TooBig packet may have updated dst->dev's mtu */
  882. if (!t->parms.collect_md && dst && dst_mtu(dst) > dst->dev->mtu)
  883. dst->ops->update_pmtu(dst, NULL, skb, dst->dev->mtu, false);
  884. err = ip6_tnl_xmit(skb, dev, dsfield, &fl6, encap_limit, &mtu,
  885. NEXTHDR_GRE);
  886. if (err != 0) {
  887. /* XXX: send ICMP error even if DF is not set. */
  888. if (err == -EMSGSIZE) {
  889. if (skb->protocol == htons(ETH_P_IP))
  890. icmp_send(skb, ICMP_DEST_UNREACH,
  891. ICMP_FRAG_NEEDED, htonl(mtu));
  892. else
  893. icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
  894. }
  895. goto tx_err;
  896. }
  897. return NETDEV_TX_OK;
  898. tx_err:
  899. stats = &t->dev->stats;
  900. stats->tx_errors++;
  901. stats->tx_dropped++;
  902. kfree_skb(skb);
  903. return NETDEV_TX_OK;
  904. }
  905. static void ip6gre_tnl_link_config_common(struct ip6_tnl *t)
  906. {
  907. struct net_device *dev = t->dev;
  908. struct __ip6_tnl_parm *p = &t->parms;
  909. struct flowi6 *fl6 = &t->fl.u.ip6;
  910. if (dev->type != ARPHRD_ETHER) {
  911. memcpy(dev->dev_addr, &p->laddr, sizeof(struct in6_addr));
  912. memcpy(dev->broadcast, &p->raddr, sizeof(struct in6_addr));
  913. }
  914. /* Set up flowi template */
  915. fl6->saddr = p->laddr;
  916. fl6->daddr = p->raddr;
  917. fl6->flowi6_oif = p->link;
  918. fl6->flowlabel = 0;
  919. fl6->flowi6_proto = IPPROTO_GRE;
  920. if (!(p->flags&IP6_TNL_F_USE_ORIG_TCLASS))
  921. fl6->flowlabel |= IPV6_TCLASS_MASK & p->flowinfo;
  922. if (!(p->flags&IP6_TNL_F_USE_ORIG_FLOWLABEL))
  923. fl6->flowlabel |= IPV6_FLOWLABEL_MASK & p->flowinfo;
  924. p->flags &= ~(IP6_TNL_F_CAP_XMIT|IP6_TNL_F_CAP_RCV|IP6_TNL_F_CAP_PER_PACKET);
  925. p->flags |= ip6_tnl_get_cap(t, &p->laddr, &p->raddr);
  926. if (p->flags&IP6_TNL_F_CAP_XMIT &&
  927. p->flags&IP6_TNL_F_CAP_RCV && dev->type != ARPHRD_ETHER)
  928. dev->flags |= IFF_POINTOPOINT;
  929. else
  930. dev->flags &= ~IFF_POINTOPOINT;
  931. }
  932. static void ip6gre_tnl_link_config_route(struct ip6_tnl *t, int set_mtu,
  933. int t_hlen)
  934. {
  935. const struct __ip6_tnl_parm *p = &t->parms;
  936. struct net_device *dev = t->dev;
  937. if (p->flags & IP6_TNL_F_CAP_XMIT) {
  938. int strict = (ipv6_addr_type(&p->raddr) &
  939. (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL));
  940. struct rt6_info *rt = rt6_lookup(t->net,
  941. &p->raddr, &p->laddr,
  942. p->link, NULL, strict);
  943. if (!rt)
  944. return;
  945. if (rt->dst.dev) {
  946. dev->needed_headroom = rt->dst.dev->hard_header_len +
  947. t_hlen;
  948. if (set_mtu) {
  949. dev->mtu = rt->dst.dev->mtu - t_hlen;
  950. if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
  951. dev->mtu -= 8;
  952. if (dev->type == ARPHRD_ETHER)
  953. dev->mtu -= ETH_HLEN;
  954. if (dev->mtu < IPV6_MIN_MTU)
  955. dev->mtu = IPV6_MIN_MTU;
  956. }
  957. }
  958. ip6_rt_put(rt);
  959. }
  960. }
  961. static int ip6gre_calc_hlen(struct ip6_tnl *tunnel)
  962. {
  963. int t_hlen;
  964. tunnel->tun_hlen = gre_calc_hlen(tunnel->parms.o_flags);
  965. tunnel->hlen = tunnel->tun_hlen + tunnel->encap_hlen;
  966. t_hlen = tunnel->hlen + sizeof(struct ipv6hdr);
  967. tunnel->dev->needed_headroom = LL_MAX_HEADER + t_hlen;
  968. return t_hlen;
  969. }
  970. static void ip6gre_tnl_link_config(struct ip6_tnl *t, int set_mtu)
  971. {
  972. ip6gre_tnl_link_config_common(t);
  973. ip6gre_tnl_link_config_route(t, set_mtu, ip6gre_calc_hlen(t));
  974. }
  975. static void ip6gre_tnl_copy_tnl_parm(struct ip6_tnl *t,
  976. const struct __ip6_tnl_parm *p)
  977. {
  978. t->parms.laddr = p->laddr;
  979. t->parms.raddr = p->raddr;
  980. t->parms.flags = p->flags;
  981. t->parms.hop_limit = p->hop_limit;
  982. t->parms.encap_limit = p->encap_limit;
  983. t->parms.flowinfo = p->flowinfo;
  984. t->parms.link = p->link;
  985. t->parms.proto = p->proto;
  986. t->parms.i_key = p->i_key;
  987. t->parms.o_key = p->o_key;
  988. t->parms.i_flags = p->i_flags;
  989. t->parms.o_flags = p->o_flags;
  990. t->parms.fwmark = p->fwmark;
  991. t->parms.erspan_ver = p->erspan_ver;
  992. t->parms.index = p->index;
  993. t->parms.dir = p->dir;
  994. t->parms.hwid = p->hwid;
  995. dst_cache_reset(&t->dst_cache);
  996. }
  997. static int ip6gre_tnl_change(struct ip6_tnl *t, const struct __ip6_tnl_parm *p,
  998. int set_mtu)
  999. {
  1000. ip6gre_tnl_copy_tnl_parm(t, p);
  1001. ip6gre_tnl_link_config(t, set_mtu);
  1002. return 0;
  1003. }
  1004. static void ip6gre_tnl_parm_from_user(struct __ip6_tnl_parm *p,
  1005. const struct ip6_tnl_parm2 *u)
  1006. {
  1007. p->laddr = u->laddr;
  1008. p->raddr = u->raddr;
  1009. p->flags = u->flags;
  1010. p->hop_limit = u->hop_limit;
  1011. p->encap_limit = u->encap_limit;
  1012. p->flowinfo = u->flowinfo;
  1013. p->link = u->link;
  1014. p->i_key = u->i_key;
  1015. p->o_key = u->o_key;
  1016. p->i_flags = gre_flags_to_tnl_flags(u->i_flags);
  1017. p->o_flags = gre_flags_to_tnl_flags(u->o_flags);
  1018. memcpy(p->name, u->name, sizeof(u->name));
  1019. }
  1020. static void ip6gre_tnl_parm_to_user(struct ip6_tnl_parm2 *u,
  1021. const struct __ip6_tnl_parm *p)
  1022. {
  1023. u->proto = IPPROTO_GRE;
  1024. u->laddr = p->laddr;
  1025. u->raddr = p->raddr;
  1026. u->flags = p->flags;
  1027. u->hop_limit = p->hop_limit;
  1028. u->encap_limit = p->encap_limit;
  1029. u->flowinfo = p->flowinfo;
  1030. u->link = p->link;
  1031. u->i_key = p->i_key;
  1032. u->o_key = p->o_key;
  1033. u->i_flags = gre_tnl_flags_to_gre_flags(p->i_flags);
  1034. u->o_flags = gre_tnl_flags_to_gre_flags(p->o_flags);
  1035. memcpy(u->name, p->name, sizeof(u->name));
  1036. }
  1037. static int ip6gre_tunnel_ioctl(struct net_device *dev,
  1038. struct ifreq *ifr, int cmd)
  1039. {
  1040. int err = 0;
  1041. struct ip6_tnl_parm2 p;
  1042. struct __ip6_tnl_parm p1;
  1043. struct ip6_tnl *t = netdev_priv(dev);
  1044. struct net *net = t->net;
  1045. struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
  1046. memset(&p1, 0, sizeof(p1));
  1047. switch (cmd) {
  1048. case SIOCGETTUNNEL:
  1049. if (dev == ign->fb_tunnel_dev) {
  1050. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) {
  1051. err = -EFAULT;
  1052. break;
  1053. }
  1054. ip6gre_tnl_parm_from_user(&p1, &p);
  1055. t = ip6gre_tunnel_locate(net, &p1, 0);
  1056. if (!t)
  1057. t = netdev_priv(dev);
  1058. }
  1059. memset(&p, 0, sizeof(p));
  1060. ip6gre_tnl_parm_to_user(&p, &t->parms);
  1061. if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p)))
  1062. err = -EFAULT;
  1063. break;
  1064. case SIOCADDTUNNEL:
  1065. case SIOCCHGTUNNEL:
  1066. err = -EPERM;
  1067. if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  1068. goto done;
  1069. err = -EFAULT;
  1070. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
  1071. goto done;
  1072. err = -EINVAL;
  1073. if ((p.i_flags|p.o_flags)&(GRE_VERSION|GRE_ROUTING))
  1074. goto done;
  1075. if (!(p.i_flags&GRE_KEY))
  1076. p.i_key = 0;
  1077. if (!(p.o_flags&GRE_KEY))
  1078. p.o_key = 0;
  1079. ip6gre_tnl_parm_from_user(&p1, &p);
  1080. t = ip6gre_tunnel_locate(net, &p1, cmd == SIOCADDTUNNEL);
  1081. if (dev != ign->fb_tunnel_dev && cmd == SIOCCHGTUNNEL) {
  1082. if (t) {
  1083. if (t->dev != dev) {
  1084. err = -EEXIST;
  1085. break;
  1086. }
  1087. } else {
  1088. t = netdev_priv(dev);
  1089. ip6gre_tunnel_unlink(ign, t);
  1090. synchronize_net();
  1091. ip6gre_tnl_change(t, &p1, 1);
  1092. ip6gre_tunnel_link(ign, t);
  1093. netdev_state_change(dev);
  1094. }
  1095. }
  1096. if (t) {
  1097. err = 0;
  1098. memset(&p, 0, sizeof(p));
  1099. ip6gre_tnl_parm_to_user(&p, &t->parms);
  1100. if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p)))
  1101. err = -EFAULT;
  1102. } else
  1103. err = (cmd == SIOCADDTUNNEL ? -ENOBUFS : -ENOENT);
  1104. break;
  1105. case SIOCDELTUNNEL:
  1106. err = -EPERM;
  1107. if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  1108. goto done;
  1109. if (dev == ign->fb_tunnel_dev) {
  1110. err = -EFAULT;
  1111. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
  1112. goto done;
  1113. err = -ENOENT;
  1114. ip6gre_tnl_parm_from_user(&p1, &p);
  1115. t = ip6gre_tunnel_locate(net, &p1, 0);
  1116. if (!t)
  1117. goto done;
  1118. err = -EPERM;
  1119. if (t == netdev_priv(ign->fb_tunnel_dev))
  1120. goto done;
  1121. dev = t->dev;
  1122. }
  1123. unregister_netdevice(dev);
  1124. err = 0;
  1125. break;
  1126. default:
  1127. err = -EINVAL;
  1128. }
  1129. done:
  1130. return err;
  1131. }
  1132. static int ip6gre_header(struct sk_buff *skb, struct net_device *dev,
  1133. unsigned short type, const void *daddr,
  1134. const void *saddr, unsigned int len)
  1135. {
  1136. struct ip6_tnl *t = netdev_priv(dev);
  1137. struct ipv6hdr *ipv6h;
  1138. __be16 *p;
  1139. ipv6h = skb_push(skb, t->hlen + sizeof(*ipv6h));
  1140. ip6_flow_hdr(ipv6h, 0, ip6_make_flowlabel(dev_net(dev), skb,
  1141. t->fl.u.ip6.flowlabel,
  1142. true, &t->fl.u.ip6));
  1143. ipv6h->hop_limit = t->parms.hop_limit;
  1144. ipv6h->nexthdr = NEXTHDR_GRE;
  1145. ipv6h->saddr = t->parms.laddr;
  1146. ipv6h->daddr = t->parms.raddr;
  1147. p = (__be16 *)(ipv6h + 1);
  1148. p[0] = t->parms.o_flags;
  1149. p[1] = htons(type);
  1150. /*
  1151. * Set the source hardware address.
  1152. */
  1153. if (saddr)
  1154. memcpy(&ipv6h->saddr, saddr, sizeof(struct in6_addr));
  1155. if (daddr)
  1156. memcpy(&ipv6h->daddr, daddr, sizeof(struct in6_addr));
  1157. if (!ipv6_addr_any(&ipv6h->daddr))
  1158. return t->hlen;
  1159. return -t->hlen;
  1160. }
  1161. static const struct header_ops ip6gre_header_ops = {
  1162. .create = ip6gre_header,
  1163. };
  1164. static const struct net_device_ops ip6gre_netdev_ops = {
  1165. .ndo_init = ip6gre_tunnel_init,
  1166. .ndo_uninit = ip6gre_tunnel_uninit,
  1167. .ndo_start_xmit = ip6gre_tunnel_xmit,
  1168. .ndo_do_ioctl = ip6gre_tunnel_ioctl,
  1169. .ndo_change_mtu = ip6_tnl_change_mtu,
  1170. .ndo_get_stats64 = ip_tunnel_get_stats64,
  1171. .ndo_get_iflink = ip6_tnl_get_iflink,
  1172. };
  1173. static void ip6gre_dev_free(struct net_device *dev)
  1174. {
  1175. struct ip6_tnl *t = netdev_priv(dev);
  1176. gro_cells_destroy(&t->gro_cells);
  1177. dst_cache_destroy(&t->dst_cache);
  1178. free_percpu(dev->tstats);
  1179. }
  1180. static void ip6gre_tunnel_setup(struct net_device *dev)
  1181. {
  1182. dev->netdev_ops = &ip6gre_netdev_ops;
  1183. dev->needs_free_netdev = true;
  1184. dev->priv_destructor = ip6gre_dev_free;
  1185. dev->type = ARPHRD_IP6GRE;
  1186. dev->flags |= IFF_NOARP;
  1187. dev->addr_len = sizeof(struct in6_addr);
  1188. netif_keep_dst(dev);
  1189. /* This perm addr will be used as interface identifier by IPv6 */
  1190. dev->addr_assign_type = NET_ADDR_RANDOM;
  1191. eth_random_addr(dev->perm_addr);
  1192. }
  1193. #define GRE6_FEATURES (NETIF_F_SG | \
  1194. NETIF_F_FRAGLIST | \
  1195. NETIF_F_HIGHDMA | \
  1196. NETIF_F_HW_CSUM)
  1197. static void ip6gre_tnl_init_features(struct net_device *dev)
  1198. {
  1199. struct ip6_tnl *nt = netdev_priv(dev);
  1200. dev->features |= GRE6_FEATURES;
  1201. dev->hw_features |= GRE6_FEATURES;
  1202. if (!(nt->parms.o_flags & TUNNEL_SEQ)) {
  1203. /* TCP offload with GRE SEQ is not supported, nor
  1204. * can we support 2 levels of outer headers requiring
  1205. * an update.
  1206. */
  1207. if (!(nt->parms.o_flags & TUNNEL_CSUM) ||
  1208. nt->encap.type == TUNNEL_ENCAP_NONE) {
  1209. dev->features |= NETIF_F_GSO_SOFTWARE;
  1210. dev->hw_features |= NETIF_F_GSO_SOFTWARE;
  1211. }
  1212. /* Can use a lockless transmit, unless we generate
  1213. * output sequences
  1214. */
  1215. dev->features |= NETIF_F_LLTX;
  1216. }
  1217. }
  1218. static int ip6gre_tunnel_init_common(struct net_device *dev)
  1219. {
  1220. struct ip6_tnl *tunnel;
  1221. int ret;
  1222. int t_hlen;
  1223. tunnel = netdev_priv(dev);
  1224. tunnel->dev = dev;
  1225. tunnel->net = dev_net(dev);
  1226. strcpy(tunnel->parms.name, dev->name);
  1227. dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
  1228. if (!dev->tstats)
  1229. return -ENOMEM;
  1230. ret = dst_cache_init(&tunnel->dst_cache, GFP_KERNEL);
  1231. if (ret)
  1232. goto cleanup_alloc_pcpu_stats;
  1233. ret = gro_cells_init(&tunnel->gro_cells, dev);
  1234. if (ret)
  1235. goto cleanup_dst_cache_init;
  1236. t_hlen = ip6gre_calc_hlen(tunnel);
  1237. dev->mtu = ETH_DATA_LEN - t_hlen;
  1238. if (dev->type == ARPHRD_ETHER)
  1239. dev->mtu -= ETH_HLEN;
  1240. if (!(tunnel->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
  1241. dev->mtu -= 8;
  1242. if (tunnel->parms.collect_md) {
  1243. netif_keep_dst(dev);
  1244. }
  1245. ip6gre_tnl_init_features(dev);
  1246. return 0;
  1247. cleanup_dst_cache_init:
  1248. dst_cache_destroy(&tunnel->dst_cache);
  1249. cleanup_alloc_pcpu_stats:
  1250. free_percpu(dev->tstats);
  1251. dev->tstats = NULL;
  1252. return ret;
  1253. }
  1254. static int ip6gre_tunnel_init(struct net_device *dev)
  1255. {
  1256. struct ip6_tnl *tunnel;
  1257. int ret;
  1258. ret = ip6gre_tunnel_init_common(dev);
  1259. if (ret)
  1260. return ret;
  1261. tunnel = netdev_priv(dev);
  1262. if (tunnel->parms.collect_md)
  1263. return 0;
  1264. memcpy(dev->dev_addr, &tunnel->parms.laddr, sizeof(struct in6_addr));
  1265. memcpy(dev->broadcast, &tunnel->parms.raddr, sizeof(struct in6_addr));
  1266. if (ipv6_addr_any(&tunnel->parms.raddr))
  1267. dev->header_ops = &ip6gre_header_ops;
  1268. return 0;
  1269. }
  1270. static void ip6gre_fb_tunnel_init(struct net_device *dev)
  1271. {
  1272. struct ip6_tnl *tunnel = netdev_priv(dev);
  1273. tunnel->dev = dev;
  1274. tunnel->net = dev_net(dev);
  1275. strcpy(tunnel->parms.name, dev->name);
  1276. tunnel->hlen = sizeof(struct ipv6hdr) + 4;
  1277. dev_hold(dev);
  1278. }
  1279. static struct inet6_protocol ip6gre_protocol __read_mostly = {
  1280. .handler = gre_rcv,
  1281. .err_handler = ip6gre_err,
  1282. .flags = INET6_PROTO_NOPOLICY|INET6_PROTO_FINAL,
  1283. };
  1284. static void ip6gre_destroy_tunnels(struct net *net, struct list_head *head)
  1285. {
  1286. struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
  1287. struct net_device *dev, *aux;
  1288. int prio;
  1289. for_each_netdev_safe(net, dev, aux)
  1290. if (dev->rtnl_link_ops == &ip6gre_link_ops ||
  1291. dev->rtnl_link_ops == &ip6gre_tap_ops ||
  1292. dev->rtnl_link_ops == &ip6erspan_tap_ops)
  1293. unregister_netdevice_queue(dev, head);
  1294. for (prio = 0; prio < 4; prio++) {
  1295. int h;
  1296. for (h = 0; h < IP6_GRE_HASH_SIZE; h++) {
  1297. struct ip6_tnl *t;
  1298. t = rtnl_dereference(ign->tunnels[prio][h]);
  1299. while (t) {
  1300. /* If dev is in the same netns, it has already
  1301. * been added to the list by the previous loop.
  1302. */
  1303. if (!net_eq(dev_net(t->dev), net))
  1304. unregister_netdevice_queue(t->dev,
  1305. head);
  1306. t = rtnl_dereference(t->next);
  1307. }
  1308. }
  1309. }
  1310. }
  1311. static int __net_init ip6gre_init_net(struct net *net)
  1312. {
  1313. struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
  1314. int err;
  1315. if (!net_has_fallback_tunnels(net))
  1316. return 0;
  1317. ign->fb_tunnel_dev = alloc_netdev(sizeof(struct ip6_tnl), "ip6gre0",
  1318. NET_NAME_UNKNOWN,
  1319. ip6gre_tunnel_setup);
  1320. if (!ign->fb_tunnel_dev) {
  1321. err = -ENOMEM;
  1322. goto err_alloc_dev;
  1323. }
  1324. dev_net_set(ign->fb_tunnel_dev, net);
  1325. /* FB netdevice is special: we have one, and only one per netns.
  1326. * Allowing to move it to another netns is clearly unsafe.
  1327. */
  1328. ign->fb_tunnel_dev->features |= NETIF_F_NETNS_LOCAL;
  1329. ip6gre_fb_tunnel_init(ign->fb_tunnel_dev);
  1330. ign->fb_tunnel_dev->rtnl_link_ops = &ip6gre_link_ops;
  1331. err = register_netdev(ign->fb_tunnel_dev);
  1332. if (err)
  1333. goto err_reg_dev;
  1334. rcu_assign_pointer(ign->tunnels_wc[0],
  1335. netdev_priv(ign->fb_tunnel_dev));
  1336. return 0;
  1337. err_reg_dev:
  1338. free_netdev(ign->fb_tunnel_dev);
  1339. err_alloc_dev:
  1340. return err;
  1341. }
  1342. static void __net_exit ip6gre_exit_batch_net(struct list_head *net_list)
  1343. {
  1344. struct net *net;
  1345. LIST_HEAD(list);
  1346. rtnl_lock();
  1347. list_for_each_entry(net, net_list, exit_list)
  1348. ip6gre_destroy_tunnels(net, &list);
  1349. unregister_netdevice_many(&list);
  1350. rtnl_unlock();
  1351. }
  1352. static struct pernet_operations ip6gre_net_ops = {
  1353. .init = ip6gre_init_net,
  1354. .exit_batch = ip6gre_exit_batch_net,
  1355. .id = &ip6gre_net_id,
  1356. .size = sizeof(struct ip6gre_net),
  1357. };
  1358. static int ip6gre_tunnel_validate(struct nlattr *tb[], struct nlattr *data[],
  1359. struct netlink_ext_ack *extack)
  1360. {
  1361. __be16 flags;
  1362. if (!data)
  1363. return 0;
  1364. flags = 0;
  1365. if (data[IFLA_GRE_IFLAGS])
  1366. flags |= nla_get_be16(data[IFLA_GRE_IFLAGS]);
  1367. if (data[IFLA_GRE_OFLAGS])
  1368. flags |= nla_get_be16(data[IFLA_GRE_OFLAGS]);
  1369. if (flags & (GRE_VERSION|GRE_ROUTING))
  1370. return -EINVAL;
  1371. return 0;
  1372. }
  1373. static int ip6gre_tap_validate(struct nlattr *tb[], struct nlattr *data[],
  1374. struct netlink_ext_ack *extack)
  1375. {
  1376. struct in6_addr daddr;
  1377. if (tb[IFLA_ADDRESS]) {
  1378. if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
  1379. return -EINVAL;
  1380. if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
  1381. return -EADDRNOTAVAIL;
  1382. }
  1383. if (!data)
  1384. goto out;
  1385. if (data[IFLA_GRE_REMOTE]) {
  1386. daddr = nla_get_in6_addr(data[IFLA_GRE_REMOTE]);
  1387. if (ipv6_addr_any(&daddr))
  1388. return -EINVAL;
  1389. }
  1390. out:
  1391. return ip6gre_tunnel_validate(tb, data, extack);
  1392. }
  1393. static int ip6erspan_tap_validate(struct nlattr *tb[], struct nlattr *data[],
  1394. struct netlink_ext_ack *extack)
  1395. {
  1396. __be16 flags = 0;
  1397. int ret, ver = 0;
  1398. if (!data)
  1399. return 0;
  1400. ret = ip6gre_tap_validate(tb, data, extack);
  1401. if (ret)
  1402. return ret;
  1403. /* ERSPAN should only have GRE sequence and key flag */
  1404. if (data[IFLA_GRE_OFLAGS])
  1405. flags |= nla_get_be16(data[IFLA_GRE_OFLAGS]);
  1406. if (data[IFLA_GRE_IFLAGS])
  1407. flags |= nla_get_be16(data[IFLA_GRE_IFLAGS]);
  1408. if (!data[IFLA_GRE_COLLECT_METADATA] &&
  1409. flags != (GRE_SEQ | GRE_KEY))
  1410. return -EINVAL;
  1411. /* ERSPAN Session ID only has 10-bit. Since we reuse
  1412. * 32-bit key field as ID, check it's range.
  1413. */
  1414. if (data[IFLA_GRE_IKEY] &&
  1415. (ntohl(nla_get_be32(data[IFLA_GRE_IKEY])) & ~ID_MASK))
  1416. return -EINVAL;
  1417. if (data[IFLA_GRE_OKEY] &&
  1418. (ntohl(nla_get_be32(data[IFLA_GRE_OKEY])) & ~ID_MASK))
  1419. return -EINVAL;
  1420. if (data[IFLA_GRE_ERSPAN_VER]) {
  1421. ver = nla_get_u8(data[IFLA_GRE_ERSPAN_VER]);
  1422. if (ver != 1 && ver != 2)
  1423. return -EINVAL;
  1424. }
  1425. if (ver == 1) {
  1426. if (data[IFLA_GRE_ERSPAN_INDEX]) {
  1427. u32 index = nla_get_u32(data[IFLA_GRE_ERSPAN_INDEX]);
  1428. if (index & ~INDEX_MASK)
  1429. return -EINVAL;
  1430. }
  1431. } else if (ver == 2) {
  1432. if (data[IFLA_GRE_ERSPAN_DIR]) {
  1433. u16 dir = nla_get_u8(data[IFLA_GRE_ERSPAN_DIR]);
  1434. if (dir & ~(DIR_MASK >> DIR_OFFSET))
  1435. return -EINVAL;
  1436. }
  1437. if (data[IFLA_GRE_ERSPAN_HWID]) {
  1438. u16 hwid = nla_get_u16(data[IFLA_GRE_ERSPAN_HWID]);
  1439. if (hwid & ~(HWID_MASK >> HWID_OFFSET))
  1440. return -EINVAL;
  1441. }
  1442. }
  1443. return 0;
  1444. }
  1445. static void ip6erspan_set_version(struct nlattr *data[],
  1446. struct __ip6_tnl_parm *parms)
  1447. {
  1448. if (!data)
  1449. return;
  1450. parms->erspan_ver = 1;
  1451. if (data[IFLA_GRE_ERSPAN_VER])
  1452. parms->erspan_ver = nla_get_u8(data[IFLA_GRE_ERSPAN_VER]);
  1453. if (parms->erspan_ver == 1) {
  1454. if (data[IFLA_GRE_ERSPAN_INDEX])
  1455. parms->index = nla_get_u32(data[IFLA_GRE_ERSPAN_INDEX]);
  1456. } else if (parms->erspan_ver == 2) {
  1457. if (data[IFLA_GRE_ERSPAN_DIR])
  1458. parms->dir = nla_get_u8(data[IFLA_GRE_ERSPAN_DIR]);
  1459. if (data[IFLA_GRE_ERSPAN_HWID])
  1460. parms->hwid = nla_get_u16(data[IFLA_GRE_ERSPAN_HWID]);
  1461. }
  1462. }
  1463. static void ip6gre_netlink_parms(struct nlattr *data[],
  1464. struct __ip6_tnl_parm *parms)
  1465. {
  1466. memset(parms, 0, sizeof(*parms));
  1467. if (!data)
  1468. return;
  1469. if (data[IFLA_GRE_LINK])
  1470. parms->link = nla_get_u32(data[IFLA_GRE_LINK]);
  1471. if (data[IFLA_GRE_IFLAGS])
  1472. parms->i_flags = gre_flags_to_tnl_flags(
  1473. nla_get_be16(data[IFLA_GRE_IFLAGS]));
  1474. if (data[IFLA_GRE_OFLAGS])
  1475. parms->o_flags = gre_flags_to_tnl_flags(
  1476. nla_get_be16(data[IFLA_GRE_OFLAGS]));
  1477. if (data[IFLA_GRE_IKEY])
  1478. parms->i_key = nla_get_be32(data[IFLA_GRE_IKEY]);
  1479. if (data[IFLA_GRE_OKEY])
  1480. parms->o_key = nla_get_be32(data[IFLA_GRE_OKEY]);
  1481. if (data[IFLA_GRE_LOCAL])
  1482. parms->laddr = nla_get_in6_addr(data[IFLA_GRE_LOCAL]);
  1483. if (data[IFLA_GRE_REMOTE])
  1484. parms->raddr = nla_get_in6_addr(data[IFLA_GRE_REMOTE]);
  1485. if (data[IFLA_GRE_TTL])
  1486. parms->hop_limit = nla_get_u8(data[IFLA_GRE_TTL]);
  1487. if (data[IFLA_GRE_ENCAP_LIMIT])
  1488. parms->encap_limit = nla_get_u8(data[IFLA_GRE_ENCAP_LIMIT]);
  1489. if (data[IFLA_GRE_FLOWINFO])
  1490. parms->flowinfo = nla_get_be32(data[IFLA_GRE_FLOWINFO]);
  1491. if (data[IFLA_GRE_FLAGS])
  1492. parms->flags = nla_get_u32(data[IFLA_GRE_FLAGS]);
  1493. if (data[IFLA_GRE_FWMARK])
  1494. parms->fwmark = nla_get_u32(data[IFLA_GRE_FWMARK]);
  1495. if (data[IFLA_GRE_COLLECT_METADATA])
  1496. parms->collect_md = true;
  1497. }
  1498. static int ip6gre_tap_init(struct net_device *dev)
  1499. {
  1500. int ret;
  1501. ret = ip6gre_tunnel_init_common(dev);
  1502. if (ret)
  1503. return ret;
  1504. dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
  1505. return 0;
  1506. }
  1507. static const struct net_device_ops ip6gre_tap_netdev_ops = {
  1508. .ndo_init = ip6gre_tap_init,
  1509. .ndo_uninit = ip6gre_tunnel_uninit,
  1510. .ndo_start_xmit = ip6gre_tunnel_xmit,
  1511. .ndo_set_mac_address = eth_mac_addr,
  1512. .ndo_validate_addr = eth_validate_addr,
  1513. .ndo_change_mtu = ip6_tnl_change_mtu,
  1514. .ndo_get_stats64 = ip_tunnel_get_stats64,
  1515. .ndo_get_iflink = ip6_tnl_get_iflink,
  1516. };
  1517. static int ip6erspan_calc_hlen(struct ip6_tnl *tunnel)
  1518. {
  1519. int t_hlen;
  1520. tunnel->tun_hlen = 8;
  1521. tunnel->hlen = tunnel->tun_hlen + tunnel->encap_hlen +
  1522. erspan_hdr_len(tunnel->parms.erspan_ver);
  1523. t_hlen = tunnel->hlen + sizeof(struct ipv6hdr);
  1524. tunnel->dev->needed_headroom = LL_MAX_HEADER + t_hlen;
  1525. return t_hlen;
  1526. }
  1527. static int ip6erspan_tap_init(struct net_device *dev)
  1528. {
  1529. struct ip6_tnl *tunnel;
  1530. int t_hlen;
  1531. int ret;
  1532. tunnel = netdev_priv(dev);
  1533. tunnel->dev = dev;
  1534. tunnel->net = dev_net(dev);
  1535. strcpy(tunnel->parms.name, dev->name);
  1536. dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
  1537. if (!dev->tstats)
  1538. return -ENOMEM;
  1539. ret = dst_cache_init(&tunnel->dst_cache, GFP_KERNEL);
  1540. if (ret)
  1541. goto cleanup_alloc_pcpu_stats;
  1542. ret = gro_cells_init(&tunnel->gro_cells, dev);
  1543. if (ret)
  1544. goto cleanup_dst_cache_init;
  1545. t_hlen = ip6erspan_calc_hlen(tunnel);
  1546. dev->mtu = ETH_DATA_LEN - t_hlen;
  1547. if (dev->type == ARPHRD_ETHER)
  1548. dev->mtu -= ETH_HLEN;
  1549. if (!(tunnel->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
  1550. dev->mtu -= 8;
  1551. dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
  1552. ip6erspan_tnl_link_config(tunnel, 1);
  1553. return 0;
  1554. cleanup_dst_cache_init:
  1555. dst_cache_destroy(&tunnel->dst_cache);
  1556. cleanup_alloc_pcpu_stats:
  1557. free_percpu(dev->tstats);
  1558. dev->tstats = NULL;
  1559. return ret;
  1560. }
  1561. static const struct net_device_ops ip6erspan_netdev_ops = {
  1562. .ndo_init = ip6erspan_tap_init,
  1563. .ndo_uninit = ip6erspan_tunnel_uninit,
  1564. .ndo_start_xmit = ip6erspan_tunnel_xmit,
  1565. .ndo_set_mac_address = eth_mac_addr,
  1566. .ndo_validate_addr = eth_validate_addr,
  1567. .ndo_change_mtu = ip6_tnl_change_mtu,
  1568. .ndo_get_stats64 = ip_tunnel_get_stats64,
  1569. .ndo_get_iflink = ip6_tnl_get_iflink,
  1570. };
  1571. static void ip6gre_tap_setup(struct net_device *dev)
  1572. {
  1573. ether_setup(dev);
  1574. dev->max_mtu = 0;
  1575. dev->netdev_ops = &ip6gre_tap_netdev_ops;
  1576. dev->needs_free_netdev = true;
  1577. dev->priv_destructor = ip6gre_dev_free;
  1578. dev->priv_flags &= ~IFF_TX_SKB_SHARING;
  1579. dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
  1580. netif_keep_dst(dev);
  1581. }
  1582. bool is_ip6gretap_dev(const struct net_device *dev)
  1583. {
  1584. return dev->netdev_ops == &ip6gre_tap_netdev_ops;
  1585. }
  1586. EXPORT_SYMBOL_GPL(is_ip6gretap_dev);
  1587. static bool ip6gre_netlink_encap_parms(struct nlattr *data[],
  1588. struct ip_tunnel_encap *ipencap)
  1589. {
  1590. bool ret = false;
  1591. memset(ipencap, 0, sizeof(*ipencap));
  1592. if (!data)
  1593. return ret;
  1594. if (data[IFLA_GRE_ENCAP_TYPE]) {
  1595. ret = true;
  1596. ipencap->type = nla_get_u16(data[IFLA_GRE_ENCAP_TYPE]);
  1597. }
  1598. if (data[IFLA_GRE_ENCAP_FLAGS]) {
  1599. ret = true;
  1600. ipencap->flags = nla_get_u16(data[IFLA_GRE_ENCAP_FLAGS]);
  1601. }
  1602. if (data[IFLA_GRE_ENCAP_SPORT]) {
  1603. ret = true;
  1604. ipencap->sport = nla_get_be16(data[IFLA_GRE_ENCAP_SPORT]);
  1605. }
  1606. if (data[IFLA_GRE_ENCAP_DPORT]) {
  1607. ret = true;
  1608. ipencap->dport = nla_get_be16(data[IFLA_GRE_ENCAP_DPORT]);
  1609. }
  1610. return ret;
  1611. }
  1612. static int ip6gre_newlink_common(struct net *src_net, struct net_device *dev,
  1613. struct nlattr *tb[], struct nlattr *data[],
  1614. struct netlink_ext_ack *extack)
  1615. {
  1616. struct ip6_tnl *nt;
  1617. struct ip_tunnel_encap ipencap;
  1618. int err;
  1619. nt = netdev_priv(dev);
  1620. if (ip6gre_netlink_encap_parms(data, &ipencap)) {
  1621. int err = ip6_tnl_encap_setup(nt, &ipencap);
  1622. if (err < 0)
  1623. return err;
  1624. }
  1625. if (dev->type == ARPHRD_ETHER && !tb[IFLA_ADDRESS])
  1626. eth_hw_addr_random(dev);
  1627. nt->dev = dev;
  1628. nt->net = dev_net(dev);
  1629. err = register_netdevice(dev);
  1630. if (err)
  1631. goto out;
  1632. if (tb[IFLA_MTU])
  1633. ip6_tnl_change_mtu(dev, nla_get_u32(tb[IFLA_MTU]));
  1634. dev_hold(dev);
  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 = FIELD_SIZEOF(struct ipv6hdr, saddr) },
  1813. [IFLA_GRE_REMOTE] = { .len = FIELD_SIZEOF(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");