ip6_flowlabel.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706
  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/config.h>
  13. #include <linux/errno.h>
  14. #include <linux/types.h>
  15. #include <linux/socket.h>
  16. #include <linux/net.h>
  17. #include <linux/netdevice.h>
  18. #include <linux/if_arp.h>
  19. #include <linux/in6.h>
  20. #include <linux/route.h>
  21. #include <linux/proc_fs.h>
  22. #include <linux/seq_file.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(u32 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(u32 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 int fl_intern(struct ip6_flowlabel *fl, __u32 label)
  131. {
  132. fl->label = label & IPV6_FLOWLABEL_MASK;
  133. write_lock_bh(&ip6_fl_lock);
  134. if (label == 0) {
  135. for (;;) {
  136. fl->label = htonl(net_random())&IPV6_FLOWLABEL_MASK;
  137. if (fl->label) {
  138. struct ip6_flowlabel *lfl;
  139. lfl = __fl_lookup(fl->label);
  140. if (lfl == NULL)
  141. break;
  142. }
  143. }
  144. }
  145. fl->lastuse = jiffies;
  146. fl->next = fl_ht[FL_HASH(fl->label)];
  147. fl_ht[FL_HASH(fl->label)] = fl;
  148. atomic_inc(&fl_size);
  149. write_unlock_bh(&ip6_fl_lock);
  150. return 0;
  151. }
  152. /* Socket flowlabel lists */
  153. struct ip6_flowlabel * fl6_sock_lookup(struct sock *sk, u32 label)
  154. {
  155. struct ipv6_fl_socklist *sfl;
  156. struct ipv6_pinfo *np = inet6_sk(sk);
  157. label &= IPV6_FLOWLABEL_MASK;
  158. for (sfl=np->ipv6_fl_list; sfl; sfl = sfl->next) {
  159. struct ip6_flowlabel *fl = sfl->fl;
  160. if (fl->label == label) {
  161. fl->lastuse = jiffies;
  162. atomic_inc(&fl->users);
  163. return fl;
  164. }
  165. }
  166. return NULL;
  167. }
  168. EXPORT_SYMBOL_GPL(fl6_sock_lookup);
  169. void fl6_free_socklist(struct sock *sk)
  170. {
  171. struct ipv6_pinfo *np = inet6_sk(sk);
  172. struct ipv6_fl_socklist *sfl;
  173. while ((sfl = np->ipv6_fl_list) != NULL) {
  174. np->ipv6_fl_list = sfl->next;
  175. fl_release(sfl->fl);
  176. kfree(sfl);
  177. }
  178. }
  179. /* Service routines */
  180. /*
  181. It is the only difficult place. flowlabel enforces equal headers
  182. before and including routing header, however user may supply options
  183. following rthdr.
  184. */
  185. struct ipv6_txoptions *fl6_merge_options(struct ipv6_txoptions * opt_space,
  186. struct ip6_flowlabel * fl,
  187. struct ipv6_txoptions * fopt)
  188. {
  189. struct ipv6_txoptions * fl_opt = fl->opt;
  190. if (fopt == NULL || fopt->opt_flen == 0)
  191. return fl_opt;
  192. if (fl_opt != NULL) {
  193. opt_space->hopopt = fl_opt->hopopt;
  194. opt_space->dst0opt = fl_opt->dst0opt;
  195. opt_space->srcrt = fl_opt->srcrt;
  196. opt_space->opt_nflen = fl_opt->opt_nflen;
  197. } else {
  198. if (fopt->opt_nflen == 0)
  199. return fopt;
  200. opt_space->hopopt = NULL;
  201. opt_space->dst0opt = NULL;
  202. opt_space->srcrt = NULL;
  203. opt_space->opt_nflen = 0;
  204. }
  205. opt_space->dst1opt = fopt->dst1opt;
  206. opt_space->opt_flen = fopt->opt_flen;
  207. return opt_space;
  208. }
  209. static unsigned long check_linger(unsigned long ttl)
  210. {
  211. if (ttl < FL_MIN_LINGER)
  212. return FL_MIN_LINGER*HZ;
  213. if (ttl > FL_MAX_LINGER && !capable(CAP_NET_ADMIN))
  214. return 0;
  215. return ttl*HZ;
  216. }
  217. static int fl6_renew(struct ip6_flowlabel *fl, unsigned long linger, unsigned long expires)
  218. {
  219. linger = check_linger(linger);
  220. if (!linger)
  221. return -EPERM;
  222. expires = check_linger(expires);
  223. if (!expires)
  224. return -EPERM;
  225. fl->lastuse = jiffies;
  226. if (time_before(fl->linger, linger))
  227. fl->linger = linger;
  228. if (time_before(expires, fl->linger))
  229. expires = fl->linger;
  230. if (time_before(fl->expires, fl->lastuse + expires))
  231. fl->expires = fl->lastuse + expires;
  232. return 0;
  233. }
  234. static struct ip6_flowlabel *
  235. fl_create(struct in6_flowlabel_req *freq, char __user *optval, int optlen, int *err_p)
  236. {
  237. struct ip6_flowlabel *fl;
  238. int olen;
  239. int addr_type;
  240. int err;
  241. err = -ENOMEM;
  242. fl = kzalloc(sizeof(*fl), GFP_KERNEL);
  243. if (fl == NULL)
  244. goto done;
  245. olen = optlen - CMSG_ALIGN(sizeof(*freq));
  246. if (olen > 0) {
  247. struct msghdr msg;
  248. struct flowi flowi;
  249. int junk;
  250. err = -ENOMEM;
  251. fl->opt = kmalloc(sizeof(*fl->opt) + olen, GFP_KERNEL);
  252. if (fl->opt == NULL)
  253. goto done;
  254. memset(fl->opt, 0, sizeof(*fl->opt));
  255. fl->opt->tot_len = sizeof(*fl->opt) + olen;
  256. err = -EFAULT;
  257. if (copy_from_user(fl->opt+1, optval+CMSG_ALIGN(sizeof(*freq)), olen))
  258. goto done;
  259. msg.msg_controllen = olen;
  260. msg.msg_control = (void*)(fl->opt+1);
  261. flowi.oif = 0;
  262. err = datagram_send_ctl(&msg, &flowi, fl->opt, &junk, &junk);
  263. if (err)
  264. goto done;
  265. err = -EINVAL;
  266. if (fl->opt->opt_flen)
  267. goto done;
  268. if (fl->opt->opt_nflen == 0) {
  269. kfree(fl->opt);
  270. fl->opt = NULL;
  271. }
  272. }
  273. fl->expires = jiffies;
  274. err = fl6_renew(fl, freq->flr_linger, freq->flr_expires);
  275. if (err)
  276. goto done;
  277. fl->share = freq->flr_share;
  278. addr_type = ipv6_addr_type(&freq->flr_dst);
  279. if ((addr_type&IPV6_ADDR_MAPPED)
  280. || addr_type == IPV6_ADDR_ANY)
  281. goto done;
  282. ipv6_addr_copy(&fl->dst, &freq->flr_dst);
  283. atomic_set(&fl->users, 1);
  284. switch (fl->share) {
  285. case IPV6_FL_S_EXCL:
  286. case IPV6_FL_S_ANY:
  287. break;
  288. case IPV6_FL_S_PROCESS:
  289. fl->owner = current->pid;
  290. break;
  291. case IPV6_FL_S_USER:
  292. fl->owner = current->euid;
  293. break;
  294. default:
  295. err = -EINVAL;
  296. goto done;
  297. }
  298. return fl;
  299. done:
  300. fl_free(fl);
  301. *err_p = err;
  302. return NULL;
  303. }
  304. static int mem_check(struct sock *sk)
  305. {
  306. struct ipv6_pinfo *np = inet6_sk(sk);
  307. struct ipv6_fl_socklist *sfl;
  308. int room = FL_MAX_SIZE - atomic_read(&fl_size);
  309. int count = 0;
  310. if (room > FL_MAX_SIZE - FL_MAX_PER_SOCK)
  311. return 0;
  312. for (sfl = np->ipv6_fl_list; sfl; sfl = sfl->next)
  313. count++;
  314. if (room <= 0 ||
  315. ((count >= FL_MAX_PER_SOCK ||
  316. (count > 0 && room < FL_MAX_SIZE/2) || room < FL_MAX_SIZE/4)
  317. && !capable(CAP_NET_ADMIN)))
  318. return -ENOBUFS;
  319. return 0;
  320. }
  321. static int ipv6_hdr_cmp(struct ipv6_opt_hdr *h1, struct ipv6_opt_hdr *h2)
  322. {
  323. if (h1 == h2)
  324. return 0;
  325. if (h1 == NULL || h2 == NULL)
  326. return 1;
  327. if (h1->hdrlen != h2->hdrlen)
  328. return 1;
  329. return memcmp(h1+1, h2+1, ((h1->hdrlen+1)<<3) - sizeof(*h1));
  330. }
  331. static int ipv6_opt_cmp(struct ipv6_txoptions *o1, struct ipv6_txoptions *o2)
  332. {
  333. if (o1 == o2)
  334. return 0;
  335. if (o1 == NULL || o2 == NULL)
  336. return 1;
  337. if (o1->opt_nflen != o2->opt_nflen)
  338. return 1;
  339. if (ipv6_hdr_cmp(o1->hopopt, o2->hopopt))
  340. return 1;
  341. if (ipv6_hdr_cmp(o1->dst0opt, o2->dst0opt))
  342. return 1;
  343. if (ipv6_hdr_cmp((struct ipv6_opt_hdr *)o1->srcrt, (struct ipv6_opt_hdr *)o2->srcrt))
  344. return 1;
  345. return 0;
  346. }
  347. int ipv6_flowlabel_opt(struct sock *sk, char __user *optval, int optlen)
  348. {
  349. int err;
  350. struct ipv6_pinfo *np = inet6_sk(sk);
  351. struct in6_flowlabel_req freq;
  352. struct ipv6_fl_socklist *sfl1=NULL;
  353. struct ipv6_fl_socklist *sfl, **sflp;
  354. struct ip6_flowlabel *fl;
  355. if (optlen < sizeof(freq))
  356. return -EINVAL;
  357. if (copy_from_user(&freq, optval, sizeof(freq)))
  358. return -EFAULT;
  359. switch (freq.flr_action) {
  360. case IPV6_FL_A_PUT:
  361. write_lock_bh(&ip6_sk_fl_lock);
  362. for (sflp = &np->ipv6_fl_list; (sfl=*sflp)!=NULL; sflp = &sfl->next) {
  363. if (sfl->fl->label == freq.flr_label) {
  364. if (freq.flr_label == (np->flow_label&IPV6_FLOWLABEL_MASK))
  365. np->flow_label &= ~IPV6_FLOWLABEL_MASK;
  366. *sflp = sfl->next;
  367. write_unlock_bh(&ip6_sk_fl_lock);
  368. fl_release(sfl->fl);
  369. kfree(sfl);
  370. return 0;
  371. }
  372. }
  373. write_unlock_bh(&ip6_sk_fl_lock);
  374. return -ESRCH;
  375. case IPV6_FL_A_RENEW:
  376. read_lock_bh(&ip6_sk_fl_lock);
  377. for (sfl = np->ipv6_fl_list; sfl; sfl = sfl->next) {
  378. if (sfl->fl->label == freq.flr_label) {
  379. err = fl6_renew(sfl->fl, freq.flr_linger, freq.flr_expires);
  380. read_unlock_bh(&ip6_sk_fl_lock);
  381. return err;
  382. }
  383. }
  384. read_unlock_bh(&ip6_sk_fl_lock);
  385. if (freq.flr_share == IPV6_FL_S_NONE && capable(CAP_NET_ADMIN)) {
  386. fl = fl_lookup(freq.flr_label);
  387. if (fl) {
  388. err = fl6_renew(fl, freq.flr_linger, freq.flr_expires);
  389. fl_release(fl);
  390. return err;
  391. }
  392. }
  393. return -ESRCH;
  394. case IPV6_FL_A_GET:
  395. if (freq.flr_label & ~IPV6_FLOWLABEL_MASK)
  396. return -EINVAL;
  397. fl = fl_create(&freq, optval, optlen, &err);
  398. if (fl == NULL)
  399. return err;
  400. sfl1 = kmalloc(sizeof(*sfl1), GFP_KERNEL);
  401. if (freq.flr_label) {
  402. struct ip6_flowlabel *fl1 = NULL;
  403. err = -EEXIST;
  404. read_lock_bh(&ip6_sk_fl_lock);
  405. for (sfl = np->ipv6_fl_list; sfl; sfl = sfl->next) {
  406. if (sfl->fl->label == freq.flr_label) {
  407. if (freq.flr_flags&IPV6_FL_F_EXCL) {
  408. read_unlock_bh(&ip6_sk_fl_lock);
  409. goto done;
  410. }
  411. fl1 = sfl->fl;
  412. atomic_inc(&fl1->users);
  413. break;
  414. }
  415. }
  416. read_unlock_bh(&ip6_sk_fl_lock);
  417. if (fl1 == NULL)
  418. fl1 = fl_lookup(freq.flr_label);
  419. if (fl1) {
  420. err = -EEXIST;
  421. if (freq.flr_flags&IPV6_FL_F_EXCL)
  422. goto release;
  423. err = -EPERM;
  424. if (fl1->share == IPV6_FL_S_EXCL ||
  425. fl1->share != fl->share ||
  426. fl1->owner != fl->owner)
  427. goto release;
  428. err = -EINVAL;
  429. if (!ipv6_addr_equal(&fl1->dst, &fl->dst) ||
  430. ipv6_opt_cmp(fl1->opt, fl->opt))
  431. goto release;
  432. err = -ENOMEM;
  433. if (sfl1 == NULL)
  434. goto release;
  435. if (fl->linger > fl1->linger)
  436. fl1->linger = fl->linger;
  437. if ((long)(fl->expires - fl1->expires) > 0)
  438. fl1->expires = fl->expires;
  439. write_lock_bh(&ip6_sk_fl_lock);
  440. sfl1->fl = fl1;
  441. sfl1->next = np->ipv6_fl_list;
  442. np->ipv6_fl_list = sfl1;
  443. write_unlock_bh(&ip6_sk_fl_lock);
  444. fl_free(fl);
  445. return 0;
  446. release:
  447. fl_release(fl1);
  448. goto done;
  449. }
  450. }
  451. err = -ENOENT;
  452. if (!(freq.flr_flags&IPV6_FL_F_CREATE))
  453. goto done;
  454. err = -ENOMEM;
  455. if (sfl1 == NULL || (err = mem_check(sk)) != 0)
  456. goto done;
  457. err = fl_intern(fl, freq.flr_label);
  458. if (err)
  459. goto done;
  460. if (!freq.flr_label) {
  461. if (copy_to_user(&((struct in6_flowlabel_req __user *) optval)->flr_label,
  462. &fl->label, sizeof(fl->label))) {
  463. /* Intentionally ignore fault. */
  464. }
  465. }
  466. sfl1->fl = fl;
  467. sfl1->next = np->ipv6_fl_list;
  468. np->ipv6_fl_list = sfl1;
  469. return 0;
  470. default:
  471. return -EINVAL;
  472. }
  473. done:
  474. fl_free(fl);
  475. kfree(sfl1);
  476. return err;
  477. }
  478. #ifdef CONFIG_PROC_FS
  479. struct ip6fl_iter_state {
  480. int bucket;
  481. };
  482. #define ip6fl_seq_private(seq) ((struct ip6fl_iter_state *)(seq)->private)
  483. static struct ip6_flowlabel *ip6fl_get_first(struct seq_file *seq)
  484. {
  485. struct ip6_flowlabel *fl = NULL;
  486. struct ip6fl_iter_state *state = ip6fl_seq_private(seq);
  487. for (state->bucket = 0; state->bucket <= FL_HASH_MASK; ++state->bucket) {
  488. if (fl_ht[state->bucket]) {
  489. fl = fl_ht[state->bucket];
  490. break;
  491. }
  492. }
  493. return fl;
  494. }
  495. static struct ip6_flowlabel *ip6fl_get_next(struct seq_file *seq, struct ip6_flowlabel *fl)
  496. {
  497. struct ip6fl_iter_state *state = ip6fl_seq_private(seq);
  498. fl = fl->next;
  499. while (!fl) {
  500. if (++state->bucket <= FL_HASH_MASK)
  501. fl = fl_ht[state->bucket];
  502. }
  503. return fl;
  504. }
  505. static struct ip6_flowlabel *ip6fl_get_idx(struct seq_file *seq, loff_t pos)
  506. {
  507. struct ip6_flowlabel *fl = ip6fl_get_first(seq);
  508. if (fl)
  509. while (pos && (fl = ip6fl_get_next(seq, fl)) != NULL)
  510. --pos;
  511. return pos ? NULL : fl;
  512. }
  513. static void *ip6fl_seq_start(struct seq_file *seq, loff_t *pos)
  514. {
  515. read_lock_bh(&ip6_fl_lock);
  516. return *pos ? ip6fl_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
  517. }
  518. static void *ip6fl_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  519. {
  520. struct ip6_flowlabel *fl;
  521. if (v == SEQ_START_TOKEN)
  522. fl = ip6fl_get_first(seq);
  523. else
  524. fl = ip6fl_get_next(seq, v);
  525. ++*pos;
  526. return fl;
  527. }
  528. static void ip6fl_seq_stop(struct seq_file *seq, void *v)
  529. {
  530. read_unlock_bh(&ip6_fl_lock);
  531. }
  532. static void ip6fl_fl_seq_show(struct seq_file *seq, struct ip6_flowlabel *fl)
  533. {
  534. while(fl) {
  535. seq_printf(seq,
  536. "%05X %-1d %-6d %-6d %-6ld %-8ld " NIP6_SEQFMT " %-4d\n",
  537. (unsigned)ntohl(fl->label),
  538. fl->share,
  539. (unsigned)fl->owner,
  540. atomic_read(&fl->users),
  541. fl->linger/HZ,
  542. (long)(fl->expires - jiffies)/HZ,
  543. NIP6(fl->dst),
  544. fl->opt ? fl->opt->opt_nflen : 0);
  545. fl = fl->next;
  546. }
  547. }
  548. static int ip6fl_seq_show(struct seq_file *seq, void *v)
  549. {
  550. if (v == SEQ_START_TOKEN)
  551. seq_printf(seq, "%-5s %-1s %-6s %-6s %-6s %-8s %-32s %s\n",
  552. "Label", "S", "Owner", "Users", "Linger", "Expires", "Dst", "Opt");
  553. else
  554. ip6fl_fl_seq_show(seq, v);
  555. return 0;
  556. }
  557. static struct seq_operations ip6fl_seq_ops = {
  558. .start = ip6fl_seq_start,
  559. .next = ip6fl_seq_next,
  560. .stop = ip6fl_seq_stop,
  561. .show = ip6fl_seq_show,
  562. };
  563. static int ip6fl_seq_open(struct inode *inode, struct file *file)
  564. {
  565. struct seq_file *seq;
  566. int rc = -ENOMEM;
  567. struct ip6fl_iter_state *s = kzalloc(sizeof(*s), GFP_KERNEL);
  568. if (!s)
  569. goto out;
  570. rc = seq_open(file, &ip6fl_seq_ops);
  571. if (rc)
  572. goto out_kfree;
  573. seq = file->private_data;
  574. seq->private = s;
  575. out:
  576. return rc;
  577. out_kfree:
  578. kfree(s);
  579. goto out;
  580. }
  581. static struct file_operations ip6fl_seq_fops = {
  582. .owner = THIS_MODULE,
  583. .open = ip6fl_seq_open,
  584. .read = seq_read,
  585. .llseek = seq_lseek,
  586. .release = seq_release_private,
  587. };
  588. #endif
  589. void ip6_flowlabel_init(void)
  590. {
  591. #ifdef CONFIG_PROC_FS
  592. proc_net_fops_create("ip6_flowlabel", S_IRUGO, &ip6fl_seq_fops);
  593. #endif
  594. }
  595. void ip6_flowlabel_cleanup(void)
  596. {
  597. del_timer(&ip6_fl_gc_timer);
  598. #ifdef CONFIG_PROC_FS
  599. proc_net_remove("ip6_flowlabel");
  600. #endif
  601. }