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