ip6_flowlabel.c 16 KB

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