vxlan.c 31 KB

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