netpoll.c 31 KB

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