sit.c 46 KB

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