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> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | #include "user.h" |
| 24 | #include "os.h" |
Paolo 'Blaisorblade' Giarrusso | c13e569 | 2006-10-19 23:28:20 -0700 | [diff] [blame] | 25 | #include "um_malloc.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | |
| 27 | #define MAX_PACKET (ETH_MAX_PACKET + ETH_HEADER_OTHER) |
| 28 | |
| 29 | static struct sockaddr_in *new_addr(char *addr, unsigned short port) |
| 30 | { |
| 31 | struct sockaddr_in *sin; |
| 32 | |
Jeff Dike | e4c4bf9 | 2007-07-15 23:38:56 -0700 | [diff] [blame] | 33 | sin = kmalloc(sizeof(struct sockaddr_in), UM_GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | if(sin == NULL){ |
| 35 | printk("new_addr: allocation of sockaddr_in failed\n"); |
Jeff Dike | 56bd194 | 2007-05-06 14:51:02 -0700 | [diff] [blame] | 36 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | } |
| 38 | sin->sin_family = AF_INET; |
| 39 | sin->sin_addr.s_addr = in_aton(addr); |
Jeff Dike | 7c00c31 | 2005-05-20 13:59:09 -0700 | [diff] [blame] | 40 | sin->sin_port = htons(port); |
Jeff Dike | 56bd194 | 2007-05-06 14:51:02 -0700 | [diff] [blame] | 41 | return sin; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | } |
| 43 | |
Jeff Dike | f34d9d2 | 2007-05-06 14:51:04 -0700 | [diff] [blame] | 44 | static int mcast_user_init(void *data, void *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | { |
| 46 | struct mcast_data *pri = data; |
| 47 | |
| 48 | pri->mcast_addr = new_addr(pri->addr, pri->port); |
| 49 | pri->dev = dev; |
Jeff Dike | f34d9d2 | 2007-05-06 14:51:04 -0700 | [diff] [blame] | 50 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | } |
| 52 | |
Paolo 'Blaisorblade' Giarrusso | 83f4e8a | 2007-03-07 20:41:09 -0800 | [diff] [blame] | 53 | static void mcast_remove(void *data) |
| 54 | { |
| 55 | struct mcast_data *pri = data; |
| 56 | |
| 57 | kfree(pri->mcast_addr); |
| 58 | pri->mcast_addr = NULL; |
| 59 | } |
| 60 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | static int mcast_open(void *data) |
| 62 | { |
| 63 | struct mcast_data *pri = data; |
| 64 | struct sockaddr_in *sin = pri->mcast_addr; |
| 65 | struct ip_mreq mreq; |
Paolo 'Blaisorblade' Giarrusso | c50d2c4 | 2005-11-13 16:07:07 -0800 | [diff] [blame] | 66 | int fd, yes = 1, err = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | |
| 68 | |
Jeff Dike | 7c00c31 | 2005-05-20 13:59:09 -0700 | [diff] [blame] | 69 | if ((sin->sin_addr.s_addr == 0) || (sin->sin_port == 0)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | |
| 72 | fd = socket(AF_INET, SOCK_DGRAM, 0); |
Jeff Dike | 7c00c31 | 2005-05-20 13:59:09 -0700 | [diff] [blame] | 73 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | if (fd < 0){ |
Paolo 'Blaisorblade' Giarrusso | c50d2c4 | 2005-11-13 16:07:07 -0800 | [diff] [blame] | 75 | err = -errno; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | printk("mcast_open : data socket failed, errno = %d\n", |
| 77 | errno); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | goto out; |
| 79 | } |
| 80 | |
| 81 | if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) < 0) { |
Paolo 'Blaisorblade' Giarrusso | c50d2c4 | 2005-11-13 16:07:07 -0800 | [diff] [blame] | 82 | err = -errno; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | printk("mcast_open: SO_REUSEADDR failed, errno = %d\n", |
| 84 | errno); |
Jeff Dike | 7c00c31 | 2005-05-20 13:59:09 -0700 | [diff] [blame] | 85 | goto out_close; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | /* set ttl according to config */ |
| 89 | if (setsockopt(fd, SOL_IP, IP_MULTICAST_TTL, &pri->ttl, |
| 90 | sizeof(pri->ttl)) < 0) { |
Paolo 'Blaisorblade' Giarrusso | c50d2c4 | 2005-11-13 16:07:07 -0800 | [diff] [blame] | 91 | err = -errno; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | printk("mcast_open: IP_MULTICAST_TTL failed, error = %d\n", |
| 93 | errno); |
Jeff Dike | 7c00c31 | 2005-05-20 13:59:09 -0700 | [diff] [blame] | 94 | goto out_close; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | /* set LOOP, so data does get fed back to local sockets */ |
| 98 | if (setsockopt(fd, SOL_IP, IP_MULTICAST_LOOP, &yes, sizeof(yes)) < 0) { |
Paolo 'Blaisorblade' Giarrusso | c50d2c4 | 2005-11-13 16:07:07 -0800 | [diff] [blame] | 99 | err = -errno; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | printk("mcast_open: IP_MULTICAST_LOOP failed, error = %d\n", |
| 101 | errno); |
Jeff Dike | 7c00c31 | 2005-05-20 13:59:09 -0700 | [diff] [blame] | 102 | goto out_close; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | /* bind socket to mcast address */ |
| 106 | if (bind(fd, (struct sockaddr *) sin, sizeof(*sin)) < 0) { |
Jeff Dike | b4fd310 | 2005-09-16 19:27:49 -0700 | [diff] [blame] | 107 | err = -errno; |
Paolo 'Blaisorblade' Giarrusso | c50d2c4 | 2005-11-13 16:07:07 -0800 | [diff] [blame] | 108 | printk("mcast_open : data bind failed, errno = %d\n", errno); |
Jeff Dike | 7c00c31 | 2005-05-20 13:59:09 -0700 | [diff] [blame] | 109 | goto out_close; |
Jeff Dike | 56bd194 | 2007-05-06 14:51:02 -0700 | [diff] [blame] | 110 | } |
| 111 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | /* subscribe to the multicast group */ |
| 113 | mreq.imr_multiaddr.s_addr = sin->sin_addr.s_addr; |
| 114 | mreq.imr_interface.s_addr = 0; |
| 115 | if (setsockopt(fd, SOL_IP, IP_ADD_MEMBERSHIP, |
| 116 | &mreq, sizeof(mreq)) < 0) { |
Paolo 'Blaisorblade' Giarrusso | c50d2c4 | 2005-11-13 16:07:07 -0800 | [diff] [blame] | 117 | err = -errno; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | printk("mcast_open: IP_ADD_MEMBERSHIP failed, error = %d\n", |
| 119 | errno); |
| 120 | printk("There appears not to be a multicast-capable network " |
| 121 | "interface on the host.\n"); |
| 122 | printk("eth0 should be configured in order to use the " |
| 123 | "multicast transport.\n"); |
Paolo 'Blaisorblade' Giarrusso | c50d2c4 | 2005-11-13 16:07:07 -0800 | [diff] [blame] | 124 | goto out_close; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | } |
| 126 | |
Jeff Dike | 7c00c31 | 2005-05-20 13:59:09 -0700 | [diff] [blame] | 127 | return fd; |
| 128 | |
| 129 | out_close: |
Paolo 'Blaisorblade' Giarrusso | c50d2c4 | 2005-11-13 16:07:07 -0800 | [diff] [blame] | 130 | os_close_file(fd); |
Jeff Dike | b4fd310 | 2005-09-16 19:27:49 -0700 | [diff] [blame] | 131 | out: |
Paolo 'Blaisorblade' Giarrusso | c50d2c4 | 2005-11-13 16:07:07 -0800 | [diff] [blame] | 132 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | static void mcast_close(int fd, void *data) |
| 136 | { |
| 137 | struct ip_mreq mreq; |
| 138 | struct mcast_data *pri = data; |
| 139 | struct sockaddr_in *sin = pri->mcast_addr; |
| 140 | |
| 141 | mreq.imr_multiaddr.s_addr = sin->sin_addr.s_addr; |
| 142 | mreq.imr_interface.s_addr = 0; |
| 143 | if (setsockopt(fd, SOL_IP, IP_DROP_MEMBERSHIP, |
| 144 | &mreq, sizeof(mreq)) < 0) { |
| 145 | printk("mcast_open: IP_DROP_MEMBERSHIP failed, error = %d\n", |
| 146 | errno); |
| 147 | } |
| 148 | |
| 149 | os_close_file(fd); |
| 150 | } |
| 151 | |
| 152 | int mcast_user_write(int fd, void *buf, int len, struct mcast_data *pri) |
| 153 | { |
| 154 | struct sockaddr_in *data_addr = pri->mcast_addr; |
| 155 | |
Jeff Dike | 56bd194 | 2007-05-06 14:51:02 -0700 | [diff] [blame] | 156 | return net_sendto(fd, buf, len, data_addr, sizeof(*data_addr)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | static int mcast_set_mtu(int mtu, void *data) |
| 160 | { |
Jeff Dike | 56bd194 | 2007-05-06 14:51:02 -0700 | [diff] [blame] | 161 | return mtu; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | } |
| 163 | |
Jeff Dike | 5e7672e | 2006-09-27 01:50:33 -0700 | [diff] [blame] | 164 | const struct net_user_info mcast_user_info = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | .init = mcast_user_init, |
| 166 | .open = mcast_open, |
| 167 | .close = mcast_close, |
Paolo 'Blaisorblade' Giarrusso | 83f4e8a | 2007-03-07 20:41:09 -0800 | [diff] [blame] | 168 | .remove = mcast_remove, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | .set_mtu = mcast_set_mtu, |
| 170 | .add_address = NULL, |
| 171 | .delete_address = NULL, |
| 172 | .max_packet = MAX_PACKET - ETH_HEADER_OTHER |
| 173 | }; |