netpoll.c 31 KB

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