netpoll.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325
  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 napi_struct *napi, int budget)
  123. {
  124. int work;
  125. /* net_rx_action's ->poll() invocations and our's are
  126. * synchronized by this test which is only made while
  127. * holding the napi->poll_lock.
  128. */
  129. if (!test_bit(NAPI_STATE_SCHED, &napi->state))
  130. return budget;
  131. set_bit(NAPI_STATE_NPSVC, &napi->state);
  132. work = napi->poll(napi, budget);
  133. trace_napi_poll(napi);
  134. clear_bit(NAPI_STATE_NPSVC, &napi->state);
  135. return budget - work;
  136. }
  137. static void poll_napi(struct net_device *dev)
  138. {
  139. struct napi_struct *napi;
  140. int budget = 16;
  141. list_for_each_entry(napi, &dev->napi_list, dev_list) {
  142. if (napi->poll_owner != smp_processor_id() &&
  143. spin_trylock(&napi->poll_lock)) {
  144. budget = poll_one_napi(napi, budget);
  145. spin_unlock(&napi->poll_lock);
  146. if (!budget)
  147. break;
  148. }
  149. }
  150. }
  151. static void service_neigh_queue(struct netpoll_info *npi)
  152. {
  153. if (npi) {
  154. struct sk_buff *skb;
  155. while ((skb = skb_dequeue(&npi->neigh_tx)))
  156. netpoll_neigh_reply(skb, npi);
  157. }
  158. }
  159. static void netpoll_poll_dev(struct net_device *dev)
  160. {
  161. const struct net_device_ops *ops;
  162. struct netpoll_info *ni = rcu_dereference_bh(dev->npinfo);
  163. /* Don't do any rx activity if the dev_lock mutex is held
  164. * the dev_open/close paths use this to block netpoll activity
  165. * while changing device state
  166. */
  167. if (down_trylock(&ni->dev_lock))
  168. return;
  169. if (!netif_running(dev)) {
  170. up(&ni->dev_lock);
  171. return;
  172. }
  173. ni->rx_flags |= NETPOLL_RX_DROP;
  174. atomic_inc(&trapped);
  175. ops = dev->netdev_ops;
  176. if (!ops->ndo_poll_controller) {
  177. up(&ni->dev_lock);
  178. return;
  179. }
  180. /* Process pending work on NIC */
  181. ops->ndo_poll_controller(dev);
  182. poll_napi(dev);
  183. atomic_dec(&trapped);
  184. ni->rx_flags &= ~NETPOLL_RX_DROP;
  185. up(&ni->dev_lock);
  186. if (dev->flags & IFF_SLAVE) {
  187. if (ni) {
  188. struct net_device *bond_dev;
  189. struct sk_buff *skb;
  190. struct netpoll_info *bond_ni;
  191. bond_dev = netdev_master_upper_dev_get_rcu(dev);
  192. bond_ni = rcu_dereference_bh(bond_dev->npinfo);
  193. while ((skb = skb_dequeue(&ni->neigh_tx))) {
  194. skb->dev = bond_dev;
  195. skb_queue_tail(&bond_ni->neigh_tx, skb);
  196. }
  197. }
  198. }
  199. service_neigh_queue(ni);
  200. zap_completion_queue();
  201. }
  202. void netpoll_rx_disable(struct net_device *dev)
  203. {
  204. struct netpoll_info *ni;
  205. int idx;
  206. might_sleep();
  207. idx = srcu_read_lock(&netpoll_srcu);
  208. ni = srcu_dereference(dev->npinfo, &netpoll_srcu);
  209. if (ni)
  210. down(&ni->dev_lock);
  211. srcu_read_unlock(&netpoll_srcu, idx);
  212. }
  213. EXPORT_SYMBOL(netpoll_rx_disable);
  214. void netpoll_rx_enable(struct net_device *dev)
  215. {
  216. struct netpoll_info *ni;
  217. rcu_read_lock();
  218. ni = rcu_dereference(dev->npinfo);
  219. if (ni)
  220. up(&ni->dev_lock);
  221. rcu_read_unlock();
  222. }
  223. EXPORT_SYMBOL(netpoll_rx_enable);
  224. static void refill_skbs(void)
  225. {
  226. struct sk_buff *skb;
  227. unsigned long flags;
  228. spin_lock_irqsave(&skb_pool.lock, flags);
  229. while (skb_pool.qlen < MAX_SKBS) {
  230. skb = alloc_skb(MAX_SKB_SIZE, GFP_ATOMIC);
  231. if (!skb)
  232. break;
  233. __skb_queue_tail(&skb_pool, skb);
  234. }
  235. spin_unlock_irqrestore(&skb_pool.lock, flags);
  236. }
  237. static void zap_completion_queue(void)
  238. {
  239. unsigned long flags;
  240. struct softnet_data *sd = &get_cpu_var(softnet_data);
  241. if (sd->completion_queue) {
  242. struct sk_buff *clist;
  243. local_irq_save(flags);
  244. clist = sd->completion_queue;
  245. sd->completion_queue = NULL;
  246. local_irq_restore(flags);
  247. while (clist != NULL) {
  248. struct sk_buff *skb = clist;
  249. clist = clist->next;
  250. if (skb->destructor) {
  251. atomic_inc(&skb->users);
  252. dev_kfree_skb_any(skb); /* put this one back */
  253. } else {
  254. __kfree_skb(skb);
  255. }
  256. }
  257. }
  258. put_cpu_var(softnet_data);
  259. }
  260. static struct sk_buff *find_skb(struct netpoll *np, int len, int reserve)
  261. {
  262. int count = 0;
  263. struct sk_buff *skb;
  264. zap_completion_queue();
  265. refill_skbs();
  266. repeat:
  267. skb = alloc_skb(len, GFP_ATOMIC);
  268. if (!skb)
  269. skb = skb_dequeue(&skb_pool);
  270. if (!skb) {
  271. if (++count < 10) {
  272. netpoll_poll_dev(np->dev);
  273. goto repeat;
  274. }
  275. return NULL;
  276. }
  277. atomic_set(&skb->users, 1);
  278. skb_reserve(skb, reserve);
  279. return skb;
  280. }
  281. static int netpoll_owner_active(struct net_device *dev)
  282. {
  283. struct napi_struct *napi;
  284. list_for_each_entry(napi, &dev->napi_list, dev_list) {
  285. if (napi->poll_owner == smp_processor_id())
  286. return 1;
  287. }
  288. return 0;
  289. }
  290. /* call with IRQ disabled */
  291. void netpoll_send_skb_on_dev(struct netpoll *np, struct sk_buff *skb,
  292. struct net_device *dev)
  293. {
  294. int status = NETDEV_TX_BUSY;
  295. unsigned long tries;
  296. const struct net_device_ops *ops = dev->netdev_ops;
  297. /* It is up to the caller to keep npinfo alive. */
  298. struct netpoll_info *npinfo;
  299. WARN_ON_ONCE(!irqs_disabled());
  300. npinfo = rcu_dereference_bh(np->dev->npinfo);
  301. if (!npinfo || !netif_running(dev) || !netif_device_present(dev)) {
  302. __kfree_skb(skb);
  303. return;
  304. }
  305. /* don't get messages out of order, and no recursion */
  306. if (skb_queue_len(&npinfo->txq) == 0 && !netpoll_owner_active(dev)) {
  307. struct netdev_queue *txq;
  308. txq = netdev_pick_tx(dev, skb, NULL);
  309. /* try until next clock tick */
  310. for (tries = jiffies_to_usecs(1)/USEC_PER_POLL;
  311. tries > 0; --tries) {
  312. if (__netif_tx_trylock(txq)) {
  313. if (!netif_xmit_stopped(txq)) {
  314. if (vlan_tx_tag_present(skb) &&
  315. !vlan_hw_offload_capable(netif_skb_features(skb),
  316. skb->vlan_proto)) {
  317. skb = __vlan_put_tag(skb, skb->vlan_proto, vlan_tx_tag_get(skb));
  318. if (unlikely(!skb)) {
  319. /* This is actually a packet drop, but we
  320. * don't want the code at the end of this
  321. * function to try and re-queue a NULL skb.
  322. */
  323. status = NETDEV_TX_OK;
  324. goto unlock_txq;
  325. }
  326. skb->vlan_tci = 0;
  327. }
  328. status = ops->ndo_start_xmit(skb, dev);
  329. if (status == NETDEV_TX_OK)
  330. txq_trans_update(txq);
  331. }
  332. unlock_txq:
  333. __netif_tx_unlock(txq);
  334. if (status == NETDEV_TX_OK)
  335. break;
  336. }
  337. /* tickle device maybe there is some cleanup */
  338. netpoll_poll_dev(np->dev);
  339. udelay(USEC_PER_POLL);
  340. }
  341. WARN_ONCE(!irqs_disabled(),
  342. "netpoll_send_skb_on_dev(): %s enabled interrupts in poll (%pF)\n",
  343. dev->name, ops->ndo_start_xmit);
  344. }
  345. if (status != NETDEV_TX_OK) {
  346. skb_queue_tail(&npinfo->txq, skb);
  347. schedule_delayed_work(&npinfo->tx_work,0);
  348. }
  349. }
  350. EXPORT_SYMBOL(netpoll_send_skb_on_dev);
  351. void netpoll_send_udp(struct netpoll *np, const char *msg, int len)
  352. {
  353. int total_len, ip_len, udp_len;
  354. struct sk_buff *skb;
  355. struct udphdr *udph;
  356. struct iphdr *iph;
  357. struct ethhdr *eth;
  358. static atomic_t ip_ident;
  359. struct ipv6hdr *ip6h;
  360. udp_len = len + sizeof(*udph);
  361. if (np->ipv6)
  362. ip_len = udp_len + sizeof(*ip6h);
  363. else
  364. ip_len = udp_len + sizeof(*iph);
  365. total_len = ip_len + LL_RESERVED_SPACE(np->dev);
  366. skb = find_skb(np, total_len + np->dev->needed_tailroom,
  367. total_len - len);
  368. if (!skb)
  369. return;
  370. skb_copy_to_linear_data(skb, msg, len);
  371. skb_put(skb, len);
  372. skb_push(skb, sizeof(*udph));
  373. skb_reset_transport_header(skb);
  374. udph = udp_hdr(skb);
  375. udph->source = htons(np->local_port);
  376. udph->dest = htons(np->remote_port);
  377. udph->len = htons(udp_len);
  378. if (np->ipv6) {
  379. udph->check = 0;
  380. udph->check = csum_ipv6_magic(&np->local_ip.in6,
  381. &np->remote_ip.in6,
  382. udp_len, IPPROTO_UDP,
  383. csum_partial(udph, udp_len, 0));
  384. if (udph->check == 0)
  385. udph->check = CSUM_MANGLED_0;
  386. skb_push(skb, sizeof(*ip6h));
  387. skb_reset_network_header(skb);
  388. ip6h = ipv6_hdr(skb);
  389. /* ip6h->version = 6; ip6h->priority = 0; */
  390. put_unaligned(0x60, (unsigned char *)ip6h);
  391. ip6h->flow_lbl[0] = 0;
  392. ip6h->flow_lbl[1] = 0;
  393. ip6h->flow_lbl[2] = 0;
  394. ip6h->payload_len = htons(sizeof(struct udphdr) + len);
  395. ip6h->nexthdr = IPPROTO_UDP;
  396. ip6h->hop_limit = 32;
  397. ip6h->saddr = np->local_ip.in6;
  398. ip6h->daddr = np->remote_ip.in6;
  399. eth = (struct ethhdr *) skb_push(skb, ETH_HLEN);
  400. skb_reset_mac_header(skb);
  401. skb->protocol = eth->h_proto = htons(ETH_P_IPV6);
  402. } else {
  403. udph->check = 0;
  404. udph->check = csum_tcpudp_magic(np->local_ip.ip,
  405. np->remote_ip.ip,
  406. udp_len, IPPROTO_UDP,
  407. csum_partial(udph, udp_len, 0));
  408. if (udph->check == 0)
  409. udph->check = CSUM_MANGLED_0;
  410. skb_push(skb, sizeof(*iph));
  411. skb_reset_network_header(skb);
  412. iph = ip_hdr(skb);
  413. /* iph->version = 4; iph->ihl = 5; */
  414. put_unaligned(0x45, (unsigned char *)iph);
  415. iph->tos = 0;
  416. put_unaligned(htons(ip_len), &(iph->tot_len));
  417. iph->id = htons(atomic_inc_return(&ip_ident));
  418. iph->frag_off = 0;
  419. iph->ttl = 64;
  420. iph->protocol = IPPROTO_UDP;
  421. iph->check = 0;
  422. put_unaligned(np->local_ip.ip, &(iph->saddr));
  423. put_unaligned(np->remote_ip.ip, &(iph->daddr));
  424. iph->check = ip_fast_csum((unsigned char *)iph, iph->ihl);
  425. eth = (struct ethhdr *) skb_push(skb, ETH_HLEN);
  426. skb_reset_mac_header(skb);
  427. skb->protocol = eth->h_proto = htons(ETH_P_IP);
  428. }
  429. ether_addr_copy(eth->h_source, np->dev->dev_addr);
  430. ether_addr_copy(eth->h_dest, np->remote_mac);
  431. skb->dev = np->dev;
  432. netpoll_send_skb(np, skb);
  433. }
  434. EXPORT_SYMBOL(netpoll_send_udp);
  435. static void netpoll_neigh_reply(struct sk_buff *skb, struct netpoll_info *npinfo)
  436. {
  437. int size, type = ARPOP_REPLY;
  438. __be32 sip, tip;
  439. unsigned char *sha;
  440. struct sk_buff *send_skb;
  441. struct netpoll *np, *tmp;
  442. unsigned long flags;
  443. int hlen, tlen;
  444. int hits = 0, proto;
  445. if (list_empty(&npinfo->rx_np))
  446. return;
  447. /* Before checking the packet, we do some early
  448. inspection whether this is interesting at all */
  449. spin_lock_irqsave(&npinfo->rx_lock, flags);
  450. list_for_each_entry_safe(np, tmp, &npinfo->rx_np, rx) {
  451. if (np->dev == skb->dev)
  452. hits++;
  453. }
  454. spin_unlock_irqrestore(&npinfo->rx_lock, flags);
  455. /* No netpoll struct is using this dev */
  456. if (!hits)
  457. return;
  458. proto = ntohs(eth_hdr(skb)->h_proto);
  459. if (proto == ETH_P_ARP) {
  460. struct arphdr *arp;
  461. unsigned char *arp_ptr;
  462. /* No arp on this interface */
  463. if (skb->dev->flags & IFF_NOARP)
  464. return;
  465. if (!pskb_may_pull(skb, arp_hdr_len(skb->dev)))
  466. return;
  467. skb_reset_network_header(skb);
  468. skb_reset_transport_header(skb);
  469. arp = arp_hdr(skb);
  470. if ((arp->ar_hrd != htons(ARPHRD_ETHER) &&
  471. arp->ar_hrd != htons(ARPHRD_IEEE802)) ||
  472. arp->ar_pro != htons(ETH_P_IP) ||
  473. arp->ar_op != htons(ARPOP_REQUEST))
  474. return;
  475. arp_ptr = (unsigned char *)(arp+1);
  476. /* save the location of the src hw addr */
  477. sha = arp_ptr;
  478. arp_ptr += skb->dev->addr_len;
  479. memcpy(&sip, arp_ptr, 4);
  480. arp_ptr += 4;
  481. /* If we actually cared about dst hw addr,
  482. it would get copied here */
  483. arp_ptr += skb->dev->addr_len;
  484. memcpy(&tip, arp_ptr, 4);
  485. /* Should we ignore arp? */
  486. if (ipv4_is_loopback(tip) || ipv4_is_multicast(tip))
  487. return;
  488. size = arp_hdr_len(skb->dev);
  489. spin_lock_irqsave(&npinfo->rx_lock, flags);
  490. list_for_each_entry_safe(np, tmp, &npinfo->rx_np, rx) {
  491. if (tip != np->local_ip.ip)
  492. continue;
  493. hlen = LL_RESERVED_SPACE(np->dev);
  494. tlen = np->dev->needed_tailroom;
  495. send_skb = find_skb(np, size + hlen + tlen, hlen);
  496. if (!send_skb)
  497. continue;
  498. skb_reset_network_header(send_skb);
  499. arp = (struct arphdr *) skb_put(send_skb, size);
  500. send_skb->dev = skb->dev;
  501. send_skb->protocol = htons(ETH_P_ARP);
  502. /* Fill the device header for the ARP frame */
  503. if (dev_hard_header(send_skb, skb->dev, ETH_P_ARP,
  504. sha, np->dev->dev_addr,
  505. send_skb->len) < 0) {
  506. kfree_skb(send_skb);
  507. continue;
  508. }
  509. /*
  510. * Fill out the arp protocol part.
  511. *
  512. * we only support ethernet device type,
  513. * which (according to RFC 1390) should
  514. * always equal 1 (Ethernet).
  515. */
  516. arp->ar_hrd = htons(np->dev->type);
  517. arp->ar_pro = htons(ETH_P_IP);
  518. arp->ar_hln = np->dev->addr_len;
  519. arp->ar_pln = 4;
  520. arp->ar_op = htons(type);
  521. arp_ptr = (unsigned char *)(arp + 1);
  522. memcpy(arp_ptr, np->dev->dev_addr, np->dev->addr_len);
  523. arp_ptr += np->dev->addr_len;
  524. memcpy(arp_ptr, &tip, 4);
  525. arp_ptr += 4;
  526. memcpy(arp_ptr, sha, np->dev->addr_len);
  527. arp_ptr += np->dev->addr_len;
  528. memcpy(arp_ptr, &sip, 4);
  529. netpoll_send_skb(np, send_skb);
  530. /* If there are several rx_skb_hooks for the same
  531. * address we're fine by sending a single reply
  532. */
  533. break;
  534. }
  535. spin_unlock_irqrestore(&npinfo->rx_lock, flags);
  536. } else if( proto == ETH_P_IPV6) {
  537. #if IS_ENABLED(CONFIG_IPV6)
  538. struct nd_msg *msg;
  539. u8 *lladdr = NULL;
  540. struct ipv6hdr *hdr;
  541. struct icmp6hdr *icmp6h;
  542. const struct in6_addr *saddr;
  543. const struct in6_addr *daddr;
  544. struct inet6_dev *in6_dev = NULL;
  545. struct in6_addr *target;
  546. in6_dev = in6_dev_get(skb->dev);
  547. if (!in6_dev || !in6_dev->cnf.accept_ra)
  548. return;
  549. if (!pskb_may_pull(skb, skb->len))
  550. return;
  551. msg = (struct nd_msg *)skb_transport_header(skb);
  552. __skb_push(skb, skb->data - skb_transport_header(skb));
  553. if (ipv6_hdr(skb)->hop_limit != 255)
  554. return;
  555. if (msg->icmph.icmp6_code != 0)
  556. return;
  557. if (msg->icmph.icmp6_type != NDISC_NEIGHBOUR_SOLICITATION)
  558. return;
  559. saddr = &ipv6_hdr(skb)->saddr;
  560. daddr = &ipv6_hdr(skb)->daddr;
  561. size = sizeof(struct icmp6hdr) + sizeof(struct in6_addr);
  562. spin_lock_irqsave(&npinfo->rx_lock, flags);
  563. list_for_each_entry_safe(np, tmp, &npinfo->rx_np, rx) {
  564. if (!ipv6_addr_equal(daddr, &np->local_ip.in6))
  565. continue;
  566. hlen = LL_RESERVED_SPACE(np->dev);
  567. tlen = np->dev->needed_tailroom;
  568. send_skb = find_skb(np, size + hlen + tlen, hlen);
  569. if (!send_skb)
  570. continue;
  571. send_skb->protocol = htons(ETH_P_IPV6);
  572. send_skb->dev = skb->dev;
  573. skb_reset_network_header(send_skb);
  574. hdr = (struct ipv6hdr *) skb_put(send_skb, sizeof(struct ipv6hdr));
  575. *(__be32*)hdr = htonl(0x60000000);
  576. hdr->payload_len = htons(size);
  577. hdr->nexthdr = IPPROTO_ICMPV6;
  578. hdr->hop_limit = 255;
  579. hdr->saddr = *saddr;
  580. hdr->daddr = *daddr;
  581. icmp6h = (struct icmp6hdr *) skb_put(send_skb, sizeof(struct icmp6hdr));
  582. icmp6h->icmp6_type = NDISC_NEIGHBOUR_ADVERTISEMENT;
  583. icmp6h->icmp6_router = 0;
  584. icmp6h->icmp6_solicited = 1;
  585. target = (struct in6_addr *) skb_put(send_skb, sizeof(struct in6_addr));
  586. *target = msg->target;
  587. icmp6h->icmp6_cksum = csum_ipv6_magic(saddr, daddr, size,
  588. IPPROTO_ICMPV6,
  589. csum_partial(icmp6h,
  590. size, 0));
  591. if (dev_hard_header(send_skb, skb->dev, ETH_P_IPV6,
  592. lladdr, np->dev->dev_addr,
  593. send_skb->len) < 0) {
  594. kfree_skb(send_skb);
  595. continue;
  596. }
  597. netpoll_send_skb(np, send_skb);
  598. /* If there are several rx_skb_hooks for the same
  599. * address, we're fine by sending a single reply
  600. */
  601. break;
  602. }
  603. spin_unlock_irqrestore(&npinfo->rx_lock, flags);
  604. #endif
  605. }
  606. }
  607. static bool pkt_is_ns(struct sk_buff *skb)
  608. {
  609. struct nd_msg *msg;
  610. struct ipv6hdr *hdr;
  611. if (skb->protocol != htons(ETH_P_ARP))
  612. return false;
  613. if (!pskb_may_pull(skb, sizeof(struct ipv6hdr) + sizeof(struct nd_msg)))
  614. return false;
  615. msg = (struct nd_msg *)skb_transport_header(skb);
  616. __skb_push(skb, skb->data - skb_transport_header(skb));
  617. hdr = ipv6_hdr(skb);
  618. if (hdr->nexthdr != IPPROTO_ICMPV6)
  619. return false;
  620. if (hdr->hop_limit != 255)
  621. return false;
  622. if (msg->icmph.icmp6_code != 0)
  623. return false;
  624. if (msg->icmph.icmp6_type != NDISC_NEIGHBOUR_SOLICITATION)
  625. return false;
  626. return true;
  627. }
  628. int __netpoll_rx(struct sk_buff *skb, struct netpoll_info *npinfo)
  629. {
  630. int proto, len, ulen, data_len;
  631. int hits = 0, offset;
  632. const struct iphdr *iph;
  633. struct udphdr *uh;
  634. struct netpoll *np, *tmp;
  635. uint16_t source;
  636. if (list_empty(&npinfo->rx_np))
  637. goto out;
  638. if (skb->dev->type != ARPHRD_ETHER)
  639. goto out;
  640. /* check if netpoll clients need ARP */
  641. if (skb->protocol == htons(ETH_P_ARP) && atomic_read(&trapped)) {
  642. skb_queue_tail(&npinfo->neigh_tx, skb);
  643. return 1;
  644. } else if (pkt_is_ns(skb) && atomic_read(&trapped)) {
  645. skb_queue_tail(&npinfo->neigh_tx, skb);
  646. return 1;
  647. }
  648. if (skb->protocol == cpu_to_be16(ETH_P_8021Q)) {
  649. skb = vlan_untag(skb);
  650. if (unlikely(!skb))
  651. goto out;
  652. }
  653. proto = ntohs(eth_hdr(skb)->h_proto);
  654. if (proto != ETH_P_IP && proto != ETH_P_IPV6)
  655. goto out;
  656. if (skb->pkt_type == PACKET_OTHERHOST)
  657. goto out;
  658. if (skb_shared(skb))
  659. goto out;
  660. if (proto == ETH_P_IP) {
  661. if (!pskb_may_pull(skb, sizeof(struct iphdr)))
  662. goto out;
  663. iph = (struct iphdr *)skb->data;
  664. if (iph->ihl < 5 || iph->version != 4)
  665. goto out;
  666. if (!pskb_may_pull(skb, iph->ihl*4))
  667. goto out;
  668. iph = (struct iphdr *)skb->data;
  669. if (ip_fast_csum((u8 *)iph, iph->ihl) != 0)
  670. goto out;
  671. len = ntohs(iph->tot_len);
  672. if (skb->len < len || len < iph->ihl*4)
  673. goto out;
  674. /*
  675. * Our transport medium may have padded the buffer out.
  676. * Now We trim to the true length of the frame.
  677. */
  678. if (pskb_trim_rcsum(skb, len))
  679. goto out;
  680. iph = (struct iphdr *)skb->data;
  681. if (iph->protocol != IPPROTO_UDP)
  682. goto out;
  683. len -= iph->ihl*4;
  684. uh = (struct udphdr *)(((char *)iph) + iph->ihl*4);
  685. offset = (unsigned char *)(uh + 1) - skb->data;
  686. ulen = ntohs(uh->len);
  687. data_len = skb->len - offset;
  688. source = ntohs(uh->source);
  689. if (ulen != len)
  690. goto out;
  691. if (checksum_udp(skb, uh, ulen, iph->saddr, iph->daddr))
  692. goto out;
  693. list_for_each_entry_safe(np, tmp, &npinfo->rx_np, rx) {
  694. if (np->local_ip.ip && np->local_ip.ip != iph->daddr)
  695. continue;
  696. if (np->remote_ip.ip && np->remote_ip.ip != iph->saddr)
  697. continue;
  698. if (np->local_port && np->local_port != ntohs(uh->dest))
  699. continue;
  700. np->rx_skb_hook(np, source, skb, offset, data_len);
  701. hits++;
  702. }
  703. } else {
  704. #if IS_ENABLED(CONFIG_IPV6)
  705. const struct ipv6hdr *ip6h;
  706. if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
  707. goto out;
  708. ip6h = (struct ipv6hdr *)skb->data;
  709. if (ip6h->version != 6)
  710. goto out;
  711. len = ntohs(ip6h->payload_len);
  712. if (!len)
  713. goto out;
  714. if (len + sizeof(struct ipv6hdr) > skb->len)
  715. goto out;
  716. if (pskb_trim_rcsum(skb, len + sizeof(struct ipv6hdr)))
  717. goto out;
  718. ip6h = ipv6_hdr(skb);
  719. if (!pskb_may_pull(skb, sizeof(struct udphdr)))
  720. goto out;
  721. uh = udp_hdr(skb);
  722. offset = (unsigned char *)(uh + 1) - skb->data;
  723. ulen = ntohs(uh->len);
  724. data_len = skb->len - offset;
  725. source = ntohs(uh->source);
  726. if (ulen != skb->len)
  727. goto out;
  728. if (udp6_csum_init(skb, uh, IPPROTO_UDP))
  729. goto out;
  730. list_for_each_entry_safe(np, tmp, &npinfo->rx_np, rx) {
  731. if (!ipv6_addr_equal(&np->local_ip.in6, &ip6h->daddr))
  732. continue;
  733. if (!ipv6_addr_equal(&np->remote_ip.in6, &ip6h->saddr))
  734. continue;
  735. if (np->local_port && np->local_port != ntohs(uh->dest))
  736. continue;
  737. np->rx_skb_hook(np, source, skb, offset, data_len);
  738. hits++;
  739. }
  740. #endif
  741. }
  742. if (!hits)
  743. goto out;
  744. kfree_skb(skb);
  745. return 1;
  746. out:
  747. if (atomic_read(&trapped)) {
  748. kfree_skb(skb);
  749. return 1;
  750. }
  751. return 0;
  752. }
  753. void netpoll_print_options(struct netpoll *np)
  754. {
  755. np_info(np, "local port %d\n", np->local_port);
  756. if (np->ipv6)
  757. np_info(np, "local IPv6 address %pI6c\n", &np->local_ip.in6);
  758. else
  759. np_info(np, "local IPv4 address %pI4\n", &np->local_ip.ip);
  760. np_info(np, "interface '%s'\n", np->dev_name);
  761. np_info(np, "remote port %d\n", np->remote_port);
  762. if (np->ipv6)
  763. np_info(np, "remote IPv6 address %pI6c\n", &np->remote_ip.in6);
  764. else
  765. np_info(np, "remote IPv4 address %pI4\n", &np->remote_ip.ip);
  766. np_info(np, "remote ethernet address %pM\n", np->remote_mac);
  767. }
  768. EXPORT_SYMBOL(netpoll_print_options);
  769. static int netpoll_parse_ip_addr(const char *str, union inet_addr *addr)
  770. {
  771. const char *end;
  772. if (!strchr(str, ':') &&
  773. in4_pton(str, -1, (void *)addr, -1, &end) > 0) {
  774. if (!*end)
  775. return 0;
  776. }
  777. if (in6_pton(str, -1, addr->in6.s6_addr, -1, &end) > 0) {
  778. #if IS_ENABLED(CONFIG_IPV6)
  779. if (!*end)
  780. return 1;
  781. #else
  782. return -1;
  783. #endif
  784. }
  785. return -1;
  786. }
  787. int netpoll_parse_options(struct netpoll *np, char *opt)
  788. {
  789. char *cur=opt, *delim;
  790. int ipv6;
  791. bool ipversion_set = false;
  792. if (*cur != '@') {
  793. if ((delim = strchr(cur, '@')) == NULL)
  794. goto parse_failed;
  795. *delim = 0;
  796. if (kstrtou16(cur, 10, &np->local_port))
  797. goto parse_failed;
  798. cur = delim;
  799. }
  800. cur++;
  801. if (*cur != '/') {
  802. ipversion_set = true;
  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 (ipversion_set && 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);