debian11ssh无法使用route
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了debian11ssh无法使用route相关的知识,希望对你有一定的参考价值。
参考技术A 安装ssh服务 sudo apt install openssh-server2. 安装vim apt-getinstall vim
3. 用vim打开/etc/ssh/sshd_config 在如图位置追加如下 PermitRootLoginyes PasswordAuthenticationyes
无法使用 NETLINK_ROUTE 通道接收从内核模块发送到用户应用程序的自定义消息
【中文标题】无法使用 NETLINK_ROUTE 通道接收从内核模块发送到用户应用程序的自定义消息【英文标题】:Unable to receive customized message sent from kernel module to user application using NETLINK_ROUTE channel 【发布时间】:2014-10-12 05:53:43 【问题描述】:我正在使用 Netlink 套接字通过NETLINK_ROUTE
通道将有关以太网接口状态的自定义通知从内核模块发送到用户空间应用程序。我浏览了几篇文章和论文,但它们都展示了一种方法,您需要定义自己的家庭,例如NETLINK_TEST 在netlink.h
标头中或使用NETLINK_GENERIC
。我知道使用 NETLINK_ROUTE 的套接字已经归内核所有,因此无法在内核模块中创建它。我无法在用户空间接收消息。任何指导将不胜感激。所以这里有两个代码:
内核模块:
#include <linux/notifier.h>
#include <asm/kdebug.h>
#include <linux/netdevice.h>
#include <linux/inetdevice.h>
#include <linux/module.h>
#include <net/sock.h>
#include <linux/netlink.h>
#include <linux/rtnetlink.h>
#include <asm/types.h>
#include <linux/skbuff.h>
MODULE_LICENSE("GPL");
int my_dev_event_handler(struct notifier_block *this, unsigned long event, void *ptr)
struct sk_buff *skb = NULL;
struct nlmsghdr *nlh;
int size = 0;
char buf[512];
switch (event)
case NETDEV_REGISTER:
sprintf (buf, "Interface:: %s is Registered with the Notifier...", ((struct net_device *) ptr)->name);
break;
case NETDEV_UP:
sprintf (buf, "Interface:: %s is Up and Running...", ((struct net_device *) ptr)->name);
break;
case NETDEV_GOING_DOWN:
sprintf (buf, "Interface:: %s is going Down...", ((struct net_device *) ptr)->name);
break;
case NETDEV_DOWN:
sprintf (buf, "Interface:: %s is Down...", ((struct net_device *) ptr)->name);
break;
printk (KERN_INFO "Content of Buf :: %s" , buf);
size = sizeof(buf);
skb = nlmsg_new(size, GFP_ATOMIC);
if (skb == NULL)
printk(KERN_ERR "\nError Allocating skb for sending Netlink Message...\n");
return -1;
nlh = nlmsg_put(skb, 0, 0, NLMSG_DONE, size, 0);
if (nlh == NULL)
printk(KERN_ERR "\nError putting Netlink Message data into skb...\n");
goto nlmsg_failure;
NETLINK_CB(skb).dst_group = RTNLGRP_LINK;
strncpy(nlmsg_data(nlh), buf, size);
nlmsg_end(skb, nlh);
rtnl_notify(skb, &init_net, 0, RTNLGRP_LINK, nlh, 0);
return 0;
nlmsg_failure:
kfree_skb(skb);
return -EMSGSIZE;
static struct notifier_block my_dev_notifier =
.notifier_call = my_dev_event_handler,
;
static int __init my_init (void)
printk(KERN_ALERT "***IFM Module Loaded***\n");
register_netdevice_notifier (&my_dev_notifier);
return 0;
static void __exit my_end(void)
printk(KERN_ALERT "***IFM Module Unloaded***\n");
unregister_netdevice_notifier (&my_dev_notifier);
module_init(my_init);
module_exit(my_end);
用户空间应用:
#include <asm/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <net/if.h>
#include <netinet/in.h>
#include <linux/netlink.h>
#include <linux/rtnetlink.h>
#include <stdlib.h>
#include <sys/time.h>
#include <sys/types.h>
#define MAX_PAYLOAD 1024
struct sockaddr_nl src_addr, dest_addr;
int read_event (int sockint)
int status;
int ret = 0;
char buf[4096];
struct iovec iov = buf, sizeof(buf) ;
struct msghdr msg = (void *) &dest_addr, sizeof dest_addr, &iov, 1, NULL, 0, 0 ;
struct nlmsghdr *h;
h = (struct nlmsghdr *)malloc(NLMSG_SPACE(MAX_PAYLOAD));
memset(h, 0, NLMSG_SPACE(MAX_PAYLOAD));
h->nlmsg_len = NLMSG_SPACE(MAX_PAYLOAD);
h->nlmsg_pid = getpid();
h->nlmsg_flags = 0;
strcpy(NLMSG_DATA(h), "Hello");
printf("Sending message to kernel\n");
sendmsg(sockint, &msg, 0);
printf("Waiting for message from kernel\n");
memset(h, 0, NLMSG_SPACE(MAX_PAYLOAD));
status = recvmsg (sockint, &msg, 0);
if (status < 0)
if (errno == EWOULDBLOCK || errno == EAGAIN)
return ret;
printf ("read_netlink: Error recvmsg: %d\n", status);
perror ("read_netlink: Error: ");
return status;
if (status == 0)
printf ("read_netlink: EOF\n");
printf("\nNo. of Bytes read : %d\n", status);
printf("Received Payload data : %s", NLMSG_DATA (h));
return ret;
int main (void)
fd_set rfds;
struct timeval tv;
int retval;
int nl_socket = socket (AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
if (nl_socket < 0)
printf ("Socket Open Error!");
exit (1);
memset ((void *) &src_addr, 0, sizeof (src_addr));
src_addr.nl_family = AF_NETLINK;
src_addr.nl_pid = getpid ();
//src_addr.nl_pid = 0;
src_addr.nl_groups = RTMGRP_LINK;
//src_addr.nl_groups = RTMGRP_LINK | RTMGRP_IPV4_IFADDR | RTMGRP_IPV6_IFADDR;
if (bind (nl_socket, (struct sockaddr *) &src_addr, sizeof (src_addr)) < 0)
printf ("Socket bind failed!");
exit (1);
memset(&dest_addr, 0, sizeof(dest_addr));
dest_addr.nl_family = AF_NETLINK;
dest_addr.nl_pid = 0; /* For Linux Kernel */
dest_addr.nl_groups = 0; /* unicast */
while (1)
FD_ZERO (&rfds);
//FD_CLR (nl_socket, &rfds);
FD_SET (nl_socket, &rfds);
tv.tv_sec = 5;
tv.tv_usec = 0;
retval = select (FD_SETSIZE, &rfds, NULL, NULL, &tv);
if (retval == -1)
printf ("Error in select() \n");
else if (retval)
printf ("Event received >> ");
read_event (nl_socket);
else
printf ("## Select Timed Out ## \n");
return 0;
【问题讨论】:
对您来说可能为时已晚,但在这里:iov
应该引用 h
,而不是 buf
。目前你的h
没有在任何地方使用,所以难怪它不起作用。顺便提一句。由于您有 2 个大部分独立的部分,因此首先实现用户空间部分(处理现有消息)是有意义的,因此您不必一次调试两组代码。
【参考方案1】:
我认为你的错误应该将buf
转换为struct nlmsghdr *
然后填写信息。
char buf[4096];
struct iovec iov = buf, sizeof(buf) ;
struct msghdr msg = (void *) &dest_addr, sizeof dest_addr, &iov, 1, NULL, 0, 0 ;
struct nlmsghdr *h;
memset(buf, 0, sizeof(buf));
h = (struct nlmsghdr *)buf;
h->nlmsg_len = NLMSG_SPACE(MAX_PAYLOAD);
h->nlmsg_pid = getpid();
h->nlmsg_flags = 0;
strcpy(NLMSG_DATA(h), "Hello");
printf("Sending message to kernel\n");
sendmsg(sockint, &msg, 0);
【讨论】:
以上是关于debian11ssh无法使用route的主要内容,如果未能解决你的问题,请参考以下文章
使用 EC2 和 Route53 更新后,我的域无法正常工作