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. 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 (!netif_running(dev)) {
  175. mutex_unlock(&ni->dev_lock);
  176. return;
  177. }
  178. ops = dev->netdev_ops;
  179. if (!ops->ndo_poll_controller) {
  180. mutex_unlock(&ni->dev_lock);
  181. return;
  182. }
  183. /* Process pending work on NIC */
  184. ops->ndo_poll_controller(dev);
  185. poll_napi(dev);
  186. mutex_unlock(&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. mutex_lock(&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. mutex_unlock(&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. !(netif_skb_features(skb) & NETIF_F_HW_VLAN_TX)) {
  318. skb = __vlan_put_tag(skb, vlan_tx_tag_get(skb));
  319. if (unlikely(!skb))
  320. break;
  321. skb->vlan_tci = 0;
  322. }
  323. status = ops->ndo_start_xmit(skb, dev);
  324. if (status == NETDEV_TX_OK)
  325. txq_trans_update(txq);
  326. }
  327. __netif_tx_unlock(txq);
  328. if (status == NETDEV_TX_OK)
  329. break;
  330. }
  331. /* tickle device maybe there is some cleanup */
  332. netpoll_poll_dev(np->dev);
  333. udelay(USEC_PER_POLL);
  334. }
  335. WARN_ONCE(!irqs_disabled(),
  336. "netpoll_send_skb_on_dev(): %s enabled interrupts in poll (%pF)\n",
  337. dev->name, ops->ndo_start_xmit);
  338. }
  339. if (status != NETDEV_TX_OK) {
  340. skb_queue_tail(&npinfo->txq, skb);
  341. schedule_delayed_work(&npinfo->tx_work,0);
  342. }
  343. }
  344. EXPORT_SYMBOL(netpoll_send_skb_on_dev);
  345. void netpoll_send_udp(struct netpoll *np, const char *msg, int len)
  346. {
  347. int total_len, ip_len, udp_len;
  348. struct sk_buff *skb;
  349. struct udphdr *udph;
  350. struct iphdr *iph;
  351. struct ethhdr *eth;
  352. static atomic_t ip_ident;
  353. struct ipv6hdr *ip6h;
  354. udp_len = len + sizeof(*udph);
  355. if (np->ipv6)
  356. ip_len = udp_len + sizeof(*ip6h);
  357. else
  358. ip_len = udp_len + sizeof(*iph);
  359. total_len = ip_len + LL_RESERVED_SPACE(np->dev);
  360. skb = find_skb(np, total_len + np->dev->needed_tailroom,
  361. total_len - len);
  362. if (!skb)
  363. return;
  364. skb_copy_to_linear_data(skb, msg, len);
  365. skb_put(skb, len);
  366. skb_push(skb, sizeof(*udph));
  367. skb_reset_transport_header(skb);
  368. udph = udp_hdr(skb);
  369. udph->source = htons(np->local_port);
  370. udph->dest = htons(np->remote_port);
  371. udph->len = htons(udp_len);
  372. if (np->ipv6) {
  373. udph->check = 0;
  374. udph->check = csum_ipv6_magic(&np->local_ip.in6,
  375. &np->remote_ip.in6,
  376. udp_len, IPPROTO_UDP,
  377. csum_partial(udph, udp_len, 0));
  378. if (udph->check == 0)
  379. udph->check = CSUM_MANGLED_0;
  380. skb_push(skb, sizeof(*ip6h));
  381. skb_reset_network_header(skb);
  382. ip6h = ipv6_hdr(skb);
  383. /* ip6h->version = 6; ip6h->priority = 0; */
  384. put_unaligned(0x60, (unsigned char *)ip6h);
  385. ip6h->flow_lbl[0] = 0;
  386. ip6h->flow_lbl[1] = 0;
  387. ip6h->flow_lbl[2] = 0;
  388. ip6h->payload_len = htons(sizeof(struct udphdr) + len);
  389. ip6h->nexthdr = IPPROTO_UDP;
  390. ip6h->hop_limit = 32;
  391. ip6h->saddr = np->local_ip.in6;
  392. ip6h->daddr = np->remote_ip.in6;
  393. eth = (struct ethhdr *) skb_push(skb, ETH_HLEN);
  394. skb_reset_mac_header(skb);
  395. skb->protocol = eth->h_proto = htons(ETH_P_IPV6);
  396. } else {
  397. udph->check = 0;
  398. udph->check = csum_tcpudp_magic(np->local_ip.ip,
  399. np->remote_ip.ip,
  400. udp_len, IPPROTO_UDP,
  401. csum_partial(udph, udp_len, 0));
  402. if (udph->check == 0)
  403. udph->check = CSUM_MANGLED_0;
  404. skb_push(skb, sizeof(*iph));
  405. skb_reset_network_header(skb);
  406. iph = ip_hdr(skb);
  407. /* iph->version = 4; iph->ihl = 5; */
  408. put_unaligned(0x45, (unsigned char *)iph);
  409. iph->tos = 0;
  410. put_unaligned(htons(ip_len), &(iph->tot_len));
  411. iph->id = htons(atomic_inc_return(&ip_ident));
  412. iph->frag_off = 0;
  413. iph->ttl = 64;
  414. iph->protocol = IPPROTO_UDP;
  415. iph->check = 0;
  416. put_unaligned(np->local_ip.ip, &(iph->saddr));
  417. put_unaligned(np->remote_ip.ip, &(iph->daddr));
  418. iph->check = ip_fast_csum((unsigned char *)iph, iph->ihl);
  419. eth = (struct ethhdr *) skb_push(skb, ETH_HLEN);
  420. skb_reset_mac_header(skb);
  421. skb->protocol = eth->h_proto = htons(ETH_P_IP);
  422. }
  423. memcpy(eth->h_source, np->dev->dev_addr, ETH_ALEN);
  424. memcpy(eth->h_dest, np->remote_mac, ETH_ALEN);
  425. skb->dev = np->dev;
  426. netpoll_send_skb(np, skb);
  427. }
  428. EXPORT_SYMBOL(netpoll_send_udp);
  429. static void netpoll_neigh_reply(struct sk_buff *skb, struct netpoll_info *npinfo)
  430. {
  431. int size, type = ARPOP_REPLY;
  432. __be32 sip, tip;
  433. unsigned char *sha;
  434. struct sk_buff *send_skb;
  435. struct netpoll *np, *tmp;
  436. unsigned long flags;
  437. int hlen, tlen;
  438. int hits = 0, proto;
  439. if (list_empty(&npinfo->rx_np))
  440. return;
  441. /* Before checking the packet, we do some early
  442. inspection whether this is interesting at all */
  443. spin_lock_irqsave(&npinfo->rx_lock, flags);
  444. list_for_each_entry_safe(np, tmp, &npinfo->rx_np, rx) {
  445. if (np->dev == skb->dev)
  446. hits++;
  447. }
  448. spin_unlock_irqrestore(&npinfo->rx_lock, flags);
  449. /* No netpoll struct is using this dev */
  450. if (!hits)
  451. return;
  452. proto = ntohs(eth_hdr(skb)->h_proto);
  453. if (proto == ETH_P_IP) {
  454. struct arphdr *arp;
  455. unsigned char *arp_ptr;
  456. /* No arp on this interface */
  457. if (skb->dev->flags & IFF_NOARP)
  458. return;
  459. if (!pskb_may_pull(skb, arp_hdr_len(skb->dev)))
  460. return;
  461. skb_reset_network_header(skb);
  462. skb_reset_transport_header(skb);
  463. arp = arp_hdr(skb);
  464. if ((arp->ar_hrd != htons(ARPHRD_ETHER) &&
  465. arp->ar_hrd != htons(ARPHRD_IEEE802)) ||
  466. arp->ar_pro != htons(ETH_P_IP) ||
  467. arp->ar_op != htons(ARPOP_REQUEST))
  468. return;
  469. arp_ptr = (unsigned char *)(arp+1);
  470. /* save the location of the src hw addr */
  471. sha = arp_ptr;
  472. arp_ptr += skb->dev->addr_len;
  473. memcpy(&sip, arp_ptr, 4);
  474. arp_ptr += 4;
  475. /* If we actually cared about dst hw addr,
  476. it would get copied here */
  477. arp_ptr += skb->dev->addr_len;
  478. memcpy(&tip, arp_ptr, 4);
  479. /* Should we ignore arp? */
  480. if (ipv4_is_loopback(tip) || ipv4_is_multicast(tip))
  481. return;
  482. size = arp_hdr_len(skb->dev);
  483. spin_lock_irqsave(&npinfo->rx_lock, flags);
  484. list_for_each_entry_safe(np, tmp, &npinfo->rx_np, rx) {
  485. if (tip != np->local_ip.ip)
  486. continue;
  487. hlen = LL_RESERVED_SPACE(np->dev);
  488. tlen = np->dev->needed_tailroom;
  489. send_skb = find_skb(np, size + hlen + tlen, hlen);
  490. if (!send_skb)
  491. continue;
  492. skb_reset_network_header(send_skb);
  493. arp = (struct arphdr *) skb_put(send_skb, size);
  494. send_skb->dev = skb->dev;
  495. send_skb->protocol = htons(ETH_P_ARP);
  496. /* Fill the device header for the ARP frame */
  497. if (dev_hard_header(send_skb, skb->dev, ETH_P_ARP,
  498. sha, np->dev->dev_addr,
  499. send_skb->len) < 0) {
  500. kfree_skb(send_skb);
  501. continue;
  502. }
  503. /*
  504. * Fill out the arp protocol part.
  505. *
  506. * we only support ethernet device type,
  507. * which (according to RFC 1390) should
  508. * always equal 1 (Ethernet).
  509. */
  510. arp->ar_hrd = htons(np->dev->type);
  511. arp->ar_pro = htons(ETH_P_IP);
  512. arp->ar_hln = np->dev->addr_len;
  513. arp->ar_pln = 4;
  514. arp->ar_op = htons(type);
  515. arp_ptr = (unsigned char *)(arp + 1);
  516. memcpy(arp_ptr, np->dev->dev_addr, np->dev->addr_len);
  517. arp_ptr += np->dev->addr_len;
  518. memcpy(arp_ptr, &tip, 4);
  519. arp_ptr += 4;
  520. memcpy(arp_ptr, sha, np->dev->addr_len);
  521. arp_ptr += np->dev->addr_len;
  522. memcpy(arp_ptr, &sip, 4);
  523. netpoll_send_skb(np, send_skb);
  524. /* If there are several rx_hooks for the same address,
  525. we're fine by sending a single reply */
  526. break;
  527. }
  528. spin_unlock_irqrestore(&npinfo->rx_lock, flags);
  529. } else if( proto == ETH_P_IPV6) {
  530. #if IS_ENABLED(CONFIG_IPV6)
  531. struct nd_msg *msg;
  532. u8 *lladdr = NULL;
  533. struct ipv6hdr *hdr;
  534. struct icmp6hdr *icmp6h;
  535. const struct in6_addr *saddr;
  536. const struct in6_addr *daddr;
  537. struct inet6_dev *in6_dev = NULL;
  538. struct in6_addr *target;
  539. in6_dev = in6_dev_get(skb->dev);
  540. if (!in6_dev || !in6_dev->cnf.accept_ra)
  541. return;
  542. if (!pskb_may_pull(skb, skb->len))
  543. return;
  544. msg = (struct nd_msg *)skb_transport_header(skb);
  545. __skb_push(skb, skb->data - skb_transport_header(skb));
  546. if (ipv6_hdr(skb)->hop_limit != 255)
  547. return;
  548. if (msg->icmph.icmp6_code != 0)
  549. return;
  550. if (msg->icmph.icmp6_type != NDISC_NEIGHBOUR_SOLICITATION)
  551. return;
  552. saddr = &ipv6_hdr(skb)->saddr;
  553. daddr = &ipv6_hdr(skb)->daddr;
  554. size = sizeof(struct icmp6hdr) + sizeof(struct in6_addr);
  555. spin_lock_irqsave(&npinfo->rx_lock, flags);
  556. list_for_each_entry_safe(np, tmp, &npinfo->rx_np, rx) {
  557. if (!ipv6_addr_equal(daddr, &np->local_ip.in6))
  558. continue;
  559. hlen = LL_RESERVED_SPACE(np->dev);
  560. tlen = np->dev->needed_tailroom;
  561. send_skb = find_skb(np, size + hlen + tlen, hlen);
  562. if (!send_skb)
  563. continue;
  564. send_skb->protocol = htons(ETH_P_IPV6);
  565. send_skb->dev = skb->dev;
  566. skb_reset_network_header(send_skb);
  567. skb_put(send_skb, sizeof(struct ipv6hdr));
  568. hdr = ipv6_hdr(send_skb);
  569. *(__be32*)hdr = htonl(0x60000000);
  570. hdr->payload_len = htons(size);
  571. hdr->nexthdr = IPPROTO_ICMPV6;
  572. hdr->hop_limit = 255;
  573. hdr->saddr = *saddr;
  574. hdr->daddr = *daddr;
  575. send_skb->transport_header = send_skb->tail;
  576. skb_put(send_skb, size);
  577. icmp6h = (struct icmp6hdr *)skb_transport_header(skb);
  578. icmp6h->icmp6_type = NDISC_NEIGHBOUR_ADVERTISEMENT;
  579. icmp6h->icmp6_router = 0;
  580. icmp6h->icmp6_solicited = 1;
  581. target = (struct in6_addr *)(skb_transport_header(send_skb) + sizeof(struct icmp6hdr));
  582. *target = msg->target;
  583. icmp6h->icmp6_cksum = csum_ipv6_magic(saddr, daddr, size,
  584. IPPROTO_ICMPV6,
  585. csum_partial(icmp6h,
  586. size, 0));
  587. if (dev_hard_header(send_skb, skb->dev, ETH_P_IPV6,
  588. lladdr, np->dev->dev_addr,
  589. send_skb->len) < 0) {
  590. kfree_skb(send_skb);
  591. continue;
  592. }
  593. netpoll_send_skb(np, send_skb);
  594. /* If there are several rx_hooks for the same address,
  595. we're fine by sending a single reply */
  596. break;
  597. }
  598. spin_unlock_irqrestore(&npinfo->rx_lock, flags);
  599. #endif
  600. }
  601. }
  602. static bool pkt_is_ns(struct sk_buff *skb)
  603. {
  604. struct nd_msg *msg;
  605. struct ipv6hdr *hdr;
  606. if (skb->protocol != htons(ETH_P_ARP))
  607. return false;
  608. if (!pskb_may_pull(skb, sizeof(struct ipv6hdr) + sizeof(struct nd_msg)))
  609. return false;
  610. msg = (struct nd_msg *)skb_transport_header(skb);
  611. __skb_push(skb, skb->data - skb_transport_header(skb));
  612. hdr = ipv6_hdr(skb);
  613. if (hdr->nexthdr != IPPROTO_ICMPV6)
  614. return false;
  615. if (hdr->hop_limit != 255)
  616. return false;
  617. if (msg->icmph.icmp6_code != 0)
  618. return false;
  619. if (msg->icmph.icmp6_type != NDISC_NEIGHBOUR_SOLICITATION)
  620. return false;
  621. return true;
  622. }
  623. int __netpoll_rx(struct sk_buff *skb, struct netpoll_info *npinfo)
  624. {
  625. int proto, len, ulen;
  626. int hits = 0;
  627. const struct iphdr *iph;
  628. struct udphdr *uh;
  629. struct netpoll *np, *tmp;
  630. if (list_empty(&npinfo->rx_np))
  631. goto out;
  632. if (skb->dev->type != ARPHRD_ETHER)
  633. goto out;
  634. /* check if netpoll clients need ARP */
  635. if (skb->protocol == htons(ETH_P_ARP) && atomic_read(&trapped)) {
  636. skb_queue_tail(&npinfo->neigh_tx, skb);
  637. return 1;
  638. } else if (pkt_is_ns(skb) && atomic_read(&trapped)) {
  639. skb_queue_tail(&npinfo->neigh_tx, skb);
  640. return 1;
  641. }
  642. if (skb->protocol == cpu_to_be16(ETH_P_8021Q)) {
  643. skb = vlan_untag(skb);
  644. if (unlikely(!skb))
  645. goto out;
  646. }
  647. proto = ntohs(eth_hdr(skb)->h_proto);
  648. if (proto != ETH_P_IP && proto != ETH_P_IPV6)
  649. goto out;
  650. if (skb->pkt_type == PACKET_OTHERHOST)
  651. goto out;
  652. if (skb_shared(skb))
  653. goto out;
  654. if (proto == ETH_P_IP) {
  655. if (!pskb_may_pull(skb, sizeof(struct iphdr)))
  656. goto out;
  657. iph = (struct iphdr *)skb->data;
  658. if (iph->ihl < 5 || iph->version != 4)
  659. goto out;
  660. if (!pskb_may_pull(skb, iph->ihl*4))
  661. goto out;
  662. iph = (struct iphdr *)skb->data;
  663. if (ip_fast_csum((u8 *)iph, iph->ihl) != 0)
  664. goto out;
  665. len = ntohs(iph->tot_len);
  666. if (skb->len < len || len < iph->ihl*4)
  667. goto out;
  668. /*
  669. * Our transport medium may have padded the buffer out.
  670. * Now We trim to the true length of the frame.
  671. */
  672. if (pskb_trim_rcsum(skb, len))
  673. goto out;
  674. iph = (struct iphdr *)skb->data;
  675. if (iph->protocol != IPPROTO_UDP)
  676. goto out;
  677. len -= iph->ihl*4;
  678. uh = (struct udphdr *)(((char *)iph) + iph->ihl*4);
  679. ulen = ntohs(uh->len);
  680. if (ulen != len)
  681. goto out;
  682. if (checksum_udp(skb, uh, ulen, iph->saddr, iph->daddr))
  683. goto out;
  684. list_for_each_entry_safe(np, tmp, &npinfo->rx_np, rx) {
  685. if (np->local_ip.ip && np->local_ip.ip != iph->daddr)
  686. continue;
  687. if (np->remote_ip.ip && np->remote_ip.ip != iph->saddr)
  688. continue;
  689. if (np->local_port && np->local_port != ntohs(uh->dest))
  690. continue;
  691. np->rx_hook(np, ntohs(uh->source),
  692. (char *)(uh+1),
  693. ulen - sizeof(struct udphdr));
  694. hits++;
  695. }
  696. } else {
  697. #if IS_ENABLED(CONFIG_IPV6)
  698. const struct ipv6hdr *ip6h;
  699. if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
  700. goto out;
  701. ip6h = (struct ipv6hdr *)skb->data;
  702. if (ip6h->version != 6)
  703. goto out;
  704. len = ntohs(ip6h->payload_len);
  705. if (!len)
  706. goto out;
  707. if (len + sizeof(struct ipv6hdr) > skb->len)
  708. goto out;
  709. if (pskb_trim_rcsum(skb, len + sizeof(struct ipv6hdr)))
  710. goto out;
  711. ip6h = ipv6_hdr(skb);
  712. if (!pskb_may_pull(skb, sizeof(struct udphdr)))
  713. goto out;
  714. uh = udp_hdr(skb);
  715. ulen = ntohs(uh->len);
  716. if (ulen != skb->len)
  717. goto out;
  718. if (udp6_csum_init(skb, uh, IPPROTO_UDP))
  719. goto out;
  720. list_for_each_entry_safe(np, tmp, &npinfo->rx_np, rx) {
  721. if (!ipv6_addr_equal(&np->local_ip.in6, &ip6h->daddr))
  722. continue;
  723. if (!ipv6_addr_equal(&np->remote_ip.in6, &ip6h->saddr))
  724. continue;
  725. if (np->local_port && np->local_port != ntohs(uh->dest))
  726. continue;
  727. np->rx_hook(np, ntohs(uh->source),
  728. (char *)(uh+1),
  729. ulen - sizeof(struct udphdr));
  730. hits++;
  731. }
  732. #endif
  733. }
  734. if (!hits)
  735. goto out;
  736. kfree_skb(skb);
  737. return 1;
  738. out:
  739. if (atomic_read(&trapped)) {
  740. kfree_skb(skb);
  741. return 1;
  742. }
  743. return 0;
  744. }
  745. void netpoll_print_options(struct netpoll *np)
  746. {
  747. np_info(np, "local port %d\n", np->local_port);
  748. if (np->ipv6)
  749. np_info(np, "local IPv6 address %pI6c\n", &np->local_ip.in6);
  750. else
  751. np_info(np, "local IPv4 address %pI4\n", &np->local_ip.ip);
  752. np_info(np, "interface '%s'\n", np->dev_name);
  753. np_info(np, "remote port %d\n", np->remote_port);
  754. if (np->ipv6)
  755. np_info(np, "remote IPv6 address %pI6c\n", &np->remote_ip.in6);
  756. else
  757. np_info(np, "remote IPv4 address %pI4\n", &np->remote_ip.ip);
  758. np_info(np, "remote ethernet address %pM\n", np->remote_mac);
  759. }
  760. EXPORT_SYMBOL(netpoll_print_options);
  761. static int netpoll_parse_ip_addr(const char *str, union inet_addr *addr)
  762. {
  763. const char *end;
  764. if (!strchr(str, ':') &&
  765. in4_pton(str, -1, (void *)addr, -1, &end) > 0) {
  766. if (!*end)
  767. return 0;
  768. }
  769. if (in6_pton(str, -1, addr->in6.s6_addr, -1, &end) > 0) {
  770. #if IS_ENABLED(CONFIG_IPV6)
  771. if (!*end)
  772. return 1;
  773. #else
  774. return -1;
  775. #endif
  776. }
  777. return -1;
  778. }
  779. int netpoll_parse_options(struct netpoll *np, char *opt)
  780. {
  781. char *cur=opt, *delim;
  782. int ipv6;
  783. if (*cur != '@') {
  784. if ((delim = strchr(cur, '@')) == NULL)
  785. goto parse_failed;
  786. *delim = 0;
  787. if (kstrtou16(cur, 10, &np->local_port))
  788. goto parse_failed;
  789. cur = delim;
  790. }
  791. cur++;
  792. if (*cur != '/') {
  793. if ((delim = strchr(cur, '/')) == NULL)
  794. goto parse_failed;
  795. *delim = 0;
  796. ipv6 = netpoll_parse_ip_addr(cur, &np->local_ip);
  797. if (ipv6 < 0)
  798. goto parse_failed;
  799. else
  800. np->ipv6 = (bool)ipv6;
  801. cur = delim;
  802. }
  803. cur++;
  804. if (*cur != ',') {
  805. /* parse out dev name */
  806. if ((delim = strchr(cur, ',')) == NULL)
  807. goto parse_failed;
  808. *delim = 0;
  809. strlcpy(np->dev_name, cur, sizeof(np->dev_name));
  810. cur = delim;
  811. }
  812. cur++;
  813. if (*cur != '@') {
  814. /* dst port */
  815. if ((delim = strchr(cur, '@')) == NULL)
  816. goto parse_failed;
  817. *delim = 0;
  818. if (*cur == ' ' || *cur == '\t')
  819. np_info(np, "warning: whitespace is not allowed\n");
  820. if (kstrtou16(cur, 10, &np->remote_port))
  821. goto parse_failed;
  822. cur = delim;
  823. }
  824. cur++;
  825. /* dst ip */
  826. if ((delim = strchr(cur, '/')) == NULL)
  827. goto parse_failed;
  828. *delim = 0;
  829. ipv6 = netpoll_parse_ip_addr(cur, &np->remote_ip);
  830. if (ipv6 < 0)
  831. goto parse_failed;
  832. else if (np->ipv6 != (bool)ipv6)
  833. goto parse_failed;
  834. else
  835. np->ipv6 = (bool)ipv6;
  836. cur = delim + 1;
  837. if (*cur != 0) {
  838. /* MAC address */
  839. if (!mac_pton(cur, np->remote_mac))
  840. goto parse_failed;
  841. }
  842. netpoll_print_options(np);
  843. return 0;
  844. parse_failed:
  845. np_info(np, "couldn't parse config at '%s'!\n", cur);
  846. return -1;
  847. }
  848. EXPORT_SYMBOL(netpoll_parse_options);
  849. int __netpoll_setup(struct netpoll *np, struct net_device *ndev, gfp_t gfp)
  850. {
  851. struct netpoll_info *npinfo;
  852. const struct net_device_ops *ops;
  853. unsigned long flags;
  854. int err;
  855. np->dev = ndev;
  856. strlcpy(np->dev_name, ndev->name, IFNAMSIZ);
  857. INIT_WORK(&np->cleanup_work, netpoll_async_cleanup);
  858. if ((ndev->priv_flags & IFF_DISABLE_NETPOLL) ||
  859. !ndev->netdev_ops->ndo_poll_controller) {
  860. np_err(np, "%s doesn't support polling, aborting\n",
  861. np->dev_name);
  862. err = -ENOTSUPP;
  863. goto out;
  864. }
  865. if (!ndev->npinfo) {
  866. npinfo = kmalloc(sizeof(*npinfo), gfp);
  867. if (!npinfo) {
  868. err = -ENOMEM;
  869. goto out;
  870. }
  871. npinfo->rx_flags = 0;
  872. INIT_LIST_HEAD(&npinfo->rx_np);
  873. spin_lock_init(&npinfo->rx_lock);
  874. mutex_init(&npinfo->dev_lock);
  875. skb_queue_head_init(&npinfo->neigh_tx);
  876. skb_queue_head_init(&npinfo->txq);
  877. INIT_DELAYED_WORK(&npinfo->tx_work, queue_process);
  878. atomic_set(&npinfo->refcnt, 1);
  879. ops = np->dev->netdev_ops;
  880. if (ops->ndo_netpoll_setup) {
  881. err = ops->ndo_netpoll_setup(ndev, npinfo, gfp);
  882. if (err)
  883. goto free_npinfo;
  884. }
  885. } else {
  886. npinfo = rtnl_dereference(ndev->npinfo);
  887. atomic_inc(&npinfo->refcnt);
  888. }
  889. npinfo->netpoll = np;
  890. if (np->rx_hook) {
  891. spin_lock_irqsave(&npinfo->rx_lock, flags);
  892. npinfo->rx_flags |= NETPOLL_RX_ENABLED;
  893. list_add_tail(&np->rx, &npinfo->rx_np);
  894. spin_unlock_irqrestore(&npinfo->rx_lock, flags);
  895. }
  896. /* last thing to do is link it to the net device structure */
  897. rcu_assign_pointer(ndev->npinfo, npinfo);
  898. return 0;
  899. free_npinfo:
  900. kfree(npinfo);
  901. out:
  902. return err;
  903. }
  904. EXPORT_SYMBOL_GPL(__netpoll_setup);
  905. int netpoll_setup(struct netpoll *np)
  906. {
  907. struct net_device *ndev = NULL;
  908. struct in_device *in_dev;
  909. int err;
  910. rtnl_lock();
  911. if (np->dev_name) {
  912. struct net *net = current->nsproxy->net_ns;
  913. ndev = __dev_get_by_name(net, np->dev_name);
  914. }
  915. if (!ndev) {
  916. np_err(np, "%s doesn't exist, aborting\n", np->dev_name);
  917. err = -ENODEV;
  918. goto unlock;
  919. }
  920. dev_hold(ndev);
  921. if (netdev_master_upper_dev_get(ndev)) {
  922. np_err(np, "%s is a slave device, aborting\n", np->dev_name);
  923. err = -EBUSY;
  924. goto put;
  925. }
  926. if (!netif_running(ndev)) {
  927. unsigned long atmost, atleast;
  928. np_info(np, "device %s not up yet, forcing it\n", np->dev_name);
  929. err = dev_open(ndev);
  930. if (err) {
  931. np_err(np, "failed to open %s\n", ndev->name);
  932. goto put;
  933. }
  934. rtnl_unlock();
  935. atleast = jiffies + HZ/10;
  936. atmost = jiffies + carrier_timeout * HZ;
  937. while (!netif_carrier_ok(ndev)) {
  938. if (time_after(jiffies, atmost)) {
  939. np_notice(np, "timeout waiting for carrier\n");
  940. break;
  941. }
  942. msleep(1);
  943. }
  944. /* If carrier appears to come up instantly, we don't
  945. * trust it and pause so that we don't pump all our
  946. * queued console messages into the bitbucket.
  947. */
  948. if (time_before(jiffies, atleast)) {
  949. np_notice(np, "carrier detect appears untrustworthy, waiting 4 seconds\n");
  950. msleep(4000);
  951. }
  952. rtnl_lock();
  953. }
  954. if (!np->local_ip.ip) {
  955. if (!np->ipv6) {
  956. in_dev = __in_dev_get_rtnl(ndev);
  957. if (!in_dev || !in_dev->ifa_list) {
  958. np_err(np, "no IP address for %s, aborting\n",
  959. np->dev_name);
  960. err = -EDESTADDRREQ;
  961. goto put;
  962. }
  963. np->local_ip.ip = in_dev->ifa_list->ifa_local;
  964. np_info(np, "local IP %pI4\n", &np->local_ip.ip);
  965. } else {
  966. #if IS_ENABLED(CONFIG_IPV6)
  967. struct inet6_dev *idev;
  968. err = -EDESTADDRREQ;
  969. idev = __in6_dev_get(ndev);
  970. if (idev) {
  971. struct inet6_ifaddr *ifp;
  972. read_lock_bh(&idev->lock);
  973. list_for_each_entry(ifp, &idev->addr_list, if_list) {
  974. if (ipv6_addr_type(&ifp->addr) & IPV6_ADDR_LINKLOCAL)
  975. continue;
  976. np->local_ip.in6 = ifp->addr;
  977. err = 0;
  978. break;
  979. }
  980. read_unlock_bh(&idev->lock);
  981. }
  982. if (err) {
  983. np_err(np, "no IPv6 address for %s, aborting\n",
  984. np->dev_name);
  985. goto put;
  986. } else
  987. np_info(np, "local IPv6 %pI6c\n", &np->local_ip.in6);
  988. #else
  989. np_err(np, "IPv6 is not supported %s, aborting\n",
  990. np->dev_name);
  991. err = -EINVAL;
  992. goto put;
  993. #endif
  994. }
  995. }
  996. /* fill up the skb queue */
  997. refill_skbs();
  998. err = __netpoll_setup(np, ndev, GFP_KERNEL);
  999. if (err)
  1000. goto put;
  1001. rtnl_unlock();
  1002. return 0;
  1003. put:
  1004. dev_put(ndev);
  1005. unlock:
  1006. rtnl_unlock();
  1007. return err;
  1008. }
  1009. EXPORT_SYMBOL(netpoll_setup);
  1010. static int __init netpoll_init(void)
  1011. {
  1012. skb_queue_head_init(&skb_pool);
  1013. init_srcu_struct(&netpoll_srcu);
  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);