sysctl_net_decnet.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. /*
  2. * DECnet An implementation of the DECnet protocol suite for the LINUX
  3. * operating system. DECnet is implemented using the BSD Socket
  4. * interface as the means of communication with the user level.
  5. *
  6. * DECnet sysctl support functions
  7. *
  8. * Author: Steve Whitehouse <SteveW@ACM.org>
  9. *
  10. *
  11. * Changes:
  12. * Steve Whitehouse - C99 changes and default device handling
  13. *
  14. */
  15. #include <linux/config.h>
  16. #include <linux/mm.h>
  17. #include <linux/sysctl.h>
  18. #include <linux/fs.h>
  19. #include <linux/netdevice.h>
  20. #include <linux/string.h>
  21. #include <net/neighbour.h>
  22. #include <net/dst.h>
  23. #include <net/flow.h>
  24. #include <asm/uaccess.h>
  25. #include <net/dn.h>
  26. #include <net/dn_dev.h>
  27. #include <net/dn_route.h>
  28. int decnet_debug_level;
  29. int decnet_time_wait = 30;
  30. int decnet_dn_count = 1;
  31. int decnet_di_count = 3;
  32. int decnet_dr_count = 3;
  33. int decnet_log_martians = 1;
  34. int decnet_no_fc_max_cwnd = NSP_MIN_WINDOW;
  35. #ifdef CONFIG_SYSCTL
  36. extern int decnet_dst_gc_interval;
  37. static int min_decnet_time_wait[] = { 5 };
  38. static int max_decnet_time_wait[] = { 600 };
  39. static int min_state_count[] = { 1 };
  40. static int max_state_count[] = { NSP_MAXRXTSHIFT };
  41. static int min_decnet_dst_gc_interval[] = { 1 };
  42. static int max_decnet_dst_gc_interval[] = { 60 };
  43. static int min_decnet_no_fc_max_cwnd[] = { NSP_MIN_WINDOW };
  44. static int max_decnet_no_fc_max_cwnd[] = { NSP_MAX_WINDOW };
  45. static char node_name[7] = "???";
  46. static struct ctl_table_header *dn_table_header = NULL;
  47. /*
  48. * ctype.h :-)
  49. */
  50. #define ISNUM(x) (((x) >= '0') && ((x) <= '9'))
  51. #define ISLOWER(x) (((x) >= 'a') && ((x) <= 'z'))
  52. #define ISUPPER(x) (((x) >= 'A') && ((x) <= 'Z'))
  53. #define ISALPHA(x) (ISLOWER(x) || ISUPPER(x))
  54. #define INVALID_END_CHAR(x) (ISNUM(x) || ISALPHA(x))
  55. static void strip_it(char *str)
  56. {
  57. for(;;) {
  58. switch(*str) {
  59. case ' ':
  60. case '\n':
  61. case '\r':
  62. case ':':
  63. *str = 0;
  64. case 0:
  65. return;
  66. }
  67. str++;
  68. }
  69. }
  70. /*
  71. * Simple routine to parse an ascii DECnet address
  72. * into a network order address.
  73. */
  74. static int parse_addr(dn_address *addr, char *str)
  75. {
  76. dn_address area, node;
  77. while(*str && !ISNUM(*str)) str++;
  78. if (*str == 0)
  79. return -1;
  80. area = (*str++ - '0');
  81. if (ISNUM(*str)) {
  82. area *= 10;
  83. area += (*str++ - '0');
  84. }
  85. if (*str++ != '.')
  86. return -1;
  87. if (!ISNUM(*str))
  88. return -1;
  89. node = *str++ - '0';
  90. if (ISNUM(*str)) {
  91. node *= 10;
  92. node += (*str++ - '0');
  93. }
  94. if (ISNUM(*str)) {
  95. node *= 10;
  96. node += (*str++ - '0');
  97. }
  98. if (ISNUM(*str)) {
  99. node *= 10;
  100. node += (*str++ - '0');
  101. }
  102. if ((node > 1023) || (area > 63))
  103. return -1;
  104. if (INVALID_END_CHAR(*str))
  105. return -1;
  106. *addr = dn_htons((area << 10) | node);
  107. return 0;
  108. }
  109. static int dn_node_address_strategy(ctl_table *table, int __user *name, int nlen,
  110. void __user *oldval, size_t __user *oldlenp,
  111. void __user *newval, size_t newlen,
  112. void **context)
  113. {
  114. size_t len;
  115. dn_address addr;
  116. if (oldval && oldlenp) {
  117. if (get_user(len, oldlenp))
  118. return -EFAULT;
  119. if (len) {
  120. if (len != sizeof(unsigned short))
  121. return -EINVAL;
  122. if (put_user(decnet_address, (unsigned short __user *)oldval))
  123. return -EFAULT;
  124. }
  125. }
  126. if (newval && newlen) {
  127. if (newlen != sizeof(unsigned short))
  128. return -EINVAL;
  129. if (get_user(addr, (unsigned short __user *)newval))
  130. return -EFAULT;
  131. dn_dev_devices_off();
  132. decnet_address = addr;
  133. dn_dev_devices_on();
  134. }
  135. return 0;
  136. }
  137. static int dn_node_address_handler(ctl_table *table, int write,
  138. struct file *filp,
  139. void __user *buffer,
  140. size_t *lenp, loff_t *ppos)
  141. {
  142. char addr[DN_ASCBUF_LEN];
  143. size_t len;
  144. dn_address dnaddr;
  145. if (!*lenp || (*ppos && !write)) {
  146. *lenp = 0;
  147. return 0;
  148. }
  149. if (write) {
  150. int len = (*lenp < DN_ASCBUF_LEN) ? *lenp : (DN_ASCBUF_LEN-1);
  151. if (copy_from_user(addr, buffer, len))
  152. return -EFAULT;
  153. addr[len] = 0;
  154. strip_it(addr);
  155. if (parse_addr(&dnaddr, addr))
  156. return -EINVAL;
  157. dn_dev_devices_off();
  158. decnet_address = dnaddr;
  159. dn_dev_devices_on();
  160. *ppos += len;
  161. return 0;
  162. }
  163. dn_addr2asc(dn_ntohs(decnet_address), addr);
  164. len = strlen(addr);
  165. addr[len++] = '\n';
  166. if (len > *lenp) len = *lenp;
  167. if (copy_to_user(buffer, addr, len))
  168. return -EFAULT;
  169. *lenp = len;
  170. *ppos += len;
  171. return 0;
  172. }
  173. static int dn_def_dev_strategy(ctl_table *table, int __user *name, int nlen,
  174. void __user *oldval, size_t __user *oldlenp,
  175. void __user *newval, size_t newlen,
  176. void **context)
  177. {
  178. size_t len;
  179. struct net_device *dev;
  180. char devname[17];
  181. size_t namel;
  182. int rv = 0;
  183. devname[0] = 0;
  184. if (oldval && oldlenp) {
  185. if (get_user(len, oldlenp))
  186. return -EFAULT;
  187. if (len) {
  188. dev = dn_dev_get_default();
  189. if (dev) {
  190. strcpy(devname, dev->name);
  191. dev_put(dev);
  192. }
  193. namel = strlen(devname) + 1;
  194. if (len > namel) len = namel;
  195. if (copy_to_user(oldval, devname, len))
  196. return -EFAULT;
  197. if (put_user(len, oldlenp))
  198. return -EFAULT;
  199. }
  200. }
  201. if (newval && newlen) {
  202. if (newlen > 16)
  203. return -E2BIG;
  204. if (copy_from_user(devname, newval, newlen))
  205. return -EFAULT;
  206. devname[newlen] = 0;
  207. dev = dev_get_by_name(devname);
  208. if (dev == NULL)
  209. return -ENODEV;
  210. rv = -ENODEV;
  211. if (dev->dn_ptr != NULL) {
  212. rv = dn_dev_set_default(dev, 1);
  213. if (rv)
  214. dev_put(dev);
  215. }
  216. }
  217. return rv;
  218. }
  219. static int dn_def_dev_handler(ctl_table *table, int write,
  220. struct file * filp,
  221. void __user *buffer,
  222. size_t *lenp, loff_t *ppos)
  223. {
  224. size_t len;
  225. struct net_device *dev;
  226. char devname[17];
  227. if (!*lenp || (*ppos && !write)) {
  228. *lenp = 0;
  229. return 0;
  230. }
  231. if (write) {
  232. if (*lenp > 16)
  233. return -E2BIG;
  234. if (copy_from_user(devname, buffer, *lenp))
  235. return -EFAULT;
  236. devname[*lenp] = 0;
  237. strip_it(devname);
  238. dev = dev_get_by_name(devname);
  239. if (dev == NULL)
  240. return -ENODEV;
  241. if (dev->dn_ptr == NULL) {
  242. dev_put(dev);
  243. return -ENODEV;
  244. }
  245. if (dn_dev_set_default(dev, 1)) {
  246. dev_put(dev);
  247. return -ENODEV;
  248. }
  249. *ppos += *lenp;
  250. return 0;
  251. }
  252. dev = dn_dev_get_default();
  253. if (dev == NULL) {
  254. *lenp = 0;
  255. return 0;
  256. }
  257. strcpy(devname, dev->name);
  258. dev_put(dev);
  259. len = strlen(devname);
  260. devname[len++] = '\n';
  261. if (len > *lenp) len = *lenp;
  262. if (copy_to_user(buffer, devname, len))
  263. return -EFAULT;
  264. *lenp = len;
  265. *ppos += len;
  266. return 0;
  267. }
  268. static ctl_table dn_table[] = {
  269. {
  270. .ctl_name = NET_DECNET_NODE_ADDRESS,
  271. .procname = "node_address",
  272. .maxlen = 7,
  273. .mode = 0644,
  274. .proc_handler = dn_node_address_handler,
  275. .strategy = dn_node_address_strategy,
  276. },
  277. {
  278. .ctl_name = NET_DECNET_NODE_NAME,
  279. .procname = "node_name",
  280. .data = node_name,
  281. .maxlen = 7,
  282. .mode = 0644,
  283. .proc_handler = &proc_dostring,
  284. .strategy = &sysctl_string,
  285. },
  286. {
  287. .ctl_name = NET_DECNET_DEFAULT_DEVICE,
  288. .procname = "default_device",
  289. .maxlen = 16,
  290. .mode = 0644,
  291. .proc_handler = dn_def_dev_handler,
  292. .strategy = dn_def_dev_strategy,
  293. },
  294. {
  295. .ctl_name = NET_DECNET_TIME_WAIT,
  296. .procname = "time_wait",
  297. .data = &decnet_time_wait,
  298. .maxlen = sizeof(int),
  299. .mode = 0644,
  300. .proc_handler = &proc_dointvec_minmax,
  301. .strategy = &sysctl_intvec,
  302. .extra1 = &min_decnet_time_wait,
  303. .extra2 = &max_decnet_time_wait
  304. },
  305. {
  306. .ctl_name = NET_DECNET_DN_COUNT,
  307. .procname = "dn_count",
  308. .data = &decnet_dn_count,
  309. .maxlen = sizeof(int),
  310. .mode = 0644,
  311. .proc_handler = &proc_dointvec_minmax,
  312. .strategy = &sysctl_intvec,
  313. .extra1 = &min_state_count,
  314. .extra2 = &max_state_count
  315. },
  316. {
  317. .ctl_name = NET_DECNET_DI_COUNT,
  318. .procname = "di_count",
  319. .data = &decnet_di_count,
  320. .maxlen = sizeof(int),
  321. .mode = 0644,
  322. .proc_handler = &proc_dointvec_minmax,
  323. .strategy = &sysctl_intvec,
  324. .extra1 = &min_state_count,
  325. .extra2 = &max_state_count
  326. },
  327. {
  328. .ctl_name = NET_DECNET_DR_COUNT,
  329. .procname = "dr_count",
  330. .data = &decnet_dr_count,
  331. .maxlen = sizeof(int),
  332. .mode = 0644,
  333. .proc_handler = &proc_dointvec_minmax,
  334. .strategy = &sysctl_intvec,
  335. .extra1 = &min_state_count,
  336. .extra2 = &max_state_count
  337. },
  338. {
  339. .ctl_name = NET_DECNET_DST_GC_INTERVAL,
  340. .procname = "dst_gc_interval",
  341. .data = &decnet_dst_gc_interval,
  342. .maxlen = sizeof(int),
  343. .mode = 0644,
  344. .proc_handler = &proc_dointvec_minmax,
  345. .strategy = &sysctl_intvec,
  346. .extra1 = &min_decnet_dst_gc_interval,
  347. .extra2 = &max_decnet_dst_gc_interval
  348. },
  349. {
  350. .ctl_name = NET_DECNET_NO_FC_MAX_CWND,
  351. .procname = "no_fc_max_cwnd",
  352. .data = &decnet_no_fc_max_cwnd,
  353. .maxlen = sizeof(int),
  354. .mode = 0644,
  355. .proc_handler = &proc_dointvec_minmax,
  356. .strategy = &sysctl_intvec,
  357. .extra1 = &min_decnet_no_fc_max_cwnd,
  358. .extra2 = &max_decnet_no_fc_max_cwnd
  359. },
  360. {
  361. .ctl_name = NET_DECNET_DEBUG_LEVEL,
  362. .procname = "debug",
  363. .data = &decnet_debug_level,
  364. .maxlen = sizeof(int),
  365. .mode = 0644,
  366. .proc_handler = &proc_dointvec,
  367. .strategy = &sysctl_intvec,
  368. },
  369. {0}
  370. };
  371. static ctl_table dn_dir_table[] = {
  372. {
  373. .ctl_name = NET_DECNET,
  374. .procname = "decnet",
  375. .mode = 0555,
  376. .child = dn_table},
  377. {0}
  378. };
  379. static ctl_table dn_root_table[] = {
  380. {
  381. .ctl_name = CTL_NET,
  382. .procname = "net",
  383. .mode = 0555,
  384. .child = dn_dir_table
  385. },
  386. {0}
  387. };
  388. void dn_register_sysctl(void)
  389. {
  390. dn_table_header = register_sysctl_table(dn_root_table, 1);
  391. }
  392. void dn_unregister_sysctl(void)
  393. {
  394. unregister_sysctl_table(dn_table_header);
  395. }
  396. #else /* CONFIG_SYSCTL */
  397. void dn_unregister_sysctl(void)
  398. {
  399. }
  400. void dn_register_sysctl(void)
  401. {
  402. }
  403. #endif