ip6_flowlabel.c 17 KB

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