ax25_uid.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /*
  2. * This program is free software; you can redistribute it and/or modify
  3. * it under the terms of the GNU General Public License as published by
  4. * the Free Software Foundation; either version 2 of the License, or
  5. * (at your option) any later version.
  6. *
  7. * Copyright (C) Jonathan Naylor G4KLX (g4klx@g4klx.demon.co.uk)
  8. */
  9. #include <linux/errno.h>
  10. #include <linux/types.h>
  11. #include <linux/socket.h>
  12. #include <linux/in.h>
  13. #include <linux/kernel.h>
  14. #include <linux/sched.h>
  15. #include <linux/timer.h>
  16. #include <linux/string.h>
  17. #include <linux/sockios.h>
  18. #include <linux/net.h>
  19. #include <linux/spinlock.h>
  20. #include <net/ax25.h>
  21. #include <linux/inet.h>
  22. #include <linux/netdevice.h>
  23. #include <linux/if_arp.h>
  24. #include <linux/skbuff.h>
  25. #include <net/sock.h>
  26. #include <asm/uaccess.h>
  27. #include <asm/system.h>
  28. #include <linux/fcntl.h>
  29. #include <linux/mm.h>
  30. #include <linux/interrupt.h>
  31. #include <linux/notifier.h>
  32. #include <linux/proc_fs.h>
  33. #include <linux/seq_file.h>
  34. #include <linux/stat.h>
  35. #include <linux/netfilter.h>
  36. #include <linux/sysctl.h>
  37. #include <net/ip.h>
  38. #include <net/arp.h>
  39. /*
  40. * Callsign/UID mapper. This is in kernel space for security on multi-amateur machines.
  41. */
  42. static ax25_uid_assoc *ax25_uid_list;
  43. static DEFINE_RWLOCK(ax25_uid_lock);
  44. int ax25_uid_policy = 0;
  45. ax25_address *ax25_findbyuid(uid_t uid)
  46. {
  47. ax25_uid_assoc *ax25_uid;
  48. ax25_address *res = NULL;
  49. read_lock(&ax25_uid_lock);
  50. for (ax25_uid = ax25_uid_list; ax25_uid != NULL; ax25_uid = ax25_uid->next) {
  51. if (ax25_uid->uid == uid) {
  52. res = &ax25_uid->call;
  53. break;
  54. }
  55. }
  56. read_unlock(&ax25_uid_lock);
  57. return NULL;
  58. }
  59. int ax25_uid_ioctl(int cmd, struct sockaddr_ax25 *sax)
  60. {
  61. ax25_uid_assoc *s, *ax25_uid;
  62. unsigned long res;
  63. switch (cmd) {
  64. case SIOCAX25GETUID:
  65. res = -ENOENT;
  66. read_lock(&ax25_uid_lock);
  67. for (ax25_uid = ax25_uid_list; ax25_uid != NULL; ax25_uid = ax25_uid->next) {
  68. if (ax25cmp(&sax->sax25_call, &ax25_uid->call) == 0) {
  69. res = ax25_uid->uid;
  70. break;
  71. }
  72. }
  73. read_unlock(&ax25_uid_lock);
  74. return res;
  75. case SIOCAX25ADDUID:
  76. if (!capable(CAP_NET_ADMIN))
  77. return -EPERM;
  78. if (ax25_findbyuid(sax->sax25_uid))
  79. return -EEXIST;
  80. if (sax->sax25_uid == 0)
  81. return -EINVAL;
  82. if ((ax25_uid = kmalloc(sizeof(*ax25_uid), GFP_KERNEL)) == NULL)
  83. return -ENOMEM;
  84. ax25_uid->uid = sax->sax25_uid;
  85. ax25_uid->call = sax->sax25_call;
  86. write_lock(&ax25_uid_lock);
  87. ax25_uid->next = ax25_uid_list;
  88. ax25_uid_list = ax25_uid;
  89. write_unlock(&ax25_uid_lock);
  90. return 0;
  91. case SIOCAX25DELUID:
  92. if (!capable(CAP_NET_ADMIN))
  93. return -EPERM;
  94. write_lock(&ax25_uid_lock);
  95. for (ax25_uid = ax25_uid_list; ax25_uid != NULL; ax25_uid = ax25_uid->next) {
  96. if (ax25cmp(&sax->sax25_call, &ax25_uid->call) == 0) {
  97. break;
  98. }
  99. }
  100. if (ax25_uid == NULL) {
  101. write_unlock(&ax25_uid_lock);
  102. return -ENOENT;
  103. }
  104. if ((s = ax25_uid_list) == ax25_uid) {
  105. ax25_uid_list = s->next;
  106. write_unlock(&ax25_uid_lock);
  107. kfree(ax25_uid);
  108. return 0;
  109. }
  110. while (s != NULL && s->next != NULL) {
  111. if (s->next == ax25_uid) {
  112. s->next = ax25_uid->next;
  113. write_unlock(&ax25_uid_lock);
  114. kfree(ax25_uid);
  115. return 0;
  116. }
  117. s = s->next;
  118. }
  119. write_unlock(&ax25_uid_lock);
  120. return -ENOENT;
  121. default:
  122. return -EINVAL;
  123. }
  124. return -EINVAL; /*NOTREACHED */
  125. }
  126. #ifdef CONFIG_PROC_FS
  127. static void *ax25_uid_seq_start(struct seq_file *seq, loff_t *pos)
  128. {
  129. struct ax25_uid_assoc *pt;
  130. int i = 1;
  131. read_lock(&ax25_uid_lock);
  132. if (*pos == 0)
  133. return SEQ_START_TOKEN;
  134. for (pt = ax25_uid_list; pt != NULL; pt = pt->next) {
  135. if (i == *pos)
  136. return pt;
  137. ++i;
  138. }
  139. return NULL;
  140. }
  141. static void *ax25_uid_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  142. {
  143. ++*pos;
  144. return (v == SEQ_START_TOKEN) ? ax25_uid_list :
  145. ((struct ax25_uid_assoc *) v)->next;
  146. }
  147. static void ax25_uid_seq_stop(struct seq_file *seq, void *v)
  148. {
  149. read_unlock(&ax25_uid_lock);
  150. }
  151. static int ax25_uid_seq_show(struct seq_file *seq, void *v)
  152. {
  153. if (v == SEQ_START_TOKEN)
  154. seq_printf(seq, "Policy: %d\n", ax25_uid_policy);
  155. else {
  156. struct ax25_uid_assoc *pt = v;
  157. seq_printf(seq, "%6d %s\n", pt->uid, ax2asc(&pt->call));
  158. }
  159. return 0;
  160. }
  161. static struct seq_operations ax25_uid_seqops = {
  162. .start = ax25_uid_seq_start,
  163. .next = ax25_uid_seq_next,
  164. .stop = ax25_uid_seq_stop,
  165. .show = ax25_uid_seq_show,
  166. };
  167. static int ax25_uid_info_open(struct inode *inode, struct file *file)
  168. {
  169. return seq_open(file, &ax25_uid_seqops);
  170. }
  171. struct file_operations ax25_uid_fops = {
  172. .owner = THIS_MODULE,
  173. .open = ax25_uid_info_open,
  174. .read = seq_read,
  175. .llseek = seq_lseek,
  176. .release = seq_release,
  177. };
  178. #endif
  179. /*
  180. * Free all memory associated with UID/Callsign structures.
  181. */
  182. void __exit ax25_uid_free(void)
  183. {
  184. ax25_uid_assoc *s, *ax25_uid;
  185. write_lock(&ax25_uid_lock);
  186. ax25_uid = ax25_uid_list;
  187. while (ax25_uid != NULL) {
  188. s = ax25_uid;
  189. ax25_uid = ax25_uid->next;
  190. kfree(s);
  191. }
  192. ax25_uid_list = NULL;
  193. write_unlock(&ax25_uid_lock);
  194. }