cregit-Linux how code gets into the kernel

Release 4.10 fs/ncpfs/sock.c

Directory: fs/ncpfs
/*
 *  linux/fs/ncpfs/sock.c
 *
 *  Copyright (C) 1992, 1993  Rick Sladkey
 *
 *  Modified 1995, 1996 by Volker Lendecke to be usable for ncp
 *  Modified 1997 Peter Waltenberg, Bill Hawes, David Woodhouse for 2.1 dcache
 *
 */


#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

#include <linux/time.h>
#include <linux/errno.h>
#include <linux/socket.h>
#include <linux/fcntl.h>
#include <linux/stat.h>
#include <linux/string.h>
#include <linux/uaccess.h>
#include <linux/in.h>
#include <linux/net.h>
#include <linux/mm.h>
#include <linux/netdevice.h>
#include <linux/signal.h>
#include <linux/slab.h>
#include <net/scm.h>
#include <net/sock.h>
#include <linux/ipx.h>
#include <linux/poll.h>
#include <linux/file.h>

#include "ncp_fs.h"

#include "ncpsign_kernel.h"


static int _recv(struct socket *sock, void *buf, int size, unsigned flags) { struct msghdr msg = {NULL, }; struct kvec iov = {buf, size}; return kernel_recvmsg(sock, &msg, &iov, 1, size, flags); }

Contributors

PersonTokensPropCommitsCommitProp
al viroal viro3052.63%133.33%
pre-gitpre-git2747.37%266.67%
Total57100.00%3100.00%


static inline int do_send(struct socket *sock, struct kvec *vec, int count, int len, unsigned flags) { struct msghdr msg = { .msg_flags = flags }; return kernel_sendmsg(sock, &msg, vec, count, len); }

Contributors

PersonTokensPropCommitsCommitProp
al viroal viro3873.08%150.00%
pre-gitpre-git1426.92%150.00%
Total52100.00%2100.00%


static int _send(struct socket *sock, const void *buff, int len) { struct kvec vec; vec.iov_base = (void *) buff; vec.iov_len = len; return do_send(sock, &vec, 1, len, 0); }

Contributors

PersonTokensPropCommitsCommitProp
pre-gitpre-git4481.48%375.00%
al viroal viro1018.52%125.00%
Total54100.00%4100.00%

struct ncp_request_reply { struct list_head req; wait_queue_head_t wq; atomic_t refs; unsigned char* reply_buf; size_t datalen; int result; enum { RQ_DONE, RQ_INPROGRESS, RQ_QUEUED, RQ_IDLE, RQ_ABANDONED } status; struct kvec* tx_ciov; size_t tx_totallen; size_t tx_iovlen; struct kvec tx_iov[3]; u_int16_t tx_type; u_int32_t sign[6]; };
static inline struct ncp_request_reply* ncp_alloc_req(void) { struct ncp_request_reply *req; req = kmalloc(sizeof(struct ncp_request_reply), GFP_KERNEL); if (!req) return NULL; init_waitqueue_head(&req->wq); atomic_set(&req->refs, (1)); req->status = RQ_IDLE; return req; }

Contributors

PersonTokensPropCommitsCommitProp
pierre ossmanpierre ossman66100.00%1100.00%
Total66100.00%1100.00%


static void ncp_req_get(struct ncp_request_reply *req) { atomic_inc(&req->refs); }

Contributors

PersonTokensPropCommitsCommitProp
pierre ossmanpierre ossman19100.00%1100.00%
Total19100.00%1100.00%


static void ncp_req_put(struct ncp_request_reply *req) { if (atomic_dec_and_test(&req->refs)) kfree(req); }

Contributors

PersonTokensPropCommitsCommitProp
pierre ossmanpierre ossman26100.00%1100.00%
Total26100.00%1100.00%


void ncp_tcp_data_ready(struct sock *sk) { struct ncp_server *server = sk->sk_user_data; server->data_ready(sk); schedule_work(&server->rcv.tq); }

Contributors

PersonTokensPropCommitsCommitProp
petr vandrovec*petr vandrovec*2158.33%120.00%
pre-gitpre-git1130.56%120.00%
al viroal viro25.56%120.00%
ingo molnaringo molnar12.78%120.00%
arnaldo carvalho de meloarnaldo carvalho de melo12.78%120.00%
Total36100.00%5100.00%


void ncp_tcp_error_report(struct sock *sk) { struct ncp_server *server = sk->sk_user_data; server->error_report(sk); schedule_work(&server->rcv.tq); }

Contributors

PersonTokensPropCommitsCommitProp
petr vandrovec*petr vandrovec*2569.44%125.00%
pre-gitpre-git925.00%125.00%
ingo molnaringo molnar12.78%125.00%
arnaldo carvalho de meloarnaldo carvalho de melo12.78%125.00%
Total36100.00%4100.00%


void ncp_tcp_write_space(struct sock *sk) { struct ncp_server *server = sk->sk_user_data; /* We do not need any locking: we first set tx.creq, and then we do sendmsg, not vice versa... */ server->write_space(sk); if (server->tx.creq) schedule_work(&server->tx.tq); }

Contributors

PersonTokensPropCommitsCommitProp
petr vandrovec*petr vandrovec*3373.33%125.00%
pre-gitpre-git1022.22%125.00%
ingo molnaringo molnar12.22%125.00%
arnaldo carvalho de meloarnaldo carvalho de melo12.22%125.00%
Total45100.00%4100.00%


void ncpdgram_timeout_call(unsigned long v) { struct ncp_server *server = (void*)v; schedule_work(&server->timeout_tq); }

Contributors

PersonTokensPropCommitsCommitProp
petr vandrovec*petr vandrovec*2382.14%120.00%
pre-gitpre-git414.29%360.00%
ingo molnaringo molnar13.57%120.00%
Total28100.00%5100.00%


static inline void ncp_finish_request(struct ncp_server *server, struct ncp_request_reply *req, int result) { req->result = result; if (req->status != RQ_ABANDONED) memcpy(req->reply_buf, server->rxbuf, req->datalen); req->status = RQ_DONE; wake_up_all(&req->wq); ncp_req_put(req); }

Contributors

PersonTokensPropCommitsCommitProp
pierre ossmanpierre ossman3348.53%125.00%
petr vandrovec*petr vandrovec*3044.12%125.00%
pre-gitpre-git57.35%250.00%
Total68100.00%4100.00%


static void __abort_ncp_connection(struct ncp_server *server) { struct ncp_request_reply *req; ncp_invalidate_conn(server); del_timer(&server->timeout_tm); while (!list_empty(&server->tx.requests)) { req = list_entry(server->tx.requests.next, struct ncp_request_reply, req); list_del_init(&req->req); ncp_finish_request(server, req, -EIO); } req = server->rcv.creq; if (req) { server->rcv.creq = NULL; ncp_finish_request(server, req, -EIO); server->rcv.ptr = NULL; server->rcv.state = 0; } req = server->tx.creq; if (req) { server->tx.creq = NULL; ncp_finish_request(server, req, -EIO); } }

Contributors

PersonTokensPropCommitsCommitProp
petr vandrovec*petr vandrovec*11370.62%111.11%
pre-gitpre-git4427.50%777.78%
pierre ossmanpierre ossman31.88%111.11%
Total160100.00%9100.00%


static inline int get_conn_number(struct ncp_reply_header *rp) { return rp->conn_low | (rp->conn_high << 8); }

Contributors

PersonTokensPropCommitsCommitProp
petr vandrovec*petr vandrovec*2288.00%150.00%
pre-gitpre-git312.00%150.00%
Total25100.00%2100.00%


static inline void __ncp_abort_request(struct ncp_server *server, struct ncp_request_reply *req, int err) { /* If req is done, we got signal, but we also received answer... */ switch (req->status) { case RQ_IDLE: case RQ_DONE: break; case RQ_QUEUED: list_del_init(&req->req); ncp_finish_request(server, req, err); break; case RQ_INPROGRESS: req->status = RQ_ABANDONED; break; case RQ_ABANDONED: break; } }

Contributors

PersonTokensPropCommitsCommitProp
petr vandrovec*petr vandrovec*4867.61%133.33%
pre-gitpre-git1318.31%133.33%
pierre ossmanpierre ossman1014.08%133.33%
Total71100.00%3100.00%


static inline void ncp_abort_request(struct ncp_server *server, struct ncp_request_reply *req, int err) { mutex_lock(&server->rcv.creq_mutex); __ncp_abort_request(server, req, err); mutex_unlock(&server->rcv.creq_mutex); }

Contributors

PersonTokensPropCommitsCommitProp
petr vandrovec*petr vandrovec*3673.47%133.33%
pre-gitpre-git918.37%133.33%
ingo molnaringo molnar48.16%133.33%
Total49100.00%3100.00%


static inline void __ncptcp_abort(struct ncp_server *server) { __abort_ncp_connection(server); }

Contributors

PersonTokensPropCommitsCommitProp
petr vandrovec*petr vandrovec*1588.24%150.00%
pre-gitpre-git211.76%150.00%
Total17100.00%2100.00%


static int ncpdgram_send(struct socket *sock, struct ncp_request_reply *req) { struct kvec vec[3]; /* sock_sendmsg updates iov pointers for us :-( */ memcpy(vec, req->tx_ciov, req->tx_iovlen * sizeof(vec[0])); return do_send(sock, vec, req->tx_iovlen, req->tx_totallen, MSG_DONTWAIT); }

Contributors

PersonTokensPropCommitsCommitProp
petr vandrovec*petr vandrovec*4876.19%250.00%
al viroal viro1219.05%125.00%
pre-gitpre-git34.76%125.00%
Total63100.00%4100.00%


static void __ncptcp_try_send(struct ncp_server *server) { struct ncp_request_reply *rq; struct kvec *iov; struct kvec iovc[3]; int result; rq = server->tx.creq; if (!rq) return; /* sock_sendmsg updates iov pointers for us :-( */ memcpy(iovc, rq->tx_ciov, rq->tx_iovlen * sizeof(iov[0])); result = do_send(server->ncp_sock, iovc, rq->tx_iovlen, rq->tx_totallen, MSG_NOSIGNAL | MSG_DONTWAIT); if (result == -EAGAIN) return; if (result < 0) { pr_err("tcp: Send failed: %d\n", result); __ncp_abort_request(server, rq, result); return; } if (result >= rq->tx_totallen) { server->rcv.creq = rq; server->tx.creq = NULL; return; } rq->tx_totallen -= result; iov = rq->tx_ciov; while (iov->iov_len <= result) { result -= iov->iov_len; iov++; rq->tx_iovlen--; } iov->iov_base += result; iov->iov_len -= result; rq->tx_ciov = iov; }

Contributors

PersonTokensPropCommitsCommitProp
petr vandrovec*petr vandrovec*15877.45%240.00%
pre-gitpre-git3215.69%120.00%
al viroal viro125.88%120.00%
joe perchesjoe perches20.98%120.00%
Total204100.00%5100.00%


static inline void ncp_init_header(struct ncp_server *server, struct ncp_request_reply *req, struct ncp_request_header *h) { req->status = RQ_INPROGRESS; h->conn_low = server->connection; h->conn_high = server->connection >> 8; h->sequence = ++server->sequence; }

Contributors

PersonTokensPropCommitsCommitProp
petr vandrovec*petr vandrovec*3360.00%125.00%
pre-gitpre-git2240.00%375.00%
Total55100.00%4100.00%


static void ncpdgram_start_request(struct ncp_server *server, struct ncp_request_reply *req) { size_t signlen; struct ncp_request_header* h; req->tx_ciov = req->tx_iov + 1; h = req->tx_iov[1].iov_base; ncp_init_header(server, req, h); signlen = sign_packet(server, req->tx_iov[1].iov_base + sizeof(struct ncp_request_header) - 1, req->tx_iov[1].iov_len - sizeof(struct ncp_request_header) + 1, cpu_to_le32(req->tx_totallen), req->sign); if (signlen) { req->tx_ciov[1].iov_base = req->sign; req->tx_ciov[1].iov_len = signlen; req->tx_iovlen += 1; req->tx_totallen += signlen; } server->rcv.creq = req; server->timeout_last = server->m.time_out; server->timeout_retries = server->m.retry_count; ncpdgram_send(server->ncp_sock, req); mod_timer(&server->timeout_tm, jiffies + server->m.time_out); }

Contributors

PersonTokensPropCommitsCommitProp
petr vandrovec*petr vandrovec*16481.59%150.00%
pre-gitpre-git3718.41%150.00%
Total201100.00%2100.00%

#define NCP_TCP_XMIT_MAGIC (0x446D6454) #define NCP_TCP_XMIT_VERSION (1) #define NCP_TCP_RCVD_MAGIC (0x744E6350)
static void ncptcp_start_request(struct ncp_server *server, struct ncp_request_reply *req) { size_t signlen; struct ncp_request_header* h; req->tx_ciov = req->tx_iov; h = req->tx_iov[1].iov_base; ncp_init_header(server, req, h); signlen = sign_packet(server, req->tx_iov[1].iov_base + sizeof(struct ncp_request_header) - 1, req->tx_iov[1].iov_len - sizeof(struct ncp_request_header) + 1, cpu_to_be32(req->tx_totallen + 24), req->sign + 4) + 16; req->sign[0] = htonl(NCP_TCP_XMIT_MAGIC); req->sign[1] = htonl(req->tx_totallen + signlen); req->sign[2] = htonl(NCP_TCP_XMIT_VERSION); req->sign[3] = htonl(req->datalen + 8); req->tx_iov[0].iov_base = req->sign; req->tx_iov[0].iov_len = signlen; req->tx_iovlen += 1; req->tx_totallen += signlen; server->tx.creq = req; __ncptcp_try_send(server); }

Contributors

PersonTokensPropCommitsCommitProp
petr vandrovec*petr vandrovec*12960.00%133.33%
pre-gitpre-git8439.07%133.33%
al viroal viro20.93%133.33%
Total215100.00%3100.00%


static inline void __ncp_start_request(struct ncp_server *server, struct ncp_request_reply *req) { /* we copy the data so that we do not depend on the caller staying alive */ memcpy(server->txbuf, req->tx_iov[1].iov_base, req->tx_iov[1].iov_len); req->tx_iov[1].iov_base = server->txbuf; if (server->ncp_sock->type == SOCK_STREAM) ncptcp_start_request(server, req); else ncpdgram_start_request(server, req); }

Contributors

PersonTokensPropCommitsCommitProp
pierre ossmanpierre ossman3948.15%133.33%
petr vandrovec*petr vandrovec*2935.80%133.33%
pre-gitpre-git1316.05%133.33%
Total81100.00%3100.00%


static int ncp_add_request(struct ncp_server *server, struct ncp_request_reply *req) { mutex_lock(&server->rcv.creq_mutex); if (!ncp_conn_valid(server)) { mutex_unlock(&server->rcv.creq_mutex); pr_err("tcp: Server died\n"); return -EIO; } ncp_req_get(req); if (server->tx.creq || server->rcv.creq) { req->status = RQ_QUEUED; list_add_tail(&req->req, &server->tx.requests); mutex_unlock(&server->rcv.creq_mutex); return 0; } __ncp_start_request(server, req); mutex_unlock(&server->rcv.creq_mutex); return 0; }

Contributors

PersonTokensPropCommitsCommitProp
petr vandrovec*petr vandrovec*7456.92%120.00%
pre-gitpre-git4131.54%120.00%
ingo molnaringo molnar86.15%120.00%
pierre ossmanpierre ossman53.85%120.00%
joe perchesjoe perches21.54%120.00%
Total130100.00%5100.00%


static void __ncp_next_request(struct ncp_server *server) { struct ncp_request_reply *req; server->rcv.creq = NULL; if (list_empty(&server->tx.requests)) { return; } req = list_entry(server->tx.requests.next, struct ncp_request_reply, req); list_del_init(&req->req); __ncp_start_request(server, req); }

Contributors

PersonTokensPropCommitsCommitProp
petr vandrovec*petr vandrovec*6488.89%150.00%
pre-gitpre-git811.11%150.00%
Total72100.00%2100.00%


static void info_server(struct ncp_server *server, unsigned int id, const void * data, size_t len) { if (server->info_sock) { struct kvec iov[2]; __be32 hdr[2]; hdr[0] = cpu_to_be32(len + 8); hdr[1] = cpu_to_be32(id); iov[0].iov_base = hdr; iov[0].iov_len = 8; iov[1].iov_base = (void *) data; iov[1].iov_len = len; do_send(server->info_sock, iov, 2, len + 8, MSG_NOSIGNAL); } }

Contributors

PersonTokensPropCommitsCommitProp
petr vandrovec*petr vandrovec*11593.50%133.33%
al viroal viro86.50%266.67%
Total123100.00%3100.00%


void ncpdgram_rcv_proc(struct work_struct *work) { struct ncp_server *server = container_of(work, struct ncp_server, rcv.tq); struct socket* sock; sock = server->ncp_sock; while (1) { struct ncp_reply_header reply; int result; result = _recv(sock, &reply, sizeof(reply), MSG_PEEK | MSG_DONTWAIT); if (result < 0) { break; } if (result >= sizeof(reply)) { struct ncp_request_reply *req; if (reply.type == NCP_WATCHDOG) { unsigned char buf[10]; if (server->connection != get_conn_number(&reply)) { goto drop; } result = _recv(sock, buf, sizeof(buf), MSG_DONTWAIT); if (result < 0) { ncp_dbg(1, "recv failed with %d\n", result); continue; } if (result < 10) { ncp_dbg(1, "too short (%u) watchdog packet\n", result); continue; } if (buf[9] != '?') { ncp_dbg(1, "bad signature (%02X) in watchdog packet\n", buf[9]); continue; } buf[9] = 'Y'; _send(sock, buf, sizeof(buf)); continue; } if (reply.type != NCP_POSITIVE_ACK && reply.type != NCP_REPLY) { result = _recv(sock, server->unexpected_packet.data, sizeof(server->unexpected_packet.data), MSG_DONTWAIT); if (result < 0) { continue; } info_server(server, 0, server->unexpected_packet.data, result); continue; } mutex_lock(&server->rcv.creq_mutex); req = server->rcv.creq; if (req && (req->tx_type == NCP_ALLOC_SLOT_REQUEST || (server->sequence == reply.sequence && server->connection == get_conn_number(&reply)))) { if (reply.type == NCP_POSITIVE_ACK) { server->timeout_retries = server->m.retry_count; server->timeout_last = NCP_MAX_RPC_TIMEOUT; mod_timer(&server->timeout_tm, jiffies + NCP_MAX_RPC_TIMEOUT); } else if (reply.type == NCP_REPLY) { result = _recv(sock, server->rxbuf, req->datalen, MSG_DONTWAIT); #ifdef CONFIG_NCPFS_PACKET_SIGNING if (result >= 0 && server->sign_active && req->tx_type != NCP_DEALLOC_SLOT_REQUEST) { if (result < 8 + 8) { result = -EIO; } else { unsigned int hdrl; result -= 8; hdrl = sock->sk->sk_family == AF_INET ? 8 : 6; if (sign_verify_reply(server, server->rxbuf + hdrl, result - hdrl, cpu_to_le32(result), server->rxbuf + result)) { pr_info("Signature violation\n"); result = -EIO; } } } #endif del_timer(&server->timeout_tm); server->rcv.creq = NULL; ncp_finish_request(server, req, result); __ncp_next_request(server); mutex_unlock(&server->rcv.creq_mutex); continue; } } mutex_unlock(&server->rcv.creq_mutex); } drop:; _recv(sock, &reply, sizeof(reply), MSG_DONTWAIT); } }

Contributors

PersonTokensPropCommitsCommitProp
petr vandrovec*petr vandrovec*53492.07%327.27%
david howellsdavid howells142.41%19.09%
joe perchesjoe perches111.90%218.18%
pierre ossmanpierre ossman81.38%19.09%
ingo molnaringo molnar61.03%19.09%
pre-gitpre-git50.86%19.09%
al viroal viro10.17%19.09%
arnaldo carvalho de meloarnaldo carvalho de melo10.17%19.09%
Total580100.00%11100.00%


static void __ncpdgram_timeout_proc(struct ncp_server *server) { /* If timer is pending, we are processing another request... */ if (!timer_pending(&server->timeout_tm)) { struct ncp_request_reply* req; req = server->rcv.creq; if (req) { int timeout; if (server->m.flags & NCP_MOUNT_SOFT) { if (server->timeout_retries-- == 0) { __ncp_abort_request(server, req, -ETIMEDOUT); return; } } /* Ignore errors */ ncpdgram_send(server->ncp_sock, req); timeout = server->timeout_last << 1; if (timeout > NCP_MAX_RPC_TIMEOUT) { timeout = NCP_MAX_RPC_TIMEOUT; } server->timeout_last = timeout; mod_timer(&server->timeout_tm, jiffies + timeout); } } }

Contributors

PersonTokensPropCommitsCommitProp
petr vandrovec*petr vandrovec*129100.00%1100.00%
Total129100.00%1100.00%


void ncpdgram_timeout_proc(struct work_struct *work) { struct ncp_server *server = container_of(work, struct ncp_server, timeout_tq); mutex_lock(&server->rcv.creq_mutex); __ncpdgram_timeout_proc(server); mutex_unlock(&server->rcv.creq_mutex); }

Contributors

PersonTokensPropCommitsCommitProp
petr vandrovec*petr vandrovec*3468.00%133.33%
david howellsdavid howells1224.00%133.33%
ingo molnaringo molnar48.00%133.33%
Total50100.00%3100.00%


static int do_tcp_rcv(struct ncp_server *server, void *buffer, size_t len) { int result; if (buffer) { result = _recv(server->ncp_sock, buffer, len, MSG_DONTWAIT); } else {