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