vxlan.c 45 KB

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