| 1 |
/* SPDX-License-Identifier: LGPL-2.1-only */ |
| 2 |
|
| 3 |
#include "nl-default.h" |
| 4 |
|
| 5 |
#include <netlink/route/link/ipgre.h> |
| 6 |
|
| 7 |
int main(int argc, char *argv[]) |
| 8 |
{ |
| 9 |
struct nl_cache *link_cache; |
| 10 |
struct rtnl_link *link; |
| 11 |
struct in_addr addr; |
| 12 |
struct nl_sock *sk; |
| 13 |
int err, if_index; |
| 14 |
|
| 15 |
sk = nl_socket_alloc(); |
| 16 |
if ((err = nl_connect(sk, NETLINK_ROUTE)) < 0) { |
| 17 |
nl_perror(err, "Unable to connect socket"); |
| 18 |
return err; |
| 19 |
} |
| 20 |
|
| 21 |
err = rtnl_link_alloc_cache(sk, AF_UNSPEC, &link_cache); |
| 22 |
if ( err < 0) { |
| 23 |
nl_perror(err, "Unable to allocate cache"); |
| 24 |
return err; |
| 25 |
} |
| 26 |
|
| 27 |
if_index = rtnl_link_name2i(link_cache, "eno16777736"); |
| 28 |
if (!if_index) { |
| 29 |
fprintf(stderr, "Unable to lookup eno16777736"); |
| 30 |
return -1; |
| 31 |
} |
| 32 |
|
| 33 |
link = rtnl_link_ipgre_alloc(); |
| 34 |
if(!link) { |
| 35 |
nl_perror(err, "Unable to allocate link"); |
| 36 |
return -1; |
| 37 |
|
| 38 |
} |
| 39 |
rtnl_link_set_name(link, "ipgre-tun"); |
| 40 |
rtnl_link_ipgre_set_link(link, if_index); |
| 41 |
|
| 42 |
inet_pton(AF_INET, "192.168.254.12", &addr.s_addr); |
| 43 |
rtnl_link_ipgre_set_local(link, addr.s_addr); |
| 44 |
|
| 45 |
inet_pton(AF_INET, "192.168.254.13", &addr.s_addr); |
| 46 |
rtnl_link_ipgre_set_remote(link, addr.s_addr); |
| 47 |
|
| 48 |
rtnl_link_ipgre_set_ttl(link, 64); |
| 49 |
err = rtnl_link_add(sk, link, NLM_F_CREATE); |
| 50 |
if (err < 0) { |
| 51 |
nl_perror(err, "Unable to add link"); |
| 52 |
return err; |
| 53 |
} |
| 54 |
|
| 55 |
rtnl_link_put(link); |
| 56 |
nl_close(sk); |
| 57 |
|
| 58 |
return 0; |
| 59 |
} |