ip6_gre.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753
  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. #include <net/gre.h>
  55. static bool log_ecn_error = true;
  56. module_param(log_ecn_error, bool, 0644);
  57. MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN");
  58. #define HASH_SIZE_SHIFT 5
  59. #define HASH_SIZE (1 << HASH_SIZE_SHIFT)
  60. static int ip6gre_net_id __read_mostly;
  61. struct ip6gre_net {
  62. struct ip6_tnl __rcu *tunnels[4][HASH_SIZE];
  63. struct net_device *fb_tunnel_dev;
  64. };
  65. static struct rtnl_link_ops ip6gre_link_ops __read_mostly;
  66. static struct rtnl_link_ops ip6gre_tap_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)
  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 NULL;
  268. if (t || !create)
  269. return t;
  270. if (parms->name[0]) {
  271. if (!dev_valid_name(parms->name))
  272. return NULL;
  273. strlcpy(name, parms->name, IFNAMSIZ);
  274. } else {
  275. strcpy(name, "ip6gre%d");
  276. }
  277. dev = alloc_netdev(sizeof(*t), name, NET_NAME_UNKNOWN,
  278. ip6gre_tunnel_setup);
  279. if (!dev)
  280. return NULL;
  281. dev_net_set(dev, net);
  282. nt = netdev_priv(dev);
  283. nt->parms = *parms;
  284. dev->rtnl_link_ops = &ip6gre_link_ops;
  285. nt->dev = dev;
  286. nt->net = dev_net(dev);
  287. ip6gre_tnl_link_config(nt, 1);
  288. if (register_netdevice(dev) < 0)
  289. goto failed_free;
  290. /* Can use a lockless transmit, unless we generate output sequences */
  291. if (!(nt->parms.o_flags & GRE_SEQ))
  292. dev->features |= NETIF_F_LLTX;
  293. dev_hold(dev);
  294. ip6gre_tunnel_link(ign, nt);
  295. return nt;
  296. failed_free:
  297. free_netdev(dev);
  298. return NULL;
  299. }
  300. static void ip6gre_tunnel_uninit(struct net_device *dev)
  301. {
  302. struct ip6_tnl *t = netdev_priv(dev);
  303. struct ip6gre_net *ign = net_generic(t->net, ip6gre_net_id);
  304. ip6gre_tunnel_unlink(ign, t);
  305. dst_cache_reset(&t->dst_cache);
  306. dev_put(dev);
  307. }
  308. static void ip6gre_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
  309. u8 type, u8 code, int offset, __be32 info)
  310. {
  311. const struct gre_base_hdr *greh;
  312. const struct ipv6hdr *ipv6h;
  313. int grehlen = sizeof(*greh);
  314. struct ip6_tnl *t;
  315. int key_off = 0;
  316. __be16 flags;
  317. __be32 key;
  318. if (!pskb_may_pull(skb, offset + grehlen))
  319. return;
  320. greh = (const struct gre_base_hdr *)(skb->data + offset);
  321. flags = greh->flags;
  322. if (flags & (GRE_VERSION | GRE_ROUTING))
  323. return;
  324. if (flags & GRE_CSUM)
  325. grehlen += 4;
  326. if (flags & GRE_KEY) {
  327. key_off = grehlen + offset;
  328. grehlen += 4;
  329. }
  330. if (!pskb_may_pull(skb, offset + grehlen))
  331. return;
  332. ipv6h = (const struct ipv6hdr *)skb->data;
  333. greh = (const struct gre_base_hdr *)(skb->data + offset);
  334. key = key_off ? *(__be32 *)(skb->data + key_off) : 0;
  335. t = ip6gre_tunnel_lookup(skb->dev, &ipv6h->daddr, &ipv6h->saddr,
  336. key, greh->protocol);
  337. if (!t)
  338. return;
  339. switch (type) {
  340. __u32 teli;
  341. struct ipv6_tlv_tnl_enc_lim *tel;
  342. __u32 mtu;
  343. case ICMPV6_DEST_UNREACH:
  344. net_dbg_ratelimited("%s: Path to destination invalid or inactive!\n",
  345. t->parms.name);
  346. if (code != ICMPV6_PORT_UNREACH)
  347. break;
  348. return;
  349. case ICMPV6_TIME_EXCEED:
  350. if (code == ICMPV6_EXC_HOPLIMIT) {
  351. net_dbg_ratelimited("%s: Too small hop limit or routing loop in tunnel!\n",
  352. t->parms.name);
  353. break;
  354. }
  355. return;
  356. case ICMPV6_PARAMPROB:
  357. teli = 0;
  358. if (code == ICMPV6_HDR_FIELD)
  359. teli = ip6_tnl_parse_tlv_enc_lim(skb, skb->data);
  360. if (teli && teli == be32_to_cpu(info) - 2) {
  361. tel = (struct ipv6_tlv_tnl_enc_lim *) &skb->data[teli];
  362. if (tel->encap_limit == 0) {
  363. net_dbg_ratelimited("%s: Too small encapsulation limit or routing loop in tunnel!\n",
  364. t->parms.name);
  365. }
  366. } else {
  367. net_dbg_ratelimited("%s: Recipient unable to parse tunneled packet!\n",
  368. t->parms.name);
  369. }
  370. return;
  371. case ICMPV6_PKT_TOOBIG:
  372. mtu = be32_to_cpu(info) - offset;
  373. if (mtu < IPV6_MIN_MTU)
  374. mtu = IPV6_MIN_MTU;
  375. t->dev->mtu = mtu;
  376. return;
  377. }
  378. if (time_before(jiffies, t->err_time + IP6TUNNEL_ERR_TIMEO))
  379. t->err_count++;
  380. else
  381. t->err_count = 1;
  382. t->err_time = jiffies;
  383. }
  384. static int ip6gre_rcv(struct sk_buff *skb)
  385. {
  386. const struct ipv6hdr *ipv6h;
  387. u8 *h;
  388. __be16 flags;
  389. __sum16 csum = 0;
  390. __be32 key = 0;
  391. u32 seqno = 0;
  392. struct ip6_tnl *tunnel;
  393. int offset = 4;
  394. __be16 gre_proto;
  395. int err;
  396. if (!pskb_may_pull(skb, sizeof(struct in6_addr)))
  397. goto drop;
  398. ipv6h = ipv6_hdr(skb);
  399. h = skb->data;
  400. flags = *(__be16 *)h;
  401. if (flags&(GRE_CSUM|GRE_KEY|GRE_ROUTING|GRE_SEQ|GRE_VERSION)) {
  402. /* - Version must be 0.
  403. - We do not support routing headers.
  404. */
  405. if (flags&(GRE_VERSION|GRE_ROUTING))
  406. goto drop;
  407. if (flags&GRE_CSUM) {
  408. csum = skb_checksum_simple_validate(skb);
  409. offset += 4;
  410. }
  411. if (flags&GRE_KEY) {
  412. key = *(__be32 *)(h + offset);
  413. offset += 4;
  414. }
  415. if (flags&GRE_SEQ) {
  416. seqno = ntohl(*(__be32 *)(h + offset));
  417. offset += 4;
  418. }
  419. }
  420. gre_proto = *(__be16 *)(h + 2);
  421. tunnel = ip6gre_tunnel_lookup(skb->dev,
  422. &ipv6h->saddr, &ipv6h->daddr, key,
  423. gre_proto);
  424. if (tunnel) {
  425. struct pcpu_sw_netstats *tstats;
  426. if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb))
  427. goto drop;
  428. if (!ip6_tnl_rcv_ctl(tunnel, &ipv6h->daddr, &ipv6h->saddr)) {
  429. tunnel->dev->stats.rx_dropped++;
  430. goto drop;
  431. }
  432. skb->protocol = gre_proto;
  433. /* WCCP version 1 and 2 protocol decoding.
  434. * - Change protocol to IPv6
  435. * - When dealing with WCCPv2, Skip extra 4 bytes in GRE header
  436. */
  437. if (flags == 0 && gre_proto == htons(ETH_P_WCCP)) {
  438. skb->protocol = htons(ETH_P_IPV6);
  439. if ((*(h + offset) & 0xF0) != 0x40)
  440. offset += 4;
  441. }
  442. skb->mac_header = skb->network_header;
  443. __pskb_pull(skb, offset);
  444. skb_postpull_rcsum(skb, skb_transport_header(skb), offset);
  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, tunnel->net);
  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 ip6_tnl *tunnel = netdev_priv(dev);
  522. struct net *net = tunnel->net;
  523. struct net_device *tdev; /* Device to other host */
  524. struct ipv6hdr *ipv6h; /* Our new IP header */
  525. unsigned int max_headroom = 0; /* 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. __be16 protocol;
  535. if (dev->type == ARPHRD_ETHER)
  536. IPCB(skb)->flags = 0;
  537. if (dev->header_ops && dev->type == ARPHRD_IP6GRE) {
  538. gre_hlen = 0;
  539. ipv6h = (struct ipv6hdr *)skb->data;
  540. fl6->daddr = ipv6h->daddr;
  541. } else {
  542. gre_hlen = tunnel->hlen;
  543. fl6->daddr = tunnel->parms.raddr;
  544. }
  545. if (!fl6->flowi6_mark)
  546. dst = dst_cache_get(&tunnel->dst_cache);
  547. if (!dst) {
  548. dst = ip6_route_output(net, NULL, fl6);
  549. if (dst->error)
  550. goto tx_err_link_failure;
  551. dst = xfrm_lookup(net, dst, flowi6_to_flowi(fl6), NULL, 0);
  552. if (IS_ERR(dst)) {
  553. err = PTR_ERR(dst);
  554. dst = NULL;
  555. goto tx_err_link_failure;
  556. }
  557. ndst = dst;
  558. }
  559. tdev = dst->dev;
  560. if (tdev == dev) {
  561. stats->collisions++;
  562. net_warn_ratelimited("%s: Local routing loop detected!\n",
  563. tunnel->parms.name);
  564. goto tx_err_dst_release;
  565. }
  566. mtu = dst_mtu(dst) - sizeof(*ipv6h);
  567. if (encap_limit >= 0) {
  568. max_headroom += 8;
  569. mtu -= 8;
  570. }
  571. if (mtu < IPV6_MIN_MTU)
  572. mtu = IPV6_MIN_MTU;
  573. if (skb_dst(skb))
  574. skb_dst(skb)->ops->update_pmtu(skb_dst(skb), NULL, skb, mtu);
  575. if (skb->len > mtu) {
  576. *pmtu = mtu;
  577. err = -EMSGSIZE;
  578. goto tx_err_dst_release;
  579. }
  580. if (tunnel->err_count > 0) {
  581. if (time_before(jiffies,
  582. tunnel->err_time + IP6TUNNEL_ERR_TIMEO)) {
  583. tunnel->err_count--;
  584. dst_link_failure(skb);
  585. } else
  586. tunnel->err_count = 0;
  587. }
  588. skb_scrub_packet(skb, !net_eq(tunnel->net, dev_net(dev)));
  589. max_headroom += LL_RESERVED_SPACE(tdev) + gre_hlen + dst->header_len;
  590. if (skb_headroom(skb) < max_headroom || skb_shared(skb) ||
  591. (skb_cloned(skb) && !skb_clone_writable(skb, 0))) {
  592. new_skb = skb_realloc_headroom(skb, max_headroom);
  593. if (max_headroom > dev->needed_headroom)
  594. dev->needed_headroom = max_headroom;
  595. if (!new_skb)
  596. goto tx_err_dst_release;
  597. if (skb->sk)
  598. skb_set_owner_w(new_skb, skb->sk);
  599. consume_skb(skb);
  600. skb = new_skb;
  601. }
  602. if (!fl6->flowi6_mark && ndst)
  603. dst_cache_set_ip6(&tunnel->dst_cache, ndst, &fl6->saddr);
  604. skb_dst_set(skb, dst);
  605. proto = NEXTHDR_GRE;
  606. if (encap_limit >= 0) {
  607. init_tel_txopt(&opt, encap_limit);
  608. ipv6_push_nfrag_opts(skb, &opt.ops, &proto, NULL);
  609. }
  610. if (likely(!skb->encapsulation)) {
  611. skb_reset_inner_headers(skb);
  612. skb->encapsulation = 1;
  613. }
  614. skb_push(skb, gre_hlen);
  615. skb_reset_network_header(skb);
  616. skb_set_transport_header(skb, sizeof(*ipv6h));
  617. /*
  618. * Push down and install the IP header.
  619. */
  620. ipv6h = ipv6_hdr(skb);
  621. ip6_flow_hdr(ipv6h, INET_ECN_encapsulate(0, dsfield),
  622. ip6_make_flowlabel(net, skb, fl6->flowlabel, true, fl6));
  623. ipv6h->hop_limit = tunnel->parms.hop_limit;
  624. ipv6h->nexthdr = proto;
  625. ipv6h->saddr = fl6->saddr;
  626. ipv6h->daddr = fl6->daddr;
  627. ((__be16 *)(ipv6h + 1))[0] = tunnel->parms.o_flags;
  628. protocol = (dev->type == ARPHRD_ETHER) ?
  629. htons(ETH_P_TEB) : skb->protocol;
  630. ((__be16 *)(ipv6h + 1))[1] = protocol;
  631. if (tunnel->parms.o_flags&(GRE_KEY|GRE_CSUM|GRE_SEQ)) {
  632. __be32 *ptr = (__be32 *)(((u8 *)ipv6h) + tunnel->hlen - 4);
  633. if (tunnel->parms.o_flags&GRE_SEQ) {
  634. ++tunnel->o_seqno;
  635. *ptr = htonl(tunnel->o_seqno);
  636. ptr--;
  637. }
  638. if (tunnel->parms.o_flags&GRE_KEY) {
  639. *ptr = tunnel->parms.o_key;
  640. ptr--;
  641. }
  642. if (tunnel->parms.o_flags&GRE_CSUM) {
  643. *ptr = 0;
  644. *(__sum16 *)ptr = ip_compute_csum((void *)(ipv6h+1),
  645. skb->len - sizeof(struct ipv6hdr));
  646. }
  647. }
  648. skb_set_inner_protocol(skb, protocol);
  649. ip6tunnel_xmit(NULL, skb, dev);
  650. return 0;
  651. tx_err_link_failure:
  652. stats->tx_carrier_errors++;
  653. dst_link_failure(skb);
  654. tx_err_dst_release:
  655. dst_release(dst);
  656. return err;
  657. }
  658. static inline int ip6gre_xmit_ipv4(struct sk_buff *skb, struct net_device *dev)
  659. {
  660. struct ip6_tnl *t = netdev_priv(dev);
  661. const struct iphdr *iph = ip_hdr(skb);
  662. int encap_limit = -1;
  663. struct flowi6 fl6;
  664. __u8 dsfield;
  665. __u32 mtu;
  666. int err;
  667. memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
  668. if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
  669. encap_limit = t->parms.encap_limit;
  670. memcpy(&fl6, &t->fl.u.ip6, sizeof(fl6));
  671. fl6.flowi6_proto = IPPROTO_GRE;
  672. dsfield = ipv4_get_dsfield(iph);
  673. if (t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS)
  674. fl6.flowlabel |= htonl((__u32)iph->tos << IPV6_TCLASS_SHIFT)
  675. & IPV6_TCLASS_MASK;
  676. if (t->parms.flags & IP6_TNL_F_USE_ORIG_FWMARK)
  677. fl6.flowi6_mark = skb->mark;
  678. err = ip6gre_xmit2(skb, dev, dsfield, &fl6, encap_limit, &mtu);
  679. if (err != 0) {
  680. /* XXX: send ICMP error even if DF is not set. */
  681. if (err == -EMSGSIZE)
  682. icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED,
  683. htonl(mtu));
  684. return -1;
  685. }
  686. return 0;
  687. }
  688. static inline int ip6gre_xmit_ipv6(struct sk_buff *skb, struct net_device *dev)
  689. {
  690. struct ip6_tnl *t = netdev_priv(dev);
  691. struct ipv6hdr *ipv6h = ipv6_hdr(skb);
  692. int encap_limit = -1;
  693. __u16 offset;
  694. struct flowi6 fl6;
  695. __u8 dsfield;
  696. __u32 mtu;
  697. int err;
  698. if (ipv6_addr_equal(&t->parms.raddr, &ipv6h->saddr))
  699. return -1;
  700. offset = ip6_tnl_parse_tlv_enc_lim(skb, skb_network_header(skb));
  701. if (offset > 0) {
  702. struct ipv6_tlv_tnl_enc_lim *tel;
  703. tel = (struct ipv6_tlv_tnl_enc_lim *)&skb_network_header(skb)[offset];
  704. if (tel->encap_limit == 0) {
  705. icmpv6_send(skb, ICMPV6_PARAMPROB,
  706. ICMPV6_HDR_FIELD, offset + 2);
  707. return -1;
  708. }
  709. encap_limit = tel->encap_limit - 1;
  710. } else if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
  711. encap_limit = t->parms.encap_limit;
  712. memcpy(&fl6, &t->fl.u.ip6, sizeof(fl6));
  713. fl6.flowi6_proto = IPPROTO_GRE;
  714. dsfield = ipv6_get_dsfield(ipv6h);
  715. if (t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS)
  716. fl6.flowlabel |= (*(__be32 *) ipv6h & IPV6_TCLASS_MASK);
  717. if (t->parms.flags & IP6_TNL_F_USE_ORIG_FLOWLABEL)
  718. fl6.flowlabel |= ip6_flowlabel(ipv6h);
  719. if (t->parms.flags & IP6_TNL_F_USE_ORIG_FWMARK)
  720. fl6.flowi6_mark = skb->mark;
  721. err = ip6gre_xmit2(skb, dev, dsfield, &fl6, encap_limit, &mtu);
  722. if (err != 0) {
  723. if (err == -EMSGSIZE)
  724. icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
  725. return -1;
  726. }
  727. return 0;
  728. }
  729. /**
  730. * ip6_tnl_addr_conflict - compare packet addresses to tunnel's own
  731. * @t: the outgoing tunnel device
  732. * @hdr: IPv6 header from the incoming packet
  733. *
  734. * Description:
  735. * Avoid trivial tunneling loop by checking that tunnel exit-point
  736. * doesn't match source of incoming packet.
  737. *
  738. * Return:
  739. * 1 if conflict,
  740. * 0 else
  741. **/
  742. static inline bool ip6gre_tnl_addr_conflict(const struct ip6_tnl *t,
  743. const struct ipv6hdr *hdr)
  744. {
  745. return ipv6_addr_equal(&t->parms.raddr, &hdr->saddr);
  746. }
  747. static int ip6gre_xmit_other(struct sk_buff *skb, struct net_device *dev)
  748. {
  749. struct ip6_tnl *t = netdev_priv(dev);
  750. int encap_limit = -1;
  751. struct flowi6 fl6;
  752. __u32 mtu;
  753. int err;
  754. if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
  755. encap_limit = t->parms.encap_limit;
  756. memcpy(&fl6, &t->fl.u.ip6, sizeof(fl6));
  757. err = ip6gre_xmit2(skb, dev, 0, &fl6, encap_limit, &mtu);
  758. return err;
  759. }
  760. static netdev_tx_t ip6gre_tunnel_xmit(struct sk_buff *skb,
  761. struct net_device *dev)
  762. {
  763. struct ip6_tnl *t = netdev_priv(dev);
  764. struct net_device_stats *stats = &t->dev->stats;
  765. int ret;
  766. if (!ip6_tnl_xmit_ctl(t, &t->parms.laddr, &t->parms.raddr))
  767. goto tx_err;
  768. switch (skb->protocol) {
  769. case htons(ETH_P_IP):
  770. ret = ip6gre_xmit_ipv4(skb, dev);
  771. break;
  772. case htons(ETH_P_IPV6):
  773. ret = ip6gre_xmit_ipv6(skb, dev);
  774. break;
  775. default:
  776. ret = ip6gre_xmit_other(skb, dev);
  777. break;
  778. }
  779. if (ret < 0)
  780. goto tx_err;
  781. return NETDEV_TX_OK;
  782. tx_err:
  783. stats->tx_errors++;
  784. stats->tx_dropped++;
  785. kfree_skb(skb);
  786. return NETDEV_TX_OK;
  787. }
  788. static void ip6gre_tnl_link_config(struct ip6_tnl *t, int set_mtu)
  789. {
  790. struct net_device *dev = t->dev;
  791. struct __ip6_tnl_parm *p = &t->parms;
  792. struct flowi6 *fl6 = &t->fl.u.ip6;
  793. int addend = sizeof(struct ipv6hdr) + 4;
  794. if (dev->type != ARPHRD_ETHER) {
  795. memcpy(dev->dev_addr, &p->laddr, sizeof(struct in6_addr));
  796. memcpy(dev->broadcast, &p->raddr, sizeof(struct in6_addr));
  797. }
  798. /* Set up flowi template */
  799. fl6->saddr = p->laddr;
  800. fl6->daddr = p->raddr;
  801. fl6->flowi6_oif = p->link;
  802. fl6->flowlabel = 0;
  803. if (!(p->flags&IP6_TNL_F_USE_ORIG_TCLASS))
  804. fl6->flowlabel |= IPV6_TCLASS_MASK & p->flowinfo;
  805. if (!(p->flags&IP6_TNL_F_USE_ORIG_FLOWLABEL))
  806. fl6->flowlabel |= IPV6_FLOWLABEL_MASK & p->flowinfo;
  807. p->flags &= ~(IP6_TNL_F_CAP_XMIT|IP6_TNL_F_CAP_RCV|IP6_TNL_F_CAP_PER_PACKET);
  808. p->flags |= ip6_tnl_get_cap(t, &p->laddr, &p->raddr);
  809. if (p->flags&IP6_TNL_F_CAP_XMIT &&
  810. p->flags&IP6_TNL_F_CAP_RCV && dev->type != ARPHRD_ETHER)
  811. dev->flags |= IFF_POINTOPOINT;
  812. else
  813. dev->flags &= ~IFF_POINTOPOINT;
  814. /* Precalculate GRE options length */
  815. if (t->parms.o_flags&(GRE_CSUM|GRE_KEY|GRE_SEQ)) {
  816. if (t->parms.o_flags&GRE_CSUM)
  817. addend += 4;
  818. if (t->parms.o_flags&GRE_KEY)
  819. addend += 4;
  820. if (t->parms.o_flags&GRE_SEQ)
  821. addend += 4;
  822. }
  823. t->hlen = addend;
  824. if (p->flags & IP6_TNL_F_CAP_XMIT) {
  825. int strict = (ipv6_addr_type(&p->raddr) &
  826. (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL));
  827. struct rt6_info *rt = rt6_lookup(t->net,
  828. &p->raddr, &p->laddr,
  829. p->link, strict);
  830. if (!rt)
  831. return;
  832. if (rt->dst.dev) {
  833. dev->hard_header_len = rt->dst.dev->hard_header_len + addend;
  834. if (set_mtu) {
  835. dev->mtu = rt->dst.dev->mtu - addend;
  836. if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
  837. dev->mtu -= 8;
  838. if (dev->mtu < IPV6_MIN_MTU)
  839. dev->mtu = IPV6_MIN_MTU;
  840. }
  841. }
  842. ip6_rt_put(rt);
  843. }
  844. }
  845. static int ip6gre_tnl_change(struct ip6_tnl *t,
  846. const struct __ip6_tnl_parm *p, int set_mtu)
  847. {
  848. t->parms.laddr = p->laddr;
  849. t->parms.raddr = p->raddr;
  850. t->parms.flags = p->flags;
  851. t->parms.hop_limit = p->hop_limit;
  852. t->parms.encap_limit = p->encap_limit;
  853. t->parms.flowinfo = p->flowinfo;
  854. t->parms.link = p->link;
  855. t->parms.proto = p->proto;
  856. t->parms.i_key = p->i_key;
  857. t->parms.o_key = p->o_key;
  858. t->parms.i_flags = p->i_flags;
  859. t->parms.o_flags = p->o_flags;
  860. dst_cache_reset(&t->dst_cache);
  861. ip6gre_tnl_link_config(t, set_mtu);
  862. return 0;
  863. }
  864. static void ip6gre_tnl_parm_from_user(struct __ip6_tnl_parm *p,
  865. const struct ip6_tnl_parm2 *u)
  866. {
  867. p->laddr = u->laddr;
  868. p->raddr = u->raddr;
  869. p->flags = u->flags;
  870. p->hop_limit = u->hop_limit;
  871. p->encap_limit = u->encap_limit;
  872. p->flowinfo = u->flowinfo;
  873. p->link = u->link;
  874. p->i_key = u->i_key;
  875. p->o_key = u->o_key;
  876. p->i_flags = u->i_flags;
  877. p->o_flags = u->o_flags;
  878. memcpy(p->name, u->name, sizeof(u->name));
  879. }
  880. static void ip6gre_tnl_parm_to_user(struct ip6_tnl_parm2 *u,
  881. const struct __ip6_tnl_parm *p)
  882. {
  883. u->proto = IPPROTO_GRE;
  884. u->laddr = p->laddr;
  885. u->raddr = p->raddr;
  886. u->flags = p->flags;
  887. u->hop_limit = p->hop_limit;
  888. u->encap_limit = p->encap_limit;
  889. u->flowinfo = p->flowinfo;
  890. u->link = p->link;
  891. u->i_key = p->i_key;
  892. u->o_key = p->o_key;
  893. u->i_flags = p->i_flags;
  894. u->o_flags = p->o_flags;
  895. memcpy(u->name, p->name, sizeof(u->name));
  896. }
  897. static int ip6gre_tunnel_ioctl(struct net_device *dev,
  898. struct ifreq *ifr, int cmd)
  899. {
  900. int err = 0;
  901. struct ip6_tnl_parm2 p;
  902. struct __ip6_tnl_parm p1;
  903. struct ip6_tnl *t = netdev_priv(dev);
  904. struct net *net = t->net;
  905. struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
  906. switch (cmd) {
  907. case SIOCGETTUNNEL:
  908. if (dev == ign->fb_tunnel_dev) {
  909. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) {
  910. err = -EFAULT;
  911. break;
  912. }
  913. ip6gre_tnl_parm_from_user(&p1, &p);
  914. t = ip6gre_tunnel_locate(net, &p1, 0);
  915. if (!t)
  916. t = netdev_priv(dev);
  917. }
  918. memset(&p, 0, sizeof(p));
  919. ip6gre_tnl_parm_to_user(&p, &t->parms);
  920. if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p)))
  921. err = -EFAULT;
  922. break;
  923. case SIOCADDTUNNEL:
  924. case SIOCCHGTUNNEL:
  925. err = -EPERM;
  926. if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  927. goto done;
  928. err = -EFAULT;
  929. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
  930. goto done;
  931. err = -EINVAL;
  932. if ((p.i_flags|p.o_flags)&(GRE_VERSION|GRE_ROUTING))
  933. goto done;
  934. if (!(p.i_flags&GRE_KEY))
  935. p.i_key = 0;
  936. if (!(p.o_flags&GRE_KEY))
  937. p.o_key = 0;
  938. ip6gre_tnl_parm_from_user(&p1, &p);
  939. t = ip6gre_tunnel_locate(net, &p1, cmd == SIOCADDTUNNEL);
  940. if (dev != ign->fb_tunnel_dev && cmd == SIOCCHGTUNNEL) {
  941. if (t) {
  942. if (t->dev != dev) {
  943. err = -EEXIST;
  944. break;
  945. }
  946. } else {
  947. t = netdev_priv(dev);
  948. ip6gre_tunnel_unlink(ign, t);
  949. synchronize_net();
  950. ip6gre_tnl_change(t, &p1, 1);
  951. ip6gre_tunnel_link(ign, t);
  952. netdev_state_change(dev);
  953. }
  954. }
  955. if (t) {
  956. err = 0;
  957. memset(&p, 0, sizeof(p));
  958. ip6gre_tnl_parm_to_user(&p, &t->parms);
  959. if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p)))
  960. err = -EFAULT;
  961. } else
  962. err = (cmd == SIOCADDTUNNEL ? -ENOBUFS : -ENOENT);
  963. break;
  964. case SIOCDELTUNNEL:
  965. err = -EPERM;
  966. if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  967. goto done;
  968. if (dev == ign->fb_tunnel_dev) {
  969. err = -EFAULT;
  970. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
  971. goto done;
  972. err = -ENOENT;
  973. ip6gre_tnl_parm_from_user(&p1, &p);
  974. t = ip6gre_tunnel_locate(net, &p1, 0);
  975. if (!t)
  976. goto done;
  977. err = -EPERM;
  978. if (t == netdev_priv(ign->fb_tunnel_dev))
  979. goto done;
  980. dev = t->dev;
  981. }
  982. unregister_netdevice(dev);
  983. err = 0;
  984. break;
  985. default:
  986. err = -EINVAL;
  987. }
  988. done:
  989. return err;
  990. }
  991. static int ip6gre_tunnel_change_mtu(struct net_device *dev, int new_mtu)
  992. {
  993. if (new_mtu < 68 ||
  994. new_mtu > 0xFFF8 - dev->hard_header_len)
  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, const void *daddr,
  1001. const void *saddr, unsigned int len)
  1002. {
  1003. struct ip6_tnl *t = netdev_priv(dev);
  1004. struct ipv6hdr *ipv6h;
  1005. __be16 *p;
  1006. ipv6h = (struct ipv6hdr *)skb_push(skb, t->hlen + sizeof(*ipv6h));
  1007. ip6_flow_hdr(ipv6h, 0, ip6_make_flowlabel(dev_net(dev), skb,
  1008. t->fl.u.ip6.flowlabel,
  1009. true, &t->fl.u.ip6));
  1010. ipv6h->hop_limit = t->parms.hop_limit;
  1011. ipv6h->nexthdr = NEXTHDR_GRE;
  1012. ipv6h->saddr = t->parms.laddr;
  1013. ipv6h->daddr = t->parms.raddr;
  1014. p = (__be16 *)(ipv6h + 1);
  1015. p[0] = t->parms.o_flags;
  1016. p[1] = htons(type);
  1017. /*
  1018. * Set the source hardware address.
  1019. */
  1020. if (saddr)
  1021. memcpy(&ipv6h->saddr, saddr, sizeof(struct in6_addr));
  1022. if (daddr)
  1023. memcpy(&ipv6h->daddr, daddr, sizeof(struct in6_addr));
  1024. if (!ipv6_addr_any(&ipv6h->daddr))
  1025. return t->hlen;
  1026. return -t->hlen;
  1027. }
  1028. static const struct header_ops ip6gre_header_ops = {
  1029. .create = ip6gre_header,
  1030. };
  1031. static const struct net_device_ops ip6gre_netdev_ops = {
  1032. .ndo_init = ip6gre_tunnel_init,
  1033. .ndo_uninit = ip6gre_tunnel_uninit,
  1034. .ndo_start_xmit = ip6gre_tunnel_xmit,
  1035. .ndo_do_ioctl = ip6gre_tunnel_ioctl,
  1036. .ndo_change_mtu = ip6gre_tunnel_change_mtu,
  1037. .ndo_get_stats64 = ip_tunnel_get_stats64,
  1038. .ndo_get_iflink = ip6_tnl_get_iflink,
  1039. };
  1040. static void ip6gre_dev_free(struct net_device *dev)
  1041. {
  1042. struct ip6_tnl *t = netdev_priv(dev);
  1043. dst_cache_destroy(&t->dst_cache);
  1044. free_percpu(dev->tstats);
  1045. free_netdev(dev);
  1046. }
  1047. static void ip6gre_tunnel_setup(struct net_device *dev)
  1048. {
  1049. struct ip6_tnl *t;
  1050. dev->netdev_ops = &ip6gre_netdev_ops;
  1051. dev->destructor = ip6gre_dev_free;
  1052. dev->type = ARPHRD_IP6GRE;
  1053. dev->hard_header_len = LL_MAX_HEADER + sizeof(struct ipv6hdr) + 4;
  1054. dev->mtu = ETH_DATA_LEN - sizeof(struct ipv6hdr) - 4;
  1055. t = netdev_priv(dev);
  1056. if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
  1057. dev->mtu -= 8;
  1058. dev->flags |= IFF_NOARP;
  1059. dev->addr_len = sizeof(struct in6_addr);
  1060. netif_keep_dst(dev);
  1061. }
  1062. static int ip6gre_tunnel_init_common(struct net_device *dev)
  1063. {
  1064. struct ip6_tnl *tunnel;
  1065. int ret;
  1066. tunnel = netdev_priv(dev);
  1067. tunnel->dev = dev;
  1068. tunnel->net = dev_net(dev);
  1069. strcpy(tunnel->parms.name, dev->name);
  1070. dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
  1071. if (!dev->tstats)
  1072. return -ENOMEM;
  1073. ret = dst_cache_init(&tunnel->dst_cache, GFP_KERNEL);
  1074. if (ret) {
  1075. free_percpu(dev->tstats);
  1076. dev->tstats = NULL;
  1077. return ret;
  1078. }
  1079. return 0;
  1080. }
  1081. static int ip6gre_tunnel_init(struct net_device *dev)
  1082. {
  1083. struct ip6_tnl *tunnel;
  1084. int ret;
  1085. ret = ip6gre_tunnel_init_common(dev);
  1086. if (ret)
  1087. return ret;
  1088. tunnel = netdev_priv(dev);
  1089. memcpy(dev->dev_addr, &tunnel->parms.laddr, sizeof(struct in6_addr));
  1090. memcpy(dev->broadcast, &tunnel->parms.raddr, sizeof(struct in6_addr));
  1091. if (ipv6_addr_any(&tunnel->parms.raddr))
  1092. dev->header_ops = &ip6gre_header_ops;
  1093. return 0;
  1094. }
  1095. static void ip6gre_fb_tunnel_init(struct net_device *dev)
  1096. {
  1097. struct ip6_tnl *tunnel = netdev_priv(dev);
  1098. tunnel->dev = dev;
  1099. tunnel->net = dev_net(dev);
  1100. strcpy(tunnel->parms.name, dev->name);
  1101. tunnel->hlen = sizeof(struct ipv6hdr) + 4;
  1102. dev_hold(dev);
  1103. }
  1104. static struct inet6_protocol ip6gre_protocol __read_mostly = {
  1105. .handler = ip6gre_rcv,
  1106. .err_handler = ip6gre_err,
  1107. .flags = INET6_PROTO_NOPOLICY|INET6_PROTO_FINAL,
  1108. };
  1109. static void ip6gre_destroy_tunnels(struct net *net, struct list_head *head)
  1110. {
  1111. struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
  1112. struct net_device *dev, *aux;
  1113. int prio;
  1114. for_each_netdev_safe(net, dev, aux)
  1115. if (dev->rtnl_link_ops == &ip6gre_link_ops ||
  1116. dev->rtnl_link_ops == &ip6gre_tap_ops)
  1117. unregister_netdevice_queue(dev, head);
  1118. for (prio = 0; prio < 4; prio++) {
  1119. int h;
  1120. for (h = 0; h < HASH_SIZE; h++) {
  1121. struct ip6_tnl *t;
  1122. t = rtnl_dereference(ign->tunnels[prio][h]);
  1123. while (t) {
  1124. /* If dev is in the same netns, it has already
  1125. * been added to the list by the previous loop.
  1126. */
  1127. if (!net_eq(dev_net(t->dev), net))
  1128. unregister_netdevice_queue(t->dev,
  1129. head);
  1130. t = rtnl_dereference(t->next);
  1131. }
  1132. }
  1133. }
  1134. }
  1135. static int __net_init ip6gre_init_net(struct net *net)
  1136. {
  1137. struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
  1138. int err;
  1139. ign->fb_tunnel_dev = alloc_netdev(sizeof(struct ip6_tnl), "ip6gre0",
  1140. NET_NAME_UNKNOWN,
  1141. ip6gre_tunnel_setup);
  1142. if (!ign->fb_tunnel_dev) {
  1143. err = -ENOMEM;
  1144. goto err_alloc_dev;
  1145. }
  1146. dev_net_set(ign->fb_tunnel_dev, net);
  1147. /* FB netdevice is special: we have one, and only one per netns.
  1148. * Allowing to move it to another netns is clearly unsafe.
  1149. */
  1150. ign->fb_tunnel_dev->features |= NETIF_F_NETNS_LOCAL;
  1151. ip6gre_fb_tunnel_init(ign->fb_tunnel_dev);
  1152. ign->fb_tunnel_dev->rtnl_link_ops = &ip6gre_link_ops;
  1153. err = register_netdev(ign->fb_tunnel_dev);
  1154. if (err)
  1155. goto err_reg_dev;
  1156. rcu_assign_pointer(ign->tunnels_wc[0],
  1157. netdev_priv(ign->fb_tunnel_dev));
  1158. return 0;
  1159. err_reg_dev:
  1160. ip6gre_dev_free(ign->fb_tunnel_dev);
  1161. err_alloc_dev:
  1162. return err;
  1163. }
  1164. static void __net_exit ip6gre_exit_net(struct net *net)
  1165. {
  1166. LIST_HEAD(list);
  1167. rtnl_lock();
  1168. ip6gre_destroy_tunnels(net, &list);
  1169. unregister_netdevice_many(&list);
  1170. rtnl_unlock();
  1171. }
  1172. static struct pernet_operations ip6gre_net_ops = {
  1173. .init = ip6gre_init_net,
  1174. .exit = ip6gre_exit_net,
  1175. .id = &ip6gre_net_id,
  1176. .size = sizeof(struct ip6gre_net),
  1177. };
  1178. static int ip6gre_tunnel_validate(struct nlattr *tb[], struct nlattr *data[])
  1179. {
  1180. __be16 flags;
  1181. if (!data)
  1182. return 0;
  1183. flags = 0;
  1184. if (data[IFLA_GRE_IFLAGS])
  1185. flags |= nla_get_be16(data[IFLA_GRE_IFLAGS]);
  1186. if (data[IFLA_GRE_OFLAGS])
  1187. flags |= nla_get_be16(data[IFLA_GRE_OFLAGS]);
  1188. if (flags & (GRE_VERSION|GRE_ROUTING))
  1189. return -EINVAL;
  1190. return 0;
  1191. }
  1192. static int ip6gre_tap_validate(struct nlattr *tb[], struct nlattr *data[])
  1193. {
  1194. struct in6_addr daddr;
  1195. if (tb[IFLA_ADDRESS]) {
  1196. if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
  1197. return -EINVAL;
  1198. if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
  1199. return -EADDRNOTAVAIL;
  1200. }
  1201. if (!data)
  1202. goto out;
  1203. if (data[IFLA_GRE_REMOTE]) {
  1204. daddr = nla_get_in6_addr(data[IFLA_GRE_REMOTE]);
  1205. if (ipv6_addr_any(&daddr))
  1206. return -EINVAL;
  1207. }
  1208. out:
  1209. return ip6gre_tunnel_validate(tb, data);
  1210. }
  1211. static void ip6gre_netlink_parms(struct nlattr *data[],
  1212. struct __ip6_tnl_parm *parms)
  1213. {
  1214. memset(parms, 0, sizeof(*parms));
  1215. if (!data)
  1216. return;
  1217. if (data[IFLA_GRE_LINK])
  1218. parms->link = nla_get_u32(data[IFLA_GRE_LINK]);
  1219. if (data[IFLA_GRE_IFLAGS])
  1220. parms->i_flags = nla_get_be16(data[IFLA_GRE_IFLAGS]);
  1221. if (data[IFLA_GRE_OFLAGS])
  1222. parms->o_flags = nla_get_be16(data[IFLA_GRE_OFLAGS]);
  1223. if (data[IFLA_GRE_IKEY])
  1224. parms->i_key = nla_get_be32(data[IFLA_GRE_IKEY]);
  1225. if (data[IFLA_GRE_OKEY])
  1226. parms->o_key = nla_get_be32(data[IFLA_GRE_OKEY]);
  1227. if (data[IFLA_GRE_LOCAL])
  1228. parms->laddr = nla_get_in6_addr(data[IFLA_GRE_LOCAL]);
  1229. if (data[IFLA_GRE_REMOTE])
  1230. parms->raddr = nla_get_in6_addr(data[IFLA_GRE_REMOTE]);
  1231. if (data[IFLA_GRE_TTL])
  1232. parms->hop_limit = nla_get_u8(data[IFLA_GRE_TTL]);
  1233. if (data[IFLA_GRE_ENCAP_LIMIT])
  1234. parms->encap_limit = nla_get_u8(data[IFLA_GRE_ENCAP_LIMIT]);
  1235. if (data[IFLA_GRE_FLOWINFO])
  1236. parms->flowinfo = nla_get_u32(data[IFLA_GRE_FLOWINFO]);
  1237. if (data[IFLA_GRE_FLAGS])
  1238. parms->flags = nla_get_u32(data[IFLA_GRE_FLAGS]);
  1239. }
  1240. static int ip6gre_tap_init(struct net_device *dev)
  1241. {
  1242. struct ip6_tnl *tunnel;
  1243. int ret;
  1244. ret = ip6gre_tunnel_init_common(dev);
  1245. if (ret)
  1246. return ret;
  1247. tunnel = netdev_priv(dev);
  1248. ip6gre_tnl_link_config(tunnel, 1);
  1249. return 0;
  1250. }
  1251. static const struct net_device_ops ip6gre_tap_netdev_ops = {
  1252. .ndo_init = ip6gre_tap_init,
  1253. .ndo_uninit = ip6gre_tunnel_uninit,
  1254. .ndo_start_xmit = ip6gre_tunnel_xmit,
  1255. .ndo_set_mac_address = eth_mac_addr,
  1256. .ndo_validate_addr = eth_validate_addr,
  1257. .ndo_change_mtu = ip6gre_tunnel_change_mtu,
  1258. .ndo_get_stats64 = ip_tunnel_get_stats64,
  1259. .ndo_get_iflink = ip6_tnl_get_iflink,
  1260. };
  1261. static void ip6gre_tap_setup(struct net_device *dev)
  1262. {
  1263. ether_setup(dev);
  1264. dev->netdev_ops = &ip6gre_tap_netdev_ops;
  1265. dev->destructor = ip6gre_dev_free;
  1266. dev->features |= NETIF_F_NETNS_LOCAL;
  1267. }
  1268. static int ip6gre_newlink(struct net *src_net, struct net_device *dev,
  1269. struct nlattr *tb[], struct nlattr *data[])
  1270. {
  1271. struct ip6_tnl *nt;
  1272. struct net *net = dev_net(dev);
  1273. struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
  1274. int err;
  1275. nt = netdev_priv(dev);
  1276. ip6gre_netlink_parms(data, &nt->parms);
  1277. if (ip6gre_tunnel_find(net, &nt->parms, dev->type))
  1278. return -EEXIST;
  1279. if (dev->type == ARPHRD_ETHER && !tb[IFLA_ADDRESS])
  1280. eth_hw_addr_random(dev);
  1281. nt->dev = dev;
  1282. nt->net = dev_net(dev);
  1283. ip6gre_tnl_link_config(nt, !tb[IFLA_MTU]);
  1284. /* Can use a lockless transmit, unless we generate output sequences */
  1285. if (!(nt->parms.o_flags & GRE_SEQ))
  1286. dev->features |= NETIF_F_LLTX;
  1287. err = register_netdevice(dev);
  1288. if (err)
  1289. goto out;
  1290. dev_hold(dev);
  1291. ip6gre_tunnel_link(ign, nt);
  1292. out:
  1293. return err;
  1294. }
  1295. static int ip6gre_changelink(struct net_device *dev, struct nlattr *tb[],
  1296. struct nlattr *data[])
  1297. {
  1298. struct ip6_tnl *t, *nt = netdev_priv(dev);
  1299. struct net *net = nt->net;
  1300. struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
  1301. struct __ip6_tnl_parm p;
  1302. if (dev == ign->fb_tunnel_dev)
  1303. return -EINVAL;
  1304. ip6gre_netlink_parms(data, &p);
  1305. t = ip6gre_tunnel_locate(net, &p, 0);
  1306. if (t) {
  1307. if (t->dev != dev)
  1308. return -EEXIST;
  1309. } else {
  1310. t = nt;
  1311. }
  1312. ip6gre_tunnel_unlink(ign, t);
  1313. ip6gre_tnl_change(t, &p, !tb[IFLA_MTU]);
  1314. ip6gre_tunnel_link(ign, t);
  1315. return 0;
  1316. }
  1317. static void ip6gre_dellink(struct net_device *dev, struct list_head *head)
  1318. {
  1319. struct net *net = dev_net(dev);
  1320. struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
  1321. if (dev != ign->fb_tunnel_dev)
  1322. unregister_netdevice_queue(dev, head);
  1323. }
  1324. static size_t ip6gre_get_size(const struct net_device *dev)
  1325. {
  1326. return
  1327. /* IFLA_GRE_LINK */
  1328. nla_total_size(4) +
  1329. /* IFLA_GRE_IFLAGS */
  1330. nla_total_size(2) +
  1331. /* IFLA_GRE_OFLAGS */
  1332. nla_total_size(2) +
  1333. /* IFLA_GRE_IKEY */
  1334. nla_total_size(4) +
  1335. /* IFLA_GRE_OKEY */
  1336. nla_total_size(4) +
  1337. /* IFLA_GRE_LOCAL */
  1338. nla_total_size(sizeof(struct in6_addr)) +
  1339. /* IFLA_GRE_REMOTE */
  1340. nla_total_size(sizeof(struct in6_addr)) +
  1341. /* IFLA_GRE_TTL */
  1342. nla_total_size(1) +
  1343. /* IFLA_GRE_TOS */
  1344. nla_total_size(1) +
  1345. /* IFLA_GRE_ENCAP_LIMIT */
  1346. nla_total_size(1) +
  1347. /* IFLA_GRE_FLOWINFO */
  1348. nla_total_size(4) +
  1349. /* IFLA_GRE_FLAGS */
  1350. nla_total_size(4) +
  1351. 0;
  1352. }
  1353. static int ip6gre_fill_info(struct sk_buff *skb, const struct net_device *dev)
  1354. {
  1355. struct ip6_tnl *t = netdev_priv(dev);
  1356. struct __ip6_tnl_parm *p = &t->parms;
  1357. if (nla_put_u32(skb, IFLA_GRE_LINK, p->link) ||
  1358. nla_put_be16(skb, IFLA_GRE_IFLAGS, p->i_flags) ||
  1359. nla_put_be16(skb, IFLA_GRE_OFLAGS, p->o_flags) ||
  1360. nla_put_be32(skb, IFLA_GRE_IKEY, p->i_key) ||
  1361. nla_put_be32(skb, IFLA_GRE_OKEY, p->o_key) ||
  1362. nla_put_in6_addr(skb, IFLA_GRE_LOCAL, &p->laddr) ||
  1363. nla_put_in6_addr(skb, IFLA_GRE_REMOTE, &p->raddr) ||
  1364. nla_put_u8(skb, IFLA_GRE_TTL, p->hop_limit) ||
  1365. /*nla_put_u8(skb, IFLA_GRE_TOS, t->priority) ||*/
  1366. nla_put_u8(skb, IFLA_GRE_ENCAP_LIMIT, p->encap_limit) ||
  1367. nla_put_be32(skb, IFLA_GRE_FLOWINFO, p->flowinfo) ||
  1368. nla_put_u32(skb, IFLA_GRE_FLAGS, p->flags))
  1369. goto nla_put_failure;
  1370. return 0;
  1371. nla_put_failure:
  1372. return -EMSGSIZE;
  1373. }
  1374. static const struct nla_policy ip6gre_policy[IFLA_GRE_MAX + 1] = {
  1375. [IFLA_GRE_LINK] = { .type = NLA_U32 },
  1376. [IFLA_GRE_IFLAGS] = { .type = NLA_U16 },
  1377. [IFLA_GRE_OFLAGS] = { .type = NLA_U16 },
  1378. [IFLA_GRE_IKEY] = { .type = NLA_U32 },
  1379. [IFLA_GRE_OKEY] = { .type = NLA_U32 },
  1380. [IFLA_GRE_LOCAL] = { .len = FIELD_SIZEOF(struct ipv6hdr, saddr) },
  1381. [IFLA_GRE_REMOTE] = { .len = FIELD_SIZEOF(struct ipv6hdr, daddr) },
  1382. [IFLA_GRE_TTL] = { .type = NLA_U8 },
  1383. [IFLA_GRE_ENCAP_LIMIT] = { .type = NLA_U8 },
  1384. [IFLA_GRE_FLOWINFO] = { .type = NLA_U32 },
  1385. [IFLA_GRE_FLAGS] = { .type = NLA_U32 },
  1386. };
  1387. static struct rtnl_link_ops ip6gre_link_ops __read_mostly = {
  1388. .kind = "ip6gre",
  1389. .maxtype = IFLA_GRE_MAX,
  1390. .policy = ip6gre_policy,
  1391. .priv_size = sizeof(struct ip6_tnl),
  1392. .setup = ip6gre_tunnel_setup,
  1393. .validate = ip6gre_tunnel_validate,
  1394. .newlink = ip6gre_newlink,
  1395. .changelink = ip6gre_changelink,
  1396. .dellink = ip6gre_dellink,
  1397. .get_size = ip6gre_get_size,
  1398. .fill_info = ip6gre_fill_info,
  1399. .get_link_net = ip6_tnl_get_link_net,
  1400. };
  1401. static struct rtnl_link_ops ip6gre_tap_ops __read_mostly = {
  1402. .kind = "ip6gretap",
  1403. .maxtype = IFLA_GRE_MAX,
  1404. .policy = ip6gre_policy,
  1405. .priv_size = sizeof(struct ip6_tnl),
  1406. .setup = ip6gre_tap_setup,
  1407. .validate = ip6gre_tap_validate,
  1408. .newlink = ip6gre_newlink,
  1409. .changelink = ip6gre_changelink,
  1410. .get_size = ip6gre_get_size,
  1411. .fill_info = ip6gre_fill_info,
  1412. .get_link_net = ip6_tnl_get_link_net,
  1413. };
  1414. /*
  1415. * And now the modules code and kernel interface.
  1416. */
  1417. static int __init ip6gre_init(void)
  1418. {
  1419. int err;
  1420. pr_info("GRE over IPv6 tunneling driver\n");
  1421. err = register_pernet_device(&ip6gre_net_ops);
  1422. if (err < 0)
  1423. return err;
  1424. err = inet6_add_protocol(&ip6gre_protocol, IPPROTO_GRE);
  1425. if (err < 0) {
  1426. pr_info("%s: can't add protocol\n", __func__);
  1427. goto add_proto_failed;
  1428. }
  1429. err = rtnl_link_register(&ip6gre_link_ops);
  1430. if (err < 0)
  1431. goto rtnl_link_failed;
  1432. err = rtnl_link_register(&ip6gre_tap_ops);
  1433. if (err < 0)
  1434. goto tap_ops_failed;
  1435. out:
  1436. return err;
  1437. tap_ops_failed:
  1438. rtnl_link_unregister(&ip6gre_link_ops);
  1439. rtnl_link_failed:
  1440. inet6_del_protocol(&ip6gre_protocol, IPPROTO_GRE);
  1441. add_proto_failed:
  1442. unregister_pernet_device(&ip6gre_net_ops);
  1443. goto out;
  1444. }
  1445. static void __exit ip6gre_fini(void)
  1446. {
  1447. rtnl_link_unregister(&ip6gre_tap_ops);
  1448. rtnl_link_unregister(&ip6gre_link_ops);
  1449. inet6_del_protocol(&ip6gre_protocol, IPPROTO_GRE);
  1450. unregister_pernet_device(&ip6gre_net_ops);
  1451. }
  1452. module_init(ip6gre_init);
  1453. module_exit(ip6gre_fini);
  1454. MODULE_LICENSE("GPL");
  1455. MODULE_AUTHOR("D. Kozlov (xeb@mail.ru)");
  1456. MODULE_DESCRIPTION("GRE over IPv6 tunneling device");
  1457. MODULE_ALIAS_RTNL_LINK("ip6gre");
  1458. MODULE_ALIAS_RTNL_LINK("ip6gretap");
  1459. MODULE_ALIAS_NETDEV("ip6gre0");