ip6_flowlabel.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * ip6_flowlabel.c IPv6 flowlabel manager.
  4. *
  5. * Authors: Alexey Kuznetsov, <[email protected]>
  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_bh(fl_ht[(hash)]); \
  47. fl != NULL; \
  48. fl = rcu_dereference_bh(fl->next))
  49. #define for_each_fl_continue_rcu(fl) \
  50. for (fl = rcu_dereference_bh(fl->next); \
  51. fl != NULL; \
  52. fl = rcu_dereference_bh(fl->next))
  53. #define for_each_sk_fl_rcu(np, sfl) \
  54. for (sfl = rcu_dereference_bh(np->ipv6_fl_list); \
  55. sfl != NULL; \
  56. sfl = rcu_dereference_bh(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_bh();
  70. fl = __fl_lookup(net, label);
  71. if (fl && !atomic_inc_not_zero(&fl->users))
  72. fl = NULL;
  73. rcu_read_unlock_bh();
  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. spin_lock_bh(&ip6_fl_lock);
  182. if (label == 0) {
  183. for (;;) {
  184. fl->label = htonl(get_random_u32())&IPV6_FLOWLABEL_MASK;
  185. if (fl->label) {
  186. lfl = __fl_lookup(net, fl->label);
  187. if (!lfl)
  188. break;
  189. }
  190. }
  191. } else {
  192. /*
  193. * we dropper the ip6_fl_lock, so this entry could reappear
  194. * and we need to recheck with it.
  195. *
  196. * OTOH no need to search the active socket first, like it is
  197. * done in ipv6_flowlabel_opt - sock is locked, so new entry
  198. * with the same label can only appear on another sock
  199. */
  200. lfl = __fl_lookup(net, fl->label);
  201. if (lfl) {
  202. atomic_inc(&lfl->users);
  203. spin_unlock_bh(&ip6_fl_lock);
  204. return lfl;
  205. }
  206. }
  207. fl->lastuse = jiffies;
  208. fl->next = fl_ht[FL_HASH(fl->label)];
  209. rcu_assign_pointer(fl_ht[FL_HASH(fl->label)], fl);
  210. atomic_inc(&fl_size);
  211. spin_unlock_bh(&ip6_fl_lock);
  212. return NULL;
  213. }
  214. /* Socket flowlabel lists */
  215. struct ip6_flowlabel *__fl6_sock_lookup(struct sock *sk, __be32 label)
  216. {
  217. struct ipv6_fl_socklist *sfl;
  218. struct ipv6_pinfo *np = inet6_sk(sk);
  219. label &= IPV6_FLOWLABEL_MASK;
  220. rcu_read_lock_bh();
  221. for_each_sk_fl_rcu(np, sfl) {
  222. struct ip6_flowlabel *fl = sfl->fl;
  223. if (fl->label == label && atomic_inc_not_zero(&fl->users)) {
  224. fl->lastuse = jiffies;
  225. rcu_read_unlock_bh();
  226. return fl;
  227. }
  228. }
  229. rcu_read_unlock_bh();
  230. return NULL;
  231. }
  232. EXPORT_SYMBOL_GPL(__fl6_sock_lookup);
  233. void fl6_free_socklist(struct sock *sk)
  234. {
  235. struct ipv6_pinfo *np = inet6_sk(sk);
  236. struct ipv6_fl_socklist *sfl;
  237. if (!rcu_access_pointer(np->ipv6_fl_list))
  238. return;
  239. spin_lock_bh(&ip6_sk_fl_lock);
  240. while ((sfl = rcu_dereference_protected(np->ipv6_fl_list,
  241. lockdep_is_held(&ip6_sk_fl_lock))) != NULL) {
  242. np->ipv6_fl_list = sfl->next;
  243. spin_unlock_bh(&ip6_sk_fl_lock);
  244. fl_release(sfl->fl);
  245. kfree_rcu(sfl, rcu);
  246. spin_lock_bh(&ip6_sk_fl_lock);
  247. }
  248. spin_unlock_bh(&ip6_sk_fl_lock);
  249. }
  250. /* Service routines */
  251. /*
  252. It is the only difficult place. flowlabel enforces equal headers
  253. before and including routing header, however user may supply options
  254. following rthdr.
  255. */
  256. struct ipv6_txoptions *fl6_merge_options(struct ipv6_txoptions *opt_space,
  257. struct ip6_flowlabel *fl,
  258. struct ipv6_txoptions *fopt)
  259. {
  260. struct ipv6_txoptions *fl_opt = fl->opt;
  261. if (!fopt || fopt->opt_flen == 0)
  262. return fl_opt;
  263. if (fl_opt) {
  264. opt_space->hopopt = fl_opt->hopopt;
  265. opt_space->dst0opt = fl_opt->dst0opt;
  266. opt_space->srcrt = fl_opt->srcrt;
  267. opt_space->opt_nflen = fl_opt->opt_nflen;
  268. } else {
  269. if (fopt->opt_nflen == 0)
  270. return fopt;
  271. opt_space->hopopt = NULL;
  272. opt_space->dst0opt = NULL;
  273. opt_space->srcrt = NULL;
  274. opt_space->opt_nflen = 0;
  275. }
  276. opt_space->dst1opt = fopt->dst1opt;
  277. opt_space->opt_flen = fopt->opt_flen;
  278. opt_space->tot_len = fopt->tot_len;
  279. return opt_space;
  280. }
  281. EXPORT_SYMBOL_GPL(fl6_merge_options);
  282. static unsigned long check_linger(unsigned long ttl)
  283. {
  284. if (ttl < FL_MIN_LINGER)
  285. return FL_MIN_LINGER*HZ;
  286. if (ttl > FL_MAX_LINGER && !capable(CAP_NET_ADMIN))
  287. return 0;
  288. return ttl*HZ;
  289. }
  290. static int fl6_renew(struct ip6_flowlabel *fl, unsigned long linger, unsigned long expires)
  291. {
  292. linger = check_linger(linger);
  293. if (!linger)
  294. return -EPERM;
  295. expires = check_linger(expires);
  296. if (!expires)
  297. return -EPERM;
  298. spin_lock_bh(&ip6_fl_lock);
  299. fl->lastuse = jiffies;
  300. if (time_before(fl->linger, linger))
  301. fl->linger = linger;
  302. if (time_before(expires, fl->linger))
  303. expires = fl->linger;
  304. if (time_before(fl->expires, fl->lastuse + expires))
  305. fl->expires = fl->lastuse + expires;
  306. spin_unlock_bh(&ip6_fl_lock);
  307. return 0;
  308. }
  309. static struct ip6_flowlabel *
  310. fl_create(struct net *net, struct sock *sk, struct in6_flowlabel_req *freq,
  311. sockptr_t optval, int optlen, int *err_p)
  312. {
  313. struct ip6_flowlabel *fl = NULL;
  314. int olen;
  315. int addr_type;
  316. int err;
  317. olen = optlen - CMSG_ALIGN(sizeof(*freq));
  318. err = -EINVAL;
  319. if (olen > 64 * 1024)
  320. goto done;
  321. err = -ENOMEM;
  322. fl = kzalloc(sizeof(*fl), GFP_KERNEL);
  323. if (!fl)
  324. goto done;
  325. if (olen > 0) {
  326. struct msghdr msg;
  327. struct flowi6 flowi6;
  328. struct ipcm6_cookie ipc6;
  329. err = -ENOMEM;
  330. fl->opt = kmalloc(sizeof(*fl->opt) + olen, GFP_KERNEL);
  331. if (!fl->opt)
  332. goto done;
  333. memset(fl->opt, 0, sizeof(*fl->opt));
  334. fl->opt->tot_len = sizeof(*fl->opt) + olen;
  335. err = -EFAULT;
  336. if (copy_from_sockptr_offset(fl->opt + 1, optval,
  337. CMSG_ALIGN(sizeof(*freq)), olen))
  338. goto done;
  339. msg.msg_controllen = olen;
  340. msg.msg_control = (void *)(fl->opt+1);
  341. memset(&flowi6, 0, sizeof(flowi6));
  342. ipc6.opt = fl->opt;
  343. err = ip6_datagram_send_ctl(net, sk, &msg, &flowi6, &ipc6);
  344. if (err)
  345. goto done;
  346. err = -EINVAL;
  347. if (fl->opt->opt_flen)
  348. goto done;
  349. if (fl->opt->opt_nflen == 0) {
  350. kfree(fl->opt);
  351. fl->opt = NULL;
  352. }
  353. }
  354. fl->fl_net = net;
  355. fl->expires = jiffies;
  356. err = fl6_renew(fl, freq->flr_linger, freq->flr_expires);
  357. if (err)
  358. goto done;
  359. fl->share = freq->flr_share;
  360. addr_type = ipv6_addr_type(&freq->flr_dst);
  361. if ((addr_type & IPV6_ADDR_MAPPED) ||
  362. addr_type == IPV6_ADDR_ANY) {
  363. err = -EINVAL;
  364. goto done;
  365. }
  366. fl->dst = freq->flr_dst;
  367. atomic_set(&fl->users, 1);
  368. switch (fl->share) {
  369. case IPV6_FL_S_EXCL:
  370. case IPV6_FL_S_ANY:
  371. break;
  372. case IPV6_FL_S_PROCESS:
  373. fl->owner.pid = get_task_pid(current, PIDTYPE_PID);
  374. break;
  375. case IPV6_FL_S_USER:
  376. fl->owner.uid = current_euid();
  377. break;
  378. default:
  379. err = -EINVAL;
  380. goto done;
  381. }
  382. if (fl_shared_exclusive(fl) || fl->opt) {
  383. WRITE_ONCE(sock_net(sk)->ipv6.flowlabel_has_excl, 1);
  384. static_branch_deferred_inc(&ipv6_flowlabel_exclusive);
  385. }
  386. return fl;
  387. done:
  388. if (fl) {
  389. kfree(fl->opt);
  390. kfree(fl);
  391. }
  392. *err_p = err;
  393. return NULL;
  394. }
  395. static int mem_check(struct sock *sk)
  396. {
  397. struct ipv6_pinfo *np = inet6_sk(sk);
  398. struct ipv6_fl_socklist *sfl;
  399. int room = FL_MAX_SIZE - atomic_read(&fl_size);
  400. int count = 0;
  401. if (room > FL_MAX_SIZE - FL_MAX_PER_SOCK)
  402. return 0;
  403. rcu_read_lock_bh();
  404. for_each_sk_fl_rcu(np, sfl)
  405. count++;
  406. rcu_read_unlock_bh();
  407. if (room <= 0 ||
  408. ((count >= FL_MAX_PER_SOCK ||
  409. (count > 0 && room < FL_MAX_SIZE/2) || room < FL_MAX_SIZE/4) &&
  410. !capable(CAP_NET_ADMIN)))
  411. return -ENOBUFS;
  412. return 0;
  413. }
  414. static inline void fl_link(struct ipv6_pinfo *np, struct ipv6_fl_socklist *sfl,
  415. struct ip6_flowlabel *fl)
  416. {
  417. spin_lock_bh(&ip6_sk_fl_lock);
  418. sfl->fl = fl;
  419. sfl->next = np->ipv6_fl_list;
  420. rcu_assign_pointer(np->ipv6_fl_list, sfl);
  421. spin_unlock_bh(&ip6_sk_fl_lock);
  422. }
  423. int ipv6_flowlabel_opt_get(struct sock *sk, struct in6_flowlabel_req *freq,
  424. int flags)
  425. {
  426. struct ipv6_pinfo *np = inet6_sk(sk);
  427. struct ipv6_fl_socklist *sfl;
  428. if (flags & IPV6_FL_F_REMOTE) {
  429. freq->flr_label = np->rcv_flowinfo & IPV6_FLOWLABEL_MASK;
  430. return 0;
  431. }
  432. if (np->repflow) {
  433. freq->flr_label = np->flow_label;
  434. return 0;
  435. }
  436. rcu_read_lock_bh();
  437. for_each_sk_fl_rcu(np, sfl) {
  438. if (sfl->fl->label == (np->flow_label & IPV6_FLOWLABEL_MASK)) {
  439. spin_lock_bh(&ip6_fl_lock);
  440. freq->flr_label = sfl->fl->label;
  441. freq->flr_dst = sfl->fl->dst;
  442. freq->flr_share = sfl->fl->share;
  443. freq->flr_expires = (sfl->fl->expires - jiffies) / HZ;
  444. freq->flr_linger = sfl->fl->linger / HZ;
  445. spin_unlock_bh(&ip6_fl_lock);
  446. rcu_read_unlock_bh();
  447. return 0;
  448. }
  449. }
  450. rcu_read_unlock_bh();
  451. return -ENOENT;
  452. }
  453. #define socklist_dereference(__sflp) \
  454. rcu_dereference_protected(__sflp, lockdep_is_held(&ip6_sk_fl_lock))
  455. static int ipv6_flowlabel_put(struct sock *sk, struct in6_flowlabel_req *freq)
  456. {
  457. struct ipv6_pinfo *np = inet6_sk(sk);
  458. struct ipv6_fl_socklist __rcu **sflp;
  459. struct ipv6_fl_socklist *sfl;
  460. if (freq->flr_flags & IPV6_FL_F_REFLECT) {
  461. if (sk->sk_protocol != IPPROTO_TCP)
  462. return -ENOPROTOOPT;
  463. if (!np->repflow)
  464. return -ESRCH;
  465. np->flow_label = 0;
  466. np->repflow = 0;
  467. return 0;
  468. }
  469. spin_lock_bh(&ip6_sk_fl_lock);
  470. for (sflp = &np->ipv6_fl_list;
  471. (sfl = socklist_dereference(*sflp)) != NULL;
  472. sflp = &sfl->next) {
  473. if (sfl->fl->label == freq->flr_label)
  474. goto found;
  475. }
  476. spin_unlock_bh(&ip6_sk_fl_lock);
  477. return -ESRCH;
  478. found:
  479. if (freq->flr_label == (np->flow_label & IPV6_FLOWLABEL_MASK))
  480. np->flow_label &= ~IPV6_FLOWLABEL_MASK;
  481. *sflp = sfl->next;
  482. spin_unlock_bh(&ip6_sk_fl_lock);
  483. fl_release(sfl->fl);
  484. kfree_rcu(sfl, rcu);
  485. return 0;
  486. }
  487. static int ipv6_flowlabel_renew(struct sock *sk, struct in6_flowlabel_req *freq)
  488. {
  489. struct ipv6_pinfo *np = inet6_sk(sk);
  490. struct net *net = sock_net(sk);
  491. struct ipv6_fl_socklist *sfl;
  492. int err;
  493. rcu_read_lock_bh();
  494. for_each_sk_fl_rcu(np, sfl) {
  495. if (sfl->fl->label == freq->flr_label) {
  496. err = fl6_renew(sfl->fl, freq->flr_linger,
  497. freq->flr_expires);
  498. rcu_read_unlock_bh();
  499. return err;
  500. }
  501. }
  502. rcu_read_unlock_bh();
  503. if (freq->flr_share == IPV6_FL_S_NONE &&
  504. ns_capable(net->user_ns, CAP_NET_ADMIN)) {
  505. struct ip6_flowlabel *fl = fl_lookup(net, freq->flr_label);
  506. if (fl) {
  507. err = fl6_renew(fl, freq->flr_linger,
  508. freq->flr_expires);
  509. fl_release(fl);
  510. return err;
  511. }
  512. }
  513. return -ESRCH;
  514. }
  515. static int ipv6_flowlabel_get(struct sock *sk, struct in6_flowlabel_req *freq,
  516. sockptr_t optval, int optlen)
  517. {
  518. struct ipv6_fl_socklist *sfl, *sfl1 = NULL;
  519. struct ip6_flowlabel *fl, *fl1 = NULL;
  520. struct ipv6_pinfo *np = inet6_sk(sk);
  521. struct net *net = sock_net(sk);
  522. int err;
  523. if (freq->flr_flags & IPV6_FL_F_REFLECT) {
  524. if (net->ipv6.sysctl.flowlabel_consistency) {
  525. net_info_ratelimited("Can not set IPV6_FL_F_REFLECT if flowlabel_consistency sysctl is enable\n");
  526. return -EPERM;
  527. }
  528. if (sk->sk_protocol != IPPROTO_TCP)
  529. return -ENOPROTOOPT;
  530. np->repflow = 1;
  531. return 0;
  532. }
  533. if (freq->flr_label & ~IPV6_FLOWLABEL_MASK)
  534. return -EINVAL;
  535. if (net->ipv6.sysctl.flowlabel_state_ranges &&
  536. (freq->flr_label & IPV6_FLOWLABEL_STATELESS_FLAG))
  537. return -ERANGE;
  538. fl = fl_create(net, sk, freq, optval, optlen, &err);
  539. if (!fl)
  540. return err;
  541. sfl1 = kmalloc(sizeof(*sfl1), GFP_KERNEL);
  542. if (freq->flr_label) {
  543. err = -EEXIST;
  544. rcu_read_lock_bh();
  545. for_each_sk_fl_rcu(np, sfl) {
  546. if (sfl->fl->label == freq->flr_label) {
  547. if (freq->flr_flags & IPV6_FL_F_EXCL) {
  548. rcu_read_unlock_bh();
  549. goto done;
  550. }
  551. fl1 = sfl->fl;
  552. if (!atomic_inc_not_zero(&fl1->users))
  553. fl1 = NULL;
  554. break;
  555. }
  556. }
  557. rcu_read_unlock_bh();
  558. if (!fl1)
  559. fl1 = fl_lookup(net, freq->flr_label);
  560. if (fl1) {
  561. recheck:
  562. err = -EEXIST;
  563. if (freq->flr_flags&IPV6_FL_F_EXCL)
  564. goto release;
  565. err = -EPERM;
  566. if (fl1->share == IPV6_FL_S_EXCL ||
  567. fl1->share != fl->share ||
  568. ((fl1->share == IPV6_FL_S_PROCESS) &&
  569. (fl1->owner.pid != fl->owner.pid)) ||
  570. ((fl1->share == IPV6_FL_S_USER) &&
  571. !uid_eq(fl1->owner.uid, fl->owner.uid)))
  572. goto release;
  573. err = -ENOMEM;
  574. if (!sfl1)
  575. goto release;
  576. if (fl->linger > fl1->linger)
  577. fl1->linger = fl->linger;
  578. if ((long)(fl->expires - fl1->expires) > 0)
  579. fl1->expires = fl->expires;
  580. fl_link(np, sfl1, fl1);
  581. fl_free(fl);
  582. return 0;
  583. release:
  584. fl_release(fl1);
  585. goto done;
  586. }
  587. }
  588. err = -ENOENT;
  589. if (!(freq->flr_flags & IPV6_FL_F_CREATE))
  590. goto done;
  591. err = -ENOMEM;
  592. if (!sfl1)
  593. goto done;
  594. err = mem_check(sk);
  595. if (err != 0)
  596. goto done;
  597. fl1 = fl_intern(net, fl, freq->flr_label);
  598. if (fl1)
  599. goto recheck;
  600. if (!freq->flr_label) {
  601. size_t offset = offsetof(struct in6_flowlabel_req, flr_label);
  602. if (copy_to_sockptr_offset(optval, offset, &fl->label,
  603. sizeof(fl->label))) {
  604. /* Intentionally ignore fault. */
  605. }
  606. }
  607. fl_link(np, sfl1, fl);
  608. return 0;
  609. done:
  610. fl_free(fl);
  611. kfree(sfl1);
  612. return err;
  613. }
  614. int ipv6_flowlabel_opt(struct sock *sk, sockptr_t optval, int optlen)
  615. {
  616. struct in6_flowlabel_req freq;
  617. if (optlen < sizeof(freq))
  618. return -EINVAL;
  619. if (copy_from_sockptr(&freq, optval, sizeof(freq)))
  620. return -EFAULT;
  621. switch (freq.flr_action) {
  622. case IPV6_FL_A_PUT:
  623. return ipv6_flowlabel_put(sk, &freq);
  624. case IPV6_FL_A_RENEW:
  625. return ipv6_flowlabel_renew(sk, &freq);
  626. case IPV6_FL_A_GET:
  627. return ipv6_flowlabel_get(sk, &freq, optval, optlen);
  628. default:
  629. return -EINVAL;
  630. }
  631. }
  632. #ifdef CONFIG_PROC_FS
  633. struct ip6fl_iter_state {
  634. struct seq_net_private p;
  635. struct pid_namespace *pid_ns;
  636. int bucket;
  637. };
  638. #define ip6fl_seq_private(seq) ((struct ip6fl_iter_state *)(seq)->private)
  639. static struct ip6_flowlabel *ip6fl_get_first(struct seq_file *seq)
  640. {
  641. struct ip6_flowlabel *fl = NULL;
  642. struct ip6fl_iter_state *state = ip6fl_seq_private(seq);
  643. struct net *net = seq_file_net(seq);
  644. for (state->bucket = 0; state->bucket <= FL_HASH_MASK; ++state->bucket) {
  645. for_each_fl_rcu(state->bucket, fl) {
  646. if (net_eq(fl->fl_net, net))
  647. goto out;
  648. }
  649. }
  650. fl = NULL;
  651. out:
  652. return fl;
  653. }
  654. static struct ip6_flowlabel *ip6fl_get_next(struct seq_file *seq, struct ip6_flowlabel *fl)
  655. {
  656. struct ip6fl_iter_state *state = ip6fl_seq_private(seq);
  657. struct net *net = seq_file_net(seq);
  658. for_each_fl_continue_rcu(fl) {
  659. if (net_eq(fl->fl_net, net))
  660. goto out;
  661. }
  662. try_again:
  663. if (++state->bucket <= FL_HASH_MASK) {
  664. for_each_fl_rcu(state->bucket, fl) {
  665. if (net_eq(fl->fl_net, net))
  666. goto out;
  667. }
  668. goto try_again;
  669. }
  670. fl = NULL;
  671. out:
  672. return fl;
  673. }
  674. static struct ip6_flowlabel *ip6fl_get_idx(struct seq_file *seq, loff_t pos)
  675. {
  676. struct ip6_flowlabel *fl = ip6fl_get_first(seq);
  677. if (fl)
  678. while (pos && (fl = ip6fl_get_next(seq, fl)) != NULL)
  679. --pos;
  680. return pos ? NULL : fl;
  681. }
  682. static void *ip6fl_seq_start(struct seq_file *seq, loff_t *pos)
  683. __acquires(RCU)
  684. {
  685. struct ip6fl_iter_state *state = ip6fl_seq_private(seq);
  686. state->pid_ns = proc_pid_ns(file_inode(seq->file)->i_sb);
  687. rcu_read_lock_bh();
  688. return *pos ? ip6fl_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
  689. }
  690. static void *ip6fl_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  691. {
  692. struct ip6_flowlabel *fl;
  693. if (v == SEQ_START_TOKEN)
  694. fl = ip6fl_get_first(seq);
  695. else
  696. fl = ip6fl_get_next(seq, v);
  697. ++*pos;
  698. return fl;
  699. }
  700. static void ip6fl_seq_stop(struct seq_file *seq, void *v)
  701. __releases(RCU)
  702. {
  703. rcu_read_unlock_bh();
  704. }
  705. static int ip6fl_seq_show(struct seq_file *seq, void *v)
  706. {
  707. struct ip6fl_iter_state *state = ip6fl_seq_private(seq);
  708. if (v == SEQ_START_TOKEN) {
  709. seq_puts(seq, "Label S Owner Users Linger Expires Dst Opt\n");
  710. } else {
  711. struct ip6_flowlabel *fl = v;
  712. seq_printf(seq,
  713. "%05X %-1d %-6d %-6d %-6ld %-8ld %pi6 %-4d\n",
  714. (unsigned int)ntohl(fl->label),
  715. fl->share,
  716. ((fl->share == IPV6_FL_S_PROCESS) ?
  717. pid_nr_ns(fl->owner.pid, state->pid_ns) :
  718. ((fl->share == IPV6_FL_S_USER) ?
  719. from_kuid_munged(seq_user_ns(seq), fl->owner.uid) :
  720. 0)),
  721. atomic_read(&fl->users),
  722. fl->linger/HZ,
  723. (long)(fl->expires - jiffies)/HZ,
  724. &fl->dst,
  725. fl->opt ? fl->opt->opt_nflen : 0);
  726. }
  727. return 0;
  728. }
  729. static const struct seq_operations ip6fl_seq_ops = {
  730. .start = ip6fl_seq_start,
  731. .next = ip6fl_seq_next,
  732. .stop = ip6fl_seq_stop,
  733. .show = ip6fl_seq_show,
  734. };
  735. static int __net_init ip6_flowlabel_proc_init(struct net *net)
  736. {
  737. if (!proc_create_net("ip6_flowlabel", 0444, net->proc_net,
  738. &ip6fl_seq_ops, sizeof(struct ip6fl_iter_state)))
  739. return -ENOMEM;
  740. return 0;
  741. }
  742. static void __net_exit ip6_flowlabel_proc_fini(struct net *net)
  743. {
  744. remove_proc_entry("ip6_flowlabel", net->proc_net);
  745. }
  746. #else
  747. static inline int ip6_flowlabel_proc_init(struct net *net)
  748. {
  749. return 0;
  750. }
  751. static inline void ip6_flowlabel_proc_fini(struct net *net)
  752. {
  753. }
  754. #endif
  755. static void __net_exit ip6_flowlabel_net_exit(struct net *net)
  756. {
  757. ip6_fl_purge(net);
  758. ip6_flowlabel_proc_fini(net);
  759. }
  760. static struct pernet_operations ip6_flowlabel_net_ops = {
  761. .init = ip6_flowlabel_proc_init,
  762. .exit = ip6_flowlabel_net_exit,
  763. };
  764. int ip6_flowlabel_init(void)
  765. {
  766. return register_pernet_subsys(&ip6_flowlabel_net_ops);
  767. }
  768. void ip6_flowlabel_cleanup(void)
  769. {
  770. static_key_deferred_flush(&ipv6_flowlabel_exclusive);
  771. del_timer(&ip6_fl_gc_timer);
  772. unregister_pernet_subsys(&ip6_flowlabel_net_ops);
  773. }