18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * Copyright (C) 2001 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com) 48c2ecf20Sopenharmony_ci */ 58c2ecf20Sopenharmony_ci 68c2ecf20Sopenharmony_ci#include <linux/netdevice.h> 78c2ecf20Sopenharmony_ci#include <linux/init.h> 88c2ecf20Sopenharmony_ci#include <linux/skbuff.h> 98c2ecf20Sopenharmony_ci#include <asm/errno.h> 108c2ecf20Sopenharmony_ci#include <net_kern.h> 118c2ecf20Sopenharmony_ci#include "tuntap.h" 128c2ecf20Sopenharmony_ci 138c2ecf20Sopenharmony_cistruct tuntap_init { 148c2ecf20Sopenharmony_ci char *dev_name; 158c2ecf20Sopenharmony_ci char *gate_addr; 168c2ecf20Sopenharmony_ci}; 178c2ecf20Sopenharmony_ci 188c2ecf20Sopenharmony_cistatic void tuntap_init(struct net_device *dev, void *data) 198c2ecf20Sopenharmony_ci{ 208c2ecf20Sopenharmony_ci struct uml_net_private *pri; 218c2ecf20Sopenharmony_ci struct tuntap_data *tpri; 228c2ecf20Sopenharmony_ci struct tuntap_init *init = data; 238c2ecf20Sopenharmony_ci 248c2ecf20Sopenharmony_ci pri = netdev_priv(dev); 258c2ecf20Sopenharmony_ci tpri = (struct tuntap_data *) pri->user; 268c2ecf20Sopenharmony_ci tpri->dev_name = init->dev_name; 278c2ecf20Sopenharmony_ci tpri->fixed_config = (init->dev_name != NULL); 288c2ecf20Sopenharmony_ci tpri->gate_addr = init->gate_addr; 298c2ecf20Sopenharmony_ci tpri->fd = -1; 308c2ecf20Sopenharmony_ci tpri->dev = dev; 318c2ecf20Sopenharmony_ci 328c2ecf20Sopenharmony_ci printk(KERN_INFO "TUN/TAP backend - "); 338c2ecf20Sopenharmony_ci if (tpri->gate_addr != NULL) 348c2ecf20Sopenharmony_ci printk(KERN_CONT "IP = %s", tpri->gate_addr); 358c2ecf20Sopenharmony_ci printk(KERN_CONT "\n"); 368c2ecf20Sopenharmony_ci} 378c2ecf20Sopenharmony_ci 388c2ecf20Sopenharmony_cistatic int tuntap_read(int fd, struct sk_buff *skb, struct uml_net_private *lp) 398c2ecf20Sopenharmony_ci{ 408c2ecf20Sopenharmony_ci return net_read(fd, skb_mac_header(skb), 418c2ecf20Sopenharmony_ci skb->dev->mtu + ETH_HEADER_OTHER); 428c2ecf20Sopenharmony_ci} 438c2ecf20Sopenharmony_ci 448c2ecf20Sopenharmony_cistatic int tuntap_write(int fd, struct sk_buff *skb, struct uml_net_private *lp) 458c2ecf20Sopenharmony_ci{ 468c2ecf20Sopenharmony_ci return net_write(fd, skb->data, skb->len); 478c2ecf20Sopenharmony_ci} 488c2ecf20Sopenharmony_ci 498c2ecf20Sopenharmony_ciconst struct net_kern_info tuntap_kern_info = { 508c2ecf20Sopenharmony_ci .init = tuntap_init, 518c2ecf20Sopenharmony_ci .protocol = eth_protocol, 528c2ecf20Sopenharmony_ci .read = tuntap_read, 538c2ecf20Sopenharmony_ci .write = tuntap_write, 548c2ecf20Sopenharmony_ci}; 558c2ecf20Sopenharmony_ci 568c2ecf20Sopenharmony_ciint tuntap_setup(char *str, char **mac_out, void *data) 578c2ecf20Sopenharmony_ci{ 588c2ecf20Sopenharmony_ci struct tuntap_init *init = data; 598c2ecf20Sopenharmony_ci 608c2ecf20Sopenharmony_ci *init = ((struct tuntap_init) 618c2ecf20Sopenharmony_ci { .dev_name = NULL, 628c2ecf20Sopenharmony_ci .gate_addr = NULL }); 638c2ecf20Sopenharmony_ci if (tap_setup_common(str, "tuntap", &init->dev_name, mac_out, 648c2ecf20Sopenharmony_ci &init->gate_addr)) 658c2ecf20Sopenharmony_ci return 0; 668c2ecf20Sopenharmony_ci 678c2ecf20Sopenharmony_ci return 1; 688c2ecf20Sopenharmony_ci} 698c2ecf20Sopenharmony_ci 708c2ecf20Sopenharmony_cistatic struct transport tuntap_transport = { 718c2ecf20Sopenharmony_ci .list = LIST_HEAD_INIT(tuntap_transport.list), 728c2ecf20Sopenharmony_ci .name = "tuntap", 738c2ecf20Sopenharmony_ci .setup = tuntap_setup, 748c2ecf20Sopenharmony_ci .user = &tuntap_user_info, 758c2ecf20Sopenharmony_ci .kern = &tuntap_kern_info, 768c2ecf20Sopenharmony_ci .private_size = sizeof(struct tuntap_data), 778c2ecf20Sopenharmony_ci .setup_size = sizeof(struct tuntap_init), 788c2ecf20Sopenharmony_ci}; 798c2ecf20Sopenharmony_ci 808c2ecf20Sopenharmony_cistatic int register_tuntap(void) 818c2ecf20Sopenharmony_ci{ 828c2ecf20Sopenharmony_ci register_transport(&tuntap_transport); 838c2ecf20Sopenharmony_ci return 0; 848c2ecf20Sopenharmony_ci} 858c2ecf20Sopenharmony_ci 868c2ecf20Sopenharmony_cilate_initcall(register_tuntap); 87