ip6_gre.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705
  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 IPV6_TCLASS_MASK (IPV6_FLOWINFO_MASK & ~IPV6_FLOWLABEL_MASK)
  58. #define IPV6_TCLASS_SHIFT 20
  59. #define HASH_SIZE_SHIFT 5
  60. #define HASH_SIZE (1 << HASH_SIZE_SHIFT)
  61. static int ip6gre_net_id __read_mostly;
  62. struct ip6gre_net {
  63. struct ip6_tnl __rcu *tunnels[4][HASH_SIZE];
  64. struct net_device *fb_tunnel_dev;
  65. };
  66. static struct rtnl_link_ops ip6gre_link_ops __read_mostly;
  67. static int ip6gre_tunnel_init(struct net_device *dev);
  68. static void ip6gre_tunnel_setup(struct net_device *dev);
  69. static void ip6gre_tunnel_link(struct ip6gre_net *ign, struct ip6_tnl *t);
  70. static void ip6gre_tnl_link_config(struct ip6_tnl *t, int set_mtu);
  71. /* Tunnel hash table */
  72. /*
  73. 4 hash tables:
  74. 3: (remote,local)
  75. 2: (remote,*)
  76. 1: (*,local)
  77. 0: (*,*)
  78. We require exact key match i.e. if a key is present in packet
  79. it will match only tunnel with the same key; if it is not present,
  80. it will match only keyless tunnel.
  81. All keysless packets, if not matched configured keyless tunnels
  82. will match fallback tunnel.
  83. */
  84. #define HASH_KEY(key) (((__force u32)key^((__force u32)key>>4))&(HASH_SIZE - 1))
  85. static u32 HASH_ADDR(const struct in6_addr *addr)
  86. {
  87. u32 hash = ipv6_addr_hash(addr);
  88. return hash_32(hash, HASH_SIZE_SHIFT);
  89. }
  90. #define tunnels_r_l tunnels[3]
  91. #define tunnels_r tunnels[2]
  92. #define tunnels_l tunnels[1]
  93. #define tunnels_wc tunnels[0]
  94. /* Given src, dst and key, find appropriate for input tunnel. */
  95. static struct ip6_tnl *ip6gre_tunnel_lookup(struct net_device *dev,
  96. const struct in6_addr *remote, const struct in6_addr *local,
  97. __be32 key, __be16 gre_proto)
  98. {
  99. struct net *net = dev_net(dev);
  100. int link = dev->ifindex;
  101. unsigned int h0 = HASH_ADDR(remote);
  102. unsigned int h1 = HASH_KEY(key);
  103. struct ip6_tnl *t, *cand = NULL;
  104. struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
  105. int dev_type = (gre_proto == htons(ETH_P_TEB)) ?
  106. ARPHRD_ETHER : ARPHRD_IP6GRE;
  107. int score, cand_score = 4;
  108. for_each_ip_tunnel_rcu(t, ign->tunnels_r_l[h0 ^ h1]) {
  109. if (!ipv6_addr_equal(local, &t->parms.laddr) ||
  110. !ipv6_addr_equal(remote, &t->parms.raddr) ||
  111. key != t->parms.i_key ||
  112. !(t->dev->flags & IFF_UP))
  113. continue;
  114. if (t->dev->type != ARPHRD_IP6GRE &&
  115. t->dev->type != dev_type)
  116. continue;
  117. score = 0;
  118. if (t->parms.link != link)
  119. score |= 1;
  120. if (t->dev->type != dev_type)
  121. score |= 2;
  122. if (score == 0)
  123. return t;
  124. if (score < cand_score) {
  125. cand = t;
  126. cand_score = score;
  127. }
  128. }
  129. for_each_ip_tunnel_rcu(t, ign->tunnels_r[h0 ^ h1]) {
  130. if (!ipv6_addr_equal(remote, &t->parms.raddr) ||
  131. key != t->parms.i_key ||
  132. !(t->dev->flags & IFF_UP))
  133. continue;
  134. if (t->dev->type != ARPHRD_IP6GRE &&
  135. t->dev->type != dev_type)
  136. continue;
  137. score = 0;
  138. if (t->parms.link != link)
  139. score |= 1;
  140. if (t->dev->type != dev_type)
  141. score |= 2;
  142. if (score == 0)
  143. return t;
  144. if (score < cand_score) {
  145. cand = t;
  146. cand_score = score;
  147. }
  148. }
  149. for_each_ip_tunnel_rcu(t, ign->tunnels_l[h1]) {
  150. if ((!ipv6_addr_equal(local, &t->parms.laddr) &&
  151. (!ipv6_addr_equal(local, &t->parms.raddr) ||
  152. !ipv6_addr_is_multicast(local))) ||
  153. key != t->parms.i_key ||
  154. !(t->dev->flags & IFF_UP))
  155. continue;
  156. if (t->dev->type != ARPHRD_IP6GRE &&
  157. t->dev->type != dev_type)
  158. continue;
  159. score = 0;
  160. if (t->parms.link != link)
  161. score |= 1;
  162. if (t->dev->type != dev_type)
  163. score |= 2;
  164. if (score == 0)
  165. return t;
  166. if (score < cand_score) {
  167. cand = t;
  168. cand_score = score;
  169. }
  170. }
  171. for_each_ip_tunnel_rcu(t, ign->tunnels_wc[h1]) {
  172. if (t->parms.i_key != key ||
  173. !(t->dev->flags & IFF_UP))
  174. continue;
  175. if (t->dev->type != ARPHRD_IP6GRE &&
  176. t->dev->type != dev_type)
  177. continue;
  178. score = 0;
  179. if (t->parms.link != link)
  180. score |= 1;
  181. if (t->dev->type != dev_type)
  182. score |= 2;
  183. if (score == 0)
  184. return t;
  185. if (score < cand_score) {
  186. cand = t;
  187. cand_score = score;
  188. }
  189. }
  190. if (cand != NULL)
  191. return cand;
  192. dev = ign->fb_tunnel_dev;
  193. if (dev->flags & IFF_UP)
  194. return netdev_priv(dev);
  195. return NULL;
  196. }
  197. static struct ip6_tnl __rcu **__ip6gre_bucket(struct ip6gre_net *ign,
  198. const struct __ip6_tnl_parm *p)
  199. {
  200. const struct in6_addr *remote = &p->raddr;
  201. const struct in6_addr *local = &p->laddr;
  202. unsigned int h = HASH_KEY(p->i_key);
  203. int prio = 0;
  204. if (!ipv6_addr_any(local))
  205. prio |= 1;
  206. if (!ipv6_addr_any(remote) && !ipv6_addr_is_multicast(remote)) {
  207. prio |= 2;
  208. h ^= HASH_ADDR(remote);
  209. }
  210. return &ign->tunnels[prio][h];
  211. }
  212. static inline struct ip6_tnl __rcu **ip6gre_bucket(struct ip6gre_net *ign,
  213. const struct ip6_tnl *t)
  214. {
  215. return __ip6gre_bucket(ign, &t->parms);
  216. }
  217. static void ip6gre_tunnel_link(struct ip6gre_net *ign, struct ip6_tnl *t)
  218. {
  219. struct ip6_tnl __rcu **tp = ip6gre_bucket(ign, t);
  220. rcu_assign_pointer(t->next, rtnl_dereference(*tp));
  221. rcu_assign_pointer(*tp, t);
  222. }
  223. static void ip6gre_tunnel_unlink(struct ip6gre_net *ign, struct ip6_tnl *t)
  224. {
  225. struct ip6_tnl __rcu **tp;
  226. struct ip6_tnl *iter;
  227. for (tp = ip6gre_bucket(ign, t);
  228. (iter = rtnl_dereference(*tp)) != NULL;
  229. tp = &iter->next) {
  230. if (t == iter) {
  231. rcu_assign_pointer(*tp, t->next);
  232. break;
  233. }
  234. }
  235. }
  236. static struct ip6_tnl *ip6gre_tunnel_find(struct net *net,
  237. const struct __ip6_tnl_parm *parms,
  238. int type)
  239. {
  240. const struct in6_addr *remote = &parms->raddr;
  241. const struct in6_addr *local = &parms->laddr;
  242. __be32 key = parms->i_key;
  243. int link = parms->link;
  244. struct ip6_tnl *t;
  245. struct ip6_tnl __rcu **tp;
  246. struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
  247. for (tp = __ip6gre_bucket(ign, parms);
  248. (t = rtnl_dereference(*tp)) != NULL;
  249. tp = &t->next)
  250. if (ipv6_addr_equal(local, &t->parms.laddr) &&
  251. ipv6_addr_equal(remote, &t->parms.raddr) &&
  252. key == t->parms.i_key &&
  253. link == t->parms.link &&
  254. type == t->dev->type)
  255. break;
  256. return t;
  257. }
  258. static struct ip6_tnl *ip6gre_tunnel_locate(struct net *net,
  259. const struct __ip6_tnl_parm *parms, int create)
  260. {
  261. struct ip6_tnl *t, *nt;
  262. struct net_device *dev;
  263. char name[IFNAMSIZ];
  264. struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
  265. t = ip6gre_tunnel_find(net, parms, ARPHRD_IP6GRE);
  266. if (t || !create)
  267. return t;
  268. if (parms->name[0])
  269. strlcpy(name, parms->name, IFNAMSIZ);
  270. else
  271. strcpy(name, "ip6gre%d");
  272. dev = alloc_netdev(sizeof(*t), name, ip6gre_tunnel_setup);
  273. if (!dev)
  274. return NULL;
  275. dev_net_set(dev, net);
  276. nt = netdev_priv(dev);
  277. nt->parms = *parms;
  278. dev->rtnl_link_ops = &ip6gre_link_ops;
  279. nt->dev = dev;
  280. nt->net = dev_net(dev);
  281. ip6gre_tnl_link_config(nt, 1);
  282. if (register_netdevice(dev) < 0)
  283. goto failed_free;
  284. /* Can use a lockless transmit, unless we generate output sequences */
  285. if (!(nt->parms.o_flags & GRE_SEQ))
  286. dev->features |= NETIF_F_LLTX;
  287. dev_hold(dev);
  288. ip6gre_tunnel_link(ign, nt);
  289. return nt;
  290. failed_free:
  291. free_netdev(dev);
  292. return NULL;
  293. }
  294. static void ip6gre_tunnel_uninit(struct net_device *dev)
  295. {
  296. struct net *net = dev_net(dev);
  297. struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
  298. ip6gre_tunnel_unlink(ign, netdev_priv(dev));
  299. dev_put(dev);
  300. }
  301. static void ip6gre_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
  302. u8 type, u8 code, int offset, __be32 info)
  303. {
  304. const struct ipv6hdr *ipv6h = (const struct ipv6hdr *)skb->data;
  305. __be16 *p = (__be16 *)(skb->data + offset);
  306. int grehlen = offset + 4;
  307. struct ip6_tnl *t;
  308. __be16 flags;
  309. flags = p[0];
  310. if (flags&(GRE_CSUM|GRE_KEY|GRE_SEQ|GRE_ROUTING|GRE_VERSION)) {
  311. if (flags&(GRE_VERSION|GRE_ROUTING))
  312. return;
  313. if (flags&GRE_KEY) {
  314. grehlen += 4;
  315. if (flags&GRE_CSUM)
  316. grehlen += 4;
  317. }
  318. }
  319. /* If only 8 bytes returned, keyed message will be dropped here */
  320. if (!pskb_may_pull(skb, grehlen))
  321. return;
  322. ipv6h = (const struct ipv6hdr *)skb->data;
  323. p = (__be16 *)(skb->data + offset);
  324. t = ip6gre_tunnel_lookup(skb->dev, &ipv6h->daddr, &ipv6h->saddr,
  325. flags & GRE_KEY ?
  326. *(((__be32 *)p) + (grehlen / 4) - 1) : 0,
  327. p[1]);
  328. if (t == NULL)
  329. return;
  330. switch (type) {
  331. __u32 teli;
  332. struct ipv6_tlv_tnl_enc_lim *tel;
  333. __u32 mtu;
  334. case ICMPV6_DEST_UNREACH:
  335. net_warn_ratelimited("%s: Path to destination invalid or inactive!\n",
  336. t->parms.name);
  337. break;
  338. case ICMPV6_TIME_EXCEED:
  339. if (code == ICMPV6_EXC_HOPLIMIT) {
  340. net_warn_ratelimited("%s: Too small hop limit or routing loop in tunnel!\n",
  341. t->parms.name);
  342. }
  343. break;
  344. case ICMPV6_PARAMPROB:
  345. teli = 0;
  346. if (code == ICMPV6_HDR_FIELD)
  347. teli = ip6_tnl_parse_tlv_enc_lim(skb, skb->data);
  348. if (teli && teli == info - 2) {
  349. tel = (struct ipv6_tlv_tnl_enc_lim *) &skb->data[teli];
  350. if (tel->encap_limit == 0) {
  351. net_warn_ratelimited("%s: Too small encapsulation limit or routing loop in tunnel!\n",
  352. t->parms.name);
  353. }
  354. } else {
  355. net_warn_ratelimited("%s: Recipient unable to parse tunneled packet!\n",
  356. t->parms.name);
  357. }
  358. break;
  359. case ICMPV6_PKT_TOOBIG:
  360. mtu = info - offset;
  361. if (mtu < IPV6_MIN_MTU)
  362. mtu = IPV6_MIN_MTU;
  363. t->dev->mtu = mtu;
  364. break;
  365. }
  366. if (time_before(jiffies, t->err_time + IP6TUNNEL_ERR_TIMEO))
  367. t->err_count++;
  368. else
  369. t->err_count = 1;
  370. t->err_time = jiffies;
  371. }
  372. static int ip6gre_rcv(struct sk_buff *skb)
  373. {
  374. const struct ipv6hdr *ipv6h;
  375. u8 *h;
  376. __be16 flags;
  377. __sum16 csum = 0;
  378. __be32 key = 0;
  379. u32 seqno = 0;
  380. struct ip6_tnl *tunnel;
  381. int offset = 4;
  382. __be16 gre_proto;
  383. int err;
  384. if (!pskb_may_pull(skb, sizeof(struct in6_addr)))
  385. goto drop;
  386. ipv6h = ipv6_hdr(skb);
  387. h = skb->data;
  388. flags = *(__be16 *)h;
  389. if (flags&(GRE_CSUM|GRE_KEY|GRE_ROUTING|GRE_SEQ|GRE_VERSION)) {
  390. /* - Version must be 0.
  391. - We do not support routing headers.
  392. */
  393. if (flags&(GRE_VERSION|GRE_ROUTING))
  394. goto drop;
  395. if (flags&GRE_CSUM) {
  396. switch (skb->ip_summed) {
  397. case CHECKSUM_COMPLETE:
  398. csum = csum_fold(skb->csum);
  399. if (!csum)
  400. break;
  401. /* fall through */
  402. case CHECKSUM_NONE:
  403. skb->csum = 0;
  404. csum = __skb_checksum_complete(skb);
  405. skb->ip_summed = CHECKSUM_COMPLETE;
  406. }
  407. offset += 4;
  408. }
  409. if (flags&GRE_KEY) {
  410. key = *(__be32 *)(h + offset);
  411. offset += 4;
  412. }
  413. if (flags&GRE_SEQ) {
  414. seqno = ntohl(*(__be32 *)(h + offset));
  415. offset += 4;
  416. }
  417. }
  418. gre_proto = *(__be16 *)(h + 2);
  419. tunnel = ip6gre_tunnel_lookup(skb->dev,
  420. &ipv6h->saddr, &ipv6h->daddr, key,
  421. gre_proto);
  422. if (tunnel) {
  423. struct pcpu_tstats *tstats;
  424. if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb))
  425. goto drop;
  426. if (!ip6_tnl_rcv_ctl(tunnel, &ipv6h->daddr, &ipv6h->saddr)) {
  427. tunnel->dev->stats.rx_dropped++;
  428. goto drop;
  429. }
  430. secpath_reset(skb);
  431. skb->protocol = gre_proto;
  432. /* WCCP version 1 and 2 protocol decoding.
  433. * - Change protocol to IP
  434. * - When dealing with WCCPv2, Skip extra 4 bytes in GRE header
  435. */
  436. if (flags == 0 && gre_proto == htons(ETH_P_WCCP)) {
  437. skb->protocol = htons(ETH_P_IP);
  438. if ((*(h + offset) & 0xF0) != 0x40)
  439. offset += 4;
  440. }
  441. skb->mac_header = skb->network_header;
  442. __pskb_pull(skb, offset);
  443. skb_postpull_rcsum(skb, skb_transport_header(skb), offset);
  444. skb->pkt_type = PACKET_HOST;
  445. if (((flags&GRE_CSUM) && csum) ||
  446. (!(flags&GRE_CSUM) && tunnel->parms.i_flags&GRE_CSUM)) {
  447. tunnel->dev->stats.rx_crc_errors++;
  448. tunnel->dev->stats.rx_errors++;
  449. goto drop;
  450. }
  451. if (tunnel->parms.i_flags&GRE_SEQ) {
  452. if (!(flags&GRE_SEQ) ||
  453. (tunnel->i_seqno &&
  454. (s32)(seqno - tunnel->i_seqno) < 0)) {
  455. tunnel->dev->stats.rx_fifo_errors++;
  456. tunnel->dev->stats.rx_errors++;
  457. goto drop;
  458. }
  459. tunnel->i_seqno = seqno + 1;
  460. }
  461. /* Warning: All skb pointers will be invalidated! */
  462. if (tunnel->dev->type == ARPHRD_ETHER) {
  463. if (!pskb_may_pull(skb, ETH_HLEN)) {
  464. tunnel->dev->stats.rx_length_errors++;
  465. tunnel->dev->stats.rx_errors++;
  466. goto drop;
  467. }
  468. ipv6h = ipv6_hdr(skb);
  469. skb->protocol = eth_type_trans(skb, tunnel->dev);
  470. skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN);
  471. }
  472. __skb_tunnel_rx(skb, tunnel->dev);
  473. skb_reset_network_header(skb);
  474. err = IP6_ECN_decapsulate(ipv6h, skb);
  475. if (unlikely(err)) {
  476. if (log_ecn_error)
  477. net_info_ratelimited("non-ECT from %pI6 with dsfield=%#x\n",
  478. &ipv6h->saddr,
  479. ipv6_get_dsfield(ipv6h));
  480. if (err > 1) {
  481. ++tunnel->dev->stats.rx_frame_errors;
  482. ++tunnel->dev->stats.rx_errors;
  483. goto drop;
  484. }
  485. }
  486. tstats = this_cpu_ptr(tunnel->dev->tstats);
  487. u64_stats_update_begin(&tstats->syncp);
  488. tstats->rx_packets++;
  489. tstats->rx_bytes += skb->len;
  490. u64_stats_update_end(&tstats->syncp);
  491. netif_rx(skb);
  492. return 0;
  493. }
  494. icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0);
  495. drop:
  496. kfree_skb(skb);
  497. return 0;
  498. }
  499. struct ipv6_tel_txoption {
  500. struct ipv6_txoptions ops;
  501. __u8 dst_opt[8];
  502. };
  503. static void init_tel_txopt(struct ipv6_tel_txoption *opt, __u8 encap_limit)
  504. {
  505. memset(opt, 0, sizeof(struct ipv6_tel_txoption));
  506. opt->dst_opt[2] = IPV6_TLV_TNL_ENCAP_LIMIT;
  507. opt->dst_opt[3] = 1;
  508. opt->dst_opt[4] = encap_limit;
  509. opt->dst_opt[5] = IPV6_TLV_PADN;
  510. opt->dst_opt[6] = 1;
  511. opt->ops.dst0opt = (struct ipv6_opt_hdr *) opt->dst_opt;
  512. opt->ops.opt_nflen = 8;
  513. }
  514. static netdev_tx_t ip6gre_xmit2(struct sk_buff *skb,
  515. struct net_device *dev,
  516. __u8 dsfield,
  517. struct flowi6 *fl6,
  518. int encap_limit,
  519. __u32 *pmtu)
  520. {
  521. struct net *net = dev_net(dev);
  522. struct ip6_tnl *tunnel = netdev_priv(dev);
  523. struct net_device *tdev; /* Device to other host */
  524. struct ipv6hdr *ipv6h; /* Our new IP header */
  525. unsigned int max_headroom; /* The extra header space needed */
  526. int gre_hlen;
  527. struct ipv6_tel_txoption opt;
  528. int mtu;
  529. struct dst_entry *dst = NULL, *ndst = NULL;
  530. struct net_device_stats *stats = &tunnel->dev->stats;
  531. int err = -1;
  532. u8 proto;
  533. struct sk_buff *new_skb;
  534. if (dev->type == ARPHRD_ETHER)
  535. IPCB(skb)->flags = 0;
  536. if (dev->header_ops && dev->type == ARPHRD_IP6GRE) {
  537. gre_hlen = 0;
  538. ipv6h = (struct ipv6hdr *)skb->data;
  539. fl6->daddr = ipv6h->daddr;
  540. } else {
  541. gre_hlen = tunnel->hlen;
  542. fl6->daddr = tunnel->parms.raddr;
  543. }
  544. if (!fl6->flowi6_mark)
  545. dst = ip6_tnl_dst_check(tunnel);
  546. if (!dst) {
  547. ndst = ip6_route_output(net, NULL, fl6);
  548. if (ndst->error)
  549. goto tx_err_link_failure;
  550. ndst = xfrm_lookup(net, ndst, flowi6_to_flowi(fl6), NULL, 0);
  551. if (IS_ERR(ndst)) {
  552. err = PTR_ERR(ndst);
  553. ndst = NULL;
  554. goto tx_err_link_failure;
  555. }
  556. dst = ndst;
  557. }
  558. tdev = dst->dev;
  559. if (tdev == dev) {
  560. stats->collisions++;
  561. net_warn_ratelimited("%s: Local routing loop detected!\n",
  562. tunnel->parms.name);
  563. goto tx_err_dst_release;
  564. }
  565. mtu = dst_mtu(dst) - sizeof(*ipv6h);
  566. if (encap_limit >= 0) {
  567. max_headroom += 8;
  568. mtu -= 8;
  569. }
  570. if (mtu < IPV6_MIN_MTU)
  571. mtu = IPV6_MIN_MTU;
  572. if (skb_dst(skb))
  573. skb_dst(skb)->ops->update_pmtu(skb_dst(skb), NULL, skb, mtu);
  574. if (skb->len > mtu) {
  575. *pmtu = mtu;
  576. err = -EMSGSIZE;
  577. goto tx_err_dst_release;
  578. }
  579. if (tunnel->err_count > 0) {
  580. if (time_before(jiffies,
  581. tunnel->err_time + IP6TUNNEL_ERR_TIMEO)) {
  582. tunnel->err_count--;
  583. dst_link_failure(skb);
  584. } else
  585. tunnel->err_count = 0;
  586. }
  587. skb_scrub_packet(skb, !net_eq(tunnel->net, dev_net(dev)));
  588. max_headroom = LL_RESERVED_SPACE(tdev) + gre_hlen + dst->header_len;
  589. if (skb_headroom(skb) < max_headroom || skb_shared(skb) ||
  590. (skb_cloned(skb) && !skb_clone_writable(skb, 0))) {
  591. new_skb = skb_realloc_headroom(skb, max_headroom);
  592. if (max_headroom > dev->needed_headroom)
  593. dev->needed_headroom = max_headroom;
  594. if (!new_skb)
  595. goto tx_err_dst_release;
  596. if (skb->sk)
  597. skb_set_owner_w(new_skb, skb->sk);
  598. consume_skb(skb);
  599. skb = new_skb;
  600. }
  601. if (fl6->flowi6_mark) {
  602. skb_dst_set(skb, dst);
  603. ndst = NULL;
  604. } else {
  605. skb_dst_set_noref(skb, dst);
  606. }
  607. proto = NEXTHDR_GRE;
  608. if (encap_limit >= 0) {
  609. init_tel_txopt(&opt, encap_limit);
  610. ipv6_push_nfrag_opts(skb, &opt.ops, &proto, NULL);
  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 |= (*(__be32 *) ipv6h & IPV6_FLOWLABEL_MASK);
  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. if (p->flags & IP6_TNL_F_CAP_XMIT) {
  822. int strict = (ipv6_addr_type(&p->raddr) &
  823. (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL));
  824. struct rt6_info *rt = rt6_lookup(dev_net(dev),
  825. &p->raddr, &p->laddr,
  826. p->link, strict);
  827. if (rt == NULL)
  828. return;
  829. if (rt->dst.dev) {
  830. dev->hard_header_len = rt->dst.dev->hard_header_len + addend;
  831. if (set_mtu) {
  832. dev->mtu = rt->dst.dev->mtu - addend;
  833. if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
  834. dev->mtu -= 8;
  835. if (dev->mtu < IPV6_MIN_MTU)
  836. dev->mtu = IPV6_MIN_MTU;
  837. }
  838. }
  839. ip6_rt_put(rt);
  840. }
  841. t->hlen = addend;
  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. struct ip6_tnl *tunnel = netdev_priv(dev);
  993. if (new_mtu < 68 ||
  994. new_mtu > 0xFFF8 - dev->hard_header_len - tunnel->hlen)
  995. return -EINVAL;
  996. dev->mtu = new_mtu;
  997. return 0;
  998. }
  999. static int ip6gre_header(struct sk_buff *skb, struct net_device *dev,
  1000. unsigned short type,
  1001. const void *daddr, const void *saddr, unsigned int len)
  1002. {
  1003. struct ip6_tnl *t = netdev_priv(dev);
  1004. struct ipv6hdr *ipv6h = (struct ipv6hdr *)skb_push(skb, t->hlen);
  1005. __be16 *p = (__be16 *)(ipv6h+1);
  1006. ip6_flow_hdr(ipv6h, 0, t->fl.u.ip6.flowlabel);
  1007. ipv6h->hop_limit = t->parms.hop_limit;
  1008. ipv6h->nexthdr = NEXTHDR_GRE;
  1009. ipv6h->saddr = t->parms.laddr;
  1010. ipv6h->daddr = t->parms.raddr;
  1011. p[0] = t->parms.o_flags;
  1012. p[1] = htons(type);
  1013. /*
  1014. * Set the source hardware address.
  1015. */
  1016. if (saddr)
  1017. memcpy(&ipv6h->saddr, saddr, sizeof(struct in6_addr));
  1018. if (daddr)
  1019. memcpy(&ipv6h->daddr, daddr, sizeof(struct in6_addr));
  1020. if (!ipv6_addr_any(&ipv6h->daddr))
  1021. return t->hlen;
  1022. return -t->hlen;
  1023. }
  1024. static const struct header_ops ip6gre_header_ops = {
  1025. .create = ip6gre_header,
  1026. };
  1027. static const struct net_device_ops ip6gre_netdev_ops = {
  1028. .ndo_init = ip6gre_tunnel_init,
  1029. .ndo_uninit = ip6gre_tunnel_uninit,
  1030. .ndo_start_xmit = ip6gre_tunnel_xmit,
  1031. .ndo_do_ioctl = ip6gre_tunnel_ioctl,
  1032. .ndo_change_mtu = ip6gre_tunnel_change_mtu,
  1033. .ndo_get_stats64 = ip_tunnel_get_stats64,
  1034. };
  1035. static void ip6gre_dev_free(struct net_device *dev)
  1036. {
  1037. free_percpu(dev->tstats);
  1038. free_netdev(dev);
  1039. }
  1040. static void ip6gre_tunnel_setup(struct net_device *dev)
  1041. {
  1042. struct ip6_tnl *t;
  1043. dev->netdev_ops = &ip6gre_netdev_ops;
  1044. dev->destructor = ip6gre_dev_free;
  1045. dev->type = ARPHRD_IP6GRE;
  1046. dev->hard_header_len = LL_MAX_HEADER + sizeof(struct ipv6hdr) + 4;
  1047. dev->mtu = ETH_DATA_LEN - sizeof(struct ipv6hdr) - 4;
  1048. t = netdev_priv(dev);
  1049. if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
  1050. dev->mtu -= 8;
  1051. dev->flags |= IFF_NOARP;
  1052. dev->iflink = 0;
  1053. dev->addr_len = sizeof(struct in6_addr);
  1054. dev->features |= NETIF_F_NETNS_LOCAL;
  1055. dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
  1056. }
  1057. static int ip6gre_tunnel_init(struct net_device *dev)
  1058. {
  1059. struct ip6_tnl *tunnel;
  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_tstats);
  1069. if (!dev->tstats)
  1070. return -ENOMEM;
  1071. return 0;
  1072. }
  1073. static void ip6gre_fb_tunnel_init(struct net_device *dev)
  1074. {
  1075. struct ip6_tnl *tunnel = netdev_priv(dev);
  1076. tunnel->dev = dev;
  1077. tunnel->net = dev_net(dev);
  1078. strcpy(tunnel->parms.name, dev->name);
  1079. tunnel->hlen = sizeof(struct ipv6hdr) + 4;
  1080. dev_hold(dev);
  1081. }
  1082. static struct inet6_protocol ip6gre_protocol __read_mostly = {
  1083. .handler = ip6gre_rcv,
  1084. .err_handler = ip6gre_err,
  1085. .flags = INET6_PROTO_NOPOLICY|INET6_PROTO_FINAL,
  1086. };
  1087. static void ip6gre_destroy_tunnels(struct ip6gre_net *ign,
  1088. struct list_head *head)
  1089. {
  1090. int prio;
  1091. for (prio = 0; prio < 4; prio++) {
  1092. int h;
  1093. for (h = 0; h < HASH_SIZE; h++) {
  1094. struct ip6_tnl *t;
  1095. t = rtnl_dereference(ign->tunnels[prio][h]);
  1096. while (t != NULL) {
  1097. unregister_netdevice_queue(t->dev, head);
  1098. t = rtnl_dereference(t->next);
  1099. }
  1100. }
  1101. }
  1102. }
  1103. static int __net_init ip6gre_init_net(struct net *net)
  1104. {
  1105. struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
  1106. int err;
  1107. ign->fb_tunnel_dev = alloc_netdev(sizeof(struct ip6_tnl), "ip6gre0",
  1108. ip6gre_tunnel_setup);
  1109. if (!ign->fb_tunnel_dev) {
  1110. err = -ENOMEM;
  1111. goto err_alloc_dev;
  1112. }
  1113. dev_net_set(ign->fb_tunnel_dev, net);
  1114. ip6gre_fb_tunnel_init(ign->fb_tunnel_dev);
  1115. ign->fb_tunnel_dev->rtnl_link_ops = &ip6gre_link_ops;
  1116. err = register_netdev(ign->fb_tunnel_dev);
  1117. if (err)
  1118. goto err_reg_dev;
  1119. rcu_assign_pointer(ign->tunnels_wc[0],
  1120. netdev_priv(ign->fb_tunnel_dev));
  1121. return 0;
  1122. err_reg_dev:
  1123. ip6gre_dev_free(ign->fb_tunnel_dev);
  1124. err_alloc_dev:
  1125. return err;
  1126. }
  1127. static void __net_exit ip6gre_exit_net(struct net *net)
  1128. {
  1129. struct ip6gre_net *ign;
  1130. LIST_HEAD(list);
  1131. ign = net_generic(net, ip6gre_net_id);
  1132. rtnl_lock();
  1133. ip6gre_destroy_tunnels(ign, &list);
  1134. unregister_netdevice_many(&list);
  1135. rtnl_unlock();
  1136. }
  1137. static struct pernet_operations ip6gre_net_ops = {
  1138. .init = ip6gre_init_net,
  1139. .exit = ip6gre_exit_net,
  1140. .id = &ip6gre_net_id,
  1141. .size = sizeof(struct ip6gre_net),
  1142. };
  1143. static int ip6gre_tunnel_validate(struct nlattr *tb[], struct nlattr *data[])
  1144. {
  1145. __be16 flags;
  1146. if (!data)
  1147. return 0;
  1148. flags = 0;
  1149. if (data[IFLA_GRE_IFLAGS])
  1150. flags |= nla_get_be16(data[IFLA_GRE_IFLAGS]);
  1151. if (data[IFLA_GRE_OFLAGS])
  1152. flags |= nla_get_be16(data[IFLA_GRE_OFLAGS]);
  1153. if (flags & (GRE_VERSION|GRE_ROUTING))
  1154. return -EINVAL;
  1155. return 0;
  1156. }
  1157. static int ip6gre_tap_validate(struct nlattr *tb[], struct nlattr *data[])
  1158. {
  1159. struct in6_addr daddr;
  1160. if (tb[IFLA_ADDRESS]) {
  1161. if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
  1162. return -EINVAL;
  1163. if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
  1164. return -EADDRNOTAVAIL;
  1165. }
  1166. if (!data)
  1167. goto out;
  1168. if (data[IFLA_GRE_REMOTE]) {
  1169. nla_memcpy(&daddr, data[IFLA_GRE_REMOTE], sizeof(struct in6_addr));
  1170. if (ipv6_addr_any(&daddr))
  1171. return -EINVAL;
  1172. }
  1173. out:
  1174. return ip6gre_tunnel_validate(tb, data);
  1175. }
  1176. static void ip6gre_netlink_parms(struct nlattr *data[],
  1177. struct __ip6_tnl_parm *parms)
  1178. {
  1179. memset(parms, 0, sizeof(*parms));
  1180. if (!data)
  1181. return;
  1182. if (data[IFLA_GRE_LINK])
  1183. parms->link = nla_get_u32(data[IFLA_GRE_LINK]);
  1184. if (data[IFLA_GRE_IFLAGS])
  1185. parms->i_flags = nla_get_be16(data[IFLA_GRE_IFLAGS]);
  1186. if (data[IFLA_GRE_OFLAGS])
  1187. parms->o_flags = nla_get_be16(data[IFLA_GRE_OFLAGS]);
  1188. if (data[IFLA_GRE_IKEY])
  1189. parms->i_key = nla_get_be32(data[IFLA_GRE_IKEY]);
  1190. if (data[IFLA_GRE_OKEY])
  1191. parms->o_key = nla_get_be32(data[IFLA_GRE_OKEY]);
  1192. if (data[IFLA_GRE_LOCAL])
  1193. nla_memcpy(&parms->laddr, data[IFLA_GRE_LOCAL], sizeof(struct in6_addr));
  1194. if (data[IFLA_GRE_REMOTE])
  1195. nla_memcpy(&parms->raddr, data[IFLA_GRE_REMOTE], sizeof(struct in6_addr));
  1196. if (data[IFLA_GRE_TTL])
  1197. parms->hop_limit = nla_get_u8(data[IFLA_GRE_TTL]);
  1198. if (data[IFLA_GRE_ENCAP_LIMIT])
  1199. parms->encap_limit = nla_get_u8(data[IFLA_GRE_ENCAP_LIMIT]);
  1200. if (data[IFLA_GRE_FLOWINFO])
  1201. parms->flowinfo = nla_get_u32(data[IFLA_GRE_FLOWINFO]);
  1202. if (data[IFLA_GRE_FLAGS])
  1203. parms->flags = nla_get_u32(data[IFLA_GRE_FLAGS]);
  1204. }
  1205. static int ip6gre_tap_init(struct net_device *dev)
  1206. {
  1207. struct ip6_tnl *tunnel;
  1208. tunnel = netdev_priv(dev);
  1209. tunnel->dev = dev;
  1210. tunnel->net = dev_net(dev);
  1211. strcpy(tunnel->parms.name, dev->name);
  1212. ip6gre_tnl_link_config(tunnel, 1);
  1213. dev->tstats = alloc_percpu(struct pcpu_tstats);
  1214. if (!dev->tstats)
  1215. return -ENOMEM;
  1216. return 0;
  1217. }
  1218. static const struct net_device_ops ip6gre_tap_netdev_ops = {
  1219. .ndo_init = ip6gre_tap_init,
  1220. .ndo_uninit = ip6gre_tunnel_uninit,
  1221. .ndo_start_xmit = ip6gre_tunnel_xmit,
  1222. .ndo_set_mac_address = eth_mac_addr,
  1223. .ndo_validate_addr = eth_validate_addr,
  1224. .ndo_change_mtu = ip6gre_tunnel_change_mtu,
  1225. .ndo_get_stats64 = ip_tunnel_get_stats64,
  1226. };
  1227. static void ip6gre_tap_setup(struct net_device *dev)
  1228. {
  1229. ether_setup(dev);
  1230. dev->netdev_ops = &ip6gre_tap_netdev_ops;
  1231. dev->destructor = ip6gre_dev_free;
  1232. dev->iflink = 0;
  1233. dev->features |= NETIF_F_NETNS_LOCAL;
  1234. }
  1235. static int ip6gre_newlink(struct net *src_net, struct net_device *dev,
  1236. struct nlattr *tb[], struct nlattr *data[])
  1237. {
  1238. struct ip6_tnl *nt;
  1239. struct net *net = dev_net(dev);
  1240. struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
  1241. int err;
  1242. nt = netdev_priv(dev);
  1243. ip6gre_netlink_parms(data, &nt->parms);
  1244. if (ip6gre_tunnel_find(net, &nt->parms, dev->type))
  1245. return -EEXIST;
  1246. if (dev->type == ARPHRD_ETHER && !tb[IFLA_ADDRESS])
  1247. eth_hw_addr_random(dev);
  1248. nt->dev = dev;
  1249. nt->net = dev_net(dev);
  1250. ip6gre_tnl_link_config(nt, !tb[IFLA_MTU]);
  1251. /* Can use a lockless transmit, unless we generate output sequences */
  1252. if (!(nt->parms.o_flags & GRE_SEQ))
  1253. dev->features |= NETIF_F_LLTX;
  1254. err = register_netdevice(dev);
  1255. if (err)
  1256. goto out;
  1257. dev_hold(dev);
  1258. ip6gre_tunnel_link(ign, nt);
  1259. out:
  1260. return err;
  1261. }
  1262. static int ip6gre_changelink(struct net_device *dev, struct nlattr *tb[],
  1263. struct nlattr *data[])
  1264. {
  1265. struct ip6_tnl *t, *nt;
  1266. struct net *net = dev_net(dev);
  1267. struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
  1268. struct __ip6_tnl_parm p;
  1269. if (dev == ign->fb_tunnel_dev)
  1270. return -EINVAL;
  1271. nt = netdev_priv(dev);
  1272. ip6gre_netlink_parms(data, &p);
  1273. t = ip6gre_tunnel_locate(net, &p, 0);
  1274. if (t) {
  1275. if (t->dev != dev)
  1276. return -EEXIST;
  1277. } else {
  1278. t = nt;
  1279. ip6gre_tunnel_unlink(ign, t);
  1280. ip6gre_tnl_change(t, &p, !tb[IFLA_MTU]);
  1281. ip6gre_tunnel_link(ign, t);
  1282. netdev_state_change(dev);
  1283. }
  1284. return 0;
  1285. }
  1286. static size_t ip6gre_get_size(const struct net_device *dev)
  1287. {
  1288. return
  1289. /* IFLA_GRE_LINK */
  1290. nla_total_size(4) +
  1291. /* IFLA_GRE_IFLAGS */
  1292. nla_total_size(2) +
  1293. /* IFLA_GRE_OFLAGS */
  1294. nla_total_size(2) +
  1295. /* IFLA_GRE_IKEY */
  1296. nla_total_size(4) +
  1297. /* IFLA_GRE_OKEY */
  1298. nla_total_size(4) +
  1299. /* IFLA_GRE_LOCAL */
  1300. nla_total_size(sizeof(struct in6_addr)) +
  1301. /* IFLA_GRE_REMOTE */
  1302. nla_total_size(sizeof(struct in6_addr)) +
  1303. /* IFLA_GRE_TTL */
  1304. nla_total_size(1) +
  1305. /* IFLA_GRE_TOS */
  1306. nla_total_size(1) +
  1307. /* IFLA_GRE_ENCAP_LIMIT */
  1308. nla_total_size(1) +
  1309. /* IFLA_GRE_FLOWINFO */
  1310. nla_total_size(4) +
  1311. /* IFLA_GRE_FLAGS */
  1312. nla_total_size(4) +
  1313. 0;
  1314. }
  1315. static int ip6gre_fill_info(struct sk_buff *skb, const struct net_device *dev)
  1316. {
  1317. struct ip6_tnl *t = netdev_priv(dev);
  1318. struct __ip6_tnl_parm *p = &t->parms;
  1319. if (nla_put_u32(skb, IFLA_GRE_LINK, p->link) ||
  1320. nla_put_be16(skb, IFLA_GRE_IFLAGS, p->i_flags) ||
  1321. nla_put_be16(skb, IFLA_GRE_OFLAGS, p->o_flags) ||
  1322. nla_put_be32(skb, IFLA_GRE_IKEY, p->i_key) ||
  1323. nla_put_be32(skb, IFLA_GRE_OKEY, p->o_key) ||
  1324. nla_put(skb, IFLA_GRE_LOCAL, sizeof(struct in6_addr), &p->laddr) ||
  1325. nla_put(skb, IFLA_GRE_REMOTE, sizeof(struct in6_addr), &p->raddr) ||
  1326. nla_put_u8(skb, IFLA_GRE_TTL, p->hop_limit) ||
  1327. /*nla_put_u8(skb, IFLA_GRE_TOS, t->priority) ||*/
  1328. nla_put_u8(skb, IFLA_GRE_ENCAP_LIMIT, p->encap_limit) ||
  1329. nla_put_be32(skb, IFLA_GRE_FLOWINFO, p->flowinfo) ||
  1330. nla_put_u32(skb, IFLA_GRE_FLAGS, p->flags))
  1331. goto nla_put_failure;
  1332. return 0;
  1333. nla_put_failure:
  1334. return -EMSGSIZE;
  1335. }
  1336. static const struct nla_policy ip6gre_policy[IFLA_GRE_MAX + 1] = {
  1337. [IFLA_GRE_LINK] = { .type = NLA_U32 },
  1338. [IFLA_GRE_IFLAGS] = { .type = NLA_U16 },
  1339. [IFLA_GRE_OFLAGS] = { .type = NLA_U16 },
  1340. [IFLA_GRE_IKEY] = { .type = NLA_U32 },
  1341. [IFLA_GRE_OKEY] = { .type = NLA_U32 },
  1342. [IFLA_GRE_LOCAL] = { .len = FIELD_SIZEOF(struct ipv6hdr, saddr) },
  1343. [IFLA_GRE_REMOTE] = { .len = FIELD_SIZEOF(struct ipv6hdr, daddr) },
  1344. [IFLA_GRE_TTL] = { .type = NLA_U8 },
  1345. [IFLA_GRE_ENCAP_LIMIT] = { .type = NLA_U8 },
  1346. [IFLA_GRE_FLOWINFO] = { .type = NLA_U32 },
  1347. [IFLA_GRE_FLAGS] = { .type = NLA_U32 },
  1348. };
  1349. static struct rtnl_link_ops ip6gre_link_ops __read_mostly = {
  1350. .kind = "ip6gre",
  1351. .maxtype = IFLA_GRE_MAX,
  1352. .policy = ip6gre_policy,
  1353. .priv_size = sizeof(struct ip6_tnl),
  1354. .setup = ip6gre_tunnel_setup,
  1355. .validate = ip6gre_tunnel_validate,
  1356. .newlink = ip6gre_newlink,
  1357. .changelink = ip6gre_changelink,
  1358. .get_size = ip6gre_get_size,
  1359. .fill_info = ip6gre_fill_info,
  1360. };
  1361. static struct rtnl_link_ops ip6gre_tap_ops __read_mostly = {
  1362. .kind = "ip6gretap",
  1363. .maxtype = IFLA_GRE_MAX,
  1364. .policy = ip6gre_policy,
  1365. .priv_size = sizeof(struct ip6_tnl),
  1366. .setup = ip6gre_tap_setup,
  1367. .validate = ip6gre_tap_validate,
  1368. .newlink = ip6gre_newlink,
  1369. .changelink = ip6gre_changelink,
  1370. .get_size = ip6gre_get_size,
  1371. .fill_info = ip6gre_fill_info,
  1372. };
  1373. /*
  1374. * And now the modules code and kernel interface.
  1375. */
  1376. static int __init ip6gre_init(void)
  1377. {
  1378. int err;
  1379. pr_info("GRE over IPv6 tunneling driver\n");
  1380. err = register_pernet_device(&ip6gre_net_ops);
  1381. if (err < 0)
  1382. return err;
  1383. err = inet6_add_protocol(&ip6gre_protocol, IPPROTO_GRE);
  1384. if (err < 0) {
  1385. pr_info("%s: can't add protocol\n", __func__);
  1386. goto add_proto_failed;
  1387. }
  1388. err = rtnl_link_register(&ip6gre_link_ops);
  1389. if (err < 0)
  1390. goto rtnl_link_failed;
  1391. err = rtnl_link_register(&ip6gre_tap_ops);
  1392. if (err < 0)
  1393. goto tap_ops_failed;
  1394. out:
  1395. return err;
  1396. tap_ops_failed:
  1397. rtnl_link_unregister(&ip6gre_link_ops);
  1398. rtnl_link_failed:
  1399. inet6_del_protocol(&ip6gre_protocol, IPPROTO_GRE);
  1400. add_proto_failed:
  1401. unregister_pernet_device(&ip6gre_net_ops);
  1402. goto out;
  1403. }
  1404. static void __exit ip6gre_fini(void)
  1405. {
  1406. rtnl_link_unregister(&ip6gre_tap_ops);
  1407. rtnl_link_unregister(&ip6gre_link_ops);
  1408. inet6_del_protocol(&ip6gre_protocol, IPPROTO_GRE);
  1409. unregister_pernet_device(&ip6gre_net_ops);
  1410. }
  1411. module_init(ip6gre_init);
  1412. module_exit(ip6gre_fini);
  1413. MODULE_LICENSE("GPL");
  1414. MODULE_AUTHOR("D. Kozlov (xeb@mail.ru)");
  1415. MODULE_DESCRIPTION("GRE over IPv6 tunneling device");
  1416. MODULE_ALIAS_RTNL_LINK("ip6gre");
  1417. MODULE_ALIAS_NETDEV("ip6gre0");