stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1 | /* |
Rami Rosen | eb5ce43 | 2012-11-13 13:29:15 +0000 | [diff] [blame] | 2 | * VXLAN: Virtual eXtensible Local Area Network |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 3 | * |
stephen hemminger | 3b8df3c | 2013-04-27 11:31:52 +0000 | [diff] [blame] | 4 | * Copyright (c) 2012-2013 Vyatta Inc. |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License version 2 as |
| 8 | * published by the Free Software Foundation. |
| 9 | * |
| 10 | * TODO |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 11 | * - IPv6 (not in RFC) |
| 12 | */ |
| 13 | |
| 14 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 15 | |
| 16 | #include <linux/kernel.h> |
| 17 | #include <linux/types.h> |
| 18 | #include <linux/module.h> |
| 19 | #include <linux/errno.h> |
| 20 | #include <linux/slab.h> |
| 21 | #include <linux/skbuff.h> |
| 22 | #include <linux/rculist.h> |
| 23 | #include <linux/netdevice.h> |
| 24 | #include <linux/in.h> |
| 25 | #include <linux/ip.h> |
| 26 | #include <linux/udp.h> |
| 27 | #include <linux/igmp.h> |
| 28 | #include <linux/etherdevice.h> |
| 29 | #include <linux/if_ether.h> |
Pravin B Shelar | 1eaa817 | 2013-08-19 11:23:29 -0700 | [diff] [blame^] | 30 | #include <linux/if_vlan.h> |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 31 | #include <linux/hash.h> |
Yan Burman | 1b13c97 | 2013-01-29 23:43:07 +0000 | [diff] [blame] | 32 | #include <linux/ethtool.h> |
David Stevens | e4f67ad | 2012-11-20 02:50:14 +0000 | [diff] [blame] | 33 | #include <net/arp.h> |
| 34 | #include <net/ndisc.h> |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 35 | #include <net/ip.h> |
Pravin B Shelar | c544193 | 2013-03-25 14:49:35 +0000 | [diff] [blame] | 36 | #include <net/ip_tunnels.h> |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 37 | #include <net/icmp.h> |
| 38 | #include <net/udp.h> |
| 39 | #include <net/rtnetlink.h> |
| 40 | #include <net/route.h> |
| 41 | #include <net/dsfield.h> |
| 42 | #include <net/inet_ecn.h> |
| 43 | #include <net/net_namespace.h> |
| 44 | #include <net/netns/generic.h> |
Pravin B Shelar | 012a572 | 2013-08-19 11:23:07 -0700 | [diff] [blame] | 45 | #include <net/vxlan.h> |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 46 | |
| 47 | #define VXLAN_VERSION "0.1" |
| 48 | |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame] | 49 | #define PORT_HASH_BITS 8 |
| 50 | #define PORT_HASH_SIZE (1<<PORT_HASH_BITS) |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 51 | #define VNI_HASH_BITS 10 |
| 52 | #define VNI_HASH_SIZE (1<<VNI_HASH_BITS) |
| 53 | #define FDB_HASH_BITS 8 |
| 54 | #define FDB_HASH_SIZE (1<<FDB_HASH_BITS) |
| 55 | #define FDB_AGE_DEFAULT 300 /* 5 min */ |
| 56 | #define FDB_AGE_INTERVAL (10 * HZ) /* rescan interval */ |
| 57 | |
| 58 | #define VXLAN_N_VID (1u << 24) |
| 59 | #define VXLAN_VID_MASK (VXLAN_N_VID - 1) |
Alexander Duyck | 52b702f | 2012-11-09 13:35:24 +0000 | [diff] [blame] | 60 | /* IP header + UDP + VXLAN + Ethernet header */ |
| 61 | #define VXLAN_HEADROOM (20 + 8 + 8 + 14) |
Pravin B Shelar | 7ce0475 | 2013-08-19 11:22:54 -0700 | [diff] [blame] | 62 | #define VXLAN_HLEN (sizeof(struct udphdr) + sizeof(struct vxlanhdr)) |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 63 | |
| 64 | #define VXLAN_FLAGS 0x08000000 /* struct vxlanhdr.vx_flags required value. */ |
| 65 | |
| 66 | /* VXLAN protocol header */ |
| 67 | struct vxlanhdr { |
| 68 | __be32 vx_flags; |
| 69 | __be32 vx_vni; |
| 70 | }; |
| 71 | |
stephen hemminger | 23c578b | 2013-04-27 11:31:53 +0000 | [diff] [blame] | 72 | /* UDP port for VXLAN traffic. |
| 73 | * The IANA assigned port is 4789, but the Linux default is 8472 |
Stephen Hemminger | 234f5b7 | 2013-06-17 14:16:41 -0700 | [diff] [blame] | 74 | * for compatibility with early adopters. |
stephen hemminger | 23c578b | 2013-04-27 11:31:53 +0000 | [diff] [blame] | 75 | */ |
Stephen Hemminger | 9daaa39 | 2013-06-17 14:16:12 -0700 | [diff] [blame] | 76 | static unsigned short vxlan_port __read_mostly = 8472; |
| 77 | module_param_named(udp_port, vxlan_port, ushort, 0444); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 78 | MODULE_PARM_DESC(udp_port, "Destination UDP port"); |
| 79 | |
| 80 | static bool log_ecn_error = true; |
| 81 | module_param(log_ecn_error, bool, 0644); |
| 82 | MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN"); |
| 83 | |
Pravin B Shelar | 60d9d4c | 2013-06-20 00:26:31 -0700 | [diff] [blame] | 84 | static int vxlan_net_id; |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame] | 85 | |
Mike Rapoport | afbd8ba | 2013-06-25 16:01:51 +0300 | [diff] [blame] | 86 | static const u8 all_zeros_mac[ETH_ALEN]; |
| 87 | |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame] | 88 | /* per-network namespace private data for this module */ |
| 89 | struct vxlan_net { |
| 90 | struct list_head vxlan_list; |
| 91 | struct hlist_head sock_list[PORT_HASH_SIZE]; |
Stephen Hemminger | 1c51a91 | 2013-06-17 14:16:11 -0700 | [diff] [blame] | 92 | spinlock_t sock_lock; |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame] | 93 | }; |
| 94 | |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 95 | struct vxlan_rdst { |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 96 | __be32 remote_ip; |
| 97 | __be16 remote_port; |
| 98 | u32 remote_vni; |
| 99 | u32 remote_ifindex; |
Stephen Hemminger | 3e61aa8 | 2013-06-17 14:16:12 -0700 | [diff] [blame] | 100 | struct list_head list; |
Mike Rapoport | bc7892b | 2013-06-25 16:01:54 +0300 | [diff] [blame] | 101 | struct rcu_head rcu; |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 102 | }; |
| 103 | |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 104 | /* Forwarding table entry */ |
| 105 | struct vxlan_fdb { |
| 106 | struct hlist_node hlist; /* linked list of entries */ |
| 107 | struct rcu_head rcu; |
| 108 | unsigned long updated; /* jiffies */ |
| 109 | unsigned long used; |
Stephen Hemminger | 3e61aa8 | 2013-06-17 14:16:12 -0700 | [diff] [blame] | 110 | struct list_head remotes; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 111 | u16 state; /* see ndm_state */ |
David Stevens | ae88408 | 2013-04-19 00:36:26 +0000 | [diff] [blame] | 112 | u8 flags; /* see ndm_flags */ |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 113 | u8 eth_addr[ETH_ALEN]; |
| 114 | }; |
| 115 | |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 116 | /* Pseudo network device */ |
| 117 | struct vxlan_dev { |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame] | 118 | struct hlist_node hlist; /* vni hash table */ |
| 119 | struct list_head next; /* vxlan's per namespace list */ |
| 120 | struct vxlan_sock *vn_sock; /* listening socket */ |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 121 | struct net_device *dev; |
Atzm Watanabe | c7995c4 | 2013-04-16 02:50:52 +0000 | [diff] [blame] | 122 | struct vxlan_rdst default_dst; /* default destination */ |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 123 | __be32 saddr; /* source address */ |
stephen hemminger | 823aa87 | 2013-04-27 11:31:57 +0000 | [diff] [blame] | 124 | __be16 dst_port; |
stephen hemminger | 05f47d6 | 2012-10-09 20:35:50 +0000 | [diff] [blame] | 125 | __u16 port_min; /* source port range */ |
| 126 | __u16 port_max; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 127 | __u8 tos; /* TOS override */ |
| 128 | __u8 ttl; |
David Stevens | e4f67ad | 2012-11-20 02:50:14 +0000 | [diff] [blame] | 129 | u32 flags; /* VXLAN_F_* below */ |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 130 | |
Stephen Hemminger | 1c51a91 | 2013-06-17 14:16:11 -0700 | [diff] [blame] | 131 | struct work_struct sock_work; |
stephen hemminger | 3fc2de2 | 2013-07-18 08:40:15 -0700 | [diff] [blame] | 132 | struct work_struct igmp_join; |
| 133 | struct work_struct igmp_leave; |
Stephen Hemminger | 1c51a91 | 2013-06-17 14:16:11 -0700 | [diff] [blame] | 134 | |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 135 | unsigned long age_interval; |
| 136 | struct timer_list age_timer; |
| 137 | spinlock_t hash_lock; |
| 138 | unsigned int addrcnt; |
| 139 | unsigned int addrmax; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 140 | |
| 141 | struct hlist_head fdb_head[FDB_HASH_SIZE]; |
| 142 | }; |
| 143 | |
David Stevens | e4f67ad | 2012-11-20 02:50:14 +0000 | [diff] [blame] | 144 | #define VXLAN_F_LEARN 0x01 |
| 145 | #define VXLAN_F_PROXY 0x02 |
| 146 | #define VXLAN_F_RSC 0x04 |
| 147 | #define VXLAN_F_L2MISS 0x08 |
| 148 | #define VXLAN_F_L3MISS 0x10 |
| 149 | |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 150 | /* salt for hash table */ |
| 151 | static u32 vxlan_salt __read_mostly; |
Stephen Hemminger | 758c57d | 2013-06-17 14:16:09 -0700 | [diff] [blame] | 152 | static struct workqueue_struct *vxlan_wq; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 153 | |
Stephen Hemminger | 1c51a91 | 2013-06-17 14:16:11 -0700 | [diff] [blame] | 154 | static void vxlan_sock_work(struct work_struct *work); |
| 155 | |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame] | 156 | /* Virtual Network hash table head */ |
| 157 | static inline struct hlist_head *vni_head(struct vxlan_sock *vs, u32 id) |
| 158 | { |
| 159 | return &vs->vni_list[hash_32(id, VNI_HASH_BITS)]; |
| 160 | } |
| 161 | |
| 162 | /* Socket hash table head */ |
| 163 | static inline struct hlist_head *vs_head(struct net *net, __be16 port) |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 164 | { |
| 165 | struct vxlan_net *vn = net_generic(net, vxlan_net_id); |
| 166 | |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame] | 167 | return &vn->sock_list[hash_32(ntohs(port), PORT_HASH_BITS)]; |
| 168 | } |
| 169 | |
Stephen Hemminger | 3e61aa8 | 2013-06-17 14:16:12 -0700 | [diff] [blame] | 170 | /* First remote destination for a forwarding entry. |
| 171 | * Guaranteed to be non-NULL because remotes are never deleted. |
| 172 | */ |
stephen hemminger | 5ca5461 | 2013-08-04 17:17:39 -0700 | [diff] [blame] | 173 | static inline struct vxlan_rdst *first_remote_rcu(struct vxlan_fdb *fdb) |
Stephen Hemminger | 3e61aa8 | 2013-06-17 14:16:12 -0700 | [diff] [blame] | 174 | { |
stephen hemminger | 5ca5461 | 2013-08-04 17:17:39 -0700 | [diff] [blame] | 175 | return list_entry_rcu(fdb->remotes.next, struct vxlan_rdst, list); |
| 176 | } |
| 177 | |
| 178 | static inline struct vxlan_rdst *first_remote_rtnl(struct vxlan_fdb *fdb) |
| 179 | { |
| 180 | return list_first_entry(&fdb->remotes, struct vxlan_rdst, list); |
Stephen Hemminger | 3e61aa8 | 2013-06-17 14:16:12 -0700 | [diff] [blame] | 181 | } |
| 182 | |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame] | 183 | /* Find VXLAN socket based on network namespace and UDP port */ |
Pravin B Shelar | 9c2e24e | 2013-08-19 11:22:48 -0700 | [diff] [blame] | 184 | static struct vxlan_sock *vxlan_find_sock(struct net *net, __be16 port) |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame] | 185 | { |
| 186 | struct vxlan_sock *vs; |
| 187 | |
| 188 | hlist_for_each_entry_rcu(vs, vs_head(net, port), hlist) { |
| 189 | if (inet_sk(vs->sock->sk)->inet_sport == port) |
| 190 | return vs; |
| 191 | } |
| 192 | return NULL; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 193 | } |
| 194 | |
Pravin B Shelar | 5cfccc5 | 2013-08-19 11:23:02 -0700 | [diff] [blame] | 195 | static struct vxlan_dev *vxlan_vs_find_vni(struct vxlan_sock *vs, u32 id) |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 196 | { |
| 197 | struct vxlan_dev *vxlan; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 198 | |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame] | 199 | hlist_for_each_entry_rcu(vxlan, vni_head(vs, id), hlist) { |
Atzm Watanabe | c7995c4 | 2013-04-16 02:50:52 +0000 | [diff] [blame] | 200 | if (vxlan->default_dst.remote_vni == id) |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 201 | return vxlan; |
| 202 | } |
| 203 | |
| 204 | return NULL; |
| 205 | } |
| 206 | |
Pravin B Shelar | 5cfccc5 | 2013-08-19 11:23:02 -0700 | [diff] [blame] | 207 | /* Look up VNI in a per net namespace table */ |
| 208 | static struct vxlan_dev *vxlan_find_vni(struct net *net, u32 id, __be16 port) |
| 209 | { |
| 210 | struct vxlan_sock *vs; |
| 211 | |
| 212 | vs = vxlan_find_sock(net, port); |
| 213 | if (!vs) |
| 214 | return NULL; |
| 215 | |
| 216 | return vxlan_vs_find_vni(vs, id); |
| 217 | } |
| 218 | |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 219 | /* Fill in neighbour message in skbuff. */ |
| 220 | static int vxlan_fdb_info(struct sk_buff *skb, struct vxlan_dev *vxlan, |
Stephen Hemminger | 234f5b7 | 2013-06-17 14:16:41 -0700 | [diff] [blame] | 221 | const struct vxlan_fdb *fdb, |
| 222 | u32 portid, u32 seq, int type, unsigned int flags, |
| 223 | const struct vxlan_rdst *rdst) |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 224 | { |
| 225 | unsigned long now = jiffies; |
| 226 | struct nda_cacheinfo ci; |
| 227 | struct nlmsghdr *nlh; |
| 228 | struct ndmsg *ndm; |
David Stevens | e4f67ad | 2012-11-20 02:50:14 +0000 | [diff] [blame] | 229 | bool send_ip, send_eth; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 230 | |
| 231 | nlh = nlmsg_put(skb, portid, seq, type, sizeof(*ndm), flags); |
| 232 | if (nlh == NULL) |
| 233 | return -EMSGSIZE; |
| 234 | |
| 235 | ndm = nlmsg_data(nlh); |
| 236 | memset(ndm, 0, sizeof(*ndm)); |
David Stevens | e4f67ad | 2012-11-20 02:50:14 +0000 | [diff] [blame] | 237 | |
| 238 | send_eth = send_ip = true; |
| 239 | |
| 240 | if (type == RTM_GETNEIGH) { |
| 241 | ndm->ndm_family = AF_INET; |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 242 | send_ip = rdst->remote_ip != htonl(INADDR_ANY); |
David Stevens | e4f67ad | 2012-11-20 02:50:14 +0000 | [diff] [blame] | 243 | send_eth = !is_zero_ether_addr(fdb->eth_addr); |
| 244 | } else |
| 245 | ndm->ndm_family = AF_BRIDGE; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 246 | ndm->ndm_state = fdb->state; |
| 247 | ndm->ndm_ifindex = vxlan->dev->ifindex; |
David Stevens | ae88408 | 2013-04-19 00:36:26 +0000 | [diff] [blame] | 248 | ndm->ndm_flags = fdb->flags; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 249 | ndm->ndm_type = NDA_DST; |
| 250 | |
David Stevens | e4f67ad | 2012-11-20 02:50:14 +0000 | [diff] [blame] | 251 | if (send_eth && nla_put(skb, NDA_LLADDR, ETH_ALEN, &fdb->eth_addr)) |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 252 | goto nla_put_failure; |
| 253 | |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 254 | if (send_ip && nla_put_be32(skb, NDA_DST, rdst->remote_ip)) |
| 255 | goto nla_put_failure; |
| 256 | |
stephen hemminger | 823aa87 | 2013-04-27 11:31:57 +0000 | [diff] [blame] | 257 | if (rdst->remote_port && rdst->remote_port != vxlan->dst_port && |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 258 | nla_put_be16(skb, NDA_PORT, rdst->remote_port)) |
| 259 | goto nla_put_failure; |
Atzm Watanabe | c7995c4 | 2013-04-16 02:50:52 +0000 | [diff] [blame] | 260 | if (rdst->remote_vni != vxlan->default_dst.remote_vni && |
Pravin B Shelar | 60d9d4c | 2013-06-20 00:26:31 -0700 | [diff] [blame] | 261 | nla_put_u32(skb, NDA_VNI, rdst->remote_vni)) |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 262 | goto nla_put_failure; |
| 263 | if (rdst->remote_ifindex && |
| 264 | nla_put_u32(skb, NDA_IFINDEX, rdst->remote_ifindex)) |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 265 | goto nla_put_failure; |
| 266 | |
| 267 | ci.ndm_used = jiffies_to_clock_t(now - fdb->used); |
| 268 | ci.ndm_confirmed = 0; |
| 269 | ci.ndm_updated = jiffies_to_clock_t(now - fdb->updated); |
| 270 | ci.ndm_refcnt = 0; |
| 271 | |
| 272 | if (nla_put(skb, NDA_CACHEINFO, sizeof(ci), &ci)) |
| 273 | goto nla_put_failure; |
| 274 | |
| 275 | return nlmsg_end(skb, nlh); |
| 276 | |
| 277 | nla_put_failure: |
| 278 | nlmsg_cancel(skb, nlh); |
| 279 | return -EMSGSIZE; |
| 280 | } |
| 281 | |
| 282 | static inline size_t vxlan_nlmsg_size(void) |
| 283 | { |
| 284 | return NLMSG_ALIGN(sizeof(struct ndmsg)) |
| 285 | + nla_total_size(ETH_ALEN) /* NDA_LLADDR */ |
| 286 | + nla_total_size(sizeof(__be32)) /* NDA_DST */ |
stephen hemminger | 73cf331 | 2013-04-27 11:31:54 +0000 | [diff] [blame] | 287 | + nla_total_size(sizeof(__be16)) /* NDA_PORT */ |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 288 | + nla_total_size(sizeof(__be32)) /* NDA_VNI */ |
| 289 | + nla_total_size(sizeof(__u32)) /* NDA_IFINDEX */ |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 290 | + nla_total_size(sizeof(struct nda_cacheinfo)); |
| 291 | } |
| 292 | |
| 293 | static void vxlan_fdb_notify(struct vxlan_dev *vxlan, |
Stephen Hemminger | 3e61aa8 | 2013-06-17 14:16:12 -0700 | [diff] [blame] | 294 | struct vxlan_fdb *fdb, int type) |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 295 | { |
| 296 | struct net *net = dev_net(vxlan->dev); |
| 297 | struct sk_buff *skb; |
| 298 | int err = -ENOBUFS; |
| 299 | |
| 300 | skb = nlmsg_new(vxlan_nlmsg_size(), GFP_ATOMIC); |
| 301 | if (skb == NULL) |
| 302 | goto errout; |
| 303 | |
stephen hemminger | 5ca5461 | 2013-08-04 17:17:39 -0700 | [diff] [blame] | 304 | err = vxlan_fdb_info(skb, vxlan, fdb, 0, 0, type, 0, |
| 305 | first_remote_rtnl(fdb)); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 306 | if (err < 0) { |
| 307 | /* -EMSGSIZE implies BUG in vxlan_nlmsg_size() */ |
| 308 | WARN_ON(err == -EMSGSIZE); |
| 309 | kfree_skb(skb); |
| 310 | goto errout; |
| 311 | } |
| 312 | |
| 313 | rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC); |
| 314 | return; |
| 315 | errout: |
| 316 | if (err < 0) |
| 317 | rtnl_set_sk_err(net, RTNLGRP_NEIGH, err); |
| 318 | } |
| 319 | |
David Stevens | e4f67ad | 2012-11-20 02:50:14 +0000 | [diff] [blame] | 320 | static void vxlan_ip_miss(struct net_device *dev, __be32 ipa) |
| 321 | { |
| 322 | struct vxlan_dev *vxlan = netdev_priv(dev); |
Stephen Hemminger | bb3fd68 | 2013-06-17 14:16:40 -0700 | [diff] [blame] | 323 | struct vxlan_fdb f = { |
| 324 | .state = NUD_STALE, |
| 325 | }; |
| 326 | struct vxlan_rdst remote = { |
| 327 | .remote_ip = ipa, /* goes to NDA_DST */ |
| 328 | .remote_vni = VXLAN_N_VID, |
| 329 | }; |
Stephen Hemminger | 3e61aa8 | 2013-06-17 14:16:12 -0700 | [diff] [blame] | 330 | |
| 331 | INIT_LIST_HEAD(&f.remotes); |
| 332 | list_add_rcu(&remote.list, &f.remotes); |
David Stevens | e4f67ad | 2012-11-20 02:50:14 +0000 | [diff] [blame] | 333 | |
| 334 | vxlan_fdb_notify(vxlan, &f, RTM_GETNEIGH); |
| 335 | } |
| 336 | |
| 337 | static void vxlan_fdb_miss(struct vxlan_dev *vxlan, const u8 eth_addr[ETH_ALEN]) |
| 338 | { |
Stephen Hemminger | bb3fd68 | 2013-06-17 14:16:40 -0700 | [diff] [blame] | 339 | struct vxlan_fdb f = { |
| 340 | .state = NUD_STALE, |
| 341 | }; |
David Stevens | e4f67ad | 2012-11-20 02:50:14 +0000 | [diff] [blame] | 342 | |
Stephen Hemminger | 3e61aa8 | 2013-06-17 14:16:12 -0700 | [diff] [blame] | 343 | INIT_LIST_HEAD(&f.remotes); |
David Stevens | e4f67ad | 2012-11-20 02:50:14 +0000 | [diff] [blame] | 344 | memcpy(f.eth_addr, eth_addr, ETH_ALEN); |
| 345 | |
| 346 | vxlan_fdb_notify(vxlan, &f, RTM_GETNEIGH); |
| 347 | } |
| 348 | |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 349 | /* Hash Ethernet address */ |
| 350 | static u32 eth_hash(const unsigned char *addr) |
| 351 | { |
| 352 | u64 value = get_unaligned((u64 *)addr); |
| 353 | |
| 354 | /* only want 6 bytes */ |
| 355 | #ifdef __BIG_ENDIAN |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 356 | value >>= 16; |
stephen hemminger | 321fb99 | 2012-10-09 20:35:47 +0000 | [diff] [blame] | 357 | #else |
| 358 | value <<= 16; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 359 | #endif |
| 360 | return hash_64(value, FDB_HASH_BITS); |
| 361 | } |
| 362 | |
| 363 | /* Hash chain to use given mac address */ |
| 364 | static inline struct hlist_head *vxlan_fdb_head(struct vxlan_dev *vxlan, |
| 365 | const u8 *mac) |
| 366 | { |
| 367 | return &vxlan->fdb_head[eth_hash(mac)]; |
| 368 | } |
| 369 | |
| 370 | /* Look up Ethernet address in forwarding table */ |
Sridhar Samudrala | 014be2c | 2013-05-17 06:39:07 +0000 | [diff] [blame] | 371 | static struct vxlan_fdb *__vxlan_find_mac(struct vxlan_dev *vxlan, |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 372 | const u8 *mac) |
| 373 | |
| 374 | { |
| 375 | struct hlist_head *head = vxlan_fdb_head(vxlan, mac); |
| 376 | struct vxlan_fdb *f; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 377 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 378 | hlist_for_each_entry_rcu(f, head, hlist) { |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 379 | if (compare_ether_addr(mac, f->eth_addr) == 0) |
| 380 | return f; |
| 381 | } |
| 382 | |
| 383 | return NULL; |
| 384 | } |
| 385 | |
Sridhar Samudrala | 014be2c | 2013-05-17 06:39:07 +0000 | [diff] [blame] | 386 | static struct vxlan_fdb *vxlan_find_mac(struct vxlan_dev *vxlan, |
| 387 | const u8 *mac) |
| 388 | { |
| 389 | struct vxlan_fdb *f; |
| 390 | |
| 391 | f = __vxlan_find_mac(vxlan, mac); |
| 392 | if (f) |
| 393 | f->used = jiffies; |
| 394 | |
| 395 | return f; |
| 396 | } |
| 397 | |
Mike Rapoport | a5e7c10 | 2013-06-25 16:01:52 +0300 | [diff] [blame] | 398 | /* caller should hold vxlan->hash_lock */ |
| 399 | static struct vxlan_rdst *vxlan_fdb_find_rdst(struct vxlan_fdb *f, |
| 400 | __be32 ip, __be16 port, |
| 401 | __u32 vni, __u32 ifindex) |
| 402 | { |
| 403 | struct vxlan_rdst *rd; |
| 404 | |
| 405 | list_for_each_entry(rd, &f->remotes, list) { |
| 406 | if (rd->remote_ip == ip && |
| 407 | rd->remote_port == port && |
| 408 | rd->remote_vni == vni && |
| 409 | rd->remote_ifindex == ifindex) |
| 410 | return rd; |
| 411 | } |
| 412 | |
| 413 | return NULL; |
| 414 | } |
| 415 | |
Thomas Richter | 906dc18 | 2013-07-19 17:20:07 +0200 | [diff] [blame] | 416 | /* Replace destination of unicast mac */ |
| 417 | static int vxlan_fdb_replace(struct vxlan_fdb *f, |
| 418 | __be32 ip, __be16 port, __u32 vni, __u32 ifindex) |
| 419 | { |
| 420 | struct vxlan_rdst *rd; |
| 421 | |
| 422 | rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex); |
| 423 | if (rd) |
| 424 | return 0; |
| 425 | |
| 426 | rd = list_first_entry_or_null(&f->remotes, struct vxlan_rdst, list); |
| 427 | if (!rd) |
| 428 | return 0; |
| 429 | rd->remote_ip = ip; |
| 430 | rd->remote_port = port; |
| 431 | rd->remote_vni = vni; |
| 432 | rd->remote_ifindex = ifindex; |
| 433 | return 1; |
| 434 | } |
| 435 | |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 436 | /* Add/update destinations for multicast */ |
| 437 | static int vxlan_fdb_append(struct vxlan_fdb *f, |
stephen hemminger | 73cf331 | 2013-04-27 11:31:54 +0000 | [diff] [blame] | 438 | __be32 ip, __be16 port, __u32 vni, __u32 ifindex) |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 439 | { |
Stephen Hemminger | 3e61aa8 | 2013-06-17 14:16:12 -0700 | [diff] [blame] | 440 | struct vxlan_rdst *rd; |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 441 | |
Mike Rapoport | a5e7c10 | 2013-06-25 16:01:52 +0300 | [diff] [blame] | 442 | rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex); |
| 443 | if (rd) |
| 444 | return 0; |
Stephen Hemminger | 3e61aa8 | 2013-06-17 14:16:12 -0700 | [diff] [blame] | 445 | |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 446 | rd = kmalloc(sizeof(*rd), GFP_ATOMIC); |
| 447 | if (rd == NULL) |
| 448 | return -ENOBUFS; |
| 449 | rd->remote_ip = ip; |
| 450 | rd->remote_port = port; |
| 451 | rd->remote_vni = vni; |
| 452 | rd->remote_ifindex = ifindex; |
Stephen Hemminger | 3e61aa8 | 2013-06-17 14:16:12 -0700 | [diff] [blame] | 453 | |
| 454 | list_add_tail_rcu(&rd->list, &f->remotes); |
| 455 | |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 456 | return 1; |
| 457 | } |
| 458 | |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 459 | /* Add new entry to forwarding table -- assumes lock held */ |
| 460 | static int vxlan_fdb_create(struct vxlan_dev *vxlan, |
| 461 | const u8 *mac, __be32 ip, |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 462 | __u16 state, __u16 flags, |
stephen hemminger | 73cf331 | 2013-04-27 11:31:54 +0000 | [diff] [blame] | 463 | __be16 port, __u32 vni, __u32 ifindex, |
David Stevens | ae88408 | 2013-04-19 00:36:26 +0000 | [diff] [blame] | 464 | __u8 ndm_flags) |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 465 | { |
| 466 | struct vxlan_fdb *f; |
| 467 | int notify = 0; |
| 468 | |
Sridhar Samudrala | 014be2c | 2013-05-17 06:39:07 +0000 | [diff] [blame] | 469 | f = __vxlan_find_mac(vxlan, mac); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 470 | if (f) { |
| 471 | if (flags & NLM_F_EXCL) { |
| 472 | netdev_dbg(vxlan->dev, |
| 473 | "lost race to create %pM\n", mac); |
| 474 | return -EEXIST; |
| 475 | } |
| 476 | if (f->state != state) { |
| 477 | f->state = state; |
| 478 | f->updated = jiffies; |
| 479 | notify = 1; |
| 480 | } |
David Stevens | ae88408 | 2013-04-19 00:36:26 +0000 | [diff] [blame] | 481 | if (f->flags != ndm_flags) { |
| 482 | f->flags = ndm_flags; |
| 483 | f->updated = jiffies; |
| 484 | notify = 1; |
| 485 | } |
Thomas Richter | 906dc18 | 2013-07-19 17:20:07 +0200 | [diff] [blame] | 486 | if ((flags & NLM_F_REPLACE)) { |
| 487 | /* Only change unicasts */ |
| 488 | if (!(is_multicast_ether_addr(f->eth_addr) || |
| 489 | is_zero_ether_addr(f->eth_addr))) { |
| 490 | int rc = vxlan_fdb_replace(f, ip, port, vni, |
| 491 | ifindex); |
| 492 | |
| 493 | if (rc < 0) |
| 494 | return rc; |
| 495 | notify |= rc; |
| 496 | } else |
| 497 | return -EOPNOTSUPP; |
| 498 | } |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 499 | if ((flags & NLM_F_APPEND) && |
Mike Rapoport | 58e4c76 | 2013-06-25 16:01:56 +0300 | [diff] [blame] | 500 | (is_multicast_ether_addr(f->eth_addr) || |
| 501 | is_zero_ether_addr(f->eth_addr))) { |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 502 | int rc = vxlan_fdb_append(f, ip, port, vni, ifindex); |
| 503 | |
| 504 | if (rc < 0) |
| 505 | return rc; |
| 506 | notify |= rc; |
| 507 | } |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 508 | } else { |
| 509 | if (!(flags & NLM_F_CREATE)) |
| 510 | return -ENOENT; |
| 511 | |
| 512 | if (vxlan->addrmax && vxlan->addrcnt >= vxlan->addrmax) |
| 513 | return -ENOSPC; |
| 514 | |
Thomas Richter | 906dc18 | 2013-07-19 17:20:07 +0200 | [diff] [blame] | 515 | /* Disallow replace to add a multicast entry */ |
| 516 | if ((flags & NLM_F_REPLACE) && |
| 517 | (is_multicast_ether_addr(mac) || is_zero_ether_addr(mac))) |
| 518 | return -EOPNOTSUPP; |
| 519 | |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 520 | netdev_dbg(vxlan->dev, "add %pM -> %pI4\n", mac, &ip); |
| 521 | f = kmalloc(sizeof(*f), GFP_ATOMIC); |
| 522 | if (!f) |
| 523 | return -ENOMEM; |
| 524 | |
| 525 | notify = 1; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 526 | f->state = state; |
David Stevens | ae88408 | 2013-04-19 00:36:26 +0000 | [diff] [blame] | 527 | f->flags = ndm_flags; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 528 | f->updated = f->used = jiffies; |
Stephen Hemminger | 3e61aa8 | 2013-06-17 14:16:12 -0700 | [diff] [blame] | 529 | INIT_LIST_HEAD(&f->remotes); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 530 | memcpy(f->eth_addr, mac, ETH_ALEN); |
| 531 | |
Stephen Hemminger | 3e61aa8 | 2013-06-17 14:16:12 -0700 | [diff] [blame] | 532 | vxlan_fdb_append(f, ip, port, vni, ifindex); |
| 533 | |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 534 | ++vxlan->addrcnt; |
| 535 | hlist_add_head_rcu(&f->hlist, |
| 536 | vxlan_fdb_head(vxlan, mac)); |
| 537 | } |
| 538 | |
| 539 | if (notify) |
| 540 | vxlan_fdb_notify(vxlan, f, RTM_NEWNEIGH); |
| 541 | |
| 542 | return 0; |
| 543 | } |
| 544 | |
Mike Rapoport | bc7892b | 2013-06-25 16:01:54 +0300 | [diff] [blame] | 545 | static void vxlan_fdb_free_rdst(struct rcu_head *head) |
| 546 | { |
| 547 | struct vxlan_rdst *rd = container_of(head, struct vxlan_rdst, rcu); |
| 548 | kfree(rd); |
| 549 | } |
| 550 | |
Wei Yongjun | 6706c82 | 2013-04-11 19:00:35 +0000 | [diff] [blame] | 551 | static void vxlan_fdb_free(struct rcu_head *head) |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 552 | { |
| 553 | struct vxlan_fdb *f = container_of(head, struct vxlan_fdb, rcu); |
Stephen Hemminger | 3e61aa8 | 2013-06-17 14:16:12 -0700 | [diff] [blame] | 554 | struct vxlan_rdst *rd, *nd; |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 555 | |
Stephen Hemminger | 3e61aa8 | 2013-06-17 14:16:12 -0700 | [diff] [blame] | 556 | list_for_each_entry_safe(rd, nd, &f->remotes, list) |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 557 | kfree(rd); |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 558 | kfree(f); |
| 559 | } |
| 560 | |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 561 | static void vxlan_fdb_destroy(struct vxlan_dev *vxlan, struct vxlan_fdb *f) |
| 562 | { |
| 563 | netdev_dbg(vxlan->dev, |
| 564 | "delete %pM\n", f->eth_addr); |
| 565 | |
| 566 | --vxlan->addrcnt; |
| 567 | vxlan_fdb_notify(vxlan, f, RTM_DELNEIGH); |
| 568 | |
| 569 | hlist_del_rcu(&f->hlist); |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 570 | call_rcu(&f->rcu, vxlan_fdb_free); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 571 | } |
| 572 | |
Mike Rapoport | f0b074b | 2013-06-25 16:01:53 +0300 | [diff] [blame] | 573 | static int vxlan_fdb_parse(struct nlattr *tb[], struct vxlan_dev *vxlan, |
| 574 | __be32 *ip, __be16 *port, u32 *vni, u32 *ifindex) |
| 575 | { |
| 576 | struct net *net = dev_net(vxlan->dev); |
| 577 | |
| 578 | if (tb[NDA_DST]) { |
| 579 | if (nla_len(tb[NDA_DST]) != sizeof(__be32)) |
| 580 | return -EAFNOSUPPORT; |
| 581 | |
| 582 | *ip = nla_get_be32(tb[NDA_DST]); |
| 583 | } else { |
| 584 | *ip = htonl(INADDR_ANY); |
| 585 | } |
| 586 | |
| 587 | if (tb[NDA_PORT]) { |
| 588 | if (nla_len(tb[NDA_PORT]) != sizeof(__be16)) |
| 589 | return -EINVAL; |
| 590 | *port = nla_get_be16(tb[NDA_PORT]); |
| 591 | } else { |
| 592 | *port = vxlan->dst_port; |
| 593 | } |
| 594 | |
| 595 | if (tb[NDA_VNI]) { |
| 596 | if (nla_len(tb[NDA_VNI]) != sizeof(u32)) |
| 597 | return -EINVAL; |
| 598 | *vni = nla_get_u32(tb[NDA_VNI]); |
| 599 | } else { |
| 600 | *vni = vxlan->default_dst.remote_vni; |
| 601 | } |
| 602 | |
| 603 | if (tb[NDA_IFINDEX]) { |
| 604 | struct net_device *tdev; |
| 605 | |
| 606 | if (nla_len(tb[NDA_IFINDEX]) != sizeof(u32)) |
| 607 | return -EINVAL; |
| 608 | *ifindex = nla_get_u32(tb[NDA_IFINDEX]); |
| 609 | tdev = dev_get_by_index(net, *ifindex); |
| 610 | if (!tdev) |
| 611 | return -EADDRNOTAVAIL; |
| 612 | dev_put(tdev); |
| 613 | } else { |
| 614 | *ifindex = 0; |
| 615 | } |
| 616 | |
| 617 | return 0; |
| 618 | } |
| 619 | |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 620 | /* Add static entry (via netlink) */ |
| 621 | static int vxlan_fdb_add(struct ndmsg *ndm, struct nlattr *tb[], |
| 622 | struct net_device *dev, |
| 623 | const unsigned char *addr, u16 flags) |
| 624 | { |
| 625 | struct vxlan_dev *vxlan = netdev_priv(dev); |
Mike Rapoport | f0b074b | 2013-06-25 16:01:53 +0300 | [diff] [blame] | 626 | /* struct net *net = dev_net(vxlan->dev); */ |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 627 | __be32 ip; |
stephen hemminger | 73cf331 | 2013-04-27 11:31:54 +0000 | [diff] [blame] | 628 | __be16 port; |
| 629 | u32 vni, ifindex; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 630 | int err; |
| 631 | |
| 632 | if (!(ndm->ndm_state & (NUD_PERMANENT|NUD_REACHABLE))) { |
| 633 | pr_info("RTM_NEWNEIGH with invalid state %#x\n", |
| 634 | ndm->ndm_state); |
| 635 | return -EINVAL; |
| 636 | } |
| 637 | |
| 638 | if (tb[NDA_DST] == NULL) |
| 639 | return -EINVAL; |
| 640 | |
Mike Rapoport | f0b074b | 2013-06-25 16:01:53 +0300 | [diff] [blame] | 641 | err = vxlan_fdb_parse(tb, vxlan, &ip, &port, &vni, &ifindex); |
| 642 | if (err) |
| 643 | return err; |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 644 | |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 645 | spin_lock_bh(&vxlan->hash_lock); |
stephen hemminger | 73cf331 | 2013-04-27 11:31:54 +0000 | [diff] [blame] | 646 | err = vxlan_fdb_create(vxlan, addr, ip, ndm->ndm_state, flags, |
| 647 | port, vni, ifindex, ndm->ndm_flags); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 648 | spin_unlock_bh(&vxlan->hash_lock); |
| 649 | |
| 650 | return err; |
| 651 | } |
| 652 | |
| 653 | /* Delete entry (via netlink) */ |
Vlad Yasevich | 1690be6 | 2013-02-13 12:00:18 +0000 | [diff] [blame] | 654 | static int vxlan_fdb_delete(struct ndmsg *ndm, struct nlattr *tb[], |
| 655 | struct net_device *dev, |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 656 | const unsigned char *addr) |
| 657 | { |
| 658 | struct vxlan_dev *vxlan = netdev_priv(dev); |
| 659 | struct vxlan_fdb *f; |
Mike Rapoport | bc7892b | 2013-06-25 16:01:54 +0300 | [diff] [blame] | 660 | struct vxlan_rdst *rd = NULL; |
| 661 | __be32 ip; |
| 662 | __be16 port; |
| 663 | u32 vni, ifindex; |
| 664 | int err; |
| 665 | |
| 666 | err = vxlan_fdb_parse(tb, vxlan, &ip, &port, &vni, &ifindex); |
| 667 | if (err) |
| 668 | return err; |
| 669 | |
| 670 | err = -ENOENT; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 671 | |
| 672 | spin_lock_bh(&vxlan->hash_lock); |
| 673 | f = vxlan_find_mac(vxlan, addr); |
Mike Rapoport | bc7892b | 2013-06-25 16:01:54 +0300 | [diff] [blame] | 674 | if (!f) |
| 675 | goto out; |
| 676 | |
| 677 | if (ip != htonl(INADDR_ANY)) { |
| 678 | rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex); |
| 679 | if (!rd) |
| 680 | goto out; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 681 | } |
Mike Rapoport | bc7892b | 2013-06-25 16:01:54 +0300 | [diff] [blame] | 682 | |
| 683 | err = 0; |
| 684 | |
| 685 | /* remove a destination if it's not the only one on the list, |
| 686 | * otherwise destroy the fdb entry |
| 687 | */ |
| 688 | if (rd && !list_is_singular(&f->remotes)) { |
| 689 | list_del_rcu(&rd->list); |
| 690 | call_rcu(&rd->rcu, vxlan_fdb_free_rdst); |
| 691 | goto out; |
| 692 | } |
| 693 | |
| 694 | vxlan_fdb_destroy(vxlan, f); |
| 695 | |
| 696 | out: |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 697 | spin_unlock_bh(&vxlan->hash_lock); |
| 698 | |
| 699 | return err; |
| 700 | } |
| 701 | |
| 702 | /* Dump forwarding table */ |
| 703 | static int vxlan_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb, |
| 704 | struct net_device *dev, int idx) |
| 705 | { |
| 706 | struct vxlan_dev *vxlan = netdev_priv(dev); |
| 707 | unsigned int h; |
| 708 | |
| 709 | for (h = 0; h < FDB_HASH_SIZE; ++h) { |
| 710 | struct vxlan_fdb *f; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 711 | int err; |
| 712 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 713 | hlist_for_each_entry_rcu(f, &vxlan->fdb_head[h], hlist) { |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 714 | struct vxlan_rdst *rd; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 715 | |
Stephen Hemminger | 3e61aa8 | 2013-06-17 14:16:12 -0700 | [diff] [blame] | 716 | if (idx < cb->args[0]) |
| 717 | goto skip; |
| 718 | |
| 719 | list_for_each_entry_rcu(rd, &f->remotes, list) { |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 720 | err = vxlan_fdb_info(skb, vxlan, f, |
| 721 | NETLINK_CB(cb->skb).portid, |
| 722 | cb->nlh->nlmsg_seq, |
| 723 | RTM_NEWNEIGH, |
| 724 | NLM_F_MULTI, rd); |
| 725 | if (err < 0) |
Stephen Hemminger | 3e61aa8 | 2013-06-17 14:16:12 -0700 | [diff] [blame] | 726 | goto out; |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 727 | } |
Stephen Hemminger | 3e61aa8 | 2013-06-17 14:16:12 -0700 | [diff] [blame] | 728 | skip: |
| 729 | ++idx; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 730 | } |
| 731 | } |
Stephen Hemminger | 3e61aa8 | 2013-06-17 14:16:12 -0700 | [diff] [blame] | 732 | out: |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 733 | return idx; |
| 734 | } |
| 735 | |
| 736 | /* Watch incoming packets to learn mapping between Ethernet address |
| 737 | * and Tunnel endpoint. |
stephen hemminger | 26a41ae6 | 2013-06-17 12:09:58 -0700 | [diff] [blame] | 738 | * Return true if packet is bogus and should be droppped. |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 739 | */ |
stephen hemminger | 26a41ae6 | 2013-06-17 12:09:58 -0700 | [diff] [blame] | 740 | static bool vxlan_snoop(struct net_device *dev, |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 741 | __be32 src_ip, const u8 *src_mac) |
| 742 | { |
| 743 | struct vxlan_dev *vxlan = netdev_priv(dev); |
| 744 | struct vxlan_fdb *f; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 745 | |
| 746 | f = vxlan_find_mac(vxlan, src_mac); |
| 747 | if (likely(f)) { |
stephen hemminger | 5ca5461 | 2013-08-04 17:17:39 -0700 | [diff] [blame] | 748 | struct vxlan_rdst *rdst = first_remote_rcu(f); |
Stephen Hemminger | 3e61aa8 | 2013-06-17 14:16:12 -0700 | [diff] [blame] | 749 | |
| 750 | if (likely(rdst->remote_ip == src_ip)) |
stephen hemminger | 26a41ae6 | 2013-06-17 12:09:58 -0700 | [diff] [blame] | 751 | return false; |
| 752 | |
| 753 | /* Don't migrate static entries, drop packets */ |
stephen hemminger | eb064c3 | 2013-06-18 14:27:01 -0700 | [diff] [blame] | 754 | if (f->state & NUD_NOARP) |
stephen hemminger | 26a41ae6 | 2013-06-17 12:09:58 -0700 | [diff] [blame] | 755 | return true; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 756 | |
| 757 | if (net_ratelimit()) |
| 758 | netdev_info(dev, |
| 759 | "%pM migrated from %pI4 to %pI4\n", |
Stephen Hemminger | 3e61aa8 | 2013-06-17 14:16:12 -0700 | [diff] [blame] | 760 | src_mac, &rdst->remote_ip, &src_ip); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 761 | |
Stephen Hemminger | 3e61aa8 | 2013-06-17 14:16:12 -0700 | [diff] [blame] | 762 | rdst->remote_ip = src_ip; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 763 | f->updated = jiffies; |
Stephen Hemminger | 8385f50 | 2013-06-17 14:16:10 -0700 | [diff] [blame] | 764 | vxlan_fdb_notify(vxlan, f, RTM_NEWNEIGH); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 765 | } else { |
| 766 | /* learned new entry */ |
| 767 | spin_lock(&vxlan->hash_lock); |
stephen hemminger | 3bf74b1 | 2013-06-17 12:09:57 -0700 | [diff] [blame] | 768 | |
| 769 | /* close off race between vxlan_flush and incoming packets */ |
| 770 | if (netif_running(dev)) |
| 771 | vxlan_fdb_create(vxlan, src_mac, src_ip, |
| 772 | NUD_REACHABLE, |
| 773 | NLM_F_EXCL|NLM_F_CREATE, |
| 774 | vxlan->dst_port, |
| 775 | vxlan->default_dst.remote_vni, |
| 776 | 0, NTF_SELF); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 777 | spin_unlock(&vxlan->hash_lock); |
| 778 | } |
stephen hemminger | 26a41ae6 | 2013-06-17 12:09:58 -0700 | [diff] [blame] | 779 | |
| 780 | return false; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 781 | } |
| 782 | |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 783 | /* See if multicast group is already in use by other ID */ |
Stephen Hemminger | 7c47ced | 2013-06-17 14:16:10 -0700 | [diff] [blame] | 784 | static bool vxlan_group_used(struct vxlan_net *vn, __be32 remote_ip) |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 785 | { |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame] | 786 | struct vxlan_dev *vxlan; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 787 | |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame] | 788 | list_for_each_entry(vxlan, &vn->vxlan_list, next) { |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame] | 789 | if (!netif_running(vxlan->dev)) |
| 790 | continue; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 791 | |
Stephen Hemminger | 7c47ced | 2013-06-17 14:16:10 -0700 | [diff] [blame] | 792 | if (vxlan->default_dst.remote_ip == remote_ip) |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame] | 793 | return true; |
| 794 | } |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 795 | |
| 796 | return false; |
| 797 | } |
| 798 | |
Stephen Hemminger | 7c47ced | 2013-06-17 14:16:10 -0700 | [diff] [blame] | 799 | static void vxlan_sock_hold(struct vxlan_sock *vs) |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 800 | { |
Stephen Hemminger | 7c47ced | 2013-06-17 14:16:10 -0700 | [diff] [blame] | 801 | atomic_inc(&vs->refcnt); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 802 | } |
| 803 | |
Pravin B Shelar | 012a572 | 2013-08-19 11:23:07 -0700 | [diff] [blame] | 804 | void vxlan_sock_release(struct vxlan_sock *vs) |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 805 | { |
Pravin B Shelar | 012a572 | 2013-08-19 11:23:07 -0700 | [diff] [blame] | 806 | struct vxlan_net *vn = net_generic(sock_net(vs->sock->sk), vxlan_net_id); |
| 807 | |
Stephen Hemminger | 7c47ced | 2013-06-17 14:16:10 -0700 | [diff] [blame] | 808 | if (!atomic_dec_and_test(&vs->refcnt)) |
| 809 | return; |
| 810 | |
Stephen Hemminger | 1c51a91 | 2013-06-17 14:16:11 -0700 | [diff] [blame] | 811 | spin_lock(&vn->sock_lock); |
Stephen Hemminger | 7c47ced | 2013-06-17 14:16:10 -0700 | [diff] [blame] | 812 | hlist_del_rcu(&vs->hlist); |
Stephen Hemminger | 1c51a91 | 2013-06-17 14:16:11 -0700 | [diff] [blame] | 813 | spin_unlock(&vn->sock_lock); |
| 814 | |
Stephen Hemminger | 7c47ced | 2013-06-17 14:16:10 -0700 | [diff] [blame] | 815 | queue_work(vxlan_wq, &vs->del_work); |
| 816 | } |
Pravin B Shelar | 012a572 | 2013-08-19 11:23:07 -0700 | [diff] [blame] | 817 | EXPORT_SYMBOL_GPL(vxlan_sock_release); |
Stephen Hemminger | 7c47ced | 2013-06-17 14:16:10 -0700 | [diff] [blame] | 818 | |
stephen hemminger | 3fc2de2 | 2013-07-18 08:40:15 -0700 | [diff] [blame] | 819 | /* Callback to update multicast group membership when first VNI on |
| 820 | * multicast asddress is brought up |
| 821 | * Done as workqueue because ip_mc_join_group acquires RTNL. |
Stephen Hemminger | 7c47ced | 2013-06-17 14:16:10 -0700 | [diff] [blame] | 822 | */ |
stephen hemminger | 3fc2de2 | 2013-07-18 08:40:15 -0700 | [diff] [blame] | 823 | static void vxlan_igmp_join(struct work_struct *work) |
Stephen Hemminger | 7c47ced | 2013-06-17 14:16:10 -0700 | [diff] [blame] | 824 | { |
stephen hemminger | 3fc2de2 | 2013-07-18 08:40:15 -0700 | [diff] [blame] | 825 | struct vxlan_dev *vxlan = container_of(work, struct vxlan_dev, igmp_join); |
Stephen Hemminger | 7c47ced | 2013-06-17 14:16:10 -0700 | [diff] [blame] | 826 | struct vxlan_sock *vs = vxlan->vn_sock; |
| 827 | struct sock *sk = vs->sock->sk; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 828 | struct ip_mreqn mreq = { |
Atzm Watanabe | c7995c4 | 2013-04-16 02:50:52 +0000 | [diff] [blame] | 829 | .imr_multiaddr.s_addr = vxlan->default_dst.remote_ip, |
| 830 | .imr_ifindex = vxlan->default_dst.remote_ifindex, |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 831 | }; |
| 832 | |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 833 | lock_sock(sk); |
stephen hemminger | 3fc2de2 | 2013-07-18 08:40:15 -0700 | [diff] [blame] | 834 | ip_mc_join_group(sk, &mreq); |
| 835 | release_sock(sk); |
| 836 | |
Pravin B Shelar | 012a572 | 2013-08-19 11:23:07 -0700 | [diff] [blame] | 837 | vxlan_sock_release(vs); |
stephen hemminger | 3fc2de2 | 2013-07-18 08:40:15 -0700 | [diff] [blame] | 838 | dev_put(vxlan->dev); |
| 839 | } |
| 840 | |
| 841 | /* Inverse of vxlan_igmp_join when last VNI is brought down */ |
| 842 | static void vxlan_igmp_leave(struct work_struct *work) |
| 843 | { |
| 844 | struct vxlan_dev *vxlan = container_of(work, struct vxlan_dev, igmp_leave); |
stephen hemminger | 3fc2de2 | 2013-07-18 08:40:15 -0700 | [diff] [blame] | 845 | struct vxlan_sock *vs = vxlan->vn_sock; |
| 846 | struct sock *sk = vs->sock->sk; |
| 847 | struct ip_mreqn mreq = { |
| 848 | .imr_multiaddr.s_addr = vxlan->default_dst.remote_ip, |
| 849 | .imr_ifindex = vxlan->default_dst.remote_ifindex, |
| 850 | }; |
| 851 | |
| 852 | lock_sock(sk); |
| 853 | ip_mc_leave_group(sk, &mreq); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 854 | release_sock(sk); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 855 | |
Pravin B Shelar | 012a572 | 2013-08-19 11:23:07 -0700 | [diff] [blame] | 856 | vxlan_sock_release(vs); |
Stephen Hemminger | 7c47ced | 2013-06-17 14:16:10 -0700 | [diff] [blame] | 857 | dev_put(vxlan->dev); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 858 | } |
| 859 | |
| 860 | /* Callback from net/ipv4/udp.c to receive packets */ |
| 861 | static int vxlan_udp_encap_recv(struct sock *sk, struct sk_buff *skb) |
| 862 | { |
Pravin B Shelar | 5cfccc5 | 2013-08-19 11:23:02 -0700 | [diff] [blame] | 863 | struct vxlan_sock *vs; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 864 | struct vxlanhdr *vxh; |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame] | 865 | __be16 port; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 866 | |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 867 | /* Need Vxlan and inner Ethernet header to be present */ |
Pravin B Shelar | 7ce0475 | 2013-08-19 11:22:54 -0700 | [diff] [blame] | 868 | if (!pskb_may_pull(skb, VXLAN_HLEN)) |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 869 | goto error; |
| 870 | |
Pravin B Shelar | 7ce0475 | 2013-08-19 11:22:54 -0700 | [diff] [blame] | 871 | /* Return packets with reserved bits set */ |
| 872 | vxh = (struct vxlanhdr *)(udp_hdr(skb) + 1); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 873 | if (vxh->vx_flags != htonl(VXLAN_FLAGS) || |
| 874 | (vxh->vx_vni & htonl(0xff))) { |
| 875 | netdev_dbg(skb->dev, "invalid vxlan flags=%#x vni=%#x\n", |
| 876 | ntohl(vxh->vx_flags), ntohl(vxh->vx_vni)); |
| 877 | goto error; |
| 878 | } |
| 879 | |
Pravin B Shelar | 5cfccc5 | 2013-08-19 11:23:02 -0700 | [diff] [blame] | 880 | if (iptunnel_pull_header(skb, VXLAN_HLEN, htons(ETH_P_TEB))) |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 881 | goto drop; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 882 | |
Pravin B Shelar | 5cfccc5 | 2013-08-19 11:23:02 -0700 | [diff] [blame] | 883 | port = inet_sk(sk)->inet_sport; |
| 884 | |
| 885 | vs = vxlan_find_sock(sock_net(sk), port); |
| 886 | if (!vs) |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 887 | goto drop; |
Pravin B Shelar | 5cfccc5 | 2013-08-19 11:23:02 -0700 | [diff] [blame] | 888 | |
| 889 | vs->rcv(vs, skb, vxh->vx_vni); |
| 890 | return 0; |
| 891 | |
| 892 | drop: |
| 893 | /* Consume bad packet */ |
| 894 | kfree_skb(skb); |
| 895 | return 0; |
| 896 | |
| 897 | error: |
| 898 | /* Return non vxlan pkt */ |
| 899 | return 1; |
| 900 | } |
| 901 | |
| 902 | static void vxlan_rcv(struct vxlan_sock *vs, |
| 903 | struct sk_buff *skb, __be32 vx_vni) |
| 904 | { |
| 905 | struct iphdr *oip; |
| 906 | struct vxlan_dev *vxlan; |
| 907 | struct pcpu_tstats *stats; |
| 908 | __u32 vni; |
| 909 | int err; |
| 910 | |
| 911 | vni = ntohl(vx_vni) >> 8; |
| 912 | /* Is this VNI defined? */ |
| 913 | vxlan = vxlan_vs_find_vni(vs, vni); |
| 914 | if (!vxlan) |
| 915 | goto drop; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 916 | |
David Stevens | e4f67ad | 2012-11-20 02:50:14 +0000 | [diff] [blame] | 917 | skb_reset_mac_header(skb); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 918 | skb->protocol = eth_type_trans(skb, vxlan->dev); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 919 | |
| 920 | /* Ignore packet loops (and multicast echo) */ |
| 921 | if (compare_ether_addr(eth_hdr(skb)->h_source, |
| 922 | vxlan->dev->dev_addr) == 0) |
| 923 | goto drop; |
| 924 | |
Pravin B Shelar | 7ce0475 | 2013-08-19 11:22:54 -0700 | [diff] [blame] | 925 | /* Re-examine inner Ethernet packet */ |
| 926 | oip = ip_hdr(skb); |
stephen hemminger | 26a41ae6 | 2013-06-17 12:09:58 -0700 | [diff] [blame] | 927 | if ((vxlan->flags & VXLAN_F_LEARN) && |
| 928 | vxlan_snoop(skb->dev, oip->saddr, eth_hdr(skb)->h_source)) |
| 929 | goto drop; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 930 | |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 931 | skb_reset_network_header(skb); |
Joseph Gasparakis | 0afb166 | 2012-12-07 14:14:18 +0000 | [diff] [blame] | 932 | |
| 933 | /* If the NIC driver gave us an encapsulated packet with |
| 934 | * CHECKSUM_UNNECESSARY and Rx checksum feature is enabled, |
| 935 | * leave the CHECKSUM_UNNECESSARY, the device checksummed it |
| 936 | * for us. Otherwise force the upper layers to verify it. |
| 937 | */ |
| 938 | if (skb->ip_summed != CHECKSUM_UNNECESSARY || !skb->encapsulation || |
| 939 | !(vxlan->dev->features & NETIF_F_RXCSUM)) |
| 940 | skb->ip_summed = CHECKSUM_NONE; |
| 941 | |
| 942 | skb->encapsulation = 0; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 943 | |
| 944 | err = IP_ECN_decapsulate(oip, skb); |
| 945 | if (unlikely(err)) { |
| 946 | if (log_ecn_error) |
| 947 | net_info_ratelimited("non-ECT from %pI4 with TOS=%#x\n", |
| 948 | &oip->saddr, oip->tos); |
| 949 | if (err > 1) { |
| 950 | ++vxlan->dev->stats.rx_frame_errors; |
| 951 | ++vxlan->dev->stats.rx_errors; |
| 952 | goto drop; |
| 953 | } |
| 954 | } |
| 955 | |
Pravin B Shelar | e817104 | 2013-03-25 14:49:46 +0000 | [diff] [blame] | 956 | stats = this_cpu_ptr(vxlan->dev->tstats); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 957 | u64_stats_update_begin(&stats->syncp); |
| 958 | stats->rx_packets++; |
| 959 | stats->rx_bytes += skb->len; |
| 960 | u64_stats_update_end(&stats->syncp); |
| 961 | |
| 962 | netif_rx(skb); |
| 963 | |
Pravin B Shelar | 5cfccc5 | 2013-08-19 11:23:02 -0700 | [diff] [blame] | 964 | return; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 965 | drop: |
| 966 | /* Consume bad packet */ |
| 967 | kfree_skb(skb); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 968 | } |
| 969 | |
David Stevens | e4f67ad | 2012-11-20 02:50:14 +0000 | [diff] [blame] | 970 | static int arp_reduce(struct net_device *dev, struct sk_buff *skb) |
| 971 | { |
| 972 | struct vxlan_dev *vxlan = netdev_priv(dev); |
| 973 | struct arphdr *parp; |
| 974 | u8 *arpptr, *sha; |
| 975 | __be32 sip, tip; |
| 976 | struct neighbour *n; |
| 977 | |
| 978 | if (dev->flags & IFF_NOARP) |
| 979 | goto out; |
| 980 | |
| 981 | if (!pskb_may_pull(skb, arp_hdr_len(dev))) { |
| 982 | dev->stats.tx_dropped++; |
| 983 | goto out; |
| 984 | } |
| 985 | parp = arp_hdr(skb); |
| 986 | |
| 987 | if ((parp->ar_hrd != htons(ARPHRD_ETHER) && |
| 988 | parp->ar_hrd != htons(ARPHRD_IEEE802)) || |
| 989 | parp->ar_pro != htons(ETH_P_IP) || |
| 990 | parp->ar_op != htons(ARPOP_REQUEST) || |
| 991 | parp->ar_hln != dev->addr_len || |
| 992 | parp->ar_pln != 4) |
| 993 | goto out; |
| 994 | arpptr = (u8 *)parp + sizeof(struct arphdr); |
| 995 | sha = arpptr; |
| 996 | arpptr += dev->addr_len; /* sha */ |
| 997 | memcpy(&sip, arpptr, sizeof(sip)); |
| 998 | arpptr += sizeof(sip); |
| 999 | arpptr += dev->addr_len; /* tha */ |
| 1000 | memcpy(&tip, arpptr, sizeof(tip)); |
| 1001 | |
| 1002 | if (ipv4_is_loopback(tip) || |
| 1003 | ipv4_is_multicast(tip)) |
| 1004 | goto out; |
| 1005 | |
| 1006 | n = neigh_lookup(&arp_tbl, &tip, dev); |
| 1007 | |
| 1008 | if (n) { |
David Stevens | e4f67ad | 2012-11-20 02:50:14 +0000 | [diff] [blame] | 1009 | struct vxlan_fdb *f; |
| 1010 | struct sk_buff *reply; |
| 1011 | |
| 1012 | if (!(n->nud_state & NUD_CONNECTED)) { |
| 1013 | neigh_release(n); |
| 1014 | goto out; |
| 1015 | } |
| 1016 | |
| 1017 | f = vxlan_find_mac(vxlan, n->ha); |
stephen hemminger | 5ca5461 | 2013-08-04 17:17:39 -0700 | [diff] [blame] | 1018 | if (f && first_remote_rcu(f)->remote_ip == htonl(INADDR_ANY)) { |
David Stevens | e4f67ad | 2012-11-20 02:50:14 +0000 | [diff] [blame] | 1019 | /* bridge-local neighbor */ |
| 1020 | neigh_release(n); |
| 1021 | goto out; |
| 1022 | } |
| 1023 | |
| 1024 | reply = arp_create(ARPOP_REPLY, ETH_P_ARP, sip, dev, tip, sha, |
| 1025 | n->ha, sha); |
| 1026 | |
| 1027 | neigh_release(n); |
| 1028 | |
| 1029 | skb_reset_mac_header(reply); |
| 1030 | __skb_pull(reply, skb_network_offset(reply)); |
| 1031 | reply->ip_summed = CHECKSUM_UNNECESSARY; |
| 1032 | reply->pkt_type = PACKET_HOST; |
| 1033 | |
| 1034 | if (netif_rx_ni(reply) == NET_RX_DROP) |
| 1035 | dev->stats.rx_dropped++; |
| 1036 | } else if (vxlan->flags & VXLAN_F_L3MISS) |
| 1037 | vxlan_ip_miss(dev, tip); |
| 1038 | out: |
| 1039 | consume_skb(skb); |
| 1040 | return NETDEV_TX_OK; |
| 1041 | } |
| 1042 | |
| 1043 | static bool route_shortcircuit(struct net_device *dev, struct sk_buff *skb) |
| 1044 | { |
| 1045 | struct vxlan_dev *vxlan = netdev_priv(dev); |
| 1046 | struct neighbour *n; |
| 1047 | struct iphdr *pip; |
| 1048 | |
| 1049 | if (is_multicast_ether_addr(eth_hdr(skb)->h_dest)) |
| 1050 | return false; |
| 1051 | |
| 1052 | n = NULL; |
| 1053 | switch (ntohs(eth_hdr(skb)->h_proto)) { |
| 1054 | case ETH_P_IP: |
| 1055 | if (!pskb_may_pull(skb, sizeof(struct iphdr))) |
| 1056 | return false; |
| 1057 | pip = ip_hdr(skb); |
| 1058 | n = neigh_lookup(&arp_tbl, &pip->daddr, dev); |
| 1059 | break; |
| 1060 | default: |
| 1061 | return false; |
| 1062 | } |
| 1063 | |
| 1064 | if (n) { |
| 1065 | bool diff; |
| 1066 | |
| 1067 | diff = compare_ether_addr(eth_hdr(skb)->h_dest, n->ha) != 0; |
| 1068 | if (diff) { |
| 1069 | memcpy(eth_hdr(skb)->h_source, eth_hdr(skb)->h_dest, |
| 1070 | dev->addr_len); |
| 1071 | memcpy(eth_hdr(skb)->h_dest, n->ha, dev->addr_len); |
| 1072 | } |
| 1073 | neigh_release(n); |
| 1074 | return diff; |
| 1075 | } else if (vxlan->flags & VXLAN_F_L3MISS) |
| 1076 | vxlan_ip_miss(dev, pip->daddr); |
| 1077 | return false; |
| 1078 | } |
| 1079 | |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame] | 1080 | static void vxlan_sock_put(struct sk_buff *skb) |
stephen hemminger | 1cad871 | 2012-10-09 20:35:49 +0000 | [diff] [blame] | 1081 | { |
| 1082 | sock_put(skb->sk); |
| 1083 | } |
| 1084 | |
| 1085 | /* On transmit, associate with the tunnel socket */ |
Pravin B Shelar | 4956053 | 2013-08-19 11:23:17 -0700 | [diff] [blame] | 1086 | static void vxlan_set_owner(struct sock *sk, struct sk_buff *skb) |
stephen hemminger | 1cad871 | 2012-10-09 20:35:49 +0000 | [diff] [blame] | 1087 | { |
stephen hemminger | 1cad871 | 2012-10-09 20:35:49 +0000 | [diff] [blame] | 1088 | skb_orphan(skb); |
| 1089 | sock_hold(sk); |
| 1090 | skb->sk = sk; |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame] | 1091 | skb->destructor = vxlan_sock_put; |
stephen hemminger | 1cad871 | 2012-10-09 20:35:49 +0000 | [diff] [blame] | 1092 | } |
| 1093 | |
stephen hemminger | 05f47d6 | 2012-10-09 20:35:50 +0000 | [diff] [blame] | 1094 | /* Compute source port for outgoing packet |
| 1095 | * first choice to use L4 flow hash since it will spread |
| 1096 | * better and maybe available from hardware |
| 1097 | * secondary choice is to use jhash on the Ethernet header |
| 1098 | */ |
Pravin B Shelar | 4956053 | 2013-08-19 11:23:17 -0700 | [diff] [blame] | 1099 | __be16 vxlan_src_port(__u16 port_min, __u16 port_max, struct sk_buff *skb) |
stephen hemminger | 05f47d6 | 2012-10-09 20:35:50 +0000 | [diff] [blame] | 1100 | { |
Pravin B Shelar | 4956053 | 2013-08-19 11:23:17 -0700 | [diff] [blame] | 1101 | unsigned int range = (port_max - port_min) + 1; |
stephen hemminger | 05f47d6 | 2012-10-09 20:35:50 +0000 | [diff] [blame] | 1102 | u32 hash; |
| 1103 | |
| 1104 | hash = skb_get_rxhash(skb); |
| 1105 | if (!hash) |
| 1106 | hash = jhash(skb->data, 2 * ETH_ALEN, |
| 1107 | (__force u32) skb->protocol); |
| 1108 | |
Pravin B Shelar | 4956053 | 2013-08-19 11:23:17 -0700 | [diff] [blame] | 1109 | return htons((((u64) hash * range) >> 32) + port_min); |
stephen hemminger | 05f47d6 | 2012-10-09 20:35:50 +0000 | [diff] [blame] | 1110 | } |
Pravin B Shelar | 4956053 | 2013-08-19 11:23:17 -0700 | [diff] [blame] | 1111 | EXPORT_SYMBOL_GPL(vxlan_src_port); |
stephen hemminger | 05f47d6 | 2012-10-09 20:35:50 +0000 | [diff] [blame] | 1112 | |
Pravin B Shelar | 05c0db0 | 2013-03-07 13:22:36 +0000 | [diff] [blame] | 1113 | static int handle_offloads(struct sk_buff *skb) |
| 1114 | { |
| 1115 | if (skb_is_gso(skb)) { |
| 1116 | int err = skb_unclone(skb, GFP_ATOMIC); |
| 1117 | if (unlikely(err)) |
| 1118 | return err; |
| 1119 | |
Dmitry Kravkov | f6ace50 | 2013-04-28 08:16:01 +0000 | [diff] [blame] | 1120 | skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL; |
Pravin B Shelar | 05c0db0 | 2013-03-07 13:22:36 +0000 | [diff] [blame] | 1121 | } else if (skb->ip_summed != CHECKSUM_PARTIAL) |
| 1122 | skb->ip_summed = CHECKSUM_NONE; |
| 1123 | |
| 1124 | return 0; |
| 1125 | } |
| 1126 | |
Pravin B Shelar | 4956053 | 2013-08-19 11:23:17 -0700 | [diff] [blame] | 1127 | int vxlan_xmit_skb(struct net *net, struct vxlan_sock *vs, |
| 1128 | struct rtable *rt, struct sk_buff *skb, |
| 1129 | __be32 src, __be32 dst, __u8 tos, __u8 ttl, __be16 df, |
| 1130 | __be16 src_port, __be16 dst_port, __be32 vni) |
| 1131 | { |
| 1132 | struct vxlanhdr *vxh; |
| 1133 | struct udphdr *uh; |
Pravin B Shelar | 649c5b8 | 2013-08-19 11:23:22 -0700 | [diff] [blame] | 1134 | int min_headroom; |
Pravin B Shelar | 4956053 | 2013-08-19 11:23:17 -0700 | [diff] [blame] | 1135 | int err; |
| 1136 | |
| 1137 | if (!skb->encapsulation) { |
| 1138 | skb_reset_inner_headers(skb); |
| 1139 | skb->encapsulation = 1; |
| 1140 | } |
| 1141 | |
Pravin B Shelar | 649c5b8 | 2013-08-19 11:23:22 -0700 | [diff] [blame] | 1142 | min_headroom = LL_RESERVED_SPACE(rt->dst.dev) + rt->dst.header_len |
Pravin B Shelar | 1eaa817 | 2013-08-19 11:23:29 -0700 | [diff] [blame^] | 1143 | + VXLAN_HLEN + sizeof(struct iphdr) |
| 1144 | + (vlan_tx_tag_present(skb) ? VLAN_HLEN : 0); |
Pravin B Shelar | 649c5b8 | 2013-08-19 11:23:22 -0700 | [diff] [blame] | 1145 | |
| 1146 | /* Need space for new headers (invalidates iph ptr) */ |
| 1147 | err = skb_cow_head(skb, min_headroom); |
| 1148 | if (unlikely(err)) |
| 1149 | return err; |
| 1150 | |
Pravin B Shelar | 1eaa817 | 2013-08-19 11:23:29 -0700 | [diff] [blame^] | 1151 | if (vlan_tx_tag_present(skb)) { |
| 1152 | if (WARN_ON(!__vlan_put_tag(skb, |
| 1153 | skb->vlan_proto, |
| 1154 | vlan_tx_tag_get(skb)))) |
| 1155 | return -ENOMEM; |
| 1156 | |
| 1157 | skb->vlan_tci = 0; |
| 1158 | } |
| 1159 | |
Pravin B Shelar | 4956053 | 2013-08-19 11:23:17 -0700 | [diff] [blame] | 1160 | vxh = (struct vxlanhdr *) __skb_push(skb, sizeof(*vxh)); |
| 1161 | vxh->vx_flags = htonl(VXLAN_FLAGS); |
| 1162 | vxh->vx_vni = vni; |
| 1163 | |
| 1164 | __skb_push(skb, sizeof(*uh)); |
| 1165 | skb_reset_transport_header(skb); |
| 1166 | uh = udp_hdr(skb); |
| 1167 | |
| 1168 | uh->dest = dst_port; |
| 1169 | uh->source = src_port; |
| 1170 | |
| 1171 | uh->len = htons(skb->len); |
| 1172 | uh->check = 0; |
| 1173 | |
| 1174 | vxlan_set_owner(vs->sock->sk, skb); |
| 1175 | |
| 1176 | err = handle_offloads(skb); |
| 1177 | if (err) |
| 1178 | return err; |
| 1179 | |
| 1180 | return iptunnel_xmit(net, rt, skb, src, dst, |
| 1181 | IPPROTO_UDP, tos, ttl, df); |
| 1182 | } |
| 1183 | EXPORT_SYMBOL_GPL(vxlan_xmit_skb); |
| 1184 | |
Sridhar Samudrala | 9dcc71e | 2013-04-02 12:31:52 +0000 | [diff] [blame] | 1185 | /* Bypass encapsulation if the destination is local */ |
| 1186 | static void vxlan_encap_bypass(struct sk_buff *skb, struct vxlan_dev *src_vxlan, |
| 1187 | struct vxlan_dev *dst_vxlan) |
| 1188 | { |
| 1189 | struct pcpu_tstats *tx_stats = this_cpu_ptr(src_vxlan->dev->tstats); |
| 1190 | struct pcpu_tstats *rx_stats = this_cpu_ptr(dst_vxlan->dev->tstats); |
| 1191 | |
| 1192 | skb->pkt_type = PACKET_HOST; |
| 1193 | skb->encapsulation = 0; |
| 1194 | skb->dev = dst_vxlan->dev; |
| 1195 | __skb_pull(skb, skb_network_offset(skb)); |
| 1196 | |
| 1197 | if (dst_vxlan->flags & VXLAN_F_LEARN) |
Mike Rapoport | 9d9f163 | 2013-04-13 23:21:39 +0000 | [diff] [blame] | 1198 | vxlan_snoop(skb->dev, htonl(INADDR_LOOPBACK), |
| 1199 | eth_hdr(skb)->h_source); |
Sridhar Samudrala | 9dcc71e | 2013-04-02 12:31:52 +0000 | [diff] [blame] | 1200 | |
| 1201 | u64_stats_update_begin(&tx_stats->syncp); |
| 1202 | tx_stats->tx_packets++; |
| 1203 | tx_stats->tx_bytes += skb->len; |
| 1204 | u64_stats_update_end(&tx_stats->syncp); |
| 1205 | |
| 1206 | if (netif_rx(skb) == NET_RX_SUCCESS) { |
| 1207 | u64_stats_update_begin(&rx_stats->syncp); |
| 1208 | rx_stats->rx_packets++; |
| 1209 | rx_stats->rx_bytes += skb->len; |
| 1210 | u64_stats_update_end(&rx_stats->syncp); |
| 1211 | } else { |
| 1212 | skb->dev->stats.rx_dropped++; |
| 1213 | } |
| 1214 | } |
| 1215 | |
Stephen Hemminger | 4ad1693 | 2013-06-17 14:16:11 -0700 | [diff] [blame] | 1216 | static void vxlan_xmit_one(struct sk_buff *skb, struct net_device *dev, |
| 1217 | struct vxlan_rdst *rdst, bool did_rsc) |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1218 | { |
| 1219 | struct vxlan_dev *vxlan = netdev_priv(dev); |
| 1220 | struct rtable *rt; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1221 | const struct iphdr *old_iph; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1222 | struct flowi4 fl4; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1223 | __be32 dst; |
stephen hemminger | 7d836a7 | 2013-04-27 11:31:56 +0000 | [diff] [blame] | 1224 | __be16 src_port, dst_port; |
Stephen Hemminger | 234f5b7 | 2013-06-17 14:16:41 -0700 | [diff] [blame] | 1225 | u32 vni; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1226 | __be16 df = 0; |
| 1227 | __u8 tos, ttl; |
Pravin B Shelar | 0e6fbc5 | 2013-06-17 17:49:56 -0700 | [diff] [blame] | 1228 | int err; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1229 | |
stephen hemminger | 823aa87 | 2013-04-27 11:31:57 +0000 | [diff] [blame] | 1230 | dst_port = rdst->remote_port ? rdst->remote_port : vxlan->dst_port; |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 1231 | vni = rdst->remote_vni; |
| 1232 | dst = rdst->remote_ip; |
David Stevens | e4f67ad | 2012-11-20 02:50:14 +0000 | [diff] [blame] | 1233 | |
| 1234 | if (!dst) { |
| 1235 | if (did_rsc) { |
David Stevens | e4f67ad | 2012-11-20 02:50:14 +0000 | [diff] [blame] | 1236 | /* short-circuited back to local bridge */ |
Sridhar Samudrala | 9dcc71e | 2013-04-02 12:31:52 +0000 | [diff] [blame] | 1237 | vxlan_encap_bypass(skb, vxlan, vxlan); |
Stephen Hemminger | 4ad1693 | 2013-06-17 14:16:11 -0700 | [diff] [blame] | 1238 | return; |
David Stevens | e4f67ad | 2012-11-20 02:50:14 +0000 | [diff] [blame] | 1239 | } |
stephen hemminger | ef59feb | 2012-10-09 20:35:46 +0000 | [diff] [blame] | 1240 | goto drop; |
David Stevens | e4f67ad | 2012-11-20 02:50:14 +0000 | [diff] [blame] | 1241 | } |
stephen hemminger | ef59feb | 2012-10-09 20:35:46 +0000 | [diff] [blame] | 1242 | |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1243 | old_iph = ip_hdr(skb); |
| 1244 | |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1245 | ttl = vxlan->ttl; |
| 1246 | if (!ttl && IN_MULTICAST(ntohl(dst))) |
| 1247 | ttl = 1; |
| 1248 | |
| 1249 | tos = vxlan->tos; |
| 1250 | if (tos == 1) |
Pravin B Shelar | 206aaaf | 2013-03-25 14:49:53 +0000 | [diff] [blame] | 1251 | tos = ip_tunnel_get_dsfield(old_iph, skb); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1252 | |
Pravin B Shelar | 4956053 | 2013-08-19 11:23:17 -0700 | [diff] [blame] | 1253 | src_port = vxlan_src_port(vxlan->port_min, vxlan->port_max, skb); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1254 | |
stephen hemminger | ca78f18 | 2012-10-09 20:35:48 +0000 | [diff] [blame] | 1255 | memset(&fl4, 0, sizeof(fl4)); |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 1256 | fl4.flowi4_oif = rdst->remote_ifindex; |
stephen hemminger | ca78f18 | 2012-10-09 20:35:48 +0000 | [diff] [blame] | 1257 | fl4.flowi4_tos = RT_TOS(tos); |
| 1258 | fl4.daddr = dst; |
| 1259 | fl4.saddr = vxlan->saddr; |
| 1260 | |
| 1261 | rt = ip_route_output_key(dev_net(dev), &fl4); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1262 | if (IS_ERR(rt)) { |
| 1263 | netdev_dbg(dev, "no route to %pI4\n", &dst); |
| 1264 | dev->stats.tx_carrier_errors++; |
| 1265 | goto tx_error; |
| 1266 | } |
| 1267 | |
| 1268 | if (rt->dst.dev == dev) { |
| 1269 | netdev_dbg(dev, "circular route to %pI4\n", &dst); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1270 | dev->stats.collisions++; |
Pravin B Shelar | 4956053 | 2013-08-19 11:23:17 -0700 | [diff] [blame] | 1271 | goto rt_tx_error; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1272 | } |
| 1273 | |
Sridhar Samudrala | 9dcc71e | 2013-04-02 12:31:52 +0000 | [diff] [blame] | 1274 | /* Bypass encapsulation if the destination is local */ |
Mike Rapoport | ab09a6d | 2013-04-13 23:21:51 +0000 | [diff] [blame] | 1275 | if (rt->rt_flags & RTCF_LOCAL && |
| 1276 | !(rt->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST))) { |
Sridhar Samudrala | 9dcc71e | 2013-04-02 12:31:52 +0000 | [diff] [blame] | 1277 | struct vxlan_dev *dst_vxlan; |
| 1278 | |
| 1279 | ip_rt_put(rt); |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame] | 1280 | dst_vxlan = vxlan_find_vni(dev_net(dev), vni, dst_port); |
Sridhar Samudrala | 9dcc71e | 2013-04-02 12:31:52 +0000 | [diff] [blame] | 1281 | if (!dst_vxlan) |
| 1282 | goto tx_error; |
| 1283 | vxlan_encap_bypass(skb, vxlan, dst_vxlan); |
Stephen Hemminger | 4ad1693 | 2013-06-17 14:16:11 -0700 | [diff] [blame] | 1284 | return; |
Sridhar Samudrala | 9dcc71e | 2013-04-02 12:31:52 +0000 | [diff] [blame] | 1285 | } |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1286 | |
Pravin B Shelar | 0e6fbc5 | 2013-06-17 17:49:56 -0700 | [diff] [blame] | 1287 | tos = ip_tunnel_ecn_encap(tos, old_iph, skb); |
| 1288 | ttl = ttl ? : ip4_dst_hoplimit(&rt->dst); |
| 1289 | |
Pravin B Shelar | 4956053 | 2013-08-19 11:23:17 -0700 | [diff] [blame] | 1290 | err = vxlan_xmit_skb(dev_net(dev), vxlan->vn_sock, rt, skb, |
| 1291 | fl4.saddr, dst, tos, ttl, df, |
| 1292 | src_port, dst_port, htonl(vni << 8)); |
| 1293 | |
| 1294 | if (err < 0) |
| 1295 | goto rt_tx_error; |
Pravin B Shelar | 0e6fbc5 | 2013-06-17 17:49:56 -0700 | [diff] [blame] | 1296 | iptunnel_xmit_stats(err, &dev->stats, dev->tstats); |
| 1297 | |
Stephen Hemminger | 4ad1693 | 2013-06-17 14:16:11 -0700 | [diff] [blame] | 1298 | return; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1299 | |
| 1300 | drop: |
| 1301 | dev->stats.tx_dropped++; |
| 1302 | goto tx_free; |
| 1303 | |
Pravin B Shelar | 4956053 | 2013-08-19 11:23:17 -0700 | [diff] [blame] | 1304 | rt_tx_error: |
| 1305 | ip_rt_put(rt); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1306 | tx_error: |
| 1307 | dev->stats.tx_errors++; |
| 1308 | tx_free: |
| 1309 | dev_kfree_skb(skb); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1310 | } |
| 1311 | |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 1312 | /* Transmit local packets over Vxlan |
| 1313 | * |
| 1314 | * Outer IP header inherits ECN and DF from inner header. |
| 1315 | * Outer UDP destination is the VXLAN assigned port. |
| 1316 | * source port is based on hash of flow |
| 1317 | */ |
| 1318 | static netdev_tx_t vxlan_xmit(struct sk_buff *skb, struct net_device *dev) |
| 1319 | { |
| 1320 | struct vxlan_dev *vxlan = netdev_priv(dev); |
| 1321 | struct ethhdr *eth; |
| 1322 | bool did_rsc = false; |
Mike Rapoport | afbd8ba | 2013-06-25 16:01:51 +0300 | [diff] [blame] | 1323 | struct vxlan_rdst *rdst; |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 1324 | struct vxlan_fdb *f; |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 1325 | |
| 1326 | skb_reset_mac_header(skb); |
| 1327 | eth = eth_hdr(skb); |
| 1328 | |
| 1329 | if ((vxlan->flags & VXLAN_F_PROXY) && ntohs(eth->h_proto) == ETH_P_ARP) |
| 1330 | return arp_reduce(dev, skb); |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 1331 | |
| 1332 | f = vxlan_find_mac(vxlan, eth->h_dest); |
David Stevens | ae88408 | 2013-04-19 00:36:26 +0000 | [diff] [blame] | 1333 | did_rsc = false; |
| 1334 | |
| 1335 | if (f && (f->flags & NTF_ROUTER) && (vxlan->flags & VXLAN_F_RSC) && |
| 1336 | ntohs(eth->h_proto) == ETH_P_IP) { |
| 1337 | did_rsc = route_shortcircuit(dev, skb); |
| 1338 | if (did_rsc) |
| 1339 | f = vxlan_find_mac(vxlan, eth->h_dest); |
| 1340 | } |
| 1341 | |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 1342 | if (f == NULL) { |
Mike Rapoport | afbd8ba | 2013-06-25 16:01:51 +0300 | [diff] [blame] | 1343 | f = vxlan_find_mac(vxlan, all_zeros_mac); |
| 1344 | if (f == NULL) { |
| 1345 | if ((vxlan->flags & VXLAN_F_L2MISS) && |
| 1346 | !is_multicast_ether_addr(eth->h_dest)) |
| 1347 | vxlan_fdb_miss(vxlan, eth->h_dest); |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 1348 | |
Mike Rapoport | afbd8ba | 2013-06-25 16:01:51 +0300 | [diff] [blame] | 1349 | dev->stats.tx_dropped++; |
| 1350 | dev_kfree_skb(skb); |
| 1351 | return NETDEV_TX_OK; |
Stephen Hemminger | 3e61aa8 | 2013-06-17 14:16:12 -0700 | [diff] [blame] | 1352 | } |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 1353 | } |
| 1354 | |
Mike Rapoport | afbd8ba | 2013-06-25 16:01:51 +0300 | [diff] [blame] | 1355 | list_for_each_entry_rcu(rdst, &f->remotes, list) { |
| 1356 | struct sk_buff *skb1; |
| 1357 | |
| 1358 | skb1 = skb_clone(skb, GFP_ATOMIC); |
| 1359 | if (skb1) |
| 1360 | vxlan_xmit_one(skb1, dev, rdst, did_rsc); |
| 1361 | } |
| 1362 | |
| 1363 | dev_kfree_skb(skb); |
Stephen Hemminger | 4ad1693 | 2013-06-17 14:16:11 -0700 | [diff] [blame] | 1364 | return NETDEV_TX_OK; |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 1365 | } |
| 1366 | |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1367 | /* Walk the forwarding table and purge stale entries */ |
| 1368 | static void vxlan_cleanup(unsigned long arg) |
| 1369 | { |
| 1370 | struct vxlan_dev *vxlan = (struct vxlan_dev *) arg; |
| 1371 | unsigned long next_timer = jiffies + FDB_AGE_INTERVAL; |
| 1372 | unsigned int h; |
| 1373 | |
| 1374 | if (!netif_running(vxlan->dev)) |
| 1375 | return; |
| 1376 | |
| 1377 | spin_lock_bh(&vxlan->hash_lock); |
| 1378 | for (h = 0; h < FDB_HASH_SIZE; ++h) { |
| 1379 | struct hlist_node *p, *n; |
| 1380 | hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) { |
| 1381 | struct vxlan_fdb *f |
| 1382 | = container_of(p, struct vxlan_fdb, hlist); |
| 1383 | unsigned long timeout; |
| 1384 | |
stephen hemminger | 3c17286 | 2012-10-26 06:24:34 +0000 | [diff] [blame] | 1385 | if (f->state & NUD_PERMANENT) |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1386 | continue; |
| 1387 | |
| 1388 | timeout = f->used + vxlan->age_interval * HZ; |
| 1389 | if (time_before_eq(timeout, jiffies)) { |
| 1390 | netdev_dbg(vxlan->dev, |
| 1391 | "garbage collect %pM\n", |
| 1392 | f->eth_addr); |
| 1393 | f->state = NUD_STALE; |
| 1394 | vxlan_fdb_destroy(vxlan, f); |
| 1395 | } else if (time_before(timeout, next_timer)) |
| 1396 | next_timer = timeout; |
| 1397 | } |
| 1398 | } |
| 1399 | spin_unlock_bh(&vxlan->hash_lock); |
| 1400 | |
| 1401 | mod_timer(&vxlan->age_timer, next_timer); |
| 1402 | } |
| 1403 | |
Pravin B Shelar | 9c2e24e | 2013-08-19 11:22:48 -0700 | [diff] [blame] | 1404 | static void vxlan_vs_add_dev(struct vxlan_sock *vs, struct vxlan_dev *vxlan) |
| 1405 | { |
| 1406 | __u32 vni = vxlan->default_dst.remote_vni; |
| 1407 | |
| 1408 | vxlan->vn_sock = vs; |
| 1409 | hlist_add_head_rcu(&vxlan->hlist, vni_head(vs, vni)); |
| 1410 | } |
| 1411 | |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1412 | /* Setup stats when device is created */ |
| 1413 | static int vxlan_init(struct net_device *dev) |
| 1414 | { |
Stephen Hemminger | 1c51a91 | 2013-06-17 14:16:11 -0700 | [diff] [blame] | 1415 | struct vxlan_dev *vxlan = netdev_priv(dev); |
| 1416 | struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id); |
| 1417 | struct vxlan_sock *vs; |
Stephen Hemminger | 1c51a91 | 2013-06-17 14:16:11 -0700 | [diff] [blame] | 1418 | |
Pravin B Shelar | e817104 | 2013-03-25 14:49:46 +0000 | [diff] [blame] | 1419 | dev->tstats = alloc_percpu(struct pcpu_tstats); |
| 1420 | if (!dev->tstats) |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1421 | return -ENOMEM; |
| 1422 | |
Stephen Hemminger | 1c51a91 | 2013-06-17 14:16:11 -0700 | [diff] [blame] | 1423 | spin_lock(&vn->sock_lock); |
Pravin B Shelar | 9c2e24e | 2013-08-19 11:22:48 -0700 | [diff] [blame] | 1424 | vs = vxlan_find_sock(dev_net(dev), vxlan->dst_port); |
Stephen Hemminger | 1c51a91 | 2013-06-17 14:16:11 -0700 | [diff] [blame] | 1425 | if (vs) { |
| 1426 | /* If we have a socket with same port already, reuse it */ |
| 1427 | atomic_inc(&vs->refcnt); |
Pravin B Shelar | 9c2e24e | 2013-08-19 11:22:48 -0700 | [diff] [blame] | 1428 | vxlan_vs_add_dev(vs, vxlan); |
Stephen Hemminger | 1c51a91 | 2013-06-17 14:16:11 -0700 | [diff] [blame] | 1429 | } else { |
| 1430 | /* otherwise make new socket outside of RTNL */ |
| 1431 | dev_hold(dev); |
| 1432 | queue_work(vxlan_wq, &vxlan->sock_work); |
| 1433 | } |
| 1434 | spin_unlock(&vn->sock_lock); |
| 1435 | |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1436 | return 0; |
| 1437 | } |
| 1438 | |
Stephen Hemminger | ba609e9 | 2013-06-25 17:06:01 -0700 | [diff] [blame] | 1439 | static void vxlan_fdb_delete_default(struct vxlan_dev *vxlan) |
Mike Rapoport | afbd8ba | 2013-06-25 16:01:51 +0300 | [diff] [blame] | 1440 | { |
| 1441 | struct vxlan_fdb *f; |
| 1442 | |
| 1443 | spin_lock_bh(&vxlan->hash_lock); |
| 1444 | f = __vxlan_find_mac(vxlan, all_zeros_mac); |
| 1445 | if (f) |
| 1446 | vxlan_fdb_destroy(vxlan, f); |
| 1447 | spin_unlock_bh(&vxlan->hash_lock); |
| 1448 | } |
| 1449 | |
Stephen Hemminger | ebf4063 | 2013-06-17 14:16:11 -0700 | [diff] [blame] | 1450 | static void vxlan_uninit(struct net_device *dev) |
| 1451 | { |
| 1452 | struct vxlan_dev *vxlan = netdev_priv(dev); |
Stephen Hemminger | ebf4063 | 2013-06-17 14:16:11 -0700 | [diff] [blame] | 1453 | struct vxlan_sock *vs = vxlan->vn_sock; |
| 1454 | |
Stephen Hemminger | ba609e9 | 2013-06-25 17:06:01 -0700 | [diff] [blame] | 1455 | vxlan_fdb_delete_default(vxlan); |
Mike Rapoport | afbd8ba | 2013-06-25 16:01:51 +0300 | [diff] [blame] | 1456 | |
Stephen Hemminger | ebf4063 | 2013-06-17 14:16:11 -0700 | [diff] [blame] | 1457 | if (vs) |
Pravin B Shelar | 012a572 | 2013-08-19 11:23:07 -0700 | [diff] [blame] | 1458 | vxlan_sock_release(vs); |
Stephen Hemminger | ebf4063 | 2013-06-17 14:16:11 -0700 | [diff] [blame] | 1459 | free_percpu(dev->tstats); |
| 1460 | } |
| 1461 | |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1462 | /* Start ageing timer and join group when device is brought up */ |
| 1463 | static int vxlan_open(struct net_device *dev) |
| 1464 | { |
stephen hemminger | 3fc2de2 | 2013-07-18 08:40:15 -0700 | [diff] [blame] | 1465 | struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1466 | struct vxlan_dev *vxlan = netdev_priv(dev); |
Stephen Hemminger | 1c51a91 | 2013-06-17 14:16:11 -0700 | [diff] [blame] | 1467 | struct vxlan_sock *vs = vxlan->vn_sock; |
| 1468 | |
| 1469 | /* socket hasn't been created */ |
| 1470 | if (!vs) |
| 1471 | return -ENOTCONN; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1472 | |
stephen hemminger | 3fc2de2 | 2013-07-18 08:40:15 -0700 | [diff] [blame] | 1473 | if (IN_MULTICAST(ntohl(vxlan->default_dst.remote_ip)) && |
Cong Wang | 614334d | 2013-08-07 16:35:45 +0800 | [diff] [blame] | 1474 | vxlan_group_used(vn, vxlan->default_dst.remote_ip)) { |
Stephen Hemminger | 1c51a91 | 2013-06-17 14:16:11 -0700 | [diff] [blame] | 1475 | vxlan_sock_hold(vs); |
Stephen Hemminger | 7c47ced | 2013-06-17 14:16:10 -0700 | [diff] [blame] | 1476 | dev_hold(dev); |
stephen hemminger | 3fc2de2 | 2013-07-18 08:40:15 -0700 | [diff] [blame] | 1477 | queue_work(vxlan_wq, &vxlan->igmp_join); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1478 | } |
| 1479 | |
| 1480 | if (vxlan->age_interval) |
| 1481 | mod_timer(&vxlan->age_timer, jiffies + FDB_AGE_INTERVAL); |
| 1482 | |
| 1483 | return 0; |
| 1484 | } |
| 1485 | |
| 1486 | /* Purge the forwarding table */ |
| 1487 | static void vxlan_flush(struct vxlan_dev *vxlan) |
| 1488 | { |
Cong Wang | 31fec5a | 2013-05-27 22:35:52 +0000 | [diff] [blame] | 1489 | unsigned int h; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1490 | |
| 1491 | spin_lock_bh(&vxlan->hash_lock); |
| 1492 | for (h = 0; h < FDB_HASH_SIZE; ++h) { |
| 1493 | struct hlist_node *p, *n; |
| 1494 | hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) { |
| 1495 | struct vxlan_fdb *f |
| 1496 | = container_of(p, struct vxlan_fdb, hlist); |
Mike Rapoport | afbd8ba | 2013-06-25 16:01:51 +0300 | [diff] [blame] | 1497 | /* the all_zeros_mac entry is deleted at vxlan_uninit */ |
| 1498 | if (!is_zero_ether_addr(f->eth_addr)) |
| 1499 | vxlan_fdb_destroy(vxlan, f); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1500 | } |
| 1501 | } |
| 1502 | spin_unlock_bh(&vxlan->hash_lock); |
| 1503 | } |
| 1504 | |
| 1505 | /* Cleanup timer and forwarding table on shutdown */ |
| 1506 | static int vxlan_stop(struct net_device *dev) |
| 1507 | { |
stephen hemminger | 3fc2de2 | 2013-07-18 08:40:15 -0700 | [diff] [blame] | 1508 | struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1509 | struct vxlan_dev *vxlan = netdev_priv(dev); |
Stephen Hemminger | 1c51a91 | 2013-06-17 14:16:11 -0700 | [diff] [blame] | 1510 | struct vxlan_sock *vs = vxlan->vn_sock; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1511 | |
stephen hemminger | 3fc2de2 | 2013-07-18 08:40:15 -0700 | [diff] [blame] | 1512 | if (vs && IN_MULTICAST(ntohl(vxlan->default_dst.remote_ip)) && |
| 1513 | ! vxlan_group_used(vn, vxlan->default_dst.remote_ip)) { |
Stephen Hemminger | 1c51a91 | 2013-06-17 14:16:11 -0700 | [diff] [blame] | 1514 | vxlan_sock_hold(vs); |
Stephen Hemminger | 7c47ced | 2013-06-17 14:16:10 -0700 | [diff] [blame] | 1515 | dev_hold(dev); |
stephen hemminger | 3fc2de2 | 2013-07-18 08:40:15 -0700 | [diff] [blame] | 1516 | queue_work(vxlan_wq, &vxlan->igmp_leave); |
Stephen Hemminger | 7c47ced | 2013-06-17 14:16:10 -0700 | [diff] [blame] | 1517 | } |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1518 | |
| 1519 | del_timer_sync(&vxlan->age_timer); |
| 1520 | |
| 1521 | vxlan_flush(vxlan); |
| 1522 | |
| 1523 | return 0; |
| 1524 | } |
| 1525 | |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1526 | /* Stub, nothing needs to be done. */ |
| 1527 | static void vxlan_set_multicast_list(struct net_device *dev) |
| 1528 | { |
| 1529 | } |
| 1530 | |
| 1531 | static const struct net_device_ops vxlan_netdev_ops = { |
| 1532 | .ndo_init = vxlan_init, |
Stephen Hemminger | ebf4063 | 2013-06-17 14:16:11 -0700 | [diff] [blame] | 1533 | .ndo_uninit = vxlan_uninit, |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1534 | .ndo_open = vxlan_open, |
| 1535 | .ndo_stop = vxlan_stop, |
| 1536 | .ndo_start_xmit = vxlan_xmit, |
Pravin B Shelar | e817104 | 2013-03-25 14:49:46 +0000 | [diff] [blame] | 1537 | .ndo_get_stats64 = ip_tunnel_get_stats64, |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1538 | .ndo_set_rx_mode = vxlan_set_multicast_list, |
| 1539 | .ndo_change_mtu = eth_change_mtu, |
| 1540 | .ndo_validate_addr = eth_validate_addr, |
| 1541 | .ndo_set_mac_address = eth_mac_addr, |
| 1542 | .ndo_fdb_add = vxlan_fdb_add, |
| 1543 | .ndo_fdb_del = vxlan_fdb_delete, |
| 1544 | .ndo_fdb_dump = vxlan_fdb_dump, |
| 1545 | }; |
| 1546 | |
| 1547 | /* Info for udev, that this is a virtual tunnel endpoint */ |
| 1548 | static struct device_type vxlan_type = { |
| 1549 | .name = "vxlan", |
| 1550 | }; |
| 1551 | |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1552 | /* Initialize the device structure. */ |
| 1553 | static void vxlan_setup(struct net_device *dev) |
| 1554 | { |
| 1555 | struct vxlan_dev *vxlan = netdev_priv(dev); |
Cong Wang | 31fec5a | 2013-05-27 22:35:52 +0000 | [diff] [blame] | 1556 | unsigned int h; |
stephen hemminger | 05f47d6 | 2012-10-09 20:35:50 +0000 | [diff] [blame] | 1557 | int low, high; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1558 | |
| 1559 | eth_hw_addr_random(dev); |
| 1560 | ether_setup(dev); |
stephen hemminger | 2840bf2 | 2012-10-09 20:35:51 +0000 | [diff] [blame] | 1561 | dev->hard_header_len = ETH_HLEN + VXLAN_HEADROOM; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1562 | |
| 1563 | dev->netdev_ops = &vxlan_netdev_ops; |
Stephen Hemminger | ebf4063 | 2013-06-17 14:16:11 -0700 | [diff] [blame] | 1564 | dev->destructor = free_netdev; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1565 | SET_NETDEV_DEVTYPE(dev, &vxlan_type); |
| 1566 | |
| 1567 | dev->tx_queue_len = 0; |
| 1568 | dev->features |= NETIF_F_LLTX; |
| 1569 | dev->features |= NETIF_F_NETNS_LOCAL; |
Joseph Gasparakis | d6727fe | 2012-12-07 14:14:16 +0000 | [diff] [blame] | 1570 | dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM; |
Joseph Gasparakis | 0afb166 | 2012-12-07 14:14:18 +0000 | [diff] [blame] | 1571 | dev->features |= NETIF_F_RXCSUM; |
Pravin B Shelar | 05c0db0 | 2013-03-07 13:22:36 +0000 | [diff] [blame] | 1572 | dev->features |= NETIF_F_GSO_SOFTWARE; |
Joseph Gasparakis | 0afb166 | 2012-12-07 14:14:18 +0000 | [diff] [blame] | 1573 | |
Pravin B Shelar | 1eaa817 | 2013-08-19 11:23:29 -0700 | [diff] [blame^] | 1574 | dev->vlan_features = dev->features; |
| 1575 | dev->features |= NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_STAG_TX; |
Joseph Gasparakis | 0afb166 | 2012-12-07 14:14:18 +0000 | [diff] [blame] | 1576 | dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_RXCSUM; |
Pravin B Shelar | 05c0db0 | 2013-03-07 13:22:36 +0000 | [diff] [blame] | 1577 | dev->hw_features |= NETIF_F_GSO_SOFTWARE; |
Pravin B Shelar | 1eaa817 | 2013-08-19 11:23:29 -0700 | [diff] [blame^] | 1578 | dev->hw_features |= NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_STAG_TX; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1579 | dev->priv_flags &= ~IFF_XMIT_DST_RELEASE; |
stephen hemminger | 6602d00 | 2012-12-31 12:00:21 +0000 | [diff] [blame] | 1580 | dev->priv_flags |= IFF_LIVE_ADDR_CHANGE; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1581 | |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame] | 1582 | INIT_LIST_HEAD(&vxlan->next); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1583 | spin_lock_init(&vxlan->hash_lock); |
stephen hemminger | 3fc2de2 | 2013-07-18 08:40:15 -0700 | [diff] [blame] | 1584 | INIT_WORK(&vxlan->igmp_join, vxlan_igmp_join); |
| 1585 | INIT_WORK(&vxlan->igmp_leave, vxlan_igmp_leave); |
Stephen Hemminger | 1c51a91 | 2013-06-17 14:16:11 -0700 | [diff] [blame] | 1586 | INIT_WORK(&vxlan->sock_work, vxlan_sock_work); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1587 | |
| 1588 | init_timer_deferrable(&vxlan->age_timer); |
| 1589 | vxlan->age_timer.function = vxlan_cleanup; |
| 1590 | vxlan->age_timer.data = (unsigned long) vxlan; |
| 1591 | |
stephen hemminger | 05f47d6 | 2012-10-09 20:35:50 +0000 | [diff] [blame] | 1592 | inet_get_local_port_range(&low, &high); |
| 1593 | vxlan->port_min = low; |
| 1594 | vxlan->port_max = high; |
stephen hemminger | 823aa87 | 2013-04-27 11:31:57 +0000 | [diff] [blame] | 1595 | vxlan->dst_port = htons(vxlan_port); |
stephen hemminger | 05f47d6 | 2012-10-09 20:35:50 +0000 | [diff] [blame] | 1596 | |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1597 | vxlan->dev = dev; |
| 1598 | |
| 1599 | for (h = 0; h < FDB_HASH_SIZE; ++h) |
| 1600 | INIT_HLIST_HEAD(&vxlan->fdb_head[h]); |
| 1601 | } |
| 1602 | |
| 1603 | static const struct nla_policy vxlan_policy[IFLA_VXLAN_MAX + 1] = { |
| 1604 | [IFLA_VXLAN_ID] = { .type = NLA_U32 }, |
stephen hemminger | 5d174dd | 2013-04-27 11:31:55 +0000 | [diff] [blame] | 1605 | [IFLA_VXLAN_GROUP] = { .len = FIELD_SIZEOF(struct iphdr, daddr) }, |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1606 | [IFLA_VXLAN_LINK] = { .type = NLA_U32 }, |
| 1607 | [IFLA_VXLAN_LOCAL] = { .len = FIELD_SIZEOF(struct iphdr, saddr) }, |
| 1608 | [IFLA_VXLAN_TOS] = { .type = NLA_U8 }, |
| 1609 | [IFLA_VXLAN_TTL] = { .type = NLA_U8 }, |
| 1610 | [IFLA_VXLAN_LEARNING] = { .type = NLA_U8 }, |
| 1611 | [IFLA_VXLAN_AGEING] = { .type = NLA_U32 }, |
| 1612 | [IFLA_VXLAN_LIMIT] = { .type = NLA_U32 }, |
stephen hemminger | 05f47d6 | 2012-10-09 20:35:50 +0000 | [diff] [blame] | 1613 | [IFLA_VXLAN_PORT_RANGE] = { .len = sizeof(struct ifla_vxlan_port_range) }, |
David Stevens | e4f67ad | 2012-11-20 02:50:14 +0000 | [diff] [blame] | 1614 | [IFLA_VXLAN_PROXY] = { .type = NLA_U8 }, |
| 1615 | [IFLA_VXLAN_RSC] = { .type = NLA_U8 }, |
| 1616 | [IFLA_VXLAN_L2MISS] = { .type = NLA_U8 }, |
| 1617 | [IFLA_VXLAN_L3MISS] = { .type = NLA_U8 }, |
stephen hemminger | 823aa87 | 2013-04-27 11:31:57 +0000 | [diff] [blame] | 1618 | [IFLA_VXLAN_PORT] = { .type = NLA_U16 }, |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1619 | }; |
| 1620 | |
| 1621 | static int vxlan_validate(struct nlattr *tb[], struct nlattr *data[]) |
| 1622 | { |
| 1623 | if (tb[IFLA_ADDRESS]) { |
| 1624 | if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) { |
| 1625 | pr_debug("invalid link address (not ethernet)\n"); |
| 1626 | return -EINVAL; |
| 1627 | } |
| 1628 | |
| 1629 | if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) { |
| 1630 | pr_debug("invalid all zero ethernet address\n"); |
| 1631 | return -EADDRNOTAVAIL; |
| 1632 | } |
| 1633 | } |
| 1634 | |
| 1635 | if (!data) |
| 1636 | return -EINVAL; |
| 1637 | |
| 1638 | if (data[IFLA_VXLAN_ID]) { |
| 1639 | __u32 id = nla_get_u32(data[IFLA_VXLAN_ID]); |
| 1640 | if (id >= VXLAN_VID_MASK) |
| 1641 | return -ERANGE; |
| 1642 | } |
| 1643 | |
stephen hemminger | 05f47d6 | 2012-10-09 20:35:50 +0000 | [diff] [blame] | 1644 | if (data[IFLA_VXLAN_PORT_RANGE]) { |
| 1645 | const struct ifla_vxlan_port_range *p |
| 1646 | = nla_data(data[IFLA_VXLAN_PORT_RANGE]); |
| 1647 | |
| 1648 | if (ntohs(p->high) < ntohs(p->low)) { |
| 1649 | pr_debug("port range %u .. %u not valid\n", |
| 1650 | ntohs(p->low), ntohs(p->high)); |
| 1651 | return -EINVAL; |
| 1652 | } |
| 1653 | } |
| 1654 | |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1655 | return 0; |
| 1656 | } |
| 1657 | |
Yan Burman | 1b13c97 | 2013-01-29 23:43:07 +0000 | [diff] [blame] | 1658 | static void vxlan_get_drvinfo(struct net_device *netdev, |
| 1659 | struct ethtool_drvinfo *drvinfo) |
| 1660 | { |
| 1661 | strlcpy(drvinfo->version, VXLAN_VERSION, sizeof(drvinfo->version)); |
| 1662 | strlcpy(drvinfo->driver, "vxlan", sizeof(drvinfo->driver)); |
| 1663 | } |
| 1664 | |
| 1665 | static const struct ethtool_ops vxlan_ethtool_ops = { |
| 1666 | .get_drvinfo = vxlan_get_drvinfo, |
| 1667 | .get_link = ethtool_op_get_link, |
| 1668 | }; |
| 1669 | |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame] | 1670 | static void vxlan_del_work(struct work_struct *work) |
| 1671 | { |
| 1672 | struct vxlan_sock *vs = container_of(work, struct vxlan_sock, del_work); |
| 1673 | |
| 1674 | sk_release_kernel(vs->sock->sk); |
| 1675 | kfree_rcu(vs, rcu); |
| 1676 | } |
| 1677 | |
Pravin B Shelar | 5cfccc5 | 2013-08-19 11:23:02 -0700 | [diff] [blame] | 1678 | static struct vxlan_sock *vxlan_socket_create(struct net *net, __be16 port, |
Pravin B Shelar | 012a572 | 2013-08-19 11:23:07 -0700 | [diff] [blame] | 1679 | vxlan_rcv_t *rcv, void *data) |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame] | 1680 | { |
Pravin B Shelar | 9c2e24e | 2013-08-19 11:22:48 -0700 | [diff] [blame] | 1681 | struct vxlan_net *vn = net_generic(net, vxlan_net_id); |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame] | 1682 | struct vxlan_sock *vs; |
| 1683 | struct sock *sk; |
| 1684 | struct sockaddr_in vxlan_addr = { |
| 1685 | .sin_family = AF_INET, |
| 1686 | .sin_addr.s_addr = htonl(INADDR_ANY), |
Stephen Hemminger | bb3fd68 | 2013-06-17 14:16:40 -0700 | [diff] [blame] | 1687 | .sin_port = port, |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame] | 1688 | }; |
| 1689 | int rc; |
Cong Wang | 31fec5a | 2013-05-27 22:35:52 +0000 | [diff] [blame] | 1690 | unsigned int h; |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame] | 1691 | |
| 1692 | vs = kmalloc(sizeof(*vs), GFP_KERNEL); |
Pravin B Shelar | 9c2e24e | 2013-08-19 11:22:48 -0700 | [diff] [blame] | 1693 | if (!vs) { |
| 1694 | pr_debug("memory alocation failure\n"); |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame] | 1695 | return ERR_PTR(-ENOMEM); |
Pravin B Shelar | 9c2e24e | 2013-08-19 11:22:48 -0700 | [diff] [blame] | 1696 | } |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame] | 1697 | |
| 1698 | for (h = 0; h < VNI_HASH_SIZE; ++h) |
| 1699 | INIT_HLIST_HEAD(&vs->vni_list[h]); |
| 1700 | |
| 1701 | INIT_WORK(&vs->del_work, vxlan_del_work); |
| 1702 | |
| 1703 | /* Create UDP socket for encapsulation receive. */ |
| 1704 | rc = sock_create_kern(AF_INET, SOCK_DGRAM, IPPROTO_UDP, &vs->sock); |
| 1705 | if (rc < 0) { |
| 1706 | pr_debug("UDP socket create failed\n"); |
| 1707 | kfree(vs); |
| 1708 | return ERR_PTR(rc); |
| 1709 | } |
| 1710 | |
| 1711 | /* Put in proper namespace */ |
| 1712 | sk = vs->sock->sk; |
| 1713 | sk_change_net(sk, net); |
| 1714 | |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame] | 1715 | rc = kernel_bind(vs->sock, (struct sockaddr *) &vxlan_addr, |
| 1716 | sizeof(vxlan_addr)); |
| 1717 | if (rc < 0) { |
| 1718 | pr_debug("bind for UDP socket %pI4:%u (%d)\n", |
| 1719 | &vxlan_addr.sin_addr, ntohs(vxlan_addr.sin_port), rc); |
| 1720 | sk_release_kernel(sk); |
| 1721 | kfree(vs); |
| 1722 | return ERR_PTR(rc); |
| 1723 | } |
Pravin B Shelar | 9c2e24e | 2013-08-19 11:22:48 -0700 | [diff] [blame] | 1724 | atomic_set(&vs->refcnt, 1); |
Pravin B Shelar | 5cfccc5 | 2013-08-19 11:23:02 -0700 | [diff] [blame] | 1725 | vs->rcv = rcv; |
Pravin B Shelar | 012a572 | 2013-08-19 11:23:07 -0700 | [diff] [blame] | 1726 | vs->data = data; |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame] | 1727 | |
| 1728 | /* Disable multicast loopback */ |
| 1729 | inet_sk(sk)->mc_loop = 0; |
Pravin B Shelar | 9c2e24e | 2013-08-19 11:22:48 -0700 | [diff] [blame] | 1730 | spin_lock(&vn->sock_lock); |
| 1731 | hlist_add_head_rcu(&vs->hlist, vs_head(net, port)); |
| 1732 | spin_unlock(&vn->sock_lock); |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame] | 1733 | |
| 1734 | /* Mark socket as an encapsulation socket. */ |
| 1735 | udp_sk(sk)->encap_type = 1; |
| 1736 | udp_sk(sk)->encap_rcv = vxlan_udp_encap_recv; |
| 1737 | udp_encap_enable(); |
Pravin B Shelar | 9c2e24e | 2013-08-19 11:22:48 -0700 | [diff] [blame] | 1738 | return vs; |
| 1739 | } |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame] | 1740 | |
Pravin B Shelar | 012a572 | 2013-08-19 11:23:07 -0700 | [diff] [blame] | 1741 | struct vxlan_sock *vxlan_sock_add(struct net *net, __be16 port, |
| 1742 | vxlan_rcv_t *rcv, void *data, |
| 1743 | bool no_share) |
Pravin B Shelar | 9c2e24e | 2013-08-19 11:22:48 -0700 | [diff] [blame] | 1744 | { |
| 1745 | struct vxlan_net *vn = net_generic(net, vxlan_net_id); |
| 1746 | struct vxlan_sock *vs; |
| 1747 | |
Pravin B Shelar | 012a572 | 2013-08-19 11:23:07 -0700 | [diff] [blame] | 1748 | vs = vxlan_socket_create(net, port, rcv, data); |
Pravin B Shelar | 9c2e24e | 2013-08-19 11:22:48 -0700 | [diff] [blame] | 1749 | if (!IS_ERR(vs)) |
| 1750 | return vs; |
| 1751 | |
Pravin B Shelar | 012a572 | 2013-08-19 11:23:07 -0700 | [diff] [blame] | 1752 | if (no_share) /* Return error if sharing is not allowed. */ |
| 1753 | return vs; |
| 1754 | |
Pravin B Shelar | 9c2e24e | 2013-08-19 11:22:48 -0700 | [diff] [blame] | 1755 | spin_lock(&vn->sock_lock); |
| 1756 | vs = vxlan_find_sock(net, port); |
Pravin B Shelar | 5cfccc5 | 2013-08-19 11:23:02 -0700 | [diff] [blame] | 1757 | if (vs) { |
| 1758 | if (vs->rcv == rcv) |
| 1759 | atomic_inc(&vs->refcnt); |
| 1760 | else |
| 1761 | vs = ERR_PTR(-EBUSY); |
| 1762 | } |
| 1763 | spin_unlock(&vn->sock_lock); |
| 1764 | |
| 1765 | if (!vs) |
Pravin B Shelar | 9c2e24e | 2013-08-19 11:22:48 -0700 | [diff] [blame] | 1766 | vs = ERR_PTR(-EINVAL); |
| 1767 | |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame] | 1768 | return vs; |
| 1769 | } |
Pravin B Shelar | 012a572 | 2013-08-19 11:23:07 -0700 | [diff] [blame] | 1770 | EXPORT_SYMBOL_GPL(vxlan_sock_add); |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame] | 1771 | |
Stephen Hemminger | 1c51a91 | 2013-06-17 14:16:11 -0700 | [diff] [blame] | 1772 | /* Scheduled at device creation to bind to a socket */ |
| 1773 | static void vxlan_sock_work(struct work_struct *work) |
| 1774 | { |
Pravin B Shelar | 9c2e24e | 2013-08-19 11:22:48 -0700 | [diff] [blame] | 1775 | struct vxlan_dev *vxlan = container_of(work, struct vxlan_dev, sock_work); |
| 1776 | struct net *net = dev_net(vxlan->dev); |
Stephen Hemminger | 1c51a91 | 2013-06-17 14:16:11 -0700 | [diff] [blame] | 1777 | struct vxlan_net *vn = net_generic(net, vxlan_net_id); |
Pravin B Shelar | 9c2e24e | 2013-08-19 11:22:48 -0700 | [diff] [blame] | 1778 | __be16 port = vxlan->dst_port; |
| 1779 | struct vxlan_sock *nvs; |
Stephen Hemminger | 1c51a91 | 2013-06-17 14:16:11 -0700 | [diff] [blame] | 1780 | |
Pravin B Shelar | 012a572 | 2013-08-19 11:23:07 -0700 | [diff] [blame] | 1781 | nvs = vxlan_sock_add(net, port, vxlan_rcv, NULL, false); |
Stephen Hemminger | 1c51a91 | 2013-06-17 14:16:11 -0700 | [diff] [blame] | 1782 | spin_lock(&vn->sock_lock); |
Pravin B Shelar | 9c2e24e | 2013-08-19 11:22:48 -0700 | [diff] [blame] | 1783 | if (!IS_ERR(nvs)) |
| 1784 | vxlan_vs_add_dev(nvs, vxlan); |
| 1785 | spin_unlock(&vn->sock_lock); |
Stephen Hemminger | 1c51a91 | 2013-06-17 14:16:11 -0700 | [diff] [blame] | 1786 | |
Pravin B Shelar | 9c2e24e | 2013-08-19 11:22:48 -0700 | [diff] [blame] | 1787 | dev_put(vxlan->dev); |
Stephen Hemminger | 1c51a91 | 2013-06-17 14:16:11 -0700 | [diff] [blame] | 1788 | } |
| 1789 | |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1790 | static int vxlan_newlink(struct net *net, struct net_device *dev, |
| 1791 | struct nlattr *tb[], struct nlattr *data[]) |
| 1792 | { |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame] | 1793 | struct vxlan_net *vn = net_generic(net, vxlan_net_id); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1794 | struct vxlan_dev *vxlan = netdev_priv(dev); |
Atzm Watanabe | c7995c4 | 2013-04-16 02:50:52 +0000 | [diff] [blame] | 1795 | struct vxlan_rdst *dst = &vxlan->default_dst; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1796 | __u32 vni; |
| 1797 | int err; |
| 1798 | |
| 1799 | if (!data[IFLA_VXLAN_ID]) |
| 1800 | return -EINVAL; |
| 1801 | |
| 1802 | vni = nla_get_u32(data[IFLA_VXLAN_ID]); |
Atzm Watanabe | c7995c4 | 2013-04-16 02:50:52 +0000 | [diff] [blame] | 1803 | dst->remote_vni = vni; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1804 | |
stephen hemminger | 5d174dd | 2013-04-27 11:31:55 +0000 | [diff] [blame] | 1805 | if (data[IFLA_VXLAN_GROUP]) |
| 1806 | dst->remote_ip = nla_get_be32(data[IFLA_VXLAN_GROUP]); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1807 | |
| 1808 | if (data[IFLA_VXLAN_LOCAL]) |
| 1809 | vxlan->saddr = nla_get_be32(data[IFLA_VXLAN_LOCAL]); |
| 1810 | |
stephen hemminger | 34e02aa | 2012-10-09 20:35:53 +0000 | [diff] [blame] | 1811 | if (data[IFLA_VXLAN_LINK] && |
Atzm Watanabe | c7995c4 | 2013-04-16 02:50:52 +0000 | [diff] [blame] | 1812 | (dst->remote_ifindex = nla_get_u32(data[IFLA_VXLAN_LINK]))) { |
stephen hemminger | 34e02aa | 2012-10-09 20:35:53 +0000 | [diff] [blame] | 1813 | struct net_device *lowerdev |
Atzm Watanabe | c7995c4 | 2013-04-16 02:50:52 +0000 | [diff] [blame] | 1814 | = __dev_get_by_index(net, dst->remote_ifindex); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1815 | |
stephen hemminger | 34e02aa | 2012-10-09 20:35:53 +0000 | [diff] [blame] | 1816 | if (!lowerdev) { |
Atzm Watanabe | c7995c4 | 2013-04-16 02:50:52 +0000 | [diff] [blame] | 1817 | pr_info("ifindex %d does not exist\n", dst->remote_ifindex); |
stephen hemminger | 34e02aa | 2012-10-09 20:35:53 +0000 | [diff] [blame] | 1818 | return -ENODEV; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1819 | } |
stephen hemminger | 34e02aa | 2012-10-09 20:35:53 +0000 | [diff] [blame] | 1820 | |
| 1821 | if (!tb[IFLA_MTU]) |
| 1822 | dev->mtu = lowerdev->mtu - VXLAN_HEADROOM; |
Alexander Duyck | 1ba56fb | 2012-11-13 13:10:59 +0000 | [diff] [blame] | 1823 | |
| 1824 | /* update header length based on lower device */ |
| 1825 | dev->hard_header_len = lowerdev->hard_header_len + |
| 1826 | VXLAN_HEADROOM; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1827 | } |
| 1828 | |
| 1829 | if (data[IFLA_VXLAN_TOS]) |
| 1830 | vxlan->tos = nla_get_u8(data[IFLA_VXLAN_TOS]); |
| 1831 | |
Vincent Bernat | afb9718 | 2012-10-30 10:27:16 +0000 | [diff] [blame] | 1832 | if (data[IFLA_VXLAN_TTL]) |
| 1833 | vxlan->ttl = nla_get_u8(data[IFLA_VXLAN_TTL]); |
| 1834 | |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1835 | if (!data[IFLA_VXLAN_LEARNING] || nla_get_u8(data[IFLA_VXLAN_LEARNING])) |
David Stevens | e4f67ad | 2012-11-20 02:50:14 +0000 | [diff] [blame] | 1836 | vxlan->flags |= VXLAN_F_LEARN; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1837 | |
| 1838 | if (data[IFLA_VXLAN_AGEING]) |
| 1839 | vxlan->age_interval = nla_get_u32(data[IFLA_VXLAN_AGEING]); |
| 1840 | else |
| 1841 | vxlan->age_interval = FDB_AGE_DEFAULT; |
| 1842 | |
David Stevens | e4f67ad | 2012-11-20 02:50:14 +0000 | [diff] [blame] | 1843 | if (data[IFLA_VXLAN_PROXY] && nla_get_u8(data[IFLA_VXLAN_PROXY])) |
| 1844 | vxlan->flags |= VXLAN_F_PROXY; |
| 1845 | |
| 1846 | if (data[IFLA_VXLAN_RSC] && nla_get_u8(data[IFLA_VXLAN_RSC])) |
| 1847 | vxlan->flags |= VXLAN_F_RSC; |
| 1848 | |
| 1849 | if (data[IFLA_VXLAN_L2MISS] && nla_get_u8(data[IFLA_VXLAN_L2MISS])) |
| 1850 | vxlan->flags |= VXLAN_F_L2MISS; |
| 1851 | |
| 1852 | if (data[IFLA_VXLAN_L3MISS] && nla_get_u8(data[IFLA_VXLAN_L3MISS])) |
| 1853 | vxlan->flags |= VXLAN_F_L3MISS; |
| 1854 | |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1855 | if (data[IFLA_VXLAN_LIMIT]) |
| 1856 | vxlan->addrmax = nla_get_u32(data[IFLA_VXLAN_LIMIT]); |
| 1857 | |
stephen hemminger | 05f47d6 | 2012-10-09 20:35:50 +0000 | [diff] [blame] | 1858 | if (data[IFLA_VXLAN_PORT_RANGE]) { |
| 1859 | const struct ifla_vxlan_port_range *p |
| 1860 | = nla_data(data[IFLA_VXLAN_PORT_RANGE]); |
| 1861 | vxlan->port_min = ntohs(p->low); |
| 1862 | vxlan->port_max = ntohs(p->high); |
| 1863 | } |
| 1864 | |
stephen hemminger | 823aa87 | 2013-04-27 11:31:57 +0000 | [diff] [blame] | 1865 | if (data[IFLA_VXLAN_PORT]) |
| 1866 | vxlan->dst_port = nla_get_be16(data[IFLA_VXLAN_PORT]); |
| 1867 | |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame] | 1868 | if (vxlan_find_vni(net, vni, vxlan->dst_port)) { |
| 1869 | pr_info("duplicate VNI %u\n", vni); |
| 1870 | return -EEXIST; |
| 1871 | } |
| 1872 | |
Yan Burman | 1b13c97 | 2013-01-29 23:43:07 +0000 | [diff] [blame] | 1873 | SET_ETHTOOL_OPS(dev, &vxlan_ethtool_ops); |
| 1874 | |
Mike Rapoport | afbd8ba | 2013-06-25 16:01:51 +0300 | [diff] [blame] | 1875 | /* create an fdb entry for default destination */ |
| 1876 | err = vxlan_fdb_create(vxlan, all_zeros_mac, |
| 1877 | vxlan->default_dst.remote_ip, |
| 1878 | NUD_REACHABLE|NUD_PERMANENT, |
| 1879 | NLM_F_EXCL|NLM_F_CREATE, |
| 1880 | vxlan->dst_port, vxlan->default_dst.remote_vni, |
| 1881 | vxlan->default_dst.remote_ifindex, NTF_SELF); |
Stephen Hemminger | 1c51a91 | 2013-06-17 14:16:11 -0700 | [diff] [blame] | 1882 | if (err) |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame] | 1883 | return err; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1884 | |
Mike Rapoport | afbd8ba | 2013-06-25 16:01:51 +0300 | [diff] [blame] | 1885 | err = register_netdevice(dev); |
| 1886 | if (err) { |
Stephen Hemminger | ba609e9 | 2013-06-25 17:06:01 -0700 | [diff] [blame] | 1887 | vxlan_fdb_delete_default(vxlan); |
Mike Rapoport | afbd8ba | 2013-06-25 16:01:51 +0300 | [diff] [blame] | 1888 | return err; |
| 1889 | } |
| 1890 | |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame] | 1891 | list_add(&vxlan->next, &vn->vxlan_list); |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame] | 1892 | |
| 1893 | return 0; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1894 | } |
| 1895 | |
| 1896 | static void vxlan_dellink(struct net_device *dev, struct list_head *head) |
| 1897 | { |
stephen hemminger | fe5c356 | 2013-07-13 10:18:18 -0700 | [diff] [blame] | 1898 | struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1899 | struct vxlan_dev *vxlan = netdev_priv(dev); |
| 1900 | |
stephen hemminger | fe5c356 | 2013-07-13 10:18:18 -0700 | [diff] [blame] | 1901 | spin_lock(&vn->sock_lock); |
Pravin B Shelar | 9c2e24e | 2013-08-19 11:22:48 -0700 | [diff] [blame] | 1902 | if (!hlist_unhashed(&vxlan->hlist)) |
| 1903 | hlist_del_rcu(&vxlan->hlist); |
stephen hemminger | fe5c356 | 2013-07-13 10:18:18 -0700 | [diff] [blame] | 1904 | spin_unlock(&vn->sock_lock); |
| 1905 | |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame] | 1906 | list_del(&vxlan->next); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1907 | unregister_netdevice_queue(dev, head); |
| 1908 | } |
| 1909 | |
| 1910 | static size_t vxlan_get_size(const struct net_device *dev) |
| 1911 | { |
| 1912 | |
| 1913 | return nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_ID */ |
stephen hemminger | 5d174dd | 2013-04-27 11:31:55 +0000 | [diff] [blame] | 1914 | nla_total_size(sizeof(__be32)) +/* IFLA_VXLAN_GROUP */ |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1915 | nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LINK */ |
| 1916 | nla_total_size(sizeof(__be32))+ /* IFLA_VXLAN_LOCAL */ |
| 1917 | nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TTL */ |
| 1918 | nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TOS */ |
| 1919 | nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_LEARNING */ |
David Stevens | e4f67ad | 2012-11-20 02:50:14 +0000 | [diff] [blame] | 1920 | nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_PROXY */ |
| 1921 | nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_RSC */ |
| 1922 | nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L2MISS */ |
| 1923 | nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L3MISS */ |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1924 | nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_AGEING */ |
| 1925 | nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LIMIT */ |
stephen hemminger | 05f47d6 | 2012-10-09 20:35:50 +0000 | [diff] [blame] | 1926 | nla_total_size(sizeof(struct ifla_vxlan_port_range)) + |
stephen hemminger | 823aa87 | 2013-04-27 11:31:57 +0000 | [diff] [blame] | 1927 | nla_total_size(sizeof(__be16))+ /* IFLA_VXLAN_PORT */ |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1928 | 0; |
| 1929 | } |
| 1930 | |
| 1931 | static int vxlan_fill_info(struct sk_buff *skb, const struct net_device *dev) |
| 1932 | { |
| 1933 | const struct vxlan_dev *vxlan = netdev_priv(dev); |
Atzm Watanabe | c7995c4 | 2013-04-16 02:50:52 +0000 | [diff] [blame] | 1934 | const struct vxlan_rdst *dst = &vxlan->default_dst; |
stephen hemminger | 05f47d6 | 2012-10-09 20:35:50 +0000 | [diff] [blame] | 1935 | struct ifla_vxlan_port_range ports = { |
| 1936 | .low = htons(vxlan->port_min), |
| 1937 | .high = htons(vxlan->port_max), |
| 1938 | }; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1939 | |
Atzm Watanabe | c7995c4 | 2013-04-16 02:50:52 +0000 | [diff] [blame] | 1940 | if (nla_put_u32(skb, IFLA_VXLAN_ID, dst->remote_vni)) |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1941 | goto nla_put_failure; |
| 1942 | |
stephen hemminger | 5d174dd | 2013-04-27 11:31:55 +0000 | [diff] [blame] | 1943 | if (dst->remote_ip && nla_put_be32(skb, IFLA_VXLAN_GROUP, dst->remote_ip)) |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1944 | goto nla_put_failure; |
| 1945 | |
Atzm Watanabe | c7995c4 | 2013-04-16 02:50:52 +0000 | [diff] [blame] | 1946 | if (dst->remote_ifindex && nla_put_u32(skb, IFLA_VXLAN_LINK, dst->remote_ifindex)) |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1947 | goto nla_put_failure; |
| 1948 | |
Stephen Hemminger | 7c41c42 | 2012-10-08 14:55:30 -0700 | [diff] [blame] | 1949 | if (vxlan->saddr && nla_put_be32(skb, IFLA_VXLAN_LOCAL, vxlan->saddr)) |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1950 | goto nla_put_failure; |
| 1951 | |
| 1952 | if (nla_put_u8(skb, IFLA_VXLAN_TTL, vxlan->ttl) || |
| 1953 | nla_put_u8(skb, IFLA_VXLAN_TOS, vxlan->tos) || |
David Stevens | e4f67ad | 2012-11-20 02:50:14 +0000 | [diff] [blame] | 1954 | nla_put_u8(skb, IFLA_VXLAN_LEARNING, |
| 1955 | !!(vxlan->flags & VXLAN_F_LEARN)) || |
| 1956 | nla_put_u8(skb, IFLA_VXLAN_PROXY, |
| 1957 | !!(vxlan->flags & VXLAN_F_PROXY)) || |
| 1958 | nla_put_u8(skb, IFLA_VXLAN_RSC, !!(vxlan->flags & VXLAN_F_RSC)) || |
| 1959 | nla_put_u8(skb, IFLA_VXLAN_L2MISS, |
| 1960 | !!(vxlan->flags & VXLAN_F_L2MISS)) || |
| 1961 | nla_put_u8(skb, IFLA_VXLAN_L3MISS, |
| 1962 | !!(vxlan->flags & VXLAN_F_L3MISS)) || |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1963 | nla_put_u32(skb, IFLA_VXLAN_AGEING, vxlan->age_interval) || |
stephen hemminger | 823aa87 | 2013-04-27 11:31:57 +0000 | [diff] [blame] | 1964 | nla_put_u32(skb, IFLA_VXLAN_LIMIT, vxlan->addrmax) || |
| 1965 | nla_put_be16(skb, IFLA_VXLAN_PORT, vxlan->dst_port)) |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1966 | goto nla_put_failure; |
| 1967 | |
stephen hemminger | 05f47d6 | 2012-10-09 20:35:50 +0000 | [diff] [blame] | 1968 | if (nla_put(skb, IFLA_VXLAN_PORT_RANGE, sizeof(ports), &ports)) |
| 1969 | goto nla_put_failure; |
| 1970 | |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1971 | return 0; |
| 1972 | |
| 1973 | nla_put_failure: |
| 1974 | return -EMSGSIZE; |
| 1975 | } |
| 1976 | |
| 1977 | static struct rtnl_link_ops vxlan_link_ops __read_mostly = { |
| 1978 | .kind = "vxlan", |
| 1979 | .maxtype = IFLA_VXLAN_MAX, |
| 1980 | .policy = vxlan_policy, |
| 1981 | .priv_size = sizeof(struct vxlan_dev), |
| 1982 | .setup = vxlan_setup, |
| 1983 | .validate = vxlan_validate, |
| 1984 | .newlink = vxlan_newlink, |
| 1985 | .dellink = vxlan_dellink, |
| 1986 | .get_size = vxlan_get_size, |
| 1987 | .fill_info = vxlan_fill_info, |
| 1988 | }; |
| 1989 | |
| 1990 | static __net_init int vxlan_init_net(struct net *net) |
| 1991 | { |
| 1992 | struct vxlan_net *vn = net_generic(net, vxlan_net_id); |
Cong Wang | 31fec5a | 2013-05-27 22:35:52 +0000 | [diff] [blame] | 1993 | unsigned int h; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1994 | |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame] | 1995 | INIT_LIST_HEAD(&vn->vxlan_list); |
Stephen Hemminger | 1c51a91 | 2013-06-17 14:16:11 -0700 | [diff] [blame] | 1996 | spin_lock_init(&vn->sock_lock); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1997 | |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame] | 1998 | for (h = 0; h < PORT_HASH_SIZE; ++h) |
| 1999 | INIT_HLIST_HEAD(&vn->sock_list[h]); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 2000 | |
| 2001 | return 0; |
| 2002 | } |
| 2003 | |
| 2004 | static __net_exit void vxlan_exit_net(struct net *net) |
| 2005 | { |
| 2006 | struct vxlan_net *vn = net_generic(net, vxlan_net_id); |
Zang MingJie | 9cb6cb7 | 2013-03-06 04:37:37 +0000 | [diff] [blame] | 2007 | struct vxlan_dev *vxlan; |
stephen hemminger | 372675a | 2013-07-18 08:38:26 -0700 | [diff] [blame] | 2008 | LIST_HEAD(list); |
Zang MingJie | 9cb6cb7 | 2013-03-06 04:37:37 +0000 | [diff] [blame] | 2009 | |
| 2010 | rtnl_lock(); |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame] | 2011 | list_for_each_entry(vxlan, &vn->vxlan_list, next) |
stephen hemminger | 372675a | 2013-07-18 08:38:26 -0700 | [diff] [blame] | 2012 | unregister_netdevice_queue(vxlan->dev, &list); |
| 2013 | unregister_netdevice_many(&list); |
Zang MingJie | 9cb6cb7 | 2013-03-06 04:37:37 +0000 | [diff] [blame] | 2014 | rtnl_unlock(); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 2015 | } |
| 2016 | |
| 2017 | static struct pernet_operations vxlan_net_ops = { |
| 2018 | .init = vxlan_init_net, |
| 2019 | .exit = vxlan_exit_net, |
| 2020 | .id = &vxlan_net_id, |
| 2021 | .size = sizeof(struct vxlan_net), |
| 2022 | }; |
| 2023 | |
| 2024 | static int __init vxlan_init_module(void) |
| 2025 | { |
| 2026 | int rc; |
| 2027 | |
Stephen Hemminger | 758c57d | 2013-06-17 14:16:09 -0700 | [diff] [blame] | 2028 | vxlan_wq = alloc_workqueue("vxlan", 0, 0); |
| 2029 | if (!vxlan_wq) |
| 2030 | return -ENOMEM; |
| 2031 | |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 2032 | get_random_bytes(&vxlan_salt, sizeof(vxlan_salt)); |
| 2033 | |
| 2034 | rc = register_pernet_device(&vxlan_net_ops); |
| 2035 | if (rc) |
| 2036 | goto out1; |
| 2037 | |
| 2038 | rc = rtnl_link_register(&vxlan_link_ops); |
| 2039 | if (rc) |
| 2040 | goto out2; |
| 2041 | |
| 2042 | return 0; |
| 2043 | |
| 2044 | out2: |
| 2045 | unregister_pernet_device(&vxlan_net_ops); |
| 2046 | out1: |
Stephen Hemminger | 758c57d | 2013-06-17 14:16:09 -0700 | [diff] [blame] | 2047 | destroy_workqueue(vxlan_wq); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 2048 | return rc; |
| 2049 | } |
Cong Wang | 7332a13 | 2013-05-27 22:35:53 +0000 | [diff] [blame] | 2050 | late_initcall(vxlan_init_module); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 2051 | |
| 2052 | static void __exit vxlan_cleanup_module(void) |
| 2053 | { |
Stephen Hemminger | b715398 | 2013-06-17 14:16:09 -0700 | [diff] [blame] | 2054 | rtnl_link_unregister(&vxlan_link_ops); |
Stephen Hemminger | 758c57d | 2013-06-17 14:16:09 -0700 | [diff] [blame] | 2055 | destroy_workqueue(vxlan_wq); |
Pravin B Shelar | f89e57c | 2013-07-11 11:38:06 -0700 | [diff] [blame] | 2056 | unregister_pernet_device(&vxlan_net_ops); |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 2057 | rcu_barrier(); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 2058 | } |
| 2059 | module_exit(vxlan_cleanup_module); |
| 2060 | |
| 2061 | MODULE_LICENSE("GPL"); |
| 2062 | MODULE_VERSION(VXLAN_VERSION); |
stephen hemminger | 3b8df3c | 2013-04-27 11:31:52 +0000 | [diff] [blame] | 2063 | MODULE_AUTHOR("Stephen Hemminger <stephen@networkplumber.org>"); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 2064 | MODULE_ALIAS_RTNL_LINK("vxlan"); |