diff --git a/MAINTAINERS b/MAINTAINERS index 64c7169db617..37d664e505c6 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -18106,6 +18106,7 @@ S: Maintained W: http://vtun.sourceforge.net/tun F: Documentation/networking/tuntap.rst F: arch/um/os-Linux/drivers/ +F: include/linux/tun_util.h TURBOCHANNEL SUBSYSTEM M: "Maciej W. Rozycki" diff --git a/drivers/net/tun.c b/drivers/net/tun.c index 978ac0981d16..6373c6f3416e 100644 --- a/drivers/net/tun.c +++ b/drivers/net/tun.c @@ -69,6 +69,7 @@ #include #include #include +#include #include #include @@ -3088,6 +3089,7 @@ static long __tun_chr_ioctl(struct file *file, unsigned int cmd, break; } tun->dev->type = (int) arg; + tun->dev->addr_len = tun_get_addr_len(tun->dev->type); netif_info(tun, drv, tun->dev, "linktype set to %d\n", tun->dev->type); call_netdevice_notifiers(NETDEV_POST_TYPE_CHANGE, diff --git a/include/linux/tun_util.h b/include/linux/tun_util.h new file mode 100644 index 000000000000..daf11320cacc --- /dev/null +++ b/include/linux/tun_util.h @@ -0,0 +1,64 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ + +#ifndef _LINUX_TUN_UTIL_H +#define _LINUX_TUN_UTIL_H + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +/* + * Intended for use within the tun driver, in order to correctly + * set tun->dev->addr_len during TUNSETLINK ioctl processing. + */ +static inline unsigned char tun_get_addr_len(unsigned short type) +{ + switch (type) { + case ARPHRD_IP6GRE: + fallthrough; + case ARPHRD_TUNNEL6: + return sizeof(struct in6_addr); + case ARPHRD_IPGRE: + fallthrough; + case ARPHRD_TUNNEL: + fallthrough; + case ARPHRD_SIT: + return 4; + case ARPHRD_ETHER: + return ETH_ALEN; + case ARPHRD_IEEE802154: + fallthrough; + case ARPHRD_IEEE802154_MONITOR: + return IEEE802154_EXTENDED_ADDR_LEN; + case ARPHRD_PHONET_PIPE: + fallthrough; + case ARPHRD_PPP: + fallthrough; + case ARPHRD_NONE: + return 0; + case ARPHRD_6LOWPAN: + return EUI64_ADDR_LEN; + case ARPHRD_FDDI: + return FDDI_K_ALEN; + case ARPHRD_HIPPI: + return HIPPI_ALEN; + case ARPHRD_IEEE802: + return FC_ALEN; + case ARPHRD_ROSE: + return ROSE_ADDR_LEN; + case ARPHRD_NETROM: + return AX25_ADDR_LEN; + case ARPHRD_LOCALTLK: + return LTALK_ALEN; + default: + return 0; + } +} + +#endif