ip6_gre.c 41 KB

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