From: Stuart Longland Subject: [PATCH] ax25ipd: Add support for multicast AX/UDPv4 Date: Sat, 8 Mar 2014 19:29:12 +1000 Message-ID: <1394270952-18477-1-git-send-email-me@vk4msl.yi.org> References: Return-path: In-Reply-To: Sender: linux-hams-owner@vger.kernel.org List-ID: MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit To: Ralf Baechle Cc: linux-hams@vger.kernel.org This allows the use of multicast IP addresses for exchanging UDPv4 traffic, such as on a local area network whose addresses are dynamically assigned. In /etc/ax25/ax25ipd.conf: socket udp route 0 your.multicast.ip.addr # e.g. I'm using 224.0.1.1 Then start ax25ipd as normal. --- ax25ipd/ax25ipd.h | 5 +++++ ax25ipd/config.c | 9 +++++++++ ax25ipd/io.c | 3 ++- ax25ipd/routing.c | 40 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 56 insertions(+), 1 deletion(-) diff --git a/ax25ipd/ax25ipd.h b/ax25ipd/ax25ipd.h index b088247..6c5404b 100644 --- a/ax25ipd/ax25ipd.h +++ b/ax25ipd/ax25ipd.h @@ -72,8 +72,12 @@ #include #include +#define IPV4_MULTICAST_ADDR (0xe0000000ul) +#define IPV4_MULTICAST_MASK (0xf0000000ul) + int udp_mode; /* true if we need a UDP socket */ int ip_mode; /* true if we need the raw IP socket */ +struct in_addr my_addr; /* the IP address to bind to (default INADDR_ANY) */ unsigned short my_udp; /* the UDP port to use (network byte order) */ char ttydevice[PATH_MAX]; /* the tty device for serial comms */ int ttyspeed; /* The baud rate on the tty device */ @@ -141,6 +145,7 @@ unsigned char *call_to_ip(unsigned char *); int is_call_bcast(unsigned char *); void send_broadcast(unsigned char *, int); void dump_routes(void); +void join_multicast(int); /* config.c */ void config_init(void); diff --git a/ax25ipd/config.c b/ax25ipd/config.c index 8d9405b..434bf7c 100644 --- a/ax25ipd/config.c +++ b/ax25ipd/config.c @@ -47,6 +47,7 @@ void config_init(void) udp_mode = 0; ip_mode = 0; dual_port = 0; + my_addr.s_addr = INADDR_ANY; stats.kiss_in = 0; stats.kiss_toobig = 0; @@ -254,6 +255,14 @@ int parse_line(char *buf) return -9; return 0; + } else if (strcmp(p, "bindip") == 0) { + q = strtok(NULL, " \t\n\r"); + if (q != NULL) { + if (inet_aton(q, &my_addr) < 0) + return -10; + } + return 0; + } else if (strcmp(p, "beacon") == 0) { q = strtok(NULL, " \t\n\r"); if (q == NULL) diff --git a/ax25ipd/io.c b/ax25ipd/io.c index f0b3993..4ac1cdb 100644 --- a/ax25ipd/io.c +++ b/ax25ipd/io.c @@ -184,12 +184,13 @@ void io_open(void) * Ok, the udp socket is open. Now express our interest in receiving * data destined for a particular socket. */ - udpbind.sin_addr.s_addr = INADDR_ANY; + memcpy(&udpbind.sin_addr, &my_addr, sizeof(udpbind.sin_addr)); udpbind.sin_port = my_udp; if (bind(udpsock, (struct sockaddr *) &udpbind, sizeof udpbind) < 0) { perror("binding udp socket"); exit(1); } + join_multicast(udpsock); } if (!strcmp("/dev/ptmx", ttydevice)) diff --git a/ax25ipd/routing.c b/ax25ipd/routing.c index 4c3faed..155f211 100644 --- a/ax25ipd/routing.c +++ b/ax25ipd/routing.c @@ -26,6 +26,7 @@ struct route_table_entry { unsigned char pad1; unsigned char pad2; unsigned int flags; /* route flags */ + unsigned int multicast; /* Is this a multicast address? */ struct route_table_entry *next; }; @@ -252,3 +253,42 @@ void dump_routes(void) } fflush(stdout); } + +/* Look for routes to multicast groups and join them */ +void join_multicast(int udpsock) +{ + struct route_table_entry *rp; + int num_groups = 0; + + rp = route_tbl; + while (rp) { + if (rp->udp_port && + ((htonl(rp->ip_addr_in.s_addr) + & IPV4_MULTICAST_MASK) + == IPV4_MULTICAST_ADDR)) { + struct ip_mreqn group; + group.imr_ifindex = 0; + memcpy(&group.imr_multiaddr, &rp->ip_addr_in, + sizeof(group.imr_multiaddr)); + memcpy(&group.imr_address, &my_addr, + sizeof(group.imr_address)); + if (setsockopt(udpsock, IPPROTO_IP, IP_ADD_MEMBERSHIP, + (char*)&group, sizeof(group)) < 0) { + perror("adding multicast group"); + close(udpsock); + exit(1); + } + num_groups++; + } + rp = rp->next; + } + if (num_groups) { + u_char loop = 0; + if (setsockopt(udpsock, IPPROTO_IP, + IP_MULTICAST_LOOP, &loop, sizeof(loop)) < 0) { + perror("disabling loopback"); + close(udpsock); + exit(1); + } + } +} -- 1.8.3.2