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