2 * Copyright (C)2006 USAGI/WIDE Project
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 * split from ip_tunnel.c
23 * Masahide NAKAMURA @USAGI
30 #include <sys/types.h>
31 #include <sys/socket.h>
32 #include <sys/ioctl.h>
33 #include <netinet/in.h>
36 #include <linux/if_tunnel.h>
41 const char *tnl_strproto(__u8 proto)
59 strcpy(buf, "unknown");
66 int tnl_get_ioctl(const char *basedev, void *p)
72 strncpy(ifr.ifr_name, basedev, IFNAMSIZ);
73 ifr.ifr_ifru.ifru_data = (void*)p;
74 fd = socket(preferred_family, SOCK_DGRAM, 0);
75 err = ioctl(fd, SIOCGETTUNNEL, &ifr);
77 fprintf(stderr, "get tunnel %s failed: %s\n", basedev,
84 int tnl_add_ioctl(int cmd, const char *basedev, const char *name, void *p)
90 if (cmd == SIOCCHGTUNNEL && name[0])
91 strncpy(ifr.ifr_name, name, IFNAMSIZ);
93 strncpy(ifr.ifr_name, basedev, IFNAMSIZ);
94 ifr.ifr_ifru.ifru_data = p;
95 fd = socket(preferred_family, SOCK_DGRAM, 0);
96 err = ioctl(fd, cmd, &ifr);
98 fprintf(stderr, "add tunnel %s failed: %s\n", ifr.ifr_name,
104 int tnl_del_ioctl(const char *basedev, const char *name, void *p)
111 strncpy(ifr.ifr_name, name, IFNAMSIZ);
113 strncpy(ifr.ifr_name, basedev, IFNAMSIZ);
115 ifr.ifr_ifru.ifru_data = p;
116 fd = socket(preferred_family, SOCK_DGRAM, 0);
117 err = ioctl(fd, SIOCDELTUNNEL, &ifr);
119 fprintf(stderr, "delete tunnel %s failed: %s\n",
120 ifr.ifr_name, strerror(errno));
125 static int tnl_gen_ioctl(int cmd, const char *name,
126 void *p, int skiperr)
132 strncpy(ifr.ifr_name, name, IFNAMSIZ);
133 ifr.ifr_ifru.ifru_data = p;
134 fd = socket(preferred_family, SOCK_DGRAM, 0);
135 err = ioctl(fd, cmd, &ifr);
136 if (err && errno != skiperr)
137 fprintf(stderr, "%s: ioctl %x failed: %s\n", name,
138 cmd, strerror(errno));
143 int tnl_prl_ioctl(int cmd, const char *name, void *p)
145 return tnl_gen_ioctl(cmd, name, p, -1);
148 int tnl_6rd_ioctl(int cmd, const char *name, void *p)
150 return tnl_gen_ioctl(cmd, name, p, -1);
153 int tnl_ioctl_get_6rd(const char *name, void *p)
155 return tnl_gen_ioctl(SIOCGET6RD, name, p, EINVAL);