netpoll.c 29 KB

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