vxlan.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227
  1. /*
  2. * VXLAN: Virtual eXtensiable Local Area Network
  3. *
  4. * Copyright (c) 2012 Vyatta Inc.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. * TODO
  11. * - use IANA UDP port number (when defined)
  12. * - IPv6 (not in RFC)
  13. */
  14. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  15. #include <linux/kernel.h>
  16. #include <linux/types.h>
  17. #include <linux/module.h>
  18. #include <linux/errno.h>
  19. #include <linux/slab.h>
  20. #include <linux/skbuff.h>
  21. #include <linux/rculist.h>
  22. #include <linux/netdevice.h>
  23. #include <linux/in.h>
  24. #include <linux/ip.h>
  25. #include <linux/udp.h>
  26. #include <linux/igmp.h>
  27. #include <linux/etherdevice.h>
  28. #include <linux/if_ether.h>
  29. #include <linux/hash.h>
  30. #include <net/ip.h>
  31. #include <net/icmp.h>
  32. #include <net/udp.h>
  33. #include <net/rtnetlink.h>
  34. #include <net/route.h>
  35. #include <net/dsfield.h>
  36. #include <net/inet_ecn.h>
  37. #include <net/net_namespace.h>
  38. #include <net/netns/generic.h>
  39. #define VXLAN_VERSION "0.1"
  40. #define VNI_HASH_BITS 10
  41. #define VNI_HASH_SIZE (1<<VNI_HASH_BITS)
  42. #define FDB_HASH_BITS 8
  43. #define FDB_HASH_SIZE (1<<FDB_HASH_BITS)
  44. #define FDB_AGE_DEFAULT 300 /* 5 min */
  45. #define FDB_AGE_INTERVAL (10 * HZ) /* rescan interval */
  46. #define VXLAN_N_VID (1u << 24)
  47. #define VXLAN_VID_MASK (VXLAN_N_VID - 1)
  48. /* VLAN + IP header + UDP + VXLAN */
  49. #define VXLAN_HEADROOM (4 + 20 + 8 + 8)
  50. #define VXLAN_FLAGS 0x08000000 /* struct vxlanhdr.vx_flags required value. */
  51. /* VXLAN protocol header */
  52. struct vxlanhdr {
  53. __be32 vx_flags;
  54. __be32 vx_vni;
  55. };
  56. /* UDP port for VXLAN traffic. */
  57. static unsigned int vxlan_port __read_mostly = 8472;
  58. module_param_named(udp_port, vxlan_port, uint, 0444);
  59. MODULE_PARM_DESC(udp_port, "Destination UDP port");
  60. static bool log_ecn_error = true;
  61. module_param(log_ecn_error, bool, 0644);
  62. MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN");
  63. /* per-net private data for this module */
  64. static unsigned int vxlan_net_id;
  65. struct vxlan_net {
  66. struct socket *sock; /* UDP encap socket */
  67. struct hlist_head vni_list[VNI_HASH_SIZE];
  68. };
  69. /* Forwarding table entry */
  70. struct vxlan_fdb {
  71. struct hlist_node hlist; /* linked list of entries */
  72. struct rcu_head rcu;
  73. unsigned long updated; /* jiffies */
  74. unsigned long used;
  75. __be32 remote_ip;
  76. u16 state; /* see ndm_state */
  77. u8 eth_addr[ETH_ALEN];
  78. };
  79. /* Per-cpu network traffic stats */
  80. struct vxlan_stats {
  81. u64 rx_packets;
  82. u64 rx_bytes;
  83. u64 tx_packets;
  84. u64 tx_bytes;
  85. struct u64_stats_sync syncp;
  86. };
  87. /* Pseudo network device */
  88. struct vxlan_dev {
  89. struct hlist_node hlist;
  90. struct net_device *dev;
  91. struct vxlan_stats __percpu *stats;
  92. __u32 vni; /* virtual network id */
  93. __be32 gaddr; /* multicast group */
  94. __be32 saddr; /* source address */
  95. unsigned int link; /* link to multicast over */
  96. __u8 tos; /* TOS override */
  97. __u8 ttl;
  98. bool learn;
  99. unsigned long age_interval;
  100. struct timer_list age_timer;
  101. spinlock_t hash_lock;
  102. unsigned int addrcnt;
  103. unsigned int addrmax;
  104. unsigned int addrexceeded;
  105. struct hlist_head fdb_head[FDB_HASH_SIZE];
  106. };
  107. /* salt for hash table */
  108. static u32 vxlan_salt __read_mostly;
  109. static inline struct hlist_head *vni_head(struct net *net, u32 id)
  110. {
  111. struct vxlan_net *vn = net_generic(net, vxlan_net_id);
  112. return &vn->vni_list[hash_32(id, VNI_HASH_BITS)];
  113. }
  114. /* Look up VNI in a per net namespace table */
  115. static struct vxlan_dev *vxlan_find_vni(struct net *net, u32 id)
  116. {
  117. struct vxlan_dev *vxlan;
  118. struct hlist_node *node;
  119. hlist_for_each_entry_rcu(vxlan, node, vni_head(net, id), hlist) {
  120. if (vxlan->vni == id)
  121. return vxlan;
  122. }
  123. return NULL;
  124. }
  125. /* Fill in neighbour message in skbuff. */
  126. static int vxlan_fdb_info(struct sk_buff *skb, struct vxlan_dev *vxlan,
  127. const struct vxlan_fdb *fdb,
  128. u32 portid, u32 seq, int type, unsigned int flags)
  129. {
  130. unsigned long now = jiffies;
  131. struct nda_cacheinfo ci;
  132. struct nlmsghdr *nlh;
  133. struct ndmsg *ndm;
  134. nlh = nlmsg_put(skb, portid, seq, type, sizeof(*ndm), flags);
  135. if (nlh == NULL)
  136. return -EMSGSIZE;
  137. ndm = nlmsg_data(nlh);
  138. memset(ndm, 0, sizeof(*ndm));
  139. ndm->ndm_family = AF_BRIDGE;
  140. ndm->ndm_state = fdb->state;
  141. ndm->ndm_ifindex = vxlan->dev->ifindex;
  142. ndm->ndm_flags = NTF_SELF;
  143. ndm->ndm_type = NDA_DST;
  144. if (nla_put(skb, NDA_LLADDR, ETH_ALEN, &fdb->eth_addr))
  145. goto nla_put_failure;
  146. if (nla_put_be32(skb, NDA_DST, fdb->remote_ip))
  147. goto nla_put_failure;
  148. ci.ndm_used = jiffies_to_clock_t(now - fdb->used);
  149. ci.ndm_confirmed = 0;
  150. ci.ndm_updated = jiffies_to_clock_t(now - fdb->updated);
  151. ci.ndm_refcnt = 0;
  152. if (nla_put(skb, NDA_CACHEINFO, sizeof(ci), &ci))
  153. goto nla_put_failure;
  154. return nlmsg_end(skb, nlh);
  155. nla_put_failure:
  156. nlmsg_cancel(skb, nlh);
  157. return -EMSGSIZE;
  158. }
  159. static inline size_t vxlan_nlmsg_size(void)
  160. {
  161. return NLMSG_ALIGN(sizeof(struct ndmsg))
  162. + nla_total_size(ETH_ALEN) /* NDA_LLADDR */
  163. + nla_total_size(sizeof(__be32)) /* NDA_DST */
  164. + nla_total_size(sizeof(struct nda_cacheinfo));
  165. }
  166. static void vxlan_fdb_notify(struct vxlan_dev *vxlan,
  167. const struct vxlan_fdb *fdb, int type)
  168. {
  169. struct net *net = dev_net(vxlan->dev);
  170. struct sk_buff *skb;
  171. int err = -ENOBUFS;
  172. skb = nlmsg_new(vxlan_nlmsg_size(), GFP_ATOMIC);
  173. if (skb == NULL)
  174. goto errout;
  175. err = vxlan_fdb_info(skb, vxlan, fdb, 0, 0, type, 0);
  176. if (err < 0) {
  177. /* -EMSGSIZE implies BUG in vxlan_nlmsg_size() */
  178. WARN_ON(err == -EMSGSIZE);
  179. kfree_skb(skb);
  180. goto errout;
  181. }
  182. rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
  183. return;
  184. errout:
  185. if (err < 0)
  186. rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
  187. }
  188. /* Hash Ethernet address */
  189. static u32 eth_hash(const unsigned char *addr)
  190. {
  191. u64 value = get_unaligned((u64 *)addr);
  192. /* only want 6 bytes */
  193. #ifdef __BIG_ENDIAN
  194. value >>= 16;
  195. #else
  196. value <<= 16;
  197. #endif
  198. return hash_64(value, FDB_HASH_BITS);
  199. }
  200. /* Hash chain to use given mac address */
  201. static inline struct hlist_head *vxlan_fdb_head(struct vxlan_dev *vxlan,
  202. const u8 *mac)
  203. {
  204. return &vxlan->fdb_head[eth_hash(mac)];
  205. }
  206. /* Look up Ethernet address in forwarding table */
  207. static struct vxlan_fdb *vxlan_find_mac(struct vxlan_dev *vxlan,
  208. const u8 *mac)
  209. {
  210. struct hlist_head *head = vxlan_fdb_head(vxlan, mac);
  211. struct vxlan_fdb *f;
  212. struct hlist_node *node;
  213. hlist_for_each_entry_rcu(f, node, head, hlist) {
  214. if (compare_ether_addr(mac, f->eth_addr) == 0)
  215. return f;
  216. }
  217. return NULL;
  218. }
  219. /* Add new entry to forwarding table -- assumes lock held */
  220. static int vxlan_fdb_create(struct vxlan_dev *vxlan,
  221. const u8 *mac, __be32 ip,
  222. __u16 state, __u16 flags)
  223. {
  224. struct vxlan_fdb *f;
  225. int notify = 0;
  226. f = vxlan_find_mac(vxlan, mac);
  227. if (f) {
  228. if (flags & NLM_F_EXCL) {
  229. netdev_dbg(vxlan->dev,
  230. "lost race to create %pM\n", mac);
  231. return -EEXIST;
  232. }
  233. if (f->state != state) {
  234. f->state = state;
  235. f->updated = jiffies;
  236. notify = 1;
  237. }
  238. } else {
  239. if (!(flags & NLM_F_CREATE))
  240. return -ENOENT;
  241. if (vxlan->addrmax && vxlan->addrcnt >= vxlan->addrmax)
  242. return -ENOSPC;
  243. netdev_dbg(vxlan->dev, "add %pM -> %pI4\n", mac, &ip);
  244. f = kmalloc(sizeof(*f), GFP_ATOMIC);
  245. if (!f)
  246. return -ENOMEM;
  247. notify = 1;
  248. f->remote_ip = ip;
  249. f->state = state;
  250. f->updated = f->used = jiffies;
  251. memcpy(f->eth_addr, mac, ETH_ALEN);
  252. ++vxlan->addrcnt;
  253. hlist_add_head_rcu(&f->hlist,
  254. vxlan_fdb_head(vxlan, mac));
  255. }
  256. if (notify)
  257. vxlan_fdb_notify(vxlan, f, RTM_NEWNEIGH);
  258. return 0;
  259. }
  260. static void vxlan_fdb_destroy(struct vxlan_dev *vxlan, struct vxlan_fdb *f)
  261. {
  262. netdev_dbg(vxlan->dev,
  263. "delete %pM\n", f->eth_addr);
  264. --vxlan->addrcnt;
  265. vxlan_fdb_notify(vxlan, f, RTM_DELNEIGH);
  266. hlist_del_rcu(&f->hlist);
  267. kfree_rcu(f, rcu);
  268. }
  269. /* Add static entry (via netlink) */
  270. static int vxlan_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
  271. struct net_device *dev,
  272. const unsigned char *addr, u16 flags)
  273. {
  274. struct vxlan_dev *vxlan = netdev_priv(dev);
  275. __be32 ip;
  276. int err;
  277. if (!(ndm->ndm_state & (NUD_PERMANENT|NUD_REACHABLE))) {
  278. pr_info("RTM_NEWNEIGH with invalid state %#x\n",
  279. ndm->ndm_state);
  280. return -EINVAL;
  281. }
  282. if (tb[NDA_DST] == NULL)
  283. return -EINVAL;
  284. if (nla_len(tb[NDA_DST]) != sizeof(__be32))
  285. return -EAFNOSUPPORT;
  286. ip = nla_get_be32(tb[NDA_DST]);
  287. spin_lock_bh(&vxlan->hash_lock);
  288. err = vxlan_fdb_create(vxlan, addr, ip, ndm->ndm_state, flags);
  289. spin_unlock_bh(&vxlan->hash_lock);
  290. return err;
  291. }
  292. /* Delete entry (via netlink) */
  293. static int vxlan_fdb_delete(struct ndmsg *ndm, struct net_device *dev,
  294. const unsigned char *addr)
  295. {
  296. struct vxlan_dev *vxlan = netdev_priv(dev);
  297. struct vxlan_fdb *f;
  298. int err = -ENOENT;
  299. spin_lock_bh(&vxlan->hash_lock);
  300. f = vxlan_find_mac(vxlan, addr);
  301. if (f) {
  302. vxlan_fdb_destroy(vxlan, f);
  303. err = 0;
  304. }
  305. spin_unlock_bh(&vxlan->hash_lock);
  306. return err;
  307. }
  308. /* Dump forwarding table */
  309. static int vxlan_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
  310. struct net_device *dev, int idx)
  311. {
  312. struct vxlan_dev *vxlan = netdev_priv(dev);
  313. unsigned int h;
  314. for (h = 0; h < FDB_HASH_SIZE; ++h) {
  315. struct vxlan_fdb *f;
  316. struct hlist_node *n;
  317. int err;
  318. hlist_for_each_entry_rcu(f, n, &vxlan->fdb_head[h], hlist) {
  319. if (idx < cb->args[0])
  320. goto skip;
  321. err = vxlan_fdb_info(skb, vxlan, f,
  322. NETLINK_CB(cb->skb).portid,
  323. cb->nlh->nlmsg_seq,
  324. RTM_NEWNEIGH,
  325. NLM_F_MULTI);
  326. if (err < 0)
  327. break;
  328. skip:
  329. ++idx;
  330. }
  331. }
  332. return idx;
  333. }
  334. /* Watch incoming packets to learn mapping between Ethernet address
  335. * and Tunnel endpoint.
  336. */
  337. static void vxlan_snoop(struct net_device *dev,
  338. __be32 src_ip, const u8 *src_mac)
  339. {
  340. struct vxlan_dev *vxlan = netdev_priv(dev);
  341. struct vxlan_fdb *f;
  342. int err;
  343. f = vxlan_find_mac(vxlan, src_mac);
  344. if (likely(f)) {
  345. f->used = jiffies;
  346. if (likely(f->remote_ip == src_ip))
  347. return;
  348. if (net_ratelimit())
  349. netdev_info(dev,
  350. "%pM migrated from %pI4 to %pI4\n",
  351. src_mac, &f->remote_ip, &src_ip);
  352. f->remote_ip = src_ip;
  353. f->updated = jiffies;
  354. } else {
  355. /* learned new entry */
  356. spin_lock(&vxlan->hash_lock);
  357. err = vxlan_fdb_create(vxlan, src_mac, src_ip,
  358. NUD_REACHABLE,
  359. NLM_F_EXCL|NLM_F_CREATE);
  360. spin_unlock(&vxlan->hash_lock);
  361. }
  362. }
  363. /* See if multicast group is already in use by other ID */
  364. static bool vxlan_group_used(struct vxlan_net *vn,
  365. const struct vxlan_dev *this)
  366. {
  367. const struct vxlan_dev *vxlan;
  368. struct hlist_node *node;
  369. unsigned h;
  370. for (h = 0; h < VNI_HASH_SIZE; ++h)
  371. hlist_for_each_entry(vxlan, node, &vn->vni_list[h], hlist) {
  372. if (vxlan == this)
  373. continue;
  374. if (!netif_running(vxlan->dev))
  375. continue;
  376. if (vxlan->gaddr == this->gaddr)
  377. return true;
  378. }
  379. return false;
  380. }
  381. /* kernel equivalent to IP_ADD_MEMBERSHIP */
  382. static int vxlan_join_group(struct net_device *dev)
  383. {
  384. struct vxlan_dev *vxlan = netdev_priv(dev);
  385. struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
  386. struct sock *sk = vn->sock->sk;
  387. struct ip_mreqn mreq = {
  388. .imr_multiaddr.s_addr = vxlan->gaddr,
  389. };
  390. int err;
  391. /* Already a member of group */
  392. if (vxlan_group_used(vn, vxlan))
  393. return 0;
  394. /* Need to drop RTNL to call multicast join */
  395. rtnl_unlock();
  396. lock_sock(sk);
  397. err = ip_mc_join_group(sk, &mreq);
  398. release_sock(sk);
  399. rtnl_lock();
  400. return err;
  401. }
  402. /* kernel equivalent to IP_DROP_MEMBERSHIP */
  403. static int vxlan_leave_group(struct net_device *dev)
  404. {
  405. struct vxlan_dev *vxlan = netdev_priv(dev);
  406. struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
  407. int err = 0;
  408. struct sock *sk = vn->sock->sk;
  409. struct ip_mreqn mreq = {
  410. .imr_multiaddr.s_addr = vxlan->gaddr,
  411. };
  412. /* Only leave group when last vxlan is done. */
  413. if (vxlan_group_used(vn, vxlan))
  414. return 0;
  415. /* Need to drop RTNL to call multicast leave */
  416. rtnl_unlock();
  417. lock_sock(sk);
  418. err = ip_mc_leave_group(sk, &mreq);
  419. release_sock(sk);
  420. rtnl_lock();
  421. return err;
  422. }
  423. /* Callback from net/ipv4/udp.c to receive packets */
  424. static int vxlan_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
  425. {
  426. struct iphdr *oip;
  427. struct vxlanhdr *vxh;
  428. struct vxlan_dev *vxlan;
  429. struct vxlan_stats *stats;
  430. __u32 vni;
  431. int err;
  432. /* pop off outer UDP header */
  433. __skb_pull(skb, sizeof(struct udphdr));
  434. /* Need Vxlan and inner Ethernet header to be present */
  435. if (!pskb_may_pull(skb, sizeof(struct vxlanhdr)))
  436. goto error;
  437. /* Drop packets with reserved bits set */
  438. vxh = (struct vxlanhdr *) skb->data;
  439. if (vxh->vx_flags != htonl(VXLAN_FLAGS) ||
  440. (vxh->vx_vni & htonl(0xff))) {
  441. netdev_dbg(skb->dev, "invalid vxlan flags=%#x vni=%#x\n",
  442. ntohl(vxh->vx_flags), ntohl(vxh->vx_vni));
  443. goto error;
  444. }
  445. __skb_pull(skb, sizeof(struct vxlanhdr));
  446. skb_postpull_rcsum(skb, eth_hdr(skb), sizeof(struct vxlanhdr));
  447. /* Is this VNI defined? */
  448. vni = ntohl(vxh->vx_vni) >> 8;
  449. vxlan = vxlan_find_vni(sock_net(sk), vni);
  450. if (!vxlan) {
  451. netdev_dbg(skb->dev, "unknown vni %d\n", vni);
  452. goto drop;
  453. }
  454. if (!pskb_may_pull(skb, ETH_HLEN)) {
  455. vxlan->dev->stats.rx_length_errors++;
  456. vxlan->dev->stats.rx_errors++;
  457. goto drop;
  458. }
  459. /* Re-examine inner Ethernet packet */
  460. oip = ip_hdr(skb);
  461. skb->protocol = eth_type_trans(skb, vxlan->dev);
  462. skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN);
  463. /* Ignore packet loops (and multicast echo) */
  464. if (compare_ether_addr(eth_hdr(skb)->h_source,
  465. vxlan->dev->dev_addr) == 0)
  466. goto drop;
  467. if (vxlan->learn)
  468. vxlan_snoop(skb->dev, oip->saddr, eth_hdr(skb)->h_source);
  469. __skb_tunnel_rx(skb, vxlan->dev);
  470. skb_reset_network_header(skb);
  471. err = IP_ECN_decapsulate(oip, skb);
  472. if (unlikely(err)) {
  473. if (log_ecn_error)
  474. net_info_ratelimited("non-ECT from %pI4 with TOS=%#x\n",
  475. &oip->saddr, oip->tos);
  476. if (err > 1) {
  477. ++vxlan->dev->stats.rx_frame_errors;
  478. ++vxlan->dev->stats.rx_errors;
  479. goto drop;
  480. }
  481. }
  482. stats = this_cpu_ptr(vxlan->stats);
  483. u64_stats_update_begin(&stats->syncp);
  484. stats->rx_packets++;
  485. stats->rx_bytes += skb->len;
  486. u64_stats_update_end(&stats->syncp);
  487. netif_rx(skb);
  488. return 0;
  489. error:
  490. /* Put UDP header back */
  491. __skb_push(skb, sizeof(struct udphdr));
  492. return 1;
  493. drop:
  494. /* Consume bad packet */
  495. kfree_skb(skb);
  496. return 0;
  497. }
  498. /* Extract dsfield from inner protocol */
  499. static inline u8 vxlan_get_dsfield(const struct iphdr *iph,
  500. const struct sk_buff *skb)
  501. {
  502. if (skb->protocol == htons(ETH_P_IP))
  503. return iph->tos;
  504. else if (skb->protocol == htons(ETH_P_IPV6))
  505. return ipv6_get_dsfield((const struct ipv6hdr *)iph);
  506. else
  507. return 0;
  508. }
  509. /* Propogate ECN bits out */
  510. static inline u8 vxlan_ecn_encap(u8 tos,
  511. const struct iphdr *iph,
  512. const struct sk_buff *skb)
  513. {
  514. u8 inner = vxlan_get_dsfield(iph, skb);
  515. return INET_ECN_encapsulate(tos, inner);
  516. }
  517. static __be32 vxlan_find_dst(struct vxlan_dev *vxlan, struct sk_buff *skb)
  518. {
  519. const struct ethhdr *eth = (struct ethhdr *) skb->data;
  520. const struct vxlan_fdb *f;
  521. if (is_multicast_ether_addr(eth->h_dest))
  522. return vxlan->gaddr;
  523. f = vxlan_find_mac(vxlan, eth->h_dest);
  524. if (f)
  525. return f->remote_ip;
  526. else
  527. return vxlan->gaddr;
  528. }
  529. /* Transmit local packets over Vxlan
  530. *
  531. * Outer IP header inherits ECN and DF from inner header.
  532. * Outer UDP destination is the VXLAN assigned port.
  533. * source port is based on hash of flow if available
  534. * otherwise use a random value
  535. */
  536. static netdev_tx_t vxlan_xmit(struct sk_buff *skb, struct net_device *dev)
  537. {
  538. struct vxlan_dev *vxlan = netdev_priv(dev);
  539. struct rtable *rt;
  540. const struct iphdr *old_iph;
  541. struct iphdr *iph;
  542. struct vxlanhdr *vxh;
  543. struct udphdr *uh;
  544. struct flowi4 fl4;
  545. unsigned int pkt_len = skb->len;
  546. u32 hash;
  547. __be32 dst;
  548. __be16 df = 0;
  549. __u8 tos, ttl;
  550. int err;
  551. dst = vxlan_find_dst(vxlan, skb);
  552. if (!dst)
  553. goto drop;
  554. /* Need space for new headers (invalidates iph ptr) */
  555. if (skb_cow_head(skb, VXLAN_HEADROOM))
  556. goto drop;
  557. old_iph = ip_hdr(skb);
  558. ttl = vxlan->ttl;
  559. if (!ttl && IN_MULTICAST(ntohl(dst)))
  560. ttl = 1;
  561. tos = vxlan->tos;
  562. if (tos == 1)
  563. tos = vxlan_get_dsfield(old_iph, skb);
  564. hash = skb_get_rxhash(skb);
  565. rt = ip_route_output_gre(dev_net(dev), &fl4, dst,
  566. vxlan->saddr, vxlan->vni,
  567. RT_TOS(tos), vxlan->link);
  568. if (IS_ERR(rt)) {
  569. netdev_dbg(dev, "no route to %pI4\n", &dst);
  570. dev->stats.tx_carrier_errors++;
  571. goto tx_error;
  572. }
  573. if (rt->dst.dev == dev) {
  574. netdev_dbg(dev, "circular route to %pI4\n", &dst);
  575. ip_rt_put(rt);
  576. dev->stats.collisions++;
  577. goto tx_error;
  578. }
  579. memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
  580. IPCB(skb)->flags &= ~(IPSKB_XFRM_TUNNEL_SIZE | IPSKB_XFRM_TRANSFORMED |
  581. IPSKB_REROUTED);
  582. skb_dst_drop(skb);
  583. skb_dst_set(skb, &rt->dst);
  584. vxh = (struct vxlanhdr *) __skb_push(skb, sizeof(*vxh));
  585. vxh->vx_flags = htonl(VXLAN_FLAGS);
  586. vxh->vx_vni = htonl(vxlan->vni << 8);
  587. __skb_push(skb, sizeof(*uh));
  588. skb_reset_transport_header(skb);
  589. uh = udp_hdr(skb);
  590. uh->dest = htons(vxlan_port);
  591. uh->source = hash ? :random32();
  592. uh->len = htons(skb->len);
  593. uh->check = 0;
  594. __skb_push(skb, sizeof(*iph));
  595. skb_reset_network_header(skb);
  596. iph = ip_hdr(skb);
  597. iph->version = 4;
  598. iph->ihl = sizeof(struct iphdr) >> 2;
  599. iph->frag_off = df;
  600. iph->protocol = IPPROTO_UDP;
  601. iph->tos = vxlan_ecn_encap(tos, old_iph, skb);
  602. iph->daddr = fl4.daddr;
  603. iph->saddr = fl4.saddr;
  604. iph->ttl = ttl ? : ip4_dst_hoplimit(&rt->dst);
  605. /* See __IPTUNNEL_XMIT */
  606. skb->ip_summed = CHECKSUM_NONE;
  607. ip_select_ident(iph, &rt->dst, NULL);
  608. err = ip_local_out(skb);
  609. if (likely(net_xmit_eval(err) == 0)) {
  610. struct vxlan_stats *stats = this_cpu_ptr(vxlan->stats);
  611. u64_stats_update_begin(&stats->syncp);
  612. stats->tx_packets++;
  613. stats->tx_bytes += pkt_len;
  614. u64_stats_update_end(&stats->syncp);
  615. } else {
  616. dev->stats.tx_errors++;
  617. dev->stats.tx_aborted_errors++;
  618. }
  619. return NETDEV_TX_OK;
  620. drop:
  621. dev->stats.tx_dropped++;
  622. goto tx_free;
  623. tx_error:
  624. dev->stats.tx_errors++;
  625. tx_free:
  626. dev_kfree_skb(skb);
  627. return NETDEV_TX_OK;
  628. }
  629. /* Walk the forwarding table and purge stale entries */
  630. static void vxlan_cleanup(unsigned long arg)
  631. {
  632. struct vxlan_dev *vxlan = (struct vxlan_dev *) arg;
  633. unsigned long next_timer = jiffies + FDB_AGE_INTERVAL;
  634. unsigned int h;
  635. if (!netif_running(vxlan->dev))
  636. return;
  637. spin_lock_bh(&vxlan->hash_lock);
  638. for (h = 0; h < FDB_HASH_SIZE; ++h) {
  639. struct hlist_node *p, *n;
  640. hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
  641. struct vxlan_fdb *f
  642. = container_of(p, struct vxlan_fdb, hlist);
  643. unsigned long timeout;
  644. if (f->state == NUD_PERMANENT)
  645. continue;
  646. timeout = f->used + vxlan->age_interval * HZ;
  647. if (time_before_eq(timeout, jiffies)) {
  648. netdev_dbg(vxlan->dev,
  649. "garbage collect %pM\n",
  650. f->eth_addr);
  651. f->state = NUD_STALE;
  652. vxlan_fdb_destroy(vxlan, f);
  653. } else if (time_before(timeout, next_timer))
  654. next_timer = timeout;
  655. }
  656. }
  657. spin_unlock_bh(&vxlan->hash_lock);
  658. mod_timer(&vxlan->age_timer, next_timer);
  659. }
  660. /* Setup stats when device is created */
  661. static int vxlan_init(struct net_device *dev)
  662. {
  663. struct vxlan_dev *vxlan = netdev_priv(dev);
  664. vxlan->stats = alloc_percpu(struct vxlan_stats);
  665. if (!vxlan->stats)
  666. return -ENOMEM;
  667. return 0;
  668. }
  669. /* Start ageing timer and join group when device is brought up */
  670. static int vxlan_open(struct net_device *dev)
  671. {
  672. struct vxlan_dev *vxlan = netdev_priv(dev);
  673. int err;
  674. if (vxlan->gaddr) {
  675. err = vxlan_join_group(dev);
  676. if (err)
  677. return err;
  678. }
  679. if (vxlan->age_interval)
  680. mod_timer(&vxlan->age_timer, jiffies + FDB_AGE_INTERVAL);
  681. return 0;
  682. }
  683. /* Purge the forwarding table */
  684. static void vxlan_flush(struct vxlan_dev *vxlan)
  685. {
  686. unsigned h;
  687. spin_lock_bh(&vxlan->hash_lock);
  688. for (h = 0; h < FDB_HASH_SIZE; ++h) {
  689. struct hlist_node *p, *n;
  690. hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
  691. struct vxlan_fdb *f
  692. = container_of(p, struct vxlan_fdb, hlist);
  693. vxlan_fdb_destroy(vxlan, f);
  694. }
  695. }
  696. spin_unlock_bh(&vxlan->hash_lock);
  697. }
  698. /* Cleanup timer and forwarding table on shutdown */
  699. static int vxlan_stop(struct net_device *dev)
  700. {
  701. struct vxlan_dev *vxlan = netdev_priv(dev);
  702. if (vxlan->gaddr)
  703. vxlan_leave_group(dev);
  704. del_timer_sync(&vxlan->age_timer);
  705. vxlan_flush(vxlan);
  706. return 0;
  707. }
  708. /* Merge per-cpu statistics */
  709. static struct rtnl_link_stats64 *vxlan_stats64(struct net_device *dev,
  710. struct rtnl_link_stats64 *stats)
  711. {
  712. struct vxlan_dev *vxlan = netdev_priv(dev);
  713. struct vxlan_stats tmp, sum = { 0 };
  714. unsigned int cpu;
  715. for_each_possible_cpu(cpu) {
  716. unsigned int start;
  717. const struct vxlan_stats *stats
  718. = per_cpu_ptr(vxlan->stats, cpu);
  719. do {
  720. start = u64_stats_fetch_begin_bh(&stats->syncp);
  721. memcpy(&tmp, stats, sizeof(tmp));
  722. } while (u64_stats_fetch_retry_bh(&stats->syncp, start));
  723. sum.tx_bytes += tmp.tx_bytes;
  724. sum.tx_packets += tmp.tx_packets;
  725. sum.rx_bytes += tmp.rx_bytes;
  726. sum.rx_packets += tmp.rx_packets;
  727. }
  728. stats->tx_bytes = sum.tx_bytes;
  729. stats->tx_packets = sum.tx_packets;
  730. stats->rx_bytes = sum.rx_bytes;
  731. stats->rx_packets = sum.rx_packets;
  732. stats->multicast = dev->stats.multicast;
  733. stats->rx_length_errors = dev->stats.rx_length_errors;
  734. stats->rx_frame_errors = dev->stats.rx_frame_errors;
  735. stats->rx_errors = dev->stats.rx_errors;
  736. stats->tx_dropped = dev->stats.tx_dropped;
  737. stats->tx_carrier_errors = dev->stats.tx_carrier_errors;
  738. stats->tx_aborted_errors = dev->stats.tx_aborted_errors;
  739. stats->collisions = dev->stats.collisions;
  740. stats->tx_errors = dev->stats.tx_errors;
  741. return stats;
  742. }
  743. /* Stub, nothing needs to be done. */
  744. static void vxlan_set_multicast_list(struct net_device *dev)
  745. {
  746. }
  747. static const struct net_device_ops vxlan_netdev_ops = {
  748. .ndo_init = vxlan_init,
  749. .ndo_open = vxlan_open,
  750. .ndo_stop = vxlan_stop,
  751. .ndo_start_xmit = vxlan_xmit,
  752. .ndo_get_stats64 = vxlan_stats64,
  753. .ndo_set_rx_mode = vxlan_set_multicast_list,
  754. .ndo_change_mtu = eth_change_mtu,
  755. .ndo_validate_addr = eth_validate_addr,
  756. .ndo_set_mac_address = eth_mac_addr,
  757. .ndo_fdb_add = vxlan_fdb_add,
  758. .ndo_fdb_del = vxlan_fdb_delete,
  759. .ndo_fdb_dump = vxlan_fdb_dump,
  760. };
  761. /* Info for udev, that this is a virtual tunnel endpoint */
  762. static struct device_type vxlan_type = {
  763. .name = "vxlan",
  764. };
  765. static void vxlan_free(struct net_device *dev)
  766. {
  767. struct vxlan_dev *vxlan = netdev_priv(dev);
  768. free_percpu(vxlan->stats);
  769. free_netdev(dev);
  770. }
  771. /* Initialize the device structure. */
  772. static void vxlan_setup(struct net_device *dev)
  773. {
  774. struct vxlan_dev *vxlan = netdev_priv(dev);
  775. unsigned h;
  776. eth_hw_addr_random(dev);
  777. ether_setup(dev);
  778. dev->netdev_ops = &vxlan_netdev_ops;
  779. dev->destructor = vxlan_free;
  780. SET_NETDEV_DEVTYPE(dev, &vxlan_type);
  781. dev->tx_queue_len = 0;
  782. dev->features |= NETIF_F_LLTX;
  783. dev->features |= NETIF_F_NETNS_LOCAL;
  784. dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
  785. spin_lock_init(&vxlan->hash_lock);
  786. init_timer_deferrable(&vxlan->age_timer);
  787. vxlan->age_timer.function = vxlan_cleanup;
  788. vxlan->age_timer.data = (unsigned long) vxlan;
  789. vxlan->dev = dev;
  790. for (h = 0; h < FDB_HASH_SIZE; ++h)
  791. INIT_HLIST_HEAD(&vxlan->fdb_head[h]);
  792. }
  793. static const struct nla_policy vxlan_policy[IFLA_VXLAN_MAX + 1] = {
  794. [IFLA_VXLAN_ID] = { .type = NLA_U32 },
  795. [IFLA_VXLAN_GROUP] = { .len = FIELD_SIZEOF(struct iphdr, daddr) },
  796. [IFLA_VXLAN_LINK] = { .type = NLA_U32 },
  797. [IFLA_VXLAN_LOCAL] = { .len = FIELD_SIZEOF(struct iphdr, saddr) },
  798. [IFLA_VXLAN_TOS] = { .type = NLA_U8 },
  799. [IFLA_VXLAN_TTL] = { .type = NLA_U8 },
  800. [IFLA_VXLAN_LEARNING] = { .type = NLA_U8 },
  801. [IFLA_VXLAN_AGEING] = { .type = NLA_U32 },
  802. [IFLA_VXLAN_LIMIT] = { .type = NLA_U32 },
  803. };
  804. static int vxlan_validate(struct nlattr *tb[], struct nlattr *data[])
  805. {
  806. if (tb[IFLA_ADDRESS]) {
  807. if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) {
  808. pr_debug("invalid link address (not ethernet)\n");
  809. return -EINVAL;
  810. }
  811. if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) {
  812. pr_debug("invalid all zero ethernet address\n");
  813. return -EADDRNOTAVAIL;
  814. }
  815. }
  816. if (!data)
  817. return -EINVAL;
  818. if (data[IFLA_VXLAN_ID]) {
  819. __u32 id = nla_get_u32(data[IFLA_VXLAN_ID]);
  820. if (id >= VXLAN_VID_MASK)
  821. return -ERANGE;
  822. }
  823. if (data[IFLA_VXLAN_GROUP]) {
  824. __be32 gaddr = nla_get_be32(data[IFLA_VXLAN_GROUP]);
  825. if (!IN_MULTICAST(ntohl(gaddr))) {
  826. pr_debug("group address is not IPv4 multicast\n");
  827. return -EADDRNOTAVAIL;
  828. }
  829. }
  830. return 0;
  831. }
  832. static int vxlan_newlink(struct net *net, struct net_device *dev,
  833. struct nlattr *tb[], struct nlattr *data[])
  834. {
  835. struct vxlan_dev *vxlan = netdev_priv(dev);
  836. __u32 vni;
  837. int err;
  838. if (!data[IFLA_VXLAN_ID])
  839. return -EINVAL;
  840. vni = nla_get_u32(data[IFLA_VXLAN_ID]);
  841. if (vxlan_find_vni(net, vni)) {
  842. pr_info("duplicate VNI %u\n", vni);
  843. return -EEXIST;
  844. }
  845. vxlan->vni = vni;
  846. if (data[IFLA_VXLAN_GROUP])
  847. vxlan->gaddr = nla_get_be32(data[IFLA_VXLAN_GROUP]);
  848. if (data[IFLA_VXLAN_LOCAL])
  849. vxlan->saddr = nla_get_be32(data[IFLA_VXLAN_LOCAL]);
  850. if (data[IFLA_VXLAN_LINK]) {
  851. vxlan->link = nla_get_u32(data[IFLA_VXLAN_LINK]);
  852. if (!tb[IFLA_MTU]) {
  853. struct net_device *lowerdev;
  854. lowerdev = __dev_get_by_index(net, vxlan->link);
  855. dev->mtu = lowerdev->mtu - VXLAN_HEADROOM;
  856. }
  857. }
  858. if (data[IFLA_VXLAN_TOS])
  859. vxlan->tos = nla_get_u8(data[IFLA_VXLAN_TOS]);
  860. if (!data[IFLA_VXLAN_LEARNING] || nla_get_u8(data[IFLA_VXLAN_LEARNING]))
  861. vxlan->learn = true;
  862. if (data[IFLA_VXLAN_AGEING])
  863. vxlan->age_interval = nla_get_u32(data[IFLA_VXLAN_AGEING]);
  864. else
  865. vxlan->age_interval = FDB_AGE_DEFAULT;
  866. if (data[IFLA_VXLAN_LIMIT])
  867. vxlan->addrmax = nla_get_u32(data[IFLA_VXLAN_LIMIT]);
  868. err = register_netdevice(dev);
  869. if (!err)
  870. hlist_add_head_rcu(&vxlan->hlist, vni_head(net, vxlan->vni));
  871. return err;
  872. }
  873. static void vxlan_dellink(struct net_device *dev, struct list_head *head)
  874. {
  875. struct vxlan_dev *vxlan = netdev_priv(dev);
  876. hlist_del_rcu(&vxlan->hlist);
  877. unregister_netdevice_queue(dev, head);
  878. }
  879. static size_t vxlan_get_size(const struct net_device *dev)
  880. {
  881. return nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_ID */
  882. nla_total_size(sizeof(__be32)) +/* IFLA_VXLAN_GROUP */
  883. nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LINK */
  884. nla_total_size(sizeof(__be32))+ /* IFLA_VXLAN_LOCAL */
  885. nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TTL */
  886. nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TOS */
  887. nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_LEARNING */
  888. nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_AGEING */
  889. nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LIMIT */
  890. 0;
  891. }
  892. static int vxlan_fill_info(struct sk_buff *skb, const struct net_device *dev)
  893. {
  894. const struct vxlan_dev *vxlan = netdev_priv(dev);
  895. if (nla_put_u32(skb, IFLA_VXLAN_ID, vxlan->vni))
  896. goto nla_put_failure;
  897. if (vxlan->gaddr && nla_put_be32(skb, IFLA_VXLAN_GROUP, vxlan->gaddr))
  898. goto nla_put_failure;
  899. if (vxlan->link && nla_put_u32(skb, IFLA_VXLAN_LINK, vxlan->link))
  900. goto nla_put_failure;
  901. if (vxlan->saddr && nla_put_be32(skb, IFLA_VXLAN_LOCAL, vxlan->saddr))
  902. goto nla_put_failure;
  903. if (nla_put_u8(skb, IFLA_VXLAN_TTL, vxlan->ttl) ||
  904. nla_put_u8(skb, IFLA_VXLAN_TOS, vxlan->tos) ||
  905. nla_put_u8(skb, IFLA_VXLAN_LEARNING, vxlan->learn) ||
  906. nla_put_u32(skb, IFLA_VXLAN_AGEING, vxlan->age_interval) ||
  907. nla_put_u32(skb, IFLA_VXLAN_LIMIT, vxlan->addrmax))
  908. goto nla_put_failure;
  909. return 0;
  910. nla_put_failure:
  911. return -EMSGSIZE;
  912. }
  913. static struct rtnl_link_ops vxlan_link_ops __read_mostly = {
  914. .kind = "vxlan",
  915. .maxtype = IFLA_VXLAN_MAX,
  916. .policy = vxlan_policy,
  917. .priv_size = sizeof(struct vxlan_dev),
  918. .setup = vxlan_setup,
  919. .validate = vxlan_validate,
  920. .newlink = vxlan_newlink,
  921. .dellink = vxlan_dellink,
  922. .get_size = vxlan_get_size,
  923. .fill_info = vxlan_fill_info,
  924. };
  925. static __net_init int vxlan_init_net(struct net *net)
  926. {
  927. struct vxlan_net *vn = net_generic(net, vxlan_net_id);
  928. struct sock *sk;
  929. struct sockaddr_in vxlan_addr = {
  930. .sin_family = AF_INET,
  931. .sin_addr.s_addr = htonl(INADDR_ANY),
  932. };
  933. int rc;
  934. unsigned h;
  935. /* Create UDP socket for encapsulation receive. */
  936. rc = sock_create_kern(AF_INET, SOCK_DGRAM, IPPROTO_UDP, &vn->sock);
  937. if (rc < 0) {
  938. pr_debug("UDP socket create failed\n");
  939. return rc;
  940. }
  941. /* Put in proper namespace */
  942. sk = vn->sock->sk;
  943. sk_change_net(sk, net);
  944. vxlan_addr.sin_port = htons(vxlan_port);
  945. rc = kernel_bind(vn->sock, (struct sockaddr *) &vxlan_addr,
  946. sizeof(vxlan_addr));
  947. if (rc < 0) {
  948. pr_debug("bind for UDP socket %pI4:%u (%d)\n",
  949. &vxlan_addr.sin_addr, ntohs(vxlan_addr.sin_port), rc);
  950. sk_release_kernel(sk);
  951. vn->sock = NULL;
  952. return rc;
  953. }
  954. /* Disable multicast loopback */
  955. inet_sk(sk)->mc_loop = 0;
  956. /* Mark socket as an encapsulation socket. */
  957. udp_sk(sk)->encap_type = 1;
  958. udp_sk(sk)->encap_rcv = vxlan_udp_encap_recv;
  959. udp_encap_enable();
  960. for (h = 0; h < VNI_HASH_SIZE; ++h)
  961. INIT_HLIST_HEAD(&vn->vni_list[h]);
  962. return 0;
  963. }
  964. static __net_exit void vxlan_exit_net(struct net *net)
  965. {
  966. struct vxlan_net *vn = net_generic(net, vxlan_net_id);
  967. if (vn->sock) {
  968. sk_release_kernel(vn->sock->sk);
  969. vn->sock = NULL;
  970. }
  971. }
  972. static struct pernet_operations vxlan_net_ops = {
  973. .init = vxlan_init_net,
  974. .exit = vxlan_exit_net,
  975. .id = &vxlan_net_id,
  976. .size = sizeof(struct vxlan_net),
  977. };
  978. static int __init vxlan_init_module(void)
  979. {
  980. int rc;
  981. get_random_bytes(&vxlan_salt, sizeof(vxlan_salt));
  982. rc = register_pernet_device(&vxlan_net_ops);
  983. if (rc)
  984. goto out1;
  985. rc = rtnl_link_register(&vxlan_link_ops);
  986. if (rc)
  987. goto out2;
  988. return 0;
  989. out2:
  990. unregister_pernet_device(&vxlan_net_ops);
  991. out1:
  992. return rc;
  993. }
  994. module_init(vxlan_init_module);
  995. static void __exit vxlan_cleanup_module(void)
  996. {
  997. rtnl_link_unregister(&vxlan_link_ops);
  998. unregister_pernet_device(&vxlan_net_ops);
  999. }
  1000. module_exit(vxlan_cleanup_module);
  1001. MODULE_LICENSE("GPL");
  1002. MODULE_VERSION(VXLAN_VERSION);
  1003. MODULE_AUTHOR("Stephen Hemminger <shemminger@vyatta.com>");
  1004. MODULE_ALIAS_RTNL_LINK("vxlan");