netpoll.c 31 KB

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