sit.c 45 KB

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