ip6_flowlabel.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * ip6_flowlabel.c IPv6 flowlabel manager.
  4. *
  5. * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
  6. */
  7. #include <linux/capability.h>
  8. #include <linux/errno.h>
  9. #include <linux/types.h>
  10. #include <linux/socket.h>
  11. #include <linux/net.h>
  12. #include <linux/netdevice.h>
  13. #include <linux/in6.h>
  14. #include <linux/proc_fs.h>
  15. #include <linux/seq_file.h>
  16. #include <linux/slab.h>
  17. #include <linux/export.h>
  18. #include <linux/pid_namespace.h>
  19. #include <linux/jump_label_ratelimit.h>
  20. #include <net/net_namespace.h>
  21. #include <net/sock.h>
  22. #include <net/ipv6.h>
  23. #include <net/rawv6.h>
  24. #include <net/transp_v6.h>
  25. #include <linux/uaccess.h>
  26. #define FL_MIN_LINGER 6 /* Minimal linger. It is set to 6sec specified
  27. in old IPv6 RFC. Well, it was reasonable value.
  28. */
  29. #define FL_MAX_LINGER 150 /* Maximal linger timeout */
  30. /* FL hash table */
  31. #define FL_MAX_PER_SOCK 32
  32. #define FL_MAX_SIZE 4096
  33. #define FL_HASH_MASK 255
  34. #define FL_HASH(l) (ntohl(l)&FL_HASH_MASK)
  35. static atomic_t fl_size = ATOMIC_INIT(0);
  36. static struct ip6_flowlabel __rcu *fl_ht[FL_HASH_MASK+1];
  37. static void ip6_fl_gc(struct timer_list *unused);
  38. static DEFINE_TIMER(ip6_fl_gc_timer, ip6_fl_gc);
  39. /* FL hash table lock: it protects only of GC */
  40. static DEFINE_SPINLOCK(ip6_fl_lock);
  41. /* Big socket sock */
  42. static DEFINE_SPINLOCK(ip6_sk_fl_lock);
  43. DEFINE_STATIC_KEY_DEFERRED_FALSE(ipv6_flowlabel_exclusive, HZ);
  44. EXPORT_SYMBOL(ipv6_flowlabel_exclusive);
  45. #define for_each_fl_rcu(hash, fl) \
  46. for (fl = rcu_dereference(fl_ht[(hash)]); \
  47. fl != NULL; \
  48. fl = rcu_dereference(fl->next))
  49. #define for_each_fl_continue_rcu(fl) \
  50. for (fl = rcu_dereference(fl->next); \
  51. fl != NULL; \
  52. fl = rcu_dereference(fl->next))
  53. #define for_each_sk_fl_rcu(np, sfl) \
  54. for (sfl = rcu_dereference(np->ipv6_fl_list); \
  55. sfl != NULL; \
  56. sfl = rcu_dereference(sfl->next))
  57. static inline struct ip6_flowlabel *__fl_lookup(struct net *net, __be32 label)
  58. {
  59. struct ip6_flowlabel *fl;
  60. for_each_fl_rcu(FL_HASH(label), fl) {
  61. if (fl->label == label && net_eq(fl->fl_net, net))
  62. return fl;
  63. }
  64. return NULL;
  65. }
  66. static struct ip6_flowlabel *fl_lookup(struct net *net, __be32 label)
  67. {
  68. struct ip6_flowlabel *fl;
  69. rcu_read_lock();
  70. fl = __fl_lookup(net, label);
  71. if (fl && !atomic_inc_not_zero(&fl->users))
  72. fl = NULL;
  73. rcu_read_unlock();
  74. return fl;
  75. }
  76. static bool fl_shared_exclusive(struct ip6_flowlabel *fl)
  77. {
  78. return fl->share == IPV6_FL_S_EXCL ||
  79. fl->share == IPV6_FL_S_PROCESS ||
  80. fl->share == IPV6_FL_S_USER;
  81. }
  82. static void fl_free_rcu(struct rcu_head *head)
  83. {
  84. struct ip6_flowlabel *fl = container_of(head, struct ip6_flowlabel, rcu);
  85. if (fl->share == IPV6_FL_S_PROCESS)
  86. put_pid(fl->owner.pid);
  87. kfree(fl->opt);
  88. kfree(fl);
  89. }
  90. static void fl_free(struct ip6_flowlabel *fl)
  91. {
  92. if (!fl)
  93. return;
  94. if (fl_shared_exclusive(fl) || fl->opt)
  95. static_branch_slow_dec_deferred(&ipv6_flowlabel_exclusive);
  96. call_rcu(&fl->rcu, fl_free_rcu);
  97. }
  98. static void fl_release(struct ip6_flowlabel *fl)
  99. {
  100. spin_lock_bh(&ip6_fl_lock);
  101. fl->lastuse = jiffies;
  102. if (atomic_dec_and_test(&fl->users)) {
  103. unsigned long ttd = fl->lastuse + fl->linger;
  104. if (time_after(ttd, fl->expires))
  105. fl->expires = ttd;
  106. ttd = fl->expires;
  107. if (fl->opt && fl->share == IPV6_FL_S_EXCL) {
  108. struct ipv6_txoptions *opt = fl->opt;
  109. fl->opt = NULL;
  110. kfree(opt);
  111. }
  112. if (!timer_pending(&ip6_fl_gc_timer) ||
  113. time_after(ip6_fl_gc_timer.expires, ttd))
  114. mod_timer(&ip6_fl_gc_timer, ttd);
  115. }
  116. spin_unlock_bh(&ip6_fl_lock);
  117. }
  118. static void ip6_fl_gc(struct timer_list *unused)
  119. {
  120. int i;
  121. unsigned long now = jiffies;
  122. unsigned long sched = 0;
  123. spin_lock(&ip6_fl_lock);
  124. for (i = 0; i <= FL_HASH_MASK; i++) {
  125. struct ip6_flowlabel *fl;
  126. struct ip6_flowlabel __rcu **flp;
  127. flp = &fl_ht[i];
  128. while ((fl = rcu_dereference_protected(*flp,
  129. lockdep_is_held(&ip6_fl_lock))) != NULL) {
  130. if (atomic_read(&fl->users) == 0) {
  131. unsigned long ttd = fl->lastuse + fl->linger;
  132. if (time_after(ttd, fl->expires))
  133. fl->expires = ttd;
  134. ttd = fl->expires;
  135. if (time_after_eq(now, ttd)) {
  136. *flp = fl->next;
  137. fl_free(fl);
  138. atomic_dec(&fl_size);
  139. continue;
  140. }
  141. if (!sched || time_before(ttd, sched))
  142. sched = ttd;
  143. }
  144. flp = &fl->next;
  145. }
  146. }
  147. if (!sched && atomic_read(&fl_size))
  148. sched = now + FL_MAX_LINGER;
  149. if (sched) {
  150. mod_timer(&ip6_fl_gc_timer, sched);
  151. }
  152. spin_unlock(&ip6_fl_lock);
  153. }
  154. static void __net_exit ip6_fl_purge(struct net *net)
  155. {
  156. int i;
  157. spin_lock_bh(&ip6_fl_lock);
  158. for (i = 0; i <= FL_HASH_MASK; i++) {
  159. struct ip6_flowlabel *fl;
  160. struct ip6_flowlabel __rcu **flp;
  161. flp = &fl_ht[i];
  162. while ((fl = rcu_dereference_protected(*flp,
  163. lockdep_is_held(&ip6_fl_lock))) != NULL) {
  164. if (net_eq(fl->fl_net, net) &&
  165. atomic_read(&fl->users) == 0) {
  166. *flp = fl->next;
  167. fl_free(fl);
  168. atomic_dec(&fl_size);
  169. continue;
  170. }
  171. flp = &fl->next;
  172. }
  173. }
  174. spin_unlock_bh(&ip6_fl_lock);
  175. }
  176. static struct ip6_flowlabel *fl_intern(struct net *net,
  177. struct ip6_flowlabel *fl, __be32 label)
  178. {
  179. struct ip6_flowlabel *lfl;
  180. fl->label = label & IPV6_FLOWLABEL_MASK;
  181. rcu_read_lock();
  182. spin_lock_bh(&ip6_fl_lock);
  183. if (label == 0) {
  184. for (;;) {
  185. fl->label = htonl(get_random_u32())&IPV6_FLOWLABEL_MASK;
  186. if (fl->label) {
  187. lfl = __fl_lookup(net, fl->label);
  188. if (!lfl)
  189. break;
  190. }
  191. }
  192. } else {
  193. /*
  194. * we dropper the ip6_fl_lock, so this entry could reappear
  195. * and we need to recheck with it.
  196. *
  197. * OTOH no need to search the active socket first, like it is
  198. * done in ipv6_flowlabel_opt - sock is locked, so new entry
  199. * with the same label can only appear on another sock
  200. */
  201. lfl = __fl_lookup(net, fl->label);
  202. if (lfl) {
  203. atomic_inc(&lfl->users);
  204. spin_unlock_bh(&ip6_fl_lock);
  205. rcu_read_unlock();
  206. return lfl;
  207. }
  208. }
  209. fl->lastuse = jiffies;
  210. fl->next = fl_ht[FL_HASH(fl->label)];
  211. rcu_assign_pointer(fl_ht[FL_HASH(fl->label)], fl);
  212. atomic_inc(&fl_size);
  213. spin_unlock_bh(&ip6_fl_lock);
  214. rcu_read_unlock();
  215. return NULL;
  216. }
  217. /* Socket flowlabel lists */
  218. struct ip6_flowlabel *__fl6_sock_lookup(struct sock *sk, __be32 label)
  219. {
  220. struct ipv6_fl_socklist *sfl;
  221. struct ipv6_pinfo *np = inet6_sk(sk);
  222. label &= IPV6_FLOWLABEL_MASK;
  223. rcu_read_lock();
  224. for_each_sk_fl_rcu(np, sfl) {
  225. struct ip6_flowlabel *fl = sfl->fl;
  226. if (fl->label == label && atomic_inc_not_zero(&fl->users)) {
  227. fl->lastuse = jiffies;
  228. rcu_read_unlock();
  229. return fl;
  230. }
  231. }
  232. rcu_read_unlock();
  233. return NULL;
  234. }
  235. EXPORT_SYMBOL_GPL(__fl6_sock_lookup);
  236. void fl6_free_socklist(struct sock *sk)
  237. {
  238. struct ipv6_pinfo *np = inet6_sk(sk);
  239. struct ipv6_fl_socklist *sfl;
  240. if (!rcu_access_pointer(np->ipv6_fl_list))
  241. return;
  242. spin_lock_bh(&ip6_sk_fl_lock);
  243. while ((sfl = rcu_dereference_protected(np->ipv6_fl_list,
  244. lockdep_is_held(&ip6_sk_fl_lock))) != NULL) {
  245. np->ipv6_fl_list = sfl->next;
  246. spin_unlock_bh(&ip6_sk_fl_lock);
  247. fl_release(sfl->fl);
  248. kfree_rcu(sfl, rcu);
  249. spin_lock_bh(&ip6_sk_fl_lock);
  250. }
  251. spin_unlock_bh(&ip6_sk_fl_lock);
  252. }
  253. /* Service routines */
  254. /*
  255. It is the only difficult place. flowlabel enforces equal headers
  256. before and including routing header, however user may supply options
  257. following rthdr.
  258. */
  259. struct ipv6_txoptions *fl6_merge_options(struct ipv6_txoptions *opt_space,
  260. struct ip6_flowlabel *fl,
  261. struct ipv6_txoptions *fopt)
  262. {
  263. struct ipv6_txoptions *fl_opt = fl->opt;
  264. if (!fopt || fopt->opt_flen == 0)
  265. return fl_opt;
  266. if (fl_opt) {
  267. opt_space->hopopt = fl_opt->hopopt;
  268. opt_space->dst0opt = fl_opt->dst0opt;
  269. opt_space->srcrt = fl_opt->srcrt;
  270. opt_space->opt_nflen = fl_opt->opt_nflen;
  271. } else {
  272. if (fopt->opt_nflen == 0)
  273. return fopt;
  274. opt_space->hopopt = NULL;
  275. opt_space->dst0opt = NULL;
  276. opt_space->srcrt = NULL;
  277. opt_space->opt_nflen = 0;
  278. }
  279. opt_space->dst1opt = fopt->dst1opt;
  280. opt_space->opt_flen = fopt->opt_flen;
  281. opt_space->tot_len = fopt->tot_len;
  282. return opt_space;
  283. }
  284. EXPORT_SYMBOL_GPL(fl6_merge_options);
  285. static unsigned long check_linger(unsigned long ttl)
  286. {
  287. if (ttl < FL_MIN_LINGER)
  288. return FL_MIN_LINGER*HZ;
  289. if (ttl > FL_MAX_LINGER && !capable(CAP_NET_ADMIN))
  290. return 0;
  291. return ttl*HZ;
  292. }
  293. static int fl6_renew(struct ip6_flowlabel *fl, unsigned long linger, unsigned long expires)
  294. {
  295. linger = check_linger(linger);
  296. if (!linger)
  297. return -EPERM;
  298. expires = check_linger(expires);
  299. if (!expires)
  300. return -EPERM;
  301. spin_lock_bh(&ip6_fl_lock);
  302. fl->lastuse = jiffies;
  303. if (time_before(fl->linger, linger))
  304. fl->linger = linger;
  305. if (time_before(expires, fl->linger))
  306. expires = fl->linger;
  307. if (time_before(fl->expires, fl->lastuse + expires))
  308. fl->expires = fl->lastuse + expires;
  309. spin_unlock_bh(&ip6_fl_lock);
  310. return 0;
  311. }
  312. static struct ip6_flowlabel *
  313. fl_create(struct net *net, struct sock *sk, struct in6_flowlabel_req *freq,
  314. sockptr_t optval, int optlen, int *err_p)
  315. {
  316. struct ip6_flowlabel *fl = NULL;
  317. int olen;
  318. int addr_type;
  319. int err;
  320. olen = optlen - CMSG_ALIGN(sizeof(*freq));
  321. err = -EINVAL;
  322. if (olen > 64 * 1024)
  323. goto done;
  324. err = -ENOMEM;
  325. fl = kzalloc(sizeof(*fl), GFP_KERNEL);
  326. if (!fl)
  327. goto done;
  328. if (olen > 0) {
  329. struct msghdr msg;
  330. struct flowi6 flowi6;
  331. struct ipcm6_cookie ipc6;
  332. err = -ENOMEM;
  333. fl->opt = kmalloc(sizeof(*fl->opt) + olen, GFP_KERNEL);
  334. if (!fl->opt)
  335. goto done;
  336. memset(fl->opt, 0, sizeof(*fl->opt));
  337. fl->opt->tot_len = sizeof(*fl->opt) + olen;
  338. err = -EFAULT;
  339. if (copy_from_sockptr_offset(fl->opt + 1, optval,
  340. CMSG_ALIGN(sizeof(*freq)), olen))
  341. goto done;
  342. msg.msg_controllen = olen;
  343. msg.msg_control = (void *)(fl->opt+1);
  344. memset(&flowi6, 0, sizeof(flowi6));
  345. ipc6.opt = fl->opt;
  346. err = ip6_datagram_send_ctl(net, sk, &msg, &flowi6, &ipc6);
  347. if (err)
  348. goto done;
  349. err = -EINVAL;
  350. if (fl->opt->opt_flen)
  351. goto done;
  352. if (fl->opt->opt_nflen == 0) {
  353. kfree(fl->opt);
  354. fl->opt = NULL;
  355. }
  356. }
  357. fl->fl_net = net;
  358. fl->expires = jiffies;
  359. err = fl6_renew(fl, freq->flr_linger, freq->flr_expires);
  360. if (err)
  361. goto done;
  362. fl->share = freq->flr_share;
  363. addr_type = ipv6_addr_type(&freq->flr_dst);
  364. if ((addr_type & IPV6_ADDR_MAPPED) ||
  365. addr_type == IPV6_ADDR_ANY) {
  366. err = -EINVAL;
  367. goto done;
  368. }
  369. fl->dst = freq->flr_dst;
  370. atomic_set(&fl->users, 1);
  371. switch (fl->share) {
  372. case IPV6_FL_S_EXCL:
  373. case IPV6_FL_S_ANY:
  374. break;
  375. case IPV6_FL_S_PROCESS:
  376. fl->owner.pid = get_task_pid(current, PIDTYPE_PID);
  377. break;
  378. case IPV6_FL_S_USER:
  379. fl->owner.uid = current_euid();
  380. break;
  381. default:
  382. err = -EINVAL;
  383. goto done;
  384. }
  385. if (fl_shared_exclusive(fl) || fl->opt) {
  386. WRITE_ONCE(sock_net(sk)->ipv6.flowlabel_has_excl, 1);
  387. static_branch_deferred_inc(&ipv6_flowlabel_exclusive);
  388. }
  389. return fl;
  390. done:
  391. if (fl) {
  392. kfree(fl->opt);
  393. kfree(fl);
  394. }
  395. *err_p = err;
  396. return NULL;
  397. }
  398. static int mem_check(struct sock *sk)
  399. {
  400. struct ipv6_pinfo *np = inet6_sk(sk);
  401. struct ipv6_fl_socklist *sfl;
  402. int room = FL_MAX_SIZE - atomic_read(&fl_size);
  403. int count = 0;
  404. if (room > FL_MAX_SIZE - FL_MAX_PER_SOCK)
  405. return 0;
  406. rcu_read_lock();
  407. for_each_sk_fl_rcu(np, sfl)
  408. count++;
  409. rcu_read_unlock();
  410. if (room <= 0 ||
  411. ((count >= FL_MAX_PER_SOCK ||
  412. (count > 0 && room < FL_MAX_SIZE/2) || room < FL_MAX_SIZE/4) &&
  413. !capable(CAP_NET_ADMIN)))
  414. return -ENOBUFS;
  415. return 0;
  416. }
  417. static inline void fl_link(struct ipv6_pinfo *np, struct ipv6_fl_socklist *sfl,
  418. struct ip6_flowlabel *fl)
  419. {
  420. spin_lock_bh(&ip6_sk_fl_lock);
  421. sfl->fl = fl;
  422. sfl->next = np->ipv6_fl_list;
  423. rcu_assign_pointer(np->ipv6_fl_list, sfl);
  424. spin_unlock_bh(&ip6_sk_fl_lock);
  425. }
  426. int ipv6_flowlabel_opt_get(struct sock *sk, struct in6_flowlabel_req *freq,
  427. int flags)
  428. {
  429. struct ipv6_pinfo *np = inet6_sk(sk);
  430. struct ipv6_fl_socklist *sfl;
  431. if (flags & IPV6_FL_F_REMOTE) {
  432. freq->flr_label = np->rcv_flowinfo & IPV6_FLOWLABEL_MASK;
  433. return 0;
  434. }
  435. if (inet6_test_bit(REPFLOW, sk)) {
  436. freq->flr_label = np->flow_label;
  437. return 0;
  438. }
  439. rcu_read_lock();
  440. for_each_sk_fl_rcu(np, sfl) {
  441. if (sfl->fl->label == (np->flow_label & IPV6_FLOWLABEL_MASK)) {
  442. spin_lock_bh(&ip6_fl_lock);
  443. freq->flr_label = sfl->fl->label;
  444. freq->flr_dst = sfl->fl->dst;
  445. freq->flr_share = sfl->fl->share;
  446. freq->flr_expires = (sfl->fl->expires - jiffies) / HZ;
  447. freq->flr_linger = sfl->fl->linger / HZ;
  448. spin_unlock_bh(&ip6_fl_lock);
  449. rcu_read_unlock();
  450. return 0;
  451. }
  452. }
  453. rcu_read_unlock();
  454. return -ENOENT;
  455. }
  456. #define socklist_dereference(__sflp) \
  457. rcu_dereference_protected(__sflp, lockdep_is_held(&ip6_sk_fl_lock))
  458. static int ipv6_flowlabel_put(struct sock *sk, struct in6_flowlabel_req *freq)
  459. {
  460. struct ipv6_pinfo *np = inet6_sk(sk);
  461. struct ipv6_fl_socklist __rcu **sflp;
  462. struct ipv6_fl_socklist *sfl;
  463. if (freq->flr_flags & IPV6_FL_F_REFLECT) {
  464. if (sk->sk_protocol != IPPROTO_TCP)
  465. return -ENOPROTOOPT;
  466. if (!inet6_test_bit(REPFLOW, sk))
  467. return -ESRCH;
  468. np->flow_label = 0;
  469. inet6_clear_bit(REPFLOW, sk);
  470. return 0;
  471. }
  472. spin_lock_bh(&ip6_sk_fl_lock);
  473. for (sflp = &np->ipv6_fl_list;
  474. (sfl = socklist_dereference(*sflp)) != NULL;
  475. sflp = &sfl->next) {
  476. if (sfl->fl->label == freq->flr_label)
  477. goto found;
  478. }
  479. spin_unlock_bh(&ip6_sk_fl_lock);
  480. return -ESRCH;
  481. found:
  482. if (freq->flr_label == (np->flow_label & IPV6_FLOWLABEL_MASK))
  483. np->flow_label &= ~IPV6_FLOWLABEL_MASK;
  484. *sflp = sfl->next;
  485. spin_unlock_bh(&ip6_sk_fl_lock);
  486. fl_release(sfl->fl);
  487. kfree_rcu(sfl, rcu);
  488. return 0;
  489. }
  490. static int ipv6_flowlabel_renew(struct sock *sk, struct in6_flowlabel_req *freq)
  491. {
  492. struct ipv6_pinfo *np = inet6_sk(sk);
  493. struct net *net = sock_net(sk);
  494. struct ipv6_fl_socklist *sfl;
  495. int err;
  496. rcu_read_lock();
  497. for_each_sk_fl_rcu(np, sfl) {
  498. if (sfl->fl->label == freq->flr_label) {
  499. err = fl6_renew(sfl->fl, freq->flr_linger,
  500. freq->flr_expires);
  501. rcu_read_unlock();
  502. return err;
  503. }
  504. }
  505. rcu_read_unlock();
  506. if (freq->flr_share == IPV6_FL_S_NONE &&
  507. ns_capable(net->user_ns, CAP_NET_ADMIN)) {
  508. struct ip6_flowlabel *fl = fl_lookup(net, freq->flr_label);
  509. if (fl) {
  510. err = fl6_renew(fl, freq->flr_linger,
  511. freq->flr_expires);
  512. fl_release(fl);
  513. return err;
  514. }
  515. }
  516. return -ESRCH;
  517. }
  518. static int ipv6_flowlabel_get(struct sock *sk, struct in6_flowlabel_req *freq,
  519. sockptr_t optval, int optlen)
  520. {
  521. struct ipv6_fl_socklist *sfl, *sfl1 = NULL;
  522. struct ip6_flowlabel *fl, *fl1 = NULL;
  523. struct ipv6_pinfo *np = inet6_sk(sk);
  524. struct net *net = sock_net(sk);
  525. int err;
  526. if (freq->flr_flags & IPV6_FL_F_REFLECT) {
  527. if (net->ipv6.sysctl.flowlabel_consistency) {
  528. net_info_ratelimited("Can not set IPV6_FL_F_REFLECT if flowlabel_consistency sysctl is enable\n");
  529. return -EPERM;
  530. }
  531. if (sk->sk_protocol != IPPROTO_TCP)
  532. return -ENOPROTOOPT;
  533. inet6_set_bit(REPFLOW, sk);
  534. return 0;
  535. }
  536. if (freq->flr_label & ~IPV6_FLOWLABEL_MASK)
  537. return -EINVAL;
  538. if (net->ipv6.sysctl.flowlabel_state_ranges &&
  539. (freq->flr_label & IPV6_FLOWLABEL_STATELESS_FLAG))
  540. return -ERANGE;
  541. fl = fl_create(net, sk, freq, optval, optlen, &err);
  542. if (!fl)
  543. return err;
  544. sfl1 = kmalloc(sizeof(*sfl1), GFP_KERNEL);
  545. if (freq->flr_label) {
  546. err = -EEXIST;
  547. rcu_read_lock();
  548. for_each_sk_fl_rcu(np, sfl) {
  549. if (sfl->fl->label == freq->flr_label) {
  550. if (freq->flr_flags & IPV6_FL_F_EXCL) {
  551. rcu_read_unlock();
  552. goto done;
  553. }
  554. fl1 = sfl->fl;
  555. if (!atomic_inc_not_zero(&fl1->users))
  556. fl1 = NULL;
  557. break;
  558. }
  559. }
  560. rcu_read_unlock();
  561. if (!fl1)
  562. fl1 = fl_lookup(net, freq->flr_label);
  563. if (fl1) {
  564. recheck:
  565. err = -EEXIST;
  566. if (freq->flr_flags&IPV6_FL_F_EXCL)
  567. goto release;
  568. err = -EPERM;
  569. if (fl1->share == IPV6_FL_S_EXCL ||
  570. fl1->share != fl->share ||
  571. ((fl1->share == IPV6_FL_S_PROCESS) &&
  572. (fl1->owner.pid != fl->owner.pid)) ||
  573. ((fl1->share == IPV6_FL_S_USER) &&
  574. !uid_eq(fl1->owner.uid, fl->owner.uid)))
  575. goto release;
  576. err = -ENOMEM;
  577. if (!sfl1)
  578. goto release;
  579. if (fl->linger > fl1->linger)
  580. fl1->linger = fl->linger;
  581. if ((long)(fl->expires - fl1->expires) > 0)
  582. fl1->expires = fl->expires;
  583. fl_link(np, sfl1, fl1);
  584. fl_free(fl);
  585. return 0;
  586. release:
  587. fl_release(fl1);
  588. goto done;
  589. }
  590. }
  591. err = -ENOENT;
  592. if (!(freq->flr_flags & IPV6_FL_F_CREATE))
  593. goto done;
  594. err = -ENOMEM;
  595. if (!sfl1)
  596. goto done;
  597. err = mem_check(sk);
  598. if (err != 0)
  599. goto done;
  600. fl1 = fl_intern(net, fl, freq->flr_label);
  601. if (fl1)
  602. goto recheck;
  603. if (!freq->flr_label) {
  604. size_t offset = offsetof(struct in6_flowlabel_req, flr_label);
  605. if (copy_to_sockptr_offset(optval, offset, &fl->label,
  606. sizeof(fl->label))) {
  607. /* Intentionally ignore fault. */
  608. }
  609. }
  610. fl_link(np, sfl1, fl);
  611. return 0;
  612. done:
  613. fl_free(fl);
  614. kfree(sfl1);
  615. return err;
  616. }
  617. int ipv6_flowlabel_opt(struct sock *sk, sockptr_t optval, int optlen)
  618. {
  619. struct in6_flowlabel_req freq;
  620. if (optlen < sizeof(freq))
  621. return -EINVAL;
  622. if (copy_from_sockptr(&freq, optval, sizeof(freq)))
  623. return -EFAULT;
  624. switch (freq.flr_action) {
  625. case IPV6_FL_A_PUT:
  626. return ipv6_flowlabel_put(sk, &freq);
  627. case IPV6_FL_A_RENEW:
  628. return ipv6_flowlabel_renew(sk, &freq);
  629. case IPV6_FL_A_GET:
  630. return ipv6_flowlabel_get(sk, &freq, optval, optlen);
  631. default:
  632. return -EINVAL;
  633. }
  634. }
  635. #ifdef CONFIG_PROC_FS
  636. struct ip6fl_iter_state {
  637. struct seq_net_private p;
  638. struct pid_namespace *pid_ns;
  639. int bucket;
  640. };
  641. #define ip6fl_seq_private(seq) ((struct ip6fl_iter_state *)(seq)->private)
  642. static struct ip6_flowlabel *ip6fl_get_first(struct seq_file *seq)
  643. {
  644. struct ip6_flowlabel *fl = NULL;
  645. struct ip6fl_iter_state *state = ip6fl_seq_private(seq);
  646. struct net *net = seq_file_net(seq);
  647. for (state->bucket = 0; state->bucket <= FL_HASH_MASK; ++state->bucket) {
  648. for_each_fl_rcu(state->bucket, fl) {
  649. if (net_eq(fl->fl_net, net))
  650. goto out;
  651. }
  652. }
  653. fl = NULL;
  654. out:
  655. return fl;
  656. }
  657. static struct ip6_flowlabel *ip6fl_get_next(struct seq_file *seq, struct ip6_flowlabel *fl)
  658. {
  659. struct ip6fl_iter_state *state = ip6fl_seq_private(seq);
  660. struct net *net = seq_file_net(seq);
  661. for_each_fl_continue_rcu(fl) {
  662. if (net_eq(fl->fl_net, net))
  663. goto out;
  664. }
  665. try_again:
  666. if (++state->bucket <= FL_HASH_MASK) {
  667. for_each_fl_rcu(state->bucket, fl) {
  668. if (net_eq(fl->fl_net, net))
  669. goto out;
  670. }
  671. goto try_again;
  672. }
  673. fl = NULL;
  674. out:
  675. return fl;
  676. }
  677. static struct ip6_flowlabel *ip6fl_get_idx(struct seq_file *seq, loff_t pos)
  678. {
  679. struct ip6_flowlabel *fl = ip6fl_get_first(seq);
  680. if (fl)
  681. while (pos && (fl = ip6fl_get_next(seq, fl)) != NULL)
  682. --pos;
  683. return pos ? NULL : fl;
  684. }
  685. static void *ip6fl_seq_start(struct seq_file *seq, loff_t *pos)
  686. __acquires(RCU)
  687. {
  688. struct ip6fl_iter_state *state = ip6fl_seq_private(seq);
  689. state->pid_ns = proc_pid_ns(file_inode(seq->file)->i_sb);
  690. rcu_read_lock();
  691. return *pos ? ip6fl_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
  692. }
  693. static void *ip6fl_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  694. {
  695. struct ip6_flowlabel *fl;
  696. if (v == SEQ_START_TOKEN)
  697. fl = ip6fl_get_first(seq);
  698. else
  699. fl = ip6fl_get_next(seq, v);
  700. ++*pos;
  701. return fl;
  702. }
  703. static void ip6fl_seq_stop(struct seq_file *seq, void *v)
  704. __releases(RCU)
  705. {
  706. rcu_read_unlock();
  707. }
  708. static int ip6fl_seq_show(struct seq_file *seq, void *v)
  709. {
  710. struct ip6fl_iter_state *state = ip6fl_seq_private(seq);
  711. if (v == SEQ_START_TOKEN) {
  712. seq_puts(seq, "Label S Owner Users Linger Expires Dst Opt\n");
  713. } else {
  714. struct ip6_flowlabel *fl = v;
  715. seq_printf(seq,
  716. "%05X %-1d %-6d %-6d %-6ld %-8ld %pi6 %-4d\n",
  717. (unsigned int)ntohl(fl->label),
  718. fl->share,
  719. ((fl->share == IPV6_FL_S_PROCESS) ?
  720. pid_nr_ns(fl->owner.pid, state->pid_ns) :
  721. ((fl->share == IPV6_FL_S_USER) ?
  722. from_kuid_munged(seq_user_ns(seq), fl->owner.uid) :
  723. 0)),
  724. atomic_read(&fl->users),
  725. fl->linger/HZ,
  726. (long)(fl->expires - jiffies)/HZ,
  727. &fl->dst,
  728. fl->opt ? fl->opt->opt_nflen : 0);
  729. }
  730. return 0;
  731. }
  732. static const struct seq_operations ip6fl_seq_ops = {
  733. .start = ip6fl_seq_start,
  734. .next = ip6fl_seq_next,
  735. .stop = ip6fl_seq_stop,
  736. .show = ip6fl_seq_show,
  737. };
  738. static int __net_init ip6_flowlabel_proc_init(struct net *net)
  739. {
  740. if (!proc_create_net("ip6_flowlabel", 0444, net->proc_net,
  741. &ip6fl_seq_ops, sizeof(struct ip6fl_iter_state)))
  742. return -ENOMEM;
  743. return 0;
  744. }
  745. static void __net_exit ip6_flowlabel_proc_fini(struct net *net)
  746. {
  747. remove_proc_entry("ip6_flowlabel", net->proc_net);
  748. }
  749. #else
  750. static inline int ip6_flowlabel_proc_init(struct net *net)
  751. {
  752. return 0;
  753. }
  754. static inline void ip6_flowlabel_proc_fini(struct net *net)
  755. {
  756. }
  757. #endif
  758. static void __net_exit ip6_flowlabel_net_exit(struct net *net)
  759. {
  760. ip6_fl_purge(net);
  761. ip6_flowlabel_proc_fini(net);
  762. }
  763. static struct pernet_operations ip6_flowlabel_net_ops = {
  764. .init = ip6_flowlabel_proc_init,
  765. .exit = ip6_flowlabel_net_exit,
  766. };
  767. int ip6_flowlabel_init(void)
  768. {
  769. return register_pernet_subsys(&ip6_flowlabel_net_ops);
  770. }
  771. void ip6_flowlabel_cleanup(void)
  772. {
  773. static_key_deferred_flush(&ipv6_flowlabel_exclusive);
  774. del_timer(&ip6_fl_gc_timer);
  775. unregister_pernet_subsys(&ip6_flowlabel_net_ops);
  776. }