libmnl  1.0.4
nl-crash.c
1 /* Test to crash netlink with large vmalloc'ed skbuff */
2 #include <netinet/in.h>
3 #include <arpa/inet.h>
4 #include <time.h>
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <unistd.h>
8 #include <strings.h>
9 #include <net/if.h>
10 
11 #include <libmnl/libmnl.h>
12 #include <linux/if_link.h>
13 #include <linux/rtnetlink.h>
14 
15 static int test(int family)
16 {
17  struct mnl_socket *nl;
18  char buf[MNL_SOCKET_BUFFER_SIZE*4];
19  struct nlmsghdr *nlh;
20  struct rtmsg *rtm;
21  uint32_t prefix, seq, portid;
22  int ret;
23 
24  nlh = mnl_nlmsg_put_header(buf);
25  nlh->nlmsg_type = 20; /* don't care */
26  nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_CREATE | NLM_F_ACK;
27  nlh->nlmsg_seq = seq = time(NULL);
28  nlh->nlmsg_len += sizeof(buf) - sizeof(struct nlmsghdr) - 100;
29 
30  nl = mnl_socket_open(family);
31  if (nl == NULL) {
32  perror("mnl_socket_open");
33  return -1;
34  }
35 
36  if (mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID) < 0) {
37  perror("mnl_socket_bind");
38  return -1;
39  }
40  portid = mnl_socket_get_portid(nl);
41 
42  /* Send garbage, don't care */
43  if (mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) < 0) {
44  perror("mnl_socket_send");
45  return -1;
46  }
47 
48  ret = mnl_cb_run(buf, ret, seq, portid, NULL, NULL);
49  if (ret < 0) {
50  perror("mnl_cb_run");
51  return -1;
52  }
53 
54  mnl_socket_close(nl);
55  return 0;
56 }
57 
58 int main(int argc, char *argv[])
59 {
60  int i=0;
61 
62  for (i=0; i<20; i++) {
63  printf("testing %d\n", i);
64  test(i);
65  }
66 }