static int setup_tcp(void) { struct sockaddr_in saddr = {}; mm_segment_t fs; int buffersize = PAGE_SIZE; int ret; ret = sock_create_kern(AF_INET, SOCK_STREAM, IPPROTO_TCP, &server); if (unlikely(ret < 0)) { DBG("error creating socket"); return ret; } saddr.sin_family = AF_INET; saddr.sin_port = htons(tcp_port); saddr.sin_addr.s_addr = INADDR_ANY; fs = get_fs(); set_fs(KERNEL_DS); ret = sock_setsockopt(server, SOL_SOCKET, SO_SNDBUF, (void *)&buffersize, sizeof(buffersize)); set_fs(fs); if (unlikely(ret < 0)) { DBG("error setting buffsize"); goto out_err; } ret = server->ops->bind(server, (struct sockaddr *)&saddr, sizeof(saddr)); if (unlikely(ret < 0)) { DBG("error binding socket"); goto out_err; } ret = server->ops->listen(server, 1); if (unlikely(ret < 0)) { DBG("error listening on socket"); goto out_err; } ret = sock_create_kern(PF_INET, SOCK_STREAM, IPPROTO_TCP, &client); if (ret < 0) { DBG("error creating accept socket"); goto out_err; } out: return ret; out_err: server->ops->shutdown(server, 0); server->ops->release(server); goto out; }
void *app_glue_create_socket(int family,int type) { struct timeval tv; struct socket *sock = NULL; if(sock_create_kern(family,type,0,&sock)) { syslog(LOG_ERR,"cannot create socket %s %d\n",__FILE__,__LINE__); return NULL; } tv.tv_sec = -1; tv.tv_usec = 0; if(sock_setsockopt(sock,SOL_SOCKET,SO_RCVTIMEO,(char *)&tv,sizeof(tv))) { syslog(LOG_ERR,"%s %d cannot set notimeout option\n",__FILE__,__LINE__); } tv.tv_sec = -1; tv.tv_usec = 0; if(sock_setsockopt(sock,SOL_SOCKET,SO_SNDTIMEO,(char *)&tv,sizeof(tv))) { syslog(LOG_ERR,"%s %d cannot set notimeout option\n",__FILE__,__LINE__); } if(type != SOCK_STREAM) { if(sock->sk) { sock_reset_flag(sock->sk,SOCK_USE_WRITE_QUEUE); sock->sk->sk_data_ready = app_glue_sock_readable; sock->sk->sk_write_space = app_glue_sock_write_space; app_glue_sock_write_space(sock->sk); } } return sock; }
static int socket_init(void) { INIT_WORK(&work, sendmsg); sock_create_kern(PF_INET, SOCK_DGRAM, 0, &sock); schedule_work(&work); return 0; }
/* rxk_NewSocket * open and bind RX socket */ osi_socket * rxk_NewSocketHost(afs_uint32 ahost, short aport) { struct socket *sockp; struct sockaddr_in myaddr; int code; #ifdef AFS_ADAPT_PMTU int pmtu = IP_PMTUDISC_WANT; #else int pmtu = IP_PMTUDISC_DONT; #endif #ifdef HAVE_LINUX_SOCK_CREATE_KERN_NS code = sock_create_kern(&init_net, AF_INET, SOCK_DGRAM, IPPROTO_UDP, &sockp); #elif defined(HAVE_LINUX_SOCK_CREATE_KERN) code = sock_create_kern(AF_INET, SOCK_DGRAM, IPPROTO_UDP, &sockp); #elif defined(LINUX_KERNEL_SOCK_CREATE_V) code = sock_create(AF_INET, SOCK_DGRAM, IPPROTO_UDP, &sockp, 0); #else code = sock_create(AF_INET, SOCK_DGRAM, IPPROTO_UDP, &sockp); #endif if (code < 0) return NULL; /* Bind socket */ myaddr.sin_family = AF_INET; myaddr.sin_addr.s_addr = ahost; myaddr.sin_port = aport; code = sockp->ops->bind(sockp, (struct sockaddr *)&myaddr, sizeof(myaddr)); if (code < 0) { printk("sock_release(rx_socket) FIXME\n"); return NULL; } kernel_setsockopt(sockp, SOL_IP, IP_MTU_DISCOVER, (char *)&pmtu, sizeof(pmtu)); #ifdef AFS_RXERRQ_ENV { int recverr = 1; kernel_setsockopt(sockp, SOL_IP, IP_RECVERR, (char *)&recverr, sizeof(recverr)); } #endif return (osi_socket *)sockp; }
int rds_tcp_conn_connect(struct rds_connection *conn) { struct socket *sock = NULL; struct sockaddr_in src, dest; int ret; struct rds_tcp_connection *tc = conn->c_transport_data; mutex_lock(&tc->t_conn_lock); if (rds_conn_up(conn)) { mutex_unlock(&tc->t_conn_lock); return 0; } ret = sock_create_kern(rds_conn_net(conn), PF_INET, SOCK_STREAM, IPPROTO_TCP, &sock); if (ret < 0) goto out; rds_tcp_tune(sock); src.sin_family = AF_INET; src.sin_addr.s_addr = (__force u32)conn->c_laddr; src.sin_port = (__force u16)htons(0); ret = sock->ops->bind(sock, (struct sockaddr *)&src, sizeof(src)); if (ret) { rdsdebug("bind failed with %d at address %pI4\n", ret, &conn->c_laddr); goto out; } dest.sin_family = AF_INET; dest.sin_addr.s_addr = (__force u32)conn->c_faddr; dest.sin_port = (__force u16)htons(RDS_TCP_PORT); /* * once we call connect() we can start getting callbacks and they * own the socket */ rds_tcp_set_callbacks(sock, conn); ret = sock->ops->connect(sock, (struct sockaddr *)&dest, sizeof(dest), O_NONBLOCK); rdsdebug("connect to address %pI4 returned %d\n", &conn->c_faddr, ret); if (ret == -EINPROGRESS) ret = 0; if (ret == 0) { rds_tcp_keepalive(sock); sock = NULL; } else { rds_tcp_restore_callbacks(sock, conn->c_transport_data); } out: mutex_unlock(&tc->t_conn_lock); if (sock) sock_release(sock); return ret; }
static int rxe_qp_init_req(struct rxe_dev *rxe, struct rxe_qp *qp, struct ib_qp_init_attr *init, struct ib_ucontext *context, struct ib_udata *udata) { int err; int wqe_size; err = sock_create_kern(&init_net, AF_INET, SOCK_DGRAM, 0, &qp->sk); if (err < 0) return err; qp->sk->sk->sk_user_data = qp; qp->sq.max_wr = init->cap.max_send_wr; qp->sq.max_sge = init->cap.max_send_sge; qp->sq.max_inline = init->cap.max_inline_data; wqe_size = max_t(int, sizeof(struct rxe_send_wqe) + qp->sq.max_sge * sizeof(struct ib_sge), sizeof(struct rxe_send_wqe) + qp->sq.max_inline); qp->sq.queue = rxe_queue_init(rxe, &qp->sq.max_wr, wqe_size); if (!qp->sq.queue) return -ENOMEM; err = do_mmap_info(rxe, udata, true, context, qp->sq.queue->buf, qp->sq.queue->buf_size, &qp->sq.queue->ip); if (err) { kvfree(qp->sq.queue->buf); kfree(qp->sq.queue); return err; } qp->req.wqe_index = producer_index(qp->sq.queue); qp->req.state = QP_STATE_RESET; qp->req.opcode = -1; qp->comp.opcode = -1; spin_lock_init(&qp->sq.sq_lock); skb_queue_head_init(&qp->req_pkts); rxe_init_task(rxe, &qp->req.task, qp, rxe_requester, "req"); rxe_init_task(rxe, &qp->comp.task, qp, rxe_completer, "comp"); qp->qp_timeout_jiffies = 0; /* Can't be set for UD/UC in modify_qp */ if (init->qp_type == IB_QPT_RC) { timer_setup(&qp->rnr_nak_timer, rnr_nak_timer, 0); timer_setup(&qp->retrans_timer, retransmit_timer, 0); } return 0; }
int myclient(void) { struct socket *sock; struct sockaddr_in s_addr; unsigned short portnum = 0x8888; int ret = 0; memset(&s_addr, 0, sizeof(s_addr)); s_addr.sin_family = AF_INET; s_addr.sin_port = htons(portnum); s_addr.sin_addr.s_addr = in_aton("192.168.209.134"); /*server ip is 192.168.209.134*/ sock = (struct socket *) kmalloc(sizeof(struct socket), GFP_KERNEL); /*create a socket*/ ret = sock_create_kern(AF_INET, SOCK_STREAM, 0, &sock); if (ret < 0) { printk("client:socket create error!\n"); return ret; } printk("client: socket create ok!\n"); /*connect server*/ ret = sock->ops->connect(sock, (struct sockaddr *) &s_addr, sizeof(s_addr), 0); if (ret != 0) { printk("client:connect error!\n"); return ret; } printk("client:connect ok!\n"); /*kmalloc sendbuf*/ char *sendbuf = NULL; sendbuf = kmalloc(1024, GFP_KERNEL); if (sendbuf == NULL) { printk("client: sendbuf kmalloc error!\n"); return -1; } memset(sendbuf, 1, 1024); struct kvec vec; struct msghdr msg; vec.iov_base = sendbuf; vec.iov_len = 1024; memset(&msg, 0, sizeof(msg)); ret = kernel_sendmsg(sock, &msg, &vec, 1, 1024); /*send message */ if (ret < 0) { printk("client: kernel_sendmsg error!\n"); return ret; } else if (ret != 1024) { printk("client: ret!=1024"); } printk("client:send ok!\n"); return ret; }
/* * open an RxRPC socket and bind it to be a server for callback notifications * - the socket is left in blocking mode and non-blocking ops use MSG_DONTWAIT */ int afs_open_socket(struct afs_net *net) { struct sockaddr_rxrpc srx; struct socket *socket; unsigned int min_level; int ret; _enter(""); ret = sock_create_kern(net->net, AF_RXRPC, SOCK_DGRAM, PF_INET6, &socket); if (ret < 0) goto error_1; socket->sk->sk_allocation = GFP_NOFS; /* bind the callback manager's address to make this a server socket */ memset(&srx, 0, sizeof(srx)); srx.srx_family = AF_RXRPC; srx.srx_service = CM_SERVICE; srx.transport_type = SOCK_DGRAM; srx.transport_len = sizeof(srx.transport.sin6); srx.transport.sin6.sin6_family = AF_INET6; srx.transport.sin6.sin6_port = htons(AFS_CM_PORT); min_level = RXRPC_SECURITY_ENCRYPT; ret = kernel_setsockopt(socket, SOL_RXRPC, RXRPC_MIN_SECURITY_LEVEL, (void *)&min_level, sizeof(min_level)); if (ret < 0) goto error_2; ret = kernel_bind(socket, (struct sockaddr *) &srx, sizeof(srx)); if (ret == -EADDRINUSE) { srx.transport.sin6.sin6_port = 0; ret = kernel_bind(socket, (struct sockaddr *) &srx, sizeof(srx)); } if (ret < 0) goto error_2; rxrpc_kernel_new_call_notification(socket, afs_rx_new_call, afs_rx_discard_new_call); ret = kernel_listen(socket, INT_MAX); if (ret < 0) goto error_2; net->socket = socket; afs_charge_preallocation(&net->charge_preallocation_work); _leave(" = 0"); return 0; error_2: sock_release(socket); error_1: _leave(" = %d", ret); return ret; }
void init_socket(void) { // Create the socket int err = sock_create_kern(AF_INET, SOCK_DGRAM, IPPROTO_UDP, &syslog_sock); if (err) { // Indicate we don't have a working socket syslog_sock = NULL; return; } }
/** * xs_udp_connect_worker - set up a UDP socket * @args: RPC transport to connect * * Invoked by a work queue tasklet. */ static void xs_udp_connect_worker(void *args) { struct rpc_xprt *xprt = (struct rpc_xprt *) args; struct socket *sock = xprt->sock; int err, status = -EIO; if (xprt->shutdown || xprt->addr.sin_port == 0) goto out; dprintk("RPC: xs_udp_connect_worker for xprt %p\n", xprt); /* Start by resetting any existing state */ xs_close(xprt); if ((err = sock_create_kern(PF_INET, SOCK_DGRAM, IPPROTO_UDP, &sock)) < 0) { dprintk("RPC: can't create UDP transport socket (%d).\n", -err); goto out; } xs_reclassify_socket(sock); if (xs_bind(xprt, sock)) { sock_release(sock); goto out; } if (!xprt->inet) { struct sock *sk = sock->sk; write_lock_bh(&sk->sk_callback_lock); sk->sk_user_data = xprt; xprt->old_data_ready = sk->sk_data_ready; xprt->old_state_change = sk->sk_state_change; xprt->old_write_space = sk->sk_write_space; sk->sk_data_ready = xs_udp_data_ready; sk->sk_write_space = xs_udp_write_space; sk->sk_no_check = UDP_CSUM_NORCV; sk->sk_allocation = GFP_ATOMIC; xprt_set_connected(xprt); /* Reset to new socket */ xprt->sock = sock; xprt->inet = sk; write_unlock_bh(&sk->sk_callback_lock); } xs_udp_do_set_buffer_size(xprt); status = 0; out: xprt_wake_pending_tasks(xprt, status); xprt_clear_connecting(xprt); }
struct p9_transport *p9_trans_create_tcp(const char *addr, int port) { int err; struct p9_transport *trans; struct socket *csocket; struct sockaddr_in sin_server; csocket = NULL; trans = kmalloc(sizeof(struct p9_transport), GFP_KERNEL); if (!trans) return ERR_PTR(-ENOMEM); trans->write = p9_fd_write; trans->read = p9_fd_read; trans->close = p9_fd_close; trans->poll = p9_fd_poll; sin_server.sin_family = AF_INET; sin_server.sin_addr.s_addr = in_aton(addr); sin_server.sin_port = htons(port); sock_create_kern(PF_INET, SOCK_STREAM, IPPROTO_TCP, &csocket); if (!csocket) { P9_EPRINTK(KERN_ERR, "p9_trans_tcp: problem creating socket\n"); err = -EIO; goto error; } err = csocket->ops->connect(csocket, (struct sockaddr *)&sin_server, sizeof(struct sockaddr_in), 0); if (err < 0) { P9_EPRINTK(KERN_ERR, "p9_trans_tcp: problem connecting socket to %s\n", addr); goto error; } err = p9_socket_open(trans, csocket); if (err < 0) goto error; return trans; error: if (csocket) sock_release(csocket); kfree(trans); return ERR_PTR(err); }
static int sc_socket_create(int type, union capwap_addr* sockaddr, uint16_t protocol) { int ret; TRACEKMOD("### sc_socket_create\n"); /* Create socket */ ret = sock_create_kern(sockaddr->ss.ss_family, SOCK_DGRAM, protocol, &sc_sockets[type]); if (ret) { return ret; } /* Bind to interface */ ret = kernel_bind(sc_sockets[type], &sockaddr->sa, sizeof(union capwap_addr)); if (ret) { goto failure; } /* Set callback */ udp_sk(sc_sockets[type]->sk)->encap_type = 1; udp_sk(sc_sockets[type]->sk)->encap_rcv = sc_socket_recvpacket; /* */ if (!((sockaddr->ss.ss_family == AF_INET) ? sockaddr->sin.sin_port : sockaddr->sin6.sin6_port)) { union capwap_addr localaddr; int localaddrsize = sizeof(union capwap_addr); /* Retrieve port */ ret = kernel_getsockname(sc_sockets[type], &localaddr.sa, &localaddrsize); if (ret) { goto failure; } /* */ if ((sockaddr->ss.ss_family == AF_INET) && (localaddr.ss.ss_family == AF_INET)) { sockaddr->sin.sin_port = localaddr.sin.sin_port; } else if ((sockaddr->ss.ss_family == AF_INET6) && (localaddr.ss.ss_family == AF_INET6)) { sockaddr->sin6.sin6_port = localaddr.sin6.sin6_port; } else { ret = -EFAULT; goto failure; } } return 0; failure: sock_release(sc_sockets[type]); sc_sockets[type] = 0; return ret; }
SOCKET socket(int af, int type, int protocol) { int rc; SOCKET socket; rc = sock_create_kern(af, type, protocol, &socket); if (rc < 0) { socket = NULL; goto Exit; } Exit: return socket; }
/* * open an RxRPC socket and bind it to be a server for callback notifications * - the socket is left in blocking mode and non-blocking ops use MSG_DONTWAIT */ int afs_open_socket(void) { struct sockaddr_rxrpc srx; struct socket *socket; int ret; _enter(""); skb_queue_head_init(&afs_incoming_calls); afs_async_calls = create_singlethread_workqueue("kafsd"); if (!afs_async_calls) { _leave(" = -ENOMEM [wq]"); return -ENOMEM; } ret = sock_create_kern(AF_RXRPC, SOCK_DGRAM, PF_INET, &socket); if (ret < 0) { destroy_workqueue(afs_async_calls); _leave(" = %d [socket]", ret); return ret; } socket->sk->sk_allocation = GFP_NOFS; /* bind the callback manager's address to make this a server socket */ srx.srx_family = AF_RXRPC; srx.srx_service = CM_SERVICE; srx.transport_type = SOCK_DGRAM; srx.transport_len = sizeof(srx.transport.sin); srx.transport.sin.sin_family = AF_INET; srx.transport.sin.sin_port = htons(AFS_CM_PORT); memset(&srx.transport.sin.sin_addr, 0, sizeof(srx.transport.sin.sin_addr)); ret = kernel_bind(socket, (struct sockaddr *) &srx, sizeof(srx)); if (ret < 0) { sock_release(socket); destroy_workqueue(afs_async_calls); _leave(" = %d [bind]", ret); return ret; } rxrpc_kernel_intercept_rx_messages(socket, afs_rx_interceptor); afs_socket = socket; _leave(" = 0"); return 0; }
int __init kserver_init(void) { int r; struct socket *lsk; struct sockaddr_in saddr; r = ss_hooks_register(&ssocket_hooks); if (r) { printk(KERN_ERR "Can't register synchronous socket callbacks\n"); return r; } r = sock_create_kern(AF_INET, SOCK_STREAM, IPPROTO_TCP, &lsk); if (r) { printk(KERN_ERR "Can't listening socket\n"); goto err_create; } inet_sk(lsk->sk)->freebind = 1; lsk->sk->sk_reuse = 1; /* Set TCP handlers. */ ss_tcp_set_listen(lsk, (SsProto *)&my_proto); memset(&saddr, 0, sizeof(saddr)); saddr.sin_family = AF_INET; saddr.sin_addr.s_addr = htonl(INADDR_ANY); saddr.sin_port = htons(PORT); r = lsk->ops->bind(lsk, (struct sockaddr *)&saddr, sizeof(saddr)); if (r) { printk(KERN_ERR "Can't bind listening socket\n"); goto err_call; } r = lsk->ops->listen(lsk, 1000); if (r) { printk(KERN_ERR "Can't listen on socket\n"); goto err_call; } return 0; err_call: sock_release(lsk); err_create: ss_hooks_unregister(&ssocket_hooks); return r; }
struct p9_transport *p9_trans_create_unix(const char *addr) { int err; struct socket *csocket; struct sockaddr_un sun_server; struct p9_transport *trans; csocket = NULL; trans = kmalloc(sizeof(struct p9_transport), GFP_KERNEL); if (!trans) return ERR_PTR(-ENOMEM); trans->write = p9_fd_write; trans->read = p9_fd_read; trans->close = p9_fd_close; trans->poll = p9_fd_poll; if (strlen(addr) > UNIX_PATH_MAX) { P9_EPRINTK(KERN_ERR, "p9_trans_unix: address too long: %s\n", addr); err = -ENAMETOOLONG; goto error; } sun_server.sun_family = PF_UNIX; strcpy(sun_server.sun_path, addr); sock_create_kern(PF_UNIX, SOCK_STREAM, 0, &csocket); err = csocket->ops->connect(csocket, (struct sockaddr *)&sun_server, sizeof(struct sockaddr_un) - 1, 0); if (err < 0) { P9_EPRINTK(KERN_ERR, "p9_trans_unix: problem connecting socket: %s: %d\n", addr, err); goto error; } err = p9_socket_open(trans, csocket); if (err < 0) goto error; return trans; error: if (csocket) sock_release(csocket); kfree(trans); return ERR_PTR(err); }
int udp_sock_create6(struct net *net, struct udp_port_cfg *cfg, struct socket **sockp) { struct sockaddr_in6 udp6_addr; int err; struct socket *sock = NULL; err = sock_create_kern(AF_INET6, SOCK_DGRAM, 0, &sock); if (err < 0) goto error; sk_change_net(sock->sk, net); udp6_addr.sin6_family = AF_INET6; memcpy(&udp6_addr.sin6_addr, &cfg->local_ip6, sizeof(udp6_addr.sin6_addr)); udp6_addr.sin6_port = cfg->local_udp_port; err = kernel_bind(sock, (struct sockaddr *)&udp6_addr, sizeof(udp6_addr)); if (err < 0) goto error; if (cfg->peer_udp_port) { udp6_addr.sin6_family = AF_INET6; memcpy(&udp6_addr.sin6_addr, &cfg->peer_ip6, sizeof(udp6_addr.sin6_addr)); udp6_addr.sin6_port = cfg->peer_udp_port; err = kernel_connect(sock, (struct sockaddr *)&udp6_addr, sizeof(udp6_addr), 0); } if (err < 0) goto error; udp_set_no_check6_tx(sock->sk, !cfg->use_udp6_tx_checksums); udp_set_no_check6_rx(sock->sk, !cfg->use_udp6_rx_checksums); *sockp = sock; return 0; error: if (sock) { kernel_sock_shutdown(sock, SHUT_RDWR); sk_release_kernel(sock->sk); } *sockp = NULL; return err; }
int __init my_vmci_sock_init(void) { int err; /* address to bind on */ struct sockaddr_vm serv_addr = { .svm_family = AF_VMCI, .svm_port = htons(REMOTE_PORT), .svm_cid = REMOTE_CID }; int addrlen = sizeof(serv_addr); long bytes_sent = 0; memset(buffer, 0, BUFF_SIZE); /* create socket */ err = sock_create_kern(AF_VMCI, SOCK_STREAM, 0, &sock); if (err < 0) { printk(LOG_LEVEL "Failed to create socket"); goto out_release; } /* connect to server */ err = sock->ops->connect(sock, (struct sockaddr*) &serv_addr, addrlen, O_WRONLY ); if (err < 0) { printk(LOG_LEVEL "Failed to connect to server"); goto out_release; } while (bytes_sent < TRANSFER_SIZE) { /* send message trough socket */ err = send_msg(sock, buffer, BUFF_SIZE); if (err < 0) { printk(LOG_LEVEL "Failed to send message errno: {%d}", err); goto out_release; } bytes_sent += BUFF_SIZE; printk(KERN_ALERT "Bytes sent :%ld\n", bytes_sent); } printk(LOG_LEVEL "Message was sent successfully"); sock->ops->release(sock); return 0; out_release: sock->ops->release(sock); return err; }
int udp_sock_create4(struct net *net, struct udp_port_cfg *cfg, struct socket **sockp) { int err; struct socket *sock = NULL; struct sockaddr_in udp_addr; err = sock_create_kern(AF_INET, SOCK_DGRAM, 0, &sock); if (err < 0) goto error; sk_change_net(sock->sk, net); udp_addr.sin_family = AF_INET; udp_addr.sin_addr = cfg->local_ip; udp_addr.sin_port = cfg->local_udp_port; err = kernel_bind(sock, (struct sockaddr *)&udp_addr, sizeof(udp_addr)); if (err < 0) goto error; if (cfg->peer_udp_port) { udp_addr.sin_family = AF_INET; udp_addr.sin_addr = cfg->peer_ip; udp_addr.sin_port = cfg->peer_udp_port; err = kernel_connect(sock, (struct sockaddr *)&udp_addr, sizeof(udp_addr), 0); if (err < 0) goto error; } sock->sk->sk_no_check_tx = !cfg->use_udp_checksums; *sockp = sock; return 0; error: if (sock) { kernel_sock_shutdown(sock, SHUT_RDWR); sk_release_kernel(sock->sk); } *sockp = NULL; return err; }
static int __init dccp_ctl_sock_init(void) { int rc = sock_create_kern(PF_INET, SOCK_DCCP, IPPROTO_DCCP, &dccp_ctl_socket); if (rc < 0) printk(dccp_ctl_socket_err_msg); else { dccp_ctl_socket->sk->sk_allocation = GFP_ATOMIC; inet_sk(dccp_ctl_socket->sk)->uc_ttl = -1; /* Unhash it so that IP input processing does not even * see it, we do not wish this socket to see incoming * packets. */ dccp_ctl_socket->sk->sk_prot->unhash(dccp_ctl_socket->sk); } return rc; }
int __init my_udp_sock_init(void) { int err; /* address to bind on */ struct sockaddr_in addr = { .sin_family = AF_INET, .sin_port = htons(MY_UDP_LOCAL_PORT), .sin_addr = { htonl(INADDR_LOOPBACK) } }; int addrlen = sizeof(addr); LOG("UDP Module Initialized"); /* TODO: create UDP socket */ err = sock_create_kern(PF_INET, SOCK_DGRAM, IPPROTO_UDP, &sock); if (err < 0) { printk(LOG_LEVEL "can't create socket\n"); goto out; } /* TODO: bind socket to loopback on port MY_UDP_LOCAL_PORT */ err = sock->ops->bind(sock, (struct sockaddr *) &addr, addrlen); if (err < 0) { printk(LOG_LEVEL "can't bind socket\n"); goto out_release; } /* send message */ err = my_udp_msgsend(sock); if (err < 0) { printk(LOG_LEVEL "can't send message\n"); goto out_release; } return 0; out_release: /* TODO: cleanup socket */ sock->ops->release(sock); out: return err; }
/** * Parse IP address, create a socket and bind it with the address, * but not yet start listening. */ static int add_listen_sock(TfwAddr *addr, int type) { int r; SsProto *proto; struct socket *s; if (listen_socks_n == ARRAY_SIZE(listen_socks)) { TFW_ERR("maximum number of listen sockets (%d) is reached\n", listen_socks_n); return -ENOBUFS; } r = sock_create_kern(addr->sa.sa_family, SOCK_STREAM, IPPROTO_TCP, &s); if (r) { TFW_ERR("can't create socket (err: %d)\n", r); return r; } inet_sk(s->sk)->freebind = 1; s->sk->sk_reuse = 1; r = s->ops->bind(s, &addr->sa, tfw_addr_sa_len(addr)); if (r) { TFW_ERR_ADDR("can't bind to", addr); sock_release(s); return r; } proto = &protos[listen_socks_n]; proto->type = type; BUG_ON(proto->listener); ss_tcp_set_listen(s, proto); TFW_DBG("created front-end socket: sk=%p\n", s->sk); BUG_ON(listen_socks[listen_socks_n]); listen_socks[listen_socks_n] = s; ++listen_socks_n; return 0; }
int sclp_sock_create4(struct net *net, struct sclp_port_cfg *cfg, struct socket **sockp) { int err; struct socket *sock = NULL; struct sockaddr_in sclp_addr; err = sock_create_kern(AF_INET, SOCK_SCLP, 0, &sock); if (err < 0) goto error; sk_change_net(sock->sk, net); sclp_addr.sin_family = AF_INET; sclp_addr.sin_addr = cfg->local_ip; sclp_addr.sin_port = cfg->local_sclp_port; err = kernel_bind(sock, (struct sockaddr*)&sclp_addr, sizeof(sclp_addr)); if (err < 0) goto error; if (cfg->peer_sclp_port) { sclp_addr.sin_family = AF_INET; sclp_addr.sin_addr = cfg->peer_ip; sclp_addr.sin_port = cfg->peer_sclp_port; err = kernel_connect(sock, (struct sockaddr*)&sclp_addr, sizeof(sclp_addr), 0); if (err < 0) goto error; } *sockp = sock; return 0; error: if (sock) { kernel_sock_shutdown(sock, SHUT_RDWR); sk_release_kernel(sock->sk); } *sockp = NULL; return err; }
/** * Create a listening front-end socket. */ static int __open_listen_socket(SsProto *proto, void *addr) { struct socket *s; unsigned short family = *(unsigned short *)addr; unsigned short sza = family == AF_INET ? sizeof(struct sockaddr_in) : sizeof(struct sockaddr_in6); int r; r = sock_create_kern(family, SOCK_STREAM, IPPROTO_TCP, &s); if (r) { TFW_ERR("Can't create front-end listening socket (%d)\n", r); return r; } inet_sk(s->sk)->freebind = 1; s->sk->sk_reuse = 1; r = s->ops->bind(s, (struct sockaddr *)addr, sza); if (r) { TFW_ERR("Can't bind front-end listening socket (%d)\n", r); goto err; } ss_tcp_set_listen(s, proto); TFW_DBG("Created listening socket %p\n", s->sk); /* TODO adjust /proc/sys/net/core/somaxconn */ r = s->ops->listen(s, 1024); if (r) { TFW_ERR("Can't listen on front-end socket (%d)\n", r); goto err; } return r; err: sock_release(s); return r; }
struct socket* createServerSocket(void) { struct socket *_sock; struct sockaddr_in _saddr; unsigned short _port=0x8080; int ret = 0; memset(&_saddr,0,sizeof(_saddr)); _saddr.sin_family = AF_INET; _saddr.sin_port = htons(_port); _saddr.sin_addr.s_addr = htonl(INADDR_ANY); _sock = (struct socket*)kmalloc(sizeof(struct socket),GFP_KERNEL); //_clientSock = (struct socket*)kmalloc(sizeof(struct socket),GFP_KERNEL); ret = sock_create_kern(AF_INET, SOCK_STREAM,0,&_sock); if(ret ) { printk("server socket created error\n"); return NULL; } int value=1; if(kernel_setsockopt(_sock, SOL_SOCKET, SO_REUSEPORT, &value, 4)) { printk("set reuseport option failed\n"); return NULL; } if(kernel_setsockopt(_sock, SOL_SOCKET, SO_REUSEADDR, &value, 4)) { printk("set option reuseaddr failed\n"); return NULL; } printk("reuse port set\n"); ret = _sock->ops->bind(_sock,(struct sockaddr *)&_saddr,sizeof(struct sockaddr_in)); if(ret<0){ printk("server: bind error\n"); return NULL; } printk("server:bind ok!\n"); return _sock; }
struct socket *rds_tcp_listen_init(struct net *net) { struct sockaddr_in sin; struct socket *sock = NULL; int ret; ret = sock_create_kern(net, PF_INET, SOCK_STREAM, IPPROTO_TCP, &sock); if (ret < 0) goto out; sock->sk->sk_reuse = SK_CAN_REUSE; rds_tcp_nonagle(sock); write_lock_bh(&sock->sk->sk_callback_lock); sock->sk->sk_user_data = sock->sk->sk_data_ready; sock->sk->sk_data_ready = rds_tcp_listen_data_ready; write_unlock_bh(&sock->sk->sk_callback_lock); sin.sin_family = PF_INET; sin.sin_addr.s_addr = (__force u32)htonl(INADDR_ANY); sin.sin_port = (__force u16)htons(RDS_TCP_PORT); ret = sock->ops->bind(sock, (struct sockaddr *)&sin, sizeof(sin)); if (ret < 0) goto out; ret = sock->ops->listen(sock, 64); if (ret < 0) goto out; return sock; out: if (sock) sock_release(sock); return NULL; }
static int syslog_connect(struct socket **socket) { int ret = 0; int retry = 0; int logtype = SOCK_DGRAM; struct sockaddr_un syslog_server; while (retry < 2) { ret = sock_create_kern(PF_UNIX, logtype, 0, socket); if (ret < 0) break; syslog_server.sun_family = PF_UNIX; strcpy(syslog_server.sun_path , "/dev/log"); ret = kernel_connect(*socket, (struct sockaddr *)&syslog_server, sizeof(struct sockaddr_un) - 1, 0); if (ret < 0) { if (ret == -EPROTOTYPE) { logtype = (logtype == SOCK_DGRAM ? SOCK_STREAM : SOCK_DGRAM); } retry++; goto cleanup_sock; } (*socket)->sk->sk_allocation = GFP_NOIO; return ret; cleanup_sock: sock_release(*socket); *socket = NULL; } return ret; }
int rds_tcp_accept_one(struct socket *sock) { struct socket *new_sock = NULL; struct rds_connection *conn; int ret; struct inet_sock *inet; struct rds_tcp_connection *rs_tcp; ret = sock_create_kern(sock_net(sock->sk), sock->sk->sk_family, sock->sk->sk_type, sock->sk->sk_protocol, &new_sock); if (ret) goto out; new_sock->type = sock->type; new_sock->ops = sock->ops; ret = sock->ops->accept(sock, new_sock, O_NONBLOCK); if (ret < 0) goto out; ret = rds_tcp_keepalive(new_sock); if (ret < 0) goto out; rds_tcp_tune(new_sock); inet = inet_sk(new_sock->sk); rdsdebug("accepted tcp %pI4:%u -> %pI4:%u\n", &inet->inet_saddr, ntohs(inet->inet_sport), &inet->inet_daddr, ntohs(inet->inet_dport)); conn = rds_conn_create(sock_net(sock->sk), inet->inet_saddr, inet->inet_daddr, &rds_tcp_transport, GFP_KERNEL); if (IS_ERR(conn)) { ret = PTR_ERR(conn); goto out; } /* An incoming SYN request came in, and TCP just accepted it. * * If the client reboots, this conn will need to be cleaned up. * rds_tcp_state_change() will do that cleanup */ rs_tcp = (struct rds_tcp_connection *)conn->c_transport_data; if (rs_tcp->t_sock && ntohl(inet->inet_saddr) < ntohl(inet->inet_daddr)) { struct sock *nsk = new_sock->sk; nsk->sk_user_data = NULL; nsk->sk_prot->disconnect(nsk, 0); tcp_done(nsk); new_sock = NULL; ret = 0; goto out; } else if (rs_tcp->t_sock) { rds_tcp_restore_callbacks(rs_tcp->t_sock, rs_tcp); conn->c_outgoing = 0; } rds_conn_transition(conn, RDS_CONN_DOWN, RDS_CONN_CONNECTING); rds_tcp_set_callbacks(new_sock, conn); rds_connect_complete(conn); new_sock = NULL; ret = 0; out: if (new_sock) sock_release(new_sock); return ret; }
int rpl_udp_sock_create(struct net *net, struct udp_port_cfg *cfg, struct socket **sockp) { int err; struct socket *sock = NULL; #if IS_ENABLED(CONFIG_IPV6) if (cfg->family == AF_INET6) { struct sockaddr_in6 udp6_addr; err = sock_create_kern(net, AF_INET6, SOCK_DGRAM, 0, &sock); if (err < 0) goto error; udp6_addr.sin6_family = AF_INET6; memcpy(&udp6_addr.sin6_addr, &cfg->local_ip6, sizeof(udp6_addr.sin6_addr)); udp6_addr.sin6_port = cfg->local_udp_port; err = kernel_bind(sock, (struct sockaddr *)&udp6_addr, sizeof(udp6_addr)); if (err < 0) goto error; if (cfg->peer_udp_port) { udp6_addr.sin6_family = AF_INET6; memcpy(&udp6_addr.sin6_addr, &cfg->peer_ip6, sizeof(udp6_addr.sin6_addr)); udp6_addr.sin6_port = cfg->peer_udp_port; err = kernel_connect(sock, (struct sockaddr *)&udp6_addr, sizeof(udp6_addr), 0); } if (err < 0) goto error; } else #endif if (cfg->family == AF_INET) { struct sockaddr_in udp_addr; err = sock_create_kern(net, AF_INET, SOCK_DGRAM, 0, &sock); if (err < 0) goto error; udp_addr.sin_family = AF_INET; udp_addr.sin_addr = cfg->local_ip; udp_addr.sin_port = cfg->local_udp_port; err = kernel_bind(sock, (struct sockaddr *)&udp_addr, sizeof(udp_addr)); if (err < 0) goto error; if (cfg->peer_udp_port) { udp_addr.sin_family = AF_INET; udp_addr.sin_addr = cfg->peer_ip; udp_addr.sin_port = cfg->peer_udp_port; err = kernel_connect(sock, (struct sockaddr *)&udp_addr, sizeof(udp_addr), 0); if (err < 0) goto error; } } else { return -EPFNOSUPPORT; } *sockp = sock; return 0; error: if (sock) { kernel_sock_shutdown(sock, SHUT_RDWR); sock_release(sock); } *sockp = NULL; return err; }
void btv_send_msg_thru_eth(const unsigned char *buf, int size) { struct socket *sock[NUM_OF_SOCKS]; struct sockaddr ksockaddr[NUM_OF_SOCKS] = {0}; struct msghdr msg; struct kvec iov; printk("Send Message Thru Eth\n"); for (;;) { /* To communicate TVSOC, we have to prepare two sockets for sync and async ports */ sock_create_kern(PF_INET, SOCK_STREAM, IPPROTO_TCP, &sock[SOCK_FOR_SYNC]); sock_create_kern(PF_INET, SOCK_STREAM, IPPROTO_TCP, &sock[SOCK_FOR_ASYNC]); /* initialize socket for sync command */ ksockaddr[SOCK_FOR_SYNC].sa_family = PF_INET; /* sockaddr.sin_addr.s_addr = inet_addr((const char*)("198.18.186.199")); */ //*((unsigned short *)&(ksockaddr.sa_data[0])) = 0x409c; /* 0x9c40:40000 */ *((unsigned short *)&(ksockaddr[SOCK_FOR_SYNC].sa_data[0])) = htons(40000); /* 0x9c40:40000 */ //*((unsigned int *)&(ksockaddr.sa_data[2])) = 0x0b9c082b; /* 0x2b:43 0x08:08 0x9c:156 0x0b:11 */ *((unsigned int *)&(ksockaddr[SOCK_FOR_SYNC].sa_data[2])) = 0xc7ba12c6; /* 0xc6:198 0x12:18 0xba:186 0xc7:199 */ /* initialize socket for async command */ ksockaddr[SOCK_FOR_ASYNC].sa_family = PF_INET; *((unsigned short *)&(ksockaddr[SOCK_FOR_ASYNC].sa_data[0])) = htons(40001); /* 0x9d40:40001 */ //*((unsigned int *)&(ksockaddr.sa_data[2])) = 0x0b9c082b; /* 0x2b:43 0x08:08 0x9c:156 0x0b:11 */ *((unsigned int *)&(ksockaddr[SOCK_FOR_ASYNC].sa_data[2])) = 0xc7ba12c6; /* 0xc6:198 0x12:18 0xba:186 0xc7:199 */ while(kernel_connect(sock[SOCK_FOR_SYNC], (struct sockaddr *)&ksockaddr[SOCK_FOR_SYNC], sizeof(struct sockaddr), 0) < 0) { printk("Cannot connect server sync port, Retry \n"); mdelay(REBOOT_CMD_INTERVAL_MSEC); } while(kernel_connect(sock[SOCK_FOR_ASYNC], (struct sockaddr *)&ksockaddr[SOCK_FOR_ASYNC], sizeof(struct sockaddr), 0) < 0) { printk("Cannot connect server async port, Retry \n"); mdelay(REBOOT_CMD_INTERVAL_MSEC); } iov.iov_base = (void *)buf; iov.iov_len = ETH_CMD_SIZE; msg.msg_name = (struct sockaddr *)&ksockaddr[SOCK_FOR_SYNC]; msg.msg_namelen = sizeof(struct sockaddr); msg.msg_iov = (struct iovec *) &iov; msg.msg_iovlen = 1; msg.msg_control = NULL; kernel_sendmsg(sock[SOCK_FOR_SYNC], &msg, &iov, 1, ETH_CMD_SIZE); sock_release(sock[SOCK_FOR_SYNC]); sock_release(sock[SOCK_FOR_ASYNC]); mdelay(ETHER_CMD_INTERVAL); } /* NOREACHED */ }