ip6_flowlabel.c 17 KB

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