sit.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602
  1. /*
  2. * IPv6 over IPv4 tunnel device - Simple Internet Transition (SIT)
  3. * Linux INET6 implementation
  4. *
  5. * Authors:
  6. * Pedro Roque <roque@di.fc.ul.pt>
  7. * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License
  11. * as published by the Free Software Foundation; either version
  12. * 2 of the License, or (at your option) any later version.
  13. *
  14. * Changes:
  15. * Roger Venning <r.venning@telstra.com>: 6to4 support
  16. * Nate Thompson <nate@thebog.net>: 6to4 support
  17. * Fred Templin <fred.l.templin@boeing.com>: isatap support
  18. */
  19. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  20. #include <linux/module.h>
  21. #include <linux/capability.h>
  22. #include <linux/errno.h>
  23. #include <linux/types.h>
  24. #include <linux/socket.h>
  25. #include <linux/sockios.h>
  26. #include <linux/net.h>
  27. #include <linux/in6.h>
  28. #include <linux/netdevice.h>
  29. #include <linux/if_arp.h>
  30. #include <linux/icmp.h>
  31. #include <linux/slab.h>
  32. #include <asm/uaccess.h>
  33. #include <linux/init.h>
  34. #include <linux/netfilter_ipv4.h>
  35. #include <linux/if_ether.h>
  36. #include <net/sock.h>
  37. #include <net/snmp.h>
  38. #include <net/ipv6.h>
  39. #include <net/protocol.h>
  40. #include <net/transp_v6.h>
  41. #include <net/ip6_fib.h>
  42. #include <net/ip6_route.h>
  43. #include <net/ndisc.h>
  44. #include <net/addrconf.h>
  45. #include <net/ip.h>
  46. #include <net/udp.h>
  47. #include <net/icmp.h>
  48. #include <net/ipip.h>
  49. #include <net/inet_ecn.h>
  50. #include <net/xfrm.h>
  51. #include <net/dsfield.h>
  52. #include <net/net_namespace.h>
  53. #include <net/netns/generic.h>
  54. /*
  55. This version of net/ipv6/sit.c is cloned of net/ipv4/ip_gre.c
  56. For comments look at net/ipv4/ip_gre.c --ANK
  57. */
  58. #define HASH_SIZE 16
  59. #define HASH(addr) (((__force u32)addr^((__force u32)addr>>4))&0xF)
  60. static bool log_ecn_error = true;
  61. module_param(log_ecn_error, bool, 0644);
  62. MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN");
  63. static int ipip6_tunnel_init(struct net_device *dev);
  64. static void ipip6_tunnel_setup(struct net_device *dev);
  65. static void ipip6_dev_free(struct net_device *dev);
  66. static struct rtnl_link_ops sit_link_ops __read_mostly;
  67. static int sit_net_id __read_mostly;
  68. struct sit_net {
  69. struct ip_tunnel __rcu *tunnels_r_l[HASH_SIZE];
  70. struct ip_tunnel __rcu *tunnels_r[HASH_SIZE];
  71. struct ip_tunnel __rcu *tunnels_l[HASH_SIZE];
  72. struct ip_tunnel __rcu *tunnels_wc[1];
  73. struct ip_tunnel __rcu **tunnels[4];
  74. struct net_device *fb_tunnel_dev;
  75. };
  76. static struct rtnl_link_stats64 *ipip6_get_stats64(struct net_device *dev,
  77. struct rtnl_link_stats64 *tot)
  78. {
  79. int i;
  80. for_each_possible_cpu(i) {
  81. const struct pcpu_tstats *tstats = per_cpu_ptr(dev->tstats, i);
  82. u64 rx_packets, rx_bytes, tx_packets, tx_bytes;
  83. unsigned int start;
  84. do {
  85. start = u64_stats_fetch_begin_bh(&tstats->syncp);
  86. rx_packets = tstats->rx_packets;
  87. tx_packets = tstats->tx_packets;
  88. rx_bytes = tstats->rx_bytes;
  89. tx_bytes = tstats->tx_bytes;
  90. } while (u64_stats_fetch_retry_bh(&tstats->syncp, start));
  91. tot->rx_packets += rx_packets;
  92. tot->tx_packets += tx_packets;
  93. tot->rx_bytes += rx_bytes;
  94. tot->tx_bytes += tx_bytes;
  95. }
  96. tot->rx_errors = dev->stats.rx_errors;
  97. tot->rx_frame_errors = dev->stats.rx_frame_errors;
  98. tot->tx_fifo_errors = dev->stats.tx_fifo_errors;
  99. tot->tx_carrier_errors = dev->stats.tx_carrier_errors;
  100. tot->tx_dropped = dev->stats.tx_dropped;
  101. tot->tx_aborted_errors = dev->stats.tx_aborted_errors;
  102. tot->tx_errors = dev->stats.tx_errors;
  103. return tot;
  104. }
  105. /*
  106. * Must be invoked with rcu_read_lock
  107. */
  108. static struct ip_tunnel *ipip6_tunnel_lookup(struct net *net,
  109. struct net_device *dev, __be32 remote, __be32 local)
  110. {
  111. unsigned int h0 = HASH(remote);
  112. unsigned int h1 = HASH(local);
  113. struct ip_tunnel *t;
  114. struct sit_net *sitn = net_generic(net, sit_net_id);
  115. for_each_ip_tunnel_rcu(t, sitn->tunnels_r_l[h0 ^ h1]) {
  116. if (local == t->parms.iph.saddr &&
  117. remote == t->parms.iph.daddr &&
  118. (!dev || !t->parms.link || dev->iflink == t->parms.link) &&
  119. (t->dev->flags & IFF_UP))
  120. return t;
  121. }
  122. for_each_ip_tunnel_rcu(t, sitn->tunnels_r[h0]) {
  123. if (remote == t->parms.iph.daddr &&
  124. (!dev || !t->parms.link || dev->iflink == t->parms.link) &&
  125. (t->dev->flags & IFF_UP))
  126. return t;
  127. }
  128. for_each_ip_tunnel_rcu(t, sitn->tunnels_l[h1]) {
  129. if (local == t->parms.iph.saddr &&
  130. (!dev || !t->parms.link || dev->iflink == t->parms.link) &&
  131. (t->dev->flags & IFF_UP))
  132. return t;
  133. }
  134. t = rcu_dereference(sitn->tunnels_wc[0]);
  135. if ((t != NULL) && (t->dev->flags & IFF_UP))
  136. return t;
  137. return NULL;
  138. }
  139. static struct ip_tunnel __rcu **__ipip6_bucket(struct sit_net *sitn,
  140. struct ip_tunnel_parm *parms)
  141. {
  142. __be32 remote = parms->iph.daddr;
  143. __be32 local = parms->iph.saddr;
  144. unsigned int h = 0;
  145. int prio = 0;
  146. if (remote) {
  147. prio |= 2;
  148. h ^= HASH(remote);
  149. }
  150. if (local) {
  151. prio |= 1;
  152. h ^= HASH(local);
  153. }
  154. return &sitn->tunnels[prio][h];
  155. }
  156. static inline struct ip_tunnel __rcu **ipip6_bucket(struct sit_net *sitn,
  157. struct ip_tunnel *t)
  158. {
  159. return __ipip6_bucket(sitn, &t->parms);
  160. }
  161. static void ipip6_tunnel_unlink(struct sit_net *sitn, struct ip_tunnel *t)
  162. {
  163. struct ip_tunnel __rcu **tp;
  164. struct ip_tunnel *iter;
  165. for (tp = ipip6_bucket(sitn, t);
  166. (iter = rtnl_dereference(*tp)) != NULL;
  167. tp = &iter->next) {
  168. if (t == iter) {
  169. rcu_assign_pointer(*tp, t->next);
  170. break;
  171. }
  172. }
  173. }
  174. static void ipip6_tunnel_link(struct sit_net *sitn, struct ip_tunnel *t)
  175. {
  176. struct ip_tunnel __rcu **tp = ipip6_bucket(sitn, t);
  177. rcu_assign_pointer(t->next, rtnl_dereference(*tp));
  178. rcu_assign_pointer(*tp, t);
  179. }
  180. static void ipip6_tunnel_clone_6rd(struct net_device *dev, struct sit_net *sitn)
  181. {
  182. #ifdef CONFIG_IPV6_SIT_6RD
  183. struct ip_tunnel *t = netdev_priv(dev);
  184. if (t->dev == sitn->fb_tunnel_dev) {
  185. ipv6_addr_set(&t->ip6rd.prefix, htonl(0x20020000), 0, 0, 0);
  186. t->ip6rd.relay_prefix = 0;
  187. t->ip6rd.prefixlen = 16;
  188. t->ip6rd.relay_prefixlen = 0;
  189. } else {
  190. struct ip_tunnel *t0 = netdev_priv(sitn->fb_tunnel_dev);
  191. memcpy(&t->ip6rd, &t0->ip6rd, sizeof(t->ip6rd));
  192. }
  193. #endif
  194. }
  195. static int ipip6_tunnel_create(struct net_device *dev)
  196. {
  197. struct ip_tunnel *t = netdev_priv(dev);
  198. struct net *net = dev_net(dev);
  199. struct sit_net *sitn = net_generic(net, sit_net_id);
  200. int err;
  201. err = ipip6_tunnel_init(dev);
  202. if (err < 0)
  203. goto out;
  204. ipip6_tunnel_clone_6rd(dev, sitn);
  205. if ((__force u16)t->parms.i_flags & SIT_ISATAP)
  206. dev->priv_flags |= IFF_ISATAP;
  207. err = register_netdevice(dev);
  208. if (err < 0)
  209. goto out;
  210. strcpy(t->parms.name, dev->name);
  211. dev->rtnl_link_ops = &sit_link_ops;
  212. dev_hold(dev);
  213. ipip6_tunnel_link(sitn, t);
  214. return 0;
  215. out:
  216. return err;
  217. }
  218. static struct ip_tunnel *ipip6_tunnel_locate(struct net *net,
  219. struct ip_tunnel_parm *parms, int create)
  220. {
  221. __be32 remote = parms->iph.daddr;
  222. __be32 local = parms->iph.saddr;
  223. struct ip_tunnel *t, *nt;
  224. struct ip_tunnel __rcu **tp;
  225. struct net_device *dev;
  226. char name[IFNAMSIZ];
  227. struct sit_net *sitn = net_generic(net, sit_net_id);
  228. for (tp = __ipip6_bucket(sitn, parms);
  229. (t = rtnl_dereference(*tp)) != NULL;
  230. tp = &t->next) {
  231. if (local == t->parms.iph.saddr &&
  232. remote == t->parms.iph.daddr &&
  233. parms->link == t->parms.link) {
  234. if (create)
  235. return NULL;
  236. else
  237. return t;
  238. }
  239. }
  240. if (!create)
  241. goto failed;
  242. if (parms->name[0])
  243. strlcpy(name, parms->name, IFNAMSIZ);
  244. else
  245. strcpy(name, "sit%d");
  246. dev = alloc_netdev(sizeof(*t), name, ipip6_tunnel_setup);
  247. if (dev == NULL)
  248. return NULL;
  249. dev_net_set(dev, net);
  250. nt = netdev_priv(dev);
  251. nt->parms = *parms;
  252. if (ipip6_tunnel_create(dev) < 0)
  253. goto failed_free;
  254. return nt;
  255. failed_free:
  256. ipip6_dev_free(dev);
  257. failed:
  258. return NULL;
  259. }
  260. #define for_each_prl_rcu(start) \
  261. for (prl = rcu_dereference(start); \
  262. prl; \
  263. prl = rcu_dereference(prl->next))
  264. static struct ip_tunnel_prl_entry *
  265. __ipip6_tunnel_locate_prl(struct ip_tunnel *t, __be32 addr)
  266. {
  267. struct ip_tunnel_prl_entry *prl;
  268. for_each_prl_rcu(t->prl)
  269. if (prl->addr == addr)
  270. break;
  271. return prl;
  272. }
  273. static int ipip6_tunnel_get_prl(struct ip_tunnel *t,
  274. struct ip_tunnel_prl __user *a)
  275. {
  276. struct ip_tunnel_prl kprl, *kp;
  277. struct ip_tunnel_prl_entry *prl;
  278. unsigned int cmax, c = 0, ca, len;
  279. int ret = 0;
  280. if (copy_from_user(&kprl, a, sizeof(kprl)))
  281. return -EFAULT;
  282. cmax = kprl.datalen / sizeof(kprl);
  283. if (cmax > 1 && kprl.addr != htonl(INADDR_ANY))
  284. cmax = 1;
  285. /* For simple GET or for root users,
  286. * we try harder to allocate.
  287. */
  288. kp = (cmax <= 1 || capable(CAP_NET_ADMIN)) ?
  289. kcalloc(cmax, sizeof(*kp), GFP_KERNEL) :
  290. NULL;
  291. rcu_read_lock();
  292. ca = t->prl_count < cmax ? t->prl_count : cmax;
  293. if (!kp) {
  294. /* We don't try hard to allocate much memory for
  295. * non-root users.
  296. * For root users, retry allocating enough memory for
  297. * the answer.
  298. */
  299. kp = kcalloc(ca, sizeof(*kp), GFP_ATOMIC);
  300. if (!kp) {
  301. ret = -ENOMEM;
  302. goto out;
  303. }
  304. }
  305. c = 0;
  306. for_each_prl_rcu(t->prl) {
  307. if (c >= cmax)
  308. break;
  309. if (kprl.addr != htonl(INADDR_ANY) && prl->addr != kprl.addr)
  310. continue;
  311. kp[c].addr = prl->addr;
  312. kp[c].flags = prl->flags;
  313. c++;
  314. if (kprl.addr != htonl(INADDR_ANY))
  315. break;
  316. }
  317. out:
  318. rcu_read_unlock();
  319. len = sizeof(*kp) * c;
  320. ret = 0;
  321. if ((len && copy_to_user(a + 1, kp, len)) || put_user(len, &a->datalen))
  322. ret = -EFAULT;
  323. kfree(kp);
  324. return ret;
  325. }
  326. static int
  327. ipip6_tunnel_add_prl(struct ip_tunnel *t, struct ip_tunnel_prl *a, int chg)
  328. {
  329. struct ip_tunnel_prl_entry *p;
  330. int err = 0;
  331. if (a->addr == htonl(INADDR_ANY))
  332. return -EINVAL;
  333. ASSERT_RTNL();
  334. for (p = rtnl_dereference(t->prl); p; p = rtnl_dereference(p->next)) {
  335. if (p->addr == a->addr) {
  336. if (chg) {
  337. p->flags = a->flags;
  338. goto out;
  339. }
  340. err = -EEXIST;
  341. goto out;
  342. }
  343. }
  344. if (chg) {
  345. err = -ENXIO;
  346. goto out;
  347. }
  348. p = kzalloc(sizeof(struct ip_tunnel_prl_entry), GFP_KERNEL);
  349. if (!p) {
  350. err = -ENOBUFS;
  351. goto out;
  352. }
  353. p->next = t->prl;
  354. p->addr = a->addr;
  355. p->flags = a->flags;
  356. t->prl_count++;
  357. rcu_assign_pointer(t->prl, p);
  358. out:
  359. return err;
  360. }
  361. static void prl_list_destroy_rcu(struct rcu_head *head)
  362. {
  363. struct ip_tunnel_prl_entry *p, *n;
  364. p = container_of(head, struct ip_tunnel_prl_entry, rcu_head);
  365. do {
  366. n = rcu_dereference_protected(p->next, 1);
  367. kfree(p);
  368. p = n;
  369. } while (p);
  370. }
  371. static int
  372. ipip6_tunnel_del_prl(struct ip_tunnel *t, struct ip_tunnel_prl *a)
  373. {
  374. struct ip_tunnel_prl_entry *x;
  375. struct ip_tunnel_prl_entry __rcu **p;
  376. int err = 0;
  377. ASSERT_RTNL();
  378. if (a && a->addr != htonl(INADDR_ANY)) {
  379. for (p = &t->prl;
  380. (x = rtnl_dereference(*p)) != NULL;
  381. p = &x->next) {
  382. if (x->addr == a->addr) {
  383. *p = x->next;
  384. kfree_rcu(x, rcu_head);
  385. t->prl_count--;
  386. goto out;
  387. }
  388. }
  389. err = -ENXIO;
  390. } else {
  391. x = rtnl_dereference(t->prl);
  392. if (x) {
  393. t->prl_count = 0;
  394. call_rcu(&x->rcu_head, prl_list_destroy_rcu);
  395. t->prl = NULL;
  396. }
  397. }
  398. out:
  399. return err;
  400. }
  401. static int
  402. isatap_chksrc(struct sk_buff *skb, const struct iphdr *iph, struct ip_tunnel *t)
  403. {
  404. struct ip_tunnel_prl_entry *p;
  405. int ok = 1;
  406. rcu_read_lock();
  407. p = __ipip6_tunnel_locate_prl(t, iph->saddr);
  408. if (p) {
  409. if (p->flags & PRL_DEFAULT)
  410. skb->ndisc_nodetype = NDISC_NODETYPE_DEFAULT;
  411. else
  412. skb->ndisc_nodetype = NDISC_NODETYPE_NODEFAULT;
  413. } else {
  414. const struct in6_addr *addr6 = &ipv6_hdr(skb)->saddr;
  415. if (ipv6_addr_is_isatap(addr6) &&
  416. (addr6->s6_addr32[3] == iph->saddr) &&
  417. ipv6_chk_prefix(addr6, t->dev))
  418. skb->ndisc_nodetype = NDISC_NODETYPE_HOST;
  419. else
  420. ok = 0;
  421. }
  422. rcu_read_unlock();
  423. return ok;
  424. }
  425. static void ipip6_tunnel_uninit(struct net_device *dev)
  426. {
  427. struct net *net = dev_net(dev);
  428. struct sit_net *sitn = net_generic(net, sit_net_id);
  429. if (dev == sitn->fb_tunnel_dev) {
  430. RCU_INIT_POINTER(sitn->tunnels_wc[0], NULL);
  431. } else {
  432. ipip6_tunnel_unlink(sitn, netdev_priv(dev));
  433. ipip6_tunnel_del_prl(netdev_priv(dev), NULL);
  434. }
  435. dev_put(dev);
  436. }
  437. static int ipip6_err(struct sk_buff *skb, u32 info)
  438. {
  439. /* All the routers (except for Linux) return only
  440. 8 bytes of packet payload. It means, that precise relaying of
  441. ICMP in the real Internet is absolutely infeasible.
  442. */
  443. const struct iphdr *iph = (const struct iphdr *)skb->data;
  444. const int type = icmp_hdr(skb)->type;
  445. const int code = icmp_hdr(skb)->code;
  446. struct ip_tunnel *t;
  447. int err;
  448. switch (type) {
  449. default:
  450. case ICMP_PARAMETERPROB:
  451. return 0;
  452. case ICMP_DEST_UNREACH:
  453. switch (code) {
  454. case ICMP_SR_FAILED:
  455. case ICMP_PORT_UNREACH:
  456. /* Impossible event. */
  457. return 0;
  458. default:
  459. /* All others are translated to HOST_UNREACH.
  460. rfc2003 contains "deep thoughts" about NET_UNREACH,
  461. I believe they are just ether pollution. --ANK
  462. */
  463. break;
  464. }
  465. break;
  466. case ICMP_TIME_EXCEEDED:
  467. if (code != ICMP_EXC_TTL)
  468. return 0;
  469. break;
  470. case ICMP_REDIRECT:
  471. break;
  472. }
  473. err = -ENOENT;
  474. t = ipip6_tunnel_lookup(dev_net(skb->dev),
  475. skb->dev,
  476. iph->daddr,
  477. iph->saddr);
  478. if (t == NULL)
  479. goto out;
  480. if (type == ICMP_DEST_UNREACH && code == ICMP_FRAG_NEEDED) {
  481. ipv4_update_pmtu(skb, dev_net(skb->dev), info,
  482. t->dev->ifindex, 0, IPPROTO_IPV6, 0);
  483. err = 0;
  484. goto out;
  485. }
  486. if (type == ICMP_REDIRECT) {
  487. ipv4_redirect(skb, dev_net(skb->dev), t->dev->ifindex, 0,
  488. IPPROTO_IPV6, 0);
  489. err = 0;
  490. goto out;
  491. }
  492. if (t->parms.iph.daddr == 0)
  493. goto out;
  494. err = 0;
  495. if (t->parms.iph.ttl == 0 && type == ICMP_TIME_EXCEEDED)
  496. goto out;
  497. if (time_before(jiffies, t->err_time + IPTUNNEL_ERR_TIMEO))
  498. t->err_count++;
  499. else
  500. t->err_count = 1;
  501. t->err_time = jiffies;
  502. out:
  503. return err;
  504. }
  505. static int ipip6_rcv(struct sk_buff *skb)
  506. {
  507. const struct iphdr *iph;
  508. struct ip_tunnel *tunnel;
  509. int err;
  510. if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
  511. goto out;
  512. iph = ip_hdr(skb);
  513. tunnel = ipip6_tunnel_lookup(dev_net(skb->dev), skb->dev,
  514. iph->saddr, iph->daddr);
  515. if (tunnel != NULL) {
  516. struct pcpu_tstats *tstats;
  517. secpath_reset(skb);
  518. skb->mac_header = skb->network_header;
  519. skb_reset_network_header(skb);
  520. IPCB(skb)->flags = 0;
  521. skb->protocol = htons(ETH_P_IPV6);
  522. skb->pkt_type = PACKET_HOST;
  523. if ((tunnel->dev->priv_flags & IFF_ISATAP) &&
  524. !isatap_chksrc(skb, iph, tunnel)) {
  525. tunnel->dev->stats.rx_errors++;
  526. goto out;
  527. }
  528. __skb_tunnel_rx(skb, tunnel->dev);
  529. err = IP_ECN_decapsulate(iph, skb);
  530. if (unlikely(err)) {
  531. if (log_ecn_error)
  532. net_info_ratelimited("non-ECT from %pI4 with TOS=%#x\n",
  533. &iph->saddr, iph->tos);
  534. if (err > 1) {
  535. ++tunnel->dev->stats.rx_frame_errors;
  536. ++tunnel->dev->stats.rx_errors;
  537. goto out;
  538. }
  539. }
  540. tstats = this_cpu_ptr(tunnel->dev->tstats);
  541. tstats->rx_packets++;
  542. tstats->rx_bytes += skb->len;
  543. netif_rx(skb);
  544. return 0;
  545. }
  546. /* no tunnel matched, let upstream know, ipsec may handle it */
  547. return 1;
  548. out:
  549. kfree_skb(skb);
  550. return 0;
  551. }
  552. /*
  553. * Returns the embedded IPv4 address if the IPv6 address
  554. * comes from 6rd / 6to4 (RFC 3056) addr space.
  555. */
  556. static inline
  557. __be32 try_6rd(const struct in6_addr *v6dst, struct ip_tunnel *tunnel)
  558. {
  559. __be32 dst = 0;
  560. #ifdef CONFIG_IPV6_SIT_6RD
  561. if (ipv6_prefix_equal(v6dst, &tunnel->ip6rd.prefix,
  562. tunnel->ip6rd.prefixlen)) {
  563. unsigned int pbw0, pbi0;
  564. int pbi1;
  565. u32 d;
  566. pbw0 = tunnel->ip6rd.prefixlen >> 5;
  567. pbi0 = tunnel->ip6rd.prefixlen & 0x1f;
  568. d = (ntohl(v6dst->s6_addr32[pbw0]) << pbi0) >>
  569. tunnel->ip6rd.relay_prefixlen;
  570. pbi1 = pbi0 - tunnel->ip6rd.relay_prefixlen;
  571. if (pbi1 > 0)
  572. d |= ntohl(v6dst->s6_addr32[pbw0 + 1]) >>
  573. (32 - pbi1);
  574. dst = tunnel->ip6rd.relay_prefix | htonl(d);
  575. }
  576. #else
  577. if (v6dst->s6_addr16[0] == htons(0x2002)) {
  578. /* 6to4 v6 addr has 16 bits prefix, 32 v4addr, 16 SLA, ... */
  579. memcpy(&dst, &v6dst->s6_addr16[1], 4);
  580. }
  581. #endif
  582. return dst;
  583. }
  584. /*
  585. * This function assumes it is being called from dev_queue_xmit()
  586. * and that skb is filled properly by that function.
  587. */
  588. static netdev_tx_t ipip6_tunnel_xmit(struct sk_buff *skb,
  589. struct net_device *dev)
  590. {
  591. struct ip_tunnel *tunnel = netdev_priv(dev);
  592. const struct iphdr *tiph = &tunnel->parms.iph;
  593. const struct ipv6hdr *iph6 = ipv6_hdr(skb);
  594. u8 tos = tunnel->parms.iph.tos;
  595. __be16 df = tiph->frag_off;
  596. struct rtable *rt; /* Route to the other host */
  597. struct net_device *tdev; /* Device to other host */
  598. struct iphdr *iph; /* Our new IP header */
  599. unsigned int max_headroom; /* The extra header space needed */
  600. __be32 dst = tiph->daddr;
  601. struct flowi4 fl4;
  602. int mtu;
  603. const struct in6_addr *addr6;
  604. int addr_type;
  605. if (skb->protocol != htons(ETH_P_IPV6))
  606. goto tx_error;
  607. if (tos == 1)
  608. tos = ipv6_get_dsfield(iph6);
  609. /* ISATAP (RFC4214) - must come before 6to4 */
  610. if (dev->priv_flags & IFF_ISATAP) {
  611. struct neighbour *neigh = NULL;
  612. bool do_tx_error = false;
  613. if (skb_dst(skb))
  614. neigh = dst_neigh_lookup(skb_dst(skb), &iph6->daddr);
  615. if (neigh == NULL) {
  616. net_dbg_ratelimited("sit: nexthop == NULL\n");
  617. goto tx_error;
  618. }
  619. addr6 = (const struct in6_addr *)&neigh->primary_key;
  620. addr_type = ipv6_addr_type(addr6);
  621. if ((addr_type & IPV6_ADDR_UNICAST) &&
  622. ipv6_addr_is_isatap(addr6))
  623. dst = addr6->s6_addr32[3];
  624. else
  625. do_tx_error = true;
  626. neigh_release(neigh);
  627. if (do_tx_error)
  628. goto tx_error;
  629. }
  630. if (!dst)
  631. dst = try_6rd(&iph6->daddr, tunnel);
  632. if (!dst) {
  633. struct neighbour *neigh = NULL;
  634. bool do_tx_error = false;
  635. if (skb_dst(skb))
  636. neigh = dst_neigh_lookup(skb_dst(skb), &iph6->daddr);
  637. if (neigh == NULL) {
  638. net_dbg_ratelimited("sit: nexthop == NULL\n");
  639. goto tx_error;
  640. }
  641. addr6 = (const struct in6_addr *)&neigh->primary_key;
  642. addr_type = ipv6_addr_type(addr6);
  643. if (addr_type == IPV6_ADDR_ANY) {
  644. addr6 = &ipv6_hdr(skb)->daddr;
  645. addr_type = ipv6_addr_type(addr6);
  646. }
  647. if ((addr_type & IPV6_ADDR_COMPATv4) != 0)
  648. dst = addr6->s6_addr32[3];
  649. else
  650. do_tx_error = true;
  651. neigh_release(neigh);
  652. if (do_tx_error)
  653. goto tx_error;
  654. }
  655. rt = ip_route_output_ports(dev_net(dev), &fl4, NULL,
  656. dst, tiph->saddr,
  657. 0, 0,
  658. IPPROTO_IPV6, RT_TOS(tos),
  659. tunnel->parms.link);
  660. if (IS_ERR(rt)) {
  661. dev->stats.tx_carrier_errors++;
  662. goto tx_error_icmp;
  663. }
  664. if (rt->rt_type != RTN_UNICAST) {
  665. ip_rt_put(rt);
  666. dev->stats.tx_carrier_errors++;
  667. goto tx_error_icmp;
  668. }
  669. tdev = rt->dst.dev;
  670. if (tdev == dev) {
  671. ip_rt_put(rt);
  672. dev->stats.collisions++;
  673. goto tx_error;
  674. }
  675. if (df) {
  676. mtu = dst_mtu(&rt->dst) - sizeof(struct iphdr);
  677. if (mtu < 68) {
  678. dev->stats.collisions++;
  679. ip_rt_put(rt);
  680. goto tx_error;
  681. }
  682. if (mtu < IPV6_MIN_MTU) {
  683. mtu = IPV6_MIN_MTU;
  684. df = 0;
  685. }
  686. if (tunnel->parms.iph.daddr && skb_dst(skb))
  687. skb_dst(skb)->ops->update_pmtu(skb_dst(skb), NULL, skb, mtu);
  688. if (skb->len > mtu) {
  689. icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
  690. ip_rt_put(rt);
  691. goto tx_error;
  692. }
  693. }
  694. if (tunnel->err_count > 0) {
  695. if (time_before(jiffies,
  696. tunnel->err_time + IPTUNNEL_ERR_TIMEO)) {
  697. tunnel->err_count--;
  698. dst_link_failure(skb);
  699. } else
  700. tunnel->err_count = 0;
  701. }
  702. /*
  703. * Okay, now see if we can stuff it in the buffer as-is.
  704. */
  705. max_headroom = LL_RESERVED_SPACE(tdev)+sizeof(struct iphdr);
  706. if (skb_headroom(skb) < max_headroom || skb_shared(skb) ||
  707. (skb_cloned(skb) && !skb_clone_writable(skb, 0))) {
  708. struct sk_buff *new_skb = skb_realloc_headroom(skb, max_headroom);
  709. if (!new_skb) {
  710. ip_rt_put(rt);
  711. dev->stats.tx_dropped++;
  712. dev_kfree_skb(skb);
  713. return NETDEV_TX_OK;
  714. }
  715. if (skb->sk)
  716. skb_set_owner_w(new_skb, skb->sk);
  717. dev_kfree_skb(skb);
  718. skb = new_skb;
  719. iph6 = ipv6_hdr(skb);
  720. }
  721. skb->transport_header = skb->network_header;
  722. skb_push(skb, sizeof(struct iphdr));
  723. skb_reset_network_header(skb);
  724. memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
  725. IPCB(skb)->flags = 0;
  726. skb_dst_drop(skb);
  727. skb_dst_set(skb, &rt->dst);
  728. /*
  729. * Push down and install the IPIP header.
  730. */
  731. iph = ip_hdr(skb);
  732. iph->version = 4;
  733. iph->ihl = sizeof(struct iphdr)>>2;
  734. iph->frag_off = df;
  735. iph->protocol = IPPROTO_IPV6;
  736. iph->tos = INET_ECN_encapsulate(tos, ipv6_get_dsfield(iph6));
  737. iph->daddr = fl4.daddr;
  738. iph->saddr = fl4.saddr;
  739. if ((iph->ttl = tiph->ttl) == 0)
  740. iph->ttl = iph6->hop_limit;
  741. iptunnel_xmit(skb, dev);
  742. return NETDEV_TX_OK;
  743. tx_error_icmp:
  744. dst_link_failure(skb);
  745. tx_error:
  746. dev->stats.tx_errors++;
  747. dev_kfree_skb(skb);
  748. return NETDEV_TX_OK;
  749. }
  750. static void ipip6_tunnel_bind_dev(struct net_device *dev)
  751. {
  752. struct net_device *tdev = NULL;
  753. struct ip_tunnel *tunnel;
  754. const struct iphdr *iph;
  755. struct flowi4 fl4;
  756. tunnel = netdev_priv(dev);
  757. iph = &tunnel->parms.iph;
  758. if (iph->daddr) {
  759. struct rtable *rt = ip_route_output_ports(dev_net(dev), &fl4, NULL,
  760. iph->daddr, iph->saddr,
  761. 0, 0,
  762. IPPROTO_IPV6,
  763. RT_TOS(iph->tos),
  764. tunnel->parms.link);
  765. if (!IS_ERR(rt)) {
  766. tdev = rt->dst.dev;
  767. ip_rt_put(rt);
  768. }
  769. dev->flags |= IFF_POINTOPOINT;
  770. }
  771. if (!tdev && tunnel->parms.link)
  772. tdev = __dev_get_by_index(dev_net(dev), tunnel->parms.link);
  773. if (tdev) {
  774. dev->hard_header_len = tdev->hard_header_len + sizeof(struct iphdr);
  775. dev->mtu = tdev->mtu - sizeof(struct iphdr);
  776. if (dev->mtu < IPV6_MIN_MTU)
  777. dev->mtu = IPV6_MIN_MTU;
  778. }
  779. dev->iflink = tunnel->parms.link;
  780. }
  781. static void ipip6_tunnel_update(struct ip_tunnel *t, struct ip_tunnel_parm *p)
  782. {
  783. struct net *net = dev_net(t->dev);
  784. struct sit_net *sitn = net_generic(net, sit_net_id);
  785. ipip6_tunnel_unlink(sitn, t);
  786. synchronize_net();
  787. t->parms.iph.saddr = p->iph.saddr;
  788. t->parms.iph.daddr = p->iph.daddr;
  789. memcpy(t->dev->dev_addr, &p->iph.saddr, 4);
  790. memcpy(t->dev->broadcast, &p->iph.daddr, 4);
  791. ipip6_tunnel_link(sitn, t);
  792. t->parms.iph.ttl = p->iph.ttl;
  793. t->parms.iph.tos = p->iph.tos;
  794. if (t->parms.link != p->link) {
  795. t->parms.link = p->link;
  796. ipip6_tunnel_bind_dev(t->dev);
  797. }
  798. netdev_state_change(t->dev);
  799. }
  800. #ifdef CONFIG_IPV6_SIT_6RD
  801. static int ipip6_tunnel_update_6rd(struct ip_tunnel *t,
  802. struct ip_tunnel_6rd *ip6rd)
  803. {
  804. struct in6_addr prefix;
  805. __be32 relay_prefix;
  806. if (ip6rd->relay_prefixlen > 32 ||
  807. ip6rd->prefixlen + (32 - ip6rd->relay_prefixlen) > 64)
  808. return -EINVAL;
  809. ipv6_addr_prefix(&prefix, &ip6rd->prefix, ip6rd->prefixlen);
  810. if (!ipv6_addr_equal(&prefix, &ip6rd->prefix))
  811. return -EINVAL;
  812. if (ip6rd->relay_prefixlen)
  813. relay_prefix = ip6rd->relay_prefix &
  814. htonl(0xffffffffUL <<
  815. (32 - ip6rd->relay_prefixlen));
  816. else
  817. relay_prefix = 0;
  818. if (relay_prefix != ip6rd->relay_prefix)
  819. return -EINVAL;
  820. t->ip6rd.prefix = prefix;
  821. t->ip6rd.relay_prefix = relay_prefix;
  822. t->ip6rd.prefixlen = ip6rd->prefixlen;
  823. t->ip6rd.relay_prefixlen = ip6rd->relay_prefixlen;
  824. netdev_state_change(t->dev);
  825. return 0;
  826. }
  827. #endif
  828. static int
  829. ipip6_tunnel_ioctl (struct net_device *dev, struct ifreq *ifr, int cmd)
  830. {
  831. int err = 0;
  832. struct ip_tunnel_parm p;
  833. struct ip_tunnel_prl prl;
  834. struct ip_tunnel *t;
  835. struct net *net = dev_net(dev);
  836. struct sit_net *sitn = net_generic(net, sit_net_id);
  837. #ifdef CONFIG_IPV6_SIT_6RD
  838. struct ip_tunnel_6rd ip6rd;
  839. #endif
  840. switch (cmd) {
  841. case SIOCGETTUNNEL:
  842. #ifdef CONFIG_IPV6_SIT_6RD
  843. case SIOCGET6RD:
  844. #endif
  845. t = NULL;
  846. if (dev == sitn->fb_tunnel_dev) {
  847. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) {
  848. err = -EFAULT;
  849. break;
  850. }
  851. t = ipip6_tunnel_locate(net, &p, 0);
  852. }
  853. if (t == NULL)
  854. t = netdev_priv(dev);
  855. err = -EFAULT;
  856. if (cmd == SIOCGETTUNNEL) {
  857. memcpy(&p, &t->parms, sizeof(p));
  858. if (copy_to_user(ifr->ifr_ifru.ifru_data, &p,
  859. sizeof(p)))
  860. goto done;
  861. #ifdef CONFIG_IPV6_SIT_6RD
  862. } else {
  863. ip6rd.prefix = t->ip6rd.prefix;
  864. ip6rd.relay_prefix = t->ip6rd.relay_prefix;
  865. ip6rd.prefixlen = t->ip6rd.prefixlen;
  866. ip6rd.relay_prefixlen = t->ip6rd.relay_prefixlen;
  867. if (copy_to_user(ifr->ifr_ifru.ifru_data, &ip6rd,
  868. sizeof(ip6rd)))
  869. goto done;
  870. #endif
  871. }
  872. err = 0;
  873. break;
  874. case SIOCADDTUNNEL:
  875. case SIOCCHGTUNNEL:
  876. err = -EPERM;
  877. if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  878. goto done;
  879. err = -EFAULT;
  880. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
  881. goto done;
  882. err = -EINVAL;
  883. if (p.iph.version != 4 || p.iph.protocol != IPPROTO_IPV6 ||
  884. p.iph.ihl != 5 || (p.iph.frag_off&htons(~IP_DF)))
  885. goto done;
  886. if (p.iph.ttl)
  887. p.iph.frag_off |= htons(IP_DF);
  888. t = ipip6_tunnel_locate(net, &p, cmd == SIOCADDTUNNEL);
  889. if (dev != sitn->fb_tunnel_dev && cmd == SIOCCHGTUNNEL) {
  890. if (t != NULL) {
  891. if (t->dev != dev) {
  892. err = -EEXIST;
  893. break;
  894. }
  895. } else {
  896. if (((dev->flags&IFF_POINTOPOINT) && !p.iph.daddr) ||
  897. (!(dev->flags&IFF_POINTOPOINT) && p.iph.daddr)) {
  898. err = -EINVAL;
  899. break;
  900. }
  901. t = netdev_priv(dev);
  902. }
  903. ipip6_tunnel_update(t, &p);
  904. }
  905. if (t) {
  906. err = 0;
  907. if (copy_to_user(ifr->ifr_ifru.ifru_data, &t->parms, sizeof(p)))
  908. err = -EFAULT;
  909. } else
  910. err = (cmd == SIOCADDTUNNEL ? -ENOBUFS : -ENOENT);
  911. break;
  912. case SIOCDELTUNNEL:
  913. err = -EPERM;
  914. if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  915. goto done;
  916. if (dev == sitn->fb_tunnel_dev) {
  917. err = -EFAULT;
  918. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
  919. goto done;
  920. err = -ENOENT;
  921. if ((t = ipip6_tunnel_locate(net, &p, 0)) == NULL)
  922. goto done;
  923. err = -EPERM;
  924. if (t == netdev_priv(sitn->fb_tunnel_dev))
  925. goto done;
  926. dev = t->dev;
  927. }
  928. unregister_netdevice(dev);
  929. err = 0;
  930. break;
  931. case SIOCGETPRL:
  932. err = -EINVAL;
  933. if (dev == sitn->fb_tunnel_dev)
  934. goto done;
  935. err = -ENOENT;
  936. if (!(t = netdev_priv(dev)))
  937. goto done;
  938. err = ipip6_tunnel_get_prl(t, ifr->ifr_ifru.ifru_data);
  939. break;
  940. case SIOCADDPRL:
  941. case SIOCDELPRL:
  942. case SIOCCHGPRL:
  943. err = -EPERM;
  944. if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  945. goto done;
  946. err = -EINVAL;
  947. if (dev == sitn->fb_tunnel_dev)
  948. goto done;
  949. err = -EFAULT;
  950. if (copy_from_user(&prl, ifr->ifr_ifru.ifru_data, sizeof(prl)))
  951. goto done;
  952. err = -ENOENT;
  953. if (!(t = netdev_priv(dev)))
  954. goto done;
  955. switch (cmd) {
  956. case SIOCDELPRL:
  957. err = ipip6_tunnel_del_prl(t, &prl);
  958. break;
  959. case SIOCADDPRL:
  960. case SIOCCHGPRL:
  961. err = ipip6_tunnel_add_prl(t, &prl, cmd == SIOCCHGPRL);
  962. break;
  963. }
  964. netdev_state_change(dev);
  965. break;
  966. #ifdef CONFIG_IPV6_SIT_6RD
  967. case SIOCADD6RD:
  968. case SIOCCHG6RD:
  969. case SIOCDEL6RD:
  970. err = -EPERM;
  971. if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  972. goto done;
  973. err = -EFAULT;
  974. if (copy_from_user(&ip6rd, ifr->ifr_ifru.ifru_data,
  975. sizeof(ip6rd)))
  976. goto done;
  977. t = netdev_priv(dev);
  978. if (cmd != SIOCDEL6RD) {
  979. err = ipip6_tunnel_update_6rd(t, &ip6rd);
  980. if (err < 0)
  981. goto done;
  982. } else
  983. ipip6_tunnel_clone_6rd(dev, sitn);
  984. err = 0;
  985. break;
  986. #endif
  987. default:
  988. err = -EINVAL;
  989. }
  990. done:
  991. return err;
  992. }
  993. static int ipip6_tunnel_change_mtu(struct net_device *dev, int new_mtu)
  994. {
  995. if (new_mtu < IPV6_MIN_MTU || new_mtu > 0xFFF8 - sizeof(struct iphdr))
  996. return -EINVAL;
  997. dev->mtu = new_mtu;
  998. return 0;
  999. }
  1000. static const struct net_device_ops ipip6_netdev_ops = {
  1001. .ndo_uninit = ipip6_tunnel_uninit,
  1002. .ndo_start_xmit = ipip6_tunnel_xmit,
  1003. .ndo_do_ioctl = ipip6_tunnel_ioctl,
  1004. .ndo_change_mtu = ipip6_tunnel_change_mtu,
  1005. .ndo_get_stats64= ipip6_get_stats64,
  1006. };
  1007. static void ipip6_dev_free(struct net_device *dev)
  1008. {
  1009. free_percpu(dev->tstats);
  1010. free_netdev(dev);
  1011. }
  1012. static void ipip6_tunnel_setup(struct net_device *dev)
  1013. {
  1014. dev->netdev_ops = &ipip6_netdev_ops;
  1015. dev->destructor = ipip6_dev_free;
  1016. dev->type = ARPHRD_SIT;
  1017. dev->hard_header_len = LL_MAX_HEADER + sizeof(struct iphdr);
  1018. dev->mtu = ETH_DATA_LEN - sizeof(struct iphdr);
  1019. dev->flags = IFF_NOARP;
  1020. dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
  1021. dev->iflink = 0;
  1022. dev->addr_len = 4;
  1023. dev->features |= NETIF_F_NETNS_LOCAL;
  1024. dev->features |= NETIF_F_LLTX;
  1025. }
  1026. static int ipip6_tunnel_init(struct net_device *dev)
  1027. {
  1028. struct ip_tunnel *tunnel = netdev_priv(dev);
  1029. tunnel->dev = dev;
  1030. memcpy(dev->dev_addr, &tunnel->parms.iph.saddr, 4);
  1031. memcpy(dev->broadcast, &tunnel->parms.iph.daddr, 4);
  1032. ipip6_tunnel_bind_dev(dev);
  1033. dev->tstats = alloc_percpu(struct pcpu_tstats);
  1034. if (!dev->tstats)
  1035. return -ENOMEM;
  1036. return 0;
  1037. }
  1038. static int __net_init ipip6_fb_tunnel_init(struct net_device *dev)
  1039. {
  1040. struct ip_tunnel *tunnel = netdev_priv(dev);
  1041. struct iphdr *iph = &tunnel->parms.iph;
  1042. struct net *net = dev_net(dev);
  1043. struct sit_net *sitn = net_generic(net, sit_net_id);
  1044. tunnel->dev = dev;
  1045. strcpy(tunnel->parms.name, dev->name);
  1046. iph->version = 4;
  1047. iph->protocol = IPPROTO_IPV6;
  1048. iph->ihl = 5;
  1049. iph->ttl = 64;
  1050. dev->tstats = alloc_percpu(struct pcpu_tstats);
  1051. if (!dev->tstats)
  1052. return -ENOMEM;
  1053. dev_hold(dev);
  1054. rcu_assign_pointer(sitn->tunnels_wc[0], tunnel);
  1055. return 0;
  1056. }
  1057. static void ipip6_netlink_parms(struct nlattr *data[],
  1058. struct ip_tunnel_parm *parms)
  1059. {
  1060. memset(parms, 0, sizeof(*parms));
  1061. parms->iph.version = 4;
  1062. parms->iph.protocol = IPPROTO_IPV6;
  1063. parms->iph.ihl = 5;
  1064. parms->iph.ttl = 64;
  1065. if (!data)
  1066. return;
  1067. if (data[IFLA_IPTUN_LINK])
  1068. parms->link = nla_get_u32(data[IFLA_IPTUN_LINK]);
  1069. if (data[IFLA_IPTUN_LOCAL])
  1070. parms->iph.saddr = nla_get_be32(data[IFLA_IPTUN_LOCAL]);
  1071. if (data[IFLA_IPTUN_REMOTE])
  1072. parms->iph.daddr = nla_get_be32(data[IFLA_IPTUN_REMOTE]);
  1073. if (data[IFLA_IPTUN_TTL]) {
  1074. parms->iph.ttl = nla_get_u8(data[IFLA_IPTUN_TTL]);
  1075. if (parms->iph.ttl)
  1076. parms->iph.frag_off = htons(IP_DF);
  1077. }
  1078. if (data[IFLA_IPTUN_TOS])
  1079. parms->iph.tos = nla_get_u8(data[IFLA_IPTUN_TOS]);
  1080. if (!data[IFLA_IPTUN_PMTUDISC] || nla_get_u8(data[IFLA_IPTUN_PMTUDISC]))
  1081. parms->iph.frag_off = htons(IP_DF);
  1082. if (data[IFLA_IPTUN_FLAGS])
  1083. parms->i_flags = nla_get_be16(data[IFLA_IPTUN_FLAGS]);
  1084. }
  1085. #ifdef CONFIG_IPV6_SIT_6RD
  1086. /* This function returns true when 6RD attributes are present in the nl msg */
  1087. static bool ipip6_netlink_6rd_parms(struct nlattr *data[],
  1088. struct ip_tunnel_6rd *ip6rd)
  1089. {
  1090. bool ret = false;
  1091. memset(ip6rd, 0, sizeof(*ip6rd));
  1092. if (!data)
  1093. return ret;
  1094. if (data[IFLA_IPTUN_6RD_PREFIX]) {
  1095. ret = true;
  1096. nla_memcpy(&ip6rd->prefix, data[IFLA_IPTUN_6RD_PREFIX],
  1097. sizeof(struct in6_addr));
  1098. }
  1099. if (data[IFLA_IPTUN_6RD_RELAY_PREFIX]) {
  1100. ret = true;
  1101. ip6rd->relay_prefix =
  1102. nla_get_be32(data[IFLA_IPTUN_6RD_RELAY_PREFIX]);
  1103. }
  1104. if (data[IFLA_IPTUN_6RD_PREFIXLEN]) {
  1105. ret = true;
  1106. ip6rd->prefixlen = nla_get_u16(data[IFLA_IPTUN_6RD_PREFIXLEN]);
  1107. }
  1108. if (data[IFLA_IPTUN_6RD_RELAY_PREFIXLEN]) {
  1109. ret = true;
  1110. ip6rd->relay_prefixlen =
  1111. nla_get_u16(data[IFLA_IPTUN_6RD_RELAY_PREFIXLEN]);
  1112. }
  1113. return ret;
  1114. }
  1115. #endif
  1116. static int ipip6_newlink(struct net *src_net, struct net_device *dev,
  1117. struct nlattr *tb[], struct nlattr *data[])
  1118. {
  1119. struct net *net = dev_net(dev);
  1120. struct ip_tunnel *nt;
  1121. #ifdef CONFIG_IPV6_SIT_6RD
  1122. struct ip_tunnel_6rd ip6rd;
  1123. #endif
  1124. int err;
  1125. nt = netdev_priv(dev);
  1126. ipip6_netlink_parms(data, &nt->parms);
  1127. if (ipip6_tunnel_locate(net, &nt->parms, 0))
  1128. return -EEXIST;
  1129. err = ipip6_tunnel_create(dev);
  1130. if (err < 0)
  1131. return err;
  1132. #ifdef CONFIG_IPV6_SIT_6RD
  1133. if (ipip6_netlink_6rd_parms(data, &ip6rd))
  1134. err = ipip6_tunnel_update_6rd(nt, &ip6rd);
  1135. #endif
  1136. return err;
  1137. }
  1138. static int ipip6_changelink(struct net_device *dev, struct nlattr *tb[],
  1139. struct nlattr *data[])
  1140. {
  1141. struct ip_tunnel *t;
  1142. struct ip_tunnel_parm p;
  1143. struct net *net = dev_net(dev);
  1144. struct sit_net *sitn = net_generic(net, sit_net_id);
  1145. #ifdef CONFIG_IPV6_SIT_6RD
  1146. struct ip_tunnel_6rd ip6rd;
  1147. #endif
  1148. if (dev == sitn->fb_tunnel_dev)
  1149. return -EINVAL;
  1150. ipip6_netlink_parms(data, &p);
  1151. if (((dev->flags & IFF_POINTOPOINT) && !p.iph.daddr) ||
  1152. (!(dev->flags & IFF_POINTOPOINT) && p.iph.daddr))
  1153. return -EINVAL;
  1154. t = ipip6_tunnel_locate(net, &p, 0);
  1155. if (t) {
  1156. if (t->dev != dev)
  1157. return -EEXIST;
  1158. } else
  1159. t = netdev_priv(dev);
  1160. ipip6_tunnel_update(t, &p);
  1161. #ifdef CONFIG_IPV6_SIT_6RD
  1162. if (ipip6_netlink_6rd_parms(data, &ip6rd))
  1163. return ipip6_tunnel_update_6rd(t, &ip6rd);
  1164. #endif
  1165. return 0;
  1166. }
  1167. static size_t ipip6_get_size(const struct net_device *dev)
  1168. {
  1169. return
  1170. /* IFLA_IPTUN_LINK */
  1171. nla_total_size(4) +
  1172. /* IFLA_IPTUN_LOCAL */
  1173. nla_total_size(4) +
  1174. /* IFLA_IPTUN_REMOTE */
  1175. nla_total_size(4) +
  1176. /* IFLA_IPTUN_TTL */
  1177. nla_total_size(1) +
  1178. /* IFLA_IPTUN_TOS */
  1179. nla_total_size(1) +
  1180. /* IFLA_IPTUN_PMTUDISC */
  1181. nla_total_size(1) +
  1182. /* IFLA_IPTUN_FLAGS */
  1183. nla_total_size(2) +
  1184. #ifdef CONFIG_IPV6_SIT_6RD
  1185. /* IFLA_IPTUN_6RD_PREFIX */
  1186. nla_total_size(sizeof(struct in6_addr)) +
  1187. /* IFLA_IPTUN_6RD_RELAY_PREFIX */
  1188. nla_total_size(4) +
  1189. /* IFLA_IPTUN_6RD_PREFIXLEN */
  1190. nla_total_size(2) +
  1191. /* IFLA_IPTUN_6RD_RELAY_PREFIXLEN */
  1192. nla_total_size(2) +
  1193. #endif
  1194. 0;
  1195. }
  1196. static int ipip6_fill_info(struct sk_buff *skb, const struct net_device *dev)
  1197. {
  1198. struct ip_tunnel *tunnel = netdev_priv(dev);
  1199. struct ip_tunnel_parm *parm = &tunnel->parms;
  1200. if (nla_put_u32(skb, IFLA_IPTUN_LINK, parm->link) ||
  1201. nla_put_be32(skb, IFLA_IPTUN_LOCAL, parm->iph.saddr) ||
  1202. nla_put_be32(skb, IFLA_IPTUN_REMOTE, parm->iph.daddr) ||
  1203. nla_put_u8(skb, IFLA_IPTUN_TTL, parm->iph.ttl) ||
  1204. nla_put_u8(skb, IFLA_IPTUN_TOS, parm->iph.tos) ||
  1205. nla_put_u8(skb, IFLA_IPTUN_PMTUDISC,
  1206. !!(parm->iph.frag_off & htons(IP_DF))) ||
  1207. nla_put_be16(skb, IFLA_IPTUN_FLAGS, parm->i_flags))
  1208. goto nla_put_failure;
  1209. #ifdef CONFIG_IPV6_SIT_6RD
  1210. if (nla_put(skb, IFLA_IPTUN_6RD_PREFIX, sizeof(struct in6_addr),
  1211. &tunnel->ip6rd.prefix) ||
  1212. nla_put_be32(skb, IFLA_IPTUN_6RD_RELAY_PREFIX,
  1213. tunnel->ip6rd.relay_prefix) ||
  1214. nla_put_u16(skb, IFLA_IPTUN_6RD_PREFIXLEN,
  1215. tunnel->ip6rd.prefixlen) ||
  1216. nla_put_u16(skb, IFLA_IPTUN_6RD_RELAY_PREFIXLEN,
  1217. tunnel->ip6rd.relay_prefixlen))
  1218. goto nla_put_failure;
  1219. #endif
  1220. return 0;
  1221. nla_put_failure:
  1222. return -EMSGSIZE;
  1223. }
  1224. static const struct nla_policy ipip6_policy[IFLA_IPTUN_MAX + 1] = {
  1225. [IFLA_IPTUN_LINK] = { .type = NLA_U32 },
  1226. [IFLA_IPTUN_LOCAL] = { .type = NLA_U32 },
  1227. [IFLA_IPTUN_REMOTE] = { .type = NLA_U32 },
  1228. [IFLA_IPTUN_TTL] = { .type = NLA_U8 },
  1229. [IFLA_IPTUN_TOS] = { .type = NLA_U8 },
  1230. [IFLA_IPTUN_PMTUDISC] = { .type = NLA_U8 },
  1231. [IFLA_IPTUN_FLAGS] = { .type = NLA_U16 },
  1232. #ifdef CONFIG_IPV6_SIT_6RD
  1233. [IFLA_IPTUN_6RD_PREFIX] = { .len = sizeof(struct in6_addr) },
  1234. [IFLA_IPTUN_6RD_RELAY_PREFIX] = { .type = NLA_U32 },
  1235. [IFLA_IPTUN_6RD_PREFIXLEN] = { .type = NLA_U16 },
  1236. [IFLA_IPTUN_6RD_RELAY_PREFIXLEN] = { .type = NLA_U16 },
  1237. #endif
  1238. };
  1239. static struct rtnl_link_ops sit_link_ops __read_mostly = {
  1240. .kind = "sit",
  1241. .maxtype = IFLA_IPTUN_MAX,
  1242. .policy = ipip6_policy,
  1243. .priv_size = sizeof(struct ip_tunnel),
  1244. .setup = ipip6_tunnel_setup,
  1245. .newlink = ipip6_newlink,
  1246. .changelink = ipip6_changelink,
  1247. .get_size = ipip6_get_size,
  1248. .fill_info = ipip6_fill_info,
  1249. };
  1250. static struct xfrm_tunnel sit_handler __read_mostly = {
  1251. .handler = ipip6_rcv,
  1252. .err_handler = ipip6_err,
  1253. .priority = 1,
  1254. };
  1255. static void __net_exit sit_destroy_tunnels(struct sit_net *sitn, struct list_head *head)
  1256. {
  1257. int prio;
  1258. for (prio = 1; prio < 4; prio++) {
  1259. int h;
  1260. for (h = 0; h < HASH_SIZE; h++) {
  1261. struct ip_tunnel *t;
  1262. t = rtnl_dereference(sitn->tunnels[prio][h]);
  1263. while (t != NULL) {
  1264. unregister_netdevice_queue(t->dev, head);
  1265. t = rtnl_dereference(t->next);
  1266. }
  1267. }
  1268. }
  1269. }
  1270. static int __net_init sit_init_net(struct net *net)
  1271. {
  1272. struct sit_net *sitn = net_generic(net, sit_net_id);
  1273. struct ip_tunnel *t;
  1274. int err;
  1275. sitn->tunnels[0] = sitn->tunnels_wc;
  1276. sitn->tunnels[1] = sitn->tunnels_l;
  1277. sitn->tunnels[2] = sitn->tunnels_r;
  1278. sitn->tunnels[3] = sitn->tunnels_r_l;
  1279. sitn->fb_tunnel_dev = alloc_netdev(sizeof(struct ip_tunnel), "sit0",
  1280. ipip6_tunnel_setup);
  1281. if (!sitn->fb_tunnel_dev) {
  1282. err = -ENOMEM;
  1283. goto err_alloc_dev;
  1284. }
  1285. dev_net_set(sitn->fb_tunnel_dev, net);
  1286. err = ipip6_fb_tunnel_init(sitn->fb_tunnel_dev);
  1287. if (err)
  1288. goto err_dev_free;
  1289. ipip6_tunnel_clone_6rd(sitn->fb_tunnel_dev, sitn);
  1290. if ((err = register_netdev(sitn->fb_tunnel_dev)))
  1291. goto err_reg_dev;
  1292. t = netdev_priv(sitn->fb_tunnel_dev);
  1293. strcpy(t->parms.name, sitn->fb_tunnel_dev->name);
  1294. return 0;
  1295. err_reg_dev:
  1296. dev_put(sitn->fb_tunnel_dev);
  1297. err_dev_free:
  1298. ipip6_dev_free(sitn->fb_tunnel_dev);
  1299. err_alloc_dev:
  1300. return err;
  1301. }
  1302. static void __net_exit sit_exit_net(struct net *net)
  1303. {
  1304. struct sit_net *sitn = net_generic(net, sit_net_id);
  1305. LIST_HEAD(list);
  1306. rtnl_lock();
  1307. sit_destroy_tunnels(sitn, &list);
  1308. unregister_netdevice_queue(sitn->fb_tunnel_dev, &list);
  1309. unregister_netdevice_many(&list);
  1310. rtnl_unlock();
  1311. }
  1312. static struct pernet_operations sit_net_ops = {
  1313. .init = sit_init_net,
  1314. .exit = sit_exit_net,
  1315. .id = &sit_net_id,
  1316. .size = sizeof(struct sit_net),
  1317. };
  1318. static void __exit sit_cleanup(void)
  1319. {
  1320. rtnl_link_unregister(&sit_link_ops);
  1321. xfrm4_tunnel_deregister(&sit_handler, AF_INET6);
  1322. unregister_pernet_device(&sit_net_ops);
  1323. rcu_barrier(); /* Wait for completion of call_rcu()'s */
  1324. }
  1325. static int __init sit_init(void)
  1326. {
  1327. int err;
  1328. pr_info("IPv6 over IPv4 tunneling driver\n");
  1329. err = register_pernet_device(&sit_net_ops);
  1330. if (err < 0)
  1331. return err;
  1332. err = xfrm4_tunnel_register(&sit_handler, AF_INET6);
  1333. if (err < 0) {
  1334. pr_info("%s: can't add protocol\n", __func__);
  1335. goto xfrm_tunnel_failed;
  1336. }
  1337. err = rtnl_link_register(&sit_link_ops);
  1338. if (err < 0)
  1339. goto rtnl_link_failed;
  1340. out:
  1341. return err;
  1342. rtnl_link_failed:
  1343. xfrm4_tunnel_deregister(&sit_handler, AF_INET6);
  1344. xfrm_tunnel_failed:
  1345. unregister_pernet_device(&sit_net_ops);
  1346. goto out;
  1347. }
  1348. module_init(sit_init);
  1349. module_exit(sit_cleanup);
  1350. MODULE_LICENSE("GPL");
  1351. MODULE_ALIAS_NETDEV("sit0");