vxlan.c 31 KB

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