netconsole.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405
  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. #include <linux/utsname.h>
  37. #include <linux/rtnetlink.h>
  38. MODULE_AUTHOR("Matt Mackall <mpm@selenic.com>");
  39. MODULE_DESCRIPTION("Console driver for network interfaces");
  40. MODULE_LICENSE("GPL");
  41. #define MAX_PARAM_LENGTH 256
  42. #define MAX_USERDATA_ENTRY_LENGTH 256
  43. #define MAX_USERDATA_VALUE_LENGTH 200
  44. /* The number 3 comes from userdata entry format characters (' ', '=', '\n') */
  45. #define MAX_USERDATA_NAME_LENGTH (MAX_USERDATA_ENTRY_LENGTH - \
  46. MAX_USERDATA_VALUE_LENGTH - 3)
  47. #define MAX_USERDATA_ITEMS 16
  48. #define MAX_PRINT_CHUNK 1000
  49. static char config[MAX_PARAM_LENGTH];
  50. module_param_string(netconsole, config, MAX_PARAM_LENGTH, 0);
  51. MODULE_PARM_DESC(netconsole, " netconsole=[src-port]@[src-ip]/[dev],[tgt-port]@<tgt-ip>/[tgt-macaddr]");
  52. static bool oops_only;
  53. module_param(oops_only, bool, 0600);
  54. MODULE_PARM_DESC(oops_only, "Only log oops messages");
  55. #define NETCONSOLE_PARAM_TARGET_PREFIX "cmdline"
  56. #ifndef MODULE
  57. static int __init option_setup(char *opt)
  58. {
  59. strscpy(config, opt, MAX_PARAM_LENGTH);
  60. return 1;
  61. }
  62. __setup("netconsole=", option_setup);
  63. #endif /* MODULE */
  64. /* Linked list of all configured targets */
  65. static LIST_HEAD(target_list);
  66. /* target_cleanup_list is used to track targets that need to be cleaned outside
  67. * of target_list_lock. It should be cleaned in the same function it is
  68. * populated.
  69. */
  70. static LIST_HEAD(target_cleanup_list);
  71. /* This needs to be a spinlock because write_msg() cannot sleep */
  72. static DEFINE_SPINLOCK(target_list_lock);
  73. /* This needs to be a mutex because netpoll_cleanup might sleep */
  74. static DEFINE_MUTEX(target_cleanup_list_lock);
  75. /*
  76. * Console driver for extended netconsoles. Registered on the first use to
  77. * avoid unnecessarily enabling ext message formatting.
  78. */
  79. static struct console netconsole_ext;
  80. /**
  81. * struct netconsole_target - Represents a configured netconsole target.
  82. * @list: Links this target into the target_list.
  83. * @group: Links us into the configfs subsystem hierarchy.
  84. * @userdata_group: Links to the userdata configfs hierarchy
  85. * @userdata_complete: Cached, formatted string of append
  86. * @userdata_length: String length of userdata_complete
  87. * @enabled: On / off knob to enable / disable target.
  88. * Visible from userspace (read-write).
  89. * We maintain a strict 1:1 correspondence between this and
  90. * whether the corresponding netpoll is active or inactive.
  91. * Also, other parameters of a target may be modified at
  92. * runtime only when it is disabled (enabled == 0).
  93. * @extended: Denotes whether console is extended or not.
  94. * @release: Denotes whether kernel release version should be prepended
  95. * to the message. Depends on extended console.
  96. * @np: The netpoll structure for this target.
  97. * Contains the other userspace visible parameters:
  98. * dev_name (read-write)
  99. * local_port (read-write)
  100. * remote_port (read-write)
  101. * local_ip (read-write)
  102. * remote_ip (read-write)
  103. * local_mac (read-only)
  104. * remote_mac (read-write)
  105. */
  106. struct netconsole_target {
  107. struct list_head list;
  108. #ifdef CONFIG_NETCONSOLE_DYNAMIC
  109. struct config_group group;
  110. struct config_group userdata_group;
  111. char userdata_complete[MAX_USERDATA_ENTRY_LENGTH * MAX_USERDATA_ITEMS];
  112. size_t userdata_length;
  113. #endif
  114. bool enabled;
  115. bool extended;
  116. bool release;
  117. struct netpoll np;
  118. };
  119. #ifdef CONFIG_NETCONSOLE_DYNAMIC
  120. static struct configfs_subsystem netconsole_subsys;
  121. static DEFINE_MUTEX(dynamic_netconsole_mutex);
  122. static int __init dynamic_netconsole_init(void)
  123. {
  124. config_group_init(&netconsole_subsys.su_group);
  125. mutex_init(&netconsole_subsys.su_mutex);
  126. return configfs_register_subsystem(&netconsole_subsys);
  127. }
  128. static void __exit dynamic_netconsole_exit(void)
  129. {
  130. configfs_unregister_subsystem(&netconsole_subsys);
  131. }
  132. /*
  133. * Targets that were created by parsing the boot/module option string
  134. * do not exist in the configfs hierarchy (and have NULL names) and will
  135. * never go away, so make these a no-op for them.
  136. */
  137. static void netconsole_target_get(struct netconsole_target *nt)
  138. {
  139. if (config_item_name(&nt->group.cg_item))
  140. config_group_get(&nt->group);
  141. }
  142. static void netconsole_target_put(struct netconsole_target *nt)
  143. {
  144. if (config_item_name(&nt->group.cg_item))
  145. config_group_put(&nt->group);
  146. }
  147. #else /* !CONFIG_NETCONSOLE_DYNAMIC */
  148. static int __init dynamic_netconsole_init(void)
  149. {
  150. return 0;
  151. }
  152. static void __exit dynamic_netconsole_exit(void)
  153. {
  154. }
  155. /*
  156. * No danger of targets going away from under us when dynamic
  157. * reconfigurability is off.
  158. */
  159. static void netconsole_target_get(struct netconsole_target *nt)
  160. {
  161. }
  162. static void netconsole_target_put(struct netconsole_target *nt)
  163. {
  164. }
  165. static void populate_configfs_item(struct netconsole_target *nt,
  166. int cmdline_count)
  167. {
  168. }
  169. #endif /* CONFIG_NETCONSOLE_DYNAMIC */
  170. /* Allocate and initialize with defaults.
  171. * Note that these targets get their config_item fields zeroed-out.
  172. */
  173. static struct netconsole_target *alloc_and_init(void)
  174. {
  175. struct netconsole_target *nt;
  176. nt = kzalloc(sizeof(*nt), GFP_KERNEL);
  177. if (!nt)
  178. return nt;
  179. if (IS_ENABLED(CONFIG_NETCONSOLE_EXTENDED_LOG))
  180. nt->extended = true;
  181. if (IS_ENABLED(CONFIG_NETCONSOLE_PREPEND_RELEASE))
  182. nt->release = true;
  183. nt->np.name = "netconsole";
  184. strscpy(nt->np.dev_name, "eth0", IFNAMSIZ);
  185. nt->np.local_port = 6665;
  186. nt->np.remote_port = 6666;
  187. eth_broadcast_addr(nt->np.remote_mac);
  188. return nt;
  189. }
  190. /* Clean up every target in the cleanup_list and move the clean targets back to
  191. * the main target_list.
  192. */
  193. static void netconsole_process_cleanups_core(void)
  194. {
  195. struct netconsole_target *nt, *tmp;
  196. unsigned long flags;
  197. /* The cleanup needs RTNL locked */
  198. ASSERT_RTNL();
  199. mutex_lock(&target_cleanup_list_lock);
  200. list_for_each_entry_safe(nt, tmp, &target_cleanup_list, list) {
  201. /* all entries in the cleanup_list needs to be disabled */
  202. WARN_ON_ONCE(nt->enabled);
  203. do_netpoll_cleanup(&nt->np);
  204. /* moved the cleaned target to target_list. Need to hold both
  205. * locks
  206. */
  207. spin_lock_irqsave(&target_list_lock, flags);
  208. list_move(&nt->list, &target_list);
  209. spin_unlock_irqrestore(&target_list_lock, flags);
  210. }
  211. WARN_ON_ONCE(!list_empty(&target_cleanup_list));
  212. mutex_unlock(&target_cleanup_list_lock);
  213. }
  214. #ifdef CONFIG_NETCONSOLE_DYNAMIC
  215. /*
  216. * Our subsystem hierarchy is:
  217. *
  218. * /sys/kernel/config/netconsole/
  219. * |
  220. * <target>/
  221. * | enabled
  222. * | release
  223. * | dev_name
  224. * | local_port
  225. * | remote_port
  226. * | local_ip
  227. * | remote_ip
  228. * | local_mac
  229. * | remote_mac
  230. * | userdata/
  231. * | <key>/
  232. * | value
  233. * | ...
  234. * |
  235. * <target>/...
  236. */
  237. static struct netconsole_target *to_target(struct config_item *item)
  238. {
  239. struct config_group *cfg_group;
  240. cfg_group = to_config_group(item);
  241. if (!cfg_group)
  242. return NULL;
  243. return container_of(to_config_group(item),
  244. struct netconsole_target, group);
  245. }
  246. /* Do the list cleanup with the rtnl lock hold. rtnl lock is necessary because
  247. * netdev might be cleaned-up by calling __netpoll_cleanup(),
  248. */
  249. static void netconsole_process_cleanups(void)
  250. {
  251. /* rtnl lock is called here, because it has precedence over
  252. * target_cleanup_list_lock mutex and target_cleanup_list
  253. */
  254. rtnl_lock();
  255. netconsole_process_cleanups_core();
  256. rtnl_unlock();
  257. }
  258. /* Get rid of possible trailing newline, returning the new length */
  259. static void trim_newline(char *s, size_t maxlen)
  260. {
  261. size_t len;
  262. len = strnlen(s, maxlen);
  263. if (s[len - 1] == '\n')
  264. s[len - 1] = '\0';
  265. }
  266. /*
  267. * Attribute operations for netconsole_target.
  268. */
  269. static ssize_t enabled_show(struct config_item *item, char *buf)
  270. {
  271. return sysfs_emit(buf, "%d\n", to_target(item)->enabled);
  272. }
  273. static ssize_t extended_show(struct config_item *item, char *buf)
  274. {
  275. return sysfs_emit(buf, "%d\n", to_target(item)->extended);
  276. }
  277. static ssize_t release_show(struct config_item *item, char *buf)
  278. {
  279. return sysfs_emit(buf, "%d\n", to_target(item)->release);
  280. }
  281. static ssize_t dev_name_show(struct config_item *item, char *buf)
  282. {
  283. return sysfs_emit(buf, "%s\n", to_target(item)->np.dev_name);
  284. }
  285. static ssize_t local_port_show(struct config_item *item, char *buf)
  286. {
  287. return sysfs_emit(buf, "%d\n", to_target(item)->np.local_port);
  288. }
  289. static ssize_t remote_port_show(struct config_item *item, char *buf)
  290. {
  291. return sysfs_emit(buf, "%d\n", to_target(item)->np.remote_port);
  292. }
  293. static ssize_t local_ip_show(struct config_item *item, char *buf)
  294. {
  295. struct netconsole_target *nt = to_target(item);
  296. if (nt->np.ipv6)
  297. return sysfs_emit(buf, "%pI6c\n", &nt->np.local_ip.in6);
  298. else
  299. return sysfs_emit(buf, "%pI4\n", &nt->np.local_ip);
  300. }
  301. static ssize_t remote_ip_show(struct config_item *item, char *buf)
  302. {
  303. struct netconsole_target *nt = to_target(item);
  304. if (nt->np.ipv6)
  305. return sysfs_emit(buf, "%pI6c\n", &nt->np.remote_ip.in6);
  306. else
  307. return sysfs_emit(buf, "%pI4\n", &nt->np.remote_ip);
  308. }
  309. static ssize_t local_mac_show(struct config_item *item, char *buf)
  310. {
  311. struct net_device *dev = to_target(item)->np.dev;
  312. static const u8 bcast[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
  313. return sysfs_emit(buf, "%pM\n", dev ? dev->dev_addr : bcast);
  314. }
  315. static ssize_t remote_mac_show(struct config_item *item, char *buf)
  316. {
  317. return sysfs_emit(buf, "%pM\n", to_target(item)->np.remote_mac);
  318. }
  319. /*
  320. * This one is special -- targets created through the configfs interface
  321. * are not enabled (and the corresponding netpoll activated) by default.
  322. * The user is expected to set the desired parameters first (which
  323. * would enable him to dynamically add new netpoll targets for new
  324. * network interfaces as and when they come up).
  325. */
  326. static ssize_t enabled_store(struct config_item *item,
  327. const char *buf, size_t count)
  328. {
  329. struct netconsole_target *nt = to_target(item);
  330. unsigned long flags;
  331. bool enabled;
  332. ssize_t ret;
  333. mutex_lock(&dynamic_netconsole_mutex);
  334. ret = kstrtobool(buf, &enabled);
  335. if (ret)
  336. goto out_unlock;
  337. ret = -EINVAL;
  338. if (enabled == nt->enabled) {
  339. pr_info("network logging has already %s\n",
  340. nt->enabled ? "started" : "stopped");
  341. goto out_unlock;
  342. }
  343. if (enabled) { /* true */
  344. if (nt->release && !nt->extended) {
  345. pr_err("Not enabling netconsole. Release feature requires extended log message");
  346. goto out_unlock;
  347. }
  348. if (nt->extended && !console_is_registered(&netconsole_ext))
  349. register_console(&netconsole_ext);
  350. /*
  351. * Skip netpoll_parse_options() -- all the attributes are
  352. * already configured via configfs. Just print them out.
  353. */
  354. netpoll_print_options(&nt->np);
  355. ret = netpoll_setup(&nt->np);
  356. if (ret)
  357. goto out_unlock;
  358. nt->enabled = true;
  359. pr_info("network logging started\n");
  360. } else { /* false */
  361. /* We need to disable the netconsole before cleaning it up
  362. * otherwise we might end up in write_msg() with
  363. * nt->np.dev == NULL and nt->enabled == true
  364. */
  365. mutex_lock(&target_cleanup_list_lock);
  366. spin_lock_irqsave(&target_list_lock, flags);
  367. nt->enabled = false;
  368. /* Remove the target from the list, while holding
  369. * target_list_lock
  370. */
  371. list_move(&nt->list, &target_cleanup_list);
  372. spin_unlock_irqrestore(&target_list_lock, flags);
  373. mutex_unlock(&target_cleanup_list_lock);
  374. }
  375. ret = strnlen(buf, count);
  376. /* Deferred cleanup */
  377. netconsole_process_cleanups();
  378. out_unlock:
  379. mutex_unlock(&dynamic_netconsole_mutex);
  380. return ret;
  381. }
  382. static ssize_t release_store(struct config_item *item, const char *buf,
  383. size_t count)
  384. {
  385. struct netconsole_target *nt = to_target(item);
  386. bool release;
  387. ssize_t ret;
  388. mutex_lock(&dynamic_netconsole_mutex);
  389. if (nt->enabled) {
  390. pr_err("target (%s) is enabled, disable to update parameters\n",
  391. config_item_name(&nt->group.cg_item));
  392. ret = -EINVAL;
  393. goto out_unlock;
  394. }
  395. ret = kstrtobool(buf, &release);
  396. if (ret)
  397. goto out_unlock;
  398. nt->release = release;
  399. ret = strnlen(buf, count);
  400. out_unlock:
  401. mutex_unlock(&dynamic_netconsole_mutex);
  402. return ret;
  403. }
  404. static ssize_t extended_store(struct config_item *item, const char *buf,
  405. size_t count)
  406. {
  407. struct netconsole_target *nt = to_target(item);
  408. bool extended;
  409. ssize_t ret;
  410. mutex_lock(&dynamic_netconsole_mutex);
  411. if (nt->enabled) {
  412. pr_err("target (%s) is enabled, disable to update parameters\n",
  413. config_item_name(&nt->group.cg_item));
  414. ret = -EINVAL;
  415. goto out_unlock;
  416. }
  417. ret = kstrtobool(buf, &extended);
  418. if (ret)
  419. goto out_unlock;
  420. nt->extended = extended;
  421. ret = strnlen(buf, count);
  422. out_unlock:
  423. mutex_unlock(&dynamic_netconsole_mutex);
  424. return ret;
  425. }
  426. static ssize_t dev_name_store(struct config_item *item, const char *buf,
  427. size_t count)
  428. {
  429. struct netconsole_target *nt = to_target(item);
  430. mutex_lock(&dynamic_netconsole_mutex);
  431. if (nt->enabled) {
  432. pr_err("target (%s) is enabled, disable to update parameters\n",
  433. config_item_name(&nt->group.cg_item));
  434. mutex_unlock(&dynamic_netconsole_mutex);
  435. return -EINVAL;
  436. }
  437. strscpy(nt->np.dev_name, buf, IFNAMSIZ);
  438. trim_newline(nt->np.dev_name, IFNAMSIZ);
  439. mutex_unlock(&dynamic_netconsole_mutex);
  440. return strnlen(buf, count);
  441. }
  442. static ssize_t local_port_store(struct config_item *item, const char *buf,
  443. size_t count)
  444. {
  445. struct netconsole_target *nt = to_target(item);
  446. ssize_t ret = -EINVAL;
  447. mutex_lock(&dynamic_netconsole_mutex);
  448. if (nt->enabled) {
  449. pr_err("target (%s) is enabled, disable to update parameters\n",
  450. config_item_name(&nt->group.cg_item));
  451. goto out_unlock;
  452. }
  453. ret = kstrtou16(buf, 10, &nt->np.local_port);
  454. if (ret < 0)
  455. goto out_unlock;
  456. ret = strnlen(buf, count);
  457. out_unlock:
  458. mutex_unlock(&dynamic_netconsole_mutex);
  459. return ret;
  460. }
  461. static ssize_t remote_port_store(struct config_item *item,
  462. const char *buf, size_t count)
  463. {
  464. struct netconsole_target *nt = to_target(item);
  465. ssize_t ret = -EINVAL;
  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->group.cg_item));
  470. goto out_unlock;
  471. }
  472. ret = kstrtou16(buf, 10, &nt->np.remote_port);
  473. if (ret < 0)
  474. goto out_unlock;
  475. ret = strnlen(buf, count);
  476. out_unlock:
  477. mutex_unlock(&dynamic_netconsole_mutex);
  478. return ret;
  479. }
  480. static ssize_t local_ip_store(struct config_item *item, const char *buf,
  481. size_t count)
  482. {
  483. struct netconsole_target *nt = to_target(item);
  484. ssize_t ret = -EINVAL;
  485. mutex_lock(&dynamic_netconsole_mutex);
  486. if (nt->enabled) {
  487. pr_err("target (%s) is enabled, disable to update parameters\n",
  488. config_item_name(&nt->group.cg_item));
  489. goto out_unlock;
  490. }
  491. if (strnchr(buf, count, ':')) {
  492. const char *end;
  493. if (in6_pton(buf, count, nt->np.local_ip.in6.s6_addr, -1, &end) > 0) {
  494. if (*end && *end != '\n') {
  495. pr_err("invalid IPv6 address at: <%c>\n", *end);
  496. goto out_unlock;
  497. }
  498. nt->np.ipv6 = true;
  499. } else
  500. goto out_unlock;
  501. } else {
  502. if (!nt->np.ipv6)
  503. nt->np.local_ip.ip = in_aton(buf);
  504. else
  505. goto out_unlock;
  506. }
  507. ret = strnlen(buf, count);
  508. out_unlock:
  509. mutex_unlock(&dynamic_netconsole_mutex);
  510. return ret;
  511. }
  512. static ssize_t remote_ip_store(struct config_item *item, const char *buf,
  513. size_t count)
  514. {
  515. struct netconsole_target *nt = to_target(item);
  516. ssize_t ret = -EINVAL;
  517. mutex_lock(&dynamic_netconsole_mutex);
  518. if (nt->enabled) {
  519. pr_err("target (%s) is enabled, disable to update parameters\n",
  520. config_item_name(&nt->group.cg_item));
  521. goto out_unlock;
  522. }
  523. if (strnchr(buf, count, ':')) {
  524. const char *end;
  525. if (in6_pton(buf, count, nt->np.remote_ip.in6.s6_addr, -1, &end) > 0) {
  526. if (*end && *end != '\n') {
  527. pr_err("invalid IPv6 address at: <%c>\n", *end);
  528. goto out_unlock;
  529. }
  530. nt->np.ipv6 = true;
  531. } else
  532. goto out_unlock;
  533. } else {
  534. if (!nt->np.ipv6)
  535. nt->np.remote_ip.ip = in_aton(buf);
  536. else
  537. goto out_unlock;
  538. }
  539. ret = strnlen(buf, count);
  540. out_unlock:
  541. mutex_unlock(&dynamic_netconsole_mutex);
  542. return ret;
  543. }
  544. static ssize_t remote_mac_store(struct config_item *item, const char *buf,
  545. size_t count)
  546. {
  547. struct netconsole_target *nt = to_target(item);
  548. u8 remote_mac[ETH_ALEN];
  549. ssize_t ret = -EINVAL;
  550. mutex_lock(&dynamic_netconsole_mutex);
  551. if (nt->enabled) {
  552. pr_err("target (%s) is enabled, disable to update parameters\n",
  553. config_item_name(&nt->group.cg_item));
  554. goto out_unlock;
  555. }
  556. if (!mac_pton(buf, remote_mac))
  557. goto out_unlock;
  558. if (buf[3 * ETH_ALEN - 1] && buf[3 * ETH_ALEN - 1] != '\n')
  559. goto out_unlock;
  560. memcpy(nt->np.remote_mac, remote_mac, ETH_ALEN);
  561. ret = strnlen(buf, count);
  562. out_unlock:
  563. mutex_unlock(&dynamic_netconsole_mutex);
  564. return ret;
  565. }
  566. struct userdatum {
  567. struct config_item item;
  568. char value[MAX_USERDATA_VALUE_LENGTH];
  569. };
  570. static struct userdatum *to_userdatum(struct config_item *item)
  571. {
  572. return container_of(item, struct userdatum, item);
  573. }
  574. struct userdata {
  575. struct config_group group;
  576. };
  577. static struct userdata *to_userdata(struct config_item *item)
  578. {
  579. return container_of(to_config_group(item), struct userdata, group);
  580. }
  581. static struct netconsole_target *userdata_to_target(struct userdata *ud)
  582. {
  583. struct config_group *netconsole_group;
  584. netconsole_group = to_config_group(ud->group.cg_item.ci_parent);
  585. return to_target(&netconsole_group->cg_item);
  586. }
  587. static ssize_t userdatum_value_show(struct config_item *item, char *buf)
  588. {
  589. return sysfs_emit(buf, "%s\n", &(to_userdatum(item)->value[0]));
  590. }
  591. static void update_userdata(struct netconsole_target *nt)
  592. {
  593. int complete_idx = 0, child_count = 0;
  594. struct list_head *entry;
  595. /* Clear the current string in case the last userdatum was deleted */
  596. nt->userdata_length = 0;
  597. nt->userdata_complete[0] = 0;
  598. list_for_each(entry, &nt->userdata_group.cg_children) {
  599. struct userdatum *udm_item;
  600. struct config_item *item;
  601. if (child_count >= MAX_USERDATA_ITEMS)
  602. break;
  603. child_count++;
  604. item = container_of(entry, struct config_item, ci_entry);
  605. udm_item = to_userdatum(item);
  606. /* Skip userdata with no value set */
  607. if (strnlen(udm_item->value, MAX_USERDATA_VALUE_LENGTH) == 0)
  608. continue;
  609. /* This doesn't overflow userdata_complete since it will write
  610. * one entry length (1/MAX_USERDATA_ITEMS long), entry count is
  611. * checked to not exceed MAX items with child_count above
  612. */
  613. complete_idx += scnprintf(&nt->userdata_complete[complete_idx],
  614. MAX_USERDATA_ENTRY_LENGTH, " %s=%s\n",
  615. item->ci_name, udm_item->value);
  616. }
  617. nt->userdata_length = strnlen(nt->userdata_complete,
  618. sizeof(nt->userdata_complete));
  619. }
  620. static ssize_t userdatum_value_store(struct config_item *item, const char *buf,
  621. size_t count)
  622. {
  623. struct userdatum *udm = to_userdatum(item);
  624. struct netconsole_target *nt;
  625. struct userdata *ud;
  626. ssize_t ret;
  627. if (count > MAX_USERDATA_VALUE_LENGTH)
  628. return -EMSGSIZE;
  629. mutex_lock(&dynamic_netconsole_mutex);
  630. ret = strscpy(udm->value, buf, sizeof(udm->value));
  631. if (ret < 0)
  632. goto out_unlock;
  633. trim_newline(udm->value, sizeof(udm->value));
  634. ud = to_userdata(item->ci_parent);
  635. nt = userdata_to_target(ud);
  636. update_userdata(nt);
  637. ret = count;
  638. out_unlock:
  639. mutex_unlock(&dynamic_netconsole_mutex);
  640. return ret;
  641. }
  642. CONFIGFS_ATTR(userdatum_, value);
  643. static struct configfs_attribute *userdatum_attrs[] = {
  644. &userdatum_attr_value,
  645. NULL,
  646. };
  647. static void userdatum_release(struct config_item *item)
  648. {
  649. kfree(to_userdatum(item));
  650. }
  651. static struct configfs_item_operations userdatum_ops = {
  652. .release = userdatum_release,
  653. };
  654. static const struct config_item_type userdatum_type = {
  655. .ct_item_ops = &userdatum_ops,
  656. .ct_attrs = userdatum_attrs,
  657. .ct_owner = THIS_MODULE,
  658. };
  659. static struct config_item *userdatum_make_item(struct config_group *group,
  660. const char *name)
  661. {
  662. struct netconsole_target *nt;
  663. struct userdatum *udm;
  664. struct userdata *ud;
  665. size_t child_count;
  666. if (strlen(name) > MAX_USERDATA_NAME_LENGTH)
  667. return ERR_PTR(-ENAMETOOLONG);
  668. ud = to_userdata(&group->cg_item);
  669. nt = userdata_to_target(ud);
  670. child_count = list_count_nodes(&nt->userdata_group.cg_children);
  671. if (child_count >= MAX_USERDATA_ITEMS)
  672. return ERR_PTR(-ENOSPC);
  673. udm = kzalloc(sizeof(*udm), GFP_KERNEL);
  674. if (!udm)
  675. return ERR_PTR(-ENOMEM);
  676. config_item_init_type_name(&udm->item, name, &userdatum_type);
  677. return &udm->item;
  678. }
  679. static void userdatum_drop(struct config_group *group, struct config_item *item)
  680. {
  681. struct netconsole_target *nt;
  682. struct userdata *ud;
  683. ud = to_userdata(&group->cg_item);
  684. nt = userdata_to_target(ud);
  685. mutex_lock(&dynamic_netconsole_mutex);
  686. update_userdata(nt);
  687. config_item_put(item);
  688. mutex_unlock(&dynamic_netconsole_mutex);
  689. }
  690. static struct configfs_attribute *userdata_attrs[] = {
  691. NULL,
  692. };
  693. static struct configfs_group_operations userdata_ops = {
  694. .make_item = userdatum_make_item,
  695. .drop_item = userdatum_drop,
  696. };
  697. static const struct config_item_type userdata_type = {
  698. .ct_item_ops = &userdatum_ops,
  699. .ct_group_ops = &userdata_ops,
  700. .ct_attrs = userdata_attrs,
  701. .ct_owner = THIS_MODULE,
  702. };
  703. CONFIGFS_ATTR(, enabled);
  704. CONFIGFS_ATTR(, extended);
  705. CONFIGFS_ATTR(, dev_name);
  706. CONFIGFS_ATTR(, local_port);
  707. CONFIGFS_ATTR(, remote_port);
  708. CONFIGFS_ATTR(, local_ip);
  709. CONFIGFS_ATTR(, remote_ip);
  710. CONFIGFS_ATTR_RO(, local_mac);
  711. CONFIGFS_ATTR(, remote_mac);
  712. CONFIGFS_ATTR(, release);
  713. static struct configfs_attribute *netconsole_target_attrs[] = {
  714. &attr_enabled,
  715. &attr_extended,
  716. &attr_release,
  717. &attr_dev_name,
  718. &attr_local_port,
  719. &attr_remote_port,
  720. &attr_local_ip,
  721. &attr_remote_ip,
  722. &attr_local_mac,
  723. &attr_remote_mac,
  724. NULL,
  725. };
  726. /*
  727. * Item operations and type for netconsole_target.
  728. */
  729. static void netconsole_target_release(struct config_item *item)
  730. {
  731. kfree(to_target(item));
  732. }
  733. static struct configfs_item_operations netconsole_target_item_ops = {
  734. .release = netconsole_target_release,
  735. };
  736. static const struct config_item_type netconsole_target_type = {
  737. .ct_attrs = netconsole_target_attrs,
  738. .ct_item_ops = &netconsole_target_item_ops,
  739. .ct_owner = THIS_MODULE,
  740. };
  741. static void init_target_config_group(struct netconsole_target *nt,
  742. const char *name)
  743. {
  744. config_group_init_type_name(&nt->group, name, &netconsole_target_type);
  745. config_group_init_type_name(&nt->userdata_group, "userdata",
  746. &userdata_type);
  747. configfs_add_default_group(&nt->userdata_group, &nt->group);
  748. }
  749. static struct netconsole_target *find_cmdline_target(const char *name)
  750. {
  751. struct netconsole_target *nt, *ret = NULL;
  752. unsigned long flags;
  753. spin_lock_irqsave(&target_list_lock, flags);
  754. list_for_each_entry(nt, &target_list, list) {
  755. if (!strcmp(nt->group.cg_item.ci_name, name)) {
  756. ret = nt;
  757. break;
  758. }
  759. }
  760. spin_unlock_irqrestore(&target_list_lock, flags);
  761. return ret;
  762. }
  763. /*
  764. * Group operations and type for netconsole_subsys.
  765. */
  766. static struct config_group *make_netconsole_target(struct config_group *group,
  767. const char *name)
  768. {
  769. struct netconsole_target *nt;
  770. unsigned long flags;
  771. /* Checking if a target by this name was created at boot time. If so,
  772. * attach a configfs entry to that target. This enables dynamic
  773. * control.
  774. */
  775. if (!strncmp(name, NETCONSOLE_PARAM_TARGET_PREFIX,
  776. strlen(NETCONSOLE_PARAM_TARGET_PREFIX))) {
  777. nt = find_cmdline_target(name);
  778. if (nt) {
  779. init_target_config_group(nt, name);
  780. return &nt->group;
  781. }
  782. }
  783. nt = alloc_and_init();
  784. if (!nt)
  785. return ERR_PTR(-ENOMEM);
  786. /* Initialize the config_group member */
  787. init_target_config_group(nt, name);
  788. /* Adding, but it is disabled */
  789. spin_lock_irqsave(&target_list_lock, flags);
  790. list_add(&nt->list, &target_list);
  791. spin_unlock_irqrestore(&target_list_lock, flags);
  792. return &nt->group;
  793. }
  794. static void drop_netconsole_target(struct config_group *group,
  795. struct config_item *item)
  796. {
  797. unsigned long flags;
  798. struct netconsole_target *nt = to_target(item);
  799. spin_lock_irqsave(&target_list_lock, flags);
  800. list_del(&nt->list);
  801. spin_unlock_irqrestore(&target_list_lock, flags);
  802. /*
  803. * The target may have never been enabled, or was manually disabled
  804. * before being removed so netpoll may have already been cleaned up.
  805. */
  806. if (nt->enabled)
  807. netpoll_cleanup(&nt->np);
  808. config_item_put(&nt->group.cg_item);
  809. }
  810. static struct configfs_group_operations netconsole_subsys_group_ops = {
  811. .make_group = make_netconsole_target,
  812. .drop_item = drop_netconsole_target,
  813. };
  814. static const struct config_item_type netconsole_subsys_type = {
  815. .ct_group_ops = &netconsole_subsys_group_ops,
  816. .ct_owner = THIS_MODULE,
  817. };
  818. /* The netconsole configfs subsystem */
  819. static struct configfs_subsystem netconsole_subsys = {
  820. .su_group = {
  821. .cg_item = {
  822. .ci_namebuf = "netconsole",
  823. .ci_type = &netconsole_subsys_type,
  824. },
  825. },
  826. };
  827. static void populate_configfs_item(struct netconsole_target *nt,
  828. int cmdline_count)
  829. {
  830. char target_name[16];
  831. snprintf(target_name, sizeof(target_name), "%s%d",
  832. NETCONSOLE_PARAM_TARGET_PREFIX, cmdline_count);
  833. init_target_config_group(nt, target_name);
  834. }
  835. #endif /* CONFIG_NETCONSOLE_DYNAMIC */
  836. /* Handle network interface device notifications */
  837. static int netconsole_netdev_event(struct notifier_block *this,
  838. unsigned long event, void *ptr)
  839. {
  840. unsigned long flags;
  841. struct netconsole_target *nt, *tmp;
  842. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  843. bool stopped = false;
  844. if (!(event == NETDEV_CHANGENAME || event == NETDEV_UNREGISTER ||
  845. event == NETDEV_RELEASE || event == NETDEV_JOIN))
  846. goto done;
  847. mutex_lock(&target_cleanup_list_lock);
  848. spin_lock_irqsave(&target_list_lock, flags);
  849. list_for_each_entry_safe(nt, tmp, &target_list, list) {
  850. netconsole_target_get(nt);
  851. if (nt->np.dev == dev) {
  852. switch (event) {
  853. case NETDEV_CHANGENAME:
  854. strscpy(nt->np.dev_name, dev->name, IFNAMSIZ);
  855. break;
  856. case NETDEV_RELEASE:
  857. case NETDEV_JOIN:
  858. case NETDEV_UNREGISTER:
  859. nt->enabled = false;
  860. list_move(&nt->list, &target_cleanup_list);
  861. stopped = true;
  862. }
  863. }
  864. netconsole_target_put(nt);
  865. }
  866. spin_unlock_irqrestore(&target_list_lock, flags);
  867. mutex_unlock(&target_cleanup_list_lock);
  868. if (stopped) {
  869. const char *msg = "had an event";
  870. switch (event) {
  871. case NETDEV_UNREGISTER:
  872. msg = "unregistered";
  873. break;
  874. case NETDEV_RELEASE:
  875. msg = "released slaves";
  876. break;
  877. case NETDEV_JOIN:
  878. msg = "is joining a master device";
  879. break;
  880. }
  881. pr_info("network logging stopped on interface %s as it %s\n",
  882. dev->name, msg);
  883. }
  884. /* Process target_cleanup_list entries. By the end, target_cleanup_list
  885. * should be empty
  886. */
  887. netconsole_process_cleanups_core();
  888. done:
  889. return NOTIFY_DONE;
  890. }
  891. static struct notifier_block netconsole_netdev_notifier = {
  892. .notifier_call = netconsole_netdev_event,
  893. };
  894. /**
  895. * send_ext_msg_udp - send extended log message to target
  896. * @nt: target to send message to
  897. * @msg: extended log message to send
  898. * @msg_len: length of message
  899. *
  900. * Transfer extended log @msg to @nt. If @msg is longer than
  901. * MAX_PRINT_CHUNK, it'll be split and transmitted in multiple chunks with
  902. * ncfrag header field added to identify them.
  903. */
  904. static void send_ext_msg_udp(struct netconsole_target *nt, const char *msg,
  905. int msg_len)
  906. {
  907. static char buf[MAX_PRINT_CHUNK]; /* protected by target_list_lock */
  908. const char *header, *body;
  909. int offset = 0;
  910. int header_len, body_len;
  911. const char *msg_ready = msg;
  912. const char *release;
  913. int release_len = 0;
  914. int userdata_len = 0;
  915. char *userdata = NULL;
  916. #ifdef CONFIG_NETCONSOLE_DYNAMIC
  917. userdata = nt->userdata_complete;
  918. userdata_len = nt->userdata_length;
  919. #endif
  920. if (nt->release) {
  921. release = init_utsname()->release;
  922. release_len = strlen(release) + 1;
  923. }
  924. if (msg_len + release_len + userdata_len <= MAX_PRINT_CHUNK) {
  925. /* No fragmentation needed */
  926. if (nt->release) {
  927. scnprintf(buf, MAX_PRINT_CHUNK, "%s,%s", release, msg);
  928. msg_len += release_len;
  929. } else {
  930. memcpy(buf, msg, msg_len);
  931. }
  932. if (userdata)
  933. msg_len += scnprintf(&buf[msg_len],
  934. MAX_PRINT_CHUNK - msg_len,
  935. "%s", userdata);
  936. msg_ready = buf;
  937. netpoll_send_udp(&nt->np, msg_ready, msg_len);
  938. return;
  939. }
  940. /* need to insert extra header fields, detect header and body */
  941. header = msg;
  942. body = memchr(msg, ';', msg_len);
  943. if (WARN_ON_ONCE(!body))
  944. return;
  945. header_len = body - header;
  946. body_len = msg_len - header_len - 1;
  947. body++;
  948. /*
  949. * Transfer multiple chunks with the following extra header.
  950. * "ncfrag=<byte-offset>/<total-bytes>"
  951. */
  952. if (nt->release)
  953. scnprintf(buf, MAX_PRINT_CHUNK, "%s,", release);
  954. memcpy(buf + release_len, header, header_len);
  955. header_len += release_len;
  956. while (offset < body_len + userdata_len) {
  957. int this_header = header_len;
  958. int this_offset = 0;
  959. int this_chunk = 0;
  960. this_header += scnprintf(buf + this_header,
  961. sizeof(buf) - this_header,
  962. ",ncfrag=%d/%d;", offset,
  963. body_len + userdata_len);
  964. /* Not all body data has been written yet */
  965. if (offset < body_len) {
  966. this_chunk = min(body_len - offset,
  967. MAX_PRINT_CHUNK - this_header);
  968. if (WARN_ON_ONCE(this_chunk <= 0))
  969. return;
  970. memcpy(buf + this_header, body + offset, this_chunk);
  971. this_offset += this_chunk;
  972. }
  973. /* Body is fully written and there is pending userdata to write,
  974. * append userdata in this chunk
  975. */
  976. if (offset + this_offset >= body_len &&
  977. offset + this_offset < userdata_len + body_len) {
  978. int sent_userdata = (offset + this_offset) - body_len;
  979. int preceding_bytes = this_chunk + this_header;
  980. if (WARN_ON_ONCE(sent_userdata < 0))
  981. return;
  982. this_chunk = min(userdata_len - sent_userdata,
  983. MAX_PRINT_CHUNK - preceding_bytes);
  984. if (WARN_ON_ONCE(this_chunk < 0))
  985. /* this_chunk could be zero if all the previous
  986. * message used all the buffer. This is not a
  987. * problem, userdata will be sent in the next
  988. * iteration
  989. */
  990. return;
  991. memcpy(buf + this_header + this_offset,
  992. userdata + sent_userdata,
  993. this_chunk);
  994. this_offset += this_chunk;
  995. }
  996. netpoll_send_udp(&nt->np, buf, this_header + this_offset);
  997. offset += this_offset;
  998. }
  999. }
  1000. static void write_ext_msg(struct console *con, const char *msg,
  1001. unsigned int len)
  1002. {
  1003. struct netconsole_target *nt;
  1004. unsigned long flags;
  1005. if ((oops_only && !oops_in_progress) || list_empty(&target_list))
  1006. return;
  1007. spin_lock_irqsave(&target_list_lock, flags);
  1008. list_for_each_entry(nt, &target_list, list)
  1009. if (nt->extended && nt->enabled && netif_running(nt->np.dev))
  1010. send_ext_msg_udp(nt, msg, len);
  1011. spin_unlock_irqrestore(&target_list_lock, flags);
  1012. }
  1013. static void write_msg(struct console *con, const char *msg, unsigned int len)
  1014. {
  1015. int frag, left;
  1016. unsigned long flags;
  1017. struct netconsole_target *nt;
  1018. const char *tmp;
  1019. if (oops_only && !oops_in_progress)
  1020. return;
  1021. /* Avoid taking lock and disabling interrupts unnecessarily */
  1022. if (list_empty(&target_list))
  1023. return;
  1024. spin_lock_irqsave(&target_list_lock, flags);
  1025. list_for_each_entry(nt, &target_list, list) {
  1026. if (!nt->extended && nt->enabled && netif_running(nt->np.dev)) {
  1027. /*
  1028. * We nest this inside the for-each-target loop above
  1029. * so that we're able to get as much logging out to
  1030. * at least one target if we die inside here, instead
  1031. * of unnecessarily keeping all targets in lock-step.
  1032. */
  1033. tmp = msg;
  1034. for (left = len; left;) {
  1035. frag = min(left, MAX_PRINT_CHUNK);
  1036. netpoll_send_udp(&nt->np, tmp, frag);
  1037. tmp += frag;
  1038. left -= frag;
  1039. }
  1040. }
  1041. }
  1042. spin_unlock_irqrestore(&target_list_lock, flags);
  1043. }
  1044. /* Allocate new target (from boot/module param) and setup netpoll for it */
  1045. static struct netconsole_target *alloc_param_target(char *target_config,
  1046. int cmdline_count)
  1047. {
  1048. struct netconsole_target *nt;
  1049. int err;
  1050. nt = alloc_and_init();
  1051. if (!nt) {
  1052. err = -ENOMEM;
  1053. goto fail;
  1054. }
  1055. if (*target_config == '+') {
  1056. nt->extended = true;
  1057. target_config++;
  1058. }
  1059. if (*target_config == 'r') {
  1060. if (!nt->extended) {
  1061. pr_err("Netconsole configuration error. Release feature requires extended log message");
  1062. err = -EINVAL;
  1063. goto fail;
  1064. }
  1065. nt->release = true;
  1066. target_config++;
  1067. }
  1068. /* Parse parameters and setup netpoll */
  1069. err = netpoll_parse_options(&nt->np, target_config);
  1070. if (err)
  1071. goto fail;
  1072. err = netpoll_setup(&nt->np);
  1073. if (err) {
  1074. pr_err("Not enabling netconsole for %s%d. Netpoll setup failed\n",
  1075. NETCONSOLE_PARAM_TARGET_PREFIX, cmdline_count);
  1076. if (!IS_ENABLED(CONFIG_NETCONSOLE_DYNAMIC))
  1077. /* only fail if dynamic reconfiguration is set,
  1078. * otherwise, keep the target in the list, but disabled.
  1079. */
  1080. goto fail;
  1081. } else {
  1082. nt->enabled = true;
  1083. }
  1084. populate_configfs_item(nt, cmdline_count);
  1085. return nt;
  1086. fail:
  1087. kfree(nt);
  1088. return ERR_PTR(err);
  1089. }
  1090. /* Cleanup netpoll for given target (from boot/module param) and free it */
  1091. static void free_param_target(struct netconsole_target *nt)
  1092. {
  1093. netpoll_cleanup(&nt->np);
  1094. kfree(nt);
  1095. }
  1096. static struct console netconsole_ext = {
  1097. .name = "netcon_ext",
  1098. .flags = CON_ENABLED | CON_EXTENDED,
  1099. .write = write_ext_msg,
  1100. };
  1101. static struct console netconsole = {
  1102. .name = "netcon",
  1103. .flags = CON_ENABLED,
  1104. .write = write_msg,
  1105. };
  1106. static int __init init_netconsole(void)
  1107. {
  1108. int err;
  1109. struct netconsole_target *nt, *tmp;
  1110. unsigned int count = 0;
  1111. bool extended = false;
  1112. unsigned long flags;
  1113. char *target_config;
  1114. char *input = config;
  1115. if (strnlen(input, MAX_PARAM_LENGTH)) {
  1116. while ((target_config = strsep(&input, ";"))) {
  1117. nt = alloc_param_target(target_config, count);
  1118. if (IS_ERR(nt)) {
  1119. if (IS_ENABLED(CONFIG_NETCONSOLE_DYNAMIC))
  1120. continue;
  1121. err = PTR_ERR(nt);
  1122. goto fail;
  1123. }
  1124. /* Dump existing printks when we register */
  1125. if (nt->extended) {
  1126. extended = true;
  1127. netconsole_ext.flags |= CON_PRINTBUFFER;
  1128. } else {
  1129. netconsole.flags |= CON_PRINTBUFFER;
  1130. }
  1131. spin_lock_irqsave(&target_list_lock, flags);
  1132. list_add(&nt->list, &target_list);
  1133. spin_unlock_irqrestore(&target_list_lock, flags);
  1134. count++;
  1135. }
  1136. }
  1137. err = register_netdevice_notifier(&netconsole_netdev_notifier);
  1138. if (err)
  1139. goto fail;
  1140. err = dynamic_netconsole_init();
  1141. if (err)
  1142. goto undonotifier;
  1143. if (extended)
  1144. register_console(&netconsole_ext);
  1145. register_console(&netconsole);
  1146. pr_info("network logging started\n");
  1147. return err;
  1148. undonotifier:
  1149. unregister_netdevice_notifier(&netconsole_netdev_notifier);
  1150. fail:
  1151. pr_err("cleaning up\n");
  1152. /*
  1153. * Remove all targets and destroy them (only targets created
  1154. * from the boot/module option exist here). Skipping the list
  1155. * lock is safe here, and netpoll_cleanup() will sleep.
  1156. */
  1157. list_for_each_entry_safe(nt, tmp, &target_list, list) {
  1158. list_del(&nt->list);
  1159. free_param_target(nt);
  1160. }
  1161. return err;
  1162. }
  1163. static void __exit cleanup_netconsole(void)
  1164. {
  1165. struct netconsole_target *nt, *tmp;
  1166. if (console_is_registered(&netconsole_ext))
  1167. unregister_console(&netconsole_ext);
  1168. unregister_console(&netconsole);
  1169. dynamic_netconsole_exit();
  1170. unregister_netdevice_notifier(&netconsole_netdev_notifier);
  1171. /*
  1172. * Targets created via configfs pin references on our module
  1173. * and would first be rmdir(2)'ed from userspace. We reach
  1174. * here only when they are already destroyed, and only those
  1175. * created from the boot/module option are left, so remove and
  1176. * destroy them. Skipping the list lock is safe here, and
  1177. * netpoll_cleanup() will sleep.
  1178. */
  1179. list_for_each_entry_safe(nt, tmp, &target_list, list) {
  1180. list_del(&nt->list);
  1181. free_param_target(nt);
  1182. }
  1183. }
  1184. /*
  1185. * Use late_initcall to ensure netconsole is
  1186. * initialized after network device driver if built-in.
  1187. *
  1188. * late_initcall() and module_init() are identical if built as module.
  1189. */
  1190. late_initcall(init_netconsole);
  1191. module_exit(cleanup_netconsole);