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