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