blob: ee19e91568a2585765127970fc9d1ba821bcc613 [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>
Jeff Dike43f5b302008-05-12 14:01:52 -070018#include "kern_constants.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include "mcast.h"
Jeff Dikecd1ae0e2007-10-16 01:27:29 -070020#include "net_user.h"
Paolo 'Blaisorblade' Giarrussoc13e5692006-10-19 23:28:20 -070021#include "um_malloc.h"
Jeff Dikecd1ae0e2007-10-16 01:27:29 -070022#include "user.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
Linus Torvalds1da177e2005-04-16 15:20:36 -070024static struct sockaddr_in *new_addr(char *addr, unsigned short port)
25{
26 struct sockaddr_in *sin;
27
Jeff Dike43f5b302008-05-12 14:01:52 -070028 sin = uml_kmalloc(sizeof(struct sockaddr_in), UM_GFP_KERNEL);
Jeff Dikecd1ae0e2007-10-16 01:27:29 -070029 if (sin == NULL) {
30 printk(UM_KERN_ERR "new_addr: allocation of sockaddr_in "
31 "failed\n");
Jeff Dike56bd1942007-05-06 14:51:02 -070032 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 }
34 sin->sin_family = AF_INET;
35 sin->sin_addr.s_addr = in_aton(addr);
Jeff Dike7c00c312005-05-20 13:59:09 -070036 sin->sin_port = htons(port);
Jeff Dike56bd1942007-05-06 14:51:02 -070037 return sin;
Linus Torvalds1da177e2005-04-16 15:20:36 -070038}
39
Jeff Dikef34d9d22007-05-06 14:51:04 -070040static int mcast_user_init(void *data, void *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070041{
42 struct mcast_data *pri = data;
43
44 pri->mcast_addr = new_addr(pri->addr, pri->port);
45 pri->dev = dev;
Jeff Dikef34d9d22007-05-06 14:51:04 -070046 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047}
48
Paolo 'Blaisorblade' Giarrusso83f4e8a2007-03-07 20:41:09 -080049static void mcast_remove(void *data)
50{
51 struct mcast_data *pri = data;
52
53 kfree(pri->mcast_addr);
54 pri->mcast_addr = NULL;
55}
56
Linus Torvalds1da177e2005-04-16 15:20:36 -070057static int mcast_open(void *data)
58{
59 struct mcast_data *pri = data;
60 struct sockaddr_in *sin = pri->mcast_addr;
61 struct ip_mreq mreq;
Paolo 'Blaisorblade' Giarrussoc50d2c42005-11-13 16:07:07 -080062 int fd, yes = 1, err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
64
Jeff Dike7c00c312005-05-20 13:59:09 -070065 if ((sin->sin_addr.s_addr == 0) || (sin->sin_port == 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
68 fd = socket(AF_INET, SOCK_DGRAM, 0);
Jeff Dike7c00c312005-05-20 13:59:09 -070069
Jeff Dikecd1ae0e2007-10-16 01:27:29 -070070 if (fd < 0) {
Paolo 'Blaisorblade' Giarrussoc50d2c42005-11-13 16:07:07 -080071 err = -errno;
Jeff Dikecd1ae0e2007-10-16 01:27:29 -070072 printk(UM_KERN_ERR "mcast_open : data socket failed, "
73 "errno = %d\n", errno);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 goto out;
75 }
76
77 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) < 0) {
Paolo 'Blaisorblade' Giarrussoc50d2c42005-11-13 16:07:07 -080078 err = -errno;
Jeff Dikecd1ae0e2007-10-16 01:27:29 -070079 printk(UM_KERN_ERR "mcast_open: SO_REUSEADDR failed, "
80 "errno = %d\n", errno);
Jeff Dike7c00c312005-05-20 13:59:09 -070081 goto out_close;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 }
83
84 /* set ttl according to config */
85 if (setsockopt(fd, SOL_IP, IP_MULTICAST_TTL, &pri->ttl,
86 sizeof(pri->ttl)) < 0) {
Paolo 'Blaisorblade' Giarrussoc50d2c42005-11-13 16:07:07 -080087 err = -errno;
Jeff Dikecd1ae0e2007-10-16 01:27:29 -070088 printk(UM_KERN_ERR "mcast_open: IP_MULTICAST_TTL failed, "
89 "error = %d\n", errno);
Jeff Dike7c00c312005-05-20 13:59:09 -070090 goto out_close;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 }
92
93 /* set LOOP, so data does get fed back to local sockets */
94 if (setsockopt(fd, SOL_IP, IP_MULTICAST_LOOP, &yes, sizeof(yes)) < 0) {
Paolo 'Blaisorblade' Giarrussoc50d2c42005-11-13 16:07:07 -080095 err = -errno;
Jeff Dikecd1ae0e2007-10-16 01:27:29 -070096 printk(UM_KERN_ERR "mcast_open: IP_MULTICAST_LOOP failed, "
97 "error = %d\n", errno);
Jeff Dike7c00c312005-05-20 13:59:09 -070098 goto out_close;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 }
100
101 /* bind socket to mcast address */
102 if (bind(fd, (struct sockaddr *) sin, sizeof(*sin)) < 0) {
Jeff Dikeb4fd3102005-09-16 19:27:49 -0700103 err = -errno;
Jeff Dikecd1ae0e2007-10-16 01:27:29 -0700104 printk(UM_KERN_ERR "mcast_open : data bind failed, "
105 "errno = %d\n", errno);
Jeff Dike7c00c312005-05-20 13:59:09 -0700106 goto out_close;
Jeff Dike56bd1942007-05-06 14:51:02 -0700107 }
108
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 /* subscribe to the multicast group */
110 mreq.imr_multiaddr.s_addr = sin->sin_addr.s_addr;
111 mreq.imr_interface.s_addr = 0;
Jeff Dikecd1ae0e2007-10-16 01:27:29 -0700112 if (setsockopt(fd, SOL_IP, IP_ADD_MEMBERSHIP,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 &mreq, sizeof(mreq)) < 0) {
Paolo 'Blaisorblade' Giarrussoc50d2c42005-11-13 16:07:07 -0800114 err = -errno;
Jeff Dikecd1ae0e2007-10-16 01:27:29 -0700115 printk(UM_KERN_ERR "mcast_open: IP_ADD_MEMBERSHIP failed, "
116 "error = %d\n", errno);
117 printk(UM_KERN_ERR "There appears not to be a multicast-"
118 "capable network interface on the host.\n");
119 printk(UM_KERN_ERR "eth0 should be configured in order to use "
120 "the multicast transport.\n");
Paolo 'Blaisorblade' Giarrussoc50d2c42005-11-13 16:07:07 -0800121 goto out_close;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 }
123
Jeff Dike7c00c312005-05-20 13:59:09 -0700124 return fd;
125
126 out_close:
Jeff Dikecd1ae0e2007-10-16 01:27:29 -0700127 close(fd);
Jeff Dikeb4fd3102005-09-16 19:27:49 -0700128 out:
Paolo 'Blaisorblade' Giarrussoc50d2c42005-11-13 16:07:07 -0800129 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130}
131
132static void mcast_close(int fd, void *data)
133{
134 struct ip_mreq mreq;
135 struct mcast_data *pri = data;
136 struct sockaddr_in *sin = pri->mcast_addr;
137
138 mreq.imr_multiaddr.s_addr = sin->sin_addr.s_addr;
139 mreq.imr_interface.s_addr = 0;
140 if (setsockopt(fd, SOL_IP, IP_DROP_MEMBERSHIP,
141 &mreq, sizeof(mreq)) < 0) {
Jeff Dikecd1ae0e2007-10-16 01:27:29 -0700142 printk(UM_KERN_ERR "mcast_open: IP_DROP_MEMBERSHIP failed, "
143 "error = %d\n", errno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 }
145
Jeff Dikecd1ae0e2007-10-16 01:27:29 -0700146 close(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147}
148
149int mcast_user_write(int fd, void *buf, int len, struct mcast_data *pri)
150{
151 struct sockaddr_in *data_addr = pri->mcast_addr;
152
Jeff Dike56bd1942007-05-06 14:51:02 -0700153 return net_sendto(fd, buf, len, data_addr, sizeof(*data_addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154}
155
Jeff Dike5e7672e2006-09-27 01:50:33 -0700156const struct net_user_info mcast_user_info = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 .init = mcast_user_init,
158 .open = mcast_open,
159 .close = mcast_close,
Paolo 'Blaisorblade' Giarrusso83f4e8a2007-03-07 20:41:09 -0800160 .remove = mcast_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 .add_address = NULL,
162 .delete_address = NULL,
Jeff Dikeb53f35a2007-10-16 01:27:31 -0700163 .mtu = ETH_MAX_PACKET,
164 .max_packet = ETH_MAX_PACKET + ETH_HEADER_OTHER,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165};