blob: 0f64d9467286169742ba41df30c4f07d6da1efe0 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * user-mode-linux networking multicast transport
3 * Copyright (C) 2001 by Harald Welte <laforge@gnumonks.org>
4 *
5 * based on the existing uml-networking code, which is
6 * Copyright (C) 2001 Lennert Buytenhek (buytenh@gnu.org) and
7 * James Leu (jleu@mindspring.net).
8 * Copyright (C) 2001 by various other people who didn't put their name here.
9 *
10 * Licensed under the GPL.
11 *
12 */
13
14#include <errno.h>
15#include <unistd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <sys/socket.h>
17#include <sys/un.h>
18#include <sys/time.h>
19#include <netinet/in.h>
20#include "net_user.h"
21#include "mcast.h"
22#include "kern_util.h"
23#include "user_util.h"
24#include "user.h"
25#include "os.h"
Paolo 'Blaisorblade' Giarrussoc13e5692006-10-19 23:28:20 -070026#include "um_malloc.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
28#define MAX_PACKET (ETH_MAX_PACKET + ETH_HEADER_OTHER)
29
30static struct sockaddr_in *new_addr(char *addr, unsigned short port)
31{
32 struct sockaddr_in *sin;
33
34 sin = um_kmalloc(sizeof(struct sockaddr_in));
35 if(sin == NULL){
36 printk("new_addr: allocation of sockaddr_in failed\n");
Jeff Dike56bd1942007-05-06 14:51:02 -070037 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 }
39 sin->sin_family = AF_INET;
40 sin->sin_addr.s_addr = in_aton(addr);
Jeff Dike7c00c312005-05-20 13:59:09 -070041 sin->sin_port = htons(port);
Jeff Dike56bd1942007-05-06 14:51:02 -070042 return sin;
Linus Torvalds1da177e2005-04-16 15:20:36 -070043}
44
Jeff Dikef34d9d22007-05-06 14:51:04 -070045static int mcast_user_init(void *data, void *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070046{
47 struct mcast_data *pri = data;
48
49 pri->mcast_addr = new_addr(pri->addr, pri->port);
50 pri->dev = dev;
Jeff Dikef34d9d22007-05-06 14:51:04 -070051 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052}
53
Paolo 'Blaisorblade' Giarrusso83f4e8a2007-03-07 20:41:09 -080054static void mcast_remove(void *data)
55{
56 struct mcast_data *pri = data;
57
58 kfree(pri->mcast_addr);
59 pri->mcast_addr = NULL;
60}
61
Linus Torvalds1da177e2005-04-16 15:20:36 -070062static int mcast_open(void *data)
63{
64 struct mcast_data *pri = data;
65 struct sockaddr_in *sin = pri->mcast_addr;
66 struct ip_mreq mreq;
Paolo 'Blaisorblade' Giarrussoc50d2c42005-11-13 16:07:07 -080067 int fd, yes = 1, err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
69
Jeff Dike7c00c312005-05-20 13:59:09 -070070 if ((sin->sin_addr.s_addr == 0) || (sin->sin_port == 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
73 fd = socket(AF_INET, SOCK_DGRAM, 0);
Jeff Dike7c00c312005-05-20 13:59:09 -070074
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 if (fd < 0){
Paolo 'Blaisorblade' Giarrussoc50d2c42005-11-13 16:07:07 -080076 err = -errno;
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 printk("mcast_open : data socket failed, errno = %d\n",
78 errno);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 goto out;
80 }
81
82 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) < 0) {
Paolo 'Blaisorblade' Giarrussoc50d2c42005-11-13 16:07:07 -080083 err = -errno;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 printk("mcast_open: SO_REUSEADDR failed, errno = %d\n",
85 errno);
Jeff Dike7c00c312005-05-20 13:59:09 -070086 goto out_close;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 }
88
89 /* set ttl according to config */
90 if (setsockopt(fd, SOL_IP, IP_MULTICAST_TTL, &pri->ttl,
91 sizeof(pri->ttl)) < 0) {
Paolo 'Blaisorblade' Giarrussoc50d2c42005-11-13 16:07:07 -080092 err = -errno;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 printk("mcast_open: IP_MULTICAST_TTL failed, error = %d\n",
94 errno);
Jeff Dike7c00c312005-05-20 13:59:09 -070095 goto out_close;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 }
97
98 /* set LOOP, so data does get fed back to local sockets */
99 if (setsockopt(fd, SOL_IP, IP_MULTICAST_LOOP, &yes, sizeof(yes)) < 0) {
Paolo 'Blaisorblade' Giarrussoc50d2c42005-11-13 16:07:07 -0800100 err = -errno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 printk("mcast_open: IP_MULTICAST_LOOP failed, error = %d\n",
102 errno);
Jeff Dike7c00c312005-05-20 13:59:09 -0700103 goto out_close;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 }
105
106 /* bind socket to mcast address */
107 if (bind(fd, (struct sockaddr *) sin, sizeof(*sin)) < 0) {
Jeff Dikeb4fd3102005-09-16 19:27:49 -0700108 err = -errno;
Paolo 'Blaisorblade' Giarrussoc50d2c42005-11-13 16:07:07 -0800109 printk("mcast_open : data bind failed, errno = %d\n", errno);
Jeff Dike7c00c312005-05-20 13:59:09 -0700110 goto out_close;
Jeff Dike56bd1942007-05-06 14:51:02 -0700111 }
112
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 /* subscribe to the multicast group */
114 mreq.imr_multiaddr.s_addr = sin->sin_addr.s_addr;
115 mreq.imr_interface.s_addr = 0;
116 if (setsockopt(fd, SOL_IP, IP_ADD_MEMBERSHIP,
117 &mreq, sizeof(mreq)) < 0) {
Paolo 'Blaisorblade' Giarrussoc50d2c42005-11-13 16:07:07 -0800118 err = -errno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 printk("mcast_open: IP_ADD_MEMBERSHIP failed, error = %d\n",
120 errno);
121 printk("There appears not to be a multicast-capable network "
122 "interface on the host.\n");
123 printk("eth0 should be configured in order to use the "
124 "multicast transport.\n");
Paolo 'Blaisorblade' Giarrussoc50d2c42005-11-13 16:07:07 -0800125 goto out_close;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 }
127
Jeff Dike7c00c312005-05-20 13:59:09 -0700128 return fd;
129
130 out_close:
Paolo 'Blaisorblade' Giarrussoc50d2c42005-11-13 16:07:07 -0800131 os_close_file(fd);
Jeff Dikeb4fd3102005-09-16 19:27:49 -0700132 out:
Paolo 'Blaisorblade' Giarrussoc50d2c42005-11-13 16:07:07 -0800133 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134}
135
136static void mcast_close(int fd, void *data)
137{
138 struct ip_mreq mreq;
139 struct mcast_data *pri = data;
140 struct sockaddr_in *sin = pri->mcast_addr;
141
142 mreq.imr_multiaddr.s_addr = sin->sin_addr.s_addr;
143 mreq.imr_interface.s_addr = 0;
144 if (setsockopt(fd, SOL_IP, IP_DROP_MEMBERSHIP,
145 &mreq, sizeof(mreq)) < 0) {
146 printk("mcast_open: IP_DROP_MEMBERSHIP failed, error = %d\n",
147 errno);
148 }
149
150 os_close_file(fd);
151}
152
153int mcast_user_write(int fd, void *buf, int len, struct mcast_data *pri)
154{
155 struct sockaddr_in *data_addr = pri->mcast_addr;
156
Jeff Dike56bd1942007-05-06 14:51:02 -0700157 return net_sendto(fd, buf, len, data_addr, sizeof(*data_addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158}
159
160static int mcast_set_mtu(int mtu, void *data)
161{
Jeff Dike56bd1942007-05-06 14:51:02 -0700162 return mtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163}
164
Jeff Dike5e7672e2006-09-27 01:50:33 -0700165const struct net_user_info mcast_user_info = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 .init = mcast_user_init,
167 .open = mcast_open,
168 .close = mcast_close,
Paolo 'Blaisorblade' Giarrusso83f4e8a2007-03-07 20:41:09 -0800169 .remove = mcast_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 .set_mtu = mcast_set_mtu,
171 .add_address = NULL,
172 .delete_address = NULL,
173 .max_packet = MAX_PACKET - ETH_HEADER_OTHER
174};