tun.c (43a08e0f58b3f236165029710a4e3b303815253b) tun.c (1ec010e705934c8acbe7dbf31afc81e60e3d828b)
1/*
2 * TUN - Universal TUN/TAP device driver.
3 * Copyright (C) 1999-2002 Maxim Krasnyansky <maxk@qualcomm.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.

--- 64 unchanged lines hidden (view full) ---

73#include <linux/seq_file.h>
74#include <linux/uio.h>
75#include <linux/skb_array.h>
76#include <linux/bpf.h>
77#include <linux/bpf_trace.h>
78#include <linux/mutex.h>
79
80#include <linux/uaccess.h>
1/*
2 * TUN - Universal TUN/TAP device driver.
3 * Copyright (C) 1999-2002 Maxim Krasnyansky <maxk@qualcomm.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.

--- 64 unchanged lines hidden (view full) ---

73#include <linux/seq_file.h>
74#include <linux/uio.h>
75#include <linux/skb_array.h>
76#include <linux/bpf.h>
77#include <linux/bpf_trace.h>
78#include <linux/mutex.h>
79
80#include <linux/uaccess.h>
81#include <linux/proc_fs.h>
81
82/* Uncomment to enable debugging */
83/* #define TUN_DEBUG 1 */
84
85#ifdef TUN_DEBUG
86static int debug;
87
88#define tun_debug(level, tun, fmt, args...) \

--- 1395 unchanged lines hidden (view full) ---

1484 if (err)
1485 goto free;
1486
1487 skb->len = len;
1488 skb->data_len = len - linear;
1489 skb->truesize += skb->data_len;
1490
1491 for (i = 1; i < it->nr_segs; i++) {
82
83/* Uncomment to enable debugging */
84/* #define TUN_DEBUG 1 */
85
86#ifdef TUN_DEBUG
87static int debug;
88
89#define tun_debug(level, tun, fmt, args...) \

--- 1395 unchanged lines hidden (view full) ---

1485 if (err)
1486 goto free;
1487
1488 skb->len = len;
1489 skb->data_len = len - linear;
1490 skb->truesize += skb->data_len;
1491
1492 for (i = 1; i < it->nr_segs; i++) {
1492 struct page_frag *pfrag = &current->task_frag;
1493 size_t fragsz = it->iov[i].iov_len;
1493 size_t fragsz = it->iov[i].iov_len;
1494 unsigned long offset;
1495 struct page *page;
1496 void *data;
1494
1495 if (fragsz == 0 || fragsz > PAGE_SIZE) {
1496 err = -EINVAL;
1497 goto free;
1498 }
1499
1497
1498 if (fragsz == 0 || fragsz > PAGE_SIZE) {
1499 err = -EINVAL;
1500 goto free;
1501 }
1502
1500 if (!skb_page_frag_refill(fragsz, pfrag, GFP_KERNEL)) {
1503 local_bh_disable();
1504 data = napi_alloc_frag(fragsz);
1505 local_bh_enable();
1506 if (!data) {
1501 err = -ENOMEM;
1502 goto free;
1503 }
1504
1507 err = -ENOMEM;
1508 goto free;
1509 }
1510
1505 skb_fill_page_desc(skb, i - 1, pfrag->page,
1506 pfrag->offset, fragsz);
1507 page_ref_inc(pfrag->page);
1508 pfrag->offset += fragsz;
1511 page = virt_to_head_page(data);
1512 offset = data - page_address(page);
1513 skb_fill_page_desc(skb, i - 1, page, offset, fragsz);
1509 }
1510
1511 return skb;
1512free:
1513 /* frees skb and all frags allocated with napi_alloc_frag() */
1514 napi_free_frags(&tfile->napi);
1515 return ERR_PTR(err);
1516}

--- 764 unchanged lines hidden (view full) ---

2281 * device with netlink.
2282 */
2283static int tun_validate(struct nlattr *tb[], struct nlattr *data[],
2284 struct netlink_ext_ack *extack)
2285{
2286 return -EINVAL;
2287}
2288
1514 }
1515
1516 return skb;
1517free:
1518 /* frees skb and all frags allocated with napi_alloc_frag() */
1519 napi_free_frags(&tfile->napi);
1520 return ERR_PTR(err);
1521}

--- 764 unchanged lines hidden (view full) ---

2286 * device with netlink.
2287 */
2288static int tun_validate(struct nlattr *tb[], struct nlattr *data[],
2289 struct netlink_ext_ack *extack)
2290{
2291 return -EINVAL;
2292}
2293
2294static size_t tun_get_size(const struct net_device *dev)
2295{
2296 BUILD_BUG_ON(sizeof(u32) != sizeof(uid_t));
2297 BUILD_BUG_ON(sizeof(u32) != sizeof(gid_t));
2298
2299 return nla_total_size(sizeof(uid_t)) + /* OWNER */
2300 nla_total_size(sizeof(gid_t)) + /* GROUP */
2301 nla_total_size(sizeof(u8)) + /* TYPE */
2302 nla_total_size(sizeof(u8)) + /* PI */
2303 nla_total_size(sizeof(u8)) + /* VNET_HDR */
2304 nla_total_size(sizeof(u8)) + /* PERSIST */
2305 nla_total_size(sizeof(u8)) + /* MULTI_QUEUE */
2306 nla_total_size(sizeof(u32)) + /* NUM_QUEUES */
2307 nla_total_size(sizeof(u32)) + /* NUM_DISABLED_QUEUES */
2308 0;
2309}
2310
2311static int tun_fill_info(struct sk_buff *skb, const struct net_device *dev)
2312{
2313 struct tun_struct *tun = netdev_priv(dev);
2314
2315 if (nla_put_u8(skb, IFLA_TUN_TYPE, tun->flags & TUN_TYPE_MASK))
2316 goto nla_put_failure;
2317 if (uid_valid(tun->owner) &&
2318 nla_put_u32(skb, IFLA_TUN_OWNER,
2319 from_kuid_munged(current_user_ns(), tun->owner)))
2320 goto nla_put_failure;
2321 if (gid_valid(tun->group) &&
2322 nla_put_u32(skb, IFLA_TUN_GROUP,
2323 from_kgid_munged(current_user_ns(), tun->group)))
2324 goto nla_put_failure;
2325 if (nla_put_u8(skb, IFLA_TUN_PI, !(tun->flags & IFF_NO_PI)))
2326 goto nla_put_failure;
2327 if (nla_put_u8(skb, IFLA_TUN_VNET_HDR, !!(tun->flags & IFF_VNET_HDR)))
2328 goto nla_put_failure;
2329 if (nla_put_u8(skb, IFLA_TUN_PERSIST, !!(tun->flags & IFF_PERSIST)))
2330 goto nla_put_failure;
2331 if (nla_put_u8(skb, IFLA_TUN_MULTI_QUEUE,
2332 !!(tun->flags & IFF_MULTI_QUEUE)))
2333 goto nla_put_failure;
2334 if (tun->flags & IFF_MULTI_QUEUE) {
2335 if (nla_put_u32(skb, IFLA_TUN_NUM_QUEUES, tun->numqueues))
2336 goto nla_put_failure;
2337 if (nla_put_u32(skb, IFLA_TUN_NUM_DISABLED_QUEUES,
2338 tun->numdisabled))
2339 goto nla_put_failure;
2340 }
2341
2342 return 0;
2343
2344nla_put_failure:
2345 return -EMSGSIZE;
2346}
2347
2289static struct rtnl_link_ops tun_link_ops __read_mostly = {
2290 .kind = DRV_NAME,
2291 .priv_size = sizeof(struct tun_struct),
2292 .setup = tun_setup,
2293 .validate = tun_validate,
2348static struct rtnl_link_ops tun_link_ops __read_mostly = {
2349 .kind = DRV_NAME,
2350 .priv_size = sizeof(struct tun_struct),
2351 .setup = tun_setup,
2352 .validate = tun_validate,
2353 .get_size = tun_get_size,
2354 .fill_info = tun_fill_info,
2294};
2295
2296static void tun_sock_write_space(struct sock *sk)
2297{
2298 struct tun_file *tfile;
2299 wait_queue_head_t *wqueue;
2300
2301 if (!sock_writeable(sk))

--- 482 unchanged lines hidden (view full) ---

2784
2785static long __tun_chr_ioctl(struct file *file, unsigned int cmd,
2786 unsigned long arg, int ifreq_len)
2787{
2788 struct tun_file *tfile = file->private_data;
2789 struct tun_struct *tun;
2790 void __user* argp = (void __user*)arg;
2791 struct ifreq ifr;
2355};
2356
2357static void tun_sock_write_space(struct sock *sk)
2358{
2359 struct tun_file *tfile;
2360 wait_queue_head_t *wqueue;
2361
2362 if (!sock_writeable(sk))

--- 482 unchanged lines hidden (view full) ---

2845
2846static long __tun_chr_ioctl(struct file *file, unsigned int cmd,
2847 unsigned long arg, int ifreq_len)
2848{
2849 struct tun_file *tfile = file->private_data;
2850 struct tun_struct *tun;
2851 void __user* argp = (void __user*)arg;
2852 struct ifreq ifr;
2853 struct net *net;
2792 kuid_t owner;
2793 kgid_t group;
2794 int sndbuf;
2795 int vnet_hdr_sz;
2796 unsigned int ifindex;
2797 int le;
2798 int ret;
2799
2854 kuid_t owner;
2855 kgid_t group;
2856 int sndbuf;
2857 int vnet_hdr_sz;
2858 unsigned int ifindex;
2859 int le;
2860 int ret;
2861
2800 if (cmd == TUNSETIFF || cmd == TUNSETQUEUE || _IOC_TYPE(cmd) == SOCK_IOC_TYPE) {
2862 if (cmd == TUNSETIFF || cmd == TUNSETQUEUE ||
2863 (_IOC_TYPE(cmd) == SOCK_IOC_TYPE && cmd != SIOCGSKNS)) {
2801 if (copy_from_user(&ifr, argp, ifreq_len))
2802 return -EFAULT;
2803 } else {
2804 memset(&ifr, 0, sizeof(ifr));
2805 }
2806 if (cmd == TUNGETFEATURES) {
2807 /* Currently this just means: "what IFF flags are valid?".
2808 * This is needed because we never checked for invalid flags on
2809 * TUNSETIFF.
2810 */
2811 return put_user(IFF_TUN | IFF_TAP | TUN_FEATURES,
2812 (unsigned int __user*)argp);
2813 } else if (cmd == TUNSETQUEUE)
2814 return tun_set_queue(file, &ifr);
2815
2816 ret = 0;
2817 rtnl_lock();
2818
2819 tun = tun_get(tfile);
2864 if (copy_from_user(&ifr, argp, ifreq_len))
2865 return -EFAULT;
2866 } else {
2867 memset(&ifr, 0, sizeof(ifr));
2868 }
2869 if (cmd == TUNGETFEATURES) {
2870 /* Currently this just means: "what IFF flags are valid?".
2871 * This is needed because we never checked for invalid flags on
2872 * TUNSETIFF.
2873 */
2874 return put_user(IFF_TUN | IFF_TAP | TUN_FEATURES,
2875 (unsigned int __user*)argp);
2876 } else if (cmd == TUNSETQUEUE)
2877 return tun_set_queue(file, &ifr);
2878
2879 ret = 0;
2880 rtnl_lock();
2881
2882 tun = tun_get(tfile);
2883 net = sock_net(&tfile->sk);
2820 if (cmd == TUNSETIFF) {
2821 ret = -EEXIST;
2822 if (tun)
2823 goto unlock;
2824
2825 ifr.ifr_name[IFNAMSIZ-1] = '\0';
2826
2884 if (cmd == TUNSETIFF) {
2885 ret = -EEXIST;
2886 if (tun)
2887 goto unlock;
2888
2889 ifr.ifr_name[IFNAMSIZ-1] = '\0';
2890
2827 ret = tun_set_iff(sock_net(&tfile->sk), file, &ifr);
2891 ret = tun_set_iff(net, file, &ifr);
2828
2829 if (ret)
2830 goto unlock;
2831
2832 if (copy_to_user(argp, &ifr, ifreq_len))
2833 ret = -EFAULT;
2834 goto unlock;
2835 }

--- 5 unchanged lines hidden (view full) ---

2841 ret = -EFAULT;
2842 if (copy_from_user(&ifindex, argp, sizeof(ifindex)))
2843 goto unlock;
2844
2845 ret = 0;
2846 tfile->ifindex = ifindex;
2847 goto unlock;
2848 }
2892
2893 if (ret)
2894 goto unlock;
2895
2896 if (copy_to_user(argp, &ifr, ifreq_len))
2897 ret = -EFAULT;
2898 goto unlock;
2899 }

--- 5 unchanged lines hidden (view full) ---

2905 ret = -EFAULT;
2906 if (copy_from_user(&ifindex, argp, sizeof(ifindex)))
2907 goto unlock;
2908
2909 ret = 0;
2910 tfile->ifindex = ifindex;
2911 goto unlock;
2912 }
2913 if (cmd == SIOCGSKNS) {
2914 ret = -EPERM;
2915 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
2916 goto unlock;
2849
2917
2918 ret = open_related_ns(&net->ns, get_net_ns);
2919 goto unlock;
2920 }
2921
2850 ret = -EBADFD;
2851 if (!tun)
2852 goto unlock;
2853
2854 tun_debug(KERN_INFO, tun, "tun_chr_ioctl cmd %u\n", cmd);
2855
2856 ret = 0;
2857 switch (cmd) {

--- 582 unchanged lines hidden ---
2922 ret = -EBADFD;
2923 if (!tun)
2924 goto unlock;
2925
2926 tun_debug(KERN_INFO, tun, "tun_chr_ioctl cmd %u\n", cmd);
2927
2928 ret = 0;
2929 switch (cmd) {

--- 582 unchanged lines hidden ---