ip6_flowlabel.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886
  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. return opt_space;
  268. }
  269. EXPORT_SYMBOL_GPL(fl6_merge_options);
  270. static unsigned long check_linger(unsigned long ttl)
  271. {
  272. if (ttl < FL_MIN_LINGER)
  273. return FL_MIN_LINGER*HZ;
  274. if (ttl > FL_MAX_LINGER && !capable(CAP_NET_ADMIN))
  275. return 0;
  276. return ttl*HZ;
  277. }
  278. static int fl6_renew(struct ip6_flowlabel *fl, unsigned long linger, unsigned long expires)
  279. {
  280. linger = check_linger(linger);
  281. if (!linger)
  282. return -EPERM;
  283. expires = check_linger(expires);
  284. if (!expires)
  285. return -EPERM;
  286. spin_lock_bh(&ip6_fl_lock);
  287. fl->lastuse = jiffies;
  288. if (time_before(fl->linger, linger))
  289. fl->linger = linger;
  290. if (time_before(expires, fl->linger))
  291. expires = fl->linger;
  292. if (time_before(fl->expires, fl->lastuse + expires))
  293. fl->expires = fl->lastuse + expires;
  294. spin_unlock_bh(&ip6_fl_lock);
  295. return 0;
  296. }
  297. static struct ip6_flowlabel *
  298. fl_create(struct net *net, struct sock *sk, struct in6_flowlabel_req *freq,
  299. char __user *optval, int optlen, int *err_p)
  300. {
  301. struct ip6_flowlabel *fl = NULL;
  302. int olen;
  303. int addr_type;
  304. int err;
  305. olen = optlen - CMSG_ALIGN(sizeof(*freq));
  306. err = -EINVAL;
  307. if (olen > 64 * 1024)
  308. goto done;
  309. err = -ENOMEM;
  310. fl = kzalloc(sizeof(*fl), GFP_KERNEL);
  311. if (!fl)
  312. goto done;
  313. if (olen > 0) {
  314. struct msghdr msg;
  315. struct flowi6 flowi6;
  316. int junk;
  317. err = -ENOMEM;
  318. fl->opt = kmalloc(sizeof(*fl->opt) + olen, GFP_KERNEL);
  319. if (!fl->opt)
  320. goto done;
  321. memset(fl->opt, 0, sizeof(*fl->opt));
  322. fl->opt->tot_len = sizeof(*fl->opt) + olen;
  323. err = -EFAULT;
  324. if (copy_from_user(fl->opt+1, optval+CMSG_ALIGN(sizeof(*freq)), olen))
  325. goto done;
  326. msg.msg_controllen = olen;
  327. msg.msg_control = (void *)(fl->opt+1);
  328. memset(&flowi6, 0, sizeof(flowi6));
  329. err = ip6_datagram_send_ctl(net, sk, &msg, &flowi6, fl->opt,
  330. &junk, &junk, &junk);
  331. if (err)
  332. goto done;
  333. err = -EINVAL;
  334. if (fl->opt->opt_flen)
  335. goto done;
  336. if (fl->opt->opt_nflen == 0) {
  337. kfree(fl->opt);
  338. fl->opt = NULL;
  339. }
  340. }
  341. fl->fl_net = net;
  342. fl->expires = jiffies;
  343. err = fl6_renew(fl, freq->flr_linger, freq->flr_expires);
  344. if (err)
  345. goto done;
  346. fl->share = freq->flr_share;
  347. addr_type = ipv6_addr_type(&freq->flr_dst);
  348. if ((addr_type & IPV6_ADDR_MAPPED) ||
  349. addr_type == IPV6_ADDR_ANY) {
  350. err = -EINVAL;
  351. goto done;
  352. }
  353. fl->dst = freq->flr_dst;
  354. atomic_set(&fl->users, 1);
  355. switch (fl->share) {
  356. case IPV6_FL_S_EXCL:
  357. case IPV6_FL_S_ANY:
  358. break;
  359. case IPV6_FL_S_PROCESS:
  360. fl->owner.pid = get_task_pid(current, PIDTYPE_PID);
  361. break;
  362. case IPV6_FL_S_USER:
  363. fl->owner.uid = current_euid();
  364. break;
  365. default:
  366. err = -EINVAL;
  367. goto done;
  368. }
  369. return fl;
  370. done:
  371. fl_free(fl);
  372. *err_p = err;
  373. return NULL;
  374. }
  375. static int mem_check(struct sock *sk)
  376. {
  377. struct ipv6_pinfo *np = inet6_sk(sk);
  378. struct ipv6_fl_socklist *sfl;
  379. int room = FL_MAX_SIZE - atomic_read(&fl_size);
  380. int count = 0;
  381. if (room > FL_MAX_SIZE - FL_MAX_PER_SOCK)
  382. return 0;
  383. rcu_read_lock_bh();
  384. for_each_sk_fl_rcu(np, sfl)
  385. count++;
  386. rcu_read_unlock_bh();
  387. if (room <= 0 ||
  388. ((count >= FL_MAX_PER_SOCK ||
  389. (count > 0 && room < FL_MAX_SIZE/2) || room < FL_MAX_SIZE/4) &&
  390. !capable(CAP_NET_ADMIN)))
  391. return -ENOBUFS;
  392. return 0;
  393. }
  394. static inline void fl_link(struct ipv6_pinfo *np, struct ipv6_fl_socklist *sfl,
  395. struct ip6_flowlabel *fl)
  396. {
  397. spin_lock_bh(&ip6_sk_fl_lock);
  398. sfl->fl = fl;
  399. sfl->next = np->ipv6_fl_list;
  400. rcu_assign_pointer(np->ipv6_fl_list, sfl);
  401. spin_unlock_bh(&ip6_sk_fl_lock);
  402. }
  403. int ipv6_flowlabel_opt_get(struct sock *sk, struct in6_flowlabel_req *freq,
  404. int flags)
  405. {
  406. struct ipv6_pinfo *np = inet6_sk(sk);
  407. struct ipv6_fl_socklist *sfl;
  408. if (flags & IPV6_FL_F_REMOTE) {
  409. freq->flr_label = np->rcv_flowinfo & IPV6_FLOWLABEL_MASK;
  410. return 0;
  411. }
  412. if (np->repflow) {
  413. freq->flr_label = np->flow_label;
  414. return 0;
  415. }
  416. rcu_read_lock_bh();
  417. for_each_sk_fl_rcu(np, sfl) {
  418. if (sfl->fl->label == (np->flow_label & IPV6_FLOWLABEL_MASK)) {
  419. spin_lock_bh(&ip6_fl_lock);
  420. freq->flr_label = sfl->fl->label;
  421. freq->flr_dst = sfl->fl->dst;
  422. freq->flr_share = sfl->fl->share;
  423. freq->flr_expires = (sfl->fl->expires - jiffies) / HZ;
  424. freq->flr_linger = sfl->fl->linger / HZ;
  425. spin_unlock_bh(&ip6_fl_lock);
  426. rcu_read_unlock_bh();
  427. return 0;
  428. }
  429. }
  430. rcu_read_unlock_bh();
  431. return -ENOENT;
  432. }
  433. int ipv6_flowlabel_opt(struct sock *sk, char __user *optval, int optlen)
  434. {
  435. int uninitialized_var(err);
  436. struct net *net = sock_net(sk);
  437. struct ipv6_pinfo *np = inet6_sk(sk);
  438. struct in6_flowlabel_req freq;
  439. struct ipv6_fl_socklist *sfl1 = NULL;
  440. struct ipv6_fl_socklist *sfl;
  441. struct ipv6_fl_socklist __rcu **sflp;
  442. struct ip6_flowlabel *fl, *fl1 = NULL;
  443. if (optlen < sizeof(freq))
  444. return -EINVAL;
  445. if (copy_from_user(&freq, optval, sizeof(freq)))
  446. return -EFAULT;
  447. switch (freq.flr_action) {
  448. case IPV6_FL_A_PUT:
  449. if (freq.flr_flags & IPV6_FL_F_REFLECT) {
  450. if (sk->sk_protocol != IPPROTO_TCP)
  451. return -ENOPROTOOPT;
  452. if (!np->repflow)
  453. return -ESRCH;
  454. np->flow_label = 0;
  455. np->repflow = 0;
  456. return 0;
  457. }
  458. spin_lock_bh(&ip6_sk_fl_lock);
  459. for (sflp = &np->ipv6_fl_list;
  460. (sfl = rcu_dereference(*sflp)) != NULL;
  461. sflp = &sfl->next) {
  462. if (sfl->fl->label == freq.flr_label) {
  463. if (freq.flr_label == (np->flow_label&IPV6_FLOWLABEL_MASK))
  464. np->flow_label &= ~IPV6_FLOWLABEL_MASK;
  465. *sflp = rcu_dereference(sfl->next);
  466. spin_unlock_bh(&ip6_sk_fl_lock);
  467. fl_release(sfl->fl);
  468. kfree_rcu(sfl, rcu);
  469. return 0;
  470. }
  471. }
  472. spin_unlock_bh(&ip6_sk_fl_lock);
  473. return -ESRCH;
  474. case IPV6_FL_A_RENEW:
  475. rcu_read_lock_bh();
  476. for_each_sk_fl_rcu(np, sfl) {
  477. if (sfl->fl->label == freq.flr_label) {
  478. err = fl6_renew(sfl->fl, freq.flr_linger, freq.flr_expires);
  479. rcu_read_unlock_bh();
  480. return err;
  481. }
  482. }
  483. rcu_read_unlock_bh();
  484. if (freq.flr_share == IPV6_FL_S_NONE &&
  485. ns_capable(net->user_ns, CAP_NET_ADMIN)) {
  486. fl = fl_lookup(net, freq.flr_label);
  487. if (fl) {
  488. err = fl6_renew(fl, freq.flr_linger, freq.flr_expires);
  489. fl_release(fl);
  490. return err;
  491. }
  492. }
  493. return -ESRCH;
  494. case IPV6_FL_A_GET:
  495. if (freq.flr_flags & IPV6_FL_F_REFLECT) {
  496. struct net *net = sock_net(sk);
  497. if (net->ipv6.sysctl.flowlabel_consistency) {
  498. net_info_ratelimited("Can not set IPV6_FL_F_REFLECT if flowlabel_consistency sysctl is enable\n");
  499. return -EPERM;
  500. }
  501. if (sk->sk_protocol != IPPROTO_TCP)
  502. return -ENOPROTOOPT;
  503. np->repflow = 1;
  504. return 0;
  505. }
  506. if (freq.flr_label & ~IPV6_FLOWLABEL_MASK)
  507. return -EINVAL;
  508. if (net->ipv6.sysctl.flowlabel_state_ranges &&
  509. (freq.flr_label & IPV6_FLOWLABEL_STATELESS_FLAG))
  510. return -ERANGE;
  511. fl = fl_create(net, sk, &freq, optval, optlen, &err);
  512. if (!fl)
  513. return err;
  514. sfl1 = kmalloc(sizeof(*sfl1), GFP_KERNEL);
  515. if (freq.flr_label) {
  516. err = -EEXIST;
  517. rcu_read_lock_bh();
  518. for_each_sk_fl_rcu(np, sfl) {
  519. if (sfl->fl->label == freq.flr_label) {
  520. if (freq.flr_flags&IPV6_FL_F_EXCL) {
  521. rcu_read_unlock_bh();
  522. goto done;
  523. }
  524. fl1 = sfl->fl;
  525. atomic_inc(&fl1->users);
  526. break;
  527. }
  528. }
  529. rcu_read_unlock_bh();
  530. if (!fl1)
  531. fl1 = fl_lookup(net, freq.flr_label);
  532. if (fl1) {
  533. recheck:
  534. err = -EEXIST;
  535. if (freq.flr_flags&IPV6_FL_F_EXCL)
  536. goto release;
  537. err = -EPERM;
  538. if (fl1->share == IPV6_FL_S_EXCL ||
  539. fl1->share != fl->share ||
  540. ((fl1->share == IPV6_FL_S_PROCESS) &&
  541. (fl1->owner.pid == fl->owner.pid)) ||
  542. ((fl1->share == IPV6_FL_S_USER) &&
  543. uid_eq(fl1->owner.uid, fl->owner.uid)))
  544. goto release;
  545. err = -ENOMEM;
  546. if (!sfl1)
  547. goto release;
  548. if (fl->linger > fl1->linger)
  549. fl1->linger = fl->linger;
  550. if ((long)(fl->expires - fl1->expires) > 0)
  551. fl1->expires = fl->expires;
  552. fl_link(np, sfl1, fl1);
  553. fl_free(fl);
  554. return 0;
  555. release:
  556. fl_release(fl1);
  557. goto done;
  558. }
  559. }
  560. err = -ENOENT;
  561. if (!(freq.flr_flags&IPV6_FL_F_CREATE))
  562. goto done;
  563. err = -ENOMEM;
  564. if (!sfl1)
  565. goto done;
  566. err = mem_check(sk);
  567. if (err != 0)
  568. goto done;
  569. fl1 = fl_intern(net, fl, freq.flr_label);
  570. if (fl1)
  571. goto recheck;
  572. if (!freq.flr_label) {
  573. if (copy_to_user(&((struct in6_flowlabel_req __user *) optval)->flr_label,
  574. &fl->label, sizeof(fl->label))) {
  575. /* Intentionally ignore fault. */
  576. }
  577. }
  578. fl_link(np, sfl1, fl);
  579. return 0;
  580. default:
  581. return -EINVAL;
  582. }
  583. done:
  584. fl_free(fl);
  585. kfree(sfl1);
  586. return err;
  587. }
  588. #ifdef CONFIG_PROC_FS
  589. struct ip6fl_iter_state {
  590. struct seq_net_private p;
  591. struct pid_namespace *pid_ns;
  592. int bucket;
  593. };
  594. #define ip6fl_seq_private(seq) ((struct ip6fl_iter_state *)(seq)->private)
  595. static struct ip6_flowlabel *ip6fl_get_first(struct seq_file *seq)
  596. {
  597. struct ip6_flowlabel *fl = NULL;
  598. struct ip6fl_iter_state *state = ip6fl_seq_private(seq);
  599. struct net *net = seq_file_net(seq);
  600. for (state->bucket = 0; state->bucket <= FL_HASH_MASK; ++state->bucket) {
  601. for_each_fl_rcu(state->bucket, fl) {
  602. if (net_eq(fl->fl_net, net))
  603. goto out;
  604. }
  605. }
  606. fl = NULL;
  607. out:
  608. return fl;
  609. }
  610. static struct ip6_flowlabel *ip6fl_get_next(struct seq_file *seq, struct ip6_flowlabel *fl)
  611. {
  612. struct ip6fl_iter_state *state = ip6fl_seq_private(seq);
  613. struct net *net = seq_file_net(seq);
  614. for_each_fl_continue_rcu(fl) {
  615. if (net_eq(fl->fl_net, net))
  616. goto out;
  617. }
  618. try_again:
  619. if (++state->bucket <= FL_HASH_MASK) {
  620. for_each_fl_rcu(state->bucket, fl) {
  621. if (net_eq(fl->fl_net, net))
  622. goto out;
  623. }
  624. goto try_again;
  625. }
  626. fl = NULL;
  627. out:
  628. return fl;
  629. }
  630. static struct ip6_flowlabel *ip6fl_get_idx(struct seq_file *seq, loff_t pos)
  631. {
  632. struct ip6_flowlabel *fl = ip6fl_get_first(seq);
  633. if (fl)
  634. while (pos && (fl = ip6fl_get_next(seq, fl)) != NULL)
  635. --pos;
  636. return pos ? NULL : fl;
  637. }
  638. static void *ip6fl_seq_start(struct seq_file *seq, loff_t *pos)
  639. __acquires(RCU)
  640. {
  641. rcu_read_lock_bh();
  642. return *pos ? ip6fl_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
  643. }
  644. static void *ip6fl_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  645. {
  646. struct ip6_flowlabel *fl;
  647. if (v == SEQ_START_TOKEN)
  648. fl = ip6fl_get_first(seq);
  649. else
  650. fl = ip6fl_get_next(seq, v);
  651. ++*pos;
  652. return fl;
  653. }
  654. static void ip6fl_seq_stop(struct seq_file *seq, void *v)
  655. __releases(RCU)
  656. {
  657. rcu_read_unlock_bh();
  658. }
  659. static int ip6fl_seq_show(struct seq_file *seq, void *v)
  660. {
  661. struct ip6fl_iter_state *state = ip6fl_seq_private(seq);
  662. if (v == SEQ_START_TOKEN) {
  663. seq_puts(seq, "Label S Owner Users Linger Expires Dst Opt\n");
  664. } else {
  665. struct ip6_flowlabel *fl = v;
  666. seq_printf(seq,
  667. "%05X %-1d %-6d %-6d %-6ld %-8ld %pi6 %-4d\n",
  668. (unsigned int)ntohl(fl->label),
  669. fl->share,
  670. ((fl->share == IPV6_FL_S_PROCESS) ?
  671. pid_nr_ns(fl->owner.pid, state->pid_ns) :
  672. ((fl->share == IPV6_FL_S_USER) ?
  673. from_kuid_munged(seq_user_ns(seq), fl->owner.uid) :
  674. 0)),
  675. atomic_read(&fl->users),
  676. fl->linger/HZ,
  677. (long)(fl->expires - jiffies)/HZ,
  678. &fl->dst,
  679. fl->opt ? fl->opt->opt_nflen : 0);
  680. }
  681. return 0;
  682. }
  683. static const struct seq_operations ip6fl_seq_ops = {
  684. .start = ip6fl_seq_start,
  685. .next = ip6fl_seq_next,
  686. .stop = ip6fl_seq_stop,
  687. .show = ip6fl_seq_show,
  688. };
  689. static int ip6fl_seq_open(struct inode *inode, struct file *file)
  690. {
  691. struct seq_file *seq;
  692. struct ip6fl_iter_state *state;
  693. int err;
  694. err = seq_open_net(inode, file, &ip6fl_seq_ops,
  695. sizeof(struct ip6fl_iter_state));
  696. if (!err) {
  697. seq = file->private_data;
  698. state = ip6fl_seq_private(seq);
  699. rcu_read_lock();
  700. state->pid_ns = get_pid_ns(task_active_pid_ns(current));
  701. rcu_read_unlock();
  702. }
  703. return err;
  704. }
  705. static int ip6fl_seq_release(struct inode *inode, struct file *file)
  706. {
  707. struct seq_file *seq = file->private_data;
  708. struct ip6fl_iter_state *state = ip6fl_seq_private(seq);
  709. put_pid_ns(state->pid_ns);
  710. return seq_release_net(inode, file);
  711. }
  712. static const struct file_operations ip6fl_seq_fops = {
  713. .owner = THIS_MODULE,
  714. .open = ip6fl_seq_open,
  715. .read = seq_read,
  716. .llseek = seq_lseek,
  717. .release = ip6fl_seq_release,
  718. };
  719. static int __net_init ip6_flowlabel_proc_init(struct net *net)
  720. {
  721. if (!proc_create("ip6_flowlabel", S_IRUGO, net->proc_net,
  722. &ip6fl_seq_fops))
  723. return -ENOMEM;
  724. return 0;
  725. }
  726. static void __net_exit ip6_flowlabel_proc_fini(struct net *net)
  727. {
  728. remove_proc_entry("ip6_flowlabel", net->proc_net);
  729. }
  730. #else
  731. static inline int ip6_flowlabel_proc_init(struct net *net)
  732. {
  733. return 0;
  734. }
  735. static inline void ip6_flowlabel_proc_fini(struct net *net)
  736. {
  737. }
  738. #endif
  739. static void __net_exit ip6_flowlabel_net_exit(struct net *net)
  740. {
  741. ip6_fl_purge(net);
  742. ip6_flowlabel_proc_fini(net);
  743. }
  744. static struct pernet_operations ip6_flowlabel_net_ops = {
  745. .init = ip6_flowlabel_proc_init,
  746. .exit = ip6_flowlabel_net_exit,
  747. };
  748. int ip6_flowlabel_init(void)
  749. {
  750. return register_pernet_subsys(&ip6_flowlabel_net_ops);
  751. }
  752. void ip6_flowlabel_cleanup(void)
  753. {
  754. del_timer(&ip6_fl_gc_timer);
  755. unregister_pernet_subsys(&ip6_flowlabel_net_ops);
  756. }