ip6_gre.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717
  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/mroute.h>
  26. #include <linux/init.h>
  27. #include <linux/in6.h>
  28. #include <linux/inetdevice.h>
  29. #include <linux/igmp.h>
  30. #include <linux/netfilter_ipv4.h>
  31. #include <linux/etherdevice.h>
  32. #include <linux/if_ether.h>
  33. #include <linux/hash.h>
  34. #include <linux/if_tunnel.h>
  35. #include <linux/ip6_tunnel.h>
  36. #include <net/sock.h>
  37. #include <net/ip.h>
  38. #include <net/ip_tunnels.h>
  39. #include <net/icmp.h>
  40. #include <net/protocol.h>
  41. #include <net/addrconf.h>
  42. #include <net/arp.h>
  43. #include <net/checksum.h>
  44. #include <net/dsfield.h>
  45. #include <net/inet_ecn.h>
  46. #include <net/xfrm.h>
  47. #include <net/net_namespace.h>
  48. #include <net/netns/generic.h>
  49. #include <net/rtnetlink.h>
  50. #include <net/ipv6.h>
  51. #include <net/ip6_fib.h>
  52. #include <net/ip6_route.h>
  53. #include <net/ip6_tunnel.h>
  54. static bool log_ecn_error = true;
  55. module_param(log_ecn_error, bool, 0644);
  56. MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN");
  57. #define HASH_SIZE_SHIFT 5
  58. #define HASH_SIZE (1 << HASH_SIZE_SHIFT)
  59. static int ip6gre_net_id __read_mostly;
  60. struct ip6gre_net {
  61. struct ip6_tnl __rcu *tunnels[4][HASH_SIZE];
  62. struct net_device *fb_tunnel_dev;
  63. };
  64. static struct rtnl_link_ops ip6gre_link_ops __read_mostly;
  65. static int ip6gre_tunnel_init(struct net_device *dev);
  66. static void ip6gre_tunnel_setup(struct net_device *dev);
  67. static void ip6gre_tunnel_link(struct ip6gre_net *ign, struct ip6_tnl *t);
  68. static void ip6gre_tnl_link_config(struct ip6_tnl *t, int set_mtu);
  69. /* Tunnel hash table */
  70. /*
  71. 4 hash tables:
  72. 3: (remote,local)
  73. 2: (remote,*)
  74. 1: (*,local)
  75. 0: (*,*)
  76. We require exact key match i.e. if a key is present in packet
  77. it will match only tunnel with the same key; if it is not present,
  78. it will match only keyless tunnel.
  79. All keysless packets, if not matched configured keyless tunnels
  80. will match fallback tunnel.
  81. */
  82. #define HASH_KEY(key) (((__force u32)key^((__force u32)key>>4))&(HASH_SIZE - 1))
  83. static u32 HASH_ADDR(const struct in6_addr *addr)
  84. {
  85. u32 hash = ipv6_addr_hash(addr);
  86. return hash_32(hash, HASH_SIZE_SHIFT);
  87. }
  88. #define tunnels_r_l tunnels[3]
  89. #define tunnels_r tunnels[2]
  90. #define tunnels_l tunnels[1]
  91. #define tunnels_wc tunnels[0]
  92. /* Given src, dst and key, find appropriate for input tunnel. */
  93. static struct ip6_tnl *ip6gre_tunnel_lookup(struct net_device *dev,
  94. const struct in6_addr *remote, const struct in6_addr *local,
  95. __be32 key, __be16 gre_proto)
  96. {
  97. struct net *net = dev_net(dev);
  98. int link = dev->ifindex;
  99. unsigned int h0 = HASH_ADDR(remote);
  100. unsigned int h1 = HASH_KEY(key);
  101. struct ip6_tnl *t, *cand = NULL;
  102. struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
  103. int dev_type = (gre_proto == htons(ETH_P_TEB)) ?
  104. ARPHRD_ETHER : ARPHRD_IP6GRE;
  105. int score, cand_score = 4;
  106. for_each_ip_tunnel_rcu(t, ign->tunnels_r_l[h0 ^ h1]) {
  107. if (!ipv6_addr_equal(local, &t->parms.laddr) ||
  108. !ipv6_addr_equal(remote, &t->parms.raddr) ||
  109. key != t->parms.i_key ||
  110. !(t->dev->flags & IFF_UP))
  111. continue;
  112. if (t->dev->type != ARPHRD_IP6GRE &&
  113. t->dev->type != dev_type)
  114. continue;
  115. score = 0;
  116. if (t->parms.link != link)
  117. score |= 1;
  118. if (t->dev->type != dev_type)
  119. score |= 2;
  120. if (score == 0)
  121. return t;
  122. if (score < cand_score) {
  123. cand = t;
  124. cand_score = score;
  125. }
  126. }
  127. for_each_ip_tunnel_rcu(t, ign->tunnels_r[h0 ^ h1]) {
  128. if (!ipv6_addr_equal(remote, &t->parms.raddr) ||
  129. key != t->parms.i_key ||
  130. !(t->dev->flags & IFF_UP))
  131. continue;
  132. if (t->dev->type != ARPHRD_IP6GRE &&
  133. t->dev->type != dev_type)
  134. continue;
  135. score = 0;
  136. if (t->parms.link != link)
  137. score |= 1;
  138. if (t->dev->type != dev_type)
  139. score |= 2;
  140. if (score == 0)
  141. return t;
  142. if (score < cand_score) {
  143. cand = t;
  144. cand_score = score;
  145. }
  146. }
  147. for_each_ip_tunnel_rcu(t, ign->tunnels_l[h1]) {
  148. if ((!ipv6_addr_equal(local, &t->parms.laddr) &&
  149. (!ipv6_addr_equal(local, &t->parms.raddr) ||
  150. !ipv6_addr_is_multicast(local))) ||
  151. key != t->parms.i_key ||
  152. !(t->dev->flags & IFF_UP))
  153. continue;
  154. if (t->dev->type != ARPHRD_IP6GRE &&
  155. t->dev->type != dev_type)
  156. continue;
  157. score = 0;
  158. if (t->parms.link != link)
  159. score |= 1;
  160. if (t->dev->type != dev_type)
  161. score |= 2;
  162. if (score == 0)
  163. return t;
  164. if (score < cand_score) {
  165. cand = t;
  166. cand_score = score;
  167. }
  168. }
  169. for_each_ip_tunnel_rcu(t, ign->tunnels_wc[h1]) {
  170. if (t->parms.i_key != key ||
  171. !(t->dev->flags & IFF_UP))
  172. continue;
  173. if (t->dev->type != ARPHRD_IP6GRE &&
  174. t->dev->type != dev_type)
  175. continue;
  176. score = 0;
  177. if (t->parms.link != link)
  178. score |= 1;
  179. if (t->dev->type != dev_type)
  180. score |= 2;
  181. if (score == 0)
  182. return t;
  183. if (score < cand_score) {
  184. cand = t;
  185. cand_score = score;
  186. }
  187. }
  188. if (cand != NULL)
  189. return cand;
  190. dev = ign->fb_tunnel_dev;
  191. if (dev->flags & IFF_UP)
  192. return netdev_priv(dev);
  193. return NULL;
  194. }
  195. static struct ip6_tnl __rcu **__ip6gre_bucket(struct ip6gre_net *ign,
  196. const struct __ip6_tnl_parm *p)
  197. {
  198. const struct in6_addr *remote = &p->raddr;
  199. const struct in6_addr *local = &p->laddr;
  200. unsigned int h = HASH_KEY(p->i_key);
  201. int prio = 0;
  202. if (!ipv6_addr_any(local))
  203. prio |= 1;
  204. if (!ipv6_addr_any(remote) && !ipv6_addr_is_multicast(remote)) {
  205. prio |= 2;
  206. h ^= HASH_ADDR(remote);
  207. }
  208. return &ign->tunnels[prio][h];
  209. }
  210. static inline struct ip6_tnl __rcu **ip6gre_bucket(struct ip6gre_net *ign,
  211. const struct ip6_tnl *t)
  212. {
  213. return __ip6gre_bucket(ign, &t->parms);
  214. }
  215. static void ip6gre_tunnel_link(struct ip6gre_net *ign, struct ip6_tnl *t)
  216. {
  217. struct ip6_tnl __rcu **tp = ip6gre_bucket(ign, t);
  218. rcu_assign_pointer(t->next, rtnl_dereference(*tp));
  219. rcu_assign_pointer(*tp, t);
  220. }
  221. static void ip6gre_tunnel_unlink(struct ip6gre_net *ign, struct ip6_tnl *t)
  222. {
  223. struct ip6_tnl __rcu **tp;
  224. struct ip6_tnl *iter;
  225. for (tp = ip6gre_bucket(ign, t);
  226. (iter = rtnl_dereference(*tp)) != NULL;
  227. tp = &iter->next) {
  228. if (t == iter) {
  229. rcu_assign_pointer(*tp, t->next);
  230. break;
  231. }
  232. }
  233. }
  234. static struct ip6_tnl *ip6gre_tunnel_find(struct net *net,
  235. const struct __ip6_tnl_parm *parms,
  236. int type)
  237. {
  238. const struct in6_addr *remote = &parms->raddr;
  239. const struct in6_addr *local = &parms->laddr;
  240. __be32 key = parms->i_key;
  241. int link = parms->link;
  242. struct ip6_tnl *t;
  243. struct ip6_tnl __rcu **tp;
  244. struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
  245. for (tp = __ip6gre_bucket(ign, parms);
  246. (t = rtnl_dereference(*tp)) != NULL;
  247. tp = &t->next)
  248. if (ipv6_addr_equal(local, &t->parms.laddr) &&
  249. ipv6_addr_equal(remote, &t->parms.raddr) &&
  250. key == t->parms.i_key &&
  251. link == t->parms.link &&
  252. type == t->dev->type)
  253. break;
  254. return t;
  255. }
  256. static struct ip6_tnl *ip6gre_tunnel_locate(struct net *net,
  257. const struct __ip6_tnl_parm *parms, int create)
  258. {
  259. struct ip6_tnl *t, *nt;
  260. struct net_device *dev;
  261. char name[IFNAMSIZ];
  262. struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
  263. t = ip6gre_tunnel_find(net, parms, ARPHRD_IP6GRE);
  264. if (t || !create)
  265. return t;
  266. if (parms->name[0])
  267. strlcpy(name, parms->name, IFNAMSIZ);
  268. else
  269. strcpy(name, "ip6gre%d");
  270. dev = alloc_netdev(sizeof(*t), name, ip6gre_tunnel_setup);
  271. if (!dev)
  272. return NULL;
  273. dev_net_set(dev, net);
  274. nt = netdev_priv(dev);
  275. nt->parms = *parms;
  276. dev->rtnl_link_ops = &ip6gre_link_ops;
  277. nt->dev = dev;
  278. nt->net = dev_net(dev);
  279. ip6gre_tnl_link_config(nt, 1);
  280. if (register_netdevice(dev) < 0)
  281. goto failed_free;
  282. /* Can use a lockless transmit, unless we generate output sequences */
  283. if (!(nt->parms.o_flags & GRE_SEQ))
  284. dev->features |= NETIF_F_LLTX;
  285. dev_hold(dev);
  286. ip6gre_tunnel_link(ign, nt);
  287. return nt;
  288. failed_free:
  289. free_netdev(dev);
  290. return NULL;
  291. }
  292. static void ip6gre_tunnel_uninit(struct net_device *dev)
  293. {
  294. struct net *net = dev_net(dev);
  295. struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
  296. ip6gre_tunnel_unlink(ign, netdev_priv(dev));
  297. dev_put(dev);
  298. }
  299. static void ip6gre_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
  300. u8 type, u8 code, int offset, __be32 info)
  301. {
  302. const struct ipv6hdr *ipv6h = (const struct ipv6hdr *)skb->data;
  303. __be16 *p = (__be16 *)(skb->data + offset);
  304. int grehlen = offset + 4;
  305. struct ip6_tnl *t;
  306. __be16 flags;
  307. flags = p[0];
  308. if (flags&(GRE_CSUM|GRE_KEY|GRE_SEQ|GRE_ROUTING|GRE_VERSION)) {
  309. if (flags&(GRE_VERSION|GRE_ROUTING))
  310. return;
  311. if (flags&GRE_KEY) {
  312. grehlen += 4;
  313. if (flags&GRE_CSUM)
  314. grehlen += 4;
  315. }
  316. }
  317. /* If only 8 bytes returned, keyed message will be dropped here */
  318. if (!pskb_may_pull(skb, grehlen))
  319. return;
  320. ipv6h = (const struct ipv6hdr *)skb->data;
  321. p = (__be16 *)(skb->data + offset);
  322. t = ip6gre_tunnel_lookup(skb->dev, &ipv6h->daddr, &ipv6h->saddr,
  323. flags & GRE_KEY ?
  324. *(((__be32 *)p) + (grehlen / 4) - 1) : 0,
  325. p[1]);
  326. if (t == NULL)
  327. return;
  328. switch (type) {
  329. __u32 teli;
  330. struct ipv6_tlv_tnl_enc_lim *tel;
  331. __u32 mtu;
  332. case ICMPV6_DEST_UNREACH:
  333. net_warn_ratelimited("%s: Path to destination invalid or inactive!\n",
  334. t->parms.name);
  335. break;
  336. case ICMPV6_TIME_EXCEED:
  337. if (code == ICMPV6_EXC_HOPLIMIT) {
  338. net_warn_ratelimited("%s: Too small hop limit or routing loop in tunnel!\n",
  339. t->parms.name);
  340. }
  341. break;
  342. case ICMPV6_PARAMPROB:
  343. teli = 0;
  344. if (code == ICMPV6_HDR_FIELD)
  345. teli = ip6_tnl_parse_tlv_enc_lim(skb, skb->data);
  346. if (teli && teli == info - 2) {
  347. tel = (struct ipv6_tlv_tnl_enc_lim *) &skb->data[teli];
  348. if (tel->encap_limit == 0) {
  349. net_warn_ratelimited("%s: Too small encapsulation limit or routing loop in tunnel!\n",
  350. t->parms.name);
  351. }
  352. } else {
  353. net_warn_ratelimited("%s: Recipient unable to parse tunneled packet!\n",
  354. t->parms.name);
  355. }
  356. break;
  357. case ICMPV6_PKT_TOOBIG:
  358. mtu = info - offset;
  359. if (mtu < IPV6_MIN_MTU)
  360. mtu = IPV6_MIN_MTU;
  361. t->dev->mtu = mtu;
  362. break;
  363. }
  364. if (time_before(jiffies, t->err_time + IP6TUNNEL_ERR_TIMEO))
  365. t->err_count++;
  366. else
  367. t->err_count = 1;
  368. t->err_time = jiffies;
  369. }
  370. static int ip6gre_rcv(struct sk_buff *skb)
  371. {
  372. const struct ipv6hdr *ipv6h;
  373. u8 *h;
  374. __be16 flags;
  375. __sum16 csum = 0;
  376. __be32 key = 0;
  377. u32 seqno = 0;
  378. struct ip6_tnl *tunnel;
  379. int offset = 4;
  380. __be16 gre_proto;
  381. int err;
  382. if (!pskb_may_pull(skb, sizeof(struct in6_addr)))
  383. goto drop;
  384. ipv6h = ipv6_hdr(skb);
  385. h = skb->data;
  386. flags = *(__be16 *)h;
  387. if (flags&(GRE_CSUM|GRE_KEY|GRE_ROUTING|GRE_SEQ|GRE_VERSION)) {
  388. /* - Version must be 0.
  389. - We do not support routing headers.
  390. */
  391. if (flags&(GRE_VERSION|GRE_ROUTING))
  392. goto drop;
  393. if (flags&GRE_CSUM) {
  394. switch (skb->ip_summed) {
  395. case CHECKSUM_COMPLETE:
  396. csum = csum_fold(skb->csum);
  397. if (!csum)
  398. break;
  399. /* fall through */
  400. case CHECKSUM_NONE:
  401. skb->csum = 0;
  402. csum = __skb_checksum_complete(skb);
  403. skb->ip_summed = CHECKSUM_COMPLETE;
  404. }
  405. offset += 4;
  406. }
  407. if (flags&GRE_KEY) {
  408. key = *(__be32 *)(h + offset);
  409. offset += 4;
  410. }
  411. if (flags&GRE_SEQ) {
  412. seqno = ntohl(*(__be32 *)(h + offset));
  413. offset += 4;
  414. }
  415. }
  416. gre_proto = *(__be16 *)(h + 2);
  417. tunnel = ip6gre_tunnel_lookup(skb->dev,
  418. &ipv6h->saddr, &ipv6h->daddr, key,
  419. gre_proto);
  420. if (tunnel) {
  421. struct pcpu_sw_netstats *tstats;
  422. if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb))
  423. goto drop;
  424. if (!ip6_tnl_rcv_ctl(tunnel, &ipv6h->daddr, &ipv6h->saddr)) {
  425. tunnel->dev->stats.rx_dropped++;
  426. goto drop;
  427. }
  428. skb->protocol = gre_proto;
  429. /* WCCP version 1 and 2 protocol decoding.
  430. * - Change protocol to IP
  431. * - When dealing with WCCPv2, Skip extra 4 bytes in GRE header
  432. */
  433. if (flags == 0 && gre_proto == htons(ETH_P_WCCP)) {
  434. skb->protocol = htons(ETH_P_IP);
  435. if ((*(h + offset) & 0xF0) != 0x40)
  436. offset += 4;
  437. }
  438. skb->mac_header = skb->network_header;
  439. __pskb_pull(skb, offset);
  440. skb_postpull_rcsum(skb, skb_transport_header(skb), offset);
  441. if (((flags&GRE_CSUM) && csum) ||
  442. (!(flags&GRE_CSUM) && tunnel->parms.i_flags&GRE_CSUM)) {
  443. tunnel->dev->stats.rx_crc_errors++;
  444. tunnel->dev->stats.rx_errors++;
  445. goto drop;
  446. }
  447. if (tunnel->parms.i_flags&GRE_SEQ) {
  448. if (!(flags&GRE_SEQ) ||
  449. (tunnel->i_seqno &&
  450. (s32)(seqno - tunnel->i_seqno) < 0)) {
  451. tunnel->dev->stats.rx_fifo_errors++;
  452. tunnel->dev->stats.rx_errors++;
  453. goto drop;
  454. }
  455. tunnel->i_seqno = seqno + 1;
  456. }
  457. /* Warning: All skb pointers will be invalidated! */
  458. if (tunnel->dev->type == ARPHRD_ETHER) {
  459. if (!pskb_may_pull(skb, ETH_HLEN)) {
  460. tunnel->dev->stats.rx_length_errors++;
  461. tunnel->dev->stats.rx_errors++;
  462. goto drop;
  463. }
  464. ipv6h = ipv6_hdr(skb);
  465. skb->protocol = eth_type_trans(skb, tunnel->dev);
  466. skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN);
  467. }
  468. __skb_tunnel_rx(skb, tunnel->dev, tunnel->net);
  469. skb_reset_network_header(skb);
  470. err = IP6_ECN_decapsulate(ipv6h, skb);
  471. if (unlikely(err)) {
  472. if (log_ecn_error)
  473. net_info_ratelimited("non-ECT from %pI6 with dsfield=%#x\n",
  474. &ipv6h->saddr,
  475. ipv6_get_dsfield(ipv6h));
  476. if (err > 1) {
  477. ++tunnel->dev->stats.rx_frame_errors;
  478. ++tunnel->dev->stats.rx_errors;
  479. goto drop;
  480. }
  481. }
  482. tstats = this_cpu_ptr(tunnel->dev->tstats);
  483. u64_stats_update_begin(&tstats->syncp);
  484. tstats->rx_packets++;
  485. tstats->rx_bytes += skb->len;
  486. u64_stats_update_end(&tstats->syncp);
  487. netif_rx(skb);
  488. return 0;
  489. }
  490. icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0);
  491. drop:
  492. kfree_skb(skb);
  493. return 0;
  494. }
  495. struct ipv6_tel_txoption {
  496. struct ipv6_txoptions ops;
  497. __u8 dst_opt[8];
  498. };
  499. static void init_tel_txopt(struct ipv6_tel_txoption *opt, __u8 encap_limit)
  500. {
  501. memset(opt, 0, sizeof(struct ipv6_tel_txoption));
  502. opt->dst_opt[2] = IPV6_TLV_TNL_ENCAP_LIMIT;
  503. opt->dst_opt[3] = 1;
  504. opt->dst_opt[4] = encap_limit;
  505. opt->dst_opt[5] = IPV6_TLV_PADN;
  506. opt->dst_opt[6] = 1;
  507. opt->ops.dst0opt = (struct ipv6_opt_hdr *) opt->dst_opt;
  508. opt->ops.opt_nflen = 8;
  509. }
  510. static netdev_tx_t ip6gre_xmit2(struct sk_buff *skb,
  511. struct net_device *dev,
  512. __u8 dsfield,
  513. struct flowi6 *fl6,
  514. int encap_limit,
  515. __u32 *pmtu)
  516. {
  517. struct net *net = dev_net(dev);
  518. struct ip6_tnl *tunnel = netdev_priv(dev);
  519. struct net_device *tdev; /* Device to other host */
  520. struct ipv6hdr *ipv6h; /* Our new IP header */
  521. unsigned int max_headroom = 0; /* The extra header space needed */
  522. int gre_hlen;
  523. struct ipv6_tel_txoption opt;
  524. int mtu;
  525. struct dst_entry *dst = NULL, *ndst = NULL;
  526. struct net_device_stats *stats = &tunnel->dev->stats;
  527. int err = -1;
  528. u8 proto;
  529. struct sk_buff *new_skb;
  530. if (dev->type == ARPHRD_ETHER)
  531. IPCB(skb)->flags = 0;
  532. if (dev->header_ops && dev->type == ARPHRD_IP6GRE) {
  533. gre_hlen = 0;
  534. ipv6h = (struct ipv6hdr *)skb->data;
  535. fl6->daddr = ipv6h->daddr;
  536. } else {
  537. gre_hlen = tunnel->hlen;
  538. fl6->daddr = tunnel->parms.raddr;
  539. }
  540. if (!fl6->flowi6_mark)
  541. dst = ip6_tnl_dst_check(tunnel);
  542. if (!dst) {
  543. ndst = ip6_route_output(net, NULL, fl6);
  544. if (ndst->error)
  545. goto tx_err_link_failure;
  546. ndst = xfrm_lookup(net, ndst, flowi6_to_flowi(fl6), NULL, 0);
  547. if (IS_ERR(ndst)) {
  548. err = PTR_ERR(ndst);
  549. ndst = NULL;
  550. goto tx_err_link_failure;
  551. }
  552. dst = ndst;
  553. }
  554. tdev = dst->dev;
  555. if (tdev == dev) {
  556. stats->collisions++;
  557. net_warn_ratelimited("%s: Local routing loop detected!\n",
  558. tunnel->parms.name);
  559. goto tx_err_dst_release;
  560. }
  561. mtu = dst_mtu(dst) - sizeof(*ipv6h);
  562. if (encap_limit >= 0) {
  563. max_headroom += 8;
  564. mtu -= 8;
  565. }
  566. if (mtu < IPV6_MIN_MTU)
  567. mtu = IPV6_MIN_MTU;
  568. if (skb_dst(skb))
  569. skb_dst(skb)->ops->update_pmtu(skb_dst(skb), NULL, skb, mtu);
  570. if (skb->len > mtu) {
  571. *pmtu = mtu;
  572. err = -EMSGSIZE;
  573. goto tx_err_dst_release;
  574. }
  575. if (tunnel->err_count > 0) {
  576. if (time_before(jiffies,
  577. tunnel->err_time + IP6TUNNEL_ERR_TIMEO)) {
  578. tunnel->err_count--;
  579. dst_link_failure(skb);
  580. } else
  581. tunnel->err_count = 0;
  582. }
  583. skb_scrub_packet(skb, !net_eq(tunnel->net, dev_net(dev)));
  584. max_headroom += LL_RESERVED_SPACE(tdev) + gre_hlen + dst->header_len;
  585. if (skb_headroom(skb) < max_headroom || skb_shared(skb) ||
  586. (skb_cloned(skb) && !skb_clone_writable(skb, 0))) {
  587. new_skb = skb_realloc_headroom(skb, max_headroom);
  588. if (max_headroom > dev->needed_headroom)
  589. dev->needed_headroom = max_headroom;
  590. if (!new_skb)
  591. goto tx_err_dst_release;
  592. if (skb->sk)
  593. skb_set_owner_w(new_skb, skb->sk);
  594. consume_skb(skb);
  595. skb = new_skb;
  596. }
  597. if (fl6->flowi6_mark) {
  598. skb_dst_set(skb, dst);
  599. ndst = NULL;
  600. } else {
  601. skb_dst_set_noref(skb, dst);
  602. }
  603. proto = NEXTHDR_GRE;
  604. if (encap_limit >= 0) {
  605. init_tel_txopt(&opt, encap_limit);
  606. ipv6_push_nfrag_opts(skb, &opt.ops, &proto, NULL);
  607. }
  608. if (likely(!skb->encapsulation)) {
  609. skb_reset_inner_headers(skb);
  610. skb->encapsulation = 1;
  611. }
  612. skb_push(skb, gre_hlen);
  613. skb_reset_network_header(skb);
  614. skb_set_transport_header(skb, sizeof(*ipv6h));
  615. /*
  616. * Push down and install the IP header.
  617. */
  618. ipv6h = ipv6_hdr(skb);
  619. ip6_flow_hdr(ipv6h, INET_ECN_encapsulate(0, dsfield), fl6->flowlabel);
  620. ipv6h->hop_limit = tunnel->parms.hop_limit;
  621. ipv6h->nexthdr = proto;
  622. ipv6h->saddr = fl6->saddr;
  623. ipv6h->daddr = fl6->daddr;
  624. ((__be16 *)(ipv6h + 1))[0] = tunnel->parms.o_flags;
  625. ((__be16 *)(ipv6h + 1))[1] = (dev->type == ARPHRD_ETHER) ?
  626. htons(ETH_P_TEB) : skb->protocol;
  627. if (tunnel->parms.o_flags&(GRE_KEY|GRE_CSUM|GRE_SEQ)) {
  628. __be32 *ptr = (__be32 *)(((u8 *)ipv6h) + tunnel->hlen - 4);
  629. if (tunnel->parms.o_flags&GRE_SEQ) {
  630. ++tunnel->o_seqno;
  631. *ptr = htonl(tunnel->o_seqno);
  632. ptr--;
  633. }
  634. if (tunnel->parms.o_flags&GRE_KEY) {
  635. *ptr = tunnel->parms.o_key;
  636. ptr--;
  637. }
  638. if (tunnel->parms.o_flags&GRE_CSUM) {
  639. *ptr = 0;
  640. *(__sum16 *)ptr = ip_compute_csum((void *)(ipv6h+1),
  641. skb->len - sizeof(struct ipv6hdr));
  642. }
  643. }
  644. ip6tunnel_xmit(skb, dev);
  645. if (ndst)
  646. ip6_tnl_dst_store(tunnel, ndst);
  647. return 0;
  648. tx_err_link_failure:
  649. stats->tx_carrier_errors++;
  650. dst_link_failure(skb);
  651. tx_err_dst_release:
  652. dst_release(ndst);
  653. return err;
  654. }
  655. static inline int ip6gre_xmit_ipv4(struct sk_buff *skb, struct net_device *dev)
  656. {
  657. struct ip6_tnl *t = netdev_priv(dev);
  658. const struct iphdr *iph = ip_hdr(skb);
  659. int encap_limit = -1;
  660. struct flowi6 fl6;
  661. __u8 dsfield;
  662. __u32 mtu;
  663. int err;
  664. if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
  665. encap_limit = t->parms.encap_limit;
  666. memcpy(&fl6, &t->fl.u.ip6, sizeof(fl6));
  667. fl6.flowi6_proto = IPPROTO_IPIP;
  668. dsfield = ipv4_get_dsfield(iph);
  669. if (t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS)
  670. fl6.flowlabel |= htonl((__u32)iph->tos << IPV6_TCLASS_SHIFT)
  671. & IPV6_TCLASS_MASK;
  672. if (t->parms.flags & IP6_TNL_F_USE_ORIG_FWMARK)
  673. fl6.flowi6_mark = skb->mark;
  674. err = ip6gre_xmit2(skb, dev, dsfield, &fl6, encap_limit, &mtu);
  675. if (err != 0) {
  676. /* XXX: send ICMP error even if DF is not set. */
  677. if (err == -EMSGSIZE)
  678. icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED,
  679. htonl(mtu));
  680. return -1;
  681. }
  682. return 0;
  683. }
  684. static inline int ip6gre_xmit_ipv6(struct sk_buff *skb, struct net_device *dev)
  685. {
  686. struct ip6_tnl *t = netdev_priv(dev);
  687. struct ipv6hdr *ipv6h = ipv6_hdr(skb);
  688. int encap_limit = -1;
  689. __u16 offset;
  690. struct flowi6 fl6;
  691. __u8 dsfield;
  692. __u32 mtu;
  693. int err;
  694. if (ipv6_addr_equal(&t->parms.raddr, &ipv6h->saddr))
  695. return -1;
  696. offset = ip6_tnl_parse_tlv_enc_lim(skb, skb_network_header(skb));
  697. if (offset > 0) {
  698. struct ipv6_tlv_tnl_enc_lim *tel;
  699. tel = (struct ipv6_tlv_tnl_enc_lim *)&skb_network_header(skb)[offset];
  700. if (tel->encap_limit == 0) {
  701. icmpv6_send(skb, ICMPV6_PARAMPROB,
  702. ICMPV6_HDR_FIELD, offset + 2);
  703. return -1;
  704. }
  705. encap_limit = tel->encap_limit - 1;
  706. } else if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
  707. encap_limit = t->parms.encap_limit;
  708. memcpy(&fl6, &t->fl.u.ip6, sizeof(fl6));
  709. fl6.flowi6_proto = IPPROTO_IPV6;
  710. dsfield = ipv6_get_dsfield(ipv6h);
  711. if (t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS)
  712. fl6.flowlabel |= (*(__be32 *) ipv6h & IPV6_TCLASS_MASK);
  713. if (t->parms.flags & IP6_TNL_F_USE_ORIG_FLOWLABEL)
  714. fl6.flowlabel |= ip6_flowlabel(ipv6h);
  715. if (t->parms.flags & IP6_TNL_F_USE_ORIG_FWMARK)
  716. fl6.flowi6_mark = skb->mark;
  717. err = ip6gre_xmit2(skb, dev, dsfield, &fl6, encap_limit, &mtu);
  718. if (err != 0) {
  719. if (err == -EMSGSIZE)
  720. icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
  721. return -1;
  722. }
  723. return 0;
  724. }
  725. /**
  726. * ip6_tnl_addr_conflict - compare packet addresses to tunnel's own
  727. * @t: the outgoing tunnel device
  728. * @hdr: IPv6 header from the incoming packet
  729. *
  730. * Description:
  731. * Avoid trivial tunneling loop by checking that tunnel exit-point
  732. * doesn't match source of incoming packet.
  733. *
  734. * Return:
  735. * 1 if conflict,
  736. * 0 else
  737. **/
  738. static inline bool ip6gre_tnl_addr_conflict(const struct ip6_tnl *t,
  739. const struct ipv6hdr *hdr)
  740. {
  741. return ipv6_addr_equal(&t->parms.raddr, &hdr->saddr);
  742. }
  743. static int ip6gre_xmit_other(struct sk_buff *skb, struct net_device *dev)
  744. {
  745. struct ip6_tnl *t = netdev_priv(dev);
  746. int encap_limit = -1;
  747. struct flowi6 fl6;
  748. __u32 mtu;
  749. int err;
  750. if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
  751. encap_limit = t->parms.encap_limit;
  752. memcpy(&fl6, &t->fl.u.ip6, sizeof(fl6));
  753. fl6.flowi6_proto = skb->protocol;
  754. err = ip6gre_xmit2(skb, dev, 0, &fl6, encap_limit, &mtu);
  755. return err;
  756. }
  757. static netdev_tx_t ip6gre_tunnel_xmit(struct sk_buff *skb,
  758. struct net_device *dev)
  759. {
  760. struct ip6_tnl *t = netdev_priv(dev);
  761. struct net_device_stats *stats = &t->dev->stats;
  762. int ret;
  763. if (!ip6_tnl_xmit_ctl(t))
  764. goto tx_err;
  765. switch (skb->protocol) {
  766. case htons(ETH_P_IP):
  767. ret = ip6gre_xmit_ipv4(skb, dev);
  768. break;
  769. case htons(ETH_P_IPV6):
  770. ret = ip6gre_xmit_ipv6(skb, dev);
  771. break;
  772. default:
  773. ret = ip6gre_xmit_other(skb, dev);
  774. break;
  775. }
  776. if (ret < 0)
  777. goto tx_err;
  778. return NETDEV_TX_OK;
  779. tx_err:
  780. stats->tx_errors++;
  781. stats->tx_dropped++;
  782. kfree_skb(skb);
  783. return NETDEV_TX_OK;
  784. }
  785. static void ip6gre_tnl_link_config(struct ip6_tnl *t, int set_mtu)
  786. {
  787. struct net_device *dev = t->dev;
  788. struct __ip6_tnl_parm *p = &t->parms;
  789. struct flowi6 *fl6 = &t->fl.u.ip6;
  790. int addend = sizeof(struct ipv6hdr) + 4;
  791. if (dev->type != ARPHRD_ETHER) {
  792. memcpy(dev->dev_addr, &p->laddr, sizeof(struct in6_addr));
  793. memcpy(dev->broadcast, &p->raddr, sizeof(struct in6_addr));
  794. }
  795. /* Set up flowi template */
  796. fl6->saddr = p->laddr;
  797. fl6->daddr = p->raddr;
  798. fl6->flowi6_oif = p->link;
  799. fl6->flowlabel = 0;
  800. if (!(p->flags&IP6_TNL_F_USE_ORIG_TCLASS))
  801. fl6->flowlabel |= IPV6_TCLASS_MASK & p->flowinfo;
  802. if (!(p->flags&IP6_TNL_F_USE_ORIG_FLOWLABEL))
  803. fl6->flowlabel |= IPV6_FLOWLABEL_MASK & p->flowinfo;
  804. p->flags &= ~(IP6_TNL_F_CAP_XMIT|IP6_TNL_F_CAP_RCV|IP6_TNL_F_CAP_PER_PACKET);
  805. p->flags |= ip6_tnl_get_cap(t, &p->laddr, &p->raddr);
  806. if (p->flags&IP6_TNL_F_CAP_XMIT &&
  807. p->flags&IP6_TNL_F_CAP_RCV && dev->type != ARPHRD_ETHER)
  808. dev->flags |= IFF_POINTOPOINT;
  809. else
  810. dev->flags &= ~IFF_POINTOPOINT;
  811. dev->iflink = p->link;
  812. /* Precalculate GRE options length */
  813. if (t->parms.o_flags&(GRE_CSUM|GRE_KEY|GRE_SEQ)) {
  814. if (t->parms.o_flags&GRE_CSUM)
  815. addend += 4;
  816. if (t->parms.o_flags&GRE_KEY)
  817. addend += 4;
  818. if (t->parms.o_flags&GRE_SEQ)
  819. addend += 4;
  820. }
  821. t->hlen = addend;
  822. if (p->flags & IP6_TNL_F_CAP_XMIT) {
  823. int strict = (ipv6_addr_type(&p->raddr) &
  824. (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL));
  825. struct rt6_info *rt = rt6_lookup(dev_net(dev),
  826. &p->raddr, &p->laddr,
  827. p->link, strict);
  828. if (rt == NULL)
  829. return;
  830. if (rt->dst.dev) {
  831. dev->hard_header_len = rt->dst.dev->hard_header_len + addend;
  832. if (set_mtu) {
  833. dev->mtu = rt->dst.dev->mtu - addend;
  834. if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
  835. dev->mtu -= 8;
  836. if (dev->mtu < IPV6_MIN_MTU)
  837. dev->mtu = IPV6_MIN_MTU;
  838. }
  839. }
  840. ip6_rt_put(rt);
  841. }
  842. }
  843. static int ip6gre_tnl_change(struct ip6_tnl *t,
  844. const struct __ip6_tnl_parm *p, int set_mtu)
  845. {
  846. t->parms.laddr = p->laddr;
  847. t->parms.raddr = p->raddr;
  848. t->parms.flags = p->flags;
  849. t->parms.hop_limit = p->hop_limit;
  850. t->parms.encap_limit = p->encap_limit;
  851. t->parms.flowinfo = p->flowinfo;
  852. t->parms.link = p->link;
  853. t->parms.proto = p->proto;
  854. t->parms.i_key = p->i_key;
  855. t->parms.o_key = p->o_key;
  856. t->parms.i_flags = p->i_flags;
  857. t->parms.o_flags = p->o_flags;
  858. ip6_tnl_dst_reset(t);
  859. ip6gre_tnl_link_config(t, set_mtu);
  860. return 0;
  861. }
  862. static void ip6gre_tnl_parm_from_user(struct __ip6_tnl_parm *p,
  863. const struct ip6_tnl_parm2 *u)
  864. {
  865. p->laddr = u->laddr;
  866. p->raddr = u->raddr;
  867. p->flags = u->flags;
  868. p->hop_limit = u->hop_limit;
  869. p->encap_limit = u->encap_limit;
  870. p->flowinfo = u->flowinfo;
  871. p->link = u->link;
  872. p->i_key = u->i_key;
  873. p->o_key = u->o_key;
  874. p->i_flags = u->i_flags;
  875. p->o_flags = u->o_flags;
  876. memcpy(p->name, u->name, sizeof(u->name));
  877. }
  878. static void ip6gre_tnl_parm_to_user(struct ip6_tnl_parm2 *u,
  879. const struct __ip6_tnl_parm *p)
  880. {
  881. u->proto = IPPROTO_GRE;
  882. u->laddr = p->laddr;
  883. u->raddr = p->raddr;
  884. u->flags = p->flags;
  885. u->hop_limit = p->hop_limit;
  886. u->encap_limit = p->encap_limit;
  887. u->flowinfo = p->flowinfo;
  888. u->link = p->link;
  889. u->i_key = p->i_key;
  890. u->o_key = p->o_key;
  891. u->i_flags = p->i_flags;
  892. u->o_flags = p->o_flags;
  893. memcpy(u->name, p->name, sizeof(u->name));
  894. }
  895. static int ip6gre_tunnel_ioctl(struct net_device *dev,
  896. struct ifreq *ifr, int cmd)
  897. {
  898. int err = 0;
  899. struct ip6_tnl_parm2 p;
  900. struct __ip6_tnl_parm p1;
  901. struct ip6_tnl *t;
  902. struct net *net = dev_net(dev);
  903. struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
  904. switch (cmd) {
  905. case SIOCGETTUNNEL:
  906. t = NULL;
  907. if (dev == ign->fb_tunnel_dev) {
  908. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) {
  909. err = -EFAULT;
  910. break;
  911. }
  912. ip6gre_tnl_parm_from_user(&p1, &p);
  913. t = ip6gre_tunnel_locate(net, &p1, 0);
  914. }
  915. if (t == NULL)
  916. t = netdev_priv(dev);
  917. memset(&p, 0, sizeof(p));
  918. ip6gre_tnl_parm_to_user(&p, &t->parms);
  919. if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p)))
  920. err = -EFAULT;
  921. break;
  922. case SIOCADDTUNNEL:
  923. case SIOCCHGTUNNEL:
  924. err = -EPERM;
  925. if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  926. goto done;
  927. err = -EFAULT;
  928. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
  929. goto done;
  930. err = -EINVAL;
  931. if ((p.i_flags|p.o_flags)&(GRE_VERSION|GRE_ROUTING))
  932. goto done;
  933. if (!(p.i_flags&GRE_KEY))
  934. p.i_key = 0;
  935. if (!(p.o_flags&GRE_KEY))
  936. p.o_key = 0;
  937. ip6gre_tnl_parm_from_user(&p1, &p);
  938. t = ip6gre_tunnel_locate(net, &p1, cmd == SIOCADDTUNNEL);
  939. if (dev != ign->fb_tunnel_dev && cmd == SIOCCHGTUNNEL) {
  940. if (t != NULL) {
  941. if (t->dev != dev) {
  942. err = -EEXIST;
  943. break;
  944. }
  945. } else {
  946. t = netdev_priv(dev);
  947. ip6gre_tunnel_unlink(ign, t);
  948. synchronize_net();
  949. ip6gre_tnl_change(t, &p1, 1);
  950. ip6gre_tunnel_link(ign, t);
  951. netdev_state_change(dev);
  952. }
  953. }
  954. if (t) {
  955. err = 0;
  956. memset(&p, 0, sizeof(p));
  957. ip6gre_tnl_parm_to_user(&p, &t->parms);
  958. if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p)))
  959. err = -EFAULT;
  960. } else
  961. err = (cmd == SIOCADDTUNNEL ? -ENOBUFS : -ENOENT);
  962. break;
  963. case SIOCDELTUNNEL:
  964. err = -EPERM;
  965. if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  966. goto done;
  967. if (dev == ign->fb_tunnel_dev) {
  968. err = -EFAULT;
  969. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
  970. goto done;
  971. err = -ENOENT;
  972. ip6gre_tnl_parm_from_user(&p1, &p);
  973. t = ip6gre_tunnel_locate(net, &p1, 0);
  974. if (t == NULL)
  975. goto done;
  976. err = -EPERM;
  977. if (t == netdev_priv(ign->fb_tunnel_dev))
  978. goto done;
  979. dev = t->dev;
  980. }
  981. unregister_netdevice(dev);
  982. err = 0;
  983. break;
  984. default:
  985. err = -EINVAL;
  986. }
  987. done:
  988. return err;
  989. }
  990. static int ip6gre_tunnel_change_mtu(struct net_device *dev, int new_mtu)
  991. {
  992. if (new_mtu < 68 ||
  993. new_mtu > 0xFFF8 - dev->hard_header_len)
  994. return -EINVAL;
  995. dev->mtu = new_mtu;
  996. return 0;
  997. }
  998. static int ip6gre_header(struct sk_buff *skb, struct net_device *dev,
  999. unsigned short type,
  1000. const void *daddr, const void *saddr, unsigned int len)
  1001. {
  1002. struct ip6_tnl *t = netdev_priv(dev);
  1003. struct ipv6hdr *ipv6h = (struct ipv6hdr *)skb_push(skb, t->hlen);
  1004. __be16 *p = (__be16 *)(ipv6h+1);
  1005. ip6_flow_hdr(ipv6h, 0, t->fl.u.ip6.flowlabel);
  1006. ipv6h->hop_limit = t->parms.hop_limit;
  1007. ipv6h->nexthdr = NEXTHDR_GRE;
  1008. ipv6h->saddr = t->parms.laddr;
  1009. ipv6h->daddr = t->parms.raddr;
  1010. p[0] = t->parms.o_flags;
  1011. p[1] = htons(type);
  1012. /*
  1013. * Set the source hardware address.
  1014. */
  1015. if (saddr)
  1016. memcpy(&ipv6h->saddr, saddr, sizeof(struct in6_addr));
  1017. if (daddr)
  1018. memcpy(&ipv6h->daddr, daddr, sizeof(struct in6_addr));
  1019. if (!ipv6_addr_any(&ipv6h->daddr))
  1020. return t->hlen;
  1021. return -t->hlen;
  1022. }
  1023. static const struct header_ops ip6gre_header_ops = {
  1024. .create = ip6gre_header,
  1025. };
  1026. static const struct net_device_ops ip6gre_netdev_ops = {
  1027. .ndo_init = ip6gre_tunnel_init,
  1028. .ndo_uninit = ip6gre_tunnel_uninit,
  1029. .ndo_start_xmit = ip6gre_tunnel_xmit,
  1030. .ndo_do_ioctl = ip6gre_tunnel_ioctl,
  1031. .ndo_change_mtu = ip6gre_tunnel_change_mtu,
  1032. .ndo_get_stats64 = ip_tunnel_get_stats64,
  1033. };
  1034. static void ip6gre_dev_free(struct net_device *dev)
  1035. {
  1036. free_percpu(dev->tstats);
  1037. free_netdev(dev);
  1038. }
  1039. static void ip6gre_tunnel_setup(struct net_device *dev)
  1040. {
  1041. struct ip6_tnl *t;
  1042. dev->netdev_ops = &ip6gre_netdev_ops;
  1043. dev->destructor = ip6gre_dev_free;
  1044. dev->type = ARPHRD_IP6GRE;
  1045. dev->hard_header_len = LL_MAX_HEADER + sizeof(struct ipv6hdr) + 4;
  1046. dev->mtu = ETH_DATA_LEN - sizeof(struct ipv6hdr) - 4;
  1047. t = netdev_priv(dev);
  1048. if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
  1049. dev->mtu -= 8;
  1050. dev->flags |= IFF_NOARP;
  1051. dev->iflink = 0;
  1052. dev->addr_len = sizeof(struct in6_addr);
  1053. dev->features |= NETIF_F_NETNS_LOCAL;
  1054. dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
  1055. }
  1056. static int ip6gre_tunnel_init(struct net_device *dev)
  1057. {
  1058. struct ip6_tnl *tunnel;
  1059. int i;
  1060. tunnel = netdev_priv(dev);
  1061. tunnel->dev = dev;
  1062. tunnel->net = dev_net(dev);
  1063. strcpy(tunnel->parms.name, dev->name);
  1064. memcpy(dev->dev_addr, &tunnel->parms.laddr, sizeof(struct in6_addr));
  1065. memcpy(dev->broadcast, &tunnel->parms.raddr, sizeof(struct in6_addr));
  1066. if (ipv6_addr_any(&tunnel->parms.raddr))
  1067. dev->header_ops = &ip6gre_header_ops;
  1068. dev->tstats = alloc_percpu(struct pcpu_sw_netstats);
  1069. if (!dev->tstats)
  1070. return -ENOMEM;
  1071. for_each_possible_cpu(i) {
  1072. struct pcpu_sw_netstats *ip6gre_tunnel_stats;
  1073. ip6gre_tunnel_stats = per_cpu_ptr(dev->tstats, i);
  1074. u64_stats_init(&ip6gre_tunnel_stats->syncp);
  1075. }
  1076. return 0;
  1077. }
  1078. static void ip6gre_fb_tunnel_init(struct net_device *dev)
  1079. {
  1080. struct ip6_tnl *tunnel = netdev_priv(dev);
  1081. tunnel->dev = dev;
  1082. tunnel->net = dev_net(dev);
  1083. strcpy(tunnel->parms.name, dev->name);
  1084. tunnel->hlen = sizeof(struct ipv6hdr) + 4;
  1085. dev_hold(dev);
  1086. }
  1087. static struct inet6_protocol ip6gre_protocol __read_mostly = {
  1088. .handler = ip6gre_rcv,
  1089. .err_handler = ip6gre_err,
  1090. .flags = INET6_PROTO_NOPOLICY|INET6_PROTO_FINAL,
  1091. };
  1092. static void ip6gre_destroy_tunnels(struct ip6gre_net *ign,
  1093. struct list_head *head)
  1094. {
  1095. int prio;
  1096. for (prio = 0; prio < 4; prio++) {
  1097. int h;
  1098. for (h = 0; h < HASH_SIZE; h++) {
  1099. struct ip6_tnl *t;
  1100. t = rtnl_dereference(ign->tunnels[prio][h]);
  1101. while (t != NULL) {
  1102. unregister_netdevice_queue(t->dev, head);
  1103. t = rtnl_dereference(t->next);
  1104. }
  1105. }
  1106. }
  1107. }
  1108. static int __net_init ip6gre_init_net(struct net *net)
  1109. {
  1110. struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
  1111. int err;
  1112. ign->fb_tunnel_dev = alloc_netdev(sizeof(struct ip6_tnl), "ip6gre0",
  1113. ip6gre_tunnel_setup);
  1114. if (!ign->fb_tunnel_dev) {
  1115. err = -ENOMEM;
  1116. goto err_alloc_dev;
  1117. }
  1118. dev_net_set(ign->fb_tunnel_dev, net);
  1119. ip6gre_fb_tunnel_init(ign->fb_tunnel_dev);
  1120. ign->fb_tunnel_dev->rtnl_link_ops = &ip6gre_link_ops;
  1121. err = register_netdev(ign->fb_tunnel_dev);
  1122. if (err)
  1123. goto err_reg_dev;
  1124. rcu_assign_pointer(ign->tunnels_wc[0],
  1125. netdev_priv(ign->fb_tunnel_dev));
  1126. return 0;
  1127. err_reg_dev:
  1128. ip6gre_dev_free(ign->fb_tunnel_dev);
  1129. err_alloc_dev:
  1130. return err;
  1131. }
  1132. static void __net_exit ip6gre_exit_net(struct net *net)
  1133. {
  1134. struct ip6gre_net *ign;
  1135. LIST_HEAD(list);
  1136. ign = net_generic(net, ip6gre_net_id);
  1137. rtnl_lock();
  1138. ip6gre_destroy_tunnels(ign, &list);
  1139. unregister_netdevice_many(&list);
  1140. rtnl_unlock();
  1141. }
  1142. static struct pernet_operations ip6gre_net_ops = {
  1143. .init = ip6gre_init_net,
  1144. .exit = ip6gre_exit_net,
  1145. .id = &ip6gre_net_id,
  1146. .size = sizeof(struct ip6gre_net),
  1147. };
  1148. static int ip6gre_tunnel_validate(struct nlattr *tb[], struct nlattr *data[])
  1149. {
  1150. __be16 flags;
  1151. if (!data)
  1152. return 0;
  1153. flags = 0;
  1154. if (data[IFLA_GRE_IFLAGS])
  1155. flags |= nla_get_be16(data[IFLA_GRE_IFLAGS]);
  1156. if (data[IFLA_GRE_OFLAGS])
  1157. flags |= nla_get_be16(data[IFLA_GRE_OFLAGS]);
  1158. if (flags & (GRE_VERSION|GRE_ROUTING))
  1159. return -EINVAL;
  1160. return 0;
  1161. }
  1162. static int ip6gre_tap_validate(struct nlattr *tb[], struct nlattr *data[])
  1163. {
  1164. struct in6_addr daddr;
  1165. if (tb[IFLA_ADDRESS]) {
  1166. if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
  1167. return -EINVAL;
  1168. if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
  1169. return -EADDRNOTAVAIL;
  1170. }
  1171. if (!data)
  1172. goto out;
  1173. if (data[IFLA_GRE_REMOTE]) {
  1174. nla_memcpy(&daddr, data[IFLA_GRE_REMOTE], sizeof(struct in6_addr));
  1175. if (ipv6_addr_any(&daddr))
  1176. return -EINVAL;
  1177. }
  1178. out:
  1179. return ip6gre_tunnel_validate(tb, data);
  1180. }
  1181. static void ip6gre_netlink_parms(struct nlattr *data[],
  1182. struct __ip6_tnl_parm *parms)
  1183. {
  1184. memset(parms, 0, sizeof(*parms));
  1185. if (!data)
  1186. return;
  1187. if (data[IFLA_GRE_LINK])
  1188. parms->link = nla_get_u32(data[IFLA_GRE_LINK]);
  1189. if (data[IFLA_GRE_IFLAGS])
  1190. parms->i_flags = nla_get_be16(data[IFLA_GRE_IFLAGS]);
  1191. if (data[IFLA_GRE_OFLAGS])
  1192. parms->o_flags = nla_get_be16(data[IFLA_GRE_OFLAGS]);
  1193. if (data[IFLA_GRE_IKEY])
  1194. parms->i_key = nla_get_be32(data[IFLA_GRE_IKEY]);
  1195. if (data[IFLA_GRE_OKEY])
  1196. parms->o_key = nla_get_be32(data[IFLA_GRE_OKEY]);
  1197. if (data[IFLA_GRE_LOCAL])
  1198. nla_memcpy(&parms->laddr, data[IFLA_GRE_LOCAL], sizeof(struct in6_addr));
  1199. if (data[IFLA_GRE_REMOTE])
  1200. nla_memcpy(&parms->raddr, data[IFLA_GRE_REMOTE], sizeof(struct in6_addr));
  1201. if (data[IFLA_GRE_TTL])
  1202. parms->hop_limit = nla_get_u8(data[IFLA_GRE_TTL]);
  1203. if (data[IFLA_GRE_ENCAP_LIMIT])
  1204. parms->encap_limit = nla_get_u8(data[IFLA_GRE_ENCAP_LIMIT]);
  1205. if (data[IFLA_GRE_FLOWINFO])
  1206. parms->flowinfo = nla_get_u32(data[IFLA_GRE_FLOWINFO]);
  1207. if (data[IFLA_GRE_FLAGS])
  1208. parms->flags = nla_get_u32(data[IFLA_GRE_FLAGS]);
  1209. }
  1210. static int ip6gre_tap_init(struct net_device *dev)
  1211. {
  1212. struct ip6_tnl *tunnel;
  1213. int i;
  1214. tunnel = netdev_priv(dev);
  1215. tunnel->dev = dev;
  1216. tunnel->net = dev_net(dev);
  1217. strcpy(tunnel->parms.name, dev->name);
  1218. ip6gre_tnl_link_config(tunnel, 1);
  1219. dev->tstats = alloc_percpu(struct pcpu_sw_netstats);
  1220. if (!dev->tstats)
  1221. return -ENOMEM;
  1222. for_each_possible_cpu(i) {
  1223. struct pcpu_sw_netstats *ip6gre_tap_stats;
  1224. ip6gre_tap_stats = per_cpu_ptr(dev->tstats, i);
  1225. u64_stats_init(&ip6gre_tap_stats->syncp);
  1226. }
  1227. return 0;
  1228. }
  1229. static const struct net_device_ops ip6gre_tap_netdev_ops = {
  1230. .ndo_init = ip6gre_tap_init,
  1231. .ndo_uninit = ip6gre_tunnel_uninit,
  1232. .ndo_start_xmit = ip6gre_tunnel_xmit,
  1233. .ndo_set_mac_address = eth_mac_addr,
  1234. .ndo_validate_addr = eth_validate_addr,
  1235. .ndo_change_mtu = ip6gre_tunnel_change_mtu,
  1236. .ndo_get_stats64 = ip_tunnel_get_stats64,
  1237. };
  1238. static void ip6gre_tap_setup(struct net_device *dev)
  1239. {
  1240. ether_setup(dev);
  1241. dev->netdev_ops = &ip6gre_tap_netdev_ops;
  1242. dev->destructor = ip6gre_dev_free;
  1243. dev->iflink = 0;
  1244. dev->features |= NETIF_F_NETNS_LOCAL;
  1245. }
  1246. static int ip6gre_newlink(struct net *src_net, struct net_device *dev,
  1247. struct nlattr *tb[], struct nlattr *data[])
  1248. {
  1249. struct ip6_tnl *nt;
  1250. struct net *net = dev_net(dev);
  1251. struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
  1252. int err;
  1253. nt = netdev_priv(dev);
  1254. ip6gre_netlink_parms(data, &nt->parms);
  1255. if (ip6gre_tunnel_find(net, &nt->parms, dev->type))
  1256. return -EEXIST;
  1257. if (dev->type == ARPHRD_ETHER && !tb[IFLA_ADDRESS])
  1258. eth_hw_addr_random(dev);
  1259. nt->dev = dev;
  1260. nt->net = dev_net(dev);
  1261. ip6gre_tnl_link_config(nt, !tb[IFLA_MTU]);
  1262. /* Can use a lockless transmit, unless we generate output sequences */
  1263. if (!(nt->parms.o_flags & GRE_SEQ))
  1264. dev->features |= NETIF_F_LLTX;
  1265. err = register_netdevice(dev);
  1266. if (err)
  1267. goto out;
  1268. dev_hold(dev);
  1269. ip6gre_tunnel_link(ign, nt);
  1270. out:
  1271. return err;
  1272. }
  1273. static int ip6gre_changelink(struct net_device *dev, struct nlattr *tb[],
  1274. struct nlattr *data[])
  1275. {
  1276. struct ip6_tnl *t, *nt;
  1277. struct net *net = dev_net(dev);
  1278. struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
  1279. struct __ip6_tnl_parm p;
  1280. if (dev == ign->fb_tunnel_dev)
  1281. return -EINVAL;
  1282. nt = netdev_priv(dev);
  1283. ip6gre_netlink_parms(data, &p);
  1284. t = ip6gre_tunnel_locate(net, &p, 0);
  1285. if (t) {
  1286. if (t->dev != dev)
  1287. return -EEXIST;
  1288. } else {
  1289. t = nt;
  1290. ip6gre_tunnel_unlink(ign, t);
  1291. ip6gre_tnl_change(t, &p, !tb[IFLA_MTU]);
  1292. ip6gre_tunnel_link(ign, t);
  1293. netdev_state_change(dev);
  1294. }
  1295. return 0;
  1296. }
  1297. static size_t ip6gre_get_size(const struct net_device *dev)
  1298. {
  1299. return
  1300. /* IFLA_GRE_LINK */
  1301. nla_total_size(4) +
  1302. /* IFLA_GRE_IFLAGS */
  1303. nla_total_size(2) +
  1304. /* IFLA_GRE_OFLAGS */
  1305. nla_total_size(2) +
  1306. /* IFLA_GRE_IKEY */
  1307. nla_total_size(4) +
  1308. /* IFLA_GRE_OKEY */
  1309. nla_total_size(4) +
  1310. /* IFLA_GRE_LOCAL */
  1311. nla_total_size(sizeof(struct in6_addr)) +
  1312. /* IFLA_GRE_REMOTE */
  1313. nla_total_size(sizeof(struct in6_addr)) +
  1314. /* IFLA_GRE_TTL */
  1315. nla_total_size(1) +
  1316. /* IFLA_GRE_TOS */
  1317. nla_total_size(1) +
  1318. /* IFLA_GRE_ENCAP_LIMIT */
  1319. nla_total_size(1) +
  1320. /* IFLA_GRE_FLOWINFO */
  1321. nla_total_size(4) +
  1322. /* IFLA_GRE_FLAGS */
  1323. nla_total_size(4) +
  1324. 0;
  1325. }
  1326. static int ip6gre_fill_info(struct sk_buff *skb, const struct net_device *dev)
  1327. {
  1328. struct ip6_tnl *t = netdev_priv(dev);
  1329. struct __ip6_tnl_parm *p = &t->parms;
  1330. if (nla_put_u32(skb, IFLA_GRE_LINK, p->link) ||
  1331. nla_put_be16(skb, IFLA_GRE_IFLAGS, p->i_flags) ||
  1332. nla_put_be16(skb, IFLA_GRE_OFLAGS, p->o_flags) ||
  1333. nla_put_be32(skb, IFLA_GRE_IKEY, p->i_key) ||
  1334. nla_put_be32(skb, IFLA_GRE_OKEY, p->o_key) ||
  1335. nla_put(skb, IFLA_GRE_LOCAL, sizeof(struct in6_addr), &p->laddr) ||
  1336. nla_put(skb, IFLA_GRE_REMOTE, sizeof(struct in6_addr), &p->raddr) ||
  1337. nla_put_u8(skb, IFLA_GRE_TTL, p->hop_limit) ||
  1338. /*nla_put_u8(skb, IFLA_GRE_TOS, t->priority) ||*/
  1339. nla_put_u8(skb, IFLA_GRE_ENCAP_LIMIT, p->encap_limit) ||
  1340. nla_put_be32(skb, IFLA_GRE_FLOWINFO, p->flowinfo) ||
  1341. nla_put_u32(skb, IFLA_GRE_FLAGS, p->flags))
  1342. goto nla_put_failure;
  1343. return 0;
  1344. nla_put_failure:
  1345. return -EMSGSIZE;
  1346. }
  1347. static const struct nla_policy ip6gre_policy[IFLA_GRE_MAX + 1] = {
  1348. [IFLA_GRE_LINK] = { .type = NLA_U32 },
  1349. [IFLA_GRE_IFLAGS] = { .type = NLA_U16 },
  1350. [IFLA_GRE_OFLAGS] = { .type = NLA_U16 },
  1351. [IFLA_GRE_IKEY] = { .type = NLA_U32 },
  1352. [IFLA_GRE_OKEY] = { .type = NLA_U32 },
  1353. [IFLA_GRE_LOCAL] = { .len = FIELD_SIZEOF(struct ipv6hdr, saddr) },
  1354. [IFLA_GRE_REMOTE] = { .len = FIELD_SIZEOF(struct ipv6hdr, daddr) },
  1355. [IFLA_GRE_TTL] = { .type = NLA_U8 },
  1356. [IFLA_GRE_ENCAP_LIMIT] = { .type = NLA_U8 },
  1357. [IFLA_GRE_FLOWINFO] = { .type = NLA_U32 },
  1358. [IFLA_GRE_FLAGS] = { .type = NLA_U32 },
  1359. };
  1360. static struct rtnl_link_ops ip6gre_link_ops __read_mostly = {
  1361. .kind = "ip6gre",
  1362. .maxtype = IFLA_GRE_MAX,
  1363. .policy = ip6gre_policy,
  1364. .priv_size = sizeof(struct ip6_tnl),
  1365. .setup = ip6gre_tunnel_setup,
  1366. .validate = ip6gre_tunnel_validate,
  1367. .newlink = ip6gre_newlink,
  1368. .changelink = ip6gre_changelink,
  1369. .get_size = ip6gre_get_size,
  1370. .fill_info = ip6gre_fill_info,
  1371. };
  1372. static struct rtnl_link_ops ip6gre_tap_ops __read_mostly = {
  1373. .kind = "ip6gretap",
  1374. .maxtype = IFLA_GRE_MAX,
  1375. .policy = ip6gre_policy,
  1376. .priv_size = sizeof(struct ip6_tnl),
  1377. .setup = ip6gre_tap_setup,
  1378. .validate = ip6gre_tap_validate,
  1379. .newlink = ip6gre_newlink,
  1380. .changelink = ip6gre_changelink,
  1381. .get_size = ip6gre_get_size,
  1382. .fill_info = ip6gre_fill_info,
  1383. };
  1384. /*
  1385. * And now the modules code and kernel interface.
  1386. */
  1387. static int __init ip6gre_init(void)
  1388. {
  1389. int err;
  1390. pr_info("GRE over IPv6 tunneling driver\n");
  1391. err = register_pernet_device(&ip6gre_net_ops);
  1392. if (err < 0)
  1393. return err;
  1394. err = inet6_add_protocol(&ip6gre_protocol, IPPROTO_GRE);
  1395. if (err < 0) {
  1396. pr_info("%s: can't add protocol\n", __func__);
  1397. goto add_proto_failed;
  1398. }
  1399. err = rtnl_link_register(&ip6gre_link_ops);
  1400. if (err < 0)
  1401. goto rtnl_link_failed;
  1402. err = rtnl_link_register(&ip6gre_tap_ops);
  1403. if (err < 0)
  1404. goto tap_ops_failed;
  1405. out:
  1406. return err;
  1407. tap_ops_failed:
  1408. rtnl_link_unregister(&ip6gre_link_ops);
  1409. rtnl_link_failed:
  1410. inet6_del_protocol(&ip6gre_protocol, IPPROTO_GRE);
  1411. add_proto_failed:
  1412. unregister_pernet_device(&ip6gre_net_ops);
  1413. goto out;
  1414. }
  1415. static void __exit ip6gre_fini(void)
  1416. {
  1417. rtnl_link_unregister(&ip6gre_tap_ops);
  1418. rtnl_link_unregister(&ip6gre_link_ops);
  1419. inet6_del_protocol(&ip6gre_protocol, IPPROTO_GRE);
  1420. unregister_pernet_device(&ip6gre_net_ops);
  1421. }
  1422. module_init(ip6gre_init);
  1423. module_exit(ip6gre_fini);
  1424. MODULE_LICENSE("GPL");
  1425. MODULE_AUTHOR("D. Kozlov (xeb@mail.ru)");
  1426. MODULE_DESCRIPTION("GRE over IPv6 tunneling device");
  1427. MODULE_ALIAS_RTNL_LINK("ip6gre");
  1428. MODULE_ALIAS_NETDEV("ip6gre0");