ip6_gre.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617
  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/init.h>
  26. #include <linux/in6.h>
  27. #include <linux/inetdevice.h>
  28. #include <linux/igmp.h>
  29. #include <linux/netfilter_ipv4.h>
  30. #include <linux/etherdevice.h>
  31. #include <linux/if_ether.h>
  32. #include <linux/hash.h>
  33. #include <linux/if_tunnel.h>
  34. #include <linux/ip6_tunnel.h>
  35. #include <net/sock.h>
  36. #include <net/ip.h>
  37. #include <net/ip_tunnels.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. #include <net/gre.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 IP6_GRE_HASH_SIZE_SHIFT 5
  58. #define IP6_GRE_HASH_SIZE (1 << IP6_GRE_HASH_SIZE_SHIFT)
  59. static int ip6gre_net_id __read_mostly;
  60. struct ip6gre_net {
  61. struct ip6_tnl __rcu *tunnels[4][IP6_GRE_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))&(IP6_GRE_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, IP6_GRE_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 & TUNNEL_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. dst_cache_reset(&t->dst_cache);
  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 gre_base_hdr *greh;
  308. const struct ipv6hdr *ipv6h;
  309. int grehlen = sizeof(*greh);
  310. struct ip6_tnl *t;
  311. int key_off = 0;
  312. __be16 flags;
  313. __be32 key;
  314. if (!pskb_may_pull(skb, offset + grehlen))
  315. return;
  316. greh = (const struct gre_base_hdr *)(skb->data + offset);
  317. flags = greh->flags;
  318. if (flags & (GRE_VERSION | GRE_ROUTING))
  319. return;
  320. if (flags & GRE_CSUM)
  321. grehlen += 4;
  322. if (flags & GRE_KEY) {
  323. key_off = grehlen + offset;
  324. grehlen += 4;
  325. }
  326. if (!pskb_may_pull(skb, offset + grehlen))
  327. return;
  328. ipv6h = (const struct ipv6hdr *)skb->data;
  329. greh = (const struct gre_base_hdr *)(skb->data + offset);
  330. key = key_off ? *(__be32 *)(skb->data + key_off) : 0;
  331. t = ip6gre_tunnel_lookup(skb->dev, &ipv6h->daddr, &ipv6h->saddr,
  332. key, greh->protocol);
  333. if (!t)
  334. return;
  335. switch (type) {
  336. __u32 teli;
  337. struct ipv6_tlv_tnl_enc_lim *tel;
  338. __u32 mtu;
  339. case ICMPV6_DEST_UNREACH:
  340. net_dbg_ratelimited("%s: Path to destination invalid or inactive!\n",
  341. t->parms.name);
  342. break;
  343. case ICMPV6_TIME_EXCEED:
  344. if (code == ICMPV6_EXC_HOPLIMIT) {
  345. net_dbg_ratelimited("%s: Too small hop limit or routing loop in tunnel!\n",
  346. t->parms.name);
  347. }
  348. break;
  349. case ICMPV6_PARAMPROB:
  350. teli = 0;
  351. if (code == ICMPV6_HDR_FIELD)
  352. teli = ip6_tnl_parse_tlv_enc_lim(skb, skb->data);
  353. if (teli && teli == be32_to_cpu(info) - 2) {
  354. tel = (struct ipv6_tlv_tnl_enc_lim *) &skb->data[teli];
  355. if (tel->encap_limit == 0) {
  356. net_dbg_ratelimited("%s: Too small encapsulation limit or routing loop in tunnel!\n",
  357. t->parms.name);
  358. }
  359. } else {
  360. net_dbg_ratelimited("%s: Recipient unable to parse tunneled packet!\n",
  361. t->parms.name);
  362. }
  363. break;
  364. case ICMPV6_PKT_TOOBIG:
  365. mtu = be32_to_cpu(info) - offset - t->tun_hlen;
  366. if (t->dev->type == ARPHRD_ETHER)
  367. mtu -= ETH_HLEN;
  368. if (mtu < IPV6_MIN_MTU)
  369. mtu = IPV6_MIN_MTU;
  370. t->dev->mtu = mtu;
  371. break;
  372. }
  373. if (time_before(jiffies, t->err_time + IP6TUNNEL_ERR_TIMEO))
  374. t->err_count++;
  375. else
  376. t->err_count = 1;
  377. t->err_time = jiffies;
  378. }
  379. static int ip6gre_rcv(struct sk_buff *skb, const struct tnl_ptk_info *tpi)
  380. {
  381. const struct ipv6hdr *ipv6h;
  382. struct ip6_tnl *tunnel;
  383. ipv6h = ipv6_hdr(skb);
  384. tunnel = ip6gre_tunnel_lookup(skb->dev,
  385. &ipv6h->saddr, &ipv6h->daddr, tpi->key,
  386. tpi->proto);
  387. if (tunnel) {
  388. ip6_tnl_rcv(tunnel, skb, tpi, NULL, false);
  389. return PACKET_RCVD;
  390. }
  391. return PACKET_REJECT;
  392. }
  393. static int gre_rcv(struct sk_buff *skb)
  394. {
  395. struct tnl_ptk_info tpi;
  396. bool csum_err = false;
  397. int hdr_len;
  398. hdr_len = gre_parse_header(skb, &tpi, &csum_err, htons(ETH_P_IPV6), 0);
  399. if (hdr_len < 0)
  400. goto drop;
  401. if (iptunnel_pull_header(skb, hdr_len, tpi.proto, false))
  402. goto drop;
  403. if (ip6gre_rcv(skb, &tpi) == PACKET_RCVD)
  404. return 0;
  405. icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0);
  406. drop:
  407. kfree_skb(skb);
  408. return 0;
  409. }
  410. struct ipv6_tel_txoption {
  411. struct ipv6_txoptions ops;
  412. __u8 dst_opt[8];
  413. };
  414. static int gre_handle_offloads(struct sk_buff *skb, bool csum)
  415. {
  416. return iptunnel_handle_offloads(skb,
  417. csum ? SKB_GSO_GRE_CSUM : SKB_GSO_GRE);
  418. }
  419. static netdev_tx_t __gre6_xmit(struct sk_buff *skb,
  420. struct net_device *dev, __u8 dsfield,
  421. struct flowi6 *fl6, int encap_limit,
  422. __u32 *pmtu, __be16 proto)
  423. {
  424. struct ip6_tnl *tunnel = netdev_priv(dev);
  425. __be16 protocol = (dev->type == ARPHRD_ETHER) ?
  426. htons(ETH_P_TEB) : proto;
  427. if (dev->type == ARPHRD_ETHER)
  428. IPCB(skb)->flags = 0;
  429. if (dev->header_ops && dev->type == ARPHRD_IP6GRE)
  430. fl6->daddr = ((struct ipv6hdr *)skb->data)->daddr;
  431. else
  432. fl6->daddr = tunnel->parms.raddr;
  433. if (tunnel->parms.o_flags & TUNNEL_SEQ)
  434. tunnel->o_seqno++;
  435. /* Push GRE header. */
  436. gre_build_header(skb, tunnel->tun_hlen, tunnel->parms.o_flags,
  437. protocol, tunnel->parms.o_key, htonl(tunnel->o_seqno));
  438. return ip6_tnl_xmit(skb, dev, dsfield, fl6, encap_limit, pmtu,
  439. NEXTHDR_GRE);
  440. }
  441. static inline int ip6gre_xmit_ipv4(struct sk_buff *skb, struct net_device *dev)
  442. {
  443. struct ip6_tnl *t = netdev_priv(dev);
  444. const struct iphdr *iph = ip_hdr(skb);
  445. int encap_limit = -1;
  446. struct flowi6 fl6;
  447. __u8 dsfield;
  448. __u32 mtu;
  449. int err;
  450. memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
  451. if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
  452. encap_limit = t->parms.encap_limit;
  453. memcpy(&fl6, &t->fl.u.ip6, sizeof(fl6));
  454. if (t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS)
  455. dsfield = ipv4_get_dsfield(iph);
  456. else
  457. dsfield = ip6_tclass(t->parms.flowinfo);
  458. if (t->parms.flags & IP6_TNL_F_USE_ORIG_FWMARK)
  459. fl6.flowi6_mark = skb->mark;
  460. err = gre_handle_offloads(skb, !!(t->parms.o_flags & TUNNEL_CSUM));
  461. if (err)
  462. return -1;
  463. err = __gre6_xmit(skb, dev, dsfield, &fl6, encap_limit, &mtu,
  464. skb->protocol);
  465. if (err != 0) {
  466. /* XXX: send ICMP error even if DF is not set. */
  467. if (err == -EMSGSIZE)
  468. icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED,
  469. htonl(mtu));
  470. return -1;
  471. }
  472. return 0;
  473. }
  474. static inline int ip6gre_xmit_ipv6(struct sk_buff *skb, struct net_device *dev)
  475. {
  476. struct ip6_tnl *t = netdev_priv(dev);
  477. struct ipv6hdr *ipv6h = ipv6_hdr(skb);
  478. int encap_limit = -1;
  479. __u16 offset;
  480. struct flowi6 fl6;
  481. __u8 dsfield;
  482. __u32 mtu;
  483. int err;
  484. if (ipv6_addr_equal(&t->parms.raddr, &ipv6h->saddr))
  485. return -1;
  486. offset = ip6_tnl_parse_tlv_enc_lim(skb, skb_network_header(skb));
  487. /* ip6_tnl_parse_tlv_enc_lim() might have reallocated skb->head */
  488. ipv6h = ipv6_hdr(skb);
  489. if (offset > 0) {
  490. struct ipv6_tlv_tnl_enc_lim *tel;
  491. tel = (struct ipv6_tlv_tnl_enc_lim *)&skb_network_header(skb)[offset];
  492. if (tel->encap_limit == 0) {
  493. icmpv6_send(skb, ICMPV6_PARAMPROB,
  494. ICMPV6_HDR_FIELD, offset + 2);
  495. return -1;
  496. }
  497. encap_limit = tel->encap_limit - 1;
  498. } else if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
  499. encap_limit = t->parms.encap_limit;
  500. memcpy(&fl6, &t->fl.u.ip6, sizeof(fl6));
  501. if (t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS)
  502. dsfield = ipv6_get_dsfield(ipv6h);
  503. else
  504. dsfield = ip6_tclass(t->parms.flowinfo);
  505. if (t->parms.flags & IP6_TNL_F_USE_ORIG_FLOWLABEL)
  506. fl6.flowlabel |= ip6_flowlabel(ipv6h);
  507. if (t->parms.flags & IP6_TNL_F_USE_ORIG_FWMARK)
  508. fl6.flowi6_mark = skb->mark;
  509. if (gre_handle_offloads(skb, !!(t->parms.o_flags & TUNNEL_CSUM)))
  510. return -1;
  511. err = __gre6_xmit(skb, dev, dsfield, &fl6, encap_limit,
  512. &mtu, skb->protocol);
  513. if (err != 0) {
  514. if (err == -EMSGSIZE)
  515. icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
  516. return -1;
  517. }
  518. return 0;
  519. }
  520. /**
  521. * ip6_tnl_addr_conflict - compare packet addresses to tunnel's own
  522. * @t: the outgoing tunnel device
  523. * @hdr: IPv6 header from the incoming packet
  524. *
  525. * Description:
  526. * Avoid trivial tunneling loop by checking that tunnel exit-point
  527. * doesn't match source of incoming packet.
  528. *
  529. * Return:
  530. * 1 if conflict,
  531. * 0 else
  532. **/
  533. static inline bool ip6gre_tnl_addr_conflict(const struct ip6_tnl *t,
  534. const struct ipv6hdr *hdr)
  535. {
  536. return ipv6_addr_equal(&t->parms.raddr, &hdr->saddr);
  537. }
  538. static int ip6gre_xmit_other(struct sk_buff *skb, struct net_device *dev)
  539. {
  540. struct ip6_tnl *t = netdev_priv(dev);
  541. int encap_limit = -1;
  542. struct flowi6 fl6;
  543. __u32 mtu;
  544. int err;
  545. if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
  546. encap_limit = t->parms.encap_limit;
  547. memcpy(&fl6, &t->fl.u.ip6, sizeof(fl6));
  548. err = gre_handle_offloads(skb, !!(t->parms.o_flags & TUNNEL_CSUM));
  549. if (err)
  550. return err;
  551. err = __gre6_xmit(skb, dev, 0, &fl6, encap_limit, &mtu, skb->protocol);
  552. return err;
  553. }
  554. static netdev_tx_t ip6gre_tunnel_xmit(struct sk_buff *skb,
  555. struct net_device *dev)
  556. {
  557. struct ip6_tnl *t = netdev_priv(dev);
  558. struct net_device_stats *stats = &t->dev->stats;
  559. int ret;
  560. if (!ip6_tnl_xmit_ctl(t, &t->parms.laddr, &t->parms.raddr))
  561. goto tx_err;
  562. switch (skb->protocol) {
  563. case htons(ETH_P_IP):
  564. ret = ip6gre_xmit_ipv4(skb, dev);
  565. break;
  566. case htons(ETH_P_IPV6):
  567. ret = ip6gre_xmit_ipv6(skb, dev);
  568. break;
  569. default:
  570. ret = ip6gre_xmit_other(skb, dev);
  571. break;
  572. }
  573. if (ret < 0)
  574. goto tx_err;
  575. return NETDEV_TX_OK;
  576. tx_err:
  577. stats->tx_errors++;
  578. stats->tx_dropped++;
  579. kfree_skb(skb);
  580. return NETDEV_TX_OK;
  581. }
  582. static void ip6gre_tnl_link_config(struct ip6_tnl *t, int set_mtu)
  583. {
  584. struct net_device *dev = t->dev;
  585. struct __ip6_tnl_parm *p = &t->parms;
  586. struct flowi6 *fl6 = &t->fl.u.ip6;
  587. int t_hlen;
  588. if (dev->type != ARPHRD_ETHER) {
  589. memcpy(dev->dev_addr, &p->laddr, sizeof(struct in6_addr));
  590. memcpy(dev->broadcast, &p->raddr, sizeof(struct in6_addr));
  591. }
  592. /* Set up flowi template */
  593. fl6->saddr = p->laddr;
  594. fl6->daddr = p->raddr;
  595. fl6->flowi6_oif = p->link;
  596. fl6->flowlabel = 0;
  597. fl6->flowi6_proto = IPPROTO_GRE;
  598. if (!(p->flags&IP6_TNL_F_USE_ORIG_TCLASS))
  599. fl6->flowlabel |= IPV6_TCLASS_MASK & p->flowinfo;
  600. if (!(p->flags&IP6_TNL_F_USE_ORIG_FLOWLABEL))
  601. fl6->flowlabel |= IPV6_FLOWLABEL_MASK & p->flowinfo;
  602. p->flags &= ~(IP6_TNL_F_CAP_XMIT|IP6_TNL_F_CAP_RCV|IP6_TNL_F_CAP_PER_PACKET);
  603. p->flags |= ip6_tnl_get_cap(t, &p->laddr, &p->raddr);
  604. if (p->flags&IP6_TNL_F_CAP_XMIT &&
  605. p->flags&IP6_TNL_F_CAP_RCV && dev->type != ARPHRD_ETHER)
  606. dev->flags |= IFF_POINTOPOINT;
  607. else
  608. dev->flags &= ~IFF_POINTOPOINT;
  609. t->tun_hlen = gre_calc_hlen(t->parms.o_flags);
  610. t->hlen = t->encap_hlen + t->tun_hlen;
  611. t_hlen = t->hlen + sizeof(struct ipv6hdr);
  612. if (p->flags & IP6_TNL_F_CAP_XMIT) {
  613. int strict = (ipv6_addr_type(&p->raddr) &
  614. (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL));
  615. struct rt6_info *rt = rt6_lookup(t->net,
  616. &p->raddr, &p->laddr,
  617. p->link, strict);
  618. if (!rt)
  619. return;
  620. if (rt->dst.dev) {
  621. dev->hard_header_len = rt->dst.dev->hard_header_len +
  622. t_hlen;
  623. if (set_mtu) {
  624. dev->mtu = rt->dst.dev->mtu - t_hlen;
  625. if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
  626. dev->mtu -= 8;
  627. if (dev->type == ARPHRD_ETHER)
  628. dev->mtu -= ETH_HLEN;
  629. if (dev->mtu < IPV6_MIN_MTU)
  630. dev->mtu = IPV6_MIN_MTU;
  631. }
  632. }
  633. ip6_rt_put(rt);
  634. }
  635. }
  636. static int ip6gre_tnl_change(struct ip6_tnl *t,
  637. const struct __ip6_tnl_parm *p, int set_mtu)
  638. {
  639. t->parms.laddr = p->laddr;
  640. t->parms.raddr = p->raddr;
  641. t->parms.flags = p->flags;
  642. t->parms.hop_limit = p->hop_limit;
  643. t->parms.encap_limit = p->encap_limit;
  644. t->parms.flowinfo = p->flowinfo;
  645. t->parms.link = p->link;
  646. t->parms.proto = p->proto;
  647. t->parms.i_key = p->i_key;
  648. t->parms.o_key = p->o_key;
  649. t->parms.i_flags = p->i_flags;
  650. t->parms.o_flags = p->o_flags;
  651. dst_cache_reset(&t->dst_cache);
  652. ip6gre_tnl_link_config(t, set_mtu);
  653. return 0;
  654. }
  655. static void ip6gre_tnl_parm_from_user(struct __ip6_tnl_parm *p,
  656. const struct ip6_tnl_parm2 *u)
  657. {
  658. p->laddr = u->laddr;
  659. p->raddr = u->raddr;
  660. p->flags = u->flags;
  661. p->hop_limit = u->hop_limit;
  662. p->encap_limit = u->encap_limit;
  663. p->flowinfo = u->flowinfo;
  664. p->link = u->link;
  665. p->i_key = u->i_key;
  666. p->o_key = u->o_key;
  667. p->i_flags = gre_flags_to_tnl_flags(u->i_flags);
  668. p->o_flags = gre_flags_to_tnl_flags(u->o_flags);
  669. memcpy(p->name, u->name, sizeof(u->name));
  670. }
  671. static void ip6gre_tnl_parm_to_user(struct ip6_tnl_parm2 *u,
  672. const struct __ip6_tnl_parm *p)
  673. {
  674. u->proto = IPPROTO_GRE;
  675. u->laddr = p->laddr;
  676. u->raddr = p->raddr;
  677. u->flags = p->flags;
  678. u->hop_limit = p->hop_limit;
  679. u->encap_limit = p->encap_limit;
  680. u->flowinfo = p->flowinfo;
  681. u->link = p->link;
  682. u->i_key = p->i_key;
  683. u->o_key = p->o_key;
  684. u->i_flags = gre_tnl_flags_to_gre_flags(p->i_flags);
  685. u->o_flags = gre_tnl_flags_to_gre_flags(p->o_flags);
  686. memcpy(u->name, p->name, sizeof(u->name));
  687. }
  688. static int ip6gre_tunnel_ioctl(struct net_device *dev,
  689. struct ifreq *ifr, int cmd)
  690. {
  691. int err = 0;
  692. struct ip6_tnl_parm2 p;
  693. struct __ip6_tnl_parm p1;
  694. struct ip6_tnl *t = netdev_priv(dev);
  695. struct net *net = t->net;
  696. struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
  697. memset(&p1, 0, sizeof(p1));
  698. switch (cmd) {
  699. case SIOCGETTUNNEL:
  700. if (dev == ign->fb_tunnel_dev) {
  701. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) {
  702. err = -EFAULT;
  703. break;
  704. }
  705. ip6gre_tnl_parm_from_user(&p1, &p);
  706. t = ip6gre_tunnel_locate(net, &p1, 0);
  707. if (!t)
  708. t = netdev_priv(dev);
  709. }
  710. memset(&p, 0, sizeof(p));
  711. ip6gre_tnl_parm_to_user(&p, &t->parms);
  712. if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p)))
  713. err = -EFAULT;
  714. break;
  715. case SIOCADDTUNNEL:
  716. case SIOCCHGTUNNEL:
  717. err = -EPERM;
  718. if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  719. goto done;
  720. err = -EFAULT;
  721. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
  722. goto done;
  723. err = -EINVAL;
  724. if ((p.i_flags|p.o_flags)&(GRE_VERSION|GRE_ROUTING))
  725. goto done;
  726. if (!(p.i_flags&GRE_KEY))
  727. p.i_key = 0;
  728. if (!(p.o_flags&GRE_KEY))
  729. p.o_key = 0;
  730. ip6gre_tnl_parm_from_user(&p1, &p);
  731. t = ip6gre_tunnel_locate(net, &p1, cmd == SIOCADDTUNNEL);
  732. if (dev != ign->fb_tunnel_dev && cmd == SIOCCHGTUNNEL) {
  733. if (t) {
  734. if (t->dev != dev) {
  735. err = -EEXIST;
  736. break;
  737. }
  738. } else {
  739. t = netdev_priv(dev);
  740. ip6gre_tunnel_unlink(ign, t);
  741. synchronize_net();
  742. ip6gre_tnl_change(t, &p1, 1);
  743. ip6gre_tunnel_link(ign, t);
  744. netdev_state_change(dev);
  745. }
  746. }
  747. if (t) {
  748. err = 0;
  749. memset(&p, 0, sizeof(p));
  750. ip6gre_tnl_parm_to_user(&p, &t->parms);
  751. if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p)))
  752. err = -EFAULT;
  753. } else
  754. err = (cmd == SIOCADDTUNNEL ? -ENOBUFS : -ENOENT);
  755. break;
  756. case SIOCDELTUNNEL:
  757. err = -EPERM;
  758. if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  759. goto done;
  760. if (dev == ign->fb_tunnel_dev) {
  761. err = -EFAULT;
  762. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
  763. goto done;
  764. err = -ENOENT;
  765. ip6gre_tnl_parm_from_user(&p1, &p);
  766. t = ip6gre_tunnel_locate(net, &p1, 0);
  767. if (!t)
  768. goto done;
  769. err = -EPERM;
  770. if (t == netdev_priv(ign->fb_tunnel_dev))
  771. goto done;
  772. dev = t->dev;
  773. }
  774. unregister_netdevice(dev);
  775. err = 0;
  776. break;
  777. default:
  778. err = -EINVAL;
  779. }
  780. done:
  781. return err;
  782. }
  783. static int ip6gre_header(struct sk_buff *skb, struct net_device *dev,
  784. unsigned short type, const void *daddr,
  785. const void *saddr, unsigned int len)
  786. {
  787. struct ip6_tnl *t = netdev_priv(dev);
  788. struct ipv6hdr *ipv6h;
  789. __be16 *p;
  790. ipv6h = (struct ipv6hdr *)skb_push(skb, t->hlen + sizeof(*ipv6h));
  791. ip6_flow_hdr(ipv6h, 0, ip6_make_flowlabel(dev_net(dev), skb,
  792. t->fl.u.ip6.flowlabel,
  793. true, &t->fl.u.ip6));
  794. ipv6h->hop_limit = t->parms.hop_limit;
  795. ipv6h->nexthdr = NEXTHDR_GRE;
  796. ipv6h->saddr = t->parms.laddr;
  797. ipv6h->daddr = t->parms.raddr;
  798. p = (__be16 *)(ipv6h + 1);
  799. p[0] = t->parms.o_flags;
  800. p[1] = htons(type);
  801. /*
  802. * Set the source hardware address.
  803. */
  804. if (saddr)
  805. memcpy(&ipv6h->saddr, saddr, sizeof(struct in6_addr));
  806. if (daddr)
  807. memcpy(&ipv6h->daddr, daddr, sizeof(struct in6_addr));
  808. if (!ipv6_addr_any(&ipv6h->daddr))
  809. return t->hlen;
  810. return -t->hlen;
  811. }
  812. static const struct header_ops ip6gre_header_ops = {
  813. .create = ip6gre_header,
  814. };
  815. static const struct net_device_ops ip6gre_netdev_ops = {
  816. .ndo_init = ip6gre_tunnel_init,
  817. .ndo_uninit = ip6gre_tunnel_uninit,
  818. .ndo_start_xmit = ip6gre_tunnel_xmit,
  819. .ndo_do_ioctl = ip6gre_tunnel_ioctl,
  820. .ndo_change_mtu = ip6_tnl_change_mtu,
  821. .ndo_get_stats64 = ip_tunnel_get_stats64,
  822. .ndo_get_iflink = ip6_tnl_get_iflink,
  823. };
  824. static void ip6gre_dev_free(struct net_device *dev)
  825. {
  826. struct ip6_tnl *t = netdev_priv(dev);
  827. dst_cache_destroy(&t->dst_cache);
  828. free_percpu(dev->tstats);
  829. free_netdev(dev);
  830. }
  831. static void ip6gre_tunnel_setup(struct net_device *dev)
  832. {
  833. dev->netdev_ops = &ip6gre_netdev_ops;
  834. dev->destructor = ip6gre_dev_free;
  835. dev->type = ARPHRD_IP6GRE;
  836. dev->flags |= IFF_NOARP;
  837. dev->addr_len = sizeof(struct in6_addr);
  838. netif_keep_dst(dev);
  839. }
  840. static int ip6gre_tunnel_init_common(struct net_device *dev)
  841. {
  842. struct ip6_tnl *tunnel;
  843. int ret;
  844. int t_hlen;
  845. tunnel = netdev_priv(dev);
  846. tunnel->dev = dev;
  847. tunnel->net = dev_net(dev);
  848. strcpy(tunnel->parms.name, dev->name);
  849. dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
  850. if (!dev->tstats)
  851. return -ENOMEM;
  852. ret = dst_cache_init(&tunnel->dst_cache, GFP_KERNEL);
  853. if (ret) {
  854. free_percpu(dev->tstats);
  855. dev->tstats = NULL;
  856. return ret;
  857. }
  858. tunnel->tun_hlen = gre_calc_hlen(tunnel->parms.o_flags);
  859. tunnel->hlen = tunnel->tun_hlen + tunnel->encap_hlen;
  860. t_hlen = tunnel->hlen + sizeof(struct ipv6hdr);
  861. dev->hard_header_len = LL_MAX_HEADER + t_hlen;
  862. dev->mtu = ETH_DATA_LEN - t_hlen;
  863. if (dev->type == ARPHRD_ETHER)
  864. dev->mtu -= ETH_HLEN;
  865. if (!(tunnel->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
  866. dev->mtu -= 8;
  867. return 0;
  868. }
  869. static int ip6gre_tunnel_init(struct net_device *dev)
  870. {
  871. struct ip6_tnl *tunnel;
  872. int ret;
  873. ret = ip6gre_tunnel_init_common(dev);
  874. if (ret)
  875. return ret;
  876. tunnel = netdev_priv(dev);
  877. memcpy(dev->dev_addr, &tunnel->parms.laddr, sizeof(struct in6_addr));
  878. memcpy(dev->broadcast, &tunnel->parms.raddr, sizeof(struct in6_addr));
  879. if (ipv6_addr_any(&tunnel->parms.raddr))
  880. dev->header_ops = &ip6gre_header_ops;
  881. return 0;
  882. }
  883. static void ip6gre_fb_tunnel_init(struct net_device *dev)
  884. {
  885. struct ip6_tnl *tunnel = netdev_priv(dev);
  886. tunnel->dev = dev;
  887. tunnel->net = dev_net(dev);
  888. strcpy(tunnel->parms.name, dev->name);
  889. tunnel->hlen = sizeof(struct ipv6hdr) + 4;
  890. dev_hold(dev);
  891. }
  892. static struct inet6_protocol ip6gre_protocol __read_mostly = {
  893. .handler = gre_rcv,
  894. .err_handler = ip6gre_err,
  895. .flags = INET6_PROTO_NOPOLICY|INET6_PROTO_FINAL,
  896. };
  897. static void ip6gre_destroy_tunnels(struct net *net, struct list_head *head)
  898. {
  899. struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
  900. struct net_device *dev, *aux;
  901. int prio;
  902. for_each_netdev_safe(net, dev, aux)
  903. if (dev->rtnl_link_ops == &ip6gre_link_ops ||
  904. dev->rtnl_link_ops == &ip6gre_tap_ops)
  905. unregister_netdevice_queue(dev, head);
  906. for (prio = 0; prio < 4; prio++) {
  907. int h;
  908. for (h = 0; h < IP6_GRE_HASH_SIZE; h++) {
  909. struct ip6_tnl *t;
  910. t = rtnl_dereference(ign->tunnels[prio][h]);
  911. while (t) {
  912. /* If dev is in the same netns, it has already
  913. * been added to the list by the previous loop.
  914. */
  915. if (!net_eq(dev_net(t->dev), net))
  916. unregister_netdevice_queue(t->dev,
  917. head);
  918. t = rtnl_dereference(t->next);
  919. }
  920. }
  921. }
  922. }
  923. static int __net_init ip6gre_init_net(struct net *net)
  924. {
  925. struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
  926. int err;
  927. ign->fb_tunnel_dev = alloc_netdev(sizeof(struct ip6_tnl), "ip6gre0",
  928. NET_NAME_UNKNOWN,
  929. ip6gre_tunnel_setup);
  930. if (!ign->fb_tunnel_dev) {
  931. err = -ENOMEM;
  932. goto err_alloc_dev;
  933. }
  934. dev_net_set(ign->fb_tunnel_dev, net);
  935. /* FB netdevice is special: we have one, and only one per netns.
  936. * Allowing to move it to another netns is clearly unsafe.
  937. */
  938. ign->fb_tunnel_dev->features |= NETIF_F_NETNS_LOCAL;
  939. ip6gre_fb_tunnel_init(ign->fb_tunnel_dev);
  940. ign->fb_tunnel_dev->rtnl_link_ops = &ip6gre_link_ops;
  941. err = register_netdev(ign->fb_tunnel_dev);
  942. if (err)
  943. goto err_reg_dev;
  944. rcu_assign_pointer(ign->tunnels_wc[0],
  945. netdev_priv(ign->fb_tunnel_dev));
  946. return 0;
  947. err_reg_dev:
  948. ip6gre_dev_free(ign->fb_tunnel_dev);
  949. err_alloc_dev:
  950. return err;
  951. }
  952. static void __net_exit ip6gre_exit_net(struct net *net)
  953. {
  954. LIST_HEAD(list);
  955. rtnl_lock();
  956. ip6gre_destroy_tunnels(net, &list);
  957. unregister_netdevice_many(&list);
  958. rtnl_unlock();
  959. }
  960. static struct pernet_operations ip6gre_net_ops = {
  961. .init = ip6gre_init_net,
  962. .exit = ip6gre_exit_net,
  963. .id = &ip6gre_net_id,
  964. .size = sizeof(struct ip6gre_net),
  965. };
  966. static int ip6gre_tunnel_validate(struct nlattr *tb[], struct nlattr *data[])
  967. {
  968. __be16 flags;
  969. if (!data)
  970. return 0;
  971. flags = 0;
  972. if (data[IFLA_GRE_IFLAGS])
  973. flags |= nla_get_be16(data[IFLA_GRE_IFLAGS]);
  974. if (data[IFLA_GRE_OFLAGS])
  975. flags |= nla_get_be16(data[IFLA_GRE_OFLAGS]);
  976. if (flags & (GRE_VERSION|GRE_ROUTING))
  977. return -EINVAL;
  978. return 0;
  979. }
  980. static int ip6gre_tap_validate(struct nlattr *tb[], struct nlattr *data[])
  981. {
  982. struct in6_addr daddr;
  983. if (tb[IFLA_ADDRESS]) {
  984. if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
  985. return -EINVAL;
  986. if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
  987. return -EADDRNOTAVAIL;
  988. }
  989. if (!data)
  990. goto out;
  991. if (data[IFLA_GRE_REMOTE]) {
  992. daddr = nla_get_in6_addr(data[IFLA_GRE_REMOTE]);
  993. if (ipv6_addr_any(&daddr))
  994. return -EINVAL;
  995. }
  996. out:
  997. return ip6gre_tunnel_validate(tb, data);
  998. }
  999. static void ip6gre_netlink_parms(struct nlattr *data[],
  1000. struct __ip6_tnl_parm *parms)
  1001. {
  1002. memset(parms, 0, sizeof(*parms));
  1003. if (!data)
  1004. return;
  1005. if (data[IFLA_GRE_LINK])
  1006. parms->link = nla_get_u32(data[IFLA_GRE_LINK]);
  1007. if (data[IFLA_GRE_IFLAGS])
  1008. parms->i_flags = gre_flags_to_tnl_flags(
  1009. nla_get_be16(data[IFLA_GRE_IFLAGS]));
  1010. if (data[IFLA_GRE_OFLAGS])
  1011. parms->o_flags = gre_flags_to_tnl_flags(
  1012. nla_get_be16(data[IFLA_GRE_OFLAGS]));
  1013. if (data[IFLA_GRE_IKEY])
  1014. parms->i_key = nla_get_be32(data[IFLA_GRE_IKEY]);
  1015. if (data[IFLA_GRE_OKEY])
  1016. parms->o_key = nla_get_be32(data[IFLA_GRE_OKEY]);
  1017. if (data[IFLA_GRE_LOCAL])
  1018. parms->laddr = nla_get_in6_addr(data[IFLA_GRE_LOCAL]);
  1019. if (data[IFLA_GRE_REMOTE])
  1020. parms->raddr = nla_get_in6_addr(data[IFLA_GRE_REMOTE]);
  1021. if (data[IFLA_GRE_TTL])
  1022. parms->hop_limit = nla_get_u8(data[IFLA_GRE_TTL]);
  1023. if (data[IFLA_GRE_ENCAP_LIMIT])
  1024. parms->encap_limit = nla_get_u8(data[IFLA_GRE_ENCAP_LIMIT]);
  1025. if (data[IFLA_GRE_FLOWINFO])
  1026. parms->flowinfo = nla_get_be32(data[IFLA_GRE_FLOWINFO]);
  1027. if (data[IFLA_GRE_FLAGS])
  1028. parms->flags = nla_get_u32(data[IFLA_GRE_FLAGS]);
  1029. }
  1030. static int ip6gre_tap_init(struct net_device *dev)
  1031. {
  1032. struct ip6_tnl *tunnel;
  1033. int ret;
  1034. ret = ip6gre_tunnel_init_common(dev);
  1035. if (ret)
  1036. return ret;
  1037. dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
  1038. tunnel = netdev_priv(dev);
  1039. ip6gre_tnl_link_config(tunnel, 1);
  1040. return 0;
  1041. }
  1042. static const struct net_device_ops ip6gre_tap_netdev_ops = {
  1043. .ndo_init = ip6gre_tap_init,
  1044. .ndo_uninit = ip6gre_tunnel_uninit,
  1045. .ndo_start_xmit = ip6gre_tunnel_xmit,
  1046. .ndo_set_mac_address = eth_mac_addr,
  1047. .ndo_validate_addr = eth_validate_addr,
  1048. .ndo_change_mtu = ip6_tnl_change_mtu,
  1049. .ndo_get_stats64 = ip_tunnel_get_stats64,
  1050. .ndo_get_iflink = ip6_tnl_get_iflink,
  1051. };
  1052. #define GRE6_FEATURES (NETIF_F_SG | \
  1053. NETIF_F_FRAGLIST | \
  1054. NETIF_F_HIGHDMA | \
  1055. NETIF_F_HW_CSUM)
  1056. static void ip6gre_tap_setup(struct net_device *dev)
  1057. {
  1058. ether_setup(dev);
  1059. dev->netdev_ops = &ip6gre_tap_netdev_ops;
  1060. dev->destructor = ip6gre_dev_free;
  1061. dev->features |= NETIF_F_NETNS_LOCAL;
  1062. dev->priv_flags &= ~IFF_TX_SKB_SHARING;
  1063. dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
  1064. netif_keep_dst(dev);
  1065. }
  1066. static bool ip6gre_netlink_encap_parms(struct nlattr *data[],
  1067. struct ip_tunnel_encap *ipencap)
  1068. {
  1069. bool ret = false;
  1070. memset(ipencap, 0, sizeof(*ipencap));
  1071. if (!data)
  1072. return ret;
  1073. if (data[IFLA_GRE_ENCAP_TYPE]) {
  1074. ret = true;
  1075. ipencap->type = nla_get_u16(data[IFLA_GRE_ENCAP_TYPE]);
  1076. }
  1077. if (data[IFLA_GRE_ENCAP_FLAGS]) {
  1078. ret = true;
  1079. ipencap->flags = nla_get_u16(data[IFLA_GRE_ENCAP_FLAGS]);
  1080. }
  1081. if (data[IFLA_GRE_ENCAP_SPORT]) {
  1082. ret = true;
  1083. ipencap->sport = nla_get_be16(data[IFLA_GRE_ENCAP_SPORT]);
  1084. }
  1085. if (data[IFLA_GRE_ENCAP_DPORT]) {
  1086. ret = true;
  1087. ipencap->dport = nla_get_be16(data[IFLA_GRE_ENCAP_DPORT]);
  1088. }
  1089. return ret;
  1090. }
  1091. static int ip6gre_newlink(struct net *src_net, struct net_device *dev,
  1092. struct nlattr *tb[], struct nlattr *data[])
  1093. {
  1094. struct ip6_tnl *nt;
  1095. struct net *net = dev_net(dev);
  1096. struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
  1097. struct ip_tunnel_encap ipencap;
  1098. int err;
  1099. nt = netdev_priv(dev);
  1100. if (ip6gre_netlink_encap_parms(data, &ipencap)) {
  1101. int err = ip6_tnl_encap_setup(nt, &ipencap);
  1102. if (err < 0)
  1103. return err;
  1104. }
  1105. ip6gre_netlink_parms(data, &nt->parms);
  1106. if (ip6gre_tunnel_find(net, &nt->parms, dev->type))
  1107. return -EEXIST;
  1108. if (dev->type == ARPHRD_ETHER && !tb[IFLA_ADDRESS])
  1109. eth_hw_addr_random(dev);
  1110. nt->dev = dev;
  1111. nt->net = dev_net(dev);
  1112. ip6gre_tnl_link_config(nt, !tb[IFLA_MTU]);
  1113. dev->features |= GRE6_FEATURES;
  1114. dev->hw_features |= GRE6_FEATURES;
  1115. if (!(nt->parms.o_flags & TUNNEL_SEQ)) {
  1116. /* TCP offload with GRE SEQ is not supported, nor
  1117. * can we support 2 levels of outer headers requiring
  1118. * an update.
  1119. */
  1120. if (!(nt->parms.o_flags & TUNNEL_CSUM) ||
  1121. (nt->encap.type == TUNNEL_ENCAP_NONE)) {
  1122. dev->features |= NETIF_F_GSO_SOFTWARE;
  1123. dev->hw_features |= NETIF_F_GSO_SOFTWARE;
  1124. }
  1125. /* Can use a lockless transmit, unless we generate
  1126. * output sequences
  1127. */
  1128. dev->features |= NETIF_F_LLTX;
  1129. }
  1130. err = register_netdevice(dev);
  1131. if (err)
  1132. goto out;
  1133. dev_hold(dev);
  1134. ip6gre_tunnel_link(ign, nt);
  1135. out:
  1136. return err;
  1137. }
  1138. static int ip6gre_changelink(struct net_device *dev, struct nlattr *tb[],
  1139. struct nlattr *data[])
  1140. {
  1141. struct ip6_tnl *t, *nt = netdev_priv(dev);
  1142. struct net *net = nt->net;
  1143. struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
  1144. struct __ip6_tnl_parm p;
  1145. struct ip_tunnel_encap ipencap;
  1146. if (dev == ign->fb_tunnel_dev)
  1147. return -EINVAL;
  1148. if (ip6gre_netlink_encap_parms(data, &ipencap)) {
  1149. int err = ip6_tnl_encap_setup(nt, &ipencap);
  1150. if (err < 0)
  1151. return err;
  1152. }
  1153. ip6gre_netlink_parms(data, &p);
  1154. t = ip6gre_tunnel_locate(net, &p, 0);
  1155. if (t) {
  1156. if (t->dev != dev)
  1157. return -EEXIST;
  1158. } else {
  1159. t = nt;
  1160. }
  1161. ip6gre_tunnel_unlink(ign, t);
  1162. ip6gre_tnl_change(t, &p, !tb[IFLA_MTU]);
  1163. ip6gre_tunnel_link(ign, t);
  1164. return 0;
  1165. }
  1166. static void ip6gre_dellink(struct net_device *dev, struct list_head *head)
  1167. {
  1168. struct net *net = dev_net(dev);
  1169. struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
  1170. if (dev != ign->fb_tunnel_dev)
  1171. unregister_netdevice_queue(dev, head);
  1172. }
  1173. static size_t ip6gre_get_size(const struct net_device *dev)
  1174. {
  1175. return
  1176. /* IFLA_GRE_LINK */
  1177. nla_total_size(4) +
  1178. /* IFLA_GRE_IFLAGS */
  1179. nla_total_size(2) +
  1180. /* IFLA_GRE_OFLAGS */
  1181. nla_total_size(2) +
  1182. /* IFLA_GRE_IKEY */
  1183. nla_total_size(4) +
  1184. /* IFLA_GRE_OKEY */
  1185. nla_total_size(4) +
  1186. /* IFLA_GRE_LOCAL */
  1187. nla_total_size(sizeof(struct in6_addr)) +
  1188. /* IFLA_GRE_REMOTE */
  1189. nla_total_size(sizeof(struct in6_addr)) +
  1190. /* IFLA_GRE_TTL */
  1191. nla_total_size(1) +
  1192. /* IFLA_GRE_ENCAP_LIMIT */
  1193. nla_total_size(1) +
  1194. /* IFLA_GRE_FLOWINFO */
  1195. nla_total_size(4) +
  1196. /* IFLA_GRE_FLAGS */
  1197. nla_total_size(4) +
  1198. /* IFLA_GRE_ENCAP_TYPE */
  1199. nla_total_size(2) +
  1200. /* IFLA_GRE_ENCAP_FLAGS */
  1201. nla_total_size(2) +
  1202. /* IFLA_GRE_ENCAP_SPORT */
  1203. nla_total_size(2) +
  1204. /* IFLA_GRE_ENCAP_DPORT */
  1205. nla_total_size(2) +
  1206. 0;
  1207. }
  1208. static int ip6gre_fill_info(struct sk_buff *skb, const struct net_device *dev)
  1209. {
  1210. struct ip6_tnl *t = netdev_priv(dev);
  1211. struct __ip6_tnl_parm *p = &t->parms;
  1212. if (nla_put_u32(skb, IFLA_GRE_LINK, p->link) ||
  1213. nla_put_be16(skb, IFLA_GRE_IFLAGS,
  1214. gre_tnl_flags_to_gre_flags(p->i_flags)) ||
  1215. nla_put_be16(skb, IFLA_GRE_OFLAGS,
  1216. gre_tnl_flags_to_gre_flags(p->o_flags)) ||
  1217. nla_put_be32(skb, IFLA_GRE_IKEY, p->i_key) ||
  1218. nla_put_be32(skb, IFLA_GRE_OKEY, p->o_key) ||
  1219. nla_put_in6_addr(skb, IFLA_GRE_LOCAL, &p->laddr) ||
  1220. nla_put_in6_addr(skb, IFLA_GRE_REMOTE, &p->raddr) ||
  1221. nla_put_u8(skb, IFLA_GRE_TTL, p->hop_limit) ||
  1222. nla_put_u8(skb, IFLA_GRE_ENCAP_LIMIT, p->encap_limit) ||
  1223. nla_put_be32(skb, IFLA_GRE_FLOWINFO, p->flowinfo) ||
  1224. nla_put_u32(skb, IFLA_GRE_FLAGS, p->flags))
  1225. goto nla_put_failure;
  1226. if (nla_put_u16(skb, IFLA_GRE_ENCAP_TYPE,
  1227. t->encap.type) ||
  1228. nla_put_be16(skb, IFLA_GRE_ENCAP_SPORT,
  1229. t->encap.sport) ||
  1230. nla_put_be16(skb, IFLA_GRE_ENCAP_DPORT,
  1231. t->encap.dport) ||
  1232. nla_put_u16(skb, IFLA_GRE_ENCAP_FLAGS,
  1233. t->encap.flags))
  1234. goto nla_put_failure;
  1235. return 0;
  1236. nla_put_failure:
  1237. return -EMSGSIZE;
  1238. }
  1239. static const struct nla_policy ip6gre_policy[IFLA_GRE_MAX + 1] = {
  1240. [IFLA_GRE_LINK] = { .type = NLA_U32 },
  1241. [IFLA_GRE_IFLAGS] = { .type = NLA_U16 },
  1242. [IFLA_GRE_OFLAGS] = { .type = NLA_U16 },
  1243. [IFLA_GRE_IKEY] = { .type = NLA_U32 },
  1244. [IFLA_GRE_OKEY] = { .type = NLA_U32 },
  1245. [IFLA_GRE_LOCAL] = { .len = FIELD_SIZEOF(struct ipv6hdr, saddr) },
  1246. [IFLA_GRE_REMOTE] = { .len = FIELD_SIZEOF(struct ipv6hdr, daddr) },
  1247. [IFLA_GRE_TTL] = { .type = NLA_U8 },
  1248. [IFLA_GRE_ENCAP_LIMIT] = { .type = NLA_U8 },
  1249. [IFLA_GRE_FLOWINFO] = { .type = NLA_U32 },
  1250. [IFLA_GRE_FLAGS] = { .type = NLA_U32 },
  1251. [IFLA_GRE_ENCAP_TYPE] = { .type = NLA_U16 },
  1252. [IFLA_GRE_ENCAP_FLAGS] = { .type = NLA_U16 },
  1253. [IFLA_GRE_ENCAP_SPORT] = { .type = NLA_U16 },
  1254. [IFLA_GRE_ENCAP_DPORT] = { .type = NLA_U16 },
  1255. };
  1256. static struct rtnl_link_ops ip6gre_link_ops __read_mostly = {
  1257. .kind = "ip6gre",
  1258. .maxtype = IFLA_GRE_MAX,
  1259. .policy = ip6gre_policy,
  1260. .priv_size = sizeof(struct ip6_tnl),
  1261. .setup = ip6gre_tunnel_setup,
  1262. .validate = ip6gre_tunnel_validate,
  1263. .newlink = ip6gre_newlink,
  1264. .changelink = ip6gre_changelink,
  1265. .dellink = ip6gre_dellink,
  1266. .get_size = ip6gre_get_size,
  1267. .fill_info = ip6gre_fill_info,
  1268. .get_link_net = ip6_tnl_get_link_net,
  1269. };
  1270. static struct rtnl_link_ops ip6gre_tap_ops __read_mostly = {
  1271. .kind = "ip6gretap",
  1272. .maxtype = IFLA_GRE_MAX,
  1273. .policy = ip6gre_policy,
  1274. .priv_size = sizeof(struct ip6_tnl),
  1275. .setup = ip6gre_tap_setup,
  1276. .validate = ip6gre_tap_validate,
  1277. .newlink = ip6gre_newlink,
  1278. .changelink = ip6gre_changelink,
  1279. .get_size = ip6gre_get_size,
  1280. .fill_info = ip6gre_fill_info,
  1281. .get_link_net = ip6_tnl_get_link_net,
  1282. };
  1283. /*
  1284. * And now the modules code and kernel interface.
  1285. */
  1286. static int __init ip6gre_init(void)
  1287. {
  1288. int err;
  1289. pr_info("GRE over IPv6 tunneling driver\n");
  1290. err = register_pernet_device(&ip6gre_net_ops);
  1291. if (err < 0)
  1292. return err;
  1293. err = inet6_add_protocol(&ip6gre_protocol, IPPROTO_GRE);
  1294. if (err < 0) {
  1295. pr_info("%s: can't add protocol\n", __func__);
  1296. goto add_proto_failed;
  1297. }
  1298. err = rtnl_link_register(&ip6gre_link_ops);
  1299. if (err < 0)
  1300. goto rtnl_link_failed;
  1301. err = rtnl_link_register(&ip6gre_tap_ops);
  1302. if (err < 0)
  1303. goto tap_ops_failed;
  1304. out:
  1305. return err;
  1306. tap_ops_failed:
  1307. rtnl_link_unregister(&ip6gre_link_ops);
  1308. rtnl_link_failed:
  1309. inet6_del_protocol(&ip6gre_protocol, IPPROTO_GRE);
  1310. add_proto_failed:
  1311. unregister_pernet_device(&ip6gre_net_ops);
  1312. goto out;
  1313. }
  1314. static void __exit ip6gre_fini(void)
  1315. {
  1316. rtnl_link_unregister(&ip6gre_tap_ops);
  1317. rtnl_link_unregister(&ip6gre_link_ops);
  1318. inet6_del_protocol(&ip6gre_protocol, IPPROTO_GRE);
  1319. unregister_pernet_device(&ip6gre_net_ops);
  1320. }
  1321. module_init(ip6gre_init);
  1322. module_exit(ip6gre_fini);
  1323. MODULE_LICENSE("GPL");
  1324. MODULE_AUTHOR("D. Kozlov (xeb@mail.ru)");
  1325. MODULE_DESCRIPTION("GRE over IPv6 tunneling device");
  1326. MODULE_ALIAS_RTNL_LINK("ip6gre");
  1327. MODULE_ALIAS_RTNL_LINK("ip6gretap");
  1328. MODULE_ALIAS_NETDEV("ip6gre0");