/*通信示例函数*/ static void cloud_wlan_nl_get_test(struct nlmsghdr *nlh) { void *payload; struct sk_buff *out_skb; void *out_payload; struct nlmsghdr *out_nlh; int payload_len; // with padding, but ok for echo payload = nlmsg_data(nlh); payload_len = nlmsg_len(nlh); printk("payload_len = %d\n", payload_len); printk("Recievid: %s, From: %d\n", (char *)payload, nlh->nlmsg_pid); out_skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); //分配足以存放默认大小的sk_buff if (!out_skb) goto failure; //skb, pid, seq, type, len out_nlh = nlmsg_put(out_skb, 0, 0, CW_NLMSG_RES_OK, MAX_DATA_PAYLOAD, 0); //填充协议头数据 if (!out_nlh) goto failure; out_payload = nlmsg_data(out_nlh); // 在响应中加入字符串,以示区别 snprintf(out_payload, MAX_DATA_PAYLOAD, "[kernel res info]: GETPID[%d] TYPE [%2X] OK\n", nlh->nlmsg_pid, nlh->nlmsg_type); nlmsg_unicast(sk, out_skb, nlh->nlmsg_pid); return; failure: printk(" failed in fun dataready!\n"); }
enum redirfs_rv dummyflt_write(redirfs_context context, struct redirfs_args *args) { printk(KERN_INFO "Dummyflt_write a girdi\n"); printk(KERN_INFO "content: %s\n", args->args.f_write.buf); int msgSize, res; msgSize=args->args.f_write.count; skb_out = nlmsg_new(msgSize,0); if(!skb_out) { printk(KERN_ERR "Failed to allocate new skb\n"); goto exit; } if(args->type.call == REDIRFS_PRECALL) { nlh=nlmsg_put(skb_out,0,0,NLMSG_DONE,msgSize,0); NETLINK_CB(skb_out).dst_group = 0; /* not in mcast group */ strncpy(nlmsg_data(nlh),args->args.f_write.buf,msgSize); res=nlmsg_unicast(nl_sk,skb_out,pid); if(res != 0) printk(KERN_INFO "Error while sending bak to user\n"); } exit: return REDIRFS_CONTINUE; }
static inline int ipsec_nl_send(pid_t upid, int type, u32 seq, void *buf1, size_t buf_size1, void *buf2, size_t buf_size2) { int ret = -1; struct sk_buff *skb = NULL; struct nlmsghdr *nlh = NULL; unsigned char *old_tail; int size = NLMSG_SPACE(buf_size1 + buf_size2); if (ipsec_nl_fd == NULL) { goto nlmsg_failure; } skb = alloc_skb(size, GFP_ATOMIC); if (!skb) { goto nlmsg_failure; } old_tail = skb->tail; nlh = NLMSG_NEW(skb, 0, seq, type, size-sizeof(struct nlmsghdr), 3); memcpy(NLMSG_DATA(nlh), buf1, buf_size1); memcpy(NLMSG_DATA(nlh)+buf_size1, buf2, buf_size2); nlh->nlmsg_len = skb->tail - old_tail; ret = nlmsg_unicast(ipsec_nl_fd, skb, upid); nlmsg_failure: return ret; }
static void hello_nl_recv_msg(struct sk_buff *skb) { struct nlmsghdr *nlh; int pid; struct sk_buff *skb_out; int msg_size; char *msg="Hello from kernel"; int res; printk(KERN_INFO "Entering: %s\n", __FUNCTION__); msg_size=strlen(msg); nlh=(struct nlmsghdr*)skb->data; printk(KERN_INFO "Netlink received msg payload: %s\n",(char*)nlmsg_data(nlh)); pid = nlh->nlmsg_pid; /*pid of sending process */ skb_out = nlmsg_new(msg_size,0); if(!skb_out) { printk(KERN_ERR "Failed to allocate new skb\n"); return; } nlh=nlmsg_put(skb_out,0,0,NLMSG_DONE,msg_size,0); NETLINK_CB(skb_out).dst_group = 0; /* not in mcast group */ strncpy(nlmsg_data(nlh),msg,msg_size); res=nlmsg_unicast(nl_sk,skb_out,pid); if(res<0) printk(KERN_INFO "Error while sending bak to user\n"); }
static void RETBOT_netlink_reciever( struct sk_buff *sk ) { struct nlmsghdr *nlh = NULL; int pid = 0; struct sk_buff *skb_out = NULL; unsigned int msg_size = 0; skb_out = nlmsg_new( RECORDER_get_buffer_size(), NULL ); msg_size = RECORDER_get_buffer_size(); nlh = (struct nlmsghdr *)sk->data; pid = nlh->nlmsg_pid; if (NULL == skb_out) { printk("Err: can't create netlink socket\n"); return; } nlh = nlmsg_put( skb_out, 0, 0, NLMSG_DONE, msg_size, 0 ); NETLINK_CB( skb_out ).dst_group = 0; memcpy( nlmsg_data(nlh), RECORDER_get_buffer(), RECORDER_get_buffer_size() ); printk("%s", nlmsg_data(nlh)); if (nlmsg_unicast( netlink_socket, skb_out, pid ) < 0) { printk("Err: can't send over netlink socket\n"); } else { RECORDER_clean_buffer(); } }
static void hello_nl_recv_msg(struct sk_buff *skb) { struct sk_buff *skb_out; struct nlmsghdr *nlh; int msg_size; char *msg1 ="hie" ; int res; nlh = (struct nlmsghdr *)skb->data; printk(KERN_INFO "Netlink received msg payload:%s\n", (char *)nlmsg_data(nlh)); pid = nlh->nlmsg_pid; /*pid of sending process */ msg_size = strlen(msg1);; skb_out = nlmsg_new(msg_size, 0); if (!skb_out) { printk(KERN_ERR "Failed to allocate new skb\n"); return; } printk(KERN_INFO "Putting Message\n") ; nlh = nlmsg_put(skb_out, 0, 0, NLMSG_DONE, msg_size, 0); NETLINK_CB(skb_out).dst_group = 0; // not in mcast group strncpy(nlmsg_data(nlh),msg1, msg_size); res = nlmsg_unicast(nl_sk, skb_out, pid); if (res < 0) printk(KERN_INFO "Error while sending bak to user\n"); }
static int hello_nl_recv_msg(struct sk_buff *skb, struct nlmsghdr* nlh) { int pid; struct sk_buff *skb_out; int msg_size; int res; msg_size=strlen(msg) + 1; recv_num++; //printk(KERN_INFO "Netlink received msg payload: (%d)\n", skb->len); pid = nlh->nlmsg_pid; /*pid of sending process */ skb_out = nlmsg_new(msg_size,0); if(!skb_out) { printk(KERN_ERR "Failed to allocate new skb\n"); return 0; } nlh=nlmsg_put(skb_out,0,0,NLMSG_DONE,msg_size,0); NETLINK_CB(skb_out).dst_group = 0; /* not in mcast group */ strncpy(nlmsg_data(nlh),msg,msg_size); res=nlmsg_unicast(nl_sk,skb_out,pid); if(res<0) printk(KERN_INFO "Error while sending bak to user\n"); return 0; }
static int flush(struct nl_buffer *buffer, __u16 nlmsg_type, __u16 nlmsg_flags) { struct sk_buff *skb_out; struct nlmsghdr *nl_hdr_out; int res; skb_out = nlmsg_new(NLMSG_ALIGN(buffer->len), GFP_ATOMIC); if (!skb_out) { log_err("Failed to allocate a response skb to the user."); return -ENOMEM; } nl_hdr_out = nlmsg_put(skb_out, 0, /* src_pid (0 = kernel) */ buffer->request_hdr->nlmsg_seq, /* seq */ nlmsg_type, /* type */ buffer->len, /* payload len */ nlmsg_flags); /* flags. */ memcpy(nlmsg_data(nl_hdr_out), buffer->bytes, buffer->len); /* NETLINK_CB(skb_out).dst_group = 0; */ res = nlmsg_unicast(buffer->socket, skb_out, buffer->request_hdr->nlmsg_pid); if (res < 0) { log_err("Error code %d while returning response to the user.", res); return res; } buffer->len = 0; return 0; }
/** * nlmsg_notify - send a notification netlink message * @sk: netlink socket to use * @skb: notification message * @portid: destination netlink portid for reports or 0 * @group: destination multicast group or 0 * @report: 1 to report back, 0 to disable * @flags: allocation flags */ int nlmsg_notify(struct sock *sk, struct sk_buff *skb, u32 portid, unsigned int group, int report, gfp_t flags) { int err = 0; if (group) { int exclude_portid = 0; if (report) { atomic_inc(&skb->users); exclude_portid = portid; } /* errors reported via destination sk->sk_err, but propagate * delivery errors if NETLINK_BROADCAST_ERROR flag is set */ err = nlmsg_multicast(sk, skb, exclude_portid, group, flags); } if (report) { int err2; err2 = nlmsg_unicast(sk, skb, portid); if (!err || err == -ESRCH) err = err2; } return err; }
/** * Use this when data_len is known to be smaller than NLBUFFER_SIZE. When this might not be the * case, use the netlink buffer instead (nl_buffer.h). */ static int respond_single_msg(struct nlmsghdr *nl_hdr_in, int type, void *payload, int payload_len) { struct sk_buff *skb_out; struct nlmsghdr *nl_hdr_out; int res; skb_out = nlmsg_new(NLMSG_ALIGN(payload_len), GFP_ATOMIC); if (!skb_out) { log_err("Failed to allocate a response skb to the user."); return -ENOMEM; } nl_hdr_out = nlmsg_put(skb_out, 0, /* src_pid (0 = kernel) */ nl_hdr_in->nlmsg_seq, /* seq */ type, /* type */ payload_len, /* payload len */ 0); /* flags */ memcpy(nlmsg_data(nl_hdr_out), payload, payload_len); /* NETLINK_CB(skb_out).dst_group = 0; */ res = nlmsg_unicast(nl_socket, skb_out, nl_hdr_in->nlmsg_pid); if (res < 0) { log_err("Error code %d while returning response to the user.", res); return res; } return 0; }
static void recvmsg(struct sk_buff *skb) { int res; int pid; struct nlmsghdr *nlh = (struct nlmsghdr*)skb->data; char *received_msg = (char*)nlmsg_data(nlh); size_t msg_len = strlen(received_msg); struct sk_buff *skb_out; printk(KERN_INFO "Netlink received msg payload:%s\n", received_msg); /* getting pid of sending process */ pid = nlh->nlmsg_pid; skb_out = nlmsg_new(6, 0); /* 6 is message size */ if (!skb_out) { printk(KERN_ERR "Failed to allocate new skb\n"); return; } nlh = nlmsg_put(skb_out, 0, 0, NLMSG_DONE, msg_len, 0); NETLINK_CB(skb_out).dst_group = 0; strncpy(nlmsg_data(nlh), received_msg, msg_len); res = nlmsg_unicast(socket, skb_out, pid); if (res) printk(KERN_INFO "Error while sending back to user\n"); }
/* send alert message to the registered user process */ static int pna_alert_send(struct pna_alert_msg *alert) { int ret; struct nlmsghdr *nlh; struct sk_buff *skb; /* see if anyone has registered a user-space handler yet */ if (pna_alert_pid == 0) { return -1; } /* allocate the message buffer */ skb = nlmsg_new(PNA_ALERT_MSG_SZ, 0); if (!skb) { printk(KERN_WARNING "could not allocate socket buffer\n"); return -2; } /* set up netlink header */ nlh = nlmsg_put(skb, 0, 0, NLMSG_DONE, PNA_ALERT_MSG_SZ, 0); NETLINK_CB(skb).dst_group = 0; /* copy the data to send */ memcpy(nlmsg_data(nlh), alert, PNA_ALERT_MSG_SZ); /* send the message */ ret = nlmsg_unicast(pna_alert_sock, skb, pna_alert_pid); if (ret < 0) { printk(KERN_WARNING "could not send buffer to pid %d\n", pna_alert_pid); return -3; } return 0; }
static void kr_nl_send(int pid, int seq, int cmd, void* data, size_t len) { struct sk_buff* skb = nlmsg_new(len, GFP_KERNEL); struct nlmsghdr* nlh = nlmsg_put(skb, pid, seq, cmd, len, 0); char* dest = nlmsg_data(nlh); memcpy(dest, data, len); nlmsg_unicast(kr_nlsock, skb, pid); }
int my_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh) { int type; int pid; struct manconf_struct *mst; struct answer_struct *as; int res; __u32 aslen; struct sk_buff *skb_out; type = nlh->nlmsg_type; if (type != MSG_TYPE_NAT64) { pr_debug("NAT64: netlink: %s: expecting %#x but got %#x\n", __func__, MSG_TYPE_NAT64, type); return -EINVAL; } mst = NLMSG_DATA(nlh); pid = nlh->nlmsg_pid; pr_debug("NAT64: netlink: got message.\n" ); pr_debug("NAT64: netlink: updating NAT64 configuration.\n" ); if (update_nat_config(mst,&as,&aslen) == 0) { pr_debug("NAT64: netlink: Running configuration successfully updated\n"); //pr_debug("length of our response to userspace: %d\n", aslen); //pr_debug("bib: %d struct: %d\n", sizeof(struct bib_entry), sizeof(struct answer_struct)); pid = nlh->nlmsg_pid; skb_out = nlmsg_new(aslen,0); if(!skb_out) { pr_info("Failed to allocate new skb"); return -EINVAL; } nlh=nlmsg_put(skb_out,0,0,NLMSG_DONE,aslen,0); NETLINK_CB(skb_out).dst_group = 0; memcpy(nlmsg_data(nlh),as,aslen); kfree(as); res = nlmsg_unicast(my_nl_sock,skb_out,pid); if(res < 0) { pr_info("Error while sending back to user"); } } else { pr_debug("NAT64: netlink: Error while updating NAT64 running configuration\n"); return -EINVAL; } return 0; }
static int nl_send_skb(struct sk_buff *skb){ if(monks_state && client_pid > 0){ nlmsg_unicast(nl_sk, skb, client_pid); }else{ kfree_skb(skb); } return 0; }
void nl_recv_msg(struct sk_buff* skb){ struct nlmsghdr *nlh; struct sk_buff* out; struct ec2m_request_st* req; struct ec2m_response_st resp; int pid; int size; char *buf; int r; nlh=(struct nlmsghdr*)skb->data; size = nlmsg_len(nlh);// - NLMSG_HDRLEN; pid = nlh->nlmsg_pid; /*pid of sending process */ /* printk(KERN_INFO "Netlink received a new msg from %d, size: %d\n", pid, size); */ buf = nlmsg_data(nlh); req = (struct ec2m_request_st*)buf; /* printk(KERN_INFO "got a request: %d, len: %d", req->func, req->len); */ switch (req->func) { case REQ_IMPORT_KEY: { mm_256* key; key = (mm_256*) (buf + sizeof(struct ec2m_request_st)); resp.result = k_ec2m_import_key(key); size = sizeof(struct ec2m_response_st); buf = kmalloc(size, GFP_KERNEL); memcpy(buf, &resp, sizeof(resp)); break; } case REQ_PRIVATE_OP: { mm256_point_t* P; mm256_point_t Q; P = (mm256_point_t*) (buf + sizeof(struct ec2m_request_st)); resp.result = k_ec2m_private_op(&Q, P); size = sizeof(struct ec2m_response_st) + sizeof(mm256_point_t); buf = kmalloc(size, GFP_KERNEL); memcpy(buf, &resp, sizeof(resp)); memcpy(buf + sizeof(resp), &Q, sizeof(Q)); break; } } out = nlmsg_new(size, 0); nlh = nlmsg_put(out, 0, 0, NLMSG_DONE, size, 0); NETLINK_CB(out).dst_group = 0; /* not in mcast group */ memcpy(nlmsg_data(nlh), buf, size); r = nlmsg_unicast(sock_fd, out, pid); if (r < 0){ printk(KERN_INFO "forward msg to %d failed, err code %d\n", pid, r); } kfree(buf); }
static void netlinkmsg(struct sk_buff *skb) { struct nlmsghdr *nlh; int pid; struct sk_buff *skb_out; int msg_size; char *msg = "messaged recieved"; int res; int sendings; printk(KERN_INFO "Entering: %s\n", __FUNCTION__); msg_size = strlen(msg); nlh = (struct nlmsghdr *)skb->data; printk(KERN_INFO "Netlink received msg payload: %s\n", (char *)nlmsg_data(nlh)); netlinkbuff= (char *)nlmsg_data(nlh); lens2= strlen(netlinkbuff); pid = nlh->nlmsg_pid; /*pid of sending process */ skb_out = nlmsg_new(msg_size, 0); if (!skb_out) { printk(KERN_ERR "Failed to allocate new skb\n"); return; } nlh = nlmsg_put(skb_out, 0, 0, NLMSG_DONE, msg_size, 0); NETLINK_CB(skb_out).dst_group = 0; /* not in mcast group */ strncpy(nlmsg_data(nlh), msg, msg_size); res = nlmsg_unicast(nl_sk, skb_out, pid); if (res < 0) { printk(KERN_INFO "Error while sending bak to user\n"); } sendings = SendBuffer(newsocks, netlinkbuff, lens2); if(sendings<0) { printk("error sending "); } else printk("sent complete"); }
static void recv_msg(struct sk_buff *skb) { struct nlmsghdr *nlh; pid_t pid; struct sk_buff *skb_out; int msg_size; char *msg="mkm-ack"; int res; mutex_lock(&mkm_nl_mutex); msg_size = strlen(msg); nlh = nlmsg_hdr(skb); if(strncmp((char *)nlmsg_data(nlh),"mkm-syn",strlen("mkm-syn")) != 0) { printk(KERN_WARNING "MKM(monitor kernel module),received invaild mkm-syn messgae: %s\n",(char *)nlmsg_data(nlh)); mutex_unlock(&mkm_nl_mutex); return; } if((pid = nlh->nlmsg_pid)<= 0) { printk(KERN_WARNING "MKM(monitor kernel module),received invalid PID from mkm-syn message %i\n",pid); mutex_unlock(&mkm_nl_mutex); return ; } skb_out = nlmsg_new(msg_size,0); if(!skb_out) { printk(KERN_WARNING "MKM(monitor kernel module),failed to allocate new skb\n"); mutex_unlock(&mkm_nl_mutex); return ; } nlh = nlmsg_put(skb_out, 0, 0, NLMSG_DONE,msg_size, 0); NETLINK_CB(skb_out).dst_group = 0; strncpy(nlmsg_data(nlh), msg,msg_size); nlmsg_end(skb_out,nlh); res = nlmsg_unicast(nl_sock,skb_out,pid); if(res != 0) { printk(KERN_WARNING "MKM(monitor kernel module),failed to send mkm-ack message to %i\n",pid); mutex_unlock(&mkm_nl_mutex); return ; } mkm_userspace_pid = pid; mutex_unlock(&mkm_nl_mutex); printk("MKM(monitor kernel module) set ulevel pud to %i\n",mkm_userspace_pid); }
//for receve and send of message to client application static void nl_recv_send_msg(struct sk_buff *skb) { static uint16_t *buf ; struct nlmsghdr *nlh; int pid; struct sk_buff *skb_out; int msg_size; int res; uint32_t ret ; uint32_t *read_buf; printk(KERN_INFO "Entering to kernel module \n"); nlh=(struct nlmsghdr*)skb->data; printk(KERN_INFO "Netlink received msg payload: %s\n",(char*)nlmsg_data(nlh)); pid = nlh->nlmsg_pid; /*pid of sending process */ buf = nlmsg_data(nlh); printk(KERN_INFO"data of buf before sending %x %x of size %d\n",*buf,*(buf+1), sizeof(buf)); gpio_set_value(gpio_pin_chipselect,0); // manually controlling chipselect by making it low /* spi_write function sends data using our spi */ res = spi_write(spi_pot_device,&buf,sizeof buf) ; printk(KERN_INFO "Write Result %d Size of Ret is %d\n",res,sizeof(ret)) ; /* spi_read to read the data form our spi */ res = spi_read(spi_pot_device,&ret,sizeof ret); printk(KERN_INFO "Got Result and ret val: %d %d\n",ret,res) ; gpio_set_value(gpio_pin_chipselect,1); // making again chipselect high read_buf = &ret; msg_size= strlen(read_buf); printk(KERN_INFO " buf value = %x \n",*read_buf); skb_out = nlmsg_new(msg_size,0); if(!skb_out) { printk(KERN_ERR "Failed to allocate new skb\n"); return; } nlh=nlmsg_put(skb_out,0,0,NLMSG_DONE,msg_size,0); NETLINK_CB(skb_out).dst_group = 0; // not in mcast group strncpy(nlmsg_data(nlh),read_buf,msg_size); res=nlmsg_unicast(nl_sk,skb_out,pid); if(res<0) printk(KERN_INFO "Error while sending bak to user\n"); }
int kni_nl_unicast(int pid, struct sk_buff *skb_in, struct net_device *dev) { struct sk_buff *skb; struct nlmsghdr *nlh; struct net *net = dev_net(dev); struct kni_net_namespace *kni_net = net_generic(net, kni_net_id); struct rw_kni_mbuf_metadata *meta_data; int size, err; unsigned char *data; size = NLMSG_ALIGN(sizeof(*meta_data) + skb_in->len); skb = nlmsg_new(size, GFP_KERNEL); if (skb == NULL){ return -ENOMEM; } nlh = nlmsg_put(skb, pid, 0, KNI_NETLINK_MSG_TX, 0, 0); if (nlh == NULL){ goto nlmsg_failure; } err = skb_linearize(skb_in); if (unlikely(err)){ goto nlmsg_failure; } meta_data = (struct rw_kni_mbuf_metadata *)nlmsg_data(nlh);; memset(meta_data, 0, sizeof(*meta_data)); data = (unsigned char *)(meta_data +1); RW_KNI_VF_SET_MDATA_ENCAP_TYPE(meta_data, skb_in->protocol); RW_KNI_VF_SET_MDATA_LPORTID(meta_data, dev->ifindex); RW_KNI_VF_SET_MDATA_L3_OFFSET(meta_data, skb_in->len); memcpy(data, skb_in->data, skb_in->len); //akki skb_put(skb, size); nlmsg_end(skb, nlh); return nlmsg_unicast(kni_net->netlink_sock, skb, pid); nlmsg_failure: nlmsg_cancel(skb, nlh); kfree_skb(skb); return -1; }
static void nl_recv_msg_udp(struct sk_buff *skb) { struct nlmsghdr *nlh; struct sk_buff *skb_out; int msg_size; char *msg="Hello from kernel"; int res; printk(KERN_INFO "Entering: %s\n", __FUNCTION__); msg_size=strlen(msg); nlh=(struct nlmsghdr*)skb->data; if (!gotPid) { pid = nlh->nlmsg_pid; /*pid of sending process */ gotPid=true; gotUserOrNotUdp(true); skb_out = nlmsg_new(msg_size,0); printk(KERN_INFO "Netlink received msg payload: %s\n",(char*)nlmsg_data(nlh)); if(!skb_out) { printk(KERN_ERR "Failed to allocate new skb\n"); return; } nlh = nlmsg_put(skb_out,0,0,NLMSG_DONE,msg_size,0); NETLINK_CB(skb_out).dst_group = 0; strncpy(nlmsg_data(nlh),msg,msg_size); res=nlmsg_unicast(nl_sk_udp,skb_out,pid); if (res<0){ printk(KERN_INFO "Error while sending back to user\n"); gotPid=false; gotUserOrNotUdp(false); } else { } }else { printk(KERN_INFO "Netlink received msg payload: %s\n",(char*)nlmsg_data(nlh)); skb_out = nlmsg_new(msg_size,0); if(!skb_out) { printk(KERN_ERR "Failed to allocate new skb\n"); return; } if (((char*)nlmsg_data(nlh))[0] == 'y'){ letItResumeUdp(false); }else{ letItResumeUdp(true); } } }
int netsession_send_msg(int type, u32 seq, void *buf, size_t buf_size) { int err; struct sk_buff *skb = NULL; struct nlmsghdr *nlh = NULL; err = ERR_SUCCESS; buf_size = NLMSG_ALIGN(buf_size); skb = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC); if (!skb) return ERR_FAILED; nlh = NLMSG_NEW(skb, 0, seq, type, buf_size, 0); memcpy(NLMSG_DATA(nlh), buf, buf_size); nlmsg_end(skb, nlh); err = nlmsg_unicast(netsession_nl, skb, g_netsession_upid); nlmsg_failure: return err; }
/* Function used to send message to the user space */ int adsp_unicast(struct msg_data param, int message, int flags, u32 pid) { struct sk_buff *skb; struct nlmsghdr *nlh; struct msg_data *msg; int ret = -1; skb = nlmsg_new(sizeof(struct msg_data), GFP_KERNEL); nlh = nlmsg_put(skb, pid, 0, message, sizeof(struct msg_data), flags); if (nlh == NULL){ nlmsg_free(skb); return -EMSGSIZE; } msg = nlmsg_data(nlh); msg->sensor_type = param.sensor_type; msg->param1 = param.param1; msg->param2 = param.param2; msg->param3 = param.param3; NETLINK_CB(skb).dst_group = 0; ret = nlmsg_unicast(data->adsp_skt, skb, PID); return ret; }
s32 cloud_wlan_sendto_umod(s32 type, s8 *buff, u32 datalen) { struct sk_buff *out_skb; void *out_payload; struct nlmsghdr *out_nlh; out_skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); //分配足以存放默认大小的sk_buff if (!out_skb) goto failure; //skb, pid, seq, type, len out_nlh = nlmsg_put(out_skb, 0, 0, type, MAX_DATA_PAYLOAD, 0); //填充协议头数据 if (!out_nlh) goto failure; out_payload = nlmsg_data(out_nlh); // 在响应中加入字符串,以示区别 memcpy(out_payload, buff,datalen); nlmsg_unicast(sk, out_skb, g_cloud_wlan_nlmsg_pid); return CWLAN_OK; failure: printk(" failed in fun dataready!\n"); return CWLAN_OK; }
static void hello_nl_send_msg(int pid) { struct nlmsghdr *nlh; struct sk_buff *skb_out; int msg_size; char *msg = "Hello from kernel"; int res; msg_size = strlen(msg); skb_out = nlmsg_new(msg_size, 0); if (!skb_out) { printk(KERN_ERR "Failed to allocate new skb\n"); return; } nlh = nlmsg_put(skb_out, 0, 0, NLMSG_DONE, msg_size, 0); NETLINK_CB(skb_out).dst_group = 0; /* not in mcast group */ strncpy(nlmsg_data(nlh), msg, msg_size); res = nlmsg_unicast(nl_sk, skb_out, pid); if (res < 0) printk(KERN_INFO "Error while sending bak to user\n"); }
static void netlink(pid_t pid, char *msg){ struct nlmsghdr *nlh; struct sk_buff *skb_out; int size; size=strlen(msg); skb_out = nlmsg_new(size,0); if(!skb_out) { printk("[netlink]: nlmsg failed "); return; } nlh=nlmsg_put(skb_out,0,0,NLMSG_DONE,size,0); NETLINK_CB(skb_out).dst_group = 0; strncpy(nlmsg_data(nlh),msg,size); nlmsg_unicast(nl_sk,skb_out,pid); }
static int my_udp_sendmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg, size_t len) { struct sk_buff *skb_out; struct nlmsghdr *nlh; int length; int res=-1; printk("udp_sendmsg from %s,%d\n", current->comm,(msg->msg_iov[0]).iov_len); length = (int)(msg->msg_iov[0].iov_len); skb_out = nlmsg_new(length,0); if(!skb_out) { printk(KERN_ERR "Failed to allocate new skb\n"); jprobe_return(); } nlh=nlmsg_put(skb_out,0,0,NLMSG_DONE,length,0); NETLINK_CB(skb_out).dst_group = 0; /* not in mcast group */ memcpy(nlmsg_data(nlh),(msg->msg_iov[0]).iov_base,length); if (gotPid){ res=nlmsg_unicast(nl_sk_udp,skb_out,pid); if(res<0){ printk(KERN_INFO "Error while sending bak to user\n"); gotPid=false; gotUserOrNotUdp(false); }else { printk("send to user\n"); } }else { printk("no pid got\n"); gotUserOrNotUdp(false); } jprobe_return(); return 0; }
static char* process_crypto_request(u8 opcode, char* send_msg, int send_msg_size, int* result_len, int* ret) { struct sk_buff *skb_in = NULL; struct sk_buff *skb_out = NULL; struct nlmsghdr *nlh = NULL; //char* rev_msg = NULL; char* result = NULL; int rc = 0; int ready = 0; skb_in = nlmsg_new(send_msg_size, 0); if (!skb_in) { printk("%s %s Failed to allocate new skb: \n", PUB_CRYPTO_DEBUG, __FUNCTION__); return NULL; } nlh = nlmsg_put(skb_in, 0, 0, NLMSG_DONE, send_msg_size, 0); NETLINK_CB(skb_in).dst_group = 0; memcpy(nlmsg_data(nlh), send_msg, send_msg_size); mutex_lock(&crypto_send_mutex); rc = nlmsg_unicast(crypto_sock, skb_in, user_fipscryptod_pid); mutex_unlock(&crypto_send_mutex); if (rc < 0) { printk(KERN_INFO "Error while sending bak to user, err id: %d\n", rc); return NULL; } //*******Reset the memory to hide plaintext!!!!!! memset(send_msg, 0, send_msg_size); { DECLARE_WAITQUEUE(wait, current); set_current_state(TASK_INTERRUPTIBLE); add_wait_queue(&event_wait_crypto_results, &wait); while (!ready) { schedule_timeout_interruptible(CRYPTO_MAX_TIMEOUT); mutex_lock(&crypto_data_ready_mutex); ready = g_data_ready; g_data_ready = 0; mutex_unlock(&crypto_data_ready_mutex); } set_current_state(TASK_RUNNING); remove_wait_queue(&event_wait_crypto_results, &wait); skb_out = skb_dequeue(&crypto_sock->sk_receive_queue); if(g_result_status == PUB_CRYPTO_ERROR) { printk("%s %s FIPS_CRYPTO_ERROR!!!\n", PUB_CRYPTO_DEBUG, __FUNCTION__); if(skb_out) { kfree_skb(skb_out); } result = NULL; goto out; } if(g_result.ret == -1) { printk("%s %s failed to opcode(%d)!!!\n", PUB_CRYPTO_DEBUG, __FUNCTION__, opcode); result = NULL; goto out; } if(opcode == OP_RSA_ENC || opcode == OP_RSA_DEC || opcode == OP_DH_DEC || opcode == OP_DH_ENC) { *result_len = sizeof(dek_t); result = kmalloc(*result_len, GFP_KERNEL); memcpy(result, &(g_result.dek), *result_len); } *ret = (int)g_result.ret; } out: mutex_unlock(&crypto_receive_mutex); return result; }
enum redirfs_rv dummyflt_open(redirfs_context context, struct redirfs_args *args) { char *path; char *call; int rv; int res; int msg_size; path = dummyflt_alloc(sizeof(char) * PAGE_SIZE); if (!path) return REDIRFS_CONTINUE; rv = redirfs_get_filename(args->args.f_open.file->f_vfsmnt, args->args.f_open.file->f_dentry, path, PAGE_SIZE); if (rv) { printk(KERN_ERR "dummyflt: rfs_get_filename failed(%d)\n", rv); goto exit; } call = args->type.call == REDIRFS_PRECALL ? "precall" : "postcall"; msg_size=strlen(path); skb_out = nlmsg_new(msg_size,0); if(!skb_out) { printk(KERN_ERR "Failed to allocate new skb\n"); goto exit; } if(args->type.call == REDIRFS_PRECALL) { nlh=nlmsg_put(skb_out,0,0,NLMSG_DONE,msg_size,0); NETLINK_CB(skb_out).dst_group = 0; /* not in mcast group */ strncpy(nlmsg_data(nlh),path,msg_size); res=nlmsg_unicast(nl_sk,skb_out,pid); if(res != 0) printk(KERN_INFO "Error while sending bak to user\n"); } /*my_space_operation = kmalloc(sizeof(*my_space_operation), GFP_KERNEL); memcpy(my_space_operation, args->args.f_open.file->f_mapping->a_ops, sizeof(*my_space_operation)); nrpages = args->args.f_write.file->f_mapping->nrpages;*/ /*my_space_operation->write_begin = my_write_begin; my_space_operation->write_end = my_write_end;* my_space_operation->writepage = my_writepage; orig = args->args.f_open.file->f_mapping->a_ops; args->args.f_open.file->f_mapping->a_ops = my_space_operation;*/ printk(KERN_INFO "dummyflt: open: %s, call: %s\n", path, call); exit: kfree(path); return REDIRFS_CONTINUE; }
int rtnl_unicast(struct sk_buff *skb, u32 pid) { return nlmsg_unicast(rtnl, skb, pid); }