sit.c 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959
  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 <linux/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/ip_tunnels.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 IP6_SIT_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 bool check_6rd(struct ip_tunnel *tunnel, const struct in6_addr *v6dst,
  67. __be32 *v4dst);
  68. static struct rtnl_link_ops sit_link_ops __read_mostly;
  69. static unsigned int sit_net_id __read_mostly;
  70. struct sit_net {
  71. struct ip_tunnel __rcu *tunnels_r_l[IP6_SIT_HASH_SIZE];
  72. struct ip_tunnel __rcu *tunnels_r[IP6_SIT_HASH_SIZE];
  73. struct ip_tunnel __rcu *tunnels_l[IP6_SIT_HASH_SIZE];
  74. struct ip_tunnel __rcu *tunnels_wc[1];
  75. struct ip_tunnel __rcu **tunnels[4];
  76. struct net_device *fb_tunnel_dev;
  77. };
  78. /*
  79. * Must be invoked with rcu_read_lock
  80. */
  81. static struct ip_tunnel *ipip6_tunnel_lookup(struct net *net,
  82. struct net_device *dev,
  83. __be32 remote, __be32 local,
  84. int sifindex)
  85. {
  86. unsigned int h0 = HASH(remote);
  87. unsigned int h1 = HASH(local);
  88. struct ip_tunnel *t;
  89. struct sit_net *sitn = net_generic(net, sit_net_id);
  90. int ifindex = dev ? dev->ifindex : 0;
  91. for_each_ip_tunnel_rcu(t, sitn->tunnels_r_l[h0 ^ h1]) {
  92. if (local == t->parms.iph.saddr &&
  93. remote == t->parms.iph.daddr &&
  94. (!dev || !t->parms.link || ifindex == t->parms.link ||
  95. sifindex == t->parms.link) &&
  96. (t->dev->flags & IFF_UP))
  97. return t;
  98. }
  99. for_each_ip_tunnel_rcu(t, sitn->tunnels_r[h0]) {
  100. if (remote == t->parms.iph.daddr &&
  101. (!dev || !t->parms.link || ifindex == t->parms.link ||
  102. sifindex == t->parms.link) &&
  103. (t->dev->flags & IFF_UP))
  104. return t;
  105. }
  106. for_each_ip_tunnel_rcu(t, sitn->tunnels_l[h1]) {
  107. if (local == t->parms.iph.saddr &&
  108. (!dev || !t->parms.link || ifindex == t->parms.link ||
  109. sifindex == t->parms.link) &&
  110. (t->dev->flags & IFF_UP))
  111. return t;
  112. }
  113. t = rcu_dereference(sitn->tunnels_wc[0]);
  114. if (t && (t->dev->flags & IFF_UP))
  115. return t;
  116. return NULL;
  117. }
  118. static struct ip_tunnel __rcu **__ipip6_bucket(struct sit_net *sitn,
  119. struct ip_tunnel_parm *parms)
  120. {
  121. __be32 remote = parms->iph.daddr;
  122. __be32 local = parms->iph.saddr;
  123. unsigned int h = 0;
  124. int prio = 0;
  125. if (remote) {
  126. prio |= 2;
  127. h ^= HASH(remote);
  128. }
  129. if (local) {
  130. prio |= 1;
  131. h ^= HASH(local);
  132. }
  133. return &sitn->tunnels[prio][h];
  134. }
  135. static inline struct ip_tunnel __rcu **ipip6_bucket(struct sit_net *sitn,
  136. struct ip_tunnel *t)
  137. {
  138. return __ipip6_bucket(sitn, &t->parms);
  139. }
  140. static void ipip6_tunnel_unlink(struct sit_net *sitn, struct ip_tunnel *t)
  141. {
  142. struct ip_tunnel __rcu **tp;
  143. struct ip_tunnel *iter;
  144. for (tp = ipip6_bucket(sitn, t);
  145. (iter = rtnl_dereference(*tp)) != NULL;
  146. tp = &iter->next) {
  147. if (t == iter) {
  148. rcu_assign_pointer(*tp, t->next);
  149. break;
  150. }
  151. }
  152. }
  153. static void ipip6_tunnel_link(struct sit_net *sitn, struct ip_tunnel *t)
  154. {
  155. struct ip_tunnel __rcu **tp = ipip6_bucket(sitn, t);
  156. rcu_assign_pointer(t->next, rtnl_dereference(*tp));
  157. rcu_assign_pointer(*tp, t);
  158. }
  159. static void ipip6_tunnel_clone_6rd(struct net_device *dev, struct sit_net *sitn)
  160. {
  161. #ifdef CONFIG_IPV6_SIT_6RD
  162. struct ip_tunnel *t = netdev_priv(dev);
  163. if (dev == sitn->fb_tunnel_dev || !sitn->fb_tunnel_dev) {
  164. ipv6_addr_set(&t->ip6rd.prefix, htonl(0x20020000), 0, 0, 0);
  165. t->ip6rd.relay_prefix = 0;
  166. t->ip6rd.prefixlen = 16;
  167. t->ip6rd.relay_prefixlen = 0;
  168. } else {
  169. struct ip_tunnel *t0 = netdev_priv(sitn->fb_tunnel_dev);
  170. memcpy(&t->ip6rd, &t0->ip6rd, sizeof(t->ip6rd));
  171. }
  172. #endif
  173. }
  174. static int ipip6_tunnel_create(struct net_device *dev)
  175. {
  176. struct ip_tunnel *t = netdev_priv(dev);
  177. struct net *net = dev_net(dev);
  178. struct sit_net *sitn = net_generic(net, sit_net_id);
  179. int err;
  180. memcpy(dev->dev_addr, &t->parms.iph.saddr, 4);
  181. memcpy(dev->broadcast, &t->parms.iph.daddr, 4);
  182. if ((__force u16)t->parms.i_flags & SIT_ISATAP)
  183. dev->priv_flags |= IFF_ISATAP;
  184. dev->rtnl_link_ops = &sit_link_ops;
  185. err = register_netdevice(dev);
  186. if (err < 0)
  187. goto out;
  188. ipip6_tunnel_clone_6rd(dev, sitn);
  189. dev_hold(dev);
  190. ipip6_tunnel_link(sitn, t);
  191. return 0;
  192. out:
  193. return err;
  194. }
  195. static struct ip_tunnel *ipip6_tunnel_locate(struct net *net,
  196. struct ip_tunnel_parm *parms, int create)
  197. {
  198. __be32 remote = parms->iph.daddr;
  199. __be32 local = parms->iph.saddr;
  200. struct ip_tunnel *t, *nt;
  201. struct ip_tunnel __rcu **tp;
  202. struct net_device *dev;
  203. char name[IFNAMSIZ];
  204. struct sit_net *sitn = net_generic(net, sit_net_id);
  205. for (tp = __ipip6_bucket(sitn, parms);
  206. (t = rtnl_dereference(*tp)) != NULL;
  207. tp = &t->next) {
  208. if (local == t->parms.iph.saddr &&
  209. remote == t->parms.iph.daddr &&
  210. parms->link == t->parms.link) {
  211. if (create)
  212. return NULL;
  213. else
  214. return t;
  215. }
  216. }
  217. if (!create)
  218. goto failed;
  219. if (parms->name[0]) {
  220. if (!dev_valid_name(parms->name))
  221. goto failed;
  222. strlcpy(name, parms->name, IFNAMSIZ);
  223. } else {
  224. strcpy(name, "sit%d");
  225. }
  226. dev = alloc_netdev(sizeof(*t), name, NET_NAME_UNKNOWN,
  227. ipip6_tunnel_setup);
  228. if (!dev)
  229. return NULL;
  230. dev_net_set(dev, net);
  231. nt = netdev_priv(dev);
  232. nt->parms = *parms;
  233. if (ipip6_tunnel_create(dev) < 0)
  234. goto failed_free;
  235. return nt;
  236. failed_free:
  237. free_netdev(dev);
  238. failed:
  239. return NULL;
  240. }
  241. #define for_each_prl_rcu(start) \
  242. for (prl = rcu_dereference(start); \
  243. prl; \
  244. prl = rcu_dereference(prl->next))
  245. static struct ip_tunnel_prl_entry *
  246. __ipip6_tunnel_locate_prl(struct ip_tunnel *t, __be32 addr)
  247. {
  248. struct ip_tunnel_prl_entry *prl;
  249. for_each_prl_rcu(t->prl)
  250. if (prl->addr == addr)
  251. break;
  252. return prl;
  253. }
  254. static int ipip6_tunnel_get_prl(struct ip_tunnel *t,
  255. struct ip_tunnel_prl __user *a)
  256. {
  257. struct ip_tunnel_prl kprl, *kp;
  258. struct ip_tunnel_prl_entry *prl;
  259. unsigned int cmax, c = 0, ca, len;
  260. int ret = 0;
  261. if (copy_from_user(&kprl, a, sizeof(kprl)))
  262. return -EFAULT;
  263. cmax = kprl.datalen / sizeof(kprl);
  264. if (cmax > 1 && kprl.addr != htonl(INADDR_ANY))
  265. cmax = 1;
  266. /* For simple GET or for root users,
  267. * we try harder to allocate.
  268. */
  269. kp = (cmax <= 1 || capable(CAP_NET_ADMIN)) ?
  270. kcalloc(cmax, sizeof(*kp), GFP_KERNEL | __GFP_NOWARN) :
  271. NULL;
  272. rcu_read_lock();
  273. ca = t->prl_count < cmax ? t->prl_count : cmax;
  274. if (!kp) {
  275. /* We don't try hard to allocate much memory for
  276. * non-root users.
  277. * For root users, retry allocating enough memory for
  278. * the answer.
  279. */
  280. kp = kcalloc(ca, sizeof(*kp), GFP_ATOMIC);
  281. if (!kp) {
  282. ret = -ENOMEM;
  283. goto out;
  284. }
  285. }
  286. c = 0;
  287. for_each_prl_rcu(t->prl) {
  288. if (c >= cmax)
  289. break;
  290. if (kprl.addr != htonl(INADDR_ANY) && prl->addr != kprl.addr)
  291. continue;
  292. kp[c].addr = prl->addr;
  293. kp[c].flags = prl->flags;
  294. c++;
  295. if (kprl.addr != htonl(INADDR_ANY))
  296. break;
  297. }
  298. out:
  299. rcu_read_unlock();
  300. len = sizeof(*kp) * c;
  301. ret = 0;
  302. if ((len && copy_to_user(a + 1, kp, len)) || put_user(len, &a->datalen))
  303. ret = -EFAULT;
  304. kfree(kp);
  305. return ret;
  306. }
  307. static int
  308. ipip6_tunnel_add_prl(struct ip_tunnel *t, struct ip_tunnel_prl *a, int chg)
  309. {
  310. struct ip_tunnel_prl_entry *p;
  311. int err = 0;
  312. if (a->addr == htonl(INADDR_ANY))
  313. return -EINVAL;
  314. ASSERT_RTNL();
  315. for (p = rtnl_dereference(t->prl); p; p = rtnl_dereference(p->next)) {
  316. if (p->addr == a->addr) {
  317. if (chg) {
  318. p->flags = a->flags;
  319. goto out;
  320. }
  321. err = -EEXIST;
  322. goto out;
  323. }
  324. }
  325. if (chg) {
  326. err = -ENXIO;
  327. goto out;
  328. }
  329. p = kzalloc(sizeof(struct ip_tunnel_prl_entry), GFP_KERNEL);
  330. if (!p) {
  331. err = -ENOBUFS;
  332. goto out;
  333. }
  334. p->next = t->prl;
  335. p->addr = a->addr;
  336. p->flags = a->flags;
  337. t->prl_count++;
  338. rcu_assign_pointer(t->prl, p);
  339. out:
  340. return err;
  341. }
  342. static void prl_list_destroy_rcu(struct rcu_head *head)
  343. {
  344. struct ip_tunnel_prl_entry *p, *n;
  345. p = container_of(head, struct ip_tunnel_prl_entry, rcu_head);
  346. do {
  347. n = rcu_dereference_protected(p->next, 1);
  348. kfree(p);
  349. p = n;
  350. } while (p);
  351. }
  352. static int
  353. ipip6_tunnel_del_prl(struct ip_tunnel *t, struct ip_tunnel_prl *a)
  354. {
  355. struct ip_tunnel_prl_entry *x;
  356. struct ip_tunnel_prl_entry __rcu **p;
  357. int err = 0;
  358. ASSERT_RTNL();
  359. if (a && a->addr != htonl(INADDR_ANY)) {
  360. for (p = &t->prl;
  361. (x = rtnl_dereference(*p)) != NULL;
  362. p = &x->next) {
  363. if (x->addr == a->addr) {
  364. *p = x->next;
  365. kfree_rcu(x, rcu_head);
  366. t->prl_count--;
  367. goto out;
  368. }
  369. }
  370. err = -ENXIO;
  371. } else {
  372. x = rtnl_dereference(t->prl);
  373. if (x) {
  374. t->prl_count = 0;
  375. call_rcu(&x->rcu_head, prl_list_destroy_rcu);
  376. t->prl = NULL;
  377. }
  378. }
  379. out:
  380. return err;
  381. }
  382. static int
  383. isatap_chksrc(struct sk_buff *skb, const struct iphdr *iph, struct ip_tunnel *t)
  384. {
  385. struct ip_tunnel_prl_entry *p;
  386. int ok = 1;
  387. rcu_read_lock();
  388. p = __ipip6_tunnel_locate_prl(t, iph->saddr);
  389. if (p) {
  390. if (p->flags & PRL_DEFAULT)
  391. skb->ndisc_nodetype = NDISC_NODETYPE_DEFAULT;
  392. else
  393. skb->ndisc_nodetype = NDISC_NODETYPE_NODEFAULT;
  394. } else {
  395. const struct in6_addr *addr6 = &ipv6_hdr(skb)->saddr;
  396. if (ipv6_addr_is_isatap(addr6) &&
  397. (addr6->s6_addr32[3] == iph->saddr) &&
  398. ipv6_chk_prefix(addr6, t->dev))
  399. skb->ndisc_nodetype = NDISC_NODETYPE_HOST;
  400. else
  401. ok = 0;
  402. }
  403. rcu_read_unlock();
  404. return ok;
  405. }
  406. static void ipip6_tunnel_uninit(struct net_device *dev)
  407. {
  408. struct ip_tunnel *tunnel = netdev_priv(dev);
  409. struct sit_net *sitn = net_generic(tunnel->net, sit_net_id);
  410. if (dev == sitn->fb_tunnel_dev) {
  411. RCU_INIT_POINTER(sitn->tunnels_wc[0], NULL);
  412. } else {
  413. ipip6_tunnel_unlink(sitn, tunnel);
  414. ipip6_tunnel_del_prl(tunnel, NULL);
  415. }
  416. dst_cache_reset(&tunnel->dst_cache);
  417. dev_put(dev);
  418. }
  419. static int ipip6_err(struct sk_buff *skb, u32 info)
  420. {
  421. const struct iphdr *iph = (const struct iphdr *)skb->data;
  422. const int type = icmp_hdr(skb)->type;
  423. const int code = icmp_hdr(skb)->code;
  424. unsigned int data_len = 0;
  425. struct ip_tunnel *t;
  426. int sifindex;
  427. int err;
  428. switch (type) {
  429. default:
  430. case ICMP_PARAMETERPROB:
  431. return 0;
  432. case ICMP_DEST_UNREACH:
  433. switch (code) {
  434. case ICMP_SR_FAILED:
  435. /* Impossible event. */
  436. return 0;
  437. default:
  438. /* All others are translated to HOST_UNREACH.
  439. rfc2003 contains "deep thoughts" about NET_UNREACH,
  440. I believe they are just ether pollution. --ANK
  441. */
  442. break;
  443. }
  444. break;
  445. case ICMP_TIME_EXCEEDED:
  446. if (code != ICMP_EXC_TTL)
  447. return 0;
  448. data_len = icmp_hdr(skb)->un.reserved[1] * 4; /* RFC 4884 4.1 */
  449. break;
  450. case ICMP_REDIRECT:
  451. break;
  452. }
  453. err = -ENOENT;
  454. sifindex = netif_is_l3_master(skb->dev) ? IPCB(skb)->iif : 0;
  455. t = ipip6_tunnel_lookup(dev_net(skb->dev), skb->dev,
  456. iph->daddr, iph->saddr, sifindex);
  457. if (!t)
  458. goto out;
  459. if (type == ICMP_DEST_UNREACH && code == ICMP_FRAG_NEEDED) {
  460. ipv4_update_pmtu(skb, dev_net(skb->dev), info,
  461. t->parms.link, iph->protocol);
  462. err = 0;
  463. goto out;
  464. }
  465. if (type == ICMP_REDIRECT) {
  466. ipv4_redirect(skb, dev_net(skb->dev), t->parms.link,
  467. iph->protocol);
  468. err = 0;
  469. goto out;
  470. }
  471. err = 0;
  472. if (!ip6_err_gen_icmpv6_unreach(skb, iph->ihl * 4, type, data_len))
  473. goto out;
  474. if (t->parms.iph.daddr == 0)
  475. goto out;
  476. if (t->parms.iph.ttl == 0 && type == ICMP_TIME_EXCEEDED)
  477. goto out;
  478. if (time_before(jiffies, t->err_time + IPTUNNEL_ERR_TIMEO))
  479. t->err_count++;
  480. else
  481. t->err_count = 1;
  482. t->err_time = jiffies;
  483. out:
  484. return err;
  485. }
  486. static inline bool is_spoofed_6rd(struct ip_tunnel *tunnel, const __be32 v4addr,
  487. const struct in6_addr *v6addr)
  488. {
  489. __be32 v4embed = 0;
  490. if (check_6rd(tunnel, v6addr, &v4embed) && v4addr != v4embed)
  491. return true;
  492. return false;
  493. }
  494. /* Checks if an address matches an address on the tunnel interface.
  495. * Used to detect the NAT of proto 41 packets and let them pass spoofing test.
  496. * Long story:
  497. * This function is called after we considered the packet as spoofed
  498. * in is_spoofed_6rd.
  499. * We may have a router that is doing NAT for proto 41 packets
  500. * for an internal station. Destination a.a.a.a/PREFIX:bbbb:bbbb
  501. * will be translated to n.n.n.n/PREFIX:bbbb:bbbb. And is_spoofed_6rd
  502. * function will return true, dropping the packet.
  503. * But, we can still check if is spoofed against the IP
  504. * addresses associated with the interface.
  505. */
  506. static bool only_dnatted(const struct ip_tunnel *tunnel,
  507. const struct in6_addr *v6dst)
  508. {
  509. int prefix_len;
  510. #ifdef CONFIG_IPV6_SIT_6RD
  511. prefix_len = tunnel->ip6rd.prefixlen + 32
  512. - tunnel->ip6rd.relay_prefixlen;
  513. #else
  514. prefix_len = 48;
  515. #endif
  516. return ipv6_chk_custom_prefix(v6dst, prefix_len, tunnel->dev);
  517. }
  518. /* Returns true if a packet is spoofed */
  519. static bool packet_is_spoofed(struct sk_buff *skb,
  520. const struct iphdr *iph,
  521. struct ip_tunnel *tunnel)
  522. {
  523. const struct ipv6hdr *ipv6h;
  524. if (tunnel->dev->priv_flags & IFF_ISATAP) {
  525. if (!isatap_chksrc(skb, iph, tunnel))
  526. return true;
  527. return false;
  528. }
  529. if (tunnel->dev->flags & IFF_POINTOPOINT)
  530. return false;
  531. ipv6h = ipv6_hdr(skb);
  532. if (unlikely(is_spoofed_6rd(tunnel, iph->saddr, &ipv6h->saddr))) {
  533. net_warn_ratelimited("Src spoofed %pI4/%pI6c -> %pI4/%pI6c\n",
  534. &iph->saddr, &ipv6h->saddr,
  535. &iph->daddr, &ipv6h->daddr);
  536. return true;
  537. }
  538. if (likely(!is_spoofed_6rd(tunnel, iph->daddr, &ipv6h->daddr)))
  539. return false;
  540. if (only_dnatted(tunnel, &ipv6h->daddr))
  541. return false;
  542. net_warn_ratelimited("Dst spoofed %pI4/%pI6c -> %pI4/%pI6c\n",
  543. &iph->saddr, &ipv6h->saddr,
  544. &iph->daddr, &ipv6h->daddr);
  545. return true;
  546. }
  547. static int ipip6_rcv(struct sk_buff *skb)
  548. {
  549. const struct iphdr *iph = ip_hdr(skb);
  550. struct ip_tunnel *tunnel;
  551. int sifindex;
  552. int err;
  553. sifindex = netif_is_l3_master(skb->dev) ? IPCB(skb)->iif : 0;
  554. tunnel = ipip6_tunnel_lookup(dev_net(skb->dev), skb->dev,
  555. iph->saddr, iph->daddr, sifindex);
  556. if (tunnel) {
  557. struct pcpu_sw_netstats *tstats;
  558. if (tunnel->parms.iph.protocol != IPPROTO_IPV6 &&
  559. tunnel->parms.iph.protocol != 0)
  560. goto out;
  561. skb->mac_header = skb->network_header;
  562. skb_reset_network_header(skb);
  563. IPCB(skb)->flags = 0;
  564. skb->dev = tunnel->dev;
  565. if (packet_is_spoofed(skb, iph, tunnel)) {
  566. tunnel->dev->stats.rx_errors++;
  567. goto out;
  568. }
  569. if (iptunnel_pull_header(skb, 0, htons(ETH_P_IPV6),
  570. !net_eq(tunnel->net, dev_net(tunnel->dev))))
  571. goto out;
  572. err = IP_ECN_decapsulate(iph, skb);
  573. if (unlikely(err)) {
  574. if (log_ecn_error)
  575. net_info_ratelimited("non-ECT from %pI4 with TOS=%#x\n",
  576. &iph->saddr, iph->tos);
  577. if (err > 1) {
  578. ++tunnel->dev->stats.rx_frame_errors;
  579. ++tunnel->dev->stats.rx_errors;
  580. goto out;
  581. }
  582. }
  583. tstats = this_cpu_ptr(tunnel->dev->tstats);
  584. u64_stats_update_begin(&tstats->syncp);
  585. tstats->rx_packets++;
  586. tstats->rx_bytes += skb->len;
  587. u64_stats_update_end(&tstats->syncp);
  588. netif_rx(skb);
  589. return 0;
  590. }
  591. /* no tunnel matched, let upstream know, ipsec may handle it */
  592. return 1;
  593. out:
  594. kfree_skb(skb);
  595. return 0;
  596. }
  597. static const struct tnl_ptk_info ipip_tpi = {
  598. /* no tunnel info required for ipip. */
  599. .proto = htons(ETH_P_IP),
  600. };
  601. #if IS_ENABLED(CONFIG_MPLS)
  602. static const struct tnl_ptk_info mplsip_tpi = {
  603. /* no tunnel info required for mplsip. */
  604. .proto = htons(ETH_P_MPLS_UC),
  605. };
  606. #endif
  607. static int sit_tunnel_rcv(struct sk_buff *skb, u8 ipproto)
  608. {
  609. const struct iphdr *iph;
  610. struct ip_tunnel *tunnel;
  611. int sifindex;
  612. sifindex = netif_is_l3_master(skb->dev) ? IPCB(skb)->iif : 0;
  613. iph = ip_hdr(skb);
  614. tunnel = ipip6_tunnel_lookup(dev_net(skb->dev), skb->dev,
  615. iph->saddr, iph->daddr, sifindex);
  616. if (tunnel) {
  617. const struct tnl_ptk_info *tpi;
  618. if (tunnel->parms.iph.protocol != ipproto &&
  619. tunnel->parms.iph.protocol != 0)
  620. goto drop;
  621. if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb))
  622. goto drop;
  623. #if IS_ENABLED(CONFIG_MPLS)
  624. if (ipproto == IPPROTO_MPLS)
  625. tpi = &mplsip_tpi;
  626. else
  627. #endif
  628. tpi = &ipip_tpi;
  629. if (iptunnel_pull_header(skb, 0, tpi->proto, false))
  630. goto drop;
  631. return ip_tunnel_rcv(tunnel, skb, tpi, NULL, log_ecn_error);
  632. }
  633. return 1;
  634. drop:
  635. kfree_skb(skb);
  636. return 0;
  637. }
  638. static int ipip_rcv(struct sk_buff *skb)
  639. {
  640. return sit_tunnel_rcv(skb, IPPROTO_IPIP);
  641. }
  642. #if IS_ENABLED(CONFIG_MPLS)
  643. static int mplsip_rcv(struct sk_buff *skb)
  644. {
  645. return sit_tunnel_rcv(skb, IPPROTO_MPLS);
  646. }
  647. #endif
  648. /*
  649. * If the IPv6 address comes from 6rd / 6to4 (RFC 3056) addr space this function
  650. * stores the embedded IPv4 address in v4dst and returns true.
  651. */
  652. static bool check_6rd(struct ip_tunnel *tunnel, const struct in6_addr *v6dst,
  653. __be32 *v4dst)
  654. {
  655. #ifdef CONFIG_IPV6_SIT_6RD
  656. if (ipv6_prefix_equal(v6dst, &tunnel->ip6rd.prefix,
  657. tunnel->ip6rd.prefixlen)) {
  658. unsigned int pbw0, pbi0;
  659. int pbi1;
  660. u32 d;
  661. pbw0 = tunnel->ip6rd.prefixlen >> 5;
  662. pbi0 = tunnel->ip6rd.prefixlen & 0x1f;
  663. d = (ntohl(v6dst->s6_addr32[pbw0]) << pbi0) >>
  664. tunnel->ip6rd.relay_prefixlen;
  665. pbi1 = pbi0 - tunnel->ip6rd.relay_prefixlen;
  666. if (pbi1 > 0)
  667. d |= ntohl(v6dst->s6_addr32[pbw0 + 1]) >>
  668. (32 - pbi1);
  669. *v4dst = tunnel->ip6rd.relay_prefix | htonl(d);
  670. return true;
  671. }
  672. #else
  673. if (v6dst->s6_addr16[0] == htons(0x2002)) {
  674. /* 6to4 v6 addr has 16 bits prefix, 32 v4addr, 16 SLA, ... */
  675. memcpy(v4dst, &v6dst->s6_addr16[1], 4);
  676. return true;
  677. }
  678. #endif
  679. return false;
  680. }
  681. static inline __be32 try_6rd(struct ip_tunnel *tunnel,
  682. const struct in6_addr *v6dst)
  683. {
  684. __be32 dst = 0;
  685. check_6rd(tunnel, v6dst, &dst);
  686. return dst;
  687. }
  688. /*
  689. * This function assumes it is being called from dev_queue_xmit()
  690. * and that skb is filled properly by that function.
  691. */
  692. static netdev_tx_t ipip6_tunnel_xmit(struct sk_buff *skb,
  693. struct net_device *dev)
  694. {
  695. struct ip_tunnel *tunnel = netdev_priv(dev);
  696. const struct iphdr *tiph = &tunnel->parms.iph;
  697. const struct ipv6hdr *iph6 = ipv6_hdr(skb);
  698. u8 tos = tunnel->parms.iph.tos;
  699. __be16 df = tiph->frag_off;
  700. struct rtable *rt; /* Route to the other host */
  701. struct net_device *tdev; /* Device to other host */
  702. unsigned int max_headroom; /* The extra header space needed */
  703. __be32 dst = tiph->daddr;
  704. struct flowi4 fl4;
  705. int mtu;
  706. const struct in6_addr *addr6;
  707. int addr_type;
  708. u8 ttl;
  709. u8 protocol = IPPROTO_IPV6;
  710. int t_hlen = tunnel->hlen + sizeof(struct iphdr);
  711. if (tos == 1)
  712. tos = ipv6_get_dsfield(iph6);
  713. /* ISATAP (RFC4214) - must come before 6to4 */
  714. if (dev->priv_flags & IFF_ISATAP) {
  715. struct neighbour *neigh = NULL;
  716. bool do_tx_error = false;
  717. if (skb_dst(skb))
  718. neigh = dst_neigh_lookup(skb_dst(skb), &iph6->daddr);
  719. if (!neigh) {
  720. net_dbg_ratelimited("nexthop == NULL\n");
  721. goto tx_error;
  722. }
  723. addr6 = (const struct in6_addr *)&neigh->primary_key;
  724. addr_type = ipv6_addr_type(addr6);
  725. if ((addr_type & IPV6_ADDR_UNICAST) &&
  726. ipv6_addr_is_isatap(addr6))
  727. dst = addr6->s6_addr32[3];
  728. else
  729. do_tx_error = true;
  730. neigh_release(neigh);
  731. if (do_tx_error)
  732. goto tx_error;
  733. }
  734. if (!dst)
  735. dst = try_6rd(tunnel, &iph6->daddr);
  736. if (!dst) {
  737. struct neighbour *neigh = NULL;
  738. bool do_tx_error = false;
  739. if (skb_dst(skb))
  740. neigh = dst_neigh_lookup(skb_dst(skb), &iph6->daddr);
  741. if (!neigh) {
  742. net_dbg_ratelimited("nexthop == NULL\n");
  743. goto tx_error;
  744. }
  745. addr6 = (const struct in6_addr *)&neigh->primary_key;
  746. addr_type = ipv6_addr_type(addr6);
  747. if (addr_type == IPV6_ADDR_ANY) {
  748. addr6 = &ipv6_hdr(skb)->daddr;
  749. addr_type = ipv6_addr_type(addr6);
  750. }
  751. if ((addr_type & IPV6_ADDR_COMPATv4) != 0)
  752. dst = addr6->s6_addr32[3];
  753. else
  754. do_tx_error = true;
  755. neigh_release(neigh);
  756. if (do_tx_error)
  757. goto tx_error;
  758. }
  759. flowi4_init_output(&fl4, tunnel->parms.link, tunnel->fwmark,
  760. RT_TOS(tos), RT_SCOPE_UNIVERSE, IPPROTO_IPV6,
  761. 0, dst, tiph->saddr, 0, 0,
  762. sock_net_uid(tunnel->net, NULL));
  763. rt = ip_route_output_flow(tunnel->net, &fl4, NULL);
  764. if (IS_ERR(rt)) {
  765. dev->stats.tx_carrier_errors++;
  766. goto tx_error_icmp;
  767. }
  768. if (rt->rt_type != RTN_UNICAST) {
  769. ip_rt_put(rt);
  770. dev->stats.tx_carrier_errors++;
  771. goto tx_error_icmp;
  772. }
  773. tdev = rt->dst.dev;
  774. if (tdev == dev) {
  775. ip_rt_put(rt);
  776. dev->stats.collisions++;
  777. goto tx_error;
  778. }
  779. if (iptunnel_handle_offloads(skb, SKB_GSO_IPXIP4)) {
  780. ip_rt_put(rt);
  781. goto tx_error;
  782. }
  783. if (df) {
  784. mtu = dst_mtu(&rt->dst) - t_hlen;
  785. if (mtu < 68) {
  786. dev->stats.collisions++;
  787. ip_rt_put(rt);
  788. goto tx_error;
  789. }
  790. if (mtu < IPV6_MIN_MTU) {
  791. mtu = IPV6_MIN_MTU;
  792. df = 0;
  793. }
  794. if (tunnel->parms.iph.daddr)
  795. skb_dst_update_pmtu(skb, mtu);
  796. if (skb->len > mtu && !skb_is_gso(skb)) {
  797. icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
  798. ip_rt_put(rt);
  799. goto tx_error;
  800. }
  801. }
  802. if (tunnel->err_count > 0) {
  803. if (time_before(jiffies,
  804. tunnel->err_time + IPTUNNEL_ERR_TIMEO)) {
  805. tunnel->err_count--;
  806. dst_link_failure(skb);
  807. } else
  808. tunnel->err_count = 0;
  809. }
  810. /*
  811. * Okay, now see if we can stuff it in the buffer as-is.
  812. */
  813. max_headroom = LL_RESERVED_SPACE(tdev) + t_hlen;
  814. if (skb_headroom(skb) < max_headroom || skb_shared(skb) ||
  815. (skb_cloned(skb) && !skb_clone_writable(skb, 0))) {
  816. struct sk_buff *new_skb = skb_realloc_headroom(skb, max_headroom);
  817. if (!new_skb) {
  818. ip_rt_put(rt);
  819. dev->stats.tx_dropped++;
  820. kfree_skb(skb);
  821. return NETDEV_TX_OK;
  822. }
  823. if (skb->sk)
  824. skb_set_owner_w(new_skb, skb->sk);
  825. dev_kfree_skb(skb);
  826. skb = new_skb;
  827. iph6 = ipv6_hdr(skb);
  828. }
  829. ttl = tiph->ttl;
  830. if (ttl == 0)
  831. ttl = iph6->hop_limit;
  832. tos = INET_ECN_encapsulate(tos, ipv6_get_dsfield(iph6));
  833. if (ip_tunnel_encap(skb, tunnel, &protocol, &fl4) < 0) {
  834. ip_rt_put(rt);
  835. goto tx_error;
  836. }
  837. skb_set_inner_ipproto(skb, IPPROTO_IPV6);
  838. iptunnel_xmit(NULL, rt, skb, fl4.saddr, fl4.daddr, protocol, tos, ttl,
  839. df, !net_eq(tunnel->net, dev_net(dev)));
  840. return NETDEV_TX_OK;
  841. tx_error_icmp:
  842. dst_link_failure(skb);
  843. tx_error:
  844. kfree_skb(skb);
  845. dev->stats.tx_errors++;
  846. return NETDEV_TX_OK;
  847. }
  848. static netdev_tx_t sit_tunnel_xmit__(struct sk_buff *skb,
  849. struct net_device *dev, u8 ipproto)
  850. {
  851. struct ip_tunnel *tunnel = netdev_priv(dev);
  852. const struct iphdr *tiph = &tunnel->parms.iph;
  853. if (iptunnel_handle_offloads(skb, SKB_GSO_IPXIP4))
  854. goto tx_error;
  855. skb_set_inner_ipproto(skb, ipproto);
  856. ip_tunnel_xmit(skb, dev, tiph, ipproto);
  857. return NETDEV_TX_OK;
  858. tx_error:
  859. kfree_skb(skb);
  860. dev->stats.tx_errors++;
  861. return NETDEV_TX_OK;
  862. }
  863. static netdev_tx_t sit_tunnel_xmit(struct sk_buff *skb,
  864. struct net_device *dev)
  865. {
  866. switch (skb->protocol) {
  867. case htons(ETH_P_IP):
  868. sit_tunnel_xmit__(skb, dev, IPPROTO_IPIP);
  869. break;
  870. case htons(ETH_P_IPV6):
  871. ipip6_tunnel_xmit(skb, dev);
  872. break;
  873. #if IS_ENABLED(CONFIG_MPLS)
  874. case htons(ETH_P_MPLS_UC):
  875. sit_tunnel_xmit__(skb, dev, IPPROTO_MPLS);
  876. break;
  877. #endif
  878. default:
  879. goto tx_err;
  880. }
  881. return NETDEV_TX_OK;
  882. tx_err:
  883. dev->stats.tx_errors++;
  884. kfree_skb(skb);
  885. return NETDEV_TX_OK;
  886. }
  887. static void ipip6_tunnel_bind_dev(struct net_device *dev)
  888. {
  889. struct net_device *tdev = NULL;
  890. struct ip_tunnel *tunnel;
  891. const struct iphdr *iph;
  892. struct flowi4 fl4;
  893. tunnel = netdev_priv(dev);
  894. iph = &tunnel->parms.iph;
  895. if (iph->daddr) {
  896. struct rtable *rt = ip_route_output_ports(tunnel->net, &fl4,
  897. NULL,
  898. iph->daddr, iph->saddr,
  899. 0, 0,
  900. IPPROTO_IPV6,
  901. RT_TOS(iph->tos),
  902. tunnel->parms.link);
  903. if (!IS_ERR(rt)) {
  904. tdev = rt->dst.dev;
  905. ip_rt_put(rt);
  906. }
  907. dev->flags |= IFF_POINTOPOINT;
  908. }
  909. if (!tdev && tunnel->parms.link)
  910. tdev = __dev_get_by_index(tunnel->net, tunnel->parms.link);
  911. if (tdev) {
  912. int t_hlen = tunnel->hlen + sizeof(struct iphdr);
  913. dev->hard_header_len = tdev->hard_header_len + sizeof(struct iphdr);
  914. dev->mtu = tdev->mtu - t_hlen;
  915. if (dev->mtu < IPV6_MIN_MTU)
  916. dev->mtu = IPV6_MIN_MTU;
  917. }
  918. }
  919. static void ipip6_tunnel_update(struct ip_tunnel *t, struct ip_tunnel_parm *p,
  920. __u32 fwmark)
  921. {
  922. struct net *net = t->net;
  923. struct sit_net *sitn = net_generic(net, sit_net_id);
  924. ipip6_tunnel_unlink(sitn, t);
  925. synchronize_net();
  926. t->parms.iph.saddr = p->iph.saddr;
  927. t->parms.iph.daddr = p->iph.daddr;
  928. memcpy(t->dev->dev_addr, &p->iph.saddr, 4);
  929. memcpy(t->dev->broadcast, &p->iph.daddr, 4);
  930. ipip6_tunnel_link(sitn, t);
  931. t->parms.iph.ttl = p->iph.ttl;
  932. t->parms.iph.tos = p->iph.tos;
  933. t->parms.iph.frag_off = p->iph.frag_off;
  934. if (t->parms.link != p->link || t->fwmark != fwmark) {
  935. t->parms.link = p->link;
  936. t->fwmark = fwmark;
  937. ipip6_tunnel_bind_dev(t->dev);
  938. }
  939. dst_cache_reset(&t->dst_cache);
  940. netdev_state_change(t->dev);
  941. }
  942. #ifdef CONFIG_IPV6_SIT_6RD
  943. static int ipip6_tunnel_update_6rd(struct ip_tunnel *t,
  944. struct ip_tunnel_6rd *ip6rd)
  945. {
  946. struct in6_addr prefix;
  947. __be32 relay_prefix;
  948. if (ip6rd->relay_prefixlen > 32 ||
  949. ip6rd->prefixlen + (32 - ip6rd->relay_prefixlen) > 64)
  950. return -EINVAL;
  951. ipv6_addr_prefix(&prefix, &ip6rd->prefix, ip6rd->prefixlen);
  952. if (!ipv6_addr_equal(&prefix, &ip6rd->prefix))
  953. return -EINVAL;
  954. if (ip6rd->relay_prefixlen)
  955. relay_prefix = ip6rd->relay_prefix &
  956. htonl(0xffffffffUL <<
  957. (32 - ip6rd->relay_prefixlen));
  958. else
  959. relay_prefix = 0;
  960. if (relay_prefix != ip6rd->relay_prefix)
  961. return -EINVAL;
  962. t->ip6rd.prefix = prefix;
  963. t->ip6rd.relay_prefix = relay_prefix;
  964. t->ip6rd.prefixlen = ip6rd->prefixlen;
  965. t->ip6rd.relay_prefixlen = ip6rd->relay_prefixlen;
  966. dst_cache_reset(&t->dst_cache);
  967. netdev_state_change(t->dev);
  968. return 0;
  969. }
  970. #endif
  971. static bool ipip6_valid_ip_proto(u8 ipproto)
  972. {
  973. return ipproto == IPPROTO_IPV6 ||
  974. ipproto == IPPROTO_IPIP ||
  975. #if IS_ENABLED(CONFIG_MPLS)
  976. ipproto == IPPROTO_MPLS ||
  977. #endif
  978. ipproto == 0;
  979. }
  980. static int
  981. ipip6_tunnel_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  982. {
  983. int err = 0;
  984. struct ip_tunnel_parm p;
  985. struct ip_tunnel_prl prl;
  986. struct ip_tunnel *t = netdev_priv(dev);
  987. struct net *net = t->net;
  988. struct sit_net *sitn = net_generic(net, sit_net_id);
  989. #ifdef CONFIG_IPV6_SIT_6RD
  990. struct ip_tunnel_6rd ip6rd;
  991. #endif
  992. switch (cmd) {
  993. case SIOCGETTUNNEL:
  994. #ifdef CONFIG_IPV6_SIT_6RD
  995. case SIOCGET6RD:
  996. #endif
  997. if (dev == sitn->fb_tunnel_dev) {
  998. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) {
  999. err = -EFAULT;
  1000. break;
  1001. }
  1002. t = ipip6_tunnel_locate(net, &p, 0);
  1003. if (!t)
  1004. t = netdev_priv(dev);
  1005. }
  1006. err = -EFAULT;
  1007. if (cmd == SIOCGETTUNNEL) {
  1008. memcpy(&p, &t->parms, sizeof(p));
  1009. if (copy_to_user(ifr->ifr_ifru.ifru_data, &p,
  1010. sizeof(p)))
  1011. goto done;
  1012. #ifdef CONFIG_IPV6_SIT_6RD
  1013. } else {
  1014. ip6rd.prefix = t->ip6rd.prefix;
  1015. ip6rd.relay_prefix = t->ip6rd.relay_prefix;
  1016. ip6rd.prefixlen = t->ip6rd.prefixlen;
  1017. ip6rd.relay_prefixlen = t->ip6rd.relay_prefixlen;
  1018. if (copy_to_user(ifr->ifr_ifru.ifru_data, &ip6rd,
  1019. sizeof(ip6rd)))
  1020. goto done;
  1021. #endif
  1022. }
  1023. err = 0;
  1024. break;
  1025. case SIOCADDTUNNEL:
  1026. case SIOCCHGTUNNEL:
  1027. err = -EPERM;
  1028. if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  1029. goto done;
  1030. err = -EFAULT;
  1031. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
  1032. goto done;
  1033. err = -EINVAL;
  1034. if (!ipip6_valid_ip_proto(p.iph.protocol))
  1035. goto done;
  1036. if (p.iph.version != 4 ||
  1037. p.iph.ihl != 5 || (p.iph.frag_off&htons(~IP_DF)))
  1038. goto done;
  1039. if (p.iph.ttl)
  1040. p.iph.frag_off |= htons(IP_DF);
  1041. t = ipip6_tunnel_locate(net, &p, cmd == SIOCADDTUNNEL);
  1042. if (dev != sitn->fb_tunnel_dev && cmd == SIOCCHGTUNNEL) {
  1043. if (t) {
  1044. if (t->dev != dev) {
  1045. err = -EEXIST;
  1046. break;
  1047. }
  1048. } else {
  1049. if (((dev->flags&IFF_POINTOPOINT) && !p.iph.daddr) ||
  1050. (!(dev->flags&IFF_POINTOPOINT) && p.iph.daddr)) {
  1051. err = -EINVAL;
  1052. break;
  1053. }
  1054. t = netdev_priv(dev);
  1055. }
  1056. ipip6_tunnel_update(t, &p, t->fwmark);
  1057. }
  1058. if (t) {
  1059. err = 0;
  1060. if (copy_to_user(ifr->ifr_ifru.ifru_data, &t->parms, sizeof(p)))
  1061. err = -EFAULT;
  1062. } else
  1063. err = (cmd == SIOCADDTUNNEL ? -ENOBUFS : -ENOENT);
  1064. break;
  1065. case SIOCDELTUNNEL:
  1066. err = -EPERM;
  1067. if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  1068. goto done;
  1069. if (dev == sitn->fb_tunnel_dev) {
  1070. err = -EFAULT;
  1071. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
  1072. goto done;
  1073. err = -ENOENT;
  1074. t = ipip6_tunnel_locate(net, &p, 0);
  1075. if (!t)
  1076. goto done;
  1077. err = -EPERM;
  1078. if (t == netdev_priv(sitn->fb_tunnel_dev))
  1079. goto done;
  1080. dev = t->dev;
  1081. }
  1082. unregister_netdevice(dev);
  1083. err = 0;
  1084. break;
  1085. case SIOCGETPRL:
  1086. err = -EINVAL;
  1087. if (dev == sitn->fb_tunnel_dev)
  1088. goto done;
  1089. err = ipip6_tunnel_get_prl(t, ifr->ifr_ifru.ifru_data);
  1090. break;
  1091. case SIOCADDPRL:
  1092. case SIOCDELPRL:
  1093. case SIOCCHGPRL:
  1094. err = -EPERM;
  1095. if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  1096. goto done;
  1097. err = -EINVAL;
  1098. if (dev == sitn->fb_tunnel_dev)
  1099. goto done;
  1100. err = -EFAULT;
  1101. if (copy_from_user(&prl, ifr->ifr_ifru.ifru_data, sizeof(prl)))
  1102. goto done;
  1103. switch (cmd) {
  1104. case SIOCDELPRL:
  1105. err = ipip6_tunnel_del_prl(t, &prl);
  1106. break;
  1107. case SIOCADDPRL:
  1108. case SIOCCHGPRL:
  1109. err = ipip6_tunnel_add_prl(t, &prl, cmd == SIOCCHGPRL);
  1110. break;
  1111. }
  1112. dst_cache_reset(&t->dst_cache);
  1113. netdev_state_change(dev);
  1114. break;
  1115. #ifdef CONFIG_IPV6_SIT_6RD
  1116. case SIOCADD6RD:
  1117. case SIOCCHG6RD:
  1118. case SIOCDEL6RD:
  1119. err = -EPERM;
  1120. if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  1121. goto done;
  1122. err = -EFAULT;
  1123. if (copy_from_user(&ip6rd, ifr->ifr_ifru.ifru_data,
  1124. sizeof(ip6rd)))
  1125. goto done;
  1126. if (cmd != SIOCDEL6RD) {
  1127. err = ipip6_tunnel_update_6rd(t, &ip6rd);
  1128. if (err < 0)
  1129. goto done;
  1130. } else
  1131. ipip6_tunnel_clone_6rd(dev, sitn);
  1132. err = 0;
  1133. break;
  1134. #endif
  1135. default:
  1136. err = -EINVAL;
  1137. }
  1138. done:
  1139. return err;
  1140. }
  1141. static const struct net_device_ops ipip6_netdev_ops = {
  1142. .ndo_init = ipip6_tunnel_init,
  1143. .ndo_uninit = ipip6_tunnel_uninit,
  1144. .ndo_start_xmit = sit_tunnel_xmit,
  1145. .ndo_do_ioctl = ipip6_tunnel_ioctl,
  1146. .ndo_get_stats64 = ip_tunnel_get_stats64,
  1147. .ndo_get_iflink = ip_tunnel_get_iflink,
  1148. };
  1149. static void ipip6_dev_free(struct net_device *dev)
  1150. {
  1151. struct ip_tunnel *tunnel = netdev_priv(dev);
  1152. dst_cache_destroy(&tunnel->dst_cache);
  1153. free_percpu(dev->tstats);
  1154. }
  1155. #define SIT_FEATURES (NETIF_F_SG | \
  1156. NETIF_F_FRAGLIST | \
  1157. NETIF_F_HIGHDMA | \
  1158. NETIF_F_GSO_SOFTWARE | \
  1159. NETIF_F_HW_CSUM)
  1160. static void ipip6_tunnel_setup(struct net_device *dev)
  1161. {
  1162. struct ip_tunnel *tunnel = netdev_priv(dev);
  1163. int t_hlen = tunnel->hlen + sizeof(struct iphdr);
  1164. dev->netdev_ops = &ipip6_netdev_ops;
  1165. dev->needs_free_netdev = true;
  1166. dev->priv_destructor = ipip6_dev_free;
  1167. dev->type = ARPHRD_SIT;
  1168. dev->hard_header_len = LL_MAX_HEADER + t_hlen;
  1169. dev->mtu = ETH_DATA_LEN - t_hlen;
  1170. dev->min_mtu = IPV6_MIN_MTU;
  1171. dev->max_mtu = IP6_MAX_MTU - t_hlen;
  1172. dev->flags = IFF_NOARP;
  1173. netif_keep_dst(dev);
  1174. dev->addr_len = 4;
  1175. dev->features |= NETIF_F_LLTX;
  1176. dev->features |= SIT_FEATURES;
  1177. dev->hw_features |= SIT_FEATURES;
  1178. }
  1179. static int ipip6_tunnel_init(struct net_device *dev)
  1180. {
  1181. struct ip_tunnel *tunnel = netdev_priv(dev);
  1182. int err;
  1183. tunnel->dev = dev;
  1184. tunnel->net = dev_net(dev);
  1185. strcpy(tunnel->parms.name, dev->name);
  1186. ipip6_tunnel_bind_dev(dev);
  1187. dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
  1188. if (!dev->tstats)
  1189. return -ENOMEM;
  1190. err = dst_cache_init(&tunnel->dst_cache, GFP_KERNEL);
  1191. if (err) {
  1192. free_percpu(dev->tstats);
  1193. dev->tstats = NULL;
  1194. return err;
  1195. }
  1196. return 0;
  1197. }
  1198. static void __net_init ipip6_fb_tunnel_init(struct net_device *dev)
  1199. {
  1200. struct ip_tunnel *tunnel = netdev_priv(dev);
  1201. struct iphdr *iph = &tunnel->parms.iph;
  1202. struct net *net = dev_net(dev);
  1203. struct sit_net *sitn = net_generic(net, sit_net_id);
  1204. iph->version = 4;
  1205. iph->protocol = IPPROTO_IPV6;
  1206. iph->ihl = 5;
  1207. iph->ttl = 64;
  1208. dev_hold(dev);
  1209. rcu_assign_pointer(sitn->tunnels_wc[0], tunnel);
  1210. }
  1211. static int ipip6_validate(struct nlattr *tb[], struct nlattr *data[],
  1212. struct netlink_ext_ack *extack)
  1213. {
  1214. u8 proto;
  1215. if (!data || !data[IFLA_IPTUN_PROTO])
  1216. return 0;
  1217. proto = nla_get_u8(data[IFLA_IPTUN_PROTO]);
  1218. if (!ipip6_valid_ip_proto(proto))
  1219. return -EINVAL;
  1220. return 0;
  1221. }
  1222. static void ipip6_netlink_parms(struct nlattr *data[],
  1223. struct ip_tunnel_parm *parms,
  1224. __u32 *fwmark)
  1225. {
  1226. memset(parms, 0, sizeof(*parms));
  1227. parms->iph.version = 4;
  1228. parms->iph.protocol = IPPROTO_IPV6;
  1229. parms->iph.ihl = 5;
  1230. parms->iph.ttl = 64;
  1231. if (!data)
  1232. return;
  1233. if (data[IFLA_IPTUN_LINK])
  1234. parms->link = nla_get_u32(data[IFLA_IPTUN_LINK]);
  1235. if (data[IFLA_IPTUN_LOCAL])
  1236. parms->iph.saddr = nla_get_be32(data[IFLA_IPTUN_LOCAL]);
  1237. if (data[IFLA_IPTUN_REMOTE])
  1238. parms->iph.daddr = nla_get_be32(data[IFLA_IPTUN_REMOTE]);
  1239. if (data[IFLA_IPTUN_TTL]) {
  1240. parms->iph.ttl = nla_get_u8(data[IFLA_IPTUN_TTL]);
  1241. if (parms->iph.ttl)
  1242. parms->iph.frag_off = htons(IP_DF);
  1243. }
  1244. if (data[IFLA_IPTUN_TOS])
  1245. parms->iph.tos = nla_get_u8(data[IFLA_IPTUN_TOS]);
  1246. if (!data[IFLA_IPTUN_PMTUDISC] || nla_get_u8(data[IFLA_IPTUN_PMTUDISC]))
  1247. parms->iph.frag_off = htons(IP_DF);
  1248. if (data[IFLA_IPTUN_FLAGS])
  1249. parms->i_flags = nla_get_be16(data[IFLA_IPTUN_FLAGS]);
  1250. if (data[IFLA_IPTUN_PROTO])
  1251. parms->iph.protocol = nla_get_u8(data[IFLA_IPTUN_PROTO]);
  1252. if (data[IFLA_IPTUN_FWMARK])
  1253. *fwmark = nla_get_u32(data[IFLA_IPTUN_FWMARK]);
  1254. }
  1255. /* This function returns true when ENCAP attributes are present in the nl msg */
  1256. static bool ipip6_netlink_encap_parms(struct nlattr *data[],
  1257. struct ip_tunnel_encap *ipencap)
  1258. {
  1259. bool ret = false;
  1260. memset(ipencap, 0, sizeof(*ipencap));
  1261. if (!data)
  1262. return ret;
  1263. if (data[IFLA_IPTUN_ENCAP_TYPE]) {
  1264. ret = true;
  1265. ipencap->type = nla_get_u16(data[IFLA_IPTUN_ENCAP_TYPE]);
  1266. }
  1267. if (data[IFLA_IPTUN_ENCAP_FLAGS]) {
  1268. ret = true;
  1269. ipencap->flags = nla_get_u16(data[IFLA_IPTUN_ENCAP_FLAGS]);
  1270. }
  1271. if (data[IFLA_IPTUN_ENCAP_SPORT]) {
  1272. ret = true;
  1273. ipencap->sport = nla_get_be16(data[IFLA_IPTUN_ENCAP_SPORT]);
  1274. }
  1275. if (data[IFLA_IPTUN_ENCAP_DPORT]) {
  1276. ret = true;
  1277. ipencap->dport = nla_get_be16(data[IFLA_IPTUN_ENCAP_DPORT]);
  1278. }
  1279. return ret;
  1280. }
  1281. #ifdef CONFIG_IPV6_SIT_6RD
  1282. /* This function returns true when 6RD attributes are present in the nl msg */
  1283. static bool ipip6_netlink_6rd_parms(struct nlattr *data[],
  1284. struct ip_tunnel_6rd *ip6rd)
  1285. {
  1286. bool ret = false;
  1287. memset(ip6rd, 0, sizeof(*ip6rd));
  1288. if (!data)
  1289. return ret;
  1290. if (data[IFLA_IPTUN_6RD_PREFIX]) {
  1291. ret = true;
  1292. ip6rd->prefix = nla_get_in6_addr(data[IFLA_IPTUN_6RD_PREFIX]);
  1293. }
  1294. if (data[IFLA_IPTUN_6RD_RELAY_PREFIX]) {
  1295. ret = true;
  1296. ip6rd->relay_prefix =
  1297. nla_get_be32(data[IFLA_IPTUN_6RD_RELAY_PREFIX]);
  1298. }
  1299. if (data[IFLA_IPTUN_6RD_PREFIXLEN]) {
  1300. ret = true;
  1301. ip6rd->prefixlen = nla_get_u16(data[IFLA_IPTUN_6RD_PREFIXLEN]);
  1302. }
  1303. if (data[IFLA_IPTUN_6RD_RELAY_PREFIXLEN]) {
  1304. ret = true;
  1305. ip6rd->relay_prefixlen =
  1306. nla_get_u16(data[IFLA_IPTUN_6RD_RELAY_PREFIXLEN]);
  1307. }
  1308. return ret;
  1309. }
  1310. #endif
  1311. static int ipip6_newlink(struct net *src_net, struct net_device *dev,
  1312. struct nlattr *tb[], struct nlattr *data[],
  1313. struct netlink_ext_ack *extack)
  1314. {
  1315. struct net *net = dev_net(dev);
  1316. struct ip_tunnel *nt;
  1317. struct ip_tunnel_encap ipencap;
  1318. #ifdef CONFIG_IPV6_SIT_6RD
  1319. struct ip_tunnel_6rd ip6rd;
  1320. #endif
  1321. int err;
  1322. nt = netdev_priv(dev);
  1323. if (ipip6_netlink_encap_parms(data, &ipencap)) {
  1324. err = ip_tunnel_encap_setup(nt, &ipencap);
  1325. if (err < 0)
  1326. return err;
  1327. }
  1328. ipip6_netlink_parms(data, &nt->parms, &nt->fwmark);
  1329. if (ipip6_tunnel_locate(net, &nt->parms, 0))
  1330. return -EEXIST;
  1331. err = ipip6_tunnel_create(dev);
  1332. if (err < 0)
  1333. return err;
  1334. if (tb[IFLA_MTU]) {
  1335. u32 mtu = nla_get_u32(tb[IFLA_MTU]);
  1336. if (mtu >= IPV6_MIN_MTU &&
  1337. mtu <= IP6_MAX_MTU - dev->hard_header_len)
  1338. dev->mtu = mtu;
  1339. }
  1340. #ifdef CONFIG_IPV6_SIT_6RD
  1341. if (ipip6_netlink_6rd_parms(data, &ip6rd))
  1342. err = ipip6_tunnel_update_6rd(nt, &ip6rd);
  1343. #endif
  1344. return err;
  1345. }
  1346. static int ipip6_changelink(struct net_device *dev, struct nlattr *tb[],
  1347. struct nlattr *data[],
  1348. struct netlink_ext_ack *extack)
  1349. {
  1350. struct ip_tunnel *t = netdev_priv(dev);
  1351. struct ip_tunnel_parm p;
  1352. struct ip_tunnel_encap ipencap;
  1353. struct net *net = t->net;
  1354. struct sit_net *sitn = net_generic(net, sit_net_id);
  1355. #ifdef CONFIG_IPV6_SIT_6RD
  1356. struct ip_tunnel_6rd ip6rd;
  1357. #endif
  1358. __u32 fwmark = t->fwmark;
  1359. int err;
  1360. if (dev == sitn->fb_tunnel_dev)
  1361. return -EINVAL;
  1362. if (ipip6_netlink_encap_parms(data, &ipencap)) {
  1363. err = ip_tunnel_encap_setup(t, &ipencap);
  1364. if (err < 0)
  1365. return err;
  1366. }
  1367. ipip6_netlink_parms(data, &p, &fwmark);
  1368. if (((dev->flags & IFF_POINTOPOINT) && !p.iph.daddr) ||
  1369. (!(dev->flags & IFF_POINTOPOINT) && p.iph.daddr))
  1370. return -EINVAL;
  1371. t = ipip6_tunnel_locate(net, &p, 0);
  1372. if (t) {
  1373. if (t->dev != dev)
  1374. return -EEXIST;
  1375. } else
  1376. t = netdev_priv(dev);
  1377. ipip6_tunnel_update(t, &p, fwmark);
  1378. #ifdef CONFIG_IPV6_SIT_6RD
  1379. if (ipip6_netlink_6rd_parms(data, &ip6rd))
  1380. return ipip6_tunnel_update_6rd(t, &ip6rd);
  1381. #endif
  1382. return 0;
  1383. }
  1384. static size_t ipip6_get_size(const struct net_device *dev)
  1385. {
  1386. return
  1387. /* IFLA_IPTUN_LINK */
  1388. nla_total_size(4) +
  1389. /* IFLA_IPTUN_LOCAL */
  1390. nla_total_size(4) +
  1391. /* IFLA_IPTUN_REMOTE */
  1392. nla_total_size(4) +
  1393. /* IFLA_IPTUN_TTL */
  1394. nla_total_size(1) +
  1395. /* IFLA_IPTUN_TOS */
  1396. nla_total_size(1) +
  1397. /* IFLA_IPTUN_PMTUDISC */
  1398. nla_total_size(1) +
  1399. /* IFLA_IPTUN_FLAGS */
  1400. nla_total_size(2) +
  1401. /* IFLA_IPTUN_PROTO */
  1402. nla_total_size(1) +
  1403. #ifdef CONFIG_IPV6_SIT_6RD
  1404. /* IFLA_IPTUN_6RD_PREFIX */
  1405. nla_total_size(sizeof(struct in6_addr)) +
  1406. /* IFLA_IPTUN_6RD_RELAY_PREFIX */
  1407. nla_total_size(4) +
  1408. /* IFLA_IPTUN_6RD_PREFIXLEN */
  1409. nla_total_size(2) +
  1410. /* IFLA_IPTUN_6RD_RELAY_PREFIXLEN */
  1411. nla_total_size(2) +
  1412. #endif
  1413. /* IFLA_IPTUN_ENCAP_TYPE */
  1414. nla_total_size(2) +
  1415. /* IFLA_IPTUN_ENCAP_FLAGS */
  1416. nla_total_size(2) +
  1417. /* IFLA_IPTUN_ENCAP_SPORT */
  1418. nla_total_size(2) +
  1419. /* IFLA_IPTUN_ENCAP_DPORT */
  1420. nla_total_size(2) +
  1421. /* IFLA_IPTUN_FWMARK */
  1422. nla_total_size(4) +
  1423. 0;
  1424. }
  1425. static int ipip6_fill_info(struct sk_buff *skb, const struct net_device *dev)
  1426. {
  1427. struct ip_tunnel *tunnel = netdev_priv(dev);
  1428. struct ip_tunnel_parm *parm = &tunnel->parms;
  1429. if (nla_put_u32(skb, IFLA_IPTUN_LINK, parm->link) ||
  1430. nla_put_in_addr(skb, IFLA_IPTUN_LOCAL, parm->iph.saddr) ||
  1431. nla_put_in_addr(skb, IFLA_IPTUN_REMOTE, parm->iph.daddr) ||
  1432. nla_put_u8(skb, IFLA_IPTUN_TTL, parm->iph.ttl) ||
  1433. nla_put_u8(skb, IFLA_IPTUN_TOS, parm->iph.tos) ||
  1434. nla_put_u8(skb, IFLA_IPTUN_PMTUDISC,
  1435. !!(parm->iph.frag_off & htons(IP_DF))) ||
  1436. nla_put_u8(skb, IFLA_IPTUN_PROTO, parm->iph.protocol) ||
  1437. nla_put_be16(skb, IFLA_IPTUN_FLAGS, parm->i_flags) ||
  1438. nla_put_u32(skb, IFLA_IPTUN_FWMARK, tunnel->fwmark))
  1439. goto nla_put_failure;
  1440. #ifdef CONFIG_IPV6_SIT_6RD
  1441. if (nla_put_in6_addr(skb, IFLA_IPTUN_6RD_PREFIX,
  1442. &tunnel->ip6rd.prefix) ||
  1443. nla_put_in_addr(skb, IFLA_IPTUN_6RD_RELAY_PREFIX,
  1444. tunnel->ip6rd.relay_prefix) ||
  1445. nla_put_u16(skb, IFLA_IPTUN_6RD_PREFIXLEN,
  1446. tunnel->ip6rd.prefixlen) ||
  1447. nla_put_u16(skb, IFLA_IPTUN_6RD_RELAY_PREFIXLEN,
  1448. tunnel->ip6rd.relay_prefixlen))
  1449. goto nla_put_failure;
  1450. #endif
  1451. if (nla_put_u16(skb, IFLA_IPTUN_ENCAP_TYPE,
  1452. tunnel->encap.type) ||
  1453. nla_put_be16(skb, IFLA_IPTUN_ENCAP_SPORT,
  1454. tunnel->encap.sport) ||
  1455. nla_put_be16(skb, IFLA_IPTUN_ENCAP_DPORT,
  1456. tunnel->encap.dport) ||
  1457. nla_put_u16(skb, IFLA_IPTUN_ENCAP_FLAGS,
  1458. tunnel->encap.flags))
  1459. goto nla_put_failure;
  1460. return 0;
  1461. nla_put_failure:
  1462. return -EMSGSIZE;
  1463. }
  1464. static const struct nla_policy ipip6_policy[IFLA_IPTUN_MAX + 1] = {
  1465. [IFLA_IPTUN_LINK] = { .type = NLA_U32 },
  1466. [IFLA_IPTUN_LOCAL] = { .type = NLA_U32 },
  1467. [IFLA_IPTUN_REMOTE] = { .type = NLA_U32 },
  1468. [IFLA_IPTUN_TTL] = { .type = NLA_U8 },
  1469. [IFLA_IPTUN_TOS] = { .type = NLA_U8 },
  1470. [IFLA_IPTUN_PMTUDISC] = { .type = NLA_U8 },
  1471. [IFLA_IPTUN_FLAGS] = { .type = NLA_U16 },
  1472. [IFLA_IPTUN_PROTO] = { .type = NLA_U8 },
  1473. #ifdef CONFIG_IPV6_SIT_6RD
  1474. [IFLA_IPTUN_6RD_PREFIX] = { .len = sizeof(struct in6_addr) },
  1475. [IFLA_IPTUN_6RD_RELAY_PREFIX] = { .type = NLA_U32 },
  1476. [IFLA_IPTUN_6RD_PREFIXLEN] = { .type = NLA_U16 },
  1477. [IFLA_IPTUN_6RD_RELAY_PREFIXLEN] = { .type = NLA_U16 },
  1478. #endif
  1479. [IFLA_IPTUN_ENCAP_TYPE] = { .type = NLA_U16 },
  1480. [IFLA_IPTUN_ENCAP_FLAGS] = { .type = NLA_U16 },
  1481. [IFLA_IPTUN_ENCAP_SPORT] = { .type = NLA_U16 },
  1482. [IFLA_IPTUN_ENCAP_DPORT] = { .type = NLA_U16 },
  1483. [IFLA_IPTUN_FWMARK] = { .type = NLA_U32 },
  1484. };
  1485. static void ipip6_dellink(struct net_device *dev, struct list_head *head)
  1486. {
  1487. struct net *net = dev_net(dev);
  1488. struct sit_net *sitn = net_generic(net, sit_net_id);
  1489. if (dev != sitn->fb_tunnel_dev)
  1490. unregister_netdevice_queue(dev, head);
  1491. }
  1492. static struct rtnl_link_ops sit_link_ops __read_mostly = {
  1493. .kind = "sit",
  1494. .maxtype = IFLA_IPTUN_MAX,
  1495. .policy = ipip6_policy,
  1496. .priv_size = sizeof(struct ip_tunnel),
  1497. .setup = ipip6_tunnel_setup,
  1498. .validate = ipip6_validate,
  1499. .newlink = ipip6_newlink,
  1500. .changelink = ipip6_changelink,
  1501. .get_size = ipip6_get_size,
  1502. .fill_info = ipip6_fill_info,
  1503. .dellink = ipip6_dellink,
  1504. .get_link_net = ip_tunnel_get_link_net,
  1505. };
  1506. static struct xfrm_tunnel sit_handler __read_mostly = {
  1507. .handler = ipip6_rcv,
  1508. .err_handler = ipip6_err,
  1509. .priority = 1,
  1510. };
  1511. static struct xfrm_tunnel ipip_handler __read_mostly = {
  1512. .handler = ipip_rcv,
  1513. .err_handler = ipip6_err,
  1514. .priority = 2,
  1515. };
  1516. #if IS_ENABLED(CONFIG_MPLS)
  1517. static struct xfrm_tunnel mplsip_handler __read_mostly = {
  1518. .handler = mplsip_rcv,
  1519. .err_handler = ipip6_err,
  1520. .priority = 2,
  1521. };
  1522. #endif
  1523. static void __net_exit sit_destroy_tunnels(struct net *net,
  1524. struct list_head *head)
  1525. {
  1526. struct sit_net *sitn = net_generic(net, sit_net_id);
  1527. struct net_device *dev, *aux;
  1528. int prio;
  1529. for_each_netdev_safe(net, dev, aux)
  1530. if (dev->rtnl_link_ops == &sit_link_ops)
  1531. unregister_netdevice_queue(dev, head);
  1532. for (prio = 1; prio < 4; prio++) {
  1533. int h;
  1534. for (h = 0; h < IP6_SIT_HASH_SIZE; h++) {
  1535. struct ip_tunnel *t;
  1536. t = rtnl_dereference(sitn->tunnels[prio][h]);
  1537. while (t) {
  1538. /* If dev is in the same netns, it has already
  1539. * been added to the list by the previous loop.
  1540. */
  1541. if (!net_eq(dev_net(t->dev), net))
  1542. unregister_netdevice_queue(t->dev,
  1543. head);
  1544. t = rtnl_dereference(t->next);
  1545. }
  1546. }
  1547. }
  1548. }
  1549. static int __net_init sit_init_net(struct net *net)
  1550. {
  1551. struct sit_net *sitn = net_generic(net, sit_net_id);
  1552. struct ip_tunnel *t;
  1553. int err;
  1554. sitn->tunnels[0] = sitn->tunnels_wc;
  1555. sitn->tunnels[1] = sitn->tunnels_l;
  1556. sitn->tunnels[2] = sitn->tunnels_r;
  1557. sitn->tunnels[3] = sitn->tunnels_r_l;
  1558. if (!net_has_fallback_tunnels(net))
  1559. return 0;
  1560. sitn->fb_tunnel_dev = alloc_netdev(sizeof(struct ip_tunnel), "sit0",
  1561. NET_NAME_UNKNOWN,
  1562. ipip6_tunnel_setup);
  1563. if (!sitn->fb_tunnel_dev) {
  1564. err = -ENOMEM;
  1565. goto err_alloc_dev;
  1566. }
  1567. dev_net_set(sitn->fb_tunnel_dev, net);
  1568. sitn->fb_tunnel_dev->rtnl_link_ops = &sit_link_ops;
  1569. /* FB netdevice is special: we have one, and only one per netns.
  1570. * Allowing to move it to another netns is clearly unsafe.
  1571. */
  1572. sitn->fb_tunnel_dev->features |= NETIF_F_NETNS_LOCAL;
  1573. err = register_netdev(sitn->fb_tunnel_dev);
  1574. if (err)
  1575. goto err_reg_dev;
  1576. ipip6_tunnel_clone_6rd(sitn->fb_tunnel_dev, sitn);
  1577. ipip6_fb_tunnel_init(sitn->fb_tunnel_dev);
  1578. t = netdev_priv(sitn->fb_tunnel_dev);
  1579. strcpy(t->parms.name, sitn->fb_tunnel_dev->name);
  1580. return 0;
  1581. err_reg_dev:
  1582. ipip6_dev_free(sitn->fb_tunnel_dev);
  1583. err_alloc_dev:
  1584. return err;
  1585. }
  1586. static void __net_exit sit_exit_batch_net(struct list_head *net_list)
  1587. {
  1588. LIST_HEAD(list);
  1589. struct net *net;
  1590. rtnl_lock();
  1591. list_for_each_entry(net, net_list, exit_list)
  1592. sit_destroy_tunnels(net, &list);
  1593. unregister_netdevice_many(&list);
  1594. rtnl_unlock();
  1595. }
  1596. static struct pernet_operations sit_net_ops = {
  1597. .init = sit_init_net,
  1598. .exit_batch = sit_exit_batch_net,
  1599. .id = &sit_net_id,
  1600. .size = sizeof(struct sit_net),
  1601. };
  1602. static void __exit sit_cleanup(void)
  1603. {
  1604. rtnl_link_unregister(&sit_link_ops);
  1605. xfrm4_tunnel_deregister(&sit_handler, AF_INET6);
  1606. xfrm4_tunnel_deregister(&ipip_handler, AF_INET);
  1607. #if IS_ENABLED(CONFIG_MPLS)
  1608. xfrm4_tunnel_deregister(&mplsip_handler, AF_MPLS);
  1609. #endif
  1610. unregister_pernet_device(&sit_net_ops);
  1611. rcu_barrier(); /* Wait for completion of call_rcu()'s */
  1612. }
  1613. static int __init sit_init(void)
  1614. {
  1615. int err;
  1616. pr_info("IPv6, IPv4 and MPLS over IPv4 tunneling driver\n");
  1617. err = register_pernet_device(&sit_net_ops);
  1618. if (err < 0)
  1619. return err;
  1620. err = xfrm4_tunnel_register(&sit_handler, AF_INET6);
  1621. if (err < 0) {
  1622. pr_info("%s: can't register ip6ip4\n", __func__);
  1623. goto xfrm_tunnel_failed;
  1624. }
  1625. err = xfrm4_tunnel_register(&ipip_handler, AF_INET);
  1626. if (err < 0) {
  1627. pr_info("%s: can't register ip4ip4\n", __func__);
  1628. goto xfrm_tunnel4_failed;
  1629. }
  1630. #if IS_ENABLED(CONFIG_MPLS)
  1631. err = xfrm4_tunnel_register(&mplsip_handler, AF_MPLS);
  1632. if (err < 0) {
  1633. pr_info("%s: can't register mplsip\n", __func__);
  1634. goto xfrm_tunnel_mpls_failed;
  1635. }
  1636. #endif
  1637. err = rtnl_link_register(&sit_link_ops);
  1638. if (err < 0)
  1639. goto rtnl_link_failed;
  1640. out:
  1641. return err;
  1642. rtnl_link_failed:
  1643. #if IS_ENABLED(CONFIG_MPLS)
  1644. xfrm4_tunnel_deregister(&mplsip_handler, AF_MPLS);
  1645. xfrm_tunnel_mpls_failed:
  1646. #endif
  1647. xfrm4_tunnel_deregister(&ipip_handler, AF_INET);
  1648. xfrm_tunnel4_failed:
  1649. xfrm4_tunnel_deregister(&sit_handler, AF_INET6);
  1650. xfrm_tunnel_failed:
  1651. unregister_pernet_device(&sit_net_ops);
  1652. goto out;
  1653. }
  1654. module_init(sit_init);
  1655. module_exit(sit_cleanup);
  1656. MODULE_LICENSE("GPL");
  1657. MODULE_ALIAS_RTNL_LINK("sit");
  1658. MODULE_ALIAS_NETDEV("sit0");