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