libmnl  1.0.4
rtnl-route-add.c
1 /* This example is placed in the public domain. */
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 int main(int argc, char *argv[])
16 {
17  struct mnl_socket *nl;
18  char buf[MNL_SOCKET_BUFFER_SIZE];
19  struct nlmsghdr *nlh;
20  struct rtmsg *rtm;
21  uint32_t prefix, seq, portid;
22  union {
23  in_addr_t ip;
24  struct in6_addr ip6;
25  } dst;
26  union {
27  in_addr_t ip;
28  struct in6_addr ip6;
29  } gw;
30  int iface, ret, family = AF_INET;
31 
32  if (argc <= 3) {
33  printf("Usage: %s iface destination cidr [gateway]\n", argv[0]);
34  printf("Example: %s eth0 10.0.1.12 32 10.0.1.11\n", argv[0]);
35  printf(" %s eth0 ffff::10.0.1.12 128 fdff::1\n", argv[0]);
36  exit(EXIT_FAILURE);
37  }
38 
39  iface = if_nametoindex(argv[1]);
40  if (iface == 0) {
41  perror("if_nametoindex");
42  exit(EXIT_FAILURE);
43  }
44 
45  if (!inet_pton(AF_INET, argv[2], &dst)) {
46  if (!inet_pton(AF_INET6, argv[2], &dst)) {
47  perror("inet_pton");
48  exit(EXIT_FAILURE);
49  }
50  family = AF_INET6;
51  }
52 
53  if (sscanf(argv[3], "%u", &prefix) == 0) {
54  perror("sscanf");
55  exit(EXIT_FAILURE);
56  }
57 
58  if (argc == 5 && !inet_pton(family, argv[4], &gw)) {
59  perror("inet_pton");
60  exit(EXIT_FAILURE);
61  }
62 
63  nlh = mnl_nlmsg_put_header(buf);
64  nlh->nlmsg_type = RTM_NEWROUTE;
65  nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_CREATE | NLM_F_ACK;
66  nlh->nlmsg_seq = seq = time(NULL);
67 
68  rtm = mnl_nlmsg_put_extra_header(nlh, sizeof(struct rtmsg));
69  rtm->rtm_family = family;
70  rtm->rtm_dst_len = prefix;
71  rtm->rtm_src_len = 0;
72  rtm->rtm_tos = 0;
73  rtm->rtm_protocol = RTPROT_STATIC;
74  rtm->rtm_table = RT_TABLE_MAIN;
75  rtm->rtm_type = RTN_UNICAST;
76  /* is there any gateway? */
77  rtm->rtm_scope = (argc == 4) ? RT_SCOPE_LINK : RT_SCOPE_UNIVERSE;
78  rtm->rtm_flags = 0;
79 
80  if (family == AF_INET)
81  mnl_attr_put_u32(nlh, RTA_DST, dst.ip);
82  else
83  mnl_attr_put(nlh, RTA_DST, sizeof(struct in6_addr), &dst);
84 
85  mnl_attr_put_u32(nlh, RTA_OIF, iface);
86  if (argc == 5) {
87  if (family == AF_INET)
88  mnl_attr_put_u32(nlh, RTA_GATEWAY, gw.ip);
89  else {
90  mnl_attr_put(nlh, RTA_GATEWAY, sizeof(struct in6_addr),
91  &gw.ip6);
92  }
93  }
94 
95  nl = mnl_socket_open(NETLINK_ROUTE);
96  if (nl == NULL) {
97  perror("mnl_socket_open");
98  exit(EXIT_FAILURE);
99  }
100 
101  if (mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID) < 0) {
102  perror("mnl_socket_bind");
103  exit(EXIT_FAILURE);
104  }
105  portid = mnl_socket_get_portid(nl);
106 
107  if (mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) < 0) {
108  perror("mnl_socket_sendto");
109  exit(EXIT_FAILURE);
110  }
111 
112  ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
113  if (ret < 0) {
114  perror("mnl_socket_recvfrom");
115  exit(EXIT_FAILURE);
116  }
117 
118  ret = mnl_cb_run(buf, ret, seq, portid, NULL, NULL);
119  if (ret < 0) {
120  perror("mnl_cb_run");
121  exit(EXIT_FAILURE);
122  }
123 
124  mnl_socket_close(nl);
125 
126  return 0;
127 }