netpoll.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323
  1. /*
  2. * Common framework for low-level network console, dump, and debugger code
  3. *
  4. * Sep 8 2003 Matt Mackall <mpm@selenic.com>
  5. *
  6. * based on the netconsole code from:
  7. *
  8. * Copyright (C) 2001 Ingo Molnar <mingo@redhat.com>
  9. * Copyright (C) 2002 Red Hat, Inc.
  10. */
  11. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  12. #include <linux/moduleparam.h>
  13. #include <linux/kernel.h>
  14. #include <linux/netdevice.h>
  15. #include <linux/etherdevice.h>
  16. #include <linux/string.h>
  17. #include <linux/if_arp.h>
  18. #include <linux/inetdevice.h>
  19. #include <linux/inet.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/netpoll.h>
  22. #include <linux/sched.h>
  23. #include <linux/delay.h>
  24. #include <linux/rcupdate.h>
  25. #include <linux/workqueue.h>
  26. #include <linux/slab.h>
  27. #include <linux/export.h>
  28. #include <linux/if_vlan.h>
  29. #include <net/tcp.h>
  30. #include <net/udp.h>
  31. #include <net/addrconf.h>
  32. #include <net/ndisc.h>
  33. #include <net/ip6_checksum.h>
  34. #include <asm/unaligned.h>
  35. #include <trace/events/napi.h>
  36. /*
  37. * We maintain a small pool of fully-sized skbs, to make sure the
  38. * message gets out even in extreme OOM situations.
  39. */
  40. #define MAX_UDP_CHUNK 1460
  41. #define MAX_SKBS 32
  42. static struct sk_buff_head skb_pool;
  43. static atomic_t trapped;
  44. DEFINE_STATIC_SRCU(netpoll_srcu);
  45. #define USEC_PER_POLL 50
  46. #define NETPOLL_RX_ENABLED 1
  47. #define NETPOLL_RX_DROP 2
  48. #define MAX_SKB_SIZE \
  49. (sizeof(struct ethhdr) + \
  50. sizeof(struct iphdr) + \
  51. sizeof(struct udphdr) + \
  52. MAX_UDP_CHUNK)
  53. static void zap_completion_queue(void);
  54. static void netpoll_neigh_reply(struct sk_buff *skb, struct netpoll_info *npinfo);
  55. static void netpoll_async_cleanup(struct work_struct *work);
  56. static unsigned int carrier_timeout = 4;
  57. module_param(carrier_timeout, uint, 0644);
  58. #define np_info(np, fmt, ...) \
  59. pr_info("%s: " fmt, np->name, ##__VA_ARGS__)
  60. #define np_err(np, fmt, ...) \
  61. pr_err("%s: " fmt, np->name, ##__VA_ARGS__)
  62. #define np_notice(np, fmt, ...) \
  63. pr_notice("%s: " fmt, np->name, ##__VA_ARGS__)
  64. static void queue_process(struct work_struct *work)
  65. {
  66. struct netpoll_info *npinfo =
  67. container_of(work, struct netpoll_info, tx_work.work);
  68. struct sk_buff *skb;
  69. unsigned long flags;
  70. while ((skb = skb_dequeue(&npinfo->txq))) {
  71. struct net_device *dev = skb->dev;
  72. const struct net_device_ops *ops = dev->netdev_ops;
  73. struct netdev_queue *txq;
  74. if (!netif_device_present(dev) || !netif_running(dev)) {
  75. __kfree_skb(skb);
  76. continue;
  77. }
  78. txq = netdev_get_tx_queue(dev, skb_get_queue_mapping(skb));
  79. local_irq_save(flags);
  80. __netif_tx_lock(txq, smp_processor_id());
  81. if (netif_xmit_frozen_or_stopped(txq) ||
  82. ops->ndo_start_xmit(skb, dev) != NETDEV_TX_OK) {
  83. skb_queue_head(&npinfo->txq, skb);
  84. __netif_tx_unlock(txq);
  85. local_irq_restore(flags);
  86. schedule_delayed_work(&npinfo->tx_work, HZ/10);
  87. return;
  88. }
  89. __netif_tx_unlock(txq);
  90. local_irq_restore(flags);
  91. }
  92. }
  93. static __sum16 checksum_udp(struct sk_buff *skb, struct udphdr *uh,
  94. unsigned short ulen, __be32 saddr, __be32 daddr)
  95. {
  96. __wsum psum;
  97. if (uh->check == 0 || skb_csum_unnecessary(skb))
  98. return 0;
  99. psum = csum_tcpudp_nofold(saddr, daddr, ulen, IPPROTO_UDP, 0);
  100. if (skb->ip_summed == CHECKSUM_COMPLETE &&
  101. !csum_fold(csum_add(psum, skb->csum)))
  102. return 0;
  103. skb->csum = psum;
  104. return __skb_checksum_complete(skb);
  105. }
  106. /*
  107. * Check whether delayed processing was scheduled for our NIC. If so,
  108. * we attempt to grab the poll lock and use ->poll() to pump the card.
  109. * If this fails, either we've recursed in ->poll() or it's already
  110. * running on another CPU.
  111. *
  112. * Note: we don't mask interrupts with this lock because we're using
  113. * trylock here and interrupts are already disabled in the softirq
  114. * case. Further, we test the poll_owner to avoid recursion on UP
  115. * systems where the lock doesn't exist.
  116. *
  117. * In cases where there is bi-directional communications, reading only
  118. * one message at a time can lead to packets being dropped by the
  119. * network adapter, forcing superfluous retries and possibly timeouts.
  120. * Thus, we set our budget to greater than 1.
  121. */
  122. static int poll_one_napi(struct netpoll_info *npinfo,
  123. struct napi_struct *napi, int budget)
  124. {
  125. int work;
  126. /* net_rx_action's ->poll() invocations and our's are
  127. * synchronized by this test which is only made while
  128. * holding the napi->poll_lock.
  129. */
  130. if (!test_bit(NAPI_STATE_SCHED, &napi->state))
  131. return budget;
  132. npinfo->rx_flags |= NETPOLL_RX_DROP;
  133. atomic_inc(&trapped);
  134. set_bit(NAPI_STATE_NPSVC, &napi->state);
  135. work = napi->poll(napi, budget);
  136. trace_napi_poll(napi);
  137. clear_bit(NAPI_STATE_NPSVC, &napi->state);
  138. atomic_dec(&trapped);
  139. npinfo->rx_flags &= ~NETPOLL_RX_DROP;
  140. return budget - work;
  141. }
  142. static void poll_napi(struct net_device *dev)
  143. {
  144. struct napi_struct *napi;
  145. int budget = 16;
  146. list_for_each_entry(napi, &dev->napi_list, dev_list) {
  147. if (napi->poll_owner != smp_processor_id() &&
  148. spin_trylock(&napi->poll_lock)) {
  149. budget = poll_one_napi(rcu_dereference_bh(dev->npinfo),
  150. napi, budget);
  151. spin_unlock(&napi->poll_lock);
  152. if (!budget)
  153. break;
  154. }
  155. }
  156. }
  157. static void service_neigh_queue(struct netpoll_info *npi)
  158. {
  159. if (npi) {
  160. struct sk_buff *skb;
  161. while ((skb = skb_dequeue(&npi->neigh_tx)))
  162. netpoll_neigh_reply(skb, npi);
  163. }
  164. }
  165. static void netpoll_poll_dev(struct net_device *dev)
  166. {
  167. const struct net_device_ops *ops;
  168. struct netpoll_info *ni = rcu_dereference_bh(dev->npinfo);
  169. /* Don't do any rx activity if the dev_lock mutex is held
  170. * the dev_open/close paths use this to block netpoll activity
  171. * while changing device state
  172. */
  173. if (down_trylock(&ni->dev_lock))
  174. return;
  175. if (!netif_running(dev)) {
  176. up(&ni->dev_lock);
  177. return;
  178. }
  179. ops = dev->netdev_ops;
  180. if (!ops->ndo_poll_controller) {
  181. up(&ni->dev_lock);
  182. return;
  183. }
  184. /* Process pending work on NIC */
  185. ops->ndo_poll_controller(dev);
  186. poll_napi(dev);
  187. up(&ni->dev_lock);
  188. if (dev->flags & IFF_SLAVE) {
  189. if (ni) {
  190. struct net_device *bond_dev;
  191. struct sk_buff *skb;
  192. struct netpoll_info *bond_ni;
  193. bond_dev = netdev_master_upper_dev_get_rcu(dev);
  194. bond_ni = rcu_dereference_bh(bond_dev->npinfo);
  195. while ((skb = skb_dequeue(&ni->neigh_tx))) {
  196. skb->dev = bond_dev;
  197. skb_queue_tail(&bond_ni->neigh_tx, skb);
  198. }
  199. }
  200. }
  201. service_neigh_queue(ni);
  202. zap_completion_queue();
  203. }
  204. void netpoll_rx_disable(struct net_device *dev)
  205. {
  206. struct netpoll_info *ni;
  207. int idx;
  208. might_sleep();
  209. idx = srcu_read_lock(&netpoll_srcu);
  210. ni = srcu_dereference(dev->npinfo, &netpoll_srcu);
  211. if (ni)
  212. down(&ni->dev_lock);
  213. srcu_read_unlock(&netpoll_srcu, idx);
  214. }
  215. EXPORT_SYMBOL(netpoll_rx_disable);
  216. void netpoll_rx_enable(struct net_device *dev)
  217. {
  218. struct netpoll_info *ni;
  219. rcu_read_lock();
  220. ni = rcu_dereference(dev->npinfo);
  221. if (ni)
  222. up(&ni->dev_lock);
  223. rcu_read_unlock();
  224. }
  225. EXPORT_SYMBOL(netpoll_rx_enable);
  226. static void refill_skbs(void)
  227. {
  228. struct sk_buff *skb;
  229. unsigned long flags;
  230. spin_lock_irqsave(&skb_pool.lock, flags);
  231. while (skb_pool.qlen < MAX_SKBS) {
  232. skb = alloc_skb(MAX_SKB_SIZE, GFP_ATOMIC);
  233. if (!skb)
  234. break;
  235. __skb_queue_tail(&skb_pool, skb);
  236. }
  237. spin_unlock_irqrestore(&skb_pool.lock, flags);
  238. }
  239. static void zap_completion_queue(void)
  240. {
  241. unsigned long flags;
  242. struct softnet_data *sd = &get_cpu_var(softnet_data);
  243. if (sd->completion_queue) {
  244. struct sk_buff *clist;
  245. local_irq_save(flags);
  246. clist = sd->completion_queue;
  247. sd->completion_queue = NULL;
  248. local_irq_restore(flags);
  249. while (clist != NULL) {
  250. struct sk_buff *skb = clist;
  251. clist = clist->next;
  252. if (skb->destructor) {
  253. atomic_inc(&skb->users);
  254. dev_kfree_skb_any(skb); /* put this one back */
  255. } else {
  256. __kfree_skb(skb);
  257. }
  258. }
  259. }
  260. put_cpu_var(softnet_data);
  261. }
  262. static struct sk_buff *find_skb(struct netpoll *np, int len, int reserve)
  263. {
  264. int count = 0;
  265. struct sk_buff *skb;
  266. zap_completion_queue();
  267. refill_skbs();
  268. repeat:
  269. skb = alloc_skb(len, GFP_ATOMIC);
  270. if (!skb)
  271. skb = skb_dequeue(&skb_pool);
  272. if (!skb) {
  273. if (++count < 10) {
  274. netpoll_poll_dev(np->dev);
  275. goto repeat;
  276. }
  277. return NULL;
  278. }
  279. atomic_set(&skb->users, 1);
  280. skb_reserve(skb, reserve);
  281. return skb;
  282. }
  283. static int netpoll_owner_active(struct net_device *dev)
  284. {
  285. struct napi_struct *napi;
  286. list_for_each_entry(napi, &dev->napi_list, dev_list) {
  287. if (napi->poll_owner == smp_processor_id())
  288. return 1;
  289. }
  290. return 0;
  291. }
  292. /* call with IRQ disabled */
  293. void netpoll_send_skb_on_dev(struct netpoll *np, struct sk_buff *skb,
  294. struct net_device *dev)
  295. {
  296. int status = NETDEV_TX_BUSY;
  297. unsigned long tries;
  298. const struct net_device_ops *ops = dev->netdev_ops;
  299. /* It is up to the caller to keep npinfo alive. */
  300. struct netpoll_info *npinfo;
  301. WARN_ON_ONCE(!irqs_disabled());
  302. npinfo = rcu_dereference_bh(np->dev->npinfo);
  303. if (!npinfo || !netif_running(dev) || !netif_device_present(dev)) {
  304. __kfree_skb(skb);
  305. return;
  306. }
  307. /* don't get messages out of order, and no recursion */
  308. if (skb_queue_len(&npinfo->txq) == 0 && !netpoll_owner_active(dev)) {
  309. struct netdev_queue *txq;
  310. txq = netdev_pick_tx(dev, skb, NULL);
  311. /* try until next clock tick */
  312. for (tries = jiffies_to_usecs(1)/USEC_PER_POLL;
  313. tries > 0; --tries) {
  314. if (__netif_tx_trylock(txq)) {
  315. if (!netif_xmit_stopped(txq)) {
  316. if (vlan_tx_tag_present(skb) &&
  317. !vlan_hw_offload_capable(netif_skb_features(skb),
  318. skb->vlan_proto)) {
  319. skb = __vlan_put_tag(skb, skb->vlan_proto, vlan_tx_tag_get(skb));
  320. if (unlikely(!skb)) {
  321. /* This is actually a packet drop, but we
  322. * don't want the code at the end of this
  323. * function to try and re-queue a NULL skb.
  324. */
  325. status = NETDEV_TX_OK;
  326. goto unlock_txq;
  327. }
  328. skb->vlan_tci = 0;
  329. }
  330. status = ops->ndo_start_xmit(skb, dev);
  331. if (status == NETDEV_TX_OK)
  332. txq_trans_update(txq);
  333. }
  334. unlock_txq:
  335. __netif_tx_unlock(txq);
  336. if (status == NETDEV_TX_OK)
  337. break;
  338. }
  339. /* tickle device maybe there is some cleanup */
  340. netpoll_poll_dev(np->dev);
  341. udelay(USEC_PER_POLL);
  342. }
  343. WARN_ONCE(!irqs_disabled(),
  344. "netpoll_send_skb_on_dev(): %s enabled interrupts in poll (%pF)\n",
  345. dev->name, ops->ndo_start_xmit);
  346. }
  347. if (status != NETDEV_TX_OK) {
  348. skb_queue_tail(&npinfo->txq, skb);
  349. schedule_delayed_work(&npinfo->tx_work,0);
  350. }
  351. }
  352. EXPORT_SYMBOL(netpoll_send_skb_on_dev);
  353. void netpoll_send_udp(struct netpoll *np, const char *msg, int len)
  354. {
  355. int total_len, ip_len, udp_len;
  356. struct sk_buff *skb;
  357. struct udphdr *udph;
  358. struct iphdr *iph;
  359. struct ethhdr *eth;
  360. static atomic_t ip_ident;
  361. struct ipv6hdr *ip6h;
  362. udp_len = len + sizeof(*udph);
  363. if (np->ipv6)
  364. ip_len = udp_len + sizeof(*ip6h);
  365. else
  366. ip_len = udp_len + sizeof(*iph);
  367. total_len = ip_len + LL_RESERVED_SPACE(np->dev);
  368. skb = find_skb(np, total_len + np->dev->needed_tailroom,
  369. total_len - len);
  370. if (!skb)
  371. return;
  372. skb_copy_to_linear_data(skb, msg, len);
  373. skb_put(skb, len);
  374. skb_push(skb, sizeof(*udph));
  375. skb_reset_transport_header(skb);
  376. udph = udp_hdr(skb);
  377. udph->source = htons(np->local_port);
  378. udph->dest = htons(np->remote_port);
  379. udph->len = htons(udp_len);
  380. if (np->ipv6) {
  381. udph->check = 0;
  382. udph->check = csum_ipv6_magic(&np->local_ip.in6,
  383. &np->remote_ip.in6,
  384. udp_len, IPPROTO_UDP,
  385. csum_partial(udph, udp_len, 0));
  386. if (udph->check == 0)
  387. udph->check = CSUM_MANGLED_0;
  388. skb_push(skb, sizeof(*ip6h));
  389. skb_reset_network_header(skb);
  390. ip6h = ipv6_hdr(skb);
  391. /* ip6h->version = 6; ip6h->priority = 0; */
  392. put_unaligned(0x60, (unsigned char *)ip6h);
  393. ip6h->flow_lbl[0] = 0;
  394. ip6h->flow_lbl[1] = 0;
  395. ip6h->flow_lbl[2] = 0;
  396. ip6h->payload_len = htons(sizeof(struct udphdr) + len);
  397. ip6h->nexthdr = IPPROTO_UDP;
  398. ip6h->hop_limit = 32;
  399. ip6h->saddr = np->local_ip.in6;
  400. ip6h->daddr = np->remote_ip.in6;
  401. eth = (struct ethhdr *) skb_push(skb, ETH_HLEN);
  402. skb_reset_mac_header(skb);
  403. skb->protocol = eth->h_proto = htons(ETH_P_IPV6);
  404. } else {
  405. udph->check = 0;
  406. udph->check = csum_tcpudp_magic(np->local_ip.ip,
  407. np->remote_ip.ip,
  408. udp_len, IPPROTO_UDP,
  409. csum_partial(udph, udp_len, 0));
  410. if (udph->check == 0)
  411. udph->check = CSUM_MANGLED_0;
  412. skb_push(skb, sizeof(*iph));
  413. skb_reset_network_header(skb);
  414. iph = ip_hdr(skb);
  415. /* iph->version = 4; iph->ihl = 5; */
  416. put_unaligned(0x45, (unsigned char *)iph);
  417. iph->tos = 0;
  418. put_unaligned(htons(ip_len), &(iph->tot_len));
  419. iph->id = htons(atomic_inc_return(&ip_ident));
  420. iph->frag_off = 0;
  421. iph->ttl = 64;
  422. iph->protocol = IPPROTO_UDP;
  423. iph->check = 0;
  424. put_unaligned(np->local_ip.ip, &(iph->saddr));
  425. put_unaligned(np->remote_ip.ip, &(iph->daddr));
  426. iph->check = ip_fast_csum((unsigned char *)iph, iph->ihl);
  427. eth = (struct ethhdr *) skb_push(skb, ETH_HLEN);
  428. skb_reset_mac_header(skb);
  429. skb->protocol = eth->h_proto = htons(ETH_P_IP);
  430. }
  431. ether_addr_copy(eth->h_source, np->dev->dev_addr);
  432. ether_addr_copy(eth->h_dest, np->remote_mac);
  433. skb->dev = np->dev;
  434. netpoll_send_skb(np, skb);
  435. }
  436. EXPORT_SYMBOL(netpoll_send_udp);
  437. static void netpoll_neigh_reply(struct sk_buff *skb, struct netpoll_info *npinfo)
  438. {
  439. int size, type = ARPOP_REPLY;
  440. __be32 sip, tip;
  441. unsigned char *sha;
  442. struct sk_buff *send_skb;
  443. struct netpoll *np, *tmp;
  444. unsigned long flags;
  445. int hlen, tlen;
  446. int hits = 0, proto;
  447. if (list_empty(&npinfo->rx_np))
  448. return;
  449. /* Before checking the packet, we do some early
  450. inspection whether this is interesting at all */
  451. spin_lock_irqsave(&npinfo->rx_lock, flags);
  452. list_for_each_entry_safe(np, tmp, &npinfo->rx_np, rx) {
  453. if (np->dev == skb->dev)
  454. hits++;
  455. }
  456. spin_unlock_irqrestore(&npinfo->rx_lock, flags);
  457. /* No netpoll struct is using this dev */
  458. if (!hits)
  459. return;
  460. proto = ntohs(eth_hdr(skb)->h_proto);
  461. if (proto == ETH_P_ARP) {
  462. struct arphdr *arp;
  463. unsigned char *arp_ptr;
  464. /* No arp on this interface */
  465. if (skb->dev->flags & IFF_NOARP)
  466. return;
  467. if (!pskb_may_pull(skb, arp_hdr_len(skb->dev)))
  468. return;
  469. skb_reset_network_header(skb);
  470. skb_reset_transport_header(skb);
  471. arp = arp_hdr(skb);
  472. if ((arp->ar_hrd != htons(ARPHRD_ETHER) &&
  473. arp->ar_hrd != htons(ARPHRD_IEEE802)) ||
  474. arp->ar_pro != htons(ETH_P_IP) ||
  475. arp->ar_op != htons(ARPOP_REQUEST))
  476. return;
  477. arp_ptr = (unsigned char *)(arp+1);
  478. /* save the location of the src hw addr */
  479. sha = arp_ptr;
  480. arp_ptr += skb->dev->addr_len;
  481. memcpy(&sip, arp_ptr, 4);
  482. arp_ptr += 4;
  483. /* If we actually cared about dst hw addr,
  484. it would get copied here */
  485. arp_ptr += skb->dev->addr_len;
  486. memcpy(&tip, arp_ptr, 4);
  487. /* Should we ignore arp? */
  488. if (ipv4_is_loopback(tip) || ipv4_is_multicast(tip))
  489. return;
  490. size = arp_hdr_len(skb->dev);
  491. spin_lock_irqsave(&npinfo->rx_lock, flags);
  492. list_for_each_entry_safe(np, tmp, &npinfo->rx_np, rx) {
  493. if (tip != np->local_ip.ip)
  494. continue;
  495. hlen = LL_RESERVED_SPACE(np->dev);
  496. tlen = np->dev->needed_tailroom;
  497. send_skb = find_skb(np, size + hlen + tlen, hlen);
  498. if (!send_skb)
  499. continue;
  500. skb_reset_network_header(send_skb);
  501. arp = (struct arphdr *) skb_put(send_skb, size);
  502. send_skb->dev = skb->dev;
  503. send_skb->protocol = htons(ETH_P_ARP);
  504. /* Fill the device header for the ARP frame */
  505. if (dev_hard_header(send_skb, skb->dev, ETH_P_ARP,
  506. sha, np->dev->dev_addr,
  507. send_skb->len) < 0) {
  508. kfree_skb(send_skb);
  509. continue;
  510. }
  511. /*
  512. * Fill out the arp protocol part.
  513. *
  514. * we only support ethernet device type,
  515. * which (according to RFC 1390) should
  516. * always equal 1 (Ethernet).
  517. */
  518. arp->ar_hrd = htons(np->dev->type);
  519. arp->ar_pro = htons(ETH_P_IP);
  520. arp->ar_hln = np->dev->addr_len;
  521. arp->ar_pln = 4;
  522. arp->ar_op = htons(type);
  523. arp_ptr = (unsigned char *)(arp + 1);
  524. memcpy(arp_ptr, np->dev->dev_addr, np->dev->addr_len);
  525. arp_ptr += np->dev->addr_len;
  526. memcpy(arp_ptr, &tip, 4);
  527. arp_ptr += 4;
  528. memcpy(arp_ptr, sha, np->dev->addr_len);
  529. arp_ptr += np->dev->addr_len;
  530. memcpy(arp_ptr, &sip, 4);
  531. netpoll_send_skb(np, send_skb);
  532. /* If there are several rx_skb_hooks for the same
  533. * address we're fine by sending a single reply
  534. */
  535. break;
  536. }
  537. spin_unlock_irqrestore(&npinfo->rx_lock, flags);
  538. } else if( proto == ETH_P_IPV6) {
  539. #if IS_ENABLED(CONFIG_IPV6)
  540. struct nd_msg *msg;
  541. u8 *lladdr = NULL;
  542. struct ipv6hdr *hdr;
  543. struct icmp6hdr *icmp6h;
  544. const struct in6_addr *saddr;
  545. const struct in6_addr *daddr;
  546. struct inet6_dev *in6_dev = NULL;
  547. struct in6_addr *target;
  548. in6_dev = in6_dev_get(skb->dev);
  549. if (!in6_dev || !in6_dev->cnf.accept_ra)
  550. return;
  551. if (!pskb_may_pull(skb, skb->len))
  552. return;
  553. msg = (struct nd_msg *)skb_transport_header(skb);
  554. __skb_push(skb, skb->data - skb_transport_header(skb));
  555. if (ipv6_hdr(skb)->hop_limit != 255)
  556. return;
  557. if (msg->icmph.icmp6_code != 0)
  558. return;
  559. if (msg->icmph.icmp6_type != NDISC_NEIGHBOUR_SOLICITATION)
  560. return;
  561. saddr = &ipv6_hdr(skb)->saddr;
  562. daddr = &ipv6_hdr(skb)->daddr;
  563. size = sizeof(struct icmp6hdr) + sizeof(struct in6_addr);
  564. spin_lock_irqsave(&npinfo->rx_lock, flags);
  565. list_for_each_entry_safe(np, tmp, &npinfo->rx_np, rx) {
  566. if (!ipv6_addr_equal(daddr, &np->local_ip.in6))
  567. continue;
  568. hlen = LL_RESERVED_SPACE(np->dev);
  569. tlen = np->dev->needed_tailroom;
  570. send_skb = find_skb(np, size + hlen + tlen, hlen);
  571. if (!send_skb)
  572. continue;
  573. send_skb->protocol = htons(ETH_P_IPV6);
  574. send_skb->dev = skb->dev;
  575. skb_reset_network_header(send_skb);
  576. hdr = (struct ipv6hdr *) skb_put(send_skb, sizeof(struct ipv6hdr));
  577. *(__be32*)hdr = htonl(0x60000000);
  578. hdr->payload_len = htons(size);
  579. hdr->nexthdr = IPPROTO_ICMPV6;
  580. hdr->hop_limit = 255;
  581. hdr->saddr = *saddr;
  582. hdr->daddr = *daddr;
  583. icmp6h = (struct icmp6hdr *) skb_put(send_skb, sizeof(struct icmp6hdr));
  584. icmp6h->icmp6_type = NDISC_NEIGHBOUR_ADVERTISEMENT;
  585. icmp6h->icmp6_router = 0;
  586. icmp6h->icmp6_solicited = 1;
  587. target = (struct in6_addr *) skb_put(send_skb, sizeof(struct in6_addr));
  588. *target = msg->target;
  589. icmp6h->icmp6_cksum = csum_ipv6_magic(saddr, daddr, size,
  590. IPPROTO_ICMPV6,
  591. csum_partial(icmp6h,
  592. size, 0));
  593. if (dev_hard_header(send_skb, skb->dev, ETH_P_IPV6,
  594. lladdr, np->dev->dev_addr,
  595. send_skb->len) < 0) {
  596. kfree_skb(send_skb);
  597. continue;
  598. }
  599. netpoll_send_skb(np, send_skb);
  600. /* If there are several rx_skb_hooks for the same
  601. * address, we're fine by sending a single reply
  602. */
  603. break;
  604. }
  605. spin_unlock_irqrestore(&npinfo->rx_lock, flags);
  606. #endif
  607. }
  608. }
  609. static bool pkt_is_ns(struct sk_buff *skb)
  610. {
  611. struct nd_msg *msg;
  612. struct ipv6hdr *hdr;
  613. if (skb->protocol != htons(ETH_P_ARP))
  614. return false;
  615. if (!pskb_may_pull(skb, sizeof(struct ipv6hdr) + sizeof(struct nd_msg)))
  616. return false;
  617. msg = (struct nd_msg *)skb_transport_header(skb);
  618. __skb_push(skb, skb->data - skb_transport_header(skb));
  619. hdr = ipv6_hdr(skb);
  620. if (hdr->nexthdr != IPPROTO_ICMPV6)
  621. return false;
  622. if (hdr->hop_limit != 255)
  623. return false;
  624. if (msg->icmph.icmp6_code != 0)
  625. return false;
  626. if (msg->icmph.icmp6_type != NDISC_NEIGHBOUR_SOLICITATION)
  627. return false;
  628. return true;
  629. }
  630. int __netpoll_rx(struct sk_buff *skb, struct netpoll_info *npinfo)
  631. {
  632. int proto, len, ulen, data_len;
  633. int hits = 0, offset;
  634. const struct iphdr *iph;
  635. struct udphdr *uh;
  636. struct netpoll *np, *tmp;
  637. uint16_t source;
  638. if (list_empty(&npinfo->rx_np))
  639. goto out;
  640. if (skb->dev->type != ARPHRD_ETHER)
  641. goto out;
  642. /* check if netpoll clients need ARP */
  643. if (skb->protocol == htons(ETH_P_ARP) && atomic_read(&trapped)) {
  644. skb_queue_tail(&npinfo->neigh_tx, skb);
  645. return 1;
  646. } else if (pkt_is_ns(skb) && atomic_read(&trapped)) {
  647. skb_queue_tail(&npinfo->neigh_tx, skb);
  648. return 1;
  649. }
  650. if (skb->protocol == cpu_to_be16(ETH_P_8021Q)) {
  651. skb = vlan_untag(skb);
  652. if (unlikely(!skb))
  653. goto out;
  654. }
  655. proto = ntohs(eth_hdr(skb)->h_proto);
  656. if (proto != ETH_P_IP && proto != ETH_P_IPV6)
  657. goto out;
  658. if (skb->pkt_type == PACKET_OTHERHOST)
  659. goto out;
  660. if (skb_shared(skb))
  661. goto out;
  662. if (proto == ETH_P_IP) {
  663. if (!pskb_may_pull(skb, sizeof(struct iphdr)))
  664. goto out;
  665. iph = (struct iphdr *)skb->data;
  666. if (iph->ihl < 5 || iph->version != 4)
  667. goto out;
  668. if (!pskb_may_pull(skb, iph->ihl*4))
  669. goto out;
  670. iph = (struct iphdr *)skb->data;
  671. if (ip_fast_csum((u8 *)iph, iph->ihl) != 0)
  672. goto out;
  673. len = ntohs(iph->tot_len);
  674. if (skb->len < len || len < iph->ihl*4)
  675. goto out;
  676. /*
  677. * Our transport medium may have padded the buffer out.
  678. * Now We trim to the true length of the frame.
  679. */
  680. if (pskb_trim_rcsum(skb, len))
  681. goto out;
  682. iph = (struct iphdr *)skb->data;
  683. if (iph->protocol != IPPROTO_UDP)
  684. goto out;
  685. len -= iph->ihl*4;
  686. uh = (struct udphdr *)(((char *)iph) + iph->ihl*4);
  687. offset = (unsigned char *)(uh + 1) - skb->data;
  688. ulen = ntohs(uh->len);
  689. data_len = skb->len - offset;
  690. source = ntohs(uh->source);
  691. if (ulen != len)
  692. goto out;
  693. if (checksum_udp(skb, uh, ulen, iph->saddr, iph->daddr))
  694. goto out;
  695. list_for_each_entry_safe(np, tmp, &npinfo->rx_np, rx) {
  696. if (np->local_ip.ip && np->local_ip.ip != iph->daddr)
  697. continue;
  698. if (np->remote_ip.ip && np->remote_ip.ip != iph->saddr)
  699. continue;
  700. if (np->local_port && np->local_port != ntohs(uh->dest))
  701. continue;
  702. np->rx_skb_hook(np, source, skb, offset, data_len);
  703. hits++;
  704. }
  705. } else {
  706. #if IS_ENABLED(CONFIG_IPV6)
  707. const struct ipv6hdr *ip6h;
  708. if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
  709. goto out;
  710. ip6h = (struct ipv6hdr *)skb->data;
  711. if (ip6h->version != 6)
  712. goto out;
  713. len = ntohs(ip6h->payload_len);
  714. if (!len)
  715. goto out;
  716. if (len + sizeof(struct ipv6hdr) > skb->len)
  717. goto out;
  718. if (pskb_trim_rcsum(skb, len + sizeof(struct ipv6hdr)))
  719. goto out;
  720. ip6h = ipv6_hdr(skb);
  721. if (!pskb_may_pull(skb, sizeof(struct udphdr)))
  722. goto out;
  723. uh = udp_hdr(skb);
  724. offset = (unsigned char *)(uh + 1) - skb->data;
  725. ulen = ntohs(uh->len);
  726. data_len = skb->len - offset;
  727. source = ntohs(uh->source);
  728. if (ulen != skb->len)
  729. goto out;
  730. if (udp6_csum_init(skb, uh, IPPROTO_UDP))
  731. goto out;
  732. list_for_each_entry_safe(np, tmp, &npinfo->rx_np, rx) {
  733. if (!ipv6_addr_equal(&np->local_ip.in6, &ip6h->daddr))
  734. continue;
  735. if (!ipv6_addr_equal(&np->remote_ip.in6, &ip6h->saddr))
  736. continue;
  737. if (np->local_port && np->local_port != ntohs(uh->dest))
  738. continue;
  739. np->rx_skb_hook(np, source, skb, offset, data_len);
  740. hits++;
  741. }
  742. #endif
  743. }
  744. if (!hits)
  745. goto out;
  746. kfree_skb(skb);
  747. return 1;
  748. out:
  749. if (atomic_read(&trapped)) {
  750. kfree_skb(skb);
  751. return 1;
  752. }
  753. return 0;
  754. }
  755. void netpoll_print_options(struct netpoll *np)
  756. {
  757. np_info(np, "local port %d\n", np->local_port);
  758. if (np->ipv6)
  759. np_info(np, "local IPv6 address %pI6c\n", &np->local_ip.in6);
  760. else
  761. np_info(np, "local IPv4 address %pI4\n", &np->local_ip.ip);
  762. np_info(np, "interface '%s'\n", np->dev_name);
  763. np_info(np, "remote port %d\n", np->remote_port);
  764. if (np->ipv6)
  765. np_info(np, "remote IPv6 address %pI6c\n", &np->remote_ip.in6);
  766. else
  767. np_info(np, "remote IPv4 address %pI4\n", &np->remote_ip.ip);
  768. np_info(np, "remote ethernet address %pM\n", np->remote_mac);
  769. }
  770. EXPORT_SYMBOL(netpoll_print_options);
  771. static int netpoll_parse_ip_addr(const char *str, union inet_addr *addr)
  772. {
  773. const char *end;
  774. if (!strchr(str, ':') &&
  775. in4_pton(str, -1, (void *)addr, -1, &end) > 0) {
  776. if (!*end)
  777. return 0;
  778. }
  779. if (in6_pton(str, -1, addr->in6.s6_addr, -1, &end) > 0) {
  780. #if IS_ENABLED(CONFIG_IPV6)
  781. if (!*end)
  782. return 1;
  783. #else
  784. return -1;
  785. #endif
  786. }
  787. return -1;
  788. }
  789. int netpoll_parse_options(struct netpoll *np, char *opt)
  790. {
  791. char *cur=opt, *delim;
  792. int ipv6;
  793. if (*cur != '@') {
  794. if ((delim = strchr(cur, '@')) == NULL)
  795. goto parse_failed;
  796. *delim = 0;
  797. if (kstrtou16(cur, 10, &np->local_port))
  798. goto parse_failed;
  799. cur = delim;
  800. }
  801. cur++;
  802. if (*cur != '/') {
  803. if ((delim = strchr(cur, '/')) == NULL)
  804. goto parse_failed;
  805. *delim = 0;
  806. ipv6 = netpoll_parse_ip_addr(cur, &np->local_ip);
  807. if (ipv6 < 0)
  808. goto parse_failed;
  809. else
  810. np->ipv6 = (bool)ipv6;
  811. cur = delim;
  812. }
  813. cur++;
  814. if (*cur != ',') {
  815. /* parse out dev name */
  816. if ((delim = strchr(cur, ',')) == NULL)
  817. goto parse_failed;
  818. *delim = 0;
  819. strlcpy(np->dev_name, cur, sizeof(np->dev_name));
  820. cur = delim;
  821. }
  822. cur++;
  823. if (*cur != '@') {
  824. /* dst port */
  825. if ((delim = strchr(cur, '@')) == NULL)
  826. goto parse_failed;
  827. *delim = 0;
  828. if (*cur == ' ' || *cur == '\t')
  829. np_info(np, "warning: whitespace is not allowed\n");
  830. if (kstrtou16(cur, 10, &np->remote_port))
  831. goto parse_failed;
  832. cur = delim;
  833. }
  834. cur++;
  835. /* dst ip */
  836. if ((delim = strchr(cur, '/')) == NULL)
  837. goto parse_failed;
  838. *delim = 0;
  839. ipv6 = netpoll_parse_ip_addr(cur, &np->remote_ip);
  840. if (ipv6 < 0)
  841. goto parse_failed;
  842. else if (np->ipv6 != (bool)ipv6)
  843. goto parse_failed;
  844. else
  845. np->ipv6 = (bool)ipv6;
  846. cur = delim + 1;
  847. if (*cur != 0) {
  848. /* MAC address */
  849. if (!mac_pton(cur, np->remote_mac))
  850. goto parse_failed;
  851. }
  852. netpoll_print_options(np);
  853. return 0;
  854. parse_failed:
  855. np_info(np, "couldn't parse config at '%s'!\n", cur);
  856. return -1;
  857. }
  858. EXPORT_SYMBOL(netpoll_parse_options);
  859. int __netpoll_setup(struct netpoll *np, struct net_device *ndev, gfp_t gfp)
  860. {
  861. struct netpoll_info *npinfo;
  862. const struct net_device_ops *ops;
  863. unsigned long flags;
  864. int err;
  865. np->dev = ndev;
  866. strlcpy(np->dev_name, ndev->name, IFNAMSIZ);
  867. INIT_WORK(&np->cleanup_work, netpoll_async_cleanup);
  868. if ((ndev->priv_flags & IFF_DISABLE_NETPOLL) ||
  869. !ndev->netdev_ops->ndo_poll_controller) {
  870. np_err(np, "%s doesn't support polling, aborting\n",
  871. np->dev_name);
  872. err = -ENOTSUPP;
  873. goto out;
  874. }
  875. if (!ndev->npinfo) {
  876. npinfo = kmalloc(sizeof(*npinfo), gfp);
  877. if (!npinfo) {
  878. err = -ENOMEM;
  879. goto out;
  880. }
  881. npinfo->rx_flags = 0;
  882. INIT_LIST_HEAD(&npinfo->rx_np);
  883. spin_lock_init(&npinfo->rx_lock);
  884. sema_init(&npinfo->dev_lock, 1);
  885. skb_queue_head_init(&npinfo->neigh_tx);
  886. skb_queue_head_init(&npinfo->txq);
  887. INIT_DELAYED_WORK(&npinfo->tx_work, queue_process);
  888. atomic_set(&npinfo->refcnt, 1);
  889. ops = np->dev->netdev_ops;
  890. if (ops->ndo_netpoll_setup) {
  891. err = ops->ndo_netpoll_setup(ndev, npinfo, gfp);
  892. if (err)
  893. goto free_npinfo;
  894. }
  895. } else {
  896. npinfo = rtnl_dereference(ndev->npinfo);
  897. atomic_inc(&npinfo->refcnt);
  898. }
  899. npinfo->netpoll = np;
  900. if (np->rx_skb_hook) {
  901. spin_lock_irqsave(&npinfo->rx_lock, flags);
  902. npinfo->rx_flags |= NETPOLL_RX_ENABLED;
  903. list_add_tail(&np->rx, &npinfo->rx_np);
  904. spin_unlock_irqrestore(&npinfo->rx_lock, flags);
  905. }
  906. /* last thing to do is link it to the net device structure */
  907. rcu_assign_pointer(ndev->npinfo, npinfo);
  908. return 0;
  909. free_npinfo:
  910. kfree(npinfo);
  911. out:
  912. return err;
  913. }
  914. EXPORT_SYMBOL_GPL(__netpoll_setup);
  915. int netpoll_setup(struct netpoll *np)
  916. {
  917. struct net_device *ndev = NULL;
  918. struct in_device *in_dev;
  919. int err;
  920. rtnl_lock();
  921. if (np->dev_name) {
  922. struct net *net = current->nsproxy->net_ns;
  923. ndev = __dev_get_by_name(net, np->dev_name);
  924. }
  925. if (!ndev) {
  926. np_err(np, "%s doesn't exist, aborting\n", np->dev_name);
  927. err = -ENODEV;
  928. goto unlock;
  929. }
  930. dev_hold(ndev);
  931. if (netdev_master_upper_dev_get(ndev)) {
  932. np_err(np, "%s is a slave device, aborting\n", np->dev_name);
  933. err = -EBUSY;
  934. goto put;
  935. }
  936. if (!netif_running(ndev)) {
  937. unsigned long atmost, atleast;
  938. np_info(np, "device %s not up yet, forcing it\n", np->dev_name);
  939. err = dev_open(ndev);
  940. if (err) {
  941. np_err(np, "failed to open %s\n", ndev->name);
  942. goto put;
  943. }
  944. rtnl_unlock();
  945. atleast = jiffies + HZ/10;
  946. atmost = jiffies + carrier_timeout * HZ;
  947. while (!netif_carrier_ok(ndev)) {
  948. if (time_after(jiffies, atmost)) {
  949. np_notice(np, "timeout waiting for carrier\n");
  950. break;
  951. }
  952. msleep(1);
  953. }
  954. /* If carrier appears to come up instantly, we don't
  955. * trust it and pause so that we don't pump all our
  956. * queued console messages into the bitbucket.
  957. */
  958. if (time_before(jiffies, atleast)) {
  959. np_notice(np, "carrier detect appears untrustworthy, waiting 4 seconds\n");
  960. msleep(4000);
  961. }
  962. rtnl_lock();
  963. }
  964. if (!np->local_ip.ip) {
  965. if (!np->ipv6) {
  966. in_dev = __in_dev_get_rtnl(ndev);
  967. if (!in_dev || !in_dev->ifa_list) {
  968. np_err(np, "no IP address for %s, aborting\n",
  969. np->dev_name);
  970. err = -EDESTADDRREQ;
  971. goto put;
  972. }
  973. np->local_ip.ip = in_dev->ifa_list->ifa_local;
  974. np_info(np, "local IP %pI4\n", &np->local_ip.ip);
  975. } else {
  976. #if IS_ENABLED(CONFIG_IPV6)
  977. struct inet6_dev *idev;
  978. err = -EDESTADDRREQ;
  979. idev = __in6_dev_get(ndev);
  980. if (idev) {
  981. struct inet6_ifaddr *ifp;
  982. read_lock_bh(&idev->lock);
  983. list_for_each_entry(ifp, &idev->addr_list, if_list) {
  984. if (ipv6_addr_type(&ifp->addr) & IPV6_ADDR_LINKLOCAL)
  985. continue;
  986. np->local_ip.in6 = ifp->addr;
  987. err = 0;
  988. break;
  989. }
  990. read_unlock_bh(&idev->lock);
  991. }
  992. if (err) {
  993. np_err(np, "no IPv6 address for %s, aborting\n",
  994. np->dev_name);
  995. goto put;
  996. } else
  997. np_info(np, "local IPv6 %pI6c\n", &np->local_ip.in6);
  998. #else
  999. np_err(np, "IPv6 is not supported %s, aborting\n",
  1000. np->dev_name);
  1001. err = -EINVAL;
  1002. goto put;
  1003. #endif
  1004. }
  1005. }
  1006. /* fill up the skb queue */
  1007. refill_skbs();
  1008. err = __netpoll_setup(np, ndev, GFP_KERNEL);
  1009. if (err)
  1010. goto put;
  1011. rtnl_unlock();
  1012. return 0;
  1013. put:
  1014. dev_put(ndev);
  1015. unlock:
  1016. rtnl_unlock();
  1017. return err;
  1018. }
  1019. EXPORT_SYMBOL(netpoll_setup);
  1020. static int __init netpoll_init(void)
  1021. {
  1022. skb_queue_head_init(&skb_pool);
  1023. return 0;
  1024. }
  1025. core_initcall(netpoll_init);
  1026. static void rcu_cleanup_netpoll_info(struct rcu_head *rcu_head)
  1027. {
  1028. struct netpoll_info *npinfo =
  1029. container_of(rcu_head, struct netpoll_info, rcu);
  1030. skb_queue_purge(&npinfo->neigh_tx);
  1031. skb_queue_purge(&npinfo->txq);
  1032. /* we can't call cancel_delayed_work_sync here, as we are in softirq */
  1033. cancel_delayed_work(&npinfo->tx_work);
  1034. /* clean after last, unfinished work */
  1035. __skb_queue_purge(&npinfo->txq);
  1036. /* now cancel it again */
  1037. cancel_delayed_work(&npinfo->tx_work);
  1038. kfree(npinfo);
  1039. }
  1040. void __netpoll_cleanup(struct netpoll *np)
  1041. {
  1042. struct netpoll_info *npinfo;
  1043. unsigned long flags;
  1044. /* rtnl_dereference would be preferable here but
  1045. * rcu_cleanup_netpoll path can put us in here safely without
  1046. * holding the rtnl, so plain rcu_dereference it is
  1047. */
  1048. npinfo = rtnl_dereference(np->dev->npinfo);
  1049. if (!npinfo)
  1050. return;
  1051. if (!list_empty(&npinfo->rx_np)) {
  1052. spin_lock_irqsave(&npinfo->rx_lock, flags);
  1053. list_del(&np->rx);
  1054. if (list_empty(&npinfo->rx_np))
  1055. npinfo->rx_flags &= ~NETPOLL_RX_ENABLED;
  1056. spin_unlock_irqrestore(&npinfo->rx_lock, flags);
  1057. }
  1058. synchronize_srcu(&netpoll_srcu);
  1059. if (atomic_dec_and_test(&npinfo->refcnt)) {
  1060. const struct net_device_ops *ops;
  1061. ops = np->dev->netdev_ops;
  1062. if (ops->ndo_netpoll_cleanup)
  1063. ops->ndo_netpoll_cleanup(np->dev);
  1064. rcu_assign_pointer(np->dev->npinfo, NULL);
  1065. call_rcu_bh(&npinfo->rcu, rcu_cleanup_netpoll_info);
  1066. }
  1067. }
  1068. EXPORT_SYMBOL_GPL(__netpoll_cleanup);
  1069. static void netpoll_async_cleanup(struct work_struct *work)
  1070. {
  1071. struct netpoll *np = container_of(work, struct netpoll, cleanup_work);
  1072. rtnl_lock();
  1073. __netpoll_cleanup(np);
  1074. rtnl_unlock();
  1075. kfree(np);
  1076. }
  1077. void __netpoll_free_async(struct netpoll *np)
  1078. {
  1079. schedule_work(&np->cleanup_work);
  1080. }
  1081. EXPORT_SYMBOL_GPL(__netpoll_free_async);
  1082. void netpoll_cleanup(struct netpoll *np)
  1083. {
  1084. rtnl_lock();
  1085. if (!np->dev)
  1086. goto out;
  1087. __netpoll_cleanup(np);
  1088. dev_put(np->dev);
  1089. np->dev = NULL;
  1090. out:
  1091. rtnl_unlock();
  1092. }
  1093. EXPORT_SYMBOL(netpoll_cleanup);
  1094. int netpoll_trap(void)
  1095. {
  1096. return atomic_read(&trapped);
  1097. }
  1098. EXPORT_SYMBOL(netpoll_trap);
  1099. void netpoll_set_trap(int trap)
  1100. {
  1101. if (trap)
  1102. atomic_inc(&trapped);
  1103. else
  1104. atomic_dec(&trapped);
  1105. }
  1106. EXPORT_SYMBOL(netpoll_set_trap);