ip6_gre.c 41 KB

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