Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame^] | 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 <linux/inet.h> |
| 17 | #include <sys/socket.h> |
| 18 | #include <sys/un.h> |
| 19 | #include <sys/time.h> |
| 20 | #include <netinet/in.h> |
| 21 | #include "net_user.h" |
| 22 | #include "mcast.h" |
| 23 | #include "kern_util.h" |
| 24 | #include "user_util.h" |
| 25 | #include "user.h" |
| 26 | #include "os.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 = 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; |
| 59 | |
| 60 | |
| 61 | if ((sin->sin_addr.s_addr == 0) || (sin->sin_port == 0)) { |
| 62 | fd = -EINVAL; |
| 63 | goto out; |
| 64 | } |
| 65 | |
| 66 | fd = socket(AF_INET, SOCK_DGRAM, 0); |
| 67 | if (fd < 0){ |
| 68 | printk("mcast_open : data socket failed, errno = %d\n", |
| 69 | errno); |
| 70 | fd = -ENOMEM; |
| 71 | goto out; |
| 72 | } |
| 73 | |
| 74 | if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) < 0) { |
| 75 | printk("mcast_open: SO_REUSEADDR failed, errno = %d\n", |
| 76 | errno); |
| 77 | os_close_file(fd); |
| 78 | fd = -EINVAL; |
| 79 | goto out; |
| 80 | } |
| 81 | |
| 82 | /* set ttl according to config */ |
| 83 | if (setsockopt(fd, SOL_IP, IP_MULTICAST_TTL, &pri->ttl, |
| 84 | sizeof(pri->ttl)) < 0) { |
| 85 | printk("mcast_open: IP_MULTICAST_TTL failed, error = %d\n", |
| 86 | errno); |
| 87 | os_close_file(fd); |
| 88 | fd = -EINVAL; |
| 89 | goto out; |
| 90 | } |
| 91 | |
| 92 | /* set LOOP, so data does get fed back to local sockets */ |
| 93 | if (setsockopt(fd, SOL_IP, IP_MULTICAST_LOOP, &yes, sizeof(yes)) < 0) { |
| 94 | printk("mcast_open: IP_MULTICAST_LOOP failed, error = %d\n", |
| 95 | errno); |
| 96 | os_close_file(fd); |
| 97 | fd = -EINVAL; |
| 98 | goto out; |
| 99 | } |
| 100 | |
| 101 | /* bind socket to mcast address */ |
| 102 | if (bind(fd, (struct sockaddr *) sin, sizeof(*sin)) < 0) { |
| 103 | printk("mcast_open : data bind failed, errno = %d\n", errno); |
| 104 | os_close_file(fd); |
| 105 | fd = -EINVAL; |
| 106 | goto out; |
| 107 | } |
| 108 | |
| 109 | /* subscribe to the multicast group */ |
| 110 | mreq.imr_multiaddr.s_addr = sin->sin_addr.s_addr; |
| 111 | mreq.imr_interface.s_addr = 0; |
| 112 | if (setsockopt(fd, SOL_IP, IP_ADD_MEMBERSHIP, |
| 113 | &mreq, sizeof(mreq)) < 0) { |
| 114 | printk("mcast_open: IP_ADD_MEMBERSHIP failed, error = %d\n", |
| 115 | errno); |
| 116 | printk("There appears not to be a multicast-capable network " |
| 117 | "interface on the host.\n"); |
| 118 | printk("eth0 should be configured in order to use the " |
| 119 | "multicast transport.\n"); |
| 120 | os_close_file(fd); |
| 121 | fd = -EINVAL; |
| 122 | } |
| 123 | |
| 124 | out: |
| 125 | return(fd); |
| 126 | } |
| 127 | |
| 128 | static void mcast_close(int fd, void *data) |
| 129 | { |
| 130 | struct ip_mreq mreq; |
| 131 | struct mcast_data *pri = data; |
| 132 | struct sockaddr_in *sin = pri->mcast_addr; |
| 133 | |
| 134 | mreq.imr_multiaddr.s_addr = sin->sin_addr.s_addr; |
| 135 | mreq.imr_interface.s_addr = 0; |
| 136 | if (setsockopt(fd, SOL_IP, IP_DROP_MEMBERSHIP, |
| 137 | &mreq, sizeof(mreq)) < 0) { |
| 138 | printk("mcast_open: IP_DROP_MEMBERSHIP failed, error = %d\n", |
| 139 | errno); |
| 140 | } |
| 141 | |
| 142 | os_close_file(fd); |
| 143 | } |
| 144 | |
| 145 | int mcast_user_write(int fd, void *buf, int len, struct mcast_data *pri) |
| 146 | { |
| 147 | struct sockaddr_in *data_addr = pri->mcast_addr; |
| 148 | |
| 149 | return(net_sendto(fd, buf, len, data_addr, sizeof(*data_addr))); |
| 150 | } |
| 151 | |
| 152 | static int mcast_set_mtu(int mtu, void *data) |
| 153 | { |
| 154 | return(mtu); |
| 155 | } |
| 156 | |
| 157 | struct net_user_info mcast_user_info = { |
| 158 | .init = mcast_user_init, |
| 159 | .open = mcast_open, |
| 160 | .close = mcast_close, |
| 161 | .remove = NULL, |
| 162 | .set_mtu = mcast_set_mtu, |
| 163 | .add_address = NULL, |
| 164 | .delete_address = NULL, |
| 165 | .max_packet = MAX_PACKET - ETH_HEADER_OTHER |
| 166 | }; |
| 167 | |
| 168 | /* |
| 169 | * Overrides for Emacs so that we follow Linus's tabbing style. |
| 170 | * Emacs will notice this stuff at the end of the file and automatically |
| 171 | * adjust the settings for this buffer only. This must remain at the end |
| 172 | * of the file. |
| 173 | * --------------------------------------------------------------------------- |
| 174 | * Local variables: |
| 175 | * c-file-style: "linux" |
| 176 | * End: |
| 177 | */ |