netpoll.c 31 KB

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