vxlan.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518
  1. /*
  2. * VXLAN: Virtual eXtensible 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/arp.h>
  31. #include <net/ndisc.h>
  32. #include <net/ip.h>
  33. #include <net/icmp.h>
  34. #include <net/udp.h>
  35. #include <net/rtnetlink.h>
  36. #include <net/route.h>
  37. #include <net/dsfield.h>
  38. #include <net/inet_ecn.h>
  39. #include <net/net_namespace.h>
  40. #include <net/netns/generic.h>
  41. #define VXLAN_VERSION "0.1"
  42. #define VNI_HASH_BITS 10
  43. #define VNI_HASH_SIZE (1<<VNI_HASH_BITS)
  44. #define FDB_HASH_BITS 8
  45. #define FDB_HASH_SIZE (1<<FDB_HASH_BITS)
  46. #define FDB_AGE_DEFAULT 300 /* 5 min */
  47. #define FDB_AGE_INTERVAL (10 * HZ) /* rescan interval */
  48. #define VXLAN_N_VID (1u << 24)
  49. #define VXLAN_VID_MASK (VXLAN_N_VID - 1)
  50. /* IP header + UDP + VXLAN + Ethernet header */
  51. #define VXLAN_HEADROOM (20 + 8 + 8 + 14)
  52. #define VXLAN_FLAGS 0x08000000 /* struct vxlanhdr.vx_flags required value. */
  53. /* VXLAN protocol header */
  54. struct vxlanhdr {
  55. __be32 vx_flags;
  56. __be32 vx_vni;
  57. };
  58. /* UDP port for VXLAN traffic. */
  59. static unsigned int vxlan_port __read_mostly = 8472;
  60. module_param_named(udp_port, vxlan_port, uint, 0444);
  61. MODULE_PARM_DESC(udp_port, "Destination UDP port");
  62. static bool log_ecn_error = true;
  63. module_param(log_ecn_error, bool, 0644);
  64. MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN");
  65. /* per-net private data for this module */
  66. static unsigned int vxlan_net_id;
  67. struct vxlan_net {
  68. struct socket *sock; /* UDP encap socket */
  69. struct hlist_head vni_list[VNI_HASH_SIZE];
  70. };
  71. /* Forwarding table entry */
  72. struct vxlan_fdb {
  73. struct hlist_node hlist; /* linked list of entries */
  74. struct rcu_head rcu;
  75. unsigned long updated; /* jiffies */
  76. unsigned long used;
  77. __be32 remote_ip;
  78. u16 state; /* see ndm_state */
  79. u8 eth_addr[ETH_ALEN];
  80. };
  81. /* Per-cpu network traffic stats */
  82. struct vxlan_stats {
  83. u64 rx_packets;
  84. u64 rx_bytes;
  85. u64 tx_packets;
  86. u64 tx_bytes;
  87. struct u64_stats_sync syncp;
  88. };
  89. /* Pseudo network device */
  90. struct vxlan_dev {
  91. struct hlist_node hlist;
  92. struct net_device *dev;
  93. struct vxlan_stats __percpu *stats;
  94. __u32 vni; /* virtual network id */
  95. __be32 gaddr; /* multicast group */
  96. __be32 saddr; /* source address */
  97. unsigned int link; /* link to multicast over */
  98. __u16 port_min; /* source port range */
  99. __u16 port_max;
  100. __u8 tos; /* TOS override */
  101. __u8 ttl;
  102. u32 flags; /* VXLAN_F_* below */
  103. unsigned long age_interval;
  104. struct timer_list age_timer;
  105. spinlock_t hash_lock;
  106. unsigned int addrcnt;
  107. unsigned int addrmax;
  108. struct hlist_head fdb_head[FDB_HASH_SIZE];
  109. };
  110. #define VXLAN_F_LEARN 0x01
  111. #define VXLAN_F_PROXY 0x02
  112. #define VXLAN_F_RSC 0x04
  113. #define VXLAN_F_L2MISS 0x08
  114. #define VXLAN_F_L3MISS 0x10
  115. /* salt for hash table */
  116. static u32 vxlan_salt __read_mostly;
  117. static inline struct hlist_head *vni_head(struct net *net, u32 id)
  118. {
  119. struct vxlan_net *vn = net_generic(net, vxlan_net_id);
  120. return &vn->vni_list[hash_32(id, VNI_HASH_BITS)];
  121. }
  122. /* Look up VNI in a per net namespace table */
  123. static struct vxlan_dev *vxlan_find_vni(struct net *net, u32 id)
  124. {
  125. struct vxlan_dev *vxlan;
  126. struct hlist_node *node;
  127. hlist_for_each_entry_rcu(vxlan, node, vni_head(net, id), hlist) {
  128. if (vxlan->vni == id)
  129. return vxlan;
  130. }
  131. return NULL;
  132. }
  133. /* Fill in neighbour message in skbuff. */
  134. static int vxlan_fdb_info(struct sk_buff *skb, struct vxlan_dev *vxlan,
  135. const struct vxlan_fdb *fdb,
  136. u32 portid, u32 seq, int type, unsigned int flags)
  137. {
  138. unsigned long now = jiffies;
  139. struct nda_cacheinfo ci;
  140. struct nlmsghdr *nlh;
  141. struct ndmsg *ndm;
  142. bool send_ip, send_eth;
  143. nlh = nlmsg_put(skb, portid, seq, type, sizeof(*ndm), flags);
  144. if (nlh == NULL)
  145. return -EMSGSIZE;
  146. ndm = nlmsg_data(nlh);
  147. memset(ndm, 0, sizeof(*ndm));
  148. send_eth = send_ip = true;
  149. if (type == RTM_GETNEIGH) {
  150. ndm->ndm_family = AF_INET;
  151. send_ip = fdb->remote_ip != 0;
  152. send_eth = !is_zero_ether_addr(fdb->eth_addr);
  153. } else
  154. ndm->ndm_family = AF_BRIDGE;
  155. ndm->ndm_state = fdb->state;
  156. ndm->ndm_ifindex = vxlan->dev->ifindex;
  157. ndm->ndm_flags = NTF_SELF;
  158. ndm->ndm_type = NDA_DST;
  159. if (send_eth && nla_put(skb, NDA_LLADDR, ETH_ALEN, &fdb->eth_addr))
  160. goto nla_put_failure;
  161. if (send_ip && nla_put_be32(skb, NDA_DST, fdb->remote_ip))
  162. goto nla_put_failure;
  163. ci.ndm_used = jiffies_to_clock_t(now - fdb->used);
  164. ci.ndm_confirmed = 0;
  165. ci.ndm_updated = jiffies_to_clock_t(now - fdb->updated);
  166. ci.ndm_refcnt = 0;
  167. if (nla_put(skb, NDA_CACHEINFO, sizeof(ci), &ci))
  168. goto nla_put_failure;
  169. return nlmsg_end(skb, nlh);
  170. nla_put_failure:
  171. nlmsg_cancel(skb, nlh);
  172. return -EMSGSIZE;
  173. }
  174. static inline size_t vxlan_nlmsg_size(void)
  175. {
  176. return NLMSG_ALIGN(sizeof(struct ndmsg))
  177. + nla_total_size(ETH_ALEN) /* NDA_LLADDR */
  178. + nla_total_size(sizeof(__be32)) /* NDA_DST */
  179. + nla_total_size(sizeof(struct nda_cacheinfo));
  180. }
  181. static void vxlan_fdb_notify(struct vxlan_dev *vxlan,
  182. const struct vxlan_fdb *fdb, int type)
  183. {
  184. struct net *net = dev_net(vxlan->dev);
  185. struct sk_buff *skb;
  186. int err = -ENOBUFS;
  187. skb = nlmsg_new(vxlan_nlmsg_size(), GFP_ATOMIC);
  188. if (skb == NULL)
  189. goto errout;
  190. err = vxlan_fdb_info(skb, vxlan, fdb, 0, 0, type, 0);
  191. if (err < 0) {
  192. /* -EMSGSIZE implies BUG in vxlan_nlmsg_size() */
  193. WARN_ON(err == -EMSGSIZE);
  194. kfree_skb(skb);
  195. goto errout;
  196. }
  197. rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
  198. return;
  199. errout:
  200. if (err < 0)
  201. rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
  202. }
  203. static void vxlan_ip_miss(struct net_device *dev, __be32 ipa)
  204. {
  205. struct vxlan_dev *vxlan = netdev_priv(dev);
  206. struct vxlan_fdb f;
  207. memset(&f, 0, sizeof f);
  208. f.state = NUD_STALE;
  209. f.remote_ip = ipa; /* goes to NDA_DST */
  210. vxlan_fdb_notify(vxlan, &f, RTM_GETNEIGH);
  211. }
  212. static void vxlan_fdb_miss(struct vxlan_dev *vxlan, const u8 eth_addr[ETH_ALEN])
  213. {
  214. struct vxlan_fdb f;
  215. memset(&f, 0, sizeof f);
  216. f.state = NUD_STALE;
  217. memcpy(f.eth_addr, eth_addr, ETH_ALEN);
  218. vxlan_fdb_notify(vxlan, &f, RTM_GETNEIGH);
  219. }
  220. /* Hash Ethernet address */
  221. static u32 eth_hash(const unsigned char *addr)
  222. {
  223. u64 value = get_unaligned((u64 *)addr);
  224. /* only want 6 bytes */
  225. #ifdef __BIG_ENDIAN
  226. value >>= 16;
  227. #else
  228. value <<= 16;
  229. #endif
  230. return hash_64(value, FDB_HASH_BITS);
  231. }
  232. /* Hash chain to use given mac address */
  233. static inline struct hlist_head *vxlan_fdb_head(struct vxlan_dev *vxlan,
  234. const u8 *mac)
  235. {
  236. return &vxlan->fdb_head[eth_hash(mac)];
  237. }
  238. /* Look up Ethernet address in forwarding table */
  239. static struct vxlan_fdb *vxlan_find_mac(struct vxlan_dev *vxlan,
  240. const u8 *mac)
  241. {
  242. struct hlist_head *head = vxlan_fdb_head(vxlan, mac);
  243. struct vxlan_fdb *f;
  244. struct hlist_node *node;
  245. hlist_for_each_entry_rcu(f, node, head, hlist) {
  246. if (compare_ether_addr(mac, f->eth_addr) == 0)
  247. return f;
  248. }
  249. return NULL;
  250. }
  251. /* Add new entry to forwarding table -- assumes lock held */
  252. static int vxlan_fdb_create(struct vxlan_dev *vxlan,
  253. const u8 *mac, __be32 ip,
  254. __u16 state, __u16 flags)
  255. {
  256. struct vxlan_fdb *f;
  257. int notify = 0;
  258. f = vxlan_find_mac(vxlan, mac);
  259. if (f) {
  260. if (flags & NLM_F_EXCL) {
  261. netdev_dbg(vxlan->dev,
  262. "lost race to create %pM\n", mac);
  263. return -EEXIST;
  264. }
  265. if (f->state != state) {
  266. f->state = state;
  267. f->updated = jiffies;
  268. notify = 1;
  269. }
  270. } else {
  271. if (!(flags & NLM_F_CREATE))
  272. return -ENOENT;
  273. if (vxlan->addrmax && vxlan->addrcnt >= vxlan->addrmax)
  274. return -ENOSPC;
  275. netdev_dbg(vxlan->dev, "add %pM -> %pI4\n", mac, &ip);
  276. f = kmalloc(sizeof(*f), GFP_ATOMIC);
  277. if (!f)
  278. return -ENOMEM;
  279. notify = 1;
  280. f->remote_ip = ip;
  281. f->state = state;
  282. f->updated = f->used = jiffies;
  283. memcpy(f->eth_addr, mac, ETH_ALEN);
  284. ++vxlan->addrcnt;
  285. hlist_add_head_rcu(&f->hlist,
  286. vxlan_fdb_head(vxlan, mac));
  287. }
  288. if (notify)
  289. vxlan_fdb_notify(vxlan, f, RTM_NEWNEIGH);
  290. return 0;
  291. }
  292. static void vxlan_fdb_destroy(struct vxlan_dev *vxlan, struct vxlan_fdb *f)
  293. {
  294. netdev_dbg(vxlan->dev,
  295. "delete %pM\n", f->eth_addr);
  296. --vxlan->addrcnt;
  297. vxlan_fdb_notify(vxlan, f, RTM_DELNEIGH);
  298. hlist_del_rcu(&f->hlist);
  299. kfree_rcu(f, rcu);
  300. }
  301. /* Add static entry (via netlink) */
  302. static int vxlan_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
  303. struct net_device *dev,
  304. const unsigned char *addr, u16 flags)
  305. {
  306. struct vxlan_dev *vxlan = netdev_priv(dev);
  307. __be32 ip;
  308. int err;
  309. if (!(ndm->ndm_state & (NUD_PERMANENT|NUD_REACHABLE))) {
  310. pr_info("RTM_NEWNEIGH with invalid state %#x\n",
  311. ndm->ndm_state);
  312. return -EINVAL;
  313. }
  314. if (tb[NDA_DST] == NULL)
  315. return -EINVAL;
  316. if (nla_len(tb[NDA_DST]) != sizeof(__be32))
  317. return -EAFNOSUPPORT;
  318. ip = nla_get_be32(tb[NDA_DST]);
  319. spin_lock_bh(&vxlan->hash_lock);
  320. err = vxlan_fdb_create(vxlan, addr, ip, ndm->ndm_state, flags);
  321. spin_unlock_bh(&vxlan->hash_lock);
  322. return err;
  323. }
  324. /* Delete entry (via netlink) */
  325. static int vxlan_fdb_delete(struct ndmsg *ndm, struct net_device *dev,
  326. const unsigned char *addr)
  327. {
  328. struct vxlan_dev *vxlan = netdev_priv(dev);
  329. struct vxlan_fdb *f;
  330. int err = -ENOENT;
  331. spin_lock_bh(&vxlan->hash_lock);
  332. f = vxlan_find_mac(vxlan, addr);
  333. if (f) {
  334. vxlan_fdb_destroy(vxlan, f);
  335. err = 0;
  336. }
  337. spin_unlock_bh(&vxlan->hash_lock);
  338. return err;
  339. }
  340. /* Dump forwarding table */
  341. static int vxlan_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
  342. struct net_device *dev, int idx)
  343. {
  344. struct vxlan_dev *vxlan = netdev_priv(dev);
  345. unsigned int h;
  346. for (h = 0; h < FDB_HASH_SIZE; ++h) {
  347. struct vxlan_fdb *f;
  348. struct hlist_node *n;
  349. int err;
  350. hlist_for_each_entry_rcu(f, n, &vxlan->fdb_head[h], hlist) {
  351. if (idx < cb->args[0])
  352. goto skip;
  353. err = vxlan_fdb_info(skb, vxlan, f,
  354. NETLINK_CB(cb->skb).portid,
  355. cb->nlh->nlmsg_seq,
  356. RTM_NEWNEIGH,
  357. NLM_F_MULTI);
  358. if (err < 0)
  359. break;
  360. skip:
  361. ++idx;
  362. }
  363. }
  364. return idx;
  365. }
  366. /* Watch incoming packets to learn mapping between Ethernet address
  367. * and Tunnel endpoint.
  368. */
  369. static void vxlan_snoop(struct net_device *dev,
  370. __be32 src_ip, const u8 *src_mac)
  371. {
  372. struct vxlan_dev *vxlan = netdev_priv(dev);
  373. struct vxlan_fdb *f;
  374. int err;
  375. f = vxlan_find_mac(vxlan, src_mac);
  376. if (likely(f)) {
  377. f->used = jiffies;
  378. if (likely(f->remote_ip == src_ip))
  379. return;
  380. if (net_ratelimit())
  381. netdev_info(dev,
  382. "%pM migrated from %pI4 to %pI4\n",
  383. src_mac, &f->remote_ip, &src_ip);
  384. f->remote_ip = src_ip;
  385. f->updated = jiffies;
  386. } else {
  387. /* learned new entry */
  388. spin_lock(&vxlan->hash_lock);
  389. err = vxlan_fdb_create(vxlan, src_mac, src_ip,
  390. NUD_REACHABLE,
  391. NLM_F_EXCL|NLM_F_CREATE);
  392. spin_unlock(&vxlan->hash_lock);
  393. }
  394. }
  395. /* See if multicast group is already in use by other ID */
  396. static bool vxlan_group_used(struct vxlan_net *vn,
  397. const struct vxlan_dev *this)
  398. {
  399. const struct vxlan_dev *vxlan;
  400. struct hlist_node *node;
  401. unsigned h;
  402. for (h = 0; h < VNI_HASH_SIZE; ++h)
  403. hlist_for_each_entry(vxlan, node, &vn->vni_list[h], hlist) {
  404. if (vxlan == this)
  405. continue;
  406. if (!netif_running(vxlan->dev))
  407. continue;
  408. if (vxlan->gaddr == this->gaddr)
  409. return true;
  410. }
  411. return false;
  412. }
  413. /* kernel equivalent to IP_ADD_MEMBERSHIP */
  414. static int vxlan_join_group(struct net_device *dev)
  415. {
  416. struct vxlan_dev *vxlan = netdev_priv(dev);
  417. struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
  418. struct sock *sk = vn->sock->sk;
  419. struct ip_mreqn mreq = {
  420. .imr_multiaddr.s_addr = vxlan->gaddr,
  421. };
  422. int err;
  423. /* Already a member of group */
  424. if (vxlan_group_used(vn, vxlan))
  425. return 0;
  426. /* Need to drop RTNL to call multicast join */
  427. rtnl_unlock();
  428. lock_sock(sk);
  429. err = ip_mc_join_group(sk, &mreq);
  430. release_sock(sk);
  431. rtnl_lock();
  432. return err;
  433. }
  434. /* kernel equivalent to IP_DROP_MEMBERSHIP */
  435. static int vxlan_leave_group(struct net_device *dev)
  436. {
  437. struct vxlan_dev *vxlan = netdev_priv(dev);
  438. struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
  439. int err = 0;
  440. struct sock *sk = vn->sock->sk;
  441. struct ip_mreqn mreq = {
  442. .imr_multiaddr.s_addr = vxlan->gaddr,
  443. };
  444. /* Only leave group when last vxlan is done. */
  445. if (vxlan_group_used(vn, vxlan))
  446. return 0;
  447. /* Need to drop RTNL to call multicast leave */
  448. rtnl_unlock();
  449. lock_sock(sk);
  450. err = ip_mc_leave_group(sk, &mreq);
  451. release_sock(sk);
  452. rtnl_lock();
  453. return err;
  454. }
  455. /* Callback from net/ipv4/udp.c to receive packets */
  456. static int vxlan_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
  457. {
  458. struct iphdr *oip;
  459. struct vxlanhdr *vxh;
  460. struct vxlan_dev *vxlan;
  461. struct vxlan_stats *stats;
  462. __u32 vni;
  463. int err;
  464. /* pop off outer UDP header */
  465. __skb_pull(skb, sizeof(struct udphdr));
  466. /* Need Vxlan and inner Ethernet header to be present */
  467. if (!pskb_may_pull(skb, sizeof(struct vxlanhdr)))
  468. goto error;
  469. /* Drop packets with reserved bits set */
  470. vxh = (struct vxlanhdr *) skb->data;
  471. if (vxh->vx_flags != htonl(VXLAN_FLAGS) ||
  472. (vxh->vx_vni & htonl(0xff))) {
  473. netdev_dbg(skb->dev, "invalid vxlan flags=%#x vni=%#x\n",
  474. ntohl(vxh->vx_flags), ntohl(vxh->vx_vni));
  475. goto error;
  476. }
  477. __skb_pull(skb, sizeof(struct vxlanhdr));
  478. /* Is this VNI defined? */
  479. vni = ntohl(vxh->vx_vni) >> 8;
  480. vxlan = vxlan_find_vni(sock_net(sk), vni);
  481. if (!vxlan) {
  482. netdev_dbg(skb->dev, "unknown vni %d\n", vni);
  483. goto drop;
  484. }
  485. if (!pskb_may_pull(skb, ETH_HLEN)) {
  486. vxlan->dev->stats.rx_length_errors++;
  487. vxlan->dev->stats.rx_errors++;
  488. goto drop;
  489. }
  490. skb_reset_mac_header(skb);
  491. /* Re-examine inner Ethernet packet */
  492. oip = ip_hdr(skb);
  493. skb->protocol = eth_type_trans(skb, vxlan->dev);
  494. /* Ignore packet loops (and multicast echo) */
  495. if (compare_ether_addr(eth_hdr(skb)->h_source,
  496. vxlan->dev->dev_addr) == 0)
  497. goto drop;
  498. if (vxlan->flags & VXLAN_F_LEARN)
  499. vxlan_snoop(skb->dev, oip->saddr, eth_hdr(skb)->h_source);
  500. __skb_tunnel_rx(skb, vxlan->dev);
  501. skb_reset_network_header(skb);
  502. skb->ip_summed = CHECKSUM_NONE;
  503. err = IP_ECN_decapsulate(oip, skb);
  504. if (unlikely(err)) {
  505. if (log_ecn_error)
  506. net_info_ratelimited("non-ECT from %pI4 with TOS=%#x\n",
  507. &oip->saddr, oip->tos);
  508. if (err > 1) {
  509. ++vxlan->dev->stats.rx_frame_errors;
  510. ++vxlan->dev->stats.rx_errors;
  511. goto drop;
  512. }
  513. }
  514. stats = this_cpu_ptr(vxlan->stats);
  515. u64_stats_update_begin(&stats->syncp);
  516. stats->rx_packets++;
  517. stats->rx_bytes += skb->len;
  518. u64_stats_update_end(&stats->syncp);
  519. netif_rx(skb);
  520. return 0;
  521. error:
  522. /* Put UDP header back */
  523. __skb_push(skb, sizeof(struct udphdr));
  524. return 1;
  525. drop:
  526. /* Consume bad packet */
  527. kfree_skb(skb);
  528. return 0;
  529. }
  530. static int arp_reduce(struct net_device *dev, struct sk_buff *skb)
  531. {
  532. struct vxlan_dev *vxlan = netdev_priv(dev);
  533. struct arphdr *parp;
  534. u8 *arpptr, *sha;
  535. __be32 sip, tip;
  536. struct neighbour *n;
  537. if (dev->flags & IFF_NOARP)
  538. goto out;
  539. if (!pskb_may_pull(skb, arp_hdr_len(dev))) {
  540. dev->stats.tx_dropped++;
  541. goto out;
  542. }
  543. parp = arp_hdr(skb);
  544. if ((parp->ar_hrd != htons(ARPHRD_ETHER) &&
  545. parp->ar_hrd != htons(ARPHRD_IEEE802)) ||
  546. parp->ar_pro != htons(ETH_P_IP) ||
  547. parp->ar_op != htons(ARPOP_REQUEST) ||
  548. parp->ar_hln != dev->addr_len ||
  549. parp->ar_pln != 4)
  550. goto out;
  551. arpptr = (u8 *)parp + sizeof(struct arphdr);
  552. sha = arpptr;
  553. arpptr += dev->addr_len; /* sha */
  554. memcpy(&sip, arpptr, sizeof(sip));
  555. arpptr += sizeof(sip);
  556. arpptr += dev->addr_len; /* tha */
  557. memcpy(&tip, arpptr, sizeof(tip));
  558. if (ipv4_is_loopback(tip) ||
  559. ipv4_is_multicast(tip))
  560. goto out;
  561. n = neigh_lookup(&arp_tbl, &tip, dev);
  562. if (n) {
  563. struct vxlan_dev *vxlan = netdev_priv(dev);
  564. struct vxlan_fdb *f;
  565. struct sk_buff *reply;
  566. if (!(n->nud_state & NUD_CONNECTED)) {
  567. neigh_release(n);
  568. goto out;
  569. }
  570. f = vxlan_find_mac(vxlan, n->ha);
  571. if (f && f->remote_ip == 0) {
  572. /* bridge-local neighbor */
  573. neigh_release(n);
  574. goto out;
  575. }
  576. reply = arp_create(ARPOP_REPLY, ETH_P_ARP, sip, dev, tip, sha,
  577. n->ha, sha);
  578. neigh_release(n);
  579. skb_reset_mac_header(reply);
  580. __skb_pull(reply, skb_network_offset(reply));
  581. reply->ip_summed = CHECKSUM_UNNECESSARY;
  582. reply->pkt_type = PACKET_HOST;
  583. if (netif_rx_ni(reply) == NET_RX_DROP)
  584. dev->stats.rx_dropped++;
  585. } else if (vxlan->flags & VXLAN_F_L3MISS)
  586. vxlan_ip_miss(dev, tip);
  587. out:
  588. consume_skb(skb);
  589. return NETDEV_TX_OK;
  590. }
  591. static bool route_shortcircuit(struct net_device *dev, struct sk_buff *skb)
  592. {
  593. struct vxlan_dev *vxlan = netdev_priv(dev);
  594. struct neighbour *n;
  595. struct iphdr *pip;
  596. if (is_multicast_ether_addr(eth_hdr(skb)->h_dest))
  597. return false;
  598. n = NULL;
  599. switch (ntohs(eth_hdr(skb)->h_proto)) {
  600. case ETH_P_IP:
  601. if (!pskb_may_pull(skb, sizeof(struct iphdr)))
  602. return false;
  603. pip = ip_hdr(skb);
  604. n = neigh_lookup(&arp_tbl, &pip->daddr, dev);
  605. break;
  606. default:
  607. return false;
  608. }
  609. if (n) {
  610. bool diff;
  611. diff = compare_ether_addr(eth_hdr(skb)->h_dest, n->ha) != 0;
  612. if (diff) {
  613. memcpy(eth_hdr(skb)->h_source, eth_hdr(skb)->h_dest,
  614. dev->addr_len);
  615. memcpy(eth_hdr(skb)->h_dest, n->ha, dev->addr_len);
  616. }
  617. neigh_release(n);
  618. return diff;
  619. } else if (vxlan->flags & VXLAN_F_L3MISS)
  620. vxlan_ip_miss(dev, pip->daddr);
  621. return false;
  622. }
  623. /* Extract dsfield from inner protocol */
  624. static inline u8 vxlan_get_dsfield(const struct iphdr *iph,
  625. const struct sk_buff *skb)
  626. {
  627. if (skb->protocol == htons(ETH_P_IP))
  628. return iph->tos;
  629. else if (skb->protocol == htons(ETH_P_IPV6))
  630. return ipv6_get_dsfield((const struct ipv6hdr *)iph);
  631. else
  632. return 0;
  633. }
  634. /* Propogate ECN bits out */
  635. static inline u8 vxlan_ecn_encap(u8 tos,
  636. const struct iphdr *iph,
  637. const struct sk_buff *skb)
  638. {
  639. u8 inner = vxlan_get_dsfield(iph, skb);
  640. return INET_ECN_encapsulate(tos, inner);
  641. }
  642. static void vxlan_sock_free(struct sk_buff *skb)
  643. {
  644. sock_put(skb->sk);
  645. }
  646. /* On transmit, associate with the tunnel socket */
  647. static void vxlan_set_owner(struct net_device *dev, struct sk_buff *skb)
  648. {
  649. struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
  650. struct sock *sk = vn->sock->sk;
  651. skb_orphan(skb);
  652. sock_hold(sk);
  653. skb->sk = sk;
  654. skb->destructor = vxlan_sock_free;
  655. }
  656. /* Compute source port for outgoing packet
  657. * first choice to use L4 flow hash since it will spread
  658. * better and maybe available from hardware
  659. * secondary choice is to use jhash on the Ethernet header
  660. */
  661. static u16 vxlan_src_port(const struct vxlan_dev *vxlan, struct sk_buff *skb)
  662. {
  663. unsigned int range = (vxlan->port_max - vxlan->port_min) + 1;
  664. u32 hash;
  665. hash = skb_get_rxhash(skb);
  666. if (!hash)
  667. hash = jhash(skb->data, 2 * ETH_ALEN,
  668. (__force u32) skb->protocol);
  669. return (((u64) hash * range) >> 32) + vxlan->port_min;
  670. }
  671. /* Transmit local packets over Vxlan
  672. *
  673. * Outer IP header inherits ECN and DF from inner header.
  674. * Outer UDP destination is the VXLAN assigned port.
  675. * source port is based on hash of flow
  676. */
  677. static netdev_tx_t vxlan_xmit(struct sk_buff *skb, struct net_device *dev)
  678. {
  679. struct vxlan_dev *vxlan = netdev_priv(dev);
  680. struct rtable *rt;
  681. const struct iphdr *old_iph;
  682. struct ethhdr *eth;
  683. struct iphdr *iph;
  684. struct vxlanhdr *vxh;
  685. struct udphdr *uh;
  686. struct flowi4 fl4;
  687. unsigned int pkt_len = skb->len;
  688. __be32 dst;
  689. __u16 src_port;
  690. __be16 df = 0;
  691. __u8 tos, ttl;
  692. int err;
  693. bool did_rsc = false;
  694. const struct vxlan_fdb *f;
  695. skb_reset_mac_header(skb);
  696. eth = eth_hdr(skb);
  697. if ((vxlan->flags & VXLAN_F_PROXY) && ntohs(eth->h_proto) == ETH_P_ARP)
  698. return arp_reduce(dev, skb);
  699. else if ((vxlan->flags&VXLAN_F_RSC) && ntohs(eth->h_proto) == ETH_P_IP)
  700. did_rsc = route_shortcircuit(dev, skb);
  701. f = vxlan_find_mac(vxlan, eth->h_dest);
  702. if (f == NULL) {
  703. did_rsc = false;
  704. dst = vxlan->gaddr;
  705. if (!dst && (vxlan->flags & VXLAN_F_L2MISS) &&
  706. !is_multicast_ether_addr(eth->h_dest))
  707. vxlan_fdb_miss(vxlan, eth->h_dest);
  708. } else
  709. dst = f->remote_ip;
  710. if (!dst) {
  711. if (did_rsc) {
  712. __skb_pull(skb, skb_network_offset(skb));
  713. skb->ip_summed = CHECKSUM_NONE;
  714. skb->pkt_type = PACKET_HOST;
  715. /* short-circuited back to local bridge */
  716. if (netif_rx(skb) == NET_RX_SUCCESS) {
  717. struct vxlan_stats *stats =
  718. this_cpu_ptr(vxlan->stats);
  719. u64_stats_update_begin(&stats->syncp);
  720. stats->tx_packets++;
  721. stats->tx_bytes += pkt_len;
  722. u64_stats_update_end(&stats->syncp);
  723. } else {
  724. dev->stats.tx_errors++;
  725. dev->stats.tx_aborted_errors++;
  726. }
  727. return NETDEV_TX_OK;
  728. }
  729. goto drop;
  730. }
  731. /* Need space for new headers (invalidates iph ptr) */
  732. if (skb_cow_head(skb, VXLAN_HEADROOM))
  733. goto drop;
  734. old_iph = ip_hdr(skb);
  735. ttl = vxlan->ttl;
  736. if (!ttl && IN_MULTICAST(ntohl(dst)))
  737. ttl = 1;
  738. tos = vxlan->tos;
  739. if (tos == 1)
  740. tos = vxlan_get_dsfield(old_iph, skb);
  741. src_port = vxlan_src_port(vxlan, skb);
  742. memset(&fl4, 0, sizeof(fl4));
  743. fl4.flowi4_oif = vxlan->link;
  744. fl4.flowi4_tos = RT_TOS(tos);
  745. fl4.daddr = dst;
  746. fl4.saddr = vxlan->saddr;
  747. rt = ip_route_output_key(dev_net(dev), &fl4);
  748. if (IS_ERR(rt)) {
  749. netdev_dbg(dev, "no route to %pI4\n", &dst);
  750. dev->stats.tx_carrier_errors++;
  751. goto tx_error;
  752. }
  753. if (rt->dst.dev == dev) {
  754. netdev_dbg(dev, "circular route to %pI4\n", &dst);
  755. ip_rt_put(rt);
  756. dev->stats.collisions++;
  757. goto tx_error;
  758. }
  759. memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
  760. IPCB(skb)->flags &= ~(IPSKB_XFRM_TUNNEL_SIZE | IPSKB_XFRM_TRANSFORMED |
  761. IPSKB_REROUTED);
  762. skb_dst_drop(skb);
  763. skb_dst_set(skb, &rt->dst);
  764. vxh = (struct vxlanhdr *) __skb_push(skb, sizeof(*vxh));
  765. vxh->vx_flags = htonl(VXLAN_FLAGS);
  766. vxh->vx_vni = htonl(vxlan->vni << 8);
  767. __skb_push(skb, sizeof(*uh));
  768. skb_reset_transport_header(skb);
  769. uh = udp_hdr(skb);
  770. uh->dest = htons(vxlan_port);
  771. uh->source = htons(src_port);
  772. uh->len = htons(skb->len);
  773. uh->check = 0;
  774. __skb_push(skb, sizeof(*iph));
  775. skb_reset_network_header(skb);
  776. iph = ip_hdr(skb);
  777. iph->version = 4;
  778. iph->ihl = sizeof(struct iphdr) >> 2;
  779. iph->frag_off = df;
  780. iph->protocol = IPPROTO_UDP;
  781. iph->tos = vxlan_ecn_encap(tos, old_iph, skb);
  782. iph->daddr = dst;
  783. iph->saddr = fl4.saddr;
  784. iph->ttl = ttl ? : ip4_dst_hoplimit(&rt->dst);
  785. vxlan_set_owner(dev, skb);
  786. /* See iptunnel_xmit() */
  787. skb->ip_summed = CHECKSUM_NONE;
  788. ip_select_ident(iph, &rt->dst, NULL);
  789. err = ip_local_out(skb);
  790. if (likely(net_xmit_eval(err) == 0)) {
  791. struct vxlan_stats *stats = this_cpu_ptr(vxlan->stats);
  792. u64_stats_update_begin(&stats->syncp);
  793. stats->tx_packets++;
  794. stats->tx_bytes += pkt_len;
  795. u64_stats_update_end(&stats->syncp);
  796. } else {
  797. dev->stats.tx_errors++;
  798. dev->stats.tx_aborted_errors++;
  799. }
  800. return NETDEV_TX_OK;
  801. drop:
  802. dev->stats.tx_dropped++;
  803. goto tx_free;
  804. tx_error:
  805. dev->stats.tx_errors++;
  806. tx_free:
  807. dev_kfree_skb(skb);
  808. return NETDEV_TX_OK;
  809. }
  810. /* Walk the forwarding table and purge stale entries */
  811. static void vxlan_cleanup(unsigned long arg)
  812. {
  813. struct vxlan_dev *vxlan = (struct vxlan_dev *) arg;
  814. unsigned long next_timer = jiffies + FDB_AGE_INTERVAL;
  815. unsigned int h;
  816. if (!netif_running(vxlan->dev))
  817. return;
  818. spin_lock_bh(&vxlan->hash_lock);
  819. for (h = 0; h < FDB_HASH_SIZE; ++h) {
  820. struct hlist_node *p, *n;
  821. hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
  822. struct vxlan_fdb *f
  823. = container_of(p, struct vxlan_fdb, hlist);
  824. unsigned long timeout;
  825. if (f->state & NUD_PERMANENT)
  826. continue;
  827. timeout = f->used + vxlan->age_interval * HZ;
  828. if (time_before_eq(timeout, jiffies)) {
  829. netdev_dbg(vxlan->dev,
  830. "garbage collect %pM\n",
  831. f->eth_addr);
  832. f->state = NUD_STALE;
  833. vxlan_fdb_destroy(vxlan, f);
  834. } else if (time_before(timeout, next_timer))
  835. next_timer = timeout;
  836. }
  837. }
  838. spin_unlock_bh(&vxlan->hash_lock);
  839. mod_timer(&vxlan->age_timer, next_timer);
  840. }
  841. /* Setup stats when device is created */
  842. static int vxlan_init(struct net_device *dev)
  843. {
  844. struct vxlan_dev *vxlan = netdev_priv(dev);
  845. vxlan->stats = alloc_percpu(struct vxlan_stats);
  846. if (!vxlan->stats)
  847. return -ENOMEM;
  848. return 0;
  849. }
  850. /* Start ageing timer and join group when device is brought up */
  851. static int vxlan_open(struct net_device *dev)
  852. {
  853. struct vxlan_dev *vxlan = netdev_priv(dev);
  854. int err;
  855. if (vxlan->gaddr) {
  856. err = vxlan_join_group(dev);
  857. if (err)
  858. return err;
  859. }
  860. if (vxlan->age_interval)
  861. mod_timer(&vxlan->age_timer, jiffies + FDB_AGE_INTERVAL);
  862. return 0;
  863. }
  864. /* Purge the forwarding table */
  865. static void vxlan_flush(struct vxlan_dev *vxlan)
  866. {
  867. unsigned h;
  868. spin_lock_bh(&vxlan->hash_lock);
  869. for (h = 0; h < FDB_HASH_SIZE; ++h) {
  870. struct hlist_node *p, *n;
  871. hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
  872. struct vxlan_fdb *f
  873. = container_of(p, struct vxlan_fdb, hlist);
  874. vxlan_fdb_destroy(vxlan, f);
  875. }
  876. }
  877. spin_unlock_bh(&vxlan->hash_lock);
  878. }
  879. /* Cleanup timer and forwarding table on shutdown */
  880. static int vxlan_stop(struct net_device *dev)
  881. {
  882. struct vxlan_dev *vxlan = netdev_priv(dev);
  883. if (vxlan->gaddr)
  884. vxlan_leave_group(dev);
  885. del_timer_sync(&vxlan->age_timer);
  886. vxlan_flush(vxlan);
  887. return 0;
  888. }
  889. /* Merge per-cpu statistics */
  890. static struct rtnl_link_stats64 *vxlan_stats64(struct net_device *dev,
  891. struct rtnl_link_stats64 *stats)
  892. {
  893. struct vxlan_dev *vxlan = netdev_priv(dev);
  894. struct vxlan_stats tmp, sum = { 0 };
  895. unsigned int cpu;
  896. for_each_possible_cpu(cpu) {
  897. unsigned int start;
  898. const struct vxlan_stats *stats
  899. = per_cpu_ptr(vxlan->stats, cpu);
  900. do {
  901. start = u64_stats_fetch_begin_bh(&stats->syncp);
  902. memcpy(&tmp, stats, sizeof(tmp));
  903. } while (u64_stats_fetch_retry_bh(&stats->syncp, start));
  904. sum.tx_bytes += tmp.tx_bytes;
  905. sum.tx_packets += tmp.tx_packets;
  906. sum.rx_bytes += tmp.rx_bytes;
  907. sum.rx_packets += tmp.rx_packets;
  908. }
  909. stats->tx_bytes = sum.tx_bytes;
  910. stats->tx_packets = sum.tx_packets;
  911. stats->rx_bytes = sum.rx_bytes;
  912. stats->rx_packets = sum.rx_packets;
  913. stats->multicast = dev->stats.multicast;
  914. stats->rx_length_errors = dev->stats.rx_length_errors;
  915. stats->rx_frame_errors = dev->stats.rx_frame_errors;
  916. stats->rx_errors = dev->stats.rx_errors;
  917. stats->tx_dropped = dev->stats.tx_dropped;
  918. stats->tx_carrier_errors = dev->stats.tx_carrier_errors;
  919. stats->tx_aborted_errors = dev->stats.tx_aborted_errors;
  920. stats->collisions = dev->stats.collisions;
  921. stats->tx_errors = dev->stats.tx_errors;
  922. return stats;
  923. }
  924. /* Stub, nothing needs to be done. */
  925. static void vxlan_set_multicast_list(struct net_device *dev)
  926. {
  927. }
  928. static const struct net_device_ops vxlan_netdev_ops = {
  929. .ndo_init = vxlan_init,
  930. .ndo_open = vxlan_open,
  931. .ndo_stop = vxlan_stop,
  932. .ndo_start_xmit = vxlan_xmit,
  933. .ndo_get_stats64 = vxlan_stats64,
  934. .ndo_set_rx_mode = vxlan_set_multicast_list,
  935. .ndo_change_mtu = eth_change_mtu,
  936. .ndo_validate_addr = eth_validate_addr,
  937. .ndo_set_mac_address = eth_mac_addr,
  938. .ndo_fdb_add = vxlan_fdb_add,
  939. .ndo_fdb_del = vxlan_fdb_delete,
  940. .ndo_fdb_dump = vxlan_fdb_dump,
  941. };
  942. /* Info for udev, that this is a virtual tunnel endpoint */
  943. static struct device_type vxlan_type = {
  944. .name = "vxlan",
  945. };
  946. static void vxlan_free(struct net_device *dev)
  947. {
  948. struct vxlan_dev *vxlan = netdev_priv(dev);
  949. free_percpu(vxlan->stats);
  950. free_netdev(dev);
  951. }
  952. /* Initialize the device structure. */
  953. static void vxlan_setup(struct net_device *dev)
  954. {
  955. struct vxlan_dev *vxlan = netdev_priv(dev);
  956. unsigned h;
  957. int low, high;
  958. eth_hw_addr_random(dev);
  959. ether_setup(dev);
  960. dev->hard_header_len = ETH_HLEN + VXLAN_HEADROOM;
  961. dev->netdev_ops = &vxlan_netdev_ops;
  962. dev->destructor = vxlan_free;
  963. SET_NETDEV_DEVTYPE(dev, &vxlan_type);
  964. dev->tx_queue_len = 0;
  965. dev->features |= NETIF_F_LLTX;
  966. dev->features |= NETIF_F_NETNS_LOCAL;
  967. dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
  968. spin_lock_init(&vxlan->hash_lock);
  969. init_timer_deferrable(&vxlan->age_timer);
  970. vxlan->age_timer.function = vxlan_cleanup;
  971. vxlan->age_timer.data = (unsigned long) vxlan;
  972. inet_get_local_port_range(&low, &high);
  973. vxlan->port_min = low;
  974. vxlan->port_max = high;
  975. vxlan->dev = dev;
  976. for (h = 0; h < FDB_HASH_SIZE; ++h)
  977. INIT_HLIST_HEAD(&vxlan->fdb_head[h]);
  978. }
  979. static const struct nla_policy vxlan_policy[IFLA_VXLAN_MAX + 1] = {
  980. [IFLA_VXLAN_ID] = { .type = NLA_U32 },
  981. [IFLA_VXLAN_GROUP] = { .len = FIELD_SIZEOF(struct iphdr, daddr) },
  982. [IFLA_VXLAN_LINK] = { .type = NLA_U32 },
  983. [IFLA_VXLAN_LOCAL] = { .len = FIELD_SIZEOF(struct iphdr, saddr) },
  984. [IFLA_VXLAN_TOS] = { .type = NLA_U8 },
  985. [IFLA_VXLAN_TTL] = { .type = NLA_U8 },
  986. [IFLA_VXLAN_LEARNING] = { .type = NLA_U8 },
  987. [IFLA_VXLAN_AGEING] = { .type = NLA_U32 },
  988. [IFLA_VXLAN_LIMIT] = { .type = NLA_U32 },
  989. [IFLA_VXLAN_PORT_RANGE] = { .len = sizeof(struct ifla_vxlan_port_range) },
  990. [IFLA_VXLAN_PROXY] = { .type = NLA_U8 },
  991. [IFLA_VXLAN_RSC] = { .type = NLA_U8 },
  992. [IFLA_VXLAN_L2MISS] = { .type = NLA_U8 },
  993. [IFLA_VXLAN_L3MISS] = { .type = NLA_U8 },
  994. };
  995. static int vxlan_validate(struct nlattr *tb[], struct nlattr *data[])
  996. {
  997. if (tb[IFLA_ADDRESS]) {
  998. if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) {
  999. pr_debug("invalid link address (not ethernet)\n");
  1000. return -EINVAL;
  1001. }
  1002. if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) {
  1003. pr_debug("invalid all zero ethernet address\n");
  1004. return -EADDRNOTAVAIL;
  1005. }
  1006. }
  1007. if (!data)
  1008. return -EINVAL;
  1009. if (data[IFLA_VXLAN_ID]) {
  1010. __u32 id = nla_get_u32(data[IFLA_VXLAN_ID]);
  1011. if (id >= VXLAN_VID_MASK)
  1012. return -ERANGE;
  1013. }
  1014. if (data[IFLA_VXLAN_GROUP]) {
  1015. __be32 gaddr = nla_get_be32(data[IFLA_VXLAN_GROUP]);
  1016. if (!IN_MULTICAST(ntohl(gaddr))) {
  1017. pr_debug("group address is not IPv4 multicast\n");
  1018. return -EADDRNOTAVAIL;
  1019. }
  1020. }
  1021. if (data[IFLA_VXLAN_PORT_RANGE]) {
  1022. const struct ifla_vxlan_port_range *p
  1023. = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
  1024. if (ntohs(p->high) < ntohs(p->low)) {
  1025. pr_debug("port range %u .. %u not valid\n",
  1026. ntohs(p->low), ntohs(p->high));
  1027. return -EINVAL;
  1028. }
  1029. }
  1030. return 0;
  1031. }
  1032. static int vxlan_newlink(struct net *net, struct net_device *dev,
  1033. struct nlattr *tb[], struct nlattr *data[])
  1034. {
  1035. struct vxlan_dev *vxlan = netdev_priv(dev);
  1036. __u32 vni;
  1037. int err;
  1038. if (!data[IFLA_VXLAN_ID])
  1039. return -EINVAL;
  1040. vni = nla_get_u32(data[IFLA_VXLAN_ID]);
  1041. if (vxlan_find_vni(net, vni)) {
  1042. pr_info("duplicate VNI %u\n", vni);
  1043. return -EEXIST;
  1044. }
  1045. vxlan->vni = vni;
  1046. if (data[IFLA_VXLAN_GROUP])
  1047. vxlan->gaddr = nla_get_be32(data[IFLA_VXLAN_GROUP]);
  1048. if (data[IFLA_VXLAN_LOCAL])
  1049. vxlan->saddr = nla_get_be32(data[IFLA_VXLAN_LOCAL]);
  1050. if (data[IFLA_VXLAN_LINK] &&
  1051. (vxlan->link = nla_get_u32(data[IFLA_VXLAN_LINK]))) {
  1052. struct net_device *lowerdev
  1053. = __dev_get_by_index(net, vxlan->link);
  1054. if (!lowerdev) {
  1055. pr_info("ifindex %d does not exist\n", vxlan->link);
  1056. return -ENODEV;
  1057. }
  1058. if (!tb[IFLA_MTU])
  1059. dev->mtu = lowerdev->mtu - VXLAN_HEADROOM;
  1060. /* update header length based on lower device */
  1061. dev->hard_header_len = lowerdev->hard_header_len +
  1062. VXLAN_HEADROOM;
  1063. }
  1064. if (data[IFLA_VXLAN_TOS])
  1065. vxlan->tos = nla_get_u8(data[IFLA_VXLAN_TOS]);
  1066. if (data[IFLA_VXLAN_TTL])
  1067. vxlan->ttl = nla_get_u8(data[IFLA_VXLAN_TTL]);
  1068. if (!data[IFLA_VXLAN_LEARNING] || nla_get_u8(data[IFLA_VXLAN_LEARNING]))
  1069. vxlan->flags |= VXLAN_F_LEARN;
  1070. if (data[IFLA_VXLAN_AGEING])
  1071. vxlan->age_interval = nla_get_u32(data[IFLA_VXLAN_AGEING]);
  1072. else
  1073. vxlan->age_interval = FDB_AGE_DEFAULT;
  1074. if (data[IFLA_VXLAN_PROXY] && nla_get_u8(data[IFLA_VXLAN_PROXY]))
  1075. vxlan->flags |= VXLAN_F_PROXY;
  1076. if (data[IFLA_VXLAN_RSC] && nla_get_u8(data[IFLA_VXLAN_RSC]))
  1077. vxlan->flags |= VXLAN_F_RSC;
  1078. if (data[IFLA_VXLAN_L2MISS] && nla_get_u8(data[IFLA_VXLAN_L2MISS]))
  1079. vxlan->flags |= VXLAN_F_L2MISS;
  1080. if (data[IFLA_VXLAN_L3MISS] && nla_get_u8(data[IFLA_VXLAN_L3MISS]))
  1081. vxlan->flags |= VXLAN_F_L3MISS;
  1082. if (data[IFLA_VXLAN_LIMIT])
  1083. vxlan->addrmax = nla_get_u32(data[IFLA_VXLAN_LIMIT]);
  1084. if (data[IFLA_VXLAN_PORT_RANGE]) {
  1085. const struct ifla_vxlan_port_range *p
  1086. = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
  1087. vxlan->port_min = ntohs(p->low);
  1088. vxlan->port_max = ntohs(p->high);
  1089. }
  1090. err = register_netdevice(dev);
  1091. if (!err)
  1092. hlist_add_head_rcu(&vxlan->hlist, vni_head(net, vxlan->vni));
  1093. return err;
  1094. }
  1095. static void vxlan_dellink(struct net_device *dev, struct list_head *head)
  1096. {
  1097. struct vxlan_dev *vxlan = netdev_priv(dev);
  1098. hlist_del_rcu(&vxlan->hlist);
  1099. unregister_netdevice_queue(dev, head);
  1100. }
  1101. static size_t vxlan_get_size(const struct net_device *dev)
  1102. {
  1103. return nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_ID */
  1104. nla_total_size(sizeof(__be32)) +/* IFLA_VXLAN_GROUP */
  1105. nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LINK */
  1106. nla_total_size(sizeof(__be32))+ /* IFLA_VXLAN_LOCAL */
  1107. nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TTL */
  1108. nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TOS */
  1109. nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_LEARNING */
  1110. nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_PROXY */
  1111. nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_RSC */
  1112. nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L2MISS */
  1113. nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L3MISS */
  1114. nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_AGEING */
  1115. nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LIMIT */
  1116. nla_total_size(sizeof(struct ifla_vxlan_port_range)) +
  1117. 0;
  1118. }
  1119. static int vxlan_fill_info(struct sk_buff *skb, const struct net_device *dev)
  1120. {
  1121. const struct vxlan_dev *vxlan = netdev_priv(dev);
  1122. struct ifla_vxlan_port_range ports = {
  1123. .low = htons(vxlan->port_min),
  1124. .high = htons(vxlan->port_max),
  1125. };
  1126. if (nla_put_u32(skb, IFLA_VXLAN_ID, vxlan->vni))
  1127. goto nla_put_failure;
  1128. if (vxlan->gaddr && nla_put_be32(skb, IFLA_VXLAN_GROUP, vxlan->gaddr))
  1129. goto nla_put_failure;
  1130. if (vxlan->link && nla_put_u32(skb, IFLA_VXLAN_LINK, vxlan->link))
  1131. goto nla_put_failure;
  1132. if (vxlan->saddr && nla_put_be32(skb, IFLA_VXLAN_LOCAL, vxlan->saddr))
  1133. goto nla_put_failure;
  1134. if (nla_put_u8(skb, IFLA_VXLAN_TTL, vxlan->ttl) ||
  1135. nla_put_u8(skb, IFLA_VXLAN_TOS, vxlan->tos) ||
  1136. nla_put_u8(skb, IFLA_VXLAN_LEARNING,
  1137. !!(vxlan->flags & VXLAN_F_LEARN)) ||
  1138. nla_put_u8(skb, IFLA_VXLAN_PROXY,
  1139. !!(vxlan->flags & VXLAN_F_PROXY)) ||
  1140. nla_put_u8(skb, IFLA_VXLAN_RSC, !!(vxlan->flags & VXLAN_F_RSC)) ||
  1141. nla_put_u8(skb, IFLA_VXLAN_L2MISS,
  1142. !!(vxlan->flags & VXLAN_F_L2MISS)) ||
  1143. nla_put_u8(skb, IFLA_VXLAN_L3MISS,
  1144. !!(vxlan->flags & VXLAN_F_L3MISS)) ||
  1145. nla_put_u32(skb, IFLA_VXLAN_AGEING, vxlan->age_interval) ||
  1146. nla_put_u32(skb, IFLA_VXLAN_LIMIT, vxlan->addrmax))
  1147. goto nla_put_failure;
  1148. if (nla_put(skb, IFLA_VXLAN_PORT_RANGE, sizeof(ports), &ports))
  1149. goto nla_put_failure;
  1150. return 0;
  1151. nla_put_failure:
  1152. return -EMSGSIZE;
  1153. }
  1154. static struct rtnl_link_ops vxlan_link_ops __read_mostly = {
  1155. .kind = "vxlan",
  1156. .maxtype = IFLA_VXLAN_MAX,
  1157. .policy = vxlan_policy,
  1158. .priv_size = sizeof(struct vxlan_dev),
  1159. .setup = vxlan_setup,
  1160. .validate = vxlan_validate,
  1161. .newlink = vxlan_newlink,
  1162. .dellink = vxlan_dellink,
  1163. .get_size = vxlan_get_size,
  1164. .fill_info = vxlan_fill_info,
  1165. };
  1166. static __net_init int vxlan_init_net(struct net *net)
  1167. {
  1168. struct vxlan_net *vn = net_generic(net, vxlan_net_id);
  1169. struct sock *sk;
  1170. struct sockaddr_in vxlan_addr = {
  1171. .sin_family = AF_INET,
  1172. .sin_addr.s_addr = htonl(INADDR_ANY),
  1173. };
  1174. int rc;
  1175. unsigned h;
  1176. /* Create UDP socket for encapsulation receive. */
  1177. rc = sock_create_kern(AF_INET, SOCK_DGRAM, IPPROTO_UDP, &vn->sock);
  1178. if (rc < 0) {
  1179. pr_debug("UDP socket create failed\n");
  1180. return rc;
  1181. }
  1182. /* Put in proper namespace */
  1183. sk = vn->sock->sk;
  1184. sk_change_net(sk, net);
  1185. vxlan_addr.sin_port = htons(vxlan_port);
  1186. rc = kernel_bind(vn->sock, (struct sockaddr *) &vxlan_addr,
  1187. sizeof(vxlan_addr));
  1188. if (rc < 0) {
  1189. pr_debug("bind for UDP socket %pI4:%u (%d)\n",
  1190. &vxlan_addr.sin_addr, ntohs(vxlan_addr.sin_port), rc);
  1191. sk_release_kernel(sk);
  1192. vn->sock = NULL;
  1193. return rc;
  1194. }
  1195. /* Disable multicast loopback */
  1196. inet_sk(sk)->mc_loop = 0;
  1197. /* Mark socket as an encapsulation socket. */
  1198. udp_sk(sk)->encap_type = 1;
  1199. udp_sk(sk)->encap_rcv = vxlan_udp_encap_recv;
  1200. udp_encap_enable();
  1201. for (h = 0; h < VNI_HASH_SIZE; ++h)
  1202. INIT_HLIST_HEAD(&vn->vni_list[h]);
  1203. return 0;
  1204. }
  1205. static __net_exit void vxlan_exit_net(struct net *net)
  1206. {
  1207. struct vxlan_net *vn = net_generic(net, vxlan_net_id);
  1208. if (vn->sock) {
  1209. sk_release_kernel(vn->sock->sk);
  1210. vn->sock = NULL;
  1211. }
  1212. }
  1213. static struct pernet_operations vxlan_net_ops = {
  1214. .init = vxlan_init_net,
  1215. .exit = vxlan_exit_net,
  1216. .id = &vxlan_net_id,
  1217. .size = sizeof(struct vxlan_net),
  1218. };
  1219. static int __init vxlan_init_module(void)
  1220. {
  1221. int rc;
  1222. get_random_bytes(&vxlan_salt, sizeof(vxlan_salt));
  1223. rc = register_pernet_device(&vxlan_net_ops);
  1224. if (rc)
  1225. goto out1;
  1226. rc = rtnl_link_register(&vxlan_link_ops);
  1227. if (rc)
  1228. goto out2;
  1229. return 0;
  1230. out2:
  1231. unregister_pernet_device(&vxlan_net_ops);
  1232. out1:
  1233. return rc;
  1234. }
  1235. module_init(vxlan_init_module);
  1236. static void __exit vxlan_cleanup_module(void)
  1237. {
  1238. rtnl_link_unregister(&vxlan_link_ops);
  1239. unregister_pernet_device(&vxlan_net_ops);
  1240. }
  1241. module_exit(vxlan_cleanup_module);
  1242. MODULE_LICENSE("GPL");
  1243. MODULE_VERSION(VXLAN_VERSION);
  1244. MODULE_AUTHOR("Stephen Hemminger <shemminger@vyatta.com>");
  1245. MODULE_ALIAS_RTNL_LINK("vxlan");