af_mpls.c 61 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683
  1. #include <linux/types.h>
  2. #include <linux/skbuff.h>
  3. #include <linux/socket.h>
  4. #include <linux/sysctl.h>
  5. #include <linux/net.h>
  6. #include <linux/module.h>
  7. #include <linux/if_arp.h>
  8. #include <linux/ipv6.h>
  9. #include <linux/mpls.h>
  10. #include <linux/netconf.h>
  11. #include <linux/nospec.h>
  12. #include <linux/vmalloc.h>
  13. #include <linux/percpu.h>
  14. #include <net/ip.h>
  15. #include <net/dst.h>
  16. #include <net/sock.h>
  17. #include <net/arp.h>
  18. #include <net/ip_fib.h>
  19. #include <net/netevent.h>
  20. #include <net/ip_tunnels.h>
  21. #include <net/netns/generic.h>
  22. #if IS_ENABLED(CONFIG_IPV6)
  23. #include <net/ipv6.h>
  24. #endif
  25. #include <net/addrconf.h>
  26. #include <net/nexthop.h>
  27. #include "internal.h"
  28. /* max memory we will use for mpls_route */
  29. #define MAX_MPLS_ROUTE_MEM 4096
  30. /* Maximum number of labels to look ahead at when selecting a path of
  31. * a multipath route
  32. */
  33. #define MAX_MP_SELECT_LABELS 4
  34. #define MPLS_NEIGH_TABLE_UNSPEC (NEIGH_LINK_TABLE + 1)
  35. static int zero = 0;
  36. static int one = 1;
  37. static int label_limit = (1 << 20) - 1;
  38. static int ttl_max = 255;
  39. #if IS_ENABLED(CONFIG_NET_IP_TUNNEL)
  40. static size_t ipgre_mpls_encap_hlen(struct ip_tunnel_encap *e)
  41. {
  42. return sizeof(struct mpls_shim_hdr);
  43. }
  44. static const struct ip_tunnel_encap_ops mpls_iptun_ops = {
  45. .encap_hlen = ipgre_mpls_encap_hlen,
  46. };
  47. static int ipgre_tunnel_encap_add_mpls_ops(void)
  48. {
  49. return ip_tunnel_encap_add_ops(&mpls_iptun_ops, TUNNEL_ENCAP_MPLS);
  50. }
  51. static void ipgre_tunnel_encap_del_mpls_ops(void)
  52. {
  53. ip_tunnel_encap_del_ops(&mpls_iptun_ops, TUNNEL_ENCAP_MPLS);
  54. }
  55. #else
  56. static int ipgre_tunnel_encap_add_mpls_ops(void)
  57. {
  58. return 0;
  59. }
  60. static void ipgre_tunnel_encap_del_mpls_ops(void)
  61. {
  62. }
  63. #endif
  64. static void rtmsg_lfib(int event, u32 label, struct mpls_route *rt,
  65. struct nlmsghdr *nlh, struct net *net, u32 portid,
  66. unsigned int nlm_flags);
  67. static struct mpls_route *mpls_route_input_rcu(struct net *net, unsigned index)
  68. {
  69. struct mpls_route *rt = NULL;
  70. if (index < net->mpls.platform_labels) {
  71. struct mpls_route __rcu **platform_label =
  72. rcu_dereference(net->mpls.platform_label);
  73. rt = rcu_dereference(platform_label[index]);
  74. }
  75. return rt;
  76. }
  77. bool mpls_output_possible(const struct net_device *dev)
  78. {
  79. return dev && (dev->flags & IFF_UP) && netif_carrier_ok(dev);
  80. }
  81. EXPORT_SYMBOL_GPL(mpls_output_possible);
  82. static u8 *__mpls_nh_via(struct mpls_route *rt, struct mpls_nh *nh)
  83. {
  84. return (u8 *)nh + rt->rt_via_offset;
  85. }
  86. static const u8 *mpls_nh_via(const struct mpls_route *rt,
  87. const struct mpls_nh *nh)
  88. {
  89. return __mpls_nh_via((struct mpls_route *)rt, (struct mpls_nh *)nh);
  90. }
  91. static unsigned int mpls_nh_header_size(const struct mpls_nh *nh)
  92. {
  93. /* The size of the layer 2.5 labels to be added for this route */
  94. return nh->nh_labels * sizeof(struct mpls_shim_hdr);
  95. }
  96. unsigned int mpls_dev_mtu(const struct net_device *dev)
  97. {
  98. /* The amount of data the layer 2 frame can hold */
  99. return dev->mtu;
  100. }
  101. EXPORT_SYMBOL_GPL(mpls_dev_mtu);
  102. bool mpls_pkt_too_big(const struct sk_buff *skb, unsigned int mtu)
  103. {
  104. if (skb->len <= mtu)
  105. return false;
  106. if (skb_is_gso(skb) && skb_gso_validate_network_len(skb, mtu))
  107. return false;
  108. return true;
  109. }
  110. EXPORT_SYMBOL_GPL(mpls_pkt_too_big);
  111. void mpls_stats_inc_outucastpkts(struct net_device *dev,
  112. const struct sk_buff *skb)
  113. {
  114. struct mpls_dev *mdev;
  115. if (skb->protocol == htons(ETH_P_MPLS_UC)) {
  116. mdev = mpls_dev_get(dev);
  117. if (mdev)
  118. MPLS_INC_STATS_LEN(mdev, skb->len,
  119. tx_packets,
  120. tx_bytes);
  121. } else if (skb->protocol == htons(ETH_P_IP)) {
  122. IP_UPD_PO_STATS(dev_net(dev), IPSTATS_MIB_OUT, skb->len);
  123. #if IS_ENABLED(CONFIG_IPV6)
  124. } else if (skb->protocol == htons(ETH_P_IPV6)) {
  125. struct inet6_dev *in6dev = __in6_dev_get(dev);
  126. if (in6dev)
  127. IP6_UPD_PO_STATS(dev_net(dev), in6dev,
  128. IPSTATS_MIB_OUT, skb->len);
  129. #endif
  130. }
  131. }
  132. EXPORT_SYMBOL_GPL(mpls_stats_inc_outucastpkts);
  133. static u32 mpls_multipath_hash(struct mpls_route *rt, struct sk_buff *skb)
  134. {
  135. struct mpls_entry_decoded dec;
  136. unsigned int mpls_hdr_len = 0;
  137. struct mpls_shim_hdr *hdr;
  138. bool eli_seen = false;
  139. int label_index;
  140. u32 hash = 0;
  141. for (label_index = 0; label_index < MAX_MP_SELECT_LABELS;
  142. label_index++) {
  143. mpls_hdr_len += sizeof(*hdr);
  144. if (!pskb_may_pull(skb, mpls_hdr_len))
  145. break;
  146. /* Read and decode the current label */
  147. hdr = mpls_hdr(skb) + label_index;
  148. dec = mpls_entry_decode(hdr);
  149. /* RFC6790 - reserved labels MUST NOT be used as keys
  150. * for the load-balancing function
  151. */
  152. if (likely(dec.label >= MPLS_LABEL_FIRST_UNRESERVED)) {
  153. hash = jhash_1word(dec.label, hash);
  154. /* The entropy label follows the entropy label
  155. * indicator, so this means that the entropy
  156. * label was just added to the hash - no need to
  157. * go any deeper either in the label stack or in the
  158. * payload
  159. */
  160. if (eli_seen)
  161. break;
  162. } else if (dec.label == MPLS_LABEL_ENTROPY) {
  163. eli_seen = true;
  164. }
  165. if (!dec.bos)
  166. continue;
  167. /* found bottom label; does skb have room for a header? */
  168. if (pskb_may_pull(skb, mpls_hdr_len + sizeof(struct iphdr))) {
  169. const struct iphdr *v4hdr;
  170. v4hdr = (const struct iphdr *)(hdr + 1);
  171. if (v4hdr->version == 4) {
  172. hash = jhash_3words(ntohl(v4hdr->saddr),
  173. ntohl(v4hdr->daddr),
  174. v4hdr->protocol, hash);
  175. } else if (v4hdr->version == 6 &&
  176. pskb_may_pull(skb, mpls_hdr_len +
  177. sizeof(struct ipv6hdr))) {
  178. const struct ipv6hdr *v6hdr;
  179. v6hdr = (const struct ipv6hdr *)(hdr + 1);
  180. hash = __ipv6_addr_jhash(&v6hdr->saddr, hash);
  181. hash = __ipv6_addr_jhash(&v6hdr->daddr, hash);
  182. hash = jhash_1word(v6hdr->nexthdr, hash);
  183. }
  184. }
  185. break;
  186. }
  187. return hash;
  188. }
  189. static struct mpls_nh *mpls_get_nexthop(struct mpls_route *rt, u8 index)
  190. {
  191. return (struct mpls_nh *)((u8 *)rt->rt_nh + index * rt->rt_nh_size);
  192. }
  193. /* number of alive nexthops (rt->rt_nhn_alive) and the flags for
  194. * a next hop (nh->nh_flags) are modified by netdev event handlers.
  195. * Since those fields can change at any moment, use READ_ONCE to
  196. * access both.
  197. */
  198. static struct mpls_nh *mpls_select_multipath(struct mpls_route *rt,
  199. struct sk_buff *skb)
  200. {
  201. u32 hash = 0;
  202. int nh_index = 0;
  203. int n = 0;
  204. u8 alive;
  205. /* No need to look further into packet if there's only
  206. * one path
  207. */
  208. if (rt->rt_nhn == 1)
  209. return rt->rt_nh;
  210. alive = READ_ONCE(rt->rt_nhn_alive);
  211. if (alive == 0)
  212. return NULL;
  213. hash = mpls_multipath_hash(rt, skb);
  214. nh_index = hash % alive;
  215. if (alive == rt->rt_nhn)
  216. goto out;
  217. for_nexthops(rt) {
  218. unsigned int nh_flags = READ_ONCE(nh->nh_flags);
  219. if (nh_flags & (RTNH_F_DEAD | RTNH_F_LINKDOWN))
  220. continue;
  221. if (n == nh_index)
  222. return nh;
  223. n++;
  224. } endfor_nexthops(rt);
  225. out:
  226. return mpls_get_nexthop(rt, nh_index);
  227. }
  228. static bool mpls_egress(struct net *net, struct mpls_route *rt,
  229. struct sk_buff *skb, struct mpls_entry_decoded dec)
  230. {
  231. enum mpls_payload_type payload_type;
  232. bool success = false;
  233. /* The IPv4 code below accesses through the IPv4 header
  234. * checksum, which is 12 bytes into the packet.
  235. * The IPv6 code below accesses through the IPv6 hop limit
  236. * which is 8 bytes into the packet.
  237. *
  238. * For all supported cases there should always be at least 12
  239. * bytes of packet data present. The IPv4 header is 20 bytes
  240. * without options and the IPv6 header is always 40 bytes
  241. * long.
  242. */
  243. if (!pskb_may_pull(skb, 12))
  244. return false;
  245. payload_type = rt->rt_payload_type;
  246. if (payload_type == MPT_UNSPEC)
  247. payload_type = ip_hdr(skb)->version;
  248. switch (payload_type) {
  249. case MPT_IPV4: {
  250. struct iphdr *hdr4 = ip_hdr(skb);
  251. u8 new_ttl;
  252. skb->protocol = htons(ETH_P_IP);
  253. /* If propagating TTL, take the decremented TTL from
  254. * the incoming MPLS header, otherwise decrement the
  255. * TTL, but only if not 0 to avoid underflow.
  256. */
  257. if (rt->rt_ttl_propagate == MPLS_TTL_PROP_ENABLED ||
  258. (rt->rt_ttl_propagate == MPLS_TTL_PROP_DEFAULT &&
  259. net->mpls.ip_ttl_propagate))
  260. new_ttl = dec.ttl;
  261. else
  262. new_ttl = hdr4->ttl ? hdr4->ttl - 1 : 0;
  263. csum_replace2(&hdr4->check,
  264. htons(hdr4->ttl << 8),
  265. htons(new_ttl << 8));
  266. hdr4->ttl = new_ttl;
  267. success = true;
  268. break;
  269. }
  270. case MPT_IPV6: {
  271. struct ipv6hdr *hdr6 = ipv6_hdr(skb);
  272. skb->protocol = htons(ETH_P_IPV6);
  273. /* If propagating TTL, take the decremented TTL from
  274. * the incoming MPLS header, otherwise decrement the
  275. * hop limit, but only if not 0 to avoid underflow.
  276. */
  277. if (rt->rt_ttl_propagate == MPLS_TTL_PROP_ENABLED ||
  278. (rt->rt_ttl_propagate == MPLS_TTL_PROP_DEFAULT &&
  279. net->mpls.ip_ttl_propagate))
  280. hdr6->hop_limit = dec.ttl;
  281. else if (hdr6->hop_limit)
  282. hdr6->hop_limit = hdr6->hop_limit - 1;
  283. success = true;
  284. break;
  285. }
  286. case MPT_UNSPEC:
  287. /* Should have decided which protocol it is by now */
  288. break;
  289. }
  290. return success;
  291. }
  292. static int mpls_forward(struct sk_buff *skb, struct net_device *dev,
  293. struct packet_type *pt, struct net_device *orig_dev)
  294. {
  295. struct net *net = dev_net(dev);
  296. struct mpls_shim_hdr *hdr;
  297. struct mpls_route *rt;
  298. struct mpls_nh *nh;
  299. struct mpls_entry_decoded dec;
  300. struct net_device *out_dev;
  301. struct mpls_dev *out_mdev;
  302. struct mpls_dev *mdev;
  303. unsigned int hh_len;
  304. unsigned int new_header_size;
  305. unsigned int mtu;
  306. int err;
  307. /* Careful this entire function runs inside of an rcu critical section */
  308. mdev = mpls_dev_get(dev);
  309. if (!mdev)
  310. goto drop;
  311. MPLS_INC_STATS_LEN(mdev, skb->len, rx_packets,
  312. rx_bytes);
  313. if (!mdev->input_enabled) {
  314. MPLS_INC_STATS(mdev, rx_dropped);
  315. goto drop;
  316. }
  317. if (skb->pkt_type != PACKET_HOST)
  318. goto err;
  319. if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL)
  320. goto err;
  321. if (!pskb_may_pull(skb, sizeof(*hdr)))
  322. goto err;
  323. /* Read and decode the label */
  324. hdr = mpls_hdr(skb);
  325. dec = mpls_entry_decode(hdr);
  326. rt = mpls_route_input_rcu(net, dec.label);
  327. if (!rt) {
  328. MPLS_INC_STATS(mdev, rx_noroute);
  329. goto drop;
  330. }
  331. nh = mpls_select_multipath(rt, skb);
  332. if (!nh)
  333. goto err;
  334. /* Pop the label */
  335. skb_pull(skb, sizeof(*hdr));
  336. skb_reset_network_header(skb);
  337. skb_orphan(skb);
  338. if (skb_warn_if_lro(skb))
  339. goto err;
  340. skb_forward_csum(skb);
  341. /* Verify ttl is valid */
  342. if (dec.ttl <= 1)
  343. goto err;
  344. dec.ttl -= 1;
  345. /* Find the output device */
  346. out_dev = rcu_dereference(nh->nh_dev);
  347. if (!mpls_output_possible(out_dev))
  348. goto tx_err;
  349. /* Verify the destination can hold the packet */
  350. new_header_size = mpls_nh_header_size(nh);
  351. mtu = mpls_dev_mtu(out_dev);
  352. if (mpls_pkt_too_big(skb, mtu - new_header_size))
  353. goto tx_err;
  354. hh_len = LL_RESERVED_SPACE(out_dev);
  355. if (!out_dev->header_ops)
  356. hh_len = 0;
  357. /* Ensure there is enough space for the headers in the skb */
  358. if (skb_cow(skb, hh_len + new_header_size))
  359. goto tx_err;
  360. skb->dev = out_dev;
  361. skb->protocol = htons(ETH_P_MPLS_UC);
  362. if (unlikely(!new_header_size && dec.bos)) {
  363. /* Penultimate hop popping */
  364. if (!mpls_egress(dev_net(out_dev), rt, skb, dec))
  365. goto err;
  366. } else {
  367. bool bos;
  368. int i;
  369. skb_push(skb, new_header_size);
  370. skb_reset_network_header(skb);
  371. /* Push the new labels */
  372. hdr = mpls_hdr(skb);
  373. bos = dec.bos;
  374. for (i = nh->nh_labels - 1; i >= 0; i--) {
  375. hdr[i] = mpls_entry_encode(nh->nh_label[i],
  376. dec.ttl, 0, bos);
  377. bos = false;
  378. }
  379. }
  380. mpls_stats_inc_outucastpkts(out_dev, skb);
  381. /* If via wasn't specified then send out using device address */
  382. if (nh->nh_via_table == MPLS_NEIGH_TABLE_UNSPEC)
  383. err = neigh_xmit(NEIGH_LINK_TABLE, out_dev,
  384. out_dev->dev_addr, skb);
  385. else
  386. err = neigh_xmit(nh->nh_via_table, out_dev,
  387. mpls_nh_via(rt, nh), skb);
  388. if (err)
  389. net_dbg_ratelimited("%s: packet transmission failed: %d\n",
  390. __func__, err);
  391. return 0;
  392. tx_err:
  393. out_mdev = out_dev ? mpls_dev_get(out_dev) : NULL;
  394. if (out_mdev)
  395. MPLS_INC_STATS(out_mdev, tx_errors);
  396. goto drop;
  397. err:
  398. MPLS_INC_STATS(mdev, rx_errors);
  399. drop:
  400. kfree_skb(skb);
  401. return NET_RX_DROP;
  402. }
  403. static struct packet_type mpls_packet_type __read_mostly = {
  404. .type = cpu_to_be16(ETH_P_MPLS_UC),
  405. .func = mpls_forward,
  406. };
  407. static const struct nla_policy rtm_mpls_policy[RTA_MAX+1] = {
  408. [RTA_DST] = { .type = NLA_U32 },
  409. [RTA_OIF] = { .type = NLA_U32 },
  410. [RTA_TTL_PROPAGATE] = { .type = NLA_U8 },
  411. };
  412. struct mpls_route_config {
  413. u32 rc_protocol;
  414. u32 rc_ifindex;
  415. u8 rc_via_table;
  416. u8 rc_via_alen;
  417. u8 rc_via[MAX_VIA_ALEN];
  418. u32 rc_label;
  419. u8 rc_ttl_propagate;
  420. u8 rc_output_labels;
  421. u32 rc_output_label[MAX_NEW_LABELS];
  422. u32 rc_nlflags;
  423. enum mpls_payload_type rc_payload_type;
  424. struct nl_info rc_nlinfo;
  425. struct rtnexthop *rc_mp;
  426. int rc_mp_len;
  427. };
  428. /* all nexthops within a route have the same size based on max
  429. * number of labels and max via length for a hop
  430. */
  431. static struct mpls_route *mpls_rt_alloc(u8 num_nh, u8 max_alen, u8 max_labels)
  432. {
  433. u8 nh_size = MPLS_NH_SIZE(max_labels, max_alen);
  434. struct mpls_route *rt;
  435. size_t size;
  436. size = sizeof(*rt) + num_nh * nh_size;
  437. if (size > MAX_MPLS_ROUTE_MEM)
  438. return ERR_PTR(-EINVAL);
  439. rt = kzalloc(size, GFP_KERNEL);
  440. if (!rt)
  441. return ERR_PTR(-ENOMEM);
  442. rt->rt_nhn = num_nh;
  443. rt->rt_nhn_alive = num_nh;
  444. rt->rt_nh_size = nh_size;
  445. rt->rt_via_offset = MPLS_NH_VIA_OFF(max_labels);
  446. return rt;
  447. }
  448. static void mpls_rt_free(struct mpls_route *rt)
  449. {
  450. if (rt)
  451. kfree_rcu(rt, rt_rcu);
  452. }
  453. static void mpls_notify_route(struct net *net, unsigned index,
  454. struct mpls_route *old, struct mpls_route *new,
  455. const struct nl_info *info)
  456. {
  457. struct nlmsghdr *nlh = info ? info->nlh : NULL;
  458. unsigned portid = info ? info->portid : 0;
  459. int event = new ? RTM_NEWROUTE : RTM_DELROUTE;
  460. struct mpls_route *rt = new ? new : old;
  461. unsigned nlm_flags = (old && new) ? NLM_F_REPLACE : 0;
  462. /* Ignore reserved labels for now */
  463. if (rt && (index >= MPLS_LABEL_FIRST_UNRESERVED))
  464. rtmsg_lfib(event, index, rt, nlh, net, portid, nlm_flags);
  465. }
  466. static void mpls_route_update(struct net *net, unsigned index,
  467. struct mpls_route *new,
  468. const struct nl_info *info)
  469. {
  470. struct mpls_route __rcu **platform_label;
  471. struct mpls_route *rt;
  472. ASSERT_RTNL();
  473. platform_label = rtnl_dereference(net->mpls.platform_label);
  474. rt = rtnl_dereference(platform_label[index]);
  475. rcu_assign_pointer(platform_label[index], new);
  476. mpls_notify_route(net, index, rt, new, info);
  477. /* If we removed a route free it now */
  478. mpls_rt_free(rt);
  479. }
  480. static unsigned find_free_label(struct net *net)
  481. {
  482. struct mpls_route __rcu **platform_label;
  483. size_t platform_labels;
  484. unsigned index;
  485. platform_label = rtnl_dereference(net->mpls.platform_label);
  486. platform_labels = net->mpls.platform_labels;
  487. for (index = MPLS_LABEL_FIRST_UNRESERVED; index < platform_labels;
  488. index++) {
  489. if (!rtnl_dereference(platform_label[index]))
  490. return index;
  491. }
  492. return LABEL_NOT_SPECIFIED;
  493. }
  494. #if IS_ENABLED(CONFIG_INET)
  495. static struct net_device *inet_fib_lookup_dev(struct net *net,
  496. const void *addr)
  497. {
  498. struct net_device *dev;
  499. struct rtable *rt;
  500. struct in_addr daddr;
  501. memcpy(&daddr, addr, sizeof(struct in_addr));
  502. rt = ip_route_output(net, daddr.s_addr, 0, 0, 0);
  503. if (IS_ERR(rt))
  504. return ERR_CAST(rt);
  505. dev = rt->dst.dev;
  506. dev_hold(dev);
  507. ip_rt_put(rt);
  508. return dev;
  509. }
  510. #else
  511. static struct net_device *inet_fib_lookup_dev(struct net *net,
  512. const void *addr)
  513. {
  514. return ERR_PTR(-EAFNOSUPPORT);
  515. }
  516. #endif
  517. #if IS_ENABLED(CONFIG_IPV6)
  518. static struct net_device *inet6_fib_lookup_dev(struct net *net,
  519. const void *addr)
  520. {
  521. struct net_device *dev;
  522. struct dst_entry *dst;
  523. struct flowi6 fl6;
  524. int err;
  525. if (!ipv6_stub)
  526. return ERR_PTR(-EAFNOSUPPORT);
  527. memset(&fl6, 0, sizeof(fl6));
  528. memcpy(&fl6.daddr, addr, sizeof(struct in6_addr));
  529. err = ipv6_stub->ipv6_dst_lookup(net, NULL, &dst, &fl6);
  530. if (err)
  531. return ERR_PTR(err);
  532. dev = dst->dev;
  533. dev_hold(dev);
  534. dst_release(dst);
  535. return dev;
  536. }
  537. #else
  538. static struct net_device *inet6_fib_lookup_dev(struct net *net,
  539. const void *addr)
  540. {
  541. return ERR_PTR(-EAFNOSUPPORT);
  542. }
  543. #endif
  544. static struct net_device *find_outdev(struct net *net,
  545. struct mpls_route *rt,
  546. struct mpls_nh *nh, int oif)
  547. {
  548. struct net_device *dev = NULL;
  549. if (!oif) {
  550. switch (nh->nh_via_table) {
  551. case NEIGH_ARP_TABLE:
  552. dev = inet_fib_lookup_dev(net, mpls_nh_via(rt, nh));
  553. break;
  554. case NEIGH_ND_TABLE:
  555. dev = inet6_fib_lookup_dev(net, mpls_nh_via(rt, nh));
  556. break;
  557. case NEIGH_LINK_TABLE:
  558. break;
  559. }
  560. } else {
  561. dev = dev_get_by_index(net, oif);
  562. }
  563. if (!dev)
  564. return ERR_PTR(-ENODEV);
  565. if (IS_ERR(dev))
  566. return dev;
  567. /* The caller is holding rtnl anyways, so release the dev reference */
  568. dev_put(dev);
  569. return dev;
  570. }
  571. static int mpls_nh_assign_dev(struct net *net, struct mpls_route *rt,
  572. struct mpls_nh *nh, int oif)
  573. {
  574. struct net_device *dev = NULL;
  575. int err = -ENODEV;
  576. dev = find_outdev(net, rt, nh, oif);
  577. if (IS_ERR(dev)) {
  578. err = PTR_ERR(dev);
  579. dev = NULL;
  580. goto errout;
  581. }
  582. /* Ensure this is a supported device */
  583. err = -EINVAL;
  584. if (!mpls_dev_get(dev))
  585. goto errout;
  586. if ((nh->nh_via_table == NEIGH_LINK_TABLE) &&
  587. (dev->addr_len != nh->nh_via_alen))
  588. goto errout;
  589. RCU_INIT_POINTER(nh->nh_dev, dev);
  590. if (!(dev->flags & IFF_UP)) {
  591. nh->nh_flags |= RTNH_F_DEAD;
  592. } else {
  593. unsigned int flags;
  594. flags = dev_get_flags(dev);
  595. if (!(flags & (IFF_RUNNING | IFF_LOWER_UP)))
  596. nh->nh_flags |= RTNH_F_LINKDOWN;
  597. }
  598. return 0;
  599. errout:
  600. return err;
  601. }
  602. static int nla_get_via(const struct nlattr *nla, u8 *via_alen, u8 *via_table,
  603. u8 via_addr[], struct netlink_ext_ack *extack)
  604. {
  605. struct rtvia *via = nla_data(nla);
  606. int err = -EINVAL;
  607. int alen;
  608. if (nla_len(nla) < offsetof(struct rtvia, rtvia_addr)) {
  609. NL_SET_ERR_MSG_ATTR(extack, nla,
  610. "Invalid attribute length for RTA_VIA");
  611. goto errout;
  612. }
  613. alen = nla_len(nla) -
  614. offsetof(struct rtvia, rtvia_addr);
  615. if (alen > MAX_VIA_ALEN) {
  616. NL_SET_ERR_MSG_ATTR(extack, nla,
  617. "Invalid address length for RTA_VIA");
  618. goto errout;
  619. }
  620. /* Validate the address family */
  621. switch (via->rtvia_family) {
  622. case AF_PACKET:
  623. *via_table = NEIGH_LINK_TABLE;
  624. break;
  625. case AF_INET:
  626. *via_table = NEIGH_ARP_TABLE;
  627. if (alen != 4)
  628. goto errout;
  629. break;
  630. case AF_INET6:
  631. *via_table = NEIGH_ND_TABLE;
  632. if (alen != 16)
  633. goto errout;
  634. break;
  635. default:
  636. /* Unsupported address family */
  637. goto errout;
  638. }
  639. memcpy(via_addr, via->rtvia_addr, alen);
  640. *via_alen = alen;
  641. err = 0;
  642. errout:
  643. return err;
  644. }
  645. static int mpls_nh_build_from_cfg(struct mpls_route_config *cfg,
  646. struct mpls_route *rt)
  647. {
  648. struct net *net = cfg->rc_nlinfo.nl_net;
  649. struct mpls_nh *nh = rt->rt_nh;
  650. int err;
  651. int i;
  652. if (!nh)
  653. return -ENOMEM;
  654. nh->nh_labels = cfg->rc_output_labels;
  655. for (i = 0; i < nh->nh_labels; i++)
  656. nh->nh_label[i] = cfg->rc_output_label[i];
  657. nh->nh_via_table = cfg->rc_via_table;
  658. memcpy(__mpls_nh_via(rt, nh), cfg->rc_via, cfg->rc_via_alen);
  659. nh->nh_via_alen = cfg->rc_via_alen;
  660. err = mpls_nh_assign_dev(net, rt, nh, cfg->rc_ifindex);
  661. if (err)
  662. goto errout;
  663. if (nh->nh_flags & (RTNH_F_DEAD | RTNH_F_LINKDOWN))
  664. rt->rt_nhn_alive--;
  665. return 0;
  666. errout:
  667. return err;
  668. }
  669. static int mpls_nh_build(struct net *net, struct mpls_route *rt,
  670. struct mpls_nh *nh, int oif, struct nlattr *via,
  671. struct nlattr *newdst, u8 max_labels,
  672. struct netlink_ext_ack *extack)
  673. {
  674. int err = -ENOMEM;
  675. if (!nh)
  676. goto errout;
  677. if (newdst) {
  678. err = nla_get_labels(newdst, max_labels, &nh->nh_labels,
  679. nh->nh_label, extack);
  680. if (err)
  681. goto errout;
  682. }
  683. if (via) {
  684. err = nla_get_via(via, &nh->nh_via_alen, &nh->nh_via_table,
  685. __mpls_nh_via(rt, nh), extack);
  686. if (err)
  687. goto errout;
  688. } else {
  689. nh->nh_via_table = MPLS_NEIGH_TABLE_UNSPEC;
  690. }
  691. err = mpls_nh_assign_dev(net, rt, nh, oif);
  692. if (err)
  693. goto errout;
  694. return 0;
  695. errout:
  696. return err;
  697. }
  698. static u8 mpls_count_nexthops(struct rtnexthop *rtnh, int len,
  699. u8 cfg_via_alen, u8 *max_via_alen,
  700. u8 *max_labels)
  701. {
  702. int remaining = len;
  703. u8 nhs = 0;
  704. *max_via_alen = 0;
  705. *max_labels = 0;
  706. while (rtnh_ok(rtnh, remaining)) {
  707. struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
  708. int attrlen;
  709. u8 n_labels = 0;
  710. attrlen = rtnh_attrlen(rtnh);
  711. nla = nla_find(attrs, attrlen, RTA_VIA);
  712. if (nla && nla_len(nla) >=
  713. offsetof(struct rtvia, rtvia_addr)) {
  714. int via_alen = nla_len(nla) -
  715. offsetof(struct rtvia, rtvia_addr);
  716. if (via_alen <= MAX_VIA_ALEN)
  717. *max_via_alen = max_t(u16, *max_via_alen,
  718. via_alen);
  719. }
  720. nla = nla_find(attrs, attrlen, RTA_NEWDST);
  721. if (nla &&
  722. nla_get_labels(nla, MAX_NEW_LABELS, &n_labels,
  723. NULL, NULL) != 0)
  724. return 0;
  725. *max_labels = max_t(u8, *max_labels, n_labels);
  726. /* number of nexthops is tracked by a u8.
  727. * Check for overflow.
  728. */
  729. if (nhs == 255)
  730. return 0;
  731. nhs++;
  732. rtnh = rtnh_next(rtnh, &remaining);
  733. }
  734. /* leftover implies invalid nexthop configuration, discard it */
  735. return remaining > 0 ? 0 : nhs;
  736. }
  737. static int mpls_nh_build_multi(struct mpls_route_config *cfg,
  738. struct mpls_route *rt, u8 max_labels,
  739. struct netlink_ext_ack *extack)
  740. {
  741. struct rtnexthop *rtnh = cfg->rc_mp;
  742. struct nlattr *nla_via, *nla_newdst;
  743. int remaining = cfg->rc_mp_len;
  744. int err = 0;
  745. u8 nhs = 0;
  746. change_nexthops(rt) {
  747. int attrlen;
  748. nla_via = NULL;
  749. nla_newdst = NULL;
  750. err = -EINVAL;
  751. if (!rtnh_ok(rtnh, remaining))
  752. goto errout;
  753. /* neither weighted multipath nor any flags
  754. * are supported
  755. */
  756. if (rtnh->rtnh_hops || rtnh->rtnh_flags)
  757. goto errout;
  758. attrlen = rtnh_attrlen(rtnh);
  759. if (attrlen > 0) {
  760. struct nlattr *attrs = rtnh_attrs(rtnh);
  761. nla_via = nla_find(attrs, attrlen, RTA_VIA);
  762. nla_newdst = nla_find(attrs, attrlen, RTA_NEWDST);
  763. }
  764. err = mpls_nh_build(cfg->rc_nlinfo.nl_net, rt, nh,
  765. rtnh->rtnh_ifindex, nla_via, nla_newdst,
  766. max_labels, extack);
  767. if (err)
  768. goto errout;
  769. if (nh->nh_flags & (RTNH_F_DEAD | RTNH_F_LINKDOWN))
  770. rt->rt_nhn_alive--;
  771. rtnh = rtnh_next(rtnh, &remaining);
  772. nhs++;
  773. } endfor_nexthops(rt);
  774. rt->rt_nhn = nhs;
  775. return 0;
  776. errout:
  777. return err;
  778. }
  779. static bool mpls_label_ok(struct net *net, unsigned int *index,
  780. struct netlink_ext_ack *extack)
  781. {
  782. bool is_ok = true;
  783. /* Reserved labels may not be set */
  784. if (*index < MPLS_LABEL_FIRST_UNRESERVED) {
  785. NL_SET_ERR_MSG(extack,
  786. "Invalid label - must be MPLS_LABEL_FIRST_UNRESERVED or higher");
  787. is_ok = false;
  788. }
  789. /* The full 20 bit range may not be supported. */
  790. if (is_ok && *index >= net->mpls.platform_labels) {
  791. NL_SET_ERR_MSG(extack,
  792. "Label >= configured maximum in platform_labels");
  793. is_ok = false;
  794. }
  795. *index = array_index_nospec(*index, net->mpls.platform_labels);
  796. return is_ok;
  797. }
  798. static int mpls_route_add(struct mpls_route_config *cfg,
  799. struct netlink_ext_ack *extack)
  800. {
  801. struct mpls_route __rcu **platform_label;
  802. struct net *net = cfg->rc_nlinfo.nl_net;
  803. struct mpls_route *rt, *old;
  804. int err = -EINVAL;
  805. u8 max_via_alen;
  806. unsigned index;
  807. u8 max_labels;
  808. u8 nhs;
  809. index = cfg->rc_label;
  810. /* If a label was not specified during insert pick one */
  811. if ((index == LABEL_NOT_SPECIFIED) &&
  812. (cfg->rc_nlflags & NLM_F_CREATE)) {
  813. index = find_free_label(net);
  814. }
  815. if (!mpls_label_ok(net, &index, extack))
  816. goto errout;
  817. /* Append makes no sense with mpls */
  818. err = -EOPNOTSUPP;
  819. if (cfg->rc_nlflags & NLM_F_APPEND) {
  820. NL_SET_ERR_MSG(extack, "MPLS does not support route append");
  821. goto errout;
  822. }
  823. err = -EEXIST;
  824. platform_label = rtnl_dereference(net->mpls.platform_label);
  825. old = rtnl_dereference(platform_label[index]);
  826. if ((cfg->rc_nlflags & NLM_F_EXCL) && old)
  827. goto errout;
  828. err = -EEXIST;
  829. if (!(cfg->rc_nlflags & NLM_F_REPLACE) && old)
  830. goto errout;
  831. err = -ENOENT;
  832. if (!(cfg->rc_nlflags & NLM_F_CREATE) && !old)
  833. goto errout;
  834. err = -EINVAL;
  835. if (cfg->rc_mp) {
  836. nhs = mpls_count_nexthops(cfg->rc_mp, cfg->rc_mp_len,
  837. cfg->rc_via_alen, &max_via_alen,
  838. &max_labels);
  839. } else {
  840. max_via_alen = cfg->rc_via_alen;
  841. max_labels = cfg->rc_output_labels;
  842. nhs = 1;
  843. }
  844. if (nhs == 0) {
  845. NL_SET_ERR_MSG(extack, "Route does not contain a nexthop");
  846. goto errout;
  847. }
  848. err = -ENOMEM;
  849. rt = mpls_rt_alloc(nhs, max_via_alen, max_labels);
  850. if (IS_ERR(rt)) {
  851. err = PTR_ERR(rt);
  852. goto errout;
  853. }
  854. rt->rt_protocol = cfg->rc_protocol;
  855. rt->rt_payload_type = cfg->rc_payload_type;
  856. rt->rt_ttl_propagate = cfg->rc_ttl_propagate;
  857. if (cfg->rc_mp)
  858. err = mpls_nh_build_multi(cfg, rt, max_labels, extack);
  859. else
  860. err = mpls_nh_build_from_cfg(cfg, rt);
  861. if (err)
  862. goto freert;
  863. mpls_route_update(net, index, rt, &cfg->rc_nlinfo);
  864. return 0;
  865. freert:
  866. mpls_rt_free(rt);
  867. errout:
  868. return err;
  869. }
  870. static int mpls_route_del(struct mpls_route_config *cfg,
  871. struct netlink_ext_ack *extack)
  872. {
  873. struct net *net = cfg->rc_nlinfo.nl_net;
  874. unsigned index;
  875. int err = -EINVAL;
  876. index = cfg->rc_label;
  877. if (!mpls_label_ok(net, &index, extack))
  878. goto errout;
  879. mpls_route_update(net, index, NULL, &cfg->rc_nlinfo);
  880. err = 0;
  881. errout:
  882. return err;
  883. }
  884. static void mpls_get_stats(struct mpls_dev *mdev,
  885. struct mpls_link_stats *stats)
  886. {
  887. struct mpls_pcpu_stats *p;
  888. int i;
  889. memset(stats, 0, sizeof(*stats));
  890. for_each_possible_cpu(i) {
  891. struct mpls_link_stats local;
  892. unsigned int start;
  893. p = per_cpu_ptr(mdev->stats, i);
  894. do {
  895. start = u64_stats_fetch_begin(&p->syncp);
  896. local = p->stats;
  897. } while (u64_stats_fetch_retry(&p->syncp, start));
  898. stats->rx_packets += local.rx_packets;
  899. stats->rx_bytes += local.rx_bytes;
  900. stats->tx_packets += local.tx_packets;
  901. stats->tx_bytes += local.tx_bytes;
  902. stats->rx_errors += local.rx_errors;
  903. stats->tx_errors += local.tx_errors;
  904. stats->rx_dropped += local.rx_dropped;
  905. stats->tx_dropped += local.tx_dropped;
  906. stats->rx_noroute += local.rx_noroute;
  907. }
  908. }
  909. static int mpls_fill_stats_af(struct sk_buff *skb,
  910. const struct net_device *dev)
  911. {
  912. struct mpls_link_stats *stats;
  913. struct mpls_dev *mdev;
  914. struct nlattr *nla;
  915. mdev = mpls_dev_get(dev);
  916. if (!mdev)
  917. return -ENODATA;
  918. nla = nla_reserve_64bit(skb, MPLS_STATS_LINK,
  919. sizeof(struct mpls_link_stats),
  920. MPLS_STATS_UNSPEC);
  921. if (!nla)
  922. return -EMSGSIZE;
  923. stats = nla_data(nla);
  924. mpls_get_stats(mdev, stats);
  925. return 0;
  926. }
  927. static size_t mpls_get_stats_af_size(const struct net_device *dev)
  928. {
  929. struct mpls_dev *mdev;
  930. mdev = mpls_dev_get(dev);
  931. if (!mdev)
  932. return 0;
  933. return nla_total_size_64bit(sizeof(struct mpls_link_stats));
  934. }
  935. static int mpls_netconf_fill_devconf(struct sk_buff *skb, struct mpls_dev *mdev,
  936. u32 portid, u32 seq, int event,
  937. unsigned int flags, int type)
  938. {
  939. struct nlmsghdr *nlh;
  940. struct netconfmsg *ncm;
  941. bool all = false;
  942. nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct netconfmsg),
  943. flags);
  944. if (!nlh)
  945. return -EMSGSIZE;
  946. if (type == NETCONFA_ALL)
  947. all = true;
  948. ncm = nlmsg_data(nlh);
  949. ncm->ncm_family = AF_MPLS;
  950. if (nla_put_s32(skb, NETCONFA_IFINDEX, mdev->dev->ifindex) < 0)
  951. goto nla_put_failure;
  952. if ((all || type == NETCONFA_INPUT) &&
  953. nla_put_s32(skb, NETCONFA_INPUT,
  954. mdev->input_enabled) < 0)
  955. goto nla_put_failure;
  956. nlmsg_end(skb, nlh);
  957. return 0;
  958. nla_put_failure:
  959. nlmsg_cancel(skb, nlh);
  960. return -EMSGSIZE;
  961. }
  962. static int mpls_netconf_msgsize_devconf(int type)
  963. {
  964. int size = NLMSG_ALIGN(sizeof(struct netconfmsg))
  965. + nla_total_size(4); /* NETCONFA_IFINDEX */
  966. bool all = false;
  967. if (type == NETCONFA_ALL)
  968. all = true;
  969. if (all || type == NETCONFA_INPUT)
  970. size += nla_total_size(4);
  971. return size;
  972. }
  973. static void mpls_netconf_notify_devconf(struct net *net, int event,
  974. int type, struct mpls_dev *mdev)
  975. {
  976. struct sk_buff *skb;
  977. int err = -ENOBUFS;
  978. skb = nlmsg_new(mpls_netconf_msgsize_devconf(type), GFP_KERNEL);
  979. if (!skb)
  980. goto errout;
  981. err = mpls_netconf_fill_devconf(skb, mdev, 0, 0, event, 0, type);
  982. if (err < 0) {
  983. /* -EMSGSIZE implies BUG in mpls_netconf_msgsize_devconf() */
  984. WARN_ON(err == -EMSGSIZE);
  985. kfree_skb(skb);
  986. goto errout;
  987. }
  988. rtnl_notify(skb, net, 0, RTNLGRP_MPLS_NETCONF, NULL, GFP_KERNEL);
  989. return;
  990. errout:
  991. if (err < 0)
  992. rtnl_set_sk_err(net, RTNLGRP_MPLS_NETCONF, err);
  993. }
  994. static const struct nla_policy devconf_mpls_policy[NETCONFA_MAX + 1] = {
  995. [NETCONFA_IFINDEX] = { .len = sizeof(int) },
  996. };
  997. static int mpls_netconf_get_devconf(struct sk_buff *in_skb,
  998. struct nlmsghdr *nlh,
  999. struct netlink_ext_ack *extack)
  1000. {
  1001. struct net *net = sock_net(in_skb->sk);
  1002. struct nlattr *tb[NETCONFA_MAX + 1];
  1003. struct netconfmsg *ncm;
  1004. struct net_device *dev;
  1005. struct mpls_dev *mdev;
  1006. struct sk_buff *skb;
  1007. int ifindex;
  1008. int err;
  1009. err = nlmsg_parse(nlh, sizeof(*ncm), tb, NETCONFA_MAX,
  1010. devconf_mpls_policy, extack);
  1011. if (err < 0)
  1012. goto errout;
  1013. err = -EINVAL;
  1014. if (!tb[NETCONFA_IFINDEX])
  1015. goto errout;
  1016. ifindex = nla_get_s32(tb[NETCONFA_IFINDEX]);
  1017. dev = __dev_get_by_index(net, ifindex);
  1018. if (!dev)
  1019. goto errout;
  1020. mdev = mpls_dev_get(dev);
  1021. if (!mdev)
  1022. goto errout;
  1023. err = -ENOBUFS;
  1024. skb = nlmsg_new(mpls_netconf_msgsize_devconf(NETCONFA_ALL), GFP_KERNEL);
  1025. if (!skb)
  1026. goto errout;
  1027. err = mpls_netconf_fill_devconf(skb, mdev,
  1028. NETLINK_CB(in_skb).portid,
  1029. nlh->nlmsg_seq, RTM_NEWNETCONF, 0,
  1030. NETCONFA_ALL);
  1031. if (err < 0) {
  1032. /* -EMSGSIZE implies BUG in mpls_netconf_msgsize_devconf() */
  1033. WARN_ON(err == -EMSGSIZE);
  1034. kfree_skb(skb);
  1035. goto errout;
  1036. }
  1037. err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
  1038. errout:
  1039. return err;
  1040. }
  1041. static int mpls_netconf_dump_devconf(struct sk_buff *skb,
  1042. struct netlink_callback *cb)
  1043. {
  1044. const struct nlmsghdr *nlh = cb->nlh;
  1045. struct net *net = sock_net(skb->sk);
  1046. struct hlist_head *head;
  1047. struct net_device *dev;
  1048. struct mpls_dev *mdev;
  1049. int idx, s_idx;
  1050. int h, s_h;
  1051. if (cb->strict_check) {
  1052. struct netlink_ext_ack *extack = cb->extack;
  1053. struct netconfmsg *ncm;
  1054. if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ncm))) {
  1055. NL_SET_ERR_MSG_MOD(extack, "Invalid header for netconf dump request");
  1056. return -EINVAL;
  1057. }
  1058. if (nlmsg_attrlen(nlh, sizeof(*ncm))) {
  1059. NL_SET_ERR_MSG_MOD(extack, "Invalid data after header in netconf dump request");
  1060. return -EINVAL;
  1061. }
  1062. }
  1063. s_h = cb->args[0];
  1064. s_idx = idx = cb->args[1];
  1065. for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
  1066. idx = 0;
  1067. head = &net->dev_index_head[h];
  1068. rcu_read_lock();
  1069. cb->seq = net->dev_base_seq;
  1070. hlist_for_each_entry_rcu(dev, head, index_hlist) {
  1071. if (idx < s_idx)
  1072. goto cont;
  1073. mdev = mpls_dev_get(dev);
  1074. if (!mdev)
  1075. goto cont;
  1076. if (mpls_netconf_fill_devconf(skb, mdev,
  1077. NETLINK_CB(cb->skb).portid,
  1078. nlh->nlmsg_seq,
  1079. RTM_NEWNETCONF,
  1080. NLM_F_MULTI,
  1081. NETCONFA_ALL) < 0) {
  1082. rcu_read_unlock();
  1083. goto done;
  1084. }
  1085. nl_dump_check_consistent(cb, nlmsg_hdr(skb));
  1086. cont:
  1087. idx++;
  1088. }
  1089. rcu_read_unlock();
  1090. }
  1091. done:
  1092. cb->args[0] = h;
  1093. cb->args[1] = idx;
  1094. return skb->len;
  1095. }
  1096. #define MPLS_PERDEV_SYSCTL_OFFSET(field) \
  1097. (&((struct mpls_dev *)0)->field)
  1098. static int mpls_conf_proc(struct ctl_table *ctl, int write,
  1099. void __user *buffer,
  1100. size_t *lenp, loff_t *ppos)
  1101. {
  1102. int oval = *(int *)ctl->data;
  1103. int ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
  1104. if (write) {
  1105. struct mpls_dev *mdev = ctl->extra1;
  1106. int i = (int *)ctl->data - (int *)mdev;
  1107. struct net *net = ctl->extra2;
  1108. int val = *(int *)ctl->data;
  1109. if (i == offsetof(struct mpls_dev, input_enabled) &&
  1110. val != oval) {
  1111. mpls_netconf_notify_devconf(net, RTM_NEWNETCONF,
  1112. NETCONFA_INPUT, mdev);
  1113. }
  1114. }
  1115. return ret;
  1116. }
  1117. static const struct ctl_table mpls_dev_table[] = {
  1118. {
  1119. .procname = "input",
  1120. .maxlen = sizeof(int),
  1121. .mode = 0644,
  1122. .proc_handler = mpls_conf_proc,
  1123. .data = MPLS_PERDEV_SYSCTL_OFFSET(input_enabled),
  1124. },
  1125. { }
  1126. };
  1127. static int mpls_dev_sysctl_register(struct net_device *dev,
  1128. struct mpls_dev *mdev)
  1129. {
  1130. char path[sizeof("net/mpls/conf/") + IFNAMSIZ];
  1131. struct net *net = dev_net(dev);
  1132. struct ctl_table *table;
  1133. int i;
  1134. table = kmemdup(&mpls_dev_table, sizeof(mpls_dev_table), GFP_KERNEL);
  1135. if (!table)
  1136. goto out;
  1137. /* Table data contains only offsets relative to the base of
  1138. * the mdev at this point, so make them absolute.
  1139. */
  1140. for (i = 0; i < ARRAY_SIZE(mpls_dev_table); i++) {
  1141. table[i].data = (char *)mdev + (uintptr_t)table[i].data;
  1142. table[i].extra1 = mdev;
  1143. table[i].extra2 = net;
  1144. }
  1145. snprintf(path, sizeof(path), "net/mpls/conf/%s", dev->name);
  1146. mdev->sysctl = register_net_sysctl(net, path, table);
  1147. if (!mdev->sysctl)
  1148. goto free;
  1149. mpls_netconf_notify_devconf(net, RTM_NEWNETCONF, NETCONFA_ALL, mdev);
  1150. return 0;
  1151. free:
  1152. kfree(table);
  1153. out:
  1154. return -ENOBUFS;
  1155. }
  1156. static void mpls_dev_sysctl_unregister(struct net_device *dev,
  1157. struct mpls_dev *mdev)
  1158. {
  1159. struct net *net = dev_net(dev);
  1160. struct ctl_table *table;
  1161. table = mdev->sysctl->ctl_table_arg;
  1162. unregister_net_sysctl_table(mdev->sysctl);
  1163. kfree(table);
  1164. mpls_netconf_notify_devconf(net, RTM_DELNETCONF, 0, mdev);
  1165. }
  1166. static struct mpls_dev *mpls_add_dev(struct net_device *dev)
  1167. {
  1168. struct mpls_dev *mdev;
  1169. int err = -ENOMEM;
  1170. int i;
  1171. ASSERT_RTNL();
  1172. mdev = kzalloc(sizeof(*mdev), GFP_KERNEL);
  1173. if (!mdev)
  1174. return ERR_PTR(err);
  1175. mdev->stats = alloc_percpu(struct mpls_pcpu_stats);
  1176. if (!mdev->stats)
  1177. goto free;
  1178. for_each_possible_cpu(i) {
  1179. struct mpls_pcpu_stats *mpls_stats;
  1180. mpls_stats = per_cpu_ptr(mdev->stats, i);
  1181. u64_stats_init(&mpls_stats->syncp);
  1182. }
  1183. mdev->dev = dev;
  1184. err = mpls_dev_sysctl_register(dev, mdev);
  1185. if (err)
  1186. goto free;
  1187. rcu_assign_pointer(dev->mpls_ptr, mdev);
  1188. return mdev;
  1189. free:
  1190. free_percpu(mdev->stats);
  1191. kfree(mdev);
  1192. return ERR_PTR(err);
  1193. }
  1194. static void mpls_dev_destroy_rcu(struct rcu_head *head)
  1195. {
  1196. struct mpls_dev *mdev = container_of(head, struct mpls_dev, rcu);
  1197. free_percpu(mdev->stats);
  1198. kfree(mdev);
  1199. }
  1200. static void mpls_ifdown(struct net_device *dev, int event)
  1201. {
  1202. struct mpls_route __rcu **platform_label;
  1203. struct net *net = dev_net(dev);
  1204. u8 alive, deleted;
  1205. unsigned index;
  1206. platform_label = rtnl_dereference(net->mpls.platform_label);
  1207. for (index = 0; index < net->mpls.platform_labels; index++) {
  1208. struct mpls_route *rt = rtnl_dereference(platform_label[index]);
  1209. if (!rt)
  1210. continue;
  1211. alive = 0;
  1212. deleted = 0;
  1213. change_nexthops(rt) {
  1214. unsigned int nh_flags = nh->nh_flags;
  1215. if (rtnl_dereference(nh->nh_dev) != dev)
  1216. goto next;
  1217. switch (event) {
  1218. case NETDEV_DOWN:
  1219. case NETDEV_UNREGISTER:
  1220. nh_flags |= RTNH_F_DEAD;
  1221. /* fall through */
  1222. case NETDEV_CHANGE:
  1223. nh_flags |= RTNH_F_LINKDOWN;
  1224. break;
  1225. }
  1226. if (event == NETDEV_UNREGISTER)
  1227. RCU_INIT_POINTER(nh->nh_dev, NULL);
  1228. if (nh->nh_flags != nh_flags)
  1229. WRITE_ONCE(nh->nh_flags, nh_flags);
  1230. next:
  1231. if (!(nh_flags & (RTNH_F_DEAD | RTNH_F_LINKDOWN)))
  1232. alive++;
  1233. if (!rtnl_dereference(nh->nh_dev))
  1234. deleted++;
  1235. } endfor_nexthops(rt);
  1236. WRITE_ONCE(rt->rt_nhn_alive, alive);
  1237. /* if there are no more nexthops, delete the route */
  1238. if (event == NETDEV_UNREGISTER && deleted == rt->rt_nhn)
  1239. mpls_route_update(net, index, NULL, NULL);
  1240. }
  1241. }
  1242. static void mpls_ifup(struct net_device *dev, unsigned int flags)
  1243. {
  1244. struct mpls_route __rcu **platform_label;
  1245. struct net *net = dev_net(dev);
  1246. unsigned index;
  1247. u8 alive;
  1248. platform_label = rtnl_dereference(net->mpls.platform_label);
  1249. for (index = 0; index < net->mpls.platform_labels; index++) {
  1250. struct mpls_route *rt = rtnl_dereference(platform_label[index]);
  1251. if (!rt)
  1252. continue;
  1253. alive = 0;
  1254. change_nexthops(rt) {
  1255. unsigned int nh_flags = nh->nh_flags;
  1256. struct net_device *nh_dev =
  1257. rtnl_dereference(nh->nh_dev);
  1258. if (!(nh_flags & flags)) {
  1259. alive++;
  1260. continue;
  1261. }
  1262. if (nh_dev != dev)
  1263. continue;
  1264. alive++;
  1265. nh_flags &= ~flags;
  1266. WRITE_ONCE(nh->nh_flags, nh_flags);
  1267. } endfor_nexthops(rt);
  1268. WRITE_ONCE(rt->rt_nhn_alive, alive);
  1269. }
  1270. }
  1271. static int mpls_dev_notify(struct notifier_block *this, unsigned long event,
  1272. void *ptr)
  1273. {
  1274. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  1275. struct mpls_dev *mdev;
  1276. unsigned int flags;
  1277. if (event == NETDEV_REGISTER) {
  1278. /* For now just support Ethernet, IPGRE, IP6GRE, SIT and
  1279. * IPIP devices
  1280. */
  1281. if (dev->type == ARPHRD_ETHER ||
  1282. dev->type == ARPHRD_LOOPBACK ||
  1283. dev->type == ARPHRD_IPGRE ||
  1284. dev->type == ARPHRD_IP6GRE ||
  1285. dev->type == ARPHRD_SIT ||
  1286. dev->type == ARPHRD_TUNNEL) {
  1287. mdev = mpls_add_dev(dev);
  1288. if (IS_ERR(mdev))
  1289. return notifier_from_errno(PTR_ERR(mdev));
  1290. }
  1291. return NOTIFY_OK;
  1292. }
  1293. mdev = mpls_dev_get(dev);
  1294. if (!mdev)
  1295. return NOTIFY_OK;
  1296. switch (event) {
  1297. case NETDEV_DOWN:
  1298. mpls_ifdown(dev, event);
  1299. break;
  1300. case NETDEV_UP:
  1301. flags = dev_get_flags(dev);
  1302. if (flags & (IFF_RUNNING | IFF_LOWER_UP))
  1303. mpls_ifup(dev, RTNH_F_DEAD | RTNH_F_LINKDOWN);
  1304. else
  1305. mpls_ifup(dev, RTNH_F_DEAD);
  1306. break;
  1307. case NETDEV_CHANGE:
  1308. flags = dev_get_flags(dev);
  1309. if (flags & (IFF_RUNNING | IFF_LOWER_UP))
  1310. mpls_ifup(dev, RTNH_F_DEAD | RTNH_F_LINKDOWN);
  1311. else
  1312. mpls_ifdown(dev, event);
  1313. break;
  1314. case NETDEV_UNREGISTER:
  1315. mpls_ifdown(dev, event);
  1316. mdev = mpls_dev_get(dev);
  1317. if (mdev) {
  1318. mpls_dev_sysctl_unregister(dev, mdev);
  1319. RCU_INIT_POINTER(dev->mpls_ptr, NULL);
  1320. call_rcu(&mdev->rcu, mpls_dev_destroy_rcu);
  1321. }
  1322. break;
  1323. case NETDEV_CHANGENAME:
  1324. mdev = mpls_dev_get(dev);
  1325. if (mdev) {
  1326. int err;
  1327. mpls_dev_sysctl_unregister(dev, mdev);
  1328. err = mpls_dev_sysctl_register(dev, mdev);
  1329. if (err)
  1330. return notifier_from_errno(err);
  1331. }
  1332. break;
  1333. }
  1334. return NOTIFY_OK;
  1335. }
  1336. static struct notifier_block mpls_dev_notifier = {
  1337. .notifier_call = mpls_dev_notify,
  1338. };
  1339. static int nla_put_via(struct sk_buff *skb,
  1340. u8 table, const void *addr, int alen)
  1341. {
  1342. static const int table_to_family[NEIGH_NR_TABLES + 1] = {
  1343. AF_INET, AF_INET6, AF_DECnet, AF_PACKET,
  1344. };
  1345. struct nlattr *nla;
  1346. struct rtvia *via;
  1347. int family = AF_UNSPEC;
  1348. nla = nla_reserve(skb, RTA_VIA, alen + 2);
  1349. if (!nla)
  1350. return -EMSGSIZE;
  1351. if (table <= NEIGH_NR_TABLES)
  1352. family = table_to_family[table];
  1353. via = nla_data(nla);
  1354. via->rtvia_family = family;
  1355. memcpy(via->rtvia_addr, addr, alen);
  1356. return 0;
  1357. }
  1358. int nla_put_labels(struct sk_buff *skb, int attrtype,
  1359. u8 labels, const u32 label[])
  1360. {
  1361. struct nlattr *nla;
  1362. struct mpls_shim_hdr *nla_label;
  1363. bool bos;
  1364. int i;
  1365. nla = nla_reserve(skb, attrtype, labels*4);
  1366. if (!nla)
  1367. return -EMSGSIZE;
  1368. nla_label = nla_data(nla);
  1369. bos = true;
  1370. for (i = labels - 1; i >= 0; i--) {
  1371. nla_label[i] = mpls_entry_encode(label[i], 0, 0, bos);
  1372. bos = false;
  1373. }
  1374. return 0;
  1375. }
  1376. EXPORT_SYMBOL_GPL(nla_put_labels);
  1377. int nla_get_labels(const struct nlattr *nla, u8 max_labels, u8 *labels,
  1378. u32 label[], struct netlink_ext_ack *extack)
  1379. {
  1380. unsigned len = nla_len(nla);
  1381. struct mpls_shim_hdr *nla_label;
  1382. u8 nla_labels;
  1383. bool bos;
  1384. int i;
  1385. /* len needs to be an even multiple of 4 (the label size). Number
  1386. * of labels is a u8 so check for overflow.
  1387. */
  1388. if (len & 3 || len / 4 > 255) {
  1389. NL_SET_ERR_MSG_ATTR(extack, nla,
  1390. "Invalid length for labels attribute");
  1391. return -EINVAL;
  1392. }
  1393. /* Limit the number of new labels allowed */
  1394. nla_labels = len/4;
  1395. if (nla_labels > max_labels) {
  1396. NL_SET_ERR_MSG(extack, "Too many labels");
  1397. return -EINVAL;
  1398. }
  1399. /* when label == NULL, caller wants number of labels */
  1400. if (!label)
  1401. goto out;
  1402. nla_label = nla_data(nla);
  1403. bos = true;
  1404. for (i = nla_labels - 1; i >= 0; i--, bos = false) {
  1405. struct mpls_entry_decoded dec;
  1406. dec = mpls_entry_decode(nla_label + i);
  1407. /* Ensure the bottom of stack flag is properly set
  1408. * and ttl and tc are both clear.
  1409. */
  1410. if (dec.ttl) {
  1411. NL_SET_ERR_MSG_ATTR(extack, nla,
  1412. "TTL in label must be 0");
  1413. return -EINVAL;
  1414. }
  1415. if (dec.tc) {
  1416. NL_SET_ERR_MSG_ATTR(extack, nla,
  1417. "Traffic class in label must be 0");
  1418. return -EINVAL;
  1419. }
  1420. if (dec.bos != bos) {
  1421. NL_SET_BAD_ATTR(extack, nla);
  1422. if (bos) {
  1423. NL_SET_ERR_MSG(extack,
  1424. "BOS bit must be set in first label");
  1425. } else {
  1426. NL_SET_ERR_MSG(extack,
  1427. "BOS bit can only be set in first label");
  1428. }
  1429. return -EINVAL;
  1430. }
  1431. switch (dec.label) {
  1432. case MPLS_LABEL_IMPLNULL:
  1433. /* RFC3032: This is a label that an LSR may
  1434. * assign and distribute, but which never
  1435. * actually appears in the encapsulation.
  1436. */
  1437. NL_SET_ERR_MSG_ATTR(extack, nla,
  1438. "Implicit NULL Label (3) can not be used in encapsulation");
  1439. return -EINVAL;
  1440. }
  1441. label[i] = dec.label;
  1442. }
  1443. out:
  1444. *labels = nla_labels;
  1445. return 0;
  1446. }
  1447. EXPORT_SYMBOL_GPL(nla_get_labels);
  1448. static int rtm_to_route_config(struct sk_buff *skb,
  1449. struct nlmsghdr *nlh,
  1450. struct mpls_route_config *cfg,
  1451. struct netlink_ext_ack *extack)
  1452. {
  1453. struct rtmsg *rtm;
  1454. struct nlattr *tb[RTA_MAX+1];
  1455. int index;
  1456. int err;
  1457. err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_mpls_policy,
  1458. extack);
  1459. if (err < 0)
  1460. goto errout;
  1461. err = -EINVAL;
  1462. rtm = nlmsg_data(nlh);
  1463. if (rtm->rtm_family != AF_MPLS) {
  1464. NL_SET_ERR_MSG(extack, "Invalid address family in rtmsg");
  1465. goto errout;
  1466. }
  1467. if (rtm->rtm_dst_len != 20) {
  1468. NL_SET_ERR_MSG(extack, "rtm_dst_len must be 20 for MPLS");
  1469. goto errout;
  1470. }
  1471. if (rtm->rtm_src_len != 0) {
  1472. NL_SET_ERR_MSG(extack, "rtm_src_len must be 0 for MPLS");
  1473. goto errout;
  1474. }
  1475. if (rtm->rtm_tos != 0) {
  1476. NL_SET_ERR_MSG(extack, "rtm_tos must be 0 for MPLS");
  1477. goto errout;
  1478. }
  1479. if (rtm->rtm_table != RT_TABLE_MAIN) {
  1480. NL_SET_ERR_MSG(extack,
  1481. "MPLS only supports the main route table");
  1482. goto errout;
  1483. }
  1484. /* Any value is acceptable for rtm_protocol */
  1485. /* As mpls uses destination specific addresses
  1486. * (or source specific address in the case of multicast)
  1487. * all addresses have universal scope.
  1488. */
  1489. if (rtm->rtm_scope != RT_SCOPE_UNIVERSE) {
  1490. NL_SET_ERR_MSG(extack,
  1491. "Invalid route scope - MPLS only supports UNIVERSE");
  1492. goto errout;
  1493. }
  1494. if (rtm->rtm_type != RTN_UNICAST) {
  1495. NL_SET_ERR_MSG(extack,
  1496. "Invalid route type - MPLS only supports UNICAST");
  1497. goto errout;
  1498. }
  1499. if (rtm->rtm_flags != 0) {
  1500. NL_SET_ERR_MSG(extack, "rtm_flags must be 0 for MPLS");
  1501. goto errout;
  1502. }
  1503. cfg->rc_label = LABEL_NOT_SPECIFIED;
  1504. cfg->rc_protocol = rtm->rtm_protocol;
  1505. cfg->rc_via_table = MPLS_NEIGH_TABLE_UNSPEC;
  1506. cfg->rc_ttl_propagate = MPLS_TTL_PROP_DEFAULT;
  1507. cfg->rc_nlflags = nlh->nlmsg_flags;
  1508. cfg->rc_nlinfo.portid = NETLINK_CB(skb).portid;
  1509. cfg->rc_nlinfo.nlh = nlh;
  1510. cfg->rc_nlinfo.nl_net = sock_net(skb->sk);
  1511. for (index = 0; index <= RTA_MAX; index++) {
  1512. struct nlattr *nla = tb[index];
  1513. if (!nla)
  1514. continue;
  1515. switch (index) {
  1516. case RTA_OIF:
  1517. cfg->rc_ifindex = nla_get_u32(nla);
  1518. break;
  1519. case RTA_NEWDST:
  1520. if (nla_get_labels(nla, MAX_NEW_LABELS,
  1521. &cfg->rc_output_labels,
  1522. cfg->rc_output_label, extack))
  1523. goto errout;
  1524. break;
  1525. case RTA_DST:
  1526. {
  1527. u8 label_count;
  1528. if (nla_get_labels(nla, 1, &label_count,
  1529. &cfg->rc_label, extack))
  1530. goto errout;
  1531. if (!mpls_label_ok(cfg->rc_nlinfo.nl_net,
  1532. &cfg->rc_label, extack))
  1533. goto errout;
  1534. break;
  1535. }
  1536. case RTA_VIA:
  1537. {
  1538. if (nla_get_via(nla, &cfg->rc_via_alen,
  1539. &cfg->rc_via_table, cfg->rc_via,
  1540. extack))
  1541. goto errout;
  1542. break;
  1543. }
  1544. case RTA_MULTIPATH:
  1545. {
  1546. cfg->rc_mp = nla_data(nla);
  1547. cfg->rc_mp_len = nla_len(nla);
  1548. break;
  1549. }
  1550. case RTA_TTL_PROPAGATE:
  1551. {
  1552. u8 ttl_propagate = nla_get_u8(nla);
  1553. if (ttl_propagate > 1) {
  1554. NL_SET_ERR_MSG_ATTR(extack, nla,
  1555. "RTA_TTL_PROPAGATE can only be 0 or 1");
  1556. goto errout;
  1557. }
  1558. cfg->rc_ttl_propagate = ttl_propagate ?
  1559. MPLS_TTL_PROP_ENABLED :
  1560. MPLS_TTL_PROP_DISABLED;
  1561. break;
  1562. }
  1563. default:
  1564. NL_SET_ERR_MSG_ATTR(extack, nla, "Unknown attribute");
  1565. /* Unsupported attribute */
  1566. goto errout;
  1567. }
  1568. }
  1569. err = 0;
  1570. errout:
  1571. return err;
  1572. }
  1573. static int mpls_rtm_delroute(struct sk_buff *skb, struct nlmsghdr *nlh,
  1574. struct netlink_ext_ack *extack)
  1575. {
  1576. struct mpls_route_config *cfg;
  1577. int err;
  1578. cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
  1579. if (!cfg)
  1580. return -ENOMEM;
  1581. err = rtm_to_route_config(skb, nlh, cfg, extack);
  1582. if (err < 0)
  1583. goto out;
  1584. err = mpls_route_del(cfg, extack);
  1585. out:
  1586. kfree(cfg);
  1587. return err;
  1588. }
  1589. static int mpls_rtm_newroute(struct sk_buff *skb, struct nlmsghdr *nlh,
  1590. struct netlink_ext_ack *extack)
  1591. {
  1592. struct mpls_route_config *cfg;
  1593. int err;
  1594. cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
  1595. if (!cfg)
  1596. return -ENOMEM;
  1597. err = rtm_to_route_config(skb, nlh, cfg, extack);
  1598. if (err < 0)
  1599. goto out;
  1600. err = mpls_route_add(cfg, extack);
  1601. out:
  1602. kfree(cfg);
  1603. return err;
  1604. }
  1605. static int mpls_dump_route(struct sk_buff *skb, u32 portid, u32 seq, int event,
  1606. u32 label, struct mpls_route *rt, int flags)
  1607. {
  1608. struct net_device *dev;
  1609. struct nlmsghdr *nlh;
  1610. struct rtmsg *rtm;
  1611. nlh = nlmsg_put(skb, portid, seq, event, sizeof(*rtm), flags);
  1612. if (nlh == NULL)
  1613. return -EMSGSIZE;
  1614. rtm = nlmsg_data(nlh);
  1615. rtm->rtm_family = AF_MPLS;
  1616. rtm->rtm_dst_len = 20;
  1617. rtm->rtm_src_len = 0;
  1618. rtm->rtm_tos = 0;
  1619. rtm->rtm_table = RT_TABLE_MAIN;
  1620. rtm->rtm_protocol = rt->rt_protocol;
  1621. rtm->rtm_scope = RT_SCOPE_UNIVERSE;
  1622. rtm->rtm_type = RTN_UNICAST;
  1623. rtm->rtm_flags = 0;
  1624. if (nla_put_labels(skb, RTA_DST, 1, &label))
  1625. goto nla_put_failure;
  1626. if (rt->rt_ttl_propagate != MPLS_TTL_PROP_DEFAULT) {
  1627. bool ttl_propagate =
  1628. rt->rt_ttl_propagate == MPLS_TTL_PROP_ENABLED;
  1629. if (nla_put_u8(skb, RTA_TTL_PROPAGATE,
  1630. ttl_propagate))
  1631. goto nla_put_failure;
  1632. }
  1633. if (rt->rt_nhn == 1) {
  1634. const struct mpls_nh *nh = rt->rt_nh;
  1635. if (nh->nh_labels &&
  1636. nla_put_labels(skb, RTA_NEWDST, nh->nh_labels,
  1637. nh->nh_label))
  1638. goto nla_put_failure;
  1639. if (nh->nh_via_table != MPLS_NEIGH_TABLE_UNSPEC &&
  1640. nla_put_via(skb, nh->nh_via_table, mpls_nh_via(rt, nh),
  1641. nh->nh_via_alen))
  1642. goto nla_put_failure;
  1643. dev = rtnl_dereference(nh->nh_dev);
  1644. if (dev && nla_put_u32(skb, RTA_OIF, dev->ifindex))
  1645. goto nla_put_failure;
  1646. if (nh->nh_flags & RTNH_F_LINKDOWN)
  1647. rtm->rtm_flags |= RTNH_F_LINKDOWN;
  1648. if (nh->nh_flags & RTNH_F_DEAD)
  1649. rtm->rtm_flags |= RTNH_F_DEAD;
  1650. } else {
  1651. struct rtnexthop *rtnh;
  1652. struct nlattr *mp;
  1653. u8 linkdown = 0;
  1654. u8 dead = 0;
  1655. mp = nla_nest_start(skb, RTA_MULTIPATH);
  1656. if (!mp)
  1657. goto nla_put_failure;
  1658. for_nexthops(rt) {
  1659. dev = rtnl_dereference(nh->nh_dev);
  1660. if (!dev)
  1661. continue;
  1662. rtnh = nla_reserve_nohdr(skb, sizeof(*rtnh));
  1663. if (!rtnh)
  1664. goto nla_put_failure;
  1665. rtnh->rtnh_ifindex = dev->ifindex;
  1666. if (nh->nh_flags & RTNH_F_LINKDOWN) {
  1667. rtnh->rtnh_flags |= RTNH_F_LINKDOWN;
  1668. linkdown++;
  1669. }
  1670. if (nh->nh_flags & RTNH_F_DEAD) {
  1671. rtnh->rtnh_flags |= RTNH_F_DEAD;
  1672. dead++;
  1673. }
  1674. if (nh->nh_labels && nla_put_labels(skb, RTA_NEWDST,
  1675. nh->nh_labels,
  1676. nh->nh_label))
  1677. goto nla_put_failure;
  1678. if (nh->nh_via_table != MPLS_NEIGH_TABLE_UNSPEC &&
  1679. nla_put_via(skb, nh->nh_via_table,
  1680. mpls_nh_via(rt, nh),
  1681. nh->nh_via_alen))
  1682. goto nla_put_failure;
  1683. /* length of rtnetlink header + attributes */
  1684. rtnh->rtnh_len = nlmsg_get_pos(skb) - (void *)rtnh;
  1685. } endfor_nexthops(rt);
  1686. if (linkdown == rt->rt_nhn)
  1687. rtm->rtm_flags |= RTNH_F_LINKDOWN;
  1688. if (dead == rt->rt_nhn)
  1689. rtm->rtm_flags |= RTNH_F_DEAD;
  1690. nla_nest_end(skb, mp);
  1691. }
  1692. nlmsg_end(skb, nlh);
  1693. return 0;
  1694. nla_put_failure:
  1695. nlmsg_cancel(skb, nlh);
  1696. return -EMSGSIZE;
  1697. }
  1698. #if IS_ENABLED(CONFIG_INET)
  1699. static int mpls_valid_fib_dump_req(struct net *net, const struct nlmsghdr *nlh,
  1700. struct fib_dump_filter *filter,
  1701. struct netlink_callback *cb)
  1702. {
  1703. return ip_valid_fib_dump_req(net, nlh, filter, cb);
  1704. }
  1705. #else
  1706. static int mpls_valid_fib_dump_req(struct net *net, const struct nlmsghdr *nlh,
  1707. struct fib_dump_filter *filter,
  1708. struct netlink_callback *cb)
  1709. {
  1710. struct netlink_ext_ack *extack = cb->extack;
  1711. struct nlattr *tb[RTA_MAX + 1];
  1712. struct rtmsg *rtm;
  1713. int err, i;
  1714. if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*rtm))) {
  1715. NL_SET_ERR_MSG_MOD(extack, "Invalid header for FIB dump request");
  1716. return -EINVAL;
  1717. }
  1718. rtm = nlmsg_data(nlh);
  1719. if (rtm->rtm_dst_len || rtm->rtm_src_len || rtm->rtm_tos ||
  1720. rtm->rtm_table || rtm->rtm_scope || rtm->rtm_type ||
  1721. rtm->rtm_flags) {
  1722. NL_SET_ERR_MSG_MOD(extack, "Invalid values in header for FIB dump request");
  1723. return -EINVAL;
  1724. }
  1725. if (rtm->rtm_protocol) {
  1726. filter->protocol = rtm->rtm_protocol;
  1727. filter->filter_set = 1;
  1728. cb->answer_flags = NLM_F_DUMP_FILTERED;
  1729. }
  1730. err = nlmsg_parse_strict(nlh, sizeof(*rtm), tb, RTA_MAX,
  1731. rtm_mpls_policy, extack);
  1732. if (err < 0)
  1733. return err;
  1734. for (i = 0; i <= RTA_MAX; ++i) {
  1735. int ifindex;
  1736. if (i == RTA_OIF) {
  1737. ifindex = nla_get_u32(tb[i]);
  1738. filter->dev = __dev_get_by_index(net, ifindex);
  1739. if (!filter->dev)
  1740. return -ENODEV;
  1741. filter->filter_set = 1;
  1742. } else if (tb[i]) {
  1743. NL_SET_ERR_MSG_MOD(extack, "Unsupported attribute in dump request");
  1744. return -EINVAL;
  1745. }
  1746. }
  1747. return 0;
  1748. }
  1749. #endif
  1750. static bool mpls_rt_uses_dev(struct mpls_route *rt,
  1751. const struct net_device *dev)
  1752. {
  1753. struct net_device *nh_dev;
  1754. if (rt->rt_nhn == 1) {
  1755. struct mpls_nh *nh = rt->rt_nh;
  1756. nh_dev = rtnl_dereference(nh->nh_dev);
  1757. if (dev == nh_dev)
  1758. return true;
  1759. } else {
  1760. for_nexthops(rt) {
  1761. nh_dev = rtnl_dereference(nh->nh_dev);
  1762. if (nh_dev == dev)
  1763. return true;
  1764. } endfor_nexthops(rt);
  1765. }
  1766. return false;
  1767. }
  1768. static int mpls_dump_routes(struct sk_buff *skb, struct netlink_callback *cb)
  1769. {
  1770. const struct nlmsghdr *nlh = cb->nlh;
  1771. struct net *net = sock_net(skb->sk);
  1772. struct mpls_route __rcu **platform_label;
  1773. struct fib_dump_filter filter = {};
  1774. unsigned int flags = NLM_F_MULTI;
  1775. size_t platform_labels;
  1776. unsigned int index;
  1777. ASSERT_RTNL();
  1778. if (cb->strict_check) {
  1779. int err;
  1780. err = mpls_valid_fib_dump_req(net, nlh, &filter, cb);
  1781. if (err < 0)
  1782. return err;
  1783. /* for MPLS, there is only 1 table with fixed type and flags.
  1784. * If either are set in the filter then return nothing.
  1785. */
  1786. if ((filter.table_id && filter.table_id != RT_TABLE_MAIN) ||
  1787. (filter.rt_type && filter.rt_type != RTN_UNICAST) ||
  1788. filter.flags)
  1789. return skb->len;
  1790. }
  1791. index = cb->args[0];
  1792. if (index < MPLS_LABEL_FIRST_UNRESERVED)
  1793. index = MPLS_LABEL_FIRST_UNRESERVED;
  1794. platform_label = rtnl_dereference(net->mpls.platform_label);
  1795. platform_labels = net->mpls.platform_labels;
  1796. if (filter.filter_set)
  1797. flags |= NLM_F_DUMP_FILTERED;
  1798. for (; index < platform_labels; index++) {
  1799. struct mpls_route *rt;
  1800. rt = rtnl_dereference(platform_label[index]);
  1801. if (!rt)
  1802. continue;
  1803. if ((filter.dev && !mpls_rt_uses_dev(rt, filter.dev)) ||
  1804. (filter.protocol && rt->rt_protocol != filter.protocol))
  1805. continue;
  1806. if (mpls_dump_route(skb, NETLINK_CB(cb->skb).portid,
  1807. cb->nlh->nlmsg_seq, RTM_NEWROUTE,
  1808. index, rt, flags) < 0)
  1809. break;
  1810. }
  1811. cb->args[0] = index;
  1812. return skb->len;
  1813. }
  1814. static inline size_t lfib_nlmsg_size(struct mpls_route *rt)
  1815. {
  1816. size_t payload =
  1817. NLMSG_ALIGN(sizeof(struct rtmsg))
  1818. + nla_total_size(4) /* RTA_DST */
  1819. + nla_total_size(1); /* RTA_TTL_PROPAGATE */
  1820. if (rt->rt_nhn == 1) {
  1821. struct mpls_nh *nh = rt->rt_nh;
  1822. if (nh->nh_dev)
  1823. payload += nla_total_size(4); /* RTA_OIF */
  1824. if (nh->nh_via_table != MPLS_NEIGH_TABLE_UNSPEC) /* RTA_VIA */
  1825. payload += nla_total_size(2 + nh->nh_via_alen);
  1826. if (nh->nh_labels) /* RTA_NEWDST */
  1827. payload += nla_total_size(nh->nh_labels * 4);
  1828. } else {
  1829. /* each nexthop is packed in an attribute */
  1830. size_t nhsize = 0;
  1831. for_nexthops(rt) {
  1832. if (!rtnl_dereference(nh->nh_dev))
  1833. continue;
  1834. nhsize += nla_total_size(sizeof(struct rtnexthop));
  1835. /* RTA_VIA */
  1836. if (nh->nh_via_table != MPLS_NEIGH_TABLE_UNSPEC)
  1837. nhsize += nla_total_size(2 + nh->nh_via_alen);
  1838. if (nh->nh_labels)
  1839. nhsize += nla_total_size(nh->nh_labels * 4);
  1840. } endfor_nexthops(rt);
  1841. /* nested attribute */
  1842. payload += nla_total_size(nhsize);
  1843. }
  1844. return payload;
  1845. }
  1846. static void rtmsg_lfib(int event, u32 label, struct mpls_route *rt,
  1847. struct nlmsghdr *nlh, struct net *net, u32 portid,
  1848. unsigned int nlm_flags)
  1849. {
  1850. struct sk_buff *skb;
  1851. u32 seq = nlh ? nlh->nlmsg_seq : 0;
  1852. int err = -ENOBUFS;
  1853. skb = nlmsg_new(lfib_nlmsg_size(rt), GFP_KERNEL);
  1854. if (skb == NULL)
  1855. goto errout;
  1856. err = mpls_dump_route(skb, portid, seq, event, label, rt, nlm_flags);
  1857. if (err < 0) {
  1858. /* -EMSGSIZE implies BUG in lfib_nlmsg_size */
  1859. WARN_ON(err == -EMSGSIZE);
  1860. kfree_skb(skb);
  1861. goto errout;
  1862. }
  1863. rtnl_notify(skb, net, portid, RTNLGRP_MPLS_ROUTE, nlh, GFP_KERNEL);
  1864. return;
  1865. errout:
  1866. if (err < 0)
  1867. rtnl_set_sk_err(net, RTNLGRP_MPLS_ROUTE, err);
  1868. }
  1869. static int mpls_getroute(struct sk_buff *in_skb, struct nlmsghdr *in_nlh,
  1870. struct netlink_ext_ack *extack)
  1871. {
  1872. struct net *net = sock_net(in_skb->sk);
  1873. u32 portid = NETLINK_CB(in_skb).portid;
  1874. u32 in_label = LABEL_NOT_SPECIFIED;
  1875. struct nlattr *tb[RTA_MAX + 1];
  1876. u32 labels[MAX_NEW_LABELS];
  1877. struct mpls_shim_hdr *hdr;
  1878. unsigned int hdr_size = 0;
  1879. struct net_device *dev;
  1880. struct mpls_route *rt;
  1881. struct rtmsg *rtm, *r;
  1882. struct nlmsghdr *nlh;
  1883. struct sk_buff *skb;
  1884. struct mpls_nh *nh;
  1885. u8 n_labels;
  1886. int err;
  1887. err = nlmsg_parse(in_nlh, sizeof(*rtm), tb, RTA_MAX,
  1888. rtm_mpls_policy, extack);
  1889. if (err < 0)
  1890. goto errout;
  1891. rtm = nlmsg_data(in_nlh);
  1892. if (tb[RTA_DST]) {
  1893. u8 label_count;
  1894. if (nla_get_labels(tb[RTA_DST], 1, &label_count,
  1895. &in_label, extack)) {
  1896. err = -EINVAL;
  1897. goto errout;
  1898. }
  1899. if (!mpls_label_ok(net, &in_label, extack)) {
  1900. err = -EINVAL;
  1901. goto errout;
  1902. }
  1903. }
  1904. rt = mpls_route_input_rcu(net, in_label);
  1905. if (!rt) {
  1906. err = -ENETUNREACH;
  1907. goto errout;
  1908. }
  1909. if (rtm->rtm_flags & RTM_F_FIB_MATCH) {
  1910. skb = nlmsg_new(lfib_nlmsg_size(rt), GFP_KERNEL);
  1911. if (!skb) {
  1912. err = -ENOBUFS;
  1913. goto errout;
  1914. }
  1915. err = mpls_dump_route(skb, portid, in_nlh->nlmsg_seq,
  1916. RTM_NEWROUTE, in_label, rt, 0);
  1917. if (err < 0) {
  1918. /* -EMSGSIZE implies BUG in lfib_nlmsg_size */
  1919. WARN_ON(err == -EMSGSIZE);
  1920. goto errout_free;
  1921. }
  1922. return rtnl_unicast(skb, net, portid);
  1923. }
  1924. if (tb[RTA_NEWDST]) {
  1925. if (nla_get_labels(tb[RTA_NEWDST], MAX_NEW_LABELS, &n_labels,
  1926. labels, extack) != 0) {
  1927. err = -EINVAL;
  1928. goto errout;
  1929. }
  1930. hdr_size = n_labels * sizeof(struct mpls_shim_hdr);
  1931. }
  1932. skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
  1933. if (!skb) {
  1934. err = -ENOBUFS;
  1935. goto errout;
  1936. }
  1937. skb->protocol = htons(ETH_P_MPLS_UC);
  1938. if (hdr_size) {
  1939. bool bos;
  1940. int i;
  1941. if (skb_cow(skb, hdr_size)) {
  1942. err = -ENOBUFS;
  1943. goto errout_free;
  1944. }
  1945. skb_reserve(skb, hdr_size);
  1946. skb_push(skb, hdr_size);
  1947. skb_reset_network_header(skb);
  1948. /* Push new labels */
  1949. hdr = mpls_hdr(skb);
  1950. bos = true;
  1951. for (i = n_labels - 1; i >= 0; i--) {
  1952. hdr[i] = mpls_entry_encode(labels[i],
  1953. 1, 0, bos);
  1954. bos = false;
  1955. }
  1956. }
  1957. nh = mpls_select_multipath(rt, skb);
  1958. if (!nh) {
  1959. err = -ENETUNREACH;
  1960. goto errout_free;
  1961. }
  1962. if (hdr_size) {
  1963. skb_pull(skb, hdr_size);
  1964. skb_reset_network_header(skb);
  1965. }
  1966. nlh = nlmsg_put(skb, portid, in_nlh->nlmsg_seq,
  1967. RTM_NEWROUTE, sizeof(*r), 0);
  1968. if (!nlh) {
  1969. err = -EMSGSIZE;
  1970. goto errout_free;
  1971. }
  1972. r = nlmsg_data(nlh);
  1973. r->rtm_family = AF_MPLS;
  1974. r->rtm_dst_len = 20;
  1975. r->rtm_src_len = 0;
  1976. r->rtm_table = RT_TABLE_MAIN;
  1977. r->rtm_type = RTN_UNICAST;
  1978. r->rtm_scope = RT_SCOPE_UNIVERSE;
  1979. r->rtm_protocol = rt->rt_protocol;
  1980. r->rtm_flags = 0;
  1981. if (nla_put_labels(skb, RTA_DST, 1, &in_label))
  1982. goto nla_put_failure;
  1983. if (nh->nh_labels &&
  1984. nla_put_labels(skb, RTA_NEWDST, nh->nh_labels,
  1985. nh->nh_label))
  1986. goto nla_put_failure;
  1987. if (nh->nh_via_table != MPLS_NEIGH_TABLE_UNSPEC &&
  1988. nla_put_via(skb, nh->nh_via_table, mpls_nh_via(rt, nh),
  1989. nh->nh_via_alen))
  1990. goto nla_put_failure;
  1991. dev = rtnl_dereference(nh->nh_dev);
  1992. if (dev && nla_put_u32(skb, RTA_OIF, dev->ifindex))
  1993. goto nla_put_failure;
  1994. nlmsg_end(skb, nlh);
  1995. err = rtnl_unicast(skb, net, portid);
  1996. errout:
  1997. return err;
  1998. nla_put_failure:
  1999. nlmsg_cancel(skb, nlh);
  2000. err = -EMSGSIZE;
  2001. errout_free:
  2002. kfree_skb(skb);
  2003. return err;
  2004. }
  2005. static int resize_platform_label_table(struct net *net, size_t limit)
  2006. {
  2007. size_t size = sizeof(struct mpls_route *) * limit;
  2008. size_t old_limit;
  2009. size_t cp_size;
  2010. struct mpls_route __rcu **labels = NULL, **old;
  2011. struct mpls_route *rt0 = NULL, *rt2 = NULL;
  2012. unsigned index;
  2013. if (size) {
  2014. labels = kvzalloc(size, GFP_KERNEL);
  2015. if (!labels)
  2016. goto nolabels;
  2017. }
  2018. /* In case the predefined labels need to be populated */
  2019. if (limit > MPLS_LABEL_IPV4NULL) {
  2020. struct net_device *lo = net->loopback_dev;
  2021. rt0 = mpls_rt_alloc(1, lo->addr_len, 0);
  2022. if (IS_ERR(rt0))
  2023. goto nort0;
  2024. RCU_INIT_POINTER(rt0->rt_nh->nh_dev, lo);
  2025. rt0->rt_protocol = RTPROT_KERNEL;
  2026. rt0->rt_payload_type = MPT_IPV4;
  2027. rt0->rt_ttl_propagate = MPLS_TTL_PROP_DEFAULT;
  2028. rt0->rt_nh->nh_via_table = NEIGH_LINK_TABLE;
  2029. rt0->rt_nh->nh_via_alen = lo->addr_len;
  2030. memcpy(__mpls_nh_via(rt0, rt0->rt_nh), lo->dev_addr,
  2031. lo->addr_len);
  2032. }
  2033. if (limit > MPLS_LABEL_IPV6NULL) {
  2034. struct net_device *lo = net->loopback_dev;
  2035. rt2 = mpls_rt_alloc(1, lo->addr_len, 0);
  2036. if (IS_ERR(rt2))
  2037. goto nort2;
  2038. RCU_INIT_POINTER(rt2->rt_nh->nh_dev, lo);
  2039. rt2->rt_protocol = RTPROT_KERNEL;
  2040. rt2->rt_payload_type = MPT_IPV6;
  2041. rt2->rt_ttl_propagate = MPLS_TTL_PROP_DEFAULT;
  2042. rt2->rt_nh->nh_via_table = NEIGH_LINK_TABLE;
  2043. rt2->rt_nh->nh_via_alen = lo->addr_len;
  2044. memcpy(__mpls_nh_via(rt2, rt2->rt_nh), lo->dev_addr,
  2045. lo->addr_len);
  2046. }
  2047. rtnl_lock();
  2048. /* Remember the original table */
  2049. old = rtnl_dereference(net->mpls.platform_label);
  2050. old_limit = net->mpls.platform_labels;
  2051. /* Free any labels beyond the new table */
  2052. for (index = limit; index < old_limit; index++)
  2053. mpls_route_update(net, index, NULL, NULL);
  2054. /* Copy over the old labels */
  2055. cp_size = size;
  2056. if (old_limit < limit)
  2057. cp_size = old_limit * sizeof(struct mpls_route *);
  2058. memcpy(labels, old, cp_size);
  2059. /* If needed set the predefined labels */
  2060. if ((old_limit <= MPLS_LABEL_IPV6NULL) &&
  2061. (limit > MPLS_LABEL_IPV6NULL)) {
  2062. RCU_INIT_POINTER(labels[MPLS_LABEL_IPV6NULL], rt2);
  2063. rt2 = NULL;
  2064. }
  2065. if ((old_limit <= MPLS_LABEL_IPV4NULL) &&
  2066. (limit > MPLS_LABEL_IPV4NULL)) {
  2067. RCU_INIT_POINTER(labels[MPLS_LABEL_IPV4NULL], rt0);
  2068. rt0 = NULL;
  2069. }
  2070. /* Update the global pointers */
  2071. net->mpls.platform_labels = limit;
  2072. rcu_assign_pointer(net->mpls.platform_label, labels);
  2073. rtnl_unlock();
  2074. mpls_rt_free(rt2);
  2075. mpls_rt_free(rt0);
  2076. if (old) {
  2077. synchronize_rcu();
  2078. kvfree(old);
  2079. }
  2080. return 0;
  2081. nort2:
  2082. mpls_rt_free(rt0);
  2083. nort0:
  2084. kvfree(labels);
  2085. nolabels:
  2086. return -ENOMEM;
  2087. }
  2088. static int mpls_platform_labels(struct ctl_table *table, int write,
  2089. void __user *buffer, size_t *lenp, loff_t *ppos)
  2090. {
  2091. struct net *net = table->data;
  2092. int platform_labels = net->mpls.platform_labels;
  2093. int ret;
  2094. struct ctl_table tmp = {
  2095. .procname = table->procname,
  2096. .data = &platform_labels,
  2097. .maxlen = sizeof(int),
  2098. .mode = table->mode,
  2099. .extra1 = &zero,
  2100. .extra2 = &label_limit,
  2101. };
  2102. ret = proc_dointvec_minmax(&tmp, write, buffer, lenp, ppos);
  2103. if (write && ret == 0)
  2104. ret = resize_platform_label_table(net, platform_labels);
  2105. return ret;
  2106. }
  2107. #define MPLS_NS_SYSCTL_OFFSET(field) \
  2108. (&((struct net *)0)->field)
  2109. static const struct ctl_table mpls_table[] = {
  2110. {
  2111. .procname = "platform_labels",
  2112. .data = NULL,
  2113. .maxlen = sizeof(int),
  2114. .mode = 0644,
  2115. .proc_handler = mpls_platform_labels,
  2116. },
  2117. {
  2118. .procname = "ip_ttl_propagate",
  2119. .data = MPLS_NS_SYSCTL_OFFSET(mpls.ip_ttl_propagate),
  2120. .maxlen = sizeof(int),
  2121. .mode = 0644,
  2122. .proc_handler = proc_dointvec_minmax,
  2123. .extra1 = &zero,
  2124. .extra2 = &one,
  2125. },
  2126. {
  2127. .procname = "default_ttl",
  2128. .data = MPLS_NS_SYSCTL_OFFSET(mpls.default_ttl),
  2129. .maxlen = sizeof(int),
  2130. .mode = 0644,
  2131. .proc_handler = proc_dointvec_minmax,
  2132. .extra1 = &one,
  2133. .extra2 = &ttl_max,
  2134. },
  2135. { }
  2136. };
  2137. static int mpls_net_init(struct net *net)
  2138. {
  2139. struct ctl_table *table;
  2140. int i;
  2141. net->mpls.platform_labels = 0;
  2142. net->mpls.platform_label = NULL;
  2143. net->mpls.ip_ttl_propagate = 1;
  2144. net->mpls.default_ttl = 255;
  2145. table = kmemdup(mpls_table, sizeof(mpls_table), GFP_KERNEL);
  2146. if (table == NULL)
  2147. return -ENOMEM;
  2148. /* Table data contains only offsets relative to the base of
  2149. * the mdev at this point, so make them absolute.
  2150. */
  2151. for (i = 0; i < ARRAY_SIZE(mpls_table) - 1; i++)
  2152. table[i].data = (char *)net + (uintptr_t)table[i].data;
  2153. net->mpls.ctl = register_net_sysctl(net, "net/mpls", table);
  2154. if (net->mpls.ctl == NULL) {
  2155. kfree(table);
  2156. return -ENOMEM;
  2157. }
  2158. return 0;
  2159. }
  2160. static void mpls_net_exit(struct net *net)
  2161. {
  2162. struct mpls_route __rcu **platform_label;
  2163. size_t platform_labels;
  2164. struct ctl_table *table;
  2165. unsigned int index;
  2166. table = net->mpls.ctl->ctl_table_arg;
  2167. unregister_net_sysctl_table(net->mpls.ctl);
  2168. kfree(table);
  2169. /* An rcu grace period has passed since there was a device in
  2170. * the network namespace (and thus the last in flight packet)
  2171. * left this network namespace. This is because
  2172. * unregister_netdevice_many and netdev_run_todo has completed
  2173. * for each network device that was in this network namespace.
  2174. *
  2175. * As such no additional rcu synchronization is necessary when
  2176. * freeing the platform_label table.
  2177. */
  2178. rtnl_lock();
  2179. platform_label = rtnl_dereference(net->mpls.platform_label);
  2180. platform_labels = net->mpls.platform_labels;
  2181. for (index = 0; index < platform_labels; index++) {
  2182. struct mpls_route *rt = rtnl_dereference(platform_label[index]);
  2183. RCU_INIT_POINTER(platform_label[index], NULL);
  2184. mpls_notify_route(net, index, rt, NULL, NULL);
  2185. mpls_rt_free(rt);
  2186. }
  2187. rtnl_unlock();
  2188. kvfree(platform_label);
  2189. }
  2190. static struct pernet_operations mpls_net_ops = {
  2191. .init = mpls_net_init,
  2192. .exit = mpls_net_exit,
  2193. };
  2194. static struct rtnl_af_ops mpls_af_ops __read_mostly = {
  2195. .family = AF_MPLS,
  2196. .fill_stats_af = mpls_fill_stats_af,
  2197. .get_stats_af_size = mpls_get_stats_af_size,
  2198. };
  2199. static int __init mpls_init(void)
  2200. {
  2201. int err;
  2202. BUILD_BUG_ON(sizeof(struct mpls_shim_hdr) != 4);
  2203. err = register_pernet_subsys(&mpls_net_ops);
  2204. if (err)
  2205. goto out;
  2206. err = register_netdevice_notifier(&mpls_dev_notifier);
  2207. if (err)
  2208. goto out_unregister_pernet;
  2209. dev_add_pack(&mpls_packet_type);
  2210. rtnl_af_register(&mpls_af_ops);
  2211. rtnl_register_module(THIS_MODULE, PF_MPLS, RTM_NEWROUTE,
  2212. mpls_rtm_newroute, NULL, 0);
  2213. rtnl_register_module(THIS_MODULE, PF_MPLS, RTM_DELROUTE,
  2214. mpls_rtm_delroute, NULL, 0);
  2215. rtnl_register_module(THIS_MODULE, PF_MPLS, RTM_GETROUTE,
  2216. mpls_getroute, mpls_dump_routes, 0);
  2217. rtnl_register_module(THIS_MODULE, PF_MPLS, RTM_GETNETCONF,
  2218. mpls_netconf_get_devconf,
  2219. mpls_netconf_dump_devconf, 0);
  2220. err = ipgre_tunnel_encap_add_mpls_ops();
  2221. if (err)
  2222. pr_err("Can't add mpls over gre tunnel ops\n");
  2223. err = 0;
  2224. out:
  2225. return err;
  2226. out_unregister_pernet:
  2227. unregister_pernet_subsys(&mpls_net_ops);
  2228. goto out;
  2229. }
  2230. module_init(mpls_init);
  2231. static void __exit mpls_exit(void)
  2232. {
  2233. rtnl_unregister_all(PF_MPLS);
  2234. rtnl_af_unregister(&mpls_af_ops);
  2235. dev_remove_pack(&mpls_packet_type);
  2236. unregister_netdevice_notifier(&mpls_dev_notifier);
  2237. unregister_pernet_subsys(&mpls_net_ops);
  2238. ipgre_tunnel_encap_del_mpls_ops();
  2239. }
  2240. module_exit(mpls_exit);
  2241. MODULE_DESCRIPTION("MultiProtocol Label Switching");
  2242. MODULE_LICENSE("GPL v2");
  2243. MODULE_ALIAS_NETPROTO(PF_MPLS);