| 1 |
|
| 2 | |
| 3 | * Copyright (C) 2015-2019 Jason A. Donenfeld <[email protected]>. All Rights Reserved.
|
| 4 |
|
| 5 |
|
| 6 | #ifndef _WG_QUEUEING_H
|
| 7 | #define _WG_QUEUEING_H
|
| 8 |
|
| 9 | #include "peer.h"
|
| 10 | #include <linux/types.h>
|
| 11 | #include <linux/skbuff.h>
|
| 12 | #include <linux/ip.h>
|
| 13 | #include <linux/ipv6.h>
|
| 14 | #include <net/ip_tunnels.h>
|
| 15 |
|
| 16 | struct wg_device;
|
| 17 | struct wg_peer;
|
| 18 | struct multicore_worker;
|
| 19 | struct crypt_queue;
|
| 20 | struct prev_queue;
|
| 21 | struct sk_buff;
|
| 22 |
|
| 23 |
|
| 24 | int wg_packet_queue_init(struct crypt_queue *queue, work_func_t function,
|
| 25 | unsigned int len);
|
| 26 | void wg_packet_queue_free(struct crypt_queue *queue, bool purge);
|
| 27 | struct multicore_worker __percpu *
|
| 28 | wg_packet_percpu_multicore_worker_alloc(work_func_t function, void *ptr);
|
| 29 |
|
| 30 |
|
| 31 | void wg_packet_receive(struct wg_device *wg, struct sk_buff *skb);
|
| 32 | void wg_packet_handshake_receive_worker(struct work_struct *work);
|
| 33 |
|
| 34 | int wg_packet_rx_poll(struct napi_struct *napi, int budget);
|
| 35 |
|
| 36 | void wg_packet_decrypt_worker(struct work_struct *work);
|
| 37 |
|
| 38 |
|
| 39 | void wg_packet_send_queued_handshake_initiation(struct wg_peer *peer,
|
| 40 | bool is_retry);
|
| 41 | void wg_packet_send_handshake_response(struct wg_peer *peer);
|
| 42 | void wg_packet_send_handshake_cookie(struct wg_device *wg,
|
| 43 | struct sk_buff *initiating_skb,
|
| 44 | __le32 sender_index);
|
| 45 | void wg_packet_send_keepalive(struct wg_peer *peer);
|
| 46 | void wg_packet_purge_staged_packets(struct wg_peer *peer);
|
| 47 | void wg_packet_send_staged_packets(struct wg_peer *peer);
|
| 48 |
|
| 49 | void wg_packet_handshake_send_worker(struct work_struct *work);
|
| 50 | void wg_packet_tx_worker(struct work_struct *work);
|
| 51 | void wg_packet_encrypt_worker(struct work_struct *work);
|
| 52 |
|
| 53 | enum packet_state {
|
| 54 | PACKET_STATE_UNCRYPTED,
|
| 55 | PACKET_STATE_CRYPTED,
|
| 56 | PACKET_STATE_DEAD
|
| 57 | };
|
| 58 |
|
| 59 | struct packet_cb {
|
| 60 | u64 nonce;
|
| 61 | struct noise_keypair *keypair;
|
| 62 | atomic_t state;
|
| 63 | u32 mtu;
|
| 64 | u8 ds;
|
| 65 | };
|
| 66 |
|
| 67 | #define PACKET_CB(skb) ((struct packet_cb *)((skb)->cb))
|
| 68 | #define PACKET_PEER(skb) (PACKET_CB(skb)->keypair->entry.peer)
|
| 69 |
|
| 70 | static inline bool wg_check_packet_protocol(struct sk_buff *skb)
|
| 71 | {
|
| 72 | __be16 real_protocol = ip_tunnel_parse_protocol(skb);
|
| 73 | return real_protocol && skb->protocol == real_protocol;
|
| 74 | }
|
| 75 |
|
| 76 | static inline void wg_reset_packet(struct sk_buff *skb, bool encapsulating)
|
| 77 | {
|
| 78 | u8 l4_hash = skb->l4_hash;
|
| 79 | u8 sw_hash = skb->sw_hash;
|
| 80 | u32 hash = skb->hash;
|
| 81 | u8 tstamp_type = skb->tstamp_type;
|
| 82 | skb_scrub_packet(skb, true);
|
| 83 | memset(&skb->headers, 0, sizeof(skb->headers));
|
| 84 | if (encapsulating) {
|
| 85 | skb->l4_hash = l4_hash;
|
| 86 | skb->sw_hash = sw_hash;
|
| 87 | skb->hash = hash;
|
| 88 | skb->tstamp_type = tstamp_type;
|
| 89 | }
|
| 90 | skb->queue_mapping = 0;
|
| 91 | skb->nohdr = 0;
|
| 92 | skb->peeked = 0;
|
| 93 | skb->mac_len = 0;
|
| 94 | skb->dev = NULL;
|
| 95 | #ifdef CONFIG_NET_SCHED
|
| 96 | skb->tc_index = 0;
|
| 97 | #endif
|
| 98 | skb_reset_redirect(skb);
|
| 99 | skb->hdr_len = skb_headroom(skb);
|
| 100 | skb_reset_mac_header(skb);
|
| 101 | skb_reset_network_header(skb);
|
| 102 | skb_reset_transport_header(skb);
|
| 103 | skb_probe_transport_header(skb);
|
| 104 | skb_reset_inner_headers(skb);
|
| 105 | }
|
| 106 |
|
| 107 | static inline int wg_cpumask_choose_online(int *stored_cpu, unsigned int id)
|
| 108 | {
|
| 109 | unsigned int cpu = *stored_cpu;
|
| 110 |
|
| 111 | while (unlikely(cpu >= nr_cpu_ids || !cpu_online(cpu)))
|
| 112 | cpu = *stored_cpu = cpumask_nth(id % num_online_cpus(), cpu_online_mask);
|
| 113 |
|
| 114 | return cpu;
|
| 115 | }
|
| 116 |
|
| 117 | |
| 118 | * unlocked, so it could return the same CPU twice. Adding locking or using
|
| 119 | * atomic sequence numbers is slower though, and the consequences of racing are
|
| 120 | * harmless, so live with it.
|
| 121 |
|
| 122 | static inline int wg_cpumask_next_online(int *last_cpu)
|
| 123 | {
|
| 124 | int cpu = cpumask_next(READ_ONCE(*last_cpu), cpu_online_mask);
|
| 125 | if (cpu >= nr_cpu_ids)
|
| 126 | cpu = cpumask_first(cpu_online_mask);
|
| 127 | WRITE_ONCE(*last_cpu, cpu);
|
| 128 | return cpu;
|
| 129 | }
|
| 130 |
|
| 131 | void wg_prev_queue_init(struct prev_queue *queue);
|
| 132 |
|
| 133 |
|
| 134 | bool wg_prev_queue_enqueue(struct prev_queue *queue, struct sk_buff *skb);
|
| 135 |
|
| 136 |
|
| 137 | struct sk_buff *wg_prev_queue_dequeue(struct prev_queue *queue);
|
| 138 |
|
| 139 |
|
| 140 | static inline struct sk_buff *wg_prev_queue_peek(struct prev_queue *queue)
|
| 141 | {
|
| 142 | if (queue->peeked)
|
| 143 | return queue->peeked;
|
| 144 | queue->peeked = wg_prev_queue_dequeue(queue);
|
| 145 | return queue->peeked;
|
| 146 | }
|
| 147 |
|
| 148 |
|
| 149 | static inline void wg_prev_queue_drop_peeked(struct prev_queue *queue)
|
| 150 | {
|
| 151 | queue->peeked = NULL;
|
| 152 | }
|
| 153 |
|
| 154 | static inline int wg_queue_enqueue_per_device_and_peer(
|
| 155 | struct crypt_queue *device_queue, struct prev_queue *peer_queue,
|
| 156 | struct sk_buff *skb, struct workqueue_struct *wq)
|
| 157 | {
|
| 158 | int cpu;
|
| 159 |
|
| 160 | atomic_set_release(&PACKET_CB(skb)->state, PACKET_STATE_UNCRYPTED);
|
| 161 | |
| 162 | * will wait for the state to change to CRYPTED or DEAD before.
|
| 163 |
|
| 164 | if (unlikely(!wg_prev_queue_enqueue(peer_queue, skb)))
|
| 165 | return -ENOSPC;
|
| 166 |
|
| 167 | |
| 168 | * packet as soon as it can.
|
| 169 |
|
| 170 | cpu = wg_cpumask_next_online(&device_queue->last_cpu);
|
| 171 | if (unlikely(ptr_ring_produce_bh(&device_queue->ring, skb)))
|
| 172 | return -EPIPE;
|
| 173 | queue_work_on(cpu, wq, &per_cpu_ptr(device_queue->worker, cpu)->work);
|
| 174 | return 0;
|
| 175 | }
|
| 176 |
|
| 177 | static inline void wg_queue_enqueue_per_peer_tx(struct sk_buff *skb, enum packet_state state)
|
| 178 | {
|
| 179 | |
| 180 | * peer can be freed from below us.
|
| 181 |
|
| 182 | struct wg_peer *peer = wg_peer_get(PACKET_PEER(skb));
|
| 183 |
|
| 184 | atomic_set_release(&PACKET_CB(skb)->state, state);
|
| 185 | queue_work_on(wg_cpumask_choose_online(&peer->serial_work_cpu, peer->internal_id),
|
| 186 | peer->device->packet_crypt_wq, &peer->transmit_packet_work);
|
| 187 | wg_peer_put(peer);
|
| 188 | }
|
| 189 |
|
| 190 | static inline void wg_queue_enqueue_per_peer_rx(struct sk_buff *skb, enum packet_state state)
|
| 191 | {
|
| 192 | |
| 193 | * peer can be freed from below us.
|
| 194 |
|
| 195 | struct wg_peer *peer = wg_peer_get(PACKET_PEER(skb));
|
| 196 |
|
| 197 | atomic_set_release(&PACKET_CB(skb)->state, state);
|
| 198 | napi_schedule(&peer->napi);
|
| 199 | wg_peer_put(peer);
|
| 200 | }
|
| 201 |
|
| 202 | #ifdef DEBUG
|
| 203 | bool wg_packet_counter_selftest(void);
|
| 204 | #endif
|
| 205 |
|
| 206 | #endif
|