--- zzzz-none-000/linux-3.10.107/drivers/net/ppp/pptp.c 2017-06-27 09:49:32.000000000 +0000 +++ scorpion-7490-727/linux-3.10.107/drivers/net/ppp/pptp.c 2021-02-04 17:41:59.000000000 +0000 @@ -28,8 +28,6 @@ #include #include #include -#include -#include #include #include @@ -47,7 +45,7 @@ #define MAX_CALLID 65535 static DECLARE_BITMAP(callid_bitmap, MAX_CALLID + 1); -static struct pppox_sock **callid_sock; +static struct pppox_sock __rcu **callid_sock; static DEFINE_SPINLOCK(chan_lock); @@ -55,6 +53,8 @@ static const struct ppp_channel_ops pptp_chan_ops; static const struct proto_ops pptp_ops; +static pptp_gre_seq_offload_callback_t __rcu pptp_gre_offload_xmit_cb; + #define PPP_LCP_ECHOREQ 0x09 #define PPP_LCP_ECHOREP 0x0A #define SC_RCV_BITS (SC_RCV_B7_1|SC_RCV_B7_0|SC_RCV_ODDP|SC_RCV_EVNP) @@ -83,11 +83,11 @@ struct pptp_gre_header { u8 flags; u8 ver; - u16 protocol; - u16 payload_len; - u16 call_id; - u32 seq; - u32 ack; + __be16 protocol; + __be16 payload_len; + __be16 call_id; + __be32 seq; + __be32 ack; } __packed; static struct pppox_sock *lookup_chan(u16 call_id, __be32 s_addr) @@ -131,24 +131,74 @@ return i < MAX_CALLID; } -static int add_chan(struct pppox_sock *sock) +/* Search a pptp session based on peer call id and peer ip address */ +static int lookup_session_dst(struct pptp_opt *opt, u16 call_id, __be32 d_addr) +{ + struct pppox_sock *sock; + int i = 1; + + rcu_read_lock(); + for_each_set_bit_from(i, callid_bitmap, MAX_CALLID) { + sock = rcu_dereference(callid_sock[i]); + if (!sock) + continue; + + if (sock->proto.pptp.dst_addr.call_id == call_id && + sock->proto.pptp.dst_addr.sin_addr.s_addr == d_addr) { + sock_hold(sk_pppox(sock)); + memcpy(opt, &sock->proto.pptp, sizeof(struct pptp_opt)); + sock_put(sk_pppox(sock)); + rcu_read_unlock(); + return 0; + } + } + rcu_read_unlock(); + return -EINVAL; +} + +/* If offload mode set then this function sends all packets to + * offload module instead of network stack + */ +static int pptp_client_skb_xmit(struct sk_buff *skb, + struct net_device *pptp_dev) +{ + pptp_gre_seq_offload_callback_t pptp_gre_offload_cb_f; + int ret; + + rcu_read_lock(); + pptp_gre_offload_cb_f = rcu_dereference(pptp_gre_offload_xmit_cb); + + if (!pptp_gre_offload_cb_f) { + rcu_read_unlock(); + return -1; + } + + ret = pptp_gre_offload_cb_f(skb, pptp_dev); + rcu_read_unlock(); + return ret; +} + +static int add_chan(struct pppox_sock *sock, + struct pptp_addr *sa) { static int call_id; spin_lock(&chan_lock); - if (!sock->proto.pptp.src_addr.call_id) { + if (!sa->call_id) { call_id = find_next_zero_bit(callid_bitmap, MAX_CALLID, call_id + 1); if (call_id == MAX_CALLID) { call_id = find_next_zero_bit(callid_bitmap, MAX_CALLID, 1); if (call_id == MAX_CALLID) goto out_err; } - sock->proto.pptp.src_addr.call_id = call_id; - } else if (test_bit(sock->proto.pptp.src_addr.call_id, callid_bitmap)) + sa->call_id = call_id; + } else if (test_bit(sa->call_id, callid_bitmap)) { goto out_err; + } - set_bit(sock->proto.pptp.src_addr.call_id, callid_bitmap); - rcu_assign_pointer(callid_sock[sock->proto.pptp.src_addr.call_id], sock); + sock->proto.pptp.src_addr = *sa; + set_bit(sa->call_id, callid_bitmap); + rcu_assign_pointer(callid_sock[sa->call_id], sock); spin_unlock(&chan_lock); return 0; @@ -171,6 +221,7 @@ { struct sock *sk = (struct sock *) chan->private; struct pppox_sock *po = pppox_sk(sk); + struct net *net = sock_net(sk); struct pptp_opt *opt = &po->proto.pptp; struct pptp_gre_header *hdr; unsigned int header_len = sizeof(*hdr); @@ -183,13 +234,16 @@ struct rtable *rt; struct net_device *tdev; + struct net_device *pptp_dev; struct iphdr *iph; int max_headroom; + int pptp_ifindex; + int ret; if (sk_pppox(po)->sk_state & PPPOX_DEAD) goto tx_error; - rt = ip_route_output_ports(sock_net(sk), &fl4, NULL, + rt = ip_route_output_ports(net, &fl4, NULL, opt->dst_addr.sin_addr.s_addr, opt->src_addr.sin_addr.s_addr, 0, 0, IPPROTO_GRE, @@ -281,12 +335,36 @@ nf_reset(skb); skb->ip_summed = CHECKSUM_NONE; - ip_select_ident(skb, NULL); + ip_select_ident(net, skb, NULL); ip_send_check(iph); - ip_local_out(skb); - return 1; + pptp_ifindex = ppp_dev_index(chan); + + /* set incoming interface as the ppp interface */ + if (skb->skb_iif) + skb->skb_iif = pptp_ifindex; + + /* If the PPTP GRE seq number offload module is not enabled yet + * then sends all PPTP GRE packets through linux network stack + */ + if (!opt->pptp_offload_mode) { + ip_local_out(net, skb->sk, skb); + return 1; + } + pptp_dev = dev_get_by_index(&init_net, pptp_ifindex); + if (!pptp_dev) + goto tx_error; + + /* If PPTP offload module is enabled then forward all PPTP GRE + * packets to PPTP GRE offload module + */ + ret = pptp_client_skb_xmit(skb, pptp_dev); + dev_put(pptp_dev); + if (ret < 0) + goto tx_error; + + return 1; tx_error: kfree_skb(skb); return 1; @@ -342,6 +420,13 @@ goto drop; payload = skb->data + headersize; + + /* If offload is enabled, we expect the offload module + * to handle PPTP GRE sequence number checks + */ + if (opt->pptp_offload_mode) + goto allow_packet; + /* check for expected sequence number */ if (seq < opt->seq_recv + 1 || WRAPPED(opt->seq_recv, seq)) { if ((payload[0] == PPP_ALLSTATIONS) && (payload[1] == PPP_UI) && @@ -404,6 +489,7 @@ if (po) { skb_dst_drop(skb); nf_reset(skb); + skb->skb_iif = ppp_dev_index(&po->chan); return sk_receive_skb(sk_pppox(po), skb, 0); } drop: @@ -417,7 +503,6 @@ struct sock *sk = sock->sk; struct sockaddr_pppox *sp = (struct sockaddr_pppox *) uservaddr; struct pppox_sock *po = pppox_sk(sk); - struct pptp_opt *opt = &po->proto.pptp; int error = 0; if (sockaddr_len < sizeof(struct sockaddr_pppox)) @@ -425,10 +510,22 @@ lock_sock(sk); - opt->src_addr = sp->sa_addr.pptp; - if (add_chan(po)) + if (sk->sk_state & PPPOX_DEAD) { + error = -EALREADY; + goto out; + } + + if (sk->sk_state & PPPOX_BOUND) { + error = -EBUSY; + goto out; + } + + if (add_chan(po, &sp->sa_addr.pptp)) error = -EBUSY; + else + sk->sk_state |= PPPOX_BOUND; +out: release_sock(sk); return error; } @@ -499,8 +596,8 @@ } opt->dst_addr = sp->sa_addr.pptp; - sk->sk_state = PPPOX_CONNECTED; - + sk->sk_state |= PPPOX_CONNECTED; + opt->pptp_offload_mode = false; end: release_sock(sk); return error; @@ -567,14 +664,14 @@ skb_queue_purge(&sk->sk_receive_queue); } -static int pptp_create(struct net *net, struct socket *sock) +static int pptp_create(struct net *net, struct socket *sock, int kern) { int error = -ENOMEM; struct sock *sk; struct pppox_sock *po; struct pptp_opt *opt; - sk = sk_alloc(net, PF_PPPOX, GFP_KERNEL, &pptp_sk_proto); + sk = sk_alloc(net, PF_PPPOX, GFP_KERNEL, &pptp_sk_proto, kern); if (!sk) goto out; @@ -632,9 +729,155 @@ return err; } +/* pptp_channel_addressing_get() + * Return PPTP channel specific addressing information. + */ +void pptp_channel_addressing_get(struct pptp_opt *opt, struct ppp_channel *chan) +{ + struct sock *sk; + struct pppox_sock *po; + + if (!opt) + return; + + sk = (struct sock *)chan->private; + if (!sk) + return; + + sock_hold(sk); + + /* This is very unlikely, but check the socket is connected state */ + if (unlikely(sock_flag(sk, SOCK_DEAD) || + !(sk->sk_state & PPPOX_CONNECTED))) { + sock_put(sk); + return; + } + + po = pppox_sk(sk); + memcpy(opt, &po->proto.pptp, sizeof(struct pptp_opt)); + sock_put(sk); +} +EXPORT_SYMBOL(pptp_channel_addressing_get); + +/* pptp_session_find() + * Search and return a PPTP session info based on peer callid and IP + * address. The function accepts the parameters in network byte order. + */ +int pptp_session_find(struct pptp_opt *opt, __be16 peer_call_id, + __be32 peer_ip_addr) +{ + if (!opt) + return -EINVAL; + + return lookup_session_dst(opt, ntohs(peer_call_id), peer_ip_addr); +} +EXPORT_SYMBOL(pptp_session_find); + + /* Function to change the offload mode true/false for a PPTP session */ +static int pptp_set_offload_mode(bool accel_mode, + __be16 peer_call_id, __be32 peer_ip_addr) +{ + struct pppox_sock *sock; + int i = 1; + + rcu_read_lock(); + for_each_set_bit_from(i, callid_bitmap, MAX_CALLID) { + sock = rcu_dereference(callid_sock[i]); + if (!sock) + continue; + + if (sock->proto.pptp.dst_addr.call_id == peer_call_id && + sock->proto.pptp.dst_addr.sin_addr.s_addr == peer_ip_addr) { + sock_hold(sk_pppox(sock)); + sock->proto.pptp.pptp_offload_mode = accel_mode; + sock_put(sk_pppox(sock)); + rcu_read_unlock(); + return 0; + } + } + rcu_read_unlock(); + return -EINVAL; +} + +/* Enable the PPTP session offload flag */ +int pptp_session_enable_offload_mode(__be16 peer_call_id, __be32 peer_ip_addr) +{ + return pptp_set_offload_mode(true, peer_call_id, peer_ip_addr); +} +EXPORT_SYMBOL(pptp_session_enable_offload_mode); + +/* Disable the PPTP session offload flag */ +int pptp_session_disable_offload_mode(__be16 peer_call_id, __be32 peer_ip_addr) +{ + return pptp_set_offload_mode(false, peer_call_id, peer_ip_addr); +} +EXPORT_SYMBOL(pptp_session_disable_offload_mode); + +/* Register the offload callback function on behalf of the module which + * will own the sequence and acknowledgment number updates for all + * PPTP GRE packets. All PPTP GRE packets are then transmitted to this + * module after encapsulation in order to ensure the correct seq/ack + * fields are set in the packets before transmission. This is required + * when PPTP flows are offloaded to acceleration engines, in-order to + * ensure consistency in sequence and ack numbers between PPTP control + * (PPP LCP) and data packets + */ +int pptp_register_gre_seq_offload_callback(pptp_gre_seq_offload_callback_t + pptp_gre_offload_cb) +{ + pptp_gre_seq_offload_callback_t pptp_gre_offload_cb_f; + + rcu_read_lock(); + pptp_gre_offload_cb_f = rcu_dereference(pptp_gre_offload_xmit_cb); + + if (pptp_gre_offload_cb_f) { + rcu_read_unlock(); + return -1; + } + + rcu_assign_pointer(pptp_gre_offload_xmit_cb, pptp_gre_offload_cb); + rcu_read_unlock(); + return 0; +} +EXPORT_SYMBOL(pptp_register_gre_seq_offload_callback); + +/* Unregister the PPTP GRE packets sequence number offload callback */ +void pptp_unregister_gre_seq_offload_callback(void) +{ + rcu_assign_pointer(pptp_gre_offload_xmit_cb, NULL); +} +EXPORT_SYMBOL(pptp_unregister_gre_seq_offload_callback); + +/* pptp_hold_chan() */ +static void pptp_hold_chan(struct ppp_channel *chan) +{ + struct sock *sk = (struct sock *)chan->private; + + sock_hold(sk); +} + +/* pptp_release_chan() */ +static void pptp_release_chan(struct ppp_channel *chan) +{ + struct sock *sk = (struct sock *)chan->private; + + sock_put(sk); +} + +/* pptp_get_channel_protocol() + * Return the protocol type of the PPTP over PPP protocol + */ +static int pptp_get_channel_protocol(struct ppp_channel *chan) +{ + return PX_PROTO_PPTP; +} + static const struct ppp_channel_ops pptp_chan_ops = { .start_xmit = pptp_xmit, .ioctl = pptp_ppp_ioctl, + .get_channel_protocol = pptp_get_channel_protocol, + .hold = pptp_hold_chan, + .release = pptp_release_chan, }; static struct proto pptp_sk_proto __read_mostly = {