ip6_gre.c 53 KB

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