sit.c 44 KB

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