vxlan.c 49 KB

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