netconsole.c 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238
  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/hex.h>
  37. #include <linux/u64_stats_sync.h>
  38. #include <linux/utsname.h>
  39. #include <linux/rtnetlink.h>
  40. #include <linux/workqueue.h>
  41. MODULE_AUTHOR("Matt Mackall <mpm@selenic.com>");
  42. MODULE_DESCRIPTION("Console driver for network interfaces");
  43. MODULE_LICENSE("GPL");
  44. #define MAX_PARAM_LENGTH 256
  45. #define MAX_EXTRADATA_ENTRY_LEN 256
  46. #define MAX_EXTRADATA_VALUE_LEN 200
  47. /* The number 3 comes from userdata entry format characters (' ', '=', '\n') */
  48. #define MAX_EXTRADATA_NAME_LEN (MAX_EXTRADATA_ENTRY_LEN - \
  49. MAX_EXTRADATA_VALUE_LEN - 3)
  50. #define MAX_USERDATA_ITEMS 256
  51. #define MAX_PRINT_CHUNK 1000
  52. static char config[MAX_PARAM_LENGTH];
  53. module_param_string(netconsole, config, MAX_PARAM_LENGTH, 0);
  54. MODULE_PARM_DESC(netconsole, " netconsole=[src-port]@[src-ip]/[dev],[tgt-port]@<tgt-ip>/[tgt-macaddr]");
  55. static bool oops_only;
  56. module_param(oops_only, bool, 0600);
  57. MODULE_PARM_DESC(oops_only, "Only log oops messages");
  58. #define NETCONSOLE_PARAM_TARGET_PREFIX "cmdline"
  59. #ifndef MODULE
  60. static int __init option_setup(char *opt)
  61. {
  62. strscpy(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. /* target_cleanup_list is used to track targets that need to be cleaned outside
  70. * of target_list_lock. It should be cleaned in the same function it is
  71. * populated.
  72. */
  73. static LIST_HEAD(target_cleanup_list);
  74. /* This needs to be a spinlock because write_msg() cannot sleep */
  75. static DEFINE_SPINLOCK(target_list_lock);
  76. /* This needs to be a mutex because netpoll_cleanup might sleep */
  77. static DEFINE_MUTEX(target_cleanup_list_lock);
  78. static struct workqueue_struct *netconsole_wq;
  79. /*
  80. * Console driver for netconsoles. Register only consoles that have
  81. * an associated target of the same type.
  82. */
  83. static struct console netconsole_ext, netconsole;
  84. struct netconsole_target_stats {
  85. u64_stats_t xmit_drop_count;
  86. u64_stats_t enomem_count;
  87. struct u64_stats_sync syncp;
  88. };
  89. enum console_type {
  90. CONS_BASIC = BIT(0),
  91. CONS_EXTENDED = BIT(1),
  92. };
  93. /* Features enabled in sysdata. Contrary to userdata, this data is populated by
  94. * the kernel. The fields are designed as bitwise flags, allowing multiple
  95. * features to be set in sysdata_fields.
  96. */
  97. enum sysdata_feature {
  98. /* Populate the CPU that sends the message */
  99. SYSDATA_CPU_NR = BIT(0),
  100. /* Populate the task name (as in current->comm) in sysdata */
  101. SYSDATA_TASKNAME = BIT(1),
  102. /* Kernel release/version as part of sysdata */
  103. SYSDATA_RELEASE = BIT(2),
  104. /* Include a per-target message ID as part of sysdata */
  105. SYSDATA_MSGID = BIT(3),
  106. /* Sentinel: highest bit position */
  107. MAX_SYSDATA_ITEMS = 4,
  108. };
  109. enum target_state {
  110. STATE_DISABLED,
  111. STATE_ENABLED,
  112. STATE_DEACTIVATED,
  113. };
  114. /**
  115. * struct netconsole_target - Represents a configured netconsole target.
  116. * @list: Links this target into the target_list.
  117. * @group: Links us into the configfs subsystem hierarchy.
  118. * @userdata_group: Links to the userdata configfs hierarchy
  119. * @userdata: Cached, formatted string of append
  120. * @userdata_length: String length of userdata.
  121. * @sysdata: Cached, formatted string of append
  122. * @sysdata_fields: Sysdata features enabled.
  123. * @msgcounter: Message sent counter.
  124. * @stats: Packet send stats for the target. Used for debugging.
  125. * @state: State of the target.
  126. * Visible from userspace (read-write).
  127. * From a userspace perspective, the target is either enabled or
  128. * disabled. Internally, although both STATE_DISABLED and
  129. * STATE_DEACTIVATED correspond to inactive targets, the latter is
  130. * due to automatic interface state changes and will try
  131. * recover automatically, if the interface comes back
  132. * online.
  133. * Also, other parameters of a target may be modified at
  134. * runtime only when it is disabled (state != STATE_ENABLED).
  135. * @extended: Denotes whether console is extended or not.
  136. * @release: Denotes whether kernel release version should be prepended
  137. * to the message. Depends on extended console.
  138. * @np: The netpoll structure for this target.
  139. * Contains the other userspace visible parameters:
  140. * dev_name (read-write)
  141. * local_port (read-write)
  142. * remote_port (read-write)
  143. * local_ip (read-write)
  144. * remote_ip (read-write)
  145. * local_mac (read-only)
  146. * remote_mac (read-write)
  147. * @buf: The buffer used to send the full msg to the network stack
  148. * @resume_wq: Workqueue to resume deactivated target
  149. */
  150. struct netconsole_target {
  151. struct list_head list;
  152. #ifdef CONFIG_NETCONSOLE_DYNAMIC
  153. struct config_group group;
  154. struct config_group userdata_group;
  155. char *userdata;
  156. size_t userdata_length;
  157. char sysdata[MAX_EXTRADATA_ENTRY_LEN * MAX_SYSDATA_ITEMS];
  158. /* bit-wise with sysdata_feature bits */
  159. u32 sysdata_fields;
  160. /* protected by target_list_lock */
  161. u32 msgcounter;
  162. #endif
  163. struct netconsole_target_stats stats;
  164. enum target_state state;
  165. bool extended;
  166. bool release;
  167. struct netpoll np;
  168. /* protected by target_list_lock */
  169. char buf[MAX_PRINT_CHUNK];
  170. struct work_struct resume_wq;
  171. };
  172. #ifdef CONFIG_NETCONSOLE_DYNAMIC
  173. static struct configfs_subsystem netconsole_subsys;
  174. static DEFINE_MUTEX(dynamic_netconsole_mutex);
  175. static int __init dynamic_netconsole_init(void)
  176. {
  177. config_group_init(&netconsole_subsys.su_group);
  178. mutex_init(&netconsole_subsys.su_mutex);
  179. return configfs_register_subsystem(&netconsole_subsys);
  180. }
  181. static void __exit dynamic_netconsole_exit(void)
  182. {
  183. configfs_unregister_subsystem(&netconsole_subsys);
  184. }
  185. /*
  186. * Targets that were created by parsing the boot/module option string
  187. * do not exist in the configfs hierarchy (and have NULL names) and will
  188. * never go away, so make these a no-op for them.
  189. */
  190. static void netconsole_target_get(struct netconsole_target *nt)
  191. {
  192. if (config_item_name(&nt->group.cg_item))
  193. config_group_get(&nt->group);
  194. }
  195. static void netconsole_target_put(struct netconsole_target *nt)
  196. {
  197. if (config_item_name(&nt->group.cg_item))
  198. config_group_put(&nt->group);
  199. }
  200. static void dynamic_netconsole_mutex_lock(void)
  201. {
  202. mutex_lock(&dynamic_netconsole_mutex);
  203. }
  204. static void dynamic_netconsole_mutex_unlock(void)
  205. {
  206. mutex_unlock(&dynamic_netconsole_mutex);
  207. }
  208. #else /* !CONFIG_NETCONSOLE_DYNAMIC */
  209. static int __init dynamic_netconsole_init(void)
  210. {
  211. return 0;
  212. }
  213. static void __exit dynamic_netconsole_exit(void)
  214. {
  215. }
  216. /*
  217. * No danger of targets going away from under us when dynamic
  218. * reconfigurability is off.
  219. */
  220. static void netconsole_target_get(struct netconsole_target *nt)
  221. {
  222. }
  223. static void netconsole_target_put(struct netconsole_target *nt)
  224. {
  225. }
  226. static void populate_configfs_item(struct netconsole_target *nt,
  227. int cmdline_count)
  228. {
  229. }
  230. static void dynamic_netconsole_mutex_lock(void)
  231. {
  232. }
  233. static void dynamic_netconsole_mutex_unlock(void)
  234. {
  235. }
  236. #endif /* CONFIG_NETCONSOLE_DYNAMIC */
  237. /* Check if the target was bound by mac address. */
  238. static bool bound_by_mac(struct netconsole_target *nt)
  239. {
  240. return is_valid_ether_addr(nt->np.dev_mac);
  241. }
  242. /* Attempts to resume logging to a deactivated target. */
  243. static void resume_target(struct netconsole_target *nt)
  244. {
  245. if (netpoll_setup(&nt->np)) {
  246. /* netpoll fails setup once, do not try again. */
  247. nt->state = STATE_DISABLED;
  248. return;
  249. }
  250. nt->state = STATE_ENABLED;
  251. pr_info("network logging resumed on interface %s\n", nt->np.dev_name);
  252. }
  253. /* Checks if a deactivated target matches a device. */
  254. static bool deactivated_target_match(struct netconsole_target *nt,
  255. struct net_device *ndev)
  256. {
  257. if (nt->state != STATE_DEACTIVATED)
  258. return false;
  259. if (bound_by_mac(nt))
  260. return !memcmp(nt->np.dev_mac, ndev->dev_addr, ETH_ALEN);
  261. return !strncmp(nt->np.dev_name, ndev->name, IFNAMSIZ);
  262. }
  263. /* Process work scheduled for target resume. */
  264. static void process_resume_target(struct work_struct *work)
  265. {
  266. struct netconsole_target *nt;
  267. unsigned long flags;
  268. nt = container_of(work, struct netconsole_target, resume_wq);
  269. dynamic_netconsole_mutex_lock();
  270. spin_lock_irqsave(&target_list_lock, flags);
  271. /* Check if target is still deactivated as it may have been disabled
  272. * while resume was being scheduled.
  273. */
  274. if (nt->state != STATE_DEACTIVATED) {
  275. spin_unlock_irqrestore(&target_list_lock, flags);
  276. goto out_unlock;
  277. }
  278. /* resume_target is IRQ unsafe, remove target from
  279. * target_list in order to resume it with IRQ enabled.
  280. */
  281. list_del_init(&nt->list);
  282. spin_unlock_irqrestore(&target_list_lock, flags);
  283. resume_target(nt);
  284. /* At this point the target is either enabled or disabled and
  285. * was cleaned up before getting deactivated. Either way, add it
  286. * back to target list.
  287. */
  288. spin_lock_irqsave(&target_list_lock, flags);
  289. list_add(&nt->list, &target_list);
  290. spin_unlock_irqrestore(&target_list_lock, flags);
  291. out_unlock:
  292. dynamic_netconsole_mutex_unlock();
  293. }
  294. /* Allocate and initialize with defaults.
  295. * Note that these targets get their config_item fields zeroed-out.
  296. */
  297. static struct netconsole_target *alloc_and_init(void)
  298. {
  299. struct netconsole_target *nt;
  300. nt = kzalloc_obj(*nt);
  301. if (!nt)
  302. return nt;
  303. if (IS_ENABLED(CONFIG_NETCONSOLE_EXTENDED_LOG))
  304. nt->extended = true;
  305. if (IS_ENABLED(CONFIG_NETCONSOLE_PREPEND_RELEASE))
  306. nt->release = true;
  307. nt->np.name = "netconsole";
  308. strscpy(nt->np.dev_name, "eth0", IFNAMSIZ);
  309. nt->np.local_port = 6665;
  310. nt->np.remote_port = 6666;
  311. eth_broadcast_addr(nt->np.remote_mac);
  312. nt->state = STATE_DISABLED;
  313. INIT_WORK(&nt->resume_wq, process_resume_target);
  314. return nt;
  315. }
  316. /* Clean up every target in the cleanup_list and move the clean targets back to
  317. * the main target_list.
  318. */
  319. static void netconsole_process_cleanups_core(void)
  320. {
  321. struct netconsole_target *nt, *tmp;
  322. unsigned long flags;
  323. /* The cleanup needs RTNL locked */
  324. ASSERT_RTNL();
  325. mutex_lock(&target_cleanup_list_lock);
  326. list_for_each_entry_safe(nt, tmp, &target_cleanup_list, list) {
  327. /* all entries in the cleanup_list needs to be disabled */
  328. WARN_ON_ONCE(nt->state == STATE_ENABLED);
  329. do_netpoll_cleanup(&nt->np);
  330. if (bound_by_mac(nt))
  331. memset(&nt->np.dev_name, 0, IFNAMSIZ);
  332. /* moved the cleaned target to target_list. Need to hold both
  333. * locks
  334. */
  335. spin_lock_irqsave(&target_list_lock, flags);
  336. list_move(&nt->list, &target_list);
  337. spin_unlock_irqrestore(&target_list_lock, flags);
  338. }
  339. WARN_ON_ONCE(!list_empty(&target_cleanup_list));
  340. mutex_unlock(&target_cleanup_list_lock);
  341. }
  342. static void netconsole_print_banner(struct netpoll *np)
  343. {
  344. np_info(np, "local port %d\n", np->local_port);
  345. if (np->ipv6)
  346. np_info(np, "local IPv6 address %pI6c\n", &np->local_ip.in6);
  347. else
  348. np_info(np, "local IPv4 address %pI4\n", &np->local_ip.ip);
  349. np_info(np, "interface name '%s'\n", np->dev_name);
  350. np_info(np, "local ethernet address '%pM'\n", np->dev_mac);
  351. np_info(np, "remote port %d\n", np->remote_port);
  352. if (np->ipv6)
  353. np_info(np, "remote IPv6 address %pI6c\n", &np->remote_ip.in6);
  354. else
  355. np_info(np, "remote IPv4 address %pI4\n", &np->remote_ip.ip);
  356. np_info(np, "remote ethernet address %pM\n", np->remote_mac);
  357. }
  358. /* Parse the string and populate the `inet_addr` union. Return 0 if IPv4 is
  359. * populated, 1 if IPv6 is populated, and -1 upon failure.
  360. */
  361. static int netpoll_parse_ip_addr(const char *str, union inet_addr *addr)
  362. {
  363. const char *end = NULL;
  364. int len;
  365. len = strlen(str);
  366. if (!len)
  367. return -1;
  368. if (str[len - 1] == '\n')
  369. len -= 1;
  370. if (in4_pton(str, len, (void *)addr, -1, &end) > 0 &&
  371. (!end || *end == 0 || *end == '\n'))
  372. return 0;
  373. if (IS_ENABLED(CONFIG_IPV6) &&
  374. in6_pton(str, len, (void *)addr, -1, &end) > 0 &&
  375. (!end || *end == 0 || *end == '\n'))
  376. return 1;
  377. return -1;
  378. }
  379. #ifdef CONFIG_NETCONSOLE_DYNAMIC
  380. /*
  381. * Our subsystem hierarchy is:
  382. *
  383. * /sys/kernel/config/netconsole/
  384. * |
  385. * <target>/
  386. * | enabled
  387. * | release
  388. * | dev_name
  389. * | local_port
  390. * | remote_port
  391. * | local_ip
  392. * | remote_ip
  393. * | local_mac
  394. * | remote_mac
  395. * | transmit_errors
  396. * | userdata/
  397. * | <key>/
  398. * | value
  399. * | ...
  400. * |
  401. * <target>/...
  402. */
  403. static struct netconsole_target *to_target(struct config_item *item)
  404. {
  405. struct config_group *cfg_group;
  406. cfg_group = to_config_group(item);
  407. if (!cfg_group)
  408. return NULL;
  409. return container_of(to_config_group(item),
  410. struct netconsole_target, group);
  411. }
  412. /* Do the list cleanup with the rtnl lock hold. rtnl lock is necessary because
  413. * netdev might be cleaned-up by calling __netpoll_cleanup(),
  414. */
  415. static void netconsole_process_cleanups(void)
  416. {
  417. /* rtnl lock is called here, because it has precedence over
  418. * target_cleanup_list_lock mutex and target_cleanup_list
  419. */
  420. rtnl_lock();
  421. netconsole_process_cleanups_core();
  422. rtnl_unlock();
  423. }
  424. /* Get rid of possible trailing newline, returning the new length */
  425. static void trim_newline(char *s, size_t maxlen)
  426. {
  427. size_t len;
  428. len = strnlen(s, maxlen);
  429. if (s[len - 1] == '\n')
  430. s[len - 1] = '\0';
  431. }
  432. /*
  433. * Attribute operations for netconsole_target.
  434. */
  435. static ssize_t enabled_show(struct config_item *item, char *buf)
  436. {
  437. return sysfs_emit(buf, "%d\n", to_target(item)->state == STATE_ENABLED);
  438. }
  439. static ssize_t extended_show(struct config_item *item, char *buf)
  440. {
  441. return sysfs_emit(buf, "%d\n", to_target(item)->extended);
  442. }
  443. static ssize_t release_show(struct config_item *item, char *buf)
  444. {
  445. return sysfs_emit(buf, "%d\n", to_target(item)->release);
  446. }
  447. static ssize_t dev_name_show(struct config_item *item, char *buf)
  448. {
  449. return sysfs_emit(buf, "%s\n", to_target(item)->np.dev_name);
  450. }
  451. static ssize_t local_port_show(struct config_item *item, char *buf)
  452. {
  453. return sysfs_emit(buf, "%d\n", to_target(item)->np.local_port);
  454. }
  455. static ssize_t remote_port_show(struct config_item *item, char *buf)
  456. {
  457. return sysfs_emit(buf, "%d\n", to_target(item)->np.remote_port);
  458. }
  459. static ssize_t local_ip_show(struct config_item *item, char *buf)
  460. {
  461. struct netconsole_target *nt = to_target(item);
  462. if (nt->np.ipv6)
  463. return sysfs_emit(buf, "%pI6c\n", &nt->np.local_ip.in6);
  464. else
  465. return sysfs_emit(buf, "%pI4\n", &nt->np.local_ip);
  466. }
  467. static ssize_t remote_ip_show(struct config_item *item, char *buf)
  468. {
  469. struct netconsole_target *nt = to_target(item);
  470. if (nt->np.ipv6)
  471. return sysfs_emit(buf, "%pI6c\n", &nt->np.remote_ip.in6);
  472. else
  473. return sysfs_emit(buf, "%pI4\n", &nt->np.remote_ip);
  474. }
  475. static ssize_t local_mac_show(struct config_item *item, char *buf)
  476. {
  477. struct net_device *dev = to_target(item)->np.dev;
  478. static const u8 bcast[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
  479. return sysfs_emit(buf, "%pM\n", dev ? dev->dev_addr : bcast);
  480. }
  481. static ssize_t remote_mac_show(struct config_item *item, char *buf)
  482. {
  483. return sysfs_emit(buf, "%pM\n", to_target(item)->np.remote_mac);
  484. }
  485. static ssize_t transmit_errors_show(struct config_item *item, char *buf)
  486. {
  487. struct netconsole_target *nt = to_target(item);
  488. u64 xmit_drop_count, enomem_count;
  489. unsigned int start;
  490. do {
  491. start = u64_stats_fetch_begin(&nt->stats.syncp);
  492. xmit_drop_count = u64_stats_read(&nt->stats.xmit_drop_count);
  493. enomem_count = u64_stats_read(&nt->stats.enomem_count);
  494. } while (u64_stats_fetch_retry(&nt->stats.syncp, start));
  495. return sysfs_emit(buf, "%llu\n", xmit_drop_count + enomem_count);
  496. }
  497. /* configfs helper to display if cpu_nr sysdata feature is enabled */
  498. static ssize_t sysdata_cpu_nr_enabled_show(struct config_item *item, char *buf)
  499. {
  500. struct netconsole_target *nt = to_target(item->ci_parent);
  501. bool cpu_nr_enabled;
  502. dynamic_netconsole_mutex_lock();
  503. cpu_nr_enabled = !!(nt->sysdata_fields & SYSDATA_CPU_NR);
  504. dynamic_netconsole_mutex_unlock();
  505. return sysfs_emit(buf, "%d\n", cpu_nr_enabled);
  506. }
  507. /* configfs helper to display if taskname sysdata feature is enabled */
  508. static ssize_t sysdata_taskname_enabled_show(struct config_item *item,
  509. char *buf)
  510. {
  511. struct netconsole_target *nt = to_target(item->ci_parent);
  512. bool taskname_enabled;
  513. dynamic_netconsole_mutex_lock();
  514. taskname_enabled = !!(nt->sysdata_fields & SYSDATA_TASKNAME);
  515. dynamic_netconsole_mutex_unlock();
  516. return sysfs_emit(buf, "%d\n", taskname_enabled);
  517. }
  518. static ssize_t sysdata_release_enabled_show(struct config_item *item,
  519. char *buf)
  520. {
  521. struct netconsole_target *nt = to_target(item->ci_parent);
  522. bool release_enabled;
  523. dynamic_netconsole_mutex_lock();
  524. release_enabled = !!(nt->sysdata_fields & SYSDATA_RELEASE);
  525. dynamic_netconsole_mutex_unlock();
  526. return sysfs_emit(buf, "%d\n", release_enabled);
  527. }
  528. /* Iterate in the list of target, and make sure we don't have any console
  529. * register without targets of the same type
  530. */
  531. static void unregister_netcons_consoles(void)
  532. {
  533. struct netconsole_target *nt;
  534. u32 console_type_needed = 0;
  535. unsigned long flags;
  536. spin_lock_irqsave(&target_list_lock, flags);
  537. list_for_each_entry(nt, &target_list, list) {
  538. if (nt->extended)
  539. console_type_needed |= CONS_EXTENDED;
  540. else
  541. console_type_needed |= CONS_BASIC;
  542. }
  543. spin_unlock_irqrestore(&target_list_lock, flags);
  544. if (!(console_type_needed & CONS_EXTENDED) &&
  545. console_is_registered(&netconsole_ext))
  546. unregister_console(&netconsole_ext);
  547. if (!(console_type_needed & CONS_BASIC) &&
  548. console_is_registered(&netconsole))
  549. unregister_console(&netconsole);
  550. }
  551. static ssize_t sysdata_msgid_enabled_show(struct config_item *item,
  552. char *buf)
  553. {
  554. struct netconsole_target *nt = to_target(item->ci_parent);
  555. bool msgid_enabled;
  556. dynamic_netconsole_mutex_lock();
  557. msgid_enabled = !!(nt->sysdata_fields & SYSDATA_MSGID);
  558. dynamic_netconsole_mutex_unlock();
  559. return sysfs_emit(buf, "%d\n", msgid_enabled);
  560. }
  561. /*
  562. * This one is special -- targets created through the configfs interface
  563. * are not enabled (and the corresponding netpoll activated) by default.
  564. * The user is expected to set the desired parameters first (which
  565. * would enable him to dynamically add new netpoll targets for new
  566. * network interfaces as and when they come up).
  567. */
  568. static ssize_t enabled_store(struct config_item *item,
  569. const char *buf, size_t count)
  570. {
  571. struct netconsole_target *nt = to_target(item);
  572. bool enabled, current_enabled;
  573. unsigned long flags;
  574. ssize_t ret;
  575. dynamic_netconsole_mutex_lock();
  576. ret = kstrtobool(buf, &enabled);
  577. if (ret)
  578. goto out_unlock;
  579. /* When the user explicitly enables or disables a target that is
  580. * currently deactivated, reset its state to disabled. The DEACTIVATED
  581. * state only tracks interface-driven deactivation and should _not_
  582. * persist when the user manually changes the target's enabled state.
  583. */
  584. if (nt->state == STATE_DEACTIVATED)
  585. nt->state = STATE_DISABLED;
  586. ret = -EINVAL;
  587. current_enabled = nt->state == STATE_ENABLED;
  588. if (enabled == current_enabled) {
  589. pr_info("network logging has already %s\n",
  590. current_enabled ? "started" : "stopped");
  591. goto out_unlock;
  592. }
  593. if (enabled) { /* true */
  594. if (nt->release && !nt->extended) {
  595. pr_err("Not enabling netconsole. Release feature requires extended log message");
  596. goto out_unlock;
  597. }
  598. if (nt->extended && !console_is_registered(&netconsole_ext)) {
  599. netconsole_ext.flags |= CON_ENABLED;
  600. register_console(&netconsole_ext);
  601. }
  602. /* User might be enabling the basic format target for the very
  603. * first time, make sure the console is registered.
  604. */
  605. if (!nt->extended && !console_is_registered(&netconsole)) {
  606. netconsole.flags |= CON_ENABLED;
  607. register_console(&netconsole);
  608. }
  609. /*
  610. * Skip netconsole_parser_cmdline() -- all the attributes are
  611. * already configured via configfs. Just print them out.
  612. */
  613. netconsole_print_banner(&nt->np);
  614. ret = netpoll_setup(&nt->np);
  615. if (ret)
  616. goto out_unlock;
  617. nt->state = STATE_ENABLED;
  618. pr_info("network logging started\n");
  619. } else { /* false */
  620. /* We need to disable the netconsole before cleaning it up
  621. * otherwise we might end up in write_msg() with
  622. * nt->np.dev == NULL and nt->state == STATE_ENABLED
  623. */
  624. mutex_lock(&target_cleanup_list_lock);
  625. spin_lock_irqsave(&target_list_lock, flags);
  626. nt->state = STATE_DISABLED;
  627. /* Remove the target from the list, while holding
  628. * target_list_lock
  629. */
  630. list_move(&nt->list, &target_cleanup_list);
  631. spin_unlock_irqrestore(&target_list_lock, flags);
  632. mutex_unlock(&target_cleanup_list_lock);
  633. /* Unregister consoles, whose the last target of that type got
  634. * disabled.
  635. */
  636. unregister_netcons_consoles();
  637. }
  638. ret = strnlen(buf, count);
  639. /* Deferred cleanup */
  640. netconsole_process_cleanups();
  641. out_unlock:
  642. dynamic_netconsole_mutex_unlock();
  643. return ret;
  644. }
  645. static ssize_t release_store(struct config_item *item, const char *buf,
  646. size_t count)
  647. {
  648. struct netconsole_target *nt = to_target(item);
  649. bool release;
  650. ssize_t ret;
  651. dynamic_netconsole_mutex_lock();
  652. if (nt->state == STATE_ENABLED) {
  653. pr_err("target (%s) is enabled, disable to update parameters\n",
  654. config_item_name(&nt->group.cg_item));
  655. ret = -EINVAL;
  656. goto out_unlock;
  657. }
  658. ret = kstrtobool(buf, &release);
  659. if (ret)
  660. goto out_unlock;
  661. nt->release = release;
  662. ret = strnlen(buf, count);
  663. out_unlock:
  664. dynamic_netconsole_mutex_unlock();
  665. return ret;
  666. }
  667. static ssize_t extended_store(struct config_item *item, const char *buf,
  668. size_t count)
  669. {
  670. struct netconsole_target *nt = to_target(item);
  671. bool extended;
  672. ssize_t ret;
  673. dynamic_netconsole_mutex_lock();
  674. if (nt->state == STATE_ENABLED) {
  675. pr_err("target (%s) is enabled, disable to update parameters\n",
  676. config_item_name(&nt->group.cg_item));
  677. ret = -EINVAL;
  678. goto out_unlock;
  679. }
  680. ret = kstrtobool(buf, &extended);
  681. if (ret)
  682. goto out_unlock;
  683. nt->extended = extended;
  684. ret = strnlen(buf, count);
  685. out_unlock:
  686. dynamic_netconsole_mutex_unlock();
  687. return ret;
  688. }
  689. static ssize_t dev_name_store(struct config_item *item, const char *buf,
  690. size_t count)
  691. {
  692. struct netconsole_target *nt = to_target(item);
  693. dynamic_netconsole_mutex_lock();
  694. if (nt->state == STATE_ENABLED) {
  695. pr_err("target (%s) is enabled, disable to update parameters\n",
  696. config_item_name(&nt->group.cg_item));
  697. dynamic_netconsole_mutex_unlock();
  698. return -EINVAL;
  699. }
  700. strscpy(nt->np.dev_name, buf, IFNAMSIZ);
  701. trim_newline(nt->np.dev_name, IFNAMSIZ);
  702. dynamic_netconsole_mutex_unlock();
  703. return strnlen(buf, count);
  704. }
  705. static ssize_t local_port_store(struct config_item *item, const char *buf,
  706. size_t count)
  707. {
  708. struct netconsole_target *nt = to_target(item);
  709. ssize_t ret = -EINVAL;
  710. dynamic_netconsole_mutex_lock();
  711. if (nt->state == STATE_ENABLED) {
  712. pr_err("target (%s) is enabled, disable to update parameters\n",
  713. config_item_name(&nt->group.cg_item));
  714. goto out_unlock;
  715. }
  716. ret = kstrtou16(buf, 10, &nt->np.local_port);
  717. if (ret < 0)
  718. goto out_unlock;
  719. ret = strnlen(buf, count);
  720. out_unlock:
  721. dynamic_netconsole_mutex_unlock();
  722. return ret;
  723. }
  724. static ssize_t remote_port_store(struct config_item *item,
  725. const char *buf, size_t count)
  726. {
  727. struct netconsole_target *nt = to_target(item);
  728. ssize_t ret = -EINVAL;
  729. dynamic_netconsole_mutex_lock();
  730. if (nt->state == STATE_ENABLED) {
  731. pr_err("target (%s) is enabled, disable to update parameters\n",
  732. config_item_name(&nt->group.cg_item));
  733. goto out_unlock;
  734. }
  735. ret = kstrtou16(buf, 10, &nt->np.remote_port);
  736. if (ret < 0)
  737. goto out_unlock;
  738. ret = strnlen(buf, count);
  739. out_unlock:
  740. dynamic_netconsole_mutex_unlock();
  741. return ret;
  742. }
  743. static ssize_t local_ip_store(struct config_item *item, const char *buf,
  744. size_t count)
  745. {
  746. struct netconsole_target *nt = to_target(item);
  747. ssize_t ret = -EINVAL;
  748. int ipv6;
  749. dynamic_netconsole_mutex_lock();
  750. if (nt->state == STATE_ENABLED) {
  751. pr_err("target (%s) is enabled, disable to update parameters\n",
  752. config_item_name(&nt->group.cg_item));
  753. goto out_unlock;
  754. }
  755. ipv6 = netpoll_parse_ip_addr(buf, &nt->np.local_ip);
  756. if (ipv6 == -1)
  757. goto out_unlock;
  758. nt->np.ipv6 = !!ipv6;
  759. ret = strnlen(buf, count);
  760. out_unlock:
  761. dynamic_netconsole_mutex_unlock();
  762. return ret;
  763. }
  764. static ssize_t remote_ip_store(struct config_item *item, const char *buf,
  765. size_t count)
  766. {
  767. struct netconsole_target *nt = to_target(item);
  768. ssize_t ret = -EINVAL;
  769. int ipv6;
  770. dynamic_netconsole_mutex_lock();
  771. if (nt->state == STATE_ENABLED) {
  772. pr_err("target (%s) is enabled, disable to update parameters\n",
  773. config_item_name(&nt->group.cg_item));
  774. goto out_unlock;
  775. }
  776. ipv6 = netpoll_parse_ip_addr(buf, &nt->np.remote_ip);
  777. if (ipv6 == -1)
  778. goto out_unlock;
  779. nt->np.ipv6 = !!ipv6;
  780. ret = strnlen(buf, count);
  781. out_unlock:
  782. dynamic_netconsole_mutex_unlock();
  783. return ret;
  784. }
  785. /* Count number of entries we have in userdata.
  786. * This is important because userdata only supports MAX_USERDATA_ITEMS
  787. * entries. Before enabling any new userdata feature, number of entries needs
  788. * to checked for available space.
  789. */
  790. static size_t count_userdata_entries(struct netconsole_target *nt)
  791. {
  792. return list_count_nodes(&nt->userdata_group.cg_children);
  793. }
  794. static ssize_t remote_mac_store(struct config_item *item, const char *buf,
  795. size_t count)
  796. {
  797. struct netconsole_target *nt = to_target(item);
  798. u8 remote_mac[ETH_ALEN];
  799. ssize_t ret = -EINVAL;
  800. dynamic_netconsole_mutex_lock();
  801. if (nt->state == STATE_ENABLED) {
  802. pr_err("target (%s) is enabled, disable to update parameters\n",
  803. config_item_name(&nt->group.cg_item));
  804. goto out_unlock;
  805. }
  806. if (!mac_pton(buf, remote_mac))
  807. goto out_unlock;
  808. if (buf[MAC_ADDR_STR_LEN] && buf[MAC_ADDR_STR_LEN] != '\n')
  809. goto out_unlock;
  810. memcpy(nt->np.remote_mac, remote_mac, ETH_ALEN);
  811. ret = strnlen(buf, count);
  812. out_unlock:
  813. dynamic_netconsole_mutex_unlock();
  814. return ret;
  815. }
  816. struct userdatum {
  817. struct config_item item;
  818. char value[MAX_EXTRADATA_VALUE_LEN];
  819. };
  820. static struct userdatum *to_userdatum(struct config_item *item)
  821. {
  822. return container_of(item, struct userdatum, item);
  823. }
  824. struct userdata {
  825. struct config_group group;
  826. };
  827. static struct userdata *to_userdata(struct config_item *item)
  828. {
  829. return container_of(to_config_group(item), struct userdata, group);
  830. }
  831. static struct netconsole_target *userdata_to_target(struct userdata *ud)
  832. {
  833. struct config_group *netconsole_group;
  834. netconsole_group = to_config_group(ud->group.cg_item.ci_parent);
  835. return to_target(&netconsole_group->cg_item);
  836. }
  837. static ssize_t userdatum_value_show(struct config_item *item, char *buf)
  838. {
  839. return sysfs_emit(buf, "%s\n", &(to_userdatum(item)->value[0]));
  840. }
  841. /* Navigate configfs and calculate the lentgh of the formatted string
  842. * representing userdata.
  843. * Must be called holding netconsole_subsys.su_mutex
  844. */
  845. static int calc_userdata_len(struct netconsole_target *nt)
  846. {
  847. struct userdatum *udm_item;
  848. struct config_item *item;
  849. struct list_head *entry;
  850. int len = 0;
  851. list_for_each(entry, &nt->userdata_group.cg_children) {
  852. item = container_of(entry, struct config_item, ci_entry);
  853. udm_item = to_userdatum(item);
  854. /* Skip userdata with no value set */
  855. if (udm_item->value[0]) {
  856. len += snprintf(NULL, 0, " %s=%s\n", item->ci_name,
  857. udm_item->value);
  858. }
  859. }
  860. return len;
  861. }
  862. static int update_userdata(struct netconsole_target *nt)
  863. {
  864. struct userdatum *udm_item;
  865. struct config_item *item;
  866. struct list_head *entry;
  867. char *old_buf = NULL;
  868. char *new_buf = NULL;
  869. unsigned long flags;
  870. int offset = 0;
  871. int len;
  872. /* Calculate required buffer size */
  873. len = calc_userdata_len(nt);
  874. if (WARN_ON_ONCE(len > MAX_EXTRADATA_ENTRY_LEN * MAX_USERDATA_ITEMS))
  875. return -ENOSPC;
  876. /* Allocate new buffer */
  877. if (len) {
  878. new_buf = kmalloc(len + 1, GFP_KERNEL);
  879. if (!new_buf)
  880. return -ENOMEM;
  881. }
  882. /* Write userdata to new buffer */
  883. list_for_each(entry, &nt->userdata_group.cg_children) {
  884. item = container_of(entry, struct config_item, ci_entry);
  885. udm_item = to_userdatum(item);
  886. /* Skip userdata with no value set */
  887. if (udm_item->value[0]) {
  888. offset += scnprintf(&new_buf[offset], len + 1 - offset,
  889. " %s=%s\n", item->ci_name,
  890. udm_item->value);
  891. }
  892. }
  893. WARN_ON_ONCE(offset != len);
  894. /* Switch to new buffer and free old buffer */
  895. spin_lock_irqsave(&target_list_lock, flags);
  896. old_buf = nt->userdata;
  897. nt->userdata = new_buf;
  898. nt->userdata_length = offset;
  899. spin_unlock_irqrestore(&target_list_lock, flags);
  900. kfree(old_buf);
  901. return 0;
  902. }
  903. static ssize_t userdatum_value_store(struct config_item *item, const char *buf,
  904. size_t count)
  905. {
  906. struct userdatum *udm = to_userdatum(item);
  907. struct netconsole_target *nt;
  908. struct userdata *ud;
  909. ssize_t ret;
  910. if (count > MAX_EXTRADATA_VALUE_LEN)
  911. return -EMSGSIZE;
  912. mutex_lock(&netconsole_subsys.su_mutex);
  913. dynamic_netconsole_mutex_lock();
  914. ret = strscpy(udm->value, buf, sizeof(udm->value));
  915. if (ret < 0)
  916. goto out_unlock;
  917. trim_newline(udm->value, sizeof(udm->value));
  918. ud = to_userdata(item->ci_parent);
  919. nt = userdata_to_target(ud);
  920. ret = update_userdata(nt);
  921. if (ret < 0)
  922. goto out_unlock;
  923. ret = count;
  924. out_unlock:
  925. dynamic_netconsole_mutex_unlock();
  926. mutex_unlock(&netconsole_subsys.su_mutex);
  927. return ret;
  928. }
  929. /* disable_sysdata_feature - Disable sysdata feature and clean sysdata
  930. * @nt: target that is disabling the feature
  931. * @feature: feature being disabled
  932. */
  933. static void disable_sysdata_feature(struct netconsole_target *nt,
  934. enum sysdata_feature feature)
  935. {
  936. nt->sysdata_fields &= ~feature;
  937. nt->sysdata[0] = 0;
  938. }
  939. static ssize_t sysdata_msgid_enabled_store(struct config_item *item,
  940. const char *buf, size_t count)
  941. {
  942. struct netconsole_target *nt = to_target(item->ci_parent);
  943. bool msgid_enabled, curr;
  944. ssize_t ret;
  945. ret = kstrtobool(buf, &msgid_enabled);
  946. if (ret)
  947. return ret;
  948. mutex_lock(&netconsole_subsys.su_mutex);
  949. dynamic_netconsole_mutex_lock();
  950. curr = !!(nt->sysdata_fields & SYSDATA_MSGID);
  951. if (msgid_enabled == curr)
  952. goto unlock_ok;
  953. if (msgid_enabled)
  954. nt->sysdata_fields |= SYSDATA_MSGID;
  955. else
  956. disable_sysdata_feature(nt, SYSDATA_MSGID);
  957. unlock_ok:
  958. ret = strnlen(buf, count);
  959. dynamic_netconsole_mutex_unlock();
  960. mutex_unlock(&netconsole_subsys.su_mutex);
  961. return ret;
  962. }
  963. static ssize_t sysdata_release_enabled_store(struct config_item *item,
  964. const char *buf, size_t count)
  965. {
  966. struct netconsole_target *nt = to_target(item->ci_parent);
  967. bool release_enabled, curr;
  968. ssize_t ret;
  969. ret = kstrtobool(buf, &release_enabled);
  970. if (ret)
  971. return ret;
  972. mutex_lock(&netconsole_subsys.su_mutex);
  973. dynamic_netconsole_mutex_lock();
  974. curr = !!(nt->sysdata_fields & SYSDATA_RELEASE);
  975. if (release_enabled == curr)
  976. goto unlock_ok;
  977. if (release_enabled)
  978. nt->sysdata_fields |= SYSDATA_RELEASE;
  979. else
  980. disable_sysdata_feature(nt, SYSDATA_RELEASE);
  981. unlock_ok:
  982. ret = strnlen(buf, count);
  983. dynamic_netconsole_mutex_unlock();
  984. mutex_unlock(&netconsole_subsys.su_mutex);
  985. return ret;
  986. }
  987. static ssize_t sysdata_taskname_enabled_store(struct config_item *item,
  988. const char *buf, size_t count)
  989. {
  990. struct netconsole_target *nt = to_target(item->ci_parent);
  991. bool taskname_enabled, curr;
  992. ssize_t ret;
  993. ret = kstrtobool(buf, &taskname_enabled);
  994. if (ret)
  995. return ret;
  996. mutex_lock(&netconsole_subsys.su_mutex);
  997. dynamic_netconsole_mutex_lock();
  998. curr = !!(nt->sysdata_fields & SYSDATA_TASKNAME);
  999. if (taskname_enabled == curr)
  1000. goto unlock_ok;
  1001. if (taskname_enabled)
  1002. nt->sysdata_fields |= SYSDATA_TASKNAME;
  1003. else
  1004. disable_sysdata_feature(nt, SYSDATA_TASKNAME);
  1005. unlock_ok:
  1006. ret = strnlen(buf, count);
  1007. dynamic_netconsole_mutex_unlock();
  1008. mutex_unlock(&netconsole_subsys.su_mutex);
  1009. return ret;
  1010. }
  1011. /* configfs helper to sysdata cpu_nr feature */
  1012. static ssize_t sysdata_cpu_nr_enabled_store(struct config_item *item,
  1013. const char *buf, size_t count)
  1014. {
  1015. struct netconsole_target *nt = to_target(item->ci_parent);
  1016. bool cpu_nr_enabled, curr;
  1017. ssize_t ret;
  1018. ret = kstrtobool(buf, &cpu_nr_enabled);
  1019. if (ret)
  1020. return ret;
  1021. mutex_lock(&netconsole_subsys.su_mutex);
  1022. dynamic_netconsole_mutex_lock();
  1023. curr = !!(nt->sysdata_fields & SYSDATA_CPU_NR);
  1024. if (cpu_nr_enabled == curr)
  1025. /* no change requested */
  1026. goto unlock_ok;
  1027. if (cpu_nr_enabled)
  1028. nt->sysdata_fields |= SYSDATA_CPU_NR;
  1029. else
  1030. /* This is special because sysdata might have remaining data
  1031. * from previous sysdata, and it needs to be cleaned.
  1032. */
  1033. disable_sysdata_feature(nt, SYSDATA_CPU_NR);
  1034. unlock_ok:
  1035. ret = strnlen(buf, count);
  1036. dynamic_netconsole_mutex_unlock();
  1037. mutex_unlock(&netconsole_subsys.su_mutex);
  1038. return ret;
  1039. }
  1040. CONFIGFS_ATTR(userdatum_, value);
  1041. CONFIGFS_ATTR(sysdata_, cpu_nr_enabled);
  1042. CONFIGFS_ATTR(sysdata_, taskname_enabled);
  1043. CONFIGFS_ATTR(sysdata_, release_enabled);
  1044. CONFIGFS_ATTR(sysdata_, msgid_enabled);
  1045. static struct configfs_attribute *userdatum_attrs[] = {
  1046. &userdatum_attr_value,
  1047. NULL,
  1048. };
  1049. static void userdatum_release(struct config_item *item)
  1050. {
  1051. kfree(to_userdatum(item));
  1052. }
  1053. static struct configfs_item_operations userdatum_ops = {
  1054. .release = userdatum_release,
  1055. };
  1056. static const struct config_item_type userdatum_type = {
  1057. .ct_item_ops = &userdatum_ops,
  1058. .ct_attrs = userdatum_attrs,
  1059. .ct_owner = THIS_MODULE,
  1060. };
  1061. static struct config_item *userdatum_make_item(struct config_group *group,
  1062. const char *name)
  1063. {
  1064. struct netconsole_target *nt;
  1065. struct userdatum *udm;
  1066. struct userdata *ud;
  1067. if (strlen(name) > MAX_EXTRADATA_NAME_LEN)
  1068. return ERR_PTR(-ENAMETOOLONG);
  1069. ud = to_userdata(&group->cg_item);
  1070. nt = userdata_to_target(ud);
  1071. if (count_userdata_entries(nt) >= MAX_USERDATA_ITEMS)
  1072. return ERR_PTR(-ENOSPC);
  1073. udm = kzalloc_obj(*udm);
  1074. if (!udm)
  1075. return ERR_PTR(-ENOMEM);
  1076. config_item_init_type_name(&udm->item, name, &userdatum_type);
  1077. return &udm->item;
  1078. }
  1079. static void userdatum_drop(struct config_group *group, struct config_item *item)
  1080. {
  1081. struct netconsole_target *nt;
  1082. struct userdata *ud;
  1083. ud = to_userdata(&group->cg_item);
  1084. nt = userdata_to_target(ud);
  1085. dynamic_netconsole_mutex_lock();
  1086. update_userdata(nt);
  1087. config_item_put(item);
  1088. dynamic_netconsole_mutex_unlock();
  1089. }
  1090. static struct configfs_attribute *userdata_attrs[] = {
  1091. &sysdata_attr_cpu_nr_enabled,
  1092. &sysdata_attr_taskname_enabled,
  1093. &sysdata_attr_release_enabled,
  1094. &sysdata_attr_msgid_enabled,
  1095. NULL,
  1096. };
  1097. static struct configfs_group_operations userdata_ops = {
  1098. .make_item = userdatum_make_item,
  1099. .drop_item = userdatum_drop,
  1100. };
  1101. static const struct config_item_type userdata_type = {
  1102. .ct_item_ops = &userdatum_ops,
  1103. .ct_group_ops = &userdata_ops,
  1104. .ct_attrs = userdata_attrs,
  1105. .ct_owner = THIS_MODULE,
  1106. };
  1107. CONFIGFS_ATTR(, enabled);
  1108. CONFIGFS_ATTR(, extended);
  1109. CONFIGFS_ATTR(, dev_name);
  1110. CONFIGFS_ATTR(, local_port);
  1111. CONFIGFS_ATTR(, remote_port);
  1112. CONFIGFS_ATTR(, local_ip);
  1113. CONFIGFS_ATTR(, remote_ip);
  1114. CONFIGFS_ATTR_RO(, local_mac);
  1115. CONFIGFS_ATTR(, remote_mac);
  1116. CONFIGFS_ATTR(, release);
  1117. CONFIGFS_ATTR_RO(, transmit_errors);
  1118. static struct configfs_attribute *netconsole_target_attrs[] = {
  1119. &attr_enabled,
  1120. &attr_extended,
  1121. &attr_release,
  1122. &attr_dev_name,
  1123. &attr_local_port,
  1124. &attr_remote_port,
  1125. &attr_local_ip,
  1126. &attr_remote_ip,
  1127. &attr_local_mac,
  1128. &attr_remote_mac,
  1129. &attr_transmit_errors,
  1130. NULL,
  1131. };
  1132. /*
  1133. * Item operations and type for netconsole_target.
  1134. */
  1135. static void netconsole_target_release(struct config_item *item)
  1136. {
  1137. struct netconsole_target *nt = to_target(item);
  1138. kfree(nt->userdata);
  1139. kfree(nt);
  1140. }
  1141. static struct configfs_item_operations netconsole_target_item_ops = {
  1142. .release = netconsole_target_release,
  1143. };
  1144. static const struct config_item_type netconsole_target_type = {
  1145. .ct_attrs = netconsole_target_attrs,
  1146. .ct_item_ops = &netconsole_target_item_ops,
  1147. .ct_owner = THIS_MODULE,
  1148. };
  1149. static void init_target_config_group(struct netconsole_target *nt,
  1150. const char *name)
  1151. {
  1152. config_group_init_type_name(&nt->group, name, &netconsole_target_type);
  1153. config_group_init_type_name(&nt->userdata_group, "userdata",
  1154. &userdata_type);
  1155. configfs_add_default_group(&nt->userdata_group, &nt->group);
  1156. }
  1157. static struct netconsole_target *find_cmdline_target(const char *name)
  1158. {
  1159. struct netconsole_target *nt, *ret = NULL;
  1160. unsigned long flags;
  1161. spin_lock_irqsave(&target_list_lock, flags);
  1162. list_for_each_entry(nt, &target_list, list) {
  1163. if (!strcmp(nt->group.cg_item.ci_name, name)) {
  1164. ret = nt;
  1165. break;
  1166. }
  1167. }
  1168. spin_unlock_irqrestore(&target_list_lock, flags);
  1169. return ret;
  1170. }
  1171. /*
  1172. * Group operations and type for netconsole_subsys.
  1173. */
  1174. static struct config_group *make_netconsole_target(struct config_group *group,
  1175. const char *name)
  1176. {
  1177. struct netconsole_target *nt;
  1178. unsigned long flags;
  1179. /* Checking if a target by this name was created at boot time. If so,
  1180. * attach a configfs entry to that target. This enables dynamic
  1181. * control.
  1182. */
  1183. if (!strncmp(name, NETCONSOLE_PARAM_TARGET_PREFIX,
  1184. strlen(NETCONSOLE_PARAM_TARGET_PREFIX))) {
  1185. nt = find_cmdline_target(name);
  1186. if (nt) {
  1187. init_target_config_group(nt, name);
  1188. return &nt->group;
  1189. }
  1190. }
  1191. nt = alloc_and_init();
  1192. if (!nt)
  1193. return ERR_PTR(-ENOMEM);
  1194. /* Initialize the config_group member */
  1195. init_target_config_group(nt, name);
  1196. /* Adding, but it is disabled */
  1197. spin_lock_irqsave(&target_list_lock, flags);
  1198. list_add(&nt->list, &target_list);
  1199. spin_unlock_irqrestore(&target_list_lock, flags);
  1200. return &nt->group;
  1201. }
  1202. static void drop_netconsole_target(struct config_group *group,
  1203. struct config_item *item)
  1204. {
  1205. struct netconsole_target *nt = to_target(item);
  1206. unsigned long flags;
  1207. dynamic_netconsole_mutex_lock();
  1208. spin_lock_irqsave(&target_list_lock, flags);
  1209. /* Disable deactivated target to prevent races between resume attempt
  1210. * and target removal.
  1211. */
  1212. if (nt->state == STATE_DEACTIVATED)
  1213. nt->state = STATE_DISABLED;
  1214. list_del(&nt->list);
  1215. spin_unlock_irqrestore(&target_list_lock, flags);
  1216. dynamic_netconsole_mutex_unlock();
  1217. /* Now that the target has been marked disabled no further work
  1218. * can be scheduled. Existing work will skip as targets are not
  1219. * deactivated anymore. Cancel any scheduled resume and wait for
  1220. * completion.
  1221. */
  1222. cancel_work_sync(&nt->resume_wq);
  1223. /*
  1224. * The target may have never been enabled, or was manually disabled
  1225. * before being removed so netpoll may have already been cleaned up.
  1226. */
  1227. if (nt->state == STATE_ENABLED)
  1228. netpoll_cleanup(&nt->np);
  1229. config_item_put(&nt->group.cg_item);
  1230. }
  1231. static struct configfs_group_operations netconsole_subsys_group_ops = {
  1232. .make_group = make_netconsole_target,
  1233. .drop_item = drop_netconsole_target,
  1234. };
  1235. static const struct config_item_type netconsole_subsys_type = {
  1236. .ct_group_ops = &netconsole_subsys_group_ops,
  1237. .ct_owner = THIS_MODULE,
  1238. };
  1239. /* The netconsole configfs subsystem */
  1240. static struct configfs_subsystem netconsole_subsys = {
  1241. .su_group = {
  1242. .cg_item = {
  1243. .ci_namebuf = "netconsole",
  1244. .ci_type = &netconsole_subsys_type,
  1245. },
  1246. },
  1247. };
  1248. static void populate_configfs_item(struct netconsole_target *nt,
  1249. int cmdline_count)
  1250. {
  1251. char target_name[16];
  1252. snprintf(target_name, sizeof(target_name), "%s%d",
  1253. NETCONSOLE_PARAM_TARGET_PREFIX, cmdline_count);
  1254. init_target_config_group(nt, target_name);
  1255. }
  1256. static int sysdata_append_cpu_nr(struct netconsole_target *nt, int offset,
  1257. struct nbcon_write_context *wctxt)
  1258. {
  1259. return scnprintf(&nt->sysdata[offset],
  1260. MAX_EXTRADATA_ENTRY_LEN, " cpu=%u\n",
  1261. wctxt->cpu);
  1262. }
  1263. static int sysdata_append_taskname(struct netconsole_target *nt, int offset,
  1264. struct nbcon_write_context *wctxt)
  1265. {
  1266. return scnprintf(&nt->sysdata[offset],
  1267. MAX_EXTRADATA_ENTRY_LEN, " taskname=%s\n",
  1268. wctxt->comm);
  1269. }
  1270. static int sysdata_append_release(struct netconsole_target *nt, int offset)
  1271. {
  1272. return scnprintf(&nt->sysdata[offset],
  1273. MAX_EXTRADATA_ENTRY_LEN, " release=%s\n",
  1274. init_utsname()->release);
  1275. }
  1276. static int sysdata_append_msgid(struct netconsole_target *nt, int offset)
  1277. {
  1278. wrapping_assign_add(nt->msgcounter, 1);
  1279. return scnprintf(&nt->sysdata[offset],
  1280. MAX_EXTRADATA_ENTRY_LEN, " msgid=%u\n",
  1281. nt->msgcounter);
  1282. }
  1283. /*
  1284. * prepare_sysdata - append sysdata in runtime
  1285. * @nt: target to send message to
  1286. * @wctxt: nbcon write context containing message metadata
  1287. */
  1288. static int prepare_sysdata(struct netconsole_target *nt,
  1289. struct nbcon_write_context *wctxt)
  1290. {
  1291. int sysdata_len = 0;
  1292. if (!nt->sysdata_fields)
  1293. goto out;
  1294. if (nt->sysdata_fields & SYSDATA_CPU_NR)
  1295. sysdata_len += sysdata_append_cpu_nr(nt, sysdata_len, wctxt);
  1296. if (nt->sysdata_fields & SYSDATA_TASKNAME)
  1297. sysdata_len += sysdata_append_taskname(nt, sysdata_len, wctxt);
  1298. if (nt->sysdata_fields & SYSDATA_RELEASE)
  1299. sysdata_len += sysdata_append_release(nt, sysdata_len);
  1300. if (nt->sysdata_fields & SYSDATA_MSGID)
  1301. sysdata_len += sysdata_append_msgid(nt, sysdata_len);
  1302. WARN_ON_ONCE(sysdata_len >
  1303. MAX_EXTRADATA_ENTRY_LEN * MAX_SYSDATA_ITEMS);
  1304. out:
  1305. return sysdata_len;
  1306. }
  1307. #endif /* CONFIG_NETCONSOLE_DYNAMIC */
  1308. /* Handle network interface device notifications */
  1309. static int netconsole_netdev_event(struct notifier_block *this,
  1310. unsigned long event, void *ptr)
  1311. {
  1312. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  1313. struct netconsole_target *nt, *tmp;
  1314. bool stopped = false;
  1315. unsigned long flags;
  1316. if (!(event == NETDEV_CHANGENAME || event == NETDEV_UNREGISTER ||
  1317. event == NETDEV_RELEASE || event == NETDEV_JOIN ||
  1318. event == NETDEV_REGISTER))
  1319. goto done;
  1320. mutex_lock(&target_cleanup_list_lock);
  1321. spin_lock_irqsave(&target_list_lock, flags);
  1322. list_for_each_entry_safe(nt, tmp, &target_list, list) {
  1323. netconsole_target_get(nt);
  1324. if (nt->np.dev == dev) {
  1325. switch (event) {
  1326. case NETDEV_CHANGENAME:
  1327. strscpy(nt->np.dev_name, dev->name, IFNAMSIZ);
  1328. break;
  1329. case NETDEV_RELEASE:
  1330. case NETDEV_JOIN:
  1331. /* transition target to DISABLED instead of
  1332. * DEACTIVATED when (de)enslaving devices as
  1333. * their targets should not be automatically
  1334. * resumed when the interface is brought up.
  1335. */
  1336. nt->state = STATE_DISABLED;
  1337. list_move(&nt->list, &target_cleanup_list);
  1338. stopped = true;
  1339. break;
  1340. case NETDEV_UNREGISTER:
  1341. nt->state = STATE_DEACTIVATED;
  1342. list_move(&nt->list, &target_cleanup_list);
  1343. stopped = true;
  1344. }
  1345. }
  1346. if ((event == NETDEV_REGISTER || event == NETDEV_CHANGENAME) &&
  1347. deactivated_target_match(nt, dev))
  1348. /* Schedule resume on a workqueue as it will attempt
  1349. * to UP the device, which can't be done as part of this
  1350. * notifier.
  1351. */
  1352. queue_work(netconsole_wq, &nt->resume_wq);
  1353. netconsole_target_put(nt);
  1354. }
  1355. spin_unlock_irqrestore(&target_list_lock, flags);
  1356. mutex_unlock(&target_cleanup_list_lock);
  1357. if (stopped) {
  1358. const char *msg = "had an event";
  1359. switch (event) {
  1360. case NETDEV_UNREGISTER:
  1361. msg = "unregistered";
  1362. break;
  1363. case NETDEV_RELEASE:
  1364. msg = "released slaves";
  1365. break;
  1366. case NETDEV_JOIN:
  1367. msg = "is joining a master device";
  1368. break;
  1369. }
  1370. pr_info("network logging stopped on interface %s as it %s\n",
  1371. dev->name, msg);
  1372. }
  1373. /* Process target_cleanup_list entries. By the end, target_cleanup_list
  1374. * should be empty
  1375. */
  1376. netconsole_process_cleanups_core();
  1377. done:
  1378. return NOTIFY_DONE;
  1379. }
  1380. static struct notifier_block netconsole_netdev_notifier = {
  1381. .notifier_call = netconsole_netdev_event,
  1382. };
  1383. /**
  1384. * send_udp - Wrapper for netpoll_send_udp that counts errors
  1385. * @nt: target to send message to
  1386. * @msg: message to send
  1387. * @len: length of message
  1388. *
  1389. * Calls netpoll_send_udp and classifies the return value. If an error
  1390. * occurred it increments statistics in nt->stats accordingly.
  1391. * Only calls netpoll_send_udp if CONFIG_NETCONSOLE_DYNAMIC is disabled.
  1392. */
  1393. static void send_udp(struct netconsole_target *nt, const char *msg, int len)
  1394. {
  1395. int result = netpoll_send_udp(&nt->np, msg, len);
  1396. if (IS_ENABLED(CONFIG_NETCONSOLE_DYNAMIC)) {
  1397. if (result == NET_XMIT_DROP) {
  1398. u64_stats_update_begin(&nt->stats.syncp);
  1399. u64_stats_inc(&nt->stats.xmit_drop_count);
  1400. u64_stats_update_end(&nt->stats.syncp);
  1401. } else if (result == -ENOMEM) {
  1402. u64_stats_update_begin(&nt->stats.syncp);
  1403. u64_stats_inc(&nt->stats.enomem_count);
  1404. u64_stats_update_end(&nt->stats.syncp);
  1405. }
  1406. }
  1407. }
  1408. static void send_msg_no_fragmentation(struct netconsole_target *nt,
  1409. const char *msg,
  1410. int msg_len,
  1411. int release_len)
  1412. {
  1413. const char *userdata = NULL;
  1414. const char *sysdata = NULL;
  1415. const char *release;
  1416. #ifdef CONFIG_NETCONSOLE_DYNAMIC
  1417. userdata = nt->userdata;
  1418. sysdata = nt->sysdata;
  1419. #endif
  1420. if (release_len) {
  1421. release = init_utsname()->release;
  1422. scnprintf(nt->buf, MAX_PRINT_CHUNK, "%s,%.*s", release,
  1423. msg_len, msg);
  1424. msg_len += release_len;
  1425. } else {
  1426. memcpy(nt->buf, msg, msg_len);
  1427. }
  1428. if (userdata)
  1429. msg_len += scnprintf(&nt->buf[msg_len],
  1430. MAX_PRINT_CHUNK - msg_len, "%s",
  1431. userdata);
  1432. if (sysdata)
  1433. msg_len += scnprintf(&nt->buf[msg_len],
  1434. MAX_PRINT_CHUNK - msg_len, "%s",
  1435. sysdata);
  1436. send_udp(nt, nt->buf, msg_len);
  1437. }
  1438. static void append_release(char *buf)
  1439. {
  1440. const char *release;
  1441. release = init_utsname()->release;
  1442. scnprintf(buf, MAX_PRINT_CHUNK, "%s,", release);
  1443. }
  1444. static void send_fragmented_body(struct netconsole_target *nt,
  1445. const char *msgbody_ptr, int header_len,
  1446. int msgbody_len, int sysdata_len)
  1447. {
  1448. const char *userdata_ptr = NULL;
  1449. const char *sysdata_ptr = NULL;
  1450. int data_len, data_sent = 0;
  1451. int userdata_offset = 0;
  1452. int sysdata_offset = 0;
  1453. int msgbody_offset = 0;
  1454. int userdata_len = 0;
  1455. #ifdef CONFIG_NETCONSOLE_DYNAMIC
  1456. userdata_ptr = nt->userdata;
  1457. sysdata_ptr = nt->sysdata;
  1458. userdata_len = nt->userdata_length;
  1459. #endif
  1460. if (WARN_ON_ONCE(!userdata_ptr && userdata_len != 0))
  1461. return;
  1462. if (WARN_ON_ONCE(!sysdata_ptr && sysdata_len != 0))
  1463. return;
  1464. /* data_len represents the number of bytes that will be sent. This is
  1465. * bigger than MAX_PRINT_CHUNK, thus, it will be split in multiple
  1466. * packets
  1467. */
  1468. data_len = msgbody_len + userdata_len + sysdata_len;
  1469. /* In each iteration of the while loop below, we send a packet
  1470. * containing the header and a portion of the data. The data is
  1471. * composed of three parts: msgbody, userdata, and sysdata.
  1472. * We keep track of how many bytes have been sent from each part using
  1473. * the *_offset variables.
  1474. * We keep track of how many bytes have been sent overall using the
  1475. * data_sent variable, which ranges from 0 to the total bytes to be
  1476. * sent.
  1477. */
  1478. while (data_sent < data_len) {
  1479. int userdata_left = userdata_len - userdata_offset;
  1480. int sysdata_left = sysdata_len - sysdata_offset;
  1481. int msgbody_left = msgbody_len - msgbody_offset;
  1482. int buf_offset = 0;
  1483. int this_chunk = 0;
  1484. /* header is already populated in nt->buf, just append to it */
  1485. buf_offset = header_len;
  1486. buf_offset += scnprintf(nt->buf + buf_offset,
  1487. MAX_PRINT_CHUNK - buf_offset,
  1488. ",ncfrag=%d/%d;", data_sent,
  1489. data_len);
  1490. /* append msgbody first */
  1491. this_chunk = min(msgbody_left, MAX_PRINT_CHUNK - buf_offset);
  1492. memcpy(nt->buf + buf_offset, msgbody_ptr + msgbody_offset,
  1493. this_chunk);
  1494. msgbody_offset += this_chunk;
  1495. buf_offset += this_chunk;
  1496. data_sent += this_chunk;
  1497. /* after msgbody, append userdata */
  1498. if (userdata_ptr && userdata_left) {
  1499. this_chunk = min(userdata_left,
  1500. MAX_PRINT_CHUNK - buf_offset);
  1501. memcpy(nt->buf + buf_offset,
  1502. userdata_ptr + userdata_offset, this_chunk);
  1503. userdata_offset += this_chunk;
  1504. buf_offset += this_chunk;
  1505. data_sent += this_chunk;
  1506. }
  1507. /* after userdata, append sysdata */
  1508. if (sysdata_ptr && sysdata_left) {
  1509. this_chunk = min(sysdata_left,
  1510. MAX_PRINT_CHUNK - buf_offset);
  1511. memcpy(nt->buf + buf_offset,
  1512. sysdata_ptr + sysdata_offset, this_chunk);
  1513. sysdata_offset += this_chunk;
  1514. buf_offset += this_chunk;
  1515. data_sent += this_chunk;
  1516. }
  1517. /* if all is good, send the packet out */
  1518. if (WARN_ON_ONCE(data_sent > data_len))
  1519. return;
  1520. send_udp(nt, nt->buf, buf_offset);
  1521. }
  1522. }
  1523. static void send_msg_fragmented(struct netconsole_target *nt,
  1524. const char *msg,
  1525. int msg_len,
  1526. int release_len,
  1527. int sysdata_len)
  1528. {
  1529. int header_len, msgbody_len;
  1530. const char *msgbody;
  1531. /* need to insert extra header fields, detect header and msgbody */
  1532. msgbody = memchr(msg, ';', msg_len);
  1533. if (WARN_ON_ONCE(!msgbody))
  1534. return;
  1535. header_len = msgbody - msg;
  1536. msgbody_len = msg_len - header_len - 1;
  1537. msgbody++;
  1538. /*
  1539. * Transfer multiple chunks with the following extra header.
  1540. * "ncfrag=<byte-offset>/<total-bytes>"
  1541. */
  1542. if (release_len)
  1543. append_release(nt->buf);
  1544. /* Copy the header into the buffer */
  1545. memcpy(nt->buf + release_len, msg, header_len);
  1546. header_len += release_len;
  1547. /* for now on, the header will be persisted, and the msgbody
  1548. * will be replaced
  1549. */
  1550. send_fragmented_body(nt, msgbody, header_len, msgbody_len,
  1551. sysdata_len);
  1552. }
  1553. /**
  1554. * send_ext_msg_udp - send extended log message to target
  1555. * @nt: target to send message to
  1556. * @wctxt: nbcon write context containing message and metadata
  1557. *
  1558. * Transfer extended log message to @nt. If message is longer than
  1559. * MAX_PRINT_CHUNK, it'll be split and transmitted in multiple chunks with
  1560. * ncfrag header field added to identify them.
  1561. */
  1562. static void send_ext_msg_udp(struct netconsole_target *nt,
  1563. struct nbcon_write_context *wctxt)
  1564. {
  1565. int userdata_len = 0;
  1566. int release_len = 0;
  1567. int sysdata_len = 0;
  1568. int len;
  1569. #ifdef CONFIG_NETCONSOLE_DYNAMIC
  1570. sysdata_len = prepare_sysdata(nt, wctxt);
  1571. userdata_len = nt->userdata_length;
  1572. #endif
  1573. if (nt->release)
  1574. release_len = strlen(init_utsname()->release) + 1;
  1575. len = wctxt->len + release_len + sysdata_len + userdata_len;
  1576. if (len <= MAX_PRINT_CHUNK)
  1577. return send_msg_no_fragmentation(nt, wctxt->outbuf,
  1578. wctxt->len, release_len);
  1579. return send_msg_fragmented(nt, wctxt->outbuf, wctxt->len, release_len,
  1580. sysdata_len);
  1581. }
  1582. static void send_msg_udp(struct netconsole_target *nt, const char *msg,
  1583. unsigned int len)
  1584. {
  1585. const char *tmp = msg;
  1586. int frag, left = len;
  1587. while (left > 0) {
  1588. frag = min(left, MAX_PRINT_CHUNK);
  1589. send_udp(nt, tmp, frag);
  1590. tmp += frag;
  1591. left -= frag;
  1592. }
  1593. }
  1594. /**
  1595. * netconsole_write - Generic function to send a msg to all targets
  1596. * @wctxt: nbcon write context
  1597. * @extended: "true" for extended console mode
  1598. *
  1599. * Given an nbcon write context, send the message to the netconsole targets
  1600. */
  1601. static void netconsole_write(struct nbcon_write_context *wctxt, bool extended)
  1602. {
  1603. struct netconsole_target *nt;
  1604. if (oops_only && !oops_in_progress)
  1605. return;
  1606. list_for_each_entry(nt, &target_list, list) {
  1607. if (nt->extended != extended || nt->state != STATE_ENABLED ||
  1608. !netif_running(nt->np.dev))
  1609. continue;
  1610. /* If nbcon_enter_unsafe() fails, just return given netconsole
  1611. * lost the ownership, and iterating over the targets will not
  1612. * be able to re-acquire.
  1613. */
  1614. if (!nbcon_enter_unsafe(wctxt))
  1615. return;
  1616. if (extended)
  1617. send_ext_msg_udp(nt, wctxt);
  1618. else
  1619. send_msg_udp(nt, wctxt->outbuf, wctxt->len);
  1620. nbcon_exit_unsafe(wctxt);
  1621. }
  1622. }
  1623. static void netconsole_write_ext(struct console *con __always_unused,
  1624. struct nbcon_write_context *wctxt)
  1625. {
  1626. netconsole_write(wctxt, true);
  1627. }
  1628. static void netconsole_write_basic(struct console *con __always_unused,
  1629. struct nbcon_write_context *wctxt)
  1630. {
  1631. netconsole_write(wctxt, false);
  1632. }
  1633. static void netconsole_device_lock(struct console *con __always_unused,
  1634. unsigned long *flags)
  1635. __acquires(&target_list_lock)
  1636. {
  1637. spin_lock_irqsave(&target_list_lock, *flags);
  1638. }
  1639. static void netconsole_device_unlock(struct console *con __always_unused,
  1640. unsigned long flags)
  1641. __releases(&target_list_lock)
  1642. {
  1643. spin_unlock_irqrestore(&target_list_lock, flags);
  1644. }
  1645. static int netconsole_parser_cmdline(struct netpoll *np, char *opt)
  1646. {
  1647. bool ipversion_set = false;
  1648. char *cur = opt;
  1649. char *delim;
  1650. int ipv6;
  1651. if (*cur != '@') {
  1652. delim = strchr(cur, '@');
  1653. if (!delim)
  1654. goto parse_failed;
  1655. *delim = 0;
  1656. if (kstrtou16(cur, 10, &np->local_port))
  1657. goto parse_failed;
  1658. cur = delim;
  1659. }
  1660. cur++;
  1661. if (*cur != '/') {
  1662. ipversion_set = true;
  1663. delim = strchr(cur, '/');
  1664. if (!delim)
  1665. goto parse_failed;
  1666. *delim = 0;
  1667. ipv6 = netpoll_parse_ip_addr(cur, &np->local_ip);
  1668. if (ipv6 < 0)
  1669. goto parse_failed;
  1670. else
  1671. np->ipv6 = (bool)ipv6;
  1672. cur = delim;
  1673. }
  1674. cur++;
  1675. if (*cur != ',') {
  1676. /* parse out dev_name or dev_mac */
  1677. delim = strchr(cur, ',');
  1678. if (!delim)
  1679. goto parse_failed;
  1680. *delim = 0;
  1681. np->dev_name[0] = '\0';
  1682. eth_broadcast_addr(np->dev_mac);
  1683. if (!strchr(cur, ':'))
  1684. strscpy(np->dev_name, cur, sizeof(np->dev_name));
  1685. else if (!mac_pton(cur, np->dev_mac))
  1686. goto parse_failed;
  1687. cur = delim;
  1688. }
  1689. cur++;
  1690. if (*cur != '@') {
  1691. /* dst port */
  1692. delim = strchr(cur, '@');
  1693. if (!delim)
  1694. goto parse_failed;
  1695. *delim = 0;
  1696. if (*cur == ' ' || *cur == '\t')
  1697. np_info(np, "warning: whitespace is not allowed\n");
  1698. if (kstrtou16(cur, 10, &np->remote_port))
  1699. goto parse_failed;
  1700. cur = delim;
  1701. }
  1702. cur++;
  1703. /* dst ip */
  1704. delim = strchr(cur, '/');
  1705. if (!delim)
  1706. goto parse_failed;
  1707. *delim = 0;
  1708. ipv6 = netpoll_parse_ip_addr(cur, &np->remote_ip);
  1709. if (ipv6 < 0)
  1710. goto parse_failed;
  1711. else if (ipversion_set && np->ipv6 != (bool)ipv6)
  1712. goto parse_failed;
  1713. else
  1714. np->ipv6 = (bool)ipv6;
  1715. cur = delim + 1;
  1716. if (*cur != 0) {
  1717. /* MAC address */
  1718. if (!mac_pton(cur, np->remote_mac))
  1719. goto parse_failed;
  1720. }
  1721. netconsole_print_banner(np);
  1722. return 0;
  1723. parse_failed:
  1724. np_info(np, "couldn't parse config at '%s'!\n", cur);
  1725. return -1;
  1726. }
  1727. /* Allocate new target (from boot/module param) and setup netpoll for it */
  1728. static struct netconsole_target *alloc_param_target(char *target_config,
  1729. int cmdline_count)
  1730. {
  1731. struct netconsole_target *nt;
  1732. int err;
  1733. nt = alloc_and_init();
  1734. if (!nt) {
  1735. err = -ENOMEM;
  1736. goto fail;
  1737. }
  1738. if (*target_config == '+') {
  1739. nt->extended = true;
  1740. target_config++;
  1741. }
  1742. if (*target_config == 'r') {
  1743. if (!nt->extended) {
  1744. pr_err("Netconsole configuration error. Release feature requires extended log message");
  1745. err = -EINVAL;
  1746. goto fail;
  1747. }
  1748. nt->release = true;
  1749. target_config++;
  1750. }
  1751. /* Parse parameters and setup netpoll */
  1752. err = netconsole_parser_cmdline(&nt->np, target_config);
  1753. if (err)
  1754. goto fail;
  1755. err = netpoll_setup(&nt->np);
  1756. if (err) {
  1757. pr_err("Not enabling netconsole for %s%d. Netpoll setup failed\n",
  1758. NETCONSOLE_PARAM_TARGET_PREFIX, cmdline_count);
  1759. if (!IS_ENABLED(CONFIG_NETCONSOLE_DYNAMIC))
  1760. /* only fail if dynamic reconfiguration is set,
  1761. * otherwise, keep the target in the list, but disabled.
  1762. */
  1763. goto fail;
  1764. } else {
  1765. nt->state = STATE_ENABLED;
  1766. }
  1767. populate_configfs_item(nt, cmdline_count);
  1768. return nt;
  1769. fail:
  1770. kfree(nt);
  1771. return ERR_PTR(err);
  1772. }
  1773. /* Cleanup netpoll for given target (from boot/module param) and free it */
  1774. static void free_param_target(struct netconsole_target *nt)
  1775. {
  1776. cancel_work_sync(&nt->resume_wq);
  1777. netpoll_cleanup(&nt->np);
  1778. #ifdef CONFIG_NETCONSOLE_DYNAMIC
  1779. kfree(nt->userdata);
  1780. #endif
  1781. kfree(nt);
  1782. }
  1783. static struct console netconsole_ext = {
  1784. .name = "netcon_ext",
  1785. .flags = CON_ENABLED | CON_EXTENDED | CON_NBCON | CON_NBCON_ATOMIC_UNSAFE,
  1786. .write_thread = netconsole_write_ext,
  1787. .write_atomic = netconsole_write_ext,
  1788. .device_lock = netconsole_device_lock,
  1789. .device_unlock = netconsole_device_unlock,
  1790. };
  1791. static struct console netconsole = {
  1792. .name = "netcon",
  1793. .flags = CON_ENABLED | CON_NBCON | CON_NBCON_ATOMIC_UNSAFE,
  1794. .write_thread = netconsole_write_basic,
  1795. .write_atomic = netconsole_write_basic,
  1796. .device_lock = netconsole_device_lock,
  1797. .device_unlock = netconsole_device_unlock,
  1798. };
  1799. static int __init init_netconsole(void)
  1800. {
  1801. int err;
  1802. struct netconsole_target *nt, *tmp;
  1803. u32 console_type_needed = 0;
  1804. unsigned int count = 0;
  1805. unsigned long flags;
  1806. char *target_config;
  1807. char *input = config;
  1808. if (strnlen(input, MAX_PARAM_LENGTH)) {
  1809. while ((target_config = strsep(&input, ";"))) {
  1810. nt = alloc_param_target(target_config, count);
  1811. if (IS_ERR(nt)) {
  1812. if (IS_ENABLED(CONFIG_NETCONSOLE_DYNAMIC))
  1813. continue;
  1814. err = PTR_ERR(nt);
  1815. goto fail;
  1816. }
  1817. /* Dump existing printks when we register */
  1818. if (nt->extended) {
  1819. console_type_needed |= CONS_EXTENDED;
  1820. netconsole_ext.flags |= CON_PRINTBUFFER;
  1821. } else {
  1822. console_type_needed |= CONS_BASIC;
  1823. netconsole.flags |= CON_PRINTBUFFER;
  1824. }
  1825. spin_lock_irqsave(&target_list_lock, flags);
  1826. list_add(&nt->list, &target_list);
  1827. spin_unlock_irqrestore(&target_list_lock, flags);
  1828. count++;
  1829. }
  1830. }
  1831. netconsole_wq = alloc_workqueue("netconsole", WQ_UNBOUND, 0);
  1832. if (!netconsole_wq) {
  1833. err = -ENOMEM;
  1834. goto fail;
  1835. }
  1836. err = register_netdevice_notifier(&netconsole_netdev_notifier);
  1837. if (err)
  1838. goto fail;
  1839. err = dynamic_netconsole_init();
  1840. if (err)
  1841. goto undonotifier;
  1842. if (console_type_needed & CONS_EXTENDED)
  1843. register_console(&netconsole_ext);
  1844. if (console_type_needed & CONS_BASIC)
  1845. register_console(&netconsole);
  1846. pr_info("network logging started\n");
  1847. return err;
  1848. undonotifier:
  1849. unregister_netdevice_notifier(&netconsole_netdev_notifier);
  1850. fail:
  1851. pr_err("cleaning up\n");
  1852. if (netconsole_wq)
  1853. flush_workqueue(netconsole_wq);
  1854. /*
  1855. * Remove all targets and destroy them (only targets created
  1856. * from the boot/module option exist here). Skipping the list
  1857. * lock is safe here, and netpoll_cleanup() will sleep.
  1858. */
  1859. list_for_each_entry_safe(nt, tmp, &target_list, list) {
  1860. list_del(&nt->list);
  1861. free_param_target(nt);
  1862. }
  1863. if (netconsole_wq)
  1864. destroy_workqueue(netconsole_wq);
  1865. return err;
  1866. }
  1867. static void __exit cleanup_netconsole(void)
  1868. {
  1869. struct netconsole_target *nt, *tmp;
  1870. if (console_is_registered(&netconsole_ext))
  1871. unregister_console(&netconsole_ext);
  1872. if (console_is_registered(&netconsole))
  1873. unregister_console(&netconsole);
  1874. dynamic_netconsole_exit();
  1875. unregister_netdevice_notifier(&netconsole_netdev_notifier);
  1876. flush_workqueue(netconsole_wq);
  1877. /*
  1878. * Targets created via configfs pin references on our module
  1879. * and would first be rmdir(2)'ed from userspace. We reach
  1880. * here only when they are already destroyed, and only those
  1881. * created from the boot/module option are left, so remove and
  1882. * destroy them. Skipping the list lock is safe here, and
  1883. * netpoll_cleanup() will sleep.
  1884. */
  1885. list_for_each_entry_safe(nt, tmp, &target_list, list) {
  1886. list_del(&nt->list);
  1887. free_param_target(nt);
  1888. }
  1889. destroy_workqueue(netconsole_wq);
  1890. }
  1891. /*
  1892. * Use late_initcall to ensure netconsole is
  1893. * initialized after network device driver if built-in.
  1894. *
  1895. * late_initcall() and module_init() are identical if built as module.
  1896. */
  1897. late_initcall(init_netconsole);
  1898. module_exit(cleanup_netconsole);