blob: e427b5322a3650e9f8aa91a28e0bd50ff546ed06 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * user-mode-linux networking multicast transport
Jeff Dikecd1ae0e2007-10-16 01:27:29 -07003 * Copyright (C) 2001 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 * Copyright (C) 2001 by Harald Welte <laforge@gnumonks.org>
5 *
6 * based on the existing uml-networking code, which is
Jeff Dikecd1ae0e2007-10-16 01:27:29 -07007 * Copyright (C) 2001 Lennert Buytenhek (buytenh@gnu.org) and
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * James Leu (jleu@mindspring.net).
9 * Copyright (C) 2001 by various other people who didn't put their name here.
10 *
11 * Licensed under the GPL.
12 *
13 */
14
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <unistd.h>
Jeff Dikecd1ae0e2007-10-16 01:27:29 -070016#include <errno.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <netinet/in.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include "mcast.h"
Jeff Dikecd1ae0e2007-10-16 01:27:29 -070019#include "net_user.h"
Paolo 'Blaisorblade' Giarrussoc13e5692006-10-19 23:28:20 -070020#include "um_malloc.h"
Jeff Dikecd1ae0e2007-10-16 01:27:29 -070021#include "user.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
23#define MAX_PACKET (ETH_MAX_PACKET + ETH_HEADER_OTHER)
24
25static struct sockaddr_in *new_addr(char *addr, unsigned short port)
26{
27 struct sockaddr_in *sin;
28
Jeff Dikee4c4bf992007-07-15 23:38:56 -070029 sin = kmalloc(sizeof(struct sockaddr_in), UM_GFP_KERNEL);
Jeff Dikecd1ae0e2007-10-16 01:27:29 -070030 if (sin == NULL) {
31 printk(UM_KERN_ERR "new_addr: allocation of sockaddr_in "
32 "failed\n");
Jeff Dike56bd1942007-05-06 14:51:02 -070033 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 }
35 sin->sin_family = AF_INET;
36 sin->sin_addr.s_addr = in_aton(addr);
Jeff Dike7c00c312005-05-20 13:59:09 -070037 sin->sin_port = htons(port);
Jeff Dike56bd1942007-05-06 14:51:02 -070038 return sin;
Linus Torvalds1da177e2005-04-16 15:20:36 -070039}
40
Jeff Dikef34d9d22007-05-06 14:51:04 -070041static int mcast_user_init(void *data, void *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070042{
43 struct mcast_data *pri = data;
44
45 pri->mcast_addr = new_addr(pri->addr, pri->port);
46 pri->dev = dev;
Jeff Dikef34d9d22007-05-06 14:51:04 -070047 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048}
49
Paolo 'Blaisorblade' Giarrusso83f4e8a2007-03-07 20:41:09 -080050static void mcast_remove(void *data)
51{
52 struct mcast_data *pri = data;
53
54 kfree(pri->mcast_addr);
55 pri->mcast_addr = NULL;
56}
57
Linus Torvalds1da177e2005-04-16 15:20:36 -070058static int mcast_open(void *data)
59{
60 struct mcast_data *pri = data;
61 struct sockaddr_in *sin = pri->mcast_addr;
62 struct ip_mreq mreq;
Paolo 'Blaisorblade' Giarrussoc50d2c42005-11-13 16:07:07 -080063 int fd, yes = 1, err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
65
Jeff Dike7c00c312005-05-20 13:59:09 -070066 if ((sin->sin_addr.s_addr == 0) || (sin->sin_port == 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
69 fd = socket(AF_INET, SOCK_DGRAM, 0);
Jeff Dike7c00c312005-05-20 13:59:09 -070070
Jeff Dikecd1ae0e2007-10-16 01:27:29 -070071 if (fd < 0) {
Paolo 'Blaisorblade' Giarrussoc50d2c42005-11-13 16:07:07 -080072 err = -errno;
Jeff Dikecd1ae0e2007-10-16 01:27:29 -070073 printk(UM_KERN_ERR "mcast_open : data socket failed, "
74 "errno = %d\n", errno);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 goto out;
76 }
77
78 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) < 0) {
Paolo 'Blaisorblade' Giarrussoc50d2c42005-11-13 16:07:07 -080079 err = -errno;
Jeff Dikecd1ae0e2007-10-16 01:27:29 -070080 printk(UM_KERN_ERR "mcast_open: SO_REUSEADDR failed, "
81 "errno = %d\n", errno);
Jeff Dike7c00c312005-05-20 13:59:09 -070082 goto out_close;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 }
84
85 /* set ttl according to config */
86 if (setsockopt(fd, SOL_IP, IP_MULTICAST_TTL, &pri->ttl,
87 sizeof(pri->ttl)) < 0) {
Paolo 'Blaisorblade' Giarrussoc50d2c42005-11-13 16:07:07 -080088 err = -errno;
Jeff Dikecd1ae0e2007-10-16 01:27:29 -070089 printk(UM_KERN_ERR "mcast_open: IP_MULTICAST_TTL failed, "
90 "error = %d\n", errno);
Jeff Dike7c00c312005-05-20 13:59:09 -070091 goto out_close;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 }
93
94 /* set LOOP, so data does get fed back to local sockets */
95 if (setsockopt(fd, SOL_IP, IP_MULTICAST_LOOP, &yes, sizeof(yes)) < 0) {
Paolo 'Blaisorblade' Giarrussoc50d2c42005-11-13 16:07:07 -080096 err = -errno;
Jeff Dikecd1ae0e2007-10-16 01:27:29 -070097 printk(UM_KERN_ERR "mcast_open: IP_MULTICAST_LOOP failed, "
98 "error = %d\n", errno);
Jeff Dike7c00c312005-05-20 13:59:09 -070099 goto out_close;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 }
101
102 /* bind socket to mcast address */
103 if (bind(fd, (struct sockaddr *) sin, sizeof(*sin)) < 0) {
Jeff Dikeb4fd3102005-09-16 19:27:49 -0700104 err = -errno;
Jeff Dikecd1ae0e2007-10-16 01:27:29 -0700105 printk(UM_KERN_ERR "mcast_open : data bind failed, "
106 "errno = %d\n", errno);
Jeff Dike7c00c312005-05-20 13:59:09 -0700107 goto out_close;
Jeff Dike56bd1942007-05-06 14:51:02 -0700108 }
109
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 /* subscribe to the multicast group */
111 mreq.imr_multiaddr.s_addr = sin->sin_addr.s_addr;
112 mreq.imr_interface.s_addr = 0;
Jeff Dikecd1ae0e2007-10-16 01:27:29 -0700113 if (setsockopt(fd, SOL_IP, IP_ADD_MEMBERSHIP,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 &mreq, sizeof(mreq)) < 0) {
Paolo 'Blaisorblade' Giarrussoc50d2c42005-11-13 16:07:07 -0800115 err = -errno;
Jeff Dikecd1ae0e2007-10-16 01:27:29 -0700116 printk(UM_KERN_ERR "mcast_open: IP_ADD_MEMBERSHIP failed, "
117 "error = %d\n", errno);
118 printk(UM_KERN_ERR "There appears not to be a multicast-"
119 "capable network interface on the host.\n");
120 printk(UM_KERN_ERR "eth0 should be configured in order to use "
121 "the multicast transport.\n");
Paolo 'Blaisorblade' Giarrussoc50d2c42005-11-13 16:07:07 -0800122 goto out_close;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 }
124
Jeff Dike7c00c312005-05-20 13:59:09 -0700125 return fd;
126
127 out_close:
Jeff Dikecd1ae0e2007-10-16 01:27:29 -0700128 close(fd);
Jeff Dikeb4fd3102005-09-16 19:27:49 -0700129 out:
Paolo 'Blaisorblade' Giarrussoc50d2c42005-11-13 16:07:07 -0800130 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131}
132
133static void mcast_close(int fd, void *data)
134{
135 struct ip_mreq mreq;
136 struct mcast_data *pri = data;
137 struct sockaddr_in *sin = pri->mcast_addr;
138
139 mreq.imr_multiaddr.s_addr = sin->sin_addr.s_addr;
140 mreq.imr_interface.s_addr = 0;
141 if (setsockopt(fd, SOL_IP, IP_DROP_MEMBERSHIP,
142 &mreq, sizeof(mreq)) < 0) {
Jeff Dikecd1ae0e2007-10-16 01:27:29 -0700143 printk(UM_KERN_ERR "mcast_open: IP_DROP_MEMBERSHIP failed, "
144 "error = %d\n", errno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 }
146
Jeff Dikecd1ae0e2007-10-16 01:27:29 -0700147 close(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148}
149
150int mcast_user_write(int fd, void *buf, int len, struct mcast_data *pri)
151{
152 struct sockaddr_in *data_addr = pri->mcast_addr;
153
Jeff Dike56bd1942007-05-06 14:51:02 -0700154 return net_sendto(fd, buf, len, data_addr, sizeof(*data_addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155}
156
157static int mcast_set_mtu(int mtu, void *data)
158{
Jeff Dike56bd1942007-05-06 14:51:02 -0700159 return mtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160}
161
Jeff Dike5e7672e2006-09-27 01:50:33 -0700162const struct net_user_info mcast_user_info = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 .init = mcast_user_init,
164 .open = mcast_open,
165 .close = mcast_close,
Paolo 'Blaisorblade' Giarrusso83f4e8a2007-03-07 20:41:09 -0800166 .remove = mcast_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 .set_mtu = mcast_set_mtu,
168 .add_address = NULL,
169 .delete_address = NULL,
170 .max_packet = MAX_PACKET - ETH_HEADER_OTHER
171};