ip6_flowlabel.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708
  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 = kmalloc(sizeof(*fl), GFP_KERNEL);
  243. if (fl == NULL)
  244. goto done;
  245. memset(fl, 0, sizeof(*fl));
  246. olen = optlen - CMSG_ALIGN(sizeof(*freq));
  247. if (olen > 0) {
  248. struct msghdr msg;
  249. struct flowi flowi;
  250. int junk;
  251. err = -ENOMEM;
  252. fl->opt = kmalloc(sizeof(*fl->opt) + olen, GFP_KERNEL);
  253. if (fl->opt == NULL)
  254. goto done;
  255. memset(fl->opt, 0, sizeof(*fl->opt));
  256. fl->opt->tot_len = sizeof(*fl->opt) + olen;
  257. err = -EFAULT;
  258. if (copy_from_user(fl->opt+1, optval+CMSG_ALIGN(sizeof(*freq)), olen))
  259. goto done;
  260. msg.msg_controllen = olen;
  261. msg.msg_control = (void*)(fl->opt+1);
  262. flowi.oif = 0;
  263. err = datagram_send_ctl(&msg, &flowi, fl->opt, &junk, &junk);
  264. if (err)
  265. goto done;
  266. err = -EINVAL;
  267. if (fl->opt->opt_flen)
  268. goto done;
  269. if (fl->opt->opt_nflen == 0) {
  270. kfree(fl->opt);
  271. fl->opt = NULL;
  272. }
  273. }
  274. fl->expires = jiffies;
  275. err = fl6_renew(fl, freq->flr_linger, freq->flr_expires);
  276. if (err)
  277. goto done;
  278. fl->share = freq->flr_share;
  279. addr_type = ipv6_addr_type(&freq->flr_dst);
  280. if ((addr_type&IPV6_ADDR_MAPPED)
  281. || addr_type == IPV6_ADDR_ANY)
  282. goto done;
  283. ipv6_addr_copy(&fl->dst, &freq->flr_dst);
  284. atomic_set(&fl->users, 1);
  285. switch (fl->share) {
  286. case IPV6_FL_S_EXCL:
  287. case IPV6_FL_S_ANY:
  288. break;
  289. case IPV6_FL_S_PROCESS:
  290. fl->owner = current->pid;
  291. break;
  292. case IPV6_FL_S_USER:
  293. fl->owner = current->euid;
  294. break;
  295. default:
  296. err = -EINVAL;
  297. goto done;
  298. }
  299. return fl;
  300. done:
  301. fl_free(fl);
  302. *err_p = err;
  303. return NULL;
  304. }
  305. static int mem_check(struct sock *sk)
  306. {
  307. struct ipv6_pinfo *np = inet6_sk(sk);
  308. struct ipv6_fl_socklist *sfl;
  309. int room = FL_MAX_SIZE - atomic_read(&fl_size);
  310. int count = 0;
  311. if (room > FL_MAX_SIZE - FL_MAX_PER_SOCK)
  312. return 0;
  313. for (sfl = np->ipv6_fl_list; sfl; sfl = sfl->next)
  314. count++;
  315. if (room <= 0 ||
  316. ((count >= FL_MAX_PER_SOCK ||
  317. (count > 0 && room < FL_MAX_SIZE/2) || room < FL_MAX_SIZE/4)
  318. && !capable(CAP_NET_ADMIN)))
  319. return -ENOBUFS;
  320. return 0;
  321. }
  322. static int ipv6_hdr_cmp(struct ipv6_opt_hdr *h1, struct ipv6_opt_hdr *h2)
  323. {
  324. if (h1 == h2)
  325. return 0;
  326. if (h1 == NULL || h2 == NULL)
  327. return 1;
  328. if (h1->hdrlen != h2->hdrlen)
  329. return 1;
  330. return memcmp(h1+1, h2+1, ((h1->hdrlen+1)<<3) - sizeof(*h1));
  331. }
  332. static int ipv6_opt_cmp(struct ipv6_txoptions *o1, struct ipv6_txoptions *o2)
  333. {
  334. if (o1 == o2)
  335. return 0;
  336. if (o1 == NULL || o2 == NULL)
  337. return 1;
  338. if (o1->opt_nflen != o2->opt_nflen)
  339. return 1;
  340. if (ipv6_hdr_cmp(o1->hopopt, o2->hopopt))
  341. return 1;
  342. if (ipv6_hdr_cmp(o1->dst0opt, o2->dst0opt))
  343. return 1;
  344. if (ipv6_hdr_cmp((struct ipv6_opt_hdr *)o1->srcrt, (struct ipv6_opt_hdr *)o2->srcrt))
  345. return 1;
  346. return 0;
  347. }
  348. int ipv6_flowlabel_opt(struct sock *sk, char __user *optval, int optlen)
  349. {
  350. int err;
  351. struct ipv6_pinfo *np = inet6_sk(sk);
  352. struct in6_flowlabel_req freq;
  353. struct ipv6_fl_socklist *sfl1=NULL;
  354. struct ipv6_fl_socklist *sfl, **sflp;
  355. struct ip6_flowlabel *fl;
  356. if (optlen < sizeof(freq))
  357. return -EINVAL;
  358. if (copy_from_user(&freq, optval, sizeof(freq)))
  359. return -EFAULT;
  360. switch (freq.flr_action) {
  361. case IPV6_FL_A_PUT:
  362. write_lock_bh(&ip6_sk_fl_lock);
  363. for (sflp = &np->ipv6_fl_list; (sfl=*sflp)!=NULL; sflp = &sfl->next) {
  364. if (sfl->fl->label == freq.flr_label) {
  365. if (freq.flr_label == (np->flow_label&IPV6_FLOWLABEL_MASK))
  366. np->flow_label &= ~IPV6_FLOWLABEL_MASK;
  367. *sflp = sfl->next;
  368. write_unlock_bh(&ip6_sk_fl_lock);
  369. fl_release(sfl->fl);
  370. kfree(sfl);
  371. return 0;
  372. }
  373. }
  374. write_unlock_bh(&ip6_sk_fl_lock);
  375. return -ESRCH;
  376. case IPV6_FL_A_RENEW:
  377. read_lock_bh(&ip6_sk_fl_lock);
  378. for (sfl = np->ipv6_fl_list; sfl; sfl = sfl->next) {
  379. if (sfl->fl->label == freq.flr_label) {
  380. err = fl6_renew(sfl->fl, freq.flr_linger, freq.flr_expires);
  381. read_unlock_bh(&ip6_sk_fl_lock);
  382. return err;
  383. }
  384. }
  385. read_unlock_bh(&ip6_sk_fl_lock);
  386. if (freq.flr_share == IPV6_FL_S_NONE && capable(CAP_NET_ADMIN)) {
  387. fl = fl_lookup(freq.flr_label);
  388. if (fl) {
  389. err = fl6_renew(fl, freq.flr_linger, freq.flr_expires);
  390. fl_release(fl);
  391. return err;
  392. }
  393. }
  394. return -ESRCH;
  395. case IPV6_FL_A_GET:
  396. if (freq.flr_label & ~IPV6_FLOWLABEL_MASK)
  397. return -EINVAL;
  398. fl = fl_create(&freq, optval, optlen, &err);
  399. if (fl == NULL)
  400. return err;
  401. sfl1 = kmalloc(sizeof(*sfl1), GFP_KERNEL);
  402. if (freq.flr_label) {
  403. struct ip6_flowlabel *fl1 = NULL;
  404. err = -EEXIST;
  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. if (freq.flr_flags&IPV6_FL_F_EXCL) {
  409. read_unlock_bh(&ip6_sk_fl_lock);
  410. goto done;
  411. }
  412. fl1 = sfl->fl;
  413. atomic_inc(&fl1->users);
  414. break;
  415. }
  416. }
  417. read_unlock_bh(&ip6_sk_fl_lock);
  418. if (fl1 == NULL)
  419. fl1 = fl_lookup(freq.flr_label);
  420. if (fl1) {
  421. err = -EEXIST;
  422. if (freq.flr_flags&IPV6_FL_F_EXCL)
  423. goto release;
  424. err = -EPERM;
  425. if (fl1->share == IPV6_FL_S_EXCL ||
  426. fl1->share != fl->share ||
  427. fl1->owner != fl->owner)
  428. goto release;
  429. err = -EINVAL;
  430. if (!ipv6_addr_equal(&fl1->dst, &fl->dst) ||
  431. ipv6_opt_cmp(fl1->opt, fl->opt))
  432. goto release;
  433. err = -ENOMEM;
  434. if (sfl1 == NULL)
  435. goto release;
  436. if (fl->linger > fl1->linger)
  437. fl1->linger = fl->linger;
  438. if ((long)(fl->expires - fl1->expires) > 0)
  439. fl1->expires = fl->expires;
  440. write_lock_bh(&ip6_sk_fl_lock);
  441. sfl1->fl = fl1;
  442. sfl1->next = np->ipv6_fl_list;
  443. np->ipv6_fl_list = sfl1;
  444. write_unlock_bh(&ip6_sk_fl_lock);
  445. fl_free(fl);
  446. return 0;
  447. release:
  448. fl_release(fl1);
  449. goto done;
  450. }
  451. }
  452. err = -ENOENT;
  453. if (!(freq.flr_flags&IPV6_FL_F_CREATE))
  454. goto done;
  455. err = -ENOMEM;
  456. if (sfl1 == NULL || (err = mem_check(sk)) != 0)
  457. goto done;
  458. err = fl_intern(fl, freq.flr_label);
  459. if (err)
  460. goto done;
  461. if (!freq.flr_label) {
  462. if (copy_to_user(&((struct in6_flowlabel_req __user *) optval)->flr_label,
  463. &fl->label, sizeof(fl->label))) {
  464. /* Intentionally ignore fault. */
  465. }
  466. }
  467. sfl1->fl = fl;
  468. sfl1->next = np->ipv6_fl_list;
  469. np->ipv6_fl_list = sfl1;
  470. return 0;
  471. default:
  472. return -EINVAL;
  473. }
  474. done:
  475. fl_free(fl);
  476. kfree(sfl1);
  477. return err;
  478. }
  479. #ifdef CONFIG_PROC_FS
  480. struct ip6fl_iter_state {
  481. int bucket;
  482. };
  483. #define ip6fl_seq_private(seq) ((struct ip6fl_iter_state *)(seq)->private)
  484. static struct ip6_flowlabel *ip6fl_get_first(struct seq_file *seq)
  485. {
  486. struct ip6_flowlabel *fl = NULL;
  487. struct ip6fl_iter_state *state = ip6fl_seq_private(seq);
  488. for (state->bucket = 0; state->bucket <= FL_HASH_MASK; ++state->bucket) {
  489. if (fl_ht[state->bucket]) {
  490. fl = fl_ht[state->bucket];
  491. break;
  492. }
  493. }
  494. return fl;
  495. }
  496. static struct ip6_flowlabel *ip6fl_get_next(struct seq_file *seq, struct ip6_flowlabel *fl)
  497. {
  498. struct ip6fl_iter_state *state = ip6fl_seq_private(seq);
  499. fl = fl->next;
  500. while (!fl) {
  501. if (++state->bucket <= FL_HASH_MASK)
  502. fl = fl_ht[state->bucket];
  503. }
  504. return fl;
  505. }
  506. static struct ip6_flowlabel *ip6fl_get_idx(struct seq_file *seq, loff_t pos)
  507. {
  508. struct ip6_flowlabel *fl = ip6fl_get_first(seq);
  509. if (fl)
  510. while (pos && (fl = ip6fl_get_next(seq, fl)) != NULL)
  511. --pos;
  512. return pos ? NULL : fl;
  513. }
  514. static void *ip6fl_seq_start(struct seq_file *seq, loff_t *pos)
  515. {
  516. read_lock_bh(&ip6_fl_lock);
  517. return *pos ? ip6fl_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
  518. }
  519. static void *ip6fl_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  520. {
  521. struct ip6_flowlabel *fl;
  522. if (v == SEQ_START_TOKEN)
  523. fl = ip6fl_get_first(seq);
  524. else
  525. fl = ip6fl_get_next(seq, v);
  526. ++*pos;
  527. return fl;
  528. }
  529. static void ip6fl_seq_stop(struct seq_file *seq, void *v)
  530. {
  531. read_unlock_bh(&ip6_fl_lock);
  532. }
  533. static void ip6fl_fl_seq_show(struct seq_file *seq, struct ip6_flowlabel *fl)
  534. {
  535. while(fl) {
  536. seq_printf(seq,
  537. "%05X %-1d %-6d %-6d %-6ld %-8ld " NIP6_SEQFMT " %-4d\n",
  538. (unsigned)ntohl(fl->label),
  539. fl->share,
  540. (unsigned)fl->owner,
  541. atomic_read(&fl->users),
  542. fl->linger/HZ,
  543. (long)(fl->expires - jiffies)/HZ,
  544. NIP6(fl->dst),
  545. fl->opt ? fl->opt->opt_nflen : 0);
  546. fl = fl->next;
  547. }
  548. }
  549. static int ip6fl_seq_show(struct seq_file *seq, void *v)
  550. {
  551. if (v == SEQ_START_TOKEN)
  552. seq_printf(seq, "%-5s %-1s %-6s %-6s %-6s %-8s %-32s %s\n",
  553. "Label", "S", "Owner", "Users", "Linger", "Expires", "Dst", "Opt");
  554. else
  555. ip6fl_fl_seq_show(seq, v);
  556. return 0;
  557. }
  558. static struct seq_operations ip6fl_seq_ops = {
  559. .start = ip6fl_seq_start,
  560. .next = ip6fl_seq_next,
  561. .stop = ip6fl_seq_stop,
  562. .show = ip6fl_seq_show,
  563. };
  564. static int ip6fl_seq_open(struct inode *inode, struct file *file)
  565. {
  566. struct seq_file *seq;
  567. int rc = -ENOMEM;
  568. struct ip6fl_iter_state *s = kmalloc(sizeof(*s), GFP_KERNEL);
  569. if (!s)
  570. goto out;
  571. rc = seq_open(file, &ip6fl_seq_ops);
  572. if (rc)
  573. goto out_kfree;
  574. seq = file->private_data;
  575. seq->private = s;
  576. memset(s, 0, sizeof(*s));
  577. out:
  578. return rc;
  579. out_kfree:
  580. kfree(s);
  581. goto out;
  582. }
  583. static struct file_operations ip6fl_seq_fops = {
  584. .owner = THIS_MODULE,
  585. .open = ip6fl_seq_open,
  586. .read = seq_read,
  587. .llseek = seq_lseek,
  588. .release = seq_release_private,
  589. };
  590. #endif
  591. void ip6_flowlabel_init(void)
  592. {
  593. #ifdef CONFIG_PROC_FS
  594. proc_net_fops_create("ip6_flowlabel", S_IRUGO, &ip6fl_seq_fops);
  595. #endif
  596. }
  597. void ip6_flowlabel_cleanup(void)
  598. {
  599. del_timer(&ip6_fl_gc_timer);
  600. #ifdef CONFIG_PROC_FS
  601. proc_net_remove("ip6_flowlabel");
  602. #endif
  603. }