vxlan.c 38 KB

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