netpoll.c 23 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039
  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 <asm/unaligned.h>
  31. #include <trace/events/napi.h>
  32. /*
  33. * We maintain a small pool of fully-sized skbs, to make sure the
  34. * message gets out even in extreme OOM situations.
  35. */
  36. #define MAX_UDP_CHUNK 1460
  37. #define MAX_SKBS 32
  38. static struct sk_buff_head skb_pool;
  39. static atomic_t trapped;
  40. #define USEC_PER_POLL 50
  41. #define NETPOLL_RX_ENABLED 1
  42. #define NETPOLL_RX_DROP 2
  43. #define MAX_SKB_SIZE \
  44. (sizeof(struct ethhdr) + \
  45. sizeof(struct iphdr) + \
  46. sizeof(struct udphdr) + \
  47. MAX_UDP_CHUNK)
  48. static void zap_completion_queue(void);
  49. static void netpoll_neigh_reply(struct sk_buff *skb, struct netpoll_info *npinfo);
  50. static unsigned int carrier_timeout = 4;
  51. module_param(carrier_timeout, uint, 0644);
  52. #define np_info(np, fmt, ...) \
  53. pr_info("%s: " fmt, np->name, ##__VA_ARGS__)
  54. #define np_err(np, fmt, ...) \
  55. pr_err("%s: " fmt, np->name, ##__VA_ARGS__)
  56. #define np_notice(np, fmt, ...) \
  57. pr_notice("%s: " fmt, np->name, ##__VA_ARGS__)
  58. static void queue_process(struct work_struct *work)
  59. {
  60. struct netpoll_info *npinfo =
  61. container_of(work, struct netpoll_info, tx_work.work);
  62. struct sk_buff *skb;
  63. unsigned long flags;
  64. while ((skb = skb_dequeue(&npinfo->txq))) {
  65. struct net_device *dev = skb->dev;
  66. const struct net_device_ops *ops = dev->netdev_ops;
  67. struct netdev_queue *txq;
  68. if (!netif_device_present(dev) || !netif_running(dev)) {
  69. __kfree_skb(skb);
  70. continue;
  71. }
  72. txq = netdev_get_tx_queue(dev, skb_get_queue_mapping(skb));
  73. local_irq_save(flags);
  74. __netif_tx_lock(txq, smp_processor_id());
  75. if (netif_xmit_frozen_or_stopped(txq) ||
  76. ops->ndo_start_xmit(skb, dev) != NETDEV_TX_OK) {
  77. skb_queue_head(&npinfo->txq, skb);
  78. __netif_tx_unlock(txq);
  79. local_irq_restore(flags);
  80. schedule_delayed_work(&npinfo->tx_work, HZ/10);
  81. return;
  82. }
  83. __netif_tx_unlock(txq);
  84. local_irq_restore(flags);
  85. }
  86. }
  87. static __sum16 checksum_udp(struct sk_buff *skb, struct udphdr *uh,
  88. unsigned short ulen, __be32 saddr, __be32 daddr)
  89. {
  90. __wsum psum;
  91. if (uh->check == 0 || skb_csum_unnecessary(skb))
  92. return 0;
  93. psum = csum_tcpudp_nofold(saddr, daddr, ulen, IPPROTO_UDP, 0);
  94. if (skb->ip_summed == CHECKSUM_COMPLETE &&
  95. !csum_fold(csum_add(psum, skb->csum)))
  96. return 0;
  97. skb->csum = psum;
  98. return __skb_checksum_complete(skb);
  99. }
  100. /*
  101. * Check whether delayed processing was scheduled for our NIC. If so,
  102. * we attempt to grab the poll lock and use ->poll() to pump the card.
  103. * If this fails, either we've recursed in ->poll() or it's already
  104. * running on another CPU.
  105. *
  106. * Note: we don't mask interrupts with this lock because we're using
  107. * trylock here and interrupts are already disabled in the softirq
  108. * case. Further, we test the poll_owner to avoid recursion on UP
  109. * systems where the lock doesn't exist.
  110. *
  111. * In cases where there is bi-directional communications, reading only
  112. * one message at a time can lead to packets being dropped by the
  113. * network adapter, forcing superfluous retries and possibly timeouts.
  114. * Thus, we set our budget to greater than 1.
  115. */
  116. static int poll_one_napi(struct netpoll_info *npinfo,
  117. struct napi_struct *napi, int budget)
  118. {
  119. int work;
  120. /* net_rx_action's ->poll() invocations and our's are
  121. * synchronized by this test which is only made while
  122. * holding the napi->poll_lock.
  123. */
  124. if (!test_bit(NAPI_STATE_SCHED, &napi->state))
  125. return budget;
  126. npinfo->rx_flags |= NETPOLL_RX_DROP;
  127. atomic_inc(&trapped);
  128. set_bit(NAPI_STATE_NPSVC, &napi->state);
  129. work = napi->poll(napi, budget);
  130. trace_napi_poll(napi);
  131. clear_bit(NAPI_STATE_NPSVC, &napi->state);
  132. atomic_dec(&trapped);
  133. npinfo->rx_flags &= ~NETPOLL_RX_DROP;
  134. return budget - work;
  135. }
  136. static void poll_napi(struct net_device *dev)
  137. {
  138. struct napi_struct *napi;
  139. int budget = 16;
  140. list_for_each_entry(napi, &dev->napi_list, dev_list) {
  141. if (napi->poll_owner != smp_processor_id() &&
  142. spin_trylock(&napi->poll_lock)) {
  143. budget = poll_one_napi(rcu_dereference_bh(dev->npinfo),
  144. napi, budget);
  145. spin_unlock(&napi->poll_lock);
  146. if (!budget)
  147. break;
  148. }
  149. }
  150. }
  151. static void service_neigh_queue(struct netpoll_info *npi)
  152. {
  153. if (npi) {
  154. struct sk_buff *skb;
  155. while ((skb = skb_dequeue(&npi->neigh_tx)))
  156. netpoll_neigh_reply(skb, npi);
  157. }
  158. }
  159. static void netpoll_poll_dev(struct net_device *dev)
  160. {
  161. const struct net_device_ops *ops;
  162. struct netpoll_info *ni = rcu_dereference_bh(dev->npinfo);
  163. if (!dev || !netif_running(dev))
  164. return;
  165. ops = dev->netdev_ops;
  166. if (!ops->ndo_poll_controller)
  167. return;
  168. /* Process pending work on NIC */
  169. ops->ndo_poll_controller(dev);
  170. poll_napi(dev);
  171. if (dev->flags & IFF_SLAVE) {
  172. if (ni) {
  173. struct net_device *bond_dev;
  174. struct sk_buff *skb;
  175. struct netpoll_info *bond_ni;
  176. bond_dev = netdev_master_upper_dev_get_rcu(dev);
  177. bond_ni = rcu_dereference_bh(bond_dev->npinfo);
  178. while ((skb = skb_dequeue(&ni->neigh_tx))) {
  179. skb->dev = bond_dev;
  180. skb_queue_tail(&bond_ni->neigh_tx, skb);
  181. }
  182. }
  183. }
  184. service_neigh_queue(ni);
  185. zap_completion_queue();
  186. }
  187. static void refill_skbs(void)
  188. {
  189. struct sk_buff *skb;
  190. unsigned long flags;
  191. spin_lock_irqsave(&skb_pool.lock, flags);
  192. while (skb_pool.qlen < MAX_SKBS) {
  193. skb = alloc_skb(MAX_SKB_SIZE, GFP_ATOMIC);
  194. if (!skb)
  195. break;
  196. __skb_queue_tail(&skb_pool, skb);
  197. }
  198. spin_unlock_irqrestore(&skb_pool.lock, flags);
  199. }
  200. static void zap_completion_queue(void)
  201. {
  202. unsigned long flags;
  203. struct softnet_data *sd = &get_cpu_var(softnet_data);
  204. if (sd->completion_queue) {
  205. struct sk_buff *clist;
  206. local_irq_save(flags);
  207. clist = sd->completion_queue;
  208. sd->completion_queue = NULL;
  209. local_irq_restore(flags);
  210. while (clist != NULL) {
  211. struct sk_buff *skb = clist;
  212. clist = clist->next;
  213. if (skb->destructor) {
  214. atomic_inc(&skb->users);
  215. dev_kfree_skb_any(skb); /* put this one back */
  216. } else {
  217. __kfree_skb(skb);
  218. }
  219. }
  220. }
  221. put_cpu_var(softnet_data);
  222. }
  223. static struct sk_buff *find_skb(struct netpoll *np, int len, int reserve)
  224. {
  225. int count = 0;
  226. struct sk_buff *skb;
  227. zap_completion_queue();
  228. refill_skbs();
  229. repeat:
  230. skb = alloc_skb(len, GFP_ATOMIC);
  231. if (!skb)
  232. skb = skb_dequeue(&skb_pool);
  233. if (!skb) {
  234. if (++count < 10) {
  235. netpoll_poll_dev(np->dev);
  236. goto repeat;
  237. }
  238. return NULL;
  239. }
  240. atomic_set(&skb->users, 1);
  241. skb_reserve(skb, reserve);
  242. return skb;
  243. }
  244. static int netpoll_owner_active(struct net_device *dev)
  245. {
  246. struct napi_struct *napi;
  247. list_for_each_entry(napi, &dev->napi_list, dev_list) {
  248. if (napi->poll_owner == smp_processor_id())
  249. return 1;
  250. }
  251. return 0;
  252. }
  253. /* call with IRQ disabled */
  254. void netpoll_send_skb_on_dev(struct netpoll *np, struct sk_buff *skb,
  255. struct net_device *dev)
  256. {
  257. int status = NETDEV_TX_BUSY;
  258. unsigned long tries;
  259. const struct net_device_ops *ops = dev->netdev_ops;
  260. /* It is up to the caller to keep npinfo alive. */
  261. struct netpoll_info *npinfo;
  262. WARN_ON_ONCE(!irqs_disabled());
  263. npinfo = rcu_dereference_bh(np->dev->npinfo);
  264. if (!npinfo || !netif_running(dev) || !netif_device_present(dev)) {
  265. __kfree_skb(skb);
  266. return;
  267. }
  268. /* don't get messages out of order, and no recursion */
  269. if (skb_queue_len(&npinfo->txq) == 0 && !netpoll_owner_active(dev)) {
  270. struct netdev_queue *txq;
  271. txq = netdev_pick_tx(dev, skb);
  272. /* try until next clock tick */
  273. for (tries = jiffies_to_usecs(1)/USEC_PER_POLL;
  274. tries > 0; --tries) {
  275. if (__netif_tx_trylock(txq)) {
  276. if (!netif_xmit_stopped(txq)) {
  277. if (vlan_tx_tag_present(skb) &&
  278. !(netif_skb_features(skb) & NETIF_F_HW_VLAN_TX)) {
  279. skb = __vlan_put_tag(skb, vlan_tx_tag_get(skb));
  280. if (unlikely(!skb))
  281. break;
  282. skb->vlan_tci = 0;
  283. }
  284. status = ops->ndo_start_xmit(skb, dev);
  285. if (status == NETDEV_TX_OK)
  286. txq_trans_update(txq);
  287. }
  288. __netif_tx_unlock(txq);
  289. if (status == NETDEV_TX_OK)
  290. break;
  291. }
  292. /* tickle device maybe there is some cleanup */
  293. netpoll_poll_dev(np->dev);
  294. udelay(USEC_PER_POLL);
  295. }
  296. WARN_ONCE(!irqs_disabled(),
  297. "netpoll_send_skb_on_dev(): %s enabled interrupts in poll (%pF)\n",
  298. dev->name, ops->ndo_start_xmit);
  299. }
  300. if (status != NETDEV_TX_OK) {
  301. skb_queue_tail(&npinfo->txq, skb);
  302. schedule_delayed_work(&npinfo->tx_work,0);
  303. }
  304. }
  305. EXPORT_SYMBOL(netpoll_send_skb_on_dev);
  306. void netpoll_send_udp(struct netpoll *np, const char *msg, int len)
  307. {
  308. int total_len, ip_len, udp_len;
  309. struct sk_buff *skb;
  310. struct udphdr *udph;
  311. struct iphdr *iph;
  312. struct ethhdr *eth;
  313. static atomic_t ip_ident;
  314. udp_len = len + sizeof(*udph);
  315. if (!np->ipv6)
  316. ip_len = udp_len + sizeof(*iph);
  317. total_len = ip_len + LL_RESERVED_SPACE(np->dev);
  318. skb = find_skb(np, total_len + np->dev->needed_tailroom,
  319. total_len - len);
  320. if (!skb)
  321. return;
  322. skb_copy_to_linear_data(skb, msg, len);
  323. skb_put(skb, len);
  324. skb_push(skb, sizeof(*udph));
  325. skb_reset_transport_header(skb);
  326. udph = udp_hdr(skb);
  327. udph->source = htons(np->local_port);
  328. udph->dest = htons(np->remote_port);
  329. udph->len = htons(udp_len);
  330. if (!np->ipv6) {
  331. udph->check = 0;
  332. udph->check = csum_tcpudp_magic(np->local_ip.ip,
  333. np->remote_ip.ip,
  334. udp_len, IPPROTO_UDP,
  335. csum_partial(udph, udp_len, 0));
  336. if (udph->check == 0)
  337. udph->check = CSUM_MANGLED_0;
  338. skb_push(skb, sizeof(*iph));
  339. skb_reset_network_header(skb);
  340. iph = ip_hdr(skb);
  341. /* iph->version = 4; iph->ihl = 5; */
  342. put_unaligned(0x45, (unsigned char *)iph);
  343. iph->tos = 0;
  344. put_unaligned(htons(ip_len), &(iph->tot_len));
  345. iph->id = htons(atomic_inc_return(&ip_ident));
  346. iph->frag_off = 0;
  347. iph->ttl = 64;
  348. iph->protocol = IPPROTO_UDP;
  349. iph->check = 0;
  350. put_unaligned(np->local_ip.ip, &(iph->saddr));
  351. put_unaligned(np->remote_ip.ip, &(iph->daddr));
  352. iph->check = ip_fast_csum((unsigned char *)iph, iph->ihl);
  353. eth = (struct ethhdr *) skb_push(skb, ETH_HLEN);
  354. skb_reset_mac_header(skb);
  355. skb->protocol = eth->h_proto = htons(ETH_P_IP);
  356. }
  357. memcpy(eth->h_source, np->dev->dev_addr, ETH_ALEN);
  358. memcpy(eth->h_dest, np->remote_mac, ETH_ALEN);
  359. skb->dev = np->dev;
  360. netpoll_send_skb(np, skb);
  361. }
  362. EXPORT_SYMBOL(netpoll_send_udp);
  363. static void netpoll_neigh_reply(struct sk_buff *skb, struct netpoll_info *npinfo)
  364. {
  365. struct arphdr *arp;
  366. unsigned char *arp_ptr;
  367. int size, type = ARPOP_REPLY, ptype = ETH_P_ARP;
  368. __be32 sip, tip;
  369. unsigned char *sha;
  370. struct sk_buff *send_skb;
  371. struct netpoll *np, *tmp;
  372. unsigned long flags;
  373. int hlen, tlen;
  374. int hits = 0, proto;
  375. if (list_empty(&npinfo->rx_np))
  376. return;
  377. /* Before checking the packet, we do some early
  378. inspection whether this is interesting at all */
  379. spin_lock_irqsave(&npinfo->rx_lock, flags);
  380. list_for_each_entry_safe(np, tmp, &npinfo->rx_np, rx) {
  381. if (np->dev == skb->dev)
  382. hits++;
  383. }
  384. spin_unlock_irqrestore(&npinfo->rx_lock, flags);
  385. /* No netpoll struct is using this dev */
  386. if (!hits)
  387. return;
  388. proto = ntohs(eth_hdr(skb)->h_proto);
  389. if (proto == ETH_P_IP) {
  390. /* No arp on this interface */
  391. if (skb->dev->flags & IFF_NOARP)
  392. return;
  393. if (!pskb_may_pull(skb, arp_hdr_len(skb->dev)))
  394. return;
  395. skb_reset_network_header(skb);
  396. skb_reset_transport_header(skb);
  397. arp = arp_hdr(skb);
  398. if ((arp->ar_hrd != htons(ARPHRD_ETHER) &&
  399. arp->ar_hrd != htons(ARPHRD_IEEE802)) ||
  400. arp->ar_pro != htons(ETH_P_IP) ||
  401. arp->ar_op != htons(ARPOP_REQUEST))
  402. return;
  403. arp_ptr = (unsigned char *)(arp+1);
  404. /* save the location of the src hw addr */
  405. sha = arp_ptr;
  406. arp_ptr += skb->dev->addr_len;
  407. memcpy(&sip, arp_ptr, 4);
  408. arp_ptr += 4;
  409. /* If we actually cared about dst hw addr,
  410. it would get copied here */
  411. arp_ptr += skb->dev->addr_len;
  412. memcpy(&tip, arp_ptr, 4);
  413. /* Should we ignore arp? */
  414. if (ipv4_is_loopback(tip) || ipv4_is_multicast(tip))
  415. return;
  416. size = arp_hdr_len(skb->dev);
  417. spin_lock_irqsave(&npinfo->rx_lock, flags);
  418. list_for_each_entry_safe(np, tmp, &npinfo->rx_np, rx) {
  419. if (tip != np->local_ip.ip)
  420. continue;
  421. hlen = LL_RESERVED_SPACE(np->dev);
  422. tlen = np->dev->needed_tailroom;
  423. send_skb = find_skb(np, size + hlen + tlen, hlen);
  424. if (!send_skb)
  425. continue;
  426. skb_reset_network_header(send_skb);
  427. arp = (struct arphdr *) skb_put(send_skb, size);
  428. send_skb->dev = skb->dev;
  429. send_skb->protocol = htons(ETH_P_ARP);
  430. /* Fill the device header for the ARP frame */
  431. if (dev_hard_header(send_skb, skb->dev, ptype,
  432. sha, np->dev->dev_addr,
  433. send_skb->len) < 0) {
  434. kfree_skb(send_skb);
  435. continue;
  436. }
  437. /*
  438. * Fill out the arp protocol part.
  439. *
  440. * we only support ethernet device type,
  441. * which (according to RFC 1390) should
  442. * always equal 1 (Ethernet).
  443. */
  444. arp->ar_hrd = htons(np->dev->type);
  445. arp->ar_pro = htons(ETH_P_IP);
  446. arp->ar_hln = np->dev->addr_len;
  447. arp->ar_pln = 4;
  448. arp->ar_op = htons(type);
  449. arp_ptr = (unsigned char *)(arp + 1);
  450. memcpy(arp_ptr, np->dev->dev_addr, np->dev->addr_len);
  451. arp_ptr += np->dev->addr_len;
  452. memcpy(arp_ptr, &tip, 4);
  453. arp_ptr += 4;
  454. memcpy(arp_ptr, sha, np->dev->addr_len);
  455. arp_ptr += np->dev->addr_len;
  456. memcpy(arp_ptr, &sip, 4);
  457. netpoll_send_skb(np, send_skb);
  458. /* If there are several rx_hooks for the same address,
  459. we're fine by sending a single reply */
  460. break;
  461. }
  462. spin_unlock_irqrestore(&npinfo->rx_lock, flags);
  463. }
  464. }
  465. int __netpoll_rx(struct sk_buff *skb, struct netpoll_info *npinfo)
  466. {
  467. int proto, len, ulen;
  468. int hits = 0;
  469. const struct iphdr *iph;
  470. struct udphdr *uh;
  471. struct netpoll *np, *tmp;
  472. if (list_empty(&npinfo->rx_np))
  473. goto out;
  474. if (skb->dev->type != ARPHRD_ETHER)
  475. goto out;
  476. /* check if netpoll clients need ARP */
  477. if (skb->protocol == htons(ETH_P_ARP) &&
  478. atomic_read(&trapped)) {
  479. skb_queue_tail(&npinfo->neigh_tx, skb);
  480. return 1;
  481. }
  482. if (skb->protocol == cpu_to_be16(ETH_P_8021Q)) {
  483. skb = vlan_untag(skb);
  484. if (unlikely(!skb))
  485. goto out;
  486. }
  487. proto = ntohs(eth_hdr(skb)->h_proto);
  488. if (proto != ETH_P_IP && proto != ETH_P_IPV6)
  489. goto out;
  490. if (skb->pkt_type == PACKET_OTHERHOST)
  491. goto out;
  492. if (skb_shared(skb))
  493. goto out;
  494. if (proto == ETH_P_IP) {
  495. if (!pskb_may_pull(skb, sizeof(struct iphdr)))
  496. goto out;
  497. iph = (struct iphdr *)skb->data;
  498. if (iph->ihl < 5 || iph->version != 4)
  499. goto out;
  500. if (!pskb_may_pull(skb, iph->ihl*4))
  501. goto out;
  502. iph = (struct iphdr *)skb->data;
  503. if (ip_fast_csum((u8 *)iph, iph->ihl) != 0)
  504. goto out;
  505. len = ntohs(iph->tot_len);
  506. if (skb->len < len || len < iph->ihl*4)
  507. goto out;
  508. /*
  509. * Our transport medium may have padded the buffer out.
  510. * Now We trim to the true length of the frame.
  511. */
  512. if (pskb_trim_rcsum(skb, len))
  513. goto out;
  514. iph = (struct iphdr *)skb->data;
  515. if (iph->protocol != IPPROTO_UDP)
  516. goto out;
  517. len -= iph->ihl*4;
  518. uh = (struct udphdr *)(((char *)iph) + iph->ihl*4);
  519. ulen = ntohs(uh->len);
  520. if (ulen != len)
  521. goto out;
  522. if (checksum_udp(skb, uh, ulen, iph->saddr, iph->daddr))
  523. goto out;
  524. list_for_each_entry_safe(np, tmp, &npinfo->rx_np, rx) {
  525. if (np->local_ip.ip && np->local_ip.ip != iph->daddr)
  526. continue;
  527. if (np->remote_ip.ip && np->remote_ip.ip != iph->saddr)
  528. continue;
  529. if (np->local_port && np->local_port != ntohs(uh->dest))
  530. continue;
  531. np->rx_hook(np, ntohs(uh->source),
  532. (char *)(uh+1),
  533. ulen - sizeof(struct udphdr));
  534. hits++;
  535. }
  536. }
  537. if (!hits)
  538. goto out;
  539. kfree_skb(skb);
  540. return 1;
  541. out:
  542. if (atomic_read(&trapped)) {
  543. kfree_skb(skb);
  544. return 1;
  545. }
  546. return 0;
  547. }
  548. void netpoll_print_options(struct netpoll *np)
  549. {
  550. np_info(np, "local port %d\n", np->local_port);
  551. if (!np->ipv6)
  552. np_info(np, "local IPv4 address %pI4\n", &np->local_ip.ip);
  553. np_info(np, "interface '%s'\n", np->dev_name);
  554. np_info(np, "remote port %d\n", np->remote_port);
  555. if (!np->ipv6)
  556. np_info(np, "remote IPv4 address %pI4\n", &np->remote_ip.ip);
  557. np_info(np, "remote ethernet address %pM\n", np->remote_mac);
  558. }
  559. EXPORT_SYMBOL(netpoll_print_options);
  560. static int netpoll_parse_ip_addr(const char *str, union inet_addr *addr)
  561. {
  562. const char *end;
  563. if (!strchr(str, ':') &&
  564. in4_pton(str, -1, (void *)addr, -1, &end) > 0) {
  565. if (!*end)
  566. return 0;
  567. }
  568. if (in6_pton(str, -1, addr->in6.s6_addr, -1, &end) > 0) {
  569. #if IS_ENABLED(CONFIG_IPV6)
  570. if (!*end)
  571. return 1;
  572. #else
  573. return -1;
  574. #endif
  575. }
  576. return -1;
  577. }
  578. int netpoll_parse_options(struct netpoll *np, char *opt)
  579. {
  580. char *cur=opt, *delim;
  581. int ipv6;
  582. if (*cur != '@') {
  583. if ((delim = strchr(cur, '@')) == NULL)
  584. goto parse_failed;
  585. *delim = 0;
  586. if (kstrtou16(cur, 10, &np->local_port))
  587. goto parse_failed;
  588. cur = delim;
  589. }
  590. cur++;
  591. if (*cur != '/') {
  592. if ((delim = strchr(cur, '/')) == NULL)
  593. goto parse_failed;
  594. *delim = 0;
  595. ipv6 = netpoll_parse_ip_addr(cur, &np->local_ip);
  596. if (ipv6 < 0)
  597. goto parse_failed;
  598. else
  599. np->ipv6 = (bool)ipv6;
  600. cur = delim;
  601. }
  602. cur++;
  603. if (*cur != ',') {
  604. /* parse out dev name */
  605. if ((delim = strchr(cur, ',')) == NULL)
  606. goto parse_failed;
  607. *delim = 0;
  608. strlcpy(np->dev_name, cur, sizeof(np->dev_name));
  609. cur = delim;
  610. }
  611. cur++;
  612. if (*cur != '@') {
  613. /* dst port */
  614. if ((delim = strchr(cur, '@')) == NULL)
  615. goto parse_failed;
  616. *delim = 0;
  617. if (*cur == ' ' || *cur == '\t')
  618. np_info(np, "warning: whitespace is not allowed\n");
  619. if (kstrtou16(cur, 10, &np->remote_port))
  620. goto parse_failed;
  621. cur = delim;
  622. }
  623. cur++;
  624. /* dst ip */
  625. if ((delim = strchr(cur, '/')) == NULL)
  626. goto parse_failed;
  627. *delim = 0;
  628. ipv6 = netpoll_parse_ip_addr(cur, &np->remote_ip);
  629. if (ipv6 < 0)
  630. goto parse_failed;
  631. else if (np->ipv6 != (bool)ipv6)
  632. goto parse_failed;
  633. else
  634. np->ipv6 = (bool)ipv6;
  635. cur = delim + 1;
  636. if (*cur != 0) {
  637. /* MAC address */
  638. if (!mac_pton(cur, np->remote_mac))
  639. goto parse_failed;
  640. }
  641. netpoll_print_options(np);
  642. return 0;
  643. parse_failed:
  644. np_info(np, "couldn't parse config at '%s'!\n", cur);
  645. return -1;
  646. }
  647. EXPORT_SYMBOL(netpoll_parse_options);
  648. int __netpoll_setup(struct netpoll *np, struct net_device *ndev, gfp_t gfp)
  649. {
  650. struct netpoll_info *npinfo;
  651. const struct net_device_ops *ops;
  652. unsigned long flags;
  653. int err;
  654. np->dev = ndev;
  655. strlcpy(np->dev_name, ndev->name, IFNAMSIZ);
  656. if ((ndev->priv_flags & IFF_DISABLE_NETPOLL) ||
  657. !ndev->netdev_ops->ndo_poll_controller) {
  658. np_err(np, "%s doesn't support polling, aborting\n",
  659. np->dev_name);
  660. err = -ENOTSUPP;
  661. goto out;
  662. }
  663. if (!ndev->npinfo) {
  664. npinfo = kmalloc(sizeof(*npinfo), gfp);
  665. if (!npinfo) {
  666. err = -ENOMEM;
  667. goto out;
  668. }
  669. npinfo->rx_flags = 0;
  670. INIT_LIST_HEAD(&npinfo->rx_np);
  671. spin_lock_init(&npinfo->rx_lock);
  672. skb_queue_head_init(&npinfo->neigh_tx);
  673. skb_queue_head_init(&npinfo->txq);
  674. INIT_DELAYED_WORK(&npinfo->tx_work, queue_process);
  675. atomic_set(&npinfo->refcnt, 1);
  676. ops = np->dev->netdev_ops;
  677. if (ops->ndo_netpoll_setup) {
  678. err = ops->ndo_netpoll_setup(ndev, npinfo, gfp);
  679. if (err)
  680. goto free_npinfo;
  681. }
  682. } else {
  683. npinfo = ndev->npinfo;
  684. atomic_inc(&npinfo->refcnt);
  685. }
  686. npinfo->netpoll = np;
  687. if (np->rx_hook) {
  688. spin_lock_irqsave(&npinfo->rx_lock, flags);
  689. npinfo->rx_flags |= NETPOLL_RX_ENABLED;
  690. list_add_tail(&np->rx, &npinfo->rx_np);
  691. spin_unlock_irqrestore(&npinfo->rx_lock, flags);
  692. }
  693. /* last thing to do is link it to the net device structure */
  694. rcu_assign_pointer(ndev->npinfo, npinfo);
  695. return 0;
  696. free_npinfo:
  697. kfree(npinfo);
  698. out:
  699. return err;
  700. }
  701. EXPORT_SYMBOL_GPL(__netpoll_setup);
  702. int netpoll_setup(struct netpoll *np)
  703. {
  704. struct net_device *ndev = NULL;
  705. struct in_device *in_dev;
  706. int err;
  707. if (np->dev_name)
  708. ndev = dev_get_by_name(&init_net, np->dev_name);
  709. if (!ndev) {
  710. np_err(np, "%s doesn't exist, aborting\n", np->dev_name);
  711. return -ENODEV;
  712. }
  713. if (netdev_master_upper_dev_get(ndev)) {
  714. np_err(np, "%s is a slave device, aborting\n", np->dev_name);
  715. err = -EBUSY;
  716. goto put;
  717. }
  718. if (!netif_running(ndev)) {
  719. unsigned long atmost, atleast;
  720. np_info(np, "device %s not up yet, forcing it\n", np->dev_name);
  721. rtnl_lock();
  722. err = dev_open(ndev);
  723. rtnl_unlock();
  724. if (err) {
  725. np_err(np, "failed to open %s\n", ndev->name);
  726. goto put;
  727. }
  728. atleast = jiffies + HZ/10;
  729. atmost = jiffies + carrier_timeout * HZ;
  730. while (!netif_carrier_ok(ndev)) {
  731. if (time_after(jiffies, atmost)) {
  732. np_notice(np, "timeout waiting for carrier\n");
  733. break;
  734. }
  735. msleep(1);
  736. }
  737. /* If carrier appears to come up instantly, we don't
  738. * trust it and pause so that we don't pump all our
  739. * queued console messages into the bitbucket.
  740. */
  741. if (time_before(jiffies, atleast)) {
  742. np_notice(np, "carrier detect appears untrustworthy, waiting 4 seconds\n");
  743. msleep(4000);
  744. }
  745. }
  746. if (!np->local_ip.ip) {
  747. if (!np->ipv6) {
  748. rcu_read_lock();
  749. in_dev = __in_dev_get_rcu(ndev);
  750. if (!in_dev || !in_dev->ifa_list) {
  751. rcu_read_unlock();
  752. np_err(np, "no IP address for %s, aborting\n",
  753. np->dev_name);
  754. err = -EDESTADDRREQ;
  755. goto put;
  756. }
  757. np->local_ip.ip = in_dev->ifa_list->ifa_local;
  758. rcu_read_unlock();
  759. np_info(np, "local IP %pI4\n", &np->local_ip.ip);
  760. }
  761. }
  762. /* fill up the skb queue */
  763. refill_skbs();
  764. rtnl_lock();
  765. err = __netpoll_setup(np, ndev, GFP_KERNEL);
  766. rtnl_unlock();
  767. if (err)
  768. goto put;
  769. return 0;
  770. put:
  771. dev_put(ndev);
  772. return err;
  773. }
  774. EXPORT_SYMBOL(netpoll_setup);
  775. static int __init netpoll_init(void)
  776. {
  777. skb_queue_head_init(&skb_pool);
  778. return 0;
  779. }
  780. core_initcall(netpoll_init);
  781. static void rcu_cleanup_netpoll_info(struct rcu_head *rcu_head)
  782. {
  783. struct netpoll_info *npinfo =
  784. container_of(rcu_head, struct netpoll_info, rcu);
  785. skb_queue_purge(&npinfo->neigh_tx);
  786. skb_queue_purge(&npinfo->txq);
  787. /* we can't call cancel_delayed_work_sync here, as we are in softirq */
  788. cancel_delayed_work(&npinfo->tx_work);
  789. /* clean after last, unfinished work */
  790. __skb_queue_purge(&npinfo->txq);
  791. /* now cancel it again */
  792. cancel_delayed_work(&npinfo->tx_work);
  793. kfree(npinfo);
  794. }
  795. void __netpoll_cleanup(struct netpoll *np)
  796. {
  797. struct netpoll_info *npinfo;
  798. unsigned long flags;
  799. npinfo = np->dev->npinfo;
  800. if (!npinfo)
  801. return;
  802. if (!list_empty(&npinfo->rx_np)) {
  803. spin_lock_irqsave(&npinfo->rx_lock, flags);
  804. list_del(&np->rx);
  805. if (list_empty(&npinfo->rx_np))
  806. npinfo->rx_flags &= ~NETPOLL_RX_ENABLED;
  807. spin_unlock_irqrestore(&npinfo->rx_lock, flags);
  808. }
  809. if (atomic_dec_and_test(&npinfo->refcnt)) {
  810. const struct net_device_ops *ops;
  811. ops = np->dev->netdev_ops;
  812. if (ops->ndo_netpoll_cleanup)
  813. ops->ndo_netpoll_cleanup(np->dev);
  814. RCU_INIT_POINTER(np->dev->npinfo, NULL);
  815. call_rcu_bh(&npinfo->rcu, rcu_cleanup_netpoll_info);
  816. }
  817. }
  818. EXPORT_SYMBOL_GPL(__netpoll_cleanup);
  819. static void rcu_cleanup_netpoll(struct rcu_head *rcu_head)
  820. {
  821. struct netpoll *np = container_of(rcu_head, struct netpoll, rcu);
  822. __netpoll_cleanup(np);
  823. kfree(np);
  824. }
  825. void __netpoll_free_rcu(struct netpoll *np)
  826. {
  827. call_rcu_bh(&np->rcu, rcu_cleanup_netpoll);
  828. }
  829. EXPORT_SYMBOL_GPL(__netpoll_free_rcu);
  830. void netpoll_cleanup(struct netpoll *np)
  831. {
  832. if (!np->dev)
  833. return;
  834. rtnl_lock();
  835. __netpoll_cleanup(np);
  836. rtnl_unlock();
  837. dev_put(np->dev);
  838. np->dev = NULL;
  839. }
  840. EXPORT_SYMBOL(netpoll_cleanup);
  841. int netpoll_trap(void)
  842. {
  843. return atomic_read(&trapped);
  844. }
  845. EXPORT_SYMBOL(netpoll_trap);
  846. void netpoll_set_trap(int trap)
  847. {
  848. if (trap)
  849. atomic_inc(&trapped);
  850. else
  851. atomic_dec(&trapped);
  852. }
  853. EXPORT_SYMBOL(netpoll_set_trap);