mcast_user.c 4.1 KB
Newer Older
L
Linus Torvalds 已提交
1 2
/*
 * user-mode-linux networking multicast transport
J
Jeff Dike 已提交
3
 * Copyright (C) 2001 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
L
Linus Torvalds 已提交
4 5 6
 * Copyright (C) 2001 by Harald Welte <laforge@gnumonks.org>
 *
 * based on the existing uml-networking code, which is
J
Jeff Dike 已提交
7
 * Copyright (C) 2001 Lennert Buytenhek (buytenh@gnu.org) and
L
Linus Torvalds 已提交
8 9 10 11 12 13 14 15
 * James Leu (jleu@mindspring.net).
 * Copyright (C) 2001 by various other people who didn't put their name here.
 *
 * Licensed under the GPL.
 *
 */

#include <unistd.h>
J
Jeff Dike 已提交
16
#include <errno.h>
L
Linus Torvalds 已提交
17 18
#include <netinet/in.h>
#include "mcast.h"
J
Jeff Dike 已提交
19
#include "net_user.h"
20
#include "um_malloc.h"
J
Jeff Dike 已提交
21
#include "user.h"
L
Linus Torvalds 已提交
22 23 24 25 26

static struct sockaddr_in *new_addr(char *addr, unsigned short port)
{
	struct sockaddr_in *sin;

27
	sin = kmalloc(sizeof(struct sockaddr_in), UM_GFP_KERNEL);
J
Jeff Dike 已提交
28 29 30
	if (sin == NULL) {
		printk(UM_KERN_ERR "new_addr: allocation of sockaddr_in "
		       "failed\n");
J
Jeff Dike 已提交
31
		return NULL;
L
Linus Torvalds 已提交
32 33 34
	}
	sin->sin_family = AF_INET;
	sin->sin_addr.s_addr = in_aton(addr);
35
	sin->sin_port = htons(port);
J
Jeff Dike 已提交
36
	return sin;
L
Linus Torvalds 已提交
37 38
}

39
static int mcast_user_init(void *data, void *dev)
L
Linus Torvalds 已提交
40 41 42 43 44
{
	struct mcast_data *pri = data;

	pri->mcast_addr = new_addr(pri->addr, pri->port);
	pri->dev = dev;
45
	return 0;
L
Linus Torvalds 已提交
46 47
}

48 49 50 51 52 53 54 55
static void mcast_remove(void *data)
{
	struct mcast_data *pri = data;

	kfree(pri->mcast_addr);
	pri->mcast_addr = NULL;
}

L
Linus Torvalds 已提交
56 57 58 59 60
static int mcast_open(void *data)
{
	struct mcast_data *pri = data;
	struct sockaddr_in *sin = pri->mcast_addr;
	struct ip_mreq mreq;
61
	int fd, yes = 1, err = -EINVAL;
L
Linus Torvalds 已提交
62 63


64
	if ((sin->sin_addr.s_addr == 0) || (sin->sin_port == 0))
L
Linus Torvalds 已提交
65 66 67
		goto out;

	fd = socket(AF_INET, SOCK_DGRAM, 0);
68

J
Jeff Dike 已提交
69
	if (fd < 0) {
70
		err = -errno;
J
Jeff Dike 已提交
71 72
		printk(UM_KERN_ERR "mcast_open : data socket failed, "
		       "errno = %d\n", errno);
L
Linus Torvalds 已提交
73 74 75 76
		goto out;
	}

	if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) < 0) {
77
		err = -errno;
J
Jeff Dike 已提交
78 79
		printk(UM_KERN_ERR "mcast_open: SO_REUSEADDR failed, "
		       "errno = %d\n", errno);
80
		goto out_close;
L
Linus Torvalds 已提交
81 82 83 84 85
	}

	/* set ttl according to config */
	if (setsockopt(fd, SOL_IP, IP_MULTICAST_TTL, &pri->ttl,
		       sizeof(pri->ttl)) < 0) {
86
		err = -errno;
J
Jeff Dike 已提交
87 88
		printk(UM_KERN_ERR "mcast_open: IP_MULTICAST_TTL failed, "
		       "error = %d\n", errno);
89
		goto out_close;
L
Linus Torvalds 已提交
90 91 92 93
	}

	/* set LOOP, so data does get fed back to local sockets */
	if (setsockopt(fd, SOL_IP, IP_MULTICAST_LOOP, &yes, sizeof(yes)) < 0) {
94
		err = -errno;
J
Jeff Dike 已提交
95 96
		printk(UM_KERN_ERR "mcast_open: IP_MULTICAST_LOOP failed, "
		       "error = %d\n", errno);
97
		goto out_close;
L
Linus Torvalds 已提交
98 99 100 101
	}

	/* bind socket to mcast address */
	if (bind(fd, (struct sockaddr *) sin, sizeof(*sin)) < 0) {
102
		err = -errno;
J
Jeff Dike 已提交
103 104
		printk(UM_KERN_ERR "mcast_open : data bind failed, "
		       "errno = %d\n", errno);
105
		goto out_close;
J
Jeff Dike 已提交
106 107
	}

L
Linus Torvalds 已提交
108 109 110
	/* subscribe to the multicast group */
	mreq.imr_multiaddr.s_addr = sin->sin_addr.s_addr;
	mreq.imr_interface.s_addr = 0;
J
Jeff Dike 已提交
111
	if (setsockopt(fd, SOL_IP, IP_ADD_MEMBERSHIP,
L
Linus Torvalds 已提交
112
		       &mreq, sizeof(mreq)) < 0) {
113
		err = -errno;
J
Jeff Dike 已提交
114 115 116 117 118 119
		printk(UM_KERN_ERR "mcast_open: IP_ADD_MEMBERSHIP failed, "
		       "error = %d\n", errno);
		printk(UM_KERN_ERR "There appears not to be a multicast-"
		       "capable network interface on the host.\n");
		printk(UM_KERN_ERR "eth0 should be configured in order to use "
		       "the multicast transport.\n");
120
		goto out_close;
L
Linus Torvalds 已提交
121 122
	}

123 124 125
	return fd;

 out_close:
J
Jeff Dike 已提交
126
	close(fd);
127
 out:
128
	return err;
L
Linus Torvalds 已提交
129 130 131 132 133 134 135 136 137 138 139 140
}

static void mcast_close(int fd, void *data)
{
	struct ip_mreq mreq;
	struct mcast_data *pri = data;
	struct sockaddr_in *sin = pri->mcast_addr;

	mreq.imr_multiaddr.s_addr = sin->sin_addr.s_addr;
	mreq.imr_interface.s_addr = 0;
	if (setsockopt(fd, SOL_IP, IP_DROP_MEMBERSHIP,
		       &mreq, sizeof(mreq)) < 0) {
J
Jeff Dike 已提交
141 142
		printk(UM_KERN_ERR "mcast_open: IP_DROP_MEMBERSHIP failed, "
		       "error = %d\n", errno);
L
Linus Torvalds 已提交
143 144
	}

J
Jeff Dike 已提交
145
	close(fd);
L
Linus Torvalds 已提交
146 147 148 149 150 151
}

int mcast_user_write(int fd, void *buf, int len, struct mcast_data *pri)
{
	struct sockaddr_in *data_addr = pri->mcast_addr;

J
Jeff Dike 已提交
152
	return net_sendto(fd, buf, len, data_addr, sizeof(*data_addr));
L
Linus Torvalds 已提交
153 154
}

J
Jeff Dike 已提交
155
const struct net_user_info mcast_user_info = {
L
Linus Torvalds 已提交
156 157 158
	.init		= mcast_user_init,
	.open		= mcast_open,
	.close	 	= mcast_close,
159
	.remove	 	= mcast_remove,
L
Linus Torvalds 已提交
160 161
	.add_address	= NULL,
	.delete_address = NULL,
J
Jeff Dike 已提交
162 163
	.mtu		= ETH_MAX_PACKET,
	.max_packet	= ETH_MAX_PACKET + ETH_HEADER_OTHER,
L
Linus Torvalds 已提交
164
};