vxlan.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310
  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/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. /* IP header + UDP + VXLAN + Ethernet header */
  49. #define VXLAN_HEADROOM (20 + 8 + 8 + 14)
  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. /* Is this VNI defined? */
  449. vni = ntohl(vxh->vx_vni) >> 8;
  450. vxlan = vxlan_find_vni(sock_net(sk), vni);
  451. if (!vxlan) {
  452. netdev_dbg(skb->dev, "unknown vni %d\n", vni);
  453. goto drop;
  454. }
  455. if (!pskb_may_pull(skb, ETH_HLEN)) {
  456. vxlan->dev->stats.rx_length_errors++;
  457. vxlan->dev->stats.rx_errors++;
  458. goto drop;
  459. }
  460. /* Re-examine inner Ethernet packet */
  461. oip = ip_hdr(skb);
  462. skb->protocol = eth_type_trans(skb, vxlan->dev);
  463. /* Ignore packet loops (and multicast echo) */
  464. if (compare_ether_addr(eth_hdr(skb)->h_source,
  465. vxlan->dev->dev_addr) == 0)
  466. goto drop;
  467. if (vxlan->learn)
  468. vxlan_snoop(skb->dev, oip->saddr, eth_hdr(skb)->h_source);
  469. __skb_tunnel_rx(skb, vxlan->dev);
  470. skb_reset_network_header(skb);
  471. skb->ip_summed = CHECKSUM_NONE;
  472. err = IP_ECN_decapsulate(oip, skb);
  473. if (unlikely(err)) {
  474. if (log_ecn_error)
  475. net_info_ratelimited("non-ECT from %pI4 with TOS=%#x\n",
  476. &oip->saddr, oip->tos);
  477. if (err > 1) {
  478. ++vxlan->dev->stats.rx_frame_errors;
  479. ++vxlan->dev->stats.rx_errors;
  480. goto drop;
  481. }
  482. }
  483. stats = this_cpu_ptr(vxlan->stats);
  484. u64_stats_update_begin(&stats->syncp);
  485. stats->rx_packets++;
  486. stats->rx_bytes += skb->len;
  487. u64_stats_update_end(&stats->syncp);
  488. netif_rx(skb);
  489. return 0;
  490. error:
  491. /* Put UDP header back */
  492. __skb_push(skb, sizeof(struct udphdr));
  493. return 1;
  494. drop:
  495. /* Consume bad packet */
  496. kfree_skb(skb);
  497. return 0;
  498. }
  499. /* Extract dsfield from inner protocol */
  500. static inline u8 vxlan_get_dsfield(const struct iphdr *iph,
  501. const struct sk_buff *skb)
  502. {
  503. if (skb->protocol == htons(ETH_P_IP))
  504. return iph->tos;
  505. else if (skb->protocol == htons(ETH_P_IPV6))
  506. return ipv6_get_dsfield((const struct ipv6hdr *)iph);
  507. else
  508. return 0;
  509. }
  510. /* Propogate ECN bits out */
  511. static inline u8 vxlan_ecn_encap(u8 tos,
  512. const struct iphdr *iph,
  513. const struct sk_buff *skb)
  514. {
  515. u8 inner = vxlan_get_dsfield(iph, skb);
  516. return INET_ECN_encapsulate(tos, inner);
  517. }
  518. static __be32 vxlan_find_dst(struct vxlan_dev *vxlan, struct sk_buff *skb)
  519. {
  520. const struct ethhdr *eth = (struct ethhdr *) skb->data;
  521. const struct vxlan_fdb *f;
  522. if (is_multicast_ether_addr(eth->h_dest))
  523. return vxlan->gaddr;
  524. f = vxlan_find_mac(vxlan, eth->h_dest);
  525. if (f)
  526. return f->remote_ip;
  527. else
  528. return vxlan->gaddr;
  529. }
  530. static void vxlan_sock_free(struct sk_buff *skb)
  531. {
  532. sock_put(skb->sk);
  533. }
  534. /* On transmit, associate with the tunnel socket */
  535. static void vxlan_set_owner(struct net_device *dev, struct sk_buff *skb)
  536. {
  537. struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
  538. struct sock *sk = vn->sock->sk;
  539. skb_orphan(skb);
  540. sock_hold(sk);
  541. skb->sk = sk;
  542. skb->destructor = vxlan_sock_free;
  543. }
  544. /* Compute source port for outgoing packet
  545. * first choice to use L4 flow hash since it will spread
  546. * better and maybe available from hardware
  547. * secondary choice is to use jhash on the Ethernet header
  548. */
  549. static u16 vxlan_src_port(const struct vxlan_dev *vxlan, struct sk_buff *skb)
  550. {
  551. unsigned int range = (vxlan->port_max - vxlan->port_min) + 1;
  552. u32 hash;
  553. hash = skb_get_rxhash(skb);
  554. if (!hash)
  555. hash = jhash(skb->data, 2 * ETH_ALEN,
  556. (__force u32) skb->protocol);
  557. return (((u64) hash * range) >> 32) + vxlan->port_min;
  558. }
  559. /* Transmit local packets over Vxlan
  560. *
  561. * Outer IP header inherits ECN and DF from inner header.
  562. * Outer UDP destination is the VXLAN assigned port.
  563. * source port is based on hash of flow
  564. */
  565. static netdev_tx_t vxlan_xmit(struct sk_buff *skb, struct net_device *dev)
  566. {
  567. struct vxlan_dev *vxlan = netdev_priv(dev);
  568. struct rtable *rt;
  569. const struct iphdr *old_iph;
  570. struct iphdr *iph;
  571. struct vxlanhdr *vxh;
  572. struct udphdr *uh;
  573. struct flowi4 fl4;
  574. unsigned int pkt_len = skb->len;
  575. __be32 dst;
  576. __u16 src_port;
  577. __be16 df = 0;
  578. __u8 tos, ttl;
  579. int err;
  580. dst = vxlan_find_dst(vxlan, skb);
  581. if (!dst)
  582. goto drop;
  583. /* Need space for new headers (invalidates iph ptr) */
  584. if (skb_cow_head(skb, VXLAN_HEADROOM))
  585. goto drop;
  586. old_iph = ip_hdr(skb);
  587. ttl = vxlan->ttl;
  588. if (!ttl && IN_MULTICAST(ntohl(dst)))
  589. ttl = 1;
  590. tos = vxlan->tos;
  591. if (tos == 1)
  592. tos = vxlan_get_dsfield(old_iph, skb);
  593. src_port = vxlan_src_port(vxlan, skb);
  594. memset(&fl4, 0, sizeof(fl4));
  595. fl4.flowi4_oif = vxlan->link;
  596. fl4.flowi4_tos = RT_TOS(tos);
  597. fl4.daddr = dst;
  598. fl4.saddr = vxlan->saddr;
  599. rt = ip_route_output_key(dev_net(dev), &fl4);
  600. if (IS_ERR(rt)) {
  601. netdev_dbg(dev, "no route to %pI4\n", &dst);
  602. dev->stats.tx_carrier_errors++;
  603. goto tx_error;
  604. }
  605. if (rt->dst.dev == dev) {
  606. netdev_dbg(dev, "circular route to %pI4\n", &dst);
  607. ip_rt_put(rt);
  608. dev->stats.collisions++;
  609. goto tx_error;
  610. }
  611. memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
  612. IPCB(skb)->flags &= ~(IPSKB_XFRM_TUNNEL_SIZE | IPSKB_XFRM_TRANSFORMED |
  613. IPSKB_REROUTED);
  614. skb_dst_drop(skb);
  615. skb_dst_set(skb, &rt->dst);
  616. vxh = (struct vxlanhdr *) __skb_push(skb, sizeof(*vxh));
  617. vxh->vx_flags = htonl(VXLAN_FLAGS);
  618. vxh->vx_vni = htonl(vxlan->vni << 8);
  619. __skb_push(skb, sizeof(*uh));
  620. skb_reset_transport_header(skb);
  621. uh = udp_hdr(skb);
  622. uh->dest = htons(vxlan_port);
  623. uh->source = htons(src_port);
  624. uh->len = htons(skb->len);
  625. uh->check = 0;
  626. __skb_push(skb, sizeof(*iph));
  627. skb_reset_network_header(skb);
  628. iph = ip_hdr(skb);
  629. iph->version = 4;
  630. iph->ihl = sizeof(struct iphdr) >> 2;
  631. iph->frag_off = df;
  632. iph->protocol = IPPROTO_UDP;
  633. iph->tos = vxlan_ecn_encap(tos, old_iph, skb);
  634. iph->daddr = dst;
  635. iph->saddr = fl4.saddr;
  636. iph->ttl = ttl ? : ip4_dst_hoplimit(&rt->dst);
  637. vxlan_set_owner(dev, skb);
  638. /* See __IPTUNNEL_XMIT */
  639. skb->ip_summed = CHECKSUM_NONE;
  640. ip_select_ident(iph, &rt->dst, NULL);
  641. err = ip_local_out(skb);
  642. if (likely(net_xmit_eval(err) == 0)) {
  643. struct vxlan_stats *stats = this_cpu_ptr(vxlan->stats);
  644. u64_stats_update_begin(&stats->syncp);
  645. stats->tx_packets++;
  646. stats->tx_bytes += pkt_len;
  647. u64_stats_update_end(&stats->syncp);
  648. } else {
  649. dev->stats.tx_errors++;
  650. dev->stats.tx_aborted_errors++;
  651. }
  652. return NETDEV_TX_OK;
  653. drop:
  654. dev->stats.tx_dropped++;
  655. goto tx_free;
  656. tx_error:
  657. dev->stats.tx_errors++;
  658. tx_free:
  659. dev_kfree_skb(skb);
  660. return NETDEV_TX_OK;
  661. }
  662. /* Walk the forwarding table and purge stale entries */
  663. static void vxlan_cleanup(unsigned long arg)
  664. {
  665. struct vxlan_dev *vxlan = (struct vxlan_dev *) arg;
  666. unsigned long next_timer = jiffies + FDB_AGE_INTERVAL;
  667. unsigned int h;
  668. if (!netif_running(vxlan->dev))
  669. return;
  670. spin_lock_bh(&vxlan->hash_lock);
  671. for (h = 0; h < FDB_HASH_SIZE; ++h) {
  672. struct hlist_node *p, *n;
  673. hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
  674. struct vxlan_fdb *f
  675. = container_of(p, struct vxlan_fdb, hlist);
  676. unsigned long timeout;
  677. if (f->state & NUD_PERMANENT)
  678. continue;
  679. timeout = f->used + vxlan->age_interval * HZ;
  680. if (time_before_eq(timeout, jiffies)) {
  681. netdev_dbg(vxlan->dev,
  682. "garbage collect %pM\n",
  683. f->eth_addr);
  684. f->state = NUD_STALE;
  685. vxlan_fdb_destroy(vxlan, f);
  686. } else if (time_before(timeout, next_timer))
  687. next_timer = timeout;
  688. }
  689. }
  690. spin_unlock_bh(&vxlan->hash_lock);
  691. mod_timer(&vxlan->age_timer, next_timer);
  692. }
  693. /* Setup stats when device is created */
  694. static int vxlan_init(struct net_device *dev)
  695. {
  696. struct vxlan_dev *vxlan = netdev_priv(dev);
  697. vxlan->stats = alloc_percpu(struct vxlan_stats);
  698. if (!vxlan->stats)
  699. return -ENOMEM;
  700. return 0;
  701. }
  702. /* Start ageing timer and join group when device is brought up */
  703. static int vxlan_open(struct net_device *dev)
  704. {
  705. struct vxlan_dev *vxlan = netdev_priv(dev);
  706. int err;
  707. if (vxlan->gaddr) {
  708. err = vxlan_join_group(dev);
  709. if (err)
  710. return err;
  711. }
  712. if (vxlan->age_interval)
  713. mod_timer(&vxlan->age_timer, jiffies + FDB_AGE_INTERVAL);
  714. return 0;
  715. }
  716. /* Purge the forwarding table */
  717. static void vxlan_flush(struct vxlan_dev *vxlan)
  718. {
  719. unsigned h;
  720. spin_lock_bh(&vxlan->hash_lock);
  721. for (h = 0; h < FDB_HASH_SIZE; ++h) {
  722. struct hlist_node *p, *n;
  723. hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
  724. struct vxlan_fdb *f
  725. = container_of(p, struct vxlan_fdb, hlist);
  726. vxlan_fdb_destroy(vxlan, f);
  727. }
  728. }
  729. spin_unlock_bh(&vxlan->hash_lock);
  730. }
  731. /* Cleanup timer and forwarding table on shutdown */
  732. static int vxlan_stop(struct net_device *dev)
  733. {
  734. struct vxlan_dev *vxlan = netdev_priv(dev);
  735. if (vxlan->gaddr)
  736. vxlan_leave_group(dev);
  737. del_timer_sync(&vxlan->age_timer);
  738. vxlan_flush(vxlan);
  739. return 0;
  740. }
  741. /* Merge per-cpu statistics */
  742. static struct rtnl_link_stats64 *vxlan_stats64(struct net_device *dev,
  743. struct rtnl_link_stats64 *stats)
  744. {
  745. struct vxlan_dev *vxlan = netdev_priv(dev);
  746. struct vxlan_stats tmp, sum = { 0 };
  747. unsigned int cpu;
  748. for_each_possible_cpu(cpu) {
  749. unsigned int start;
  750. const struct vxlan_stats *stats
  751. = per_cpu_ptr(vxlan->stats, cpu);
  752. do {
  753. start = u64_stats_fetch_begin_bh(&stats->syncp);
  754. memcpy(&tmp, stats, sizeof(tmp));
  755. } while (u64_stats_fetch_retry_bh(&stats->syncp, start));
  756. sum.tx_bytes += tmp.tx_bytes;
  757. sum.tx_packets += tmp.tx_packets;
  758. sum.rx_bytes += tmp.rx_bytes;
  759. sum.rx_packets += tmp.rx_packets;
  760. }
  761. stats->tx_bytes = sum.tx_bytes;
  762. stats->tx_packets = sum.tx_packets;
  763. stats->rx_bytes = sum.rx_bytes;
  764. stats->rx_packets = sum.rx_packets;
  765. stats->multicast = dev->stats.multicast;
  766. stats->rx_length_errors = dev->stats.rx_length_errors;
  767. stats->rx_frame_errors = dev->stats.rx_frame_errors;
  768. stats->rx_errors = dev->stats.rx_errors;
  769. stats->tx_dropped = dev->stats.tx_dropped;
  770. stats->tx_carrier_errors = dev->stats.tx_carrier_errors;
  771. stats->tx_aborted_errors = dev->stats.tx_aborted_errors;
  772. stats->collisions = dev->stats.collisions;
  773. stats->tx_errors = dev->stats.tx_errors;
  774. return stats;
  775. }
  776. /* Stub, nothing needs to be done. */
  777. static void vxlan_set_multicast_list(struct net_device *dev)
  778. {
  779. }
  780. static const struct net_device_ops vxlan_netdev_ops = {
  781. .ndo_init = vxlan_init,
  782. .ndo_open = vxlan_open,
  783. .ndo_stop = vxlan_stop,
  784. .ndo_start_xmit = vxlan_xmit,
  785. .ndo_get_stats64 = vxlan_stats64,
  786. .ndo_set_rx_mode = vxlan_set_multicast_list,
  787. .ndo_change_mtu = eth_change_mtu,
  788. .ndo_validate_addr = eth_validate_addr,
  789. .ndo_set_mac_address = eth_mac_addr,
  790. .ndo_fdb_add = vxlan_fdb_add,
  791. .ndo_fdb_del = vxlan_fdb_delete,
  792. .ndo_fdb_dump = vxlan_fdb_dump,
  793. };
  794. /* Info for udev, that this is a virtual tunnel endpoint */
  795. static struct device_type vxlan_type = {
  796. .name = "vxlan",
  797. };
  798. static void vxlan_free(struct net_device *dev)
  799. {
  800. struct vxlan_dev *vxlan = netdev_priv(dev);
  801. free_percpu(vxlan->stats);
  802. free_netdev(dev);
  803. }
  804. /* Initialize the device structure. */
  805. static void vxlan_setup(struct net_device *dev)
  806. {
  807. struct vxlan_dev *vxlan = netdev_priv(dev);
  808. unsigned h;
  809. int low, high;
  810. eth_hw_addr_random(dev);
  811. ether_setup(dev);
  812. dev->hard_header_len = ETH_HLEN + VXLAN_HEADROOM;
  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. struct net_device *lowerdev
  901. = __dev_get_by_index(net, vxlan->link);
  902. if (!lowerdev) {
  903. pr_info("ifindex %d does not exist\n", vxlan->link);
  904. return -ENODEV;
  905. }
  906. if (!tb[IFLA_MTU])
  907. dev->mtu = lowerdev->mtu - VXLAN_HEADROOM;
  908. /* update header length based on lower device */
  909. dev->hard_header_len = lowerdev->hard_header_len +
  910. VXLAN_HEADROOM;
  911. }
  912. if (data[IFLA_VXLAN_TOS])
  913. vxlan->tos = nla_get_u8(data[IFLA_VXLAN_TOS]);
  914. if (!data[IFLA_VXLAN_LEARNING] || nla_get_u8(data[IFLA_VXLAN_LEARNING]))
  915. vxlan->learn = true;
  916. if (data[IFLA_VXLAN_AGEING])
  917. vxlan->age_interval = nla_get_u32(data[IFLA_VXLAN_AGEING]);
  918. else
  919. vxlan->age_interval = FDB_AGE_DEFAULT;
  920. if (data[IFLA_VXLAN_LIMIT])
  921. vxlan->addrmax = nla_get_u32(data[IFLA_VXLAN_LIMIT]);
  922. if (data[IFLA_VXLAN_PORT_RANGE]) {
  923. const struct ifla_vxlan_port_range *p
  924. = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
  925. vxlan->port_min = ntohs(p->low);
  926. vxlan->port_max = ntohs(p->high);
  927. }
  928. err = register_netdevice(dev);
  929. if (!err)
  930. hlist_add_head_rcu(&vxlan->hlist, vni_head(net, vxlan->vni));
  931. return err;
  932. }
  933. static void vxlan_dellink(struct net_device *dev, struct list_head *head)
  934. {
  935. struct vxlan_dev *vxlan = netdev_priv(dev);
  936. hlist_del_rcu(&vxlan->hlist);
  937. unregister_netdevice_queue(dev, head);
  938. }
  939. static size_t vxlan_get_size(const struct net_device *dev)
  940. {
  941. return nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_ID */
  942. nla_total_size(sizeof(__be32)) +/* IFLA_VXLAN_GROUP */
  943. nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LINK */
  944. nla_total_size(sizeof(__be32))+ /* IFLA_VXLAN_LOCAL */
  945. nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TTL */
  946. nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TOS */
  947. nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_LEARNING */
  948. nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_AGEING */
  949. nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LIMIT */
  950. nla_total_size(sizeof(struct ifla_vxlan_port_range)) +
  951. 0;
  952. }
  953. static int vxlan_fill_info(struct sk_buff *skb, const struct net_device *dev)
  954. {
  955. const struct vxlan_dev *vxlan = netdev_priv(dev);
  956. struct ifla_vxlan_port_range ports = {
  957. .low = htons(vxlan->port_min),
  958. .high = htons(vxlan->port_max),
  959. };
  960. if (nla_put_u32(skb, IFLA_VXLAN_ID, vxlan->vni))
  961. goto nla_put_failure;
  962. if (vxlan->gaddr && nla_put_be32(skb, IFLA_VXLAN_GROUP, vxlan->gaddr))
  963. goto nla_put_failure;
  964. if (vxlan->link && nla_put_u32(skb, IFLA_VXLAN_LINK, vxlan->link))
  965. goto nla_put_failure;
  966. if (vxlan->saddr && nla_put_be32(skb, IFLA_VXLAN_LOCAL, vxlan->saddr))
  967. goto nla_put_failure;
  968. if (nla_put_u8(skb, IFLA_VXLAN_TTL, vxlan->ttl) ||
  969. nla_put_u8(skb, IFLA_VXLAN_TOS, vxlan->tos) ||
  970. nla_put_u8(skb, IFLA_VXLAN_LEARNING, vxlan->learn) ||
  971. nla_put_u32(skb, IFLA_VXLAN_AGEING, vxlan->age_interval) ||
  972. nla_put_u32(skb, IFLA_VXLAN_LIMIT, vxlan->addrmax))
  973. goto nla_put_failure;
  974. if (nla_put(skb, IFLA_VXLAN_PORT_RANGE, sizeof(ports), &ports))
  975. goto nla_put_failure;
  976. return 0;
  977. nla_put_failure:
  978. return -EMSGSIZE;
  979. }
  980. static struct rtnl_link_ops vxlan_link_ops __read_mostly = {
  981. .kind = "vxlan",
  982. .maxtype = IFLA_VXLAN_MAX,
  983. .policy = vxlan_policy,
  984. .priv_size = sizeof(struct vxlan_dev),
  985. .setup = vxlan_setup,
  986. .validate = vxlan_validate,
  987. .newlink = vxlan_newlink,
  988. .dellink = vxlan_dellink,
  989. .get_size = vxlan_get_size,
  990. .fill_info = vxlan_fill_info,
  991. };
  992. static __net_init int vxlan_init_net(struct net *net)
  993. {
  994. struct vxlan_net *vn = net_generic(net, vxlan_net_id);
  995. struct sock *sk;
  996. struct sockaddr_in vxlan_addr = {
  997. .sin_family = AF_INET,
  998. .sin_addr.s_addr = htonl(INADDR_ANY),
  999. };
  1000. int rc;
  1001. unsigned h;
  1002. /* Create UDP socket for encapsulation receive. */
  1003. rc = sock_create_kern(AF_INET, SOCK_DGRAM, IPPROTO_UDP, &vn->sock);
  1004. if (rc < 0) {
  1005. pr_debug("UDP socket create failed\n");
  1006. return rc;
  1007. }
  1008. /* Put in proper namespace */
  1009. sk = vn->sock->sk;
  1010. sk_change_net(sk, net);
  1011. vxlan_addr.sin_port = htons(vxlan_port);
  1012. rc = kernel_bind(vn->sock, (struct sockaddr *) &vxlan_addr,
  1013. sizeof(vxlan_addr));
  1014. if (rc < 0) {
  1015. pr_debug("bind for UDP socket %pI4:%u (%d)\n",
  1016. &vxlan_addr.sin_addr, ntohs(vxlan_addr.sin_port), rc);
  1017. sk_release_kernel(sk);
  1018. vn->sock = NULL;
  1019. return rc;
  1020. }
  1021. /* Disable multicast loopback */
  1022. inet_sk(sk)->mc_loop = 0;
  1023. /* Mark socket as an encapsulation socket. */
  1024. udp_sk(sk)->encap_type = 1;
  1025. udp_sk(sk)->encap_rcv = vxlan_udp_encap_recv;
  1026. udp_encap_enable();
  1027. for (h = 0; h < VNI_HASH_SIZE; ++h)
  1028. INIT_HLIST_HEAD(&vn->vni_list[h]);
  1029. return 0;
  1030. }
  1031. static __net_exit void vxlan_exit_net(struct net *net)
  1032. {
  1033. struct vxlan_net *vn = net_generic(net, vxlan_net_id);
  1034. if (vn->sock) {
  1035. sk_release_kernel(vn->sock->sk);
  1036. vn->sock = NULL;
  1037. }
  1038. }
  1039. static struct pernet_operations vxlan_net_ops = {
  1040. .init = vxlan_init_net,
  1041. .exit = vxlan_exit_net,
  1042. .id = &vxlan_net_id,
  1043. .size = sizeof(struct vxlan_net),
  1044. };
  1045. static int __init vxlan_init_module(void)
  1046. {
  1047. int rc;
  1048. get_random_bytes(&vxlan_salt, sizeof(vxlan_salt));
  1049. rc = register_pernet_device(&vxlan_net_ops);
  1050. if (rc)
  1051. goto out1;
  1052. rc = rtnl_link_register(&vxlan_link_ops);
  1053. if (rc)
  1054. goto out2;
  1055. return 0;
  1056. out2:
  1057. unregister_pernet_device(&vxlan_net_ops);
  1058. out1:
  1059. return rc;
  1060. }
  1061. module_init(vxlan_init_module);
  1062. static void __exit vxlan_cleanup_module(void)
  1063. {
  1064. rtnl_link_unregister(&vxlan_link_ops);
  1065. unregister_pernet_device(&vxlan_net_ops);
  1066. }
  1067. module_exit(vxlan_cleanup_module);
  1068. MODULE_LICENSE("GPL");
  1069. MODULE_VERSION(VXLAN_VERSION);
  1070. MODULE_AUTHOR("Stephen Hemminger <shemminger@vyatta.com>");
  1071. MODULE_ALIAS_RTNL_LINK("vxlan");