ip6_flowlabel.c 20 KB

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