ip6_flowlabel.c 20 KB

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