Merge ../linus
[linux-2.6] / arch / um / drivers / mcast_user.c
1 /*
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>
16 #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"
26 #include "um_malloc.h"
27
28 #define MAX_PACKET (ETH_MAX_PACKET + ETH_HEADER_OTHER)
29
30 static 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");
37                 return(NULL);
38         }
39         sin->sin_family = AF_INET;
40         sin->sin_addr.s_addr = in_aton(addr);
41         sin->sin_port = htons(port);
42         return(sin);
43 }
44
45 static void mcast_user_init(void *data, void *dev)
46 {
47         struct mcast_data *pri = data;
48
49         pri->mcast_addr = new_addr(pri->addr, pri->port);
50         pri->dev = dev;
51 }
52
53 static int mcast_open(void *data)
54 {
55         struct mcast_data *pri = data;
56         struct sockaddr_in *sin = pri->mcast_addr;
57         struct ip_mreq mreq;
58         int fd, yes = 1, err = -EINVAL;
59
60
61         if ((sin->sin_addr.s_addr == 0) || (sin->sin_port == 0))
62                 goto out;
63
64         fd = socket(AF_INET, SOCK_DGRAM, 0);
65
66         if (fd < 0){
67                 err = -errno;
68                 printk("mcast_open : data socket failed, errno = %d\n", 
69                        errno);
70                 goto out;
71         }
72
73         if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) < 0) {
74                 err = -errno;
75                 printk("mcast_open: SO_REUSEADDR failed, errno = %d\n",
76                         errno);
77                 goto out_close;
78         }
79
80         /* set ttl according to config */
81         if (setsockopt(fd, SOL_IP, IP_MULTICAST_TTL, &pri->ttl,
82                        sizeof(pri->ttl)) < 0) {
83                 err = -errno;
84                 printk("mcast_open: IP_MULTICAST_TTL failed, error = %d\n",
85                         errno);
86                 goto out_close;
87         }
88
89         /* set LOOP, so data does get fed back to local sockets */
90         if (setsockopt(fd, SOL_IP, IP_MULTICAST_LOOP, &yes, sizeof(yes)) < 0) {
91                 err = -errno;
92                 printk("mcast_open: IP_MULTICAST_LOOP failed, error = %d\n",
93                         errno);
94                 goto out_close;
95         }
96
97         /* bind socket to mcast address */
98         if (bind(fd, (struct sockaddr *) sin, sizeof(*sin)) < 0) {
99                 err = -errno;
100                 printk("mcast_open : data bind failed, errno = %d\n", errno);
101                 goto out_close;
102         }               
103         
104         /* subscribe to the multicast group */
105         mreq.imr_multiaddr.s_addr = sin->sin_addr.s_addr;
106         mreq.imr_interface.s_addr = 0;
107         if (setsockopt(fd, SOL_IP, IP_ADD_MEMBERSHIP, 
108                        &mreq, sizeof(mreq)) < 0) {
109                 err = -errno;
110                 printk("mcast_open: IP_ADD_MEMBERSHIP failed, error = %d\n",
111                         errno);
112                 printk("There appears not to be a multicast-capable network "
113                        "interface on the host.\n");
114                 printk("eth0 should be configured in order to use the "
115                        "multicast transport.\n");
116                 goto out_close;
117         }
118
119         return fd;
120
121  out_close:
122         os_close_file(fd);
123  out:
124         return err;
125 }
126
127 static void mcast_close(int fd, void *data)
128 {
129         struct ip_mreq mreq;
130         struct mcast_data *pri = data;
131         struct sockaddr_in *sin = pri->mcast_addr;
132
133         mreq.imr_multiaddr.s_addr = sin->sin_addr.s_addr;
134         mreq.imr_interface.s_addr = 0;
135         if (setsockopt(fd, SOL_IP, IP_DROP_MEMBERSHIP,
136                        &mreq, sizeof(mreq)) < 0) {
137                 printk("mcast_open: IP_DROP_MEMBERSHIP failed, error = %d\n",
138                         errno);
139         }
140
141         os_close_file(fd);
142 }
143
144 int mcast_user_write(int fd, void *buf, int len, struct mcast_data *pri)
145 {
146         struct sockaddr_in *data_addr = pri->mcast_addr;
147
148         return(net_sendto(fd, buf, len, data_addr, sizeof(*data_addr)));
149 }
150
151 static int mcast_set_mtu(int mtu, void *data)
152 {
153         return(mtu);
154 }
155
156 const struct net_user_info mcast_user_info = {
157         .init           = mcast_user_init,
158         .open           = mcast_open,
159         .close          = mcast_close,
160         .remove         = NULL,
161         .set_mtu        = mcast_set_mtu,
162         .add_address    = NULL,
163         .delete_address = NULL,
164         .max_packet     = MAX_PACKET - ETH_HEADER_OTHER
165 };