netconsole.c 21 KB

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