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