netconsole.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * linux/drivers/net/netconsole.c
  4. *
  5. * Copyright (C) 2001 Ingo Molnar <[email protected]>
  6. *
  7. * This file contains the implementation of an IRQ-safe, crash-safe
  8. * kernel console implementation that outputs kernel messages to the
  9. * network.
  10. *
  11. * Modification history:
  12. *
  13. * 2001-09-17 started by Ingo Molnar.
  14. * 2003-08-11 2.6 port by Matt Mackall
  15. * simplified options
  16. * generic card hooks
  17. * works non-modular
  18. * 2003-09-07 rewritten with netpoll api
  19. */
  20. /****************************************************************
  21. *
  22. ****************************************************************/
  23. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  24. #include <linux/mm.h>
  25. #include <linux/init.h>
  26. #include <linux/module.h>
  27. #include <linux/slab.h>
  28. #include <linux/console.h>
  29. #include <linux/moduleparam.h>
  30. #include <linux/kernel.h>
  31. #include <linux/string.h>
  32. #include <linux/netpoll.h>
  33. #include <linux/inet.h>
  34. #include <linux/configfs.h>
  35. #include <linux/etherdevice.h>
  36. MODULE_AUTHOR("Maintainer: Matt Mackall <[email protected]>");
  37. MODULE_DESCRIPTION("Console driver for network interfaces");
  38. MODULE_LICENSE("GPL");
  39. #define MAX_PARAM_LENGTH 256
  40. #define MAX_PRINT_CHUNK 1000
  41. static char config[MAX_PARAM_LENGTH];
  42. module_param_string(netconsole, config, MAX_PARAM_LENGTH, 0);
  43. MODULE_PARM_DESC(netconsole, " netconsole=[src-port]@[src-ip]/[dev],[tgt-port]@<tgt-ip>/[tgt-macaddr]");
  44. static bool oops_only = false;
  45. module_param(oops_only, bool, 0600);
  46. MODULE_PARM_DESC(oops_only, "Only log oops messages");
  47. #ifndef MODULE
  48. static int __init option_setup(char *opt)
  49. {
  50. strscpy(config, opt, MAX_PARAM_LENGTH);
  51. return 1;
  52. }
  53. __setup("netconsole=", option_setup);
  54. #endif /* MODULE */
  55. /* Linked list of all configured targets */
  56. static LIST_HEAD(target_list);
  57. /* This needs to be a spinlock because write_msg() cannot sleep */
  58. static DEFINE_SPINLOCK(target_list_lock);
  59. /*
  60. * Console driver for extended netconsoles. Registered on the first use to
  61. * avoid unnecessarily enabling ext message formatting.
  62. */
  63. static struct console netconsole_ext;
  64. /**
  65. * struct netconsole_target - Represents a configured netconsole target.
  66. * @list: Links this target into the target_list.
  67. * @item: Links us into the configfs subsystem hierarchy.
  68. * @enabled: On / off knob to enable / disable target.
  69. * Visible from userspace (read-write).
  70. * We maintain a strict 1:1 correspondence between this and
  71. * whether the corresponding netpoll is active or inactive.
  72. * Also, other parameters of a target may be modified at
  73. * runtime only when it is disabled (enabled == 0).
  74. * @extended: Denotes whether console is extended or not.
  75. * @np: The netpoll structure for this target.
  76. * Contains the other userspace visible parameters:
  77. * dev_name (read-write)
  78. * local_port (read-write)
  79. * remote_port (read-write)
  80. * local_ip (read-write)
  81. * remote_ip (read-write)
  82. * local_mac (read-only)
  83. * remote_mac (read-write)
  84. */
  85. struct netconsole_target {
  86. struct list_head list;
  87. #ifdef CONFIG_NETCONSOLE_DYNAMIC
  88. struct config_item item;
  89. #endif
  90. bool enabled;
  91. bool extended;
  92. struct netpoll np;
  93. };
  94. #ifdef CONFIG_NETCONSOLE_DYNAMIC
  95. static struct configfs_subsystem netconsole_subsys;
  96. static DEFINE_MUTEX(dynamic_netconsole_mutex);
  97. static int __init dynamic_netconsole_init(void)
  98. {
  99. config_group_init(&netconsole_subsys.su_group);
  100. mutex_init(&netconsole_subsys.su_mutex);
  101. return configfs_register_subsystem(&netconsole_subsys);
  102. }
  103. static void __exit dynamic_netconsole_exit(void)
  104. {
  105. configfs_unregister_subsystem(&netconsole_subsys);
  106. }
  107. /*
  108. * Targets that were created by parsing the boot/module option string
  109. * do not exist in the configfs hierarchy (and have NULL names) and will
  110. * never go away, so make these a no-op for them.
  111. */
  112. static void netconsole_target_get(struct netconsole_target *nt)
  113. {
  114. if (config_item_name(&nt->item))
  115. config_item_get(&nt->item);
  116. }
  117. static void netconsole_target_put(struct netconsole_target *nt)
  118. {
  119. if (config_item_name(&nt->item))
  120. config_item_put(&nt->item);
  121. }
  122. #else /* !CONFIG_NETCONSOLE_DYNAMIC */
  123. static int __init dynamic_netconsole_init(void)
  124. {
  125. return 0;
  126. }
  127. static void __exit dynamic_netconsole_exit(void)
  128. {
  129. }
  130. /*
  131. * No danger of targets going away from under us when dynamic
  132. * reconfigurability is off.
  133. */
  134. static void netconsole_target_get(struct netconsole_target *nt)
  135. {
  136. }
  137. static void netconsole_target_put(struct netconsole_target *nt)
  138. {
  139. }
  140. #endif /* CONFIG_NETCONSOLE_DYNAMIC */
  141. /* Allocate new target (from boot/module param) and setup netpoll for it */
  142. static struct netconsole_target *alloc_param_target(char *target_config)
  143. {
  144. int err = -ENOMEM;
  145. struct netconsole_target *nt;
  146. /*
  147. * Allocate and initialize with defaults.
  148. * Note that these targets get their config_item fields zeroed-out.
  149. */
  150. nt = kzalloc(sizeof(*nt), GFP_KERNEL);
  151. if (!nt)
  152. goto fail;
  153. nt->np.name = "netconsole";
  154. strscpy(nt->np.dev_name, "eth0", IFNAMSIZ);
  155. nt->np.local_port = 6665;
  156. nt->np.remote_port = 6666;
  157. eth_broadcast_addr(nt->np.remote_mac);
  158. if (*target_config == '+') {
  159. nt->extended = true;
  160. target_config++;
  161. }
  162. /* Parse parameters and setup netpoll */
  163. err = netpoll_parse_options(&nt->np, target_config);
  164. if (err)
  165. goto fail;
  166. err = netpoll_setup(&nt->np);
  167. if (err)
  168. goto fail;
  169. nt->enabled = true;
  170. return nt;
  171. fail:
  172. kfree(nt);
  173. return ERR_PTR(err);
  174. }
  175. /* Cleanup netpoll for given target (from boot/module param) and free it */
  176. static void free_param_target(struct netconsole_target *nt)
  177. {
  178. netpoll_cleanup(&nt->np);
  179. kfree(nt);
  180. }
  181. #ifdef CONFIG_NETCONSOLE_DYNAMIC
  182. /*
  183. * Our subsystem hierarchy is:
  184. *
  185. * /sys/kernel/config/netconsole/
  186. * |
  187. * <target>/
  188. * | enabled
  189. * | dev_name
  190. * | local_port
  191. * | remote_port
  192. * | local_ip
  193. * | remote_ip
  194. * | local_mac
  195. * | remote_mac
  196. * |
  197. * <target>/...
  198. */
  199. static struct netconsole_target *to_target(struct config_item *item)
  200. {
  201. return item ?
  202. container_of(item, struct netconsole_target, item) :
  203. NULL;
  204. }
  205. /*
  206. * Attribute operations for netconsole_target.
  207. */
  208. static ssize_t enabled_show(struct config_item *item, char *buf)
  209. {
  210. return snprintf(buf, PAGE_SIZE, "%d\n", to_target(item)->enabled);
  211. }
  212. static ssize_t extended_show(struct config_item *item, char *buf)
  213. {
  214. return snprintf(buf, PAGE_SIZE, "%d\n", to_target(item)->extended);
  215. }
  216. static ssize_t dev_name_show(struct config_item *item, char *buf)
  217. {
  218. return snprintf(buf, PAGE_SIZE, "%s\n", to_target(item)->np.dev_name);
  219. }
  220. static ssize_t local_port_show(struct config_item *item, char *buf)
  221. {
  222. return snprintf(buf, PAGE_SIZE, "%d\n", to_target(item)->np.local_port);
  223. }
  224. static ssize_t remote_port_show(struct config_item *item, char *buf)
  225. {
  226. return snprintf(buf, PAGE_SIZE, "%d\n", to_target(item)->np.remote_port);
  227. }
  228. static ssize_t local_ip_show(struct config_item *item, char *buf)
  229. {
  230. struct netconsole_target *nt = to_target(item);
  231. if (nt->np.ipv6)
  232. return snprintf(buf, PAGE_SIZE, "%pI6c\n", &nt->np.local_ip.in6);
  233. else
  234. return snprintf(buf, PAGE_SIZE, "%pI4\n", &nt->np.local_ip);
  235. }
  236. static ssize_t remote_ip_show(struct config_item *item, char *buf)
  237. {
  238. struct netconsole_target *nt = to_target(item);
  239. if (nt->np.ipv6)
  240. return snprintf(buf, PAGE_SIZE, "%pI6c\n", &nt->np.remote_ip.in6);
  241. else
  242. return snprintf(buf, PAGE_SIZE, "%pI4\n", &nt->np.remote_ip);
  243. }
  244. static ssize_t local_mac_show(struct config_item *item, char *buf)
  245. {
  246. struct net_device *dev = to_target(item)->np.dev;
  247. static const u8 bcast[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
  248. return snprintf(buf, PAGE_SIZE, "%pM\n", dev ? dev->dev_addr : bcast);
  249. }
  250. static ssize_t remote_mac_show(struct config_item *item, char *buf)
  251. {
  252. return snprintf(buf, PAGE_SIZE, "%pM\n", to_target(item)->np.remote_mac);
  253. }
  254. /*
  255. * This one is special -- targets created through the configfs interface
  256. * are not enabled (and the corresponding netpoll activated) by default.
  257. * The user is expected to set the desired parameters first (which
  258. * would enable him to dynamically add new netpoll targets for new
  259. * network interfaces as and when they come up).
  260. */
  261. static ssize_t enabled_store(struct config_item *item,
  262. const char *buf, size_t count)
  263. {
  264. struct netconsole_target *nt = to_target(item);
  265. unsigned long flags;
  266. int enabled;
  267. int err;
  268. mutex_lock(&dynamic_netconsole_mutex);
  269. err = kstrtoint(buf, 10, &enabled);
  270. if (err < 0)
  271. goto out_unlock;
  272. err = -EINVAL;
  273. if (enabled < 0 || enabled > 1)
  274. goto out_unlock;
  275. if ((bool)enabled == nt->enabled) {
  276. pr_info("network logging has already %s\n",
  277. nt->enabled ? "started" : "stopped");
  278. goto out_unlock;
  279. }
  280. if (enabled) { /* true */
  281. if (nt->extended && !(netconsole_ext.flags & CON_ENABLED)) {
  282. netconsole_ext.flags |= CON_ENABLED;
  283. register_console(&netconsole_ext);
  284. }
  285. /*
  286. * Skip netpoll_parse_options() -- all the attributes are
  287. * already configured via configfs. Just print them out.
  288. */
  289. netpoll_print_options(&nt->np);
  290. err = netpoll_setup(&nt->np);
  291. if (err)
  292. goto out_unlock;
  293. pr_info("network logging started\n");
  294. } else { /* false */
  295. /* We need to disable the netconsole before cleaning it up
  296. * otherwise we might end up in write_msg() with
  297. * nt->np.dev == NULL and nt->enabled == true
  298. */
  299. spin_lock_irqsave(&target_list_lock, flags);
  300. nt->enabled = false;
  301. spin_unlock_irqrestore(&target_list_lock, flags);
  302. netpoll_cleanup(&nt->np);
  303. }
  304. nt->enabled = enabled;
  305. mutex_unlock(&dynamic_netconsole_mutex);
  306. return strnlen(buf, count);
  307. out_unlock:
  308. mutex_unlock(&dynamic_netconsole_mutex);
  309. return err;
  310. }
  311. static ssize_t extended_store(struct config_item *item, const char *buf,
  312. size_t count)
  313. {
  314. struct netconsole_target *nt = to_target(item);
  315. int extended;
  316. int err;
  317. mutex_lock(&dynamic_netconsole_mutex);
  318. if (nt->enabled) {
  319. pr_err("target (%s) is enabled, disable to update parameters\n",
  320. config_item_name(&nt->item));
  321. err = -EINVAL;
  322. goto out_unlock;
  323. }
  324. err = kstrtoint(buf, 10, &extended);
  325. if (err < 0)
  326. goto out_unlock;
  327. if (extended < 0 || extended > 1) {
  328. err = -EINVAL;
  329. goto out_unlock;
  330. }
  331. nt->extended = extended;
  332. mutex_unlock(&dynamic_netconsole_mutex);
  333. return strnlen(buf, count);
  334. out_unlock:
  335. mutex_unlock(&dynamic_netconsole_mutex);
  336. return err;
  337. }
  338. static ssize_t dev_name_store(struct config_item *item, const char *buf,
  339. size_t count)
  340. {
  341. struct netconsole_target *nt = to_target(item);
  342. size_t len;
  343. mutex_lock(&dynamic_netconsole_mutex);
  344. if (nt->enabled) {
  345. pr_err("target (%s) is enabled, disable to update parameters\n",
  346. config_item_name(&nt->item));
  347. mutex_unlock(&dynamic_netconsole_mutex);
  348. return -EINVAL;
  349. }
  350. strscpy(nt->np.dev_name, buf, IFNAMSIZ);
  351. /* Get rid of possible trailing newline from echo(1) */
  352. len = strnlen(nt->np.dev_name, IFNAMSIZ);
  353. if (nt->np.dev_name[len - 1] == '\n')
  354. nt->np.dev_name[len - 1] = '\0';
  355. mutex_unlock(&dynamic_netconsole_mutex);
  356. return strnlen(buf, count);
  357. }
  358. static ssize_t local_port_store(struct config_item *item, const char *buf,
  359. size_t count)
  360. {
  361. struct netconsole_target *nt = to_target(item);
  362. int rv = -EINVAL;
  363. mutex_lock(&dynamic_netconsole_mutex);
  364. if (nt->enabled) {
  365. pr_err("target (%s) is enabled, disable to update parameters\n",
  366. config_item_name(&nt->item));
  367. goto out_unlock;
  368. }
  369. rv = kstrtou16(buf, 10, &nt->np.local_port);
  370. if (rv < 0)
  371. goto out_unlock;
  372. mutex_unlock(&dynamic_netconsole_mutex);
  373. return strnlen(buf, count);
  374. out_unlock:
  375. mutex_unlock(&dynamic_netconsole_mutex);
  376. return rv;
  377. }
  378. static ssize_t remote_port_store(struct config_item *item,
  379. const char *buf, size_t count)
  380. {
  381. struct netconsole_target *nt = to_target(item);
  382. int rv = -EINVAL;
  383. mutex_lock(&dynamic_netconsole_mutex);
  384. if (nt->enabled) {
  385. pr_err("target (%s) is enabled, disable to update parameters\n",
  386. config_item_name(&nt->item));
  387. goto out_unlock;
  388. }
  389. rv = kstrtou16(buf, 10, &nt->np.remote_port);
  390. if (rv < 0)
  391. goto out_unlock;
  392. mutex_unlock(&dynamic_netconsole_mutex);
  393. return strnlen(buf, count);
  394. out_unlock:
  395. mutex_unlock(&dynamic_netconsole_mutex);
  396. return rv;
  397. }
  398. static ssize_t local_ip_store(struct config_item *item, const char *buf,
  399. size_t count)
  400. {
  401. struct netconsole_target *nt = to_target(item);
  402. mutex_lock(&dynamic_netconsole_mutex);
  403. if (nt->enabled) {
  404. pr_err("target (%s) is enabled, disable to update parameters\n",
  405. config_item_name(&nt->item));
  406. goto out_unlock;
  407. }
  408. if (strnchr(buf, count, ':')) {
  409. const char *end;
  410. if (in6_pton(buf, count, nt->np.local_ip.in6.s6_addr, -1, &end) > 0) {
  411. if (*end && *end != '\n') {
  412. pr_err("invalid IPv6 address at: <%c>\n", *end);
  413. goto out_unlock;
  414. }
  415. nt->np.ipv6 = true;
  416. } else
  417. goto out_unlock;
  418. } else {
  419. if (!nt->np.ipv6) {
  420. nt->np.local_ip.ip = in_aton(buf);
  421. } else
  422. goto out_unlock;
  423. }
  424. mutex_unlock(&dynamic_netconsole_mutex);
  425. return strnlen(buf, count);
  426. out_unlock:
  427. mutex_unlock(&dynamic_netconsole_mutex);
  428. return -EINVAL;
  429. }
  430. static ssize_t remote_ip_store(struct config_item *item, const char *buf,
  431. size_t count)
  432. {
  433. struct netconsole_target *nt = to_target(item);
  434. mutex_lock(&dynamic_netconsole_mutex);
  435. if (nt->enabled) {
  436. pr_err("target (%s) is enabled, disable to update parameters\n",
  437. config_item_name(&nt->item));
  438. goto out_unlock;
  439. }
  440. if (strnchr(buf, count, ':')) {
  441. const char *end;
  442. if (in6_pton(buf, count, nt->np.remote_ip.in6.s6_addr, -1, &end) > 0) {
  443. if (*end && *end != '\n') {
  444. pr_err("invalid IPv6 address at: <%c>\n", *end);
  445. goto out_unlock;
  446. }
  447. nt->np.ipv6 = true;
  448. } else
  449. goto out_unlock;
  450. } else {
  451. if (!nt->np.ipv6) {
  452. nt->np.remote_ip.ip = in_aton(buf);
  453. } else
  454. goto out_unlock;
  455. }
  456. mutex_unlock(&dynamic_netconsole_mutex);
  457. return strnlen(buf, count);
  458. out_unlock:
  459. mutex_unlock(&dynamic_netconsole_mutex);
  460. return -EINVAL;
  461. }
  462. static ssize_t remote_mac_store(struct config_item *item, const char *buf,
  463. size_t count)
  464. {
  465. struct netconsole_target *nt = to_target(item);
  466. u8 remote_mac[ETH_ALEN];
  467. mutex_lock(&dynamic_netconsole_mutex);
  468. if (nt->enabled) {
  469. pr_err("target (%s) is enabled, disable to update parameters\n",
  470. config_item_name(&nt->item));
  471. goto out_unlock;
  472. }
  473. if (!mac_pton(buf, remote_mac))
  474. goto out_unlock;
  475. if (buf[3 * ETH_ALEN - 1] && buf[3 * ETH_ALEN - 1] != '\n')
  476. goto out_unlock;
  477. memcpy(nt->np.remote_mac, remote_mac, ETH_ALEN);
  478. mutex_unlock(&dynamic_netconsole_mutex);
  479. return strnlen(buf, count);
  480. out_unlock:
  481. mutex_unlock(&dynamic_netconsole_mutex);
  482. return -EINVAL;
  483. }
  484. CONFIGFS_ATTR(, enabled);
  485. CONFIGFS_ATTR(, extended);
  486. CONFIGFS_ATTR(, dev_name);
  487. CONFIGFS_ATTR(, local_port);
  488. CONFIGFS_ATTR(, remote_port);
  489. CONFIGFS_ATTR(, local_ip);
  490. CONFIGFS_ATTR(, remote_ip);
  491. CONFIGFS_ATTR_RO(, local_mac);
  492. CONFIGFS_ATTR(, remote_mac);
  493. static struct configfs_attribute *netconsole_target_attrs[] = {
  494. &attr_enabled,
  495. &attr_extended,
  496. &attr_dev_name,
  497. &attr_local_port,
  498. &attr_remote_port,
  499. &attr_local_ip,
  500. &attr_remote_ip,
  501. &attr_local_mac,
  502. &attr_remote_mac,
  503. NULL,
  504. };
  505. /*
  506. * Item operations and type for netconsole_target.
  507. */
  508. static void netconsole_target_release(struct config_item *item)
  509. {
  510. kfree(to_target(item));
  511. }
  512. static struct configfs_item_operations netconsole_target_item_ops = {
  513. .release = netconsole_target_release,
  514. };
  515. static const struct config_item_type netconsole_target_type = {
  516. .ct_attrs = netconsole_target_attrs,
  517. .ct_item_ops = &netconsole_target_item_ops,
  518. .ct_owner = THIS_MODULE,
  519. };
  520. /*
  521. * Group operations and type for netconsole_subsys.
  522. */
  523. static struct config_item *make_netconsole_target(struct config_group *group,
  524. const char *name)
  525. {
  526. unsigned long flags;
  527. struct netconsole_target *nt;
  528. /*
  529. * Allocate and initialize with defaults.
  530. * Target is disabled at creation (!enabled).
  531. */
  532. nt = kzalloc(sizeof(*nt), GFP_KERNEL);
  533. if (!nt)
  534. return ERR_PTR(-ENOMEM);
  535. nt->np.name = "netconsole";
  536. strscpy(nt->np.dev_name, "eth0", IFNAMSIZ);
  537. nt->np.local_port = 6665;
  538. nt->np.remote_port = 6666;
  539. eth_broadcast_addr(nt->np.remote_mac);
  540. /* Initialize the config_item member */
  541. config_item_init_type_name(&nt->item, name, &netconsole_target_type);
  542. /* Adding, but it is disabled */
  543. spin_lock_irqsave(&target_list_lock, flags);
  544. list_add(&nt->list, &target_list);
  545. spin_unlock_irqrestore(&target_list_lock, flags);
  546. return &nt->item;
  547. }
  548. static void drop_netconsole_target(struct config_group *group,
  549. struct config_item *item)
  550. {
  551. unsigned long flags;
  552. struct netconsole_target *nt = to_target(item);
  553. spin_lock_irqsave(&target_list_lock, flags);
  554. list_del(&nt->list);
  555. spin_unlock_irqrestore(&target_list_lock, flags);
  556. /*
  557. * The target may have never been enabled, or was manually disabled
  558. * before being removed so netpoll may have already been cleaned up.
  559. */
  560. if (nt->enabled)
  561. netpoll_cleanup(&nt->np);
  562. config_item_put(&nt->item);
  563. }
  564. static struct configfs_group_operations netconsole_subsys_group_ops = {
  565. .make_item = make_netconsole_target,
  566. .drop_item = drop_netconsole_target,
  567. };
  568. static const struct config_item_type netconsole_subsys_type = {
  569. .ct_group_ops = &netconsole_subsys_group_ops,
  570. .ct_owner = THIS_MODULE,
  571. };
  572. /* The netconsole configfs subsystem */
  573. static struct configfs_subsystem netconsole_subsys = {
  574. .su_group = {
  575. .cg_item = {
  576. .ci_namebuf = "netconsole",
  577. .ci_type = &netconsole_subsys_type,
  578. },
  579. },
  580. };
  581. #endif /* CONFIG_NETCONSOLE_DYNAMIC */
  582. /* Handle network interface device notifications */
  583. static int netconsole_netdev_event(struct notifier_block *this,
  584. unsigned long event, void *ptr)
  585. {
  586. unsigned long flags;
  587. struct netconsole_target *nt;
  588. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  589. bool stopped = false;
  590. if (!(event == NETDEV_CHANGENAME || event == NETDEV_UNREGISTER ||
  591. event == NETDEV_RELEASE || event == NETDEV_JOIN))
  592. goto done;
  593. spin_lock_irqsave(&target_list_lock, flags);
  594. restart:
  595. list_for_each_entry(nt, &target_list, list) {
  596. netconsole_target_get(nt);
  597. if (nt->np.dev == dev) {
  598. switch (event) {
  599. case NETDEV_CHANGENAME:
  600. strscpy(nt->np.dev_name, dev->name, IFNAMSIZ);
  601. break;
  602. case NETDEV_RELEASE:
  603. case NETDEV_JOIN:
  604. case NETDEV_UNREGISTER:
  605. /* rtnl_lock already held
  606. * we might sleep in __netpoll_cleanup()
  607. */
  608. spin_unlock_irqrestore(&target_list_lock, flags);
  609. __netpoll_cleanup(&nt->np);
  610. spin_lock_irqsave(&target_list_lock, flags);
  611. netdev_put(nt->np.dev, &nt->np.dev_tracker);
  612. nt->np.dev = NULL;
  613. nt->enabled = false;
  614. stopped = true;
  615. netconsole_target_put(nt);
  616. goto restart;
  617. }
  618. }
  619. netconsole_target_put(nt);
  620. }
  621. spin_unlock_irqrestore(&target_list_lock, flags);
  622. if (stopped) {
  623. const char *msg = "had an event";
  624. switch (event) {
  625. case NETDEV_UNREGISTER:
  626. msg = "unregistered";
  627. break;
  628. case NETDEV_RELEASE:
  629. msg = "released slaves";
  630. break;
  631. case NETDEV_JOIN:
  632. msg = "is joining a master device";
  633. break;
  634. }
  635. pr_info("network logging stopped on interface %s as it %s\n",
  636. dev->name, msg);
  637. }
  638. done:
  639. return NOTIFY_DONE;
  640. }
  641. static struct notifier_block netconsole_netdev_notifier = {
  642. .notifier_call = netconsole_netdev_event,
  643. };
  644. /**
  645. * send_ext_msg_udp - send extended log message to target
  646. * @nt: target to send message to
  647. * @msg: extended log message to send
  648. * @msg_len: length of message
  649. *
  650. * Transfer extended log @msg to @nt. If @msg is longer than
  651. * MAX_PRINT_CHUNK, it'll be split and transmitted in multiple chunks with
  652. * ncfrag header field added to identify them.
  653. */
  654. static void send_ext_msg_udp(struct netconsole_target *nt, const char *msg,
  655. int msg_len)
  656. {
  657. static char buf[MAX_PRINT_CHUNK]; /* protected by target_list_lock */
  658. const char *header, *body;
  659. int offset = 0;
  660. int header_len, body_len;
  661. if (msg_len <= MAX_PRINT_CHUNK) {
  662. netpoll_send_udp(&nt->np, msg, msg_len);
  663. return;
  664. }
  665. /* need to insert extra header fields, detect header and body */
  666. header = msg;
  667. body = memchr(msg, ';', msg_len);
  668. if (WARN_ON_ONCE(!body))
  669. return;
  670. header_len = body - header;
  671. body_len = msg_len - header_len - 1;
  672. body++;
  673. /*
  674. * Transfer multiple chunks with the following extra header.
  675. * "ncfrag=<byte-offset>/<total-bytes>"
  676. */
  677. memcpy(buf, header, header_len);
  678. while (offset < body_len) {
  679. int this_header = header_len;
  680. int this_chunk;
  681. this_header += scnprintf(buf + this_header,
  682. sizeof(buf) - this_header,
  683. ",ncfrag=%d/%d;", offset, body_len);
  684. this_chunk = min(body_len - offset,
  685. MAX_PRINT_CHUNK - this_header);
  686. if (WARN_ON_ONCE(this_chunk <= 0))
  687. return;
  688. memcpy(buf + this_header, body + offset, this_chunk);
  689. netpoll_send_udp(&nt->np, buf, this_header + this_chunk);
  690. offset += this_chunk;
  691. }
  692. }
  693. static void write_ext_msg(struct console *con, const char *msg,
  694. unsigned int len)
  695. {
  696. struct netconsole_target *nt;
  697. unsigned long flags;
  698. if ((oops_only && !oops_in_progress) || list_empty(&target_list))
  699. return;
  700. spin_lock_irqsave(&target_list_lock, flags);
  701. list_for_each_entry(nt, &target_list, list)
  702. if (nt->extended && nt->enabled && netif_running(nt->np.dev))
  703. send_ext_msg_udp(nt, msg, len);
  704. spin_unlock_irqrestore(&target_list_lock, flags);
  705. }
  706. static void write_msg(struct console *con, const char *msg, unsigned int len)
  707. {
  708. int frag, left;
  709. unsigned long flags;
  710. struct netconsole_target *nt;
  711. const char *tmp;
  712. if (oops_only && !oops_in_progress)
  713. return;
  714. /* Avoid taking lock and disabling interrupts unnecessarily */
  715. if (list_empty(&target_list))
  716. return;
  717. spin_lock_irqsave(&target_list_lock, flags);
  718. list_for_each_entry(nt, &target_list, list) {
  719. if (!nt->extended && nt->enabled && netif_running(nt->np.dev)) {
  720. /*
  721. * We nest this inside the for-each-target loop above
  722. * so that we're able to get as much logging out to
  723. * at least one target if we die inside here, instead
  724. * of unnecessarily keeping all targets in lock-step.
  725. */
  726. tmp = msg;
  727. for (left = len; left;) {
  728. frag = min(left, MAX_PRINT_CHUNK);
  729. netpoll_send_udp(&nt->np, tmp, frag);
  730. tmp += frag;
  731. left -= frag;
  732. }
  733. }
  734. }
  735. spin_unlock_irqrestore(&target_list_lock, flags);
  736. }
  737. static struct console netconsole_ext = {
  738. .name = "netcon_ext",
  739. .flags = CON_EXTENDED, /* starts disabled, registered on first use */
  740. .write = write_ext_msg,
  741. };
  742. static struct console netconsole = {
  743. .name = "netcon",
  744. .flags = CON_ENABLED,
  745. .write = write_msg,
  746. };
  747. static int __init init_netconsole(void)
  748. {
  749. int err;
  750. struct netconsole_target *nt, *tmp;
  751. unsigned long flags;
  752. char *target_config;
  753. char *input = config;
  754. if (strnlen(input, MAX_PARAM_LENGTH)) {
  755. while ((target_config = strsep(&input, ";"))) {
  756. nt = alloc_param_target(target_config);
  757. if (IS_ERR(nt)) {
  758. err = PTR_ERR(nt);
  759. goto fail;
  760. }
  761. /* Dump existing printks when we register */
  762. if (nt->extended)
  763. netconsole_ext.flags |= CON_PRINTBUFFER |
  764. CON_ENABLED;
  765. else
  766. netconsole.flags |= CON_PRINTBUFFER;
  767. spin_lock_irqsave(&target_list_lock, flags);
  768. list_add(&nt->list, &target_list);
  769. spin_unlock_irqrestore(&target_list_lock, flags);
  770. }
  771. }
  772. err = register_netdevice_notifier(&netconsole_netdev_notifier);
  773. if (err)
  774. goto fail;
  775. err = dynamic_netconsole_init();
  776. if (err)
  777. goto undonotifier;
  778. if (netconsole_ext.flags & CON_ENABLED)
  779. register_console(&netconsole_ext);
  780. register_console(&netconsole);
  781. pr_info("network logging started\n");
  782. return err;
  783. undonotifier:
  784. unregister_netdevice_notifier(&netconsole_netdev_notifier);
  785. fail:
  786. pr_err("cleaning up\n");
  787. /*
  788. * Remove all targets and destroy them (only targets created
  789. * from the boot/module option exist here). Skipping the list
  790. * lock is safe here, and netpoll_cleanup() will sleep.
  791. */
  792. list_for_each_entry_safe(nt, tmp, &target_list, list) {
  793. list_del(&nt->list);
  794. free_param_target(nt);
  795. }
  796. return err;
  797. }
  798. static void __exit cleanup_netconsole(void)
  799. {
  800. struct netconsole_target *nt, *tmp;
  801. unregister_console(&netconsole_ext);
  802. unregister_console(&netconsole);
  803. dynamic_netconsole_exit();
  804. unregister_netdevice_notifier(&netconsole_netdev_notifier);
  805. /*
  806. * Targets created via configfs pin references on our module
  807. * and would first be rmdir(2)'ed from userspace. We reach
  808. * here only when they are already destroyed, and only those
  809. * created from the boot/module option are left, so remove and
  810. * destroy them. Skipping the list lock is safe here, and
  811. * netpoll_cleanup() will sleep.
  812. */
  813. list_for_each_entry_safe(nt, tmp, &target_list, list) {
  814. list_del(&nt->list);
  815. free_param_target(nt);
  816. }
  817. }
  818. /*
  819. * Use late_initcall to ensure netconsole is
  820. * initialized after network device driver if built-in.
  821. *
  822. * late_initcall() and module_init() are identical if built as module.
  823. */
  824. late_initcall(init_netconsole);
  825. module_exit(cleanup_netconsole);