netpoll.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323
  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. void 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. }
  214. EXPORT_SYMBOL(netpoll_rx_disable);
  215. void netpoll_rx_enable(struct net_device *dev)
  216. {
  217. struct netpoll_info *ni;
  218. rcu_read_lock();
  219. ni = rcu_dereference(dev->npinfo);
  220. if (ni)
  221. up(&ni->dev_lock);
  222. rcu_read_unlock();
  223. }
  224. EXPORT_SYMBOL(netpoll_rx_enable);
  225. static void refill_skbs(void)
  226. {
  227. struct sk_buff *skb;
  228. unsigned long flags;
  229. spin_lock_irqsave(&skb_pool.lock, flags);
  230. while (skb_pool.qlen < MAX_SKBS) {
  231. skb = alloc_skb(MAX_SKB_SIZE, GFP_ATOMIC);
  232. if (!skb)
  233. break;
  234. __skb_queue_tail(&skb_pool, skb);
  235. }
  236. spin_unlock_irqrestore(&skb_pool.lock, flags);
  237. }
  238. static void zap_completion_queue(void)
  239. {
  240. unsigned long flags;
  241. struct softnet_data *sd = &get_cpu_var(softnet_data);
  242. if (sd->completion_queue) {
  243. struct sk_buff *clist;
  244. local_irq_save(flags);
  245. clist = sd->completion_queue;
  246. sd->completion_queue = NULL;
  247. local_irq_restore(flags);
  248. while (clist != NULL) {
  249. struct sk_buff *skb = clist;
  250. clist = clist->next;
  251. if (skb->destructor) {
  252. atomic_inc(&skb->users);
  253. dev_kfree_skb_any(skb); /* put this one back */
  254. } else {
  255. __kfree_skb(skb);
  256. }
  257. }
  258. }
  259. put_cpu_var(softnet_data);
  260. }
  261. static struct sk_buff *find_skb(struct netpoll *np, int len, int reserve)
  262. {
  263. int count = 0;
  264. struct sk_buff *skb;
  265. zap_completion_queue();
  266. refill_skbs();
  267. repeat:
  268. skb = alloc_skb(len, GFP_ATOMIC);
  269. if (!skb)
  270. skb = skb_dequeue(&skb_pool);
  271. if (!skb) {
  272. if (++count < 10) {
  273. netpoll_poll_dev(np->dev);
  274. goto repeat;
  275. }
  276. return NULL;
  277. }
  278. atomic_set(&skb->users, 1);
  279. skb_reserve(skb, reserve);
  280. return skb;
  281. }
  282. static int netpoll_owner_active(struct net_device *dev)
  283. {
  284. struct napi_struct *napi;
  285. list_for_each_entry(napi, &dev->napi_list, dev_list) {
  286. if (napi->poll_owner == smp_processor_id())
  287. return 1;
  288. }
  289. return 0;
  290. }
  291. /* call with IRQ disabled */
  292. void netpoll_send_skb_on_dev(struct netpoll *np, struct sk_buff *skb,
  293. struct net_device *dev)
  294. {
  295. int status = NETDEV_TX_BUSY;
  296. unsigned long tries;
  297. const struct net_device_ops *ops = dev->netdev_ops;
  298. /* It is up to the caller to keep npinfo alive. */
  299. struct netpoll_info *npinfo;
  300. WARN_ON_ONCE(!irqs_disabled());
  301. npinfo = rcu_dereference_bh(np->dev->npinfo);
  302. if (!npinfo || !netif_running(dev) || !netif_device_present(dev)) {
  303. __kfree_skb(skb);
  304. return;
  305. }
  306. /* don't get messages out of order, and no recursion */
  307. if (skb_queue_len(&npinfo->txq) == 0 && !netpoll_owner_active(dev)) {
  308. struct netdev_queue *txq;
  309. txq = netdev_pick_tx(dev, skb);
  310. /* try until next clock tick */
  311. for (tries = jiffies_to_usecs(1)/USEC_PER_POLL;
  312. tries > 0; --tries) {
  313. if (__netif_tx_trylock(txq)) {
  314. if (!netif_xmit_stopped(txq)) {
  315. if (vlan_tx_tag_present(skb) &&
  316. !vlan_hw_offload_capable(netif_skb_features(skb),
  317. skb->vlan_proto)) {
  318. skb = __vlan_put_tag(skb, skb->vlan_proto, 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. unsigned long tail_offset;
  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. tail_offset = skb_tail_offset(skb);
  577. if (tail_offset > 0xffff) {
  578. kfree_skb(send_skb);
  579. continue;
  580. }
  581. skb_set_network_header(send_skb, tail_offset);
  582. skb_put(send_skb, size);
  583. icmp6h = (struct icmp6hdr *)skb_transport_header(skb);
  584. icmp6h->icmp6_type = NDISC_NEIGHBOUR_ADVERTISEMENT;
  585. icmp6h->icmp6_router = 0;
  586. icmp6h->icmp6_solicited = 1;
  587. target = (struct in6_addr *)(skb_transport_header(send_skb) + sizeof(struct icmp6hdr));
  588. *target = msg->target;
  589. icmp6h->icmp6_cksum = csum_ipv6_magic(saddr, daddr, size,
  590. IPPROTO_ICMPV6,
  591. csum_partial(icmp6h,
  592. size, 0));
  593. if (dev_hard_header(send_skb, skb->dev, ETH_P_IPV6,
  594. lladdr, np->dev->dev_addr,
  595. send_skb->len) < 0) {
  596. kfree_skb(send_skb);
  597. continue;
  598. }
  599. netpoll_send_skb(np, send_skb);
  600. /* If there are several rx_hooks for the same address,
  601. we're fine by sending a single reply */
  602. break;
  603. }
  604. spin_unlock_irqrestore(&npinfo->rx_lock, flags);
  605. #endif
  606. }
  607. }
  608. static bool pkt_is_ns(struct sk_buff *skb)
  609. {
  610. struct nd_msg *msg;
  611. struct ipv6hdr *hdr;
  612. if (skb->protocol != htons(ETH_P_ARP))
  613. return false;
  614. if (!pskb_may_pull(skb, sizeof(struct ipv6hdr) + sizeof(struct nd_msg)))
  615. return false;
  616. msg = (struct nd_msg *)skb_transport_header(skb);
  617. __skb_push(skb, skb->data - skb_transport_header(skb));
  618. hdr = ipv6_hdr(skb);
  619. if (hdr->nexthdr != IPPROTO_ICMPV6)
  620. return false;
  621. if (hdr->hop_limit != 255)
  622. return false;
  623. if (msg->icmph.icmp6_code != 0)
  624. return false;
  625. if (msg->icmph.icmp6_type != NDISC_NEIGHBOUR_SOLICITATION)
  626. return false;
  627. return true;
  628. }
  629. int __netpoll_rx(struct sk_buff *skb, struct netpoll_info *npinfo)
  630. {
  631. int proto, len, ulen;
  632. int hits = 0;
  633. const struct iphdr *iph;
  634. struct udphdr *uh;
  635. struct netpoll *np, *tmp;
  636. if (list_empty(&npinfo->rx_np))
  637. goto out;
  638. if (skb->dev->type != ARPHRD_ETHER)
  639. goto out;
  640. /* check if netpoll clients need ARP */
  641. if (skb->protocol == htons(ETH_P_ARP) && atomic_read(&trapped)) {
  642. skb_queue_tail(&npinfo->neigh_tx, skb);
  643. return 1;
  644. } else if (pkt_is_ns(skb) && atomic_read(&trapped)) {
  645. skb_queue_tail(&npinfo->neigh_tx, skb);
  646. return 1;
  647. }
  648. if (skb->protocol == cpu_to_be16(ETH_P_8021Q)) {
  649. skb = vlan_untag(skb);
  650. if (unlikely(!skb))
  651. goto out;
  652. }
  653. proto = ntohs(eth_hdr(skb)->h_proto);
  654. if (proto != ETH_P_IP && proto != ETH_P_IPV6)
  655. goto out;
  656. if (skb->pkt_type == PACKET_OTHERHOST)
  657. goto out;
  658. if (skb_shared(skb))
  659. goto out;
  660. if (proto == ETH_P_IP) {
  661. if (!pskb_may_pull(skb, sizeof(struct iphdr)))
  662. goto out;
  663. iph = (struct iphdr *)skb->data;
  664. if (iph->ihl < 5 || iph->version != 4)
  665. goto out;
  666. if (!pskb_may_pull(skb, iph->ihl*4))
  667. goto out;
  668. iph = (struct iphdr *)skb->data;
  669. if (ip_fast_csum((u8 *)iph, iph->ihl) != 0)
  670. goto out;
  671. len = ntohs(iph->tot_len);
  672. if (skb->len < len || len < iph->ihl*4)
  673. goto out;
  674. /*
  675. * Our transport medium may have padded the buffer out.
  676. * Now We trim to the true length of the frame.
  677. */
  678. if (pskb_trim_rcsum(skb, len))
  679. goto out;
  680. iph = (struct iphdr *)skb->data;
  681. if (iph->protocol != IPPROTO_UDP)
  682. goto out;
  683. len -= iph->ihl*4;
  684. uh = (struct udphdr *)(((char *)iph) + iph->ihl*4);
  685. ulen = ntohs(uh->len);
  686. if (ulen != len)
  687. goto out;
  688. if (checksum_udp(skb, uh, ulen, iph->saddr, iph->daddr))
  689. goto out;
  690. list_for_each_entry_safe(np, tmp, &npinfo->rx_np, rx) {
  691. if (np->local_ip.ip && np->local_ip.ip != iph->daddr)
  692. continue;
  693. if (np->remote_ip.ip && np->remote_ip.ip != iph->saddr)
  694. continue;
  695. if (np->local_port && np->local_port != ntohs(uh->dest))
  696. continue;
  697. np->rx_hook(np, ntohs(uh->source),
  698. (char *)(uh+1),
  699. ulen - sizeof(struct udphdr));
  700. hits++;
  701. }
  702. } else {
  703. #if IS_ENABLED(CONFIG_IPV6)
  704. const struct ipv6hdr *ip6h;
  705. if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
  706. goto out;
  707. ip6h = (struct ipv6hdr *)skb->data;
  708. if (ip6h->version != 6)
  709. goto out;
  710. len = ntohs(ip6h->payload_len);
  711. if (!len)
  712. goto out;
  713. if (len + sizeof(struct ipv6hdr) > skb->len)
  714. goto out;
  715. if (pskb_trim_rcsum(skb, len + sizeof(struct ipv6hdr)))
  716. goto out;
  717. ip6h = ipv6_hdr(skb);
  718. if (!pskb_may_pull(skb, sizeof(struct udphdr)))
  719. goto out;
  720. uh = udp_hdr(skb);
  721. ulen = ntohs(uh->len);
  722. if (ulen != skb->len)
  723. goto out;
  724. if (udp6_csum_init(skb, uh, IPPROTO_UDP))
  725. goto out;
  726. list_for_each_entry_safe(np, tmp, &npinfo->rx_np, rx) {
  727. if (!ipv6_addr_equal(&np->local_ip.in6, &ip6h->daddr))
  728. continue;
  729. if (!ipv6_addr_equal(&np->remote_ip.in6, &ip6h->saddr))
  730. continue;
  731. if (np->local_port && np->local_port != ntohs(uh->dest))
  732. continue;
  733. np->rx_hook(np, ntohs(uh->source),
  734. (char *)(uh+1),
  735. ulen - sizeof(struct udphdr));
  736. hits++;
  737. }
  738. #endif
  739. }
  740. if (!hits)
  741. goto out;
  742. kfree_skb(skb);
  743. return 1;
  744. out:
  745. if (atomic_read(&trapped)) {
  746. kfree_skb(skb);
  747. return 1;
  748. }
  749. return 0;
  750. }
  751. void netpoll_print_options(struct netpoll *np)
  752. {
  753. np_info(np, "local port %d\n", np->local_port);
  754. if (np->ipv6)
  755. np_info(np, "local IPv6 address %pI6c\n", &np->local_ip.in6);
  756. else
  757. np_info(np, "local IPv4 address %pI4\n", &np->local_ip.ip);
  758. np_info(np, "interface '%s'\n", np->dev_name);
  759. np_info(np, "remote port %d\n", np->remote_port);
  760. if (np->ipv6)
  761. np_info(np, "remote IPv6 address %pI6c\n", &np->remote_ip.in6);
  762. else
  763. np_info(np, "remote IPv4 address %pI4\n", &np->remote_ip.ip);
  764. np_info(np, "remote ethernet address %pM\n", np->remote_mac);
  765. }
  766. EXPORT_SYMBOL(netpoll_print_options);
  767. static int netpoll_parse_ip_addr(const char *str, union inet_addr *addr)
  768. {
  769. const char *end;
  770. if (!strchr(str, ':') &&
  771. in4_pton(str, -1, (void *)addr, -1, &end) > 0) {
  772. if (!*end)
  773. return 0;
  774. }
  775. if (in6_pton(str, -1, addr->in6.s6_addr, -1, &end) > 0) {
  776. #if IS_ENABLED(CONFIG_IPV6)
  777. if (!*end)
  778. return 1;
  779. #else
  780. return -1;
  781. #endif
  782. }
  783. return -1;
  784. }
  785. int netpoll_parse_options(struct netpoll *np, char *opt)
  786. {
  787. char *cur=opt, *delim;
  788. int ipv6;
  789. if (*cur != '@') {
  790. if ((delim = strchr(cur, '@')) == NULL)
  791. goto parse_failed;
  792. *delim = 0;
  793. if (kstrtou16(cur, 10, &np->local_port))
  794. goto parse_failed;
  795. cur = delim;
  796. }
  797. cur++;
  798. if (*cur != '/') {
  799. if ((delim = strchr(cur, '/')) == NULL)
  800. goto parse_failed;
  801. *delim = 0;
  802. ipv6 = netpoll_parse_ip_addr(cur, &np->local_ip);
  803. if (ipv6 < 0)
  804. goto parse_failed;
  805. else
  806. np->ipv6 = (bool)ipv6;
  807. cur = delim;
  808. }
  809. cur++;
  810. if (*cur != ',') {
  811. /* parse out dev name */
  812. if ((delim = strchr(cur, ',')) == NULL)
  813. goto parse_failed;
  814. *delim = 0;
  815. strlcpy(np->dev_name, cur, sizeof(np->dev_name));
  816. cur = delim;
  817. }
  818. cur++;
  819. if (*cur != '@') {
  820. /* dst port */
  821. if ((delim = strchr(cur, '@')) == NULL)
  822. goto parse_failed;
  823. *delim = 0;
  824. if (*cur == ' ' || *cur == '\t')
  825. np_info(np, "warning: whitespace is not allowed\n");
  826. if (kstrtou16(cur, 10, &np->remote_port))
  827. goto parse_failed;
  828. cur = delim;
  829. }
  830. cur++;
  831. /* dst ip */
  832. if ((delim = strchr(cur, '/')) == NULL)
  833. goto parse_failed;
  834. *delim = 0;
  835. ipv6 = netpoll_parse_ip_addr(cur, &np->remote_ip);
  836. if (ipv6 < 0)
  837. goto parse_failed;
  838. else if (np->ipv6 != (bool)ipv6)
  839. goto parse_failed;
  840. else
  841. np->ipv6 = (bool)ipv6;
  842. cur = delim + 1;
  843. if (*cur != 0) {
  844. /* MAC address */
  845. if (!mac_pton(cur, np->remote_mac))
  846. goto parse_failed;
  847. }
  848. netpoll_print_options(np);
  849. return 0;
  850. parse_failed:
  851. np_info(np, "couldn't parse config at '%s'!\n", cur);
  852. return -1;
  853. }
  854. EXPORT_SYMBOL(netpoll_parse_options);
  855. int __netpoll_setup(struct netpoll *np, struct net_device *ndev, gfp_t gfp)
  856. {
  857. struct netpoll_info *npinfo;
  858. const struct net_device_ops *ops;
  859. unsigned long flags;
  860. int err;
  861. np->dev = ndev;
  862. strlcpy(np->dev_name, ndev->name, IFNAMSIZ);
  863. INIT_WORK(&np->cleanup_work, netpoll_async_cleanup);
  864. if ((ndev->priv_flags & IFF_DISABLE_NETPOLL) ||
  865. !ndev->netdev_ops->ndo_poll_controller) {
  866. np_err(np, "%s doesn't support polling, aborting\n",
  867. np->dev_name);
  868. err = -ENOTSUPP;
  869. goto out;
  870. }
  871. if (!ndev->npinfo) {
  872. npinfo = kmalloc(sizeof(*npinfo), gfp);
  873. if (!npinfo) {
  874. err = -ENOMEM;
  875. goto out;
  876. }
  877. npinfo->rx_flags = 0;
  878. INIT_LIST_HEAD(&npinfo->rx_np);
  879. spin_lock_init(&npinfo->rx_lock);
  880. sema_init(&npinfo->dev_lock, 1);
  881. skb_queue_head_init(&npinfo->neigh_tx);
  882. skb_queue_head_init(&npinfo->txq);
  883. INIT_DELAYED_WORK(&npinfo->tx_work, queue_process);
  884. atomic_set(&npinfo->refcnt, 1);
  885. ops = np->dev->netdev_ops;
  886. if (ops->ndo_netpoll_setup) {
  887. err = ops->ndo_netpoll_setup(ndev, npinfo, gfp);
  888. if (err)
  889. goto free_npinfo;
  890. }
  891. } else {
  892. npinfo = rtnl_dereference(ndev->npinfo);
  893. atomic_inc(&npinfo->refcnt);
  894. }
  895. npinfo->netpoll = np;
  896. if (np->rx_hook) {
  897. spin_lock_irqsave(&npinfo->rx_lock, flags);
  898. npinfo->rx_flags |= NETPOLL_RX_ENABLED;
  899. list_add_tail(&np->rx, &npinfo->rx_np);
  900. spin_unlock_irqrestore(&npinfo->rx_lock, flags);
  901. }
  902. /* last thing to do is link it to the net device structure */
  903. rcu_assign_pointer(ndev->npinfo, npinfo);
  904. return 0;
  905. free_npinfo:
  906. kfree(npinfo);
  907. out:
  908. return err;
  909. }
  910. EXPORT_SYMBOL_GPL(__netpoll_setup);
  911. int netpoll_setup(struct netpoll *np)
  912. {
  913. struct net_device *ndev = NULL;
  914. struct in_device *in_dev;
  915. int err;
  916. rtnl_lock();
  917. if (np->dev_name) {
  918. struct net *net = current->nsproxy->net_ns;
  919. ndev = __dev_get_by_name(net, np->dev_name);
  920. }
  921. if (!ndev) {
  922. np_err(np, "%s doesn't exist, aborting\n", np->dev_name);
  923. err = -ENODEV;
  924. goto unlock;
  925. }
  926. dev_hold(ndev);
  927. if (netdev_master_upper_dev_get(ndev)) {
  928. np_err(np, "%s is a slave device, aborting\n", np->dev_name);
  929. err = -EBUSY;
  930. goto put;
  931. }
  932. if (!netif_running(ndev)) {
  933. unsigned long atmost, atleast;
  934. np_info(np, "device %s not up yet, forcing it\n", np->dev_name);
  935. err = dev_open(ndev);
  936. if (err) {
  937. np_err(np, "failed to open %s\n", ndev->name);
  938. goto put;
  939. }
  940. rtnl_unlock();
  941. atleast = jiffies + HZ/10;
  942. atmost = jiffies + carrier_timeout * HZ;
  943. while (!netif_carrier_ok(ndev)) {
  944. if (time_after(jiffies, atmost)) {
  945. np_notice(np, "timeout waiting for carrier\n");
  946. break;
  947. }
  948. msleep(1);
  949. }
  950. /* If carrier appears to come up instantly, we don't
  951. * trust it and pause so that we don't pump all our
  952. * queued console messages into the bitbucket.
  953. */
  954. if (time_before(jiffies, atleast)) {
  955. np_notice(np, "carrier detect appears untrustworthy, waiting 4 seconds\n");
  956. msleep(4000);
  957. }
  958. rtnl_lock();
  959. }
  960. if (!np->local_ip.ip) {
  961. if (!np->ipv6) {
  962. in_dev = __in_dev_get_rtnl(ndev);
  963. if (!in_dev || !in_dev->ifa_list) {
  964. np_err(np, "no IP address for %s, aborting\n",
  965. np->dev_name);
  966. err = -EDESTADDRREQ;
  967. goto put;
  968. }
  969. np->local_ip.ip = in_dev->ifa_list->ifa_local;
  970. np_info(np, "local IP %pI4\n", &np->local_ip.ip);
  971. } else {
  972. #if IS_ENABLED(CONFIG_IPV6)
  973. struct inet6_dev *idev;
  974. err = -EDESTADDRREQ;
  975. idev = __in6_dev_get(ndev);
  976. if (idev) {
  977. struct inet6_ifaddr *ifp;
  978. read_lock_bh(&idev->lock);
  979. list_for_each_entry(ifp, &idev->addr_list, if_list) {
  980. if (ipv6_addr_type(&ifp->addr) & IPV6_ADDR_LINKLOCAL)
  981. continue;
  982. np->local_ip.in6 = ifp->addr;
  983. err = 0;
  984. break;
  985. }
  986. read_unlock_bh(&idev->lock);
  987. }
  988. if (err) {
  989. np_err(np, "no IPv6 address for %s, aborting\n",
  990. np->dev_name);
  991. goto put;
  992. } else
  993. np_info(np, "local IPv6 %pI6c\n", &np->local_ip.in6);
  994. #else
  995. np_err(np, "IPv6 is not supported %s, aborting\n",
  996. np->dev_name);
  997. err = -EINVAL;
  998. goto put;
  999. #endif
  1000. }
  1001. }
  1002. /* fill up the skb queue */
  1003. refill_skbs();
  1004. err = __netpoll_setup(np, ndev, GFP_KERNEL);
  1005. if (err)
  1006. goto put;
  1007. rtnl_unlock();
  1008. return 0;
  1009. put:
  1010. dev_put(ndev);
  1011. unlock:
  1012. rtnl_unlock();
  1013. return err;
  1014. }
  1015. EXPORT_SYMBOL(netpoll_setup);
  1016. static int __init netpoll_init(void)
  1017. {
  1018. skb_queue_head_init(&skb_pool);
  1019. return 0;
  1020. }
  1021. core_initcall(netpoll_init);
  1022. static void rcu_cleanup_netpoll_info(struct rcu_head *rcu_head)
  1023. {
  1024. struct netpoll_info *npinfo =
  1025. container_of(rcu_head, struct netpoll_info, rcu);
  1026. skb_queue_purge(&npinfo->neigh_tx);
  1027. skb_queue_purge(&npinfo->txq);
  1028. /* we can't call cancel_delayed_work_sync here, as we are in softirq */
  1029. cancel_delayed_work(&npinfo->tx_work);
  1030. /* clean after last, unfinished work */
  1031. __skb_queue_purge(&npinfo->txq);
  1032. /* now cancel it again */
  1033. cancel_delayed_work(&npinfo->tx_work);
  1034. kfree(npinfo);
  1035. }
  1036. void __netpoll_cleanup(struct netpoll *np)
  1037. {
  1038. struct netpoll_info *npinfo;
  1039. unsigned long flags;
  1040. /* rtnl_dereference would be preferable here but
  1041. * rcu_cleanup_netpoll path can put us in here safely without
  1042. * holding the rtnl, so plain rcu_dereference it is
  1043. */
  1044. npinfo = rtnl_dereference(np->dev->npinfo);
  1045. if (!npinfo)
  1046. return;
  1047. if (!list_empty(&npinfo->rx_np)) {
  1048. spin_lock_irqsave(&npinfo->rx_lock, flags);
  1049. list_del(&np->rx);
  1050. if (list_empty(&npinfo->rx_np))
  1051. npinfo->rx_flags &= ~NETPOLL_RX_ENABLED;
  1052. spin_unlock_irqrestore(&npinfo->rx_lock, flags);
  1053. }
  1054. synchronize_srcu(&netpoll_srcu);
  1055. if (atomic_dec_and_test(&npinfo->refcnt)) {
  1056. const struct net_device_ops *ops;
  1057. ops = np->dev->netdev_ops;
  1058. if (ops->ndo_netpoll_cleanup)
  1059. ops->ndo_netpoll_cleanup(np->dev);
  1060. rcu_assign_pointer(np->dev->npinfo, NULL);
  1061. call_rcu_bh(&npinfo->rcu, rcu_cleanup_netpoll_info);
  1062. }
  1063. }
  1064. EXPORT_SYMBOL_GPL(__netpoll_cleanup);
  1065. static void netpoll_async_cleanup(struct work_struct *work)
  1066. {
  1067. struct netpoll *np = container_of(work, struct netpoll, cleanup_work);
  1068. rtnl_lock();
  1069. __netpoll_cleanup(np);
  1070. rtnl_unlock();
  1071. kfree(np);
  1072. }
  1073. void __netpoll_free_async(struct netpoll *np)
  1074. {
  1075. schedule_work(&np->cleanup_work);
  1076. }
  1077. EXPORT_SYMBOL_GPL(__netpoll_free_async);
  1078. void netpoll_cleanup(struct netpoll *np)
  1079. {
  1080. if (!np->dev)
  1081. return;
  1082. rtnl_lock();
  1083. __netpoll_cleanup(np);
  1084. rtnl_unlock();
  1085. dev_put(np->dev);
  1086. np->dev = NULL;
  1087. }
  1088. EXPORT_SYMBOL(netpoll_cleanup);
  1089. int netpoll_trap(void)
  1090. {
  1091. return atomic_read(&trapped);
  1092. }
  1093. EXPORT_SYMBOL(netpoll_trap);
  1094. void netpoll_set_trap(int trap)
  1095. {
  1096. if (trap)
  1097. atomic_inc(&trapped);
  1098. else
  1099. atomic_dec(&trapped);
  1100. }
  1101. EXPORT_SYMBOL(netpoll_set_trap);